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"
28 #include <sys/types.h>
30 #ifdef HAVE_NETINET_IN_H
31 # include <netinet/in.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 # include <arpa/nameser.h>
47 WINE_DEFAULT_DEBUG_CHANNEL(dnsapi
);
49 const char *dns_type_to_str( unsigned short type
)
53 #define X(x) case (x): return #x;
111 default: { static char tmp
[7]; sprintf( tmp
, "0x%04x", type
); return tmp
; }
115 static int dns_strcmpX( LPCVOID str1
, LPCVOID str2
, BOOL wide
)
118 return lstrcmpiW( str1
, str2
);
120 return lstrcmpiA( str1
, str2
);
123 /******************************************************************************
124 * DnsRecordCompare [DNSAPI.@]
127 BOOL WINAPI
DnsRecordCompare( PDNS_RECORD r1
, PDNS_RECORD r2
)
132 TRACE( "(%p,%p)\n", r1
, r2
);
134 if (r1
->wType
!= r2
->wType
||
135 r1
->wDataLength
!= r2
->wDataLength
||
136 r1
->Flags
.DW
!= r2
->Flags
.DW
||
137 r1
->dwReserved
!= r2
->dwReserved
) return FALSE
;
139 wide
= (r1
->Flags
.S
.CharSet
== DnsCharSetUnicode
) ? TRUE
: FALSE
;
140 if (dns_strcmpX( r1
->pName
, r2
->pName
, wide
)) return FALSE
;
146 if (r1
->Data
.A
.IpAddress
!= r2
->Data
.A
.IpAddress
) return FALSE
;
151 if (r1
->Data
.SOA
.dwSerialNo
!= r2
->Data
.SOA
.dwSerialNo
||
152 r1
->Data
.SOA
.dwRefresh
!= r2
->Data
.SOA
.dwRefresh
||
153 r1
->Data
.SOA
.dwRetry
!= r2
->Data
.SOA
.dwRetry
||
154 r1
->Data
.SOA
.dwExpire
!= r2
->Data
.SOA
.dwExpire
||
155 r1
->Data
.SOA
.dwDefaultTtl
!= r2
->Data
.SOA
.dwDefaultTtl
)
157 if (dns_strcmpX( r1
->Data
.SOA
.pNamePrimaryServer
,
158 r2
->Data
.SOA
.pNamePrimaryServer
, wide
) ||
159 dns_strcmpX( r1
->Data
.SOA
.pNameAdministrator
,
160 r2
->Data
.SOA
.pNameAdministrator
, wide
))
173 if (dns_strcmpX( r1
->Data
.PTR
.pNameHost
,
174 r2
->Data
.PTR
.pNameHost
, wide
)) return FALSE
;
180 if (dns_strcmpX( r1
->Data
.MINFO
.pNameMailbox
,
181 r2
->Data
.MINFO
.pNameMailbox
, wide
) ||
182 dns_strcmpX( r1
->Data
.MINFO
.pNameErrorsMailbox
,
183 r2
->Data
.MINFO
.pNameErrorsMailbox
, wide
))
191 if (r1
->Data
.MX
.wPreference
!= r2
->Data
.MX
.wPreference
)
193 if (dns_strcmpX( r1
->Data
.MX
.pNameExchange
,
194 r2
->Data
.MX
.pNameExchange
, wide
))
203 if (r1
->Data
.TXT
.dwStringCount
!= r2
->Data
.TXT
.dwStringCount
)
205 for (i
= 0; i
< r1
->Data
.TXT
.dwStringCount
; i
++)
207 if (dns_strcmpX( r1
->Data
.TXT
.pStringArray
[i
],
208 r2
->Data
.TXT
.pStringArray
[i
], wide
))
215 if (r1
->Data
.Null
.dwByteCount
!= r2
->Data
.Null
.dwByteCount
)
217 if (memcmp( r1
->Data
.Null
.Data
,
218 r2
->Data
.Null
.Data
, r1
->Data
.Null
.dwByteCount
))
224 for (i
= 0; i
< sizeof(IP6_ADDRESS
)/sizeof(DWORD
); i
++)
226 if (r1
->Data
.AAAA
.Ip6Address
.IP6Dword
[i
] !=
227 r2
->Data
.AAAA
.Ip6Address
.IP6Dword
[i
]) return FALSE
;
233 if (r1
->Data
.KEY
.wFlags
!= r2
->Data
.KEY
.wFlags
||
234 r1
->Data
.KEY
.chProtocol
!= r2
->Data
.KEY
.chProtocol
||
235 r1
->Data
.KEY
.chAlgorithm
!= r2
->Data
.KEY
.chAlgorithm
)
237 if (memcmp( r1
->Data
.KEY
.Key
, r2
->Data
.KEY
.Key
,
238 r1
->wDataLength
- sizeof(DNS_KEY_DATA
) + 1 ))
244 if (dns_strcmpX( r1
->Data
.SIG
.pNameSigner
,
245 r2
->Data
.SIG
.pNameSigner
, wide
))
247 if (r1
->Data
.SIG
.wTypeCovered
!= r2
->Data
.SIG
.wTypeCovered
||
248 r1
->Data
.SIG
.chAlgorithm
!= r2
->Data
.SIG
.chAlgorithm
||
249 r1
->Data
.SIG
.chLabelCount
!= r2
->Data
.SIG
.chLabelCount
||
250 r1
->Data
.SIG
.dwOriginalTtl
!= r2
->Data
.SIG
.dwOriginalTtl
||
251 r1
->Data
.SIG
.dwExpiration
!= r2
->Data
.SIG
.dwExpiration
||
252 r1
->Data
.SIG
.dwTimeSigned
!= r2
->Data
.SIG
.dwTimeSigned
||
253 r1
->Data
.SIG
.wKeyTag
!= r2
->Data
.SIG
.wKeyTag
)
255 if (memcmp( r1
->Data
.SIG
.Signature
, r2
->Data
.SIG
.Signature
,
256 r1
->wDataLength
- sizeof(DNS_SIG_DATAA
) + 1 ))
262 if (r1
->Data
.ATMA
.AddressType
!= r2
->Data
.ATMA
.AddressType
)
264 for (i
= 0; i
< DNS_ATMA_MAX_ADDR_LENGTH
; i
++)
266 if (r1
->Data
.ATMA
.Address
[i
] != r2
->Data
.ATMA
.Address
[i
])
273 if (dns_strcmpX( r1
->Data
.NXT
.pNameNext
,
274 r2
->Data
.NXT
.pNameNext
, wide
)) return FALSE
;
275 if (r1
->Data
.NXT
.wNumTypes
!= r2
->Data
.NXT
.wNumTypes
) return FALSE
;
276 if (memcmp( r1
->Data
.NXT
.wTypes
, r2
->Data
.NXT
.wTypes
,
277 r1
->wDataLength
- sizeof(DNS_NXT_DATAA
) + sizeof(WORD
) ))
283 if (dns_strcmpX( r1
->Data
.SRV
.pNameTarget
,
284 r2
->Data
.SRV
.pNameTarget
, wide
)) return FALSE
;
285 if (r1
->Data
.SRV
.wPriority
!= r2
->Data
.SRV
.wPriority
||
286 r1
->Data
.SRV
.wWeight
!= r2
->Data
.SRV
.wWeight
||
287 r1
->Data
.SRV
.wPort
!= r2
->Data
.SRV
.wPort
)
293 if (dns_strcmpX( r1
->Data
.TKEY
.pNameAlgorithm
,
294 r2
->Data
.TKEY
.pNameAlgorithm
, wide
))
296 if (r1
->Data
.TKEY
.dwCreateTime
!= r2
->Data
.TKEY
.dwCreateTime
||
297 r1
->Data
.TKEY
.dwExpireTime
!= r2
->Data
.TKEY
.dwExpireTime
||
298 r1
->Data
.TKEY
.wMode
!= r2
->Data
.TKEY
.wMode
||
299 r1
->Data
.TKEY
.wError
!= r2
->Data
.TKEY
.wError
||
300 r1
->Data
.TKEY
.wKeyLength
!= r2
->Data
.TKEY
.wKeyLength
||
301 r1
->Data
.TKEY
.wOtherLength
!= r2
->Data
.TKEY
.wOtherLength
||
302 r1
->Data
.TKEY
.cAlgNameLength
!= r2
->Data
.TKEY
.cAlgNameLength
||
303 r1
->Data
.TKEY
.bPacketPointers
!= r2
->Data
.TKEY
.bPacketPointers
)
306 /* FIXME: ignoring pAlgorithmPacket field */
307 if (memcmp( r1
->Data
.TKEY
.pKey
, r2
->Data
.TKEY
.pKey
,
308 r1
->Data
.TKEY
.wKeyLength
) ||
309 memcmp( r1
->Data
.TKEY
.pOtherData
, r2
->Data
.TKEY
.pOtherData
,
310 r1
->Data
.TKEY
.wOtherLength
)) return FALSE
;
315 if (dns_strcmpX( r1
->Data
.TSIG
.pNameAlgorithm
,
316 r2
->Data
.TSIG
.pNameAlgorithm
, wide
))
318 if (r1
->Data
.TSIG
.i64CreateTime
!= r2
->Data
.TSIG
.i64CreateTime
||
319 r1
->Data
.TSIG
.wFudgeTime
!= r2
->Data
.TSIG
.wFudgeTime
||
320 r1
->Data
.TSIG
.wOriginalXid
!= r2
->Data
.TSIG
.wOriginalXid
||
321 r1
->Data
.TSIG
.wError
!= r2
->Data
.TSIG
.wError
||
322 r1
->Data
.TSIG
.wSigLength
!= r2
->Data
.TSIG
.wSigLength
||
323 r1
->Data
.TSIG
.wOtherLength
!= r2
->Data
.TSIG
.wOtherLength
||
324 r1
->Data
.TSIG
.cAlgNameLength
!= r2
->Data
.TSIG
.cAlgNameLength
||
325 r1
->Data
.TSIG
.bPacketPointers
!= r2
->Data
.TSIG
.bPacketPointers
)
328 /* FIXME: ignoring pAlgorithmPacket field */
329 if (memcmp( r1
->Data
.TSIG
.pSignature
, r2
->Data
.TSIG
.pSignature
,
330 r1
->Data
.TSIG
.wSigLength
) ||
331 memcmp( r1
->Data
.TSIG
.pOtherData
, r2
->Data
.TSIG
.pOtherData
,
332 r1
->Data
.TSIG
.wOtherLength
)) return FALSE
;
337 if (r1
->Data
.WINS
.dwMappingFlag
!= r2
->Data
.WINS
.dwMappingFlag
||
338 r1
->Data
.WINS
.dwLookupTimeout
!= r2
->Data
.WINS
.dwLookupTimeout
||
339 r1
->Data
.WINS
.dwCacheTimeout
!= r2
->Data
.WINS
.dwCacheTimeout
||
340 r1
->Data
.WINS
.cWinsServerCount
!= r2
->Data
.WINS
.cWinsServerCount
)
342 if (memcmp( r1
->Data
.WINS
.WinsServers
, r2
->Data
.WINS
.WinsServers
,
343 r1
->wDataLength
- sizeof(DNS_WINS_DATA
) + sizeof(IP4_ADDRESS
) ))
349 if (r1
->Data
.WINSR
.dwMappingFlag
!= r2
->Data
.WINSR
.dwMappingFlag
||
350 r1
->Data
.WINSR
.dwLookupTimeout
!= r2
->Data
.WINSR
.dwLookupTimeout
||
351 r1
->Data
.WINSR
.dwCacheTimeout
!= r2
->Data
.WINSR
.dwCacheTimeout
)
353 if (dns_strcmpX( r1
->Data
.WINSR
.pNameResultDomain
,
354 r2
->Data
.WINSR
.pNameResultDomain
, wide
))
359 FIXME( "unknown type: %s\n", dns_type_to_str( r1
->wType
) );
365 static LPVOID
dns_strcpyX( LPCVOID src
, DNS_CHARSET in
, DNS_CHARSET out
)
369 case DnsCharSetUnicode
:
373 case DnsCharSetUnicode
: return dns_strdup_w( src
);
374 case DnsCharSetUtf8
: return dns_strdup_wu( src
);
375 case DnsCharSetAnsi
: return dns_strdup_wa( src
);
377 WARN( "unhandled target charset: %d\n", out
);
385 case DnsCharSetUnicode
: return dns_strdup_uw( src
);
386 case DnsCharSetUtf8
: return dns_strdup_u( src
);
387 case DnsCharSetAnsi
: return dns_strdup_ua( src
);
389 WARN( "unhandled target charset: %d\n", out
);
396 case DnsCharSetUnicode
: return dns_strdup_aw( src
);
397 case DnsCharSetUtf8
: return dns_strdup_au( src
);
398 case DnsCharSetAnsi
: return dns_strdup_a( src
);
400 WARN( "unhandled target charset: %d\n", out
);
405 WARN( "unhandled source charset: %d\n", in
);
411 /******************************************************************************
412 * DnsRecordCopyEx [DNSAPI.@]
415 PDNS_RECORD WINAPI
DnsRecordCopyEx( PDNS_RECORD src
, DNS_CHARSET in
, DNS_CHARSET out
)
418 unsigned int i
, size
;
420 TRACE( "(%p,%d,%d)\n", src
, in
, out
);
422 size
= FIELD_OFFSET(DNS_RECORD
, Data
) + src
->wDataLength
;
423 dst
= heap_alloc_zero( size
);
424 if (!dst
) return NULL
;
426 memcpy( dst
, src
, size
);
428 if (src
->Flags
.S
.CharSet
== DnsCharSetUtf8
||
429 src
->Flags
.S
.CharSet
== DnsCharSetAnsi
||
430 src
->Flags
.S
.CharSet
== DnsCharSetUnicode
) in
= src
->Flags
.S
.CharSet
;
432 dst
->Flags
.S
.CharSet
= out
;
433 dst
->pName
= dns_strcpyX( src
->pName
, in
, out
);
434 if (!dst
->pName
) goto error
;
443 for (i
= 0; i
< src
->Data
.TXT
.dwStringCount
; i
++)
445 dst
->Data
.TXT
.pStringArray
[i
] =
446 dns_strcpyX( src
->Data
.TXT
.pStringArray
[i
], in
, out
);
448 if (!dst
->Data
.TXT
.pStringArray
[i
])
450 while (i
> 0) heap_free( dst
->Data
.TXT
.pStringArray
[--i
] );
459 dst
->Data
.MINFO
.pNameMailbox
=
460 dns_strcpyX( src
->Data
.MINFO
.pNameMailbox
, in
, out
);
461 if (!dst
->Data
.MINFO
.pNameMailbox
) goto error
;
463 dst
->Data
.MINFO
.pNameErrorsMailbox
=
464 dns_strcpyX( src
->Data
.MINFO
.pNameErrorsMailbox
, in
, out
);
465 if (!dst
->Data
.MINFO
.pNameErrorsMailbox
)
467 heap_free( dst
->Data
.MINFO
.pNameMailbox
);
476 dst
->Data
.MX
.pNameExchange
=
477 dns_strcpyX( src
->Data
.MX
.pNameExchange
, in
, out
);
478 if (!dst
->Data
.MX
.pNameExchange
) goto error
;
483 dst
->Data
.NXT
.pNameNext
=
484 dns_strcpyX( src
->Data
.NXT
.pNameNext
, in
, out
);
485 if (!dst
->Data
.NXT
.pNameNext
) goto error
;
497 dst
->Data
.PTR
.pNameHost
=
498 dns_strcpyX( src
->Data
.PTR
.pNameHost
, in
, out
);
499 if (!dst
->Data
.PTR
.pNameHost
) goto error
;
504 dst
->Data
.SIG
.pNameSigner
=
505 dns_strcpyX( src
->Data
.SIG
.pNameSigner
, in
, out
);
506 if (!dst
->Data
.SIG
.pNameSigner
) goto error
;
511 dst
->Data
.SOA
.pNamePrimaryServer
=
512 dns_strcpyX( src
->Data
.SOA
.pNamePrimaryServer
, in
, out
);
513 if (!dst
->Data
.SOA
.pNamePrimaryServer
) goto error
;
515 dst
->Data
.SOA
.pNameAdministrator
=
516 dns_strcpyX( src
->Data
.SOA
.pNameAdministrator
, in
, out
);
517 if (!dst
->Data
.SOA
.pNameAdministrator
)
519 heap_free( dst
->Data
.SOA
.pNamePrimaryServer
);
526 dst
->Data
.SRV
.pNameTarget
=
527 dns_strcpyX( src
->Data
.SRV
.pNameTarget
, in
, out
);
528 if (!dst
->Data
.SRV
.pNameTarget
) goto error
;
537 heap_free( dst
->pName
);
542 /******************************************************************************
543 * DnsRecordListFree [DNSAPI.@]
546 VOID WINAPI
DnsRecordListFree( PDNS_RECORD list
, DNS_FREE_TYPE type
)
548 DNS_RECORD
*r
, *next
;
551 TRACE( "(%p,%d)\n", list
, type
);
557 case DnsFreeRecordList
:
559 for (r
= list
; (list
= r
); r
= next
)
561 heap_free( r
->pName
);
570 for (i
= 0; i
< r
->Data
.TXT
.dwStringCount
; i
++)
571 heap_free( r
->Data
.TXT
.pStringArray
[i
] );
578 heap_free( r
->Data
.MINFO
.pNameMailbox
);
579 heap_free( r
->Data
.MINFO
.pNameErrorsMailbox
);
586 heap_free( r
->Data
.MX
.pNameExchange
);
591 heap_free( r
->Data
.NXT
.pNameNext
);
603 heap_free( r
->Data
.PTR
.pNameHost
);
608 heap_free( r
->Data
.SIG
.pNameSigner
);
613 heap_free( r
->Data
.SOA
.pNamePrimaryServer
);
614 heap_free( r
->Data
.SOA
.pNameAdministrator
);
619 heap_free( r
->Data
.SRV
.pNameTarget
);
632 case DnsFreeParsedMessageFields
:
634 FIXME( "unhandled free type: %d\n", type
);
638 WARN( "unknown free type: %d\n", type
);
643 /******************************************************************************
647 void WINAPI
DnsFree( PVOID data
, DNS_FREE_TYPE type
)
649 DnsRecordListFree( data
, type
);
652 /******************************************************************************
653 * DnsRecordSetCompare [DNSAPI.@]
656 BOOL WINAPI
DnsRecordSetCompare( PDNS_RECORD set1
, PDNS_RECORD set2
,
657 PDNS_RECORD
*diff1
, PDNS_RECORD
*diff2
)
660 DNS_RECORD
*r
, *t
, *u
;
663 TRACE( "(%p,%p,%p,%p)\n", set1
, set2
, diff1
, diff2
);
665 if (!set1
&& !set2
) return FALSE
;
667 if (diff1
) *diff1
= NULL
;
668 if (diff2
) *diff2
= NULL
;
672 if (diff1
) *diff1
= DnsRecordSetCopyEx( set1
, 0, set1
->Flags
.S
.CharSet
);
677 if (diff2
) *diff2
= DnsRecordSetCopyEx( set2
, 0, set2
->Flags
.S
.CharSet
);
681 DNS_RRSET_INIT( rr1
);
682 DNS_RRSET_INIT( rr2
);
684 for (r
= set1
; r
; r
= r
->pNext
)
686 for (t
= set2
; t
; t
= t
->pNext
)
688 u
= DnsRecordCopyEx( r
, r
->Flags
.S
.CharSet
, t
->Flags
.S
.CharSet
);
691 if (!DnsRecordCompare( t
, u
))
693 DNS_RRSET_ADD( rr1
, u
);
696 else DnsRecordListFree( u
, DnsFreeRecordList
);
700 for (t
= set2
; t
; t
= t
->pNext
)
702 for (r
= set1
; r
; r
= r
->pNext
)
704 u
= DnsRecordCopyEx( t
, t
->Flags
.S
.CharSet
, r
->Flags
.S
.CharSet
);
707 if (!DnsRecordCompare( r
, u
))
709 DNS_RRSET_ADD( rr2
, u
);
712 else DnsRecordListFree( u
, DnsFreeRecordList
);
716 DNS_RRSET_TERMINATE( rr1
);
717 DNS_RRSET_TERMINATE( rr2
);
719 if (diff1
) *diff1
= rr1
.pFirstRR
;
720 else DnsRecordListFree( rr1
.pFirstRR
, DnsFreeRecordList
);
722 if (diff2
) *diff2
= rr2
.pFirstRR
;
723 else DnsRecordListFree( rr2
.pFirstRR
, DnsFreeRecordList
);
728 DNS_RRSET_TERMINATE( rr1
);
729 DNS_RRSET_TERMINATE( rr2
);
731 DnsRecordListFree( rr1
.pFirstRR
, DnsFreeRecordList
);
732 DnsRecordListFree( rr2
.pFirstRR
, DnsFreeRecordList
);
737 /******************************************************************************
738 * DnsRecordSetCopyEx [DNSAPI.@]
741 PDNS_RECORD WINAPI
DnsRecordSetCopyEx( PDNS_RECORD src_set
, DNS_CHARSET in
, DNS_CHARSET out
)
744 DNS_RECORD
*src
, *dst
;
746 TRACE( "(%p,%d,%d)\n", src_set
, in
, out
);
748 DNS_RRSET_INIT( dst_set
);
750 for (src
= src_set
; (src_set
= src
); src
= src_set
->pNext
)
752 dst
= DnsRecordCopyEx( src
, in
, out
);
755 DNS_RRSET_TERMINATE( dst_set
);
756 DnsRecordListFree( dst_set
.pFirstRR
, DnsFreeRecordList
);
759 DNS_RRSET_ADD( dst_set
, dst
);
762 DNS_RRSET_TERMINATE( dst_set
);
763 return dst_set
.pFirstRR
;
766 /******************************************************************************
767 * DnsRecordSetDetach [DNSAPI.@]
770 PDNS_RECORD WINAPI
DnsRecordSetDetach( PDNS_RECORD set
)
774 TRACE( "(%p)\n", set
);
776 for (r
= set
; (set
= r
); r
= set
->pNext
)
778 if (r
->pNext
&& !r
->pNext
->pNext
)