/[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 2394 - (hide annotations) (download)
Mon Feb 23 23:29:42 2004 UTC (15 years, 9 months ago) by dean
Original Path: main/sip/contrib/ares/ares_init.c
File MIME type: text/plain
File size: 16540 byte(s)
fixed bug in handling of DnsConfigDnsServerList (DNS queries to 2.0.0.0)

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

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27