Blame ares_gethostbyname.c

Packit 514978
Packit 514978
/* Copyright 1998, 2011, 2013 by the Massachusetts Institute of Technology.
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this
Packit 514978
 * software and its documentation for any purpose and without
Packit 514978
 * fee is hereby granted, provided that the above copyright
Packit 514978
 * notice appear in all copies and that both that copyright
Packit 514978
 * notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in
Packit 514978
 * advertising or publicity pertaining to distribution of the
Packit 514978
 * software without specific, written prior permission.
Packit 514978
 * M.I.T. makes no representations about the suitability of
Packit 514978
 * this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
Packit 514978
#ifdef HAVE_NETINET_IN_H
Packit 514978
#  include <netinet/in.h>
Packit 514978
#endif
Packit 514978
#ifdef HAVE_NETDB_H
Packit 514978
#  include <netdb.h>
Packit 514978
#endif
Packit 514978
#ifdef HAVE_ARPA_INET_H
Packit 514978
#  include <arpa/inet.h>
Packit 514978
#endif
Packit 514978
#ifdef HAVE_ARPA_NAMESER_H
Packit 514978
#  include <arpa/nameser.h>
Packit 514978
#else
Packit 514978
#  include "nameser.h"
Packit 514978
#endif
Packit 514978
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
Packit 514978
#  include <arpa/nameser_compat.h>
Packit 514978
#endif
Packit 514978
Packit 514978
#ifdef HAVE_STRINGS_H
Packit 514978
#include <strings.h>
Packit 514978
#endif
Packit 514978
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_inet_net_pton.h"
Packit 514978
#include "bitncmp.h"
Packit 514978
#include "ares_platform.h"
Packit 514978
#include "ares_nowarn.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
#ifdef WATT32
Packit 514978
#undef WIN32
Packit 514978
#endif
Packit 514978
Packit 514978
struct host_query {
Packit 514978
  /* Arguments passed to ares_gethostbyname() */
Packit 514978
  ares_channel channel;
Packit 514978
  char *name;
Packit 514978
  ares_host_callback callback;
Packit 514978
  void *arg;
Packit 514978
  int sent_family; /* this family is what was is being used */
Packit 514978
  int want_family; /* this family is what is asked for in the API */
Packit 514978
  const char *remaining_lookups;
Packit 514978
  int timeouts;
Packit 514978
};
Packit 514978
Packit 514978
static void next_lookup(struct host_query *hquery, int status_code);
Packit 514978
static void host_callback(void *arg, int status, int timeouts,
Packit 514978
                          unsigned char *abuf, int alen);
Packit 514978
static void end_hquery(struct host_query *hquery, int status,
Packit 514978
                       struct hostent *host);
Packit 514978
static int fake_hostent(const char *name, int family,
Packit 514978
                        ares_host_callback callback, void *arg);
Packit 514978
static int file_lookup(const char *name, int family, struct hostent **host);
Packit 514978
static void sort_addresses(struct hostent *host,
Packit 514978
                           const struct apattern *sortlist, int nsort);
Packit 514978
static void sort6_addresses(struct hostent *host,
Packit 514978
                            const struct apattern *sortlist, int nsort);
Packit 514978
static int get_address_index(const struct in_addr *addr,
Packit 514978
                             const struct apattern *sortlist, int nsort);
Packit 514978
static int get6_address_index(const struct ares_in6_addr *addr,
Packit 514978
                              const struct apattern *sortlist, int nsort);
Packit 514978
Packit 514978
void ares_gethostbyname(ares_channel channel, const char *name, int family,
Packit 514978
                        ares_host_callback callback, void *arg)
