blob: 5c4874db71e1752dfecabf79f1068f9029c3b2e9 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1988, 1993
5 * -
6 * Copyright (c) 1985, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
Bernie Innocenti55864192018-08-30 04:05:20 +090054#include <arpa/inet.h>
55#include <arpa/nameser.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090056#include <assert.h>
57#include <ctype.h>
58#include <errno.h>
59#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090060#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090061#include <stdarg.h>
62#include <stdbool.h>
63#include <stdio.h>
64#include <strings.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090065#include <sys/param.h>
66#include <sys/socket.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090067#include <sys/types.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090068#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090069#include <syslog.h>
70#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090071
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090072#include "resolv_cache.h"
73#include "resolv_netid.h"
74#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090075
Bernie Innocentif89b3512018-08-30 07:34:37 +090076// NetBSD uses _DIAGASSERT to null-check arguments and the like,
77// but it's clear from the number of mistakes in their assertions
78// that they don't actually test or ship with this.
79#define _DIAGASSERT(e) /* nothing */
80
Bernie Innocenti55864192018-08-30 04:05:20 +090081#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090083
84#ifndef LOG_AUTH
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#define LOG_AUTH 0
Bernie Innocenti55864192018-08-30 04:05:20 +090086#endif
87
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
Bernie Innocenti55864192018-08-30 04:05:20 +090089
Bernie Innocenti55864192018-08-30 04:05:20 +090090#include <stdlib.h>
91#include <string.h>
92
93#include "hostent.h"
94
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090095#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0)
Bernie Innocenti55864192018-08-30 04:05:20 +090096#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
97#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
98
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +090099#define addalias(d, s, arr, siz) \
100 do { \
101 if (d >= &arr[siz]) { \
102 char** xptr = (char**) realloc(arr, (siz + 10) * sizeof(*arr)); \
103 if (xptr == NULL) goto nospc; \
104 d = xptr + (d - arr); \
105 arr = xptr; \
106 siz += 10; \
107 } \
108 *d++ = s; \
109 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900110
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900112
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900113static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET,
114 .app_netid = NETID_UNSET,
115 .dns_mark = MARK_UNSET,
116 .dns_netid = NETID_UNSET,
117 .uid = NET_CONTEXT_INVALID_UID};
Bernie Innocenti55864192018-08-30 04:05:20 +0900118
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900120
121typedef union {
122 HEADER hdr;
123 u_char buf[MAXPACKET];
124} querybuf;
125
126typedef union {
127 int32_t al;
128 char ac;
129} align;
130
131#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900132static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900133#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900134static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
135 char*, size_t, int*);
136static void map_v4v6_address(const char*, char*);
137static void map_v4v6_hostent(struct hostent*, char**, char*);
138static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900139
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900140struct hostent* ht_gethostbyname(char*);
141struct hostent* ht_gethostbyaddr(const char*, int, int);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900142static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
143 const android_net_context* netcontext, getnamaddr* info);
144static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900145
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900146static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
147 size_t, int*, const struct android_net_context*);
148static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
149 const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
150 const struct android_net_context*);
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +0900151static struct hostent* android_gethostbyaddrfornetcontext_proxy(
152 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900153
Bernie Innocenti55864192018-08-30 04:05:20 +0900154static int h_errno_to_result(int* herrno_p) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
156 if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
157 errno = ERANGE;
158 return errno;
159 }
160 // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
161 return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900162}
163
164#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900165static void debugprintf(const char* msg, res_state res, ...) {
166 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900167
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900168 if (res->options & RES_DEBUG) {
169 int save = errno;
170 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900171
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900172 va_start(ap, res);
173 vprintf(msg, ap);
174 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900175
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900176 errno = save;
177 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900178}
179#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900180#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900181#endif
182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183#define BOUNDED_INCR(x) \
184 do { \
185 BOUNDS_CHECK(cp, x); \
186 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900187 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900188
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189#define BOUNDS_CHECK(ptr, count) \
190 do { \
191 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900192 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
195 res_state res, struct hostent* hent, char* buf, size_t buflen,
196 int* he) {
197 const HEADER* hp;
198 const u_char* cp;
199 int n;
200 size_t qlen;
201 const u_char *eom, *erdata;
202 char *bp, **ap, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900203 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 int haveanswer, had_error;
205 int toobig = 0;
206 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900207 char* addr_ptrs[MAXADDRS];
208 const char* tname;
209 int (*name_ok)(const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900210
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900211 _DIAGASSERT(answer != NULL);
212 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900213
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214 tname = qname;
215 hent->h_name = NULL;
216 eom = answer->buf + anslen;
217 switch (qtype) {
218 case T_A:
219 case T_AAAA:
220 name_ok = res_hnok;
221 break;
222 case T_PTR:
223 name_ok = res_dnok;
224 break;
225 default:
226 *he = NO_RECOVERY;
227 return NULL; /* XXX should be abort(); */
228 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900229
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900230 size_t maxaliases = 10;
231 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
232 if (!aliases) goto nospc;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900233 /*
234 * find first satisfactory answer
235 */
236 hp = &answer->hdr;
237 ancount = ntohs(hp->ancount);
238 qdcount = ntohs(hp->qdcount);
239 bp = buf;
240 ep = buf + buflen;
241 cp = answer->buf;
242 BOUNDED_INCR(HFIXEDSZ);
243 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900244
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900245 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
246 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900247
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900248 BOUNDED_INCR(n + QFIXEDSZ);
249 if (qtype == T_A || qtype == T_AAAA) {
250 /* res_send() has already verified that the query name is the
251 * same as the one we sent; this just gets the expanded name
252 * (i.e., with the succeeding search-domain tacked on).
253 */
254 n = (int) strlen(bp) + 1; /* for the \0 */
255 if (n >= MAXHOSTNAMELEN) goto no_recovery;
256 hent->h_name = bp;
257 bp += n;
258 /* The qname can be abbreviated, but h_name is now absolute. */
259 qname = hent->h_name;
260 }
261 hent->h_aliases = ap = aliases;
262 hent->h_addr_list = hap = addr_ptrs;
263 *ap = NULL;
264 *hap = NULL;
265 haveanswer = 0;
266 had_error = 0;
267 while (ancount-- > 0 && cp < eom && !had_error) {
268 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
269 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
270 had_error++;
271 continue;
272 }
273 cp += n; /* name */
274 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900275 int type = _getshort(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276 cp += INT16SZ; /* type */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900277 int cl = _getshort(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900278 cp += INT16SZ + INT32SZ; /* class, TTL */
279 n = _getshort(cp);
280 cp += INT16SZ; /* len */
281 BOUNDS_CHECK(cp, n);
282 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900283 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900284 /* XXX - debug? syslog? */
285 cp += n;
286 continue; /* XXX - had_error++ ? */
287 }
288 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
289 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
290 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
291 had_error++;
292 continue;
293 }
294 cp += n;
295 if (cp != erdata) goto no_recovery;
296 /* Store alias. */
297 addalias(ap, bp, aliases, maxaliases);
298 n = (int) strlen(bp) + 1; /* for the \0 */
299 if (n >= MAXHOSTNAMELEN) {
300 had_error++;
301 continue;
302 }
303 bp += n;
304 /* Get canonical name. */
305 n = (int) strlen(tbuf) + 1; /* for the \0 */
306 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
307 had_error++;
308 continue;
309 }
310 strlcpy(bp, tbuf, (size_t)(ep - bp));
311 hent->h_name = bp;
312 bp += n;
313 continue;
314 }
315 if (qtype == T_PTR && type == T_CNAME) {
316 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
317 if (n < 0 || !maybe_dnok(res, tbuf)) {
318 had_error++;
319 continue;
320 }
321 cp += n;
322 if (cp != erdata) goto no_recovery;
323 /* Get canonical name. */
324 n = (int) strlen(tbuf) + 1; /* for the \0 */
325 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
326 had_error++;
327 continue;
328 }
329 strlcpy(bp, tbuf, (size_t)(ep - bp));
330 tname = bp;
331 bp += n;
332 continue;
333 }
334 if (type != qtype) {
335 if (type != T_KEY && type != T_SIG)
336 syslog(LOG_NOTICE | LOG_AUTH,
337 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
338 p_class(C_IN), p_type(qtype), p_type(type));
339 cp += n;
340 continue; /* XXX - had_error++ ? */
341 }
342 switch (type) {
343 case T_PTR:
344 if (strcasecmp(tname, bp) != 0) {
345 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
346 cp += n;
347 continue; /* XXX - had_error++ ? */
348 }
349 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
350 if ((n < 0) || !maybe_hnok(res, bp)) {
351 had_error++;
352 break;
353 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900354#if MULTI_PTRS_ARE_ALIASES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900355 cp += n;
356 if (cp != erdata) goto no_recovery;
357 if (!haveanswer)
358 hent->h_name = bp;
359 else
360 addalias(ap, bp, aliases, maxaliases);
361 if (n != -1) {
362 n = (int) strlen(bp) + 1; /* for the \0 */
363 if (n >= MAXHOSTNAMELEN) {
364 had_error++;
365 break;
366 }
367 bp += n;
368 }
369 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900370#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900371 hent->h_name = bp;
372 if (res->options & RES_USE_INET6) {
373 n = strlen(bp) + 1; /* for the \0 */
374 if (n >= MAXHOSTNAMELEN) {
375 had_error++;
376 break;
377 }
378 bp += n;
379 map_v4v6_hostent(hent, &bp, ep);
380 }
381 goto success;
Bernie Innocenti55864192018-08-30 04:05:20 +0900382#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900383 case T_A:
384 case T_AAAA:
385 if (strcasecmp(hent->h_name, bp) != 0) {
386 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
387 cp += n;
388 continue; /* XXX - had_error++ ? */
389 }
390 if (n != hent->h_length) {
391 cp += n;
392 continue;
393 }
394 if (type == T_AAAA) {
395 struct in6_addr in6;
396 memcpy(&in6, cp, NS_IN6ADDRSZ);
397 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
398 cp += n;
399 continue;
400 }
401 }
402 if (!haveanswer) {
403 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900404
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900405 hent->h_name = bp;
406 nn = (int) strlen(bp) + 1; /* for the \0 */
407 bp += nn;
408 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900409
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900410 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900411
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900412 if (bp + n >= ep) {
413 debugprintf("size (%d) too big\n", res, n);
414 had_error++;
415 continue;
416 }
417 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
418 if (!toobig++) {
419 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
420 }
421 cp += n;
422 continue;
423 }
424 (void) memcpy(*hap++ = bp, cp, (size_t) n);
425 bp += n;
426 cp += n;
427 if (cp != erdata) goto no_recovery;
428 break;
429 default:
430 abort();
431 }
432 if (!had_error) haveanswer++;
433 }
434 if (haveanswer) {
435 *ap = NULL;
436 *hap = NULL;
437 /*
438 * Note: we sort even if host can take only one address
439 * in its return structures - should give it the "best"
440 * address in that case, not some random one
441 */
442 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
443 if (!hent->h_name) {
444 n = (int) strlen(qname) + 1; /* for the \0 */
445 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
446 strlcpy(bp, qname, (size_t)(ep - bp));
447 hent->h_name = bp;
448 bp += n;
449 }
450 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
451 goto success;
452 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900453no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 free(aliases);
455 *he = NO_RECOVERY;
456 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900457success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900458 bp = (char*) ALIGN(bp);
459 n = (int) (ap - aliases);
460 qlen = (n + 1) * sizeof(*hent->h_aliases);
461 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900462 hent->h_aliases = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900463 memcpy(bp, aliases, qlen);
464 free(aliases);
465 aliases = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900466
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900467 bp += qlen;
468 n = (int) (hap - addr_ptrs);
469 qlen = (n + 1) * sizeof(*hent->h_addr_list);
470 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900471 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900472 memcpy(bp, addr_ptrs, qlen);
473 *he = NETDB_SUCCESS;
474 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900475nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900476 free(aliases);
477 errno = ENOSPC;
478 *he = NETDB_INTERNAL;
479 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900480}
481
482/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483int gethostbyname_r(const char* name, struct hostent* hp, char* buf, size_t buflen,
484 struct hostent** result, int* errorp) {
485 res_state res = __res_get_state();
486 if (res == NULL) {
487 *result = NULL;
488 *errorp = NETDB_INTERNAL;
489 return -1;
490 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900491
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900493
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900494 if (res->options & RES_USE_INET6) {
495 *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
496 &NETCONTEXT_UNSET);
497 if (*result) {
498 __res_put_state(res);
499 return 0;
500 }
501 }
502 *result =
503 gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
504 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900505}
506
507/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900508int gethostbyname2_r(const char* name, int af, struct hostent* hp, char* buf, size_t buflen,
509 struct hostent** result, int* errorp) {
510 res_state res = __res_get_state();
511 if (res == NULL) {
512 *result = NULL;
513 *errorp = NETDB_INTERNAL;
514 return -1;
515 }
516 *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
517 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900518}
519
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900520static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
521 struct hostent* hp, char* buf, size_t buflen,
522 int* he) {
523 const char* cp;
524 struct getnamaddr info;
525 char hbuf[MAXHOSTNAMELEN];
526 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 /*
548 * if there aren't any dots, it could be a user-level alias.
549 * this is also done in res_nquery() since we are not the only
550 * function that looks up host names.
551 */
552 if (!strchr(name, '.') && (cp = res_hostalias(res, name, hbuf, sizeof(hbuf)))) name = cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900553
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900554 /*
555 * disallow names consisting only of digits/dots, unless
556 * they end in a dot.
557 */
558 if (isdigit((u_char) name[0]))
559 for (cp = name;; ++cp) {
560 if (!*cp) {
561 if (*--cp == '.') break;
562 /*
563 * All-numeric, no dot at the end.
564 * Fake up a hostent as if we'd actually
565 * done a lookup.
566 */
567 goto fake;
568 }
569 if (!isdigit((u_char) *cp) && *cp != '.') break;
570 }
571 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':')
572 for (cp = name;; ++cp) {
573 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 Innocenti55864192018-08-30 04:05:20 +0900584
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900585 *he = NETDB_INTERNAL;
586 info.hp = hp;
587 info.buf = buf;
588 info.buflen = buflen;
589 info.he = he;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900590 if (!_hf_gethtbyname2(name, af, &info)) {
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900591 if (!_dns_gethtbyname(name, af, &info)) {
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900592 return NULL;
593 }
594 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900595 *he = NETDB_SUCCESS;
596 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900597nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900598 *he = NETDB_INTERNAL;
599 errno = ENOSPC;
600 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900601fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900602 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
603 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 hp->h_aliases[0] = NULL;
606 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900607
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608 if (inet_pton(af, name, buf) <= 0) {
609 *he = HOST_NOT_FOUND;
610 return NULL;
611 }
612 hp->h_addr_list[0] = buf;
613 hp->h_addr_list[1] = NULL;
614 buf += size;
615 buflen -= size;
616 HENT_SCOPY(hp->h_name, name, buf, buflen);
617 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
618 *he = NETDB_SUCCESS;
619 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900620}
621
622// very similar in proxy-ness to android_getaddrinfo_proxy
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623static struct hostent* gethostbyname_internal(const char* name, int af, res_state res,
624 struct hostent* hp, char* hbuf, size_t hbuflen,
625 int* errorp,
626 const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900627 res_setnetcontext(res, netcontext);
628 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900629}
630
631/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900632int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf,
633 size_t buflen, struct hostent** result, int* h_errnop) {
634 *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen,
635 h_errnop, &NETCONTEXT_UNSET);
636 return h_errno_to_result(h_errnop);
Bernie Innocenti55864192018-08-30 04:05:20 +0900637}
638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639static struct hostent* android_gethostbyaddrfornetcontext_real(
640 const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
641 int* he, const struct android_net_context* netcontext) {
642 const u_char* uaddr = (const u_char*) addr;
643 socklen_t size;
644 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900645
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900646 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900647
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
649 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
650 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
651 *he = HOST_NOT_FOUND;
652 return NULL;
653 }
654 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
655 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
656 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
657 /* Unmap. */
658 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
659 addr = uaddr;
660 af = AF_INET;
661 len = NS_INADDRSZ;
662 }
663 switch (af) {
664 case AF_INET:
665 size = NS_INADDRSZ;
666 break;
667 case AF_INET6:
668 size = NS_IN6ADDRSZ;
669 break;
670 default:
671 errno = EAFNOSUPPORT;
672 *he = NETDB_INTERNAL;
673 return NULL;
674 }
675 if (size != len) {
676 errno = EINVAL;
677 *he = NETDB_INTERNAL;
678 return NULL;
679 }
680 info.hp = hp;
681 info.buf = buf;
682 info.buflen = buflen;
683 info.he = he;
684 *he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900685 if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
686 if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) {
687 return NULL;
688 }
689 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900690 *he = NETDB_SUCCESS;
691 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900692}
693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
695 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
696 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900697 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
698 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900699}
700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900701struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
702 int* he) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900703 const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
704 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900705 char *cp, **q;
706 int af, len;
707 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900708 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900709
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900710 if (hf == NULL) {
711 *he = NETDB_INTERNAL;
712 errno = EINVAL;
713 return NULL;
714 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900715 char* p = NULL;
716 size_t maxaliases = 10;
717 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
718 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900719
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900720 /* Allocate a new space to read file lines like upstream does.
721 * To keep reentrancy we cannot use __res_get_static()->hostbuf here,
722 * as the buffer may be used to store content for a previous hostent
723 * returned by non-reentrant functions like gethostbyname().
724 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900725 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900726 goto nospc;
727 }
728 for (;;) {
729 if (!fgets(p, line_buf_size, hf)) {
730 free(p);
731 free(aliases);
732 *he = HOST_NOT_FOUND;
733 return NULL;
734 }
735 if (*p == '#') {
736 continue;
737 }
738 if (!(cp = strpbrk(p, "#\n"))) {
739 continue;
740 }
741 *cp = '\0';
742 if (!(cp = strpbrk(p, " \t"))) continue;
743 *cp++ = '\0';
744 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
745 af = AF_INET6;
746 len = NS_IN6ADDRSZ;
747 } else {
748 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900749
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 res_state res = __res_get_state();
751 if (res == NULL) goto nospc;
752 if (res->options & RES_USE_INET6) {
753 map_v4v6_address(buf, buf);
754 af = AF_INET6;
755 len = NS_IN6ADDRSZ;
756 } else {
757 af = AF_INET;
758 len = NS_INADDRSZ;
759 }
760 __res_put_state(res);
761 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900762
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900763 /* if this is not something we're looking for, skip it. */
764 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
765 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900766
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900767 while (*cp == ' ' || *cp == '\t') cp++;
768 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
769 q = aliases;
770 while (cp && *cp) {
771 if (*cp == ' ' || *cp == '\t') {
772 cp++;
773 continue;
774 }
775 addalias(q, cp, aliases, maxaliases);
776 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
777 }
778 break;
779 }
780 hent->h_length = len;
781 hent->h_addrtype = af;
782 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
783 anum = (size_t)(q - aliases);
784 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
785 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
786 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900787
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 }
909 res = __res_get_state();
910 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);
918 __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);
923 __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 }
974 res = __res_get_state();
975 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);
984 __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) {
990 __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
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001007 __res_put_state(res);
1008 *info->he = NETDB_SUCCESS;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001009 return true;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001010
Bernie Innocenti55864192018-08-30 04:05:20 +09001011nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001012 errno = ENOSPC;
1013 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001014 return false;
Bernie Innocenti55864192018-08-30 04:05:20 +09001015}
1016
Bernie Innocenti55864192018-08-30 04:05:20 +09001017/*
1018 * Non-reentrant versions.
1019 */
1020
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001021struct hostent* gethostbyname(const char* name) {
1022 struct hostent* result = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001023 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001024
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001025 gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1026 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001027}
1028
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001029struct hostent* gethostbyname2(const char* name, int af) {
1030 struct hostent* result = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001031 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001032
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001033 gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1034 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001035}
1036
1037// android_gethostby*fornet can be called in two different contexts.
1038// - In the proxy client context (proxy != NULL), |netid| is |app_netid|.
1039// - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|.
1040// The netcontext is constructed before checking which context we are in.
1041// Therefore, we have to populate both fields, and rely on the downstream code to check whether
1042// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
1043static struct android_net_context make_context(unsigned netid, unsigned mark) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001044 struct android_net_context netcontext = NETCONTEXT_UNSET;
1045 netcontext.app_netid = netid;
1046 netcontext.app_mark = mark;
1047 netcontext.dns_netid = netid;
1048 netcontext.dns_mark = mark;
1049 return netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001050}
1051
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001052struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid,
1053 unsigned mark) {
1054 const struct android_net_context netcontext = make_context(netid, mark);
1055 return android_gethostbynamefornetcontext(name, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001056}
1057
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001058struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
1059 const struct android_net_context* netcontext) {
1060 struct hostent* hp;
1061 res_state res = __res_get_state();
1062 if (res == NULL) return NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001063 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001064 hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
1065 &h_errno, netcontext);
1066 __res_put_state(res);
1067 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +09001068}
1069
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001070struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) {
1071 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
Bernie Innocenti55864192018-08-30 04:05:20 +09001072}
1073
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001074struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid,
1075 unsigned mark) {
1076 const struct android_net_context netcontext = make_context(netid, mark);
1077 return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001078}
1079
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001080struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1081 const struct android_net_context* netcontext) {
1082 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001083}
1084
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +09001085static struct hostent* android_gethostbyaddrfornetcontext_proxy(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001086 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001087 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001088 return android_gethostbyaddrfornetcontext_proxy_internal(
1089 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001090}