blob: 84942f876167da9f022db405fa7756dae5b259bb [file] [log] [blame]
Yabin Cui58d33a52014-12-16 17:03:44 -08001/* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002
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
54#include <sys/cdefs.h>
55#include <sys/types.h>
56
57#include <sys/param.h>
58#include <sys/socket.h>
Mattias Falkc63e5902011-08-23 14:34:14 +020059#include <sys/un.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060#include <netinet/in.h>
61#include <arpa/inet.h>
Calin Juravle569fb982014-03-04 15:01:29 +000062#include <arpa/nameser.h>
Paul Jensen5240b562014-05-15 14:43:07 -040063#include "NetdClientDispatch.h"
Szymon Jakubczakea9bf672014-02-14 17:07:23 -050064#include "resolv_netid.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065#include "resolv_private.h"
66#include "resolv_cache.h"
67#include <assert.h>
68#include <ctype.h>
69#include <errno.h>
70#include <netdb.h>
71#include <stdarg.h>
Elliott Hughes9773fa32014-12-10 14:56:46 -080072#include <stdbool.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073#include <stdio.h>
Carl Shapiro2cc2b2b2011-03-21 20:01:03 -070074#include <strings.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075#include <syslog.h>
Mattias Falkc63e5902011-08-23 14:34:14 +020076#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077
Calin Juravlec20de902014-03-20 15:21:32 +000078#define ALIGNBYTES (sizeof(uintptr_t) - 1)
79#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
80
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081#ifndef LOG_AUTH
82# define LOG_AUTH 0
83#endif
84
85#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
86
87#include "nsswitch.h"
88#include <stdlib.h>
89#include <string.h>
90
Yabin Cui58d33a52014-12-16 17:03:44 -080091#include "hostent.h"
92
Lev Rumyantsev814f38f2020-10-12 17:33:06 -070093#include "private/bionic_defs.h"
94
Yabin Cui58d33a52014-12-16 17:03:44 -080095#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
96 (ok)(nm) != 0)
97#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
98#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
99
100#define addalias(d, s, arr, siz) do { \
101 if (d >= &arr[siz]) { \
102 char **xptr = realloc(arr, (siz + 10) * sizeof(*arr)); \
103 if (xptr == NULL) \
104 goto nospc; \
105 d = xptr + (d - arr); \
106 arr = xptr; \
107 siz += 10; \
108 } \
109 *d++ = s; \
110} while (/*CONSTCOND*/0)
111
112#define setup(arr, siz) do { \
113 arr = malloc((siz = 10) * sizeof(*arr)); \
114 if (arr == NULL) \
115 goto nospc; \
116} while (/*CONSTCOND*/0)
117
Mattias Falkc63e5902011-08-23 14:34:14 +0200118// This should be synchronized to ResponseCode.h
119static const int DnsProxyQueryResult = 222;
120
Elliott Hughes68c27552014-07-07 09:44:17 -0700121static const char AskedForGot[] =
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
123
Ben Schwartz50178052017-04-24 17:57:11 -0400124static const struct android_net_context NETCONTEXT_UNSET = {
125 .app_mark = MARK_UNSET,
126 .app_netid = NETID_UNSET,
127 .dns_mark = MARK_UNSET,
128 .dns_netid = NETID_UNSET,
129 .uid = NET_CONTEXT_INVALID_UID
130};
131
Ben Schwartz47fb0e82018-01-31 13:35:03 -0500132#define MAXPACKET (8*1024)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133
134typedef union {
135 HEADER hdr;
136 u_char buf[MAXPACKET];
137} querybuf;
138
139typedef union {
140 int32_t al;
141 char ac;
142} align;
143
144#ifdef DEBUG
Yabin Cui58d33a52014-12-16 17:03:44 -0800145static void debugprintf(const char *, res_state, ...)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146 __attribute__((__format__(__printf__, 1, 3)));
147#endif
148static struct hostent *getanswer(const querybuf *, int, const char *, int,
Yabin Cui58d33a52014-12-16 17:03:44 -0800149 res_state, struct hostent *, char *, size_t, int *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150static void map_v4v6_address(const char *, char *);
151static void map_v4v6_hostent(struct hostent *, char **, char *);
152static void addrsort(char **, int, res_state);
153
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154void ht_sethostent(int);
155void ht_endhostent(void);
156struct hostent *ht_gethostbyname(char *);
157struct hostent *ht_gethostbyaddr(const char *, int, int);
158void dns_service(void);
159#undef dn_skipname
160int dn_skipname(const u_char *, const u_char *);
Jim Huange5c35e02010-09-27 23:37:10 +0800161static int _dns_gethtbyaddr(void *, void *, va_list);
162static int _dns_gethtbyname(void *, void *, va_list);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163
Yabin Cui58d33a52014-12-16 17:03:44 -0800164static struct hostent *gethostbyname_internal(const char *, int, res_state,
Ben Schwartzdd878fe2017-05-22 10:19:25 -0400165 struct hostent *, char *, size_t, int *, const struct android_net_context *);
Ben Schwartz50178052017-04-24 17:57:11 -0400166static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t,
167 int, struct hostent *, char *, size_t, int *, const struct android_net_context *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168
169static const ns_src default_dns_files[] = {
170 { NSSRC_FILES, NS_SUCCESS },
171 { NSSRC_DNS, NS_SUCCESS },
172 { 0, 0 }
173};
174
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700175static int h_errno_to_result(int* herrno_p) {
176 // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
177 if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
178 errno = ERANGE;
179 return errno;
180 }
181 // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
182 return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
183}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184
185#ifdef DEBUG
186static void
Yabin Cui58d33a52014-12-16 17:03:44 -0800187debugprintf(const char *msg, res_state res, ...)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188{
Yabin Cui58d33a52014-12-16 17:03:44 -0800189 _DIAGASSERT(msg != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
191 if (res->options & RES_DEBUG) {
192 int save = errno;
193 va_list ap;
194
195 va_start (ap, res);
196 vprintf(msg, ap);
197 va_end (ap);
198
199 errno = save;
200 }
201}
202#else
Yabin Cui58d33a52014-12-16 17:03:44 -0800203# define debugprintf(msg, res, num) /*nada*/
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204#endif
205
206#define BOUNDED_INCR(x) \
207 do { \
Elliott Hughes87c0dba2016-11-14 13:56:32 -0800208 BOUNDS_CHECK(cp, x); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209 cp += (x); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210 } while (/*CONSTCOND*/0)
211
212#define BOUNDS_CHECK(ptr, count) \
213 do { \
Elliott Hughes87c0dba2016-11-14 13:56:32 -0800214 if (eom - (ptr) < (count)) \
Yabin Cui58d33a52014-12-16 17:03:44 -0800215 goto no_recovery; \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216 } while (/*CONSTCOND*/0)
217
218static struct hostent *
219getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
Yabin Cui58d33a52014-12-16 17:03:44 -0800220 res_state res, struct hostent *hent, char *buf, size_t buflen, int *he)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221{
222 const HEADER *hp;
223 const u_char *cp;
224 int n;
Yabin Cui58d33a52014-12-16 17:03:44 -0800225 size_t qlen;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226 const u_char *eom, *erdata;
227 char *bp, **ap, **hap, *ep;
228 int type, class, ancount, qdcount;
229 int haveanswer, had_error;
230 int toobig = 0;
231 char tbuf[MAXDNAME];
Yabin Cui58d33a52014-12-16 17:03:44 -0800232 char **aliases;
233 size_t maxaliases;
234 char *addr_ptrs[MAXADDRS];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235 const char *tname;
236 int (*name_ok)(const char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237
Yabin Cui58d33a52014-12-16 17:03:44 -0800238 _DIAGASSERT(answer != NULL);
239 _DIAGASSERT(qname != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240
241 tname = qname;
Yabin Cui58d33a52014-12-16 17:03:44 -0800242 hent->h_name = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243 eom = answer->buf + anslen;
244 switch (qtype) {
245 case T_A:
246 case T_AAAA:
247 name_ok = res_hnok;
248 break;
249 case T_PTR:
250 name_ok = res_dnok;
251 break;
252 default:
Yabin Cui58d33a52014-12-16 17:03:44 -0800253 *he = NO_RECOVERY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800254 return NULL; /* XXX should be abort(); */
255 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800256
257 setup(aliases, maxaliases);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258 /*
259 * find first satisfactory answer
260 */
261 hp = &answer->hdr;
262 ancount = ntohs(hp->ancount);
263 qdcount = ntohs(hp->qdcount);
Yabin Cui58d33a52014-12-16 17:03:44 -0800264 bp = buf;
265 ep = buf + buflen;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800266 cp = answer->buf;
267 BOUNDED_INCR(HFIXEDSZ);
Yabin Cui58d33a52014-12-16 17:03:44 -0800268 if (qdcount != 1)
269 goto no_recovery;
270
271 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
272 if ((n < 0) || !maybe_ok(res, bp, name_ok))
273 goto no_recovery;
274
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800275 BOUNDED_INCR(n + QFIXEDSZ);
276 if (qtype == T_A || qtype == T_AAAA) {
277 /* res_send() has already verified that the query name is the
278 * same as the one we sent; this just gets the expanded name
279 * (i.e., with the succeeding search-domain tacked on).
280 */
Yabin Cui58d33a52014-12-16 17:03:44 -0800281 n = (int)strlen(bp) + 1; /* for the \0 */
282 if (n >= MAXHOSTNAMELEN)
283 goto no_recovery;
284 hent->h_name = bp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285 bp += n;
286 /* The qname can be abbreviated, but h_name is now absolute. */
Yabin Cui58d33a52014-12-16 17:03:44 -0800287 qname = hent->h_name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800289 hent->h_aliases = ap = aliases;
290 hent->h_addr_list = hap = addr_ptrs;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291 *ap = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292 *hap = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293 haveanswer = 0;
294 had_error = 0;
295 while (ancount-- > 0 && cp < eom && !had_error) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800296 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
297 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800298 had_error++;
299 continue;
300 }
301 cp += n; /* name */
302 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
303 type = _getshort(cp);
304 cp += INT16SZ; /* type */
305 class = _getshort(cp);
306 cp += INT16SZ + INT32SZ; /* class, TTL */
307 n = _getshort(cp);
308 cp += INT16SZ; /* len */
309 BOUNDS_CHECK(cp, n);
310 erdata = cp + n;
311 if (class != C_IN) {
312 /* XXX - debug? syslog? */
313 cp += n;
314 continue; /* XXX - had_error++ ? */
315 }
316 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800317 n = dn_expand(answer->buf, eom, cp, tbuf,
318 (int)sizeof tbuf);
319 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320 had_error++;
321 continue;
322 }
323 cp += n;
Yabin Cui58d33a52014-12-16 17:03:44 -0800324 if (cp != erdata)
325 goto no_recovery;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800326 /* Store alias. */
Yabin Cui58d33a52014-12-16 17:03:44 -0800327 addalias(ap, bp, aliases, maxaliases);
328 n = (int)strlen(bp) + 1; /* for the \0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329 if (n >= MAXHOSTNAMELEN) {
330 had_error++;
331 continue;
332 }
333 bp += n;
334 /* Get canonical name. */
Yabin Cui58d33a52014-12-16 17:03:44 -0800335 n = (int)strlen(tbuf) + 1; /* for the \0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
337 had_error++;
338 continue;
339 }
340 strlcpy(bp, tbuf, (size_t)(ep - bp));
Yabin Cui58d33a52014-12-16 17:03:44 -0800341 hent->h_name = bp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342 bp += n;
343 continue;
344 }
345 if (qtype == T_PTR && type == T_CNAME) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800346 n = dn_expand(answer->buf, eom, cp, tbuf,
347 (int)sizeof tbuf);
348 if (n < 0 || !maybe_dnok(res, tbuf)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349 had_error++;
350 continue;
351 }
352 cp += n;
Yabin Cui58d33a52014-12-16 17:03:44 -0800353 if (cp != erdata)
354 goto no_recovery;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355 /* Get canonical name. */
Yabin Cui58d33a52014-12-16 17:03:44 -0800356 n = (int)strlen(tbuf) + 1; /* for the \0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800357 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
358 had_error++;
359 continue;
360 }
361 strlcpy(bp, tbuf, (size_t)(ep - bp));
362 tname = bp;
363 bp += n;
364 continue;
365 }
366 if (type != qtype) {
367 if (type != T_KEY && type != T_SIG)
368 syslog(LOG_NOTICE|LOG_AUTH,
369 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
370 qname, p_class(C_IN), p_type(qtype),
371 p_type(type));
372 cp += n;
373 continue; /* XXX - had_error++ ? */
374 }
375 switch (type) {
376 case T_PTR:
377 if (strcasecmp(tname, bp) != 0) {
378 syslog(LOG_NOTICE|LOG_AUTH,
379 AskedForGot, qname, bp);
380 cp += n;
381 continue; /* XXX - had_error++ ? */
382 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800383 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
384 if ((n < 0) || !maybe_hnok(res, bp)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385 had_error++;
386 break;
387 }
388#if MULTI_PTRS_ARE_ALIASES
389 cp += n;
Yabin Cui58d33a52014-12-16 17:03:44 -0800390 if (cp != erdata)
391 goto no_recovery;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392 if (!haveanswer)
Yabin Cui58d33a52014-12-16 17:03:44 -0800393 hent->h_name = bp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394 else
Yabin Cui58d33a52014-12-16 17:03:44 -0800395 addalias(ap, bp, aliases, maxaliases);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396 if (n != -1) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800397 n = (int)strlen(bp) + 1; /* for the \0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398 if (n >= MAXHOSTNAMELEN) {
399 had_error++;
400 break;
401 }
402 bp += n;
403 }
404 break;
405#else
Yabin Cui58d33a52014-12-16 17:03:44 -0800406 hent->h_name = bp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800407 if (res->options & RES_USE_INET6) {
408 n = strlen(bp) + 1; /* for the \0 */
409 if (n >= MAXHOSTNAMELEN) {
410 had_error++;
411 break;
412 }
413 bp += n;
Yabin Cui58d33a52014-12-16 17:03:44 -0800414 map_v4v6_hostent(hent, &bp, ep);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800415 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800416 goto success;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800417#endif
418 case T_A:
419 case T_AAAA:
Yabin Cui58d33a52014-12-16 17:03:44 -0800420 if (strcasecmp(hent->h_name, bp) != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800421 syslog(LOG_NOTICE|LOG_AUTH,
Yabin Cui58d33a52014-12-16 17:03:44 -0800422 AskedForGot, hent->h_name, bp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423 cp += n;
424 continue; /* XXX - had_error++ ? */
425 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800426 if (n != hent->h_length) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800427 cp += n;
428 continue;
429 }
430 if (type == T_AAAA) {
431 struct in6_addr in6;
Yabin Cui58d33a52014-12-16 17:03:44 -0800432 memcpy(&in6, cp, NS_IN6ADDRSZ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
434 cp += n;
435 continue;
436 }
437 }
438 if (!haveanswer) {
439 int nn;
440
Yabin Cui58d33a52014-12-16 17:03:44 -0800441 hent->h_name = bp;
442 nn = (int)strlen(bp) + 1; /* for the \0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800443 bp += nn;
444 }
445
446 bp += sizeof(align) -
447 (size_t)((u_long)bp % sizeof(align));
448
Yabin Cui58d33a52014-12-16 17:03:44 -0800449 if (bp + n >= ep) {
450 debugprintf("size (%d) too big\n", res, n);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451 had_error++;
452 continue;
453 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800454 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
455 if (!toobig++) {
456 debugprintf("Too many addresses (%d)\n",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800457 res, MAXADDRS);
Yabin Cui58d33a52014-12-16 17:03:44 -0800458 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800459 cp += n;
460 continue;
461 }
462 (void)memcpy(*hap++ = bp, cp, (size_t)n);
463 bp += n;
464 cp += n;
Yabin Cui58d33a52014-12-16 17:03:44 -0800465 if (cp != erdata)
466 goto no_recovery;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467 break;
468 default:
469 abort();
470 }
471 if (!had_error)
472 haveanswer++;
473 }
474 if (haveanswer) {
475 *ap = NULL;
476 *hap = NULL;
477 /*
478 * Note: we sort even if host can take only one address
479 * in its return structures - should give it the "best"
480 * address in that case, not some random one
481 */
482 if (res->nsort && haveanswer > 1 && qtype == T_A)
Yabin Cui58d33a52014-12-16 17:03:44 -0800483 addrsort(addr_ptrs, haveanswer, res);
484 if (!hent->h_name) {
485 n = (int)strlen(qname) + 1; /* for the \0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800486 if (n > ep - bp || n >= MAXHOSTNAMELEN)
487 goto no_recovery;
488 strlcpy(bp, qname, (size_t)(ep - bp));
Yabin Cui58d33a52014-12-16 17:03:44 -0800489 hent->h_name = bp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800490 bp += n;
491 }
492 if (res->options & RES_USE_INET6)
Yabin Cui58d33a52014-12-16 17:03:44 -0800493 map_v4v6_hostent(hent, &bp, ep);
494 goto success;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800495 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800496no_recovery:
497 free(aliases);
498 *he = NO_RECOVERY;
499 return NULL;
500success:
501 bp = (char *)ALIGN(bp);
502 n = (int)(ap - aliases);
503 qlen = (n + 1) * sizeof(*hent->h_aliases);
504 if ((size_t)(ep - bp) < qlen)
505 goto nospc;
506 hent->h_aliases = (void *)bp;
507 memcpy(bp, aliases, qlen);
508 free(aliases);
509 aliases = NULL;
510
511 bp += qlen;
512 n = (int)(hap - addr_ptrs);
513 qlen = (n + 1) * sizeof(*hent->h_addr_list);
514 if ((size_t)(ep - bp) < qlen)
515 goto nospc;
516 hent->h_addr_list = (void *)bp;
517 memcpy(bp, addr_ptrs, qlen);
518 *he = NETDB_SUCCESS;
519 return hent;
520nospc:
521 free(aliases);
522 errno = ENOSPC;
523 *he = NETDB_INTERNAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524 return NULL;
525}
526
Yabin Cui58d33a52014-12-16 17:03:44 -0800527/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800528int
529gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen,
Yabin Cui58d33a52014-12-16 17:03:44 -0800530 struct hostent **result, int *errorp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800531{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800532 res_state res = __res_get_state();
Yabin Cui58d33a52014-12-16 17:03:44 -0800533 if (res == NULL) {
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700534 *result = NULL;
Yabin Cui58d33a52014-12-16 17:03:44 -0800535 *errorp = NETDB_INTERNAL;
536 return -1;
537 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800538
Yabin Cui58d33a52014-12-16 17:03:44 -0800539 _DIAGASSERT(name != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540
541 if (res->options & RES_USE_INET6) {
Ben Schwartzdd878fe2017-05-22 10:19:25 -0400542 *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
543 &NETCONTEXT_UNSET);
Yabin Cui58d33a52014-12-16 17:03:44 -0800544 if (*result) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800545 __res_put_state(res);
Yabin Cui58d33a52014-12-16 17:03:44 -0800546 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800547 }
548 }
Ben Schwartzdd878fe2017-05-22 10:19:25 -0400549 *result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp,
550 &NETCONTEXT_UNSET);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700551 return h_errno_to_result(errorp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800552}
553
Yabin Cui58d33a52014-12-16 17:03:44 -0800554/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
555int
556gethostbyname2_r(const char *name, int af, struct hostent *hp, char *buf,
557 size_t buflen, struct hostent **result, int *errorp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800558{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800559 res_state res = __res_get_state();
Yabin Cui58d33a52014-12-16 17:03:44 -0800560 if (res == NULL) {
561 *result = NULL;
562 *errorp = NETDB_INTERNAL;
563 return -1;
564 }
Ben Schwartzdd878fe2017-05-22 10:19:25 -0400565 *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp,
566 &NETCONTEXT_UNSET);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700567 return h_errno_to_result(errorp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800568}
569
570static struct hostent *
Yabin Cui58d33a52014-12-16 17:03:44 -0800571android_read_hostent(FILE* proxy, struct hostent* hp, char* hbuf, size_t hbuflen, int *he)
Mattias Falkc63e5902011-08-23 14:34:14 +0200572{
573 uint32_t size;
574 char buf[4];
575 if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) return NULL;
576
Elliott Hughes9773fa32014-12-10 14:56:46 -0800577 // This is reading serialized data from system/netd/server/DnsProxyListener.cpp
578 // and changes here need to be matched there.
Mattias Falkc63e5902011-08-23 14:34:14 +0200579 int result_code = strtol(buf, NULL, 10);
580 if (result_code != DnsProxyQueryResult) {
581 fread(&size, 1, sizeof(size), proxy);
Yabin Cui58d33a52014-12-16 17:03:44 -0800582 *he = HOST_NOT_FOUND;
Mattias Falkc63e5902011-08-23 14:34:14 +0200583 return NULL;
584 }
585
586 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
587 size = ntohl(size);
Mattias Falkc63e5902011-08-23 14:34:14 +0200588
Yabin Cui58d33a52014-12-16 17:03:44 -0800589 memset(hp, 0, sizeof(*hp));
590 char *ptr = hbuf;
591 char *hbuf_end = hbuf + hbuflen;
592
593 if (ptr + size > hbuf_end) {
594 goto nospc;
595 }
Mattias Falkc63e5902011-08-23 14:34:14 +0200596 if (fread(ptr, 1, size, proxy) != size) return NULL;
Yabin Cui58d33a52014-12-16 17:03:44 -0800597 hp->h_name = ptr;
Mattias Falkc63e5902011-08-23 14:34:14 +0200598 ptr += size;
Mattias Falkc63e5902011-08-23 14:34:14 +0200599
Yabin Cui58d33a52014-12-16 17:03:44 -0800600 char *aliases_ptrs[MAXALIASES];
601 char **aliases = &aliases_ptrs[0];
602
Mattias Falkc63e5902011-08-23 14:34:14 +0200603 while (1) {
604 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
605 size = ntohl(size);
606
607 if (size == 0) {
608 *aliases = NULL;
609 break;
610 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800611 if (ptr + size > hbuf_end) {
612 goto nospc;
613 }
Mattias Falkc63e5902011-08-23 14:34:14 +0200614 if (fread(ptr, 1, size, proxy) != size) return NULL;
Yabin Cui58d33a52014-12-16 17:03:44 -0800615 if (aliases < &aliases_ptrs[MAXALIASES - 1]) {
616 *aliases++ = ptr;
617 }
Mattias Falkc63e5902011-08-23 14:34:14 +0200618 ptr += size;
619 }
620
Elliott Hughes65dd8582015-07-06 14:16:12 -0700621 // Fix alignment after variable-length data.
622 ptr = (char*)ALIGN(ptr);
623
Yabin Cui58d33a52014-12-16 17:03:44 -0800624 int aliases_len = ((int)(aliases - aliases_ptrs) + 1) * sizeof(*hp->h_aliases);
625 if (ptr + aliases_len > hbuf_end) {
626 goto nospc;
627 }
628 hp->h_aliases = (void*)ptr;
629 memcpy(ptr, aliases_ptrs, aliases_len);
630 ptr += aliases_len;
Mattias Falkc63e5902011-08-23 14:34:14 +0200631
632 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
Yabin Cui58d33a52014-12-16 17:03:44 -0800633 hp->h_addrtype = ntohl(size);
Mattias Falkc63e5902011-08-23 14:34:14 +0200634
Yabin Cui58d33a52014-12-16 17:03:44 -0800635 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
636 hp->h_length = ntohl(size);
637
638 char *addr_ptrs[MAXADDRS];
639 char **addr_p = &addr_ptrs[0];
640
Mattias Falkc63e5902011-08-23 14:34:14 +0200641 while (1) {
642 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
643 size = ntohl(size);
644 if (size == 0) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800645 *addr_p = NULL;
Mattias Falkc63e5902011-08-23 14:34:14 +0200646 break;
647 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800648 if (ptr + size > hbuf_end) {
649 goto nospc;
650 }
Mattias Falkc63e5902011-08-23 14:34:14 +0200651 if (fread(ptr, 1, size, proxy) != size) return NULL;
Yabin Cui58d33a52014-12-16 17:03:44 -0800652 if (addr_p < &addr_ptrs[MAXADDRS - 1]) {
653 *addr_p++ = ptr;
654 }
Mattias Falkc63e5902011-08-23 14:34:14 +0200655 ptr += size;
656 }
657
Elliott Hughes65dd8582015-07-06 14:16:12 -0700658 // Fix alignment after variable-length data.
659 ptr = (char*)ALIGN(ptr);
660
Yabin Cui58d33a52014-12-16 17:03:44 -0800661 int addrs_len = ((int)(addr_p - addr_ptrs) + 1) * sizeof(*hp->h_addr_list);
662 if (ptr + addrs_len > hbuf_end) {
663 goto nospc;
664 }
665 hp->h_addr_list = (void*)ptr;
666 memcpy(ptr, addr_ptrs, addrs_len);
667 *he = NETDB_SUCCESS;
668 return hp;
669
670nospc:
671 *he = NETDB_INTERNAL;
672 errno = ENOSPC;
673 return NULL;
Mattias Falkc63e5902011-08-23 14:34:14 +0200674}
675
Mattias Falkc63e5902011-08-23 14:34:14 +0200676static struct hostent *
Yabin Cui58d33a52014-12-16 17:03:44 -0800677gethostbyname_internal_real(const char *name, int af, res_state res, struct hostent *hp, char *buf,
678 size_t buflen, int *he)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800679{
680 const char *cp;
Yabin Cui58d33a52014-12-16 17:03:44 -0800681 struct getnamaddr info;
682 char hbuf[MAXHOSTNAMELEN];
683 size_t size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684 static const ns_dtab dtab[] = {
Yabin Cui58d33a52014-12-16 17:03:44 -0800685 NS_FILES_CB(_hf_gethtbyname, NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686 { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
Yabin Cui58d33a52014-12-16 17:03:44 -0800687 NS_NIS_CB(_yp_gethtbyname, NULL)
688 NS_NULL_CB
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800689 };
690
Yabin Cui58d33a52014-12-16 17:03:44 -0800691 _DIAGASSERT(name != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800692
693 switch (af) {
694 case AF_INET:
Yabin Cui58d33a52014-12-16 17:03:44 -0800695 size = NS_INADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800696 break;
697 case AF_INET6:
Yabin Cui58d33a52014-12-16 17:03:44 -0800698 size = NS_IN6ADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800699 break;
700 default:
Yabin Cui58d33a52014-12-16 17:03:44 -0800701 *he = NETDB_INTERNAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800702 errno = EAFNOSUPPORT;
703 return NULL;
704 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800705 if (buflen < size)
706 goto nospc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800707
Yabin Cui58d33a52014-12-16 17:03:44 -0800708 hp->h_addrtype = af;
709 hp->h_length = (int)size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800710
711 /*
712 * if there aren't any dots, it could be a user-level alias.
713 * this is also done in res_nquery() since we are not the only
714 * function that looks up host names.
715 */
Yabin Cui58d33a52014-12-16 17:03:44 -0800716 if (!strchr(name, '.') && (cp = res_hostalias(res, name,
717 hbuf, sizeof(hbuf))))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800718 name = cp;
719
720 /*
721 * disallow names consisting only of digits/dots, unless
722 * they end in a dot.
723 */
724 if (isdigit((u_char) name[0]))
725 for (cp = name;; ++cp) {
726 if (!*cp) {
727 if (*--cp == '.')
728 break;
729 /*
730 * All-numeric, no dot at the end.
731 * Fake up a hostent as if we'd actually
732 * done a lookup.
733 */
Yabin Cui58d33a52014-12-16 17:03:44 -0800734 goto fake;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800735 }
736 if (!isdigit((u_char) *cp) && *cp != '.')
737 break;
738 }
739 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
740 name[0] == ':')
741 for (cp = name;; ++cp) {
742 if (!*cp) {
743 if (*--cp == '.')
744 break;
745 /*
746 * All-IPv6-legal, no dot at the end.
747 * Fake up a hostent as if we'd actually
748 * done a lookup.
749 */
Yabin Cui58d33a52014-12-16 17:03:44 -0800750 goto fake;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800751 }
752 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
753 break;
754 }
755
Yabin Cui58d33a52014-12-16 17:03:44 -0800756 *he = NETDB_INTERNAL;
757 info.hp = hp;
758 info.buf = buf;
759 info.buflen = buflen;
760 info.he = he;
761 if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname",
762 default_dns_files, name, strlen(name), af) != NS_SUCCESS)
763 return NULL;
764 *he = NETDB_SUCCESS;
765 return hp;
766nospc:
767 *he = NETDB_INTERNAL;
768 errno = ENOSPC;
769 return NULL;
770fake:
771 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
772 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
773
774 hp->h_aliases[0] = NULL;
775 if (size > buflen)
776 goto nospc;
777
778 if (inet_pton(af, name, buf) <= 0) {
779 *he = HOST_NOT_FOUND;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780 return NULL;
Mattias Falkc63e5902011-08-23 14:34:14 +0200781 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800782 hp->h_addr_list[0] = buf;
783 hp->h_addr_list[1] = NULL;
784 buf += size;
785 buflen -= size;
786 HENT_SCOPY(hp->h_name, name, buf, buflen);
787 if (res->options & RES_USE_INET6)
788 map_v4v6_hostent(hp, &buf, buf + buflen);
789 *he = NETDB_SUCCESS;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800790 return hp;
791}
792
Mattias Falkc63e5902011-08-23 14:34:14 +0200793static struct hostent *
Yabin Cui58d33a52014-12-16 17:03:44 -0800794gethostbyname_internal(const char *name, int af, res_state res, struct hostent *hp, char *hbuf,
Ben Schwartzdd878fe2017-05-22 10:19:25 -0400795 size_t hbuflen, int *errorp, const struct android_net_context *netcontext)
Mattias Falkc63e5902011-08-23 14:34:14 +0200796{
Luke Huange3ed8922018-11-19 16:59:08 +0800797 FILE* proxy = fdopen(__netdClientDispatch.dnsOpenProxy(), "r+");
Elliott Hughes9773fa32014-12-10 14:56:46 -0800798 if (proxy == NULL) {
799 // Either we're not supposed to be using the proxy or the proxy is unavailable.
Ben Schwartz90a83be2017-04-24 17:57:11 -0400800 res_setnetcontext(res, netcontext);
Yabin Cui58d33a52014-12-16 17:03:44 -0800801 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Mattias Falkc63e5902011-08-23 14:34:14 +0200802 }
Ben Schwartzdd878fe2017-05-22 10:19:25 -0400803 unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
Paul Jensen5240b562014-05-15 14:43:07 -0400804
Elliott Hughes9773fa32014-12-10 14:56:46 -0800805 // This is writing to system/netd/server/DnsProxyListener.cpp and changes
806 // here need to be matched there.
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500807 if (fprintf(proxy, "gethostbyname %u %s %d",
808 netid,
Mattias Falkc63e5902011-08-23 14:34:14 +0200809 name == NULL ? "^" : name,
810 af) < 0) {
Elliott Hughes9773fa32014-12-10 14:56:46 -0800811 fclose(proxy);
812 return NULL;
Mattias Falkc63e5902011-08-23 14:34:14 +0200813 }
814
815 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
Mattias Falkc63e5902011-08-23 14:34:14 +0200816 fclose(proxy);
Elliott Hughes9773fa32014-12-10 14:56:46 -0800817 return NULL;
Mattias Falkc63e5902011-08-23 14:34:14 +0200818 }
Elliott Hughes9773fa32014-12-10 14:56:46 -0800819
Yabin Cui58d33a52014-12-16 17:03:44 -0800820 struct hostent* result = android_read_hostent(proxy, hp, hbuf, hbuflen, errorp);
Elliott Hughes9773fa32014-12-10 14:56:46 -0800821 fclose(proxy);
Mattias Falkc63e5902011-08-23 14:34:14 +0200822 return result;
823}
824
Yabin Cui58d33a52014-12-16 17:03:44 -0800825/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
826int gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp, char *buf,
827 size_t buflen, struct hostent **result, int *h_errnop)
828{
Ben Schwartz50178052017-04-24 17:57:11 -0400829 *result = android_gethostbyaddrfornetcontext_proxy_internal(
830 addr, len, af, hp, buf, buflen, h_errnop, &NETCONTEXT_UNSET);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700831 return h_errno_to_result(h_errnop);
Yabin Cui58d33a52014-12-16 17:03:44 -0800832}
Mattias Falkc63e5902011-08-23 14:34:14 +0200833
Elliott Hughes9773fa32014-12-10 14:56:46 -0800834static struct hostent *
Ben Schwartz50178052017-04-24 17:57:11 -0400835android_gethostbyaddrfornetcontext_real(const void *addr, socklen_t len, int af, struct hostent *hp,
836 char *buf, size_t buflen, int *he,
837 const struct android_net_context *netcontext)
Yabin Cui58d33a52014-12-16 17:03:44 -0800838{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800839 const u_char *uaddr = (const u_char *)addr;
840 socklen_t size;
Yabin Cui58d33a52014-12-16 17:03:44 -0800841 struct getnamaddr info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800842 static const ns_dtab dtab[] = {
Yabin Cui58d33a52014-12-16 17:03:44 -0800843 NS_FILES_CB(_hf_gethtbyaddr, NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800844 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
Yabin Cui58d33a52014-12-16 17:03:44 -0800845 NS_NIS_CB(_yp_gethtbyaddr, NULL)
846 NS_NULL_CB
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800847 };
848
Yabin Cui58d33a52014-12-16 17:03:44 -0800849 _DIAGASSERT(addr != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800850
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700851 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
852 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) ||
853 IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800854 *he = HOST_NOT_FOUND;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800855 return NULL;
856 }
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700857 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
858 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) ||
859 IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800860 /* Unmap. */
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700861 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
862 addr = uaddr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800863 af = AF_INET;
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700864 len = NS_INADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800865 }
866 switch (af) {
867 case AF_INET:
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700868 size = NS_INADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800869 break;
870 case AF_INET6:
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700871 size = NS_IN6ADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800872 break;
873 default:
874 errno = EAFNOSUPPORT;
Yabin Cui58d33a52014-12-16 17:03:44 -0800875 *he = NETDB_INTERNAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800876 return NULL;
877 }
878 if (size != len) {
879 errno = EINVAL;
Yabin Cui58d33a52014-12-16 17:03:44 -0800880 *he = NETDB_INTERNAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800881 return NULL;
882 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800883 info.hp = hp;
884 info.buf = buf;
885 info.buflen = buflen;
886 info.he = he;
887 *he = NETDB_INTERNAL;
888 if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr",
Ben Schwartz50178052017-04-24 17:57:11 -0400889 default_dns_files, uaddr, len, af, netcontext) != NS_SUCCESS)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800890 return NULL;
Yabin Cui58d33a52014-12-16 17:03:44 -0800891 *he = NETDB_SUCCESS;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800892 return hp;
893}
894
Yabin Cui58d33a52014-12-16 17:03:44 -0800895static struct hostent*
Ben Schwartz50178052017-04-24 17:57:11 -0400896android_gethostbyaddrfornetcontext_proxy_internal(const void* addr, socklen_t len, int af,
Yabin Cui58d33a52014-12-16 17:03:44 -0800897 struct hostent *hp, char *hbuf, size_t hbuflen, int *he,
Ben Schwartz50178052017-04-24 17:57:11 -0400898 const struct android_net_context *netcontext)
Yabin Cui58d33a52014-12-16 17:03:44 -0800899{
Luke Huange3ed8922018-11-19 16:59:08 +0800900 FILE* proxy = fdopen(__netdClientDispatch.dnsOpenProxy(), "r+");
Elliott Hughes9773fa32014-12-10 14:56:46 -0800901 if (proxy == NULL) {
902 // Either we're not supposed to be using the proxy or the proxy is unavailable.
Ben Schwartz50178052017-04-24 17:57:11 -0400903 return android_gethostbyaddrfornetcontext_real(addr,len, af, hp, hbuf, hbuflen, he, netcontext);
Elliott Hughes9773fa32014-12-10 14:56:46 -0800904 }
Elliott Hughes9773fa32014-12-10 14:56:46 -0800905 char buf[INET6_ADDRSTRLEN]; //big enough for IPv4 and IPv6
906 const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf));
907 if (addrStr == NULL) {
908 fclose(proxy);
909 return NULL;
910 }
911
Ben Schwartz50178052017-04-24 17:57:11 -0400912 unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
Elliott Hughes9773fa32014-12-10 14:56:46 -0800913
914 if (fprintf(proxy, "gethostbyaddr %s %d %d %u",
915 addrStr, len, af, netid) < 0) {
916 fclose(proxy);
917 return NULL;
918 }
919
920 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
921 fclose(proxy);
922 return NULL;
923 }
924
Yabin Cui58d33a52014-12-16 17:03:44 -0800925 struct hostent *result = android_read_hostent(proxy, hp, hbuf, hbuflen, he);
Elliott Hughes9773fa32014-12-10 14:56:46 -0800926 fclose(proxy);
927 return result;
928}
929
Yabin Cui58d33a52014-12-16 17:03:44 -0800930struct hostent*
931netbsd_gethostent_r(FILE *hf, struct hostent *hent, char *buf, size_t buflen, int *he)
Mattias Falkc63e5902011-08-23 14:34:14 +0200932{
Yabin Cui58d33a52014-12-16 17:03:44 -0800933 char *p, *name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800934 char *cp, **q;
935 int af, len;
Yabin Cui58d33a52014-12-16 17:03:44 -0800936 size_t anum;
937 char **aliases;
938 size_t maxaliases;
939 struct in6_addr host_addr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800940
Yabin Cui58d33a52014-12-16 17:03:44 -0800941 if (hf == NULL) {
942 *he = NETDB_INTERNAL;
943 errno = EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800944 return NULL;
945 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800946 p = NULL;
947 setup(aliases, maxaliases);
948
949 /* Allocate a new space to read file lines like upstream does.
950 * To keep reentrancy we cannot use __res_get_static()->hostbuf here,
951 * as the buffer may be used to store content for a previous hostent
952 * returned by non-reentrant functions like gethostbyname().
953 */
954 const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
955 if ((p = malloc(line_buf_size)) == NULL) {
956 goto nospc;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800957 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800958 for (;;) {
959 if (!fgets(p, line_buf_size, hf)) {
960 free(p);
961 free(aliases);
962 *he = HOST_NOT_FOUND;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800963 return NULL;
Yabin Cui58d33a52014-12-16 17:03:44 -0800964 }
965 if (*p == '#') {
966 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800967 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800968 if (!(cp = strpbrk(p, "#\n"))) {
969 continue;
970 }
971 *cp = '\0';
972 if (!(cp = strpbrk(p, " \t")))
973 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800974 *cp++ = '\0';
Yabin Cui58d33a52014-12-16 17:03:44 -0800975 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
976 af = AF_INET6;
977 len = NS_IN6ADDRSZ;
978 } else {
979 if (inet_pton(AF_INET, p, &host_addr) <= 0)
980 continue;
981
982 res_state res = __res_get_state();
983 if (res == NULL)
984 goto nospc;
985 if (res->options & RES_USE_INET6) {
986 map_v4v6_address(buf, buf);
987 af = AF_INET6;
988 len = NS_IN6ADDRSZ;
989 } else {
990 af = AF_INET;
991 len = NS_INADDRSZ;
992 }
993 __res_put_state(res);
994 }
995
996 /* if this is not something we're looking for, skip it. */
997 if (hent->h_addrtype != 0 && hent->h_addrtype != af)
998 continue;
999 if (hent->h_length != 0 && hent->h_length != len)
1000 continue;
1001
1002 while (*cp == ' ' || *cp == '\t')
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001003 cp++;
Yabin Cui58d33a52014-12-16 17:03:44 -08001004 if ((cp = strpbrk(name = cp, " \t")) != NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001005 *cp++ = '\0';
Yabin Cui58d33a52014-12-16 17:03:44 -08001006 q = aliases;
1007 while (cp && *cp) {
1008 if (*cp == ' ' || *cp == '\t') {
1009 cp++;
1010 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001011 }
Yabin Cui58d33a52014-12-16 17:03:44 -08001012 addalias(q, cp, aliases, maxaliases);
1013 if ((cp = strpbrk(cp, " \t")) != NULL)
1014 *cp++ = '\0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001015 }
Yabin Cui58d33a52014-12-16 17:03:44 -08001016 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001017 }
Yabin Cui58d33a52014-12-16 17:03:44 -08001018 hent->h_length = len;
1019 hent->h_addrtype = af;
1020 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
1021 anum = (size_t)(q - aliases);
1022 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
1023 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf,
1024 buflen);
1025 hent->h_addr_list[1] = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001026
Yabin Cui58d33a52014-12-16 17:03:44 -08001027 HENT_SCOPY(hent->h_name, name, buf, buflen);
1028 for (size_t i = 0; i < anum; i++)
1029 HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
1030 hent->h_aliases[anum] = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001031
Yabin Cui58d33a52014-12-16 17:03:44 -08001032 *he = NETDB_SUCCESS;
1033 free(p);
1034 free(aliases);
1035 return hent;
1036nospc:
1037 free(p);
1038 free(aliases);
1039 errno = ENOSPC;
1040 *he = NETDB_INTERNAL;
1041 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001042}
1043
1044static void
1045map_v4v6_address(const char *src, char *dst)
1046{
1047 u_char *p = (u_char *)dst;
Yabin Cui58d33a52014-12-16 17:03:44 -08001048 char tmp[NS_INADDRSZ];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001049 int i;
1050
Yabin Cui58d33a52014-12-16 17:03:44 -08001051 _DIAGASSERT(src != NULL);
1052 _DIAGASSERT(dst != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001053
1054 /* Stash a temporary copy so our caller can update in place. */
Yabin Cui58d33a52014-12-16 17:03:44 -08001055 (void)memcpy(tmp, src, NS_INADDRSZ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001056 /* Mark this ipv6 addr as a mapped ipv4. */
1057 for (i = 0; i < 10; i++)
1058 *p++ = 0x00;
1059 *p++ = 0xff;
1060 *p++ = 0xff;
1061 /* Retrieve the saved copy and we're done. */
Yabin Cui58d33a52014-12-16 17:03:44 -08001062 (void)memcpy(p, tmp, NS_INADDRSZ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001063}
1064
1065static void
1066map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
1067{
1068 char **ap;
1069
Yabin Cui58d33a52014-12-16 17:03:44 -08001070 _DIAGASSERT(hp != NULL);
1071 _DIAGASSERT(bpp != NULL);
1072 _DIAGASSERT(ep != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001073
Yabin Cui58d33a52014-12-16 17:03:44 -08001074 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001075 return;
1076 hp->h_addrtype = AF_INET6;
Yabin Cui58d33a52014-12-16 17:03:44 -08001077 hp->h_length = NS_IN6ADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001078 for (ap = hp->h_addr_list; *ap; ap++) {
Yabin Cui58d33a52014-12-16 17:03:44 -08001079 int i = (int)(sizeof(align) -
1080 (size_t)((u_long)*bpp % sizeof(align)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001081
Yabin Cui58d33a52014-12-16 17:03:44 -08001082 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001083 /* Out of memory. Truncate address list here. XXX */
1084 *ap = NULL;
1085 return;
1086 }
1087 *bpp += i;
1088 map_v4v6_address(*ap, *bpp);
1089 *ap = *bpp;
Yabin Cui58d33a52014-12-16 17:03:44 -08001090 *bpp += NS_IN6ADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001091 }
1092}
1093
1094static void
1095addrsort(char **ap, int num, res_state res)
1096{
1097 int i, j;
1098 char **p;
1099 short aval[MAXADDRS];
1100 int needsort = 0;
1101
Yabin Cui58d33a52014-12-16 17:03:44 -08001102 _DIAGASSERT(ap != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001103
1104 p = ap;
1105 for (i = 0; i < num; i++, p++) {
1106 for (j = 0 ; (unsigned)j < res->nsort; j++)
1107 if (res->sort_list[j].addr.s_addr ==
1108 (((struct in_addr *)(void *)(*p))->s_addr &
1109 res->sort_list[j].mask))
1110 break;
1111 aval[i] = j;
1112 if (needsort == 0 && i > 0 && j < aval[i-1])
1113 needsort = i;
1114 }
1115 if (!needsort)
1116 return;
1117
1118 while (needsort < num) {
1119 for (j = needsort - 1; j >= 0; j--) {
1120 if (aval[j] > aval[j+1]) {
1121 char *hp;
1122
1123 i = aval[j];
1124 aval[j] = aval[j+1];
1125 aval[j+1] = i;
1126
1127 hp = ap[j];
1128 ap[j] = ap[j+1];
1129 ap[j+1] = hp;
1130 } else
1131 break;
1132 }
1133 needsort++;
1134 }
1135}
1136
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001137/*ARGSUSED*/
Jim Huange5c35e02010-09-27 23:37:10 +08001138static int
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001139_dns_gethtbyname(void *rv, void *cb_data, va_list ap)
1140{
1141 querybuf *buf;
1142 int n, type;
1143 struct hostent *hp;
1144 const char *name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001145 res_state res;
Yabin Cui58d33a52014-12-16 17:03:44 -08001146 struct getnamaddr *info = rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001147
Yabin Cui58d33a52014-12-16 17:03:44 -08001148 _DIAGASSERT(rv != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001149
1150 name = va_arg(ap, char *);
Yabin Cui58d33a52014-12-16 17:03:44 -08001151 /* NOSTRICT skip string len */(void)va_arg(ap, int);
1152 info->hp->h_addrtype = va_arg(ap, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001153
Yabin Cui58d33a52014-12-16 17:03:44 -08001154 switch (info->hp->h_addrtype) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001155 case AF_INET:
Yabin Cui58d33a52014-12-16 17:03:44 -08001156 info->hp->h_length = NS_INADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001157 type = T_A;
1158 break;
1159 case AF_INET6:
Yabin Cui58d33a52014-12-16 17:03:44 -08001160 info->hp->h_length = NS_IN6ADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001161 type = T_AAAA;
1162 break;
1163 default:
1164 return NS_UNAVAIL;
1165 }
1166 buf = malloc(sizeof(*buf));
1167 if (buf == NULL) {
Yabin Cui58d33a52014-12-16 17:03:44 -08001168 *info->he = NETDB_INTERNAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001169 return NS_NOTFOUND;
1170 }
1171 res = __res_get_state();
1172 if (res == NULL) {
1173 free(buf);
1174 return NS_NOTFOUND;
1175 }
Yabin Cui58d33a52014-12-16 17:03:44 -08001176 n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001177 if (n < 0) {
1178 free(buf);
Yabin Cui58d33a52014-12-16 17:03:44 -08001179 debugprintf("res_nsearch failed (%d)\n", res, n);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001180 __res_put_state(res);
1181 return NS_NOTFOUND;
1182 }
Yabin Cui58d33a52014-12-16 17:03:44 -08001183 hp = getanswer(buf, n, name, type, res, info->hp, info->buf,
1184 info->buflen, info->he);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001185 free(buf);
1186 __res_put_state(res);
1187 if (hp == NULL)
Yabin Cui70692562014-12-19 10:10:04 -08001188 switch (*info->he) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001189 case HOST_NOT_FOUND:
1190 return NS_NOTFOUND;
1191 case TRY_AGAIN:
1192 return NS_TRYAGAIN;
1193 default:
1194 return NS_UNAVAIL;
1195 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001196 return NS_SUCCESS;
1197}
1198
1199/*ARGSUSED*/
Jim Huange5c35e02010-09-27 23:37:10 +08001200static int
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001201_dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1202{
1203 char qbuf[MAXDNAME + 1], *qp, *ep;
1204 int n;
1205 querybuf *buf;
1206 struct hostent *hp;
1207 const unsigned char *uaddr;
Yabin Cui58d33a52014-12-16 17:03:44 -08001208 int advance;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209 res_state res;
Yabin Cui58d33a52014-12-16 17:03:44 -08001210 char *bf;
1211 size_t blen;
1212 struct getnamaddr *info = rv;
Ben Schwartz50178052017-04-24 17:57:11 -04001213 const struct android_net_context *netcontext;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001214
Yabin Cui58d33a52014-12-16 17:03:44 -08001215 _DIAGASSERT(rv != NULL);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001216
1217 uaddr = va_arg(ap, unsigned char *);
Yabin Cui58d33a52014-12-16 17:03:44 -08001218 info->hp->h_length = va_arg(ap, int);
1219 info->hp->h_addrtype = va_arg(ap, int);
Ben Schwartz50178052017-04-24 17:57:11 -04001220 netcontext = va_arg(ap, const struct android_net_context *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001221
Yabin Cui58d33a52014-12-16 17:03:44 -08001222 switch (info->hp->h_addrtype) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001223 case AF_INET:
1224 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1225 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1226 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1227 break;
1228
1229 case AF_INET6:
1230 qp = qbuf;
1231 ep = qbuf + sizeof(qbuf) - 1;
Yabin Cui58d33a52014-12-16 17:03:44 -08001232 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001233 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
1234 uaddr[n] & 0xf,
1235 ((unsigned int)uaddr[n] >> 4) & 0xf);
1236 if (advance > 0 && qp + advance < ep)
1237 qp += advance;
1238 else {
Yabin Cui58d33a52014-12-16 17:03:44 -08001239 *info->he = NETDB_INTERNAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001240 return NS_NOTFOUND;
1241 }
1242 }
1243 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
Yabin Cui58d33a52014-12-16 17:03:44 -08001244 *info->he = NETDB_INTERNAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001245 return NS_NOTFOUND;
1246 }
1247 break;
1248 default:
Yabin Cui58d33a52014-12-16 17:03:44 -08001249 return NS_UNAVAIL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001250 }
1251
1252 buf = malloc(sizeof(*buf));
1253 if (buf == NULL) {
Yabin Cui58d33a52014-12-16 17:03:44 -08001254 *info->he = NETDB_INTERNAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001255 return NS_NOTFOUND;
1256 }
1257 res = __res_get_state();
1258 if (res == NULL) {
1259 free(buf);
1260 return NS_NOTFOUND;
1261 }
Ben Schwartz90a83be2017-04-24 17:57:11 -04001262 res_setnetcontext(res, netcontext);
Yabin Cui58d33a52014-12-16 17:03:44 -08001263 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001264 if (n < 0) {
1265 free(buf);
Yabin Cui58d33a52014-12-16 17:03:44 -08001266 debugprintf("res_nquery failed (%d)\n", res, n);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001267 __res_put_state(res);
1268 return NS_NOTFOUND;
1269 }
Yabin Cui58d33a52014-12-16 17:03:44 -08001270 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf,
1271 info->buflen, info->he);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001272 free(buf);
1273 if (hp == NULL) {
1274 __res_put_state(res);
Yabin Cui58d33a52014-12-16 17:03:44 -08001275 switch (*info->he) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001276 case HOST_NOT_FOUND:
1277 return NS_NOTFOUND;
1278 case TRY_AGAIN:
1279 return NS_TRYAGAIN;
1280 default:
1281 return NS_UNAVAIL;
1282 }
1283 }
Yabin Cui58d33a52014-12-16 17:03:44 -08001284
1285 bf = (void *)(hp->h_addr_list + 2);
1286 blen = (size_t)(bf - info->buf);
1287 if (blen + info->hp->h_length > info->buflen)
1288 goto nospc;
1289 hp->h_addr_list[0] = bf;
1290 hp->h_addr_list[1] = NULL;
1291 (void)memcpy(bf, uaddr, (size_t)info->hp->h_length);
1292 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
1293 if (blen + NS_IN6ADDRSZ > info->buflen)
1294 goto nospc;
1295 map_v4v6_address(bf, bf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001296 hp->h_addrtype = AF_INET6;
Yabin Cui58d33a52014-12-16 17:03:44 -08001297 hp->h_length = NS_IN6ADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001298 }
1299
1300 __res_put_state(res);
Yabin Cui58d33a52014-12-16 17:03:44 -08001301 *info->he = NETDB_SUCCESS;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001302 return NS_SUCCESS;
Yabin Cui58d33a52014-12-16 17:03:44 -08001303nospc:
Yabin Cui70692562014-12-19 10:10:04 -08001304 errno = ENOSPC;
Yabin Cui58d33a52014-12-16 17:03:44 -08001305 *info->he = NETDB_INTERNAL;
1306 return NS_UNAVAIL;
1307}
1308
1309#ifdef YP
1310/*ARGSUSED*/
1311static struct hostent *
1312_yp_hostent(char *line, int af, struct getnamaddr *info)
1313{
1314 struct in6_addr host_addrs[MAXADDRS];
1315 char **aliases;
1316 size_t maxaliases;
1317 char *p = line;
1318 char *cp, **q, *ptr;
1319 size_t len, anum, i;
1320 int addrok;
1321 int more;
1322 size_t naddrs;
1323 struct hostent *hp = info->hp;
1324
1325 _DIAGASSERT(line != NULL);
1326
1327 hp->h_name = NULL;
1328 hp->h_addrtype = af;
1329 switch (af) {
1330 case AF_INET:
1331 hp->h_length = NS_INADDRSZ;
1332 break;
1333 case AF_INET6:
1334 hp->h_length = NS_IN6ADDRSZ;
1335 break;
1336 default:
1337 return NULL;
1338 }
1339 setup(aliases, maxaliases);
1340 naddrs = 0;
1341 q = aliases;
1342
1343nextline:
1344 /* check for host_addrs overflow */
1345 if (naddrs >= __arraycount(host_addrs))
1346 goto done;
1347
1348 more = 0;
1349 cp = strpbrk(p, " \t");
1350 if (cp == NULL)
1351 goto done;
1352 *cp++ = '\0';
1353
1354 /* p has should have an address */
1355 addrok = inet_pton(af, p, &host_addrs[naddrs]);
1356 if (addrok != 1) {
1357 /* skip to the next line */
1358 while (cp && *cp) {
1359 if (*cp == '\n') {
1360 cp++;
1361 goto nextline;
1362 }
1363 cp++;
1364 }
1365 goto done;
1366 }
1367 naddrs++;
1368
1369 while (*cp == ' ' || *cp == '\t')
1370 cp++;
1371 p = cp;
1372 cp = strpbrk(p, " \t\n");
1373 if (cp != NULL) {
1374 if (*cp == '\n')
1375 more = 1;
1376 *cp++ = '\0';
1377 }
1378 if (!hp->h_name)
1379 hp->h_name = p;
1380 else if (strcmp(hp->h_name, p) == 0)
1381 ;
1382 else
1383 addalias(q, p, aliases, maxaliases);
1384 p = cp;
1385 if (more)
1386 goto nextline;
1387
1388 while (cp && *cp) {
1389 if (*cp == ' ' || *cp == '\t') {
1390 cp++;
1391 continue;
1392 }
1393 if (*cp == '\n') {
1394 cp++;
1395 goto nextline;
1396 }
1397 addalias(q, cp, aliases, maxaliases);
1398 cp = strpbrk(cp, " \t");
1399 if (cp != NULL)
1400 *cp++ = '\0';
1401 }
1402
1403done:
1404 if (hp->h_name == NULL) {
1405 free(aliases);
1406 return NULL;
1407 }
1408
1409 ptr = info->buf;
1410 len = info->buflen;
1411
1412 anum = (size_t)(q - aliases);
1413 HENT_ARRAY(hp->h_addr_list, naddrs, ptr, len);
1414 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
1415
1416 for (i = 0; i < naddrs; i++)
1417 HENT_COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length,
1418 ptr, len);
1419 hp->h_addr_list[naddrs] = NULL;
1420
1421 HENT_SCOPY(hp->h_name, hp->h_name, ptr, len);
1422
1423 for (i = 0; i < anum; i++)
1424 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
1425 hp->h_aliases[anum] = NULL;
1426 free(aliases);
1427
1428 return hp;
1429nospc:
1430 free(aliases);
1431 *info->he = NETDB_INTERNAL;
1432 errno = ENOSPC;
1433 return NULL;
1434}
1435
1436/*ARGSUSED*/
1437int
1438_yp_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1439{
1440 struct hostent *hp = NULL;
1441 char *ypcurrent;
1442 int ypcurrentlen, r;
1443 char name[INET6_ADDRSTRLEN]; /* XXX enough? */
1444 const unsigned char *uaddr;
1445 int af;
1446 const char *map;
1447 struct getnamaddr *info = rv;
1448
1449 _DIAGASSERT(rv != NULL);
1450
1451 uaddr = va_arg(ap, unsigned char *);
1452 /* NOSTRICT skip len */(void)va_arg(ap, int);
1453 af = va_arg(ap, int);
1454
1455 if (!__ypdomain) {
1456 if (_yp_check(&__ypdomain) == 0)
1457 return NS_UNAVAIL;
1458 }
1459 /*
1460 * XXX unfortunately, we cannot support IPv6 extended scoped address
1461 * notation here. gethostbyaddr() is not scope-aware. too bad.
1462 */
1463 if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL)
1464 return NS_UNAVAIL;
1465 switch (af) {
1466 case AF_INET:
1467 map = "hosts.byaddr";
1468 break;
1469 default:
1470 map = "ipnodes.byaddr";
1471 break;
1472 }
1473 ypcurrent = NULL;
1474 r = yp_match(__ypdomain, map, name,
1475 (int)strlen(name), &ypcurrent, &ypcurrentlen);
1476 if (r == 0)
1477 hp = _yp_hostent(ypcurrent, af, info);
1478 else
1479 hp = NULL;
1480 free(ypcurrent);
1481 if (hp == NULL) {
1482 *info->he = HOST_NOT_FOUND;
1483 return NS_NOTFOUND;
1484 }
1485 return NS_SUCCESS;
1486}
1487
1488/*ARGSUSED*/
1489int
1490_yp_gethtbyname(void *rv, void *cb_data, va_list ap)
1491{
1492 struct hostent *hp;
1493 char *ypcurrent;
1494 int ypcurrentlen, r;
1495 const char *name;
1496 int af;
1497 const char *map;
1498 struct getnamaddr *info = rv;
1499
1500 _DIAGASSERT(rv != NULL);
1501
1502 name = va_arg(ap, char *);
1503 /* NOSTRICT skip string len */(void)va_arg(ap, int);
1504 af = va_arg(ap, int);
1505
1506 if (!__ypdomain) {
1507 if (_yp_check(&__ypdomain) == 0)
1508 return NS_UNAVAIL;
1509 }
1510 switch (af) {
1511 case AF_INET:
1512 map = "hosts.byname";
1513 break;
1514 default:
1515 map = "ipnodes.byname";
1516 break;
1517 }
1518 ypcurrent = NULL;
1519 r = yp_match(__ypdomain, map, name,
1520 (int)strlen(name), &ypcurrent, &ypcurrentlen);
1521 if (r == 0)
1522 hp = _yp_hostent(ypcurrent, af, info);
1523 else
1524 hp = NULL;
1525 free(ypcurrent);
1526 if (hp == NULL) {
1527 *info->he = HOST_NOT_FOUND;
1528 return NS_NOTFOUND;
1529 }
1530 return NS_SUCCESS;
1531}
1532#endif
1533
1534/*
1535 * Non-reentrant versions.
1536 */
1537
Lev Rumyantsev814f38f2020-10-12 17:33:06 -07001538__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Yabin Cui58d33a52014-12-16 17:03:44 -08001539struct hostent *
1540gethostbyname(const char *name)
1541{
1542 struct hostent *result = NULL;
1543 res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
1544
1545 gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1546 return result;
1547}
1548
1549struct hostent *
1550gethostbyname2(const char *name, int af)
1551{
1552 struct hostent *result = NULL;
1553 res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
1554
1555 gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1556 return result;
1557}
1558
Ben Schwartz50178052017-04-24 17:57:11 -04001559// android_gethostby*fornet can be called in two different contexts.
1560// - In the proxy client context (proxy != NULL), |netid| is |app_netid|.
1561// - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|.
1562// The netcontext is constructed before checking which context we are in.
1563// Therefore, we have to populate both fields, and rely on the downstream code to check whether
1564// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
1565static struct android_net_context make_context(unsigned netid, unsigned mark) {
1566 struct android_net_context netcontext = NETCONTEXT_UNSET;
1567 netcontext.app_netid = netid;
1568 netcontext.app_mark = mark;
1569 netcontext.dns_netid = netid;
1570 netcontext.dns_mark = mark;
1571 return netcontext;
1572}
1573
Yabin Cui58d33a52014-12-16 17:03:44 -08001574struct hostent *
1575android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark)
1576{
Ben Schwartzdd878fe2017-05-22 10:19:25 -04001577 const struct android_net_context netcontext = make_context(netid, mark);
1578 return android_gethostbynamefornetcontext(name, af, &netcontext);
1579}
1580
1581struct hostent *
1582android_gethostbynamefornetcontext(const char *name, int af,
1583 const struct android_net_context *netcontext)
1584{
Yabin Cui58d33a52014-12-16 17:03:44 -08001585 struct hostent *hp;
1586 res_state res = __res_get_state();
1587 if (res == NULL)
1588 return NULL;
1589 res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
1590 hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
Ben Schwartzdd878fe2017-05-22 10:19:25 -04001591 &h_errno, netcontext);
Yabin Cui58d33a52014-12-16 17:03:44 -08001592 __res_put_state(res);
1593 return hp;
1594}
1595
Lev Rumyantsev814f38f2020-10-12 17:33:06 -07001596__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Yabin Cui58d33a52014-12-16 17:03:44 -08001597struct hostent *
1598gethostbyaddr(const void *addr, socklen_t len, int af)
1599{
Ben Schwartz50178052017-04-24 17:57:11 -04001600 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
Yabin Cui58d33a52014-12-16 17:03:44 -08001601}
1602
1603struct hostent *
1604android_gethostbyaddrfornet(const void *addr, socklen_t len, int af, unsigned netid, unsigned mark)
1605{
Ben Schwartz50178052017-04-24 17:57:11 -04001606 const struct android_net_context netcontext = make_context(netid, mark);
1607 return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
1608}
1609
1610struct hostent *
1611android_gethostbyaddrfornetcontext(const void *addr, socklen_t len, int af,
1612 const struct android_net_context *netcontext)
1613{
1614 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Yabin Cui58d33a52014-12-16 17:03:44 -08001615}
1616
1617__LIBC_HIDDEN__ struct hostent*
Ben Schwartz50178052017-04-24 17:57:11 -04001618android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
1619 const struct android_net_context *netcontext)
Yabin Cui58d33a52014-12-16 17:03:44 -08001620{
1621 res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
Ben Schwartz50178052017-04-24 17:57:11 -04001622 return android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, &rs->host, rs->hostbuf,
1623 sizeof(rs->hostbuf), &h_errno, netcontext);
Yabin Cui58d33a52014-12-16 17:03:44 -08001624}
1625
1626struct hostent *
1627gethostent(void)
1628{
1629 res_static rs = __res_get_static();
1630 if (!rs->hostf) {
1631 sethostent_r(&rs->hostf);
1632 if (!rs->hostf) {
1633 h_errno = NETDB_INTERNAL;
1634 return NULL;
1635 }
1636 }
1637 memset(&rs->host, 0, sizeof(rs->host));
1638 return netbsd_gethostent_r(rs->hostf, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001639}