blob: 0ccc3d711b31c51d7fb3700fab4cf16a64b12b29 [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,
143 size_t hbuflen, int* errorp,
144 const android_net_context* netcontext);
145static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
146 char* buf, size_t buflen, int* he);
Hungming Chen806d4462018-12-26 17:04:43 +0800147static int android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t, int,
148 struct hostent*, char*, size_t, int*,
149 const struct android_net_context*);
150static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
151 const struct android_net_context* netcontext,
152 hostent** hp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900153
Bernie Innocenti55864192018-08-30 04:05:20 +0900154#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155static void debugprintf(const char* msg, res_state res, ...) {
156 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900157
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900158 if (res->options & RES_DEBUG) {
159 int save = errno;
160 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900161
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162 va_start(ap, res);
163 vprintf(msg, ap);
164 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900165
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166 errno = save;
167 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900168}
169#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900170#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900171#endif
172
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173#define BOUNDED_INCR(x) \
174 do { \
175 BOUNDS_CHECK(cp, x); \
176 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900177 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900178
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900179#define BOUNDS_CHECK(ptr, count) \
180 do { \
181 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900182 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900183
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900184static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
185 res_state res, struct hostent* hent, char* buf, size_t buflen,
186 int* he) {
187 const HEADER* hp;
188 const u_char* cp;
189 int n;
190 size_t qlen;
191 const u_char *eom, *erdata;
192 char *bp, **ap, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900193 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194 int haveanswer, had_error;
195 int toobig = 0;
196 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197 char* addr_ptrs[MAXADDRS];
198 const char* tname;
199 int (*name_ok)(const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900200
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201 _DIAGASSERT(answer != NULL);
202 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900203
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 tname = qname;
205 hent->h_name = NULL;
206 eom = answer->buf + anslen;
207 switch (qtype) {
208 case T_A:
209 case T_AAAA:
210 name_ok = res_hnok;
211 break;
212 case T_PTR:
213 name_ok = res_dnok;
214 break;
215 default:
216 *he = NO_RECOVERY;
217 return NULL; /* XXX should be abort(); */
218 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900219
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900220 size_t maxaliases = 10;
221 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
222 if (!aliases) goto nospc;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223 /*
224 * find first satisfactory answer
225 */
226 hp = &answer->hdr;
227 ancount = ntohs(hp->ancount);
228 qdcount = ntohs(hp->qdcount);
229 bp = buf;
230 ep = buf + buflen;
231 cp = answer->buf;
232 BOUNDED_INCR(HFIXEDSZ);
233 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900235 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
236 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900237
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900238 BOUNDED_INCR(n + QFIXEDSZ);
239 if (qtype == T_A || qtype == T_AAAA) {
240 /* res_send() has already verified that the query name is the
241 * same as the one we sent; this just gets the expanded name
242 * (i.e., with the succeeding search-domain tacked on).
243 */
244 n = (int) strlen(bp) + 1; /* for the \0 */
245 if (n >= MAXHOSTNAMELEN) goto no_recovery;
246 hent->h_name = bp;
247 bp += n;
248 /* The qname can be abbreviated, but h_name is now absolute. */
249 qname = hent->h_name;
250 }
251 hent->h_aliases = ap = aliases;
252 hent->h_addr_list = hap = addr_ptrs;
253 *ap = NULL;
254 *hap = NULL;
255 haveanswer = 0;
256 had_error = 0;
257 while (ancount-- > 0 && cp < eom && !had_error) {
258 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
259 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
260 had_error++;
261 continue;
262 }
263 cp += n; /* name */
264 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900265 int type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900267 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900268 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900269 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270 cp += INT16SZ; /* len */
271 BOUNDS_CHECK(cp, n);
272 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900273 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900274 /* XXX - debug? syslog? */
275 cp += n;
276 continue; /* XXX - had_error++ ? */
277 }
278 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
279 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
280 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
281 had_error++;
282 continue;
283 }
284 cp += n;
285 if (cp != erdata) goto no_recovery;
286 /* Store alias. */
287 addalias(ap, bp, aliases, maxaliases);
288 n = (int) strlen(bp) + 1; /* for the \0 */
289 if (n >= MAXHOSTNAMELEN) {
290 had_error++;
291 continue;
292 }
293 bp += n;
294 /* Get canonical name. */
295 n = (int) strlen(tbuf) + 1; /* for the \0 */
296 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
297 had_error++;
298 continue;
299 }
300 strlcpy(bp, tbuf, (size_t)(ep - bp));
301 hent->h_name = bp;
302 bp += n;
303 continue;
304 }
305 if (qtype == T_PTR && type == T_CNAME) {
306 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
307 if (n < 0 || !maybe_dnok(res, tbuf)) {
308 had_error++;
309 continue;
310 }
311 cp += n;
312 if (cp != erdata) goto no_recovery;
313 /* Get canonical name. */
314 n = (int) strlen(tbuf) + 1; /* for the \0 */
315 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
316 had_error++;
317 continue;
318 }
319 strlcpy(bp, tbuf, (size_t)(ep - bp));
320 tname = bp;
321 bp += n;
322 continue;
323 }
324 if (type != qtype) {
325 if (type != T_KEY && type != T_SIG)
326 syslog(LOG_NOTICE | LOG_AUTH,
327 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
328 p_class(C_IN), p_type(qtype), p_type(type));
329 cp += n;
330 continue; /* XXX - had_error++ ? */
331 }
332 switch (type) {
333 case T_PTR:
334 if (strcasecmp(tname, bp) != 0) {
335 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
336 cp += n;
337 continue; /* XXX - had_error++ ? */
338 }
339 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
340 if ((n < 0) || !maybe_hnok(res, bp)) {
341 had_error++;
342 break;
343 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900344#if MULTI_PTRS_ARE_ALIASES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900345 cp += n;
346 if (cp != erdata) goto no_recovery;
347 if (!haveanswer)
348 hent->h_name = bp;
349 else
350 addalias(ap, bp, aliases, maxaliases);
351 if (n != -1) {
352 n = (int) strlen(bp) + 1; /* for the \0 */
353 if (n >= MAXHOSTNAMELEN) {
354 had_error++;
355 break;
356 }
357 bp += n;
358 }
359 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900360#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900361 hent->h_name = bp;
362 if (res->options & RES_USE_INET6) {
363 n = strlen(bp) + 1; /* for the \0 */
364 if (n >= MAXHOSTNAMELEN) {
365 had_error++;
366 break;
367 }
368 bp += n;
369 map_v4v6_hostent(hent, &bp, ep);
370 }
371 goto success;
Bernie Innocenti55864192018-08-30 04:05:20 +0900372#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900373 case T_A:
374 case T_AAAA:
375 if (strcasecmp(hent->h_name, bp) != 0) {
376 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
377 cp += n;
378 continue; /* XXX - had_error++ ? */
379 }
380 if (n != hent->h_length) {
381 cp += n;
382 continue;
383 }
384 if (type == T_AAAA) {
385 struct in6_addr in6;
386 memcpy(&in6, cp, NS_IN6ADDRSZ);
387 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
388 cp += n;
389 continue;
390 }
391 }
392 if (!haveanswer) {
393 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900394
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900395 hent->h_name = bp;
396 nn = (int) strlen(bp) + 1; /* for the \0 */
397 bp += nn;
398 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900399
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900400 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900401
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900402 if (bp + n >= ep) {
403 debugprintf("size (%d) too big\n", res, n);
404 had_error++;
405 continue;
406 }
407 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
408 if (!toobig++) {
409 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
410 }
411 cp += n;
412 continue;
413 }
414 (void) memcpy(*hap++ = bp, cp, (size_t) n);
415 bp += n;
416 cp += n;
417 if (cp != erdata) goto no_recovery;
418 break;
419 default:
420 abort();
421 }
422 if (!had_error) haveanswer++;
423 }
424 if (haveanswer) {
425 *ap = NULL;
426 *hap = NULL;
427 /*
428 * Note: we sort even if host can take only one address
429 * in its return structures - should give it the "best"
430 * address in that case, not some random one
431 */
432 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
433 if (!hent->h_name) {
434 n = (int) strlen(qname) + 1; /* for the \0 */
435 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
436 strlcpy(bp, qname, (size_t)(ep - bp));
437 hent->h_name = bp;
438 bp += n;
439 }
440 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
nuccachen0a511732018-09-18 13:38:48 +0800441 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900442 goto success;
443 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900444no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900445 free(aliases);
446 *he = NO_RECOVERY;
447 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900448success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900449 bp = (char*) ALIGN(bp);
450 n = (int) (ap - aliases);
451 qlen = (n + 1) * sizeof(*hent->h_aliases);
452 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900453 hent->h_aliases = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 memcpy(bp, aliases, qlen);
455 free(aliases);
456 aliases = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900457
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900458 bp += qlen;
459 n = (int) (hap - addr_ptrs);
460 qlen = (n + 1) * sizeof(*hent->h_addr_list);
461 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900462 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900463 memcpy(bp, addr_ptrs, qlen);
464 *he = NETDB_SUCCESS;
465 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900466nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900467 free(aliases);
468 errno = ENOSPC;
469 *he = NETDB_INTERNAL;
470 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900471}
472
Mike Yucac05e42018-11-06 19:20:07 +0800473static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
474 char* buf, size_t buflen, int* he) {
475 getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900476 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900477
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900478 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900479
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900480 switch (af) {
481 case AF_INET:
482 size = NS_INADDRSZ;
483 break;
484 case AF_INET6:
485 size = NS_IN6ADDRSZ;
486 break;
487 default:
488 *he = NETDB_INTERNAL;
489 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 *he = NETDB_INTERNAL;
531 info.hp = hp;
532 info.buf = buf;
533 info.buflen = buflen;
534 info.he = he;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900535 if (!_hf_gethtbyname2(name, af, &info)) {
Mike Yucac05e42018-11-06 19:20:07 +0800536 int error = _dns_gethtbyname(name, af, &info);
537 if (error != 0) {
538 return error;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900539 }
540 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900541 *he = NETDB_SUCCESS;
Mike Yucac05e42018-11-06 19:20:07 +0800542 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900543nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 *he = NETDB_INTERNAL;
545 errno = ENOSPC;
Mike Yucac05e42018-11-06 19:20:07 +0800546 // Bad arguments
547 return EAI_FAIL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900548fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900549 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
550 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900551
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900552 hp->h_aliases[0] = NULL;
553 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900555 if (inet_pton(af, name, buf) <= 0) {
556 *he = HOST_NOT_FOUND;
Mike Yucac05e42018-11-06 19:20:07 +0800557 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900558 }
559 hp->h_addr_list[0] = buf;
560 hp->h_addr_list[1] = NULL;
561 buf += size;
562 buflen -= size;
563 HENT_SCOPY(hp->h_name, name, buf, buflen);
564 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
565 *he = NETDB_SUCCESS;
Mike Yucac05e42018-11-06 19:20:07 +0800566 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900567}
568
569// very similar in proxy-ness to android_getaddrinfo_proxy
Mike Yucac05e42018-11-06 19:20:07 +0800570static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
571 size_t hbuflen, int* errorp,
572 const android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900573 res_setnetcontext(res, netcontext);
574 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900575}
576
Hungming Chen806d4462018-12-26 17:04:43 +0800577static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t len, int af,
578 struct hostent* hp, char* buf, size_t buflen,
579 int* he,
580 const struct android_net_context* netcontext) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900581 const u_char* uaddr = (const u_char*) addr;
582 socklen_t size;
583 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900584
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900585 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900586
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900587 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
588 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
589 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
590 *he = HOST_NOT_FOUND;
Hungming Chen806d4462018-12-26 17:04:43 +0800591 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900592 }
593 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
594 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
595 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
596 /* Unmap. */
597 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
598 addr = uaddr;
599 af = AF_INET;
600 len = NS_INADDRSZ;
601 }
602 switch (af) {
603 case AF_INET:
604 size = NS_INADDRSZ;
605 break;
606 case AF_INET6:
607 size = NS_IN6ADDRSZ;
608 break;
609 default:
610 errno = EAFNOSUPPORT;
611 *he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800612 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900613 }
614 if (size != len) {
615 errno = EINVAL;
616 *he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800617 // TODO: Consider to remap error code without relying on errno.
618 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900619 }
620 info.hp = hp;
621 info.buf = buf;
622 info.buflen = buflen;
623 info.he = he;
624 *he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900625 if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
Hungming Chen806d4462018-12-26 17:04:43 +0800626 int error = _dns_gethtbyaddr(uaddr, len, af, netcontext, &info);
627 if (error != 0) {
628 return error;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900629 }
630 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900631 *he = NETDB_SUCCESS;
Hungming Chen806d4462018-12-26 17:04:43 +0800632 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900633}
634
Hungming Chen806d4462018-12-26 17:04:43 +0800635static int android_gethostbyaddrfornetcontext_proxy_internal(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900636 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
637 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900638 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
639 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900640}
641
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900642struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
643 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900644 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900645 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900646 char *cp, **q;
647 int af, len;
648 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900650
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900651 if (hf == NULL) {
652 *he = NETDB_INTERNAL;
653 errno = EINVAL;
654 return NULL;
655 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900656 char* p = NULL;
657 size_t maxaliases = 10;
658 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
659 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900660
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900661 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900662 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 * as the buffer may be used to store content for a previous hostent
664 * returned by non-reentrant functions like gethostbyname().
665 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900666 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900667 goto nospc;
668 }
669 for (;;) {
670 if (!fgets(p, line_buf_size, hf)) {
671 free(p);
672 free(aliases);
673 *he = HOST_NOT_FOUND;
674 return NULL;
675 }
676 if (*p == '#') {
677 continue;
678 }
679 if (!(cp = strpbrk(p, "#\n"))) {
680 continue;
681 }
682 *cp = '\0';
683 if (!(cp = strpbrk(p, " \t"))) continue;
684 *cp++ = '\0';
685 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
686 af = AF_INET6;
687 len = NS_IN6ADDRSZ;
688 } else {
689 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900690
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900691 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900692 if (res == NULL) goto nospc;
693 if (res->options & RES_USE_INET6) {
694 map_v4v6_address(buf, buf);
695 af = AF_INET6;
696 len = NS_IN6ADDRSZ;
697 } else {
698 af = AF_INET;
699 len = NS_INADDRSZ;
700 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900701 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900702
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703 /* if this is not something we're looking for, skip it. */
704 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
705 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900706
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900707 while (*cp == ' ' || *cp == '\t') cp++;
708 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
709 q = aliases;
710 while (cp && *cp) {
711 if (*cp == ' ' || *cp == '\t') {
712 cp++;
713 continue;
714 }
715 addalias(q, cp, aliases, maxaliases);
716 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
717 }
718 break;
719 }
720 hent->h_length = len;
721 hent->h_addrtype = af;
722 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
723 anum = (size_t)(q - aliases);
724 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
725 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
726 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900727
nuccachen8161c992018-09-11 11:20:00 +0800728 /* Reserve space for mapping IPv4 address to IPv6 address in place */
729 if (hent->h_addrtype == AF_INET) {
730 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
731 }
732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900733 HENT_SCOPY(hent->h_name, name, buf, buflen);
734 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
735 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900736
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900737 *he = NETDB_SUCCESS;
738 free(p);
739 free(aliases);
740 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900741nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 free(p);
743 free(aliases);
744 errno = ENOSPC;
745 *he = NETDB_INTERNAL;
746 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900747}
748
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900749static void map_v4v6_address(const char* src, char* dst) {
750 u_char* p = (u_char*) dst;
751 char tmp[NS_INADDRSZ];
752 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900753
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900754 _DIAGASSERT(src != NULL);
755 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900756
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900757 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900758 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900759 /* Mark this ipv6 addr as a mapped ipv4. */
760 for (i = 0; i < 10; i++) *p++ = 0x00;
761 *p++ = 0xff;
762 *p++ = 0xff;
763 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900764 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900765}
766
nuccachen0a511732018-09-18 13:38:48 +0800767static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
768 std::function<void(struct hostent* hp)> map_param,
769 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900770 _DIAGASSERT(hp != NULL);
771 _DIAGASSERT(bpp != NULL);
772 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900773
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900774 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800775 map_param(hp);
776 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900778
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900779 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
780 /* Out of memory. Truncate address list here. XXX */
781 *ap = NULL;
782 return;
783 }
784 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800785 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900786 *ap = *bpp;
787 *bpp += NS_IN6ADDRSZ;
788 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900789}
790
nuccachen0a511732018-09-18 13:38:48 +0800791static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
792 convert_v4v6_hostent(hp, bpp, ep,
793 [](struct hostent* hp) {
794 hp->h_addrtype = AF_INET6;
795 hp->h_length = NS_IN6ADDRSZ;
796 },
797 [](char* src, char* dst) { map_v4v6_address(src, dst); });
798}
799
800/* Reserve space for mapping IPv4 address to IPv6 address in place */
801static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
802 convert_v4v6_hostent(hp, bpp, ep,
803 [](struct hostent* hp) {
804 (void) hp; /* unused */
805 },
806 [](char* src, char* dst) {
807 memcpy(dst, src, NS_INADDRSZ);
808 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
809 });
810}
811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812static void addrsort(char** ap, int num, res_state res) {
813 int i, j;
814 char** p;
815 short aval[MAXADDRS];
816 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900818 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900819
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900820 p = ap;
821 for (i = 0; i < num; i++, p++) {
822 for (j = 0; (unsigned) j < res->nsort; j++)
823 if (res->sort_list[j].addr.s_addr ==
824 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
825 break;
826 aval[i] = j;
827 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
828 }
829 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 while (needsort < num) {
832 for (j = needsort - 1; j >= 0; j--) {
833 if (aval[j] > aval[j + 1]) {
834 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900835
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 i = aval[j];
837 aval[j] = aval[j + 1];
838 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 hp = ap[j];
841 ap[j] = ap[j + 1];
842 ap[j + 1] = hp;
843 } else
844 break;
845 }
846 needsort++;
847 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900848}
849
Mike Yucac05e42018-11-06 19:20:07 +0800850static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900851 int n, type;
852 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900854
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900855 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900856
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900857 switch (info->hp->h_addrtype) {
858 case AF_INET:
859 info->hp->h_length = NS_INADDRSZ;
860 type = T_A;
861 break;
862 case AF_INET6:
863 info->hp->h_length = NS_IN6ADDRSZ;
864 type = T_AAAA;
865 break;
866 default:
Mike Yucac05e42018-11-06 19:20:07 +0800867 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900868 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900869 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900870 if (buf == NULL) {
871 *info->he = NETDB_INTERNAL;
Mike Yucac05e42018-11-06 19:20:07 +0800872 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900873 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900874 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875 if (res == NULL) {
876 free(buf);
Mike Yucac05e42018-11-06 19:20:07 +0800877 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900878 }
Mike Yu69615f62018-11-06 15:42:36 +0800879
880 int ai_error = EAI_NODATA;
881 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf), &ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900882 if (n < 0) {
883 free(buf);
884 debugprintf("res_nsearch failed (%d)\n", res, n);
Mike Yu69615f62018-11-06 15:42:36 +0800885
886 // If server responds empty answer with rcode NOERROR, adjust the error so netd will
887 // get the nulltpr hp.
888 // TODO: Adjust the error closed to res_nsend instead of here after h_errno is removed.
889 if (ai_error == 0) {
890 return herrnoToAiError(h_errno);
891 }
892 return ai_error;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900893 }
894 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
895 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900896 if (hp == NULL) {
Mike Yu69615f62018-11-06 15:42:36 +0800897 return herrnoToAiError(h_errno);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900898 }
Mike Yucac05e42018-11-06 19:20:07 +0800899 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900900}
901
Hungming Chen806d4462018-12-26 17:04:43 +0800902static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
903 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900904 char qbuf[MAXDNAME + 1], *qp, *ep;
905 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900906 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900907 int advance;
908 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900909
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900910 info->hp->h_length = len;
911 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900912
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900913 switch (info->hp->h_addrtype) {
914 case AF_INET:
915 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
916 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
917 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900918
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900919 case AF_INET6:
920 qp = qbuf;
921 ep = qbuf + sizeof(qbuf) - 1;
922 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
923 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
924 ((unsigned int) uaddr[n] >> 4) & 0xf);
925 if (advance > 0 && qp + advance < ep)
926 qp += advance;
927 else {
928 *info->he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800929 // TODO: Consider to remap error code without relying on errno.
930 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900931 }
932 }
933 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
934 *info->he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800935 // TODO: Consider to remap error code without relying on errno.
936 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900937 }
938 break;
939 default:
Hungming Chen806d4462018-12-26 17:04:43 +0800940 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900941 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900942
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900943 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900944 if (buf == NULL) {
945 *info->he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800946 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900948 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900949 if (res == NULL) {
950 free(buf);
Hungming Chen806d4462018-12-26 17:04:43 +0800951 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900952 }
953 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +0800954 int ai_error = 0;
955 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf), &ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900956 if (n < 0) {
957 free(buf);
958 debugprintf("res_nquery failed (%d)\n", res, n);
Hungming Chen806d4462018-12-26 17:04:43 +0800959
960 // TODO: Consider to return more meaningful error codes.
961 // Currently, doesn't consider ai_error as _dns_gethtbyname() does because current ai_error
962 // comes from rcode only and rcode NOERROR doesn't really mean no error for the whole DNS
963 // query progress. DNS server may respond a DNS packet without any answer for queried
964 // address. In this case, return error code from h_errno NO_DATA rather than rcode NOERROR
965 // (ai_error).
966 return herrnoToAiError(h_errno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 }
968 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
969 free(buf);
970 if (hp == NULL) {
Hungming Chen806d4462018-12-26 17:04:43 +0800971 return herrnoToAiError(h_errno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900972 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900973
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900974 char* bf = (char*) (hp->h_addr_list + 2);
975 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900976 if (blen + info->hp->h_length > info->buflen) goto nospc;
977 hp->h_addr_list[0] = bf;
978 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900979 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900980 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
981 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
982 map_v4v6_address(bf, bf);
983 hp->h_addrtype = AF_INET6;
984 hp->h_length = NS_IN6ADDRSZ;
985 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900986
nuccachen8161c992018-09-11 11:20:00 +0800987 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
988 if (info->hp->h_addrtype == AF_INET) {
989 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
990 // Pad zero to the unused address space
991 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
992 }
993
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900994 *info->he = NETDB_SUCCESS;
Hungming Chen806d4462018-12-26 17:04:43 +0800995 return 0;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900996
Bernie Innocenti55864192018-08-30 04:05:20 +0900997nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900998 errno = ENOSPC;
999 *info->he = NETDB_INTERNAL;
Hungming Chen806d4462018-12-26 17:04:43 +08001000 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +09001001}
1002
Bernie Innocenti55864192018-08-30 04:05:20 +09001003/*
1004 * Non-reentrant versions.
1005 */
1006
Mike Yucac05e42018-11-06 19:20:07 +08001007int android_gethostbynamefornetcontext(const char* name, int af,
1008 const struct android_net_context* netcontext, hostent** hp) {
1009 int error;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001010 res_state res = res_get_state();
Mike Yucac05e42018-11-06 19:20:07 +08001011 if (res == NULL) return EAI_MEMORY;
1012 res_static* rs = res_get_static(); // For thread-safety.
1013 error = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
1014 &h_errno, netcontext);
1015 if (error == 0) {
1016 *hp = &rs->host;
1017 }
1018 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +09001019}
1020
Hungming Chen806d4462018-12-26 17:04:43 +08001021int android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1022 const struct android_net_context* netcontext, hostent** hp) {
1023 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext, hp);
Bernie Innocenti55864192018-08-30 04:05:20 +09001024}
1025
Hungming Chen806d4462018-12-26 17:04:43 +08001026static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
1027 const struct android_net_context* netcontext,
1028 hostent** hp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001029 struct res_static* rs = res_get_static(); // For thread-safety.
Hungming Chen806d4462018-12-26 17:04:43 +08001030 int error = android_gethostbyaddrfornetcontext_proxy_internal(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001031 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Hungming Chen806d4462018-12-26 17:04:43 +08001032 if (error == 0) *hp = &rs->host;
1033 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +09001034}
Mike Yu69615f62018-11-06 15:42:36 +08001035
1036int herrnoToAiError(int herror) {
1037 switch (herror) {
1038 case HOST_NOT_FOUND:
1039 return EAI_NODATA;
1040 case TRY_AGAIN:
1041 return EAI_AGAIN;
1042 default:
1043 return EAI_FAIL;
1044 }
1045}
1046
1047int rcodeToAiError(int rcode) {
1048 // Catch the two cases (success, timeout). For other cases, just set it EAI_NODATA
1049 // as EAI_NODATA is returned in dns_getaddrinfo() when res_searchN() returns -1.
1050 switch (rcode) {
1051 case NOERROR:
1052 return 0;
1053 case RCODE_TIMEOUT:
1054 return NETD_RESOLV_TIMEOUT;
1055 default:
1056 return EAI_NODATA;
1057 }
1058}