Commit | Line | Data |
---|---|---|
b423e985 | 1 | /* |
6ad0a189 | 2 | * Copyright (c) 1985, 1989 Regents of the University of California. |
6b2f9dd0 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
f4f66d2c KB |
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. | |
b423e985 RC |
16 | */ |
17 | ||
2ce81398 | 18 | #if defined(LIBC_SCCS) && !defined(lint) |
04eb0ba8 | 19 | static char sccsid[] = "@(#)res_init.c 6.11 (Berkeley) %G%"; |
6b2f9dd0 | 20 | #endif /* LIBC_SCCS and not lint */ |
8ea4199d | 21 | |
b53e7108 RC |
22 | #include <sys/types.h> |
23 | #include <sys/socket.h> | |
24 | #include <netinet/in.h> | |
25 | #include <stdio.h> | |
56d853b7 | 26 | #include <arpa/nameser.h> |
a36af7fe | 27 | #include <resolv.h> |
b53e7108 RC |
28 | |
29 | /* | |
30 | * Resolver state default settings | |
31 | */ | |
dc795f66 | 32 | |
b53e7108 | 33 | struct state _res = { |
6ad0a189 MK |
34 | RES_TIMEOUT, /* retransmition time interval */ |
35 | 4, /* number of times to retransmit */ | |
36 | RES_DEFAULT, /* options flags */ | |
37 | 1, /* number of name servers */ | |
b53e7108 RC |
38 | }; |
39 | ||
40 | /* | |
98959623 JB |
41 | * Set up default settings. If the configuration file exist, the values |
42 | * there will have precedence. Otherwise, the server address is set to | |
43 | * INADDR_ANY and the default domain name comes from the gethostname(). | |
44 | * | |
45 | * The configuration file should only be used if you want to redefine your | |
46 | * domain or run without a server on your machine. | |
47 | * | |
48 | * Return 0 if completes successfully, -1 on error | |
b53e7108 RC |
49 | */ |
50 | res_init() | |
51 | { | |
6ad0a189 MK |
52 | register FILE *fp; |
53 | register char *cp, **pp; | |
54 | register int n; | |
55 | char buf[BUFSIZ]; | |
56 | extern u_long inet_addr(); | |
57 | extern char *index(); | |
58 | extern char *strcpy(), *strncpy(); | |
59 | extern char *getenv(); | |
60 | int nserv = 0; /* number of nameserver records read from file */ | |
05f93cda | 61 | int haveenv = 0; |
6ad0a189 | 62 | int havesearch = 0; |
b53e7108 | 63 | |
6ad0a189 MK |
64 | _res.nsaddr.sin_addr.s_addr = INADDR_ANY; |
65 | _res.nsaddr.sin_family = AF_INET; | |
66 | _res.nsaddr.sin_port = htons(NAMESERVER_PORT); | |
67 | _res.nscount = 1; | |
b53e7108 | 68 | |
6ad0a189 | 69 | /* Allow user to override the local domain definition */ |
05f93cda | 70 | if ((cp = getenv("LOCALDOMAIN")) != NULL) { |
6ad0a189 | 71 | (void)strncpy(_res.defdname, cp, sizeof(_res.defdname)); |
05f93cda MK |
72 | haveenv++; |
73 | } | |
b53e7108 | 74 | |
6ad0a189 MK |
75 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { |
76 | /* read the config file */ | |
77 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
78 | /* read default domain name */ | |
79 | if (!strncmp(buf, "domain", sizeof("domain") - 1)) { | |
05f93cda | 80 | if (haveenv) /* skip if have from environ */ |
6ad0a189 MK |
81 | continue; |
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) - 1); | |
88 | if ((cp = index(_res.defdname, '\n')) != NULL) | |
89 | *cp = '\0'; | |
90 | havesearch = 0; | |
91 | continue; | |
92 | } | |
93 | /* set search list */ | |
94 | if (!strncmp(buf, "search", sizeof("search") - 1)) { | |
05f93cda MK |
95 | if (haveenv) /* skip if have from environ */ |
96 | continue; | |
6ad0a189 MK |
97 | cp = buf + sizeof("search") - 1; |
98 | while (*cp == ' ' || *cp == '\t') | |
99 | cp++; | |
100 | if (*cp == '\0') | |
101 | continue; | |
102 | (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); | |
103 | if ((cp = index(_res.defdname, '\n')) != NULL) | |
104 | *cp = '\0'; | |
105 | /* | |
106 | * Set search list to be blank-separated strings | |
107 | * on rest of line. | |
108 | */ | |
109 | cp = _res.defdname; | |
110 | pp = _res.dnsrch; | |
111 | *pp++ = cp; | |
112 | for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { | |
113 | if (*cp == ' ' || *cp == '\t') { | |
114 | *cp = 0; | |
115 | n = 1; | |
116 | } else if (n) { | |
117 | *pp++ = cp; | |
118 | n = 0; | |
119 | } | |
120 | } | |
05f93cda | 121 | *pp++ = 0; |
6ad0a189 MK |
122 | havesearch = 1; |
123 | continue; | |
124 | } | |
125 | /* read nameservers to query */ | |
126 | if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) && | |
127 | nserv < MAXNS) { | |
128 | cp = buf + sizeof("nameserver") - 1; | |
129 | while (*cp == ' ' || *cp == '\t') | |
130 | cp++; | |
131 | if (*cp == '\0') | |
132 | continue; | |
133 | if ((_res.nsaddr_list[nserv].sin_addr.s_addr = | |
134 | inet_addr(cp)) == (unsigned)-1) | |
135 | continue; | |
136 | _res.nsaddr_list[nserv].sin_family = AF_INET; | |
137 | _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT); | |
138 | nserv++; | |
139 | continue; | |
140 | } | |
141 | } | |
142 | if (nserv > 1) | |
143 | _res.nscount = nserv; | |
144 | (void) fclose(fp); | |
145 | } | |
146 | if (_res.defdname[0] == 0) { | |
147 | if (gethostname(buf, sizeof(_res.defdname)) == 0 && | |
148 | (cp = index(buf, '.'))) | |
149 | (void)strcpy(_res.defdname, cp + 1); | |
150 | } | |
a6c365b5 | 151 | |
6ad0a189 MK |
152 | /* find components of local domain that might be searched */ |
153 | if (havesearch == 0) { | |
154 | pp = _res.dnsrch; | |
155 | *pp++ = _res.defdname; | |
156 | for (cp = _res.defdname, n = 0; *cp; cp++) | |
157 | if (*cp == '.') | |
158 | n++; | |
159 | cp = _res.defdname; | |
05f93cda | 160 | for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; |
6ad0a189 MK |
161 | n--) { |
162 | cp = index(cp, '.'); | |
163 | *pp++ = ++cp; | |
164 | } | |
05f93cda | 165 | *pp++ = 0; |
6ad0a189 MK |
166 | } |
167 | _res.options |= RES_INIT; | |
168 | return (0); | |
b53e7108 | 169 | } |