blob: ac6a525d2bbee9844cf9637d46bd64eec8c79bc9 [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 Innocenti189eb502018-10-01 23:10:18 +090072#include "netd_resolv/resolv.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090073#include "resolv_cache.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090074#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
nuccachen8161c992018-09-11 11:20:00 +080081// TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent
82// buffer.
Bernie Innocenti55864192018-08-30 04:05:20 +090083#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090084#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090085
86#ifndef LOG_AUTH
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090087#define LOG_AUTH 0
Bernie Innocenti55864192018-08-30 04:05:20 +090088#endif
89
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090090#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
Bernie Innocenti55864192018-08-30 04:05:20 +090091
Bernie Innocenti55864192018-08-30 04:05:20 +090092#include <stdlib.h>
93#include <string.h>
94
95#include "hostent.h"
96
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090097#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0)
Bernie Innocenti55864192018-08-30 04:05:20 +090098#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
99#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
100
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900101#define addalias(d, s, arr, siz) \
102 do { \
103 if (d >= &arr[siz]) { \
104 char** xptr = (char**) realloc(arr, (siz + 10) * sizeof(*arr)); \
105 if (xptr == NULL) goto nospc; \
106 d = xptr + (d - arr); \
107 arr = xptr; \
108 siz += 10; \
109 } \
110 *d++ = s; \
111 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900112
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900113static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900114
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900115static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET,
116 .app_netid = NETID_UNSET,
117 .dns_mark = MARK_UNSET,
118 .dns_netid = NETID_UNSET,
119 .uid = NET_CONTEXT_INVALID_UID};
Bernie Innocenti55864192018-08-30 04:05:20 +0900120
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900122
123typedef union {
124 HEADER hdr;
125 u_char buf[MAXPACKET];
126} querybuf;
127
128typedef union {
129 int32_t al;
130 char ac;
131} align;
132
133#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900134static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900135#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900136static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
137 char*, size_t, int*);
138static void map_v4v6_address(const char*, char*);
139static void map_v4v6_hostent(struct hostent*, char**, char*);
140static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900141
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142struct hostent* ht_gethostbyname(char*);
143struct hostent* ht_gethostbyaddr(const char*, int, int);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900144static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
145 const android_net_context* netcontext, getnamaddr* info);
146static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900147
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900148static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
149 size_t, int*, const struct android_net_context*);
150static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
151 const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
152 const struct android_net_context*);
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +0900153static struct hostent* android_gethostbyaddrfornetcontext_proxy(
154 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900155
Bernie Innocenti55864192018-08-30 04:05:20 +0900156static int h_errno_to_result(int* herrno_p) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157 // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
158 if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
159 errno = ERANGE;
160 return errno;
161 }
162 // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
163 return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900164}
165
166#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900167static void debugprintf(const char* msg, res_state res, ...) {
168 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900169
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900170 if (res->options & RES_DEBUG) {
171 int save = errno;
172 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900173
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900174 va_start(ap, res);
175 vprintf(msg, ap);
176 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900177
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178 errno = save;
179 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900180}
181#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900182#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900183#endif
184
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900185#define BOUNDED_INCR(x) \
186 do { \
187 BOUNDS_CHECK(cp, x); \
188 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900189 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900191#define BOUNDS_CHECK(ptr, count) \
192 do { \
193 if (eom - (ptr) < (count)) goto no_recovery; \
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 +0900196static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
197 res_state res, struct hostent* hent, char* buf, size_t buflen,
198 int* he) {
199 const HEADER* hp;
200 const u_char* cp;
201 int n;
202 size_t qlen;
203 const u_char *eom, *erdata;
204 char *bp, **ap, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900205 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900206 int haveanswer, had_error;
207 int toobig = 0;
208 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900209 char* addr_ptrs[MAXADDRS];
210 const char* tname;
211 int (*name_ok)(const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900212
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900213 _DIAGASSERT(answer != NULL);
214 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900215
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900216 tname = qname;
217 hent->h_name = NULL;
218 eom = answer->buf + anslen;
219 switch (qtype) {
220 case T_A:
221 case T_AAAA:
222 name_ok = res_hnok;
223 break;
224 case T_PTR:
225 name_ok = res_dnok;
226 break;
227 default:
228 *he = NO_RECOVERY;
229 return NULL; /* XXX should be abort(); */
230 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900231
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900232 size_t maxaliases = 10;
233 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
234 if (!aliases) goto nospc;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900235 /*
236 * find first satisfactory answer
237 */
238 hp = &answer->hdr;
239 ancount = ntohs(hp->ancount);
240 qdcount = ntohs(hp->qdcount);
241 bp = buf;
242 ep = buf + buflen;
243 cp = answer->buf;
244 BOUNDED_INCR(HFIXEDSZ);
245 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900246
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
248 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900249
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900250 BOUNDED_INCR(n + QFIXEDSZ);
251 if (qtype == T_A || qtype == T_AAAA) {
252 /* res_send() has already verified that the query name is the
253 * same as the one we sent; this just gets the expanded name
254 * (i.e., with the succeeding search-domain tacked on).
255 */
256 n = (int) strlen(bp) + 1; /* for the \0 */
257 if (n >= MAXHOSTNAMELEN) goto no_recovery;
258 hent->h_name = bp;
259 bp += n;
260 /* The qname can be abbreviated, but h_name is now absolute. */
261 qname = hent->h_name;
262 }
263 hent->h_aliases = ap = aliases;
264 hent->h_addr_list = hap = addr_ptrs;
265 *ap = NULL;
266 *hap = NULL;
267 haveanswer = 0;
268 had_error = 0;
269 while (ancount-- > 0 && cp < eom && !had_error) {
270 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
271 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
272 had_error++;
273 continue;
274 }
275 cp += n; /* name */
276 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900277 int type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900278 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900279 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900280 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900281 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900282 cp += INT16SZ; /* len */
283 BOUNDS_CHECK(cp, n);
284 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900285 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900286 /* XXX - debug? syslog? */
287 cp += n;
288 continue; /* XXX - had_error++ ? */
289 }
290 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
291 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
292 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
293 had_error++;
294 continue;
295 }
296 cp += n;
297 if (cp != erdata) goto no_recovery;
298 /* Store alias. */
299 addalias(ap, bp, aliases, maxaliases);
300 n = (int) strlen(bp) + 1; /* for the \0 */
301 if (n >= MAXHOSTNAMELEN) {
302 had_error++;
303 continue;
304 }
305 bp += n;
306 /* Get canonical name. */
307 n = (int) strlen(tbuf) + 1; /* for the \0 */
308 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
309 had_error++;
310 continue;
311 }
312 strlcpy(bp, tbuf, (size_t)(ep - bp));
313 hent->h_name = bp;
314 bp += n;
315 continue;
316 }
317 if (qtype == T_PTR && type == T_CNAME) {
318 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
319 if (n < 0 || !maybe_dnok(res, tbuf)) {
320 had_error++;
321 continue;
322 }
323 cp += n;
324 if (cp != erdata) goto no_recovery;
325 /* Get canonical name. */
326 n = (int) strlen(tbuf) + 1; /* for the \0 */
327 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
328 had_error++;
329 continue;
330 }
331 strlcpy(bp, tbuf, (size_t)(ep - bp));
332 tname = bp;
333 bp += n;
334 continue;
335 }
336 if (type != qtype) {
337 if (type != T_KEY && type != T_SIG)
338 syslog(LOG_NOTICE | LOG_AUTH,
339 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
340 p_class(C_IN), p_type(qtype), p_type(type));
341 cp += n;
342 continue; /* XXX - had_error++ ? */
343 }
344 switch (type) {
345 case T_PTR:
346 if (strcasecmp(tname, bp) != 0) {
347 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
348 cp += n;
349 continue; /* XXX - had_error++ ? */
350 }
351 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
352 if ((n < 0) || !maybe_hnok(res, bp)) {
353 had_error++;
354 break;
355 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900356#if MULTI_PTRS_ARE_ALIASES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900357 cp += n;
358 if (cp != erdata) goto no_recovery;
359 if (!haveanswer)
360 hent->h_name = bp;
361 else
362 addalias(ap, bp, aliases, maxaliases);
363 if (n != -1) {
364 n = (int) strlen(bp) + 1; /* for the \0 */
365 if (n >= MAXHOSTNAMELEN) {
366 had_error++;
367 break;
368 }
369 bp += n;
370 }
371 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900372#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900373 hent->h_name = bp;
374 if (res->options & RES_USE_INET6) {
375 n = strlen(bp) + 1; /* for the \0 */
376 if (n >= MAXHOSTNAMELEN) {
377 had_error++;
378 break;
379 }
380 bp += n;
381 map_v4v6_hostent(hent, &bp, ep);
382 }
383 goto success;
Bernie Innocenti55864192018-08-30 04:05:20 +0900384#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900385 case T_A:
386 case T_AAAA:
387 if (strcasecmp(hent->h_name, bp) != 0) {
388 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
389 cp += n;
390 continue; /* XXX - had_error++ ? */
391 }
392 if (n != hent->h_length) {
393 cp += n;
394 continue;
395 }
396 if (type == T_AAAA) {
397 struct in6_addr in6;
398 memcpy(&in6, cp, NS_IN6ADDRSZ);
399 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
400 cp += n;
401 continue;
402 }
403 }
404 if (!haveanswer) {
405 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900406
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900407 hent->h_name = bp;
408 nn = (int) strlen(bp) + 1; /* for the \0 */
409 bp += nn;
410 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900411
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900412 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900413
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900414 if (bp + n >= ep) {
415 debugprintf("size (%d) too big\n", res, n);
416 had_error++;
417 continue;
418 }
419 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
420 if (!toobig++) {
421 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
422 }
423 cp += n;
424 continue;
425 }
426 (void) memcpy(*hap++ = bp, cp, (size_t) n);
427 bp += n;
428 cp += n;
429 if (cp != erdata) goto no_recovery;
430 break;
431 default:
432 abort();
433 }
434 if (!had_error) haveanswer++;
435 }
436 if (haveanswer) {
437 *ap = NULL;
438 *hap = NULL;
439 /*
440 * Note: we sort even if host can take only one address
441 * in its return structures - should give it the "best"
442 * address in that case, not some random one
443 */
444 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
445 if (!hent->h_name) {
446 n = (int) strlen(qname) + 1; /* for the \0 */
447 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
448 strlcpy(bp, qname, (size_t)(ep - bp));
449 hent->h_name = bp;
450 bp += n;
451 }
452 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
453 goto success;
454 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900455no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900456 free(aliases);
457 *he = NO_RECOVERY;
458 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900459success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900460 bp = (char*) ALIGN(bp);
461 n = (int) (ap - aliases);
462 qlen = (n + 1) * sizeof(*hent->h_aliases);
463 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900464 hent->h_aliases = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900465 memcpy(bp, aliases, qlen);
466 free(aliases);
467 aliases = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900468
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900469 bp += qlen;
470 n = (int) (hap - addr_ptrs);
471 qlen = (n + 1) * sizeof(*hent->h_addr_list);
472 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900473 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900474 memcpy(bp, addr_ptrs, qlen);
475 *he = NETDB_SUCCESS;
476 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900477nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900478 free(aliases);
479 errno = ENOSPC;
480 *he = NETDB_INTERNAL;
481 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900482}
483
484/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485int gethostbyname_r(const char* name, struct hostent* hp, char* buf, size_t buflen,
486 struct hostent** result, int* errorp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900487 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 if (res == NULL) {
489 *result = NULL;
490 *errorp = NETDB_INTERNAL;
491 return -1;
492 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900493
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900494 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900495
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900496 if (res->options & RES_USE_INET6) {
497 *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
498 &NETCONTEXT_UNSET);
499 if (*result) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900500 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900501 return 0;
502 }
503 }
504 *result =
505 gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
506 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900507}
508
509/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510int gethostbyname2_r(const char* name, int af, struct hostent* hp, char* buf, size_t buflen,
511 struct hostent** result, int* errorp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900512 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 if (res == NULL) {
514 *result = NULL;
515 *errorp = NETDB_INTERNAL;
516 return -1;
517 }
518 *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
519 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900520}
521
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900522static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
523 struct hostent* hp, char* buf, size_t buflen,
524 int* he) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900525 struct getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900526 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900527
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900528 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900529
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900530 switch (af) {
531 case AF_INET:
532 size = NS_INADDRSZ;
533 break;
534 case AF_INET6:
535 size = NS_IN6ADDRSZ;
536 break;
537 default:
538 *he = NETDB_INTERNAL;
539 errno = EAFNOSUPPORT;
540 return NULL;
541 }
542 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900543
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 hp->h_addrtype = af;
545 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900546
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900547 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900548 * disallow names consisting only of digits/dots, unless
549 * they end in a dot.
550 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900551 if (isdigit((u_char) name[0])) {
552 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900553 if (!*cp) {
554 if (*--cp == '.') break;
555 /*
556 * All-numeric, no dot at the end.
557 * Fake up a hostent as if we'd actually
558 * done a lookup.
559 */
560 goto fake;
561 }
562 if (!isdigit((u_char) *cp) && *cp != '.') break;
563 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900564 }
565 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
566 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900567 if (!*cp) {
568 if (*--cp == '.') break;
569 /*
570 * All-IPv6-legal, no dot at the end.
571 * Fake up a hostent as if we'd actually
572 * done a lookup.
573 */
574 goto fake;
575 }
576 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
577 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900578 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900579
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900580 *he = NETDB_INTERNAL;
581 info.hp = hp;
582 info.buf = buf;
583 info.buflen = buflen;
584 info.he = he;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900585 if (!_hf_gethtbyname2(name, af, &info)) {
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900586 if (!_dns_gethtbyname(name, af, &info)) {
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900587 return NULL;
588 }
589 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900590 *he = NETDB_SUCCESS;
591 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900592nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900593 *he = NETDB_INTERNAL;
594 errno = ENOSPC;
595 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900596fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
598 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900599
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900600 hp->h_aliases[0] = NULL;
601 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900602
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900603 if (inet_pton(af, name, buf) <= 0) {
604 *he = HOST_NOT_FOUND;
605 return NULL;
606 }
607 hp->h_addr_list[0] = buf;
608 hp->h_addr_list[1] = NULL;
609 buf += size;
610 buflen -= size;
611 HENT_SCOPY(hp->h_name, name, buf, buflen);
612 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
613 *he = NETDB_SUCCESS;
614 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900615}
616
617// very similar in proxy-ness to android_getaddrinfo_proxy
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618static struct hostent* gethostbyname_internal(const char* name, int af, res_state res,
619 struct hostent* hp, char* hbuf, size_t hbuflen,
620 int* errorp,
621 const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900622 res_setnetcontext(res, netcontext);
623 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900624}
625
626/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900627int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf,
628 size_t buflen, struct hostent** result, int* h_errnop) {
629 *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen,
630 h_errnop, &NETCONTEXT_UNSET);
631 return h_errno_to_result(h_errnop);
Bernie Innocenti55864192018-08-30 04:05:20 +0900632}
633
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900634static struct hostent* android_gethostbyaddrfornetcontext_real(
635 const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
636 int* he, const struct android_net_context* netcontext) {
637 const u_char* uaddr = (const u_char*) addr;
638 socklen_t size;
639 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900642
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900643 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
644 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
645 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
646 *he = HOST_NOT_FOUND;
647 return NULL;
648 }
649 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
650 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
651 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
652 /* Unmap. */
653 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
654 addr = uaddr;
655 af = AF_INET;
656 len = NS_INADDRSZ;
657 }
658 switch (af) {
659 case AF_INET:
660 size = NS_INADDRSZ;
661 break;
662 case AF_INET6:
663 size = NS_IN6ADDRSZ;
664 break;
665 default:
666 errno = EAFNOSUPPORT;
667 *he = NETDB_INTERNAL;
668 return NULL;
669 }
670 if (size != len) {
671 errno = EINVAL;
672 *he = NETDB_INTERNAL;
673 return NULL;
674 }
675 info.hp = hp;
676 info.buf = buf;
677 info.buflen = buflen;
678 info.he = he;
679 *he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900680 if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
681 if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) {
682 return NULL;
683 }
684 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900685 *he = NETDB_SUCCESS;
686 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900687}
688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
690 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
691 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900692 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
693 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900694}
695
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900696struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
697 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900698 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900699 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900700 char *cp, **q;
701 int af, len;
702 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900704
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900705 if (hf == NULL) {
706 *he = NETDB_INTERNAL;
707 errno = EINVAL;
708 return NULL;
709 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900710 char* p = NULL;
711 size_t maxaliases = 10;
712 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
713 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900715 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900716 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900717 * as the buffer may be used to store content for a previous hostent
718 * returned by non-reentrant functions like gethostbyname().
719 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900720 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721 goto nospc;
722 }
723 for (;;) {
724 if (!fgets(p, line_buf_size, hf)) {
725 free(p);
726 free(aliases);
727 *he = HOST_NOT_FOUND;
728 return NULL;
729 }
730 if (*p == '#') {
731 continue;
732 }
733 if (!(cp = strpbrk(p, "#\n"))) {
734 continue;
735 }
736 *cp = '\0';
737 if (!(cp = strpbrk(p, " \t"))) continue;
738 *cp++ = '\0';
739 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
740 af = AF_INET6;
741 len = NS_IN6ADDRSZ;
742 } else {
743 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900744
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900745 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900746 if (res == NULL) goto nospc;
747 if (res->options & RES_USE_INET6) {
748 map_v4v6_address(buf, buf);
749 af = AF_INET6;
750 len = NS_IN6ADDRSZ;
751 } else {
752 af = AF_INET;
753 len = NS_INADDRSZ;
754 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900755 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 /* if this is not something we're looking for, skip it. */
759 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
760 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900761
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 while (*cp == ' ' || *cp == '\t') cp++;
763 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
764 q = aliases;
765 while (cp && *cp) {
766 if (*cp == ' ' || *cp == '\t') {
767 cp++;
768 continue;
769 }
770 addalias(q, cp, aliases, maxaliases);
771 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
772 }
773 break;
774 }
775 hent->h_length = len;
776 hent->h_addrtype = af;
777 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
778 anum = (size_t)(q - aliases);
779 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
780 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
781 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900782
nuccachen8161c992018-09-11 11:20:00 +0800783 /* Reserve space for mapping IPv4 address to IPv6 address in place */
784 if (hent->h_addrtype == AF_INET) {
785 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
786 }
787
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900788 HENT_SCOPY(hent->h_name, name, buf, buflen);
789 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
790 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900791
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900792 *he = NETDB_SUCCESS;
793 free(p);
794 free(aliases);
795 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900796nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900797 free(p);
798 free(aliases);
799 errno = ENOSPC;
800 *he = NETDB_INTERNAL;
801 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900802}
803
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900804static void map_v4v6_address(const char* src, char* dst) {
805 u_char* p = (u_char*) dst;
806 char tmp[NS_INADDRSZ];
807 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900808
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900809 _DIAGASSERT(src != NULL);
810 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900813 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900814 /* Mark this ipv6 addr as a mapped ipv4. */
815 for (i = 0; i < 10; i++) *p++ = 0x00;
816 *p++ = 0xff;
817 *p++ = 0xff;
818 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900819 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900820}
821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
823 char** ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825 _DIAGASSERT(hp != NULL);
826 _DIAGASSERT(bpp != NULL);
827 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900828
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
830 hp->h_addrtype = AF_INET6;
831 hp->h_length = NS_IN6ADDRSZ;
832 for (ap = hp->h_addr_list; *ap; ap++) {
833 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900834
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900835 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
836 /* Out of memory. Truncate address list here. XXX */
837 *ap = NULL;
838 return;
839 }
840 *bpp += i;
841 map_v4v6_address(*ap, *bpp);
842 *ap = *bpp;
843 *bpp += NS_IN6ADDRSZ;
844 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900845}
846
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847static void addrsort(char** ap, int num, res_state res) {
848 int i, j;
849 char** p;
850 short aval[MAXADDRS];
851 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900854
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900855 p = ap;
856 for (i = 0; i < num; i++, p++) {
857 for (j = 0; (unsigned) j < res->nsort; j++)
858 if (res->sort_list[j].addr.s_addr ==
859 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
860 break;
861 aval[i] = j;
862 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
863 }
864 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900865
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900866 while (needsort < num) {
867 for (j = needsort - 1; j >= 0; j--) {
868 if (aval[j] > aval[j + 1]) {
869 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900870
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900871 i = aval[j];
872 aval[j] = aval[j + 1];
873 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900874
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875 hp = ap[j];
876 ap[j] = ap[j + 1];
877 ap[j + 1] = hp;
878 } else
879 break;
880 }
881 needsort++;
882 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900883}
884
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900885static bool _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900886 int n, type;
887 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900888 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900889
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900890 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900891
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900892 switch (info->hp->h_addrtype) {
893 case AF_INET:
894 info->hp->h_length = NS_INADDRSZ;
895 type = T_A;
896 break;
897 case AF_INET6:
898 info->hp->h_length = NS_IN6ADDRSZ;
899 type = T_AAAA;
900 break;
901 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900902 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900904 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900905 if (buf == NULL) {
906 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900907 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900908 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900909 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910 if (res == NULL) {
911 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900912 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900913 }
914 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf));
915 if (n < 0) {
916 free(buf);
917 debugprintf("res_nsearch failed (%d)\n", res, n);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900918 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900919 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900920 }
921 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
922 free(buf);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900923 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900924 if (hp == NULL) {
925 return false;
926 }
927 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900928}
929
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900930static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
931 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900932 char qbuf[MAXDNAME + 1], *qp, *ep;
933 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900934 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900935 int advance;
936 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900937
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900938 info->hp->h_length = len;
939 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900940
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900941 switch (info->hp->h_addrtype) {
942 case AF_INET:
943 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
944 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
945 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900946
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 case AF_INET6:
948 qp = qbuf;
949 ep = qbuf + sizeof(qbuf) - 1;
950 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
951 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
952 ((unsigned int) uaddr[n] >> 4) & 0xf);
953 if (advance > 0 && qp + advance < ep)
954 qp += advance;
955 else {
956 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900957 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900958 }
959 }
960 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
961 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900962 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900963 }
964 break;
965 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900966 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900968
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900969 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900970 if (buf == NULL) {
971 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900972 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900973 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900974 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900975 if (res == NULL) {
976 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900977 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900978 }
979 res_setnetcontext(res, netcontext);
980 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf));
981 if (n < 0) {
982 free(buf);
983 debugprintf("res_nquery failed (%d)\n", res, n);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900984 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900985 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900986 }
987 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
988 free(buf);
989 if (hp == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900990 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900991 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900992 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900993
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900994 char* bf = (char*) (hp->h_addr_list + 2);
995 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900996 if (blen + info->hp->h_length > info->buflen) goto nospc;
997 hp->h_addr_list[0] = bf;
998 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900999 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001000 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
1001 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
1002 map_v4v6_address(bf, bf);
1003 hp->h_addrtype = AF_INET6;
1004 hp->h_length = NS_IN6ADDRSZ;
1005 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001006
nuccachen8161c992018-09-11 11:20:00 +08001007 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
1008 if (info->hp->h_addrtype == AF_INET) {
1009 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
1010 // Pad zero to the unused address space
1011 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
1012 }
1013
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001014 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001015 *info->he = NETDB_SUCCESS;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001016 return true;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001017
Bernie Innocenti55864192018-08-30 04:05:20 +09001018nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001019 errno = ENOSPC;
1020 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001021 return false;
Bernie Innocenti55864192018-08-30 04:05:20 +09001022}
1023
Bernie Innocenti55864192018-08-30 04:05:20 +09001024/*
1025 * Non-reentrant versions.
1026 */
1027
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001028struct hostent* gethostbyname(const char* name) {
1029 struct hostent* result = NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001030 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001031
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001032 gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1033 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001034}
1035
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001036struct hostent* gethostbyname2(const char* name, int af) {
1037 struct hostent* result = NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001038 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001039
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001040 gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1041 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001042}
1043
1044// android_gethostby*fornet can be called in two different contexts.
1045// - In the proxy client context (proxy != NULL), |netid| is |app_netid|.
1046// - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|.
1047// The netcontext is constructed before checking which context we are in.
1048// Therefore, we have to populate both fields, and rely on the downstream code to check whether
1049// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
1050static struct android_net_context make_context(unsigned netid, unsigned mark) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001051 struct android_net_context netcontext = NETCONTEXT_UNSET;
1052 netcontext.app_netid = netid;
1053 netcontext.app_mark = mark;
1054 netcontext.dns_netid = netid;
1055 netcontext.dns_mark = mark;
1056 return netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001057}
1058
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001059struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid,
1060 unsigned mark) {
1061 const struct android_net_context netcontext = make_context(netid, mark);
1062 return android_gethostbynamefornetcontext(name, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001063}
1064
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001065struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
1066 const struct android_net_context* netcontext) {
1067 struct hostent* hp;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001068 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001069 if (res == NULL) return NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001070 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001071 hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
1072 &h_errno, netcontext);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001073 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001074 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +09001075}
1076
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001077struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) {
1078 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
Bernie Innocenti55864192018-08-30 04:05:20 +09001079}
1080
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001081struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid,
1082 unsigned mark) {
1083 const struct android_net_context netcontext = make_context(netid, mark);
1084 return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001085}
1086
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001087struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1088 const struct android_net_context* netcontext) {
1089 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001090}
1091
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +09001092static struct hostent* android_gethostbyaddrfornetcontext_proxy(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001093 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001094 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001095 return android_gethostbyaddrfornetcontext_proxy_internal(
1096 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001097}