res_init.c revision 174226
1156952Sume/* 2156952Sume * Copyright (c) 1985, 1989, 1993 3156952Sume * The Regents of the University of California. All rights reserved. 4156952Sume * 5156952Sume * Redistribution and use in source and binary forms, with or without 6156952Sume * modification, are permitted provided that the following conditions 7156952Sume * are met: 8156952Sume * 1. Redistributions of source code must retain the above copyright 9156952Sume * notice, this list of conditions and the following disclaimer. 10156952Sume * 2. Redistributions in binary form must reproduce the above copyright 11156952Sume * notice, this list of conditions and the following disclaimer in the 12156952Sume * documentation and/or other materials provided with the distribution. 13156952Sume * 4. Neither the name of the University nor the names of its contributors 14156952Sume * may be used to endorse or promote products derived from this software 15156952Sume * without specific prior written permission. 16156952Sume * 17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27156952Sume * SUCH DAMAGE. 28156952Sume */ 29156952Sume 30156952Sume/* 31156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 32156952Sume * 33156952Sume * Permission to use, copy, modify, and distribute this software for any 34156952Sume * purpose with or without fee is hereby granted, provided that the above 35156952Sume * copyright notice and this permission notice appear in all copies, and that 36156952Sume * the name of Digital Equipment Corporation not be used in advertising or 37156952Sume * publicity pertaining to distribution of the document or software without 38156952Sume * specific, written prior permission. 39156952Sume * 40156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 41156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 42156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 43156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 44156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 45156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 46156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 47156952Sume * SOFTWARE. 48156952Sume */ 49156952Sume 50156952Sume/* 51156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 52156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 53156952Sume * 54156952Sume * Permission to use, copy, modify, and distribute this software for any 55156952Sume * purpose with or without fee is hereby granted, provided that the above 56156952Sume * copyright notice and this permission notice appear in all copies. 57156952Sume * 58156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 59156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 60156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 61156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 62156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 63156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 64156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 65156952Sume */ 66156952Sume 67156952Sume#if defined(LIBC_SCCS) && !defined(lint) 68156952Sumestatic const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; 69174226Sumestatic const char rcsid[] = "$Id: res_init.c,v 1.16.18.7 2007/07/09 01:52:58 marka Exp $"; 70156952Sume#endif /* LIBC_SCCS and not lint */ 71156956Sume#include <sys/cdefs.h> 72156956Sume__FBSDID("$FreeBSD: head/lib/libc/resolv/res_init.c 174226 2007-12-03 15:13:44Z ume $"); 73156952Sume 74156952Sume#include "port_before.h" 75156952Sume 76160965Sume#include "namespace.h" 77160965Sume 78156952Sume#include <sys/types.h> 79156952Sume#include <sys/param.h> 80156952Sume#include <sys/socket.h> 81156952Sume#include <sys/time.h> 82156952Sume 83156952Sume#include <netinet/in.h> 84156952Sume#include <arpa/inet.h> 85156952Sume#include <arpa/nameser.h> 86156952Sume 87156952Sume#include <ctype.h> 88156952Sume#include <stdio.h> 89156952Sume#include <stdlib.h> 90156952Sume#include <string.h> 91156952Sume#include <unistd.h> 92156952Sume#include <netdb.h> 93156952Sume 94160965Sume#include "un-namespace.h" 95160965Sume 96156952Sume#include "port_after.h" 97156952Sume 98156952Sume/* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */ 99156952Sume#include <resolv.h> 100156952Sume 101156952Sume#include "res_private.h" 102156952Sume 103170244Sume/*% Options. Should all be left alone. */ 104156952Sume#define RESOLVSORT 105156952Sume#define DEBUG 106156952Sume 107156952Sume#ifdef SOLARIS2 108156952Sume#include <sys/systeminfo.h> 109156952Sume#endif 110156952Sume 111156956Sumestatic void res_setoptions(res_state, const char *, const char *); 112156952Sume 113156952Sume#ifdef RESOLVSORT 114156952Sumestatic const char sort_mask[] = "/&"; 115156952Sume#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) 116156956Sumestatic u_int32_t net_mask(struct in_addr); 117156952Sume#endif 118156952Sume 119170244Sume#if !defined(isascii) /*%< XXX - could be a function */ 120156952Sume# define isascii(c) (!(c & 0200)) 121156952Sume#endif 122156952Sume 123156952Sume/* 124156952Sume * Resolver state default settings. 125156952Sume */ 126156952Sume 127170244Sume/*% 128156952Sume * Set up default settings. If the configuration file exist, the values 129156952Sume * there will have precedence. Otherwise, the server address is set to 130156952Sume * INADDR_ANY and the default domain name comes from the gethostname(). 131156952Sume * 132156952Sume * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 133156952Sume * rather than INADDR_ANY ("0.0.0.0") as the default name server address 134156952Sume * since it was noted that INADDR_ANY actually meant ``the first interface 135156952Sume * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, 136156952Sume * it had to be "up" in order for you to reach your own name server. It 137156952Sume * was later decided that since the recommended practice is to always 138156952Sume * install local static routes through 127.0.0.1 for all your network 139156952Sume * interfaces, that we could solve this problem without a code change. 140156952Sume * 141156952Sume * The configuration file should always be used, since it is the only way 142156952Sume * to specify a default domain. If you are running a server on your local 143156952Sume * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" 144156952Sume * in the configuration file. 145156952Sume * 146156952Sume * Return 0 if completes successfully, -1 on error 147156952Sume */ 148156952Sumeint 149156952Sumeres_ninit(res_state statp) { 150156952Sume extern int __res_vinit(res_state, int); 151156952Sume 152156952Sume return (__res_vinit(statp, 0)); 153156952Sume} 154156952Sume 155170244Sume/*% This function has to be reachable by res_data.c but not publically. */ 156156952Sumeint 157156952Sume__res_vinit(res_state statp, int preinit) { 158156956Sume FILE *fp; 159156956Sume char *cp, **pp; 160156956Sume int n; 161156952Sume char buf[BUFSIZ]; 162170244Sume int nserv = 0; /*%< number of nameserver records read from file */ 163156952Sume int haveenv = 0; 164156952Sume int havesearch = 0; 165156952Sume#ifdef RESOLVSORT 166156952Sume int nsort = 0; 167156952Sume char *net; 168156952Sume#endif 169156952Sume int dots; 170156952Sume union res_sockaddr_union u[2]; 171174226Sume int maxns = MAXNS; 172156952Sume 173174226Sume RES_SET_H_ERRNO(statp, 0); 174156952Sume if (statp->_u._ext.ext != NULL) 175156952Sume res_ndestroy(statp); 176156952Sume 177156952Sume if (!preinit) { 178156952Sume statp->retrans = RES_TIMEOUT; 179156952Sume statp->retry = RES_DFLRETRY; 180156952Sume statp->options = RES_DEFAULT; 181156952Sume statp->id = res_randomid(); 182156952Sume } 183156952Sume 184156952Sume memset(u, 0, sizeof(u)); 185156952Sume#ifdef USELOOPBACK 186156952Sume u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 187156952Sume#else 188156952Sume u[nserv].sin.sin_addr.s_addr = INADDR_ANY; 189156952Sume#endif 190156952Sume u[nserv].sin.sin_family = AF_INET; 191156952Sume u[nserv].sin.sin_port = htons(NAMESERVER_PORT); 192156952Sume#ifdef HAVE_SA_LEN 193156952Sume u[nserv].sin.sin_len = sizeof(struct sockaddr_in); 194156952Sume#endif 195156952Sume nserv++; 196156952Sume#ifdef HAS_INET6_STRUCTS 197156952Sume#ifdef USELOOPBACK 198156952Sume u[nserv].sin6.sin6_addr = in6addr_loopback; 199156952Sume#else 200156952Sume u[nserv].sin6.sin6_addr = in6addr_any; 201156952Sume#endif 202156952Sume u[nserv].sin6.sin6_family = AF_INET6; 203156952Sume u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); 204156952Sume#ifdef HAVE_SA_LEN 205156952Sume u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); 206156952Sume#endif 207156952Sume nserv++; 208156952Sume#endif 209156952Sume statp->nscount = 0; 210156952Sume statp->ndots = 1; 211156952Sume statp->pfcode = 0; 212156952Sume statp->_vcsock = -1; 213156952Sume statp->_flags = 0; 214156952Sume statp->qhook = NULL; 215156952Sume statp->rhook = NULL; 216156952Sume statp->_u._ext.nscount = 0; 217156952Sume statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); 218156952Sume if (statp->_u._ext.ext != NULL) { 219156952Sume memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); 220156952Sume statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; 221156952Sume strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); 222156952Sume strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); 223174226Sume } else { 224174226Sume /* 225174226Sume * Historically res_init() rarely, if at all, failed. 226174226Sume * Examples and applications exist which do not check 227174226Sume * our return code. Furthermore several applications 228174226Sume * simply call us to get the systems domainname. So 229174226Sume * rather then immediately fail here we store the 230174226Sume * failure, which is returned later, in h_errno. And 231174226Sume * prevent the collection of 'nameserver' information 232174226Sume * by setting maxns to 0. Thus applications that fail 233174226Sume * to check our return code wont be able to make 234174226Sume * queries anyhow. 235174226Sume */ 236174226Sume RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 237174226Sume maxns = 0; 238174226Sume } 239156952Sume#ifdef RESOLVSORT 240156952Sume statp->nsort = 0; 241156952Sume#endif 242156952Sume res_setservers(statp, u, nserv); 243156952Sume 244156952Sume#ifdef SOLARIS2 245156952Sume /* 246156952Sume * The old libresolv derived the defaultdomain from NIS/NIS+. 247156952Sume * We want to keep this behaviour 248156952Sume */ 249156952Sume { 250156952Sume char buf[sizeof(statp->defdname)], *cp; 251156952Sume int ret; 252156952Sume 253156952Sume if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 && 254156952Sume (unsigned int)ret <= sizeof(buf)) { 255156952Sume if (buf[0] == '+') 256156952Sume buf[0] = '.'; 257156952Sume cp = strchr(buf, '.'); 258165258Sume cp = (cp == NULL) ? buf : (cp + 1); 259174226Sume strncpy(statp->defdname, cp, 260174226Sume sizeof(statp->defdname) - 1); 261174226Sume statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 262156952Sume } 263156952Sume } 264156952Sume#endif /* SOLARIS2 */ 265156952Sume 266156952Sume /* Allow user to override the local domain definition */ 267156956Sume if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { 268156952Sume (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 269156952Sume statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 270156952Sume haveenv++; 271156952Sume 272156952Sume /* 273156952Sume * Set search list to be blank-separated strings 274156952Sume * from rest of env value. Permits users of LOCALDOMAIN 275156952Sume * to still have a search list, and anyone to set the 276156952Sume * one that they want to use as an individual (even more 277156952Sume * important now that the rfc1535 stuff restricts searches) 278156952Sume */ 279156952Sume cp = statp->defdname; 280156952Sume pp = statp->dnsrch; 281156952Sume *pp++ = cp; 282156952Sume for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 283170244Sume if (*cp == '\n') /*%< silly backwards compat */ 284156952Sume break; 285156952Sume else if (*cp == ' ' || *cp == '\t') { 286156952Sume *cp = 0; 287156952Sume n = 1; 288156952Sume } else if (n) { 289156952Sume *pp++ = cp; 290156952Sume n = 0; 291156952Sume havesearch = 1; 292156952Sume } 293156952Sume } 294156952Sume /* null terminate last domain if there are excess */ 295156952Sume while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') 296156952Sume cp++; 297156952Sume *cp = '\0'; 298156952Sume *pp++ = 0; 299156952Sume } 300156952Sume 301156952Sume#define MATCH(line, name) \ 302156952Sume (!strncmp(line, name, sizeof(name) - 1) && \ 303156952Sume (line[sizeof(name) - 1] == ' ' || \ 304156952Sume line[sizeof(name) - 1] == '\t')) 305156952Sume 306156952Sume nserv = 0; 307156952Sume if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 308156952Sume /* read the config file */ 309156952Sume while (fgets(buf, sizeof(buf), fp) != NULL) { 310156952Sume /* skip comments */ 311156952Sume if (*buf == ';' || *buf == '#') 312156952Sume continue; 313156952Sume /* read default domain name */ 314156952Sume if (MATCH(buf, "domain")) { 315170244Sume if (haveenv) /*%< skip if have from environ */ 316156952Sume continue; 317156952Sume cp = buf + sizeof("domain") - 1; 318156952Sume while (*cp == ' ' || *cp == '\t') 319156952Sume cp++; 320156952Sume if ((*cp == '\0') || (*cp == '\n')) 321156952Sume continue; 322156952Sume strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 323156952Sume statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 324156952Sume if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) 325156952Sume *cp = '\0'; 326156952Sume havesearch = 0; 327156952Sume continue; 328156952Sume } 329156952Sume /* set search list */ 330156952Sume if (MATCH(buf, "search")) { 331170244Sume if (haveenv) /*%< skip if have from environ */ 332156952Sume continue; 333156952Sume cp = buf + sizeof("search") - 1; 334156952Sume while (*cp == ' ' || *cp == '\t') 335156952Sume cp++; 336156952Sume if ((*cp == '\0') || (*cp == '\n')) 337156952Sume continue; 338156952Sume strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); 339156952Sume statp->defdname[sizeof(statp->defdname) - 1] = '\0'; 340156952Sume if ((cp = strchr(statp->defdname, '\n')) != NULL) 341156952Sume *cp = '\0'; 342156952Sume /* 343156952Sume * Set search list to be blank-separated strings 344156952Sume * on rest of line. 345156952Sume */ 346156952Sume cp = statp->defdname; 347156952Sume pp = statp->dnsrch; 348156952Sume *pp++ = cp; 349156952Sume for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { 350156952Sume if (*cp == ' ' || *cp == '\t') { 351156952Sume *cp = 0; 352156952Sume n = 1; 353156952Sume } else if (n) { 354156952Sume *pp++ = cp; 355156952Sume n = 0; 356156952Sume } 357156952Sume } 358156952Sume /* null terminate last domain if there are excess */ 359156952Sume while (*cp != '\0' && *cp != ' ' && *cp != '\t') 360156952Sume cp++; 361156952Sume *cp = '\0'; 362156952Sume *pp++ = 0; 363156952Sume havesearch = 1; 364156952Sume continue; 365156952Sume } 366156952Sume /* read nameservers to query */ 367174226Sume if (MATCH(buf, "nameserver") && nserv < maxns) { 368156952Sume struct addrinfo hints, *ai; 369156952Sume char sbuf[NI_MAXSERV]; 370156952Sume const size_t minsiz = 371156952Sume sizeof(statp->_u._ext.ext->nsaddrs[0]); 372156952Sume 373156952Sume cp = buf + sizeof("nameserver") - 1; 374156952Sume while (*cp == ' ' || *cp == '\t') 375156952Sume cp++; 376156952Sume cp[strcspn(cp, ";# \t\n")] = '\0'; 377156952Sume if ((*cp != '\0') && (*cp != '\n')) { 378156952Sume memset(&hints, 0, sizeof(hints)); 379156952Sume hints.ai_family = PF_UNSPEC; 380156952Sume hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 381156952Sume hints.ai_flags = AI_NUMERICHOST; 382156952Sume sprintf(sbuf, "%u", NAMESERVER_PORT); 383156952Sume if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && 384156952Sume ai->ai_addrlen <= minsiz) { 385156952Sume if (statp->_u._ext.ext != NULL) { 386156952Sume memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 387156952Sume ai->ai_addr, ai->ai_addrlen); 388156952Sume } 389156952Sume if (ai->ai_addrlen <= 390156952Sume sizeof(statp->nsaddr_list[nserv])) { 391156952Sume memcpy(&statp->nsaddr_list[nserv], 392156952Sume ai->ai_addr, ai->ai_addrlen); 393156952Sume } else 394156952Sume statp->nsaddr_list[nserv].sin_family = 0; 395156952Sume freeaddrinfo(ai); 396156952Sume nserv++; 397156952Sume } 398156952Sume } 399156952Sume continue; 400156952Sume } 401156952Sume#ifdef RESOLVSORT 402156952Sume if (MATCH(buf, "sortlist")) { 403156952Sume struct in_addr a; 404156956Sume struct in6_addr a6; 405156956Sume int m, i; 406156956Sume u_char *u; 407156956Sume struct __res_state_ext *ext = statp->_u._ext.ext; 408156952Sume 409156952Sume cp = buf + sizeof("sortlist") - 1; 410156952Sume while (nsort < MAXRESOLVSORT) { 411156952Sume while (*cp == ' ' || *cp == '\t') 412156952Sume cp++; 413156952Sume if (*cp == '\0' || *cp == '\n' || *cp == ';') 414156952Sume break; 415156952Sume net = cp; 416156952Sume while (*cp && !ISSORTMASK(*cp) && *cp != ';' && 417156952Sume isascii(*cp) && !isspace((unsigned char)*cp)) 418156952Sume cp++; 419156952Sume n = *cp; 420156952Sume *cp = 0; 421156952Sume if (inet_aton(net, &a)) { 422156952Sume statp->sort_list[nsort].addr = a; 423156952Sume if (ISSORTMASK(n)) { 424156952Sume *cp++ = n; 425156952Sume net = cp; 426156952Sume while (*cp && *cp != ';' && 427156952Sume isascii(*cp) && 428156952Sume !isspace((unsigned char)*cp)) 429156952Sume cp++; 430156952Sume n = *cp; 431156952Sume *cp = 0; 432156952Sume if (inet_aton(net, &a)) { 433156952Sume statp->sort_list[nsort].mask = a.s_addr; 434156952Sume } else { 435156952Sume statp->sort_list[nsort].mask = 436156952Sume net_mask(statp->sort_list[nsort].addr); 437156952Sume } 438156952Sume } else { 439156952Sume statp->sort_list[nsort].mask = 440156952Sume net_mask(statp->sort_list[nsort].addr); 441156952Sume } 442156956Sume ext->sort_list[nsort].af = AF_INET; 443156956Sume ext->sort_list[nsort].addr.ina = 444156956Sume statp->sort_list[nsort].addr; 445156956Sume ext->sort_list[nsort].mask.ina.s_addr = 446156956Sume statp->sort_list[nsort].mask; 447156952Sume nsort++; 448156952Sume } 449156956Sume else if (inet_pton(AF_INET6, net, &a6) == 1) { 450156956Sume 451156956Sume ext->sort_list[nsort].af = AF_INET6; 452156956Sume ext->sort_list[nsort].addr.in6a = a6; 453156956Sume u = (u_char *)&ext->sort_list[nsort].mask.in6a; 454156956Sume *cp++ = n; 455156956Sume net = cp; 456156956Sume while (*cp && *cp != ';' && 457156956Sume isascii(*cp) && !isspace(*cp)) 458156956Sume cp++; 459156956Sume m = n; 460156956Sume n = *cp; 461156956Sume *cp = 0; 462156956Sume switch (m) { 463156956Sume case '/': 464156956Sume m = atoi(net); 465156956Sume break; 466156956Sume case '&': 467156956Sume if (inet_pton(AF_INET6, net, u) == 1) { 468156956Sume m = -1; 469156956Sume break; 470156956Sume } 471156956Sume /*FALLTHROUGH*/ 472156956Sume default: 473156956Sume m = sizeof(struct in6_addr) * CHAR_BIT; 474156956Sume break; 475156956Sume } 476156956Sume if (m >= 0) { 477156956Sume for (i = 0; i < sizeof(struct in6_addr); i++) { 478156956Sume if (m <= 0) { 479156956Sume *u = 0; 480156956Sume } else { 481156956Sume m -= CHAR_BIT; 482156956Sume *u = (u_char)~0; 483156956Sume if (m < 0) 484156956Sume *u <<= -m; 485156956Sume } 486156956Sume u++; 487156956Sume } 488156956Sume } 489156956Sume statp->sort_list[nsort].addr.s_addr = 490156956Sume (u_int32_t)0xffffffff; 491156956Sume statp->sort_list[nsort].mask = 492156956Sume (u_int32_t)0xffffffff; 493156956Sume nsort++; 494156956Sume } 495156952Sume *cp = n; 496156952Sume } 497156952Sume continue; 498156952Sume } 499156952Sume#endif 500156952Sume if (MATCH(buf, "options")) { 501156952Sume res_setoptions(statp, buf + sizeof("options") - 1, "conf"); 502156952Sume continue; 503156952Sume } 504156952Sume } 505156952Sume if (nserv > 0) 506156952Sume statp->nscount = nserv; 507156952Sume#ifdef RESOLVSORT 508156952Sume statp->nsort = nsort; 509156952Sume#endif 510156952Sume (void) fclose(fp); 511156952Sume } 512156952Sume/* 513156952Sume * Last chance to get a nameserver. This should not normally 514156952Sume * be necessary 515156952Sume */ 516156952Sume#ifdef NO_RESOLV_CONF 517156952Sume if(nserv == 0) 518156952Sume nserv = get_nameservers(statp); 519156952Sume#endif 520156952Sume 521156952Sume if (statp->defdname[0] == 0 && 522156952Sume gethostname(buf, sizeof(statp->defdname) - 1) == 0 && 523156952Sume (cp = strchr(buf, '.')) != NULL) 524156952Sume strcpy(statp->defdname, cp + 1); 525156952Sume 526156952Sume /* find components of local domain that might be searched */ 527156952Sume if (havesearch == 0) { 528156952Sume pp = statp->dnsrch; 529156952Sume *pp++ = statp->defdname; 530156952Sume *pp = NULL; 531156952Sume 532156952Sume dots = 0; 533156952Sume for (cp = statp->defdname; *cp; cp++) 534156952Sume dots += (*cp == '.'); 535156952Sume 536156952Sume cp = statp->defdname; 537156952Sume while (pp < statp->dnsrch + MAXDFLSRCH) { 538156952Sume if (dots < LOCALDOMAINPARTS) 539156952Sume break; 540170244Sume cp = strchr(cp, '.') + 1; /*%< we know there is one */ 541156952Sume *pp++ = cp; 542156952Sume dots--; 543156952Sume } 544156952Sume *pp = NULL; 545156952Sume#ifdef DEBUG 546156952Sume if (statp->options & RES_DEBUG) { 547156952Sume printf(";; res_init()... default dnsrch list:\n"); 548156952Sume for (pp = statp->dnsrch; *pp; pp++) 549156952Sume printf(";;\t%s\n", *pp); 550156952Sume printf(";;\t..END..\n"); 551156952Sume } 552156952Sume#endif 553156952Sume } 554156952Sume 555156956Sume if (issetugid()) 556156956Sume statp->options |= RES_NOALIASES; 557156956Sume else if ((cp = getenv("RES_OPTIONS")) != NULL) 558156952Sume res_setoptions(statp, cp, "env"); 559156952Sume statp->options |= RES_INIT; 560174226Sume return (statp->res_h_errno); 561156952Sume} 562156952Sume 563156952Sumestatic void 564156952Sumeres_setoptions(res_state statp, const char *options, const char *source) 565156952Sume{ 566156952Sume const char *cp = options; 567156952Sume int i; 568156956Sume#ifndef _LIBC 569156952Sume struct __res_state_ext *ext = statp->_u._ext.ext; 570156956Sume#endif 571156952Sume 572156952Sume#ifdef DEBUG 573156952Sume if (statp->options & RES_DEBUG) 574156952Sume printf(";; res_setoptions(\"%s\", \"%s\")...\n", 575156952Sume options, source); 576156952Sume#endif 577156952Sume while (*cp) { 578156952Sume /* skip leading and inner runs of spaces */ 579156952Sume while (*cp == ' ' || *cp == '\t') 580156952Sume cp++; 581156952Sume /* search for and process individual options */ 582156952Sume if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { 583156952Sume i = atoi(cp + sizeof("ndots:") - 1); 584156952Sume if (i <= RES_MAXNDOTS) 585156952Sume statp->ndots = i; 586156952Sume else 587156952Sume statp->ndots = RES_MAXNDOTS; 588156952Sume#ifdef DEBUG 589156952Sume if (statp->options & RES_DEBUG) 590156952Sume printf(";;\tndots=%d\n", statp->ndots); 591156952Sume#endif 592156952Sume } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { 593156952Sume i = atoi(cp + sizeof("timeout:") - 1); 594156952Sume if (i <= RES_MAXRETRANS) 595156952Sume statp->retrans = i; 596156952Sume else 597156952Sume statp->retrans = RES_MAXRETRANS; 598156952Sume#ifdef DEBUG 599156952Sume if (statp->options & RES_DEBUG) 600156952Sume printf(";;\ttimeout=%d\n", statp->retrans); 601156952Sume#endif 602156952Sume#ifdef SOLARIS2 603156952Sume } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) { 604156952Sume /* 605156952Sume * For backward compatibility, 'retrans' is 606156952Sume * supported as an alias for 'timeout', though 607156952Sume * without an imposed maximum. 608156952Sume */ 609156952Sume statp->retrans = atoi(cp + sizeof("retrans:") - 1); 610156952Sume } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){ 611156952Sume /* 612156952Sume * For backward compatibility, 'retry' is 613156952Sume * supported as an alias for 'attempts', though 614156952Sume * without an imposed maximum. 615156952Sume */ 616156952Sume statp->retry = atoi(cp + sizeof("retry:") - 1); 617156952Sume#endif /* SOLARIS2 */ 618156952Sume } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ 619156952Sume i = atoi(cp + sizeof("attempts:") - 1); 620156952Sume if (i <= RES_MAXRETRY) 621156952Sume statp->retry = i; 622156952Sume else 623156952Sume statp->retry = RES_MAXRETRY; 624156952Sume#ifdef DEBUG 625156952Sume if (statp->options & RES_DEBUG) 626156952Sume printf(";;\tattempts=%d\n", statp->retry); 627156952Sume#endif 628156952Sume } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { 629156952Sume#ifdef DEBUG 630156952Sume if (!(statp->options & RES_DEBUG)) { 631156952Sume printf(";; res_setoptions(\"%s\", \"%s\")..\n", 632156952Sume options, source); 633156952Sume statp->options |= RES_DEBUG; 634156952Sume } 635156952Sume printf(";;\tdebug\n"); 636156952Sume#endif 637156952Sume } else if (!strncmp(cp, "no_tld_query", 638156952Sume sizeof("no_tld_query") - 1) || 639156952Sume !strncmp(cp, "no-tld-query", 640156952Sume sizeof("no-tld-query") - 1)) { 641156952Sume statp->options |= RES_NOTLDQUERY; 642156952Sume } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { 643156952Sume statp->options |= RES_USE_INET6; 644156956Sume } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) { 645156956Sume statp->options |= RES_INSECURE1; 646156956Sume } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) { 647156956Sume statp->options |= RES_INSECURE2; 648156952Sume } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { 649156952Sume statp->options |= RES_ROTATE; 650156952Sume } else if (!strncmp(cp, "no-check-names", 651156952Sume sizeof("no-check-names") - 1)) { 652156952Sume statp->options |= RES_NOCHECKNAME; 653156952Sume } 654156952Sume#ifdef RES_USE_EDNS0 655156952Sume else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { 656156952Sume statp->options |= RES_USE_EDNS0; 657156952Sume } 658156952Sume#endif 659156956Sume#ifndef _LIBC 660156952Sume else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { 661156952Sume statp->options |= RES_USE_DNAME; 662156952Sume } 663156952Sume else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { 664156952Sume if (ext == NULL) 665156952Sume goto skip; 666156952Sume cp += sizeof("nibble:") - 1; 667156952Sume i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); 668156952Sume strncpy(ext->nsuffix, cp, i); 669156952Sume ext->nsuffix[i] = '\0'; 670156952Sume } 671156952Sume else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { 672156952Sume if (ext == NULL) 673156952Sume goto skip; 674156952Sume cp += sizeof("nibble2:") - 1; 675156952Sume i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); 676156952Sume strncpy(ext->nsuffix2, cp, i); 677156952Sume ext->nsuffix2[i] = '\0'; 678156952Sume } 679156952Sume else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { 680156952Sume cp += sizeof("v6revmode:") - 1; 681156952Sume /* "nibble" and "bitstring" used to be valid */ 682156952Sume if (!strncmp(cp, "single", sizeof("single") - 1)) { 683156952Sume statp->options |= RES_NO_NIBBLE2; 684156952Sume } else if (!strncmp(cp, "both", sizeof("both") - 1)) { 685156952Sume statp->options &= 686156952Sume ~RES_NO_NIBBLE2; 687156952Sume } 688156952Sume } 689156956Sume#endif 690156952Sume else { 691156952Sume /* XXX - print a warning here? */ 692156952Sume } 693156956Sume#ifndef _LIBC 694156952Sume skip: 695156956Sume#endif 696156952Sume /* skip to next run of spaces */ 697156952Sume while (*cp && *cp != ' ' && *cp != '\t') 698156952Sume cp++; 699156952Sume } 700156952Sume} 701156952Sume 702156952Sume#ifdef RESOLVSORT 703156952Sume/* XXX - should really support CIDR which means explicit masks always. */ 704156952Sumestatic u_int32_t 705170244Sumenet_mask(in) /*!< XXX - should really use system's version of this */ 706156952Sume struct in_addr in; 707156952Sume{ 708156956Sume u_int32_t i = ntohl(in.s_addr); 709156952Sume 710156952Sume if (IN_CLASSA(i)) 711156952Sume return (htonl(IN_CLASSA_NET)); 712156952Sume else if (IN_CLASSB(i)) 713156952Sume return (htonl(IN_CLASSB_NET)); 714156952Sume return (htonl(IN_CLASSC_NET)); 715156952Sume} 716156952Sume#endif 717156952Sume 718156952Sumeu_int 719156952Sumeres_randomid(void) { 720156952Sume struct timeval now; 721156952Sume 722156952Sume gettimeofday(&now, NULL); 723156952Sume return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); 724156952Sume} 725156952Sume 726170244Sume/*% 727156952Sume * This routine is for closing the socket if a virtual circuit is used and 728156952Sume * the program wants to close it. This provides support for endhostent() 729156952Sume * which expects to close the socket. 730156952Sume * 731156952Sume * This routine is not expected to be user visible. 732156952Sume */ 733156952Sumevoid 734156952Sumeres_nclose(res_state statp) { 735156952Sume int ns; 736156952Sume 737156952Sume if (statp->_vcsock >= 0) { 738160965Sume (void) _close(statp->_vcsock); 739156952Sume statp->_vcsock = -1; 740156952Sume statp->_flags &= ~(RES_F_VC | RES_F_CONN); 741156952Sume } 742156952Sume for (ns = 0; ns < statp->_u._ext.nscount; ns++) { 743156952Sume if (statp->_u._ext.nssocks[ns] != -1) { 744160965Sume (void) _close(statp->_u._ext.nssocks[ns]); 745156952Sume statp->_u._ext.nssocks[ns] = -1; 746156952Sume } 747156952Sume } 748156952Sume} 749156952Sume 750156952Sumevoid 751156952Sumeres_ndestroy(res_state statp) { 752156952Sume res_nclose(statp); 753156952Sume if (statp->_u._ext.ext != NULL) 754156952Sume free(statp->_u._ext.ext); 755156952Sume statp->options &= ~RES_INIT; 756156952Sume statp->_u._ext.ext = NULL; 757156952Sume} 758156952Sume 759156956Sume#ifndef _LIBC 760156952Sumeconst char * 761156952Sumeres_get_nibblesuffix(res_state statp) { 762156952Sume if (statp->_u._ext.ext) 763156952Sume return (statp->_u._ext.ext->nsuffix); 764156952Sume return ("ip6.arpa"); 765156952Sume} 766156952Sume 767156952Sumeconst char * 768156952Sumeres_get_nibblesuffix2(res_state statp) { 769156952Sume if (statp->_u._ext.ext) 770156952Sume return (statp->_u._ext.ext->nsuffix2); 771156952Sume return ("ip6.int"); 772156952Sume} 773156956Sume#endif 774156952Sume 775156952Sumevoid 776156952Sumeres_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { 777156952Sume int i, nserv; 778156952Sume size_t size; 779156952Sume 780156952Sume /* close open servers */ 781156952Sume res_nclose(statp); 782156952Sume 783156952Sume /* cause rtt times to be forgotten */ 784156952Sume statp->_u._ext.nscount = 0; 785156952Sume 786156952Sume nserv = 0; 787156952Sume for (i = 0; i < cnt && nserv < MAXNS; i++) { 788156952Sume switch (set->sin.sin_family) { 789156952Sume case AF_INET: 790156952Sume size = sizeof(set->sin); 791156952Sume if (statp->_u._ext.ext) 792156952Sume memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 793156952Sume &set->sin, size); 794156952Sume if (size <= sizeof(statp->nsaddr_list[nserv])) 795156952Sume memcpy(&statp->nsaddr_list[nserv], 796156952Sume &set->sin, size); 797156952Sume else 798156952Sume statp->nsaddr_list[nserv].sin_family = 0; 799156952Sume nserv++; 800156952Sume break; 801156952Sume 802156952Sume#ifdef HAS_INET6_STRUCTS 803156952Sume case AF_INET6: 804156952Sume size = sizeof(set->sin6); 805156952Sume if (statp->_u._ext.ext) 806156952Sume memcpy(&statp->_u._ext.ext->nsaddrs[nserv], 807156952Sume &set->sin6, size); 808156952Sume if (size <= sizeof(statp->nsaddr_list[nserv])) 809156952Sume memcpy(&statp->nsaddr_list[nserv], 810156952Sume &set->sin6, size); 811156952Sume else 812156952Sume statp->nsaddr_list[nserv].sin_family = 0; 813156952Sume nserv++; 814156952Sume break; 815156952Sume#endif 816156952Sume 817156952Sume default: 818156952Sume break; 819156952Sume } 820156952Sume set++; 821156952Sume } 822156952Sume statp->nscount = nserv; 823156952Sume 824156952Sume} 825156952Sume 826156952Sumeint 827156952Sumeres_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { 828156952Sume int i; 829156952Sume size_t size; 830156952Sume u_int16_t family; 831156952Sume 832156952Sume for (i = 0; i < statp->nscount && i < cnt; i++) { 833156952Sume if (statp->_u._ext.ext) 834156952Sume family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; 835156952Sume else 836156952Sume family = statp->nsaddr_list[i].sin_family; 837156952Sume 838156952Sume switch (family) { 839156952Sume case AF_INET: 840156952Sume size = sizeof(set->sin); 841156952Sume if (statp->_u._ext.ext) 842156952Sume memcpy(&set->sin, 843156952Sume &statp->_u._ext.ext->nsaddrs[i], 844156952Sume size); 845156952Sume else 846156952Sume memcpy(&set->sin, &statp->nsaddr_list[i], 847156952Sume size); 848156952Sume break; 849156952Sume 850156952Sume#ifdef HAS_INET6_STRUCTS 851156952Sume case AF_INET6: 852156952Sume size = sizeof(set->sin6); 853156952Sume if (statp->_u._ext.ext) 854156952Sume memcpy(&set->sin6, 855156952Sume &statp->_u._ext.ext->nsaddrs[i], 856156952Sume size); 857156952Sume else 858156952Sume memcpy(&set->sin6, &statp->nsaddr_list[i], 859156952Sume size); 860156952Sume break; 861156952Sume#endif 862156952Sume 863156952Sume default: 864156952Sume set->sin.sin_family = 0; 865156952Sume break; 866156952Sume } 867156952Sume set++; 868156952Sume } 869156952Sume return (statp->nscount); 870156952Sume} 871170244Sume 872170244Sume/*! \file */ 873