]>
git.ipfire.org Git - thirdparty/dhcp.git/blob - common/resolv.c
3 Parser for /etc/resolv.conf file. */
6 * Copyright (c) 1995, 1996, 1997 The 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 <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
44 static char copyright
[] =
45 "$Id: resolv.c,v 1.7 1999/02/24 17:56:48 mellon Exp $ Copyright (c) 1995, 1996 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
)
62 struct name_server
*sp
, *sl
, *ns
;
63 struct domain_search_list
*dp
, *dl
, *nd
;
66 new_parse (path_resolv_conf
);
69 if ((cfile
= fopen (path_resolv_conf
, "r")) == NULL
) {
70 log_error ("Can't open %s: %m", path_resolv_conf
);
75 token
= next_token (&val
, cfile
);
78 else if (token
== EOL
)
80 else if (token
== DOMAIN
|| token
== SEARCH
) {
82 struct domain_search_list
*nd
, **dp
;
85 dn
= parse_host_name (cfile
);
90 for (nd
= domains
; nd
; nd
= nd
-> next
) {
92 if (!strcmp (nd
-> domain
, dn
))
96 nd
= new_domain_search_list
99 log_fatal ("No memory for %s", dn
);
101 (struct domain_search_list
*)0;
106 nd
-> rcdate
= parse_time
;
107 token
= peek_token (&val
, cfile
);
108 } while (token
!= EOL
);
110 parse_warn ("junk after domain declaration");
111 skip_to_semi (cfile
);
113 token
= next_token (&val
, cfile
);
114 } else if (token
== NAMESERVER
) {
115 struct name_server
*ns
, **sp
;
118 parse_ip_addr (cfile
, &iaddr
);
121 for (ns
= name_servers
; ns
; ns
= ns
-> next
) {
123 if (!memcmp (&ns
-> addr
.sin_addr
,
124 iaddr
.iabuf
, iaddr
.len
))
128 ns
= new_name_server ("read_resolv_conf");
130 log_fatal ("No memory for nameserver %s",
132 ns
-> next
= (struct name_server
*)0;
134 memcpy (&ns
-> addr
.sin_addr
,
135 iaddr
.iabuf
, iaddr
.len
);
137 ns
-> addr
.sin_len
= sizeof ns
-> addr
;
139 ns
-> addr
.sin_family
= AF_INET
;
140 ns
-> addr
.sin_port
= htons (53);
141 memset (ns
-> addr
.sin_zero
, 0,
142 sizeof ns
-> addr
.sin_zero
);
144 ns
-> rcdate
= parse_time
;
145 skip_to_semi (cfile
);
147 skip_to_semi (cfile
); /* Ignore what we don't grok. */
149 token
= next_token (&val
, cfile
); /* Clear the peek buffer */
151 /* Lose servers that are no longer in /etc/resolv.conf. */
152 sl
= (struct name_server
*)0;
153 for (sp
= name_servers
; sp
; sp
= ns
) {
155 if (sp
-> rcdate
!= parse_time
) {
157 sl
-> next
= sp
-> next
;
159 name_servers
= sp
-> next
;
160 /* We can't actually free the name server structure,
161 because somebody might be hanging on to it. If
162 your /etc/resolv.conf file changes a lot, this
163 could be a noticable memory leak. */
168 /* Lose domains that are no longer in /etc/resolv.conf. */
169 dl
= (struct domain_search_list
*)0;
170 for (dp
= domains
; dp
; dp
= nd
) {
172 if (dp
-> rcdate
!= parse_time
) {
174 dl
-> next
= dp
-> next
;
176 domains
= dp
-> next
;
177 free_domain_search_list (dp
, "pick_name_server");
184 /* Pick a name server from the /etc/resolv.conf file. */
186 struct name_server
*first_name_server ()
192 /* Check /etc/resolv.conf and reload it if it's changed. */
193 if (cur_time
> rcdate
) {
194 if (stat (path_resolv_conf
, &st
) < 0) {
195 log_error ("Can't stat %s", path_resolv_conf
);
196 return (struct name_server
*)0;
198 if (st
.st_mtime
> rcdate
) {
201 rcdate
= cur_time
+ 1;
203 read_resolv_conf (rcdate
);