2 Linux DNS client library implementation
4 Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
5 Copyright (C) 2006 Gerald Carter <jerry@samba.org>
7 ** NOTE! The following LGPL license applies to the libaddns
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 /*********************************************************************
30 *********************************************************************/
32 static int32
DNSUpdateAllocateResponse( DNS_UPDATE_RESPONSE
** ppDNSResponse
)
35 DNS_UPDATE_RESPONSE
*pDNSResponse
= NULL
;
38 DNSAllocateMemory( sizeof( DNS_UPDATE_RESPONSE
),
39 ( void * ) &pDNSResponse
);
40 BAIL_ON_ERROR( dwError
);
42 *ppDNSResponse
= pDNSResponse
;
48 *ppDNSResponse
= NULL
;
53 /*********************************************************************
54 *********************************************************************/
56 static int32
DNSUpdateUnmarshallAdditionalSection( HANDLE hReceiveBuffer
,
59 **pppDNSAdditionalsRRRecords
)
63 DNS_RR_RECORD
*pDNSRRRecord
= NULL
;
64 DNS_RR_RECORD
**ppDNSAdditionalRRRecords
= NULL
;
65 DNS_RR_HEADER RRHeader
= { 0 };
66 DNS_RR_HEADER
*pRRHeader
= &RRHeader
;
67 uint8
*pRRData
= NULL
;
70 dwError
= DNSAllocateMemory( wAdditionals
* sizeof( DNS_RR_RECORD
* ),
71 ( void * ) &ppDNSAdditionalRRRecords
);
72 BAIL_ON_ERROR( dwError
);
74 for ( i
= 0; i
< wAdditionals
; i
++ ) {
76 memset( pRRHeader
, 0, sizeof( DNS_RR_HEADER
) );
77 dwError
= DNSUnmarshallRRHeader( hReceiveBuffer
, pRRHeader
);
78 BAIL_ON_ERROR( dwError
);
81 DNSUnmarshallRData( hReceiveBuffer
,
82 pRRHeader
->wRDataSize
, &pRRData
,
84 BAIL_ON_ERROR( dwError
);
87 DNSAllocateMemory( sizeof( DNS_RR_RECORD
),
88 ( void * ) &pDNSRRRecord
);
89 BAIL_ON_ERROR( dwError
);
91 memcpy( &pDNSRRRecord
->RRHeader
, pRRHeader
,
92 sizeof( DNS_RR_HEADER
) );
93 pDNSRRRecord
->pRData
= pRRData
;
95 *( ppDNSAdditionalRRRecords
+ i
) = pDNSRRRecord
;
103 /*********************************************************************
104 *********************************************************************/
106 static int32
DNSUpdateUnmarshallPRSection( HANDLE hReceiveBuffer
,
108 DNS_RR_RECORD
* **pppDNSPRRRRecords
)
112 DNS_RR_RECORD
*pDNSRRRecord
= NULL
;
113 DNS_RR_RECORD
**ppDNSPRRRRecords
= NULL
;
114 DNS_RR_HEADER RRHeader
= { 0 };
115 DNS_RR_HEADER
*pRRHeader
= &RRHeader
;
116 uint8
*pRRData
= NULL
;
119 dwError
= DNSAllocateMemory( wPRs
* sizeof( DNS_RR_RECORD
* ),
120 ( void * ) &ppDNSPRRRRecords
);
121 BAIL_ON_ERROR( dwError
);
123 for ( i
= 0; i
< wPRs
; i
++ ) {
125 memset( pRRHeader
, 0, sizeof( DNS_RR_HEADER
) );
126 dwError
= DNSUnmarshallRRHeader( hReceiveBuffer
, pRRHeader
);
127 BAIL_ON_ERROR( dwError
);
130 DNSUnmarshallRData( hReceiveBuffer
,
131 pRRHeader
->wRDataSize
, &pRRData
,
133 BAIL_ON_ERROR( dwError
);
136 DNSAllocateMemory( sizeof( DNS_RR_RECORD
),
137 ( void * ) &pDNSRRRecord
);
138 BAIL_ON_ERROR( dwError
);
140 memcpy( &pDNSRRRecord
->RRHeader
, pRRHeader
,
141 sizeof( DNS_RR_HEADER
) );
142 pDNSRRRecord
->pRData
= pRRData
;
144 *( ppDNSPRRRRecords
+ i
) = pDNSRRRecord
;
147 *pppDNSPRRRRecords
= ppDNSPRRRRecords
;
157 /*********************************************************************
158 *********************************************************************/
160 static int32
DNSUpdateUnmarshallUpdateSection( HANDLE hReceiveBuffer
,
162 DNS_RR_RECORD
* **pppDNSUpdateRRRecords
)
166 DNS_RR_RECORD
*pDNSRRRecord
= NULL
;
167 DNS_RR_RECORD
**ppDNSUpdateRRRecords
= NULL
;
168 DNS_RR_HEADER RRHeader
= { 0 };
169 DNS_RR_HEADER
*pRRHeader
= &RRHeader
;
170 uint8
*pRRData
= NULL
;
173 dwError
= DNSAllocateMemory( wUpdates
* sizeof( DNS_RR_RECORD
* ),
174 ( void * ) &ppDNSUpdateRRRecords
);
175 BAIL_ON_ERROR( dwError
);
177 for ( i
= 0; i
< wUpdates
; i
++ ) {
179 memset( pRRHeader
, 0, sizeof( DNS_RR_HEADER
) );
180 dwError
= DNSUnmarshallRRHeader( hReceiveBuffer
, pRRHeader
);
181 BAIL_ON_ERROR( dwError
);
184 DNSUnmarshallRData( hReceiveBuffer
,
185 pRRHeader
->wRDataSize
, &pRRData
,
187 BAIL_ON_ERROR( dwError
);
190 DNSAllocateMemory( sizeof( DNS_RR_RECORD
),
191 ( void * ) &pDNSRRRecord
);
192 BAIL_ON_ERROR( dwError
);
194 memcpy( &pDNSRRRecord
->RRHeader
, pRRHeader
,
195 sizeof( DNS_RR_HEADER
) );
196 pDNSRRRecord
->pRData
= pRRData
;
198 *( ppDNSUpdateRRRecords
+ i
) = pDNSRRRecord
;
201 *pppDNSUpdateRRRecords
= ppDNSUpdateRRRecords
;
211 /*********************************************************************
212 *********************************************************************/
214 static int32
DNSUpdateUnmarshallZoneSection( HANDLE hReceiveBuffer
,
216 DNS_ZONE_RECORD
* **pppDNSZoneRecords
)
221 DNS_ZONE_RECORD
*pDNSZoneRecord
= NULL
;
222 DNS_ZONE_RECORD
**ppDNSZoneRecords
= NULL
;
223 int16 wnZoneClass
= 0;
224 int16 wnZoneType
= 0;
227 dwError
= DNSAllocateMemory( wZones
* sizeof( DNS_ZONE_RECORD
* ),
228 ( void * ) &ppDNSZoneRecords
);
229 BAIL_ON_ERROR( dwError
);
231 for ( i
= 0; i
< wZones
; i
++ ) {
234 DNSAllocateMemory( sizeof( DNS_ZONE_RECORD
),
235 ( void * ) &pDNSZoneRecord
);
236 BAIL_ON_ERROR( dwError
);
239 DNSUnmarshallDomainName( hReceiveBuffer
,
242 BAIL_ON_ERROR( dwError
);
245 DNSUnmarshallBuffer( hReceiveBuffer
,
246 ( uint8
* ) & wnZoneType
,
247 ( int32
) sizeof( int16
),
249 BAIL_ON_ERROR( dwError
);
250 pDNSZoneRecord
->wZoneType
= ntohs( wnZoneType
);
253 DNSUnmarshallBuffer( hReceiveBuffer
,
254 ( uint8
* ) & wnZoneClass
,
255 ( int32
) sizeof( int16
),
257 BAIL_ON_ERROR( dwError
);
258 pDNSZoneRecord
->wZoneClass
= ntohs( wnZoneClass
);
260 *( ppDNSZoneRecords
+ i
) = pDNSZoneRecord
;
263 *pppDNSZoneRecords
= ppDNSZoneRecords
;
272 /*********************************************************************
273 *********************************************************************/
275 int32
DNSUpdateReceiveUpdateResponse( HANDLE hDNSHandle
,
276 DNS_UPDATE_RESPONSE
** ppDNSResponse
)
278 DNS_UPDATE_RESPONSE
*pDNSResponse
= NULL
;
280 int16 wnIdentification
, wIdentification
= 0;
281 int16 wnParameter
, wParameter
= 0;
282 int16 wnZones
, wZones
= 0;
283 int16 wnPRs
, wPRs
= 0;
284 int16 wnAdditionals
, wAdditionals
= 0;
285 int16 wnUpdates
, wUpdates
= 0;
287 DNS_RR_RECORD
**ppDNSPRRecords
= NULL
;
288 DNS_RR_RECORD
**ppDNSAdditionalRecords
= NULL
;
289 DNS_RR_RECORD
**ppDNSUpdateRecords
= NULL
;
290 DNS_ZONE_RECORD
**ppDNSZoneRecords
= NULL
;
291 HANDLE hRecvBuffer
= ( HANDLE
) NULL
;
293 dwError
= DNSCreateReceiveBuffer( &hRecvBuffer
);
294 BAIL_ON_ERROR( dwError
);
296 dwError
= DNSReceiveBufferContext( hDNSHandle
, hRecvBuffer
, &dwRead
);
297 BAIL_ON_ERROR( dwError
);
300 dwError
= DNSDumpRecvBufferContext( hRecvBuffer
);
301 BAIL_ON_ERROR( dwError
);
305 DNSUnmarshallBuffer( hRecvBuffer
,
306 ( uint8
* ) & wnIdentification
,
307 sizeof( int16
), &dwRead
);
308 BAIL_ON_ERROR( dwError
);
309 wIdentification
= ntohs( wnIdentification
);
312 DNSUnmarshallBuffer( hRecvBuffer
, ( uint8
* ) & wnParameter
,
313 sizeof( int16
), &dwRead
);
314 BAIL_ON_ERROR( dwError
);
315 wParameter
= ntohs( wnParameter
);
319 DNSUnmarshallBuffer( hRecvBuffer
, ( uint8
* ) & wnZones
,
320 sizeof( int16
), &dwRead
);
321 BAIL_ON_ERROR( dwError
);
322 wZones
= ntohs( wnZones
);
326 DNSUnmarshallBuffer( hRecvBuffer
, ( uint8
* ) & wnPRs
,
327 sizeof( int16
), &dwRead
);
328 BAIL_ON_ERROR( dwError
);
329 wPRs
= ntohs( wnPRs
);
333 DNSUnmarshallBuffer( hRecvBuffer
, ( uint8
* ) & wnUpdates
,
334 sizeof( int16
), &dwRead
);
335 BAIL_ON_ERROR( dwError
);
336 wUpdates
= ntohs( wnUpdates
);
339 DNSUnmarshallBuffer( hRecvBuffer
, ( uint8
* ) & wnAdditionals
,
340 sizeof( int16
), &dwRead
);
341 BAIL_ON_ERROR( dwError
);
342 wAdditionals
= ntohs( wnAdditionals
);
347 DNSUpdateUnmarshallZoneSection( hRecvBuffer
, wZones
,
349 BAIL_ON_ERROR( dwError
);
354 DNSUpdateUnmarshallPRSection( hRecvBuffer
, wPRs
,
356 BAIL_ON_ERROR( dwError
);
361 DNSUpdateUnmarshallUpdateSection( hRecvBuffer
,
363 &ppDNSUpdateRecords
);
364 BAIL_ON_ERROR( dwError
);
367 if ( wAdditionals
) {
369 DNSUpdateUnmarshallAdditionalSection( hRecvBuffer
,
371 &ppDNSAdditionalRecords
);
372 BAIL_ON_ERROR( dwError
);
375 dwError
= DNSUpdateAllocateResponse( &pDNSResponse
);
376 BAIL_ON_ERROR( dwError
);
378 pDNSResponse
->wIdentification
= wIdentification
;
379 pDNSResponse
->wParameter
= wParameter
;
380 pDNSResponse
->wZones
= wZones
;
381 pDNSResponse
->wPRs
= wPRs
;
382 pDNSResponse
->wUpdates
= wUpdates
;
383 pDNSResponse
->wAdditionals
= wAdditionals
;
385 pDNSResponse
->ppZoneRRSet
= ppDNSZoneRecords
;
386 pDNSResponse
->ppPRRRSet
= ppDNSPRRecords
;
387 pDNSResponse
->ppUpdateRRSet
= ppDNSUpdateRecords
;
388 pDNSResponse
->ppAdditionalRRSet
= ppDNSAdditionalRecords
;
390 *ppDNSResponse
= pDNSResponse
;