From d13eee6683f73e2e0644ff6e664d205dbb86f4c4 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Mon, 28 May 2012 13:52:18 +0000 Subject: [PATCH] The Cygwin resolver library doesn't fill out the list of name servers in res_init() if /etc/resolv.conf doesn't exist, which is usual. Add code to handle this by calling GetNetworkParams(). git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@198 13b04d17-f746-0410-82c6-800466cd88b0 --- configure.in | 2 ++ source/misc.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/configure.in b/configure.in index 899d84d..0f26f47 100644 --- a/configure.in +++ b/configure.in @@ -164,6 +164,8 @@ if test x"$bx_cv_lib_resolver" != x"not found"; then if test x"$bx_cv_lib_resolver" != x"none required"; then LIBS="$bx_cv_lib_resolver $LIBS" fi + dnl This is for getting the DNS servers on Windows + AC_CHECK_HEADER([iphlpapi.h], [AC_CHECK_LIB(iphlpapi, GetNetworkParams, [], [])], [], [#include ]) else AC_MSG_WARN([No resolver library found, /nslookup will be disabled.]) fi diff --git a/source/misc.c b/source/misc.c index 750c7b6..181d2b7 100644 --- a/source/misc.c +++ b/source/misc.c @@ -2053,6 +2053,36 @@ static struct resstats { int re_timeouts; } ar_reinfo; +#ifdef HAVE_LIBIPHLPAPI +#include +#include + +void ar_get_windows_dns(void) +{ + DWORD ret; + ULONG buflen = 0; + FIXED_INFO *buf; + + ret = GetNetworkParams(NULL, &buflen); + if (ret != ERROR_BUFFER_OVERFLOW) + return; + + buf = new_malloc(buflen); + + ret = GetNetworkParams(buf, &buflen); + if (ret == NO_ERROR) + { + _res.nscount = 1; + _res.nsaddr_list[0].sin_family = AF_INET; + _res.nsaddr_list[0].sin_addr.s_addr = inet_addr(buf->DnsServerList.IpAddress.String); + _res.nsaddr_list[0].sin_port = htons(53); + } + + new_free(&buf); + return; +} +#endif /* HAVE_LIBIPHLPAPI */ + /* * ar_init * @@ -2079,10 +2109,20 @@ int ar_init(int op) ret = res_init(); (void)strcpy(ar_domainname, ar_dot); (void)strncat(ar_domainname, _res.defdname, HOSTLEN-2); - if (!_res.nscount) +#ifdef HAVE_LIBIPHLPAPI + /* The Cygwin resolver library doesn't fill out _res.nsaddr_list + * and sets _res.nscount to -1 if there's no /etc/resolv.conf file, + * so we try fetching the first DNS server address ourselves. */ + if (_res.nscount < 1) + ar_get_windows_dns(); +#endif + if (_res.nscount < 1) { + /* Try falling back to the Google public DNS */ _res.nscount = 1; - _res.nsaddr_list[0].sin_addr.s_addr = inet_addr("127.0.0.1"); + _res.nsaddr_list[0].sin_family = AF_INET; + _res.nsaddr_list[0].sin_addr.s_addr = inet_addr("8.8.8.8"); + _res.nsaddr_list[0].sin_port = htons(53); } }