Packit 514978
{
Packit 514978
  struct host_query *hquery;
Packit 514978
Packit 514978
  /* Right now we only know how to look up Internet addresses - and unspec
Packit 514978
     means try both basically. */
Packit 514978
  switch (family) {
Packit 514978
  case AF_INET:
Packit 514978
  case AF_INET6:
Packit 514978
  case AF_UNSPEC:
Packit 514978
    break;
Packit 514978
  default:
Packit 514978
    callback(arg, ARES_ENOTIMP, 0, NULL);
Packit 514978
    return;
Packit 514978
  }
Packit 514978
Packit 514978
  if (fake_hostent(name, family, callback, arg))
Packit 514978
    return;
Packit 514978
Packit 514978
  /* Allocate and fill in the host query structure. */
Packit 514978
  hquery = ares_malloc(sizeof(struct host_query));
Packit 514978
  if (!hquery)
Packit 514978
    {
Packit 514978
      callback(arg, ARES_ENOMEM, 0, NULL);
Packit 514978
      return;
Packit 514978
    }
Packit 514978
  hquery->channel = channel;
Packit 514978
  hquery->name = ares_strdup(name);
Packit 514978
  hquery->want_family = family;
Packit 514978
  hquery->sent_family = -1; /* nothing is sent yet */
Packit 514978
  if (!hquery->name) {
Packit 514978
    ares_free(hquery);
Packit 514978
    callback(arg, ARES_ENOMEM, 0, NULL);
Packit 514978
    return;
Packit 514978
  }
Packit 514978
  hquery->callback = callback;
Packit 514978
  hquery->arg = arg;
Packit 514978
  hquery->remaining_lookups = channel->lookups;
Packit 514978
  hquery->timeouts = 0;
Packit 514978
Packit 514978
  /* Start performing lookups according to channel->lookups. */
Packit 514978
  next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */);
Packit 514978
}
Packit 514978
Packit 514978
static void next_lookup(struct host_query *hquery, int status_code)
Packit 514978
{
Packit 514978
  const char *p;
Packit 514978
  struct hostent *host;
Packit 514978
  int status = status_code;
Packit 514978
Packit 514978
  for (p = hquery->remaining_lookups; *p; p++)
Packit 514978
    {
Packit 514978
      switch (*p)
Packit 514978
        {
Packit 514978
        case 'b':
Packit 514978
          /* DNS lookup */
Packit 514978
          hquery->remaining_lookups = p + 1;
Packit 514978
          if ((hquery->want_family == AF_INET6) ||
Packit 514978
              (hquery->want_family == AF_UNSPEC)) {
Packit 514978
            /* if inet6 or unspec, start out with AAAA */
Packit 514978
            hquery->sent_family = AF_INET6;
Packit 514978
            ares_search(hquery->channel, hquery->name, C_IN, T_AAAA,
Packit 514978
                        host_callback, hquery);
Packit 514978
          }
Packit 514978
          else {
Packit 514978
            hquery->sent_family = AF_INET;
Packit 514978
            ares_search(hquery->channel, hquery->name, C_IN, T_A,
Packit 514978
                        host_callback, hquery);
Packit 514978
          }
Packit 514978
          return;
Packit 514978
Packit 514978
        case 'f':
Packit 514978
          /* Host file lookup */
Packit 514978
          status = file_lookup(hquery->name, hquery->want_family, &host);
Packit 514978
Packit 514978
          /* this status check below previously checked for !ARES_ENOTFOUND,
Packit 514978
             but we should not assume that this single error code is the one
Packit 514978
             that can occur, as that is in fact no longer the case */
Packit 514978
          if (status == ARES_SUCCESS)
Packit 514978
            {
Packit 514978
              end_hquery(hquery, status, host);
Packit 514978
              return;
Packit 514978
            }
Packit 514978
          status = status_code;   /* Use original status code */
Packit 514978
          break;
Packit 514978
        }
Packit 514978
    }
Packit 514978
  end_hquery(hquery, status, NULL);
Packit 514978
}
Packit 514978
Packit 514978
static void host_callback(void *arg, int status, int timeouts,
Packit 514978
                          unsigned char *abuf, int alen)
