104
104
#include "ipv6-internal.h"
105
105
#include "util-internal.h"
106
106
#include "evthread-internal.h"
107
+ #include "evdns-internal.h"
107
108
#ifdef _WIN32
108
109
#include <ctype.h>
109
110
#include <winsock2.h>
@@ -4621,21 +4622,20 @@ evdns_nameserver_ip_add_line(struct evdns_base *base, const char *ips) {
4621
4622
return 0 ;
4622
4623
}
4623
4624
4624
- typedef DWORD (WINAPI * GetNetworkParams_fn_t )( FIXED_INFO * , DWORD * );
4625
+ typedef DWORD (WINAPI * GetAdaptersAddresses_fn_t )( ULONG , ULONG , PVOID , PIP_ADAPTER_ADDRESSES , PULONG );
4625
4626
4626
- /* Use the windows GetNetworkParams interface in iphlpapi.dll to */
4627
+ /* Use the windows GetAdaptersAddresses interface in iphlpapi.dll to */
4627
4628
/* figure out what our nameservers are. */
4628
4629
static int
4629
- load_nameservers_with_getnetworkparams (struct evdns_base * base )
4630
+ load_nameservers_with_getadaptersaddresses_unlocked (struct evdns_base * base )
4630
4631
{
4631
- /* Based on MSDN examples and inspection of c-ares code. */
4632
- FIXED_INFO * fixed ;
4632
+ PIP_ADAPTER_ADDRESSES addresses = NULL ;
4633
4633
HMODULE handle = 0 ;
4634
- ULONG size = sizeof (FIXED_INFO );
4634
+ ULONG size = sizeof (IP_ADAPTER_ADDRESSES );
4635
4635
void * buf = NULL ;
4636
- int status = 0 , r , added_any ;
4637
- IP_ADDR_STRING * ns ;
4638
- GetNetworkParams_fn_t fn ;
4636
+ int status = 0 , r , added_any = 0 ;
4637
+ GetAdaptersAddresses_fn_t fn ;
4638
+ IP_ADAPTER_DNS_SERVER_ADDRESS * dnsserver = NULL ;
4639
4639
4640
4640
ASSERT_LOCKED (base );
4641
4641
if (!(handle = evutil_load_windows_system_library_ (
@@ -4644,48 +4644,59 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base)
4644
4644
status = -1 ;
4645
4645
goto done ;
4646
4646
}
4647
- if (!(fn = (GetNetworkParams_fn_t ) GetProcAddress (handle , "GetNetworkParams " ))) {
4647
+ if (!(fn = (GetAdaptersAddresses_fn_t ) GetProcAddress (handle , "GetAdaptersAddresses " ))) {
4648
4648
log (EVDNS_LOG_WARN , "Could not get address of function." );
4649
4649
status = -1 ;
4650
4650
goto done ;
4651
4651
}
4652
4652
4653
4653
buf = mm_malloc (size );
4654
4654
if (!buf ) { status = 4 ; goto done ; }
4655
- fixed = buf ;
4656
- r = fn (fixed , & size );
4657
- if (r != ERROR_SUCCESS && r != ERROR_BUFFER_OVERFLOW ) {
4655
+ addresses = buf ;
4656
+ r = fn (AF_UNSPEC , GAA_FLAG_INCLUDE_PREFIX , NULL , addresses , & size );
4657
+ if (r != NO_ERROR && r != ERROR_BUFFER_OVERFLOW ) {
4658
4658
status = -1 ;
4659
4659
goto done ;
4660
4660
}
4661
- if (r != ERROR_SUCCESS ) {
4661
+ if (r != NO_ERROR ) {
4662
4662
mm_free (buf );
4663
4663
buf = mm_malloc (size );
4664
4664
if (!buf ) { status = 4 ; goto done ; }
4665
- fixed = buf ;
4666
- r = fn (fixed , & size );
4667
- if (r != ERROR_SUCCESS ) {
4665
+ addresses = buf ;
4666
+ r = fn (AF_UNSPEC , GAA_FLAG_INCLUDE_PREFIX , NULL , addresses , & size );
4667
+ if (r != NO_ERROR ) {
4668
4668
log (EVDNS_LOG_DEBUG , "fn() failed." );
4669
4669
status = -1 ;
4670
4670
goto done ;
4671
4671
}
4672
4672
}
4673
4673
4674
- EVUTIL_ASSERT (fixed );
4675
- added_any = 0 ;
4676
- ns = & (fixed -> DnsServerList );
4677
- while (ns ) {
4678
- r = evdns_nameserver_ip_add_line (base , ns -> IpAddress .String );
4679
- if (r ) {
4680
- log (EVDNS_LOG_DEBUG ,"Could not add nameserver %s to list,error: %d" ,
4681
- (ns -> IpAddress .String ),(int )GetLastError ());
4682
- status = r ;
4683
- } else {
4684
- ++ added_any ;
4685
- log (EVDNS_LOG_DEBUG ,"Successfully added %s as nameserver" ,ns -> IpAddress .String );
4686
- }
4674
+ while (addresses ) {
4675
+ dnsserver = addresses -> FirstDnsServerAddress ;
4676
+ while (dnsserver && (addresses -> OperStatus == IfOperStatusUp )) {
4677
+ char ip [INET6_ADDRSTRLEN ] = {0 };
4678
+ if (AF_INET == dnsserver -> Address .lpSockaddr -> sa_family ) {
4679
+ inet_ntop (AF_INET , & ((SOCKADDR_IN * )dnsserver -> Address .lpSockaddr )-> sin_addr , ip , sizeof (ip ));
4680
+ } else if (AF_INET6 == dnsserver -> Address .lpSockaddr -> sa_family ) {
4681
+ inet_ntop (AF_INET6 , & ((SOCKADDR_IN6 * )dnsserver -> Address .lpSockaddr )-> sin6_addr , ip , sizeof (ip ));
4682
+ }
4687
4683
4688
- ns = ns -> Next ;
4684
+ dnsserver = dnsserver -> Next ;
4685
+ if (strncmp (ip , "fec0:" , 5 ) == 0 ) { /* remove ipv6 reserved address */
4686
+ continue ;
4687
+ }
4688
+
4689
+ r = evdns_base_nameserver_ip_add (base , ip );
4690
+ if (r ) {
4691
+ log (EVDNS_LOG_DEBUG , "Could not add nameserver %s to list, error: %d" , ip , r );
4692
+ status = r ;
4693
+ } else {
4694
+ ++ added_any ;
4695
+ log (EVDNS_LOG_DEBUG , "Successfully added %s as nameserver" , ip );
4696
+ }
4697
+ }
4698
+
4699
+ addresses = addresses -> Next ;
4689
4700
}
4690
4701
4691
4702
if (!added_any ) {
@@ -4704,6 +4715,16 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base)
4704
4715
return status ;
4705
4716
}
4706
4717
4718
+ int
4719
+ load_nameservers_with_getadaptersaddresses (struct evdns_base * base )
4720
+ {
4721
+ int r ;
4722
+ EVDNS_LOCK (base );
4723
+ r = load_nameservers_with_getadaptersaddresses_unlocked (base );
4724
+ EVDNS_UNLOCK (base );
4725
+ return r ;
4726
+ }
4727
+
4707
4728
static int
4708
4729
config_nameserver_from_reg_key (struct evdns_base * base , HKEY key , const TCHAR * subkey )
4709
4730
{
@@ -4803,7 +4824,7 @@ evdns_base_config_windows_nameservers(struct evdns_base *base)
4803
4824
if (fname )
4804
4825
mm_free (fname );
4805
4826
4806
- if (load_nameservers_with_getnetworkparams (base ) == 0 ) {
4827
+ if (load_nameservers_with_getadaptersaddresses_unlocked (base ) == 0 ) {
4807
4828
EVDNS_UNLOCK (base );
4808
4829
return 0 ;
4809
4830
}
0 commit comments