#include "ares.h"
#include "ares_private.h"
-int ares__get_hostent(FILE *fp, struct hostent **host)
+int ares__get_hostent(FILE *fp, int family, struct hostent **host)
{
char *line = NULL, *p, *q, *canonical, **alias;
int status, linesize, end_at_hostname, naliases;
struct in_addr addr;
+ struct in6_addr addr6;
+ int addrlen = sizeof(struct in_addr);
struct hostent *hostent = NULL;
while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
*p = 0;
addr.s_addr = inet_addr(line);
if (addr.s_addr == INADDR_NONE)
+ {
+ if (ares_inet_pton(AF_INET6, line, &addr6) > 0)
+ {
+ if (family != AF_INET6)
+ continue;
+ addrlen = sizeof(struct in6_addr);
+ }
+ else
+ continue;
+ }
+ else if (family != AF_INET)
continue;
/* Get the canonical hostname. */
hostent->h_addr_list = malloc(2 * sizeof(char *));
if (!hostent->h_addr_list)
break;
- hostent->h_addr_list[0] = malloc(sizeof(struct in_addr));
+ hostent->h_addr_list[0] = malloc(addrlen);
if (!hostent->h_addr_list[0])
break;
hostent->h_aliases = malloc((naliases + 1) * sizeof(char *));
}
hostent->h_aliases[naliases] = NULL;
- hostent->h_addrtype = AF_INET;
- hostent->h_length = sizeof(struct in_addr);
- memcpy(hostent->h_addr_list[0], &addr, sizeof(struct in_addr));
+ hostent->h_addrtype = family;
+ hostent->h_length = addrlen;
+ if (family == AF_INET)
+ memcpy(hostent->h_addr_list[0], &addr, addrlen);
+ else if (family == AF_INET6)
+ memcpy(hostent->h_addr_list[0], &addr6, addrlen);
hostent->h_addr_list[1] = NULL;
*host = hostent;
free(line);
* this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
-
#include "setup.h"
#include <sys/types.h>
#undef WIN32
#endif
+union ares_addr {
+ struct in_addr addr4;
+ struct in6_addr addr6;
+};
+
struct addr_query {
/* Arguments passed to ares_gethostbyaddr() */
ares_channel channel;
- struct in_addr addr;
+ union ares_addr addr;
+ int family;
ares_host_callback callback;
void *arg;
int alen);
static void end_aquery(struct addr_query *aquery, int status,
struct hostent *host);
-static int file_lookup(struct in_addr *addr, struct hostent **host);
+static int file_lookup(union ares_addr *addr, int family, struct hostent **host);
void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
int family, ares_host_callback callback, void *arg)
{
struct addr_query *aquery;
- if (family != AF_INET || addrlen != sizeof(struct in_addr))
+ if (family != AF_INET && family != AF_INET6)
+ {
+ callback(arg, ARES_ENOTIMP, NULL);
+ return;
+ }
+
+ if ((family == AF_INET && addrlen != sizeof(struct in_addr)) ||
+ (family == AF_INET6 && addrlen != sizeof(struct in6_addr)))
{
callback(arg, ARES_ENOTIMP, NULL);
return;
return;
}
aquery->channel = channel;
- memcpy(&aquery->addr, addr, sizeof(aquery->addr));
+ if (family == AF_INET)
+ memcpy(&aquery->addr.addr4, addr, sizeof(struct in_addr));
+ else
+ memcpy(&aquery->addr.addr6, addr, sizeof(struct in6_addr));
+ aquery->family = family;
aquery->callback = callback;
aquery->arg = arg;
aquery->remaining_lookups = channel->lookups;
static void next_lookup(struct addr_query *aquery)
{
const char *p;
- char name[64];
+ char name[128];
int a1, a2, a3, a4, status;
struct hostent *host;
unsigned long addr;
switch (*p)
{
case 'b':
- addr = ntohl(aquery->addr.s_addr);
- a1 = (int)((addr >> 24) & 0xff);
- a2 = (int)((addr >> 16) & 0xff);
- a3 = (int)((addr >> 8) & 0xff);
- a4 = (int)(addr & 0xff);
- sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
- aquery->remaining_lookups = p + 1;
- ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
- aquery);
+ if (aquery->family == AF_INET)
+ {
+ addr = ntohl(aquery->addr.addr4.s_addr);
+ a1 = (int)((addr >> 24) & 0xff);
+ a2 = (int)((addr >> 16) & 0xff);
+ a3 = (int)((addr >> 8) & 0xff);
+ a4 = (int)(addr & 0xff);
+ sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
+ aquery->remaining_lookups = p + 1;
+ ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
+ aquery);
+ }
+ else
+ {
+ unsigned char *bytes;
+ bytes = (unsigned char *)&aquery->addr.addr6.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",
+ bytes[15]&0xf, bytes[15] >> 4, bytes[14]&0xf, bytes[14] >> 4,
+ bytes[13]&0xf, bytes[13] >> 4, bytes[12]&0xf, bytes[12] >> 4,
+ bytes[11]&0xf, bytes[11] >> 4, bytes[10]&0xf, bytes[10] >> 4,
+ bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4,
+ bytes[7]&0xf, bytes[7] >> 4, bytes[6]&0xf, bytes[6] >> 4,
+ bytes[5]&0xf, bytes[5] >> 4, bytes[4]&0xf, bytes[4] >> 4,
+ bytes[3]&0xf, bytes[3] >> 4, bytes[2]&0xf, bytes[2] >> 4,
+ bytes[1]&0xf, bytes[1] >> 4, bytes[0]&0xf, bytes[0] >> 4);
+ 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, &host);
+ status = file_lookup(&aquery->addr, aquery->family, &host);
if (status != ARES_ENOTFOUND)
{
end_aquery(aquery, status, host);
if (status == ARES_SUCCESS)
{
- status = ares_parse_ptr_reply(abuf, alen, &aquery->addr,
- sizeof(struct in_addr), AF_INET, &host);
+ if (aquery->family == AF_INET)
+ status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr4,
+ sizeof(struct in_addr), AF_INET, &host);
+ else
+ status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addr6,
+ sizeof(struct in6_addr), AF_INET6, &host);
end_aquery(aquery, status, host);
}
else if (status == ARES_EDESTRUCTION)
free(aquery);
}
-static int file_lookup(struct in_addr *addr, struct hostent **host)
+static int file_lookup(union ares_addr *addr, int family, struct hostent **host)
{
FILE *fp;
int status;
fp = fopen(PATH_HOSTS, "r");
if (!fp)
return ARES_ENOTFOUND;
-
- while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
+ while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
{
- if (memcmp((*host)->h_addr, addr, sizeof(struct in_addr)) == 0)
- break;
+ if (family != (*host)->h_addrtype)
+ {
+ ares_free_hostent(*host);
+ continue;
+ }
+ if (family == AF_INET)
+ {
+ if (memcmp((*host)->h_addr, &addr->addr4, sizeof(struct in_addr)) == 0)
+ break;
+ }
+ else if (family == AF_INET6)
+ {
+ if (memcmp((*host)->h_addr, &addr->addr6, sizeof(struct in6_addr)) == 0)
+ break;
+ }
ares_free_hostent(*host);
}
fclose(fp);