Packit 514978
{
Packit 514978
  struct host_query *hquery = (struct host_query *) arg;
Packit 514978
  ares_channel channel = hquery->channel;
Packit 514978
  struct hostent *host = NULL;
Packit 514978
Packit 514978
  hquery->timeouts += timeouts;
Packit 514978
  if (status == ARES_SUCCESS)
Packit 514978
    {
Packit 514978
      if (hquery->sent_family == AF_INET)
Packit 514978
        {
Packit 514978
          status = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
Packit 514978
          if (host && channel->nsort)
Packit 514978
            sort_addresses(host, channel->sortlist, channel->nsort);
Packit 514978
        }
Packit 514978
      else if (hquery->sent_family == AF_INET6)
Packit 514978
        {
Packit 514978
          status = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL);
Packit 514978
          if ((status == ARES_ENODATA || status == ARES_EBADRESP ||
Packit 514978
               (status == ARES_SUCCESS && host && host->h_addr_list[0] == NULL)) &&
Packit 514978
                hquery->want_family == AF_UNSPEC) {
Packit 514978
            /* The query returned something but either there were no AAAA
Packit 514978
               records (e.g. just CNAME) or the response was malformed.  Try
Packit 514978
               looking up A instead. */
Packit 514978
            if (host)
Packit 514978
              ares_free_hostent(host);
Packit 514978
            hquery->sent_family = AF_INET;
Packit 514978
            ares_search(hquery->channel, hquery->name, C_IN, T_A,
Packit 514978
                        host_callback, hquery);
Packit 514978
            return;
Packit 514978
          }
Packit 514978
          if (host && channel->nsort)
Packit 514978
            sort6_addresses(host, channel->sortlist, channel->nsort);
Packit 514978
        }
Packit 514978
      end_hquery(hquery, status, host);
Packit 514978
    }
Packit 514978
  else if ((status == ARES_ENODATA || status == ARES_EBADRESP ||
Packit 514978
            status == ARES_ETIMEOUT) && (hquery->sent_family == AF_INET6 &&
Packit 514978
            hquery->want_family == AF_UNSPEC))
Packit 514978
    {
Packit 514978
      /* The AAAA query yielded no useful result.  Now look up an A instead. */
Packit 514978
      hquery->sent_family = AF_INET;
Packit 514978
      ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
Packit 514978
                  hquery);
Packit 514978
    }
Packit 514978
  else if (status == ARES_EDESTRUCTION)
Packit 514978
    end_hquery(hquery, status, NULL);
Packit 514978
  else
Packit 514978
    next_lookup(hquery, status);
Packit 514978
}
Packit 514978
Packit 514978
static void end_hquery(struct host_query *hquery, int status,
Packit 514978
                       struct hostent *host)
Packit 514978
{
Packit 514978
  hquery->callback(hquery->arg, status, hquery->timeouts, host);
Packit 514978
  if (host)
Packit 514978
    ares_free_hostent(host);
Packit 514978
  ares_free(hquery->name);
Packit 514978
  ares_free(hquery);
Packit 514978
}
Packit 514978
Packit 514978
/* If the name looks like an IP address, fake up a host entry, end the
Packit 514978
 * query immediately, and return true.  Otherwise return false.
Packit 514978
 */
Packit 514978
static int fake_hostent(const char *name, int family,
Packit 514978
                        ares_host_callback callback, void *arg)
