|
|
1.1 root 1: /*
2: * Copyright (c) 1985 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #if defined(LIBC_SCCS) && !defined(lint)
19: static char sccsid[] = "@(#)res_init.c 6.9 (Berkeley) 6/27/88";
20: #endif /* LIBC_SCCS and not lint */
21:
22: #include <sys/types.h>
23: #include <sys/socket.h>
24: #include <netinet/in.h>
25: #include <stdio.h>
26: #include <arpa/nameser.h>
27: #include <resolv.h>
28:
29: /*
30: * Resolver configuration file. Contains the address of the
31: * inital name server to query and the default domain for
32: * non fully qualified domain names.
33: */
34:
35: #ifndef CONFFILE
36: #define CONFFILE "/etc/resolv.conf"
37: #endif
38:
39: /*
40: * Resolver state default settings
41: */
42:
43: struct state _res = {
44: RES_TIMEOUT, /* retransmition time interval */
45: 4, /* number of times to retransmit */
46: RES_DEFAULT, /* options flags */
47: 1, /* number of name servers */
48: };
49:
50: /*
51: * Set up default settings. If the configuration file exist, the values
52: * there will have precedence. Otherwise, the server address is set to
53: * INADDR_ANY and the default domain name comes from the gethostname().
54: *
55: * The configuration file should only be used if you want to redefine your
56: * domain or run without a server on your machine.
57: *
58: * Return 0 if completes successfully, -1 on error
59: */
60: res_init()
61: {
62: register FILE *fp;
63: register char *cp, **pp;
64: char buf[BUFSIZ];
65: extern u_long inet_addr();
66: extern char *index();
67: extern char *strcpy(), *strncpy();
68: extern char *getenv();
69: int n = 0; /* number of nameserver records read from file */
70:
71: _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
72: _res.nsaddr.sin_family = AF_INET;
73: _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
74: _res.nscount = 1;
75: _res.defdname[0] = '\0';
76:
77: if ((fp = fopen(CONFFILE, "r")) != NULL) {
78: /* read the config file */
79: while (fgets(buf, sizeof(buf), fp) != NULL) {
80: /* read default domain name */
81: if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
82: cp = buf + sizeof("domain") - 1;
83: while (*cp == ' ' || *cp == '\t')
84: cp++;
85: if (*cp == '\0')
86: continue;
87: (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
88: _res.defdname[sizeof(_res.defdname) - 1] = '\0';
89: if ((cp = index(_res.defdname, '\n')) != NULL)
90: *cp = '\0';
91: continue;
92: }
93: /* read nameservers to query */
94: if (!strncmp(buf, "nameserver",
95: sizeof("nameserver") - 1) && (n < MAXNS)) {
96: cp = buf + sizeof("nameserver") - 1;
97: while (*cp == ' ' || *cp == '\t')
98: cp++;
99: if (*cp == '\0')
100: continue;
101: _res.nsaddr_list[n].sin_addr.s_addr = inet_addr(cp);
102: if (_res.nsaddr_list[n].sin_addr.s_addr == (unsigned)-1)
103: _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
104: _res.nsaddr_list[n].sin_family = AF_INET;
105: _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT);
106: if ( ++n >= MAXNS) {
107: n = MAXNS;
108: #ifdef DEBUG
109: if ( _res.options & RES_DEBUG )
110: printf("MAXNS reached, reading resolv.conf\n");
111: #endif DEBUG
112: }
113: continue;
114: }
115: }
116: if ( n > 1 )
117: _res.nscount = n;
118: (void) fclose(fp);
119: }
120: if (_res.defdname[0] == 0) {
121: if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
122: (cp = index(buf, '.')))
123: (void)strcpy(_res.defdname, cp + 1);
124: }
125:
126: /* Allow user to override the local domain definition */
127: if ((cp = getenv("LOCALDOMAIN")) != NULL)
128: (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
129:
130: /* find components of local domain that might be searched */
131: pp = _res.dnsrch;
132: *pp++ = _res.defdname;
133: for (cp = _res.defdname, n = 0; *cp; cp++)
134: if (*cp == '.')
135: n++;
136: cp = _res.defdname;
137: for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDNSRCH; n--) {
138: cp = index(cp, '.');
139: *pp++ = ++cp;
140: }
141: _res.options |= RES_INIT;
142: return(0);
143: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.