blob: a40a7b78055b9d114021fa249b5648435b45ff8a [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 Innocenti9fa30802019-05-29 10:51:38 -070054#include "gethnamaddr.h"
55
chenbruce16adee42019-02-20 19:45:50 +080056#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090057#include <arpa/inet.h>
58#include <arpa/nameser.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090059#include <assert.h>
60#include <ctype.h>
61#include <errno.h>
62#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090063#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090064#include <stdarg.h>
65#include <stdbool.h>
66#include <stdio.h>
Bernie Innocentic165ce82018-10-16 23:35:28 +090067#include <stdlib.h>
68#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090069#include <sys/param.h>
70#include <sys/socket.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090071#include <sys/types.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090072#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090073#include <unistd.h>
nuccachen0a511732018-09-18 13:38:48 +080074#include <functional>
lifr1bebdb52019-01-09 14:41:15 +080075#include <vector>
Bernie Innocentif89b3512018-08-30 07:34:37 +090076
Bernie Innocentic165ce82018-10-16 23:35:28 +090077#include "hostent.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090078#include "netd_resolv/resolv.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090079#include "resolv_cache.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090080#include "resolv_private.h"
Frank Li08ce7b82019-06-27 10:11:52 -070081#include "stats.pb.h"
82
83using android::net::NetworkDnsEventReported;
Bernie Innocenti55864192018-08-30 04:05:20 +090084
Bernie Innocentif89b3512018-08-30 07:34:37 +090085// NetBSD uses _DIAGASSERT to null-check arguments and the like,
86// but it's clear from the number of mistakes in their assertions
87// that they don't actually test or ship with this.
88#define _DIAGASSERT(e) /* nothing */
89
nuccachen8161c992018-09-11 11:20:00 +080090// TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent
91// buffer.
Bernie Innocenti55864192018-08-30 04:05:20 +090092#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090093#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090094
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 Innocentif12d5bb2018-08-31 14:09:46 +090099#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900100
101typedef union {
102 HEADER hdr;
103 u_char buf[MAXPACKET];
104} querybuf;
105
106typedef union {
107 int32_t al;
108 char ac;
109} align;
110
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
112 char*, size_t, int*);
nuccachen0a511732018-09-18 13:38:48 +0800113static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
114 std::function<void(struct hostent* hp)> mapping_param,
115 std::function<void(char* src, char* dst)> mapping_addr);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900116static void map_v4v6_address(const char*, char*);
117static void map_v4v6_hostent(struct hostent*, char**, char*);
nuccachen0a511732018-09-18 13:38:48 +0800118static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900120
waynema4c6fd922019-03-08 19:13:41 +0800121static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
Frank Li08ce7b82019-06-27 10:11:52 -0700122 const android_net_context* netcontext, getnamaddr* info,
123 NetworkDnsEventReported* event);
waynema1a1d8b62019-03-08 16:58:06 +0800124static int dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900125
Mike Yucac05e42018-11-06 19:20:07 +0800126static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Frank Li08ce7b82019-06-27 10:11:52 -0700127 size_t hbuflen, const android_net_context* netcontext,
128 NetworkDnsEventReported* event);
Mike Yucac05e42018-11-06 19:20:07 +0800129static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800130 char* buf, size_t buflen);
Hungming Chen806d4462018-12-26 17:04:43 +0800131static int android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t, int,
Hungming Chen56273d72018-12-26 16:14:17 +0800132 struct hostent*, char*, size_t,
Frank Li08ce7b82019-06-27 10:11:52 -0700133 const struct android_net_context*,
134 NetworkDnsEventReported* event);
Hungming Chen806d4462018-12-26 17:04:43 +0800135static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
136 const struct android_net_context* netcontext,
Frank Li08ce7b82019-06-27 10:11:52 -0700137 hostent** hp, NetworkDnsEventReported* event);
Bernie Innocenti55864192018-08-30 04:05:20 +0900138
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139#define BOUNDED_INCR(x) \
140 do { \
141 BOUNDS_CHECK(cp, x); \
142 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900143 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900144
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145#define BOUNDS_CHECK(ptr, count) \
146 do { \
147 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900148 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900149
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
151 res_state res, struct hostent* hent, char* buf, size_t buflen,
152 int* he) {
153 const HEADER* hp;
154 const u_char* cp;
155 int n;
156 size_t qlen;
157 const u_char *eom, *erdata;
lifr1bebdb52019-01-09 14:41:15 +0800158 char *bp, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900159 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160 int haveanswer, had_error;
161 int toobig = 0;
162 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163 char* addr_ptrs[MAXADDRS];
164 const char* tname;
165 int (*name_ok)(const char*);
lifr1bebdb52019-01-09 14:41:15 +0800166 std::vector<char*> aliases;
Bernie Innocenti55864192018-08-30 04:05:20 +0900167
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900168 _DIAGASSERT(answer != NULL);
169 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900170
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900171 tname = qname;
172 hent->h_name = NULL;
173 eom = answer->buf + anslen;
174 switch (qtype) {
175 case T_A:
176 case T_AAAA:
177 name_ok = res_hnok;
178 break;
179 case T_PTR:
180 name_ok = res_dnok;
181 break;
182 default:
183 *he = NO_RECOVERY;
184 return NULL; /* XXX should be abort(); */
185 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900186
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900187 /*
188 * find first satisfactory answer
189 */
190 hp = &answer->hdr;
191 ancount = ntohs(hp->ancount);
192 qdcount = ntohs(hp->qdcount);
193 bp = buf;
194 ep = buf + buflen;
195 cp = answer->buf;
196 BOUNDED_INCR(HFIXEDSZ);
197 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900198
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900199 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
200 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900201
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900202 BOUNDED_INCR(n + QFIXEDSZ);
203 if (qtype == T_A || qtype == T_AAAA) {
204 /* res_send() has already verified that the query name is the
205 * same as the one we sent; this just gets the expanded name
206 * (i.e., with the succeeding search-domain tacked on).
207 */
208 n = (int) strlen(bp) + 1; /* for the \0 */
209 if (n >= MAXHOSTNAMELEN) goto no_recovery;
210 hent->h_name = bp;
211 bp += n;
212 /* The qname can be abbreviated, but h_name is now absolute. */
213 qname = hent->h_name;
214 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900215 hent->h_addr_list = hap = addr_ptrs;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900216 *hap = NULL;
217 haveanswer = 0;
218 had_error = 0;
219 while (ancount-- > 0 && cp < eom && !had_error) {
220 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
221 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
222 had_error++;
223 continue;
224 }
225 cp += n; /* name */
226 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
chenbruce86a50bb2019-03-28 18:44:37 +0800227 int type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228 cp += INT16SZ; /* type */
chenbruce86a50bb2019-03-28 18:44:37 +0800229 int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900230 cp += INT16SZ + INT32SZ; /* class, TTL */
chenbruce86a50bb2019-03-28 18:44:37 +0800231 n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232 cp += INT16SZ; /* len */
233 BOUNDS_CHECK(cp, n);
234 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900235 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900236 /* XXX - debug? syslog? */
237 cp += n;
238 continue; /* XXX - had_error++ ? */
239 }
240 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
241 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
242 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
243 had_error++;
244 continue;
245 }
246 cp += n;
247 if (cp != erdata) goto no_recovery;
248 /* Store alias. */
lifr1bebdb52019-01-09 14:41:15 +0800249 aliases.push_back(bp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900250 n = (int) strlen(bp) + 1; /* for the \0 */
251 if (n >= MAXHOSTNAMELEN) {
252 had_error++;
253 continue;
254 }
255 bp += n;
256 /* Get canonical name. */
257 n = (int) strlen(tbuf) + 1; /* for the \0 */
258 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
259 had_error++;
260 continue;
261 }
262 strlcpy(bp, tbuf, (size_t)(ep - bp));
263 hent->h_name = bp;
264 bp += n;
265 continue;
266 }
267 if (qtype == T_PTR && type == T_CNAME) {
268 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
269 if (n < 0 || !maybe_dnok(res, tbuf)) {
270 had_error++;
271 continue;
272 }
273 cp += n;
274 if (cp != erdata) goto no_recovery;
275 /* Get canonical name. */
276 n = (int) strlen(tbuf) + 1; /* for the \0 */
277 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
278 had_error++;
279 continue;
280 }
281 strlcpy(bp, tbuf, (size_t)(ep - bp));
282 tname = bp;
283 bp += n;
284 continue;
285 }
286 if (type != qtype) {
287 if (type != T_KEY && type != T_SIG)
Ken Chenbab50142019-03-19 17:41:28 +0800288 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
289 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900290 cp += n;
291 continue; /* XXX - had_error++ ? */
292 }
293 switch (type) {
294 case T_PTR:
295 if (strcasecmp(tname, bp) != 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800296 LOG(DEBUG) << __func__ << ": asked for \"" << qname << "\", got \"" << bp
297 << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900298 cp += n;
299 continue; /* XXX - had_error++ ? */
300 }
301 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
302 if ((n < 0) || !maybe_hnok(res, bp)) {
303 had_error++;
304 break;
305 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900306 cp += n;
307 if (cp != erdata) goto no_recovery;
308 if (!haveanswer)
309 hent->h_name = bp;
310 else
lifr1bebdb52019-01-09 14:41:15 +0800311 aliases.push_back(bp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900312 if (n != -1) {
313 n = (int) strlen(bp) + 1; /* for the \0 */
314 if (n >= MAXHOSTNAMELEN) {
315 had_error++;
316 break;
317 }
318 bp += n;
319 }
320 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900321 case T_A:
322 case T_AAAA:
323 if (strcasecmp(hent->h_name, bp) != 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800324 LOG(DEBUG) << __func__ << ": asked for \"" << hent->h_name << "\", got \"" << bp
325 << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900326 cp += n;
327 continue; /* XXX - had_error++ ? */
328 }
329 if (n != hent->h_length) {
330 cp += n;
331 continue;
332 }
333 if (type == T_AAAA) {
334 struct in6_addr in6;
335 memcpy(&in6, cp, NS_IN6ADDRSZ);
336 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
337 cp += n;
338 continue;
339 }
340 }
341 if (!haveanswer) {
342 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900343
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900344 hent->h_name = bp;
345 nn = (int) strlen(bp) + 1; /* for the \0 */
346 bp += nn;
347 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900348
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900349 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900350
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900351 if (bp + n >= ep) {
Ken Chenbab50142019-03-19 17:41:28 +0800352 LOG(DEBUG) << __func__ << ": size (" << n << ") too big";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900353 had_error++;
354 continue;
355 }
356 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
357 if (!toobig++) {
Ken Chenbab50142019-03-19 17:41:28 +0800358 LOG(DEBUG) << __func__ << ": Too many addresses (" << MAXADDRS << ")";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900359 }
360 cp += n;
361 continue;
362 }
363 (void) memcpy(*hap++ = bp, cp, (size_t) n);
364 bp += n;
365 cp += n;
366 if (cp != erdata) goto no_recovery;
367 break;
368 default:
369 abort();
370 }
371 if (!had_error) haveanswer++;
372 }
373 if (haveanswer) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900374 *hap = NULL;
375 /*
376 * Note: we sort even if host can take only one address
377 * in its return structures - should give it the "best"
378 * address in that case, not some random one
379 */
380 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
381 if (!hent->h_name) {
382 n = (int) strlen(qname) + 1; /* for the \0 */
383 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
384 strlcpy(bp, qname, (size_t)(ep - bp));
385 hent->h_name = bp;
386 bp += n;
387 }
388 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
nuccachen0a511732018-09-18 13:38:48 +0800389 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900390 goto success;
391 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900392no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900393 *he = NO_RECOVERY;
394 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900395success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900396 bp = (char*) ALIGN(bp);
lifr1bebdb52019-01-09 14:41:15 +0800397 aliases.push_back(nullptr);
398 qlen = aliases.size() * sizeof(*hent->h_aliases);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900399 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900400 hent->h_aliases = (char**) bp;
lifr1bebdb52019-01-09 14:41:15 +0800401 memcpy(bp, aliases.data(), qlen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900402
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900403 bp += qlen;
404 n = (int) (hap - addr_ptrs);
405 qlen = (n + 1) * sizeof(*hent->h_addr_list);
406 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900407 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900408 memcpy(bp, addr_ptrs, qlen);
409 *he = NETDB_SUCCESS;
410 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900411nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900412 errno = ENOSPC;
413 *he = NETDB_INTERNAL;
414 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900415}
416
Mike Yucac05e42018-11-06 19:20:07 +0800417static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800418 char* buf, size_t buflen) {
Mike Yucac05e42018-11-06 19:20:07 +0800419 getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900420 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900421
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900422 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900423
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900424 switch (af) {
425 case AF_INET:
426 size = NS_INADDRSZ;
427 break;
428 case AF_INET6:
429 size = NS_IN6ADDRSZ;
430 break;
431 default:
Mike Yucac05e42018-11-06 19:20:07 +0800432 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900433 }
434 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900435
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900436 hp->h_addrtype = af;
437 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900438
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900439 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 * disallow names consisting only of digits/dots, unless
441 * they end in a dot.
442 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900443 if (isdigit((u_char) name[0])) {
444 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900445 if (!*cp) {
446 if (*--cp == '.') break;
447 /*
448 * All-numeric, no dot at the end.
449 * Fake up a hostent as if we'd actually
450 * done a lookup.
451 */
452 goto fake;
453 }
454 if (!isdigit((u_char) *cp) && *cp != '.') break;
455 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900456 }
457 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
458 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 if (!*cp) {
460 if (*--cp == '.') break;
461 /*
462 * All-IPv6-legal, no dot at the end.
463 * Fake up a hostent as if we'd actually
464 * done a lookup.
465 */
466 goto fake;
467 }
468 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
469 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900470 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900471
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900472 info.hp = hp;
473 info.buf = buf;
474 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800475 if (_hf_gethtbyname2(name, af, &info)) {
waynema1a1d8b62019-03-08 16:58:06 +0800476 int error = dns_gethtbyname(name, af, &info);
Hungming Chen213ce272019-01-19 15:07:04 +0800477 if (error != 0) return error;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900478 }
Mike Yucac05e42018-11-06 19:20:07 +0800479 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900480nospc:
Hungming Chen56273d72018-12-26 16:14:17 +0800481 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900482fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
484 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900485
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900486 hp->h_aliases[0] = NULL;
487 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900488
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 if (inet_pton(af, name, buf) <= 0) {
Mike Yucac05e42018-11-06 19:20:07 +0800490 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900491 }
492 hp->h_addr_list[0] = buf;
493 hp->h_addr_list[1] = NULL;
494 buf += size;
495 buflen -= size;
496 HENT_SCOPY(hp->h_name, name, buf, buflen);
497 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
Mike Yucac05e42018-11-06 19:20:07 +0800498 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900499}
500
501// very similar in proxy-ness to android_getaddrinfo_proxy
Mike Yucac05e42018-11-06 19:20:07 +0800502static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Frank Li08ce7b82019-06-27 10:11:52 -0700503 size_t hbuflen, const android_net_context* netcontext,
504 NetworkDnsEventReported* event) {
505 res_setnetcontext(res, netcontext, event);
Hungming Chen56273d72018-12-26 16:14:17 +0800506 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900507}
508
Hungming Chen806d4462018-12-26 17:04:43 +0800509static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t len, int af,
510 struct hostent* hp, char* buf, size_t buflen,
Frank Li08ce7b82019-06-27 10:11:52 -0700511 const struct android_net_context* netcontext,
512 NetworkDnsEventReported* event) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 const u_char* uaddr = (const u_char*) addr;
514 socklen_t size;
515 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900516
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900517 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900518
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900519 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
520 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
521 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
Hungming Chen806d4462018-12-26 17:04:43 +0800522 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900523 }
524 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
525 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
526 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
527 /* Unmap. */
528 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
529 addr = uaddr;
530 af = AF_INET;
531 len = NS_INADDRSZ;
532 }
533 switch (af) {
534 case AF_INET:
535 size = NS_INADDRSZ;
536 break;
537 case AF_INET6:
538 size = NS_IN6ADDRSZ;
539 break;
540 default:
Hungming Chen806d4462018-12-26 17:04:43 +0800541 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900542 }
543 if (size != len) {
Hungming Chen213ce272019-01-19 15:07:04 +0800544 // TODO: Consider converting to a private extended EAI_* error code.
545 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
546 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
547 // and EINVAL, to EAI_FAIL.
548 return EAI_FAIL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900549 }
550 info.hp = hp;
551 info.buf = buf;
552 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800553 if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
Frank Li08ce7b82019-06-27 10:11:52 -0700554 int error = dns_gethtbyaddr(uaddr, len, af, netcontext, &info, event);
Hungming Chen213ce272019-01-19 15:07:04 +0800555 if (error != 0) return error;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900556 }
Hungming Chen806d4462018-12-26 17:04:43 +0800557 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900558}
559
Hungming Chen806d4462018-12-26 17:04:43 +0800560static int android_gethostbyaddrfornetcontext_proxy_internal(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900561 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
Frank Li08ce7b82019-06-27 10:11:52 -0700562 const struct android_net_context* netcontext, NetworkDnsEventReported* event) {
563 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, netcontext,
564 event);
Bernie Innocenti55864192018-08-30 04:05:20 +0900565}
566
Hungming Chen213ce272019-01-19 15:07:04 +0800567// TODO: Consider leaving function without returning error code as _gethtent() does because
568// the error code of the caller does not currently return to netd.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900569struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
570 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900571 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900572 char *name;
lifr1bebdb52019-01-09 14:41:15 +0800573 char* cp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900574 int af, len;
575 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900576 struct in6_addr host_addr;
lifr1bebdb52019-01-09 14:41:15 +0800577 std::vector<char*> aliases;
Bernie Innocenti55864192018-08-30 04:05:20 +0900578
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900579 if (hf == NULL) {
580 *he = NETDB_INTERNAL;
581 errno = EINVAL;
582 return NULL;
583 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900584 char* p = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900585
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900586 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900587 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900588 * as the buffer may be used to store content for a previous hostent
589 * returned by non-reentrant functions like gethostbyname().
590 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900591 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900592 goto nospc;
593 }
594 for (;;) {
595 if (!fgets(p, line_buf_size, hf)) {
596 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 *he = HOST_NOT_FOUND;
598 return NULL;
599 }
600 if (*p == '#') {
601 continue;
602 }
603 if (!(cp = strpbrk(p, "#\n"))) {
604 continue;
605 }
606 *cp = '\0';
607 if (!(cp = strpbrk(p, " \t"))) continue;
608 *cp++ = '\0';
609 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
610 af = AF_INET6;
611 len = NS_IN6ADDRSZ;
612 } else {
613 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900614
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900615 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900616 if (res == NULL) goto nospc;
617 if (res->options & RES_USE_INET6) {
618 map_v4v6_address(buf, buf);
619 af = AF_INET6;
620 len = NS_IN6ADDRSZ;
621 } else {
622 af = AF_INET;
623 len = NS_INADDRSZ;
624 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900626
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900627 /* if this is not something we're looking for, skip it. */
628 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
629 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900630
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900631 while (*cp == ' ' || *cp == '\t') cp++;
632 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900633 while (cp && *cp) {
634 if (*cp == ' ' || *cp == '\t') {
635 cp++;
636 continue;
637 }
lifr1bebdb52019-01-09 14:41:15 +0800638 aliases.push_back(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
640 }
641 break;
642 }
643 hent->h_length = len;
644 hent->h_addrtype = af;
645 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
lifr1bebdb52019-01-09 14:41:15 +0800646 anum = aliases.size();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900647 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
648 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
649 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900650
nuccachen8161c992018-09-11 11:20:00 +0800651 /* Reserve space for mapping IPv4 address to IPv6 address in place */
652 if (hent->h_addrtype == AF_INET) {
653 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
654 }
655
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656 HENT_SCOPY(hent->h_name, name, buf, buflen);
657 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
658 hent->h_aliases[anum] = NULL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900659 *he = NETDB_SUCCESS;
660 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900661 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900662nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900664 errno = ENOSPC;
665 *he = NETDB_INTERNAL;
666 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900667}
668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900669static void map_v4v6_address(const char* src, char* dst) {
670 u_char* p = (u_char*) dst;
671 char tmp[NS_INADDRSZ];
672 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900673
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900674 _DIAGASSERT(src != NULL);
675 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900677 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900678 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900679 /* Mark this ipv6 addr as a mapped ipv4. */
680 for (i = 0; i < 10; i++) *p++ = 0x00;
681 *p++ = 0xff;
682 *p++ = 0xff;
683 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900684 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900685}
686
nuccachen0a511732018-09-18 13:38:48 +0800687static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
688 std::function<void(struct hostent* hp)> map_param,
689 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900690 _DIAGASSERT(hp != NULL);
691 _DIAGASSERT(bpp != NULL);
692 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800695 map_param(hp);
696 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900697 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900698
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900699 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
700 /* Out of memory. Truncate address list here. XXX */
701 *ap = NULL;
702 return;
703 }
704 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800705 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900706 *ap = *bpp;
707 *bpp += NS_IN6ADDRSZ;
708 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900709}
710
nuccachen0a511732018-09-18 13:38:48 +0800711static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
712 convert_v4v6_hostent(hp, bpp, ep,
713 [](struct hostent* hp) {
714 hp->h_addrtype = AF_INET6;
715 hp->h_length = NS_IN6ADDRSZ;
716 },
717 [](char* src, char* dst) { map_v4v6_address(src, dst); });
718}
719
720/* Reserve space for mapping IPv4 address to IPv6 address in place */
721static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
722 convert_v4v6_hostent(hp, bpp, ep,
723 [](struct hostent* hp) {
724 (void) hp; /* unused */
725 },
726 [](char* src, char* dst) {
727 memcpy(dst, src, NS_INADDRSZ);
728 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
729 });
730}
731
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900732static void addrsort(char** ap, int num, res_state res) {
733 int i, j;
734 char** p;
735 short aval[MAXADDRS];
736 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900739
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900740 p = ap;
741 for (i = 0; i < num; i++, p++) {
742 for (j = 0; (unsigned) j < res->nsort; j++)
743 if (res->sort_list[j].addr.s_addr ==
744 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
745 break;
746 aval[i] = j;
747 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
748 }
749 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900750
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900751 while (needsort < num) {
752 for (j = needsort - 1; j >= 0; j--) {
753 if (aval[j] > aval[j + 1]) {
754 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 i = aval[j];
757 aval[j] = aval[j + 1];
758 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760 hp = ap[j];
761 ap[j] = ap[j + 1];
762 ap[j + 1] = hp;
763 } else
764 break;
765 }
766 needsort++;
767 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900768}
769
waynema1a1d8b62019-03-08 16:58:06 +0800770static int dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 int n, type;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900772 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900773
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900774 switch (info->hp->h_addrtype) {
775 case AF_INET:
776 info->hp->h_length = NS_INADDRSZ;
777 type = T_A;
778 break;
779 case AF_INET6:
780 info->hp->h_length = NS_IN6ADDRSZ;
781 type = T_AAAA;
782 break;
783 default:
Mike Yucac05e42018-11-06 19:20:07 +0800784 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900785 }
waynema1a1d8b62019-03-08 16:58:06 +0800786 auto buf = std::make_unique<querybuf>();
787
788 res_state res = res_get_state();
789 if (!res) return EAI_MEMORY;
Mike Yu69615f62018-11-06 15:42:36 +0800790
Hungming Chen213ce272019-01-19 15:07:04 +0800791 int he;
792 n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf), &he);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900793 if (n < 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800794 LOG(DEBUG) << __func__ << ": res_nsearch failed (" << n << ")";
Hungming Chen213ce272019-01-19 15:07:04 +0800795 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
796 // Note that res_nsearch() doesn't set the pair NETDB_INTERNAL and errno.
797 // See also herrnoToAiErrno().
798 return herrnoToAiErrno(he);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 }
Hungming Chen213ce272019-01-19 15:07:04 +0800800 hostent* hp = getanswer(buf.get(), n, name, type, res, info->hp, info->buf, info->buflen, &he);
801 if (hp == NULL) return herrnoToAiErrno(he);
802
Mike Yucac05e42018-11-06 19:20:07 +0800803 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900804}
805
waynema4c6fd922019-03-08 19:13:41 +0800806static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
Frank Li08ce7b82019-06-27 10:11:52 -0700807 const android_net_context* netcontext, getnamaddr* info,
808 NetworkDnsEventReported* event) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900809 char qbuf[MAXDNAME + 1], *qp, *ep;
810 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811 int advance;
Bernie Innocenti55864192018-08-30 04:05:20 +0900812
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900813 info->hp->h_length = len;
814 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900815
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900816 switch (info->hp->h_addrtype) {
817 case AF_INET:
818 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
819 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
820 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822 case AF_INET6:
823 qp = qbuf;
824 ep = qbuf + sizeof(qbuf) - 1;
825 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
826 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
827 ((unsigned int) uaddr[n] >> 4) & 0xf);
828 if (advance > 0 && qp + advance < ep)
829 qp += advance;
830 else {
Hungming Chen213ce272019-01-19 15:07:04 +0800831 // TODO: Consider converting to a private extended EAI_* error code.
832 // Currently, the EAI_* value has no corresponding error code for an internal
833 // out of buffer space. In order to not rely on errno, convert the original
834 // error code EAI_SYSTEM to EAI_MEMORY.
835 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 }
837 }
838 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
Hungming Chen213ce272019-01-19 15:07:04 +0800839 // TODO: Consider converting to a private extended EAI_* error code.
840 // Currently, the EAI_* value has no corresponding error code for an internal
841 // out of buffer space. In order to not rely on errno, convert the original
842 // error code EAI_SYSTEM to EAI_MEMORY.
843 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900844 }
845 break;
846 default:
Hungming Chen806d4462018-12-26 17:04:43 +0800847 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900848 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900849
waynema4c6fd922019-03-08 19:13:41 +0800850 auto buf = std::make_unique<querybuf>();
851
852 res_state res = res_get_state();
853 if (!res) return EAI_MEMORY;
854
Frank Li08ce7b82019-06-27 10:11:52 -0700855 res_setnetcontext(res, netcontext, event);
Hungming Chen213ce272019-01-19 15:07:04 +0800856 int he;
857 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf), &he);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900858 if (n < 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800859 LOG(DEBUG) << __func__ << ": res_nquery failed (" << n << ")";
Hungming Chen213ce272019-01-19 15:07:04 +0800860 // Note that res_nquery() doesn't set the pair NETDB_INTERNAL and errno.
861 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
862 // See also herrnoToAiErrno().
863 return herrnoToAiErrno(he);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900864 }
Hungming Chen213ce272019-01-19 15:07:04 +0800865 hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &he);
866 if (hp == NULL) return herrnoToAiErrno(he);
Bernie Innocenti55864192018-08-30 04:05:20 +0900867
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900868 char* bf = (char*) (hp->h_addr_list + 2);
869 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900870 if (blen + info->hp->h_length > info->buflen) goto nospc;
871 hp->h_addr_list[0] = bf;
872 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900873 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900874 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
875 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
876 map_v4v6_address(bf, bf);
877 hp->h_addrtype = AF_INET6;
878 hp->h_length = NS_IN6ADDRSZ;
879 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900880
nuccachen8161c992018-09-11 11:20:00 +0800881 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
882 if (info->hp->h_addrtype == AF_INET) {
883 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
884 // Pad zero to the unused address space
885 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
886 }
887
Hungming Chen806d4462018-12-26 17:04:43 +0800888 return 0;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900889
Bernie Innocenti55864192018-08-30 04:05:20 +0900890nospc:
Hungming Chen806d4462018-12-26 17:04:43 +0800891 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900892}
893
Bernie Innocenti55864192018-08-30 04:05:20 +0900894/*
895 * Non-reentrant versions.
896 */
897
Mike Yucac05e42018-11-06 19:20:07 +0800898int android_gethostbynamefornetcontext(const char* name, int af,
Frank Li08ce7b82019-06-27 10:11:52 -0700899 const struct android_net_context* netcontext, hostent** hp,
900 NetworkDnsEventReported* event) {
901 assert(event != nullptr);
902
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900903 res_state res = res_get_state();
Mike Yucac05e42018-11-06 19:20:07 +0800904 if (res == NULL) return EAI_MEMORY;
905 res_static* rs = res_get_static(); // For thread-safety.
Frank Li08ce7b82019-06-27 10:11:52 -0700906 int error;
Mike Yucac05e42018-11-06 19:20:07 +0800907 error = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
Frank Li08ce7b82019-06-27 10:11:52 -0700908 netcontext, event);
Mike Yucac05e42018-11-06 19:20:07 +0800909 if (error == 0) {
910 *hp = &rs->host;
911 }
912 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900913}
914
Hungming Chen806d4462018-12-26 17:04:43 +0800915int android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
Frank Li08ce7b82019-06-27 10:11:52 -0700916 const struct android_net_context* netcontext, hostent** hp,
917 NetworkDnsEventReported* event) {
918 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext, hp, event);
Bernie Innocenti55864192018-08-30 04:05:20 +0900919}
920
Hungming Chen806d4462018-12-26 17:04:43 +0800921static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
922 const struct android_net_context* netcontext,
Frank Li08ce7b82019-06-27 10:11:52 -0700923 hostent** hp, NetworkDnsEventReported* event) {
924 assert(event != nullptr);
925
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900926 struct res_static* rs = res_get_static(); // For thread-safety.
Hungming Chen806d4462018-12-26 17:04:43 +0800927 int error = android_gethostbyaddrfornetcontext_proxy_internal(
Frank Li08ce7b82019-06-27 10:11:52 -0700928 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), netcontext, event);
Hungming Chen806d4462018-12-26 17:04:43 +0800929 if (error == 0) *hp = &rs->host;
930 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900931}
Mike Yu69615f62018-11-06 15:42:36 +0800932
Hungming Chen213ce272019-01-19 15:07:04 +0800933int herrnoToAiErrno(int he) {
934 switch (he) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800935 // extended h_errno
936 case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT:
937 return NETD_RESOLV_TIMEOUT;
938 // legacy h_errno
939 case NETDB_SUCCESS:
940 return 0;
941 case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead
942 case NO_DATA: // NO_ADDRESS
Mike Yu69615f62018-11-06 15:42:36 +0800943 return EAI_NODATA;
944 case TRY_AGAIN:
945 return EAI_AGAIN;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800946 case NETDB_INTERNAL:
Hungming Chen213ce272019-01-19 15:07:04 +0800947 // TODO: Remove ENOSPC and call abort() immediately whenever any allocation fails.
948 if (errno == ENOSPC) return EAI_MEMORY;
949 // Theoretically, this should not happen. Leave this here just in case.
950 // Currently, getanswer() of {gethnamaddr, getaddrinfo}.cpp, res_nsearch() and
951 // res_searchN() use this function to convert error code. Only getanswer()
952 // of gethnamaddr.cpp may return the error code pair, herrno NETDB_INTERNAL and
953 // errno ENOSPC, which has already converted to EAI_MEMORY. The remaining functions
954 // don't set the pair herrno and errno.
Hungming Chen7f0d3292018-12-27 18:33:19 +0800955 return EAI_SYSTEM; // see errno for detail
956 case NO_RECOVERY:
Mike Yu69615f62018-11-06 15:42:36 +0800957 default:
Hungming Chen213ce272019-01-19 15:07:04 +0800958 return EAI_FAIL; // TODO: Perhaps convert default to EAI_MAX (unknown error) instead
Mike Yu69615f62018-11-06 15:42:36 +0800959 }
Bernie Innocenti9fa30802019-05-29 10:51:38 -0700960}