]>
git.ipfire.org Git - thirdparty/dhcp.git/blob - common/resolv.c
f59cae88f770f62e4c0f6fa9096dcbf6b165dc69
3 Parser for /etc/resolv.conf file. */
6 * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996-2003 by Internet Software Consortium
9 * This Source Code Form is subject to the terms of the Mozilla Public
10 * License, v. 2.0. If a copy of the MPL was not distributed with this
11 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Newmarket, NH 03857 USA
25 * https://www.isc.org/
31 struct name_server
*name_servers
;
32 struct domain_search_list
*domains
;
33 char path_resolv_conf
[] = _PATH_RESOLV_CONF
;
35 void read_resolv_conf (parse_time
)
42 struct name_server
*sp
, *sl
, *ns
;
43 struct domain_search_list
*dp
, *dl
, *nd
;
46 if ((file
= open (path_resolv_conf
, O_RDONLY
)) < 0) {
47 log_error ("Can't open %s: %m", path_resolv_conf
);
52 status
= new_parse(&cfile
, file
, NULL
, 0, path_resolv_conf
, 1);
53 if (status
!= ISC_R_SUCCESS
|| cfile
== NULL
)
57 token
= next_token (&val
, (unsigned *)0, cfile
);
58 if (token
== END_OF_FILE
)
60 else if (token
== EOL
)
62 else if (token
== DOMAIN
|| token
== SEARCH
) {
64 struct domain_search_list
*nd
, **dp
;
67 dn
= parse_host_name (cfile
);
72 for (nd
= domains
; nd
; nd
= nd
-> next
) {
74 if (!strcmp (nd
-> domain
, dn
))
78 nd
= new_domain_search_list (MDL
);
80 log_fatal ("No memory for %s",
83 (struct domain_search_list
*)0;
87 nd
-> rcdate
= parse_time
;
88 token
= peek_token (&val
,
89 (unsigned *)0, cfile
);
90 } while (token
!= EOL
);
93 "junk after domain declaration");
96 skip_token(&val
, (unsigned *)0, cfile
);
97 } else if (token
== NAMESERVER
) {
98 struct name_server
*ns
, **sp
;
101 parse_ip_addr (cfile
, &iaddr
);
104 for (ns
= name_servers
; ns
; ns
= ns
-> next
) {
106 if (!memcmp (&ns
-> addr
.sin_addr
,
107 iaddr
.iabuf
, iaddr
.len
))
111 ns
= new_name_server (MDL
);
113 log_fatal ("No memory for nameserver %s",
115 ns
-> next
= (struct name_server
*)0;
117 memcpy (&ns
-> addr
.sin_addr
,
118 iaddr
.iabuf
, iaddr
.len
);
120 ns
-> addr
.sin_len
= sizeof ns
-> addr
;
122 ns
-> addr
.sin_family
= AF_INET
;
123 ns
-> addr
.sin_port
= htons (53);
124 memset (ns
-> addr
.sin_zero
, 0,
125 sizeof ns
-> addr
.sin_zero
);
127 ns
-> rcdate
= parse_time
;
128 skip_to_semi (cfile
);
130 skip_to_semi (cfile
); /* Ignore what we don't grok. */
132 skip_token(&val
, (unsigned *)0, cfile
);
134 /* Lose servers that are no longer in /etc/resolv.conf. */
135 sl
= (struct name_server
*)0;
136 for (sp
= name_servers
; sp
; sp
= ns
) {
138 if (sp
-> rcdate
!= parse_time
) {
140 sl
-> next
= sp
-> next
;
142 name_servers
= sp
-> next
;
143 /* We can't actually free the name server structure,
144 because somebody might be hanging on to it. If
145 your /etc/resolv.conf file changes a lot, this
146 could be a noticeable memory leak. */
151 /* Lose domains that are no longer in /etc/resolv.conf. */
152 dl
= (struct domain_search_list
*)0;
153 for (dp
= domains
; dp
; dp
= nd
) {
155 if (dp
-> rcdate
!= parse_time
) {
157 dl
-> next
= dp
-> next
;
159 domains
= dp
-> next
;
160 free_domain_search_list (dp
, MDL
);
167 /* Pick a name server from the /etc/resolv.conf file. */
169 struct name_server
*first_name_server ()
174 /* Check /etc/resolv.conf and reload it if it's changed. */
175 if (cur_time
> rcdate
) {
176 if (stat (path_resolv_conf
, &st
) < 0) {
177 log_error ("Can't stat %s", path_resolv_conf
);
178 return (struct name_server
*)0;
180 if (st
.st_mtime
> rcdate
) {
181 rcdate
= cur_time
+ 1;
183 read_resolv_conf (rcdate
);