blob: 3bf5aed0bc601a19143c01dc5254190a61c58610 [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
Hungming Chen806d4462018-12-26 17:04:43 +0800116static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
117 const android_net_context* netcontext, getnamaddr* info);
Mike Yucac05e42018-11-06 19:20:07 +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)
chenbruce16adee42019-02-20 19:45:50 +0800280 LOG(DEBUG) << __func__ << "(getanswer): asked for \"" << qname << " "
281 << p_class(C_IN) << " " << p_type(qtype) << "\", got type \""
282 << p_type(type) << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900283 cp += n;
284 continue; /* XXX - had_error++ ? */
285 }
286 switch (type) {
287 case T_PTR:
288 if (strcasecmp(tname, bp) != 0) {
chenbruce16adee42019-02-20 19:45:50 +0800289 LOG(DEBUG) << __func__ << "(getanswer): asked for \"" << qname << "\", got \""
290 << bp << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900291 cp += n;
292 continue; /* XXX - had_error++ ? */
293 }
294 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
295 if ((n < 0) || !maybe_hnok(res, bp)) {
296 had_error++;
297 break;
298 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900299 cp += n;
300 if (cp != erdata) goto no_recovery;
301 if (!haveanswer)
302 hent->h_name = bp;
303 else
lifr1bebdb52019-01-09 14:41:15 +0800304 aliases.push_back(bp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900305 if (n != -1) {
306 n = (int) strlen(bp) + 1; /* for the \0 */
307 if (n >= MAXHOSTNAMELEN) {
308 had_error++;
309 break;
310 }
311 bp += n;
312 }
313 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900314 case T_A:
315 case T_AAAA:
316 if (strcasecmp(hent->h_name, bp) != 0) {
chenbruce16adee42019-02-20 19:45:50 +0800317 LOG(DEBUG) << __func__ << "(getanswer): asked for \"" << hent->h_name
318 << "\", got \"" << bp << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900319 cp += n;
320 continue; /* XXX - had_error++ ? */
321 }
322 if (n != hent->h_length) {
323 cp += n;
324 continue;
325 }
326 if (type == T_AAAA) {
327 struct in6_addr in6;
328 memcpy(&in6, cp, NS_IN6ADDRSZ);
329 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
330 cp += n;
331 continue;
332 }
333 }
334 if (!haveanswer) {
335 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900336
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900337 hent->h_name = bp;
338 nn = (int) strlen(bp) + 1; /* for the \0 */
339 bp += nn;
340 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900341
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900342 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900343
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900344 if (bp + n >= ep) {
chenbruce16adee42019-02-20 19:45:50 +0800345 LOG(DEBUG) << "size (" << n << ") too big";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900346 had_error++;
347 continue;
348 }
349 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
350 if (!toobig++) {
chenbruce16adee42019-02-20 19:45:50 +0800351 LOG(DEBUG) << "Too many addresses (" << MAXADDRS << ")";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900352 }
353 cp += n;
354 continue;
355 }
356 (void) memcpy(*hap++ = bp, cp, (size_t) n);
357 bp += n;
358 cp += n;
359 if (cp != erdata) goto no_recovery;
360 break;
361 default:
362 abort();
363 }
364 if (!had_error) haveanswer++;
365 }
366 if (haveanswer) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900367 *hap = NULL;
368 /*
369 * Note: we sort even if host can take only one address
370 * in its return structures - should give it the "best"
371 * address in that case, not some random one
372 */
373 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
374 if (!hent->h_name) {
375 n = (int) strlen(qname) + 1; /* for the \0 */
376 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
377 strlcpy(bp, qname, (size_t)(ep - bp));
378 hent->h_name = bp;
379 bp += n;
380 }
381 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
nuccachen0a511732018-09-18 13:38:48 +0800382 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900383 goto success;
384 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900385no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900386 *he = NO_RECOVERY;
387 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900388success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900389 bp = (char*) ALIGN(bp);
lifr1bebdb52019-01-09 14:41:15 +0800390 aliases.push_back(nullptr);
391 qlen = aliases.size() * sizeof(*hent->h_aliases);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900392 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900393 hent->h_aliases = (char**) bp;
lifr1bebdb52019-01-09 14:41:15 +0800394 memcpy(bp, aliases.data(), qlen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900395
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900396 bp += qlen;
397 n = (int) (hap - addr_ptrs);
398 qlen = (n + 1) * sizeof(*hent->h_addr_list);
399 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900400 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900401 memcpy(bp, addr_ptrs, qlen);
402 *he = NETDB_SUCCESS;
403 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900404nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900405 errno = ENOSPC;
406 *he = NETDB_INTERNAL;
407 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900408}
409
Mike Yucac05e42018-11-06 19:20:07 +0800410static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800411 char* buf, size_t buflen) {
Mike Yucac05e42018-11-06 19:20:07 +0800412 getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900413 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900414
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900415 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900416
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900417 switch (af) {
418 case AF_INET:
419 size = NS_INADDRSZ;
420 break;
421 case AF_INET6:
422 size = NS_IN6ADDRSZ;
423 break;
424 default:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 errno = EAFNOSUPPORT;
Mike Yucac05e42018-11-06 19:20:07 +0800426 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900427 }
428 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900429
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900430 hp->h_addrtype = af;
431 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900432
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900433 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900434 * disallow names consisting only of digits/dots, unless
435 * they end in a dot.
436 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900437 if (isdigit((u_char) name[0])) {
438 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900439 if (!*cp) {
440 if (*--cp == '.') break;
441 /*
442 * All-numeric, no dot at the end.
443 * Fake up a hostent as if we'd actually
444 * done a lookup.
445 */
446 goto fake;
447 }
448 if (!isdigit((u_char) *cp) && *cp != '.') break;
449 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900450 }
451 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
452 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900453 if (!*cp) {
454 if (*--cp == '.') break;
455 /*
456 * All-IPv6-legal, no dot at the end.
457 * Fake up a hostent as if we'd actually
458 * done a lookup.
459 */
460 goto fake;
461 }
462 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
463 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900464 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900465
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 info.hp = hp;
467 info.buf = buf;
468 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800469 if (_hf_gethtbyname2(name, af, &info)) {
Mike Yucac05e42018-11-06 19:20:07 +0800470 int error = _dns_gethtbyname(name, af, &info);
471 if (error != 0) {
472 return error;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900473 }
474 }
Mike Yucac05e42018-11-06 19:20:07 +0800475 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900476nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900477 errno = ENOSPC;
Hungming Chen56273d72018-12-26 16:14:17 +0800478 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900479fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900480 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
481 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900482
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 hp->h_aliases[0] = NULL;
484 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900485
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900486 if (inet_pton(af, name, buf) <= 0) {
Mike Yucac05e42018-11-06 19:20:07 +0800487 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900488 }
489 hp->h_addr_list[0] = buf;
490 hp->h_addr_list[1] = NULL;
491 buf += size;
492 buflen -= size;
493 HENT_SCOPY(hp->h_name, name, buf, buflen);
494 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
Mike Yucac05e42018-11-06 19:20:07 +0800495 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900496}
497
498// very similar in proxy-ness to android_getaddrinfo_proxy
Mike Yucac05e42018-11-06 19:20:07 +0800499static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Hungming Chen56273d72018-12-26 16:14:17 +0800500 size_t hbuflen, const android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900501 res_setnetcontext(res, netcontext);
Hungming Chen56273d72018-12-26 16:14:17 +0800502 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900503}
504
Hungming Chen806d4462018-12-26 17:04:43 +0800505static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t len, int af,
506 struct hostent* hp, char* buf, size_t buflen,
Hungming Chen806d4462018-12-26 17:04:43 +0800507 const struct android_net_context* netcontext) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900508 const u_char* uaddr = (const u_char*) addr;
509 socklen_t size;
510 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900511
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900512 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900513
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900514 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
515 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
516 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
Hungming Chen806d4462018-12-26 17:04:43 +0800517 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900518 }
519 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
520 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
521 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
522 /* Unmap. */
523 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
524 addr = uaddr;
525 af = AF_INET;
526 len = NS_INADDRSZ;
527 }
528 switch (af) {
529 case AF_INET:
530 size = NS_INADDRSZ;
531 break;
532 case AF_INET6:
533 size = NS_IN6ADDRSZ;
534 break;
535 default:
536 errno = EAFNOSUPPORT;
Hungming Chen806d4462018-12-26 17:04:43 +0800537 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900538 }
539 if (size != len) {
540 errno = EINVAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800541 // TODO: Consider to remap error code without relying on errno.
542 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900543 }
544 info.hp = hp;
545 info.buf = buf;
546 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800547 if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
Hungming Chen806d4462018-12-26 17:04:43 +0800548 int error = _dns_gethtbyaddr(uaddr, len, af, netcontext, &info);
549 if (error != 0) {
550 return error;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900551 }
552 }
Hungming Chen806d4462018-12-26 17:04:43 +0800553 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900554}
555
Hungming Chen806d4462018-12-26 17:04:43 +0800556static int android_gethostbyaddrfornetcontext_proxy_internal(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900557 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
Hungming Chen56273d72018-12-26 16:14:17 +0800558 const struct android_net_context* netcontext) {
559 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900560}
561
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900562struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
563 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900564 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900565 char *name;
lifr1bebdb52019-01-09 14:41:15 +0800566 char* cp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900567 int af, len;
568 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900569 struct in6_addr host_addr;
lifr1bebdb52019-01-09 14:41:15 +0800570 std::vector<char*> aliases;
Bernie Innocenti55864192018-08-30 04:05:20 +0900571
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900572 if (hf == NULL) {
573 *he = NETDB_INTERNAL;
574 errno = EINVAL;
575 return NULL;
576 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900577 char* p = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900578
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900579 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900580 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900581 * as the buffer may be used to store content for a previous hostent
582 * returned by non-reentrant functions like gethostbyname().
583 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900584 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900585 goto nospc;
586 }
587 for (;;) {
588 if (!fgets(p, line_buf_size, hf)) {
589 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900590 *he = HOST_NOT_FOUND;
591 return NULL;
592 }
593 if (*p == '#') {
594 continue;
595 }
596 if (!(cp = strpbrk(p, "#\n"))) {
597 continue;
598 }
599 *cp = '\0';
600 if (!(cp = strpbrk(p, " \t"))) continue;
601 *cp++ = '\0';
602 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
603 af = AF_INET6;
604 len = NS_IN6ADDRSZ;
605 } else {
606 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900607
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900608 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900609 if (res == NULL) goto nospc;
610 if (res->options & RES_USE_INET6) {
611 map_v4v6_address(buf, buf);
612 af = AF_INET6;
613 len = NS_IN6ADDRSZ;
614 } else {
615 af = AF_INET;
616 len = NS_INADDRSZ;
617 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900619
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900620 /* if this is not something we're looking for, skip it. */
621 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
622 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900623
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900624 while (*cp == ' ' || *cp == '\t') cp++;
625 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900626 while (cp && *cp) {
627 if (*cp == ' ' || *cp == '\t') {
628 cp++;
629 continue;
630 }
lifr1bebdb52019-01-09 14:41:15 +0800631 aliases.push_back(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900632 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
633 }
634 break;
635 }
636 hent->h_length = len;
637 hent->h_addrtype = af;
638 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
lifr1bebdb52019-01-09 14:41:15 +0800639 anum = aliases.size();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900640 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
641 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
642 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900643
nuccachen8161c992018-09-11 11:20:00 +0800644 /* Reserve space for mapping IPv4 address to IPv6 address in place */
645 if (hent->h_addrtype == AF_INET) {
646 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
647 }
648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 HENT_SCOPY(hent->h_name, name, buf, buflen);
650 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
651 hent->h_aliases[anum] = NULL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900652 *he = NETDB_SUCCESS;
653 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900654 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900655nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900657 errno = ENOSPC;
658 *he = NETDB_INTERNAL;
659 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900660}
661
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900662static void map_v4v6_address(const char* src, char* dst) {
663 u_char* p = (u_char*) dst;
664 char tmp[NS_INADDRSZ];
665 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900666
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900667 _DIAGASSERT(src != NULL);
668 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900669
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900670 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900671 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900672 /* Mark this ipv6 addr as a mapped ipv4. */
673 for (i = 0; i < 10; i++) *p++ = 0x00;
674 *p++ = 0xff;
675 *p++ = 0xff;
676 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900677 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900678}
679
nuccachen0a511732018-09-18 13:38:48 +0800680static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
681 std::function<void(struct hostent* hp)> map_param,
682 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683 _DIAGASSERT(hp != NULL);
684 _DIAGASSERT(bpp != NULL);
685 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900686
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800688 map_param(hp);
689 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900690 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
693 /* Out of memory. Truncate address list here. XXX */
694 *ap = NULL;
695 return;
696 }
697 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800698 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900699 *ap = *bpp;
700 *bpp += NS_IN6ADDRSZ;
701 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900702}
703
nuccachen0a511732018-09-18 13:38:48 +0800704static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
705 convert_v4v6_hostent(hp, bpp, ep,
706 [](struct hostent* hp) {
707 hp->h_addrtype = AF_INET6;
708 hp->h_length = NS_IN6ADDRSZ;
709 },
710 [](char* src, char* dst) { map_v4v6_address(src, dst); });
711}
712
713/* Reserve space for mapping IPv4 address to IPv6 address in place */
714static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
715 convert_v4v6_hostent(hp, bpp, ep,
716 [](struct hostent* hp) {
717 (void) hp; /* unused */
718 },
719 [](char* src, char* dst) {
720 memcpy(dst, src, NS_INADDRSZ);
721 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
722 });
723}
724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725static void addrsort(char** ap, int num, res_state res) {
726 int i, j;
727 char** p;
728 short aval[MAXADDRS];
729 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900731 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900733 p = ap;
734 for (i = 0; i < num; i++, p++) {
735 for (j = 0; (unsigned) j < res->nsort; j++)
736 if (res->sort_list[j].addr.s_addr ==
737 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
738 break;
739 aval[i] = j;
740 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
741 }
742 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900743
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900744 while (needsort < num) {
745 for (j = needsort - 1; j >= 0; j--) {
746 if (aval[j] > aval[j + 1]) {
747 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900748
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900749 i = aval[j];
750 aval[j] = aval[j + 1];
751 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753 hp = ap[j];
754 ap[j] = ap[j + 1];
755 ap[j + 1] = hp;
756 } else
757 break;
758 }
759 needsort++;
760 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900761}
762
Mike Yucac05e42018-11-06 19:20:07 +0800763static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900764 int n, type;
765 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900766 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900767
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900768 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900769
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900770 switch (info->hp->h_addrtype) {
771 case AF_INET:
772 info->hp->h_length = NS_INADDRSZ;
773 type = T_A;
774 break;
775 case AF_INET6:
776 info->hp->h_length = NS_IN6ADDRSZ;
777 type = T_AAAA;
778 break;
779 default:
Mike Yucac05e42018-11-06 19:20:07 +0800780 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900782 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900783 if (buf == NULL) {
Mike Yucac05e42018-11-06 19:20:07 +0800784 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900785 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900786 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900787 if (res == NULL) {
788 free(buf);
Mike Yucac05e42018-11-06 19:20:07 +0800789 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 }
Mike Yu69615f62018-11-06 15:42:36 +0800791
Hungming Chen56273d72018-12-26 16:14:17 +0800792 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800793 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf), &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900794 if (n < 0) {
795 free(buf);
chenbruce16adee42019-02-20 19:45:50 +0800796 LOG(DEBUG) << "res_nsearch failed (" << n << ")";
Hungming Chen7f0d3292018-12-27 18:33:19 +0800797 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
798 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900799 }
Hungming Chen56273d72018-12-26 16:14:17 +0800800 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900801 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900802 if (hp == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800803 return herrnoToAiErrno(herrno);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900804 }
Mike Yucac05e42018-11-06 19:20:07 +0800805 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900806}
807
Hungming Chen806d4462018-12-26 17:04:43 +0800808static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
809 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900810 char qbuf[MAXDNAME + 1], *qp, *ep;
811 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900813 int advance;
814 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900815
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900816 info->hp->h_length = len;
817 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819 switch (info->hp->h_addrtype) {
820 case AF_INET:
821 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
822 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
823 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825 case AF_INET6:
826 qp = qbuf;
827 ep = qbuf + sizeof(qbuf) - 1;
828 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
829 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
830 ((unsigned int) uaddr[n] >> 4) & 0xf);
831 if (advance > 0 && qp + advance < ep)
832 qp += advance;
833 else {
Hungming Chen806d4462018-12-26 17:04:43 +0800834 // TODO: Consider to remap error code without relying on errno.
835 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 }
837 }
838 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
Hungming Chen806d4462018-12-26 17:04:43 +0800839 // TODO: Consider to remap error code without relying on errno.
840 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900841 }
842 break;
843 default:
Hungming Chen806d4462018-12-26 17:04:43 +0800844 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900845 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900846
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900847 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900848 if (buf == NULL) {
Hungming Chen806d4462018-12-26 17:04:43 +0800849 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900850 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900851 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900852 if (res == NULL) {
853 free(buf);
Hungming Chen806d4462018-12-26 17:04:43 +0800854 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900855 }
856 res_setnetcontext(res, netcontext);
Hungming Chen56273d72018-12-26 16:14:17 +0800857 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800858 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf), &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900859 if (n < 0) {
860 free(buf);
chenbruce16adee42019-02-20 19:45:50 +0800861 LOG(DEBUG) << "res_nquery failed (" << n << ")";
Hungming Chen7f0d3292018-12-27 18:33:19 +0800862 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900863 }
Hungming Chen56273d72018-12-26 16:14:17 +0800864 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900865 free(buf);
866 if (hp == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800867 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900868 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900869
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900870 char* bf = (char*) (hp->h_addr_list + 2);
871 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900872 if (blen + info->hp->h_length > info->buflen) goto nospc;
873 hp->h_addr_list[0] = bf;
874 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900875 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900876 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
877 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
878 map_v4v6_address(bf, bf);
879 hp->h_addrtype = AF_INET6;
880 hp->h_length = NS_IN6ADDRSZ;
881 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900882
nuccachen8161c992018-09-11 11:20:00 +0800883 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
884 if (info->hp->h_addrtype == AF_INET) {
885 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
886 // Pad zero to the unused address space
887 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
888 }
889
Hungming Chen806d4462018-12-26 17:04:43 +0800890 return 0;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900891
Bernie Innocenti55864192018-08-30 04:05:20 +0900892nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900893 errno = ENOSPC;
Hungming Chen806d4462018-12-26 17:04:43 +0800894 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900895}
896
Bernie Innocenti55864192018-08-30 04:05:20 +0900897/*
898 * Non-reentrant versions.
899 */
900
Mike Yucac05e42018-11-06 19:20:07 +0800901int android_gethostbynamefornetcontext(const char* name, int af,
902 const struct android_net_context* netcontext, hostent** hp) {
903 int error;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900904 res_state res = res_get_state();
Mike Yucac05e42018-11-06 19:20:07 +0800905 if (res == NULL) return EAI_MEMORY;
906 res_static* rs = res_get_static(); // For thread-safety.
907 error = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
Hungming Chen56273d72018-12-26 16:14:17 +0800908 netcontext);
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,
916 const struct android_net_context* netcontext, hostent** hp) {
917 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext, hp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900918}
919
Hungming Chen806d4462018-12-26 17:04:43 +0800920static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
921 const struct android_net_context* netcontext,
922 hostent** hp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900923 struct res_static* rs = res_get_static(); // For thread-safety.
Hungming Chen806d4462018-12-26 17:04:43 +0800924 int error = android_gethostbyaddrfornetcontext_proxy_internal(
Hungming Chen56273d72018-12-26 16:14:17 +0800925 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), netcontext);
Hungming Chen806d4462018-12-26 17:04:43 +0800926 if (error == 0) *hp = &rs->host;
927 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900928}
Mike Yu69615f62018-11-06 15:42:36 +0800929
Hungming Chen7f0d3292018-12-27 18:33:19 +0800930int herrnoToAiErrno(int herrno) {
931 switch (herrno) {
932 // extended h_errno
933 case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT:
934 return NETD_RESOLV_TIMEOUT;
935 // legacy h_errno
936 case NETDB_SUCCESS:
937 return 0;
938 case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead
939 case NO_DATA: // NO_ADDRESS
Mike Yu69615f62018-11-06 15:42:36 +0800940 return EAI_NODATA;
941 case TRY_AGAIN:
942 return EAI_AGAIN;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800943 case NETDB_INTERNAL:
944 return EAI_SYSTEM; // see errno for detail
945 case NO_RECOVERY:
Mike Yu69615f62018-11-06 15:42:36 +0800946 default:
947 return EAI_FAIL;
948 }
chenbruce16adee42019-02-20 19:45:50 +0800949}