1 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
16 static const char rcsid[] = "$Id: ares_init.c,v 1.7 1999/10/23 19:28:13 danw Exp $";
18 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <arpa/nameser.h>
33 #include "ares_private.h"
35 static int init_by_options(ares_channel channel, struct ares_options *options,
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,
44 static int config_sortlist(struct apattern **sortlist, int *nsort,
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);
53 int ares_init(ares_channel *channelptr)
55 return ares_init_options(channelptr, NULL, 0);
58 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
63 struct server_state *server;
66 channel = malloc(sizeof(struct ares_channeldata));
70 /* Set everything to distinguished values so we know they haven't
74 channel->timeout = -1;
77 channel->udp_port = -1;
78 channel->tcp_port = -1;
79 channel->nservers = -1;
80 channel->ndomains = -1;
82 channel->lookups = NULL;
84 /* Initialize configuration by each of the four sources, from highest
85 * precedence to lowest.
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)
96 /* Something failed; clean up memory we may have allocated. */
97 if (channel->nservers != -1)
98 free(channel->servers);
99 if (channel->ndomains != -1)
101 for (i = 0; i < channel->ndomains; i++)
102 free(channel->domains[i]);
103 free(channel->domains);
105 if (channel->nsort != -1)
106 free(channel->sortlist);
107 free(channel->lookups);
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;
116 /* Initialize server states. */
117 for (i = 0; i < channel->nservers; i++)
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;
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.
133 gettimeofday(&tv, NULL);
134 channel->next_id = (tv.tv_sec ^ tv.tv_usec ^ getpid()) & 0xffff;
136 channel->queries = NULL;
138 *channelptr = channel;
142 static int init_by_options(ares_channel channel, struct ares_options *options,
148 if ((optmask & ARES_OPT_FLAGS) && channel->flags == -1)
149 channel->flags = options->flags;
150 if ((optmask & ARES_OPT_TIMEOUT) && channel->timeout == -1)
151 channel->timeout = options->timeout;
152 if ((optmask & ARES_OPT_TRIES) && channel->tries == -1)
153 channel->tries = options->tries;
154 if ((optmask & ARES_OPT_NDOTS) && channel->ndots == -1)
155 channel->ndots = options->ndots;
156 if ((optmask & ARES_OPT_UDP_PORT) && channel->udp_port == -1)
157 channel->udp_port = options->udp_port;
158 if ((optmask & ARES_OPT_TCP_PORT) && channel->tcp_port == -1)
159 channel->tcp_port = options->tcp_port;
161 /* Copy the servers, if given. */
162 if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
165 malloc(options->nservers * sizeof(struct server_state));
166 if (!channel->servers && options->nservers != 0)
168 for (i = 0; i < options->nservers; i++)
169 channel->servers[i].addr = options->servers[i];
170 channel->nservers = options->nservers;
173 /* Copy the domains, if given. Keep channel->ndomains consistent so
174 * we can clean up in case of error.
176 if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)
178 channel->domains = malloc(options->ndomains * sizeof(char *));
179 if (!channel->domains && options->ndomains != 0)
181 for (i = 0; i < options->ndomains; i++)
183 channel->ndomains = i;
184 channel->domains[i] = strdup(options->domains[i]);
185 if (!channel->domains[i])
188 channel->ndomains = options->ndomains;
191 /* Set lookups, if given. */
192 if ((optmask & ARES_OPT_LOOKUPS) && !channel->lookups)
194 channel->lookups = strdup(options->lookups);
195 if (!channel->lookups)
202 static int init_by_environment(ares_channel channel)
204 const char *localdomain, *res_options;
207 localdomain = getenv("LOCALDOMAIN");
208 if (localdomain && channel->ndomains == -1)
210 status = set_search(channel, localdomain);
211 if (status != ARES_SUCCESS)
215 res_options = getenv("RES_OPTIONS");
218 status = set_options(channel, res_options);
219 if (status != ARES_SUCCESS)
226 static int init_by_resolv_conf(ares_channel channel)
229 char *line = NULL, *p;
230 int linesize, status, nservers = 0, nsort = 0;
231 struct server_state *servers = NULL;
232 struct apattern *sortlist = NULL;
234 fp = fopen(PATH_RESOLV_CONF, "r");
236 return (errno == ENOENT) ? ARES_SUCCESS : ARES_EFILE;
237 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
239 if ((p = try_config(line, "domain")) && channel->ndomains == -1)
240 status = config_domain(channel, p);
241 else if ((p = try_config(line, "lookup")) && !channel->lookups)
242 status = config_lookup(channel, p);
243 else if ((p = try_config(line, "search")) && channel->ndomains == -1)
244 status = set_search(channel, p);
245 else if ((p = try_config(line, "nameserver")) && channel->nservers == -1)
246 status = config_nameserver(&servers, &nservers, p);
247 else if ((p = try_config(line, "sortlist")) && channel->nsort == -1)
248 status = config_sortlist(&sortlist, &nsort, p);
249 else if ((p = try_config(line, "options")))
250 status = set_options(channel, p);
252 status = ARES_SUCCESS;
253 if (status != ARES_SUCCESS)
260 if (status != ARES_EOF)
267 /* If we got any name server entries, fill them in. */
270 channel->servers = servers;
271 channel->nservers = nservers;
274 /* If we got any sortlist entries, fill them in. */
277 channel->sortlist = sortlist;
278 channel->nsort = nsort;
284 static int init_by_defaults(ares_channel channel)
288 if (channel->flags == -1)
290 if (channel->timeout == -1)
291 channel->timeout = DEFAULT_TIMEOUT;
292 if (channel->tries == -1)
293 channel->tries = DEFAULT_TRIES;
294 if (channel->ndots == -1)
296 if (channel->udp_port == -1)
297 channel->udp_port = htons(NAMESERVER_PORT);
298 if (channel->tcp_port == -1)
299 channel->tcp_port = htons(NAMESERVER_PORT);
301 if (channel->nservers == -1)
303 /* If nobody specified servers, try a local named. */
304 channel->servers = malloc(sizeof(struct server_state));
305 if (!channel->servers)
307 channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);
308 channel->nservers = 1;
311 if (channel->ndomains == -1)
313 /* Derive a default domain search list from the kernel hostname,
314 * or set it to empty if the hostname isn't helpful.
316 if (gethostname(hostname, sizeof(hostname)) == -1
317 || !strchr(hostname, '.'))
319 channel->domains = malloc(0);
320 channel->ndomains = 0;
324 channel->domains = malloc(sizeof(char *));
325 if (!channel->domains)
327 channel->ndomains = 0;
328 channel->domains[0] = strdup(strchr(hostname, '.') + 1);
329 if (!channel->domains[0])
331 channel->ndomains = 1;
335 if (channel->nsort == -1)
337 channel->sortlist = NULL;
341 if (!channel->lookups)
343 channel->lookups = strdup("bf");
344 if (!channel->lookups)
351 static int config_domain(ares_channel channel, char *str)
355 /* Set a single search domain. */
357 while (*q && !isspace((unsigned char)*q))
360 return set_search(channel, str);
363 static int config_lookup(ares_channel channel, const char *str)
368 /* Set the lookup order. Only the first letter of each work
369 * is relevant, and it has to be "b" for DNS or "f" for the
370 * host file. Ignore everything else.
376 if ((*p == 'b' || *p == 'f') && l < lookups + 2)
378 while (*p && !isspace((unsigned char)*p))
380 while (isspace((unsigned char)*p))
384 channel->lookups = strdup(lookups);
385 return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
388 static int config_nameserver(struct server_state **servers, int *nservers,
392 struct server_state *newserv;
394 /* Add a nameserver entry, if this is a valid address. */
395 addr.s_addr = inet_addr(str);
396 if (addr.s_addr == INADDR_NONE)
398 newserv = realloc(*servers, (*nservers + 1) * sizeof(struct server_state));
401 newserv[*nservers].addr = addr;
407 static int config_sortlist(struct apattern **sortlist, int *nsort,
410 struct apattern pat, *newsort;
413 /* Add sortlist entries. */
414 while (*str && *str != ';')
417 while (*q && *q != '/' && *q != ';' && !isspace((unsigned char)*q))
419 if (ip_addr(str, q - str, &pat.addr) == 0)
421 /* We have a pattern address; now determine the mask. */
425 while (*q && *q != ';' && !isspace((unsigned char)*q))
427 if (ip_addr(str, q - str, &pat.mask) != 0)
433 /* Add this pattern to our list. */
434 newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
437 newsort[*nsort] = pat;
443 while (*q && *q != ';' && !isspace((unsigned char)*q))
447 while (isspace((unsigned char)*str))
454 static int set_search(ares_channel channel, const char *str)
459 /* Count the domains given. */
464 while (*p && !isspace((unsigned char)*p))
466 while (isspace((unsigned char)*p))
471 channel->domains = malloc(n * sizeof(char *));
472 if (!channel->domains && n)
475 /* Now copy the domains. */
480 channel->ndomains = n;
482 while (*q && !isspace((unsigned char)*q))
484 channel->domains[n] = malloc(q - p + 1);
485 if (!channel->domains[n])
487 memcpy(channel->domains[n], p, q - p);
488 channel->domains[n][q - p] = 0;
490 while (isspace((unsigned char)*p))
494 channel->ndomains = n;
499 static int set_options(ares_channel channel, const char *str)
501 const char *p, *q, *val;
507 while (*q && !isspace((unsigned char)*q))
509 val = try_option(p, q, "ndots:");
510 if (val && channel->ndots == -1)
511 channel->ndots = atoi(val);
512 val = try_option(p, q, "retrans:");
513 if (val && channel->timeout == -1)
514 channel->timeout = atoi(val);
515 val = try_option(p, q, "retry:");
516 if (val && channel->tries == -1)
517 channel->tries = atoi(val);
519 while (isspace((unsigned char)*p))
526 static char *try_config(char *s, char *opt)
531 if (strncmp(s, opt, len) != 0 || !isspace((unsigned char)s[len]))
534 while (isspace((unsigned char)*s))
539 static const char *try_option(const char *p, const char *q, const char *opt)
544 return (q - p > len && strncmp(p, opt, len) == 0) ? p + len : NULL;
547 static int ip_addr(const char *s, int len, struct in_addr *addr)
551 /* Four octets and three periods yields at most 15 characters. */
554 memcpy(ipbuf, s, len);
557 addr->s_addr = inet_addr(ipbuf);
558 if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
563 static void natural_mask(struct apattern *pat)
567 /* Store a host-byte-order copy of pat in a struct in_addr. Icky,
570 addr.s_addr = ntohl(pat->addr.s_addr);
572 /* This is out of date in the CIDR world, but some people might
575 if (IN_CLASSA(addr.s_addr))
576 pat->mask.s_addr = htonl(IN_CLASSA_NET);
577 else if (IN_CLASSB(addr.s_addr))
578 pat->mask.s_addr = htonl(IN_CLASSB_NET);
580 pat->mask.s_addr = htonl(IN_CLASSC_NET);