|
|
|
|
|
|
|
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
|
|
* Copyright (C) 2004-2009 by Daniel Stenberg
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose and without
|
|
|
|
* fee is hereby granted, provided that the above copyright
|
|
|
|
* notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of M.I.T. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission.
|
|
|
|
* M.I.T. makes no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ares_setup.h"
|
|
|
|
|
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_library_init.h"
|
|
|
|
#include "ares_private.h"
|
|
|
|
|
|
|
|
/* library-private global and unique instance vars */
|
|
|
|
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
fpGetNetworkParams_t ares_fpGetNetworkParams = ZERO_NULL;
|
|
|
|
fpSystemFunction036_t ares_fpSystemFunction036 = ZERO_NULL;
|
|
|
|
fpGetAdaptersAddresses_t ares_fpGetAdaptersAddresses = ZERO_NULL;
|
Windows DNS server sorting (#81)
Original Patch From Brad Spencer:
https://c-ares.haxx.se/mail/c-ares-archive-2016-04/0000.shtml
My modifications include:
* Dynamically find GetBestRoute2 since it is a Windows Vista+ symbol, and will fall back to prior behavior when not available.
* Prefer get_DNS_AdaptersAddresses as the modifications should alleviate the concerns which caused us to prefer get_DNS_NetworkParams
* Update AppVeyor to use MinGW-w64 instead of the legacy MinGW
* Fix compile error in test suite for Windows.
Original message from patch below:
From: Brad Spencer <bspencer@blackberry.com>
Date: Fri, 29 Apr 2016 14:26:23 -0300
On Windows, the c-ares DNS resolver tries first to get a full list of
DNS server addresses by enumerating the system's IPv4/v6 interfaces and
then getting the per-interface DNS server lists from those interfaces
and joining them together. The OS, at least in the way the c-ares
prefers to query them (which also may be the only or best way in some
environments), does not provide a unified list of DNS servers ordered
according to "current network conditions". Currently, c-ares will then
try to use them in whatever order the nested enumeration produces, which
may result in DNS requests being sent to servers on one interface
(hosting the current default route, for example) that are only intended
to be used via another interface (intended to be used when the first
interface is not available, for example). This, in turn, can lead to
spurious failures and timeouts simply because of the server address
order that resulted because of the enumeration process.
This patch makes the (safe?) assumption that there is no other better
rule to chose which interface's DNS server list should be prioritized.
After all, a DNS lookup isn't something "per network"; applications
don't look up "these DNS names on this interface and those DNS names on
that interface". There is a single resource pool of DNS servers and the
application should presume that any server will give it the "right"
answer. However, even if all DNS servers are assumed to give equally
useful responses, it is reasonable to expect that some DNS servers will
not accept requests on all interfaces. This patch avoids the problem by
sorting the DNS server addresses using the Windows IPv4/v6 routing tables.
For example, a request to DNS server C on interface 2 that is actually
sent over interface 1 (which may happen to have the default route) may
be rejected by or not delivered to DNS server C. So, better to use DNS
servers A and B associated with interface 1, at least as a first try.
By using the metric of the route to the DNS server itself as a proxy for
priority of the DNS server in the list, this patch is able to adapt
dynamically to changes in the interface list, the DNS server lists per
interface, which interfaces are active, the routing table, and so on,
while always picking a good "best" DNS server first.
In cases where any DNS server on any interface will do, this patch still
seems useful because it will prioritize a lower-metric route's (and thus
interface's) servers.
8 years ago
|
|
|
fpGetBestRoute2_t ares_fpGetBestRoute2 = ZERO_NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* library-private global vars with source visibility restricted to this file */
|
|
|
|
|
|
|
|
static unsigned int ares_initialized;
|
|
|
|
static int ares_init_flags;
|
|
|
|
|
|
|
|
/* library-private global vars with visibility across the whole library */
|
|
|
|
void *(*ares_malloc)(size_t size) = malloc;
|
|
|
|
void *(*ares_realloc)(void *ptr, size_t size) = realloc;
|
|
|
|
void (*ares_free)(void *ptr) = free;
|
|
|
|
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
static HMODULE hnd_iphlpapi;
|
|
|
|
static HMODULE hnd_advapi32;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static int ares_win32_init(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
|
|
|
|
hnd_iphlpapi = 0;
|
|
|
|
hnd_iphlpapi = LoadLibraryW(L"iphlpapi.dll");
|
|
|
|
if (!hnd_iphlpapi)
|
|
|
|
return ARES_ELOADIPHLPAPI;
|
|
|
|
|
|
|
|
ares_fpGetNetworkParams = (fpGetNetworkParams_t)
|
|
|
|
GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
|
|
|
|
if (!ares_fpGetNetworkParams)
|
|
|
|
{
|
|
|
|
FreeLibrary(hnd_iphlpapi);
|
|
|
|
return ARES_EADDRGETNETWORKPARAMS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ares_fpGetAdaptersAddresses = (fpGetAdaptersAddresses_t)
|
|
|
|
GetProcAddress(hnd_iphlpapi, "GetAdaptersAddresses");
|
|
|
|
if (!ares_fpGetAdaptersAddresses)
|
|
|
|
{
|
|
|
|
/* This can happen on clients before WinXP, I don't
|
|
|
|
think it should be an error, unless we don't want to
|
|
|
|
support Windows 2000 anymore */
|
|
|
|
}
|
|
|
|
|
Windows DNS server sorting (#81)
Original Patch From Brad Spencer:
https://c-ares.haxx.se/mail/c-ares-archive-2016-04/0000.shtml
My modifications include:
* Dynamically find GetBestRoute2 since it is a Windows Vista+ symbol, and will fall back to prior behavior when not available.
* Prefer get_DNS_AdaptersAddresses as the modifications should alleviate the concerns which caused us to prefer get_DNS_NetworkParams
* Update AppVeyor to use MinGW-w64 instead of the legacy MinGW
* Fix compile error in test suite for Windows.
Original message from patch below:
From: Brad Spencer <bspencer@blackberry.com>
Date: Fri, 29 Apr 2016 14:26:23 -0300
On Windows, the c-ares DNS resolver tries first to get a full list of
DNS server addresses by enumerating the system's IPv4/v6 interfaces and
then getting the per-interface DNS server lists from those interfaces
and joining them together. The OS, at least in the way the c-ares
prefers to query them (which also may be the only or best way in some
environments), does not provide a unified list of DNS servers ordered
according to "current network conditions". Currently, c-ares will then
try to use them in whatever order the nested enumeration produces, which
may result in DNS requests being sent to servers on one interface
(hosting the current default route, for example) that are only intended
to be used via another interface (intended to be used when the first
interface is not available, for example). This, in turn, can lead to
spurious failures and timeouts simply because of the server address
order that resulted because of the enumeration process.
This patch makes the (safe?) assumption that there is no other better
rule to chose which interface's DNS server list should be prioritized.
After all, a DNS lookup isn't something "per network"; applications
don't look up "these DNS names on this interface and those DNS names on
that interface". There is a single resource pool of DNS servers and the
application should presume that any server will give it the "right"
answer. However, even if all DNS servers are assumed to give equally
useful responses, it is reasonable to expect that some DNS servers will
not accept requests on all interfaces. This patch avoids the problem by
sorting the DNS server addresses using the Windows IPv4/v6 routing tables.
For example, a request to DNS server C on interface 2 that is actually
sent over interface 1 (which may happen to have the default route) may
be rejected by or not delivered to DNS server C. So, better to use DNS
servers A and B associated with interface 1, at least as a first try.
By using the metric of the route to the DNS server itself as a proxy for
priority of the DNS server in the list, this patch is able to adapt
dynamically to changes in the interface list, the DNS server lists per
interface, which interfaces are active, the routing table, and so on,
while always picking a good "best" DNS server first.
In cases where any DNS server on any interface will do, this patch still
seems useful because it will prioritize a lower-metric route's (and thus
interface's) servers.
8 years ago
|
|
|
ares_fpGetBestRoute2 = (fpGetBestRoute2_t)
|
|
|
|
GetProcAddress(hnd_iphlpapi, "GetBestRoute2");
|
|
|
|
if (!ares_fpGetBestRoute2)
|
|
|
|
{
|
|
|
|
/* This can happen on clients before Vista, I don't
|
|
|
|
think it should be an error, unless we don't want to
|
|
|
|
support Windows XP anymore */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When advapi32.dll is unavailable or advapi32.dll has no SystemFunction036,
|
|
|
|
* also known as RtlGenRandom, which is the case for Windows versions prior
|
|
|
|
* to WinXP then c-ares uses portable rand() function. Then don't error here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
hnd_advapi32 = 0;
|
|
|
|
hnd_advapi32 = LoadLibraryW(L"advapi32.dll");
|
|
|
|
if (hnd_advapi32)
|
|
|
|
{
|
|
|
|
ares_fpSystemFunction036 = (fpSystemFunction036_t)
|
|
|
|
GetProcAddress(hnd_advapi32, "SystemFunction036");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ares_win32_cleanup(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
if (hnd_advapi32)
|
|
|
|
FreeLibrary(hnd_advapi32);
|
|
|
|
if (hnd_iphlpapi)
|
|
|
|
FreeLibrary(hnd_iphlpapi);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ares_library_init(int flags)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (ares_initialized)
|
|
|
|
{
|
|
|
|
ares_initialized++;
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
|
|
|
ares_initialized++;
|
|
|
|
|
|
|
|
if (flags & ARES_LIB_INIT_WIN32)
|
|
|
|
{
|
|
|
|
res = ares_win32_init();
|
|
|
|
if (res != ARES_SUCCESS)
|
|
|
|
return res; /* LCOV_EXCL_LINE: can't test Win32 init failure */
|
|
|
|
}
|
|
|
|
|
|
|
|
ares_init_flags = flags;
|
|
|
|
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ares_library_init_mem(int flags,
|
|
|
|
void *(*amalloc)(size_t size),
|
|
|
|
void (*afree)(void *ptr),
|
|
|
|
void *(*arealloc)(void *ptr, size_t size))
|
|
|
|
{
|
|
|
|
if (amalloc)
|
|
|
|
ares_malloc = amalloc;
|
|
|
|
if (arealloc)
|
|
|
|
ares_realloc = arealloc;
|
|
|
|
if (afree)
|
|
|
|
ares_free = afree;
|
|
|
|
return ares_library_init(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ares_library_cleanup(void)
|
|
|
|
{
|
|
|
|
if (!ares_initialized)
|
|
|
|
return;
|
|
|
|
ares_initialized--;
|
|
|
|
if (ares_initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ares_init_flags & ARES_LIB_INIT_WIN32)
|
|
|
|
ares_win32_cleanup();
|
|
|
|
|
|
|
|
ares_init_flags = ARES_LIB_INIT_NONE;
|
|
|
|
ares_malloc = malloc;
|
|
|
|
ares_realloc = realloc;
|
|
|
|
ares_free = free;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ares_library_initialized(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
if (!ares_initialized)
|
|
|
|
return ARES_ENOTINITIALIZED;
|
|
|
|
#endif
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|