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
);
384 case DnsCharSetUnicode
: return dns_strdup_uw( src
);
385 case DnsCharSetUtf8
: return dns_strdup_u( src
);
386 case DnsCharSetAnsi
: return dns_strdup_ua( src
);
388 WARN( "unhandled target charset: %d\n", out
);
395 case DnsCharSetUnicode
: return dns_strdup_aw( src
);
396 case DnsCharSetUtf8
: return dns_strdup_au( src
);
397 case DnsCharSetAnsi
: return dns_strdup_a( src
);
399 WARN( "unhandled target charset: %d\n", out
);
404 WARN( "unhandled source charset: %d\n", in
);
410 /******************************************************************************
411 * DnsRecordCopyEx [DNSAPI.@]
414 PDNS_RECORD WINAPI
DnsRecordCopyEx( PDNS_RECORD src
, DNS_CHARSET in
, DNS_CHARSET out
)
417 unsigned int i
, size
;
419 TRACE( "(%p,%d,%d)\n", src
, in
, out
);
421 size
= FIELD_OFFSET(DNS_RECORD
, Data
) + src
->wDataLength
;
422 dst
= heap_alloc_zero( size
);
423 if (!dst
) return NULL
;
425 memcpy( dst
, src
, size
);
427 if (src
->Flags
.S
.CharSet
== DnsCharSetUtf8
||
428 src
->Flags
.S
.CharSet
== DnsCharSetAnsi
||
429 src
->Flags
.S
.CharSet
== DnsCharSetUnicode
) in
= src
->Flags
.S
.CharSet
;
431 dst
->Flags
.S
.CharSet
= out
;
432 dst
->pName
= dns_strcpyX( src
->pName
, in
, out
);
433 if (!dst
->pName
) goto error
;
442 for (i
= 0; i
< src
->Data
.TXT
.dwStringCount
; i
++)
444 dst
->Data
.TXT
.pStringArray
[i
] =
445 dns_strcpyX( src
->Data
.TXT
.pStringArray
[i
], in
, out
);
447 if (!dst
->Data
.TXT
.pStringArray
[i
])
449 while (i
> 0) heap_free( dst
->Data
.TXT
.pStringArray
[--i
] );
458 dst
->Data
.MINFO
.pNameMailbox
=
459 dns_strcpyX( src
->Data
.MINFO
.pNameMailbox
, in
, out
);
460 if (!dst
->Data
.MINFO
.pNameMailbox
) goto error
;
462 dst
->Data
.MINFO
.pNameErrorsMailbox
=
463 dns_strcpyX( src
->Data
.MINFO
.pNameErrorsMailbox
, in
, out
);
464 if (!dst
->Data
.MINFO
.pNameErrorsMailbox
)
466 heap_free( dst
->Data
.MINFO
.pNameMailbox
);
475 dst
->Data
.MX
.pNameExchange
=
476 dns_strcpyX( src
->Data
.MX
.pNameExchange
, in
, out
);
477 if (!dst
->Data
.MX
.pNameExchange
) goto error
;
482 dst
->Data
.NXT
.pNameNext
=
483 dns_strcpyX( src
->Data
.NXT
.pNameNext
, in
, out
);
484 if (!dst
->Data
.NXT
.pNameNext
) goto error
;
496 dst
->Data
.PTR
.pNameHost
=
497 dns_strcpyX( src
->Data
.PTR
.pNameHost
, in
, out
);
498 if (!dst
->Data
.PTR
.pNameHost
) goto error
;
503 dst
->Data
.SIG
.pNameSigner
=
504 dns_strcpyX( src
->Data
.SIG
.pNameSigner
, in
, out
);
505 if (!dst
->Data
.SIG
.pNameSigner
) goto error
;
510 dst
->Data
.SOA
.pNamePrimaryServer
=
511 dns_strcpyX( src
->Data
.SOA
.pNamePrimaryServer
, in
, out
);
512 if (!dst
->Data
.SOA
.pNamePrimaryServer
) goto error
;
514 dst
->Data
.SOA
.pNameAdministrator
=
515 dns_strcpyX( src
->Data
.SOA
.pNameAdministrator
, in
, out
);
516 if (!dst
->Data
.SOA
.pNameAdministrator
)
518 heap_free( dst
->Data
.SOA
.pNamePrimaryServer
);
525 dst
->Data
.SRV
.pNameTarget
=
526 dns_strcpyX( src
->Data
.SRV
.pNameTarget
, in
, out
);
527 if (!dst
->Data
.SRV
.pNameTarget
) goto error
;
536 heap_free( dst
->pName
);
541 /******************************************************************************
542 * DnsRecordListFree [DNSAPI.@]
545 VOID WINAPI
DnsRecordListFree( PDNS_RECORD list
, DNS_FREE_TYPE type
)
547 DNS_RECORD
*r
, *next
;
550 TRACE( "(%p,%d)\n", list
, type
);
556 case DnsFreeRecordList
:
558 for (r
= list
; (list
= r
); r
= next
)
560 heap_free( r
->pName
);
569 for (i
= 0; i
< r
->Data
.TXT
.dwStringCount
; i
++)
570 heap_free( r
->Data
.TXT
.pStringArray
[i
] );
577 heap_free( r
->Data
.MINFO
.pNameMailbox
);
578 heap_free( r
->Data
.MINFO
.pNameErrorsMailbox
);
585 heap_free( r
->Data
.MX
.pNameExchange
);
590 heap_free( r
->Data
.NXT
.pNameNext
);
602 heap_free( r
->Data
.PTR
.pNameHost
);
607 heap_free( r
->Data
.SIG
.pNameSigner
);
612 heap_free( r
->Data
.SOA
.pNamePrimaryServer
);
613 heap_free( r
->Data
.SOA
.pNameAdministrator
);
618 heap_free( r
->Data
.SRV
.pNameTarget
);
631 case DnsFreeParsedMessageFields
:
633 FIXME( "unhandled free type: %d\n", type
);
637 WARN( "unknown free type: %d\n", type
);
642 /******************************************************************************
646 void WINAPI
DnsFree( PVOID data
, DNS_FREE_TYPE type
)
648 DnsRecordListFree( data
, type
);
651 /******************************************************************************
652 * DnsRecordSetCompare [DNSAPI.@]
655 BOOL WINAPI
DnsRecordSetCompare( PDNS_RECORD set1
, PDNS_RECORD set2
,
656 PDNS_RECORD
*diff1
, PDNS_RECORD
*diff2
)
659 DNS_RECORD
*r
, *t
, *u
;
662 TRACE( "(%p,%p,%p,%p)\n", set1
, set2
, diff1
, diff2
);
664 if (!set1
&& !set2
) return FALSE
;
666 if (diff1
) *diff1
= NULL
;
667 if (diff2
) *diff2
= NULL
;
671 if (diff1
) *diff1
= DnsRecordSetCopyEx( set1
, 0, set1
->Flags
.S
.CharSet
);
676 if (diff2
) *diff2
= DnsRecordSetCopyEx( set2
, 0, set2
->Flags
.S
.CharSet
);
680 DNS_RRSET_INIT( rr1
);
681 DNS_RRSET_INIT( rr2
);
683 for (r
= set1
; r
; r
= r
->pNext
)
685 for (t
= set2
; t
; t
= t
->pNext
)
687 u
= DnsRecordCopyEx( r
, r
->Flags
.S
.CharSet
, t
->Flags
.S
.CharSet
);
690 if (!DnsRecordCompare( t
, u
))
692 DNS_RRSET_ADD( rr1
, u
);
695 else DnsRecordListFree( u
, DnsFreeRecordList
);
699 for (t
= set2
; t
; t
= t
->pNext
)
701 for (r
= set1
; r
; r
= r
->pNext
)
703 u
= DnsRecordCopyEx( t
, t
->Flags
.S
.CharSet
, r
->Flags
.S
.CharSet
);
706 if (!DnsRecordCompare( r
, u
))
708 DNS_RRSET_ADD( rr2
, u
);
711 else DnsRecordListFree( u
, DnsFreeRecordList
);
715 DNS_RRSET_TERMINATE( rr1
);
716 DNS_RRSET_TERMINATE( rr2
);
718 if (diff1
) *diff1
= rr1
.pFirstRR
;
719 else DnsRecordListFree( rr1
.pFirstRR
, DnsFreeRecordList
);
721 if (diff2
) *diff2
= rr2
.pFirstRR
;
722 else DnsRecordListFree( rr2
.pFirstRR
, DnsFreeRecordList
);
727 DNS_RRSET_TERMINATE( rr1
);
728 DNS_RRSET_TERMINATE( rr2
);
730 DnsRecordListFree( rr1
.pFirstRR
, DnsFreeRecordList
);
731 DnsRecordListFree( rr2
.pFirstRR
, DnsFreeRecordList
);
736 /******************************************************************************
737 * DnsRecordSetCopyEx [DNSAPI.@]
740 PDNS_RECORD WINAPI
DnsRecordSetCopyEx( PDNS_RECORD src_set
, DNS_CHARSET in
, DNS_CHARSET out
)
743 DNS_RECORD
*src
, *dst
;
745 TRACE( "(%p,%d,%d)\n", src_set
, in
, out
);
747 DNS_RRSET_INIT( dst_set
);
749 for (src
= src_set
; (src_set
= src
); src
= src_set
->pNext
)
751 dst
= DnsRecordCopyEx( src
, in
, out
);
754 DNS_RRSET_TERMINATE( dst_set
);
755 DnsRecordListFree( dst_set
.pFirstRR
, DnsFreeRecordList
);
758 DNS_RRSET_ADD( dst_set
, dst
);
761 DNS_RRSET_TERMINATE( dst_set
);
762 return dst_set
.pFirstRR
;
765 /******************************************************************************
766 * DnsRecordSetDetach [DNSAPI.@]
769 PDNS_RECORD WINAPI
DnsRecordSetDetach( PDNS_RECORD set
)
773 TRACE( "(%p)\n", set
);
775 for (r
= set
; (set
= r
); r
= set
->pNext
)
777 if (r
->pNext
&& !r
->pNext
->pNext
)