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.
This commit is contained in:
Yang Tse 2008-11-25 16:26:58 +00:00
Родитель 0fa14c8662
Коммит 3b0c5ae467
5 изменённых файлов: 59 добавлений и 45 удалений

Просмотреть файл

@ -1,5 +1,10 @@
Changelog for the c-ares project 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) * Nov 19 2008 (Daniel Stenberg)
- Brad Spencer brought the new function ares_gethostbyname_file() which simply - Brad Spencer brought the new function ares_gethostbyname_file() which simply
resolves a host name from the given file, using the regular hosts syntax. resolves a host name from the given file, using the regular hosts syntax.

Просмотреть файл

@ -52,8 +52,7 @@
struct addr_query { struct addr_query {
/* Arguments passed to ares_gethostbyaddr() */ /* Arguments passed to ares_gethostbyaddr() */
ares_channel channel; ares_channel channel;
union ares_addr addr; struct ares_addr addr;
int family;
ares_host_callback callback; ares_host_callback callback;
void *arg; void *arg;
@ -66,8 +65,8 @@ static void addr_callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen); unsigned char *abuf, int alen);
static void end_aquery(struct addr_query *aquery, int status, static void end_aquery(struct addr_query *aquery, int status,
struct hostent *host); struct hostent *host);
static int file_lookup(union ares_addr *addr, int family, struct hostent **host); static int file_lookup(struct ares_addr *addr, struct hostent **host);
static void ptr_rr_name(char *name, int family, union ares_addr *addr); static void ptr_rr_name(char *name, struct ares_addr *addr);
void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen, void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
int family, ares_host_callback callback, void *arg) 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; aquery->channel = channel;
if (family == AF_INET) if (family == AF_INET)
memcpy(&aquery->addr.addr4, addr, sizeof(struct in_addr)); memcpy(&aquery->addr.addrV4, addr, sizeof(struct in_addr));
else else
memcpy(&aquery->addr.addr6, addr, sizeof(struct in6_addr)); memcpy(&aquery->addr.addrV6, addr, sizeof(struct in6_addr));
aquery->family = family; aquery->addr.family = family;
aquery->callback = callback; aquery->callback = callback;
aquery->arg = arg; aquery->arg = arg;
aquery->remaining_lookups = channel->lookups; aquery->remaining_lookups = channel->lookups;
@ -119,13 +118,13 @@ static void next_lookup(struct addr_query *aquery)
switch (*p) switch (*p)
{ {
case 'b': case 'b':
ptr_rr_name(name, aquery->family, &aquery->addr); ptr_rr_name(name, &aquery->addr);
aquery->remaining_lookups = p + 1; aquery->remaining_lookups = p + 1;
ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback, ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
aquery); aquery);
return; return;
case 'f': 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, /* this status check below previously checked for !ARES_ENOTFOUND,
but we should not assume that this single error code is the one 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; aquery->timeouts += timeouts;
if (status == ARES_SUCCESS) if (status == ARES_SUCCESS)
{ {
if (aquery->family == AF_INET) if (aquery->addr.family == AF_INET)
status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr4, status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV4,
sizeof(struct in_addr), AF_INET, &host); sizeof(struct in_addr), AF_INET, &host);
else 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); sizeof(struct in6_addr), AF_INET6, &host);
end_aquery(aquery, status, host); end_aquery(aquery, status, host);
} }
@ -173,7 +172,7 @@ static void end_aquery(struct addr_query *aquery, int status,
free(aquery); 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; FILE *fp;
int status; int status;
@ -226,21 +225,21 @@ static int file_lookup(union ares_addr *addr, int family, struct hostent **host)
return ARES_EFILE; 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); ares_free_hostent(*host);
continue; 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; 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; break;
} }
ares_free_hostent(*host); ares_free_hostent(*host);
@ -253,11 +252,11 @@ static int file_lookup(union ares_addr *addr, int family, struct hostent **host)
return status; 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 a1 = (int)((laddr >> 24) & 0xff);
int a2 = (int)((laddr >> 16) & 0xff); int a2 = (int)((laddr >> 16) & 0xff);
int a3 = (int)((laddr >> 8) & 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 else
{ {
unsigned char *bytes = (unsigned char *)&addr->addr6.s6_addr; unsigned char *bytes = (unsigned char *)&addr->addrV6.s6_addr;
sprintf(name, 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."
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa", "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",

Просмотреть файл

@ -432,13 +432,13 @@ static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
continue; continue;
if (sortlist[i].type == PATTERN_MASK) if (sortlist[i].type == PATTERN_MASK)
{ {
if ((addr->s_addr & sortlist[i].mask.addr.addr4.s_addr) if ((addr->s_addr & sortlist[i].mask.addr4.s_addr)
== sortlist[i].addr.addr4.s_addr) == sortlist[i].addrV4.s_addr)
break; break;
} }
else 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)) sortlist[i].mask.bits))
break; break;
} }
@ -485,7 +485,7 @@ static int get6_address_index(struct in6_addr *addr, struct apattern *sortlist,
{ {
if (sortlist[i].family != AF_INET6) if (sortlist[i].family != AF_INET6)
continue; 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; break;
} }
return i; return i;

Просмотреть файл

@ -1179,8 +1179,8 @@ static int config_sortlist(struct apattern **sortlist, int *nsort,
/* Lets see if it is CIDR */ /* Lets see if it is CIDR */
/* First we'll try IPv6 */ /* First we'll try IPv6 */
if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx[0] ? ipbufpfx : ipbuf, if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx[0] ? ipbufpfx : ipbuf,
&pat.addr.addr6, &pat.addrV6,
sizeof(pat.addr.addr6))) > 0) sizeof(pat.addrV6))) > 0)
{ {
pat.type = PATTERN_CIDR; pat.type = PATTERN_CIDR;
pat.mask.bits = (unsigned short)bits; pat.mask.bits = (unsigned short)bits;
@ -1189,8 +1189,8 @@ static int config_sortlist(struct apattern **sortlist, int *nsort,
return ARES_ENOMEM; return ARES_ENOMEM;
} }
if (ipbufpfx[0] && if (ipbufpfx[0] &&
(bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addr.addr4, (bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addrV4,
sizeof(pat.addr.addr4))) > 0) sizeof(pat.addrV4))) > 0)
{ {
pat.type = PATTERN_CIDR; pat.type = PATTERN_CIDR;
pat.mask.bits = (unsigned short)bits; pat.mask.bits = (unsigned short)bits;
@ -1199,13 +1199,13 @@ static int config_sortlist(struct apattern **sortlist, int *nsort,
return ARES_ENOMEM; return ARES_ENOMEM;
} }
/* See if it is just a regular IP */ /* 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]) if (ipbufpfx[0])
{ {
memcpy(ipbuf, str, (int)(q-str)); memcpy(ipbuf, str, (int)(q-str));
ipbuf[(int)(q-str)] = '\0'; 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); natural_mask(&pat);
} }
else 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, /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
* but portable. * 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 /* This is out of date in the CIDR world, but some people might
* still rely on it. * still rely on it.
*/ */
if (IN_CLASSA(addr.s_addr)) 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)) 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 else
pat->mask.addr.addr4.s_addr = htonl(IN_CLASSC_NET); pat->mask.addr4.s_addr = htonl(IN_CLASSC_NET);
} }
#endif #endif
/* initialize an rc4 key. If possible a cryptographically secure random key /* initialize an rc4 key. If possible a cryptographically secure random key

Просмотреть файл

@ -115,6 +115,16 @@
# define writev(s,ptr,cnt) ares_writev(s,ptr,cnt) # define writev(s,ptr,cnt) ares_writev(s,ptr,cnt)
#endif #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 query;
struct send_request { struct send_request {
@ -213,17 +223,17 @@ struct query_server_info {
#define PATTERN_MASK 0x1 #define PATTERN_MASK 0x1
#define PATTERN_CIDR 0x2 #define PATTERN_CIDR 0x2
union ares_addr {
struct in_addr addr4;
struct in6_addr addr6;
};
struct apattern { struct apattern {
union ares_addr addr;
union union
{ {
union ares_addr addr; struct in_addr addr4;
unsigned short bits; struct in6_addr addr6;
} addr;
union
{
struct in_addr addr4;
struct in6_addr addr6;
unsigned short bits;
} mask; } mask;
int family; int family;
unsigned short type; unsigned short type;