2006-07-22 11:37:10 -04:00
|
|
|
/* $Id$ */
|
2006-07-22 11:21:13 -04:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
/* 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.
|
|
|
|
*/
|
2005-04-09 15:59:59 -04:00
|
|
|
|
2004-07-22 18:18:45 -04:00
|
|
|
#include "setup.h"
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2008-09-15 13:14:29 -04:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2008-09-16 21:02:57 -04:00
|
|
|
# include <sys/socket.h>
|
2008-09-15 13:14:29 -04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2008-09-16 21:02:57 -04:00
|
|
|
# include <netinet/in.h>
|
2008-09-15 13:14:29 -04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2008-09-16 21:02:57 -04:00
|
|
|
# include <netdb.h>
|
2008-09-15 13:14:29 -04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
2008-09-16 21:02:57 -04:00
|
|
|
# include <arpa/inet.h>
|
2008-09-15 13:14:29 -04:00
|
|
|
#endif
|
2005-04-22 11:01:02 -04:00
|
|
|
#ifdef HAVE_ARPA_NAMESER_H
|
2008-09-16 21:02:57 -04:00
|
|
|
# include <arpa/nameser.h>
|
|
|
|
#else
|
|
|
|
# include "nameser.h"
|
2003-10-07 17:54:04 -04:00
|
|
|
#endif
|
2005-04-22 11:01:02 -04:00
|
|
|
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
|
2008-09-16 21:02:57 -04:00
|
|
|
# include <arpa/nameser_compat.h>
|
2005-04-22 11:01:02 -04:00
|
|
|
#endif
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2008-05-08 18:11:38 -04:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
2004-08-20 10:10:44 -04:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
#include "ares.h"
|
2005-04-08 15:46:46 -04:00
|
|
|
#include "inet_net_pton.h"
|
2005-04-09 12:49:47 -04:00
|
|
|
#include "bitncmp.h"
|
2008-09-10 23:46:47 -04:00
|
|
|
#include "ares_private.h"
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2004-08-20 10:10:44 -04:00
|
|
|
#ifdef WATT32
|
|
|
|
#undef WIN32
|
|
|
|
#endif
|
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
struct host_query {
|
|
|
|
/* Arguments passed to ares_gethostbyname() */
|
|
|
|
ares_channel channel;
|
|
|
|
char *name;
|
|
|
|
ares_host_callback callback;
|
|
|
|
void *arg;
|
2009-01-31 15:17:41 -05:00
|
|
|
int sent_family; /* this family is what was is being used */
|
|
|
|
int want_family; /* this family is what is asked for in the API */
|
2003-10-07 17:54:04 -04:00
|
|
|
const char *remaining_lookups;
|
2007-09-28 10:46:51 -04:00
|
|
|
int timeouts;
|
2003-10-07 17:54:04 -04:00
|
|
|
};
|
|
|
|
|
2007-11-15 04:16:48 -05:00
|
|
|
static void next_lookup(struct host_query *hquery, int status_code);
|
2007-09-28 10:46:51 -04:00
|
|
|
static void host_callback(void *arg, int status, int timeouts,
|
|
|
|
unsigned char *abuf, int alen);
|
2003-10-07 17:54:04 -04:00
|
|
|
static void end_hquery(struct host_query *hquery, int status,
|
2004-12-10 16:46:00 -05:00
|
|
|
struct hostent *host);
|
2009-01-31 15:17:41 -05:00
|
|
|
static int fake_hostent(const char *name, int family,
|
|
|
|
ares_host_callback callback, void *arg);
|
2005-04-08 15:46:46 -04:00
|
|
|
static int file_lookup(const char *name, int family, struct hostent **host);
|
2009-01-31 15:17:41 -05:00
|
|
|
static void sort_addresses(struct hostent *host,
|
|
|
|
const struct apattern *sortlist, int nsort);
|
|
|
|
static void sort6_addresses(struct hostent *host,
|
|
|
|
const struct apattern *sortlist, int nsort);
|
|
|
|
static int get_address_index(const struct in_addr *addr,
|
|
|
|
const struct apattern *sortlist, int nsort);
|
|
|
|
static int get6_address_index(const struct in6_addr *addr,
|
|
|
|
const struct apattern *sortlist, int nsort);
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
void ares_gethostbyname(ares_channel channel, const char *name, int family,
|
2004-12-10 16:46:00 -05:00
|
|
|
ares_host_callback callback, void *arg)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
struct host_query *hquery;
|
|
|
|
|
2009-01-31 15:17:41 -05:00
|
|
|
/* Right now we only know how to look up Internet addresses - and unspec
|
|
|
|
means try both basically. */
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
case AF_INET6:
|
|
|
|
case AF_UNSPEC:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
callback(arg, ARES_ENOTIMP, 0, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2005-04-08 15:46:46 -04:00
|
|
|
if (fake_hostent(name, family, callback, arg))
|
2003-10-07 17:54:04 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Allocate and fill in the host query structure. */
|
|
|
|
hquery = malloc(sizeof(struct host_query));
|
|
|
|
if (!hquery)
|
|
|
|
{
|
2007-09-28 10:46:51 -04:00
|
|
|
callback(arg, ARES_ENOMEM, 0, NULL);
|
2003-10-07 17:54:04 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
hquery->channel = channel;
|
|
|
|
hquery->name = strdup(name);
|
2009-01-31 15:17:41 -05:00
|
|
|
hquery->want_family = family;
|
|
|
|
hquery->sent_family = -1; /* nothing is sent yet */
|
|
|
|
if (!hquery->name) {
|
|
|
|
free(hquery);
|
|
|
|
callback(arg, ARES_ENOMEM, 0, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
hquery->callback = callback;
|
|
|
|
hquery->arg = arg;
|
|
|
|
hquery->remaining_lookups = channel->lookups;
|
2007-09-28 10:46:51 -04:00
|
|
|
hquery->timeouts = 0;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
/* Start performing lookups according to channel->lookups. */
|
2007-11-15 04:16:48 -05:00
|
|
|
next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
2007-11-15 04:16:48 -05:00
|
|
|
static void next_lookup(struct host_query *hquery, int status_code)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
const char *p;
|
2007-06-04 17:33:02 -04:00
|
|
|
struct hostent *host;
|
2007-11-15 04:16:48 -05:00
|
|
|
int status = status_code;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
for (p = hquery->remaining_lookups; *p; p++)
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
2003-10-07 17:54:04 -04:00
|
|
|
switch (*p)
|
2004-12-10 16:46:00 -05:00
|
|
|
{
|
|
|
|
case 'b':
|
|
|
|
/* DNS lookup */
|
|
|
|
hquery->remaining_lookups = p + 1;
|
2009-01-31 15:17:41 -05:00
|
|
|
if ((hquery->want_family == AF_INET6) ||
|
|
|
|
(hquery->want_family == AF_UNSPEC)) {
|
|
|
|
/* if inet6 or unspec, start out with AAAA */
|
|
|
|
hquery->sent_family = AF_INET6;
|
2007-10-02 04:12:30 -04:00
|
|
|
ares_search(hquery->channel, hquery->name, C_IN, T_AAAA,
|
|
|
|
host_callback, hquery);
|
2009-01-31 15:17:41 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
hquery->sent_family = AF_INET;
|
2005-04-08 15:46:46 -04:00
|
|
|
ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
|
|
|
|
hquery);
|
2009-01-31 15:17:41 -05:00
|
|
|
}
|
2004-12-10 16:46:00 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
/* Host file lookup */
|
2009-01-31 15:17:41 -05:00
|
|
|
status = file_lookup(hquery->name, hquery->want_family, &host);
|
2007-11-28 05:46:40 -05:00
|
|
|
|
|
|
|
/* this status check below previously checked for !ARES_ENOTFOUND,
|
|
|
|
but we should not assume that this single error code is the one
|
|
|
|
that can occur, as that is in fact no longer the case */
|
|
|
|
if (status == ARES_SUCCESS)
|
2004-12-10 16:46:00 -05:00
|
|
|
{
|
|
|
|
end_hquery(hquery, status, host);
|
|
|
|
return;
|
|
|
|
}
|
2007-11-15 04:16:48 -05:00
|
|
|
status = status_code; /* Use original status code */
|
2004-12-10 16:46:00 -05:00
|
|
|
break;
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
2007-09-29 20:08:01 -04:00
|
|
|
end_hquery(hquery, status, NULL);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
2007-09-28 10:46:51 -04:00
|
|
|
static void host_callback(void *arg, int status, int timeouts,
|
|
|
|
unsigned char *abuf, int alen)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
struct host_query *hquery = (struct host_query *) arg;
|
|
|
|
ares_channel channel = hquery->channel;
|
2008-04-07 10:20:05 -04:00
|
|
|
struct hostent *host = NULL;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2007-09-28 10:46:51 -04:00
|
|
|
hquery->timeouts += timeouts;
|
2003-10-07 17:54:04 -04:00
|
|
|
if (status == ARES_SUCCESS)
|
|
|
|
{
|
2009-01-31 15:17:41 -05:00
|
|
|
if (hquery->sent_family == AF_INET)
|
2005-04-08 15:46:46 -04:00
|
|
|
{
|
2007-11-15 03:36:25 -05:00
|
|
|
status = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
|
2005-04-08 15:46:46 -04:00
|
|
|
if (host && channel->nsort)
|
|
|
|
sort_addresses(host, channel->sortlist, channel->nsort);
|
|
|
|
}
|
2009-01-31 15:17:41 -05:00
|
|
|
else if (hquery->sent_family == AF_INET6)
|
2005-04-08 15:46:46 -04:00
|
|
|
{
|
2007-11-15 03:36:25 -05:00
|
|
|
status = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL);
|
2009-02-03 09:38:09 -05:00
|
|
|
if (status == ARES_ENODATA || status == ARES_EBADRESP) {
|
|
|
|
/* 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. */
|
2009-01-31 15:17:41 -05:00
|
|
|
hquery->sent_family = AF_INET;
|
|
|
|
ares_search(hquery->channel, hquery->name, C_IN, T_A,
|
|
|
|
host_callback, hquery);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-08 15:46:46 -04:00
|
|
|
if (host && channel->nsort)
|
2005-04-09 15:59:59 -04:00
|
|
|
sort6_addresses(host, channel->sortlist, channel->nsort);
|
2005-04-08 15:46:46 -04:00
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
end_hquery(hquery, status, host);
|
|
|
|
}
|
2009-02-03 09:38:09 -05:00
|
|
|
else if ((status == ARES_ENODATA || status == ARES_EBADRESP || status == ARES_ETIMEOUT) && hquery->sent_family == AF_INET6)
|
2005-04-08 15:46:46 -04:00
|
|
|
{
|
2009-02-03 09:38:09 -05:00
|
|
|
/* 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. */
|
2009-01-31 15:17:41 -05:00
|
|
|
hquery->sent_family = AF_INET;
|
2005-04-08 15:46:46 -04:00
|
|
|
ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
|
|
|
|
hquery);
|
2005-08-18 04:48:31 -04:00
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
else if (status == ARES_EDESTRUCTION)
|
|
|
|
end_hquery(hquery, status, NULL);
|
|
|
|
else
|
2007-10-02 04:12:30 -04:00
|
|
|
next_lookup(hquery, status);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void end_hquery(struct host_query *hquery, int status,
|
2004-12-10 16:46:00 -05:00
|
|
|
struct hostent *host)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
2007-09-28 10:46:51 -04:00
|
|
|
hquery->callback(hquery->arg, status, hquery->timeouts, host);
|
2003-10-07 17:54:04 -04:00
|
|
|
if (host)
|
|
|
|
ares_free_hostent(host);
|
|
|
|
free(hquery->name);
|
|
|
|
free(hquery);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the name looks like an IP address, fake up a host entry, end the
|
|
|
|
* query immediately, and return true. Otherwise return false.
|
|
|
|
*/
|
2005-04-08 15:46:46 -04:00
|
|
|
static int fake_hostent(const char *name, int family, ares_host_callback callback,
|
2004-12-10 16:46:00 -05:00
|
|
|
void *arg)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
struct hostent hostent;
|
|
|
|
char *aliases[1] = { NULL };
|
|
|
|
char *addrs[2];
|
2005-04-08 15:46:46 -04:00
|
|
|
int result = 0;
|
|
|
|
struct in_addr in;
|
|
|
|
struct in6_addr in6;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2009-08-27 05:53:55 -04:00
|
|
|
if (family == AF_INET || family == AF_INET6)
|
2007-09-29 09:52:14 -04:00
|
|
|
{
|
|
|
|
/* It only looks like an IP address if it's all numbers and dots. */
|
2009-08-27 05:53:55 -04:00
|
|
|
int numdots = 0, valid = 1;
|
2007-09-29 09:52:14 -04:00
|
|
|
const char *p;
|
|
|
|
for (p = name; *p; p++)
|
|
|
|
{
|
2007-09-29 17:57:05 -04:00
|
|
|
if (!ISDIGIT(*p) && *p != '.') {
|
2009-08-27 05:53:55 -04:00
|
|
|
valid = 0;
|
|
|
|
break;
|
2007-09-29 09:52:14 -04:00
|
|
|
} else if (*p == '.') {
|
|
|
|
numdots++;
|
|
|
|
}
|
|
|
|
}
|
2008-11-20 02:50:48 -05:00
|
|
|
|
|
|
|
/* if we don't have 3 dots, it is illegal
|
2007-09-29 09:52:14 -04:00
|
|
|
* (although inet_addr doesn't think so).
|
|
|
|
*/
|
2009-08-27 05:53:55 -04:00
|
|
|
if (numdots != 3 || !valid)
|
2007-09-29 09:52:14 -04:00
|
|
|
result = 0;
|
|
|
|
else
|
|
|
|
result = ((in.s_addr = inet_addr(name)) == INADDR_NONE ? 0 : 1);
|
2009-08-27 05:53:55 -04:00
|
|
|
|
|
|
|
if (result)
|
|
|
|
family = AF_INET;
|
2007-09-29 09:52:14 -04:00
|
|
|
}
|
2009-08-27 05:53:55 -04:00
|
|
|
if (family == AF_INET6)
|
2005-04-08 15:46:46 -04:00
|
|
|
result = (ares_inet_pton(AF_INET6, name, &in6) < 1 ? 0 : 1);
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2005-04-08 15:46:46 -04:00
|
|
|
if (!result)
|
2003-10-07 17:54:04 -04:00
|
|
|
return 0;
|
|
|
|
|
2005-04-08 15:46:46 -04:00
|
|
|
if (family == AF_INET)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
2009-04-14 08:53:53 -04:00
|
|
|
hostent.h_length = (int)sizeof(struct in_addr);
|
2005-04-08 15:46:46 -04:00
|
|
|
addrs[0] = (char *)∈
|
|
|
|
}
|
|
|
|
else if (family == AF_INET6)
|
|
|
|
{
|
2009-04-14 08:53:53 -04:00
|
|
|
hostent.h_length = (int)sizeof(struct in6_addr);
|
2005-04-08 15:46:46 -04:00
|
|
|
addrs[0] = (char *)&in6;
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
/* Duplicate the name, to avoid a constness violation. */
|
|
|
|
hostent.h_name = strdup(name);
|
|
|
|
if (!hostent.h_name)
|
|
|
|
{
|
2007-09-28 10:46:51 -04:00
|
|
|
callback(arg, ARES_ENOMEM, 0, NULL);
|
2003-10-07 17:54:04 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the rest of the host structure and terminate the query. */
|
|
|
|
addrs[1] = NULL;
|
|
|
|
hostent.h_aliases = aliases;
|
2005-04-08 15:46:46 -04:00
|
|
|
hostent.h_addrtype = family;
|
2003-10-07 17:54:04 -04:00
|
|
|
hostent.h_addr_list = addrs;
|
2007-09-28 10:46:51 -04:00
|
|
|
callback(arg, ARES_SUCCESS, 0, &hostent);
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2007-01-29 15:56:27 -05:00
|
|
|
free((char *)(hostent.h_name));
|
2003-10-07 17:54:04 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-11-19 10:16:16 -05:00
|
|
|
/* This is an API method */
|
|
|
|
int ares_gethostbyname_file(ares_channel channel, const char *name,
|
|
|
|
int family, struct hostent **host)
|
|
|
|
{
|
2008-11-20 02:50:48 -05:00
|
|
|
int result;
|
|
|
|
|
2008-11-19 10:16:16 -05:00
|
|
|
/* We only take the channel to ensure that ares_init() been called. */
|
|
|
|
if(channel == NULL)
|
|
|
|
{
|
|
|
|
/* Anything will do, really. This seems fine, and is consistent with
|
|
|
|
other error cases. */
|
|
|
|
*host = NULL;
|
|
|
|
return ARES_ENOTFOUND;
|
|
|
|
}
|
2008-11-20 02:50:48 -05:00
|
|
|
|
2008-11-19 10:16:16 -05:00
|
|
|
/* Just chain to the internal implementation we use here; it's exactly
|
2008-11-20 02:50:48 -05:00
|
|
|
* what we want.
|
2008-11-19 10:16:16 -05:00
|
|
|
*/
|
2008-11-20 02:50:48 -05:00
|
|
|
result = file_lookup(name, family, host);
|
2008-11-19 10:16:16 -05:00
|
|
|
if(result != ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
/* We guarantee a NULL hostent on failure. */
|
|
|
|
*host = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-04-08 15:46:46 -04:00
|
|
|
static int file_lookup(const char *name, int family, struct hostent **host)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char **alias;
|
|
|
|
int status;
|
2007-02-18 21:03:58 -05:00
|
|
|
int error;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
char PATH_HOSTS[MAX_PATH];
|
2004-11-10 09:23:20 -05:00
|
|
|
if (IS_NT()) {
|
2005-03-21 17:38:45 -05:00
|
|
|
char tmp[MAX_PATH];
|
|
|
|
HKEY hkeyHosts;
|
2004-12-10 16:46:00 -05:00
|
|
|
|
2005-03-21 17:38:45 -05:00
|
|
|
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
|
|
|
|
== ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
DWORD dwLength = MAX_PATH;
|
|
|
|
RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
|
|
|
|
&dwLength);
|
|
|
|
ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
|
|
|
|
RegCloseKey(hkeyHosts);
|
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
2004-09-26 14:20:58 -04:00
|
|
|
else
|
2003-10-07 17:54:04 -04:00
|
|
|
GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
|
2004-09-26 14:20:58 -04:00
|
|
|
|
|
|
|
strcat(PATH_HOSTS, WIN_PATH_HOSTS);
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2004-07-01 09:55:13 -04:00
|
|
|
#elif defined(WATT32)
|
|
|
|
extern const char *_w32_GetHostsFile (void);
|
|
|
|
const char *PATH_HOSTS = _w32_GetHostsFile();
|
|
|
|
|
|
|
|
if (!PATH_HOSTS)
|
|
|
|
return ARES_ENOTFOUND;
|
2003-10-07 17:54:04 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
fp = fopen(PATH_HOSTS, "r");
|
|
|
|
if (!fp)
|
2007-02-18 21:03:58 -05:00
|
|
|
{
|
|
|
|
error = ERRNO;
|
2007-02-25 23:33:19 -05:00
|
|
|
switch(error)
|
2007-06-04 17:33:02 -04:00
|
|
|
{
|
|
|
|
case ENOENT:
|
|
|
|
case ESRCH:
|
|
|
|
return ARES_ENOTFOUND;
|
|
|
|
default:
|
|
|
|
DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
|
|
|
|
error, strerror(error)));
|
|
|
|
DEBUGF(fprintf(stderr, "Error opening file: %s\n",
|
|
|
|
PATH_HOSTS));
|
|
|
|
*host = NULL;
|
|
|
|
return ARES_EFILE;
|
|
|
|
}
|
2007-02-18 21:03:58 -05:00
|
|
|
}
|
2005-04-08 15:46:46 -04:00
|
|
|
while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
if (strcasecmp((*host)->h_name, name) == 0)
|
2007-06-04 17:33:02 -04:00
|
|
|
break;
|
|
|
|
for (alias = (*host)->h_aliases; *alias; alias++)
|
2004-12-10 16:46:00 -05:00
|
|
|
{
|
|
|
|
if (strcasecmp(*alias, name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2007-06-04 17:33:02 -04:00
|
|
|
if (*alias)
|
|
|
|
break;
|
|
|
|
ares_free_hostent(*host);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
if (status == ARES_EOF)
|
2007-06-04 17:33:02 -04:00
|
|
|
status = ARES_ENOTFOUND;
|
2003-10-07 17:54:04 -04:00
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
*host = NULL;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2009-01-18 11:33:10 -05:00
|
|
|
static void sort_addresses(struct hostent *host, const struct apattern *sortlist,
|
2004-12-10 16:46:00 -05:00
|
|
|
int nsort)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
struct in_addr a1, a2;
|
|
|
|
int i1, i2, ind1, ind2;
|
|
|
|
|
|
|
|
/* This is a simple insertion sort, not optimized at all. i1 walks
|
|
|
|
* through the address list, with the loop invariant that everything
|
|
|
|
* to the left of i1 is sorted. In the loop body, the value at i1 is moved
|
|
|
|
* back through the list (via i2) until it is in sorted order.
|
|
|
|
*/
|
|
|
|
for (i1 = 0; host->h_addr_list[i1]; i1++)
|
|
|
|
{
|
|
|
|
memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
|
|
|
|
ind1 = get_address_index(&a1, sortlist, nsort);
|
|
|
|
for (i2 = i1 - 1; i2 >= 0; i2--)
|
2004-12-10 16:46:00 -05:00
|
|
|
{
|
|
|
|
memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
|
|
|
|
ind2 = get_address_index(&a2, sortlist, nsort);
|
|
|
|
if (ind2 <= ind1)
|
|
|
|
break;
|
|
|
|
memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the first entry in sortlist which matches addr. Return nsort
|
|
|
|
* if none of them match.
|
|
|
|
*/
|
2009-01-18 11:33:10 -05:00
|
|
|
static int get_address_index(const struct in_addr *addr,
|
|
|
|
const struct apattern *sortlist,
|
2004-12-10 16:46:00 -05:00
|
|
|
int nsort)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nsort; i++)
|
|
|
|
{
|
2005-04-09 15:59:59 -04:00
|
|
|
if (sortlist[i].family != AF_INET)
|
|
|
|
continue;
|
|
|
|
if (sortlist[i].type == PATTERN_MASK)
|
|
|
|
{
|
2008-11-25 11:26:58 -05:00
|
|
|
if ((addr->s_addr & sortlist[i].mask.addr4.s_addr)
|
|
|
|
== sortlist[i].addrV4.s_addr)
|
2005-04-09 15:59:59 -04:00
|
|
|
break;
|
|
|
|
}
|
2005-04-09 12:49:47 -04:00
|
|
|
else
|
2005-04-09 15:59:59 -04:00
|
|
|
{
|
2008-11-25 11:26:58 -05:00
|
|
|
if (!ares_bitncmp(&addr->s_addr, &sortlist[i].addrV4.s_addr,
|
2005-04-09 15:59:59 -04:00
|
|
|
sortlist[i].mask.bits))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2009-01-18 11:33:10 -05:00
|
|
|
static void sort6_addresses(struct hostent *host, const struct apattern *sortlist,
|
2005-04-09 15:59:59 -04:00
|
|
|
int nsort)
|
|
|
|
{
|
|
|
|
struct in6_addr a1, a2;
|
|
|
|
int i1, i2, ind1, ind2;
|
|
|
|
|
|
|
|
/* This is a simple insertion sort, not optimized at all. i1 walks
|
|
|
|
* through the address list, with the loop invariant that everything
|
|
|
|
* to the left of i1 is sorted. In the loop body, the value at i1 is moved
|
|
|
|
* back through the list (via i2) until it is in sorted order.
|
|
|
|
*/
|
|
|
|
for (i1 = 0; host->h_addr_list[i1]; i1++)
|
|
|
|
{
|
|
|
|
memcpy(&a1, host->h_addr_list[i1], sizeof(struct in6_addr));
|
|
|
|
ind1 = get6_address_index(&a1, sortlist, nsort);
|
|
|
|
for (i2 = i1 - 1; i2 >= 0; i2--)
|
|
|
|
{
|
|
|
|
memcpy(&a2, host->h_addr_list[i2], sizeof(struct in6_addr));
|
|
|
|
ind2 = get6_address_index(&a2, sortlist, nsort);
|
|
|
|
if (ind2 <= ind1)
|
|
|
|
break;
|
|
|
|
memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in6_addr));
|
|
|
|
}
|
|
|
|
memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in6_addr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the first entry in sortlist which matches addr. Return nsort
|
|
|
|
* if none of them match.
|
|
|
|
*/
|
2009-01-18 11:33:10 -05:00
|
|
|
static int get6_address_index(const struct in6_addr *addr,
|
|
|
|
const struct apattern *sortlist,
|
|
|
|
int nsort)
|
2005-04-09 15:59:59 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nsort; i++)
|
|
|
|
{
|
|
|
|
if (sortlist[i].family != AF_INET6)
|
|
|
|
continue;
|
2008-11-25 11:26:58 -05:00
|
|
|
if (!ares_bitncmp(&addr->s6_addr, &sortlist[i].addrV6.s6_addr, sortlist[i].mask.bits))
|
2005-04-09 12:49:47 -04:00
|
|
|
break;
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|