From aa1534eb200edb08adcc987db7ea5d3588e43051 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 25 Nov 2008 16:26:58 +0000 Subject: [PATCH] In preparation for the upcomming IPv6 nameservers patch, the internal ares_addr union is now changed into an internal struct which also holds the address family. --- CHANGES | 5 +++++ ares_gethostbyaddr.c | 45 ++++++++++++++++++++++----------------------- ares_gethostbyname.c | 8 ++++---- ares_init.c | 20 ++++++++++---------- ares_private.h | 26 ++++++++++++++++++-------- 5 files changed, 59 insertions(+), 45 deletions(-) diff --git a/CHANGES b/CHANGES index 7e2875d..284ca5e 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,10 @@ Changelog for the c-ares project +* Nov 25 2008 (Yang Tse) +- In preparation for the upcomming IPv6 nameservers patch, the internal + ares_addr union is now changed into an internal struct which also holds + the address family. + * Nov 19 2008 (Daniel Stenberg) - Brad Spencer brought the new function ares_gethostbyname_file() which simply resolves a host name from the given file, using the regular hosts syntax. diff --git a/ares_gethostbyaddr.c b/ares_gethostbyaddr.c index 85e2e39..30b3cb0 100644 --- a/ares_gethostbyaddr.c +++ b/ares_gethostbyaddr.c @@ -52,8 +52,7 @@ struct addr_query { /* Arguments passed to ares_gethostbyaddr() */ ares_channel channel; - union ares_addr addr; - int family; + struct ares_addr addr; ares_host_callback callback; void *arg; @@ -66,8 +65,8 @@ static void addr_callback(void *arg, int status, int timeouts, unsigned char *abuf, int alen); static void end_aquery(struct addr_query *aquery, int status, struct hostent *host); -static int file_lookup(union ares_addr *addr, int family, struct hostent **host); -static void ptr_rr_name(char *name, int family, union ares_addr *addr); +static int file_lookup(struct ares_addr *addr, struct hostent **host); +static void ptr_rr_name(char *name, struct ares_addr *addr); void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen, int family, ares_host_callback callback, void *arg) @@ -95,10 +94,10 @@ void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen, } aquery->channel = channel; if (family == AF_INET) - memcpy(&aquery->addr.addr4, addr, sizeof(struct in_addr)); + memcpy(&aquery->addr.addrV4, addr, sizeof(struct in_addr)); else - memcpy(&aquery->addr.addr6, addr, sizeof(struct in6_addr)); - aquery->family = family; + memcpy(&aquery->addr.addrV6, addr, sizeof(struct in6_addr)); + aquery->addr.family = family; aquery->callback = callback; aquery->arg = arg; aquery->remaining_lookups = channel->lookups; @@ -119,13 +118,13 @@ static void next_lookup(struct addr_query *aquery) switch (*p) { case 'b': - ptr_rr_name(name, aquery->family, &aquery->addr); + ptr_rr_name(name, &aquery->addr); aquery->remaining_lookups = p + 1; ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback, aquery); return; case 'f': - status = file_lookup(&aquery->addr, aquery->family, &host); + status = file_lookup(&aquery->addr, &host); /* this status check below previously checked for !ARES_ENOTFOUND, but we should not assume that this single error code is the one @@ -150,11 +149,11 @@ static void addr_callback(void *arg, int status, int timeouts, aquery->timeouts += timeouts; if (status == ARES_SUCCESS) { - if (aquery->family == AF_INET) - status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr4, + if (aquery->addr.family == AF_INET) + status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV4, sizeof(struct in_addr), AF_INET, &host); else - status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr6, + status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV6, sizeof(struct in6_addr), AF_INET6, &host); end_aquery(aquery, status, host); } @@ -173,7 +172,7 @@ static void end_aquery(struct addr_query *aquery, int status, free(aquery); } -static int file_lookup(union ares_addr *addr, int family, struct hostent **host) +static int file_lookup(struct ares_addr *addr, struct hostent **host) { FILE *fp; int status; @@ -226,21 +225,21 @@ static int file_lookup(union ares_addr *addr, int family, struct hostent **host) return ARES_EFILE; } } - while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS) + while ((status = ares__get_hostent(fp, addr->family, host)) == ARES_SUCCESS) { - if (family != (*host)->h_addrtype) + if (addr->family != (*host)->h_addrtype) { ares_free_hostent(*host); continue; } - if (family == AF_INET) + if (addr->family == AF_INET) { - if (memcmp((*host)->h_addr, &addr->addr4, sizeof(struct in_addr)) == 0) + if (memcmp((*host)->h_addr, &addr->addrV4, sizeof(struct in_addr)) == 0) break; } - else if (family == AF_INET6) + else if (addr->family == AF_INET6) { - if (memcmp((*host)->h_addr, &addr->addr6, sizeof(struct in6_addr)) == 0) + if (memcmp((*host)->h_addr, &addr->addrV6, sizeof(struct in6_addr)) == 0) break; } ares_free_hostent(*host); @@ -253,11 +252,11 @@ static int file_lookup(union ares_addr *addr, int family, struct hostent **host) return status; } -static void ptr_rr_name(char *name, int family, union ares_addr *addr) +static void ptr_rr_name(char *name, struct ares_addr *addr) { - if (family == AF_INET) + if (addr->family == AF_INET) { - unsigned long laddr = ntohl(addr->addr4.s_addr); + unsigned long laddr = ntohl(addr->addrV4.s_addr); int a1 = (int)((laddr >> 24) & 0xff); int a2 = (int)((laddr >> 16) & 0xff); int a3 = (int)((laddr >> 8) & 0xff); @@ -266,7 +265,7 @@ static void ptr_rr_name(char *name, int family, union ares_addr *addr) } else { - unsigned char *bytes = (unsigned char *)&addr->addr6.s6_addr; + unsigned char *bytes = (unsigned char *)&addr->addrV6.s6_addr; sprintf(name, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa", diff --git a/ares_gethostbyname.c b/ares_gethostbyname.c index 53ca2d9..e452142 100644 --- a/ares_gethostbyname.c +++ b/ares_gethostbyname.c @@ -432,13 +432,13 @@ static int get_address_index(struct in_addr *addr, struct apattern *sortlist, continue; if (sortlist[i].type == PATTERN_MASK) { - if ((addr->s_addr & sortlist[i].mask.addr.addr4.s_addr) - == sortlist[i].addr.addr4.s_addr) + if ((addr->s_addr & sortlist[i].mask.addr4.s_addr) + == sortlist[i].addrV4.s_addr) break; } else { - if (!ares_bitncmp(&addr->s_addr, &sortlist[i].addr.addr4.s_addr, + if (!ares_bitncmp(&addr->s_addr, &sortlist[i].addrV4.s_addr, sortlist[i].mask.bits)) break; } @@ -485,7 +485,7 @@ static int get6_address_index(struct in6_addr *addr, struct apattern *sortlist, { if (sortlist[i].family != AF_INET6) continue; - if (!ares_bitncmp(&addr->s6_addr, &sortlist[i].addr.addr6.s6_addr, sortlist[i].mask.bits)) + if (!ares_bitncmp(&addr->s6_addr, &sortlist[i].addrV6.s6_addr, sortlist[i].mask.bits)) break; } return i; diff --git a/ares_init.c b/ares_init.c index d4301f4..12161f3 100644 --- a/ares_init.c +++ b/ares_init.c @@ -1179,8 +1179,8 @@ static int config_sortlist(struct apattern **sortlist, int *nsort, /* Lets see if it is CIDR */ /* First we'll try IPv6 */ if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx[0] ? ipbufpfx : ipbuf, - &pat.addr.addr6, - sizeof(pat.addr.addr6))) > 0) + &pat.addrV6, + sizeof(pat.addrV6))) > 0) { pat.type = PATTERN_CIDR; pat.mask.bits = (unsigned short)bits; @@ -1189,8 +1189,8 @@ static int config_sortlist(struct apattern **sortlist, int *nsort, return ARES_ENOMEM; } if (ipbufpfx[0] && - (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addr.addr4, - sizeof(pat.addr.addr4))) > 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; @@ -1199,13 +1199,13 @@ static int config_sortlist(struct apattern **sortlist, int *nsort, return ARES_ENOMEM; } /* See if it is just a regular IP */ - else if (ip_addr(ipbuf, (int)(q-str), &pat.addr.addr4) == 0) + else if (ip_addr(ipbuf, (int)(q-str), &pat.addrV4) == 0) { if (ipbufpfx[0]) { memcpy(ipbuf, str, (int)(q-str)); ipbuf[(int)(q-str)] = '\0'; - if (ip_addr(ipbuf, (int)(q - str), &pat.mask.addr.addr4) != 0) + if (ip_addr(ipbuf, (int)(q - str), &pat.mask.addr4) != 0) natural_mask(&pat); } else @@ -1420,17 +1420,17 @@ static void natural_mask(struct apattern *pat) /* Store a host-byte-order copy of pat in a struct in_addr. Icky, * but portable. */ - addr.s_addr = ntohl(pat->addr.addr4.s_addr); + addr.s_addr = ntohl(pat->addrV4.s_addr); /* This is out of date in the CIDR world, but some people might * still rely on it. */ if (IN_CLASSA(addr.s_addr)) - pat->mask.addr.addr4.s_addr = htonl(IN_CLASSA_NET); + pat->mask.addr4.s_addr = htonl(IN_CLASSA_NET); else if (IN_CLASSB(addr.s_addr)) - pat->mask.addr.addr4.s_addr = htonl(IN_CLASSB_NET); + pat->mask.addr4.s_addr = htonl(IN_CLASSB_NET); else - pat->mask.addr.addr4.s_addr = htonl(IN_CLASSC_NET); + pat->mask.addr4.s_addr = htonl(IN_CLASSC_NET); } #endif /* initialize an rc4 key. If possible a cryptographically secure random key diff --git a/ares_private.h b/ares_private.h index a883e50..a7be3f0 100644 --- a/ares_private.h +++ b/ares_private.h @@ -115,6 +115,16 @@ # define writev(s,ptr,cnt) ares_writev(s,ptr,cnt) #endif +struct ares_addr { + int family; + union { + struct in_addr addr4; + struct in6_addr addr6; + } addr; +}; +#define addrV4 addr.addr4 +#define addrV6 addr.addr6 + struct query; struct send_request { @@ -213,17 +223,17 @@ struct query_server_info { #define PATTERN_MASK 0x1 #define PATTERN_CIDR 0x2 -union ares_addr { - struct in_addr addr4; - struct in6_addr addr6; -}; - struct apattern { - union ares_addr addr; union { - union ares_addr addr; - unsigned short bits; + struct in_addr addr4; + struct in6_addr addr6; + } addr; + union + { + struct in_addr addr4; + struct in6_addr addr6; + unsigned short bits; } mask; int family; unsigned short type; -- 2.7.4