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
DNSUpdateMarshallZoneSection( HANDLE hSendBuffer
,
33 DNS_ZONE_RECORD
** ppDNSZoneRecords
,
39 DNS_ZONE_RECORD
*pDNSZoneRecord
= NULL
;
41 int16 wnZoneClass
= 0;
43 for ( i
= 0; i
< wZones
; i
++ ) {
45 pDNSZoneRecord
= *( ppDNSZoneRecords
+ i
);
47 DNSMarshallDomainName( hSendBuffer
,
48 pDNSZoneRecord
->pDomainName
);
49 BAIL_ON_ERROR( dwError
);
51 wnZoneType
= htons( pDNSZoneRecord
->wZoneType
);
53 DNSMarshallBuffer( hSendBuffer
,
54 ( uint8
* ) & wnZoneType
,
55 ( int32
) sizeof( int16
),
57 BAIL_ON_ERROR( dwError
);
59 wnZoneClass
= htons( pDNSZoneRecord
->wZoneClass
);
61 DNSMarshallBuffer( hSendBuffer
,
62 ( uint8
* ) & wnZoneClass
,
63 ( int32
) sizeof( int16
),
65 BAIL_ON_ERROR( dwError
);
75 /*********************************************************************
76 *********************************************************************/
78 static int32
DNSUpdateMarshallPRSection( HANDLE hSendBuffer
,
79 DNS_RR_RECORD
** ppDNSPRRRRecords
, int16 wPRs
)
83 DNS_RR_RECORD
*pDNSPRRRRecord
= NULL
;
86 for ( i
= 0; i
< wPRs
; i
++ ) {
88 pDNSPRRRRecord
= *( ppDNSPRRRRecords
+ i
);
90 dwError
= DNSMarshallRRHeader( hSendBuffer
, pDNSPRRRRecord
);
91 BAIL_ON_ERROR( dwError
);
93 if ( pDNSPRRRRecord
->RRHeader
.wRDataSize
) {
95 DNSMarshallRData( hSendBuffer
,
97 BAIL_ON_ERROR( dwError
);
105 /*********************************************************************
106 *********************************************************************/
108 static int32
DNSUpdateMarshallUpdateSection( HANDLE hSendBuffer
,
109 DNS_RR_RECORD
** ppDNSUpdateRRRecords
,
114 DNS_RR_RECORD
*pDNSUpdateRRRecord
= NULL
;
116 for ( i
= 0; i
< wZones
; i
++ ) {
118 pDNSUpdateRRRecord
= *( ppDNSUpdateRRRecords
+ i
);
121 DNSMarshallRRHeader( hSendBuffer
,
122 pDNSUpdateRRRecord
);
124 if ( pDNSUpdateRRRecord
->RRHeader
.wRDataSize
) {
125 dwError
= DNSMarshallRData( hSendBuffer
,
126 pDNSUpdateRRRecord
);
127 BAIL_ON_ERROR( dwError
);
136 /*********************************************************************
137 *********************************************************************/
139 static int32
DNSUpdateMarshallAdditionalSection( HANDLE hSendBuffer
,
141 ppDNSAdditionalsRRRecords
,
146 DNS_RR_RECORD
*pDNSAdditionalRRRecord
= NULL
;
148 for ( i
= 0; i
< wAdditionals
; i
++ ) {
150 pDNSAdditionalRRRecord
= *( ppDNSAdditionalsRRRecords
+ i
);
153 DNSMarshallRRHeader( hSendBuffer
,
154 pDNSAdditionalRRRecord
);
155 BAIL_ON_ERROR( dwError
);
157 if ( pDNSAdditionalRRRecord
->RRHeader
.wRDataSize
) {
158 dwError
= DNSMarshallRData( hSendBuffer
,
159 pDNSAdditionalRRRecord
);
160 BAIL_ON_ERROR( dwError
);
169 /*********************************************************************
170 *********************************************************************/
172 int32
DNSUpdateSendUpdateRequest2( HANDLE hDNSServer
,
173 DNS_UPDATE_REQUEST
* pDNSRequest
)
176 int32 dwBytesSent
= 0;
177 HANDLE hSendBuffer
= ( HANDLE
) NULL
;
179 dwError
= DNSUpdateBuildRequestMessage( pDNSRequest
, &hSendBuffer
);
180 BAIL_ON_ERROR( dwError
);
183 DNSSendBufferContext( hDNSServer
, hSendBuffer
, &dwBytesSent
);
184 BAIL_ON_ERROR( dwError
);
189 DNSFreeSendBufferContext( hSendBuffer
);
195 /*********************************************************************
196 *********************************************************************/
198 int32
DNSUpdateBuildRequestMessage( DNS_UPDATE_REQUEST
* pDNSRequest
,
199 HANDLE
* phSendBuffer
)
202 char DNSMessageHeader
[12];
203 int16 wnIdentification
= 0;
204 int16 wnParameter
= 0;
208 int16 wnAdditionals
= 0;
210 HANDLE hSendBuffer
= ( HANDLE
) NULL
;
212 dwError
= DNSCreateSendBuffer( &hSendBuffer
);
213 BAIL_ON_ERROR( dwError
);
215 wnIdentification
= htons( pDNSRequest
->wIdentification
);
216 memcpy( DNSMessageHeader
, ( char * ) &wnIdentification
, 2 );
218 wnParameter
= htons( pDNSRequest
->wParameter
);
219 memcpy( DNSMessageHeader
+ 2, ( char * ) &wnParameter
, 2 );
221 wnZones
= htons( pDNSRequest
->wZones
);
222 memcpy( DNSMessageHeader
+ 4, ( char * ) &wnZones
, 2 );
224 wnPRs
= htons( pDNSRequest
->wPRs
);
225 memcpy( DNSMessageHeader
+ 6, ( char * ) &wnPRs
, 2 );
227 wnUpdates
= htons( pDNSRequest
->wUpdates
);
228 memcpy( DNSMessageHeader
+ 8, ( char * ) &wnUpdates
, 2 );
230 wnAdditionals
= htons( pDNSRequest
->wAdditionals
);
231 memcpy( DNSMessageHeader
+ 10, ( char * ) &wnAdditionals
, 2 );
234 DNSMarshallBuffer( hSendBuffer
, ( uint8
* ) DNSMessageHeader
,
236 BAIL_ON_ERROR( dwError
);
238 if ( pDNSRequest
->wZones
) {
240 DNSUpdateMarshallZoneSection( hSendBuffer
,
243 pDNSRequest
->wZones
);
244 BAIL_ON_ERROR( dwError
);
247 if ( pDNSRequest
->wPRs
) {
249 DNSUpdateMarshallPRSection( hSendBuffer
,
250 pDNSRequest
->ppPRRRSet
,
252 BAIL_ON_ERROR( dwError
);
255 if ( pDNSRequest
->wUpdates
) {
257 DNSUpdateMarshallUpdateSection( hSendBuffer
,
262 BAIL_ON_ERROR( dwError
);
265 if ( pDNSRequest
->wAdditionals
) {
267 DNSUpdateMarshallAdditionalSection( hSendBuffer
,
272 BAIL_ON_ERROR( dwError
);
275 DNSDumpSendBufferContext( hSendBuffer
);
278 *phSendBuffer
= hSendBuffer
;
285 DNSFreeSendBufferContext( hSendBuffer
);
288 *phSendBuffer
= ( HANDLE
) NULL
;
293 /*********************************************************************
294 *********************************************************************/
296 void DNSUpdateFreeRequest( DNS_UPDATE_REQUEST
* pDNSRequest
)
301 /*********************************************************************
302 *********************************************************************/
304 int32
DNSUpdateAddZoneSection( DNS_UPDATE_REQUEST
* pDNSRequest
,
305 DNS_ZONE_RECORD
* pDNSZone
)
307 int32 dwNumZones
= 0;
310 dwNumZones
= pDNSRequest
->wZones
;
312 dwError
= DNSReallocMemory( ( uint8
* ) pDNSRequest
->ppZoneRRSet
,
313 ( void * ) &pDNSRequest
->ppZoneRRSet
,
315 1 ) * sizeof( DNS_ZONE_RECORD
* )
317 BAIL_ON_ERROR( dwError
);
319 *( pDNSRequest
->ppZoneRRSet
+ dwNumZones
) = pDNSZone
;
321 pDNSRequest
->wZones
+= 1;
328 /*********************************************************************
329 *********************************************************************/
331 int32
DNSUpdateAddAdditionalSection( DNS_UPDATE_REQUEST
* pDNSRequest
,
332 DNS_RR_RECORD
* pDNSRecord
)
334 int32 dwNumAdditionals
= 0;
337 dwNumAdditionals
= pDNSRequest
->wAdditionals
;
338 dwError
= DNSReallocMemory( pDNSRequest
->ppAdditionalRRSet
,
339 ( void * ) &pDNSRequest
->
342 1 ) * sizeof( DNS_RR_RECORD
* ) );
343 BAIL_ON_ERROR( dwError
);
345 *( pDNSRequest
->ppAdditionalRRSet
+ dwNumAdditionals
) = pDNSRecord
;
347 pDNSRequest
->wAdditionals
+= 1;
353 int32
DNSUpdateAddPRSection( DNS_UPDATE_REQUEST
* pDNSRequest
,
354 DNS_RR_RECORD
* pDNSRecord
)
359 dwNumPRs
= pDNSRequest
->wPRs
;
360 dwError
= DNSReallocMemory( pDNSRequest
->ppPRRRSet
,
361 ( void * ) &pDNSRequest
->ppPRRRSet
,
363 1 ) * sizeof( DNS_RR_RECORD
* ) );
364 BAIL_ON_ERROR( dwError
);
366 *( pDNSRequest
->ppPRRRSet
+ dwNumPRs
) = pDNSRecord
;
368 pDNSRequest
->wPRs
+= 1;
375 /*********************************************************************
376 *********************************************************************/
378 int32
DNSUpdateAddUpdateSection( DNS_UPDATE_REQUEST
* pDNSRequest
,
379 DNS_RR_RECORD
* pDNSRecord
)
382 int16 wNumUpdates
= 0;
384 wNumUpdates
= pDNSRequest
->wUpdates
;
385 dwError
= DNSReallocMemory( pDNSRequest
->ppUpdateRRSet
,
386 ( void * ) &pDNSRequest
->ppUpdateRRSet
,
388 1 ) * sizeof( DNS_RR_RECORD
* ) );
389 BAIL_ON_ERROR( dwError
);
391 *( pDNSRequest
->ppUpdateRRSet
+ wNumUpdates
) = pDNSRecord
;
393 pDNSRequest
->wUpdates
+= 1;
399 /*********************************************************************
400 *********************************************************************/
402 int32
DNSUpdateCreateUpdateRequest( DNS_UPDATE_REQUEST
** ppDNSRequest
)
405 DNS_UPDATE_REQUEST
*pDNSRequest
= NULL
;
408 DNSAllocateMemory( sizeof( DNS_UPDATE_REQUEST
),
409 ( void * ) &pDNSRequest
);
410 BAIL_ON_ERROR( dwError
);
412 dwError
= DNSGenerateIdentifier( &pDNSRequest
->wIdentification
);
413 BAIL_ON_ERROR( dwError
);
415 pDNSRequest
->wParameter
= 0x2800;
417 *ppDNSRequest
= pDNSRequest
;
424 DNSUpdateFreeRequest( pDNSRequest
);
426 *ppDNSRequest
= NULL
;