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.
11 files changed
tree: cbbcd8a75676bfa9eda3bc3c3bc995660f8eafd6
  1. m4/
  2. test/
  3. travis/
  4. vc/
  5. .gitignore
  6. .travis.yml
  7. acinclude.m4
  8. acountry.1
  9. acountry.c
  10. adig.1
  11. adig.c
  12. ahost.1
  13. ahost.c
  14. appveyor.yml
  15. ares.h
  16. ares__close_sockets.c
  17. ares__get_hostent.c
  18. ares__read_line.c
  19. ares__timeval.c
  20. ares_build.h.cmake
  21. ares_build.h.dist
  22. ares_build.h.in
  23. ares_cancel.3
  24. ares_cancel.c
  25. ares_config.h.cmake
  26. ares_create_query.3
  27. ares_create_query.c
  28. ares_data.c
  29. ares_data.h
  30. ares_destroy.3
  31. ares_destroy.c
  32. ares_destroy_options.3
  33. ares_dns.h
  34. ares_dup.3
  35. ares_expand_name.3
  36. ares_expand_name.c
  37. ares_expand_string.3
  38. ares_expand_string.c
  39. ares_fds.3
  40. ares_fds.c
  41. ares_free_data.3
  42. ares_free_hostent.3
  43. ares_free_hostent.c
  44. ares_free_string.3
  45. ares_free_string.c
  46. ares_get_servers.3
  47. ares_get_servers_ports.3
  48. ares_getenv.c
  49. ares_getenv.h
  50. ares_gethostbyaddr.3
  51. ares_gethostbyaddr.c
  52. ares_gethostbyname.3
  53. ares_gethostbyname.c
  54. ares_gethostbyname_file.3
  55. ares_getnameinfo.3
  56. ares_getnameinfo.c
  57. ares_getopt.c
  58. ares_getopt.h
  59. ares_getsock.3
  60. ares_getsock.c
  61. ares_inet_net_pton.h
  62. ares_inet_ntop.3
  63. ares_inet_pton.3
  64. ares_init.3
  65. ares_init.c
  66. ares_init_options.3
  67. ares_iphlpapi.h
  68. ares_ipv6.h
  69. ares_library_cleanup.3
  70. ares_library_init.3
  71. ares_library_init.c
  72. ares_library_init.h
  73. ares_library_initialized.3
  74. ares_llist.c
  75. ares_llist.h
  76. ares_mkquery.3
  77. ares_mkquery.c
  78. ares_nowarn.c
  79. ares_nowarn.h
  80. ares_options.c
  81. ares_parse_a_reply.3
  82. ares_parse_a_reply.c
  83. ares_parse_aaaa_reply.3
  84. ares_parse_aaaa_reply.c
  85. ares_parse_mx_reply.3
  86. ares_parse_mx_reply.c
  87. ares_parse_naptr_reply.3
  88. ares_parse_naptr_reply.c
  89. ares_parse_ns_reply.3
  90. ares_parse_ns_reply.c
  91. ares_parse_ptr_reply.3
  92. ares_parse_ptr_reply.c
  93. ares_parse_soa_reply.3
  94. ares_parse_soa_reply.c
  95. ares_parse_srv_reply.3
  96. ares_parse_srv_reply.c
  97. ares_parse_txt_reply.3
  98. ares_parse_txt_reply.c
  99. ares_platform.c
  100. ares_platform.h
  101. ares_private.h
  102. ares_process.3
  103. ares_process.c
  104. ares_query.3
  105. ares_query.c
  106. ares_rules.h
  107. ares_save_options.3
  108. ares_search.3
  109. ares_search.c
  110. ares_send.3
  111. ares_send.c
  112. ares_set_local_dev.3
  113. ares_set_local_ip4.3
  114. ares_set_local_ip6.3
  115. ares_set_servers.3
  116. ares_set_servers_csv.3
  117. ares_set_servers_ports.3
  118. ares_set_servers_ports_csv.3
  119. ares_set_socket_callback.3
  120. ares_set_socket_configure_callback.3
  121. ares_set_socket_functions.3
  122. ares_set_sortlist.3
  123. ares_setup.h
  124. ares_strcasecmp.c
  125. ares_strcasecmp.h
  126. ares_strdup.c
  127. ares_strdup.h
  128. ares_strerror.3
  129. ares_strerror.c
  130. ares_timeout.3
  131. ares_timeout.c
  132. ares_version.3
  133. ares_version.c
  134. ares_version.h
  135. ares_writev.c
  136. ares_writev.h
  137. AUTHORS
  138. bitncmp.c
  139. bitncmp.h
  140. buildconf
  141. buildconf.bat
  142. c-ares-config.cmake.in
  143. cares.rc
  144. CHANGES
  145. CHANGES.0
  146. CMakeLists.txt
  147. config-dos.h
  148. config-win32.h
  149. configure.ac
  150. CONTRIBUTING.md
  151. get_ver.awk
  152. GIT-INFO
  153. git2changes.pl
  154. inet_net_pton.c
  155. inet_ntop.c
  156. install-sh
  157. INSTALL.md
  158. libcares.pc.in
  159. LICENSE.md
  160. Makefile.am
  161. Makefile.dj
  162. Makefile.inc
  163. Makefile.m32
  164. Makefile.msvc
  165. Makefile.netware
  166. Makefile.Watcom
  167. maketgz
  168. mkinstalldirs
  169. msvc_ver.inc
  170. nameser.h
  171. NEWS
  172. README.cares
  173. README.md
  174. README.msvc
  175. RELEASE-NOTES
  176. SECURITY.md
  177. setup_once.h
  178. TODO
  179. windows_port.c
