summaryrefslogtreecommitdiff
path: root/source3/libaddns/dnssock.c
blob: 88bd44e0e2b495d08487de7349b845c5a0d66609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
  Linux DNS client library implementation

  Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
  Copyright (C) 2006 Gerald Carter <jerry@samba.org>

     ** NOTE! The following LGPL license applies to the libaddns
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
  02110-1301  USA
*/

#include "dns.h"

/********************************************************************
********************************************************************/

static DNS_ERROR DNSTCPOpen( char *nameserver, HANDLE * phDNSServer )
{
	DNS_ERROR dwError = ERROR_DNS_INVALID_PARAMETER;
	int sockServer;
	unsigned long ulAddress;
	struct hostent *pHost;
	struct sockaddr_in s_in;
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;

	if ( (pDNSContext = TALLOC_P( NULL, DNS_CONNECTION_CONTEXT )) == NULL ) {
		return ERROR_DNS_NO_MEMORY;
	}

	if ( (ulAddress = inet_addr( nameserver )) == INADDR_NONE ) {
		if ( (pHost = gethostbyname( nameserver )) == NULL ) {
			dwError = ERROR_DNS_INVALID_NAME_SERVER;
			BAIL_ON_DNS_ERROR( dwError );
		}
		memcpy( &ulAddress, pHost->h_addr, pHost->h_length );
	}

	if ( (sockServer = socket( PF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET ) {
		dwError = ERROR_DNS_NO_MEMORY;
		BAIL_ON_DNS_ERROR( dwError );
	}

	s_in.sin_family = AF_INET;
	s_in.sin_addr.s_addr = ulAddress;
	s_in.sin_port = htons( DNS_TCP_PORT );

	if ( (connect( sockServer, (struct sockaddr*)&s_in, sizeof( s_in ))) == SOCKET_ERROR ) {
		dwError = ERROR_DNS_CONNECTION_FAILED;
		BAIL_ON_DNS_ERROR( dwError );
	}
		
	pDNSContext->s = sockServer;
	pDNSContext->hType = DNS_TCP;

	*phDNSServer = ( HANDLE ) pDNSContext;

	dwError = ERROR_DNS_SUCCESS;

	return dwError;

error:
	TALLOC_FREE( pDNSContext );
	*phDNSServer = ( HANDLE ) NULL;

	return dwError;
}

/********************************************************************
********************************************************************/

static DNS_ERROR DNSUDPOpen( char *nameserver, HANDLE * phDNSServer )
{
	DNS_ERROR dwError = ERROR_DNS_INVALID_PARAMETER;
	int SendSocket;
	unsigned long ulAddress;
	struct hostent *pHost;
	struct sockaddr_in RecvAddr;
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;

	if ( (pDNSContext = TALLOC_P( NULL, DNS_CONNECTION_CONTEXT )) == NULL ) {
		return ERROR_DNS_NO_MEMORY;
	}

	if ( (ulAddress = inet_addr( nameserver )) == INADDR_NONE ) {
		if ( (pHost = gethostbyname( nameserver )) == NULL ) {
			dwError = ERROR_DNS_INVALID_NAME_SERVER;
			BAIL_ON_DNS_ERROR( dwError );
		}
		memcpy( &ulAddress, pHost->h_addr, pHost->h_length );
	}
	
	/* Create a socket for sending data */

	SendSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );

	/* Set up the RecvAddr structure with the IP address of
	   the receiver (in this example case "123.456.789.1")
	   and the specified port number. */

	RecvAddr.sin_family = AF_INET;
	RecvAddr.sin_port = htons( DNS_UDP_PORT );
	RecvAddr.sin_addr.s_addr = ulAddress;

	pDNSContext->s = SendSocket;
	pDNSContext->hType = DNS_UDP;
	memcpy( &pDNSContext->RecvAddr, &RecvAddr, sizeof( struct sockaddr_in ) );

	*phDNSServer = ( HANDLE ) pDNSContext;

	dwError = ERROR_DNS_SUCCESS;

	return dwError;

error:
	TALLOC_FREE( pDNSContext );
	*phDNSServer = ( HANDLE ) NULL;

	return dwError;
}

/********************************************************************
********************************************************************/

DNS_ERROR DNSOpen( char *nameserver, int32 dwType, HANDLE * phDNSServer )
{
	switch ( dwType ) {
	case DNS_TCP:
		return DNSTCPOpen( nameserver, phDNSServer );
	case DNS_UDP:
		return DNSUDPOpen( nameserver, phDNSServer );
	}
	
	return ERROR_DNS_INVALID_PARAMETER;
}

/********************************************************************
********************************************************************/

static int32 DNSSendTCPRequest( HANDLE hDNSHandle,
		   uint8 * pDNSSendBuffer,
		   int32 dwBufferSize, int32 * pdwBytesSent )
{
	int32 dwError = 0;
	int32 dwBytesSent = 0;
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;


	pDNSContext = ( DNS_CONNECTION_CONTEXT * ) hDNSHandle;

	dwBytesSent = send( pDNSContext->s, pDNSSendBuffer, dwBufferSize, 0 );
	if ( dwBytesSent == SOCKET_ERROR ) {
		dwError = WSAGetLastError(  );
		BAIL_ON_ERROR( dwError );
	}

	*pdwBytesSent = dwBytesSent;

	return dwError;

      error:
	*pdwBytesSent = 0;
	return dwError;
}

/********************************************************************
********************************************************************/

static int32 DNSSendUDPRequest( HANDLE hDNSHandle,
		   uint8 * pDNSSendBuffer,
		   int32 dwBufferSize, int32 * pdwBytesSent )
{
	int32 dwError = 0;
	int32 dwBytesSent = 0;
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;

	pDNSContext = ( DNS_CONNECTION_CONTEXT * ) hDNSHandle;

	dwBytesSent = sendto( pDNSContext->s,
			      pDNSSendBuffer,
			      dwBufferSize,
			      0,
			      ( struct sockaddr * ) & pDNSContext->RecvAddr,
			      sizeof( pDNSContext->RecvAddr )
		 );
	if ( dwBytesSent == SOCKET_ERROR ) {
		dwError = WSAGetLastError(  );
		BAIL_ON_ERROR( dwError );
	} else {
		*pdwBytesSent = dwBytesSent;
	}

	return dwError;

      error:
	*pdwBytesSent = 0;
	return dwError;
}

/********************************************************************
********************************************************************/

static int32 DNSSelect( HANDLE hDNSHandle )
{
	int32 dwError = 0;
	fd_set rfds;
	struct timeval tv;
	int32 dwNumSockets = 0;
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;

	pDNSContext = ( DNS_CONNECTION_CONTEXT * ) hDNSHandle;
	FD_ZERO( &rfds );
	FD_SET( pDNSContext->s, &rfds );

	tv.tv_sec = 10;
	tv.tv_usec = 0;
	dwNumSockets = select( pDNSContext->s + 1, &rfds, NULL, NULL, &tv );
	if ( dwNumSockets == SOCKET_ERROR ) {
		dwError = WSAGetLastError(  );
		BAIL_ON_ERROR( dwError );
	}

	if ( !dwNumSockets ) {
#ifndef WIN32
		dwError = ETIMEDOUT;
#elif
		dwError = WSAETIMEDOUT;
#endif
	}

      error:

	return dwError;
}

/********************************************************************
********************************************************************/

static int32 DNSTCPReceiveBufferContext( HANDLE hDNSHandle,
			    HANDLE hDNSRecvBuffer, int32 * pdwBytesRead )
{
	int32 dwError = 0;
	int32 dwRead = 0;
	int16 wBytesToRead = 0;
	int16 wnBytesToRead = 0;
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;
	DNS_RECEIVEBUFFER_CONTEXT *pDNSRecvContext = NULL;

	pDNSContext = ( DNS_CONNECTION_CONTEXT * ) hDNSHandle;
	pDNSRecvContext = ( DNS_RECEIVEBUFFER_CONTEXT * ) hDNSRecvBuffer;

	dwError = DNSSelect( hDNSHandle );
	BAIL_ON_ERROR( dwError );

	dwRead = recv( pDNSContext->s, ( char * ) &wnBytesToRead,
		       sizeof( int16 ), 0 );
	if ( dwRead == SOCKET_ERROR ) {
		dwError = WSAGetLastError(  );
		BAIL_ON_ERROR( dwError );
	}

	wBytesToRead = ntohs( wnBytesToRead );

	dwError = DNSSelect( hDNSHandle );
	BAIL_ON_ERROR( dwError );

	dwRead = recv( pDNSContext->s,
		       ( char * ) pDNSRecvContext->pRecvBuffer, wBytesToRead,
		       0 );
	if ( dwRead == SOCKET_ERROR ) {
		dwError = WSAGetLastError(  );
		BAIL_ON_ERROR( dwError );
	}

	pDNSRecvContext->dwBytesRecvd = dwRead;

	*pdwBytesRead = ( int32 ) dwRead;

	return dwError;

      error:

	return dwError;
}

/********************************************************************
********************************************************************/

static int32 DNSUDPReceiveBufferContext( HANDLE hDNSHandle,
			    HANDLE hDNSRecvBuffer, int32 * pdwBytesRead )
{
	int32 dwError = 0;
	int32 dwRead = 0;
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;
	DNS_RECEIVEBUFFER_CONTEXT *pDNSRecvContext = NULL;

	pDNSContext = ( DNS_CONNECTION_CONTEXT * ) hDNSHandle;
	pDNSRecvContext = ( DNS_RECEIVEBUFFER_CONTEXT * ) hDNSRecvBuffer;

	dwError = DNSSelect( hDNSHandle );
	BAIL_ON_ERROR( dwError );

	dwRead = recv( pDNSContext->s,
		       ( char * ) pDNSRecvContext->pRecvBuffer, 512, 0 );
	if ( dwRead == SOCKET_ERROR ) {
		dwError = WSAGetLastError(  );
		BAIL_ON_ERROR( dwError );
	}

	pDNSRecvContext->dwBytesRecvd = dwRead;

	*pdwBytesRead = ( int32 ) dwRead;

      error:

	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSReceiveBufferContext( HANDLE hDNSHandle,
			 HANDLE hDNSRecvBuffer, int32 * pdwBytesRead )
{
	int32 dwError = 0;
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;

	pDNSContext = ( DNS_CONNECTION_CONTEXT * ) hDNSHandle;

	switch ( pDNSContext->hType ) {
	case DNS_TCP:
		dwError =
			DNSTCPReceiveBufferContext( hDNSHandle,
						    hDNSRecvBuffer,
						    pdwBytesRead );
		break;
	case DNS_UDP:
		dwError =
			DNSUDPReceiveBufferContext( hDNSHandle,
						    hDNSRecvBuffer,
						    pdwBytesRead );
		break;
	}
	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSCreateSendBuffer( HANDLE * phDNSSendBuffer )
{
	int32 dwError = 0;
	DNS_SENDBUFFER_CONTEXT *pDNSContext = NULL;
	uint8 *pSendBuffer = NULL;

	dwError = DNSAllocateMemory( sizeof( DNS_SENDBUFFER_CONTEXT ),
				     ( void * ) &pDNSContext );
	BAIL_ON_ERROR( dwError );

	dwError =
		DNSAllocateMemory( SENDBUFFER_SIZE,
				   ( void * ) &pSendBuffer );
	BAIL_ON_ERROR( dwError );

	pDNSContext->pSendBuffer = pSendBuffer;
	pDNSContext->dwBufferSize = SENDBUFFER_SIZE;

	/* We will offset into the buffer by 2 bytes
	   If we are doing a TCP write; we will fill in these
	   two bytes and send + 2 bytes
	   If we are doing a UDP write; we will start our send
	   +2 bytes and only send dwWritten; */

	pDNSContext->dwBufferOffset += 2;

	*phDNSSendBuffer = ( HANDLE ) pDNSContext;

	return dwError;

      error:

	if ( pSendBuffer ) {
		DNSFreeMemory( pSendBuffer );
	}
	if ( pDNSContext ) {
		DNSFreeMemory( pDNSContext );
	}
	*phDNSSendBuffer = ( HANDLE ) NULL;

	return dwError;
}


/********************************************************************
********************************************************************/

int32 DNSMarshallBuffer( HANDLE hDNSSendBuffer,
		   uint8 * pDNSSendBuffer,
		   int32 dwBufferSize, int32 * pdwBytesWritten )
{
	int32 dwError = 0;
	uint8 *pTemp = NULL;
	DNS_SENDBUFFER_CONTEXT *pDNSContext = NULL;

/* BugBug - we need to check for amount of space remaining in the
SendBuffer Context - if its insufficent, we want to realloc the 
Buffer and copy the context; Right now the assumption is we have a big
enough buffer */

	pDNSContext = ( DNS_SENDBUFFER_CONTEXT * ) hDNSSendBuffer;

	pTemp = pDNSContext->pSendBuffer + pDNSContext->dwBufferOffset;

	memcpy( pTemp, pDNSSendBuffer, dwBufferSize );

	pDNSContext->dwBytesWritten += dwBufferSize;
	pDNSContext->dwBufferOffset += dwBufferSize;

	*pdwBytesWritten = dwBufferSize;

	return dwError;
}

/********************************************************************
********************************************************************/

static int32 DNSTCPSendBufferContext( HANDLE hDNSServer,
			 HANDLE hSendBuffer, int32 * pdwBytesSent )
{
	DNS_SENDBUFFER_CONTEXT *pSendBufferContext = NULL;
	int32 dwError = 0;
	int16 wBytesWritten = 0;
	int16 wnBytesWritten = 0;

	pSendBufferContext = ( DNS_SENDBUFFER_CONTEXT * ) hSendBuffer;

	wBytesWritten = ( int16 ) pSendBufferContext->dwBytesWritten;
	wnBytesWritten = htons( wBytesWritten );

	memcpy( pSendBufferContext->pSendBuffer, &wnBytesWritten,
		sizeof( int16 ) );

	dwError = DNSSendTCPRequest( hDNSServer,
				     pSendBufferContext->pSendBuffer,
				     pSendBufferContext->dwBytesWritten + 2,
				     pdwBytesSent );
	BAIL_ON_ERROR( dwError );

      error:

	return dwError;
}

/********************************************************************
********************************************************************/

static int32 DNSUDPSendBufferContext( HANDLE hDNSServer,
			 HANDLE hSendBuffer, int32 * pdwBytesSent )
{
	DNS_SENDBUFFER_CONTEXT *pSendBufferContext = NULL;
	int32 dwError = 0;

	pSendBufferContext = ( DNS_SENDBUFFER_CONTEXT * ) hSendBuffer;

	/* Now remember to send 2 bytes ahead of pSendBuffer; because
	   we ignore the 2 bytes size field. */

	dwError = DNSSendUDPRequest( hDNSServer,
				     pSendBufferContext->pSendBuffer + 2,
				     pSendBufferContext->dwBytesWritten,
				     pdwBytesSent );
	BAIL_ON_ERROR( dwError );

      error:

	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSSendBufferContext( HANDLE hDNSServer,
		      HANDLE hSendBuffer, int32 * pdwBytesSent )
{
	DNS_CONNECTION_CONTEXT *pDNSContext = NULL;
	int32 dwError = 0;

	pDNSContext = ( DNS_CONNECTION_CONTEXT * ) hDNSServer;

	switch ( pDNSContext->hType ) {
	case DNS_TCP:
		dwError = DNSTCPSendBufferContext( hDNSServer,
						   hSendBuffer,
						   pdwBytesSent );
		BAIL_ON_ERROR( dwError );
		break;

	case DNS_UDP:
		dwError = DNSUDPSendBufferContext( hDNSServer,
						   hSendBuffer,
						   pdwBytesSent );
		BAIL_ON_ERROR( dwError );
		break;
	}
      error:

	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSDumpSendBufferContext( HANDLE hSendBuffer )
{
	DNS_SENDBUFFER_CONTEXT *pSendBufferContext = NULL;
	int32 dwError = 0;
	int32 dwCurLine = 0;
	int32 i = 0;

	pSendBufferContext = ( DNS_SENDBUFFER_CONTEXT * ) hSendBuffer;
	printf( "\n" );
	printf( "Buffer Size is: %d\n", pSendBufferContext->dwBytesWritten );
	while ( i < pSendBufferContext->dwBytesWritten ) {
		if ( ( i / 16 ) > dwCurLine ) {
			printf( "\n" );
			dwCurLine++;
		}
		if ( ( i % 8 ) == 0 ) {
			printf( "  " );
		}
		printf( "%.2x ", pSendBufferContext->pSendBuffer[i] );
		i++;
	}
	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSDumpRecvBufferContext( HANDLE hRecvBuffer )
{
	DNS_RECEIVEBUFFER_CONTEXT *pRecvBufferContext = NULL;
	int32 dwError = 0;
	int32 dwCurLine = 0;
	int32 i = 0;

	pRecvBufferContext = ( DNS_RECEIVEBUFFER_CONTEXT * ) hRecvBuffer;

	printf( "\n" );
	printf( "Buffer Size is: %d\n", pRecvBufferContext->dwBytesRecvd );

	while ( i < pRecvBufferContext->dwBytesRecvd ) {
		if ( ( i / 16 ) > dwCurLine ) {
			printf( "\n" );
			dwCurLine++;
		}
		if ( ( i % 8 ) == 0 ) {
			printf( "  " );
		}
		printf( "%.2x ", pRecvBufferContext->pRecvBuffer[i] );
		i++;
	}
	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSCreateReceiveBuffer( HANDLE * phDNSRecvBuffer )
{
	int32 dwError = 0;
	DNS_RECEIVEBUFFER_CONTEXT *pDNSContext = NULL;
	uint8 *pRecvBuffer = NULL;

	dwError = DNSAllocateMemory( sizeof( DNS_RECEIVEBUFFER_CONTEXT ),
				     ( void * ) &pDNSContext );
	BAIL_ON_ERROR( dwError );

	dwError =
		DNSAllocateMemory( RECVBUFFER_SIZE,
				   ( void * ) &pRecvBuffer );
	BAIL_ON_ERROR( dwError );

	pDNSContext->pRecvBuffer = pRecvBuffer;
	pDNSContext->dwBufferSize = RECVBUFFER_SIZE;

	*phDNSRecvBuffer = ( HANDLE ) pDNSContext;

	return dwError;

      error:

	if ( pRecvBuffer ) {
		DNSFreeMemory( pRecvBuffer );
	}
	if ( pDNSContext ) {
		DNSFreeMemory( pDNSContext );
	}
	*phDNSRecvBuffer = ( HANDLE ) NULL;

	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSUnmarshallBuffer( HANDLE hDNSRecvBuffer,
		     uint8 * pDNSRecvBuffer,
		     int32 dwBufferSize, int32 * pdwBytesRead )
{
	int32 dwError = 0;
	uint8 *pTemp = NULL;
	DNS_RECEIVEBUFFER_CONTEXT *pDNSContext = NULL;

/* BugBug - we need to check for amount of space remaining in the
SendBuffer Context - if its insufficent, we want to realloc the 
Buffer and copy the context; Right now the assumption is we have a big
enough buffer */

	pDNSContext = ( DNS_RECEIVEBUFFER_CONTEXT * ) hDNSRecvBuffer;

	pTemp = pDNSContext->pRecvBuffer + pDNSContext->dwBytesRead;

	memcpy( pDNSRecvBuffer, pTemp, dwBufferSize );

	pDNSContext->dwBytesRead += dwBufferSize;

	*pdwBytesRead = dwBufferSize;

	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSUnmarshallDomainNameAtOffset( HANDLE hRecvBuffer,
				 int16 wOffset,
				 DNS_DOMAIN_NAME ** ppDomainName )
{
	int32 dwError = 0;
	DNS_DOMAIN_LABEL *pLabel = NULL;
	DNS_DOMAIN_LABEL *pLabelList = NULL;
	DNS_DOMAIN_NAME *pDomainName = NULL;
	char *pszLabel = NULL;
	char szLabel[65];
	uint8 uLen = 0;
	int32 dwCurrent = 0;
	DNS_RECEIVEBUFFER_CONTEXT *pRecvContext = NULL;

	pRecvContext = ( DNS_RECEIVEBUFFER_CONTEXT * ) hRecvBuffer;
	dwCurrent = wOffset;

	while ( 1 ) {

		memcpy( &uLen, pRecvContext->pRecvBuffer + dwCurrent,
			sizeof( char ) );
		dwCurrent++;

		if ( uLen == 0 ) {
			break;
		}

		memset( szLabel, 0, 65 );
		memcpy( szLabel, pRecvContext->pRecvBuffer + dwCurrent,
			uLen );
		dwCurrent += uLen;

		dwError = DNSAllocateString( szLabel, &pszLabel );
		BAIL_ON_ERROR( dwError );

		dwError =
			DNSAllocateMemory( sizeof( DNS_DOMAIN_LABEL ),
					   ( void * ) &pLabel );
		BAIL_ON_ERROR( dwError );

		pLabel->pszLabel = pszLabel;
		dwError = DNSAppendLabel( pLabelList, pLabel, &pLabelList );
		BAIL_ON_ERROR( dwError );
	}

	dwError =
		DNSAllocateMemory( sizeof( DNS_DOMAIN_NAME ),
				   ( void * ) &pDomainName );
	BAIL_ON_ERROR( dwError );
	pDomainName->pLabelList = pLabelList;

	*ppDomainName = pDomainName;

	return dwError;

      error:

	*ppDomainName = NULL;
	return dwError;
}

/********************************************************************
********************************************************************/

int32 DNSReceiveBufferMoveBackIndex( HANDLE hRecvBuffer, int16 wOffset )
{
	int32 dwError = 0;
	DNS_RECEIVEBUFFER_CONTEXT *pDNSContext = NULL;

	pDNSContext = ( DNS_RECEIVEBUFFER_CONTEXT * ) hRecvBuffer;
	pDNSContext->dwBytesRead -= wOffset;

	return dwError;
}

/********************************************************************
********************************************************************/

void DNSFreeSendBufferContext( HANDLE hSendBuffer )
{
	DNS_SENDBUFFER_CONTEXT *pSendBufferContext = NULL;

	pSendBufferContext = ( DNS_SENDBUFFER_CONTEXT * ) hSendBuffer;

	if ( pSendBufferContext->pSendBuffer ) {
		DNSFreeMemory( pSendBufferContext->pSendBuffer );
	}
	if ( pSendBufferContext ) {
		DNSFreeMemory( pSendBufferContext );
	}
}

/********************************************************************
********************************************************************/

int32 DNSGetSendBufferContextSize( HANDLE hSendBuffer )
{
	DNS_SENDBUFFER_CONTEXT *pSendBufferContext = NULL;

	pSendBufferContext = ( DNS_SENDBUFFER_CONTEXT * ) hSendBuffer;

	return ( pSendBufferContext->dwBytesWritten );

}

/********************************************************************
********************************************************************/

uint8 *DNSGetSendBufferContextBuffer( HANDLE hSendBuffer )
{
	DNS_SENDBUFFER_CONTEXT *pSendBufferContext = NULL;

	pSendBufferContext = ( DNS_SENDBUFFER_CONTEXT * ) hSendBuffer;

	return ( pSendBufferContext->pSendBuffer );
}