r19758: remove the LDAP_SCOPE_ONELEVEL define (not needed after svnup)
[Samba.git] / source / libaddns / dnsuprequest.c
blob9de5ec88ae7e1de2ded58f36f3482974d8b0c4d0
1 /*
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
9 ** under the LGPL
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
24 02110-1301 USA
27 #include "dns.h"
29 /*********************************************************************
30 *********************************************************************/
32 static int32 DNSUpdateMarshallZoneSection( HANDLE hSendBuffer,
33 DNS_ZONE_RECORD ** ppDNSZoneRecords,
34 int16 wZones )
36 int32 dwError = 0;
37 int32 i = 0;
38 int32 dwRead = 0;
39 DNS_ZONE_RECORD *pDNSZoneRecord = NULL;
40 int16 wnZoneType = 0;
41 int16 wnZoneClass = 0;
43 for ( i = 0; i < wZones; i++ ) {
45 pDNSZoneRecord = *( ppDNSZoneRecords + i );
46 dwError =
47 DNSMarshallDomainName( hSendBuffer,
48 pDNSZoneRecord->pDomainName );
49 BAIL_ON_ERROR( dwError );
51 wnZoneType = htons( pDNSZoneRecord->wZoneType );
52 dwError =
53 DNSMarshallBuffer( hSendBuffer,
54 ( uint8 * ) & wnZoneType,
55 ( int32 ) sizeof( int16 ),
56 &dwRead );
57 BAIL_ON_ERROR( dwError );
59 wnZoneClass = htons( pDNSZoneRecord->wZoneClass );
60 dwError =
61 DNSMarshallBuffer( hSendBuffer,
62 ( uint8 * ) & wnZoneClass,
63 ( int32 ) sizeof( int16 ),
64 &dwRead );
65 BAIL_ON_ERROR( dwError );
67 pDNSZoneRecord++;
70 error:
72 return dwError;
75 /*********************************************************************
76 *********************************************************************/
78 static int32 DNSUpdateMarshallPRSection( HANDLE hSendBuffer,
79 DNS_RR_RECORD ** ppDNSPRRRRecords, int16 wPRs )
81 int32 dwError = 0;
82 int32 i = 0;
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 ) {
94 dwError =
95 DNSMarshallRData( hSendBuffer,
96 pDNSPRRRRecord );
97 BAIL_ON_ERROR( dwError );
100 error:
102 return dwError;
105 /*********************************************************************
106 *********************************************************************/
108 static int32 DNSUpdateMarshallUpdateSection( HANDLE hSendBuffer,
109 DNS_RR_RECORD ** ppDNSUpdateRRRecords,
110 int16 wZones )
112 int32 dwError = 0;
113 int32 i = 0;
114 DNS_RR_RECORD *pDNSUpdateRRRecord = NULL;
116 for ( i = 0; i < wZones; i++ ) {
118 pDNSUpdateRRRecord = *( ppDNSUpdateRRRecords + i );
120 dwError =
121 DNSMarshallRRHeader( hSendBuffer,
122 pDNSUpdateRRRecord );
124 if ( pDNSUpdateRRRecord->RRHeader.wRDataSize ) {
125 dwError = DNSMarshallRData( hSendBuffer,
126 pDNSUpdateRRRecord );
127 BAIL_ON_ERROR( dwError );
131 error:
133 return dwError;
136 /*********************************************************************
137 *********************************************************************/
139 static int32 DNSUpdateMarshallAdditionalSection( HANDLE hSendBuffer,
140 DNS_RR_RECORD **
141 ppDNSAdditionalsRRRecords,
142 int16 wAdditionals )
144 int32 dwError = 0;
145 int32 i = 0;
146 DNS_RR_RECORD *pDNSAdditionalRRRecord = NULL;
148 for ( i = 0; i < wAdditionals; i++ ) {
150 pDNSAdditionalRRRecord = *( ppDNSAdditionalsRRRecords + i );
152 dwError =
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 );
164 error:
166 return dwError;
169 /*********************************************************************
170 *********************************************************************/
172 int32 DNSUpdateSendUpdateRequest2( HANDLE hDNSServer,
173 DNS_UPDATE_REQUEST * pDNSRequest )
175 int32 dwError = 0;
176 int32 dwBytesSent = 0;
177 HANDLE hSendBuffer = ( HANDLE ) NULL;
179 dwError = DNSUpdateBuildRequestMessage( pDNSRequest, &hSendBuffer );
180 BAIL_ON_ERROR( dwError );
182 dwError =
183 DNSSendBufferContext( hDNSServer, hSendBuffer, &dwBytesSent );
184 BAIL_ON_ERROR( dwError );
186 error:
188 if ( hSendBuffer ) {
189 DNSFreeSendBufferContext( hSendBuffer );
192 return dwError;
195 /*********************************************************************
196 *********************************************************************/
198 int32 DNSUpdateBuildRequestMessage( DNS_UPDATE_REQUEST * pDNSRequest,
199 HANDLE * phSendBuffer )
201 int32 dwError = 0;
202 char DNSMessageHeader[12];
203 int16 wnIdentification = 0;
204 int16 wnParameter = 0;
205 int16 wnZones = 0;
206 int16 wnPRs = 0;
207 int16 wnUpdates = 0;
208 int16 wnAdditionals = 0;
209 int32 dwRead = 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 );
233 dwError =
234 DNSMarshallBuffer( hSendBuffer, ( uint8 * ) DNSMessageHeader,
235 12, &dwRead );
236 BAIL_ON_ERROR( dwError );
238 if ( pDNSRequest->wZones ) {
239 dwError =
240 DNSUpdateMarshallZoneSection( hSendBuffer,
241 pDNSRequest->
242 ppZoneRRSet,
243 pDNSRequest->wZones );
244 BAIL_ON_ERROR( dwError );
247 if ( pDNSRequest->wPRs ) {
248 dwError =
249 DNSUpdateMarshallPRSection( hSendBuffer,
250 pDNSRequest->ppPRRRSet,
251 pDNSRequest->wPRs );
252 BAIL_ON_ERROR( dwError );
255 if ( pDNSRequest->wUpdates ) {
256 dwError =
257 DNSUpdateMarshallUpdateSection( hSendBuffer,
258 pDNSRequest->
259 ppUpdateRRSet,
260 pDNSRequest->
261 wUpdates );
262 BAIL_ON_ERROR( dwError );
265 if ( pDNSRequest->wAdditionals ) {
266 dwError =
267 DNSUpdateMarshallAdditionalSection( hSendBuffer,
268 pDNSRequest->
269 ppAdditionalRRSet,
270 pDNSRequest->
271 wAdditionals );
272 BAIL_ON_ERROR( dwError );
274 #if 0
275 DNSDumpSendBufferContext( hSendBuffer );
276 #endif
278 *phSendBuffer = hSendBuffer;
280 return dwError;
282 error:
284 if ( hSendBuffer ) {
285 DNSFreeSendBufferContext( hSendBuffer );
288 *phSendBuffer = ( HANDLE ) NULL;
289 return dwError;
293 /*********************************************************************
294 *********************************************************************/
296 void DNSUpdateFreeRequest( DNS_UPDATE_REQUEST * pDNSRequest )
298 return;
301 /*********************************************************************
302 *********************************************************************/
304 int32 DNSUpdateAddZoneSection( DNS_UPDATE_REQUEST * pDNSRequest,
305 DNS_ZONE_RECORD * pDNSZone )
307 int32 dwNumZones = 0;
308 int32 dwError = 0;
310 dwNumZones = pDNSRequest->wZones;
312 dwError = DNSReallocMemory( ( uint8 * ) pDNSRequest->ppZoneRRSet,
313 ( void * ) &pDNSRequest->ppZoneRRSet,
314 ( dwNumZones +
315 1 ) * sizeof( DNS_ZONE_RECORD * )
317 BAIL_ON_ERROR( dwError );
319 *( pDNSRequest->ppZoneRRSet + dwNumZones ) = pDNSZone;
321 pDNSRequest->wZones += 1;
323 error:
325 return dwError;
328 /*********************************************************************
329 *********************************************************************/
331 int32 DNSUpdateAddAdditionalSection( DNS_UPDATE_REQUEST * pDNSRequest,
332 DNS_RR_RECORD * pDNSRecord )
334 int32 dwNumAdditionals = 0;
335 int32 dwError = 0;
337 dwNumAdditionals = pDNSRequest->wAdditionals;
338 dwError = DNSReallocMemory( pDNSRequest->ppAdditionalRRSet,
339 ( void * ) &pDNSRequest->
340 ppAdditionalRRSet,
341 ( dwNumAdditionals +
342 1 ) * sizeof( DNS_RR_RECORD * ) );
343 BAIL_ON_ERROR( dwError );
345 *( pDNSRequest->ppAdditionalRRSet + dwNumAdditionals ) = pDNSRecord;
347 pDNSRequest->wAdditionals += 1;
349 error:
350 return dwError;
353 int32 DNSUpdateAddPRSection( DNS_UPDATE_REQUEST * pDNSRequest,
354 DNS_RR_RECORD * pDNSRecord )
356 int32 dwNumPRs = 0;
357 int32 dwError = 0;
359 dwNumPRs = pDNSRequest->wPRs;
360 dwError = DNSReallocMemory( pDNSRequest->ppPRRRSet,
361 ( void * ) &pDNSRequest->ppPRRRSet,
362 ( dwNumPRs +
363 1 ) * sizeof( DNS_RR_RECORD * ) );
364 BAIL_ON_ERROR( dwError );
366 *( pDNSRequest->ppPRRRSet + dwNumPRs ) = pDNSRecord;
368 pDNSRequest->wPRs += 1;
370 error:
371 return dwError;
375 /*********************************************************************
376 *********************************************************************/
378 int32 DNSUpdateAddUpdateSection( DNS_UPDATE_REQUEST * pDNSRequest,
379 DNS_RR_RECORD * pDNSRecord )
381 int32 dwError = 0;
382 int16 wNumUpdates = 0;
384 wNumUpdates = pDNSRequest->wUpdates;
385 dwError = DNSReallocMemory( pDNSRequest->ppUpdateRRSet,
386 ( void * ) &pDNSRequest->ppUpdateRRSet,
387 ( wNumUpdates +
388 1 ) * sizeof( DNS_RR_RECORD * ) );
389 BAIL_ON_ERROR( dwError );
391 *( pDNSRequest->ppUpdateRRSet + wNumUpdates ) = pDNSRecord;
393 pDNSRequest->wUpdates += 1;
395 error:
396 return dwError;
399 /*********************************************************************
400 *********************************************************************/
402 int32 DNSUpdateCreateUpdateRequest( DNS_UPDATE_REQUEST ** ppDNSRequest )
404 int32 dwError = 0;
405 DNS_UPDATE_REQUEST *pDNSRequest = NULL;
407 dwError =
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;
419 return dwError;
421 error:
423 if ( pDNSRequest ) {
424 DNSUpdateFreeRequest( pDNSRequest );
426 *ppDNSRequest = NULL;
427 return dwError;