From 083c97e28bd67f3c87ed6cb7636e423c8d18bcb6 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 24 Sep 2011 23:11:13 +0200 Subject: [PATCH] c-ares: upgrade to 1.7.5 --- include/ares.h | 13 +- include/ares_version.h | 4 +- src/ares/CHANGES | 20 ++ src/ares/README.msvc | 24 ++ src/ares/RELEASE-NOTES | 35 +-- src/ares/ares_expand_name.c | 9 +- src/ares/ares_fds.c | 1 + src/ares/ares_free_hostent.c | 3 + src/ares/ares_getenv.c | 30 +++ src/ares/ares_getenv.h | 26 ++ src/ares/ares_gethostbyaddr.c | 13 +- src/ares/ares_gethostbyname.c | 26 +- src/ares/ares_getnameinfo.c | 5 +- src/ares/ares_init.c | 329 ++++++++++++++++++-------- src/ares/ares_library_init.c | 10 + src/ares/ares_library_init.h | 5 +- src/ares/ares_nowarn.c | 130 +++++++++- src/ares/ares_nowarn.h | 33 ++- src/ares/ares_options.c | 13 +- src/ares/ares_parse_a_reply.c | 3 + src/ares/ares_parse_aaaa_reply.c | 3 + src/ares/ares_parse_mx_reply.c | 4 +- src/ares/ares_parse_ns_reply.c | 4 +- src/ares/ares_parse_ptr_reply.c | 15 +- src/ares/ares_parse_srv_reply.c | 6 +- src/ares/ares_platform.h | 43 ++++ src/ares/ares_private.h | 8 +- src/ares/ares_process.c | 49 ++-- src/ares/ares_search.c | 3 +- src/ares/ares_send.c | 2 +- src/ares/ares_setup.h | 16 +- src/ares/config_cygwin/ares_config.h | 2 + src/ares/config_darwin/ares_config.h | 2 + src/ares/config_freebsd/ares_config.h | 2 + src/ares/config_linux/ares_config.h | 2 + src/ares/config_netbsd/ares_config.h | 2 + src/ares/config_openbsd/ares_config.h | 2 + src/ares/config_sunos/ares_config.h | 2 + src/ares/inet_net_pton.c | 41 ++-- src/ares/inet_net_pton.h | 12 +- src/ares/inet_ntop.c | 182 +++++++------- src/ares/inet_ntop.h | 1 - src/ares/nameser.h | 10 + src/ares/setup_once.h | 20 +- uv.gyp | 14 +- 45 files changed, 854 insertions(+), 325 deletions(-) create mode 100644 src/ares/ares_getenv.c create mode 100644 src/ares/ares_getenv.h create mode 100644 src/ares/ares_platform.h diff --git a/include/ares.h b/include/ares.h index 905a29ee..53ac861b 100644 --- a/include/ares.h +++ b/include/ares.h @@ -1,6 +1,6 @@ /* Copyright 1998, 2009 by the Massachusetts Institute of Technology. - * Copyright (C) 2007-2010 by Daniel Stenberg + * Copyright (C) 2007-2011 by Daniel Stenberg * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without @@ -96,10 +96,19 @@ typedef int ares_socklen_t; # include # include # include +#elif defined(_WIN32_WCE) +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include +# include #elif defined(WIN32) +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include # include # include -# include #else # include # include diff --git a/include/ares_version.h b/include/ares_version.h index 8baa897c..efd0156d 100644 --- a/include/ares_version.h +++ b/include/ares_version.h @@ -7,11 +7,11 @@ #define ARES_VERSION_MAJOR 1 #define ARES_VERSION_MINOR 7 -#define ARES_VERSION_PATCH 4 +#define ARES_VERSION_PATCH 5 #define ARES_VERSION ((ARES_VERSION_MAJOR<<16)|\ (ARES_VERSION_MINOR<<8)|\ (ARES_VERSION_PATCH)) -#define ARES_VERSION_STR "1.7.4" +#define ARES_VERSION_STR "1.7.5-DEV" #if (ARES_VERSION >= 0x010700) # define CARES_HAVE_ARES_LIBRARY_INIT 1 diff --git a/src/ares/CHANGES b/src/ares/CHANGES index f739b468..73fe8c77 100644 --- a/src/ares/CHANGES +++ b/src/ares/CHANGES @@ -1,5 +1,25 @@ Changelog for the c-ares project +Version 1.7.5 (August 16, 2011) + +Fixed: + + o detection of semicolon comments in resolv.conf + o avoid using system's inet_net_pton affected by the WLB-2008080064 advisory + o replacement ares_inet_net_pton affected by the WLB-2008080064 advisory + o replacement ares_inet_ntop affected by potential out of bounds write + o added install target to Makefile.msvc + o only fall back to AF_INET searches when looking for AF_UNSPEC addresses + o fixed ares_parse_*_reply memory leaks + o Use correct sizeof in ares_getnameinfo() + o IPv6-on-windows: find DNS servers correctly + o man pages: docs for the c-ares utility programs + o getservbyport replacement for Win CE + o config_sortlist: (win32) missing else + o advance_tcp_send_queue: avoid NULL ptr dereference + o configure: fix a bashism + o ares_expand_name: Fix encoded length for indirect root + Version 1.7.4 (December 9, 2010) Changed: diff --git a/src/ares/README.msvc b/src/ares/README.msvc index 9c1163ac..4ff8700c 100644 --- a/src/ares/README.msvc +++ b/src/ares/README.msvc @@ -40,6 +40,30 @@ library version it is using. + How to install using MSVC from the command line + ----------------------------------------------- + + In order to allow easy usage of c-ares libraries it may be convenient to + install c-ares libraries and header files to a common subdirectory tree. + + Once that c-ares libraries have been built using procedure described above, + use same command prompt window to define environment variable INSTALL_DIR + to designate the top subdirectory where installation of c-ares libraries and + header files will be done. + + > set INSTALL_DIR=c:\c-ares + + Afterwards, run following command to actually perform the installation: + + > nmake -f Makefile.msvc install + + Installation procedure will copy c-ares libraries to subdirectory 'lib' and + c-ares header files to subdirectory 'include' below the INSTALL_DIR subdir. + + When environment variable INSTALL_DIR is not defined, installation is done + to c-ares source folder where Makefile.msvc file is located. + + How to build using Visual Studio 6 IDE -------------------------------------- diff --git a/src/ares/RELEASE-NOTES b/src/ares/RELEASE-NOTES index 639a314e..06d7856b 100644 --- a/src/ares/RELEASE-NOTES +++ b/src/ares/RELEASE-NOTES @@ -1,25 +1,26 @@ -c-ares version 1.7.4 - -Changed: - - o local-bind: Support binding to local interface/IPs, see - ares_set_local_ip4, ares_set_local_ip6, ares_set_local_dev +c-ares version 1.7.5 Fixed: - o memory leak in ares_getnameinfo - o add missing break that caused get_ares_servers to fail - o ares_parse_a_reply: fix CNAME response parsing - o init_by_options: don't copy an empty sortlist - o Replaced uint32_t with unsigned int to fix broken builds - on a couple of platforms - o Fix lookup with HOSTALIASES set - o adig: fix NAPTR parsing - o compiler warning cleanups + o detection of semicolon comments in resolv.conf + o avoid using system's inet_net_pton affected by the WLB-2008080064 advisory + o replacement ares_inet_net_pton affected by the WLB-2008080064 advisory + o replacement ares_inet_ntop affected by potential out of bounds write + o added install target to Makefile.msvc + o only fall back to AF_INET searches when looking for AF_UNSPEC addresses + o fixed ares_parse_*_reply memory leaks + o Use correct sizeof in ares_getnameinfo() + o IPv6-on-windows: find DNS servers correctly + o man pages: docs for the c-ares utility programs + o getservbyport replacement for Win CE + o config_sortlist: (win32) missing else + o advance_tcp_send_queue: avoid NULL ptr dereference + o configure: fix a bashism + o ares_expand_name: Fix encoded length for indirect root Thanks go to these friendly people for their efforts and contributions: - Andrew C. Morrow, Ben Greear, Ben Noordhuis, Daniel Stenberg, - Guenter Knauf, Mike Crowe, Patrik Thunstrom, Yang Tse + Yang Tse, Jakub Hrozek, Gisle Vanem, Tom Hughes, David Stuart, Dima Tisnek, + Peter Pentchev, Stefan Buhler Have fun! diff --git a/src/ares/ares_expand_name.c b/src/ares/ares_expand_name.c index 2af6b2ac..e3eccd2f 100644 --- a/src/ares/ares_expand_name.c +++ b/src/ares/ares_expand_name.c @@ -87,7 +87,14 @@ int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf, * Since this function strips trailing dots though, it becomes "" */ q[0] = '\0'; - *enclen = 1; /* the caller should move one byte to get past this */ + + /* indirect root label (like 0xc0 0x0c) is 2 bytes long (stupid, but + valid) */ + if ((*encoded & INDIR_MASK) == INDIR_MASK) + *enclen = 2; + else + *enclen = 1; /* the caller should move one byte to get past this */ + return ARES_SUCCESS; } diff --git a/src/ares/ares_fds.c b/src/ares/ares_fds.c index b57be928..ac5eedb3 100644 --- a/src/ares/ares_fds.c +++ b/src/ares/ares_fds.c @@ -21,6 +21,7 @@ #endif #include "ares.h" +#include "ares_nowarn.h" #include "ares_private.h" int ares_fds(ares_channel channel, fd_set *read_fds, fd_set *write_fds) diff --git a/src/ares/ares_free_hostent.c b/src/ares/ares_free_hostent.c index b124560d..349d379a 100644 --- a/src/ares/ares_free_hostent.c +++ b/src/ares/ares_free_hostent.c @@ -28,6 +28,9 @@ void ares_free_hostent(struct hostent *host) { char **p; + if (!host) + return; + free((char *)(host->h_name)); for (p = host->h_aliases; *p; p++) free(*p); diff --git a/src/ares/ares_getenv.c b/src/ares/ares_getenv.c new file mode 100644 index 00000000..1b2e85d2 --- /dev/null +++ b/src/ares/ares_getenv.c @@ -0,0 +1,30 @@ + + +/* Copyright 1998 by the Massachusetts Institute of Technology. + * + * 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_getenv.h" + +#ifndef HAVE_GETENV + +char *ares_getenv(const char *name) +{ +#ifdef _WIN32_WCE + return NULL; +#endif +} + +#endif diff --git a/src/ares/ares_getenv.h b/src/ares/ares_getenv.h new file mode 100644 index 00000000..6da6cc50 --- /dev/null +++ b/src/ares/ares_getenv.h @@ -0,0 +1,26 @@ +#ifndef HEADER_CARES_GETENV_H +#define HEADER_CARES_GETENV_H + + +/* Copyright 1998 by the Massachusetts Institute of Technology. + * + * 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" + +#ifndef HAVE_GETENV +extern char *ares_getenv(const char *name); +#endif + +#endif /* HEADER_CARES_GETENV_H */ diff --git a/src/ares/ares_gethostbyaddr.c b/src/ares/ares_gethostbyaddr.c index 0de2cf24..4b4c8a7d 100644 --- a/src/ares/ares_gethostbyaddr.c +++ b/src/ares/ares_gethostbyaddr.c @@ -42,6 +42,7 @@ #include "ares.h" #include "inet_net_pton.h" +#include "ares_platform.h" #include "ares_private.h" #ifdef WATT32 @@ -186,7 +187,13 @@ static int file_lookup(struct ares_addr *addr, struct hostent **host) #ifdef WIN32 char PATH_HOSTS[MAX_PATH]; - if (IS_NT()) { + win_platform platform; + + PATH_HOSTS[0] = '\0'; + + platform = ares__getplatform(); + + if (platform == WIN_NT) { char tmp[MAX_PATH]; HKEY hkeyHosts; @@ -200,8 +207,10 @@ static int file_lookup(struct ares_addr *addr, struct hostent **host) RegCloseKey(hkeyHosts); } } - else + else if (platform == WIN_9X) GetWindowsDirectory(PATH_HOSTS, MAX_PATH); + else + return ARES_ENOTFOUND; strcat(PATH_HOSTS, WIN_PATH_HOSTS); diff --git a/src/ares/ares_gethostbyname.c b/src/ares/ares_gethostbyname.c index 4469ffee..ad89dc27 100644 --- a/src/ares/ares_gethostbyname.c +++ b/src/ares/ares_gethostbyname.c @@ -48,6 +48,7 @@ #include "ares.h" #include "inet_net_pton.h" #include "bitncmp.h" +#include "ares_platform.h" #include "ares_private.h" #ifdef WATT32 @@ -193,11 +194,11 @@ static void host_callback(void *arg, int status, int timeouts, else if (hquery->sent_family == AF_INET6) { status = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL); - if (status == ARES_ENODATA || status == ARES_EBADRESP) { + if ((status == ARES_ENODATA || status == ARES_EBADRESP) && + hquery->want_family == AF_UNSPEC) { /* The query returned something but either there were no AAAA records (e.g. just CNAME) or the response was malformed. Try - looking up A instead. We should possibly limit this - attempt-next logic to AF_UNSPEC lookups only. */ + looking up A instead. */ hquery->sent_family = AF_INET; ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback, hquery); @@ -209,11 +210,10 @@ static void host_callback(void *arg, int status, int timeouts, end_hquery(hquery, status, host); } else if ((status == ARES_ENODATA || status == ARES_EBADRESP || - status == ARES_ETIMEOUT) && hquery->sent_family == AF_INET6) + status == ARES_ETIMEOUT) && (hquery->sent_family == AF_INET6 && + hquery->want_family == AF_UNSPEC)) { - /* The AAAA query yielded no useful result. Now look up an A instead. - We should possibly limit this attempt-next logic to AF_UNSPEC lookups - only. */ + /* The AAAA query yielded no useful result. Now look up an A instead. */ hquery->sent_family = AF_INET; ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback, hquery); @@ -344,7 +344,13 @@ static int file_lookup(const char *name, int family, struct hostent **host) #ifdef WIN32 char PATH_HOSTS[MAX_PATH]; - if (IS_NT()) { + win_platform platform; + + PATH_HOSTS[0] = '\0'; + + platform = ares__getplatform(); + + if (platform == WIN_NT) { char tmp[MAX_PATH]; HKEY hkeyHosts; @@ -358,8 +364,10 @@ static int file_lookup(const char *name, int family, struct hostent **host) RegCloseKey(hkeyHosts); } } - else + else if (platform == WIN_9X) GetWindowsDirectory(PATH_HOSTS, MAX_PATH); + else + return ARES_ENOTFOUND; strcat(PATH_HOSTS, WIN_PATH_HOSTS); diff --git a/src/ares/ares_getnameinfo.c b/src/ares/ares_getnameinfo.c index b6236862..82e261da 100644 --- a/src/ares/ares_getnameinfo.c +++ b/src/ares/ares_getnameinfo.c @@ -58,6 +58,7 @@ #include "ares.h" #include "ares_ipv6.h" #include "inet_ntop.h" +#include "ares_nowarn.h" #include "ares_private.h" struct nameinfo_query { @@ -187,7 +188,7 @@ void ares_getnameinfo(ares_channel channel, const struct sockaddr *sa, if (sa->sa_family == AF_INET) { niquery->family = AF_INET; - memcpy(&niquery->addr.addr4, addr, sizeof(addr)); + memcpy(&niquery->addr.addr4, addr, sizeof(struct in_addr)); ares_gethostbyaddr(channel, &addr->sin_addr, sizeof(struct in_addr), AF_INET, nameinfo_callback, niquery); @@ -195,7 +196,7 @@ void ares_getnameinfo(ares_channel channel, const struct sockaddr *sa, else { niquery->family = AF_INET6; - memcpy(&niquery->addr.addr6, addr6, sizeof(addr6)); + memcpy(&niquery->addr.addr6, addr6, sizeof(struct ares_in6_addr)); ares_gethostbyaddr(channel, &addr6->sin6_addr, sizeof(struct ares_in6_addr), AF_INET6, nameinfo_callback, niquery); diff --git a/src/ares/ares_init.c b/src/ares/ares_init.c index d7954a06..44d4cf93 100644 --- a/src/ares/ares_init.c +++ b/src/ares/ares_init.c @@ -1,6 +1,6 @@ /* Copyright 1998 by the Massachusetts Institute of Technology. - * Copyright (C) 2007-2010 by Daniel Stenberg + * Copyright (C) 2007-2011 by Daniel Stenberg * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without @@ -17,10 +17,6 @@ #include "ares_setup.h" -#ifdef USE_WINSOCK -#include -#endif - #ifdef HAVE_SYS_PARAM_H #include #endif @@ -63,17 +59,19 @@ #include #include #include -#include -#include "ares.h" -#include "inet_net_pton.h" -#include "ares_library_init.h" -#include "ares_nowarn.h" -#include "ares_private.h" #ifdef ANDROID #include #endif +#include "ares.h" +#include "inet_net_pton.h" +#include "ares_library_init.h" +#include "ares_nowarn.h" +#include "ares_platform.h" +#include "inet_ntop.h" +#include "ares_private.h" + #ifdef WATT32 #undef WIN32 /* Redefined in MingW/MSVC headers */ #endif @@ -102,7 +100,7 @@ static int config_lookup(ares_channel channel, const char *str, const char *bindch, const char *filech); static int config_sortlist(struct apattern **sortlist, int *nsort, const char *str); -static char *try_config(char *s, const char *opt); +static char *try_config(char *s, const char *opt, char scc); #endif #define ARES_CONFIG_CHECK(x) (x->lookups && x->nsort > -1 && \ @@ -363,7 +361,7 @@ int ares_save_options(ares_channel channel, struct ares_options *options, ipv4_nservers++; } if (ipv4_nservers) { - options->servers = malloc(ipv4_nservers * sizeof(struct server_state)); + options->servers = malloc(ipv4_nservers * sizeof(struct in_addr)); if (!options->servers) return ARES_ENOMEM; for (i = j = 0; i < channel->nservers; i++) @@ -595,73 +593,195 @@ static int get_res_interfaces_nt(HKEY hKey, const char *subkey, char **obuf) return 0; } +/** + * The desired output for this method is that we set "ret_buf" to + * something like: + * + * 192.168.0.1,dns01.my.domain,fe80::200:f8ff:fe21:67cf + * + * The only ordering requirement is that primary servers are listed + * before secondary. There is no requirement that IPv4 addresses should + * necessarily be before IPv6. + * + * Note that ret_size should ideally be big enough to hold around + * 2-3 IPv4 and 2-3 IPv6 addresses. + * + * Finally, we need to return the total number of DNS servers located. + */ static int get_iphlpapi_dns_info (char *ret_buf, size_t ret_size) { - FIXED_INFO *fi, *newfi; - DWORD size = sizeof (*fi); - IP_ADDR_STRING *ipAddr; - int i, count = 0; - int debug = 0; - size_t ip_size = sizeof("255.255.255.255,")-1; - size_t left = ret_size; - char *ret = ret_buf; - HRESULT res; + const size_t ipv4_size = INET_ADDRSTRLEN + 1; /* +1 for ',' at end */ + const size_t ipv6_size = INET6_ADDRSTRLEN + 12; /* +12 for "%0123456789," at end */ + size_t left = ret_size; + char *ret = ret_buf; + int count = 0; - fi = malloc(size); - if (!fi) - return 0; - - res = (*ares_fpGetNetworkParams) (fi, &size); - if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS)) - goto quit; - - newfi = realloc(fi, size); - if (!newfi) - goto quit; - - fi = newfi; - res = (*ares_fpGetNetworkParams) (fi, &size); - if (res != ERROR_SUCCESS) - goto quit; - - if (debug) + /* Use the GetAdaptersAddresses method if it's available, otherwise + fall back to GetNetworkParams. */ + if (ares_fpGetAdaptersAddresses != ZERO_NULL) { - printf ("Host Name: %s\n", fi->HostName); - printf ("Domain Name: %s\n", fi->DomainName); - printf ("DNS Servers:\n" - " %s (primary)\n", fi->DnsServerList.IpAddress.String); - } - if (strlen(fi->DnsServerList.IpAddress.String) > 0 && - inet_addr(fi->DnsServerList.IpAddress.String) != INADDR_NONE && - left > ip_size) - { - ret += sprintf (ret, "%s,", fi->DnsServerList.IpAddress.String); - left -= ret - ret_buf; - count++; - } + const ULONG working_buf_size = 15000; + IP_ADAPTER_ADDRESSES *pFirstEntry = NULL; + IP_ADAPTER_ADDRESSES *pEntry = NULL; + ULONG bufSize = 0; + ULONG result = 0; - for (i = 0, ipAddr = fi->DnsServerList.Next; ipAddr && left > ip_size; - ipAddr = ipAddr->Next, i++) - { - if (inet_addr(ipAddr->IpAddress.String) != INADDR_NONE) + /* According to MSDN, the recommended way to do this is to use a temporary + buffer of 15K, to "dramatically reduce the chance that the GetAdaptersAddresses + method returns ERROR_BUFFER_OVERFLOW" */ + pFirstEntry = ( IP_ADAPTER_ADDRESSES * ) malloc( working_buf_size ); + bufSize = working_buf_size; + if( !pFirstEntry ) + return 0; + + /* Call the method one time */ + result = ( *ares_fpGetAdaptersAddresses )( AF_UNSPEC, 0, 0, pFirstEntry, &bufSize ); + if( result == ERROR_BUFFER_OVERFLOW ) { - ret += sprintf (ret, "%s,", ipAddr->IpAddress.String); - left -= ret - ret_buf; - count++; + /* Reallocate, bufSize should now be set to the required size */ + pFirstEntry = ( IP_ADAPTER_ADDRESSES * ) realloc( pFirstEntry, bufSize ); + if( !pFirstEntry ) + return 0; + + /* Call the method a second time */ + result = ( *ares_fpGetAdaptersAddresses )( AF_UNSPEC, 0, 0, pFirstEntry, &bufSize ); + if( result == ERROR_BUFFER_OVERFLOW ) + { + /* Reallocate, bufSize should now be set to the required size */ + pFirstEntry = ( IP_ADAPTER_ADDRESSES * ) realloc( pFirstEntry, bufSize ); + if( !pFirstEntry ) + return 0; + + /* Call the method a third time. The maximum number of times we're going to do + this is 3. Three shall be the number thou shalt count, and the number of the + counting shall be three. Five is right out. */ + result = ( *ares_fpGetAdaptersAddresses )( AF_UNSPEC, 0, 0, pFirstEntry, &bufSize ); + } } - if (debug) - printf (" %s (secondary %d)\n", ipAddr->IpAddress.String, i+1); + + /* Check the current result for failure */ + if( result != ERROR_SUCCESS ) + { + free( pFirstEntry ); + return 0; + } + + /* process the results */ + for( pEntry = pFirstEntry ; pEntry != NULL ; pEntry = pEntry->Next ) + { + IP_ADAPTER_DNS_SERVER_ADDRESS* pDNSAddr = pEntry->FirstDnsServerAddress; + for( ; pDNSAddr != NULL ; pDNSAddr = pDNSAddr->Next ) + { + struct sockaddr *pGenericAddr = pDNSAddr->Address.lpSockaddr; + size_t stringlen = 0; + + if( pGenericAddr->sa_family == AF_INET && left > ipv4_size ) + { + /* Handle the v4 case */ + struct sockaddr_in *pIPv4Addr = ( struct sockaddr_in * ) pGenericAddr; + ares_inet_ntop( AF_INET, &pIPv4Addr->sin_addr, ret, ipv4_size - 1 ); /* -1 for comma */ + + /* Append a comma to the end, THEN NULL. Should be OK because we + already tested the size at the top of the if statement. */ + stringlen = strlen( ret ); + ret[ stringlen ] = ','; + ret[ stringlen + 1 ] = '\0'; + ret += stringlen + 1; + left -= ret - ret_buf; + ++count; + } + else if( pGenericAddr->sa_family == AF_INET6 && left > ipv6_size ) + { + /* Handle the v6 case */ + struct sockaddr_in6 *pIPv6Addr = ( struct sockaddr_in6 * ) pGenericAddr; + ares_inet_ntop( AF_INET6, &pIPv6Addr->sin6_addr, ret, ipv6_size - 1 ); /* -1 for comma */ + + /* Append a comma to the end, THEN NULL. Should be OK because we + already tested the size at the top of the if statement. */ + stringlen = strlen( ret ); + ret[ stringlen ] = ','; + ret[ stringlen + 1 ] = '\0'; + ret += stringlen + 1; + left -= ret - ret_buf; + ++count; + + /* NB on Windows this also returns stuff in the fec0::/10 range, + seems to be hard-coded somehow. Do we need to ignore them? */ + } + } + } + + if( pFirstEntry ) + free( pFirstEntry ); + if (ret > ret_buf) + ret[-1] = '\0'; + return count; } + else + { + FIXED_INFO *fi, *newfi; + DWORD size = sizeof (*fi); + IP_ADDR_STRING *ipAddr; + int i; + int debug = 0; + HRESULT res; + + fi = malloc(size); + if (!fi) + return 0; + + res = (*ares_fpGetNetworkParams) (fi, &size); + if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS)) + goto quit; + + newfi = realloc(fi, size); + if (!newfi) + goto quit; + + fi = newfi; + res = (*ares_fpGetNetworkParams) (fi, &size); + if (res != ERROR_SUCCESS) + goto quit; + + if (debug) + { + printf ("Host Name: %s\n", fi->HostName); + printf ("Domain Name: %s\n", fi->DomainName); + printf ("DNS Servers:\n" + " %s (primary)\n", fi->DnsServerList.IpAddress.String); + } + if (strlen(fi->DnsServerList.IpAddress.String) > 0 && + inet_addr(fi->DnsServerList.IpAddress.String) != INADDR_NONE && + left > ipv4_size) + { + ret += sprintf (ret, "%s,", fi->DnsServerList.IpAddress.String); + left -= ret - ret_buf; + ++count; + } + + for (i = 0, ipAddr = fi->DnsServerList.Next; ipAddr && left > ipv4_size; + ipAddr = ipAddr->Next, i++) + { + if (inet_addr(ipAddr->IpAddress.String) != INADDR_NONE) + { + ret += sprintf (ret, "%s,", ipAddr->IpAddress.String); + left -= ret - ret_buf; + ++count; + } + if (debug) + printf (" %s (secondary %d)\n", ipAddr->IpAddress.String, i+1); + } quit: - if (fi) - free(fi); + if (fi) + free(fi); - if (debug && left <= ip_size) - printf ("Too many nameservers. Truncating to %d addressess", count); - if (ret > ret_buf) - ret[-1] = '\0'; - return count; + if (debug && left <= ipv4_size) + printf ("Too many nameservers. Truncating to %d addressess", count); + if (ret > ret_buf) + ret[-1] = '\0'; + return count; + } } #endif @@ -704,7 +824,8 @@ DhcpNameServer DWORD data_type; DWORD bytes; DWORD result; - char buf[256]; + char buf[512]; + win_platform platform; if (channel->nservers > -1) /* don't override ARES_OPT_SERVER */ return ARES_SUCCESS; @@ -716,7 +837,9 @@ DhcpNameServer goto okay; } - if (IS_NT()) + platform = ares__getplatform(); + + if (platform == WIN_NT) { if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, @@ -750,7 +873,7 @@ DhcpNameServer RegCloseKey(mykey); } } - else + else if (platform == WIN_9X) { if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, WIN_NS_9X, 0, @@ -832,7 +955,10 @@ DhcpNameServer return ARES_ENOMEM; for (i = 0; def_nameservers[i]; i++) - servers[i].addr.addrV4.s_addr = htonl(def_nameservers[i]); + { + servers[i].addr.addrV4.s_addr = htonl(def_nameservers[i]); + servers[i].addr.family = AF_INET; + } status = ARES_EOF; #elif defined(ANDROID) @@ -856,17 +982,19 @@ DhcpNameServer if (fp) { while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) { - if ((p = try_config(line, "domain"))) + if ((p = try_config(line, "domain", ';'))) status = config_domain(channel, p); - else if ((p = try_config(line, "lookup")) && !channel->lookups) + else if ((p = try_config(line, "lookup", ';')) && !channel->lookups) status = config_lookup(channel, p, "bind", "file"); - else if ((p = try_config(line, "search"))) + else if ((p = try_config(line, "search", ';'))) status = set_search(channel, p); - else if ((p = try_config(line, "nameserver")) && channel->nservers == -1) + else if ((p = try_config(line, "nameserver", ';')) && + channel->nservers == -1) status = config_nameserver(&servers, &nservers, p); - else if ((p = try_config(line, "sortlist")) && channel->nsort == -1) + else if ((p = try_config(line, "sortlist", ';')) && + channel->nsort == -1) status = config_sortlist(&sortlist, &nsort, p); - else if ((p = try_config(line, "options"))) + else if ((p = try_config(line, "options", ';'))) status = set_options(channel, p); else status = ARES_SUCCESS; @@ -896,8 +1024,9 @@ DhcpNameServer if (fp) { while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) { - if ((p = try_config(line, "hosts:")) && !channel->lookups) - status = config_lookup(channel, p, "dns", "files"); + if ((p = try_config(line, "hosts:", '\0')) && !channel->lookups) + /* ignore errors */ + (void)config_lookup(channel, p, "dns", "files"); } fclose(fp); } @@ -923,8 +1052,9 @@ DhcpNameServer if (fp) { while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) { - if ((p = try_config(line, "order")) && !channel->lookups) - status = config_lookup(channel, p, "bind", "hosts"); + if ((p = try_config(line, "order", '\0')) && !channel->lookups) + /* ignore errors */ + (void)config_lookup(channel, p, "bind", "hosts"); } fclose(fp); } @@ -950,8 +1080,9 @@ DhcpNameServer if (fp) { while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) { - if ((p = try_config(line, "hosts=")) && !channel->lookups) - status = config_lookup(channel, p, "bind", "local"); + if ((p = try_config(line, "hosts=", '\0')) && !channel->lookups) + /* ignore errors */ + (void)config_lookup(channel, p, "bind", "local"); } fclose(fp); } @@ -1042,10 +1173,13 @@ static int init_by_defaults(ares_channel channel) channel->nservers = 1; } -#ifdef ENAMETOOLONG -#define toolong(x) (x == -1) && ((ENAMETOOLONG == errno) || (EINVAL == errno)) +#if defined(USE_WINSOCK) +#define toolong(x) (x == -1) && (SOCKERRNO == WSAEFAULT) +#elif defined(ENAMETOOLONG) +#define toolong(x) (x == -1) && ((SOCKERRNO == ENAMETOOLONG) || \ + (SOCKERRNO == EINVAL)) #else -#define toolong(x) (x == -1) && (EINVAL == errno) +#define toolong(x) (x == -1) && (SOCKERRNO == EINVAL) #endif if (channel->ndomains == -1) { @@ -1287,9 +1421,9 @@ static int config_sortlist(struct apattern **sortlist, int *nsort, if (!sortlist_alloc(sortlist, nsort, &pat)) return ARES_ENOMEM; } - if (ipbufpfx[0] && - (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addrV4, - sizeof(pat.addrV4))) > 0) + else if (ipbufpfx[0] && + (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addrV4, + sizeof(pat.addrV4))) > 0) { pat.type = PATTERN_CIDR; pat.mask.bits = (unsigned short)bits; @@ -1426,7 +1560,7 @@ static const char *try_option(const char *p, const char *q, const char *opt) } #if !defined(WIN32) && !defined(WATT32) -static char *try_config(char *s, const char *opt) +static char *try_config(char *s, const char *opt, char scc) { size_t len; char *p; @@ -1436,10 +1570,17 @@ static char *try_config(char *s, const char *opt) /* no line or no option */ return NULL; + /* Hash '#' character is always used as primary comment char, additionally + a not-NUL secondary comment char will be considered when specified. */ + /* trim line comment */ p = s; - while (*p && (*p != '#')) - p++; + if(scc) + while (*p && (*p != '#') && (*p != scc)) + p++; + else + while (*p && (*p != '#')) + p++; *p = '\0'; /* trim trailing whitespace */ diff --git a/src/ares/ares_library_init.c b/src/ares/ares_library_init.c index 1a875da1..f0137a18 100644 --- a/src/ares/ares_library_init.c +++ b/src/ares/ares_library_init.c @@ -26,6 +26,7 @@ #ifdef USE_WINSOCK fpGetNetworkParams_t ares_fpGetNetworkParams = ZERO_NULL; fpSystemFunction036_t ares_fpSystemFunction036 = ZERO_NULL; +fpGetAdaptersAddresses_t ares_fpGetAdaptersAddresses = ZERO_NULL; #endif /* library-private global vars with source visibility restricted to this file */ @@ -56,6 +57,15 @@ static int ares_win32_init(void) 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 */ + } + /* * When advapi32.dll is unavailable or advapi32.dll has no SystemFunction036, * also known as RtlGenRandom, which is the case for Windows versions prior diff --git a/src/ares/ares_library_init.h b/src/ares/ares_library_init.h index 29d5c9e7..59e5cc58 100644 --- a/src/ares/ares_library_init.h +++ b/src/ares/ares_library_init.h @@ -3,7 +3,7 @@ /* Copyright 1998 by the Massachusetts Institute of Technology. - * Copyright (C) 2004-2009 by Daniel Stenberg + * Copyright (C) 2004-2011 by Daniel Stenberg * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without @@ -23,15 +23,18 @@ #ifdef USE_WINSOCK #include +#include typedef DWORD (WINAPI *fpGetNetworkParams_t) (FIXED_INFO*, DWORD*); typedef BOOLEAN (APIENTRY *fpSystemFunction036_t) (void*, ULONG); +typedef ULONG (WINAPI *fpGetAdaptersAddresses_t) ( ULONG, ULONG, void*, IP_ADAPTER_ADDRESSES*, ULONG* ); /* Forward-declaration of variables defined in ares_library_init.c */ /* that are global and unique instances for whole c-ares library. */ extern fpGetNetworkParams_t ares_fpGetNetworkParams; extern fpSystemFunction036_t ares_fpSystemFunction036; +extern fpGetAdaptersAddresses_t ares_fpGetAdaptersAddresses; #endif /* USE_WINSOCK */ diff --git a/src/ares/ares_nowarn.c b/src/ares/ares_nowarn.c index 52d079e6..701add58 100644 --- a/src/ares/ares_nowarn.c +++ b/src/ares/ares_nowarn.c @@ -1,5 +1,5 @@ -/* Copyright (C) 2010 by Daniel Stenberg +/* Copyright (C) 2010-2011 by Daniel Stenberg * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without @@ -17,13 +17,44 @@ #include "ares_setup.h" +#ifdef HAVE_ASSERT_H +# include +#endif + +#if defined(__INTEL_COMPILER) && defined(__unix__) + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif +#ifdef HAVE_NETINET_IN_H +# include +#endif +#ifdef HAVE_ARPA_INET_H +# include +#endif + +#endif /* __INTEL_COMPILER && __unix__ */ + +#define BUILDING_ARES_NOWARN_C 1 + #include "ares_nowarn.h" -#define CARES_MASK_UINT (~(unsigned int) 0) -#define CARES_MASK_SINT (CARES_MASK_UINT >> 1) +#if (SIZEOF_INT == 2) +# define CARES_MASK_SINT 0x7FFF +# define CARES_MASK_UINT 0xFFFF +#elif (SIZEOF_INT == 4) +# define CARES_MASK_SINT 0x7FFFFFFF +# define CARES_MASK_UINT 0xFFFFFFFF +#elif (SIZEOF_INT == 8) +# define CARES_MASK_SINT 0x7FFFFFFFFFFFFFFF +# define CARES_MASK_UINT 0xFFFFFFFFFFFFFFFF +#elif (SIZEOF_INT == 16) +# define CARES_MASK_SINT 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF +# define CARES_MASK_UINT 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF +#endif /* -** size_t to signed int +** unsigned size_t to signed int */ int aresx_uztosi(size_t uznum) @@ -51,9 +82,100 @@ int aresx_sltosi(long slnum) # pragma warning(disable:810) /* conversion may lose significant bits */ #endif + DEBUGASSERT(slnum >= 0); return (int)(slnum & (long) CARES_MASK_SINT); #ifdef __INTEL_COMPILER # pragma warning(pop) #endif } + +/* +** signed ssize_t to signed int +*/ + +int aresx_sztosi(ssize_t sznum) +{ +#ifdef __INTEL_COMPILER +# pragma warning(push) +# pragma warning(disable:810) /* conversion may lose significant bits */ +#endif + + DEBUGASSERT(sznum >= 0); + return (int)(sznum & (ssize_t) CARES_MASK_SINT); + +#ifdef __INTEL_COMPILER +# pragma warning(pop) +#endif +} + +/* +** signed ssize_t to unsigned int +*/ + +unsigned int aresx_sztoui(ssize_t sznum) +{ +#ifdef __INTEL_COMPILER +# pragma warning(push) +# pragma warning(disable:810) /* conversion may lose significant bits */ +#endif + + DEBUGASSERT(sznum >= 0); + return (unsigned int)(sznum & (ssize_t) CARES_MASK_UINT); + +#ifdef __INTEL_COMPILER +# pragma warning(pop) +#endif +} + +#if defined(__INTEL_COMPILER) && defined(__unix__) + +int aresx_FD_ISSET(int fd, fd_set *fdset) +{ + #pragma warning(push) + #pragma warning(disable:1469) /* clobber ignored */ + return FD_ISSET(fd, fdset); + #pragma warning(pop) +} + +void aresx_FD_SET(int fd, fd_set *fdset) +{ + #pragma warning(push) + #pragma warning(disable:1469) /* clobber ignored */ + FD_SET(fd, fdset); + #pragma warning(pop) +} + +void aresx_FD_ZERO(fd_set *fdset) +{ + #pragma warning(push) + #pragma warning(disable:593) /* variable was set but never used */ + FD_ZERO(fdset); + #pragma warning(pop) +} + +unsigned short aresx_htons(unsigned short usnum) +{ +#if (__INTEL_COMPILER == 910) && defined(__i386__) + return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF)); +#else + #pragma warning(push) + #pragma warning(disable:810) /* conversion may lose significant bits */ + return htons(usnum); + #pragma warning(pop) +#endif +} + +unsigned short aresx_ntohs(unsigned short usnum) +{ +#if (__INTEL_COMPILER == 910) && defined(__i386__) + return (unsigned short)(((usnum << 8) & 0xFF00) | ((usnum >> 8) & 0x00FF)); +#else + #pragma warning(push) + #pragma warning(disable:810) /* conversion may lose significant bits */ + return ntohs(usnum); + #pragma warning(pop) +#endif +} + +#endif /* __INTEL_COMPILER && __unix__ */ diff --git a/src/ares/ares_nowarn.h b/src/ares/ares_nowarn.h index fb804951..bcaa2272 100644 --- a/src/ares/ares_nowarn.h +++ b/src/ares/ares_nowarn.h @@ -2,7 +2,7 @@ #define HEADER_CARES_NOWARN_H -/* Copyright (C) 2010 by Daniel Stenberg +/* Copyright (C) 2010-2011 by Daniel Stenberg * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without @@ -21,4 +21,35 @@ int aresx_uztosi(size_t uznum); int aresx_sltosi(long slnum); +int aresx_sztosi(ssize_t sznum); + +unsigned int aresx_sztoui(ssize_t sznum); + +#if defined(__INTEL_COMPILER) && defined(__unix__) + +int aresx_FD_ISSET(int fd, fd_set *fdset); + +void aresx_FD_SET(int fd, fd_set *fdset); + +void aresx_FD_ZERO(fd_set *fdset); + +unsigned short aresx_htons(unsigned short usnum); + +unsigned short aresx_ntohs(unsigned short usnum); + +#ifndef BUILDING_ARES_NOWARN_C +# undef FD_ISSET +# define FD_ISSET(a,b) aresx_FD_ISSET((a),(b)) +# undef FD_SET +# define FD_SET(a,b) aresx_FD_SET((a),(b)) +# undef FD_ZERO +# define FD_ZERO(a) aresx_FD_ZERO((a)) +# undef htons +# define htons(a) aresx_htons((a)) +# undef ntohs +# define ntohs(a) aresx_ntohs((a)) +#endif + +#endif /* __INTEL_COMPILER && __unix__ */ + #endif /* HEADER_CARES_NOWARN_H */ diff --git a/src/ares/ares_options.c b/src/ares/ares_options.c index 70280526..5174ef26 100644 --- a/src/ares/ares_options.c +++ b/src/ares/ares_options.c @@ -1,6 +1,6 @@ /* Copyright 1998 by the Massachusetts Institute of Technology. - * Copyright (C) 2008-2010 by Daniel Stenberg + * Copyright (C) 2008-2011 by Daniel Stenberg * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without @@ -135,12 +135,10 @@ int ares_set_servers(ares_channel channel, int ares_set_servers_csv(ares_channel channel, const char* _csv) { - int i; + size_t i; char* csv = NULL; char* ptr; char* start_host; - long port; - bool found_port; int rv = ARES_SUCCESS; struct ares_addr_node *servers = NULL; struct ares_addr_node *last = NULL; @@ -165,7 +163,6 @@ int ares_set_servers_csv(ares_channel channel, } start_host = csv; - found_port = false; for (ptr = csv; *ptr; ptr++) { if (*ptr == ',') { char* pp = ptr - 1; @@ -186,9 +183,8 @@ int ares_set_servers_csv(ares_channel channel, pp--; } if ((pp != start_host) && ((pp + 1) < ptr)) { - /* Found it. */ - found_port = true; - port = strtol(pp + 1, NULL, 10); + /* Found it. Parse over the port number */ + (void)strtol(pp + 1, NULL, 10); *pp = 0; /* null terminate host */ } /* resolve host, try ipv4 first, rslt is in network byte order */ @@ -233,7 +229,6 @@ int ares_set_servers_csv(ares_channel channel, } /* Set up for next one */ - found_port = false; start_host = ptr + 1; } } diff --git a/src/ares/ares_parse_a_reply.c b/src/ares/ares_parse_a_reply.c index 9926c27c..4bd08458 100644 --- a/src/ares/ares_parse_a_reply.c +++ b/src/ares/ares_parse_a_reply.c @@ -132,6 +132,7 @@ int ares_parse_a_reply(const unsigned char *abuf, int alen, aptr += len; if (aptr + RRFIXEDSZ > abuf + alen) { + free(rr_name); status = ARES_EBADRESP; break; } @@ -149,6 +150,7 @@ int ares_parse_a_reply(const unsigned char *abuf, int alen, { if (aptr + sizeof(struct in_addr) > abuf + alen) { + free(rr_name); status = ARES_EBADRESP; break; } @@ -159,6 +161,7 @@ int ares_parse_a_reply(const unsigned char *abuf, int alen, struct ares_addrttl * const at = &addrttls[naddrs]; if (aptr + sizeof(struct in_addr) > abuf + alen) { + free(rr_name); status = ARES_EBADRESP; break; } diff --git a/src/ares/ares_parse_aaaa_reply.c b/src/ares/ares_parse_aaaa_reply.c index a0f524da..1fbe8389 100644 --- a/src/ares/ares_parse_aaaa_reply.c +++ b/src/ares/ares_parse_aaaa_reply.c @@ -132,6 +132,7 @@ int ares_parse_aaaa_reply(const unsigned char *abuf, int alen, aptr += len; if (aptr + RRFIXEDSZ > abuf + alen) { + free(rr_name); status = ARES_EBADRESP; break; } @@ -149,6 +150,7 @@ int ares_parse_aaaa_reply(const unsigned char *abuf, int alen, { if (aptr + sizeof(struct ares_in6_addr) > abuf + alen) { + free(rr_name); status = ARES_EBADRESP; break; } @@ -159,6 +161,7 @@ int ares_parse_aaaa_reply(const unsigned char *abuf, int alen, struct ares_addr6ttl * const at = &addrttls[naddrs]; if (aptr + sizeof(struct ares_in6_addr) > abuf + alen) { + free(rr_name); status = ARES_EBADRESP; break; } diff --git a/src/ares/ares_parse_mx_reply.c b/src/ares/ares_parse_mx_reply.c index 186ddd34..21800546 100644 --- a/src/ares/ares_parse_mx_reply.c +++ b/src/ares/ares_parse_mx_reply.c @@ -47,7 +47,7 @@ int ares_parse_mx_reply (const unsigned char *abuf, int alen, - struct ares_mx_reply **mx_out) + struct ares_mx_reply **mx_out) { unsigned int qdcount, ancount, i; const unsigned char *aptr, *vptr; @@ -134,7 +134,7 @@ ares_parse_mx_reply (const unsigned char *abuf, int alen, mx_last = mx_curr; vptr = aptr; - mx_curr->priority = ntohs (*((unsigned short *)vptr)); + mx_curr->priority = DNS__16BIT(vptr); vptr += sizeof(unsigned short); status = ares_expand_name (vptr, abuf, alen, &mx_curr->host, &len); diff --git a/src/ares/ares_parse_ns_reply.c b/src/ares/ares_parse_ns_reply.c index 25c63299..5e9af71d 100644 --- a/src/ares/ares_parse_ns_reply.c +++ b/src/ares/ares_parse_ns_reply.c @@ -1,5 +1,3 @@ -/* $Id */ - /* Copyright 1998 by the Massachusetts Institute of Technology. * * Permission to use, copy, modify, and distribute this @@ -105,6 +103,7 @@ int ares_parse_ns_reply( const unsigned char* abuf, int alen, if ( aptr + RRFIXEDSZ > abuf + alen ) { status = ARES_EBADRESP; + free(rr_name); break; } rr_type = DNS_RR_TYPE( aptr ); @@ -119,6 +118,7 @@ int ares_parse_ns_reply( const unsigned char* abuf, int alen, &len); if ( status != ARES_SUCCESS ) { + free(rr_name); break; } diff --git a/src/ares/ares_parse_ptr_reply.c b/src/ares/ares_parse_ptr_reply.c index ccd68a26..3b6dbc32 100644 --- a/src/ares/ares_parse_ptr_reply.c +++ b/src/ares/ares_parse_ptr_reply.c @@ -99,6 +99,7 @@ int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr, aptr += len; if (aptr + RRFIXEDSZ > abuf + alen) { + free(rr_name); status = ARES_EBADRESP; break; } @@ -114,13 +115,17 @@ int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr, status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data, &len); if (status != ARES_SUCCESS) - break; + { + free(rr_name); + break; + } if (hostname) free(hostname); hostname = rr_data; - aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char *)); + aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char)); if (!aliases[aliascnt]) { + free(rr_name); status = ARES_ENOMEM; break; } @@ -131,6 +136,7 @@ int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr, alias_alloc *= 2; ptr = realloc(aliases, alias_alloc * sizeof(char *)); if(!ptr) { + free(rr_name); status = ARES_ENOMEM; break; } @@ -144,7 +150,10 @@ int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr, status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data, &len); if (status != ARES_SUCCESS) - break; + { + free(rr_name); + break; + } free(ptrname); ptrname = rr_data; } diff --git a/src/ares/ares_parse_srv_reply.c b/src/ares/ares_parse_srv_reply.c index 7d443b38..9c7eb6ee 100644 --- a/src/ares/ares_parse_srv_reply.c +++ b/src/ares/ares_parse_srv_reply.c @@ -139,11 +139,11 @@ ares_parse_srv_reply (const unsigned char *abuf, int alen, srv_last = srv_curr; vptr = aptr; - srv_curr->priority = ntohs (*((unsigned short *)vptr)); + srv_curr->priority = DNS__16BIT(vptr); vptr += sizeof(unsigned short); - srv_curr->weight = ntohs (*((unsigned short *)vptr)); + srv_curr->weight = DNS__16BIT(vptr); vptr += sizeof(unsigned short); - srv_curr->port = ntohs (*((unsigned short *)vptr)); + srv_curr->port = DNS__16BIT(vptr); vptr += sizeof(unsigned short); status = ares_expand_name (vptr, abuf, alen, &srv_curr->host, &len); diff --git a/src/ares/ares_platform.h b/src/ares/ares_platform.h new file mode 100644 index 00000000..e6885ae5 --- /dev/null +++ b/src/ares/ares_platform.h @@ -0,0 +1,43 @@ +#ifndef HEADER_CARES_PLATFORM_H +#define HEADER_CARES_PLATFORM_H + + +/* Copyright 1998 by the Massachusetts Institute of Technology. + * Copyright (C) 2004 - 2011 by Daniel Stenberg et al + * + * 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" + +#if defined(WIN32) && !defined(MSDOS) + +typedef enum { + WIN_UNKNOWN, + WIN_3X, + WIN_9X, + WIN_NT, + WIN_CE +} win_platform; + +win_platform ares__getplatform(void); + +#endif + +#if defined(_WIN32_WCE) + +struct servent *getservbyport(int port, const char *proto); + +#endif + +#endif /* HEADER_CARES_PLATFORM_H */ diff --git a/src/ares/ares_private.h b/src/ares/ares_private.h index bf21abd8..ff45ba7e 100644 --- a/src/ares/ares_private.h +++ b/src/ares/ares_private.h @@ -52,7 +52,6 @@ #if defined(WIN32) && !defined(WATT32) -#define IS_NT() ((int)GetVersion() > 0) #define WIN_NS_9X "System\\CurrentControlSet\\Services\\VxD\\MSTCP" #define WIN_NS_NT_KEY "System\\CurrentControlSet\\Services\\Tcpip\\Parameters" #define NAMESERVER "NameServer" @@ -89,6 +88,11 @@ #include "ares_ipv6.h" #include "ares_llist.h" +#ifndef HAVE_GETENV +# include "ares_getenv.h" +# define getenv(ptr) ares_getenv(ptr) +#endif + #ifndef HAVE_STRDUP # include "ares_strdup.h" # define strdup(ptr) ares_strdup(ptr) @@ -199,7 +203,7 @@ struct query { void *arg; /* Query status */ - int try; /* Number of times we tried this query already. */ + int try_count; /* Number of times we tried this query already. */ int server; /* Server this query has last been sent to. */ struct query_server_info *server_info; /* per-server state */ int using_tcp; diff --git a/src/ares/ares_process.c b/src/ares/ares_process.c index 7d04effa..e5efa5fb 100644 --- a/src/ares/ares_process.c +++ b/src/ares/ares_process.c @@ -63,10 +63,10 @@ #include #include #include -#include #include "ares.h" #include "ares_dns.h" +#include "ares_nowarn.h" #include "ares_private.h" @@ -300,29 +300,28 @@ static void advance_tcp_send_queue(ares_channel channel, int whichserver, { struct send_request *sendreq; struct server_state *server = &channel->servers[whichserver]; - while (num_bytes > 0) - { - sendreq = server->qhead; - if ((size_t)num_bytes >= sendreq->len) - { - num_bytes -= sendreq->len; - server->qhead = sendreq->next; - if (server->qhead == NULL) - { - SOCK_STATE_CALLBACK(channel, server->tcp_socket, 1, 0); - server->qtail = NULL; - } - if (sendreq->data_storage != NULL) - free(sendreq->data_storage); - free(sendreq); - } - else - { - sendreq->data += num_bytes; - sendreq->len -= num_bytes; - num_bytes = 0; - } + while (num_bytes > 0) { + sendreq = server->qhead; + if ((size_t)num_bytes >= sendreq->len) { + num_bytes -= sendreq->len; + server->qhead = sendreq->next; + if (sendreq->data_storage) + free(sendreq->data_storage); + free(sendreq); + if (server->qhead == NULL) { + SOCK_STATE_CALLBACK(channel, server->tcp_socket, 1, 0); + server->qtail = NULL; + + /* qhead is NULL so we cannot continue this loop */ + break; + } } + else { + sendreq->data += num_bytes; + sendreq->len -= num_bytes; + num_bytes = 0; + } + } } /* If any TCP socket selects true for reading, read some data, @@ -686,7 +685,7 @@ static void next_server(ares_channel channel, struct query *query, * servers to try. In total, we need to do channel->nservers * channel->tries * attempts. Use query->try to remember how many times we already attempted * this query. Use modular arithmetic to find the next server to try. */ - while (++(query->try) < (channel->nservers * channel->tries)) + while (++(query->try_count) < (channel->nservers * channel->tries)) { struct server_state *server; @@ -791,7 +790,7 @@ void ares__send_query(ares_channel channel, struct query *query, return; } } - timeplus = channel->timeout << (query->try / channel->nservers); + timeplus = channel->timeout << (query->try_count / channel->nservers); timeplus = (timeplus * (9 + (rand () & 7))) / 16; query->timeout = *now; ares__timeadd(&query->timeout, diff --git a/src/ares/ares_search.c b/src/ares/ares_search.c index 608cb153..1877c19f 100644 --- a/src/ares/ares_search.c +++ b/src/ares/ares_search.c @@ -20,7 +20,6 @@ #include #include #include -#include #ifdef HAVE_STRINGS_H # include @@ -292,7 +291,7 @@ static int single_domain(ares_channel channel, const char *name, char **s) } else { - error = errno; + error = ERRNO; switch(error) { case ENOENT: diff --git a/src/ares/ares_send.c b/src/ares/ares_send.c index 9f24d339..37b07045 100644 --- a/src/ares/ares_send.c +++ b/src/ares/ares_send.c @@ -96,7 +96,7 @@ void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen, query->arg = arg; /* Initialize query status. */ - query->try = 0; + query->try_count = 0; /* Choose the server to send the query to. If rotation is enabled, keep track * of the next server we want to use. */ diff --git a/src/ares/ares_setup.h b/src/ares/ares_setup.h index 9cec52af..254cccbb 100644 --- a/src/ares/ares_setup.h +++ b/src/ares/ares_setup.h @@ -1,7 +1,6 @@ #ifndef HEADER_CARES_SETUP_H #define HEADER_CARES_SETUP_H -/* $Id$ */ /* Copyright (C) 2004 - 2009 by Daniel Stenberg et al * @@ -31,6 +30,12 @@ #ifdef HAVE_CONFIG_H #include "ares_config.h" +#else + +#ifdef WIN32 +#include "config-win32.h" +#endif + #endif /* HAVE_CONFIG_H */ /* ================================================================ */ @@ -70,7 +75,7 @@ /* please, do it beyond the point further indicated in this file. */ /* ================================================================ */ -#if 0 /* libuv disabled */ +#if 0 /* libuv hack */ /* * c-ares external interface definitions are also used internally, * and might also include required system header files to define them. @@ -83,7 +88,7 @@ */ #include -#endif +#endif /* libuv hack */ /* ================================================================= */ /* No system header file shall be included in this file before this */ @@ -102,6 +107,10 @@ */ #ifdef HAVE_WINDOWS_H +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include # ifdef HAVE_WINSOCK2_H # include # ifdef HAVE_WS2TCPIP_H @@ -112,7 +121,6 @@ # include # endif # endif -# include #endif /* diff --git a/src/ares/config_cygwin/ares_config.h b/src/ares/config_cygwin/ares_config.h index 70595d36..592a2459 100644 --- a/src/ares/config_cygwin/ares_config.h +++ b/src/ares/config_cygwin/ares_config.h @@ -508,3 +508,5 @@ /* the signed version of size_t */ /* #undef ssize_t */ + +#define HAVE_GETENV 1 diff --git a/src/ares/config_darwin/ares_config.h b/src/ares/config_darwin/ares_config.h index 3722bba1..920d922c 100644 --- a/src/ares/config_darwin/ares_config.h +++ b/src/ares/config_darwin/ares_config.h @@ -508,3 +508,5 @@ /* the signed version of size_t */ /* #undef ssize_t */ + +#define HAVE_GETENV 1 diff --git a/src/ares/config_freebsd/ares_config.h b/src/ares/config_freebsd/ares_config.h index 4164edb3..2f9b75d0 100644 --- a/src/ares/config_freebsd/ares_config.h +++ b/src/ares/config_freebsd/ares_config.h @@ -508,3 +508,5 @@ /* the signed version of size_t */ /* #undef ssize_t */ + +#define HAVE_GETENV 1 diff --git a/src/ares/config_linux/ares_config.h b/src/ares/config_linux/ares_config.h index dca6476e..8f8e3307 100644 --- a/src/ares/config_linux/ares_config.h +++ b/src/ares/config_linux/ares_config.h @@ -508,3 +508,5 @@ /* the signed version of size_t */ /* #undef ssize_t */ + +#define HAVE_GETENV 1 diff --git a/src/ares/config_netbsd/ares_config.h b/src/ares/config_netbsd/ares_config.h index 5e3ae688..b5891486 100644 --- a/src/ares/config_netbsd/ares_config.h +++ b/src/ares/config_netbsd/ares_config.h @@ -508,3 +508,5 @@ /* the signed version of size_t */ /* #undef ssize_t */ + +#define HAVE_GETENV 1 diff --git a/src/ares/config_openbsd/ares_config.h b/src/ares/config_openbsd/ares_config.h index 5e3ae688..b5891486 100644 --- a/src/ares/config_openbsd/ares_config.h +++ b/src/ares/config_openbsd/ares_config.h @@ -508,3 +508,5 @@ /* the signed version of size_t */ /* #undef ssize_t */ + +#define HAVE_GETENV 1 diff --git a/src/ares/config_sunos/ares_config.h b/src/ares/config_sunos/ares_config.h index 836de9f5..fb8565e1 100644 --- a/src/ares/config_sunos/ares_config.h +++ b/src/ares/config_sunos/ares_config.h @@ -508,3 +508,5 @@ /* the signed version of size_t */ /* #undef ssize_t */ + +#define HAVE_GETENV 1 diff --git a/src/ares/inet_net_pton.c b/src/ares/inet_net_pton.c index 0b2accf1..45bb5d48 100644 --- a/src/ares/inet_net_pton.c +++ b/src/ares/inet_net_pton.c @@ -37,20 +37,20 @@ #endif #include -#include #include #include #include #include "ares.h" #include "ares_ipv6.h" +#include "ares_nowarn.h" #include "inet_net_pton.h" const struct ares_in6_addr ares_in6addr_any = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }; -#if !defined(HAVE_INET_NET_PTON) || !defined(HAVE_INET_NET_PTON_IPV6) +#ifndef HAVE_INET_NET_PTON /* * static int @@ -83,16 +83,17 @@ inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size) ch = *src++; if (ch == '0' && (src[0] == 'x' || src[0] == 'X') + && ISASCII(src[1]) && ISXDIGIT(src[1])) { /* Hexadecimal: Eat nybble string. */ if (!size) goto emsgsize; dirty = 0; src++; /* skip x or X. */ - while ((ch = *src++) != '\0' && ISXDIGIT(ch)) { + while ((ch = *src++) != '\0' && ISASCII(ch) && ISXDIGIT(ch)) { if (ISUPPER(ch)) ch = tolower(ch); - n = (int)(strchr(xdigits, ch) - xdigits); + n = aresx_sztosi(strchr(xdigits, ch) - xdigits); if (dirty == 0) tmp = n; else @@ -109,18 +110,18 @@ inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size) goto emsgsize; *dst++ = (unsigned char) (tmp << 4); } - } else if (ISDIGIT(ch)) { + } else if (ISASCII(ch) && ISDIGIT(ch)) { /* Decimal: eat dotted digit string. */ for (;;) { tmp = 0; do { - n = (int)(strchr(digits, ch) - digits); + n = aresx_sztosi(strchr(digits, ch) - digits); tmp *= 10; tmp += n; if (tmp > 255) goto enoent; } while ((ch = *src++) != '\0' && - ISDIGIT(ch)); + ISASCII(ch) && ISDIGIT(ch)); if (!size--) goto emsgsize; *dst++ = (unsigned char) tmp; @@ -129,27 +130,27 @@ inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size) if (ch != '.') goto enoent; ch = *src++; - if (!ISDIGIT(ch)) + if (!ISASCII(ch) || !ISDIGIT(ch)) goto enoent; } } else goto enoent; bits = -1; - if (ch == '/' && + if (ch == '/' && ISASCII(src[0]) && ISDIGIT(src[0]) && dst > odst) { /* CIDR width specifier. Nothing can follow it. */ ch = *src++; /* Skip over the /. */ bits = 0; do { - n = (int)(strchr(digits, ch) - digits); + n = aresx_sztosi(strchr(digits, ch) - digits); bits *= 10; bits += n; - } while ((ch = *src++) != '\0' && ISDIGIT(ch)); + if (bits > 32) + goto enoent; + } while ((ch = *src++) != '\0' && ISASCII(ch) && ISDIGIT(ch)); if (ch != '\0') goto enoent; - if (bits > 32) - goto emsgsize; } /* Firey death and destruction unless we prefetched EOS. */ @@ -173,7 +174,7 @@ inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size) bits = 8; /* If imputed mask is narrower than specified octets, widen. */ if (bits < ((dst - odst) * 8)) - bits = (int)(dst - odst) * 8; + bits = aresx_sztosi(dst - odst) * 8; /* * If there are no additional bits specified for a class D * address adjust bits to 4. @@ -216,7 +217,7 @@ getbits(const char *src, int *bitsp) if (n++ != 0 && val == 0) /* no leading zeros */ return (0); val *= 10; - val += (pch - digits); + val += aresx_sztosi(pch - digits); if (val > 128) /* range */ return (0); continue; @@ -248,7 +249,7 @@ getv4(const char *src, unsigned char *dst, int *bitsp) if (n++ != 0 && val == 0) /* no leading zeros */ return (0); val *= 10; - val += (pch - digits); + val += aresx_sztoui(pch - digits); if (val > 255) /* range */ return (0); continue; @@ -269,8 +270,8 @@ getv4(const char *src, unsigned char *dst, int *bitsp) return (0); if (dst - odst > 3) /* too many octets? */ return (0); - *dst++ = (unsigned char)val; - return (1); + *dst = (unsigned char)val; + return 1; } static int @@ -308,7 +309,7 @@ inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size) pch = strchr((xdigits = xdigits_u), ch); if (pch != NULL) { val <<= 4; - val |= (pch - xdigits); + val |= aresx_sztoui(pch - xdigits); if (++digits > 4) goto enoent; saw_xdigit = 1; @@ -425,7 +426,7 @@ ares_inet_net_pton(int af, const char *src, void *dst, size_t size) } } -#endif +#endif /* HAVE_INET_NET_PTON */ #ifndef HAVE_INET_PTON int ares_inet_pton(int af, const char *src, void *dst) diff --git a/src/ares/inet_net_pton.h b/src/ares/inet_net_pton.h index e04bc11b..5396a7f2 100644 --- a/src/ares/inet_net_pton.h +++ b/src/ares/inet_net_pton.h @@ -1,8 +1,7 @@ -#ifndef __ARES_INET_NET_PTON_H -#define __ARES_INET_NET_PTON_H +#ifndef HEADER_CARES_INET_NET_PTON_H +#define HEADER_CARES_INET_NET_PTON_H - -/* Copyright (C) 2005 by Daniel Stenberg +/* Copyright (C) 2005-2010 by Daniel Stenberg et al * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without @@ -22,10 +21,11 @@ #else int ares_inet_pton(int af, const char *src, void *dst); #endif -#if defined(HAVE_INET_NET_PTON) && defined(HAVE_INET_NET_PTON_IPV6) + +#ifdef HAVE_INET_NET_PTON #define ares_inet_net_pton(w,x,y,z) inet_net_pton(w,x,y,z) #else int ares_inet_net_pton(int af, const char *src, void *dst, size_t size); #endif -#endif /* __ARES_INET_NET_PTON_H */ +#endif /* HEADER_CARES_INET_NET_PTON_H */ diff --git a/src/ares/inet_ntop.c b/src/ares/inet_ntop.c index 71385cc8..57e1146b 100644 --- a/src/ares/inet_ntop.c +++ b/src/ares/inet_ntop.c @@ -1,18 +1,18 @@ - -/* Copyright (c) 1996 by Internet Software Consortium. +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS - * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE - * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS - * SOFTWARE. + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "ares_setup.h" @@ -36,7 +36,6 @@ #endif #include -#include #include #include #include @@ -48,12 +47,6 @@ #ifndef HAVE_INET_NTOP -#ifdef SPRINTF_CHAR -# define SPRINTF(x) strlen(sprintf/**/x) -#else -# define SPRINTF(x) ((size_t)sprintf x) -#endif - /* * WARNING: Don't even consider trying to compile this on a system where * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. @@ -68,32 +61,31 @@ static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size); * return: * pointer to presentation format address (`dst'), or NULL (see errno). * note: - * On Windows we store the error in the thread errno, not - * in the winsock error code. This is to avoid loosing the - * actual last winsock error. So use macro ERRNO to fetch the - * errno this funtion sets when returning NULL, not SOCKERRNO. + * On Windows we store the error in the thread errno, not + * in the winsock error code. This is to avoid loosing the + * actual last winsock error. So use macro ERRNO to fetch the + * errno this funtion sets when returning NULL, not SOCKERRNO. * author: * Paul Vixie, 1996. */ const char * ares_inet_ntop(int af, const void *src, char *dst, size_t size) { - switch (af) - { - case AF_INET: - return (inet_ntop4(src, dst, size)); - case AF_INET6: - return (inet_ntop6(src, dst, size)); - default: - SET_ERRNO(EAFNOSUPPORT); - return (NULL); - } + switch (af) { + case AF_INET: + return (inet_ntop4(src, dst, size)); + case AF_INET6: + return (inet_ntop6(src, dst, size)); + default: + SET_ERRNO(EAFNOSUPPORT); + return (NULL); + } /* NOTREACHED */ } /* const char * * inet_ntop4(src, dst, size) - * format an IPv4 address, more or less like inet_ntoa() + * format an IPv4 address * return: * `dst' (as a const) * notes: @@ -106,22 +98,21 @@ static const char * inet_ntop4(const unsigned char *src, char *dst, size_t size) { static const char fmt[] = "%u.%u.%u.%u"; - char tmp[sizeof "255.255.255.255"]; + char tmp[sizeof("255.255.255.255")]; - if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) - { - SET_ERRNO(ENOSPC); - return (NULL); - } - strcpy(dst, tmp); - return (dst); + if ((size_t)sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) >= size) { + SET_ERRNO(ENOSPC); + return (NULL); + } + strcpy(dst, tmp); + return (dst); } /* const char * * inet_ntop6(src, dst, size) - * convert IPv6 binary address into presentation (printable) format + * convert IPv6 binary address into presentation (printable) format * author: - * Paul Vixie, 1996. + * Paul Vixie, 1996. */ static const char * inet_ntop6(const unsigned char *src, char *dst, size_t size) @@ -135,11 +126,8 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size) */ char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")]; char *tp; - struct { - long base; - long len; - } best, cur; - unsigned long words[NS_IN6ADDRSZ / NS_INT16SZ]; + struct { int base, len; } best, cur; + unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ]; int i; /* @@ -149,37 +137,29 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size) */ memset(words, '\0', sizeof(words)); for (i = 0; i < NS_IN6ADDRSZ; i++) - words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); - + words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); best.base = -1; - cur.base = -1; best.len = 0; + cur.base = -1; cur.len = 0; - - for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) - { - if (words[i] == 0) - { - if (cur.base == -1) - cur.base = i, cur.len = 1; - else - cur.len++; - } + for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { + if (words[i] == 0) { + if (cur.base == -1) + cur.base = i, cur.len = 1; else - { - if (cur.base != -1) - { - if (best.base == -1 || cur.len > best.len) - best = cur; - cur.base = -1; - } - } - } - if (cur.base != -1) - { - if (best.base == -1 || cur.len > best.len) - best = cur; + cur.len++; + } else { + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + cur.base = -1; + } } + } + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + } if (best.base != -1 && best.len < 2) best.base = -1; @@ -187,46 +167,42 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size) * Format the result. */ tp = tmp; - for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) - { - /* Are we inside the best run of 0x00's? */ - if (best.base != -1 && i >= best.base && - i < (best.base + best.len)) - { - if (i == best.base) - *tp++ = ':'; - continue; - } - /* Are we following an initial run of 0x00s or any real hex? */ - if (i != 0) + for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { + /* Are we inside the best run of 0x00's? */ + if (best.base != -1 && i >= best.base && + i < (best.base + best.len)) { + if (i == best.base) *tp++ = ':'; - /* Is this address an encapsulated IPv4? */ - if (i == 6 && best.base == 0 && - (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) - { - if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp))) - return (NULL); - tp += strlen(tp); - break; - } - tp += SPRINTF((tp, "%lx", words[i])); + continue; } - + /* Are we following an initial run of 0x00s or any real hex? */ + if (i != 0) + *tp++ = ':'; + /* Is this address an encapsulated IPv4? */ + if (i == 6 && best.base == 0 && (best.len == 6 || + (best.len == 7 && words[7] != 0x0001) || + (best.len == 5 && words[5] == 0xffff))) { + if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp))) + return (NULL); + tp += strlen(tp); + break; + } + tp += sprintf(tp, "%x", words[i]); + } /* Was it a trailing run of 0x00's? */ - if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ)) + if (best.base != -1 && (best.base + best.len) == + (NS_IN6ADDRSZ / NS_INT16SZ)) *tp++ = ':'; *tp++ = '\0'; /* * Check for overflow, copy, and we're done. */ - if ((size_t)(tp - tmp) > size) - { - SET_ERRNO(ENOSPC); - return (NULL); - } + if ((size_t)(tp - tmp) > size) { + SET_ERRNO(ENOSPC); + return (NULL); + } strcpy(dst, tmp); return (dst); } #endif - diff --git a/src/ares/inet_ntop.h b/src/ares/inet_ntop.h index 1b5389e8..c583488f 100644 --- a/src/ares/inet_ntop.h +++ b/src/ares/inet_ntop.h @@ -18,7 +18,6 @@ */ #ifdef HAVE_INET_NTOP -#include #define ares_inet_ntop(w,x,y,z) inet_ntop(w,x,y,z) #else const char *ares_inet_ntop(int af, const void *src, char *dst, size_t size); diff --git a/src/ares/nameser.h b/src/ares/nameser.h index b5be424c..45f1e3eb 100644 --- a/src/ares/nameser.h +++ b/src/ares/nameser.h @@ -75,6 +75,11 @@ typedef enum __ns_type { ns_t_sink = 40, /* Kitchen sink (experimentatl) */ ns_t_opt = 41, /* EDNS0 option (meta-RR) */ ns_t_apl = 42, /* Address prefix list (RFC3123) */ + ns_t_ds = 43, /* Delegation Signer (RFC4034) */ + ns_t_sshfp = 44, /* SSH Key Fingerprint (RFC4255) */ + ns_t_rrsig = 46, /* Resource Record Signature (RFC4034) */ + ns_t_nsec = 47, /* Next Secure (RFC4034) */ + ns_t_dnskey = 48, /* DNS Public Key (RFC4034) */ ns_t_tkey = 249, /* Transaction key */ ns_t_tsig = 250, /* Transaction signature. */ ns_t_ixfr = 251, /* Incremental zone transfer. */ @@ -181,6 +186,11 @@ typedef enum __ns_rcode { #define T_SRV ns_t_srv #define T_ATMA ns_t_atma #define T_NAPTR ns_t_naptr +#define T_DS ns_t_ds +#define T_SSHFP ns_t_sshfp +#define T_RRSIG ns_t_rrsig +#define T_NSEC ns_t_nsec +#define T_DNSKEY ns_t_dnskey #define T_TSIG ns_t_tsig #define T_IXFR ns_t_ixfr #define T_AXFR ns_t_axfr diff --git a/src/ares/setup_once.h b/src/ares/setup_once.h index 0c7c8111..a333f54a 100644 --- a/src/ares/setup_once.h +++ b/src/ares/setup_once.h @@ -2,7 +2,7 @@ #define __SETUP_ONCE_H -/* Copyright (C) 2004 - 2010 by Daniel Stenberg et al +/* Copyright (C) 2004 - 2011 by Daniel Stenberg et al * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose and without fee is hereby granted, provided @@ -35,7 +35,10 @@ #include #include #include + +#ifdef HAVE_ERRNO_H #include +#endif #ifdef HAVE_SYS_TYPES_H #include @@ -248,6 +251,7 @@ struct timeval { #define ISPRINT(x) (isprint((int) ((unsigned char)x))) #define ISUPPER(x) (isupper((int) ((unsigned char)x))) #define ISLOWER(x) (islower((int) ((unsigned char)x))) +#define ISASCII(x) (isascii((int) ((unsigned char)x))) #define ISBLANK(x) (int)((((unsigned char)x) == ' ') || \ (((unsigned char)x) == '\t')) @@ -366,7 +370,7 @@ typedef int sig_atomic_t; * (or equivalent) on this platform to hide platform details to code using it. */ -#ifdef WIN32 +#if defined(WIN32) && !defined(WATT32) #define ERRNO ((int)GetLastError()) #define SET_ERRNO(x) (SetLastError((DWORD)(x))) #else @@ -455,6 +459,18 @@ typedef int sig_atomic_t; #endif +/* + * System error codes for Windows CE + */ + +#if defined(WIN32) && !defined(HAVE_ERRNO_H) +#define ENOENT ERROR_FILE_NOT_FOUND +#define ESRCH ERROR_PATH_NOT_FOUND +#define ENOMEM ERROR_NOT_ENOUGH_MEMORY +#define ENOSPC ERROR_INVALID_PARAMETER +#endif + + /* * Actually use __32_getpwuid() on 64-bit VMS builds for getpwuid() */ diff --git a/uv.gyp b/uv.gyp index e32dddd3..85937275 100644 --- a/uv.gyp +++ b/uv.gyp @@ -21,11 +21,8 @@ 'include/uv.h', 'src/uv-common.c', 'src/uv-common.h', - 'src/ares/ares__close_sockets.c', - 'src/ares/ares__get_hostent.c', - 'src/ares/ares__read_line.c', - 'src/ares/ares__timeval.c', 'src/ares/ares_cancel.c', + 'src/ares/ares__close_sockets.c', 'src/ares/ares_data.c', 'src/ares/ares_data.h', 'src/ares/ares_destroy.c', @@ -35,8 +32,10 @@ 'src/ares/ares_fds.c', 'src/ares/ares_free_hostent.c', 'src/ares/ares_free_string.c', + 'src/ares/ares_getenv.h', 'src/ares/ares_gethostbyaddr.c', 'src/ares/ares_gethostbyname.c', + 'src/ares/ares__get_hostent.c', 'src/ares/ares_getnameinfo.c', 'src/ares/ares_getopt.c', 'src/ares/ares_getopt.h', @@ -51,16 +50,18 @@ 'src/ares/ares_nowarn.c', 'src/ares/ares_nowarn.h', 'src/ares/ares_options.c', - 'src/ares/ares_parse_a_reply.c', 'src/ares/ares_parse_aaaa_reply.c', + 'src/ares/ares_parse_a_reply.c', 'src/ares/ares_parse_mx_reply.c', 'src/ares/ares_parse_ns_reply.c', 'src/ares/ares_parse_ptr_reply.c', 'src/ares/ares_parse_srv_reply.c', 'src/ares/ares_parse_txt_reply.c', + 'src/ares/ares_platform.h', 'src/ares/ares_private.h', 'src/ares/ares_process.c', 'src/ares/ares_query.c', + 'src/ares/ares__read_line.c', 'src/ares/ares_rules.h', 'src/ares/ares_search.c', 'src/ares/ares_send.c', @@ -71,6 +72,7 @@ 'src/ares/ares_strdup.h', 'src/ares/ares_strerror.c', 'src/ares/ares_timeout.c', + 'src/ares/ares__timeval.c', 'src/ares/ares_version.c', 'src/ares/ares_writev.c', 'src/ares/ares_writev.h', @@ -82,6 +84,7 @@ 'src/ares/inet_ntop.h', 'src/ares/nameser.h', 'src/ares/setup_once.h', + 'src/ares/windows_port.c', ], 'conditions': [ [ 'OS=="win"', { @@ -98,6 +101,7 @@ 'include/uv-private/uv-win.h', 'src/ares/config_win32/ares_config.h', 'src/ares/windows_port.c', + 'src/ares/ares_getenv.c', 'src/win/async.c', 'src/win/cares.c', 'src/win/core.c',