blob: b9d7205c3bd16853d15f4067a96424be7869bc88 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1988, 1993
5 * -
6 * Copyright (c) 1985, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
Bernie Innocenti55864192018-08-30 04:05:20 +090054#include <arpa/inet.h>
55#include <arpa/nameser.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090056#include <assert.h>
57#include <ctype.h>
58#include <errno.h>
59#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090060#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090061#include <stdarg.h>
62#include <stdbool.h>
63#include <stdio.h>
Bernie Innocentic165ce82018-10-16 23:35:28 +090064#include <stdlib.h>
65#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090066#include <sys/param.h>
67#include <sys/socket.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090068#include <sys/types.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090069#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090070#include <syslog.h>
71#include <unistd.h>
nuccachen0a511732018-09-18 13:38:48 +080072#include <functional>
Bernie Innocentif89b3512018-08-30 07:34:37 +090073
Bernie Innocentic165ce82018-10-16 23:35:28 +090074#include "hostent.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090075#include "netd_resolv/resolv.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090076#include "resolv_cache.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090077#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090078
Bernie Innocentif89b3512018-08-30 07:34:37 +090079// NetBSD uses _DIAGASSERT to null-check arguments and the like,
80// but it's clear from the number of mistakes in their assertions
81// that they don't actually test or ship with this.
82#define _DIAGASSERT(e) /* nothing */
83
nuccachen8161c992018-09-11 11:20:00 +080084// TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent
85// buffer.
Bernie Innocenti55864192018-08-30 04:05:20 +090086#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090087#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090088
89#ifndef LOG_AUTH
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090090#define LOG_AUTH 0
Bernie Innocenti55864192018-08-30 04:05:20 +090091#endif
92
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090093#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
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 Innocenti1f4a9fd2018-09-07 21:10:25 +090099#define addalias(d, s, arr, siz) \
100 do { \
101 if (d >= &arr[siz]) { \
102 char** xptr = (char**) realloc(arr, (siz + 10) * sizeof(*arr)); \
103 if (xptr == NULL) goto nospc; \
104 d = xptr + (d - arr); \
105 arr = xptr; \
106 siz += 10; \
107 } \
108 *d++ = s; \
109 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900110
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900112
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900113#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900114
115typedef union {
116 HEADER hdr;
117 u_char buf[MAXPACKET];
118} querybuf;
119
120typedef union {
121 int32_t al;
122 char ac;
123} align;
124
125#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900126static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900127#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900128static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
129 char*, size_t, int*);
nuccachen0a511732018-09-18 13:38:48 +0800130static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
131 std::function<void(struct hostent* hp)> mapping_param,
132 std::function<void(char* src, char* dst)> mapping_addr);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900133static void map_v4v6_address(const char*, char*);
134static void map_v4v6_hostent(struct hostent*, char**, char*);
nuccachen0a511732018-09-18 13:38:48 +0800135static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900136static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900137
Hungming Chen806d4462018-12-26 17:04:43 +0800138static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
139 const android_net_context* netcontext, getnamaddr* info);
Mike Yucac05e42018-11-06 19:20:07 +0800140static int _dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900141
Mike Yucac05e42018-11-06 19:20:07 +0800142static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Hungming Chen56273d72018-12-26 16:14:17 +0800143 size_t hbuflen, const android_net_context* netcontext);
Mike Yucac05e42018-11-06 19:20:07 +0800144static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800145 char* buf, size_t buflen);
Hungming Chen806d4462018-12-26 17:04:43 +0800146static int android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t, int,
Hungming Chen56273d72018-12-26 16:14:17 +0800147 struct hostent*, char*, size_t,
Hungming Chen806d4462018-12-26 17:04:43 +0800148 const struct android_net_context*);
149static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
150 const struct android_net_context* netcontext,
151 hostent** hp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900152
Bernie Innocenti55864192018-08-30 04:05:20 +0900153#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900154static void debugprintf(const char* msg, res_state res, ...) {
155 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900156
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157 if (res->options & RES_DEBUG) {
158 int save = errno;
159 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900160
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900161 va_start(ap, res);
162 vprintf(msg, ap);
163 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900164
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900165 errno = save;
166 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900167}
168#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900169#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900170#endif
171
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900172#define BOUNDED_INCR(x) \
173 do { \
174 BOUNDS_CHECK(cp, x); \
175 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900176 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900177
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178#define BOUNDS_CHECK(ptr, count) \
179 do { \
180 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900181 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
184 res_state res, struct hostent* hent, char* buf, size_t buflen,
185 int* he) {
186 const HEADER* hp;
187 const u_char* cp;
188 int n;
189 size_t qlen;
190 const u_char *eom, *erdata;
191 char *bp, **ap, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900192 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900193 int haveanswer, had_error;
194 int toobig = 0;
195 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196 char* addr_ptrs[MAXADDRS];
197 const char* tname;
198 int (*name_ok)(const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900199
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900200 _DIAGASSERT(answer != NULL);
201 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900202
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203 tname = qname;
204 hent->h_name = NULL;
205 eom = answer->buf + anslen;
206 switch (qtype) {
207 case T_A:
208 case T_AAAA:
209 name_ok = res_hnok;
210 break;
211 case T_PTR:
212 name_ok = res_dnok;
213 break;
214 default:
215 *he = NO_RECOVERY;
216 return NULL; /* XXX should be abort(); */
217 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900218
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900219 size_t maxaliases = 10;
220 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
221 if (!aliases) goto nospc;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900222 /*
223 * find first satisfactory answer
224 */
225 hp = &answer->hdr;
226 ancount = ntohs(hp->ancount);
227 qdcount = ntohs(hp->qdcount);
228 bp = buf;
229 ep = buf + buflen;
230 cp = answer->buf;
231 BOUNDED_INCR(HFIXEDSZ);
232 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
235 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900236
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900237 BOUNDED_INCR(n + QFIXEDSZ);
238 if (qtype == T_A || qtype == T_AAAA) {
239 /* res_send() has already verified that the query name is the
240 * same as the one we sent; this just gets the expanded name
241 * (i.e., with the succeeding search-domain tacked on).
242 */
243 n = (int) strlen(bp) + 1; /* for the \0 */
244 if (n >= MAXHOSTNAMELEN) goto no_recovery;
245 hent->h_name = bp;
246 bp += n;
247 /* The qname can be abbreviated, but h_name is now absolute. */
248 qname = hent->h_name;
249 }
250 hent->h_aliases = ap = aliases;
251 hent->h_addr_list = hap = addr_ptrs;
252 *ap = NULL;
253 *hap = NULL;
254 haveanswer = 0;
255 had_error = 0;
256 while (ancount-- > 0 && cp < eom && !had_error) {
257 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
258 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
259 had_error++;
260 continue;
261 }
262 cp += n; /* name */
263 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900264 int type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900265 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900266 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900267 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900268 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900269 cp += INT16SZ; /* len */
270 BOUNDS_CHECK(cp, n);
271 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900272 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900273 /* XXX - debug? syslog? */
274 cp += n;
275 continue; /* XXX - had_error++ ? */
276 }
277 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
278 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
279 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
280 had_error++;
281 continue;
282 }
283 cp += n;
284 if (cp != erdata) goto no_recovery;
285 /* Store alias. */
286 addalias(ap, bp, aliases, maxaliases);
287 n = (int) strlen(bp) + 1; /* for the \0 */
288 if (n >= MAXHOSTNAMELEN) {
289 had_error++;
290 continue;
291 }
292 bp += n;
293 /* Get canonical name. */
294 n = (int) strlen(tbuf) + 1; /* for the \0 */
295 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
296 had_error++;
297 continue;
298 }
299 strlcpy(bp, tbuf, (size_t)(ep - bp));
300 hent->h_name = bp;
301 bp += n;
302 continue;
303 }
304 if (qtype == T_PTR && type == T_CNAME) {
305 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
306 if (n < 0 || !maybe_dnok(res, tbuf)) {
307 had_error++;
308 continue;
309 }
310 cp += n;
311 if (cp != erdata) goto no_recovery;
312 /* Get canonical name. */
313 n = (int) strlen(tbuf) + 1; /* for the \0 */
314 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
315 had_error++;
316 continue;
317 }
318 strlcpy(bp, tbuf, (size_t)(ep - bp));
319 tname = bp;
320 bp += n;
321 continue;
322 }
323 if (type != qtype) {
324 if (type != T_KEY && type != T_SIG)
325 syslog(LOG_NOTICE | LOG_AUTH,
326 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
327 p_class(C_IN), p_type(qtype), p_type(type));
328 cp += n;
329 continue; /* XXX - had_error++ ? */
330 }
331 switch (type) {
332 case T_PTR:
333 if (strcasecmp(tname, bp) != 0) {
334 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
335 cp += n;
336 continue; /* XXX - had_error++ ? */
337 }
338 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
339 if ((n < 0) || !maybe_hnok(res, bp)) {
340 had_error++;
341 break;
342 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900343#if MULTI_PTRS_ARE_ALIASES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900344 cp += n;
345 if (cp != erdata) goto no_recovery;
346 if (!haveanswer)
347 hent->h_name = bp;
348 else
349 addalias(ap, bp, aliases, maxaliases);
350 if (n != -1) {
351 n = (int) strlen(bp) + 1; /* for the \0 */
352 if (n >= MAXHOSTNAMELEN) {
353 had_error++;
354 break;
355 }
356 bp += n;
357 }
358 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900359#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900360 hent->h_name = bp;
361 if (res->options & RES_USE_INET6) {
362 n = strlen(bp) + 1; /* for the \0 */
363 if (n >= MAXHOSTNAMELEN) {
364 had_error++;
365 break;
366 }
367 bp += n;
368 map_v4v6_hostent(hent, &bp, ep);
369 }
370 goto success;
Bernie Innocenti55864192018-08-30 04:05:20 +0900371#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900372 case T_A:
373 case T_AAAA:
374 if (strcasecmp(hent->h_name, bp) != 0) {
375 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
376 cp += n;
377 continue; /* XXX - had_error++ ? */
378 }
379 if (n != hent->h_length) {
380 cp += n;
381 continue;
382 }
383 if (type == T_AAAA) {
384 struct in6_addr in6;
385 memcpy(&in6, cp, NS_IN6ADDRSZ);
386 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
387 cp += n;
388 continue;
389 }
390 }
391 if (!haveanswer) {
392 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900393
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900394 hent->h_name = bp;
395 nn = (int) strlen(bp) + 1; /* for the \0 */
396 bp += nn;
397 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900398
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900399 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900400
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900401 if (bp + n >= ep) {
402 debugprintf("size (%d) too big\n", res, n);
403 had_error++;
404 continue;
405 }
406 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
407 if (!toobig++) {
408 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
409 }
410 cp += n;
411 continue;
412 }
413 (void) memcpy(*hap++ = bp, cp, (size_t) n);
414 bp += n;
415 cp += n;
416 if (cp != erdata) goto no_recovery;
417 break;
418 default:
419 abort();
420 }
421 if (!had_error) haveanswer++;
422 }
423 if (haveanswer) {
424 *ap = NULL;
425 *hap = NULL;
426 /*
427 * Note: we sort even if host can take only one address
428 * in its return structures - should give it the "best"
429 * address in that case, not some random one
430 */
431 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
432 if (!hent->h_name) {
433 n = (int) strlen(qname) + 1; /* for the \0 */
434 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
435 strlcpy(bp, qname, (size_t)(ep - bp));
436 hent->h_name = bp;
437 bp += n;
438 }
439 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
nuccachen0a511732018-09-18 13:38:48 +0800440 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 goto success;
442 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900443no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900444 free(aliases);
445 *he = NO_RECOVERY;
446 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900447success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900448 bp = (char*) ALIGN(bp);
449 n = (int) (ap - aliases);
450 qlen = (n + 1) * sizeof(*hent->h_aliases);
451 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900452 hent->h_aliases = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900453 memcpy(bp, aliases, qlen);
454 free(aliases);
455 aliases = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900456
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900457 bp += qlen;
458 n = (int) (hap - addr_ptrs);
459 qlen = (n + 1) * sizeof(*hent->h_addr_list);
460 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900461 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900462 memcpy(bp, addr_ptrs, qlen);
463 *he = NETDB_SUCCESS;
464 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900465nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 free(aliases);
467 errno = ENOSPC;
468 *he = NETDB_INTERNAL;
469 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900470}
471
Mike Yucac05e42018-11-06 19:20:07 +0800472static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800473 char* buf, size_t buflen) {
Mike Yucac05e42018-11-06 19:20:07 +0800474 getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 size_t size;
Hungming Chen56273d72018-12-26 16:14:17 +0800476 // TODO: Remove it once the data member "he" of struct getnamaddr is removed.
477 int he = NETDB_INTERNAL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900478
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900480
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900481 switch (af) {
482 case AF_INET:
483 size = NS_INADDRSZ;
484 break;
485 case AF_INET6:
486 size = NS_IN6ADDRSZ;
487 break;
488 default:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 errno = EAFNOSUPPORT;
Mike Yucac05e42018-11-06 19:20:07 +0800490 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900491 }
492 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900493
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900494 hp->h_addrtype = af;
495 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900496
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900497 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900498 * disallow names consisting only of digits/dots, unless
499 * they end in a dot.
500 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900501 if (isdigit((u_char) name[0])) {
502 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900503 if (!*cp) {
504 if (*--cp == '.') break;
505 /*
506 * All-numeric, no dot at the end.
507 * Fake up a hostent as if we'd actually
508 * done a lookup.
509 */
510 goto fake;
511 }
512 if (!isdigit((u_char) *cp) && *cp != '.') break;
513 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900514 }
515 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
516 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900517 if (!*cp) {
518 if (*--cp == '.') break;
519 /*
520 * All-IPv6-legal, no dot at the end.
521 * Fake up a hostent as if we'd actually
522 * done a lookup.
523 */
524 goto fake;
525 }
526 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
527 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900528 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900529
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900530 info.hp = hp;
531 info.buf = buf;
532 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800533 info.he = &he; // TODO: Remove the data member "he" of struct getnamaddr.
534 if (_hf_gethtbyname2(name, af, &info)) {
Mike Yucac05e42018-11-06 19:20:07 +0800535 int error = _dns_gethtbyname(name, af, &info);
536 if (error != 0) {
537 return error;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900538 }
539 }
Mike Yucac05e42018-11-06 19:20:07 +0800540 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900541nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900542 errno = ENOSPC;
Hungming Chen56273d72018-12-26 16:14:17 +0800543 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900544fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900545 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
546 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900547
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900548 hp->h_aliases[0] = NULL;
549 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900550
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900551 if (inet_pton(af, name, buf) <= 0) {
Mike Yucac05e42018-11-06 19:20:07 +0800552 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900553 }
554 hp->h_addr_list[0] = buf;
555 hp->h_addr_list[1] = NULL;
556 buf += size;
557 buflen -= size;
558 HENT_SCOPY(hp->h_name, name, buf, buflen);
559 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
Mike Yucac05e42018-11-06 19:20:07 +0800560 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900561}
562
563// very similar in proxy-ness to android_getaddrinfo_proxy
Mike Yucac05e42018-11-06 19:20:07 +0800564static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Hungming Chen56273d72018-12-26 16:14:17 +0800565 size_t hbuflen, const android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900566 res_setnetcontext(res, netcontext);
Hungming Chen56273d72018-12-26 16:14:17 +0800567 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900568}
569
Hungming Chen806d4462018-12-26 17:04:43 +0800570static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t len, int af,
571 struct hostent* hp, char* buf, size_t buflen,
Hungming Chen806d4462018-12-26 17:04:43 +0800572 const struct android_net_context* netcontext) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900573 const u_char* uaddr = (const u_char*) addr;
574 socklen_t size;
575 struct getnamaddr info;
Hungming Chen56273d72018-12-26 16:14:17 +0800576 // TODO: Remove it once the data member "he" of struct getnamaddr is removed.
577 int he = NETDB_INTERNAL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900578
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900579 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900580
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900581 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
582 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
583 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
Hungming Chen806d4462018-12-26 17:04:43 +0800584 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900585 }
586 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
587 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
588 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
589 /* Unmap. */
590 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
591 addr = uaddr;
592 af = AF_INET;
593 len = NS_INADDRSZ;
594 }
595 switch (af) {
596 case AF_INET:
597 size = NS_INADDRSZ;
598 break;
599 case AF_INET6:
600 size = NS_IN6ADDRSZ;
601 break;
602 default:
603 errno = EAFNOSUPPORT;
Hungming Chen806d4462018-12-26 17:04:43 +0800604 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 }
606 if (size != len) {
607 errno = EINVAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800608 // TODO: Consider to remap error code without relying on errno.
609 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900610 }
611 info.hp = hp;
612 info.buf = buf;
613 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800614 info.he = &he; // TODO: Remove the data member "he" of struct getnamaddr.
615 if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
Hungming Chen806d4462018-12-26 17:04:43 +0800616 int error = _dns_gethtbyaddr(uaddr, len, af, netcontext, &info);
617 if (error != 0) {
618 return error;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900619 }
620 }
Hungming Chen806d4462018-12-26 17:04:43 +0800621 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900622}
623
Hungming Chen806d4462018-12-26 17:04:43 +0800624static int android_gethostbyaddrfornetcontext_proxy_internal(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
Hungming Chen56273d72018-12-26 16:14:17 +0800626 const struct android_net_context* netcontext) {
627 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900628}
629
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900630struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
631 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900632 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900633 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900634 char *cp, **q;
635 int af, len;
636 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900637 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 if (hf == NULL) {
640 *he = NETDB_INTERNAL;
641 errno = EINVAL;
642 return NULL;
643 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900644 char* p = NULL;
645 size_t maxaliases = 10;
646 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
647 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900648
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900650 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900651 * as the buffer may be used to store content for a previous hostent
652 * returned by non-reentrant functions like gethostbyname().
653 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900654 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900655 goto nospc;
656 }
657 for (;;) {
658 if (!fgets(p, line_buf_size, hf)) {
659 free(p);
660 free(aliases);
661 *he = HOST_NOT_FOUND;
662 return NULL;
663 }
664 if (*p == '#') {
665 continue;
666 }
667 if (!(cp = strpbrk(p, "#\n"))) {
668 continue;
669 }
670 *cp = '\0';
671 if (!(cp = strpbrk(p, " \t"))) continue;
672 *cp++ = '\0';
673 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
674 af = AF_INET6;
675 len = NS_IN6ADDRSZ;
676 } else {
677 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900678
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900679 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680 if (res == NULL) goto nospc;
681 if (res->options & RES_USE_INET6) {
682 map_v4v6_address(buf, buf);
683 af = AF_INET6;
684 len = NS_IN6ADDRSZ;
685 } else {
686 af = AF_INET;
687 len = NS_INADDRSZ;
688 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 /* if this is not something we're looking for, skip it. */
692 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
693 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695 while (*cp == ' ' || *cp == '\t') cp++;
696 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
697 q = aliases;
698 while (cp && *cp) {
699 if (*cp == ' ' || *cp == '\t') {
700 cp++;
701 continue;
702 }
703 addalias(q, cp, aliases, maxaliases);
704 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
705 }
706 break;
707 }
708 hent->h_length = len;
709 hent->h_addrtype = af;
710 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
711 anum = (size_t)(q - aliases);
712 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
713 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
714 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900715
nuccachen8161c992018-09-11 11:20:00 +0800716 /* Reserve space for mapping IPv4 address to IPv6 address in place */
717 if (hent->h_addrtype == AF_INET) {
718 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
719 }
720
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721 HENT_SCOPY(hent->h_name, name, buf, buflen);
722 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
723 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725 *he = NETDB_SUCCESS;
726 free(p);
727 free(aliases);
728 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900729nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900730 free(p);
731 free(aliases);
732 errno = ENOSPC;
733 *he = NETDB_INTERNAL;
734 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900735}
736
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900737static void map_v4v6_address(const char* src, char* dst) {
738 u_char* p = (u_char*) dst;
739 char tmp[NS_INADDRSZ];
740 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 _DIAGASSERT(src != NULL);
743 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900745 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900746 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 /* Mark this ipv6 addr as a mapped ipv4. */
748 for (i = 0; i < 10; i++) *p++ = 0x00;
749 *p++ = 0xff;
750 *p++ = 0xff;
751 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900752 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900753}
754
nuccachen0a511732018-09-18 13:38:48 +0800755static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
756 std::function<void(struct hostent* hp)> map_param,
757 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 _DIAGASSERT(hp != NULL);
759 _DIAGASSERT(bpp != NULL);
760 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900761
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800763 map_param(hp);
764 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900765 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900766
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900767 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
768 /* Out of memory. Truncate address list here. XXX */
769 *ap = NULL;
770 return;
771 }
772 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800773 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900774 *ap = *bpp;
775 *bpp += NS_IN6ADDRSZ;
776 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900777}
778
nuccachen0a511732018-09-18 13:38:48 +0800779static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
780 convert_v4v6_hostent(hp, bpp, ep,
781 [](struct hostent* hp) {
782 hp->h_addrtype = AF_INET6;
783 hp->h_length = NS_IN6ADDRSZ;
784 },
785 [](char* src, char* dst) { map_v4v6_address(src, dst); });
786}
787
788/* Reserve space for mapping IPv4 address to IPv6 address in place */
789static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
790 convert_v4v6_hostent(hp, bpp, ep,
791 [](struct hostent* hp) {
792 (void) hp; /* unused */
793 },
794 [](char* src, char* dst) {
795 memcpy(dst, src, NS_INADDRSZ);
796 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
797 });
798}
799
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900800static void addrsort(char** ap, int num, res_state res) {
801 int i, j;
802 char** p;
803 short aval[MAXADDRS];
804 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900805
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900806 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900807
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900808 p = ap;
809 for (i = 0; i < num; i++, p++) {
810 for (j = 0; (unsigned) j < res->nsort; j++)
811 if (res->sort_list[j].addr.s_addr ==
812 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
813 break;
814 aval[i] = j;
815 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
816 }
817 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819 while (needsort < num) {
820 for (j = needsort - 1; j >= 0; j--) {
821 if (aval[j] > aval[j + 1]) {
822 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900824 i = aval[j];
825 aval[j] = aval[j + 1];
826 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 hp = ap[j];
829 ap[j] = ap[j + 1];
830 ap[j + 1] = hp;
831 } else
832 break;
833 }
834 needsort++;
835 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900836}
837
Mike Yucac05e42018-11-06 19:20:07 +0800838static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900839 int n, type;
840 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900841 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900842
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900843 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900844
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900845 switch (info->hp->h_addrtype) {
846 case AF_INET:
847 info->hp->h_length = NS_INADDRSZ;
848 type = T_A;
849 break;
850 case AF_INET6:
851 info->hp->h_length = NS_IN6ADDRSZ;
852 type = T_AAAA;
853 break;
854 default:
Mike Yucac05e42018-11-06 19:20:07 +0800855 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900856 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900857 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900858 if (buf == NULL) {
859 *info->he = NETDB_INTERNAL;
Mike Yucac05e42018-11-06 19:20:07 +0800860 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900861 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900862 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900863 if (res == NULL) {
864 free(buf);
Mike Yucac05e42018-11-06 19:20:07 +0800865 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900866 }
Mike Yu69615f62018-11-06 15:42:36 +0800867
Hungming Chen56273d72018-12-26 16:14:17 +0800868 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800869 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf), &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900870 if (n < 0) {
871 free(buf);
872 debugprintf("res_nsearch failed (%d)\n", res, n);
Hungming Chen7f0d3292018-12-27 18:33:19 +0800873 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
874 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875 }
Hungming Chen56273d72018-12-26 16:14:17 +0800876 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900877 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900878 if (hp == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800879 return herrnoToAiErrno(herrno);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900880 }
Mike Yucac05e42018-11-06 19:20:07 +0800881 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900882}
883
Hungming Chen806d4462018-12-26 17:04:43 +0800884static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
885 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900886 char qbuf[MAXDNAME + 1], *qp, *ep;
887 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900888 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900889 int advance;
890 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900891
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900892 info->hp->h_length = len;
893 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900894
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900895 switch (info->hp->h_addrtype) {
896 case AF_INET:
897 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
898 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
899 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900900
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900901 case AF_INET6:
902 qp = qbuf;
903 ep = qbuf + sizeof(qbuf) - 1;
904 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
905 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
906 ((unsigned int) uaddr[n] >> 4) & 0xf);
907 if (advance > 0 && qp + advance < ep)
908 qp += advance;
909 else {
910 *info->he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800911 // TODO: Consider to remap error code without relying on errno.
912 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900913 }
914 }
915 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
916 *info->he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800917 // TODO: Consider to remap error code without relying on errno.
918 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900919 }
920 break;
921 default:
Hungming Chen806d4462018-12-26 17:04:43 +0800922 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900923 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900924
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900925 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900926 if (buf == NULL) {
927 *info->he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800928 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900929 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900930 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900931 if (res == NULL) {
932 free(buf);
Hungming Chen806d4462018-12-26 17:04:43 +0800933 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900934 }
935 res_setnetcontext(res, netcontext);
Hungming Chen56273d72018-12-26 16:14:17 +0800936 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800937 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf), &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900938 if (n < 0) {
939 free(buf);
940 debugprintf("res_nquery failed (%d)\n", res, n);
Hungming Chen7f0d3292018-12-27 18:33:19 +0800941 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900942 }
Hungming Chen56273d72018-12-26 16:14:17 +0800943 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900944 free(buf);
945 if (hp == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800946 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900948
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900949 char* bf = (char*) (hp->h_addr_list + 2);
950 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900951 if (blen + info->hp->h_length > info->buflen) goto nospc;
952 hp->h_addr_list[0] = bf;
953 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900954 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900955 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
956 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
957 map_v4v6_address(bf, bf);
958 hp->h_addrtype = AF_INET6;
959 hp->h_length = NS_IN6ADDRSZ;
960 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900961
nuccachen8161c992018-09-11 11:20:00 +0800962 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
963 if (info->hp->h_addrtype == AF_INET) {
964 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
965 // Pad zero to the unused address space
966 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
967 }
968
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900969 *info->he = NETDB_SUCCESS;
Hungming Chen806d4462018-12-26 17:04:43 +0800970 return 0;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900971
Bernie Innocenti55864192018-08-30 04:05:20 +0900972nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900973 errno = ENOSPC;
974 *info->he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800975 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900976}
977
Bernie Innocenti55864192018-08-30 04:05:20 +0900978/*
979 * Non-reentrant versions.
980 */
981
Mike Yucac05e42018-11-06 19:20:07 +0800982int android_gethostbynamefornetcontext(const char* name, int af,
983 const struct android_net_context* netcontext, hostent** hp) {
984 int error;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900985 res_state res = res_get_state();
Mike Yucac05e42018-11-06 19:20:07 +0800986 if (res == NULL) return EAI_MEMORY;
987 res_static* rs = res_get_static(); // For thread-safety.
988 error = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
Hungming Chen56273d72018-12-26 16:14:17 +0800989 netcontext);
Mike Yucac05e42018-11-06 19:20:07 +0800990 if (error == 0) {
991 *hp = &rs->host;
992 }
993 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900994}
995
Hungming Chen806d4462018-12-26 17:04:43 +0800996int android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
997 const struct android_net_context* netcontext, hostent** hp) {
998 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext, hp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900999}
1000
Hungming Chen806d4462018-12-26 17:04:43 +08001001static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
1002 const struct android_net_context* netcontext,
1003 hostent** hp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001004 struct res_static* rs = res_get_static(); // For thread-safety.
Hungming Chen806d4462018-12-26 17:04:43 +08001005 int error = android_gethostbyaddrfornetcontext_proxy_internal(
Hungming Chen56273d72018-12-26 16:14:17 +08001006 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), netcontext);
Hungming Chen806d4462018-12-26 17:04:43 +08001007 if (error == 0) *hp = &rs->host;
1008 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +09001009}
Mike Yu69615f62018-11-06 15:42:36 +08001010
Hungming Chen7f0d3292018-12-27 18:33:19 +08001011int herrnoToAiErrno(int herrno) {
1012 switch (herrno) {
1013 // extended h_errno
1014 case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT:
1015 return NETD_RESOLV_TIMEOUT;
1016 // legacy h_errno
1017 case NETDB_SUCCESS:
1018 return 0;
1019 case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead
1020 case NO_DATA: // NO_ADDRESS
Mike Yu69615f62018-11-06 15:42:36 +08001021 return EAI_NODATA;
1022 case TRY_AGAIN:
1023 return EAI_AGAIN;
Hungming Chen7f0d3292018-12-27 18:33:19 +08001024 case NETDB_INTERNAL:
1025 return EAI_SYSTEM; // see errno for detail
1026 case NO_RECOVERY:
Mike Yu69615f62018-11-06 15:42:36 +08001027 default:
1028 return EAI_FAIL;
1029 }
Hungming Chen7f0d3292018-12-27 18:33:19 +08001030}