2005-11-11 20:20:22 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2003-10-07 23:54:04 +02: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.
|
|
|
|
*/
|
2009-11-02 12:55:53 +01:00
|
|
|
#include "ares_setup.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2008-09-15 19:14:29 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <sys/socket.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <netinet/in.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <netdb.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <arpa/inet.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_NAMESER_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <arpa/nameser.h>
|
|
|
|
#else
|
|
|
|
# include "nameser.h"
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
2005-04-07 00:27:42 +02:00
|
|
|
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <arpa/nameser_compat.h>
|
2003-10-07 23:54:04 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-08-20 16:10:44 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
#include "ares.h"
|
2005-04-08 21:46:46 +02:00
|
|
|
#include "inet_net_pton.h"
|
2008-09-11 05:46:47 +02:00
|
|
|
#include "ares_private.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2004-08-20 16:10:44 +02:00
|
|
|
#ifdef WATT32
|
|
|
|
#undef WIN32
|
|
|
|
#endif
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
struct addr_query {
|
|
|
|
/* Arguments passed to ares_gethostbyaddr() */
|
|
|
|
ares_channel channel;
|
2008-11-25 17:26:58 +01:00
|
|
|
struct ares_addr addr;
|
2003-10-07 23:54:04 +02:00
|
|
|
ares_host_callback callback;
|
|
|
|
void *arg;
|
|
|
|
|
|
|
|
const char *remaining_lookups;
|
2007-09-28 16:46:51 +02:00
|
|
|
int timeouts;
|
2003-10-07 23:54:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void next_lookup(struct addr_query *aquery);
|
2007-09-28 16:46:51 +02:00
|
|
|
static void addr_callback(void *arg, int status, int timeouts,
|
|
|
|
unsigned char *abuf, int alen);
|
2003-10-07 23:54:04 +02:00
|
|
|
static void end_aquery(struct addr_query *aquery, int status,
|
2004-12-10 22:46:00 +01:00
|
|
|
struct hostent *host);
|
2008-11-25 17:26:58 +01:00
|
|
|
static int file_lookup(struct ares_addr *addr, struct hostent **host);
|
2009-01-18 17:33:10 +01:00
|
|
|
static void ptr_rr_name(char *name, const struct ares_addr *addr);
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
|
2004-12-10 22:46:00 +01:00
|
|
|
int family, ares_host_callback callback, void *arg)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
struct addr_query *aquery;
|
|
|
|
|
2005-04-08 17:41:31 +02:00
|
|
|
if (family != AF_INET && family != AF_INET6)
|
|
|
|
{
|
2007-09-28 16:46:51 +02:00
|
|
|
callback(arg, ARES_ENOTIMP, 0, NULL);
|
2005-04-08 17:41:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-08-18 10:48:31 +02:00
|
|
|
if ((family == AF_INET && addrlen != sizeof(struct in_addr)) ||
|
2005-04-08 17:41:31 +02:00
|
|
|
(family == AF_INET6 && addrlen != sizeof(struct in6_addr)))
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2007-09-28 16:46:51 +02:00
|
|
|
callback(arg, ARES_ENOTIMP, 0, NULL);
|
2003-10-07 23:54:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aquery = malloc(sizeof(struct addr_query));
|
|
|
|
if (!aquery)
|
|
|
|
{
|
2007-09-28 16:46:51 +02:00
|
|
|
callback(arg, ARES_ENOMEM, 0, NULL);
|
2003-10-07 23:54:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
aquery->channel = channel;
|
2005-04-08 17:41:31 +02:00
|
|
|
if (family == AF_INET)
|
2008-11-25 17:26:58 +01:00
|
|
|
memcpy(&aquery->addr.addrV4, addr, sizeof(struct in_addr));
|
2005-04-08 17:41:31 +02:00
|
|
|
else
|
2008-11-25 17:26:58 +01:00
|
|
|
memcpy(&aquery->addr.addrV6, addr, sizeof(struct in6_addr));
|
|
|
|
aquery->addr.family = family;
|
2003-10-07 23:54:04 +02:00
|
|
|
aquery->callback = callback;
|
|
|
|
aquery->arg = arg;
|
|
|
|
aquery->remaining_lookups = channel->lookups;
|
2007-09-28 16:46:51 +02:00
|
|
|
aquery->timeouts = 0;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
next_lookup(aquery);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void next_lookup(struct addr_query *aquery)
|
|
|
|
{
|
|
|
|
const char *p;
|
2005-04-08 17:41:31 +02:00
|
|
|
char name[128];
|
2007-12-03 11:25:05 +01:00
|
|
|
int status;
|
2003-10-07 23:54:04 +02:00
|
|
|
struct hostent *host;
|
|
|
|
|
|
|
|
for (p = aquery->remaining_lookups; *p; p++)
|
|
|
|
{
|
|
|
|
switch (*p)
|
2004-12-10 22:46:00 +01:00
|
|
|
{
|
|
|
|
case 'b':
|
2008-11-25 17:26:58 +01:00
|
|
|
ptr_rr_name(name, &aquery->addr);
|
2007-12-03 11:25:05 +01:00
|
|
|
aquery->remaining_lookups = p + 1;
|
|
|
|
ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
|
|
|
|
aquery);
|
2004-12-10 22:46:00 +01:00
|
|
|
return;
|
|
|
|
case 'f':
|
2008-11-25 17:26:58 +01:00
|
|
|
status = file_lookup(&aquery->addr, &host);
|
2007-11-28 16:18:27 +01: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 22:46:00 +01:00
|
|
|
{
|
|
|
|
end_aquery(aquery, status, host);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
end_aquery(aquery, ARES_ENOTFOUND, NULL);
|
|
|
|
}
|
|
|
|
|
2007-09-28 16:46:51 +02:00
|
|
|
static void addr_callback(void *arg, int status, int timeouts,
|
|
|
|
unsigned char *abuf, int alen)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
struct addr_query *aquery = (struct addr_query *) arg;
|
|
|
|
struct hostent *host;
|
2009-04-14 14:53:53 +02:00
|
|
|
size_t addrlen;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2007-09-28 16:46:51 +02:00
|
|
|
aquery->timeouts += timeouts;
|
2003-10-07 23:54:04 +02:00
|
|
|
if (status == ARES_SUCCESS)
|
|
|
|
{
|
2008-11-25 17:26:58 +01:00
|
|
|
if (aquery->addr.family == AF_INET)
|
2009-04-14 14:53:53 +02:00
|
|
|
{
|
|
|
|
addrlen = sizeof(struct in_addr);
|
|
|
|
status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV4,
|
|
|
|
(int)addrlen, AF_INET, &host);
|
|
|
|
}
|
2005-04-08 17:41:31 +02:00
|
|
|
else
|
2009-04-14 14:53:53 +02:00
|
|
|
{
|
|
|
|
addrlen = sizeof(struct in6_addr);
|
|
|
|
status = ares_parse_ptr_reply(abuf, alen, &aquery->addr.addrV6,
|
|
|
|
(int)addrlen, AF_INET6, &host);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
end_aquery(aquery, status, host);
|
|
|
|
}
|
|
|
|
else if (status == ARES_EDESTRUCTION)
|
|
|
|
end_aquery(aquery, status, NULL);
|
|
|
|
else
|
|
|
|
next_lookup(aquery);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void end_aquery(struct addr_query *aquery, int status,
|
2004-12-10 22:46:00 +01:00
|
|
|
struct hostent *host)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2007-09-28 16:46:51 +02:00
|
|
|
aquery->callback(aquery->arg, status, aquery->timeouts, host);
|
2003-10-07 23:54:04 +02:00
|
|
|
if (host)
|
|
|
|
ares_free_hostent(host);
|
|
|
|
free(aquery);
|
|
|
|
}
|
|
|
|
|
2008-11-25 17:26:58 +01:00
|
|
|
static int file_lookup(struct ares_addr *addr, struct hostent **host)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int status;
|
2007-02-17 12:59:08 +01:00
|
|
|
int error;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
char PATH_HOSTS[MAX_PATH];
|
2004-11-10 15:23:20 +01:00
|
|
|
if (IS_NT()) {
|
2005-03-21 23:37:18 +01:00
|
|
|
char tmp[MAX_PATH];
|
|
|
|
HKEY hkeyHosts;
|
2004-12-10 22:46:00 +01:00
|
|
|
|
2005-03-21 23:37:18 +01: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 09:50:18 +02:00
|
|
|
}
|
2004-09-26 20:20:58 +02:00
|
|
|
else
|
2003-10-07 23:54:04 +02:00
|
|
|
GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
|
2004-09-26 20:20:58 +02:00
|
|
|
|
|
|
|
strcat(PATH_HOSTS, WIN_PATH_HOSTS);
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2004-07-01 15:55:13 +02: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 23:54:04 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
fp = fopen(PATH_HOSTS, "r");
|
|
|
|
if (!fp)
|
2007-02-16 20:17:05 +01:00
|
|
|
{
|
|
|
|
error = ERRNO;
|
2007-02-26 05:33:19 +01:00
|
|
|
switch(error)
|
2007-02-16 20:17:05 +01:00
|
|
|
{
|
|
|
|
case ENOENT:
|
2007-02-19 03:03:58 +01:00
|
|
|
case ESRCH:
|
2007-02-16 20:17:05 +01:00
|
|
|
return ARES_ENOTFOUND;
|
|
|
|
default:
|
|
|
|
DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
|
|
|
|
error, strerror(error)));
|
2007-02-26 05:33:19 +01:00
|
|
|
DEBUGF(fprintf(stderr, "Error opening file: %s\n",
|
2007-02-16 20:17:05 +01:00
|
|
|
PATH_HOSTS));
|
|
|
|
*host = NULL;
|
|
|
|
return ARES_EFILE;
|
|
|
|
}
|
|
|
|
}
|
2008-11-25 17:26:58 +01:00
|
|
|
while ((status = ares__get_hostent(fp, addr->family, host)) == ARES_SUCCESS)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2008-11-25 17:26:58 +01:00
|
|
|
if (addr->family != (*host)->h_addrtype)
|
2005-04-08 17:41:31 +02:00
|
|
|
{
|
|
|
|
ares_free_hostent(*host);
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-25 17:26:58 +01:00
|
|
|
if (addr->family == AF_INET)
|
2005-04-08 17:41:31 +02:00
|
|
|
{
|
2008-11-25 17:26:58 +01:00
|
|
|
if (memcmp((*host)->h_addr, &addr->addrV4, sizeof(struct in_addr)) == 0)
|
2005-04-08 17:41:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-11-25 17:26:58 +01:00
|
|
|
else if (addr->family == AF_INET6)
|
2005-04-08 17:41:31 +02:00
|
|
|
{
|
2008-11-25 17:26:58 +01:00
|
|
|
if (memcmp((*host)->h_addr, &addr->addrV6, sizeof(struct in6_addr)) == 0)
|
2005-04-08 17:41:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
ares_free_hostent(*host);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
if (status == ARES_EOF)
|
|
|
|
status = ARES_ENOTFOUND;
|
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
*host = NULL;
|
|
|
|
return status;
|
|
|
|
}
|
2007-12-03 11:25:05 +01:00
|
|
|
|
2009-01-18 17:33:10 +01:00
|
|
|
static void ptr_rr_name(char *name, const struct ares_addr *addr)
|
2007-12-03 20:57:18 +01:00
|
|
|
{
|
2008-11-25 17:26:58 +01:00
|
|
|
if (addr->family == AF_INET)
|
2007-12-03 11:25:05 +01:00
|
|
|
{
|
2008-11-25 17:26:58 +01:00
|
|
|
unsigned long laddr = ntohl(addr->addrV4.s_addr);
|
2007-12-03 20:57:18 +01:00
|
|
|
int a1 = (int)((laddr >> 24) & 0xff);
|
|
|
|
int a2 = (int)((laddr >> 16) & 0xff);
|
|
|
|
int a3 = (int)((laddr >> 8) & 0xff);
|
|
|
|
int a4 = (int)(laddr & 0xff);
|
|
|
|
sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
|
2007-12-03 11:25:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-25 17:26:58 +01:00
|
|
|
unsigned char *bytes = (unsigned char *)&addr->addrV6.s6_addr;
|
2008-12-09 02:02:28 +01:00
|
|
|
/* There are too many arguments to do this in one line using
|
2009-01-18 17:33:10 +01:00
|
|
|
* minimally C89-compliant compilers */
|
2007-12-03 20:57:18 +01:00
|
|
|
sprintf(name,
|
2008-12-09 02:02:28 +01:00
|
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.",
|
2007-12-03 11:25:05 +01:00
|
|
|
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,
|
2008-12-09 02:02:28 +01:00
|
|
|
bytes[9]&0xf, bytes[9] >> 4, bytes[8]&0xf, bytes[8] >> 4);
|
|
|
|
sprintf(name+strlen(name),
|
|
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
|
2007-12-03 11:25:05 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|