Packit 514978
{
Packit 514978
  struct hostent hostent;
Packit 514978
  char *aliases[1] = { NULL };
Packit 514978
  char *addrs[2];
Packit 514978
  int result = 0;
Packit 514978
  struct in_addr in;
Packit 514978
  struct ares_in6_addr in6;
Packit 514978
Packit 514978
  if (family == AF_INET || family == AF_INET6)
Packit 514978
    {
Packit 514978
      /* It only looks like an IP address if it's all numbers and dots. */
Packit 514978
      int numdots = 0, valid = 1;
Packit 514978
      const char *p;
Packit 514978
      for (p = name; *p; p++)
Packit 514978
        {
Packit 514978
          if (!ISDIGIT(*p) && *p != '.') {
Packit 514978
            valid = 0;
Packit 514978
            break;
Packit 514978
          } else if (*p == '.') {
Packit 514978
            numdots++;
Packit 514978
          }
Packit 514978
        }
Packit 514978
Packit 514978
      /* if we don't have 3 dots, it is illegal
Packit 514978
       * (although inet_addr doesn't think so).
Packit 514978
       */
Packit 514978
      if (numdots != 3 || !valid)
Packit 514978
        result = 0;
Packit 514978
      else
Packit 514978
        result = ((in.s_addr = inet_addr(name)) == INADDR_NONE ? 0 : 1);
Packit 514978
Packit 514978
      if (result)
Packit 514978
        family = AF_INET;
Packit 514978
    }
Packit 514978
  if (family == AF_INET6)
Packit 514978
    result = (ares_inet_pton(AF_INET6, name, &in6) < 1 ? 0 : 1);
Packit 514978
Packit 514978
  if (!result)
Packit 514978
    return 0;
Packit 514978
Packit 514978
  if (family == AF_INET)
Packit 514978
    {
Packit 514978
      hostent.h_length = (int)sizeof(struct in_addr);
Packit 514978
      addrs[0] = (char *)∈
Packit 514978
    }
Packit 514978
  else if (family == AF_INET6)
Packit 514978
    {
Packit 514978
      hostent.h_length = (int)sizeof(struct ares_in6_addr);
Packit 514978
      addrs[0] = (char *)&in6;
Packit 514978
    }
Packit 514978
  /* Duplicate the name, to avoid a constness violation. */
Packit 514978
  hostent.h_name = ares_strdup(name);
Packit 514978
  if (!hostent.h_name)
Packit 514978
    {
Packit 514978
      callback(arg, ARES_ENOMEM, 0, NULL);
Packit 514978
      return 1;
Packit 514978
    }
Packit 514978
Packit 514978
  /* Fill in the rest of the host structure and terminate the query. */
Packit 514978
  addrs[1] = NULL;
Packit 514978
  hostent.h_aliases = aliases;
Packit 514978
  hostent.h_addrtype = aresx_sitoss(family);
Packit 514978
  hostent.h_addr_list = addrs;
Packit 514978
  callback(arg, ARES_SUCCESS, 0, &hostent);
Packit 514978
Packit 514978
  ares_free((char *)(hostent.h_name));
Packit 514978
  return 1;
Packit 514978
}
Packit 514978
Packit 514978
/* This is an API method */
Packit 514978
int ares_gethostbyname_file(ares_channel channel, const char *name,
Packit 514978
                            int family, struct hostent **host)
Packit 514978
{
Packit 514978
  int result;
Packit 514978
Packit 514978
  /* We only take the channel to ensure that ares_init() been called. */
Packit 514978
  if(channel == NULL)
Packit 514978
    {
Packit 514978
      /* Anything will do, really.  This seems fine, and is consistent with
Packit 514978
         other error cases. */
Packit 514978
      *host = NULL;
Packit 514978
      return ARES_ENOTFOUND;
Packit 514978
    }
Packit 514978
Packit 514978
  /* Just chain to the internal implementation we use here; it's exactly
Packit 514978
   * what we want.
Packit 514978
   */
Packit 514978
  result = file_lookup(name, family, host);
Packit 514978
  if(result != ARES_SUCCESS)
Packit 514978
    {
Packit 514978
      /* We guarantee a NULL hostent on failure. */
Packit 514978
      *host = NULL;
Packit 514978
    }
Packit 514978
  return result;
Packit 514978
}
Packit 514978
Packit 514978
static int file_lookup(const char *name, int family, struct hostent **host)
Packit 514978
{
Packit 514978
  FILE *fp;
Packit 514978
  char **alias;
Packit 514978
  int status;
Packit 514978
  int error;
Packit 514978
Packit 514978
#ifdef WIN32
Packit 514978
  char PATH_HOSTS[MAX_PATH];
Packit 514978
  win_platform platform;
Packit 514978
Packit 514978
  PATH_HOSTS[0] = '\0';
Packit 514978
Packit 514978
  platform = ares__getplatform();
Packit 514978
Packit 514978
  if (platform == WIN_NT) {
Packit 514978
    char tmp[MAX_PATH];
Packit 514978
    HKEY hkeyHosts;
Packit 514978
Packit 514978
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ,
Packit 514978
                     &hkeyHosts) == ERROR_SUCCESS)
