blob: 017d06d544eebc5cc7f837a0583621ad88c3496a [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>
nuccachen0a511732018-09-18 13:38:48 +080071#include <functional>
Bernie Innocentif89b3512018-08-30 07:34:37 +090072
Bernie Innocenti189eb502018-10-01 23:10:18 +090073#include "netd_resolv/resolv.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090074#include "resolv_cache.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090075#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090076
Bernie Innocentif89b3512018-08-30 07:34:37 +090077// NetBSD uses _DIAGASSERT to null-check arguments and the like,
78// but it's clear from the number of mistakes in their assertions
79// that they don't actually test or ship with this.
80#define _DIAGASSERT(e) /* nothing */
81
nuccachen8161c992018-09-11 11:20:00 +080082// TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent
83// buffer.
Bernie Innocenti55864192018-08-30 04:05:20 +090084#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090086
87#ifndef LOG_AUTH
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#define LOG_AUTH 0
Bernie Innocenti55864192018-08-30 04:05:20 +090089#endif
90
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090091#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
Bernie Innocenti55864192018-08-30 04:05:20 +090092
Bernie Innocenti55864192018-08-30 04:05:20 +090093#include <stdlib.h>
94#include <string.h>
95
96#include "hostent.h"
97
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090098#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0)
Bernie Innocenti55864192018-08-30 04:05:20 +090099#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
100#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
101
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900102#define addalias(d, s, arr, siz) \
103 do { \
104 if (d >= &arr[siz]) { \
105 char** xptr = (char**) realloc(arr, (siz + 10) * sizeof(*arr)); \
106 if (xptr == NULL) goto nospc; \
107 d = xptr + (d - arr); \
108 arr = xptr; \
109 siz += 10; \
110 } \
111 *d++ = s; \
112 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900113
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900115
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900116static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET,
117 .app_netid = NETID_UNSET,
118 .dns_mark = MARK_UNSET,
119 .dns_netid = NETID_UNSET,
120 .uid = NET_CONTEXT_INVALID_UID};
Bernie Innocenti55864192018-08-30 04:05:20 +0900121
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900122#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900123
124typedef union {
125 HEADER hdr;
126 u_char buf[MAXPACKET];
127} querybuf;
128
129typedef union {
130 int32_t al;
131 char ac;
132} align;
133
134#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900135static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900136#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900137static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
138 char*, size_t, int*);
nuccachen0a511732018-09-18 13:38:48 +0800139static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
140 std::function<void(struct hostent* hp)> mapping_param,
141 std::function<void(char* src, char* dst)> mapping_addr);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142static void map_v4v6_address(const char*, char*);
143static void map_v4v6_hostent(struct hostent*, char**, char*);
nuccachen0a511732018-09-18 13:38:48 +0800144static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900146
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147struct hostent* ht_gethostbyname(char*);
148struct hostent* ht_gethostbyaddr(const char*, int, int);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900149static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
150 const android_net_context* netcontext, getnamaddr* info);
151static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900152
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
154 size_t, int*, const struct android_net_context*);
155static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
156 const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
157 const struct android_net_context*);
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +0900158static struct hostent* android_gethostbyaddrfornetcontext_proxy(
159 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900160
Bernie Innocenti55864192018-08-30 04:05:20 +0900161static int h_errno_to_result(int* herrno_p) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162 // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
163 if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
164 errno = ERANGE;
165 return errno;
166 }
167 // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
168 return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900169}
170
171#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900172static void debugprintf(const char* msg, res_state res, ...) {
173 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900174
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900175 if (res->options & RES_DEBUG) {
176 int save = errno;
177 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900178
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900179 va_start(ap, res);
180 vprintf(msg, ap);
181 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183 errno = save;
184 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900185}
186#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900187#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900188#endif
189
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190#define BOUNDED_INCR(x) \
191 do { \
192 BOUNDS_CHECK(cp, x); \
193 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900194 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900195
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196#define BOUNDS_CHECK(ptr, count) \
197 do { \
198 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900199 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900200
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
202 res_state res, struct hostent* hent, char* buf, size_t buflen,
203 int* he) {
204 const HEADER* hp;
205 const u_char* cp;
206 int n;
207 size_t qlen;
208 const u_char *eom, *erdata;
209 char *bp, **ap, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900210 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900211 int haveanswer, had_error;
212 int toobig = 0;
213 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214 char* addr_ptrs[MAXADDRS];
215 const char* tname;
216 int (*name_ok)(const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900217
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900218 _DIAGASSERT(answer != NULL);
219 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900220
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900221 tname = qname;
222 hent->h_name = NULL;
223 eom = answer->buf + anslen;
224 switch (qtype) {
225 case T_A:
226 case T_AAAA:
227 name_ok = res_hnok;
228 break;
229 case T_PTR:
230 name_ok = res_dnok;
231 break;
232 default:
233 *he = NO_RECOVERY;
234 return NULL; /* XXX should be abort(); */
235 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900236
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900237 size_t maxaliases = 10;
238 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
239 if (!aliases) goto nospc;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900240 /*
241 * find first satisfactory answer
242 */
243 hp = &answer->hdr;
244 ancount = ntohs(hp->ancount);
245 qdcount = ntohs(hp->qdcount);
246 bp = buf;
247 ep = buf + buflen;
248 cp = answer->buf;
249 BOUNDED_INCR(HFIXEDSZ);
250 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900251
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900252 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
253 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900254
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900255 BOUNDED_INCR(n + QFIXEDSZ);
256 if (qtype == T_A || qtype == T_AAAA) {
257 /* res_send() has already verified that the query name is the
258 * same as the one we sent; this just gets the expanded name
259 * (i.e., with the succeeding search-domain tacked on).
260 */
261 n = (int) strlen(bp) + 1; /* for the \0 */
262 if (n >= MAXHOSTNAMELEN) goto no_recovery;
263 hent->h_name = bp;
264 bp += n;
265 /* The qname can be abbreviated, but h_name is now absolute. */
266 qname = hent->h_name;
267 }
268 hent->h_aliases = ap = aliases;
269 hent->h_addr_list = hap = addr_ptrs;
270 *ap = NULL;
271 *hap = NULL;
272 haveanswer = 0;
273 had_error = 0;
274 while (ancount-- > 0 && cp < eom && !had_error) {
275 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
276 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
277 had_error++;
278 continue;
279 }
280 cp += n; /* name */
281 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900282 int type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900283 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900284 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900285 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900286 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900287 cp += INT16SZ; /* len */
288 BOUNDS_CHECK(cp, n);
289 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900290 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900291 /* XXX - debug? syslog? */
292 cp += n;
293 continue; /* XXX - had_error++ ? */
294 }
295 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
296 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
297 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
298 had_error++;
299 continue;
300 }
301 cp += n;
302 if (cp != erdata) goto no_recovery;
303 /* Store alias. */
304 addalias(ap, bp, aliases, maxaliases);
305 n = (int) strlen(bp) + 1; /* for the \0 */
306 if (n >= MAXHOSTNAMELEN) {
307 had_error++;
308 continue;
309 }
310 bp += n;
311 /* Get canonical name. */
312 n = (int) strlen(tbuf) + 1; /* for the \0 */
313 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
314 had_error++;
315 continue;
316 }
317 strlcpy(bp, tbuf, (size_t)(ep - bp));
318 hent->h_name = bp;
319 bp += n;
320 continue;
321 }
322 if (qtype == T_PTR && type == T_CNAME) {
323 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
324 if (n < 0 || !maybe_dnok(res, tbuf)) {
325 had_error++;
326 continue;
327 }
328 cp += n;
329 if (cp != erdata) goto no_recovery;
330 /* Get canonical name. */
331 n = (int) strlen(tbuf) + 1; /* for the \0 */
332 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
333 had_error++;
334 continue;
335 }
336 strlcpy(bp, tbuf, (size_t)(ep - bp));
337 tname = bp;
338 bp += n;
339 continue;
340 }
341 if (type != qtype) {
342 if (type != T_KEY && type != T_SIG)
343 syslog(LOG_NOTICE | LOG_AUTH,
344 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
345 p_class(C_IN), p_type(qtype), p_type(type));
346 cp += n;
347 continue; /* XXX - had_error++ ? */
348 }
349 switch (type) {
350 case T_PTR:
351 if (strcasecmp(tname, bp) != 0) {
352 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
353 cp += n;
354 continue; /* XXX - had_error++ ? */
355 }
356 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
357 if ((n < 0) || !maybe_hnok(res, bp)) {
358 had_error++;
359 break;
360 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900361#if MULTI_PTRS_ARE_ALIASES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900362 cp += n;
363 if (cp != erdata) goto no_recovery;
364 if (!haveanswer)
365 hent->h_name = bp;
366 else
367 addalias(ap, bp, aliases, maxaliases);
368 if (n != -1) {
369 n = (int) strlen(bp) + 1; /* for the \0 */
370 if (n >= MAXHOSTNAMELEN) {
371 had_error++;
372 break;
373 }
374 bp += n;
375 }
376 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900377#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900378 hent->h_name = bp;
379 if (res->options & RES_USE_INET6) {
380 n = strlen(bp) + 1; /* for the \0 */
381 if (n >= MAXHOSTNAMELEN) {
382 had_error++;
383 break;
384 }
385 bp += n;
386 map_v4v6_hostent(hent, &bp, ep);
387 }
388 goto success;
Bernie Innocenti55864192018-08-30 04:05:20 +0900389#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900390 case T_A:
391 case T_AAAA:
392 if (strcasecmp(hent->h_name, bp) != 0) {
393 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
394 cp += n;
395 continue; /* XXX - had_error++ ? */
396 }
397 if (n != hent->h_length) {
398 cp += n;
399 continue;
400 }
401 if (type == T_AAAA) {
402 struct in6_addr in6;
403 memcpy(&in6, cp, NS_IN6ADDRSZ);
404 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
405 cp += n;
406 continue;
407 }
408 }
409 if (!haveanswer) {
410 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900411
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900412 hent->h_name = bp;
413 nn = (int) strlen(bp) + 1; /* for the \0 */
414 bp += nn;
415 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900416
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900417 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 if (bp + n >= ep) {
420 debugprintf("size (%d) too big\n", res, n);
421 had_error++;
422 continue;
423 }
424 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
425 if (!toobig++) {
426 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
427 }
428 cp += n;
429 continue;
430 }
431 (void) memcpy(*hap++ = bp, cp, (size_t) n);
432 bp += n;
433 cp += n;
434 if (cp != erdata) goto no_recovery;
435 break;
436 default:
437 abort();
438 }
439 if (!had_error) haveanswer++;
440 }
441 if (haveanswer) {
442 *ap = NULL;
443 *hap = NULL;
444 /*
445 * Note: we sort even if host can take only one address
446 * in its return structures - should give it the "best"
447 * address in that case, not some random one
448 */
449 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
450 if (!hent->h_name) {
451 n = (int) strlen(qname) + 1; /* for the \0 */
452 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
453 strlcpy(bp, qname, (size_t)(ep - bp));
454 hent->h_name = bp;
455 bp += n;
456 }
457 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
nuccachen0a511732018-09-18 13:38:48 +0800458 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 goto success;
460 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900461no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900462 free(aliases);
463 *he = NO_RECOVERY;
464 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900465success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 bp = (char*) ALIGN(bp);
467 n = (int) (ap - aliases);
468 qlen = (n + 1) * sizeof(*hent->h_aliases);
469 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900470 hent->h_aliases = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900471 memcpy(bp, aliases, qlen);
472 free(aliases);
473 aliases = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900474
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 bp += qlen;
476 n = (int) (hap - addr_ptrs);
477 qlen = (n + 1) * sizeof(*hent->h_addr_list);
478 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900479 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900480 memcpy(bp, addr_ptrs, qlen);
481 *he = NETDB_SUCCESS;
482 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900483nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900484 free(aliases);
485 errno = ENOSPC;
486 *he = NETDB_INTERNAL;
487 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900488}
489
490/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900491int gethostbyname_r(const char* name, struct hostent* hp, char* buf, size_t buflen,
492 struct hostent** result, int* errorp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900493 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900494 if (res == NULL) {
495 *result = NULL;
496 *errorp = NETDB_INTERNAL;
497 return -1;
498 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900499
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900501
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900502 if (res->options & RES_USE_INET6) {
503 *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
504 &NETCONTEXT_UNSET);
505 if (*result) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900506 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900507 return 0;
508 }
509 }
510 *result =
511 gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
512 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900513}
514
515/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900516int gethostbyname2_r(const char* name, int af, struct hostent* hp, char* buf, size_t buflen,
517 struct hostent** result, int* errorp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900518 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900519 if (res == NULL) {
520 *result = NULL;
521 *errorp = NETDB_INTERNAL;
522 return -1;
523 }
524 *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
525 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900526}
527
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900528static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
529 struct hostent* hp, char* buf, size_t buflen,
530 int* he) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900531 struct getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900532 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900533
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900534 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900535
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900536 switch (af) {
537 case AF_INET:
538 size = NS_INADDRSZ;
539 break;
540 case AF_INET6:
541 size = NS_IN6ADDRSZ;
542 break;
543 default:
544 *he = NETDB_INTERNAL;
545 errno = EAFNOSUPPORT;
546 return NULL;
547 }
548 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900549
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900550 hp->h_addrtype = af;
551 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900552
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900553 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900554 * disallow names consisting only of digits/dots, unless
555 * they end in a dot.
556 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900557 if (isdigit((u_char) name[0])) {
558 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900559 if (!*cp) {
560 if (*--cp == '.') break;
561 /*
562 * All-numeric, no dot at the end.
563 * Fake up a hostent as if we'd actually
564 * done a lookup.
565 */
566 goto fake;
567 }
568 if (!isdigit((u_char) *cp) && *cp != '.') break;
569 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900570 }
571 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
572 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900573 if (!*cp) {
574 if (*--cp == '.') break;
575 /*
576 * All-IPv6-legal, no dot at the end.
577 * Fake up a hostent as if we'd actually
578 * done a lookup.
579 */
580 goto fake;
581 }
582 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
583 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900584 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900585
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900586 *he = NETDB_INTERNAL;
587 info.hp = hp;
588 info.buf = buf;
589 info.buflen = buflen;
590 info.he = he;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900591 if (!_hf_gethtbyname2(name, af, &info)) {
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900592 if (!_dns_gethtbyname(name, af, &info)) {
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900593 return NULL;
594 }
595 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900596 *he = NETDB_SUCCESS;
597 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900598nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900599 *he = NETDB_INTERNAL;
600 errno = ENOSPC;
601 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900602fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900603 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
604 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900606 hp->h_aliases[0] = NULL;
607 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900608
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900609 if (inet_pton(af, name, buf) <= 0) {
610 *he = HOST_NOT_FOUND;
611 return NULL;
612 }
613 hp->h_addr_list[0] = buf;
614 hp->h_addr_list[1] = NULL;
615 buf += size;
616 buflen -= size;
617 HENT_SCOPY(hp->h_name, name, buf, buflen);
618 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
619 *he = NETDB_SUCCESS;
620 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900621}
622
623// very similar in proxy-ness to android_getaddrinfo_proxy
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900624static struct hostent* gethostbyname_internal(const char* name, int af, res_state res,
625 struct hostent* hp, char* hbuf, size_t hbuflen,
626 int* errorp,
627 const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900628 res_setnetcontext(res, netcontext);
629 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900630}
631
632/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900633int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf,
634 size_t buflen, struct hostent** result, int* h_errnop) {
635 *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen,
636 h_errnop, &NETCONTEXT_UNSET);
637 return h_errno_to_result(h_errnop);
Bernie Innocenti55864192018-08-30 04:05:20 +0900638}
639
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900640static struct hostent* android_gethostbyaddrfornetcontext_real(
641 const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
642 int* he, const struct android_net_context* netcontext) {
643 const u_char* uaddr = (const u_char*) addr;
644 socklen_t size;
645 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900646
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900647 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
650 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
651 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
652 *he = HOST_NOT_FOUND;
653 return NULL;
654 }
655 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
656 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
657 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
658 /* Unmap. */
659 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
660 addr = uaddr;
661 af = AF_INET;
662 len = NS_INADDRSZ;
663 }
664 switch (af) {
665 case AF_INET:
666 size = NS_INADDRSZ;
667 break;
668 case AF_INET6:
669 size = NS_IN6ADDRSZ;
670 break;
671 default:
672 errno = EAFNOSUPPORT;
673 *he = NETDB_INTERNAL;
674 return NULL;
675 }
676 if (size != len) {
677 errno = EINVAL;
678 *he = NETDB_INTERNAL;
679 return NULL;
680 }
681 info.hp = hp;
682 info.buf = buf;
683 info.buflen = buflen;
684 info.he = he;
685 *he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900686 if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
687 if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) {
688 return NULL;
689 }
690 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 *he = NETDB_SUCCESS;
692 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900693}
694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
696 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
697 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900698 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
699 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900700}
701
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900702struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
703 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900704 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900705 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900706 char *cp, **q;
707 int af, len;
708 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900709 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900710
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900711 if (hf == NULL) {
712 *he = NETDB_INTERNAL;
713 errno = EINVAL;
714 return NULL;
715 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900716 char* p = NULL;
717 size_t maxaliases = 10;
718 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
719 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900720
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900722 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900723 * as the buffer may be used to store content for a previous hostent
724 * returned by non-reentrant functions like gethostbyname().
725 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900726 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900727 goto nospc;
728 }
729 for (;;) {
730 if (!fgets(p, line_buf_size, hf)) {
731 free(p);
732 free(aliases);
733 *he = HOST_NOT_FOUND;
734 return NULL;
735 }
736 if (*p == '#') {
737 continue;
738 }
739 if (!(cp = strpbrk(p, "#\n"))) {
740 continue;
741 }
742 *cp = '\0';
743 if (!(cp = strpbrk(p, " \t"))) continue;
744 *cp++ = '\0';
745 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
746 af = AF_INET6;
747 len = NS_IN6ADDRSZ;
748 } else {
749 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900750
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900751 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 if (res == NULL) goto nospc;
753 if (res->options & RES_USE_INET6) {
754 map_v4v6_address(buf, buf);
755 af = AF_INET6;
756 len = NS_IN6ADDRSZ;
757 } else {
758 af = AF_INET;
759 len = NS_INADDRSZ;
760 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900761 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900763
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900764 /* if this is not something we're looking for, skip it. */
765 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
766 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 while (*cp == ' ' || *cp == '\t') cp++;
769 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
770 q = aliases;
771 while (cp && *cp) {
772 if (*cp == ' ' || *cp == '\t') {
773 cp++;
774 continue;
775 }
776 addalias(q, cp, aliases, maxaliases);
777 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
778 }
779 break;
780 }
781 hent->h_length = len;
782 hent->h_addrtype = af;
783 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
784 anum = (size_t)(q - aliases);
785 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
786 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
787 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900788
nuccachen8161c992018-09-11 11:20:00 +0800789 /* Reserve space for mapping IPv4 address to IPv6 address in place */
790 if (hent->h_addrtype == AF_INET) {
791 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
792 }
793
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900794 HENT_SCOPY(hent->h_name, name, buf, buflen);
795 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
796 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900797
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900798 *he = NETDB_SUCCESS;
799 free(p);
800 free(aliases);
801 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900802nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900803 free(p);
804 free(aliases);
805 errno = ENOSPC;
806 *he = NETDB_INTERNAL;
807 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900808}
809
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900810static void map_v4v6_address(const char* src, char* dst) {
811 u_char* p = (u_char*) dst;
812 char tmp[NS_INADDRSZ];
813 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900815 _DIAGASSERT(src != NULL);
816 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900818 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900819 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900820 /* Mark this ipv6 addr as a mapped ipv4. */
821 for (i = 0; i < 10; i++) *p++ = 0x00;
822 *p++ = 0xff;
823 *p++ = 0xff;
824 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900825 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900826}
827
nuccachen0a511732018-09-18 13:38:48 +0800828static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
829 std::function<void(struct hostent* hp)> map_param,
830 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 _DIAGASSERT(hp != NULL);
832 _DIAGASSERT(bpp != NULL);
833 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900834
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900835 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800836 map_param(hp);
837 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900838 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
841 /* Out of memory. Truncate address list here. XXX */
842 *ap = NULL;
843 return;
844 }
845 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800846 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 *ap = *bpp;
848 *bpp += NS_IN6ADDRSZ;
849 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900850}
851
nuccachen0a511732018-09-18 13:38:48 +0800852static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
853 convert_v4v6_hostent(hp, bpp, ep,
854 [](struct hostent* hp) {
855 hp->h_addrtype = AF_INET6;
856 hp->h_length = NS_IN6ADDRSZ;
857 },
858 [](char* src, char* dst) { map_v4v6_address(src, dst); });
859}
860
861/* Reserve space for mapping IPv4 address to IPv6 address in place */
862static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
863 convert_v4v6_hostent(hp, bpp, ep,
864 [](struct hostent* hp) {
865 (void) hp; /* unused */
866 },
867 [](char* src, char* dst) {
868 memcpy(dst, src, NS_INADDRSZ);
869 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
870 });
871}
872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900873static void addrsort(char** ap, int num, res_state res) {
874 int i, j;
875 char** p;
876 short aval[MAXADDRS];
877 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900878
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900880
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900881 p = ap;
882 for (i = 0; i < num; i++, p++) {
883 for (j = 0; (unsigned) j < res->nsort; j++)
884 if (res->sort_list[j].addr.s_addr ==
885 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
886 break;
887 aval[i] = j;
888 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
889 }
890 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900891
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900892 while (needsort < num) {
893 for (j = needsort - 1; j >= 0; j--) {
894 if (aval[j] > aval[j + 1]) {
895 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900896
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900897 i = aval[j];
898 aval[j] = aval[j + 1];
899 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900900
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900901 hp = ap[j];
902 ap[j] = ap[j + 1];
903 ap[j + 1] = hp;
904 } else
905 break;
906 }
907 needsort++;
908 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900909}
910
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900911static bool _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912 int n, type;
913 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900915
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900916 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900917
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900918 switch (info->hp->h_addrtype) {
919 case AF_INET:
920 info->hp->h_length = NS_INADDRSZ;
921 type = T_A;
922 break;
923 case AF_INET6:
924 info->hp->h_length = NS_IN6ADDRSZ;
925 type = T_AAAA;
926 break;
927 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900928 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900929 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900930 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900931 if (buf == NULL) {
932 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900933 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900934 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900935 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900936 if (res == NULL) {
937 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900938 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900939 }
940 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf));
941 if (n < 0) {
942 free(buf);
943 debugprintf("res_nsearch failed (%d)\n", res, n);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900944 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900945 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900946 }
947 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
948 free(buf);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900949 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900950 if (hp == NULL) {
951 return false;
952 }
953 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900954}
955
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900956static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
957 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900958 char qbuf[MAXDNAME + 1], *qp, *ep;
959 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900960 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900961 int advance;
962 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900963
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900964 info->hp->h_length = len;
965 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900966
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 switch (info->hp->h_addrtype) {
968 case AF_INET:
969 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
970 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
971 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900972
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900973 case AF_INET6:
974 qp = qbuf;
975 ep = qbuf + sizeof(qbuf) - 1;
976 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
977 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
978 ((unsigned int) uaddr[n] >> 4) & 0xf);
979 if (advance > 0 && qp + advance < ep)
980 qp += advance;
981 else {
982 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900983 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900984 }
985 }
986 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
987 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900988 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900989 }
990 break;
991 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900992 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900993 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900994
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900995 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900996 if (buf == NULL) {
997 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900998 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900999 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001000 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001001 if (res == NULL) {
1002 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001003 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001004 }
1005 res_setnetcontext(res, netcontext);
1006 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf));
1007 if (n < 0) {
1008 free(buf);
1009 debugprintf("res_nquery failed (%d)\n", res, n);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001010 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001011 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001012 }
1013 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
1014 free(buf);
1015 if (hp == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001016 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001017 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001018 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001019
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001020 char* bf = (char*) (hp->h_addr_list + 2);
1021 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001022 if (blen + info->hp->h_length > info->buflen) goto nospc;
1023 hp->h_addr_list[0] = bf;
1024 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001025 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001026 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
1027 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
1028 map_v4v6_address(bf, bf);
1029 hp->h_addrtype = AF_INET6;
1030 hp->h_length = NS_IN6ADDRSZ;
1031 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001032
nuccachen8161c992018-09-11 11:20:00 +08001033 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
1034 if (info->hp->h_addrtype == AF_INET) {
1035 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
1036 // Pad zero to the unused address space
1037 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
1038 }
1039
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001040 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001041 *info->he = NETDB_SUCCESS;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001042 return true;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001043
Bernie Innocenti55864192018-08-30 04:05:20 +09001044nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001045 errno = ENOSPC;
1046 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001047 return false;
Bernie Innocenti55864192018-08-30 04:05:20 +09001048}
1049
Bernie Innocenti55864192018-08-30 04:05:20 +09001050/*
1051 * Non-reentrant versions.
1052 */
1053
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001054struct hostent* gethostbyname(const char* name) {
1055 struct hostent* result = NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001056 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001057
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001058 gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1059 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001060}
1061
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001062struct hostent* gethostbyname2(const char* name, int af) {
1063 struct hostent* result = NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001064 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001065
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001066 gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1067 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001068}
1069
1070// android_gethostby*fornet can be called in two different contexts.
1071// - In the proxy client context (proxy != NULL), |netid| is |app_netid|.
1072// - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|.
1073// The netcontext is constructed before checking which context we are in.
1074// Therefore, we have to populate both fields, and rely on the downstream code to check whether
1075// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
1076static struct android_net_context make_context(unsigned netid, unsigned mark) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001077 struct android_net_context netcontext = NETCONTEXT_UNSET;
1078 netcontext.app_netid = netid;
1079 netcontext.app_mark = mark;
1080 netcontext.dns_netid = netid;
1081 netcontext.dns_mark = mark;
1082 return netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001083}
1084
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001085struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid,
1086 unsigned mark) {
1087 const struct android_net_context netcontext = make_context(netid, mark);
1088 return android_gethostbynamefornetcontext(name, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001089}
1090
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001091struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
1092 const struct android_net_context* netcontext) {
1093 struct hostent* hp;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001094 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001095 if (res == NULL) return NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001096 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001097 hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
1098 &h_errno, netcontext);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001099 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001100 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +09001101}
1102
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001103struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) {
1104 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
Bernie Innocenti55864192018-08-30 04:05:20 +09001105}
1106
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001107struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid,
1108 unsigned mark) {
1109 const struct android_net_context netcontext = make_context(netid, mark);
1110 return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001111}
1112
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001113struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1114 const struct android_net_context* netcontext) {
1115 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001116}
1117
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +09001118static struct hostent* android_gethostbyaddrfornetcontext_proxy(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001119 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001120 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001121 return android_gethostbyaddrfornetcontext_proxy_internal(
1122 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001123}