Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1985, 1989, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software |
| 16 | * must display the following acknowledgement: |
| 17 | * This product includes software developed by the University of |
| 18 | * California, Berkeley and its contributors. |
| 19 | * 4. Neither the name of the University nor the names of its contributors |
| 20 | * may be used to endorse or promote products derived from this software |
| 21 | * without specific prior written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | * SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
| 38 | * |
| 39 | * Permission to use, copy, modify, and distribute this software for any |
| 40 | * purpose with or without fee is hereby granted, provided that the above |
| 41 | * copyright notice and this permission notice appear in all copies, and that |
| 42 | * the name of Digital Equipment Corporation not be used in advertising or |
| 43 | * publicity pertaining to distribution of the document or software without |
| 44 | * specific, written prior permission. |
| 45 | * |
| 46 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
| 47 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
| 48 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
| 49 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 50 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 51 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 52 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 53 | * SOFTWARE. |
| 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
| 58 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
| 59 | * |
| 60 | * Permission to use, copy, modify, and distribute this software for any |
| 61 | * purpose with or without fee is hereby granted, provided that the above |
| 62 | * copyright notice and this permission notice appear in all copies. |
| 63 | * |
| 64 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
| 65 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 66 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
| 67 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 68 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 69 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 70 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 71 | */ |
| 72 | |
| 73 | #include <sys/cdefs.h> |
| 74 | #if defined(LIBC_SCCS) && !defined(lint) |
| 75 | #ifdef notdef |
| 76 | static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; |
| 77 | static const char rcsid[] = "Id: res_init.c,v 1.9.2.5.4.2 2004/03/16 12:34:18 marka Exp"; |
| 78 | #else |
| 79 | __RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $"); |
| 80 | #endif |
| 81 | #endif /* LIBC_SCCS and not lint */ |
| 82 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 83 | #include <sys/param.h> |
| 84 | #include <sys/socket.h> |
| 85 | #include <sys/time.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 86 | #include <sys/types.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 87 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 88 | #include <arpa/inet.h> |
| 89 | #include <arpa/nameser.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 90 | #include <netinet/in.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 91 | |
| 92 | #include <ctype.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 93 | #include <netdb.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 94 | #include <stdio.h> |
| 95 | #include <stdlib.h> |
| 96 | #include <string.h> |
| 97 | #include <unistd.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 98 | |
| 99 | #ifdef ANDROID_CHANGES |
| 100 | #include <errno.h> |
| 101 | #include <fcntl.h> |
| 102 | #endif /* ANDROID_CHANGES */ |
| 103 | |
| 104 | /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */ |
| 105 | #ifdef ANDROID_CHANGES |
| 106 | #include "resolv_netid.h" |
| 107 | #include "resolv_private.h" |
| 108 | #else |
| 109 | #include <resolv.h> |
| 110 | #endif |
| 111 | |
| 112 | #include "res_private.h" |
| 113 | |
| 114 | /* Options. Should all be left alone. */ |
| 115 | #ifndef DEBUG |
| 116 | #define DEBUG |
| 117 | #endif |
| 118 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 119 | static void res_setoptions __P((res_state, const char*, const char*) ); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 120 | |
| 121 | #ifdef RESOLVSORT |
| 122 | static const char sort_mask[] = "/&"; |
| 123 | #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) |
| 124 | static uint32_t net_mask(struct in_addr); |
| 125 | #endif |
| 126 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 127 | #if !defined(isascii) /* XXX - could be a function */ |
| 128 | #define isascii(c) (!(c & 0200)) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | /* |
| 132 | * Resolver state default settings. |
| 133 | */ |
| 134 | |
| 135 | /* |
| 136 | * Set up default settings. If the configuration file exist, the values |
| 137 | * there will have precedence. Otherwise, the server address is set to |
| 138 | * INADDR_ANY and the default domain name comes from the gethostname(). |
| 139 | * |
| 140 | * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 |
| 141 | * rather than INADDR_ANY ("0.0.0.0") as the default name server address |
| 142 | * since it was noted that INADDR_ANY actually meant ``the first interface |
| 143 | * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, |
| 144 | * it had to be "up" in order for you to reach your own name server. It |
| 145 | * was later decided that since the recommended practice is to always |
| 146 | * install local static routes through 127.0.0.1 for all your network |
| 147 | * interfaces, that we could solve this problem without a code change. |
| 148 | * |
| 149 | * The configuration file should always be used, since it is the only way |
| 150 | * to specify a default domain. If you are running a server on your local |
| 151 | * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" |
| 152 | * in the configuration file. |
| 153 | * |
| 154 | * Return 0 if completes successfully, -1 on error |
| 155 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 156 | int res_ninit(res_state statp) { |
| 157 | extern int __res_vinit(res_state, int); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 158 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 159 | return (__res_vinit(statp, 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* This function has to be reachable by res_data.c but not publicly. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 163 | int __res_vinit(res_state statp, int preinit) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 164 | #if !defined(__BIONIC__) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 165 | register FILE* fp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 166 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 167 | register char *cp, **pp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 168 | #if !defined(__BIONIC__) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 169 | register int n; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 170 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 171 | char buf[BUFSIZ]; |
| 172 | int nserv = 0; /* number of nameserver records read from file */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 173 | #if !defined(__BIONIC__) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 174 | int haveenv = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 175 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 176 | int havesearch = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 177 | #ifdef RESOLVSORT |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 178 | int nsort = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 179 | #endif |
| 180 | #if !defined(__BIONIC__) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 181 | char* net; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 182 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 183 | int dots; |
| 184 | union res_sockaddr_union u[2]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 185 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 186 | if ((statp->options & RES_INIT) != 0U) res_ndestroy(statp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 187 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 188 | if (!preinit) { |
| 189 | statp->netid = NETID_UNSET; |
| 190 | statp->retrans = RES_TIMEOUT; |
| 191 | statp->retry = RES_DFLRETRY; |
| 192 | statp->options = RES_DEFAULT; |
| 193 | statp->id = res_randomid(); |
| 194 | statp->_mark = MARK_UNSET; |
| 195 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 196 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 197 | memset(u, 0, sizeof(u)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 198 | #ifdef USELOOPBACK |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 199 | u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 200 | #else |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 201 | u[nserv].sin.sin_addr.s_addr = INADDR_ANY; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 202 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 203 | u[nserv].sin.sin_family = AF_INET; |
| 204 | u[nserv].sin.sin_port = htons(NAMESERVER_PORT); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 205 | #ifdef HAVE_SA_LEN |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 206 | u[nserv].sin.sin_len = sizeof(struct sockaddr_in); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 207 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 208 | nserv++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 209 | #ifdef HAS_INET6_STRUCTS |
| 210 | #ifdef USELOOPBACK |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 211 | u[nserv].sin6.sin6_addr = in6addr_loopback; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 212 | #else |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 213 | u[nserv].sin6.sin6_addr = in6addr_any; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 214 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 215 | u[nserv].sin6.sin6_family = AF_INET6; |
| 216 | u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 217 | #ifdef HAVE_SA_LEN |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 218 | u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 219 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 220 | nserv++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 221 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 222 | statp->nscount = 0; |
| 223 | statp->ndots = 1; |
| 224 | statp->pfcode = 0; |
| 225 | statp->_vcsock = -1; |
| 226 | statp->_flags = 0; |
| 227 | statp->qhook = NULL; |
| 228 | statp->rhook = NULL; |
| 229 | statp->_u._ext.nscount = 0; |
| 230 | statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); |
| 231 | if (statp->_u._ext.ext != NULL) { |
| 232 | memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); |
| 233 | statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; |
| 234 | strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); |
| 235 | strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); |
| 236 | } |
| 237 | statp->nsort = 0; |
| 238 | res_setservers(statp, u, nserv); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 239 | |
| 240 | #if defined(__BIONIC__) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 241 | /* Ignore the environment. */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 242 | #else |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 243 | /* Allow user to override the local domain definition */ |
| 244 | if ((cp = getenv("LOCALDOMAIN")) != NULL) { |
| 245 | (void) strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 246 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 247 | haveenv++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 248 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 249 | /* |
| 250 | * Set search list to be blank-separated strings |
| 251 | * from rest of env value. Permits users of LOCALDOMAIN |
| 252 | * to still have a search list, and anyone to set the |
| 253 | * one that they want to use as an individual (even more |
| 254 | * important now that the rfc1535 stuff restricts searches) |
| 255 | */ |
| 256 | cp = statp->defdname; |
| 257 | pp = statp->dnsrch; |
| 258 | *pp++ = cp; |
| 259 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
| 260 | if (*cp == '\n') /* silly backwards compat */ |
| 261 | break; |
| 262 | else if (*cp == ' ' || *cp == '\t') { |
| 263 | *cp = 0; |
| 264 | n = 1; |
| 265 | } else if (n) { |
| 266 | *pp++ = cp; |
| 267 | n = 0; |
| 268 | havesearch = 1; |
| 269 | } |
| 270 | } |
| 271 | /* null terminate last domain if there are excess */ |
| 272 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') cp++; |
| 273 | *cp = '\0'; |
| 274 | *pp++ = 0; |
| 275 | } |
| 276 | if (nserv > 0) statp->nscount = nserv; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 277 | #endif |
| 278 | |
| 279 | #ifndef ANDROID_CHANGES /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 280 | #define MATCH(line, name) \ |
| 281 | (!strncmp(line, name, sizeof(name) - 1) && \ |
| 282 | (line[sizeof(name) - 1] == ' ' || line[sizeof(name) - 1] == '\t')) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 283 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 284 | nserv = 0; |
| 285 | if ((fp = fopen(_PATH_RESCONF, "re")) != NULL) { |
| 286 | /* read the config file */ |
| 287 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 288 | /* skip comments */ |
| 289 | if (*buf == ';' || *buf == '#') continue; |
| 290 | /* read default domain name */ |
| 291 | if (MATCH(buf, "domain")) { |
| 292 | if (haveenv) /* skip if have from environ */ |
| 293 | continue; |
| 294 | cp = buf + sizeof("domain") - 1; |
| 295 | while (*cp == ' ' || *cp == '\t') cp++; |
| 296 | if ((*cp == '\0') || (*cp == '\n')) continue; |
| 297 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 298 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 299 | if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) *cp = '\0'; |
| 300 | havesearch = 0; |
| 301 | continue; |
| 302 | } |
| 303 | /* set search list */ |
| 304 | if (MATCH(buf, "search")) { |
| 305 | if (haveenv) /* skip if have from environ */ |
| 306 | continue; |
| 307 | cp = buf + sizeof("search") - 1; |
| 308 | while (*cp == ' ' || *cp == '\t') cp++; |
| 309 | if ((*cp == '\0') || (*cp == '\n')) continue; |
| 310 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
| 311 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
| 312 | if ((cp = strchr(statp->defdname, '\n')) != NULL) *cp = '\0'; |
| 313 | /* |
| 314 | * Set search list to be blank-separated strings |
| 315 | * on rest of line. |
| 316 | */ |
| 317 | cp = statp->defdname; |
| 318 | pp = statp->dnsrch; |
| 319 | *pp++ = cp; |
| 320 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
| 321 | if (*cp == ' ' || *cp == '\t') { |
| 322 | *cp = 0; |
| 323 | n = 1; |
| 324 | } else if (n) { |
| 325 | *pp++ = cp; |
| 326 | n = 0; |
| 327 | } |
| 328 | } |
| 329 | /* null terminate last domain if there are excess */ |
| 330 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') cp++; |
| 331 | *cp = '\0'; |
| 332 | *pp++ = 0; |
| 333 | havesearch = 1; |
| 334 | continue; |
| 335 | } |
| 336 | /* read nameservers to query */ |
| 337 | if (MATCH(buf, "nameserver") && nserv < MAXNS) { |
| 338 | struct addrinfo hints, *ai; |
| 339 | char sbuf[NI_MAXSERV]; |
| 340 | const size_t minsiz = sizeof(statp->_u._ext.ext->nsaddrs[0]); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 341 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 342 | cp = buf + sizeof("nameserver") - 1; |
| 343 | while (*cp == ' ' || *cp == '\t') cp++; |
| 344 | cp[strcspn(cp, ";# \t\n")] = '\0'; |
| 345 | if ((*cp != '\0') && (*cp != '\n')) { |
| 346 | memset(&hints, 0, sizeof(hints)); |
| 347 | hints.ai_family = PF_UNSPEC; |
| 348 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 349 | hints.ai_flags = AI_NUMERICHOST; |
| 350 | sprintf(sbuf, "%u", NAMESERVER_PORT); |
| 351 | if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && ai->ai_addrlen <= minsiz) { |
| 352 | if (statp->_u._ext.ext != NULL) { |
| 353 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], ai->ai_addr, |
| 354 | ai->ai_addrlen); |
| 355 | } |
| 356 | if (ai->ai_addrlen <= sizeof(statp->nsaddr_list[nserv])) { |
| 357 | memcpy(&statp->nsaddr_list[nserv], ai->ai_addr, ai->ai_addrlen); |
| 358 | } else |
| 359 | statp->nsaddr_list[nserv].sin_family = 0; |
| 360 | freeaddrinfo(ai); |
| 361 | nserv++; |
| 362 | } |
| 363 | } |
| 364 | continue; |
| 365 | } |
| 366 | if (MATCH(buf, "sortlist")) { |
| 367 | struct in_addr a; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 368 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 369 | cp = buf + sizeof("sortlist") - 1; |
| 370 | while (nsort < MAXRESOLVSORT) { |
| 371 | while (*cp == ' ' || *cp == '\t') cp++; |
| 372 | if (*cp == '\0' || *cp == '\n' || *cp == ';') break; |
| 373 | net = cp; |
| 374 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && isascii(*cp) && |
| 375 | !isspace((unsigned char) *cp)) |
| 376 | cp++; |
| 377 | n = *cp; |
| 378 | *cp = 0; |
| 379 | if (inet_aton(net, &a)) { |
| 380 | statp->sort_list[nsort].addr = a; |
| 381 | if (ISSORTMASK(n)) { |
| 382 | *cp++ = n; |
| 383 | net = cp; |
| 384 | while (*cp && *cp != ';' && isascii(*cp) && |
| 385 | !isspace((unsigned char) *cp)) |
| 386 | cp++; |
| 387 | n = *cp; |
| 388 | *cp = 0; |
| 389 | if (inet_aton(net, &a)) { |
| 390 | statp->sort_list[nsort].mask = a.s_addr; |
| 391 | } else { |
| 392 | statp->sort_list[nsort].mask = |
| 393 | net_mask(statp->sort_list[nsort].addr); |
| 394 | } |
| 395 | } else { |
| 396 | statp->sort_list[nsort].mask = net_mask(statp->sort_list[nsort].addr); |
| 397 | } |
| 398 | nsort++; |
| 399 | } |
| 400 | *cp = n; |
| 401 | } |
| 402 | continue; |
| 403 | } |
| 404 | if (MATCH(buf, "options")) { |
| 405 | res_setoptions(statp, buf + sizeof("options") - 1, "conf"); |
| 406 | continue; |
| 407 | } |
| 408 | } |
| 409 | if (nserv > 0) statp->nscount = nserv; |
| 410 | statp->nsort = nsort; |
| 411 | (void) fclose(fp); |
| 412 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 413 | #endif /* !ANDROID_CHANGES */ |
| 414 | /* |
| 415 | * Last chance to get a nameserver. This should not normally |
| 416 | * be necessary |
| 417 | */ |
| 418 | #ifdef NO_RESOLV_CONF |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 419 | if (nserv == 0) nserv = get_nameservers(statp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 420 | #endif |
| 421 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 422 | if (statp->defdname[0] == 0 && gethostname(buf, sizeof(statp->defdname) - 1) == 0 && |
| 423 | (cp = strchr(buf, '.')) != NULL) |
| 424 | strcpy(statp->defdname, cp + 1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 425 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 426 | /* find components of local domain that might be searched */ |
| 427 | if (havesearch == 0) { |
| 428 | pp = statp->dnsrch; |
| 429 | *pp++ = statp->defdname; |
| 430 | *pp = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 431 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 432 | dots = 0; |
| 433 | for (cp = statp->defdname; *cp; cp++) dots += (*cp == '.'); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 434 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 435 | cp = statp->defdname; |
| 436 | while (pp < statp->dnsrch + MAXDFLSRCH) { |
| 437 | if (dots < LOCALDOMAINPARTS) break; |
| 438 | cp = strchr(cp, '.') + 1; /* we know there is one */ |
| 439 | *pp++ = cp; |
| 440 | dots--; |
| 441 | } |
| 442 | *pp = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 443 | #ifdef DEBUG |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 444 | if (statp->options & RES_DEBUG) { |
| 445 | printf(";; res_init()... default dnsrch list:\n"); |
| 446 | for (pp = statp->dnsrch; *pp; pp++) printf(";;\t%s\n", *pp); |
| 447 | printf(";;\t..END..\n"); |
| 448 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 449 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 450 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 451 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 452 | if ((cp = getenv("RES_OPTIONS")) != NULL) res_setoptions(statp, cp, "env"); |
| 453 | if (nserv > 0) { |
| 454 | statp->nscount = nserv; |
| 455 | statp->options |= RES_INIT; |
| 456 | } |
| 457 | return (0); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 458 | } |
| 459 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 460 | static void res_setoptions(res_state statp, const char* options, const char* source) { |
| 461 | const char* cp = options; |
| 462 | int i; |
| 463 | struct __res_state_ext* ext = statp->_u._ext.ext; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 464 | |
| 465 | #ifdef DEBUG |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 466 | if (statp->options & RES_DEBUG) |
| 467 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", options, source); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 468 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 469 | while (*cp) { |
| 470 | /* skip leading and inner runs of spaces */ |
| 471 | while (*cp == ' ' || *cp == '\t') cp++; |
| 472 | /* search for and process individual options */ |
| 473 | if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { |
| 474 | i = atoi(cp + sizeof("ndots:") - 1); |
| 475 | if (i <= RES_MAXNDOTS) |
| 476 | statp->ndots = i; |
| 477 | else |
| 478 | statp->ndots = RES_MAXNDOTS; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 479 | #ifdef DEBUG |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 480 | if (statp->options & RES_DEBUG) printf(";;\tndots=%d\n", statp->ndots); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 481 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 482 | } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { |
| 483 | i = atoi(cp + sizeof("timeout:") - 1); |
| 484 | if (i <= RES_MAXRETRANS) |
| 485 | statp->retrans = i; |
| 486 | else |
| 487 | statp->retrans = RES_MAXRETRANS; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 488 | #ifdef DEBUG |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 489 | if (statp->options & RES_DEBUG) printf(";;\ttimeout=%d\n", statp->retrans); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 490 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 491 | } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)) { |
| 492 | i = atoi(cp + sizeof("attempts:") - 1); |
| 493 | if (i <= RES_MAXRETRY) |
| 494 | statp->retry = i; |
| 495 | else |
| 496 | statp->retry = RES_MAXRETRY; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 497 | #ifdef DEBUG |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 498 | if (statp->options & RES_DEBUG) printf(";;\tattempts=%d\n", statp->retry); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 499 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 500 | } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 501 | #ifdef DEBUG |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 502 | if (!(statp->options & RES_DEBUG)) { |
| 503 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", options, source); |
| 504 | statp->options |= RES_DEBUG; |
| 505 | } |
| 506 | printf(";;\tdebug\n"); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 507 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 508 | } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1) || |
| 509 | !strncmp(cp, "no-tld-query", sizeof("no-tld-query") - 1)) { |
| 510 | statp->options |= RES_NOTLDQUERY; |
| 511 | } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { |
| 512 | statp->options |= RES_USE_INET6; |
| 513 | } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { |
| 514 | statp->options |= RES_ROTATE; |
| 515 | } else if (!strncmp(cp, "no-check-names", sizeof("no-check-names") - 1)) { |
| 516 | statp->options |= RES_NOCHECKNAME; |
| 517 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 518 | #ifdef RES_USE_EDNS0 |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 519 | else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { |
| 520 | statp->options |= RES_USE_EDNS0; |
| 521 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 522 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 523 | else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { |
| 524 | statp->options |= RES_USE_DNAME; |
| 525 | } else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { |
| 526 | if (ext == NULL) goto skip; |
| 527 | cp += sizeof("nibble:") - 1; |
| 528 | i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); |
| 529 | strncpy(ext->nsuffix, cp, (size_t) i); |
| 530 | ext->nsuffix[i] = '\0'; |
| 531 | } else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { |
| 532 | if (ext == NULL) goto skip; |
| 533 | cp += sizeof("nibble2:") - 1; |
| 534 | i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); |
| 535 | strncpy(ext->nsuffix2, cp, (size_t) i); |
| 536 | ext->nsuffix2[i] = '\0'; |
| 537 | } else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { |
| 538 | cp += sizeof("v6revmode:") - 1; |
| 539 | /* "nibble" and "bitstring" used to be valid */ |
| 540 | if (!strncmp(cp, "single", sizeof("single") - 1)) { |
| 541 | statp->options |= RES_NO_NIBBLE2; |
| 542 | } else if (!strncmp(cp, "both", sizeof("both") - 1)) { |
| 543 | statp->options &= ~RES_NO_NIBBLE2; |
| 544 | } |
| 545 | } else { |
| 546 | /* XXX - print a warning here? */ |
| 547 | } |
| 548 | skip: |
| 549 | /* skip to next run of spaces */ |
| 550 | while (*cp && *cp != ' ' && *cp != '\t') cp++; |
| 551 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | #ifdef RESOLVSORT |
| 555 | /* XXX - should really support CIDR which means explicit masks always. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 556 | static uint32_t net_mask(struct in_addr in) /*!< XXX - should really use system's version of this */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 557 | { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 558 | register uint32_t i = ntohl(in.s_addr); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 559 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 560 | if (IN_CLASSA(i)) |
| 561 | return (htonl(IN_CLASSA_NET)); |
| 562 | else if (IN_CLASSB(i)) |
| 563 | return (htonl(IN_CLASSB_NET)); |
| 564 | return (htonl(IN_CLASSC_NET)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 565 | } |
| 566 | #endif |
| 567 | |
| 568 | /*% |
| 569 | * This routine is for closing the socket if a virtual circuit is used and |
| 570 | * the program wants to close it. This provides support for endhostent() |
| 571 | * which expects to close the socket. |
| 572 | * |
| 573 | * This routine is not expected to be user visible. |
| 574 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 575 | void res_nclose(res_state statp) { |
| 576 | int ns; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 577 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 578 | if (statp->_vcsock >= 0) { |
| 579 | (void) close(statp->_vcsock); |
| 580 | statp->_vcsock = -1; |
| 581 | statp->_flags &= ~(RES_F_VC | RES_F_CONN); |
| 582 | } |
| 583 | for (ns = 0; ns < statp->_u._ext.nscount; ns++) { |
| 584 | if (statp->_u._ext.nssocks[ns] != -1) { |
| 585 | (void) close(statp->_u._ext.nssocks[ns]); |
| 586 | statp->_u._ext.nssocks[ns] = -1; |
| 587 | } |
| 588 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 589 | } |
| 590 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 591 | void res_ndestroy(res_state statp) { |
| 592 | res_nclose(statp); |
| 593 | if (statp->_u._ext.ext != NULL) free(statp->_u._ext.ext); |
| 594 | statp->options &= ~RES_INIT; |
| 595 | statp->_u._ext.ext = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 596 | } |
| 597 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 598 | const char* res_get_nibblesuffix(res_state statp) { |
| 599 | if (statp->_u._ext.ext) return (statp->_u._ext.ext->nsuffix); |
| 600 | return ("ip6.arpa"); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 601 | } |
| 602 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 603 | const char* res_get_nibblesuffix2(res_state statp) { |
| 604 | if (statp->_u._ext.ext) return (statp->_u._ext.ext->nsuffix2); |
| 605 | return ("ip6.int"); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 606 | } |
| 607 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 608 | void res_setservers(res_state statp, const union res_sockaddr_union* set, int cnt) { |
| 609 | int i, nserv; |
| 610 | size_t size; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 611 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 612 | /* close open servers */ |
| 613 | res_nclose(statp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 614 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 615 | /* cause rtt times to be forgotten */ |
| 616 | statp->_u._ext.nscount = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 617 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 618 | nserv = 0; |
| 619 | for (i = 0; i < cnt && nserv < MAXNS; i++) { |
| 620 | switch (set->sin.sin_family) { |
| 621 | case AF_INET: |
| 622 | size = sizeof(set->sin); |
| 623 | if (statp->_u._ext.ext) |
| 624 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin, size); |
| 625 | if (size <= sizeof(statp->nsaddr_list[nserv])) |
| 626 | memcpy(&statp->nsaddr_list[nserv], &set->sin, size); |
| 627 | else |
| 628 | statp->nsaddr_list[nserv].sin_family = 0; |
| 629 | nserv++; |
| 630 | break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 631 | |
| 632 | #ifdef HAS_INET6_STRUCTS |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 633 | case AF_INET6: |
| 634 | size = sizeof(set->sin6); |
| 635 | if (statp->_u._ext.ext) |
| 636 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin6, size); |
| 637 | if (size <= sizeof(statp->nsaddr_list[nserv])) |
| 638 | memcpy(&statp->nsaddr_list[nserv], &set->sin6, size); |
| 639 | else |
| 640 | statp->nsaddr_list[nserv].sin_family = 0; |
| 641 | nserv++; |
| 642 | break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 643 | #endif |
| 644 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 645 | default: |
| 646 | break; |
| 647 | } |
| 648 | set++; |
| 649 | } |
| 650 | statp->nscount = nserv; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 651 | } |
| 652 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 653 | int res_getservers(res_state statp, union res_sockaddr_union* set, int cnt) { |
| 654 | int i; |
| 655 | size_t size; |
| 656 | uint16_t family; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 657 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 658 | for (i = 0; i < statp->nscount && i < cnt; i++) { |
| 659 | if (statp->_u._ext.ext) |
| 660 | family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; |
| 661 | else |
| 662 | family = statp->nsaddr_list[i].sin_family; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 663 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 664 | switch (family) { |
| 665 | case AF_INET: |
| 666 | size = sizeof(set->sin); |
| 667 | if (statp->_u._ext.ext) |
| 668 | memcpy(&set->sin, &statp->_u._ext.ext->nsaddrs[i], size); |
| 669 | else |
| 670 | memcpy(&set->sin, &statp->nsaddr_list[i], size); |
| 671 | break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 672 | |
| 673 | #ifdef HAS_INET6_STRUCTS |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 674 | case AF_INET6: |
| 675 | size = sizeof(set->sin6); |
| 676 | if (statp->_u._ext.ext) |
| 677 | memcpy(&set->sin6, &statp->_u._ext.ext->nsaddrs[i], size); |
| 678 | else |
| 679 | memcpy(&set->sin6, &statp->nsaddr_list[i], size); |
| 680 | break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 681 | #endif |
| 682 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 683 | default: |
| 684 | set->sin.sin_family = 0; |
| 685 | break; |
| 686 | } |
| 687 | set++; |
| 688 | } |
| 689 | return (statp->nscount); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | #ifdef ANDROID_CHANGES |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 693 | void res_setnetcontext(res_state statp, const struct android_net_context* netcontext) { |
| 694 | if (statp != NULL) { |
| 695 | statp->netid = netcontext->dns_netid; |
| 696 | statp->_mark = netcontext->dns_mark; |
| 697 | statp->qhook = netcontext->qhook; |
| 698 | if (netcontext->flags & NET_CONTEXT_FLAG_USE_EDNS) { |
| 699 | statp->options |= RES_USE_EDNS0 | RES_USE_DNSSEC; |
| 700 | } |
| 701 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | #endif /* ANDROID_CHANGES */ |