~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to rutil/dns/ares/ares_gethostbyname.c

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 1998 by the Massachusetts Institute of Technology.
 
2
 *
 
3
 * Permission to use, copy, modify, and distribute this
 
4
 * software and its documentation for any purpose and without
 
5
 * fee is hereby granted, provided that the above copyright
 
6
 * notice appear in all copies and that both that copyright
 
7
 * notice and this permission notice appear in supporting
 
8
 * documentation, and that the name of M.I.T. not be used in
 
9
 * advertising or publicity pertaining to distribution of the
 
10
 * software without specific, written prior permission.
 
11
 * M.I.T. makes no representations about the suitability of
 
12
 * this software for any purpose.  It is provided "as is"
 
13
 * without express or implied warranty.
 
14
 */
 
15
 
 
16
 
 
17
#include <sys/types.h>
 
18
#include <stdio.h>
 
19
#include <stdlib.h>
 
20
#include <string.h>
 
21
#include <ctype.h>
 
22
 
 
23
#ifndef WIN32
 
24
#include <sys/socket.h>
 
25
#include <netinet/in.h>
 
26
#include <arpa/inet.h>
 
27
#ifndef __CYGWIN__
 
28
#  include <arpa/nameser.h>
 
29
#endif
 
30
#include <netdb.h>
 
31
#endif
 
32
 
 
33
#include "ares.h"
 
34
#include "ares_private.h"
 
35
#include "ares_local.h"
 
36
 
 
37
struct host_query {
 
38
  /* Arguments passed to ares_gethostbyname() */
 
39
  ares_channel channel;
 
40
  char *name;
 
41
  ares_host_callback callback;
 
42
  void *arg;
 
43
 
 
44
  const char *remaining_lookups;
 
45
};
 
46
 
 
47
static void next_lookup(struct host_query *hquery);
 
48
static void host_callback(void *arg, int status, unsigned char *abuf,
 
49
                          int alen);
 
50
static void end_hquery(struct host_query *hquery, int status,
 
51
                       struct hostent *host);
 
52
static int fake_hostent(const char *name, ares_host_callback callback,
 
53
                        void *arg);
 
54
static int file_lookup(const char *name, struct hostent **host);
 
55
static void sort_addresses(struct hostent *host, struct apattern *sortlist,
 
56
                           int nsort);
 
57
static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
 
58
                             int nsort);
 
59
 
 
60
#ifdef WIN32
 
61
extern char w32hostspath[];
 
62
#endif
 
63
 
 
64
void ares_gethostbyname(ares_channel channel, const char *name, int family,
 
65
                        ares_host_callback callback, void *arg)
 
66
{
 
67
  struct host_query *hquery;
 
68
 
 
69
  /* See if request can be handled by local pseudo-domain DNS */
 
70
  if (ares_local_gethostbyname(channel, name, family, callback, arg))
 
71
  {
 
72
      return;
 
73
  }
 
74
 
 
75
  /* Right now we only know how to look up Internet addresses. */
 
76
  if (family != AF_INET)
 
77
    {
 
78
      callback(arg, ARES_ENOTIMP, NULL);
 
79
      return;
 
80
    }
 
81
 
 
82
  if (fake_hostent(name, callback, arg))
 
83
    return;
 
84
 
 
85
  /* Allocate and fill in the host query structure. */
 
86
  hquery = malloc(sizeof(struct host_query));
 
87
  if (!hquery)
 
88
    {
 
89
      callback(arg, ARES_ENOMEM, NULL);
 
90
      return;
 
91
    }
 
92
  hquery->channel = channel;
 
93
  hquery->name = strdup(name);
 
94
  if (!hquery->name)
 
95
    {
 
96
      free(hquery);
 
97
      callback(arg, ARES_ENOMEM, NULL);
 
98
      return;
 
99
    }
 
100
  hquery->callback = callback;
 
101
  hquery->arg = arg;
 
102
  hquery->remaining_lookups = channel->lookups;
 
103
 
 
104
  /* Start performing lookups according to channel->lookups. */
 
105
  next_lookup(hquery);
 
106
}
 
107
 
 
108
static void next_lookup(struct host_query *hquery)
 
109
{
 
110
  int status;
 
111
  const char *p;
 
112
  struct hostent *host;
 
113
 
 
114
  for (p = hquery->remaining_lookups; *p; p++)
 
115
    {
 
116
      switch (*p)
 
117
        {
 
118
        case 'b':
 
119
          /* DNS lookup */
 
120
          hquery->remaining_lookups = p + 1;
 
121
          ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
 
122
                      hquery);
 
123
          return;
 
124
 
 
125
        case 'f':
 
126
          /* Host file lookup */
 
127
          status = file_lookup(hquery->name, &host);
 
128
          if (status != ARES_ENOTFOUND)
 
129
            {
 
130
              end_hquery(hquery, status, host);
 
131
              return;
 
132
            }
 
133
          break;
 
134
        }
 
135
    }
 
136
  end_hquery(hquery, ARES_ENOTFOUND, NULL);
 
137
}
 
138
 
 
139
static void host_callback(void *arg, int status, unsigned char *abuf, int alen)
 
140
{
 
141
  struct host_query *hquery = (struct host_query *) arg;
 
142
  ares_channel channel = hquery->channel;
 
143
  struct hostent *host;
 
144
 
 
145
  if (status == ARES_SUCCESS)
 
146
    {
 
147
      status = ares_parse_a_reply(abuf, alen, &host);
 
148
      if (host && channel->nsort)
 
149
        sort_addresses(host, channel->sortlist, channel->nsort);
 
150
      end_hquery(hquery, status, host);
 
151
    }
 
152
  else if (status == ARES_EDESTRUCTION)
 
153
    end_hquery(hquery, status, NULL);
 
154
  else
 
155
    next_lookup(hquery);
 
156
}
 
157
 
 
158
static void end_hquery(struct host_query *hquery, int status,
 
159
                       struct hostent *host)
 
160
{
 
161
  hquery->callback(hquery->arg, status, host);
 
162
  if (host)
 
163
    ares_free_hostent(host);
 
164
  free(hquery->name);
 
165
  free(hquery);
 
166
}
 
167
 
 
168
/* If the name looks like an IP address, fake up a host entry, end the
 
169
 * query immediately, and return true.  Otherwise return false.
 
170
 */
 
171
static int fake_hostent(const char *name, ares_host_callback callback,
 
172
                        void *arg)
 
173
{
 
174
  struct in_addr addr;
 
175
  struct hostent hostent;
 
176
  const char *p;
 
177
  char *aliases[1] = { NULL };
 
178
  char *addrs[2];
 
179
 
 
180
  /* It only looks like an IP address if it's all numbers and dots. */
 
181
  for (p = name; *p; p++)
 
182
    {
 
183
      if (!isdigit((unsigned char)*p) && *p != '.')
 
184
        return 0;
 
185
    }
 
186
 
 
187
  /* It also only looks like an IP address if it's non-zero-length and
 
188
   * doesn't end with a dot.
 
189
   */
 
190
  if (p == name || *(p - 1) == '.')
 
191
    return 0;
 
192
 
 
193
  /* It looks like an IP address.  Figure out what IP address it is. */
 
194
  addr.s_addr = inet_addr(name);
 
195
  if (addr.s_addr == INADDR_NONE)
 
196
    {
 
197
      callback(arg, ARES_EBADNAME, NULL);
 
198
      return 1;
 
199
    }
 
200
 
 
201
  /* Duplicate the name, to avoid a constness violation. */
 
202
  hostent.h_name = strdup(name);
 
203
  if (!hostent.h_name)
 
204
    {
 
205
      callback(arg, ARES_ENOMEM, NULL);
 
206
      return 1;
 
207
    }
 
208
 
 
209
  /* Fill in the rest of the host structure and terminate the query. */
 
210
  addrs[0] = (char *) &addr;
 
211
  addrs[1] = NULL;
 
212
  hostent.h_aliases = aliases;
 
213
  hostent.h_addrtype = AF_INET;
 
214
  hostent.h_length = sizeof(struct in_addr);
 
215
  hostent.h_addr_list = addrs;
 
216
  callback(arg, ARES_SUCCESS, &hostent);
 
217
 
 
218
  free(hostent.h_name);
 
219
  return 1;
 
220
}
 
221
 
 
222
int hostfile_lookup(const char *name, struct hostent **host)
 
223
{
 
224
   return file_lookup(name, host);
 
225
}
 
226
 
 
227
static int file_lookup(const char *name, struct hostent **host)
 
228
{
 
229
  FILE *fp;
 
230
  char **alias;
 
231
  int status;
 
232
 
 
233
#ifdef WIN32
 
234
  fp = fopen(w32hostspath, "r");
 
235
#else
 
236
  fp = fopen(PATH_HOSTS, "r");
 
237
#endif
 
238
  if (!fp)
 
239
    return ARES_ENOTFOUND;
 
240
 
 
241
  while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
 
242
    {
 
243
      if (strcasecmp((*host)->h_name, name) == 0)
 
244
        break;
 
245
      for (alias = (*host)->h_aliases; *alias; alias++)
 
246
        {
 
247
          if (strcasecmp(*alias, name) == 0)
 
248
            break;
 
249
        }
 
250
      if (*alias)
 
251
        break;
 
252
      ares_free_hostent(*host);
 
253
    }
 
254
  fclose(fp);
 
255
  if (status == ARES_EOF)
 
256
    status = ARES_ENOTFOUND;
 
257
  if (status != ARES_SUCCESS)
 
258
    *host = NULL;
 
259
  return status;
 
260
}
 
261
 
 
262
static void sort_addresses(struct hostent *host, struct apattern *sortlist,
 
263
                           int nsort)
 
264
{
 
265
  struct in_addr a1, a2;
 
266
  int i1, i2, ind1, ind2;
 
267
 
 
268
  /* This is a simple insertion sort, not optimized at all.  i1 walks
 
269
   * through the address list, with the loop invariant that everything
 
270
   * to the left of i1 is sorted.  In the loop body, the value at i1 is moved
 
271
   * back through the list (via i2) until it is in sorted order.
 
272
   */
 
273
  for (i1 = 0; host->h_addr_list[i1]; i1++)
 
274
    {
 
275
      memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
 
276
      ind1 = get_address_index(&a1, sortlist, nsort);
 
277
      for (i2 = i1 - 1; i2 >= 0; i2--)
 
278
        {
 
279
          memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
 
280
          ind2 = get_address_index(&a2, sortlist, nsort);
 
281
          if (ind2 <= ind1)
 
282
            break;
 
283
          memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
 
284
        }
 
285
      memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
 
286
    }
 
287
}
 
288
 
 
289
/* Find the first entry in sortlist which matches addr.  Return nsort
 
290
 * if none of them match.
 
291
 */
 
292
static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
 
293
                             int nsort)
 
294
{
 
295
  int i;
 
296
 
 
297
  for (i = 0; i < nsort; i++)
 
298
    {
 
299
      if ((addr->s_addr & sortlist[i].mask.s_addr) == sortlist[i].addr.s_addr)
 
300
        break;
 
301
    }
 
302
  return i;
 
303
}