3 Parser for /etc/resolv.conf file. */
6 * Copyright (c) 1996-2002 Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38 * To learn more about the Internet Software Consortium, see
39 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
40 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
41 * ``http://www.nominum.com''.
45 static char copyright[] =
46 "$Id: resolv.c,v 1.16.2.1 2002/11/17 02:26:59 dhankins Exp $ Copyright (c) 1996-2002 The Internet Software Consortium. All rights reserved.\n";
51 struct name_server *name_servers;
52 struct domain_search_list *domains;
53 char path_resolv_conf [] = _PATH_RESOLV_CONF;
55 void read_resolv_conf (parse_time)
63 struct name_server *sp, *sl, *ns;
64 struct domain_search_list *dp, *dl, *nd;
67 if ((file = open (path_resolv_conf, O_RDONLY)) < 0) {
68 log_error ("Can't open %s: %m", path_resolv_conf);
72 cfile = (struct parse *)0;
73 new_parse (&cfile, file, (char *)0, 0, path_resolv_conf, 1);
76 token = next_token (&val, (unsigned *)0, cfile);
77 if (token == END_OF_FILE)
79 else if (token == EOL)
81 else if (token == DOMAIN || token == SEARCH) {
83 struct domain_search_list *nd, **dp;
86 dn = parse_host_name (cfile);
91 for (nd = domains; nd; nd = nd -> next) {
93 if (!strcmp (nd -> domain, dn))
97 nd = new_domain_search_list (MDL);
99 log_fatal ("No memory for %s",
102 (struct domain_search_list *)0;
107 nd -> rcdate = parse_time;
108 token = peek_token (&val,
109 (unsigned *)0, cfile);
110 } while (token != EOL);
113 "junk after domain declaration");
114 skip_to_semi (cfile);
116 token = next_token (&val, (unsigned *)0, cfile);
117 } else if (token == NAMESERVER) {
118 struct name_server *ns, **sp;
121 parse_ip_addr (cfile, &iaddr);
124 for (ns = name_servers; ns; ns = ns -> next) {
126 if (!memcmp (&ns -> addr.sin_addr,
127 iaddr.iabuf, iaddr.len))
131 ns = new_name_server (MDL);
133 log_fatal ("No memory for nameserver %s",
135 ns -> next = (struct name_server *)0;
137 memcpy (&ns -> addr.sin_addr,
138 iaddr.iabuf, iaddr.len);
140 ns -> addr.sin_len = sizeof ns -> addr;
142 ns -> addr.sin_family = AF_INET;
143 ns -> addr.sin_port = htons (53);
144 memset (ns -> addr.sin_zero, 0,
145 sizeof ns -> addr.sin_zero);
147 ns -> rcdate = parse_time;
148 skip_to_semi (cfile);
150 skip_to_semi (cfile); /* Ignore what we don't grok. */
152 token = next_token (&val, (unsigned *)0, cfile);
154 /* Lose servers that are no longer in /etc/resolv.conf. */
155 sl = (struct name_server *)0;
156 for (sp = name_servers; sp; sp = ns) {
158 if (sp -> rcdate != parse_time) {
160 sl -> next = sp -> next;
162 name_servers = sp -> next;
163 /* We can't actually free the name server structure,
164 because somebody might be hanging on to it. If
165 your /etc/resolv.conf file changes a lot, this
166 could be a noticable memory leak. */
171 /* Lose domains that are no longer in /etc/resolv.conf. */
172 dl = (struct domain_search_list *)0;
173 for (dp = domains; dp; dp = nd) {
175 if (dp -> rcdate != parse_time) {
177 dl -> next = dp -> next;
179 domains = dp -> next;
180 free_domain_search_list (dp, MDL);
188 /* Pick a name server from the /etc/resolv.conf file. */
190 struct name_server *first_name_server ()
196 /* Check /etc/resolv.conf and reload it if it's changed. */
197 if (cur_time > rcdate) {
198 if (stat (path_resolv_conf, &st) < 0) {
199 log_error ("Can't stat %s", path_resolv_conf);
200 return (struct name_server *)0;
202 if (st.st_mtime > rcdate) {
205 rcdate = cur_time + 1;
207 read_resolv_conf (rcdate);