xref: /csrg-svn/lib/libc/net/res_init.c (revision 24736)
118548Sralph /*
221386Sdist  * Copyright (c) 1985 Regents of the University of California.
321386Sdist  * All rights reserved.  The Berkeley software License Agreement
421386Sdist  * specifies the terms and conditions for redistribution.
518548Sralph  */
618548Sralph 
721386Sdist #ifndef lint
8*24736Sbloom static char sccsid[] = "@(#)res_init.c	5.6 (Berkeley) 09/14/85";
921386Sdist #endif not lint
1021386Sdist 
1118142Sralph #include <sys/types.h>
1218142Sralph #include <sys/socket.h>
1318142Sralph #include <netinet/in.h>
1418142Sralph #include <stdio.h>
15*24736Sbloom #include <netdb.h>
1624081Skjd #include <arpa/nameser.h>
1724081Skjd #include <arpa/resolv.h>
1818142Sralph 
1918142Sralph /*
2024111Skjd  * Resolver configuration file. Contains the address of the
2124111Skjd  * inital name server to query and the default domain for
2224111Skjd  * non fully qualified domain names.
2324111Skjd  */
2424111Skjd 
2524111Skjd #ifdef CONFFILE
2624111Skjd char	*conffile = CONFFILE;
2724111Skjd #else
28*24736Sbloom char	*conffile = "/etc/resolv.conf";
2924111Skjd #endif
3024111Skjd 
3124111Skjd /*
3218142Sralph  * Resolver state default settings
3318142Sralph  */
3418142Sralph struct state _res = {
3524167Skjd 	10,
3624167Skjd 	4,
3718142Sralph 	RES_RECURSE|RES_DEFNAMES,
3818142Sralph };
3918142Sralph 
4018142Sralph /*
41*24736Sbloom  * Set up default settings.  If the configuration file exist, the values
42*24736Sbloom  * there will have precedence.  Otherwise, the server address is set to
43*24736Sbloom  * INADDR_ANY and the default domain name comes from the gethostname().
44*24736Sbloom  *
45*24736Sbloom  * The configuration file should only be used if you want to redefine your
46*24736Sbloom  * domain or run without a server on your machine.
47*24736Sbloom  *
48*24736Sbloom  * Return 0 if completes successfully, -1 on error
4918142Sralph  */
5018142Sralph res_init()
5118142Sralph {
5218142Sralph 	FILE *fp;
5318142Sralph 	char buf[BUFSIZ], *cp;
5418142Sralph 	extern u_long inet_addr();
55*24736Sbloom 	extern char *index();
56*24736Sbloom 	extern char *strcpy(), *strncpy();
57*24736Sbloom 	struct servent *serv;
58*24736Sbloom #ifdef DEBUG
59*24736Sbloom 	extern char *getenv();
60*24736Sbloom #endif
6118142Sralph 
6218142Sralph 	_res.nsaddr.sin_family = AF_INET;
6318142Sralph 	_res.nsaddr.sin_addr.s_addr = INADDR_ANY;
64*24736Sbloom 	_res.defdname[0] = '\0';
65*24736Sbloom 	if ((serv = getservbyname(NAMESERVER_SNAME, "tcp")) == NULL)
66*24736Sbloom 		return(-1);
67*24736Sbloom 	_res.nsaddr.sin_port = (u_short)serv->s_port;
6818142Sralph 
6924111Skjd 	if ((fp = fopen(conffile, "r")) != NULL) {
70*24736Sbloom 		while (fgets(buf, sizeof(buf), fp) != NULL) {
71*24736Sbloom 			if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
72*24736Sbloom 				cp = buf + sizeof("domain") - 1;
73*24736Sbloom 				while (*cp == ' ' || *cp == '\t')
74*24736Sbloom 					cp++;
75*24736Sbloom 				if (*cp == '\0')
76*24736Sbloom 					continue;
77*24736Sbloom 				(void)strncpy(_res.defdname, cp,
78*24736Sbloom 				        sizeof(_res.defdname));
79*24736Sbloom 				_res.defdname[sizeof(_res.defdname) - 1] = '\0';
80*24736Sbloom 				if ((cp = index(_res.defdname, '\n')) != NULL)
81*24736Sbloom 					*cp = '\0';
82*24736Sbloom 				continue;
83*24736Sbloom 			}
84*24736Sbloom 			if (!strncmp(buf, "resolver", sizeof("resolver") - 1)) {
85*24736Sbloom 				cp = buf + sizeof("resolver") - 1;
86*24736Sbloom 				while (*cp == ' ' || *cp == '\t')
87*24736Sbloom 					cp++;
88*24736Sbloom 				if (*cp == '\0')
89*24736Sbloom 					continue;
90*24736Sbloom 				_res.nsaddr.sin_addr.s_addr = inet_addr(cp);
91*24736Sbloom 				if (_res.nsaddr.sin_addr.s_addr == (unsigned)-1)
92*24736Sbloom 					_res.nsaddr.sin_addr.s_addr =
93*24736Sbloom 						INADDR_ANY;
94*24736Sbloom 				continue;
95*24736Sbloom 			}
96*24736Sbloom 		}
9718142Sralph 		(void) fclose(fp);
9818142Sralph 	}
99*24736Sbloom 	if (_res.defdname[0] == 0) {
100*24736Sbloom 		if (gethostname(buf, sizeof(_res.defdname)) == -1)
101*24736Sbloom 			return(-1);
102*24736Sbloom 		if ((cp = index(buf, '.')) == NULL)
103*24736Sbloom 			return(-1);
104*24736Sbloom 		(void)strcpy(_res.defdname, cp + 1);
105*24736Sbloom 	}
10618142Sralph 
107*24736Sbloom #ifdef DEBUG
10818527Sralph 	/* Allow user to override the local domain definition */
10918527Sralph 	if ((cp = getenv("LOCALDOMAIN")) != NULL)
110*24736Sbloom 		(void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
111*24736Sbloom #endif
112*24736Sbloom 	_res.options |= RES_INIT;
113*24736Sbloom 	return(0);
11418142Sralph }
115