r18893: Attempt to fix the Solaris build
[Samba.git] / source / libaddns / dnsupresp.c
blob26cc9ff8e2da61d42c952e88af6662499ce6ceea
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 DNSUpdateAllocateResponse( DNS_UPDATE_RESPONSE ** ppDNSResponse )
34 int32 dwError = 0;
35 DNS_UPDATE_RESPONSE *pDNSResponse = NULL;
37 dwError =
38 DNSAllocateMemory( sizeof( DNS_UPDATE_RESPONSE ),
39 ( void * ) &pDNSResponse );
40 BAIL_ON_ERROR( dwError );
42 *ppDNSResponse = pDNSResponse;
44 return dwError;
46 error:
48 *ppDNSResponse = NULL;
50 return dwError;
53 /*********************************************************************
54 *********************************************************************/
56 static int32 DNSUpdateUnmarshallAdditionalSection( HANDLE hReceiveBuffer,
57 int16 wAdditionals,
58 DNS_RR_RECORD *
59 **pppDNSAdditionalsRRRecords )
61 int32 dwError = 0;
62 int32 i = 0;
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;
68 int32 dwRead = 0;
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 );
80 dwError =
81 DNSUnmarshallRData( hReceiveBuffer,
82 pRRHeader->wRDataSize, &pRRData,
83 &dwRead );
84 BAIL_ON_ERROR( dwError );
86 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;
98 error:
100 return dwError;
103 /*********************************************************************
104 *********************************************************************/
106 static int32 DNSUpdateUnmarshallPRSection( HANDLE hReceiveBuffer,
107 int16 wPRs,
108 DNS_RR_RECORD * **pppDNSPRRRRecords )
110 int32 dwError = 0;
111 int32 i = 0;
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;
117 int32 dwRead = 0;
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 );
129 dwError =
130 DNSUnmarshallRData( hReceiveBuffer,
131 pRRHeader->wRDataSize, &pRRData,
132 &dwRead );
133 BAIL_ON_ERROR( dwError );
135 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;
149 return dwError;
151 error:
154 return dwError;
157 /*********************************************************************
158 *********************************************************************/
160 static int32 DNSUpdateUnmarshallUpdateSection( HANDLE hReceiveBuffer,
161 int16 wUpdates,
162 DNS_RR_RECORD * **pppDNSUpdateRRRecords )
164 int32 dwError = 0;
165 int32 i = 0;
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;
171 int32 dwRead = 0;
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 );
183 dwError =
184 DNSUnmarshallRData( hReceiveBuffer,
185 pRRHeader->wRDataSize, &pRRData,
186 &dwRead );
187 BAIL_ON_ERROR( dwError );
189 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;
203 return dwError;
205 error:
207 return dwError;
211 /*********************************************************************
212 *********************************************************************/
214 static int32 DNSUpdateUnmarshallZoneSection( HANDLE hReceiveBuffer,
215 int16 wZones,
216 DNS_ZONE_RECORD * **pppDNSZoneRecords )
218 int32 dwError = 0;
219 int32 i = 0;
220 int32 dwRead = 0;
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++ ) {
233 dwError =
234 DNSAllocateMemory( sizeof( DNS_ZONE_RECORD ),
235 ( void * ) &pDNSZoneRecord );
236 BAIL_ON_ERROR( dwError );
238 dwError =
239 DNSUnmarshallDomainName( hReceiveBuffer,
240 &pDNSZoneRecord->
241 pDomainName );
242 BAIL_ON_ERROR( dwError );
244 dwError =
245 DNSUnmarshallBuffer( hReceiveBuffer,
246 ( uint8 * ) & wnZoneType,
247 ( int32 ) sizeof( int16 ),
248 &dwRead );
249 BAIL_ON_ERROR( dwError );
250 pDNSZoneRecord->wZoneType = ntohs( wnZoneType );
252 dwError =
253 DNSUnmarshallBuffer( hReceiveBuffer,
254 ( uint8 * ) & wnZoneClass,
255 ( int32 ) sizeof( int16 ),
256 &dwRead );
257 BAIL_ON_ERROR( dwError );
258 pDNSZoneRecord->wZoneClass = ntohs( wnZoneClass );
260 *( ppDNSZoneRecords + i ) = pDNSZoneRecord;
263 *pppDNSZoneRecords = ppDNSZoneRecords;
264 return dwError;
266 error:
268 return dwError;
272 /*********************************************************************
273 *********************************************************************/
275 int32 DNSUpdateReceiveUpdateResponse( HANDLE hDNSHandle,
276 DNS_UPDATE_RESPONSE ** ppDNSResponse )
278 DNS_UPDATE_RESPONSE *pDNSResponse = NULL;
279 int32 dwError = 0;
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;
286 int32 dwRead = 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 );
299 #if 0
300 dwError = DNSDumpRecvBufferContext( hRecvBuffer );
301 BAIL_ON_ERROR( dwError );
302 #endif
304 dwError =
305 DNSUnmarshallBuffer( hRecvBuffer,
306 ( uint8 * ) & wnIdentification,
307 sizeof( int16 ), &dwRead );
308 BAIL_ON_ERROR( dwError );
309 wIdentification = ntohs( wnIdentification );
311 dwError =
312 DNSUnmarshallBuffer( hRecvBuffer, ( uint8 * ) & wnParameter,
313 sizeof( int16 ), &dwRead );
314 BAIL_ON_ERROR( dwError );
315 wParameter = ntohs( wnParameter );
318 dwError =
319 DNSUnmarshallBuffer( hRecvBuffer, ( uint8 * ) & wnZones,
320 sizeof( int16 ), &dwRead );
321 BAIL_ON_ERROR( dwError );
322 wZones = ntohs( wnZones );
325 dwError =
326 DNSUnmarshallBuffer( hRecvBuffer, ( uint8 * ) & wnPRs,
327 sizeof( int16 ), &dwRead );
328 BAIL_ON_ERROR( dwError );
329 wPRs = ntohs( wnPRs );
332 dwError =
333 DNSUnmarshallBuffer( hRecvBuffer, ( uint8 * ) & wnUpdates,
334 sizeof( int16 ), &dwRead );
335 BAIL_ON_ERROR( dwError );
336 wUpdates = ntohs( wnUpdates );
338 dwError =
339 DNSUnmarshallBuffer( hRecvBuffer, ( uint8 * ) & wnAdditionals,
340 sizeof( int16 ), &dwRead );
341 BAIL_ON_ERROR( dwError );
342 wAdditionals = ntohs( wnAdditionals );
345 if ( wZones ) {
346 dwError =
347 DNSUpdateUnmarshallZoneSection( hRecvBuffer, wZones,
348 &ppDNSZoneRecords );
349 BAIL_ON_ERROR( dwError );
352 if ( wPRs ) {
353 dwError =
354 DNSUpdateUnmarshallPRSection( hRecvBuffer, wPRs,
355 &ppDNSPRRecords );
356 BAIL_ON_ERROR( dwError );
359 if ( wUpdates ) {
360 dwError =
361 DNSUpdateUnmarshallUpdateSection( hRecvBuffer,
362 wUpdates,
363 &ppDNSUpdateRecords );
364 BAIL_ON_ERROR( dwError );
367 if ( wAdditionals ) {
368 dwError =
369 DNSUpdateUnmarshallAdditionalSection( hRecvBuffer,
370 wAdditionals,
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;
393 error:
395 return dwError;