4 * Copyright (C) 2006 Hans Leidekker
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
23 #include "wine/debug.h"
27 #include <sys/types.h>
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
46 WINE_DEFAULT_DEBUG_CHANNEL(dnsapi);
48 const char *dns_type_to_str( unsigned short type )
52 #define X(x) case (x): return #x;
110 default: { static char tmp[7]; sprintf( tmp, "0x%04x", type ); return tmp; }
114 static int dns_strcmpX( LPCVOID str1, LPCVOID str2, BOOL wide )
117 return lstrcmpiW( str1, str2 );
119 return lstrcmpiA( str1, str2 );
122 /******************************************************************************
123 * DnsRecordCompare [DNSAPI.@]
126 BOOL WINAPI DnsRecordCompare( PDNS_RECORD r1, PDNS_RECORD r2 )
131 TRACE( "(%p,%p)\n", r1, r2 );
133 if (r1->wType != r2->wType ||
134 r1->wDataLength != r2->wDataLength ||
135 r1->Flags.DW != r2->Flags.DW ||
136 r1->dwReserved != r2->dwReserved) return FALSE;
138 wide = (r1->Flags.S.CharSet == DnsCharSetUnicode) ? TRUE : FALSE;
139 if (dns_strcmpX( r1->pName, r2->pName, wide )) return FALSE;
145 if (r1->Data.A.IpAddress != r2->Data.A.IpAddress) return FALSE;
150 if (r1->Data.SOA.dwSerialNo != r2->Data.SOA.dwSerialNo ||
151 r1->Data.SOA.dwRefresh != r2->Data.SOA.dwRefresh ||
152 r1->Data.SOA.dwRetry != r2->Data.SOA.dwRetry ||
153 r1->Data.SOA.dwExpire != r2->Data.SOA.dwExpire ||
154 r1->Data.SOA.dwDefaultTtl != r2->Data.SOA.dwDefaultTtl)
156 if (dns_strcmpX( r1->Data.SOA.pNamePrimaryServer,
157 r2->Data.SOA.pNamePrimaryServer, wide ) ||
158 dns_strcmpX( r1->Data.SOA.pNameAdministrator,
159 r2->Data.SOA.pNameAdministrator, wide ))
172 if (dns_strcmpX( r1->Data.PTR.pNameHost,
173 r2->Data.PTR.pNameHost, wide )) return FALSE;
179 if (dns_strcmpX( r1->Data.MINFO.pNameMailbox,
180 r2->Data.MINFO.pNameMailbox, wide ) ||
181 dns_strcmpX( r1->Data.MINFO.pNameErrorsMailbox,
182 r2->Data.MINFO.pNameErrorsMailbox, wide ))
190 if (r1->Data.MX.wPreference != r2->Data.MX.wPreference)
192 if (dns_strcmpX( r1->Data.MX.pNameExchange,
193 r2->Data.MX.pNameExchange, wide ))
202 if (r1->Data.TXT.dwStringCount != r2->Data.TXT.dwStringCount)
204 for (i = 0; i < r1->Data.TXT.dwStringCount; i++)
206 if (dns_strcmpX( r1->Data.TXT.pStringArray[i],
207 r2->Data.TXT.pStringArray[i], wide ))
214 if (r1->Data.Null.dwByteCount != r2->Data.Null.dwByteCount)
216 if (memcmp( r1->Data.Null.Data,
217 r2->Data.Null.Data, r1->Data.Null.dwByteCount ))
223 for (i = 0; i < sizeof(IP6_ADDRESS)/sizeof(DWORD); i++)
225 if (r1->Data.AAAA.Ip6Address.IP6Dword[i] !=
226 r2->Data.AAAA.Ip6Address.IP6Dword[i]) return FALSE;
232 if (r1->Data.KEY.wFlags != r2->Data.KEY.wFlags ||
233 r1->Data.KEY.chProtocol != r2->Data.KEY.chProtocol ||
234 r1->Data.KEY.chAlgorithm != r2->Data.KEY.chAlgorithm)
236 if (memcmp( r1->Data.KEY.Key, r2->Data.KEY.Key,
237 r1->wDataLength - sizeof(DNS_KEY_DATA) + 1 ))
243 if (dns_strcmpX( r1->Data.SIG.pNameSigner,
244 r2->Data.SIG.pNameSigner, wide ))
246 if (r1->Data.SIG.wTypeCovered != r2->Data.SIG.wTypeCovered ||
247 r1->Data.SIG.chAlgorithm != r2->Data.SIG.chAlgorithm ||
248 r1->Data.SIG.chLabelCount != r2->Data.SIG.chLabelCount ||
249 r1->Data.SIG.dwOriginalTtl != r2->Data.SIG.dwOriginalTtl ||
250 r1->Data.SIG.dwExpiration != r2->Data.SIG.dwExpiration ||
251 r1->Data.SIG.dwTimeSigned != r2->Data.SIG.dwTimeSigned ||
252 r1->Data.SIG.wKeyTag != r2->Data.SIG.wKeyTag)
254 if (memcmp( r1->Data.SIG.Signature, r2->Data.SIG.Signature,
255 r1->wDataLength - sizeof(DNS_SIG_DATAA) + 1 ))
261 if (r1->Data.ATMA.AddressType != r2->Data.ATMA.AddressType)
263 for (i = 0; i < DNS_ATMA_MAX_ADDR_LENGTH; i++)
265 if (r1->Data.ATMA.Address[i] != r2->Data.ATMA.Address[i])
272 if (dns_strcmpX( r1->Data.NXT.pNameNext,
273 r2->Data.NXT.pNameNext, wide )) return FALSE;
274 if (r1->Data.NXT.wNumTypes != r2->Data.NXT.wNumTypes) return FALSE;
275 if (memcmp( r1->Data.NXT.wTypes, r2->Data.NXT.wTypes,
276 r1->wDataLength - sizeof(DNS_NXT_DATAA) + sizeof(WORD) ))
282 if (dns_strcmpX( r1->Data.SRV.pNameTarget,
283 r2->Data.SRV.pNameTarget, wide )) return FALSE;
284 if (r1->Data.SRV.wPriority != r2->Data.SRV.wPriority ||
285 r1->Data.SRV.wWeight != r2->Data.SRV.wWeight ||
286 r1->Data.SRV.wPort != r2->Data.SRV.wPort)
292 if (dns_strcmpX( r1->Data.TKEY.pNameAlgorithm,
293 r2->Data.TKEY.pNameAlgorithm, wide ))
295 if (r1->Data.TKEY.dwCreateTime != r2->Data.TKEY.dwCreateTime ||
296 r1->Data.TKEY.dwExpireTime != r2->Data.TKEY.dwExpireTime ||
297 r1->Data.TKEY.wMode != r2->Data.TKEY.wMode ||
298 r1->Data.TKEY.wError != r2->Data.TKEY.wError ||
299 r1->Data.TKEY.wKeyLength != r2->Data.TKEY.wKeyLength ||
300 r1->Data.TKEY.wOtherLength != r2->Data.TKEY.wOtherLength ||
301 r1->Data.TKEY.cAlgNameLength != r2->Data.TKEY.cAlgNameLength ||
302 r1->Data.TKEY.bPacketPointers != r2->Data.TKEY.bPacketPointers)
305 /* FIXME: ignoring pAlgorithmPacket field */
306 if (memcmp( r1->Data.TKEY.pKey, r2->Data.TKEY.pKey,
307 r1->Data.TKEY.wKeyLength ) ||
308 memcmp( r1->Data.TKEY.pOtherData, r2->Data.TKEY.pOtherData,
309 r1->Data.TKEY.wOtherLength )) return FALSE;
314 if (dns_strcmpX( r1->Data.TSIG.pNameAlgorithm,
315 r2->Data.TSIG.pNameAlgorithm, wide ))
317 if (r1->Data.TSIG.i64CreateTime != r2->Data.TSIG.i64CreateTime ||
318 r1->Data.TSIG.wFudgeTime != r2->Data.TSIG.wFudgeTime ||
319 r1->Data.TSIG.wOriginalXid != r2->Data.TSIG.wOriginalXid ||
320 r1->Data.TSIG.wError != r2->Data.TSIG.wError ||
321 r1->Data.TSIG.wSigLength != r2->Data.TSIG.wSigLength ||
322 r1->Data.TSIG.wOtherLength != r2->Data.TSIG.wOtherLength ||
323 r1->Data.TSIG.cAlgNameLength != r2->Data.TSIG.cAlgNameLength ||
324 r1->Data.TSIG.bPacketPointers != r2->Data.TSIG.bPacketPointers)
327 /* FIXME: ignoring pAlgorithmPacket field */
328 if (memcmp( r1->Data.TSIG.pSignature, r2->Data.TSIG.pSignature,
329 r1->Data.TSIG.wSigLength ) ||
330 memcmp( r1->Data.TSIG.pOtherData, r2->Data.TSIG.pOtherData,
331 r1->Data.TSIG.wOtherLength )) return FALSE;
336 if (r1->Data.WINS.dwMappingFlag != r2->Data.WINS.dwMappingFlag ||
337 r1->Data.WINS.dwLookupTimeout != r2->Data.WINS.dwLookupTimeout ||
338 r1->Data.WINS.dwCacheTimeout != r2->Data.WINS.dwCacheTimeout ||
339 r1->Data.WINS.cWinsServerCount != r2->Data.WINS.cWinsServerCount)
341 if (memcmp( r1->Data.WINS.WinsServers, r2->Data.WINS.WinsServers,
342 r1->wDataLength - sizeof(DNS_WINS_DATA) + sizeof(IP4_ADDRESS) ))
348 if (r1->Data.WINSR.dwMappingFlag != r2->Data.WINSR.dwMappingFlag ||
349 r1->Data.WINSR.dwLookupTimeout != r2->Data.WINSR.dwLookupTimeout ||
350 r1->Data.WINSR.dwCacheTimeout != r2->Data.WINSR.dwCacheTimeout)
352 if (dns_strcmpX( r1->Data.WINSR.pNameResultDomain,
353 r2->Data.WINSR.pNameResultDomain, wide ))
358 FIXME( "unknown type: %s\n", dns_type_to_str( r1->wType ) );
364 static LPVOID dns_strcpyX( LPCVOID src, DNS_CHARSET in, DNS_CHARSET out )
368 case DnsCharSetUnicode:
372 case DnsCharSetUnicode: return dns_strdup_w( src );
373 case DnsCharSetUtf8: return dns_strdup_wu( src );
374 case DnsCharSetAnsi: return dns_strdup_wa( src );
376 WARN( "unhandled target charset: %d\n", out );
383 case DnsCharSetUnicode: return dns_strdup_uw( src );
384 case DnsCharSetUtf8: return dns_strdup_u( src );
385 case DnsCharSetAnsi: return dns_strdup_ua( src );
387 WARN( "unhandled target charset: %d\n", out );
393 case DnsCharSetUnicode: return dns_strdup_aw( src );
394 case DnsCharSetUtf8: return dns_strdup_au( src );
395 case DnsCharSetAnsi: return dns_strdup_a( src );
397 WARN( "unhandled target charset: %d\n", out );
401 WARN( "unhandled source charset: %d\n", in );
407 /******************************************************************************
408 * DnsRecordCopyEx [DNSAPI.@]
411 PDNS_RECORD WINAPI DnsRecordCopyEx( PDNS_RECORD src, DNS_CHARSET in, DNS_CHARSET out )
414 unsigned int i, size;
416 TRACE( "(%p,%d,%d)\n", src, in, out );
418 size = FIELD_OFFSET(DNS_RECORD, Data) + src->wDataLength;
419 dst = heap_alloc_zero( size );
420 if (!dst) return NULL;
422 memcpy( dst, src, size );
424 if (src->Flags.S.CharSet == DnsCharSetUtf8 ||
425 src->Flags.S.CharSet == DnsCharSetAnsi ||
426 src->Flags.S.CharSet == DnsCharSetUnicode) in = src->Flags.S.CharSet;
428 dst->Flags.S.CharSet = out;
429 dst->pName = dns_strcpyX( src->pName, in, out );
430 if (!dst->pName) goto error;
439 for (i = 0; i < src->Data.TXT.dwStringCount; i++)
441 dst->Data.TXT.pStringArray[i] =
442 dns_strcpyX( src->Data.TXT.pStringArray[i], in, out );
444 if (!dst->Data.TXT.pStringArray[i])
446 while (i > 0) heap_free( dst->Data.TXT.pStringArray[--i] );
455 dst->Data.MINFO.pNameMailbox =
456 dns_strcpyX( src->Data.MINFO.pNameMailbox, in, out );
457 if (!dst->Data.MINFO.pNameMailbox) goto error;
459 dst->Data.MINFO.pNameErrorsMailbox =
460 dns_strcpyX( src->Data.MINFO.pNameErrorsMailbox, in, out );
461 if (!dst->Data.MINFO.pNameErrorsMailbox)
463 heap_free( dst->Data.MINFO.pNameMailbox );
472 dst->Data.MX.pNameExchange =
473 dns_strcpyX( src->Data.MX.pNameExchange, in, out );
474 if (!dst->Data.MX.pNameExchange) goto error;
479 dst->Data.NXT.pNameNext =
480 dns_strcpyX( src->Data.NXT.pNameNext, in, out );
481 if (!dst->Data.NXT.pNameNext) goto error;
493 dst->Data.PTR.pNameHost =
494 dns_strcpyX( src->Data.PTR.pNameHost, in, out );
495 if (!dst->Data.PTR.pNameHost) goto error;
500 dst->Data.SIG.pNameSigner =
501 dns_strcpyX( src->Data.SIG.pNameSigner, in, out );
502 if (!dst->Data.SIG.pNameSigner) goto error;
507 dst->Data.SOA.pNamePrimaryServer =
508 dns_strcpyX( src->Data.SOA.pNamePrimaryServer, in, out );
509 if (!dst->Data.SOA.pNamePrimaryServer) goto error;
511 dst->Data.SOA.pNameAdministrator =
512 dns_strcpyX( src->Data.SOA.pNameAdministrator, in, out );
513 if (!dst->Data.SOA.pNameAdministrator)
515 heap_free( dst->Data.SOA.pNamePrimaryServer );
522 dst->Data.SRV.pNameTarget =
523 dns_strcpyX( src->Data.SRV.pNameTarget, in, out );
524 if (!dst->Data.SRV.pNameTarget) goto error;
533 heap_free( dst->pName );
538 /******************************************************************************
539 * DnsRecordListFree [DNSAPI.@]
542 VOID WINAPI DnsRecordListFree( PDNS_RECORD list, DNS_FREE_TYPE type )
544 DNS_RECORD *r, *next;
547 TRACE( "(%p,%d)\n", list, type );
553 case DnsFreeRecordList:
555 for (r = list; (list = r); r = next)
557 heap_free( r->pName );
566 for (i = 0; i < r->Data.TXT.dwStringCount; i++)
567 heap_free( r->Data.TXT.pStringArray[i] );
574 heap_free( r->Data.MINFO.pNameMailbox );
575 heap_free( r->Data.MINFO.pNameErrorsMailbox );
582 heap_free( r->Data.MX.pNameExchange );
587 heap_free( r->Data.NXT.pNameNext );
599 heap_free( r->Data.PTR.pNameHost );
604 heap_free( r->Data.SIG.pNameSigner );
609 heap_free( r->Data.SOA.pNamePrimaryServer );
610 heap_free( r->Data.SOA.pNameAdministrator );
615 heap_free( r->Data.SRV.pNameTarget );
628 case DnsFreeParsedMessageFields:
630 FIXME( "unhandled free type: %d\n", type );
634 WARN( "unknown free type: %d\n", type );
639 /******************************************************************************
640 * DnsRecordSetCompare [DNSAPI.@]
643 BOOL WINAPI DnsRecordSetCompare( PDNS_RECORD set1, PDNS_RECORD set2,
644 PDNS_RECORD *diff1, PDNS_RECORD *diff2 )
647 DNS_RECORD *r, *t, *u;
650 TRACE( "(%p,%p,%p,%p)\n", set1, set2, diff1, diff2 );
652 if (!set1 && !set2) return FALSE;
654 if (diff1) *diff1 = NULL;
655 if (diff2) *diff2 = NULL;
659 if (diff1) *diff1 = DnsRecordSetCopyEx( set1, 0, set1->Flags.S.CharSet );
664 if (diff2) *diff2 = DnsRecordSetCopyEx( set2, 0, set2->Flags.S.CharSet );
668 DNS_RRSET_INIT( rr1 );
669 DNS_RRSET_INIT( rr2 );
671 for (r = set1; r; r = r->pNext)
673 for (t = set2; t; t = t->pNext)
675 u = DnsRecordCopyEx( r, r->Flags.S.CharSet, t->Flags.S.CharSet );
678 if (!DnsRecordCompare( t, u ))
680 DNS_RRSET_ADD( rr1, u );
683 else DnsRecordListFree( u, DnsFreeRecordList );
687 for (t = set2; t; t = t->pNext)
689 for (r = set1; r; r = r->pNext)
691 u = DnsRecordCopyEx( t, t->Flags.S.CharSet, r->Flags.S.CharSet );
694 if (!DnsRecordCompare( r, u ))
696 DNS_RRSET_ADD( rr2, u );
699 else DnsRecordListFree( u, DnsFreeRecordList );
703 DNS_RRSET_TERMINATE( rr1 );
704 DNS_RRSET_TERMINATE( rr2 );
706 if (diff1) *diff1 = rr1.pFirstRR;
707 else DnsRecordListFree( rr1.pFirstRR, DnsFreeRecordList );
709 if (diff2) *diff2 = rr2.pFirstRR;
710 else DnsRecordListFree( rr2.pFirstRR, DnsFreeRecordList );
715 DNS_RRSET_TERMINATE( rr1 );
716 DNS_RRSET_TERMINATE( rr2 );
718 DnsRecordListFree( rr1.pFirstRR, DnsFreeRecordList );
719 DnsRecordListFree( rr2.pFirstRR, DnsFreeRecordList );
724 /******************************************************************************
725 * DnsRecordSetCopyEx [DNSAPI.@]
728 PDNS_RECORD WINAPI DnsRecordSetCopyEx( PDNS_RECORD src_set, DNS_CHARSET in, DNS_CHARSET out )
731 DNS_RECORD *src, *dst;
733 TRACE( "(%p,%d,%d)\n", src_set, in, out );
735 DNS_RRSET_INIT( dst_set );
737 for (src = src_set; (src_set = src); src = src_set->pNext)
739 dst = DnsRecordCopyEx( src, in, out );
742 DNS_RRSET_TERMINATE( dst_set );
743 DnsRecordListFree( dst_set.pFirstRR, DnsFreeRecordList );
746 DNS_RRSET_ADD( dst_set, dst );
749 DNS_RRSET_TERMINATE( dst_set );
750 return dst_set.pFirstRR;
753 /******************************************************************************
754 * DnsRecordSetDetach [DNSAPI.@]
757 PDNS_RECORD WINAPI DnsRecordSetDetach( PDNS_RECORD set )
761 TRACE( "(%p)\n", set );
763 for (r = set; (set = r); r = set->pNext)
765 if (r->pNext && !r->pNext->pNext)