blob: 53abe12530cee7b53b1abfe949f152e6cf8e4582 [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
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900138static bool _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);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
148 const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
149 const struct android_net_context*);
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +0900150static struct hostent* android_gethostbyaddrfornetcontext_proxy(
151 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext);
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,
473 char* buf, size_t buflen, int* he) {
474 getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900476
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900477 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900478
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 switch (af) {
480 case AF_INET:
481 size = NS_INADDRSZ;
482 break;
483 case AF_INET6:
484 size = NS_IN6ADDRSZ;
485 break;
486 default:
487 *he = NETDB_INTERNAL;
488 errno = EAFNOSUPPORT;
Mike Yucac05e42018-11-06 19:20:07 +0800489 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 }
491 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 hp->h_addrtype = af;
494 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900495
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900496 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900497 * disallow names consisting only of digits/dots, unless
498 * they end in a dot.
499 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900500 if (isdigit((u_char) name[0])) {
501 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900502 if (!*cp) {
503 if (*--cp == '.') break;
504 /*
505 * All-numeric, no dot at the end.
506 * Fake up a hostent as if we'd actually
507 * done a lookup.
508 */
509 goto fake;
510 }
511 if (!isdigit((u_char) *cp) && *cp != '.') break;
512 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900513 }
514 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
515 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900516 if (!*cp) {
517 if (*--cp == '.') break;
518 /*
519 * All-IPv6-legal, no dot at the end.
520 * Fake up a hostent as if we'd actually
521 * done a lookup.
522 */
523 goto fake;
524 }
525 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
526 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900527 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900528
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900529 *he = NETDB_INTERNAL;
530 info.hp = hp;
531 info.buf = buf;
532 info.buflen = buflen;
533 info.he = he;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900534 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 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900540 *he = NETDB_SUCCESS;
Mike Yucac05e42018-11-06 19:20:07 +0800541 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900542nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900543 *he = NETDB_INTERNAL;
544 errno = ENOSPC;
Mike Yucac05e42018-11-06 19:20:07 +0800545 // Bad arguments
546 return EAI_FAIL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900547fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900548 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
549 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900550
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900551 hp->h_aliases[0] = NULL;
552 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900553
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900554 if (inet_pton(af, name, buf) <= 0) {
555 *he = HOST_NOT_FOUND;
Mike Yucac05e42018-11-06 19:20:07 +0800556 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900557 }
558 hp->h_addr_list[0] = buf;
559 hp->h_addr_list[1] = NULL;
560 buf += size;
561 buflen -= size;
562 HENT_SCOPY(hp->h_name, name, buf, buflen);
563 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
564 *he = NETDB_SUCCESS;
Mike Yucac05e42018-11-06 19:20:07 +0800565 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900566}
567
568// very similar in proxy-ness to android_getaddrinfo_proxy
Mike Yucac05e42018-11-06 19:20:07 +0800569static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
570 size_t hbuflen, int* errorp,
571 const android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900572 res_setnetcontext(res, netcontext);
573 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900574}
575
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900576static struct hostent* android_gethostbyaddrfornetcontext_real(
577 const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
578 int* he, const struct android_net_context* netcontext) {
579 const u_char* uaddr = (const u_char*) addr;
580 socklen_t size;
581 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900582
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900583 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900584
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900585 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
586 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
587 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
588 *he = HOST_NOT_FOUND;
589 return NULL;
590 }
591 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
592 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
593 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
594 /* Unmap. */
595 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
596 addr = uaddr;
597 af = AF_INET;
598 len = NS_INADDRSZ;
599 }
600 switch (af) {
601 case AF_INET:
602 size = NS_INADDRSZ;
603 break;
604 case AF_INET6:
605 size = NS_IN6ADDRSZ;
606 break;
607 default:
608 errno = EAFNOSUPPORT;
609 *he = NETDB_INTERNAL;
610 return NULL;
611 }
612 if (size != len) {
613 errno = EINVAL;
614 *he = NETDB_INTERNAL;
615 return NULL;
616 }
617 info.hp = hp;
618 info.buf = buf;
619 info.buflen = buflen;
620 info.he = he;
621 *he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900622 if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
623 if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) {
624 return NULL;
625 }
626 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900627 *he = NETDB_SUCCESS;
628 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900629}
630
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900631static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
632 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
633 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900634 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
635 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900636}
637
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900638struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
639 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900640 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900641 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900642 char *cp, **q;
643 int af, len;
644 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900645 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900646
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900647 if (hf == NULL) {
648 *he = NETDB_INTERNAL;
649 errno = EINVAL;
650 return NULL;
651 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900652 char* p = NULL;
653 size_t maxaliases = 10;
654 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
655 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900656
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900657 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900658 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900659 * as the buffer may be used to store content for a previous hostent
660 * returned by non-reentrant functions like gethostbyname().
661 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900662 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 goto nospc;
664 }
665 for (;;) {
666 if (!fgets(p, line_buf_size, hf)) {
667 free(p);
668 free(aliases);
669 *he = HOST_NOT_FOUND;
670 return NULL;
671 }
672 if (*p == '#') {
673 continue;
674 }
675 if (!(cp = strpbrk(p, "#\n"))) {
676 continue;
677 }
678 *cp = '\0';
679 if (!(cp = strpbrk(p, " \t"))) continue;
680 *cp++ = '\0';
681 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
682 af = AF_INET6;
683 len = NS_IN6ADDRSZ;
684 } else {
685 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900686
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900687 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900688 if (res == NULL) goto nospc;
689 if (res->options & RES_USE_INET6) {
690 map_v4v6_address(buf, buf);
691 af = AF_INET6;
692 len = NS_IN6ADDRSZ;
693 } else {
694 af = AF_INET;
695 len = NS_INADDRSZ;
696 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900697 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900698
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900699 /* if this is not something we're looking for, skip it. */
700 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
701 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900702
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703 while (*cp == ' ' || *cp == '\t') cp++;
704 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
705 q = aliases;
706 while (cp && *cp) {
707 if (*cp == ' ' || *cp == '\t') {
708 cp++;
709 continue;
710 }
711 addalias(q, cp, aliases, maxaliases);
712 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
713 }
714 break;
715 }
716 hent->h_length = len;
717 hent->h_addrtype = af;
718 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
719 anum = (size_t)(q - aliases);
720 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
721 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
722 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900723
nuccachen8161c992018-09-11 11:20:00 +0800724 /* Reserve space for mapping IPv4 address to IPv6 address in place */
725 if (hent->h_addrtype == AF_INET) {
726 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
727 }
728
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900729 HENT_SCOPY(hent->h_name, name, buf, buflen);
730 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
731 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900733 *he = NETDB_SUCCESS;
734 free(p);
735 free(aliases);
736 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900737nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738 free(p);
739 free(aliases);
740 errno = ENOSPC;
741 *he = NETDB_INTERNAL;
742 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900743}
744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900745static void map_v4v6_address(const char* src, char* dst) {
746 u_char* p = (u_char*) dst;
747 char tmp[NS_INADDRSZ];
748 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900749
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 _DIAGASSERT(src != NULL);
751 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900754 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900755 /* Mark this ipv6 addr as a mapped ipv4. */
756 for (i = 0; i < 10; i++) *p++ = 0x00;
757 *p++ = 0xff;
758 *p++ = 0xff;
759 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900760 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900761}
762
nuccachen0a511732018-09-18 13:38:48 +0800763static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
764 std::function<void(struct hostent* hp)> map_param,
765 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900766 _DIAGASSERT(hp != NULL);
767 _DIAGASSERT(bpp != NULL);
768 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900769
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900770 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800771 map_param(hp);
772 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900774
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900775 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
776 /* Out of memory. Truncate address list here. XXX */
777 *ap = NULL;
778 return;
779 }
780 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800781 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900782 *ap = *bpp;
783 *bpp += NS_IN6ADDRSZ;
784 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900785}
786
nuccachen0a511732018-09-18 13:38:48 +0800787static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
788 convert_v4v6_hostent(hp, bpp, ep,
789 [](struct hostent* hp) {
790 hp->h_addrtype = AF_INET6;
791 hp->h_length = NS_IN6ADDRSZ;
792 },
793 [](char* src, char* dst) { map_v4v6_address(src, dst); });
794}
795
796/* Reserve space for mapping IPv4 address to IPv6 address in place */
797static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
798 convert_v4v6_hostent(hp, bpp, ep,
799 [](struct hostent* hp) {
800 (void) hp; /* unused */
801 },
802 [](char* src, char* dst) {
803 memcpy(dst, src, NS_INADDRSZ);
804 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
805 });
806}
807
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900808static void addrsort(char** ap, int num, res_state res) {
809 int i, j;
810 char** p;
811 short aval[MAXADDRS];
812 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900813
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900814 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900815
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900816 p = ap;
817 for (i = 0; i < num; i++, p++) {
818 for (j = 0; (unsigned) j < res->nsort; j++)
819 if (res->sort_list[j].addr.s_addr ==
820 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
821 break;
822 aval[i] = j;
823 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
824 }
825 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900826
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900827 while (needsort < num) {
828 for (j = needsort - 1; j >= 0; j--) {
829 if (aval[j] > aval[j + 1]) {
830 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900831
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900832 i = aval[j];
833 aval[j] = aval[j + 1];
834 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900835
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 hp = ap[j];
837 ap[j] = ap[j + 1];
838 ap[j + 1] = hp;
839 } else
840 break;
841 }
842 needsort++;
843 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900844}
845
Mike Yucac05e42018-11-06 19:20:07 +0800846static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 int n, type;
848 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900850
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900851 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900852
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900853 switch (info->hp->h_addrtype) {
854 case AF_INET:
855 info->hp->h_length = NS_INADDRSZ;
856 type = T_A;
857 break;
858 case AF_INET6:
859 info->hp->h_length = NS_IN6ADDRSZ;
860 type = T_AAAA;
861 break;
862 default:
Mike Yucac05e42018-11-06 19:20:07 +0800863 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900864 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900865 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900866 if (buf == NULL) {
867 *info->he = NETDB_INTERNAL;
Mike Yucac05e42018-11-06 19:20:07 +0800868 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900869 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900870 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900871 if (res == NULL) {
872 free(buf);
Mike Yucac05e42018-11-06 19:20:07 +0800873 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900874 }
Mike Yu69615f62018-11-06 15:42:36 +0800875
876 int ai_error = EAI_NODATA;
877 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf), &ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900878 if (n < 0) {
879 free(buf);
880 debugprintf("res_nsearch failed (%d)\n", res, n);
Mike Yu69615f62018-11-06 15:42:36 +0800881
882 // If server responds empty answer with rcode NOERROR, adjust the error so netd will
883 // get the nulltpr hp.
884 // TODO: Adjust the error closed to res_nsend instead of here after h_errno is removed.
885 if (ai_error == 0) {
886 return herrnoToAiError(h_errno);
887 }
888 return ai_error;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900889 }
890 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
891 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900892 if (hp == NULL) {
Mike Yu69615f62018-11-06 15:42:36 +0800893 return herrnoToAiError(h_errno);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900894 }
Mike Yucac05e42018-11-06 19:20:07 +0800895 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900896}
897
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900898static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
899 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900900 char qbuf[MAXDNAME + 1], *qp, *ep;
901 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900902 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903 int advance;
904 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900905
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900906 info->hp->h_length = len;
907 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900908
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900909 switch (info->hp->h_addrtype) {
910 case AF_INET:
911 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
912 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
913 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900914
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900915 case AF_INET6:
916 qp = qbuf;
917 ep = qbuf + sizeof(qbuf) - 1;
918 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
919 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
920 ((unsigned int) uaddr[n] >> 4) & 0xf);
921 if (advance > 0 && qp + advance < ep)
922 qp += advance;
923 else {
924 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900925 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900926 }
927 }
928 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
929 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900930 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900931 }
932 break;
933 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900934 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900935 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900936
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900937 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900938 if (buf == NULL) {
939 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900940 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900941 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900942 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900943 if (res == NULL) {
944 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900945 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900946 }
947 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +0800948 int ai_error = 0;
949 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf), &ai_error);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900950 if (n < 0) {
951 free(buf);
952 debugprintf("res_nquery failed (%d)\n", res, n);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900953 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900954 }
955 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
956 free(buf);
957 if (hp == NULL) {
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900958 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900959 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900960
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900961 char* bf = (char*) (hp->h_addr_list + 2);
962 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900963 if (blen + info->hp->h_length > info->buflen) goto nospc;
964 hp->h_addr_list[0] = bf;
965 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900966 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
968 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
969 map_v4v6_address(bf, bf);
970 hp->h_addrtype = AF_INET6;
971 hp->h_length = NS_IN6ADDRSZ;
972 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900973
nuccachen8161c992018-09-11 11:20:00 +0800974 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
975 if (info->hp->h_addrtype == AF_INET) {
976 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
977 // Pad zero to the unused address space
978 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
979 }
980
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900981 *info->he = NETDB_SUCCESS;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900982 return true;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900983
Bernie Innocenti55864192018-08-30 04:05:20 +0900984nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985 errno = ENOSPC;
986 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900987 return false;
Bernie Innocenti55864192018-08-30 04:05:20 +0900988}
989
Bernie Innocenti55864192018-08-30 04:05:20 +0900990/*
991 * Non-reentrant versions.
992 */
993
Mike Yucac05e42018-11-06 19:20:07 +0800994int android_gethostbynamefornetcontext(const char* name, int af,
995 const struct android_net_context* netcontext, hostent** hp) {
996 int error;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900997 res_state res = res_get_state();
Mike Yucac05e42018-11-06 19:20:07 +0800998 if (res == NULL) return EAI_MEMORY;
999 res_static* rs = res_get_static(); // For thread-safety.
1000 error = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
1001 &h_errno, netcontext);
1002 if (error == 0) {
1003 *hp = &rs->host;
1004 }
1005 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +09001006}
1007
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001008struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1009 const struct android_net_context* netcontext) {
1010 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001011}
1012
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +09001013static struct hostent* android_gethostbyaddrfornetcontext_proxy(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001014 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001015 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001016 return android_gethostbyaddrfornetcontext_proxy_internal(
1017 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001018}
Mike Yu69615f62018-11-06 15:42:36 +08001019
1020int herrnoToAiError(int herror) {
1021 switch (herror) {
1022 case HOST_NOT_FOUND:
1023 return EAI_NODATA;
1024 case TRY_AGAIN:
1025 return EAI_AGAIN;
1026 default:
1027 return EAI_FAIL;
1028 }
1029}
1030
1031int rcodeToAiError(int rcode) {
1032 // Catch the two cases (success, timeout). For other cases, just set it EAI_NODATA
1033 // as EAI_NODATA is returned in dns_getaddrinfo() when res_searchN() returns -1.
1034 switch (rcode) {
1035 case NOERROR:
1036 return 0;
1037 case RCODE_TIMEOUT:
1038 return NETD_RESOLV_TIMEOUT;
1039 default:
1040 return EAI_NODATA;
1041 }
1042}