blob: 0ec645e14840cfac8b584c38a1a94bf97ecc454f [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $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 */
Bernie Innocentid017e972019-03-03 19:39:53 +090072
73#define LOG_TAG "res_init"
74
Bernie Innocenti55864192018-08-30 04:05:20 +090075#include <sys/param.h>
76#include <sys/socket.h>
77#include <sys/time.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090078#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090079
Bernie Innocenti55864192018-08-30 04:05:20 +090080#include <arpa/inet.h>
81#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090083
chenbruce16adee42019-02-20 19:45:50 +080084#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090085#include <ctype.h>
Bernie Innocenti2fd418e2018-08-30 12:04:03 +090086#include <errno.h>
87#include <fcntl.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090089#include <stdio.h>
90#include <stdlib.h>
91#include <string.h>
92#include <unistd.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090093
Bernie Innocenti189eb502018-10-01 23:10:18 +090094#include "netd_resolv/resolv.h"
95#include "res_state_ext.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090096#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090097
Bernie Innocenti55864192018-08-30 04:05:20 +090098
Bernie Innocenti2fd418e2018-08-30 12:04:03 +090099static void res_setoptions(res_state, const char*, const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900100
101/*
102 * Resolver state default settings.
103 */
104
105/*
106 * Set up default settings. If the configuration file exist, the values
107 * there will have precedence. Otherwise, the server address is set to
108 * INADDR_ANY and the default domain name comes from the gethostname().
109 *
110 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
111 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
112 * since it was noted that INADDR_ANY actually meant ``the first interface
113 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
114 * it had to be "up" in order for you to reach your own name server. It
115 * was later decided that since the recommended practice is to always
116 * install local static routes through 127.0.0.1 for all your network
117 * interfaces, that we could solve this problem without a code change.
118 *
119 * The configuration file should always be used, since it is the only way
120 * to specify a default domain. If you are running a server on your local
121 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
122 * in the configuration file.
123 *
124 * Return 0 if completes successfully, -1 on error
125 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900126int res_ninit(res_state statp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900127 return res_vinit(statp, 0);
Bernie Innocenti55864192018-08-30 04:05:20 +0900128}
129
130/* This function has to be reachable by res_data.c but not publicly. */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900131int res_vinit(res_state statp, int preinit) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900132 char *cp, **pp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900133 char buf[BUFSIZ];
134 int nserv = 0; /* number of nameserver records read from file */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900135 int havesearch = 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900136 int dots;
nuccachene172a4e2018-10-23 17:10:58 +0800137 sockaddr_union u[2];
Bernie Innocenti55864192018-08-30 04:05:20 +0900138
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139 if ((statp->options & RES_INIT) != 0U) res_ndestroy(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900140
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141 if (!preinit) {
142 statp->netid = NETID_UNSET;
143 statp->retrans = RES_TIMEOUT;
144 statp->retry = RES_DFLRETRY;
145 statp->options = RES_DEFAULT;
Luke Huang042e9422019-02-13 14:05:02 +0800146 statp->id = arc4random_uniform(65536);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147 statp->_mark = MARK_UNSET;
148 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900149
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150 memset(u, 0, sizeof(u));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900151 u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 u[nserv].sin.sin_family = AF_INET;
153 u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900154 nserv++;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 statp->nscount = 0;
156 statp->ndots = 1;
157 statp->pfcode = 0;
158 statp->_vcsock = -1;
159 statp->_flags = 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160 statp->_u._ext.nscount = 0;
Bernie Innocenti189eb502018-10-01 23:10:18 +0900161 statp->_u._ext.ext = (res_state_ext*) malloc(sizeof(*statp->_u._ext.ext));
Mike Yua46fae72018-11-01 20:07:00 +0800162 statp->use_local_nameserver = false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163 if (statp->_u._ext.ext != NULL) {
164 memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
165 statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
166 strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
167 strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
168 }
169 statp->nsort = 0;
170 res_setservers(statp, u, nserv);
Bernie Innocenti55864192018-08-30 04:05:20 +0900171
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900172 if (statp->defdname[0] == 0 && gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
173 (cp = strchr(buf, '.')) != NULL)
174 strcpy(statp->defdname, cp + 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900175
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900176 /* find components of local domain that might be searched */
177 if (havesearch == 0) {
178 pp = statp->dnsrch;
179 *pp++ = statp->defdname;
180 *pp = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900181
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900182 dots = 0;
183 for (cp = statp->defdname; *cp; cp++) dots += (*cp == '.');
Bernie Innocenti55864192018-08-30 04:05:20 +0900184
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900185 cp = statp->defdname;
186 while (pp < statp->dnsrch + MAXDFLSRCH) {
187 if (dots < LOCALDOMAINPARTS) break;
188 cp = strchr(cp, '.') + 1; /* we know there is one */
189 *pp++ = cp;
190 dots--;
191 }
192 *pp = NULL;
Bernie Innocentid017e972019-03-03 19:39:53 +0900193 LOG(DEBUG) << "res_init(): dnsrch list:";
194 for (pp = statp->dnsrch; *pp; pp++) LOG(DEBUG) << "\t" << *pp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900196
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197 if ((cp = getenv("RES_OPTIONS")) != NULL) res_setoptions(statp, cp, "env");
198 if (nserv > 0) {
199 statp->nscount = nserv;
200 statp->options |= RES_INIT;
201 }
202 return (0);
Bernie Innocenti55864192018-08-30 04:05:20 +0900203}
204
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900205static void res_setoptions(res_state statp, const char* options, const char* source) {
206 const char* cp = options;
207 int i;
Bernie Innocenti189eb502018-10-01 23:10:18 +0900208 res_state_ext* ext = statp->_u._ext.ext;
Bernie Innocenti55864192018-08-30 04:05:20 +0900209
chenbruce16adee42019-02-20 19:45:50 +0800210 LOG(DEBUG) << ";; res_setoptions(\"" << options << "\", \"" << source << "\")...";
211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900212 while (*cp) {
213 /* skip leading and inner runs of spaces */
214 while (*cp == ' ' || *cp == '\t') cp++;
215 /* search for and process individual options */
216 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
217 i = atoi(cp + sizeof("ndots:") - 1);
218 if (i <= RES_MAXNDOTS)
219 statp->ndots = i;
220 else
221 statp->ndots = RES_MAXNDOTS;
chenbruce16adee42019-02-20 19:45:50 +0800222
223 LOG(DEBUG) << ";;\tndots=" << statp->ndots;
224
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900225 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
226 i = atoi(cp + sizeof("timeout:") - 1);
227 if (i <= RES_MAXRETRANS)
228 statp->retrans = i;
229 else
230 statp->retrans = RES_MAXRETRANS;
chenbruce16adee42019-02-20 19:45:50 +0800231
232 LOG(DEBUG) << ";;\ttimeout=" << statp->retrans;
233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)) {
235 i = atoi(cp + sizeof("attempts:") - 1);
236 if (i <= RES_MAXRETRY)
237 statp->retry = i;
238 else
239 statp->retry = RES_MAXRETRY;
chenbruce16adee42019-02-20 19:45:50 +0800240
241 LOG(DEBUG) << ";;\tattempts=" << statp->retry;
242
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900243 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900244 if (!(statp->options & RES_DEBUG)) {
chenbruce16adee42019-02-20 19:45:50 +0800245 LOG(DEBUG) << ";; res_setoptions(\"" << options << "\", \"" << source << "\")..";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900246 statp->options |= RES_DEBUG;
247 }
chenbruce16adee42019-02-20 19:45:50 +0800248 LOG(DEBUG) << ";;\tdebug";
249
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900250 } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1) ||
251 !strncmp(cp, "no-tld-query", sizeof("no-tld-query") - 1)) {
252 statp->options |= RES_NOTLDQUERY;
253 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
254 statp->options |= RES_USE_INET6;
255 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
256 statp->options |= RES_ROTATE;
257 } else if (!strncmp(cp, "no-check-names", sizeof("no-check-names") - 1)) {
258 statp->options |= RES_NOCHECKNAME;
259 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900260 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
261 statp->options |= RES_USE_EDNS0;
262 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900263 else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
264 statp->options |= RES_USE_DNAME;
265 } else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
266 if (ext == NULL) goto skip;
267 cp += sizeof("nibble:") - 1;
268 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
269 strncpy(ext->nsuffix, cp, (size_t) i);
270 ext->nsuffix[i] = '\0';
271 } else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
272 if (ext == NULL) goto skip;
273 cp += sizeof("nibble2:") - 1;
274 i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
275 strncpy(ext->nsuffix2, cp, (size_t) i);
276 ext->nsuffix2[i] = '\0';
277 } else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
278 cp += sizeof("v6revmode:") - 1;
279 /* "nibble" and "bitstring" used to be valid */
280 if (!strncmp(cp, "single", sizeof("single") - 1)) {
281 statp->options |= RES_NO_NIBBLE2;
282 } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
283 statp->options &= ~RES_NO_NIBBLE2;
284 }
285 } else {
286 /* XXX - print a warning here? */
287 }
288 skip:
289 /* skip to next run of spaces */
290 while (*cp && *cp != ' ' && *cp != '\t') cp++;
291 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900292}
293
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900294/*
Bernie Innocenti55864192018-08-30 04:05:20 +0900295 * This routine is for closing the socket if a virtual circuit is used and
296 * the program wants to close it. This provides support for endhostent()
297 * which expects to close the socket.
298 *
299 * This routine is not expected to be user visible.
300 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900301void res_nclose(res_state statp) {
302 int ns;
Bernie Innocenti55864192018-08-30 04:05:20 +0900303
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900304 if (statp->_vcsock >= 0) {
305 (void) close(statp->_vcsock);
306 statp->_vcsock = -1;
Bernie Innocenti49009f42019-01-08 18:36:28 +0900307 statp->_flags &= ~RES_F_VC;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900308 }
309 for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
310 if (statp->_u._ext.nssocks[ns] != -1) {
311 (void) close(statp->_u._ext.nssocks[ns]);
312 statp->_u._ext.nssocks[ns] = -1;
313 }
314 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900315}
316
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900317void res_ndestroy(res_state statp) {
318 res_nclose(statp);
319 if (statp->_u._ext.ext != NULL) free(statp->_u._ext.ext);
320 statp->options &= ~RES_INIT;
321 statp->_u._ext.ext = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900322}
323
nuccachene172a4e2018-10-23 17:10:58 +0800324void res_setservers(res_state statp, const sockaddr_union* set, int cnt) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900325 int i, nserv;
326 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900327
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900328 /* close open servers */
329 res_nclose(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900330
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 /* cause rtt times to be forgotten */
332 statp->_u._ext.nscount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900333
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900334 nserv = 0;
335 for (i = 0; i < cnt && nserv < MAXNS; i++) {
336 switch (set->sin.sin_family) {
337 case AF_INET:
338 size = sizeof(set->sin);
339 if (statp->_u._ext.ext)
340 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin, size);
341 if (size <= sizeof(statp->nsaddr_list[nserv]))
342 memcpy(&statp->nsaddr_list[nserv], &set->sin, size);
343 else
344 statp->nsaddr_list[nserv].sin_family = 0;
345 nserv++;
346 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900347
348#ifdef HAS_INET6_STRUCTS
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900349 case AF_INET6:
350 size = sizeof(set->sin6);
351 if (statp->_u._ext.ext)
352 memcpy(&statp->_u._ext.ext->nsaddrs[nserv], &set->sin6, size);
353 if (size <= sizeof(statp->nsaddr_list[nserv]))
354 memcpy(&statp->nsaddr_list[nserv], &set->sin6, size);
355 else
356 statp->nsaddr_list[nserv].sin_family = 0;
357 nserv++;
358 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900359#endif
360
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900361 default:
362 break;
363 }
364 set++;
365 }
366 statp->nscount = nserv;
Bernie Innocenti55864192018-08-30 04:05:20 +0900367}
368
nuccachene172a4e2018-10-23 17:10:58 +0800369int res_getservers(res_state statp, sockaddr_union* set, int cnt) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900370 int i;
371 size_t size;
372 uint16_t family;
Bernie Innocenti55864192018-08-30 04:05:20 +0900373
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900374 for (i = 0; i < statp->nscount && i < cnt; i++) {
375 if (statp->_u._ext.ext)
376 family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
377 else
378 family = statp->nsaddr_list[i].sin_family;
Bernie Innocenti55864192018-08-30 04:05:20 +0900379
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900380 switch (family) {
381 case AF_INET:
382 size = sizeof(set->sin);
383 if (statp->_u._ext.ext)
384 memcpy(&set->sin, &statp->_u._ext.ext->nsaddrs[i], size);
385 else
386 memcpy(&set->sin, &statp->nsaddr_list[i], size);
387 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900388
389#ifdef HAS_INET6_STRUCTS
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900390 case AF_INET6:
391 size = sizeof(set->sin6);
392 if (statp->_u._ext.ext)
393 memcpy(&set->sin6, &statp->_u._ext.ext->nsaddrs[i], size);
394 else
395 memcpy(&set->sin6, &statp->nsaddr_list[i], size);
396 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900397#endif
398
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900399 default:
400 set->sin.sin_family = 0;
401 break;
402 }
403 set++;
404 }
405 return (statp->nscount);
Bernie Innocenti55864192018-08-30 04:05:20 +0900406}
407
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900408void res_setnetcontext(res_state statp, const struct android_net_context* netcontext) {
409 if (statp != NULL) {
410 statp->netid = netcontext->dns_netid;
411 statp->_mark = netcontext->dns_mark;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900412 if (netcontext->flags & NET_CONTEXT_FLAG_USE_EDNS) {
413 statp->options |= RES_USE_EDNS0 | RES_USE_DNSSEC;
414 }
Mike Yua46fae72018-11-01 20:07:00 +0800415 if (netcontext->flags & NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS) {
416 statp->use_local_nameserver = true;
417 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900418 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900419}