xref: /original-bsd/lib/libc/net/res_init.c (revision b7bd495c)
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 #if defined(LIBC_SCCS) && !defined(lint)
8 static char sccsid[] = "@(#)res_init.c	6.7 (Berkeley) 09/30/87";
9 #endif LIBC_SCCS and 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 <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 #ifndef	CONFFILE
25 #define	CONFFILE	"/etc/resolv.conf"
26 #endif
27 
28 /*
29  * Resolver state default settings
30  */
31 
32 struct state _res = {
33     RES_TIMEOUT,               	/* retransmition time interval */
34     4,                         	/* number of times to retransmit */
35     RES_DEFAULT,		/* options flags */
36     1,                         	/* number of name servers */
37 };
38 
39 /*
40  * Set up default settings.  If the configuration file exist, the values
41  * there will have precedence.  Otherwise, the server address is set to
42  * INADDR_ANY and the default domain name comes from the gethostname().
43  *
44  * The configuration file should only be used if you want to redefine your
45  * domain or run without a server on your machine.
46  *
47  * Return 0 if completes successfully, -1 on error
48  */
49 res_init()
50 {
51     register FILE *fp;
52     register char *cp, **pp;
53     char buf[BUFSIZ];
54     extern u_long inet_addr();
55     extern char *index();
56     extern char *strcpy(), *strncpy();
57     extern char *getenv();
58     int n = 0;    /* number of nameserver records read from file */
59 
60     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
61     _res.nsaddr.sin_family = AF_INET;
62     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
63     _res.nscount = 1;
64     _res.defdname[0] = '\0';
65 
66     if ((fp = fopen(CONFFILE, "r")) != NULL) {
67         /* read the config file */
68         while (fgets(buf, sizeof(buf), fp) != NULL) {
69             /* read default domain name */
70             if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
71                 cp = buf + sizeof("domain") - 1;
72                 while (*cp == ' ' || *cp == '\t')
73                     cp++;
74                 if (*cp == '\0')
75                     continue;
76                 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
77                 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
78                 if ((cp = index(_res.defdname, '\n')) != NULL)
79                     *cp = '\0';
80                 continue;
81             }
82             /* read nameservers to query */
83             if (!strncmp(buf, "nameserver",
84                sizeof("nameserver") - 1) && (n < MAXNS)) {
85                 cp = buf + sizeof("nameserver") - 1;
86                 while (*cp == ' ' || *cp == '\t')
87                     cp++;
88                 if (*cp == '\0')
89                     continue;
90                 _res.nsaddr_list[n].sin_addr.s_addr = inet_addr(cp);
91                 if (_res.nsaddr_list[n].sin_addr.s_addr == (unsigned)-1)
92                     _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
93                 _res.nsaddr_list[n].sin_family = AF_INET;
94                 _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT);
95                 if ( ++n >= MAXNS) {
96                     n = MAXNS;
97 #ifdef DEBUG
98                     if ( _res.options & RES_DEBUG )
99                         printf("MAXNS reached, reading resolv.conf\n");
100 #endif DEBUG
101                 }
102                 continue;
103             }
104         }
105         if ( n > 1 )
106             _res.nscount = n;
107         (void) fclose(fp);
108     }
109     if (_res.defdname[0] == 0) {
110         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
111            (cp = index(buf, '.')))
112              (void)strcpy(_res.defdname, cp + 1);
113     }
114 
115     /* Allow user to override the local domain definition */
116     if ((cp = getenv("LOCALDOMAIN")) != NULL)
117         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
118 
119     /* find components of local domain that might be searched */
120     pp = _res.dnsrch;
121     *pp++ = _res.defdname;
122     for (cp = _res.defdname, n = 0; *cp; cp++)
123 	if (*cp == '.')
124 	    n++;
125     cp = _res.defdname;
126     for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDNSRCH; n--) {
127 	cp = index(cp, '.');
128 	*pp++ = ++cp;
129     }
130     _res.options |= RES_INIT;
131     return(0);
132 }
133