blob: 604f9c1a36fc7a5f77f29996bdeca7111a828e5e [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1988, 1993
5 * -
6 * Copyright (c) 1985, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
Bernie Innocenti55864192018-08-30 04:05:20 +090054#include <arpa/inet.h>
55#include <arpa/nameser.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090056#include <assert.h>
57#include <ctype.h>
58#include <errno.h>
59#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090060#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090061#include <stdarg.h>
62#include <stdbool.h>
63#include <stdio.h>
64#include <strings.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090065#include <sys/param.h>
66#include <sys/socket.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090067#include <sys/types.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090068#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090069#include <syslog.h>
70#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090071
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090072#include "resolv_cache.h"
73#include "resolv_netid.h"
74#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090075
Bernie Innocentif89b3512018-08-30 07:34:37 +090076// NetBSD uses _DIAGASSERT to null-check arguments and the like,
77// but it's clear from the number of mistakes in their assertions
78// that they don't actually test or ship with this.
79#define _DIAGASSERT(e) /* nothing */
80
Bernie Innocenti55864192018-08-30 04:05:20 +090081#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090083
84#ifndef LOG_AUTH
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#define LOG_AUTH 0
Bernie Innocenti55864192018-08-30 04:05:20 +090086#endif
87
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
Bernie Innocenti55864192018-08-30 04:05:20 +090089
Bernie Innocenti55864192018-08-30 04:05:20 +090090#include <stdlib.h>
91#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090092#include "nsswitch.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090093
94#include "hostent.h"
95
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090096#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0)
Bernie Innocenti55864192018-08-30 04:05:20 +090097#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
98#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
99
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900100#define addalias(d, s, arr, siz) \
101 do { \
102 if (d >= &arr[siz]) { \
103 char** xptr = (char**) realloc(arr, (siz + 10) * sizeof(*arr)); \
104 if (xptr == NULL) goto nospc; \
105 d = xptr + (d - arr); \
106 arr = xptr; \
107 siz += 10; \
108 } \
109 *d++ = s; \
110 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900111
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900112static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900113
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET,
115 .app_netid = NETID_UNSET,
116 .dns_mark = MARK_UNSET,
117 .dns_netid = NETID_UNSET,
118 .uid = NET_CONTEXT_INVALID_UID};
Bernie Innocenti55864192018-08-30 04:05:20 +0900119
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900121
122typedef union {
123 HEADER hdr;
124 u_char buf[MAXPACKET];
125} querybuf;
126
127typedef union {
128 int32_t al;
129 char ac;
130} align;
131
132#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900133static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900134#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900135static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
136 char*, size_t, int*);
137static void map_v4v6_address(const char*, char*);
138static void map_v4v6_hostent(struct hostent*, char**, char*);
139static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900140
141void ht_sethostent(int);
142void ht_endhostent(void);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900143struct hostent* ht_gethostbyname(char*);
144struct hostent* ht_gethostbyaddr(const char*, int, int);
Bernie Innocenti55864192018-08-30 04:05:20 +0900145void dns_service(void);
146#undef dn_skipname
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147int dn_skipname(const u_char*, const u_char*);
148static int _dns_gethtbyaddr(void*, void*, va_list);
149static int _dns_gethtbyname(void*, void*, va_list);
Bernie Innocenti55864192018-08-30 04:05:20 +0900150
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900151static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
152 size_t, int*, const struct android_net_context*);
153static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
154 const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
155 const struct android_net_context*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900156
157static const ns_src default_dns_files[] = {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900158 {NSSRC_FILES, NS_SUCCESS},
159 {NSSRC_DNS, NS_SUCCESS},
160 {0, 0}
161};
Bernie Innocenti55864192018-08-30 04:05:20 +0900162
163static int h_errno_to_result(int* herrno_p) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900164 // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
165 if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
166 errno = ERANGE;
167 return errno;
168 }
169 // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
170 return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900171}
172
173#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900174static void debugprintf(const char* msg, res_state res, ...) {
175 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900176
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900177 if (res->options & RES_DEBUG) {
178 int save = errno;
179 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900180
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900181 va_start(ap, res);
182 vprintf(msg, ap);
183 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900184
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900185 errno = save;
186 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900187}
188#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900190#endif
191
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192#define BOUNDED_INCR(x) \
193 do { \
194 BOUNDS_CHECK(cp, x); \
195 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900196 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198#define BOUNDS_CHECK(ptr, count) \
199 do { \
200 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900201 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900202
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
204 res_state res, struct hostent* hent, char* buf, size_t buflen,
205 int* he) {
206 const HEADER* hp;
207 const u_char* cp;
208 int n;
209 size_t qlen;
210 const u_char *eom, *erdata;
211 char *bp, **ap, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900212 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900213 int haveanswer, had_error;
214 int toobig = 0;
215 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900216 char* addr_ptrs[MAXADDRS];
217 const char* tname;
218 int (*name_ok)(const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900219
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220 _DIAGASSERT(answer != NULL);
221 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223 tname = qname;
224 hent->h_name = NULL;
225 eom = answer->buf + anslen;
226 switch (qtype) {
227 case T_A:
228 case T_AAAA:
229 name_ok = res_hnok;
230 break;
231 case T_PTR:
232 name_ok = res_dnok;
233 break;
234 default:
235 *he = NO_RECOVERY;
236 return NULL; /* XXX should be abort(); */
237 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900238
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900239 size_t maxaliases = 10;
240 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
241 if (!aliases) goto nospc;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900242 /*
243 * find first satisfactory answer
244 */
245 hp = &answer->hdr;
246 ancount = ntohs(hp->ancount);
247 qdcount = ntohs(hp->qdcount);
248 bp = buf;
249 ep = buf + buflen;
250 cp = answer->buf;
251 BOUNDED_INCR(HFIXEDSZ);
252 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900253
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900254 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
255 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900256
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900257 BOUNDED_INCR(n + QFIXEDSZ);
258 if (qtype == T_A || qtype == T_AAAA) {
259 /* res_send() has already verified that the query name is the
260 * same as the one we sent; this just gets the expanded name
261 * (i.e., with the succeeding search-domain tacked on).
262 */
263 n = (int) strlen(bp) + 1; /* for the \0 */
264 if (n >= MAXHOSTNAMELEN) goto no_recovery;
265 hent->h_name = bp;
266 bp += n;
267 /* The qname can be abbreviated, but h_name is now absolute. */
268 qname = hent->h_name;
269 }
270 hent->h_aliases = ap = aliases;
271 hent->h_addr_list = hap = addr_ptrs;
272 *ap = NULL;
273 *hap = NULL;
274 haveanswer = 0;
275 had_error = 0;
276 while (ancount-- > 0 && cp < eom && !had_error) {
277 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
278 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
279 had_error++;
280 continue;
281 }
282 cp += n; /* name */
283 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900284 int type = _getshort(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900285 cp += INT16SZ; /* type */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900286 int cl = _getshort(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900287 cp += INT16SZ + INT32SZ; /* class, TTL */
288 n = _getshort(cp);
289 cp += INT16SZ; /* len */
290 BOUNDS_CHECK(cp, n);
291 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900292 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900293 /* XXX - debug? syslog? */
294 cp += n;
295 continue; /* XXX - had_error++ ? */
296 }
297 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
298 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
299 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
300 had_error++;
301 continue;
302 }
303 cp += n;
304 if (cp != erdata) goto no_recovery;
305 /* Store alias. */
306 addalias(ap, bp, aliases, maxaliases);
307 n = (int) strlen(bp) + 1; /* for the \0 */
308 if (n >= MAXHOSTNAMELEN) {
309 had_error++;
310 continue;
311 }
312 bp += n;
313 /* Get canonical name. */
314 n = (int) strlen(tbuf) + 1; /* for the \0 */
315 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
316 had_error++;
317 continue;
318 }
319 strlcpy(bp, tbuf, (size_t)(ep - bp));
320 hent->h_name = bp;
321 bp += n;
322 continue;
323 }
324 if (qtype == T_PTR && type == T_CNAME) {
325 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
326 if (n < 0 || !maybe_dnok(res, tbuf)) {
327 had_error++;
328 continue;
329 }
330 cp += n;
331 if (cp != erdata) goto no_recovery;
332 /* Get canonical name. */
333 n = (int) strlen(tbuf) + 1; /* for the \0 */
334 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
335 had_error++;
336 continue;
337 }
338 strlcpy(bp, tbuf, (size_t)(ep - bp));
339 tname = bp;
340 bp += n;
341 continue;
342 }
343 if (type != qtype) {
344 if (type != T_KEY && type != T_SIG)
345 syslog(LOG_NOTICE | LOG_AUTH,
346 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
347 p_class(C_IN), p_type(qtype), p_type(type));
348 cp += n;
349 continue; /* XXX - had_error++ ? */
350 }
351 switch (type) {
352 case T_PTR:
353 if (strcasecmp(tname, bp) != 0) {
354 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
355 cp += n;
356 continue; /* XXX - had_error++ ? */
357 }
358 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
359 if ((n < 0) || !maybe_hnok(res, bp)) {
360 had_error++;
361 break;
362 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900363#if MULTI_PTRS_ARE_ALIASES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900364 cp += n;
365 if (cp != erdata) goto no_recovery;
366 if (!haveanswer)
367 hent->h_name = bp;
368 else
369 addalias(ap, bp, aliases, maxaliases);
370 if (n != -1) {
371 n = (int) strlen(bp) + 1; /* for the \0 */
372 if (n >= MAXHOSTNAMELEN) {
373 had_error++;
374 break;
375 }
376 bp += n;
377 }
378 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900379#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900380 hent->h_name = bp;
381 if (res->options & RES_USE_INET6) {
382 n = strlen(bp) + 1; /* for the \0 */
383 if (n >= MAXHOSTNAMELEN) {
384 had_error++;
385 break;
386 }
387 bp += n;
388 map_v4v6_hostent(hent, &bp, ep);
389 }
390 goto success;
Bernie Innocenti55864192018-08-30 04:05:20 +0900391#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900392 case T_A:
393 case T_AAAA:
394 if (strcasecmp(hent->h_name, bp) != 0) {
395 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
396 cp += n;
397 continue; /* XXX - had_error++ ? */
398 }
399 if (n != hent->h_length) {
400 cp += n;
401 continue;
402 }
403 if (type == T_AAAA) {
404 struct in6_addr in6;
405 memcpy(&in6, cp, NS_IN6ADDRSZ);
406 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
407 cp += n;
408 continue;
409 }
410 }
411 if (!haveanswer) {
412 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900413
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900414 hent->h_name = bp;
415 nn = (int) strlen(bp) + 1; /* for the \0 */
416 bp += nn;
417 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900420
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900421 if (bp + n >= ep) {
422 debugprintf("size (%d) too big\n", res, n);
423 had_error++;
424 continue;
425 }
426 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
427 if (!toobig++) {
428 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
429 }
430 cp += n;
431 continue;
432 }
433 (void) memcpy(*hap++ = bp, cp, (size_t) n);
434 bp += n;
435 cp += n;
436 if (cp != erdata) goto no_recovery;
437 break;
438 default:
439 abort();
440 }
441 if (!had_error) haveanswer++;
442 }
443 if (haveanswer) {
444 *ap = NULL;
445 *hap = NULL;
446 /*
447 * Note: we sort even if host can take only one address
448 * in its return structures - should give it the "best"
449 * address in that case, not some random one
450 */
451 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
452 if (!hent->h_name) {
453 n = (int) strlen(qname) + 1; /* for the \0 */
454 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
455 strlcpy(bp, qname, (size_t)(ep - bp));
456 hent->h_name = bp;
457 bp += n;
458 }
459 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
460 goto success;
461 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900462no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900463 free(aliases);
464 *he = NO_RECOVERY;
465 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900466success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900467 bp = (char*) ALIGN(bp);
468 n = (int) (ap - aliases);
469 qlen = (n + 1) * sizeof(*hent->h_aliases);
470 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900471 hent->h_aliases = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900472 memcpy(bp, aliases, qlen);
473 free(aliases);
474 aliases = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900475
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900476 bp += qlen;
477 n = (int) (hap - addr_ptrs);
478 qlen = (n + 1) * sizeof(*hent->h_addr_list);
479 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900480 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900481 memcpy(bp, addr_ptrs, qlen);
482 *he = NETDB_SUCCESS;
483 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900484nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485 free(aliases);
486 errno = ENOSPC;
487 *he = NETDB_INTERNAL;
488 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900489}
490
491/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492int gethostbyname_r(const char* name, struct hostent* hp, char* buf, size_t buflen,
493 struct hostent** result, int* errorp) {
494 res_state res = __res_get_state();
495 if (res == NULL) {
496 *result = NULL;
497 *errorp = NETDB_INTERNAL;
498 return -1;
499 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900500
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900501 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 if (res->options & RES_USE_INET6) {
504 *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
505 &NETCONTEXT_UNSET);
506 if (*result) {
507 __res_put_state(res);
508 return 0;
509 }
510 }
511 *result =
512 gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
513 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900514}
515
516/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900517int gethostbyname2_r(const char* name, int af, struct hostent* hp, char* buf, size_t buflen,
518 struct hostent** result, int* errorp) {
519 res_state res = __res_get_state();
520 if (res == NULL) {
521 *result = NULL;
522 *errorp = NETDB_INTERNAL;
523 return -1;
524 }
525 *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
526 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900527}
528
529__LIBC_HIDDEN__ FILE* android_open_proxy() {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900530 const char* cache_mode = getenv("ANDROID_DNS_MODE");
531 bool use_proxy = (cache_mode == NULL || strcmp(cache_mode, "local") != 0);
532 if (!use_proxy) {
533 return NULL;
534 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900535
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900536 int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
537 if (s == -1) {
538 return NULL;
539 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900540
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900541 const int one = 1;
542 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Bernie Innocenti55864192018-08-30 04:05:20 +0900543
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 struct sockaddr_un proxy_addr;
545 memset(&proxy_addr, 0, sizeof(proxy_addr));
546 proxy_addr.sun_family = AF_UNIX;
547 strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", sizeof(proxy_addr.sun_path));
Bernie Innocenti55864192018-08-30 04:05:20 +0900548
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900549 if (TEMP_FAILURE_RETRY(connect(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) !=
550 0) {
551 close(s);
552 return NULL;
553 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900555 return fdopen(s, "r+");
Bernie Innocenti55864192018-08-30 04:05:20 +0900556}
557
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900558static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
559 struct hostent* hp, char* buf, size_t buflen,
560 int* he) {
561 const char* cp;
562 struct getnamaddr info;
563 char hbuf[MAXHOSTNAMELEN];
564 size_t size;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900565 static const ns_dtab dtab[] = {
566 {NSSRC_FILES, _hf_gethtbyname, NULL},
567 {NSSRC_DNS, _dns_gethtbyname, NULL},
568 {0, 0, 0}
569 };
Bernie Innocenti55864192018-08-30 04:05:20 +0900570
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900571 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900572
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900573 switch (af) {
574 case AF_INET:
575 size = NS_INADDRSZ;
576 break;
577 case AF_INET6:
578 size = NS_IN6ADDRSZ;
579 break;
580 default:
581 *he = NETDB_INTERNAL;
582 errno = EAFNOSUPPORT;
583 return NULL;
584 }
585 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900586
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900587 hp->h_addrtype = af;
588 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900589
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900590 /*
591 * if there aren't any dots, it could be a user-level alias.
592 * this is also done in res_nquery() since we are not the only
593 * function that looks up host names.
594 */
595 if (!strchr(name, '.') && (cp = res_hostalias(res, name, hbuf, sizeof(hbuf)))) name = cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900596
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 /*
598 * disallow names consisting only of digits/dots, unless
599 * they end in a dot.
600 */
601 if (isdigit((u_char) name[0]))
602 for (cp = name;; ++cp) {
603 if (!*cp) {
604 if (*--cp == '.') break;
605 /*
606 * All-numeric, no dot at the end.
607 * Fake up a hostent as if we'd actually
608 * done a lookup.
609 */
610 goto fake;
611 }
612 if (!isdigit((u_char) *cp) && *cp != '.') break;
613 }
614 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':')
615 for (cp = name;; ++cp) {
616 if (!*cp) {
617 if (*--cp == '.') break;
618 /*
619 * All-IPv6-legal, no dot at the end.
620 * Fake up a hostent as if we'd actually
621 * done a lookup.
622 */
623 goto fake;
624 }
625 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
626 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900627
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900628 *he = NETDB_INTERNAL;
629 info.hp = hp;
630 info.buf = buf;
631 info.buflen = buflen;
632 info.he = he;
633 if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname", default_dns_files, name, strlen(name),
634 af) != NS_SUCCESS)
635 return NULL;
636 *he = NETDB_SUCCESS;
637 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900638nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 *he = NETDB_INTERNAL;
640 errno = ENOSPC;
641 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900642fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900643 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
644 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900645
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900646 hp->h_aliases[0] = NULL;
647 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 if (inet_pton(af, name, buf) <= 0) {
650 *he = HOST_NOT_FOUND;
651 return NULL;
652 }
653 hp->h_addr_list[0] = buf;
654 hp->h_addr_list[1] = NULL;
655 buf += size;
656 buflen -= size;
657 HENT_SCOPY(hp->h_name, name, buf, buflen);
658 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
659 *he = NETDB_SUCCESS;
660 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900661}
662
663// very similar in proxy-ness to android_getaddrinfo_proxy
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900664static struct hostent* gethostbyname_internal(const char* name, int af, res_state res,
665 struct hostent* hp, char* hbuf, size_t hbuflen,
666 int* errorp,
667 const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900668 res_setnetcontext(res, netcontext);
669 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900670}
671
672/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf,
674 size_t buflen, struct hostent** result, int* h_errnop) {
675 *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen,
676 h_errnop, &NETCONTEXT_UNSET);
677 return h_errno_to_result(h_errnop);
Bernie Innocenti55864192018-08-30 04:05:20 +0900678}
679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680static struct hostent* android_gethostbyaddrfornetcontext_real(
681 const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
682 int* he, const struct android_net_context* netcontext) {
683 const u_char* uaddr = (const u_char*) addr;
684 socklen_t size;
685 struct getnamaddr info;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900686 static const ns_dtab dtab[] = {
687 {NSSRC_FILES, _hf_gethtbyaddr, NULL},
688 {NSSRC_DNS, _dns_gethtbyaddr, NULL},
689 {0, 0, 0}
690 };
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
695 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
696 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
697 *he = HOST_NOT_FOUND;
698 return NULL;
699 }
700 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
701 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
702 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
703 /* Unmap. */
704 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
705 addr = uaddr;
706 af = AF_INET;
707 len = NS_INADDRSZ;
708 }
709 switch (af) {
710 case AF_INET:
711 size = NS_INADDRSZ;
712 break;
713 case AF_INET6:
714 size = NS_IN6ADDRSZ;
715 break;
716 default:
717 errno = EAFNOSUPPORT;
718 *he = NETDB_INTERNAL;
719 return NULL;
720 }
721 if (size != len) {
722 errno = EINVAL;
723 *he = NETDB_INTERNAL;
724 return NULL;
725 }
726 info.hp = hp;
727 info.buf = buf;
728 info.buflen = buflen;
729 info.he = he;
730 *he = NETDB_INTERNAL;
731 if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr", default_dns_files, uaddr, len, af,
732 netcontext) != NS_SUCCESS)
733 return NULL;
734 *he = NETDB_SUCCESS;
735 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900736}
737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
739 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
740 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900741 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
742 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900743}
744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900745struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
746 int* he) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900747 const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
748 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900749 char *cp, **q;
750 int af, len;
751 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900753
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900754 if (hf == NULL) {
755 *he = NETDB_INTERNAL;
756 errno = EINVAL;
757 return NULL;
758 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900759 char* p = NULL;
760 size_t maxaliases = 10;
761 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
762 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900763
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900764 /* Allocate a new space to read file lines like upstream does.
765 * To keep reentrancy we cannot use __res_get_static()->hostbuf here,
766 * as the buffer may be used to store content for a previous hostent
767 * returned by non-reentrant functions like gethostbyname().
768 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900769 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900770 goto nospc;
771 }
772 for (;;) {
773 if (!fgets(p, line_buf_size, hf)) {
774 free(p);
775 free(aliases);
776 *he = HOST_NOT_FOUND;
777 return NULL;
778 }
779 if (*p == '#') {
780 continue;
781 }
782 if (!(cp = strpbrk(p, "#\n"))) {
783 continue;
784 }
785 *cp = '\0';
786 if (!(cp = strpbrk(p, " \t"))) continue;
787 *cp++ = '\0';
788 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
789 af = AF_INET6;
790 len = NS_IN6ADDRSZ;
791 } else {
792 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900793
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900794 res_state res = __res_get_state();
795 if (res == NULL) goto nospc;
796 if (res->options & RES_USE_INET6) {
797 map_v4v6_address(buf, buf);
798 af = AF_INET6;
799 len = NS_IN6ADDRSZ;
800 } else {
801 af = AF_INET;
802 len = NS_INADDRSZ;
803 }
804 __res_put_state(res);
805 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900806
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900807 /* if this is not something we're looking for, skip it. */
808 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
809 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900810
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811 while (*cp == ' ' || *cp == '\t') cp++;
812 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
813 q = aliases;
814 while (cp && *cp) {
815 if (*cp == ' ' || *cp == '\t') {
816 cp++;
817 continue;
818 }
819 addalias(q, cp, aliases, maxaliases);
820 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
821 }
822 break;
823 }
824 hent->h_length = len;
825 hent->h_addrtype = af;
826 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
827 anum = (size_t)(q - aliases);
828 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
829 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
830 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900831
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900832 HENT_SCOPY(hent->h_name, name, buf, buflen);
833 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
834 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900835
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 *he = NETDB_SUCCESS;
837 free(p);
838 free(aliases);
839 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900840nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900841 free(p);
842 free(aliases);
843 errno = ENOSPC;
844 *he = NETDB_INTERNAL;
845 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900846}
847
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900848static void map_v4v6_address(const char* src, char* dst) {
849 u_char* p = (u_char*) dst;
850 char tmp[NS_INADDRSZ];
851 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 _DIAGASSERT(src != NULL);
854 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900855
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900856 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900857 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900858 /* Mark this ipv6 addr as a mapped ipv4. */
859 for (i = 0; i < 10; i++) *p++ = 0x00;
860 *p++ = 0xff;
861 *p++ = 0xff;
862 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900863 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900864}
865
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900866static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
867 char** ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900868
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900869 _DIAGASSERT(hp != NULL);
870 _DIAGASSERT(bpp != NULL);
871 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900873 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
874 hp->h_addrtype = AF_INET6;
875 hp->h_length = NS_IN6ADDRSZ;
876 for (ap = hp->h_addr_list; *ap; ap++) {
877 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900878
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
880 /* Out of memory. Truncate address list here. XXX */
881 *ap = NULL;
882 return;
883 }
884 *bpp += i;
885 map_v4v6_address(*ap, *bpp);
886 *ap = *bpp;
887 *bpp += NS_IN6ADDRSZ;
888 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900889}
890
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900891static void addrsort(char** ap, int num, res_state res) {
892 int i, j;
893 char** p;
894 short aval[MAXADDRS];
895 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900896
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900897 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900898
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900899 p = ap;
900 for (i = 0; i < num; i++, p++) {
901 for (j = 0; (unsigned) j < res->nsort; j++)
902 if (res->sort_list[j].addr.s_addr ==
903 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
904 break;
905 aval[i] = j;
906 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
907 }
908 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900909
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910 while (needsort < num) {
911 for (j = needsort - 1; j >= 0; j--) {
912 if (aval[j] > aval[j + 1]) {
913 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900914
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900915 i = aval[j];
916 aval[j] = aval[j + 1];
917 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900918
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900919 hp = ap[j];
920 ap[j] = ap[j + 1];
921 ap[j + 1] = hp;
922 } else
923 break;
924 }
925 needsort++;
926 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900927}
928
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900929static int _dns_gethtbyname(void* rv, void* /*cb_data*/, va_list ap) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900930 int n, type;
931 struct hostent* hp;
932 const char* name;
933 res_state res;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900934 struct getnamaddr* info = (struct getnamaddr*) rv;
Bernie Innocenti55864192018-08-30 04:05:20 +0900935
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900936 _DIAGASSERT(rv != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900937
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900938 name = va_arg(ap, char*);
939 /* NOSTRICT skip string len */ (void) va_arg(ap, int);
940 info->hp->h_addrtype = va_arg(ap, int);
Bernie Innocenti55864192018-08-30 04:05:20 +0900941
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900942 switch (info->hp->h_addrtype) {
943 case AF_INET:
944 info->hp->h_length = NS_INADDRSZ;
945 type = T_A;
946 break;
947 case AF_INET6:
948 info->hp->h_length = NS_IN6ADDRSZ;
949 type = T_AAAA;
950 break;
951 default:
952 return NS_UNAVAIL;
953 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900954 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900955 if (buf == NULL) {
956 *info->he = NETDB_INTERNAL;
957 return NS_NOTFOUND;
958 }
959 res = __res_get_state();
960 if (res == NULL) {
961 free(buf);
962 return NS_NOTFOUND;
963 }
964 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf));
965 if (n < 0) {
966 free(buf);
967 debugprintf("res_nsearch failed (%d)\n", res, n);
968 __res_put_state(res);
969 return NS_NOTFOUND;
970 }
971 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
972 free(buf);
973 __res_put_state(res);
974 if (hp == NULL) switch (*info->he) {
975 case HOST_NOT_FOUND:
976 return NS_NOTFOUND;
977 case TRY_AGAIN:
978 return NS_TRYAGAIN;
979 default:
980 return NS_UNAVAIL;
981 }
982 return NS_SUCCESS;
Bernie Innocenti55864192018-08-30 04:05:20 +0900983}
984
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900985static int _dns_gethtbyaddr(void* rv, void* /*cb_data*/, va_list ap) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900986 char qbuf[MAXDNAME + 1], *qp, *ep;
987 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900988 struct hostent* hp;
989 const unsigned char* uaddr;
990 int advance;
991 res_state res;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900992 struct getnamaddr* info = (struct getnamaddr*) rv;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900993 const struct android_net_context* netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +0900994
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900995 _DIAGASSERT(rv != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900996
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900997 uaddr = va_arg(ap, unsigned char*);
998 info->hp->h_length = va_arg(ap, int);
999 info->hp->h_addrtype = va_arg(ap, int);
1000 netcontext = va_arg(ap, const struct android_net_context*);
Bernie Innocenti55864192018-08-30 04:05:20 +09001001
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001002 switch (info->hp->h_addrtype) {
1003 case AF_INET:
1004 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
1005 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1006 break;
Bernie Innocenti55864192018-08-30 04:05:20 +09001007
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001008 case AF_INET6:
1009 qp = qbuf;
1010 ep = qbuf + sizeof(qbuf) - 1;
1011 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
1012 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
1013 ((unsigned int) uaddr[n] >> 4) & 0xf);
1014 if (advance > 0 && qp + advance < ep)
1015 qp += advance;
1016 else {
1017 *info->he = NETDB_INTERNAL;
1018 return NS_NOTFOUND;
1019 }
1020 }
1021 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
1022 *info->he = NETDB_INTERNAL;
1023 return NS_NOTFOUND;
1024 }
1025 break;
1026 default:
1027 return NS_UNAVAIL;
1028 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001029
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001030 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001031 if (buf == NULL) {
1032 *info->he = NETDB_INTERNAL;
1033 return NS_NOTFOUND;
1034 }
1035 res = __res_get_state();
1036 if (res == NULL) {
1037 free(buf);
1038 return NS_NOTFOUND;
1039 }
1040 res_setnetcontext(res, netcontext);
1041 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf));
1042 if (n < 0) {
1043 free(buf);
1044 debugprintf("res_nquery failed (%d)\n", res, n);
1045 __res_put_state(res);
1046 return NS_NOTFOUND;
1047 }
1048 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
1049 free(buf);
1050 if (hp == NULL) {
1051 __res_put_state(res);
1052 switch (*info->he) {
1053 case HOST_NOT_FOUND:
1054 return NS_NOTFOUND;
1055 case TRY_AGAIN:
1056 return NS_TRYAGAIN;
1057 default:
1058 return NS_UNAVAIL;
1059 }
1060 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001061
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001062 char* bf = (char*) (hp->h_addr_list + 2);
1063 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001064 if (blen + info->hp->h_length > info->buflen) goto nospc;
1065 hp->h_addr_list[0] = bf;
1066 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001067 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001068 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
1069 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
1070 map_v4v6_address(bf, bf);
1071 hp->h_addrtype = AF_INET6;
1072 hp->h_length = NS_IN6ADDRSZ;
1073 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001074
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001075 __res_put_state(res);
1076 *info->he = NETDB_SUCCESS;
1077 return NS_SUCCESS;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001078
Bernie Innocenti55864192018-08-30 04:05:20 +09001079nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001080 errno = ENOSPC;
1081 *info->he = NETDB_INTERNAL;
1082 return NS_UNAVAIL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001083}
1084
Bernie Innocenti55864192018-08-30 04:05:20 +09001085/*
1086 * Non-reentrant versions.
1087 */
1088
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001089struct hostent* gethostbyname(const char* name) {
1090 struct hostent* result = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001091 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001092
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001093 gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1094 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001095}
1096
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001097struct hostent* gethostbyname2(const char* name, int af) {
1098 struct hostent* result = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001099 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001100
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001101 gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1102 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001103}
1104
1105// android_gethostby*fornet can be called in two different contexts.
1106// - In the proxy client context (proxy != NULL), |netid| is |app_netid|.
1107// - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|.
1108// The netcontext is constructed before checking which context we are in.
1109// Therefore, we have to populate both fields, and rely on the downstream code to check whether
1110// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
1111static struct android_net_context make_context(unsigned netid, unsigned mark) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001112 struct android_net_context netcontext = NETCONTEXT_UNSET;
1113 netcontext.app_netid = netid;
1114 netcontext.app_mark = mark;
1115 netcontext.dns_netid = netid;
1116 netcontext.dns_mark = mark;
1117 return netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001118}
1119
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001120struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid,
1121 unsigned mark) {
1122 const struct android_net_context netcontext = make_context(netid, mark);
1123 return android_gethostbynamefornetcontext(name, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001124}
1125
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001126struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
1127 const struct android_net_context* netcontext) {
1128 struct hostent* hp;
1129 res_state res = __res_get_state();
1130 if (res == NULL) return NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001131 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001132 hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
1133 &h_errno, netcontext);
1134 __res_put_state(res);
1135 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +09001136}
1137
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001138struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) {
1139 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
Bernie Innocenti55864192018-08-30 04:05:20 +09001140}
1141
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001142struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid,
1143 unsigned mark) {
1144 const struct android_net_context netcontext = make_context(netid, mark);
1145 return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001146}
1147
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001148struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1149 const struct android_net_context* netcontext) {
1150 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001151}
1152
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001153__LIBC_HIDDEN__ struct hostent* android_gethostbyaddrfornetcontext_proxy(
1154 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001155 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001156 return android_gethostbyaddrfornetcontext_proxy_internal(
1157 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001158}
1159
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001160struct hostent* gethostent(void) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001161 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001162 if (!rs->hostf) {
1163 sethostent_r(&rs->hostf);
1164 if (!rs->hostf) {
1165 h_errno = NETDB_INTERNAL;
1166 return NULL;
1167 }
1168 }
1169 memset(&rs->host, 0, sizeof(rs->host));
1170 return netbsd_gethostent_r(rs->hostf, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001171}