Print this page
1926 libresolv evades compiler warnings
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libresolv/res_init.c
+++ new/usr/src/lib/libresolv/res_init.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 + * Copyright 2015 Gary Mills
23 24 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 25 * Use is subject to license terms.
25 26 */
26 27
27 28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 29 /* All Rights Reserved */
29 30
30 31 /*
31 32 * University Copyright- Copyright (c) 1982, 1986, 1988
32 33 * The Regents of the University of California
33 34 * All Rights Reserved
34 35 *
35 36 * University Acknowledgment- Portions of this document are derived from
36 37 * software developed by the University of California, Berkeley, and its
37 38 * contributors.
38 39 */
39 40
40 -#pragma ident "%Z%%M% %I% %E% SMI"
41 -
42 41 #include <sys/types.h>
43 42 #include <sys/sockio.h>
44 43 #include <sys/socket.h>
45 44 #include <netinet/in.h>
46 45 #include <stdio.h>
46 +#include <string.h>
47 +#include <stdlib.h>
48 +#include <unistd.h>
49 +#include <stropts.h>
47 50 #include <arpa/nameser.h>
48 51 #include <resolv.h>
49 52
50 53 #include <netinet/in.h>
51 54 #include <net/if.h>
52 55 #include <netinet/if_ether.h>
53 56 #include <arpa/inet.h>
54 57
58 +/*
59 + * Undocumented external function in libnsl
60 + */
61 +extern int
62 +getdomainname(char *, int);
63 +
55 64 #define MAXIFS 256
56 65
57 66 /*
58 67 * Resolver state default settings
59 68 */
60 69
61 70 struct state _res = {
62 71 RES_TIMEOUT, /* retransmition time interval */
63 72 4, /* number of times to retransmit */
64 73 RES_DEFAULT, /* options flags */
65 74 1, /* number of name servers */
66 75 };
67 76
68 77 /*
69 78 * Set up default settings. If the configuration file exist, the values
70 79 * there will have precedence. Otherwise, the server address is set to
71 80 * INADDR_LOOPBACK and the default domain name comes from the gethostname().
72 81 * BUT if the NIS/RPC domain name is set, that is used if all else fails.
73 82 *
74 83 * The configuration file should only be used if you want to redefine your
75 84 * domain or run without a server on your machine.
76 85 *
77 86 * Note the user can always override then domain name with the environment
78 87 * variable LOCALDOMAIN which has absolute priority.
79 88 *
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
80 89 *
81 90 * Return 0 if completes successfully, -1 on error
82 91 */
83 92 int
84 93 res_init(void)
85 94 {
86 95 register FILE *fp;
87 96 register char *cp, **pp;
88 97 register int n;
89 98 char buf[BUFSIZ];
90 -#ifdef SYSV
91 - extern char *strchr();
92 -#else
93 - extern char *index();
94 -#endif
95 - extern char *strcpy(), *strncpy();
96 - extern char *getenv();
97 99 int nserv = 0; /* number of nameserver records read from file */
98 100 int haveenv = 0;
99 101 int havesearch = 0;
100 102
101 103 _res.nsaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* INADDR_ANY */
102 104 _res.nsaddr.sin_family = AF_INET;
103 105 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
104 106 _res.nscount = 1;
105 107
106 108 #ifdef SIOCGIFNUM
107 109 { int numifs, s, n, int_up;
108 110 struct ifconf ifc;
109 111 register struct ifreq *ifrp;
110 112 struct ifreq ifr;
111 113 unsigned bufsize;
112 114 unsigned int flags;
113 115 char *buf;
114 - extern void *malloc();
115 116
116 117 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
117 118 perror("socket");
118 119 return (-1);
119 120 }
120 121 if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
121 122 numifs = MAXIFS;
122 123 }
123 124 bufsize = numifs * sizeof (struct ifreq);
124 125 buf = (char *)malloc(bufsize);
125 126 if (buf == NULL) {
126 127 perror("out of memory");
127 - close(s);
128 + (void) close(s);
128 129 return (-1);
129 130 }
130 131 ifc.ifc_len = bufsize;
131 132 ifc.ifc_buf = buf;
132 133 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
133 134 perror("ifconfig: SIOCGIFCONF");
134 - close(s);
135 + (void) close(s);
135 136 free(buf);
136 137 return (-1);
137 138 }
138 139
139 140 int_up = 0;
140 141 ifrp = ifc.ifc_req;
141 142 for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0;
142 143 n--, ifrp++) {
143 - memset((void *) &ifr, 0, sizeof (ifr));
144 + (void) memset((void *) &ifr, 0, sizeof (ifr));
144 145 strncpy(ifr.ifr_name, ifrp->ifr_name,
145 146 sizeof (ifr.ifr_name));
146 147 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) {
147 148 perror("SIOCGIFFLAGS");
148 - close(s);
149 + (void) close(s);
149 150 free(buf);
150 151 return (-1);
151 152 }
152 153 flags = ifr.ifr_flags;
153 154 /* we are looking for a non-loopback interface */
154 155 if ((flags & IFF_UP) && ((flags & IFF_LOOPBACK) == 0))
155 156 int_up = 1;
156 157 }
157 - close(s);
158 + (void) close(s);
158 159 free(buf);
159 160 if (int_up == 0) /* all the non-LOOPBACK interfaces are DOWN */
160 161 return (-1);
161 162 }
162 163 #endif /* SIOCGIFNUM */
163 164
164 165
165 166 /*
166 167 * for the benefit of hidden NIS domains, we use the same procedure
167 168 * as sendmail: convert leading + to dot, then drop to first dot
168 169 */
169 - getdomainname(buf, BUFSIZ);
170 + (void) getdomainname(buf, BUFSIZ);
170 171 if (buf[0] == '+')
171 - buf[0] = '.';
172 + buf[0] = '.';
172 173 #ifdef SYSV
173 174 cp = strchr(buf, (int)'.');
174 175 #else
175 176 cp = index(buf, '.');
176 177 #endif
177 178 if (cp == NULL)
178 179 strcpy(_res.defdname, buf);
179 180 else
180 181 strcpy(_res.defdname, cp+1);
181 182
182 183 /* Allow user to override the local domain definition */
183 184 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
184 185 (void) strncpy(_res.defdname, cp, sizeof (_res.defdname));
185 186 haveenv++;
186 187 }
187 188
188 189 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
189 190 /* read the config file */
190 191 while (fgets(buf, sizeof (buf), fp) != NULL) {
191 192 /* read default domain name */
192 193 if (!strncmp(buf, "domain", sizeof ("domain") - 1)) {
193 194 if (haveenv) /* skip if have from environ */
194 195 continue;
195 196 cp = buf + sizeof ("domain") - 1;
196 197 while (*cp == ' ' || *cp == '\t')
197 198 cp++;
198 199 if ((*cp == '\0') || (*cp == '\n'))
199 200 continue;
200 201 (void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
201 202 #ifdef SYSV
202 203 if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
203 204 #else
204 205 if ((cp = index(_res.defdname, '\n')) != NULL)
205 206 #endif
206 207 *cp = '\0';
207 208 havesearch = 0;
208 209 continue;
209 210 }
210 211 /* set search list */
211 212 if (!strncmp(buf, "search", sizeof ("search") - 1)) {
212 213 if (haveenv) /* skip if have from environ */
213 214 continue;
214 215 cp = buf + sizeof ("search") - 1;
215 216 while (*cp == ' ' || *cp == '\t')
216 217 cp++;
217 218 if ((*cp == '\0') || (*cp == '\n'))
218 219 continue;
219 220 (void) strncpy(_res.defdname, cp, sizeof (_res.defdname) - 1);
220 221 #ifdef SYSV
221 222 if ((cp = strchr(_res.defdname, (int)'\n')) != NULL)
222 223 #else
223 224 if ((cp = index(_res.defdname, '\n')) != NULL)
224 225 #endif
225 226 *cp = '\0';
226 227 /*
227 228 * Set search list to be blank-separated strings
228 229 * on rest of line.
229 230 */
230 231 cp = _res.defdname;
231 232 pp = _res.dnsrch;
232 233 *pp++ = cp;
233 234 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
234 235 if (*cp == ' ' || *cp == '\t') {
235 236 *cp = 0;
236 237 n = 1;
237 238 } else if (n) {
238 239 *pp++ = cp;
239 240 n = 0;
240 241 }
241 242 }
242 243 /* null terminate last domain if there are excess */
243 244 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
244 245 cp++;
245 246 *cp = '\0';
246 247 *pp++ = 0;
247 248 havesearch = 1;
248 249 continue;
249 250 }
250 251 /* read nameservers to query */
251 252 if (!strncmp(buf, "nameserver", sizeof ("nameserver") - 1) &&
252 253 (nserv < MAXNS)) {
253 254 cp = buf + sizeof ("nameserver") - 1;
254 255 while (*cp == ' ' || *cp == '\t')
255 256 cp++;
256 257 if ((*cp == '\0') || (*cp == '\n'))
257 258 continue;
258 259 if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
259 260 inet_addr(cp)) == (unsigned) -1) {
260 261 _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
261 262 continue;
262 263 }
263 264 _res.nsaddr_list[nserv].sin_family = AF_INET;
264 265 _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
265 266 nserv++;
266 267 continue;
267 268 }
268 269 }
269 270 if (nserv > 1)
270 271 _res.nscount = nserv;
271 272 (void) fclose(fp);
272 273 }
273 274 if (_res.defdname[0] == 0) {
274 275 if (gethostname(buf, sizeof (_res.defdname)) == 0 &&
275 276 #ifdef SYSV
276 277 (cp = strchr(buf, (int)'.')))
277 278 #else
278 279 (cp = index(buf, '.')))
279 280 #endif
280 281 (void) strcpy(_res.defdname, cp + 1);
281 282 }
282 283
283 284 /* find components of local domain that might be searched */
284 285 if (havesearch == 0) {
285 286 pp = _res.dnsrch;
286 287 *pp++ = _res.defdname;
287 288 for (cp = _res.defdname, n = 0; *cp; cp++)
288 289 if (*cp == '.')
289 290 n++;
290 291 cp = _res.defdname;
291 292 for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; n--) {
292 293 #ifdef SYSV
293 294 cp = strchr(cp, (int)'.');
294 295 #else
295 296 cp = index(cp, '.');
296 297 #endif
297 298 *pp++ = ++cp;
298 299 }
299 300 *pp++ = 0;
300 301 }
301 302 _res.options |= RES_INIT;
302 303 return (0);
303 304 }
↓ open down ↓ |
122 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX