/[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 1728 - (hide annotations) (download)
Sun Sep 14 01:08:27 2003 UTC (16 years, 2 months ago) by fluffy
Original Path: main/sip/contrib/ares/ares_init.c
File MIME type: text/plain
File size: 15541 byte(s)
fixed to compile on unix again

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

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27