blob: 5a35dbb1de89050b97ae9ba599778b95315dc6be [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
chenbruce16adee42019-02-20 19:45:50 +080054#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090055#include <arpa/inet.h>
56#include <arpa/nameser.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090057#include <assert.h>
58#include <ctype.h>
59#include <errno.h>
60#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090061#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090062#include <stdarg.h>
63#include <stdbool.h>
64#include <stdio.h>
Bernie Innocentic165ce82018-10-16 23:35:28 +090065#include <stdlib.h>
66#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090067#include <sys/param.h>
68#include <sys/socket.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090069#include <sys/types.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090070#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090071#include <unistd.h>
nuccachen0a511732018-09-18 13:38:48 +080072#include <functional>
lifr1bebdb52019-01-09 14:41:15 +080073#include <vector>
Bernie Innocentif89b3512018-08-30 07:34:37 +090074
Bernie Innocentic165ce82018-10-16 23:35:28 +090075#include "hostent.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090076#include "netd_resolv/resolv.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090077#include "resolv_cache.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090078#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090079
Bernie Innocentif89b3512018-08-30 07:34:37 +090080// NetBSD uses _DIAGASSERT to null-check arguments and the like,
81// but it's clear from the number of mistakes in their assertions
82// that they don't actually test or ship with this.
83#define _DIAGASSERT(e) /* nothing */
84
nuccachen8161c992018-09-11 11:20:00 +080085// TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent
86// buffer.
Bernie Innocenti55864192018-08-30 04:05:20 +090087#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090089
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090090#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0)
Bernie Innocenti55864192018-08-30 04:05:20 +090091#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
92#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
93
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090094#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +090095
96typedef union {
97 HEADER hdr;
98 u_char buf[MAXPACKET];
99} querybuf;
100
101typedef union {
102 int32_t al;
103 char ac;
104} align;
105
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
107 char*, size_t, int*);
nuccachen0a511732018-09-18 13:38:48 +0800108static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
109 std::function<void(struct hostent* hp)> mapping_param,
110 std::function<void(char* src, char* dst)> mapping_addr);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111static void map_v4v6_address(const char*, char*);
112static void map_v4v6_hostent(struct hostent*, char**, char*);
nuccachen0a511732018-09-18 13:38:48 +0800113static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900115
waynema4c6fd922019-03-08 19:13:41 +0800116static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
117 const android_net_context* netcontext, getnamaddr* info);
waynema1a1d8b62019-03-08 16:58:06 +0800118static int dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900119
Mike Yucac05e42018-11-06 19:20:07 +0800120static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Hungming Chen56273d72018-12-26 16:14:17 +0800121 size_t hbuflen, const android_net_context* netcontext);
Mike Yucac05e42018-11-06 19:20:07 +0800122static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800123 char* buf, size_t buflen);
Hungming Chen806d4462018-12-26 17:04:43 +0800124static int android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t, int,
Hungming Chen56273d72018-12-26 16:14:17 +0800125 struct hostent*, char*, size_t,
Hungming Chen806d4462018-12-26 17:04:43 +0800126 const struct android_net_context*);
127static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
128 const struct android_net_context* netcontext,
129 hostent** hp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900130
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900131#define BOUNDED_INCR(x) \
132 do { \
133 BOUNDS_CHECK(cp, x); \
134 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900135 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900136
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900137#define BOUNDS_CHECK(ptr, count) \
138 do { \
139 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900140 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900141
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
143 res_state res, struct hostent* hent, char* buf, size_t buflen,
144 int* he) {
145 const HEADER* hp;
146 const u_char* cp;
147 int n;
148 size_t qlen;
149 const u_char *eom, *erdata;
lifr1bebdb52019-01-09 14:41:15 +0800150 char *bp, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900151 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 int haveanswer, had_error;
153 int toobig = 0;
154 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 char* addr_ptrs[MAXADDRS];
156 const char* tname;
157 int (*name_ok)(const char*);
lifr1bebdb52019-01-09 14:41:15 +0800158 std::vector<char*> aliases;
Bernie Innocenti55864192018-08-30 04:05:20 +0900159
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160 _DIAGASSERT(answer != NULL);
161 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900162
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163 tname = qname;
164 hent->h_name = NULL;
165 eom = answer->buf + anslen;
166 switch (qtype) {
167 case T_A:
168 case T_AAAA:
169 name_ok = res_hnok;
170 break;
171 case T_PTR:
172 name_ok = res_dnok;
173 break;
174 default:
175 *he = NO_RECOVERY;
176 return NULL; /* XXX should be abort(); */
177 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900178
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900179 /*
180 * find first satisfactory answer
181 */
182 hp = &answer->hdr;
183 ancount = ntohs(hp->ancount);
184 qdcount = ntohs(hp->qdcount);
185 bp = buf;
186 ep = buf + buflen;
187 cp = answer->buf;
188 BOUNDED_INCR(HFIXEDSZ);
189 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900191 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
192 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194 BOUNDED_INCR(n + QFIXEDSZ);
195 if (qtype == T_A || qtype == T_AAAA) {
196 /* res_send() has already verified that the query name is the
197 * same as the one we sent; this just gets the expanded name
198 * (i.e., with the succeeding search-domain tacked on).
199 */
200 n = (int) strlen(bp) + 1; /* for the \0 */
201 if (n >= MAXHOSTNAMELEN) goto no_recovery;
202 hent->h_name = bp;
203 bp += n;
204 /* The qname can be abbreviated, but h_name is now absolute. */
205 qname = hent->h_name;
206 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900207 hent->h_addr_list = hap = addr_ptrs;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208 *hap = NULL;
209 haveanswer = 0;
210 had_error = 0;
211 while (ancount-- > 0 && cp < eom && !had_error) {
212 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
213 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
214 had_error++;
215 continue;
216 }
217 cp += n; /* name */
218 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900219 int type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900221 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900222 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900223 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900224 cp += INT16SZ; /* len */
225 BOUNDS_CHECK(cp, n);
226 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900227 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228 /* XXX - debug? syslog? */
229 cp += n;
230 continue; /* XXX - had_error++ ? */
231 }
232 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
233 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
234 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
235 had_error++;
236 continue;
237 }
238 cp += n;
239 if (cp != erdata) goto no_recovery;
240 /* Store alias. */
lifr1bebdb52019-01-09 14:41:15 +0800241 aliases.push_back(bp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900242 n = (int) strlen(bp) + 1; /* for the \0 */
243 if (n >= MAXHOSTNAMELEN) {
244 had_error++;
245 continue;
246 }
247 bp += n;
248 /* Get canonical name. */
249 n = (int) strlen(tbuf) + 1; /* for the \0 */
250 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
251 had_error++;
252 continue;
253 }
254 strlcpy(bp, tbuf, (size_t)(ep - bp));
255 hent->h_name = bp;
256 bp += n;
257 continue;
258 }
259 if (qtype == T_PTR && type == T_CNAME) {
260 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
261 if (n < 0 || !maybe_dnok(res, tbuf)) {
262 had_error++;
263 continue;
264 }
265 cp += n;
266 if (cp != erdata) goto no_recovery;
267 /* Get canonical name. */
268 n = (int) strlen(tbuf) + 1; /* for the \0 */
269 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
270 had_error++;
271 continue;
272 }
273 strlcpy(bp, tbuf, (size_t)(ep - bp));
274 tname = bp;
275 bp += n;
276 continue;
277 }
278 if (type != qtype) {
279 if (type != T_KEY && type != T_SIG)
Ken Chenbab50142019-03-19 17:41:28 +0800280 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
281 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900282 cp += n;
283 continue; /* XXX - had_error++ ? */
284 }
285 switch (type) {
286 case T_PTR:
287 if (strcasecmp(tname, bp) != 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800288 LOG(DEBUG) << __func__ << ": asked for \"" << qname << "\", got \"" << bp
289 << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900290 cp += n;
291 continue; /* XXX - had_error++ ? */
292 }
293 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
294 if ((n < 0) || !maybe_hnok(res, bp)) {
295 had_error++;
296 break;
297 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900298 cp += n;
299 if (cp != erdata) goto no_recovery;
300 if (!haveanswer)
301 hent->h_name = bp;
302 else
lifr1bebdb52019-01-09 14:41:15 +0800303 aliases.push_back(bp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900304 if (n != -1) {
305 n = (int) strlen(bp) + 1; /* for the \0 */
306 if (n >= MAXHOSTNAMELEN) {
307 had_error++;
308 break;
309 }
310 bp += n;
311 }
312 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900313 case T_A:
314 case T_AAAA:
315 if (strcasecmp(hent->h_name, bp) != 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800316 LOG(DEBUG) << __func__ << ": asked for \"" << hent->h_name << "\", got \"" << bp
317 << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900318 cp += n;
319 continue; /* XXX - had_error++ ? */
320 }
321 if (n != hent->h_length) {
322 cp += n;
323 continue;
324 }
325 if (type == T_AAAA) {
326 struct in6_addr in6;
327 memcpy(&in6, cp, NS_IN6ADDRSZ);
328 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
329 cp += n;
330 continue;
331 }
332 }
333 if (!haveanswer) {
334 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900335
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900336 hent->h_name = bp;
337 nn = (int) strlen(bp) + 1; /* for the \0 */
338 bp += nn;
339 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900340
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900341 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900342
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900343 if (bp + n >= ep) {
Ken Chenbab50142019-03-19 17:41:28 +0800344 LOG(DEBUG) << __func__ << ": size (" << n << ") too big";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900345 had_error++;
346 continue;
347 }
348 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
349 if (!toobig++) {
Ken Chenbab50142019-03-19 17:41:28 +0800350 LOG(DEBUG) << __func__ << ": Too many addresses (" << MAXADDRS << ")";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900351 }
352 cp += n;
353 continue;
354 }
355 (void) memcpy(*hap++ = bp, cp, (size_t) n);
356 bp += n;
357 cp += n;
358 if (cp != erdata) goto no_recovery;
359 break;
360 default:
361 abort();
362 }
363 if (!had_error) haveanswer++;
364 }
365 if (haveanswer) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900366 *hap = NULL;
367 /*
368 * Note: we sort even if host can take only one address
369 * in its return structures - should give it the "best"
370 * address in that case, not some random one
371 */
372 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
373 if (!hent->h_name) {
374 n = (int) strlen(qname) + 1; /* for the \0 */
375 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
376 strlcpy(bp, qname, (size_t)(ep - bp));
377 hent->h_name = bp;
378 bp += n;
379 }
380 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
nuccachen0a511732018-09-18 13:38:48 +0800381 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900382 goto success;
383 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900384no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900385 *he = NO_RECOVERY;
386 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900387success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900388 bp = (char*) ALIGN(bp);
lifr1bebdb52019-01-09 14:41:15 +0800389 aliases.push_back(nullptr);
390 qlen = aliases.size() * sizeof(*hent->h_aliases);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900391 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900392 hent->h_aliases = (char**) bp;
lifr1bebdb52019-01-09 14:41:15 +0800393 memcpy(bp, aliases.data(), qlen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900394
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900395 bp += qlen;
396 n = (int) (hap - addr_ptrs);
397 qlen = (n + 1) * sizeof(*hent->h_addr_list);
398 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900399 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900400 memcpy(bp, addr_ptrs, qlen);
401 *he = NETDB_SUCCESS;
402 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900403nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900404 errno = ENOSPC;
405 *he = NETDB_INTERNAL;
406 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900407}
408
Mike Yucac05e42018-11-06 19:20:07 +0800409static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800410 char* buf, size_t buflen) {
Mike Yucac05e42018-11-06 19:20:07 +0800411 getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900412 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900413
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900414 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900415
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900416 switch (af) {
417 case AF_INET:
418 size = NS_INADDRSZ;
419 break;
420 case AF_INET6:
421 size = NS_IN6ADDRSZ;
422 break;
423 default:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900424 errno = EAFNOSUPPORT;
Mike Yucac05e42018-11-06 19:20:07 +0800425 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900426 }
427 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900428
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900429 hp->h_addrtype = af;
430 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900431
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900433 * disallow names consisting only of digits/dots, unless
434 * they end in a dot.
435 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900436 if (isdigit((u_char) name[0])) {
437 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900438 if (!*cp) {
439 if (*--cp == '.') break;
440 /*
441 * All-numeric, no dot at the end.
442 * Fake up a hostent as if we'd actually
443 * done a lookup.
444 */
445 goto fake;
446 }
447 if (!isdigit((u_char) *cp) && *cp != '.') break;
448 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900449 }
450 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
451 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900452 if (!*cp) {
453 if (*--cp == '.') break;
454 /*
455 * All-IPv6-legal, no dot at the end.
456 * Fake up a hostent as if we'd actually
457 * done a lookup.
458 */
459 goto fake;
460 }
461 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
462 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900463 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900464
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900465 info.hp = hp;
466 info.buf = buf;
467 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800468 if (_hf_gethtbyname2(name, af, &info)) {
waynema1a1d8b62019-03-08 16:58:06 +0800469 int error = dns_gethtbyname(name, af, &info);
Mike Yucac05e42018-11-06 19:20:07 +0800470 if (error != 0) {
471 return error;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900472 }
473 }
Mike Yucac05e42018-11-06 19:20:07 +0800474 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900475nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900476 errno = ENOSPC;
Hungming Chen56273d72018-12-26 16:14:17 +0800477 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900478fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
480 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900481
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900482 hp->h_aliases[0] = NULL;
483 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485 if (inet_pton(af, name, buf) <= 0) {
Mike Yucac05e42018-11-06 19:20:07 +0800486 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900487 }
488 hp->h_addr_list[0] = buf;
489 hp->h_addr_list[1] = NULL;
490 buf += size;
491 buflen -= size;
492 HENT_SCOPY(hp->h_name, name, buf, buflen);
493 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
Mike Yucac05e42018-11-06 19:20:07 +0800494 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900495}
496
497// very similar in proxy-ness to android_getaddrinfo_proxy
Mike Yucac05e42018-11-06 19:20:07 +0800498static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Hungming Chen56273d72018-12-26 16:14:17 +0800499 size_t hbuflen, const android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900500 res_setnetcontext(res, netcontext);
Hungming Chen56273d72018-12-26 16:14:17 +0800501 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900502}
503
Hungming Chen806d4462018-12-26 17:04:43 +0800504static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t len, int af,
505 struct hostent* hp, char* buf, size_t buflen,
Hungming Chen806d4462018-12-26 17:04:43 +0800506 const struct android_net_context* netcontext) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900507 const u_char* uaddr = (const u_char*) addr;
508 socklen_t size;
509 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900510
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900512
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
514 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
515 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
Hungming Chen806d4462018-12-26 17:04:43 +0800516 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900517 }
518 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
519 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
520 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
521 /* Unmap. */
522 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
523 addr = uaddr;
524 af = AF_INET;
525 len = NS_INADDRSZ;
526 }
527 switch (af) {
528 case AF_INET:
529 size = NS_INADDRSZ;
530 break;
531 case AF_INET6:
532 size = NS_IN6ADDRSZ;
533 break;
534 default:
535 errno = EAFNOSUPPORT;
Hungming Chen806d4462018-12-26 17:04:43 +0800536 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900537 }
538 if (size != len) {
539 errno = EINVAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800540 // TODO: Consider to remap error code without relying on errno.
541 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900542 }
543 info.hp = hp;
544 info.buf = buf;
545 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800546 if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
waynema4c6fd922019-03-08 19:13:41 +0800547 int error = dns_gethtbyaddr(uaddr, len, af, netcontext, &info);
Hungming Chen806d4462018-12-26 17:04:43 +0800548 if (error != 0) {
549 return error;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900550 }
551 }
Hungming Chen806d4462018-12-26 17:04:43 +0800552 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900553}
554
Hungming Chen806d4462018-12-26 17:04:43 +0800555static int android_gethostbyaddrfornetcontext_proxy_internal(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900556 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
Hungming Chen56273d72018-12-26 16:14:17 +0800557 const struct android_net_context* netcontext) {
558 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900559}
560
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900561struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
562 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900563 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900564 char *name;
lifr1bebdb52019-01-09 14:41:15 +0800565 char* cp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900566 int af, len;
567 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 struct in6_addr host_addr;
lifr1bebdb52019-01-09 14:41:15 +0800569 std::vector<char*> aliases;
Bernie Innocenti55864192018-08-30 04:05:20 +0900570
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900571 if (hf == NULL) {
572 *he = NETDB_INTERNAL;
573 errno = EINVAL;
574 return NULL;
575 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900576 char* p = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900577
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900579 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900580 * as the buffer may be used to store content for a previous hostent
581 * returned by non-reentrant functions like gethostbyname().
582 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900583 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900584 goto nospc;
585 }
586 for (;;) {
587 if (!fgets(p, line_buf_size, hf)) {
588 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900589 *he = HOST_NOT_FOUND;
590 return NULL;
591 }
592 if (*p == '#') {
593 continue;
594 }
595 if (!(cp = strpbrk(p, "#\n"))) {
596 continue;
597 }
598 *cp = '\0';
599 if (!(cp = strpbrk(p, " \t"))) continue;
600 *cp++ = '\0';
601 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
602 af = AF_INET6;
603 len = NS_IN6ADDRSZ;
604 } else {
605 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900606
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900607 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608 if (res == NULL) goto nospc;
609 if (res->options & RES_USE_INET6) {
610 map_v4v6_address(buf, buf);
611 af = AF_INET6;
612 len = NS_IN6ADDRSZ;
613 } else {
614 af = AF_INET;
615 len = NS_INADDRSZ;
616 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900617 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900618
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900619 /* if this is not something we're looking for, skip it. */
620 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
621 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900622
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900623 while (*cp == ' ' || *cp == '\t') cp++;
624 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 while (cp && *cp) {
626 if (*cp == ' ' || *cp == '\t') {
627 cp++;
628 continue;
629 }
lifr1bebdb52019-01-09 14:41:15 +0800630 aliases.push_back(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900631 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
632 }
633 break;
634 }
635 hent->h_length = len;
636 hent->h_addrtype = af;
637 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
lifr1bebdb52019-01-09 14:41:15 +0800638 anum = aliases.size();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
640 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
641 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900642
nuccachen8161c992018-09-11 11:20:00 +0800643 /* Reserve space for mapping IPv4 address to IPv6 address in place */
644 if (hent->h_addrtype == AF_INET) {
645 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
646 }
647
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648 HENT_SCOPY(hent->h_name, name, buf, buflen);
649 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
650 hent->h_aliases[anum] = NULL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900651 *he = NETDB_SUCCESS;
652 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900654nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900655 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656 errno = ENOSPC;
657 *he = NETDB_INTERNAL;
658 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900659}
660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900661static void map_v4v6_address(const char* src, char* dst) {
662 u_char* p = (u_char*) dst;
663 char tmp[NS_INADDRSZ];
664 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900665
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900666 _DIAGASSERT(src != NULL);
667 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900668
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900669 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900670 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900671 /* Mark this ipv6 addr as a mapped ipv4. */
672 for (i = 0; i < 10; i++) *p++ = 0x00;
673 *p++ = 0xff;
674 *p++ = 0xff;
675 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900676 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900677}
678
nuccachen0a511732018-09-18 13:38:48 +0800679static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
680 std::function<void(struct hostent* hp)> map_param,
681 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900682 _DIAGASSERT(hp != NULL);
683 _DIAGASSERT(bpp != NULL);
684 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900685
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900686 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800687 map_param(hp);
688 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
692 /* Out of memory. Truncate address list here. XXX */
693 *ap = NULL;
694 return;
695 }
696 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800697 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900698 *ap = *bpp;
699 *bpp += NS_IN6ADDRSZ;
700 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900701}
702
nuccachen0a511732018-09-18 13:38:48 +0800703static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
704 convert_v4v6_hostent(hp, bpp, ep,
705 [](struct hostent* hp) {
706 hp->h_addrtype = AF_INET6;
707 hp->h_length = NS_IN6ADDRSZ;
708 },
709 [](char* src, char* dst) { map_v4v6_address(src, dst); });
710}
711
712/* Reserve space for mapping IPv4 address to IPv6 address in place */
713static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
714 convert_v4v6_hostent(hp, bpp, ep,
715 [](struct hostent* hp) {
716 (void) hp; /* unused */
717 },
718 [](char* src, char* dst) {
719 memcpy(dst, src, NS_INADDRSZ);
720 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
721 });
722}
723
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900724static void addrsort(char** ap, int num, res_state res) {
725 int i, j;
726 char** p;
727 short aval[MAXADDRS];
728 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900729
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900730 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900731
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900732 p = ap;
733 for (i = 0; i < num; i++, p++) {
734 for (j = 0; (unsigned) j < res->nsort; j++)
735 if (res->sort_list[j].addr.s_addr ==
736 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
737 break;
738 aval[i] = j;
739 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
740 }
741 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900742
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900743 while (needsort < num) {
744 for (j = needsort - 1; j >= 0; j--) {
745 if (aval[j] > aval[j + 1]) {
746 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900747
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900748 i = aval[j];
749 aval[j] = aval[j + 1];
750 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900751
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 hp = ap[j];
753 ap[j] = ap[j + 1];
754 ap[j + 1] = hp;
755 } else
756 break;
757 }
758 needsort++;
759 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900760}
761
waynema1a1d8b62019-03-08 16:58:06 +0800762static int dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900763 int n, type;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900764 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900765
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900766 switch (info->hp->h_addrtype) {
767 case AF_INET:
768 info->hp->h_length = NS_INADDRSZ;
769 type = T_A;
770 break;
771 case AF_INET6:
772 info->hp->h_length = NS_IN6ADDRSZ;
773 type = T_AAAA;
774 break;
775 default:
Mike Yucac05e42018-11-06 19:20:07 +0800776 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 }
waynema1a1d8b62019-03-08 16:58:06 +0800778 auto buf = std::make_unique<querybuf>();
779
780 res_state res = res_get_state();
781 if (!res) return EAI_MEMORY;
Mike Yu69615f62018-11-06 15:42:36 +0800782
Hungming Chen56273d72018-12-26 16:14:17 +0800783 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800784 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf), &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900785 if (n < 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800786 LOG(DEBUG) << __func__ << ": res_nsearch failed (" << n << ")";
Hungming Chen7f0d3292018-12-27 18:33:19 +0800787 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
788 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900789 }
waynema1a1d8b62019-03-08 16:58:06 +0800790 hostent* hp =
791 getanswer(buf.get(), n, name, type, res, info->hp, info->buf, info->buflen, &herrno);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900792 if (hp == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800793 return herrnoToAiErrno(herrno);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900794 }
Mike Yucac05e42018-11-06 19:20:07 +0800795 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900796}
797
waynema4c6fd922019-03-08 19:13:41 +0800798static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
799 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900800 char qbuf[MAXDNAME + 1], *qp, *ep;
801 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900802 int advance;
Bernie Innocenti55864192018-08-30 04:05:20 +0900803
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900804 info->hp->h_length = len;
805 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900806
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900807 switch (info->hp->h_addrtype) {
808 case AF_INET:
809 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
810 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
811 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900812
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900813 case AF_INET6:
814 qp = qbuf;
815 ep = qbuf + sizeof(qbuf) - 1;
816 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
817 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
818 ((unsigned int) uaddr[n] >> 4) & 0xf);
819 if (advance > 0 && qp + advance < ep)
820 qp += advance;
821 else {
Hungming Chen806d4462018-12-26 17:04:43 +0800822 // TODO: Consider to remap error code without relying on errno.
823 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900824 }
825 }
826 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
Hungming Chen806d4462018-12-26 17:04:43 +0800827 // TODO: Consider to remap error code without relying on errno.
828 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829 }
830 break;
831 default:
Hungming Chen806d4462018-12-26 17:04:43 +0800832 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900833 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900834
waynema4c6fd922019-03-08 19:13:41 +0800835 auto buf = std::make_unique<querybuf>();
836
837 res_state res = res_get_state();
838 if (!res) return EAI_MEMORY;
839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 res_setnetcontext(res, netcontext);
Hungming Chen56273d72018-12-26 16:14:17 +0800841 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800842 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf), &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900843 if (n < 0) {
Ken Chenbab50142019-03-19 17:41:28 +0800844 LOG(DEBUG) << __func__ << ": res_nquery failed (" << n << ")";
Hungming Chen7f0d3292018-12-27 18:33:19 +0800845 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 }
waynema4c6fd922019-03-08 19:13:41 +0800847 hostent* hp =
848 getanswer(buf.get(), n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849 if (hp == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800850 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900851 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900853 char* bf = (char*) (hp->h_addr_list + 2);
854 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900855 if (blen + info->hp->h_length > info->buflen) goto nospc;
856 hp->h_addr_list[0] = bf;
857 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900858 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900859 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
860 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
861 map_v4v6_address(bf, bf);
862 hp->h_addrtype = AF_INET6;
863 hp->h_length = NS_IN6ADDRSZ;
864 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900865
nuccachen8161c992018-09-11 11:20:00 +0800866 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
867 if (info->hp->h_addrtype == AF_INET) {
868 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
869 // Pad zero to the unused address space
870 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
871 }
872
Hungming Chen806d4462018-12-26 17:04:43 +0800873 return 0;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900874
Bernie Innocenti55864192018-08-30 04:05:20 +0900875nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900876 errno = ENOSPC;
Hungming Chen806d4462018-12-26 17:04:43 +0800877 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900878}
879
Bernie Innocenti55864192018-08-30 04:05:20 +0900880/*
881 * Non-reentrant versions.
882 */
883
Mike Yucac05e42018-11-06 19:20:07 +0800884int android_gethostbynamefornetcontext(const char* name, int af,
885 const struct android_net_context* netcontext, hostent** hp) {
886 int error;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900887 res_state res = res_get_state();
Mike Yucac05e42018-11-06 19:20:07 +0800888 if (res == NULL) return EAI_MEMORY;
889 res_static* rs = res_get_static(); // For thread-safety.
890 error = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
Hungming Chen56273d72018-12-26 16:14:17 +0800891 netcontext);
Mike Yucac05e42018-11-06 19:20:07 +0800892 if (error == 0) {
893 *hp = &rs->host;
894 }
895 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900896}
897
Hungming Chen806d4462018-12-26 17:04:43 +0800898int android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
899 const struct android_net_context* netcontext, hostent** hp) {
900 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext, hp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900901}
902
Hungming Chen806d4462018-12-26 17:04:43 +0800903static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
904 const struct android_net_context* netcontext,
905 hostent** hp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900906 struct res_static* rs = res_get_static(); // For thread-safety.
Hungming Chen806d4462018-12-26 17:04:43 +0800907 int error = android_gethostbyaddrfornetcontext_proxy_internal(
Hungming Chen56273d72018-12-26 16:14:17 +0800908 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), netcontext);
Hungming Chen806d4462018-12-26 17:04:43 +0800909 if (error == 0) *hp = &rs->host;
910 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900911}
Mike Yu69615f62018-11-06 15:42:36 +0800912
Hungming Chen7f0d3292018-12-27 18:33:19 +0800913int herrnoToAiErrno(int herrno) {
914 switch (herrno) {
915 // extended h_errno
916 case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT:
917 return NETD_RESOLV_TIMEOUT;
918 // legacy h_errno
919 case NETDB_SUCCESS:
920 return 0;
921 case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead
922 case NO_DATA: // NO_ADDRESS
Mike Yu69615f62018-11-06 15:42:36 +0800923 return EAI_NODATA;
924 case TRY_AGAIN:
925 return EAI_AGAIN;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800926 case NETDB_INTERNAL:
927 return EAI_SYSTEM; // see errno for detail
928 case NO_RECOVERY:
Mike Yu69615f62018-11-06 15:42:36 +0800929 default:
930 return EAI_FAIL;
931 }
chenbruce16adee42019-02-20 19:45:50 +0800932}