/[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 1821 - (hide annotations) (download)
Wed Sep 24 18:46:02 2003 UTC (16 years, 2 months ago) by jason
Original Path: main/sip/contrib/ares/ares_init.c
File MIME type: text/plain
File size: 16290 byte(s)
fix compile errors on linux - windows related changes

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

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27