2006-07-22 17:37:10 +02:00
|
|
|
/* $Id$ */
|
2006-07-22 17:21:13 +02:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2004-07-23 00:18:45 +02:00
|
|
|
#include "setup.h"
|
2008-09-17 03:02:57 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2007-02-17 12:34:33 +01:00
|
|
|
#include <errno.h>
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2008-09-17 05:25:52 +02:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_private.h"
|
|
|
|
|
|
|
|
struct search_query {
|
|
|
|
/* Arguments passed to ares_search */
|
|
|
|
ares_channel channel;
|
2004-10-06 09:50:18 +02:00
|
|
|
char *name; /* copied into an allocated buffer */
|
2003-10-07 23:54:04 +02:00
|
|
|
int dnsclass;
|
|
|
|
int type;
|
|
|
|
ares_callback callback;
|
|
|
|
void *arg;
|
|
|
|
|
2004-10-06 09:50:18 +02:00
|
|
|
int status_as_is; /* error status from trying as-is */
|
|
|
|
int next_domain; /* next search domain to try */
|
|
|
|
int trying_as_is; /* current query is for name as-is */
|
2007-09-28 16:46:51 +02:00
|
|
|
int timeouts; /* number of timeouts we saw for this request */
|
2007-10-16 23:27:51 +02:00
|
|
|
int ever_got_nodata; /* did we ever get ARES_ENODATA along the way? */
|
2003-10-07 23:54:04 +02:00
|
|
|
};
|
|
|
|
|
2007-09-28 16:46:51 +02:00
|
|
|
static void search_callback(void *arg, int status, int timeouts,
|
|
|
|
unsigned char *abuf, int alen);
|
2003-10-07 23:54:04 +02:00
|
|
|
static void end_squery(struct search_query *squery, int status,
|
2004-10-06 09:50:18 +02:00
|
|
|
unsigned char *abuf, int alen);
|
2003-10-07 23:54:04 +02:00
|
|
|
static int cat_domain(const char *name, const char *domain, char **s);
|
|
|
|
static int single_domain(ares_channel channel, const char *name, char **s);
|
|
|
|
|
|
|
|
void ares_search(ares_channel channel, const char *name, int dnsclass,
|
2004-10-06 09:50:18 +02:00
|
|
|
int type, ares_callback callback, void *arg)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
struct search_query *squery;
|
|
|
|
char *s;
|
|
|
|
const char *p;
|
|
|
|
int status, ndots;
|
2007-10-16 23:27:51 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* If name only yields one domain to search, then we don't have
|
|
|
|
* to keep extra state, so just do an ares_query().
|
|
|
|
*/
|
|
|
|
status = single_domain(channel, name, &s);
|
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
{
|
2007-09-28 16:46:51 +02:00
|
|
|
callback(arg, status, 0, NULL, 0);
|
2003-10-07 23:54:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
ares_query(channel, s, dnsclass, type, callback, arg);
|
|
|
|
free(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a search_query structure to hold the state necessary for
|
|
|
|
* doing multiple lookups.
|
|
|
|
*/
|
|
|
|
squery = malloc(sizeof(struct search_query));
|
|
|
|
if (!squery)
|
|
|
|
{
|
2007-09-28 16:46:51 +02:00
|
|
|
callback(arg, ARES_ENOMEM, 0, NULL, 0);
|
2003-10-07 23:54:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
squery->channel = channel;
|
|
|
|
squery->name = strdup(name);
|
|
|
|
if (!squery->name)
|
|
|
|
{
|
|
|
|
free(squery);
|
2007-09-28 16:46:51 +02:00
|
|
|
callback(arg, ARES_ENOMEM, 0, NULL, 0);
|
2003-10-07 23:54:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
squery->dnsclass = dnsclass;
|
|
|
|
squery->type = type;
|
|
|
|
squery->status_as_is = -1;
|
|
|
|
squery->callback = callback;
|
|
|
|
squery->arg = arg;
|
2007-09-28 16:46:51 +02:00
|
|
|
squery->timeouts = 0;
|
2007-10-16 23:27:51 +02:00
|
|
|
squery->ever_got_nodata = 0;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Count the number of dots in name. */
|
|
|
|
ndots = 0;
|
|
|
|
for (p = name; *p; p++)
|
|
|
|
{
|
|
|
|
if (*p == '.')
|
2004-10-06 09:50:18 +02:00
|
|
|
ndots++;
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If ndots is at least the channel ndots threshold (usually 1),
|
|
|
|
* then we try the name as-is first. Otherwise, we try the name
|
|
|
|
* as-is last.
|
|
|
|
*/
|
|
|
|
if (ndots >= channel->ndots)
|
|
|
|
{
|
|
|
|
/* Try the name as-is first. */
|
|
|
|
squery->next_domain = 0;
|
|
|
|
squery->trying_as_is = 1;
|
|
|
|
ares_query(channel, name, dnsclass, type, search_callback, squery);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Try the name as-is last; start with the first search domain. */
|
|
|
|
squery->next_domain = 1;
|
|
|
|
squery->trying_as_is = 0;
|
|
|
|
status = cat_domain(name, channel->domains[0], &s);
|
|
|
|
if (status == ARES_SUCCESS)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
ares_query(channel, s, dnsclass, type, search_callback, squery);
|
|
|
|
free(s);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
else
|
2004-05-27 09:10:36 +02:00
|
|
|
{
|
|
|
|
/* failed, free the malloc()ed memory */
|
|
|
|
free(squery->name);
|
|
|
|
free(squery);
|
2007-09-28 16:46:51 +02:00
|
|
|
callback(arg, status, 0, NULL, 0);
|
2004-05-27 09:10:36 +02:00
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 16:46:51 +02:00
|
|
|
static void search_callback(void *arg, int status, int timeouts,
|
|
|
|
unsigned char *abuf, int alen)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
struct search_query *squery = (struct search_query *) arg;
|
|
|
|
ares_channel channel = squery->channel;
|
|
|
|
char *s;
|
2007-10-16 23:27:51 +02:00
|
|
|
|
2007-09-28 16:46:51 +02:00
|
|
|
squery->timeouts += timeouts;
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* Stop searching unless we got a non-fatal error. */
|
|
|
|
if (status != ARES_ENODATA && status != ARES_ESERVFAIL
|
|
|
|
&& status != ARES_ENOTFOUND)
|
|
|
|
end_squery(squery, status, abuf, alen);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save the status if we were trying as-is. */
|
|
|
|
if (squery->trying_as_is)
|
2004-10-06 09:50:18 +02:00
|
|
|
squery->status_as_is = status;
|
2007-10-16 23:27:51 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we ever get ARES_ENODATA along the way, record that; if the search
|
|
|
|
* should run to the very end and we got at least one ARES_ENODATA,
|
|
|
|
* then callers like ares_gethostbyname() may want to try a T_A search
|
|
|
|
* even if the last domain we queried for T_AAAA resource records
|
|
|
|
* returned ARES_ENOTFOUND.
|
|
|
|
*/
|
|
|
|
if (status == ARES_ENODATA)
|
|
|
|
squery->ever_got_nodata = 1;
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
if (squery->next_domain < channel->ndomains)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* Try the next domain. */
|
|
|
|
status = cat_domain(squery->name,
|
|
|
|
channel->domains[squery->next_domain], &s);
|
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
end_squery(squery, status, NULL, 0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
squery->trying_as_is = 0;
|
|
|
|
squery->next_domain++;
|
|
|
|
ares_query(channel, s, squery->dnsclass, squery->type,
|
|
|
|
search_callback, squery);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
else if (squery->status_as_is == -1)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* Try the name as-is at the end. */
|
|
|
|
squery->trying_as_is = 1;
|
|
|
|
ares_query(channel, squery->name, squery->dnsclass, squery->type,
|
|
|
|
search_callback, squery);
|
|
|
|
}
|
2007-10-16 23:27:51 +02:00
|
|
|
else {
|
|
|
|
if (squery->status_as_is == ARES_ENOTFOUND && squery->ever_got_nodata) {
|
|
|
|
end_squery(squery, ARES_ENODATA, NULL, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
end_squery(squery, squery->status_as_is, NULL, 0);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void end_squery(struct search_query *squery, int status,
|
2004-10-06 09:50:18 +02:00
|
|
|
unsigned char *abuf, int alen)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2007-09-28 16:46:51 +02:00
|
|
|
squery->callback(squery->arg, status, squery->timeouts, abuf, alen);
|
2003-10-07 23:54:04 +02:00
|
|
|
free(squery->name);
|
|
|
|
free(squery);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Concatenate two domains. */
|
|
|
|
static int cat_domain(const char *name, const char *domain, char **s)
|
|
|
|
{
|
2004-02-23 09:04:31 +01:00
|
|
|
size_t nlen = strlen(name);
|
|
|
|
size_t dlen = strlen(domain);
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
*s = malloc(nlen + 1 + dlen + 1);
|
|
|
|
if (!*s)
|
|
|
|
return ARES_ENOMEM;
|
|
|
|
memcpy(*s, name, nlen);
|
|
|
|
(*s)[nlen] = '.';
|
|
|
|
memcpy(*s + nlen + 1, domain, dlen);
|
|
|
|
(*s)[nlen + 1 + dlen] = 0;
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine if this name only yields one query. If it does, set *s to
|
|
|
|
* the string we should query, in an allocated buffer. If not, set *s
|
|
|
|
* to NULL.
|
|
|
|
*/
|
|
|
|
static int single_domain(ares_channel channel, const char *name, char **s)
|
|
|
|
{
|
2004-02-23 09:04:31 +01:00
|
|
|
size_t len = strlen(name);
|
2003-10-07 23:54:04 +02:00
|
|
|
const char *hostaliases;
|
|
|
|
FILE *fp;
|
|
|
|
char *line = NULL;
|
|
|
|
int linesize, status;
|
|
|
|
const char *p, *q;
|
2007-02-16 20:17:05 +01:00
|
|
|
int error;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* If the name contains a trailing dot, then the single query is the name
|
|
|
|
* sans the trailing dot.
|
|
|
|
*/
|
|
|
|
if (name[len - 1] == '.')
|
|
|
|
{
|
|
|
|
*s = strdup(name);
|
|
|
|
return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(channel->flags & ARES_FLAG_NOALIASES) && !strchr(name, '.'))
|
|
|
|
{
|
|
|
|
/* The name might be a host alias. */
|
|
|
|
hostaliases = getenv("HOSTALIASES");
|
|
|
|
if (hostaliases)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
fp = fopen(hostaliases, "r");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
while ((status = ares__read_line(fp, &line, &linesize))
|
|
|
|
== ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
if (strncasecmp(line, name, len) != 0 ||
|
2006-10-18 05:42:06 +02:00
|
|
|
!ISSPACE(line[len]))
|
2004-10-06 09:50:18 +02:00
|
|
|
continue;
|
|
|
|
p = line + len;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (ISSPACE(*p))
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
|
|
|
if (*p)
|
|
|
|
{
|
|
|
|
q = p + 1;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (*q && !ISSPACE(*q))
|
2004-10-06 09:50:18 +02:00
|
|
|
q++;
|
|
|
|
*s = malloc(q - p + 1);
|
|
|
|
if (*s)
|
|
|
|
{
|
|
|
|
memcpy(*s, p, q - p);
|
|
|
|
(*s)[q - p] = 0;
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
fclose(fp);
|
|
|
|
return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
fclose(fp);
|
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
return status;
|
|
|
|
}
|
2007-02-16 20:17:05 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
error = ERRNO;
|
|
|
|
switch(error)
|
|
|
|
{
|
|
|
|
case ENOENT:
|
2007-02-19 03:03:58 +01:00
|
|
|
case ESRCH:
|
2007-02-16 20:17:05 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
|
|
|
|
error, strerror(error)));
|
|
|
|
DEBUGF(fprintf(stderr, "Error opening file: %s\n",
|
|
|
|
hostaliases));
|
|
|
|
*s = NULL;
|
|
|
|
return ARES_EFILE;
|
|
|
|
}
|
|
|
|
}
|
2004-10-06 09:50:18 +02:00
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (channel->flags & ARES_FLAG_NOSEARCH || channel->ndomains == 0)
|
|
|
|
{
|
|
|
|
/* No domain search to do; just try the name as-is. */
|
|
|
|
*s = strdup(name);
|
|
|
|
return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = NULL;
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|