xref: /csrg-svn/lib/libc/net/res_init.c (revision 18142)
1 #ifndef lint
2 static char sccsid[] = "@(#)res_init.c	4.1 (Berkeley) 03/01/85";
3 #endif
4 
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <stdio.h>
9 #include <nameser.h>
10 #include <resolv.h>
11 
12 /*
13  * Resolver state default settings
14  */
15 struct state _res = {
16 	90,
17 	2,
18 	RES_RECURSE|RES_DEFNAMES,
19 };
20 
21 /*
22  * Read the configuration file for default settings.
23  * Return true if the name server address is initialized.
24  */
25 res_init()
26 {
27 	FILE *fp;
28 	char buf[BUFSIZ], *cp;
29 	int n;
30 	extern u_long inet_addr();
31 	extern char *index();
32 
33 	_res.options |= RES_INIT;
34 	_res.nsaddr.sin_family = AF_INET;
35 	_res.nsaddr.sin_addr.s_addr = INADDR_ANY;
36 	_res.nsaddr.sin_port = HTONS(53);	/* well known port number */
37 
38 	/* first try reading the config file */
39 	if ((fp = fopen(CONFFILE, "r")) != NULL) {
40 		if (fgets(_res.defdname, MAXDNAME, fp) == NULL)
41 			_res.defdname[0] = '\0';
42 		else if ((cp = index(_res.defdname, '\n')) != NULL)
43 			*cp = '\0';
44 		if (fgets(buf, sizeof (buf), fp) != NULL) {
45 			(void) fclose(fp);
46 			_res.nsaddr.sin_addr.s_addr = inet_addr(buf);
47 			return (1);
48 		}
49 		(void) fclose(fp);
50 	}
51 
52 	/* next, try getting the address of this host */
53 
54 	/* finally, try broadcast */
55 
56 	return (0);
57 }
58