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

Annotation of /main/contrib/ares/ares_init.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1726 - (hide annotations) (download)
Sun Sep 14 00:27:24 2003 UTC (16 years, 3 months ago) by fluffy
Original Path: main/sip/contrib/ares/ares_init.c
File MIME type: text/plain
File size: 15534 byte(s)
first hack at port to windows

1 ryker 1479 /* 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 fluffy 1726 static const char rcsid[] = "$Id: ares_init.c,v 1.2 2003/09/14 00:27:24 fluffy Exp $";
17 ryker 1479
18     #include <sys/types.h>
19 fluffy 1726 //#include <sys/time.h>
20     //#include <sys/param.h>
21     //#include <netinet/in.h>
22     //#include <arpa/inet.h>
23     //#include <arpa/nameser.h>
24 ryker 1479 #include <stdio.h>
25     #include <stdlib.h>
26     #include <string.h>
27     #include <ctype.h>
28     #include <time.h>
29 fluffy 1726 //#include <unistd.h>
30     //#include <errno.h>
31     //#include <netdb.h>
32 ryker 1479 #include "ares.h"
33     #include "ares_private.h"
34    
35     static int init_by_options(ares_channel channel, struct ares_options *options,
36     int optmask);
37     static int init_by_environment(ares_channel channel);
38     static int init_by_resolv_conf(ares_channel channel);
39     static int init_by_defaults(ares_channel channel);
40     static int config_domain(ares_channel channel, char *str);
41     static int config_lookup(ares_channel channel, const char *str);
42     static int config_nameserver(struct server_state **servers, int *nservers,
43     const char *str);
44     static int config_sortlist(struct apattern **sortlist, int *nsort,
45     const char *str);
46     static int set_search(ares_channel channel, const char *str);
47     static int set_options(ares_channel channel, const char *str);
48     static char *try_config(char *s, char *opt);
49     static const char *try_option(const char *p, const char *q, const char *opt);
50     static int ip_addr(const char *s, int len, struct in_addr *addr);
51     static void natural_mask(struct apattern *pat);
52    
53     int ares_init(ares_channel *channelptr)
54     {
55     return ares_init_options(channelptr, NULL, 0);
56     }
57    
58     int ares_init_options(ares_channel *channelptr, struct ares_options *options,
59     int optmask)
60     {
61     ares_channel channel;
62     int i, status;
63     struct server_state *server;
64 fluffy 1726 // struct timeval tv;
65 ryker 1479
66     channel = malloc(sizeof(struct ares_channeldata));
67     if (!channel)
68     return ARES_ENOMEM;
69    
70     /* Set everything to distinguished values so we know they haven't
71     * been set yet.
72     */
73     channel->flags = -1;
74     channel->timeout = -1;
75     channel->tries = -1;
76     channel->ndots = -1;
77     channel->udp_port = -1;
78     channel->tcp_port = -1;
79     channel->nservers = -1;
80     channel->ndomains = -1;
81     channel->nsort = -1;
82     channel->lookups = NULL;
83    
84     /* Initialize configuration by each of the four sources, from highest
85     * precedence to lowest.
86     */
87     status = init_by_options(channel, options, optmask);
88     if (status == ARES_SUCCESS)
89     status = init_by_environment(channel);
90     if (status == ARES_SUCCESS)
91     status = init_by_resolv_conf(channel);
92     if (status == ARES_SUCCESS)
93     status = init_by_defaults(channel);
94     if (status != ARES_SUCCESS)
95     {
96     /* Something failed; clean up memory we may have allocated. */
97     if (channel->nservers != -1)
98     free(channel->servers);
99     if (channel->ndomains != -1)
100     {
101     for (i = 0; i < channel->ndomains; i++)
102     free(channel->domains[i]);
103     free(channel->domains);
104     }
105     if (channel->nsort != -1)
106     free(channel->sortlist);
107     free(channel->lookups);
108     free(channel);
109     return status;
110     }
111    
112     /* Trim to one server if ARES_FLAG_PRIMARY is set. */
113     if ((channel->flags & ARES_FLAG_PRIMARY) && channel->nservers > 1)
114     channel->nservers = 1;
115    
116     /* Initialize server states. */
117     for (i = 0; i < channel->nservers; i++)
118     {
119     server = &channel->servers[i];
120     server->udp_socket = -1;
121     server->tcp_socket = -1;
122     server->tcp_lenbuf_pos = 0;
123     server->tcp_buffer = NULL;
124     server->qhead = NULL;
125     server->qtail = NULL;
126     }
127    
128     /* Choose a somewhat random query ID. The main point is to avoid
129     * collisions with stale queries. An attacker trying to spoof a DNS
130     * answer also has to guess the query ID, but it's only a 16-bit
131     * field, so there's not much to be done about that.
132     */
133 fluffy 1726 // gettimeofday(&tv, NULL);
134     // channel->next_id = (tv.tv_sec ^ tv.tv_usec ^ getpid()) & 0xffff;
135     {
136     int cjNextID=1;
137     channel->next_id = cjNextID++;
138     }
139 ryker 1479
140     channel->queries = NULL;
141    
142     *channelptr = channel;
143     return ARES_SUCCESS;
144     }
145    
146     static int init_by_options(ares_channel channel, struct ares_options *options,
147     int optmask)
148     {
149     int i;
150    
151     /* Easy stuff. */
152     if ((optmask & ARES_OPT_FLAGS) && channel->flags == -1)
153     channel->flags = options->flags;
154     if ((optmask & ARES_OPT_TIMEOUT) && channel->timeout == -1)
155     channel->timeout = options->timeout;
156     if ((optmask & ARES_OPT_TRIES) && channel->tries == -1)
157     channel->tries = options->tries;
158     if ((optmask & ARES_OPT_NDOTS) && channel->ndots == -1)
159     channel->ndots = options->ndots;
160     if ((optmask & ARES_OPT_UDP_PORT) && channel->udp_port == -1)
161     channel->udp_port = options->udp_port;
162     if ((optmask & ARES_OPT_TCP_PORT) && channel->tcp_port == -1)
163     channel->tcp_port = options->tcp_port;
164    
165     /* Copy the servers, if given. */
166     if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
167     {
168     channel->servers =
169     malloc(options->nservers * sizeof(struct server_state));
170     if (!channel->servers && options->nservers != 0)
171     return ARES_ENOMEM;
172     for (i = 0; i < options->nservers; i++)
173     channel->servers[i].addr = options->servers[i];
174     channel->nservers = options->nservers;
175     }
176    
177     /* Copy the domains, if given. Keep channel->ndomains consistent so
178     * we can clean up in case of error.
179     */
180     if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)
181     {
182     channel->domains = malloc(options->ndomains * sizeof(char *));
183     if (!channel->domains && options->ndomains != 0)
184     return ARES_ENOMEM;
185     for (i = 0; i < options->ndomains; i++)
186     {
187     channel->ndomains = i;
188     channel->domains[i] = strdup(options->domains[i]);
189     if (!channel->domains[i])
190     return ARES_ENOMEM;
191     }
192     channel->ndomains = options->ndomains;
193     }
194    
195     /* Set lookups, if given. */
196     if ((optmask & ARES_OPT_LOOKUPS) && !channel->lookups)
197     {
198     channel->lookups = strdup(options->lookups);
199     if (!channel->lookups)
200     return ARES_ENOMEM;
201     }
202    
203     return ARES_SUCCESS;
204     }
205    
206     static int init_by_environment(ares_channel channel)
207     {
208     const char *localdomain, *res_options;
209     int status;
210    
211     localdomain = getenv("LOCALDOMAIN");
212     if (localdomain && channel->ndomains == -1)
213     {
214     status = set_search(channel, localdomain);
215     if (status != ARES_SUCCESS)
216     return status;
217     }
218    
219     res_options = getenv("RES_OPTIONS");
220     if (res_options)
221     {
222     status = set_options(channel, res_options);
223     if (status != ARES_SUCCESS)
224     return status;
225     }
226    
227     return ARES_SUCCESS;
228     }
229    
230     static int init_by_resolv_conf(ares_channel channel)
231     {
232     FILE *fp;
233     char *line = NULL, *p;
234     int linesize, status, nservers = 0, nsort = 0;
235     struct server_state *servers = NULL;
236     struct apattern *sortlist = NULL;
237    
238     fp = fopen(PATH_RESOLV_CONF, "r");
239     if (!fp)
240     return (errno == ENOENT) ? ARES_SUCCESS : ARES_EFILE;
241     while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
242     {
243     if ((p = try_config(line, "domain")) && channel->ndomains == -1)
244     status = config_domain(channel, p);
245     else if ((p = try_config(line, "lookup")) && !channel->lookups)
246     status = config_lookup(channel, p);
247     else if ((p = try_config(line, "search")) && channel->ndomains == -1)
248     status = set_search(channel, p);
249     else if ((p = try_config(line, "nameserver")) && channel->nservers == -1)
250     status = config_nameserver(&servers, &nservers, p);
251     else if ((p = try_config(line, "sortlist")) && channel->nsort == -1)
252     status = config_sortlist(&sortlist, &nsort, p);
253     else if ((p = try_config(line, "options")))
254     status = set_options(channel, p);
255     else
256     status = ARES_SUCCESS;
257     if (status != ARES_SUCCESS)
258     break;
259     }
260     free(line);
261     fclose(fp);
262    
263     /* Handle errors. */
264     if (status != ARES_EOF)
265     {
266     free(servers);
267     free(sortlist);
268     return status;
269     }
270    
271     /* If we got any name server entries, fill them in. */
272     if (servers)
273     {
274     channel->servers = servers;
275     channel->nservers = nservers;
276     }
277    
278     /* If we got any sortlist entries, fill them in. */
279     if (sortlist)
280     {
281     channel->sortlist = sortlist;
282     channel->nsort = nsort;
283     }
284    
285     return ARES_SUCCESS;
286     }
287    
288     static int init_by_defaults(ares_channel channel)
289     {
290     char hostname[MAXHOSTNAMELEN + 1];
291    
292     if (channel->flags == -1)
293     channel->flags = 0;
294     if (channel->timeout == -1)
295     channel->timeout = DEFAULT_TIMEOUT;
296     if (channel->tries == -1)
297     channel->tries = DEFAULT_TRIES;
298     if (channel->ndots == -1)
299     channel->ndots = 1;
300     if (channel->udp_port == -1)
301     channel->udp_port = htons(NAMESERVER_PORT);
302     if (channel->tcp_port == -1)
303     channel->tcp_port = htons(NAMESERVER_PORT);
304    
305     if (channel->nservers == -1)
306     {
307     /* If nobody specified servers, try a local named. */
308     channel->servers = malloc(sizeof(struct server_state));
309     if (!channel->servers)
310     return ARES_ENOMEM;
311     channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);
312     channel->nservers = 1;
313     }
314    
315     if (channel->ndomains == -1)
316     {
317     /* Derive a default domain search list from the kernel hostname,
318     * or set it to empty if the hostname isn't helpful.
319     */
320     if (gethostname(hostname, sizeof(hostname)) == -1
321     || !strchr(hostname, '.'))
322     {
323     channel->domains = malloc(0);
324     channel->ndomains = 0;
325     }
326     else
327     {
328     channel->domains = malloc(sizeof(char *));
329     if (!channel->domains)
330     return ARES_ENOMEM;
331     channel->ndomains = 0;
332     channel->domains[0] = strdup(strchr(hostname, '.') + 1);
333     if (!channel->domains[0])
334     return ARES_ENOMEM;
335     channel->ndomains = 1;
336     }
337     }
338    
339     if (channel->nsort == -1)
340     {
341     channel->sortlist = NULL;
342     channel->nsort = 0;
343     }
344    
345     if (!channel->lookups)
346     {
347     channel->lookups = strdup("bf");
348     if (!channel->lookups)
349     return ARES_ENOMEM;
350     }
351    
352     return ARES_SUCCESS;
353     }
354    
355     static int config_domain(ares_channel channel, char *str)
356     {
357     char *q;
358    
359     /* Set a single search domain. */
360     q = str;
361     while (*q && !isspace((unsigned char)*q))
362     q++;
363     *q = 0;
364     return set_search(channel, str);
365     }
366    
367     static int config_lookup(ares_channel channel, const char *str)
368     {
369     char lookups[3], *l;
370     const char *p;
371    
372     /* Set the lookup order. Only the first letter of each work
373     * is relevant, and it has to be "b" for DNS or "f" for the
374     * host file. Ignore everything else.
375     */
376     l = lookups;
377     p = str;
378     while (*p)
379     {
380     if ((*p == 'b' || *p == 'f') && l < lookups + 2)
381     *l++ = *p;
382     while (*p && !isspace((unsigned char)*p))
383     p++;
384     while (isspace((unsigned char)*p))
385     p++;
386     }
387     *l = 0;
388     channel->lookups = strdup(lookups);
389     return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
390     }
391    
392     static int config_nameserver(struct server_state **servers, int *nservers,
393     const char *str)
394     {
395     struct in_addr addr;
396     struct server_state *newserv;
397    
398     /* Add a nameserver entry, if this is a valid address. */
399     addr.s_addr = inet_addr(str);
400     if (addr.s_addr == INADDR_NONE)
401     return ARES_SUCCESS;
402     newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
403     if (!newserv)
404     return ARES_ENOMEM;
405     newserv[*nservers].addr = addr;
406     *servers = newserv;
407     (*nservers)++;
408     return ARES_SUCCESS;
409     }
410    
411     static int config_sortlist(struct apattern **sortlist, int *nsort,
412     const char *str)
413     {
414     struct apattern pat, *newsort;
415     const char *q;
416    
417     /* Add sortlist entries. */
418     while (*str && *str != ';')
419     {
420     q = str;
421     while (*q && *q != '/' && *q != ';' && !isspace((unsigned char)*q))
422     q++;
423     if (ip_addr(str, q - str, &pat.addr) == 0)
424     {
425     /* We have a pattern address; now determine the mask. */
426     if (*q == '/')
427     {
428     str = q + 1;
429     while (*q && *q != ';' && !isspace((unsigned char)*q))
430     q++;
431     if (ip_addr(str, q - str, &pat.mask) != 0)
432     natural_mask(&pat);
433     }
434     else
435     natural_mask(&pat);
436    
437     /* Add this pattern to our list. */
438     newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
439     if (!newsort)
440     return ARES_ENOMEM;
441     newsort[*nsort] = pat;
442     *sortlist = newsort;
443     (*nsort)++;
444     }
445     else
446     {
447     while (*q && *q != ';' && !isspace((unsigned char)*q))
448     q++;
449     }
450     str = q;
451     while (isspace((unsigned char)*str))
452     str++;
453     }
454    
455     return ARES_SUCCESS;
456     }
457    
458     static int set_search(ares_channel channel, const char *str)
459     {
460     int n;
461     const char *p, *q;
462    
463     /* Count the domains given. */
464     n = 0;
465     p = str;
466     while (*p)
467     {
468     while (*p && !isspace((unsigned char)*p))
469     p++;
470     while (isspace((unsigned char)*p))
471     p++;
472     n++;
473     }
474    
475     channel->domains = malloc(n * sizeof(char *));
476     if (!channel->domains && n)
477     return ARES_ENOMEM;
478    
479     /* Now copy the domains. */
480     n = 0;
481     p = str;
482     while (*p)
483     {
484     channel->ndomains = n;
485     q = p;
486     while (*q && !isspace((unsigned char)*q))
487     q++;
488     channel->domains[n] = malloc(q - p + 1);
489     if (!channel->domains[n])
490     return ARES_ENOMEM;
491     memcpy(channel->domains[n], p, q - p);
492     channel->domains[n][q - p] = 0;
493     p = q;
494     while (isspace((unsigned char)*p))
495     p++;
496     n++;
497     }
498     channel->ndomains = n;
499    
500     return ARES_SUCCESS;
501     }
502    
503     static int set_options(ares_channel channel, const char *str)
504     {
505     const char *p, *q, *val;
506    
507     p = str;
508     while (*p)
509     {
510     q = p;
511     while (*q && !isspace((unsigned char)*q))
512     q++;
513     val = try_option(p, q, "ndots:");
514     if (val && channel->ndots == -1)
515     channel->ndots = atoi(val);
516     val = try_option(p, q, "retrans:");
517     if (val && channel->timeout == -1)
518     channel->timeout = atoi(val);
519     val = try_option(p, q, "retry:");
520     if (val && channel->tries == -1)
521     channel->tries = atoi(val);
522     p = q;
523     while (isspace((unsigned char)*p))
524     p++;
525     }
526    
527     return ARES_SUCCESS;
528     }
529    
530     static char *try_config(char *s, char *opt)
531     {
532     int len;
533    
534     len = strlen(opt);
535     if (strncmp(s, opt, len) != 0 || !isspace((unsigned char)s[len]))
536     return NULL;
537     s += len;
538     while (isspace((unsigned char)*s))
539     s++;
540     return s;
541     }
542    
543     static const char *try_option(const char *p, const char *q, const char *opt)
544     {
545     int len;
546    
547     len = strlen(opt);
548     return (q - p > len && strncmp(p, opt, len) == 0) ? p + len : NULL;
549     }
550    
551     static int ip_addr(const char *s, int len, struct in_addr *addr)
552     {
553     char ipbuf[16];
554    
555     /* Four octets and three periods yields at most 15 characters. */
556     if (len > 15)
557     return -1;
558     memcpy(ipbuf, s, len);
559     ipbuf[len] = 0;
560    
561     addr->s_addr = inet_addr(ipbuf);
562     if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
563     return -1;
564     return 0;
565     }
566    
567     static void natural_mask(struct apattern *pat)
568     {
569     struct in_addr addr;
570    
571     /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
572     * but portable.
573     */
574     addr.s_addr = ntohl(pat->addr.s_addr);
575    
576     /* This is out of date in the CIDR world, but some people might
577     * still rely on it.
578     */
579     if (IN_CLASSA(addr.s_addr))
580     pat->mask.s_addr = htonl(IN_CLASSA_NET);
581     else if (IN_CLASSB(addr.s_addr))
582     pat->mask.s_addr = htonl(IN_CLASSB_NET);
583     else
584     pat->mask.s_addr = htonl(IN_CLASSC_NET);
585     }

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27