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