blob: 5cdcd1d1e728ab82584dcacc1172010699df8c41 [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 Innocentiee1b85b2018-09-25 14:23:19 +0900275 int type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900277 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900278 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900279 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900280 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) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900485 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900486 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) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900498 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900499 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) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900510 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511 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) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900523 struct getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900524 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900525
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900526 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900527
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900528 switch (af) {
529 case AF_INET:
530 size = NS_INADDRSZ;
531 break;
532 case AF_INET6:
533 size = NS_IN6ADDRSZ;
534 break;
535 default:
536 *he = NETDB_INTERNAL;
537 errno = EAFNOSUPPORT;
538 return NULL;
539 }
540 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900541
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900542 hp->h_addrtype = af;
543 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900544
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900545 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546 * disallow names consisting only of digits/dots, unless
547 * they end in a dot.
548 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900549 if (isdigit((u_char) name[0])) {
550 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900551 if (!*cp) {
552 if (*--cp == '.') break;
553 /*
554 * All-numeric, no dot at the end.
555 * Fake up a hostent as if we'd actually
556 * done a lookup.
557 */
558 goto fake;
559 }
560 if (!isdigit((u_char) *cp) && *cp != '.') break;
561 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900562 }
563 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
564 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565 if (!*cp) {
566 if (*--cp == '.') break;
567 /*
568 * All-IPv6-legal, no dot at the end.
569 * Fake up a hostent as if we'd actually
570 * done a lookup.
571 */
572 goto fake;
573 }
574 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
575 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900576 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900577
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578 *he = NETDB_INTERNAL;
579 info.hp = hp;
580 info.buf = buf;
581 info.buflen = buflen;
582 info.he = he;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900583 if (!_hf_gethtbyname2(name, af, &info)) {
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900584 if (!_dns_gethtbyname(name, af, &info)) {
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900585 return NULL;
586 }
587 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900588 *he = NETDB_SUCCESS;
589 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900590nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900591 *he = NETDB_INTERNAL;
592 errno = ENOSPC;
593 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900594fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900595 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
596 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900597
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900598 hp->h_aliases[0] = NULL;
599 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 if (inet_pton(af, name, buf) <= 0) {
602 *he = HOST_NOT_FOUND;
603 return NULL;
604 }
605 hp->h_addr_list[0] = buf;
606 hp->h_addr_list[1] = NULL;
607 buf += size;
608 buflen -= size;
609 HENT_SCOPY(hp->h_name, name, buf, buflen);
610 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
611 *he = NETDB_SUCCESS;
612 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900613}
614
615// very similar in proxy-ness to android_getaddrinfo_proxy
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900616static struct hostent* gethostbyname_internal(const char* name, int af, res_state res,
617 struct hostent* hp, char* hbuf, size_t hbuflen,
618 int* errorp,
619 const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900620 res_setnetcontext(res, netcontext);
621 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900622}
623
624/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf,
626 size_t buflen, struct hostent** result, int* h_errnop) {
627 *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen,
628 h_errnop, &NETCONTEXT_UNSET);
629 return h_errno_to_result(h_errnop);
Bernie Innocenti55864192018-08-30 04:05:20 +0900630}
631
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900632static struct hostent* android_gethostbyaddrfornetcontext_real(
633 const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
634 int* he, const struct android_net_context* netcontext) {
635 const u_char* uaddr = (const u_char*) addr;
636 socklen_t size;
637 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900640
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
642 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
643 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
644 *he = HOST_NOT_FOUND;
645 return NULL;
646 }
647 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
648 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
649 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
650 /* Unmap. */
651 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
652 addr = uaddr;
653 af = AF_INET;
654 len = NS_INADDRSZ;
655 }
656 switch (af) {
657 case AF_INET:
658 size = NS_INADDRSZ;
659 break;
660 case AF_INET6:
661 size = NS_IN6ADDRSZ;
662 break;
663 default:
664 errno = EAFNOSUPPORT;
665 *he = NETDB_INTERNAL;
666 return NULL;
667 }
668 if (size != len) {
669 errno = EINVAL;
670 *he = NETDB_INTERNAL;
671 return NULL;
672 }
673 info.hp = hp;
674 info.buf = buf;
675 info.buflen = buflen;
676 info.he = he;
677 *he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900678 if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
679 if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) {
680 return NULL;
681 }
682 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683 *he = NETDB_SUCCESS;
684 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900685}
686
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
688 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
689 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900690 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
691 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900692}
693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
695 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900696 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900697 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900698 char *cp, **q;
699 int af, len;
700 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900701 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900702
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703 if (hf == NULL) {
704 *he = NETDB_INTERNAL;
705 errno = EINVAL;
706 return NULL;
707 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900708 char* p = NULL;
709 size_t maxaliases = 10;
710 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
711 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900714 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900715 * as the buffer may be used to store content for a previous hostent
716 * returned by non-reentrant functions like gethostbyname().
717 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900718 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900719 goto nospc;
720 }
721 for (;;) {
722 if (!fgets(p, line_buf_size, hf)) {
723 free(p);
724 free(aliases);
725 *he = HOST_NOT_FOUND;
726 return NULL;
727 }
728 if (*p == '#') {
729 continue;
730 }
731 if (!(cp = strpbrk(p, "#\n"))) {
732 continue;
733 }
734 *cp = '\0';
735 if (!(cp = strpbrk(p, " \t"))) continue;
736 *cp++ = '\0';
737 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
738 af = AF_INET6;
739 len = NS_IN6ADDRSZ;
740 } else {
741 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900742
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900743 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900744 if (res == NULL) goto nospc;
745 if (res->options & RES_USE_INET6) {
746 map_v4v6_address(buf, buf);
747 af = AF_INET6;
748 len = NS_IN6ADDRSZ;
749 } else {
750 af = AF_INET;
751 len = NS_INADDRSZ;
752 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900753 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900754 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 /* if this is not something we're looking for, skip it. */
757 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
758 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760 while (*cp == ' ' || *cp == '\t') cp++;
761 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
762 q = aliases;
763 while (cp && *cp) {
764 if (*cp == ' ' || *cp == '\t') {
765 cp++;
766 continue;
767 }
768 addalias(q, cp, aliases, maxaliases);
769 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
770 }
771 break;
772 }
773 hent->h_length = len;
774 hent->h_addrtype = af;
775 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
776 anum = (size_t)(q - aliases);
777 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
778 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
779 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 HENT_SCOPY(hent->h_name, name, buf, buflen);
782 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
783 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900784
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900785 *he = NETDB_SUCCESS;
786 free(p);
787 free(aliases);
788 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900789nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 free(p);
791 free(aliases);
792 errno = ENOSPC;
793 *he = NETDB_INTERNAL;
794 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900795}
796
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900797static void map_v4v6_address(const char* src, char* dst) {
798 u_char* p = (u_char*) dst;
799 char tmp[NS_INADDRSZ];
800 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900801
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900802 _DIAGASSERT(src != NULL);
803 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900804
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900805 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900806 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900807 /* Mark this ipv6 addr as a mapped ipv4. */
808 for (i = 0; i < 10; i++) *p++ = 0x00;
809 *p++ = 0xff;
810 *p++ = 0xff;
811 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900812 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900813}
814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900815static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
816 char** ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900818 _DIAGASSERT(hp != NULL);
819 _DIAGASSERT(bpp != NULL);
820 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
823 hp->h_addrtype = AF_INET6;
824 hp->h_length = NS_IN6ADDRSZ;
825 for (ap = hp->h_addr_list; *ap; ap++) {
826 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
829 /* Out of memory. Truncate address list here. XXX */
830 *ap = NULL;
831 return;
832 }
833 *bpp += i;
834 map_v4v6_address(*ap, *bpp);
835 *ap = *bpp;
836 *bpp += NS_IN6ADDRSZ;
837 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900838}
839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840static void addrsort(char** ap, int num, res_state res) {
841 int i, j;
842 char** p;
843 short aval[MAXADDRS];
844 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900845
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900847
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900848 p = ap;
849 for (i = 0; i < num; i++, p++) {
850 for (j = 0; (unsigned) j < res->nsort; j++)
851 if (res->sort_list[j].addr.s_addr ==
852 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
853 break;
854 aval[i] = j;
855 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
856 }
857 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900858
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900859 while (needsort < num) {
860 for (j = needsort - 1; j >= 0; j--) {
861 if (aval[j] > aval[j + 1]) {
862 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900863
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900864 i = aval[j];
865 aval[j] = aval[j + 1];
866 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900867
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900868 hp = ap[j];
869 ap[j] = ap[j + 1];
870 ap[j + 1] = hp;
871 } else
872 break;
873 }
874 needsort++;
875 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900876}
877
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900878static bool _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879 int n, type;
880 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900881 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900882
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900883 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900884
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900885 switch (info->hp->h_addrtype) {
886 case AF_INET:
887 info->hp->h_length = NS_INADDRSZ;
888 type = T_A;
889 break;
890 case AF_INET6:
891 info->hp->h_length = NS_IN6ADDRSZ;
892 type = T_AAAA;
893 break;
894 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900895 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900896 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900897 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900898 if (buf == NULL) {
899 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900900 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900901 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900902 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903 if (res == NULL) {
904 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900905 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900906 }
907 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf));
908 if (n < 0) {
909 free(buf);
910 debugprintf("res_nsearch failed (%d)\n", res, n);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900911 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900912 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900913 }
914 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
915 free(buf);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900916 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900917 if (hp == NULL) {
918 return false;
919 }
920 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900921}
922
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900923static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
924 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900925 char qbuf[MAXDNAME + 1], *qp, *ep;
926 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900927 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900928 int advance;
929 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900930
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900931 info->hp->h_length = len;
932 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900933
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900934 switch (info->hp->h_addrtype) {
935 case AF_INET:
936 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
937 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
938 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900939
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900940 case AF_INET6:
941 qp = qbuf;
942 ep = qbuf + sizeof(qbuf) - 1;
943 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
944 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
945 ((unsigned int) uaddr[n] >> 4) & 0xf);
946 if (advance > 0 && qp + advance < ep)
947 qp += advance;
948 else {
949 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900950 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900951 }
952 }
953 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
954 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900955 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900956 }
957 break;
958 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900959 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900960 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900961
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900962 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900963 if (buf == NULL) {
964 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900965 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900966 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900967 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900968 if (res == NULL) {
969 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900970 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900971 }
972 res_setnetcontext(res, netcontext);
973 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf));
974 if (n < 0) {
975 free(buf);
976 debugprintf("res_nquery failed (%d)\n", res, n);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900977 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900978 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900979 }
980 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
981 free(buf);
982 if (hp == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900983 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900984 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900986
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900987 char* bf = (char*) (hp->h_addr_list + 2);
988 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900989 if (blen + info->hp->h_length > info->buflen) goto nospc;
990 hp->h_addr_list[0] = bf;
991 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900992 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900993 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
994 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
995 map_v4v6_address(bf, bf);
996 hp->h_addrtype = AF_INET6;
997 hp->h_length = NS_IN6ADDRSZ;
998 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900999
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001000 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001001 *info->he = NETDB_SUCCESS;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001002 return true;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001003
Bernie Innocenti55864192018-08-30 04:05:20 +09001004nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001005 errno = ENOSPC;
1006 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001007 return false;
Bernie Innocenti55864192018-08-30 04:05:20 +09001008}
1009
Bernie Innocenti55864192018-08-30 04:05:20 +09001010/*
1011 * Non-reentrant versions.
1012 */
1013
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001014struct hostent* gethostbyname(const char* name) {
1015 struct hostent* result = NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001016 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001017
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001018 gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1019 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001020}
1021
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001022struct hostent* gethostbyname2(const char* name, int af) {
1023 struct hostent* result = NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001024 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001025
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001026 gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1027 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001028}
1029
1030// android_gethostby*fornet can be called in two different contexts.
1031// - In the proxy client context (proxy != NULL), |netid| is |app_netid|.
1032// - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|.
1033// The netcontext is constructed before checking which context we are in.
1034// Therefore, we have to populate both fields, and rely on the downstream code to check whether
1035// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
1036static struct android_net_context make_context(unsigned netid, unsigned mark) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001037 struct android_net_context netcontext = NETCONTEXT_UNSET;
1038 netcontext.app_netid = netid;
1039 netcontext.app_mark = mark;
1040 netcontext.dns_netid = netid;
1041 netcontext.dns_mark = mark;
1042 return netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001043}
1044
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001045struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid,
1046 unsigned mark) {
1047 const struct android_net_context netcontext = make_context(netid, mark);
1048 return android_gethostbynamefornetcontext(name, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001049}
1050
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001051struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
1052 const struct android_net_context* netcontext) {
1053 struct hostent* hp;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001054 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001055 if (res == NULL) return NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001056 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001057 hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
1058 &h_errno, netcontext);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001059 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001060 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +09001061}
1062
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001063struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) {
1064 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
Bernie Innocenti55864192018-08-30 04:05:20 +09001065}
1066
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001067struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid,
1068 unsigned mark) {
1069 const struct android_net_context netcontext = make_context(netid, mark);
1070 return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001071}
1072
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001073struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1074 const struct android_net_context* netcontext) {
1075 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001076}
1077
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +09001078static struct hostent* android_gethostbyaddrfornetcontext_proxy(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001079 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001080 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001081 return android_gethostbyaddrfornetcontext_proxy_internal(
1082 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001083}