README.md

c-ares

Build Status Windows Build Status Coverage Status CII Best Practices

This is c-ares, an asynchronous resolver library. It is intended for applications which need to perform DNS queries without blocking, or need to perform multiple DNS queries in parallel. The primary examples of such applications are servers which communicate with multiple clients and programs with graphical user interfaces.

The full source code is available in the ‘c-ares’ release archives, and in a git repository: http://github.com/c-ares/c-ares. See the INSTALL.md file for build information.

If you find bugs, correct flaws, have questions or have comments in general in regard to c-ares (or by all means the original ares too), get in touch with us on the c-ares mailing list: http://cool.haxx.se/mailman/listinfo/c-ares

c-ares is of course distributed under the same MIT-style license as the original ares.

You'll find all c-ares details and news here: https://c-ares.haxx.se/

Notes for c-ares hackers

  • The distributed ares_build.h file is only intended to be used on systems which can not run the also distributed configure script.

  • The distributed ares_build.h file is generated as a copy of ares_build.h.dist when the c-ares source code distribution archive file is originally created.

  • If you check out from git on a non-configure platform, you must run the appropriate buildconf* script to set up ares_build.h and other local files before being able to compile the library.

  • On systems capable of running the configure script, the configure process will overwrite the distributed ares_build.h file with one that is suitable and specific to the library being configured and built, this new file is generated from the ares_build.h.in template file.

  • If you intend to distribute an already compiled c-ares library you MUST also distribute along with it the generated ares_build.h which has been used to compile it. Otherwise the library will be of no use for the users of the library that you have built. It is your responsibility to provide this file. No one at the c-ares project can know how you have built the library.

  • File ares_build.h includes platform and configuration dependent info, and must not be modified by anyone. Configure script generates it for you.

  • We cannot assume anything else but very basic compiler features being present. While c-ares requires an ANSI C compiler to build, some of the earlier ANSI compilers clearly can't deal with some preprocessor operators.

  • Newlines must remain unix-style for older compilers' sake.

  • Comments must be written in the old-style /* unnested C-fashion */

  • Try to keep line lengths below 80 columns.