2 * Copyright (c) 1985, 1989 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted provided
6 * that: (1) source distributions retain this entire copyright notice and
7 * comment, and (2) distributions including binaries display the following
8 * acknowledgement: ``This product includes software developed by the
9 * University of California, Berkeley and its contributors'' in the
10 * documentation or other materials provided with the distribution and in
11 * all advertising materials mentioning features or use of this software.
12 * Neither the name of the University nor the names of its contributors may
13 * be used to endorse or promote products derived from this software without
14 * specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid
[] = "@(#)res_init.c 6.14 (Berkeley) 6/27/90";
22 #endif /* LIBC_SCCS and not lint */
25 #include <sys/types.h>
32 #include <net/gen/in.h>
33 #include <net/gen/inet.h>
34 #include <net/gen/nameser.h>
35 #include <net/gen/netdb.h>
36 #include <net/gen/resolv.h>
37 #include <net/gen/socket.h>
39 #define index(s,c) strchr(s,c)
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include <arpa/nameser.h>
55 * Set up default settings. If the configuration file exist, the values
56 * there will have precedence. Otherwise, the server address is set to
57 * 127.0.0.1 (localhost) and the default domain name comes from gethostname().
59 * The configuration file should only be used if you want to redefine your
60 * domain or run without a server on your machine.
62 * Return 0 if completes successfully, -1 on error
68 register char *cp
, **pp
;
73 struct servent
* servent
;
74 u16_t nameserver_port
;
76 /* Resolver state default settings */
77 _res
.retrans
= RES_TIMEOUT
; /* retransmition time interval */
78 _res
.retry
= 4; /* number of times to retransmit */
79 _res
.options
= RES_DEFAULT
; /* options flags */
80 _res
.nscount
= 0; /* number of name servers */
81 _res
.defdname
[0] = 0; /* domain */
83 servent
= getservbyname("domain", NULL
);
89 nameserver_port
= servent
->s_port
;
91 /* Allow user to override the local domain definition */
92 if ((cp
= getenv("LOCALDOMAIN")) != NULL
) {
93 (void)strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
));
97 if ((fp
= fopen(_PATH_RESCONF
, "r")) != NULL
) {
98 /* read the config file */
99 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
100 /* read default domain name */
101 if (!strncmp(buf
, "domain", sizeof("domain") - 1)) {
102 if (haveenv
) /* skip if have from environ */
104 cp
= buf
+ sizeof("domain") - 1;
105 while (*cp
== ' ' || *cp
== '\t')
107 if ((*cp
== '\0') || (*cp
== '\n'))
109 (void)strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
110 if ((cp
= index(_res
.defdname
, '\n')) != NULL
)
115 /* set search list */
116 if (!strncmp(buf
, "search", sizeof("search") - 1)) {
117 if (haveenv
) /* skip if have from environ */
119 cp
= buf
+ sizeof("search") - 1;
120 while (*cp
== ' ' || *cp
== '\t')
122 if ((*cp
== '\0') || (*cp
== '\n'))
124 (void)strncpy(_res
.defdname
, cp
, sizeof(_res
.defdname
) - 1);
125 if ((cp
= index(_res
.defdname
, '\n')) != NULL
)
128 * Set search list to be blank-separated strings
134 for (n
= 0; *cp
&& pp
< _res
.dnsrch
+ MAXDNSRCH
; cp
++) {
135 if (*cp
== ' ' || *cp
== '\t') {
143 /* null terminate last domain if there are excess */
144 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
151 /* read nameservers to query */
152 if (!strncmp(buf
, "nameserver", sizeof("nameserver") - 1) &&
153 _res
.nscount
< MAXNS
) {
154 cp
= buf
+ sizeof("nameserver") - 1;
155 while (*cp
== ' ' || *cp
== '\t')
157 if ((*cp
== '\0') || (*cp
== '\n'))
159 if (!inet_aton(cp
, &_res
.nsaddr_list
[_res
.nscount
]))
161 _res
.nsport_list
[_res
.nscount
]= nameserver_port
;
168 if (_res
.nscount
== 0) {
169 /* "localhost" is the default nameserver. */
170 _res
.nsaddr_list
[0]= HTONL(0x7F000001);
171 _res
.nsport_list
[0]= nameserver_port
;
174 if (_res
.defdname
[0] == 0) {
175 if (gethostname(buf
, sizeof(_res
.defdname
)) == 0 &&
176 (cp
= index(buf
, '.')))
177 (void)strcpy(_res
.defdname
, cp
+ 1);
180 /* find components of local domain that might be searched */
181 if (havesearch
== 0) {
183 *pp
++ = _res
.defdname
;
184 for (cp
= _res
.defdname
, n
= 0; *cp
; cp
++)
188 for (; n
>= LOCALDOMAINPARTS
&& pp
< _res
.dnsrch
+ MAXDFLSRCH
;
195 _res
.options
|= RES_INIT
;