xref: /csrg-svn/lib/libc/net/res_init.c (revision 24111)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)res_init.c	5.4 (Berkeley) 07/31/85";
9 #endif not lint
10 
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <stdio.h>
15 #include <arpa/nameser.h>
16 #include <arpa/resolv.h>
17 
18 /*
19  * Resolver configuration file. Contains the address of the
20  * inital name server to query and the default domain for
21  * non fully qualified domain names.
22  */
23 
24 #ifdef CONFFILE
25 char	*conffile = CONFFILE;
26 #else
27 char	*conffile = "/usr/local/lib/resolv.conf";
28 #endif
29 
30 /*
31  * Resolver state default settings
32  */
33 struct state _res = {
34 	90,
35 	2,
36 	RES_RECURSE|RES_DEFNAMES,
37 };
38 
39 /*
40  * Read the configuration file for default settings.
41  * Return true if the name server address is initialized.
42  */
43 res_init()
44 {
45 	FILE *fp;
46 	char buf[BUFSIZ], *cp;
47 	int n;
48 	extern u_long inet_addr();
49 	extern char *index(), *getenv();
50 
51 	_res.options |= RES_INIT;
52 	_res.nsaddr.sin_family = AF_INET;
53 	_res.nsaddr.sin_addr.s_addr = INADDR_ANY;
54 	_res.nsaddr.sin_port = htons(NAMESERVER_PORT);
55 
56 	/* first try reading the config file */
57 	if ((fp = fopen(conffile, "r")) != NULL) {
58 		if (fgets(_res.defdname, sizeof(_res.defdname), fp) == NULL)
59 			_res.defdname[0] = '\0';
60 		else if ((cp = index(_res.defdname, '\n')) != NULL)
61 			*cp = '\0';
62 		if (fgets(buf, sizeof (buf), fp) != NULL)
63 			_res.nsaddr.sin_addr.s_addr = inet_addr(buf);
64 		(void) fclose(fp);
65 	}
66 
67 	/* Allow user to override the local domain definition */
68 	if ((cp = getenv("LOCALDOMAIN")) != NULL)
69 		strncpy(_res.defdname, cp, sizeof(_res.defdname));
70 }
71