4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Gary Mills
24 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
41 #include <sys/types.h>
42 #include <sys/sockio.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
50 #include <arpa/nameser.h>
53 #include <netinet/in.h>
55 #include <netinet/if_ether.h>
56 #include <arpa/inet.h>
59 * Undocumented external function in libnsl
62 getdomainname(char *, int);
67 * Resolver state default settings
71 RES_TIMEOUT
, /* retransmition time interval */
72 4, /* number of times to retransmit */
73 RES_DEFAULT
, /* options flags */
74 1, /* number of name servers */
78 * Set up default settings. If the configuration file exist, the values
79 * there will have precedence. Otherwise, the server address is set to
80 * INADDR_LOOPBACK and the default domain name comes from the gethostname().
81 * BUT if the NIS/RPC domain name is set, that is used if all else fails.
83 * The configuration file should only be used if you want to redefine your
84 * domain or run without a server on your machine.
86 * Note the user can always override then domain name with the environment
87 * variable LOCALDOMAIN which has absolute priority.
90 * Return 0 if completes successfully, -1 on error
96 register char *cp
, **pp
;
99 int nserv
= 0; /* number of nameserver records read from file */
103 _res
.nsaddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
); /* INADDR_ANY */
104 _res
.nsaddr
.sin_family
= AF_INET
;
105 _res
.nsaddr
.sin_port
= htons(NAMESERVER_PORT
);
109 { int numifs
, s
, n
, int_up
;
111 register struct ifreq
*ifrp
;
117 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
121 if (ioctl(s
, SIOCGIFNUM
, (char *)&numifs
) < 0) {
124 bufsize
= numifs
* sizeof (struct ifreq
);
125 buf
= (char *)malloc(bufsize
);
127 perror("out of memory");
131 ifc
.ifc_len
= bufsize
;
133 if (ioctl(s
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
134 perror("ifconfig: SIOCGIFCONF");
142 for (n
= ifc
.ifc_len
/ sizeof (struct ifreq
); n
> 0;
144 (void) memset((void *) &ifr
, 0, sizeof (ifr
));
145 strncpy(ifr
.ifr_name
, ifrp
->ifr_name
,
146 sizeof (ifr
.ifr_name
));
147 if (ioctl(s
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
148 perror("SIOCGIFFLAGS");
153 flags
= ifr
.ifr_flags
;
154 /* we are looking for a non-loopback interface */
155 if ((flags
& IFF_UP
) && ((flags
& IFF_LOOPBACK
) == 0))
160 if (int_up
== 0) /* all the non-LOOPBACK interfaces are DOWN */
163 #endif /* SIOCGIFNUM */
167 * for the benefit of hidden NIS domains, we use the same procedure
168 * as sendmail: convert leading + to dot, then drop to first dot
170 (void) getdomainname(buf
, BUFSIZ
);
174 cp
= strchr(buf
, (int)'.');
176 cp
= index(buf
, '.');
179 strcpy(_res
.defdname
, buf
);
181 strcpy(_res
.defdname
, cp
+1);
183 /* Allow user to override the local domain definition */
184 if ((cp
= getenv("LOCALDOMAIN")) != NULL
) {
185 (void) strncpy(_res
.defdname
, cp
, sizeof (_res
.defdname
));
189 if ((fp
= fopen(_PATH_RESCONF
, "r")) != NULL
) {
190 /* read the config file */
191 while (fgets(buf
, sizeof (buf
), fp
) != NULL
) {
192 /* read default domain name */
193 if (!strncmp(buf
, "domain", sizeof ("domain") - 1)) {
194 if (haveenv
) /* skip if have from environ */
196 cp
= buf
+ sizeof ("domain") - 1;
197 while (*cp
== ' ' || *cp
== '\t')
199 if ((*cp
== '\0') || (*cp
== '\n'))
201 (void) strncpy(_res
.defdname
, cp
, sizeof (_res
.defdname
) - 1);
203 if ((cp
= strchr(_res
.defdname
, (int)'\n')) != NULL
)
205 if ((cp
= index(_res
.defdname
, '\n')) != NULL
)
211 /* set search list */
212 if (!strncmp(buf
, "search", sizeof ("search") - 1)) {
213 if (haveenv
) /* skip if have from environ */
215 cp
= buf
+ sizeof ("search") - 1;
216 while (*cp
== ' ' || *cp
== '\t')
218 if ((*cp
== '\0') || (*cp
== '\n'))
220 (void) strncpy(_res
.defdname
, cp
, sizeof (_res
.defdname
) - 1);
222 if ((cp
= strchr(_res
.defdname
, (int)'\n')) != NULL
)
224 if ((cp
= index(_res
.defdname
, '\n')) != NULL
)
228 * Set search list to be blank-separated strings
234 for (n
= 0; *cp
&& pp
< _res
.dnsrch
+ MAXDNSRCH
; cp
++) {
235 if (*cp
== ' ' || *cp
== '\t') {
243 /* null terminate last domain if there are excess */
244 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
251 /* read nameservers to query */
252 if (!strncmp(buf
, "nameserver", sizeof ("nameserver") - 1) &&
254 cp
= buf
+ sizeof ("nameserver") - 1;
255 while (*cp
== ' ' || *cp
== '\t')
257 if ((*cp
== '\0') || (*cp
== '\n'))
259 if ((_res
.nsaddr_list
[nserv
].sin_addr
.s_addr
=
260 inet_addr(cp
)) == (unsigned) -1) {
261 _res
.nsaddr_list
[n
].sin_addr
.s_addr
= INADDR_ANY
;
264 _res
.nsaddr_list
[nserv
].sin_family
= AF_INET
;
265 _res
.nsaddr_list
[nserv
].sin_port
= htons(NAMESERVER_PORT
);
271 _res
.nscount
= nserv
;
274 if (_res
.defdname
[0] == 0) {
275 if (gethostname(buf
, sizeof (_res
.defdname
)) == 0 &&
277 (cp
= strchr(buf
, (int)'.')))
279 (cp
= index(buf
, '.')))
281 (void) strcpy(_res
.defdname
, cp
+ 1);
284 /* find components of local domain that might be searched */
285 if (havesearch
== 0) {
287 *pp
++ = _res
.defdname
;
288 for (cp
= _res
.defdname
, n
= 0; *cp
; cp
++)
292 for (; n
>= LOCALDOMAINPARTS
&& pp
< _res
.dnsrch
+ MAXDFLSRCH
; n
--) {
294 cp
= strchr(cp
, (int)'.');
302 _res
.options
|= RES_INIT
;