Packit 514978
    {
Packit 514978
      DWORD dwLength = MAX_PATH;
Packit 514978
      RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
Packit 514978
                      &dwLength);
Packit 514978
      ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
Packit 514978
      RegCloseKey(hkeyHosts);
Packit 514978
    }
Packit 514978
  }
Packit 514978
  else if (platform == WIN_9X)
Packit 514978
    GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
Packit 514978
  else
Packit 514978
    return ARES_ENOTFOUND;
Packit 514978
Packit 514978
  strcat(PATH_HOSTS, WIN_PATH_HOSTS);
Packit 514978
Packit 514978
#elif defined(WATT32)
Packit 514978
  extern const char *_w32_GetHostsFile (void);
Packit 514978
  const char *PATH_HOSTS = _w32_GetHostsFile();
Packit 514978
Packit 514978
  if (!PATH_HOSTS)
Packit 514978
    return ARES_ENOTFOUND;
Packit 514978
#endif
Packit 514978
Packit 514978
  fp = fopen(PATH_HOSTS, "r");
Packit 514978
  if (!fp)
Packit 514978
    {
Packit 514978
      error = ERRNO;
Packit 514978
      switch(error)
Packit 514978
        {
Packit 514978
        case ENOENT:
Packit 514978
        case ESRCH:
Packit 514978
          return ARES_ENOTFOUND;
Packit 514978
        default:
Packit 514978
          DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
Packit 514978
                         error, strerror(error)));
Packit 514978
          DEBUGF(fprintf(stderr, "Error opening file: %s\n",
Packit 514978
                         PATH_HOSTS));
Packit 514978
          *host = NULL;
Packit 514978
          return ARES_EFILE;
Packit 514978
        }
Packit 514978
    }
Packit 514978
  while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
Packit 514978
    {
Packit 514978
      if (strcasecmp((*host)->h_name, name) == 0)
Packit 514978
        break;
Packit 514978
      for (alias = (*host)->h_aliases; *alias; alias++)
Packit 514978
        {
Packit 514978
          if (strcasecmp(*alias, name) == 0)
Packit 514978
            break;
Packit 514978
        }
Packit 514978
      if (*alias)
Packit 514978
        break;
Packit 514978
      ares_free_hostent(*host);
Packit 514978
    }
Packit 514978
  fclose(fp);
Packit 514978
  if (status == ARES_EOF)
Packit 514978
    status = ARES_ENOTFOUND;
Packit 514978
  if (status != ARES_SUCCESS)
Packit 514978
    *host = NULL;
Packit 514978
  return status;
Packit 514978
}
Packit 514978
Packit 514978
static void sort_addresses(struct hostent *host,
Packit 514978
                           const struct apattern *sortlist, int nsort)
Packit 514978
{
Packit 514978
  struct in_addr a1, a2;
Packit 514978
  int i1, i2, ind1, ind2;
Packit 514978
Packit 514978
  /* This is a simple insertion sort, not optimized at all.  i1 walks
Packit 514978
   * through the address list, with the loop invariant that everything
Packit 514978
   * to the left of i1 is sorted.  In the loop body, the value at i1 is moved
Packit 514978
   * back through the list (via i2) until it is in sorted order.
Packit 514978
   */
Packit 514978
  for (i1 = 0; host->h_addr_list[i1]; i1++)
Packit 514978
    {
Packit 514978
      memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
Packit 514978
      ind1 = get_address_index(&a1, sortlist, nsort);
Packit 514978
      for (i2 = i1 - 1; i2 >= 0; i2--)
Packit 514978
        {
Packit 514978
          memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
Packit 514978
          ind2 = get_address_index(&a2, sortlist, nsort);
Packit 514978
          if (ind2 <= ind1)
Packit 514978
            break;
Packit 514978
          memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
Packit 514978
        }
Packit 514978
      memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
Packit 514978
    }
Packit 514978
}
Packit 514978
Packit 514978
/* Find the first entry in sortlist which matches addr.  Return nsort
Packit 514978
 * if none of them match.
Packit 514978
 */
