]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/res_init.c
UefiCpuPkg/MicrocodeUpdateDxe: Fix coding style issues in INF file
[mirror_edk2.git] / StdLib / BsdSocketLib / res_init.c
CommitLineData
d7ce7006 1/*\r
2 * Copyright (c) 1985, 1989, 1993\r
3 * The Regents of the University of California. All rights reserved.\r
4 *\r
5 * Portions copyright (c) 1999, 2000\r
6 * Intel Corporation.\r
7 * All rights reserved.\r
8 *\r
9 * Redistribution and use in source and binary forms, with or without\r
10 * modification, are permitted provided that the following conditions\r
11 * are met:\r
12 *\r
13 * 1. Redistributions of source code must retain the above copyright\r
14 * notice, this list of conditions and the following disclaimer.\r
15 *\r
16 * 2. Redistributions in binary form must reproduce the above copyright\r
17 * notice, this list of conditions and the following disclaimer in the\r
18 * documentation and/or other materials provided with the distribution.\r
19 *\r
20 * 3. All advertising materials mentioning features or use of this software\r
21 * must display the following acknowledgement:\r
22 *\r
23 * This product includes software developed by the University of\r
24 * California, Berkeley, Intel Corporation, and its contributors.\r
25 *\r
26 * 4. Neither the name of University, Intel Corporation, or their respective\r
27 * contributors may be used to endorse or promote products derived from\r
28 * this software without specific prior written permission.\r
29 *\r
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND\r
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,\r
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
41 *\r
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
45 * SUCH DAMAGE.\r
46 */\r
47\r
48/*\r
49 * Portions Copyright (c) 1993 by Digital Equipment Corporation.\r
50 *\r
51 * Permission to use, copy, modify, and distribute this software for any\r
52 * purpose with or without fee is hereby granted, provided that the above\r
53 * copyright notice and this permission notice appear in all copies, and that\r
54 * the name of Digital Equipment Corporation not be used in advertising or\r
55 * publicity pertaining to distribution of the document or software without\r
56 * specific, written prior permission.\r
57 *\r
58 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL\r
59 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES\r
60 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT\r
61 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
62 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
63 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
64 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
65 * SOFTWARE.\r
66 */\r
67\r
68/*\r
69 * Portions Copyright (c) 1996 by Internet Software Consortium.\r
70 *\r
71 * Permission to use, copy, modify, and distribute this software for any\r
72 * purpose with or without fee is hereby granted, provided that the above\r
73 * copyright notice and this permission notice appear in all copies.\r
74 *\r
75 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
76 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
77 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
78 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
79 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
80 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
81 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
82 * SOFTWARE.\r
83 */\r
84\r
85#if defined(LIBC_SCCS) && !defined(lint)\r
86static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";\r
87static char orig_rcsid[] = "From: Id: res_init.c,v 8.7 1996/11/18 09:10:04 vixie Exp $";\r
88static char rcsid[] = "$Id: res_init.c,v 1.1.1.1 2003/11/19 01:51:37 kyu3 Exp $";\r
89#endif /* LIBC_SCCS and not lint */\r
90\r
91#include <sys/types.h>\r
92#include <sys/param.h>\r
93#include <sys/socket.h>\r
94#include <sys/time.h>\r
95#include <netinet/in.h>\r
96#include <arpa/inet.h>\r
97#include <arpa/nameser.h>\r
98#include <ctype.h>\r
99#include <resolv.h>\r
100#include <stdio.h>\r
101#include <stdlib.h>\r
102#include <string.h>\r
103#include <unistd.h>\r
104\r
105#include "res_config.h"\r
106\r
107static void res_setoptions __P((char *, char *));\r
108\r
109#ifdef RESOLVSORT\r
110static const char sort_mask[] = "/&";\r
111#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)\r
112static u_int32_t net_mask __P((struct in_addr));\r
113#endif\r
114\r
115#if !defined(isascii) /* XXX - could be a function */\r
116# define isascii(c) (!(c & 0200))\r
117#endif\r
118\r
119/*\r
120 * Resolver state default settings.\r
121 */\r
122\r
123struct __res_state _res\r
124# if defined(__BIND_RES_TEXT)\r
125 = { RES_TIMEOUT, } /* Motorola, et al. */\r
126# endif\r
127 ;\r
128\r
129\r
130/*\r
131 * Set up default settings. If the configuration file exist, the values\r
132 * there will have precedence. Otherwise, the server address is set to\r
133 * INADDR_ANY and the default domain name comes from the gethostname().\r
134 *\r
135 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1\r
136 * rather than INADDR_ANY ("0.0.0.0") as the default name server address\r
137 * since it was noted that INADDR_ANY actually meant ``the first interface\r
138 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,\r
139 * it had to be "up" in order for you to reach your own name server. It\r
140 * was later decided that since the recommended practice is to always\r
141 * install local static routes through 127.0.0.1 for all your network\r
142 * interfaces, that we could solve this problem without a code change.\r
143 *\r
144 * The configuration file should always be used, since it is the only way\r
145 * to specify a default domain. If you are running a server on your local\r
146 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"\r
147 * in the configuration file.\r
148 *\r
149 * Return 0 if completes successfully, -1 on error\r
150 */\r
151int\r
152res_init()\r
153{\r
154 register FILE *fp;\r
155 register char *cp, **pp;\r
156 register int n;\r
157 char buf[MAXDNAME];\r
158 int nserv = 0; /* number of nameserver records read from file */\r
159 int haveenv = 0;\r
160 int havesearch = 0;\r
161#ifdef RESOLVSORT\r
162 int nsort = 0;\r
163 char *net;\r
164#endif\r
165#ifndef RFC1535\r
166 int dots;\r
167#endif\r
168\r
169 /*\r
170 * These three fields used to be statically initialized. This made\r
171 * it hard to use this code in a shared library. It is necessary,\r
172 * now that we're doing dynamic initialization here, that we preserve\r
173 * the old semantics: if an application modifies one of these three\r
174 * fields of _res before res_init() is called, res_init() will not\r
175 * alter them. Of course, if an application is setting them to\r
176 * _zero_ before calling res_init(), hoping to override what used\r
177 * to be the static default, we can't detect it and unexpected results\r
178 * will follow. Zero for any of these fields would make no sense,\r
179 * so one can safely assume that the applications were already getting\r
180 * unexpected results.\r
181 *\r
182 * _res.options is tricky since some apps were known to diddle the bits\r
183 * before res_init() was first called. We can't replicate that semantic\r
184 * with dynamic initialization (they may have turned bits off that are\r
185 * set in RES_DEFAULT). Our solution is to declare such applications\r
186 * "broken". They could fool us by setting RES_INIT but none do (yet).\r
187 */\r
188 if (!_res.retrans)\r
189 _res.retrans = RES_TIMEOUT;\r
190 if (!_res.retry)\r
191 _res.retry = 4;\r
192 if (!(_res.options & RES_INIT))\r
193 _res.options = RES_DEFAULT;\r
194\r
195 /*\r
196 * This one used to initialize implicitly to zero, so unless the app\r
197 * has set it to something in particular, we can randomize it now.\r
198 */\r
199 if (!_res.id)\r
200 _res.id = (u_short)res_randomid();\r
201\r
202#ifdef USELOOPBACK\r
203 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);\r
204#else\r
205 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;\r
206#endif\r
207 _res.nsaddr.sin_family = AF_INET;\r
208 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);\r
209 _res.nscount = 1;\r
210 _res.ndots = 1;\r
211 _res.pfcode = 0;\r
212\r
213 /* Allow user to override the local domain definition */\r
214#ifdef _ORG_FREEBSD_\r
215 if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL)\r
216#else\r
217 if ((cp = getenv("LOCALDOMAIN")) != NULL)\r
218#endif\r
219 {\r
220 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);\r
221 _res.defdname[sizeof(_res.defdname) - 1] = '\0';\r
222 haveenv++;\r
223\r
224 /*\r
225 * Set search list to be blank-separated strings\r
226 * from rest of env value. Permits users of LOCALDOMAIN\r
227 * to still have a search list, and anyone to set the\r
228 * one that they want to use as an individual (even more\r
229 * important now that the rfc1535 stuff restricts searches)\r
230 */\r
231 cp = _res.defdname;\r
232 pp = _res.dnsrch;\r
233 *pp++ = cp;\r
234 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {\r
235 if (*cp == '\n') /* silly backwards compat */\r
236 break;\r
237 else if (*cp == ' ' || *cp == '\t') {\r
238 *cp = 0;\r
239 n = 1;\r
240 } else if (n) {\r
241 *pp++ = cp;\r
242 n = 0;\r
243 havesearch = 1;\r
244 }\r
245 }\r
246 /* null terminate last domain if there are excess */\r
247 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')\r
248 cp++;\r
249 *cp = '\0';\r
250 *pp++ = 0;\r
251 }\r
252\r
253#define MATCH(line, name) \\r
254 (!strncmp(line, name, sizeof(name) - 1) && \\r
255 (line[sizeof(name) - 1] == ' ' || \\r
256 line[sizeof(name) - 1] == '\t'))\r
257\r
258 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {\r
259 /* read the config file */\r
260 while (fgets(buf, sizeof(buf), fp) != NULL) {\r
261 /* skip comments */\r
262 if (*buf == ';' || *buf == '#')\r
263 continue;\r
264 /* read default domain name */\r
265 if (MATCH(buf, "domain")) {\r
266 if (haveenv) /* skip if have from environ */\r
267 continue;\r
268 cp = buf + sizeof("domain") - 1;\r
269 while (*cp == ' ' || *cp == '\t')\r
270 cp++;\r
271 if ((*cp == '\0') || (*cp == '\n'))\r
272 continue;\r
273 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);\r
274 _res.defdname[sizeof(_res.defdname) - 1] = '\0';\r
275 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)\r
276 *cp = '\0';\r
277 havesearch = 0;\r
278 continue;\r
279 }\r
280 /* set search list */\r
281 if (MATCH(buf, "search")) {\r
282 if (haveenv) /* skip if have from environ */\r
283 continue;\r
284 cp = buf + sizeof("search") - 1;\r
285 while (*cp == ' ' || *cp == '\t')\r
286 cp++;\r
287 if ((*cp == '\0') || (*cp == '\n'))\r
288 continue;\r
289 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);\r
290 _res.defdname[sizeof(_res.defdname) - 1] = '\0';\r
291 if ((cp = strchr(_res.defdname, '\n')) != NULL)\r
292 *cp = '\0';\r
293 /*\r
294 * Set search list to be blank-separated strings\r
295 * on rest of line.\r
296 */\r
297 cp = _res.defdname;\r
298 pp = _res.dnsrch;\r
299 *pp++ = cp;\r
300 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {\r
301 if (*cp == ' ' || *cp == '\t') {\r
302 *cp = 0;\r
303 n = 1;\r
304 } else if (n) {\r
305 *pp++ = cp;\r
306 n = 0;\r
307 }\r
308 }\r
309 /* null terminate last domain if there are excess */\r
310 while (*cp != '\0' && *cp != ' ' && *cp != '\t')\r
311 cp++;\r
312 *cp = '\0';\r
313 *pp++ = 0;\r
314 havesearch = 1;\r
315 continue;\r
316 }\r
317 /* read nameservers to query */\r
318 if (MATCH(buf, "nameserver") && nserv < MAXNS) {\r
319 struct in_addr a;\r
320\r
321 cp = buf + sizeof("nameserver") - 1;\r
322 while (*cp == ' ' || *cp == '\t')\r
323 cp++;\r
324 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {\r
325 _res.nsaddr_list[nserv].sin_addr = a;\r
326 _res.nsaddr_list[nserv].sin_family = AF_INET;\r
327 _res.nsaddr_list[nserv].sin_port =\r
328 htons(NAMESERVER_PORT);\r
329 nserv++;\r
330 }\r
331 continue;\r
332 }\r
333#ifdef RESOLVSORT\r
334 if (MATCH(buf, "sortlist")) {\r
335 struct in_addr a;\r
336\r
337 cp = buf + sizeof("sortlist") - 1;\r
338 while (nsort < MAXRESOLVSORT) {\r
339 while (*cp == ' ' || *cp == '\t')\r
340 cp++;\r
341 if (*cp == '\0' || *cp == '\n' || *cp == ';')\r
342 break;\r
343 net = cp;\r
344 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&\r
345 isascii(*cp) && !isspace(*cp))\r
346 cp++;\r
347 n = *cp;\r
348 *cp = 0;\r
349 if (inet_aton(net, &a)) {\r
350 _res.sort_list[nsort].addr = a;\r
351 if (ISSORTMASK(n)) {\r
352 *cp++ = (char)n;\r
353 net = cp;\r
354 while (*cp && *cp != ';' &&\r
355 isascii(*cp) && !isspace(*cp))\r
356 cp++;\r
357 n = *cp;\r
358 *cp = 0;\r
359 if (inet_aton(net, &a)) {\r
360 _res.sort_list[nsort].mask = a.s_addr;\r
361 } else {\r
362 _res.sort_list[nsort].mask =\r
363 net_mask(_res.sort_list[nsort].addr);\r
364 }\r
365 } else {\r
366 _res.sort_list[nsort].mask =\r
367 net_mask(_res.sort_list[nsort].addr);\r
368 }\r
369 nsort++;\r
370 }\r
371 *cp = (char)n;\r
372 }\r
373 continue;\r
374 }\r
375#endif\r
376 if (MATCH(buf, "options")) {\r
377 res_setoptions(buf + sizeof("options") - 1, "conf");\r
378 continue;\r
379 }\r
380 }\r
381 if (nserv > 1)\r
382 _res.nscount = nserv;\r
383#ifdef RESOLVSORT\r
384 _res.nsort = nsort;\r
385#endif\r
386 (void) fclose(fp);\r
387 }\r
388 if (_res.defdname[0] == 0 &&\r
389 gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&\r
390 (cp = strchr(buf, '.')) != NULL)\r
391 strcpy(_res.defdname, cp + 1);\r
392\r
393 /* find components of local domain that might be searched */\r
394 if (havesearch == 0) {\r
395 pp = _res.dnsrch;\r
396 *pp++ = _res.defdname;\r
397 *pp = NULL;\r
398\r
399#ifndef RFC1535\r
400 dots = 0;\r
401 for (cp = _res.defdname; *cp; cp++)\r
402 dots += (*cp == '.');\r
403\r
404 cp = _res.defdname;\r
405 while (pp < _res.dnsrch + MAXDFLSRCH) {\r
406 if (dots < LOCALDOMAINPARTS)\r
407 break;\r
408 cp = strchr(cp, '.') + 1; /* we know there is one */\r
409 *pp++ = cp;\r
410 dots--;\r
411 }\r
412 *pp = NULL;\r
413#ifdef DEBUG\r
414 if (_res.options & RES_DEBUG) {\r
415 printf(";; res_init()... default dnsrch list:\n");\r
416 for (pp = _res.dnsrch; *pp; pp++)\r
417 printf(";;\t%s\n", *pp);\r
418 printf(";;\t..END..\n");\r
419 }\r
420#endif\r
421#endif /* !RFC1535 */\r
422 }\r
423\r
424#ifdef _ORG_FREEBSD_\r
425 if (issetugid())\r
426#else\r
427 if (1)\r
428#endif\r
429 _res.options |= RES_NOALIASES;\r
430 else if ((cp = getenv("RES_OPTIONS")) != NULL)\r
431 res_setoptions(cp, "env");\r
432 _res.options |= RES_INIT;\r
433 if ( 0 == nserv ) {\r
434 return -1;\r
435 }\r
436 return (0);\r
437}\r
438\r
439static void\r
440res_setoptions(\r
441 char *options,\r
442 char *source\r
443 )\r
444{\r
445 char *cp = options;\r
446 int i;\r
447\r
448#ifdef DEBUG\r
449 if (_res.options & RES_DEBUG)\r
450 printf(";; res_setoptions(\"%s\", \"%s\")...\n",\r
451 options, source);\r
452#endif\r
453 while (*cp) {\r
454 /* skip leading and inner runs of spaces */\r
455 while (*cp == ' ' || *cp == '\t')\r
456 cp++;\r
457 /* search for and process individual options */\r
458 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {\r
459 i = atoi(cp + sizeof("ndots:") - 1);\r
460 if (i <= RES_MAXNDOTS)\r
461 _res.ndots = i;\r
462 else\r
463 _res.ndots = RES_MAXNDOTS;\r
464#ifdef DEBUG\r
465 if (_res.options & RES_DEBUG)\r
466 printf(";;\tndots=%d\n", _res.ndots);\r
467#endif\r
468 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {\r
469#ifdef DEBUG\r
470 if (!(_res.options & RES_DEBUG)) {\r
471 printf(";; res_setoptions(\"%s\", \"%s\")..\n",\r
472 options, source);\r
473 _res.options |= RES_DEBUG;\r
474 }\r
475 printf(";;\tdebug\n");\r
476#endif\r
477 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {\r
478 _res.options |= RES_USE_INET6;\r
479 } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1)) {\r
480 _res.options |= RES_NOTLDQUERY;\r
481 } else {\r
482 /* XXX - print a warning here? */\r
483 }\r
484 /* skip to next run of spaces */\r
485 while (*cp && *cp != ' ' && *cp != '\t')\r
486 cp++;\r
487 }\r
488}\r
489\r
490#ifdef RESOLVSORT\r
491/* XXX - should really support CIDR which means explicit masks always. */\r
492static u_int32_t\r
493net_mask(\r
494 struct in_addr in\r
495 )\r
496{\r
497 register u_int32_t i = ntohl(in.s_addr);\r
498\r
499 if (IN_CLASSA(i))\r
500 return (htonl(IN_CLASSA_NET));\r
501 else if (IN_CLASSB(i))\r
502 return (htonl(IN_CLASSB_NET));\r
503 return (htonl(IN_CLASSC_NET));\r
504}\r
505#endif\r
506\r
507u_int\r
508res_randomid()\r
509{\r
510 struct timeval now;\r
511\r
512 gettimeofday(&now, NULL);\r
513 return (0xffff & (now.tv_sec ^ now.tv_usec /* ^ getpid() */));\r
514}\r