/[resiprocate]/main/contrib/ares/ares_gethostbyname.c
ViewVC logotype

Contents of /main/contrib/ares/ares_gethostbyname.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5295 - (show annotations) (download)
Mon Aug 22 00:30:05 2005 UTC (14 years, 3 months ago) by jason
File MIME type: text/plain
File size: 7679 byte(s)
merged 5270:HEAD from b-directory-reorg
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 static int file_lookup(const char *name, struct hostent **host)
223 {
224 FILE *fp;
225 char **alias;
226 int status;
227
228 #ifdef WIN32
229 fp = fopen(w32hostspath, "r");
230 #else
231 fp = fopen(PATH_HOSTS, "r");
232 #endif
233 if (!fp)
234 return ARES_ENOTFOUND;
235
236 while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
237 {
238 if (strcasecmp((*host)->h_name, name) == 0)
239 break;
240 for (alias = (*host)->h_aliases; *alias; alias++)
241 {
242 if (strcasecmp(*alias, name) == 0)
243 break;
244 }
245 if (*alias)
246 break;
247 ares_free_hostent(*host);
248 }
249 fclose(fp);
250 if (status == ARES_EOF)
251 status = ARES_ENOTFOUND;
252 if (status != ARES_SUCCESS)
253 *host = NULL;
254 return status;
255 }
256
257 static void sort_addresses(struct hostent *host, struct apattern *sortlist,
258 int nsort)
259 {
260 struct in_addr a1, a2;
261 int i1, i2, ind1, ind2;
262
263 /* This is a simple insertion sort, not optimized at all. i1 walks
264 * through the address list, with the loop invariant that everything
265 * to the left of i1 is sorted. In the loop body, the value at i1 is moved
266 * back through the list (via i2) until it is in sorted order.
267 */
268 for (i1 = 0; host->h_addr_list[i1]; i1++)
269 {
270 memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
271 ind1 = get_address_index(&a1, sortlist, nsort);
272 for (i2 = i1 - 1; i2 >= 0; i2--)
273 {
274 memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
275 ind2 = get_address_index(&a2, sortlist, nsort);
276 if (ind2 <= ind1)
277 break;
278 memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
279 }
280 memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
281 }
282 }
283
284 /* Find the first entry in sortlist which matches addr. Return nsort
285 * if none of them match.
286 */
287 static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
288 int nsort)
289 {
290 int i;
291
292 for (i = 0; i < nsort; i++)
293 {
294 if ((addr->s_addr & sortlist[i].mask.s_addr) == sortlist[i].addr.s_addr)
295 break;
296 }
297 return i;
298 }

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27