Packit 514978
static int get_address_index(const struct in_addr *addr,
Packit 514978
                             const struct apattern *sortlist,
Packit 514978
                             int nsort)
Packit 514978
{
Packit 514978
  int i;
Packit 514978
Packit 514978
  for (i = 0; i < nsort; i++)
Packit 514978
    {
Packit 514978
      if (sortlist[i].family != AF_INET)
Packit 514978
        continue;
Packit 514978
      if (sortlist[i].type == PATTERN_MASK)
Packit 514978
        {
Packit 514978
          if ((addr->s_addr & sortlist[i].mask.addr4.s_addr)
Packit 514978
              == sortlist[i].addrV4.s_addr)
Packit 514978
            break;
Packit 514978
        }
Packit 514978
      else
Packit 514978
        {
Packit 514978
          if (!ares__bitncmp(&addr->s_addr, &sortlist[i].addrV4.s_addr,
Packit 514978
                             sortlist[i].mask.bits))
Packit 514978
            break;
Packit 514978
        }
Packit 514978
    }
Packit 514978
  return i;
Packit 514978
}
Packit 514978
Packit 514978
static void sort6_addresses(struct hostent *host,
Packit 514978
                            const struct apattern *sortlist, int nsort)
Packit 514978
{
Packit 514978
  struct ares_in6_addr a1, a2;
Packit 514978
  int i1, i2, ind1, ind2;
Packit 514978
Packit 514978
  /* This is a simple insertion sort, not optimized at all.  i1 walks
Packit 514978
   * through the address list, with the loop invariant that everything
Packit 514978
   * to the left of i1 is sorted.  In the loop body, the value at i1 is moved
Packit 514978
   * back through the list (via i2) until it is in sorted order.
Packit 514978
   */
Packit 514978
  for (i1 = 0; host->h_addr_list[i1]; i1++)
Packit 514978
    {
Packit 514978
      memcpy(&a1, host->h_addr_list[i1], sizeof(struct ares_in6_addr));
Packit 514978
      ind1 = get6_address_index(&a1, sortlist, nsort);
Packit 514978
      for (i2 = i1 - 1; i2 >= 0; i2--)
Packit 514978
        {
Packit 514978
          memcpy(&a2, host->h_addr_list[i2], sizeof(struct ares_in6_addr));
Packit 514978
          ind2 = get6_address_index(&a2, sortlist, nsort);
Packit 514978
          if (ind2 <= ind1)
Packit 514978
            break;
Packit 514978
          memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct ares_in6_addr));
Packit 514978
        }
Packit 514978
      memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct ares_in6_addr));
Packit 514978
    }
Packit 514978
}
Packit 514978
Packit 514978
/* Find the first entry in sortlist which matches addr.  Return nsort
Packit 514978
 * if none of them match.
Packit 514978
 */
Packit 514978
static int get6_address_index(const struct ares_in6_addr *addr,
Packit 514978
                              const struct apattern *sortlist,
Packit 514978
                              int nsort)
Packit 514978
{
Packit 514978
  int i;
Packit 514978
Packit 514978
  for (i = 0; i < nsort; i++)
Packit 514978
    {
Packit 514978
      if (sortlist[i].family != AF_INET6)
Packit 514978
        continue;
Packit 514978
      if (!ares__bitncmp(addr, &sortlist[i].addrV6, sortlist[i].mask.bits))
Packit 514978
        break;
Packit 514978
    }
Packit 514978
  return i;
Packit 514978
}