blob: 834a9625b2eee6c8c4a7d5434e66380b4ab669ea [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);
140static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900141
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
143 size_t, int*, const struct android_net_context*);
144static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
145 const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
146 const struct android_net_context*);
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +0900147static struct hostent* android_gethostbyaddrfornetcontext_proxy(
148 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900149
Bernie Innocenti55864192018-08-30 04:05:20 +0900150#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900151static void debugprintf(const char* msg, res_state res, ...) {
152 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900153
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900154 if (res->options & RES_DEBUG) {
155 int save = errno;
156 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900157
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900158 va_start(ap, res);
159 vprintf(msg, ap);
160 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900161
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162 errno = save;
163 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900164}
165#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900167#endif
168
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900169#define BOUNDED_INCR(x) \
170 do { \
171 BOUNDS_CHECK(cp, x); \
172 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900173 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900174
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900175#define BOUNDS_CHECK(ptr, count) \
176 do { \
177 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900178 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900179
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900180static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
181 res_state res, struct hostent* hent, char* buf, size_t buflen,
182 int* he) {
183 const HEADER* hp;
184 const u_char* cp;
185 int n;
186 size_t qlen;
187 const u_char *eom, *erdata;
188 char *bp, **ap, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900189 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190 int haveanswer, had_error;
191 int toobig = 0;
192 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900193 char* addr_ptrs[MAXADDRS];
194 const char* tname;
195 int (*name_ok)(const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900196
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197 _DIAGASSERT(answer != NULL);
198 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900199
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900200 tname = qname;
201 hent->h_name = NULL;
202 eom = answer->buf + anslen;
203 switch (qtype) {
204 case T_A:
205 case T_AAAA:
206 name_ok = res_hnok;
207 break;
208 case T_PTR:
209 name_ok = res_dnok;
210 break;
211 default:
212 *he = NO_RECOVERY;
213 return NULL; /* XXX should be abort(); */
214 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900215
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900216 size_t maxaliases = 10;
217 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
218 if (!aliases) goto nospc;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900219 /*
220 * find first satisfactory answer
221 */
222 hp = &answer->hdr;
223 ancount = ntohs(hp->ancount);
224 qdcount = ntohs(hp->qdcount);
225 bp = buf;
226 ep = buf + buflen;
227 cp = answer->buf;
228 BOUNDED_INCR(HFIXEDSZ);
229 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900230
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900231 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
232 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234 BOUNDED_INCR(n + QFIXEDSZ);
235 if (qtype == T_A || qtype == T_AAAA) {
236 /* res_send() has already verified that the query name is the
237 * same as the one we sent; this just gets the expanded name
238 * (i.e., with the succeeding search-domain tacked on).
239 */
240 n = (int) strlen(bp) + 1; /* for the \0 */
241 if (n >= MAXHOSTNAMELEN) goto no_recovery;
242 hent->h_name = bp;
243 bp += n;
244 /* The qname can be abbreviated, but h_name is now absolute. */
245 qname = hent->h_name;
246 }
247 hent->h_aliases = ap = aliases;
248 hent->h_addr_list = hap = addr_ptrs;
249 *ap = NULL;
250 *hap = NULL;
251 haveanswer = 0;
252 had_error = 0;
253 while (ancount-- > 0 && cp < eom && !had_error) {
254 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
255 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
256 had_error++;
257 continue;
258 }
259 cp += n; /* name */
260 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900261 int type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900262 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900263 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900264 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900265 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266 cp += INT16SZ; /* len */
267 BOUNDS_CHECK(cp, n);
268 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900269 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270 /* XXX - debug? syslog? */
271 cp += n;
272 continue; /* XXX - had_error++ ? */
273 }
274 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
275 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
276 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
277 had_error++;
278 continue;
279 }
280 cp += n;
281 if (cp != erdata) goto no_recovery;
282 /* Store alias. */
283 addalias(ap, bp, aliases, maxaliases);
284 n = (int) strlen(bp) + 1; /* for the \0 */
285 if (n >= MAXHOSTNAMELEN) {
286 had_error++;
287 continue;
288 }
289 bp += n;
290 /* Get canonical name. */
291 n = (int) strlen(tbuf) + 1; /* for the \0 */
292 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
293 had_error++;
294 continue;
295 }
296 strlcpy(bp, tbuf, (size_t)(ep - bp));
297 hent->h_name = bp;
298 bp += n;
299 continue;
300 }
301 if (qtype == T_PTR && type == T_CNAME) {
302 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
303 if (n < 0 || !maybe_dnok(res, tbuf)) {
304 had_error++;
305 continue;
306 }
307 cp += n;
308 if (cp != erdata) goto no_recovery;
309 /* Get canonical name. */
310 n = (int) strlen(tbuf) + 1; /* for the \0 */
311 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
312 had_error++;
313 continue;
314 }
315 strlcpy(bp, tbuf, (size_t)(ep - bp));
316 tname = bp;
317 bp += n;
318 continue;
319 }
320 if (type != qtype) {
321 if (type != T_KEY && type != T_SIG)
322 syslog(LOG_NOTICE | LOG_AUTH,
323 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
324 p_class(C_IN), p_type(qtype), p_type(type));
325 cp += n;
326 continue; /* XXX - had_error++ ? */
327 }
328 switch (type) {
329 case T_PTR:
330 if (strcasecmp(tname, bp) != 0) {
331 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
332 cp += n;
333 continue; /* XXX - had_error++ ? */
334 }
335 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
336 if ((n < 0) || !maybe_hnok(res, bp)) {
337 had_error++;
338 break;
339 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900340#if MULTI_PTRS_ARE_ALIASES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900341 cp += n;
342 if (cp != erdata) goto no_recovery;
343 if (!haveanswer)
344 hent->h_name = bp;
345 else
346 addalias(ap, bp, aliases, maxaliases);
347 if (n != -1) {
348 n = (int) strlen(bp) + 1; /* for the \0 */
349 if (n >= MAXHOSTNAMELEN) {
350 had_error++;
351 break;
352 }
353 bp += n;
354 }
355 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900356#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900357 hent->h_name = bp;
358 if (res->options & RES_USE_INET6) {
359 n = strlen(bp) + 1; /* for the \0 */
360 if (n >= MAXHOSTNAMELEN) {
361 had_error++;
362 break;
363 }
364 bp += n;
365 map_v4v6_hostent(hent, &bp, ep);
366 }
367 goto success;
Bernie Innocenti55864192018-08-30 04:05:20 +0900368#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900369 case T_A:
370 case T_AAAA:
371 if (strcasecmp(hent->h_name, bp) != 0) {
372 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
373 cp += n;
374 continue; /* XXX - had_error++ ? */
375 }
376 if (n != hent->h_length) {
377 cp += n;
378 continue;
379 }
380 if (type == T_AAAA) {
381 struct in6_addr in6;
382 memcpy(&in6, cp, NS_IN6ADDRSZ);
383 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
384 cp += n;
385 continue;
386 }
387 }
388 if (!haveanswer) {
389 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900390
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900391 hent->h_name = bp;
392 nn = (int) strlen(bp) + 1; /* for the \0 */
393 bp += nn;
394 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900395
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900396 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900397
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900398 if (bp + n >= ep) {
399 debugprintf("size (%d) too big\n", res, n);
400 had_error++;
401 continue;
402 }
403 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
404 if (!toobig++) {
405 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
406 }
407 cp += n;
408 continue;
409 }
410 (void) memcpy(*hap++ = bp, cp, (size_t) n);
411 bp += n;
412 cp += n;
413 if (cp != erdata) goto no_recovery;
414 break;
415 default:
416 abort();
417 }
418 if (!had_error) haveanswer++;
419 }
420 if (haveanswer) {
421 *ap = NULL;
422 *hap = NULL;
423 /*
424 * Note: we sort even if host can take only one address
425 * in its return structures - should give it the "best"
426 * address in that case, not some random one
427 */
428 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
429 if (!hent->h_name) {
430 n = (int) strlen(qname) + 1; /* for the \0 */
431 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
432 strlcpy(bp, qname, (size_t)(ep - bp));
433 hent->h_name = bp;
434 bp += n;
435 }
436 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
nuccachen0a511732018-09-18 13:38:48 +0800437 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900438 goto success;
439 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900440no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 free(aliases);
442 *he = NO_RECOVERY;
443 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900444success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900445 bp = (char*) ALIGN(bp);
446 n = (int) (ap - aliases);
447 qlen = (n + 1) * sizeof(*hent->h_aliases);
448 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900449 hent->h_aliases = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 memcpy(bp, aliases, qlen);
451 free(aliases);
452 aliases = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900453
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 bp += qlen;
455 n = (int) (hap - addr_ptrs);
456 qlen = (n + 1) * sizeof(*hent->h_addr_list);
457 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900458 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900459 memcpy(bp, addr_ptrs, qlen);
460 *he = NETDB_SUCCESS;
461 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900462nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900463 free(aliases);
464 errno = ENOSPC;
465 *he = NETDB_INTERNAL;
466 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900467}
468
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900469static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
470 struct hostent* hp, char* buf, size_t buflen,
471 int* he) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900472 struct getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900473 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900474
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900475 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900476
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900477 switch (af) {
478 case AF_INET:
479 size = NS_INADDRSZ;
480 break;
481 case AF_INET6:
482 size = NS_IN6ADDRSZ;
483 break;
484 default:
485 *he = NETDB_INTERNAL;
486 errno = EAFNOSUPPORT;
487 return NULL;
488 }
489 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900490
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900491 hp->h_addrtype = af;
492 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900493
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900494 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900495 * disallow names consisting only of digits/dots, unless
496 * they end in a dot.
497 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900498 if (isdigit((u_char) name[0])) {
499 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900500 if (!*cp) {
501 if (*--cp == '.') break;
502 /*
503 * All-numeric, no dot at the end.
504 * Fake up a hostent as if we'd actually
505 * done a lookup.
506 */
507 goto fake;
508 }
509 if (!isdigit((u_char) *cp) && *cp != '.') break;
510 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900511 }
512 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
513 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900514 if (!*cp) {
515 if (*--cp == '.') break;
516 /*
517 * All-IPv6-legal, no dot at the end.
518 * Fake up a hostent as if we'd actually
519 * done a lookup.
520 */
521 goto fake;
522 }
523 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
524 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900525 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900526
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900527 *he = NETDB_INTERNAL;
528 info.hp = hp;
529 info.buf = buf;
530 info.buflen = buflen;
531 info.he = he;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900532 if (!_hf_gethtbyname2(name, af, &info)) {
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900533 if (!_dns_gethtbyname(name, af, &info)) {
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900534 return NULL;
535 }
536 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900537 *he = NETDB_SUCCESS;
538 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900539nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900540 *he = NETDB_INTERNAL;
541 errno = ENOSPC;
542 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900543fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
545 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900546
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900547 hp->h_aliases[0] = NULL;
548 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900549
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900550 if (inet_pton(af, name, buf) <= 0) {
551 *he = HOST_NOT_FOUND;
552 return NULL;
553 }
554 hp->h_addr_list[0] = buf;
555 hp->h_addr_list[1] = NULL;
556 buf += size;
557 buflen -= size;
558 HENT_SCOPY(hp->h_name, name, buf, buflen);
559 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
560 *he = NETDB_SUCCESS;
561 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900562}
563
564// very similar in proxy-ness to android_getaddrinfo_proxy
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565static struct hostent* gethostbyname_internal(const char* name, int af, res_state res,
566 struct hostent* hp, char* hbuf, size_t hbuflen,
567 int* errorp,
568 const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900569 res_setnetcontext(res, netcontext);
570 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900571}
572
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900573static struct hostent* android_gethostbyaddrfornetcontext_real(
574 const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
575 int* he, const struct android_net_context* netcontext) {
576 const u_char* uaddr = (const u_char*) addr;
577 socklen_t size;
578 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900579
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900580 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900581
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900582 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
583 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
584 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
585 *he = HOST_NOT_FOUND;
586 return NULL;
587 }
588 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
589 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
590 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
591 /* Unmap. */
592 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
593 addr = uaddr;
594 af = AF_INET;
595 len = NS_INADDRSZ;
596 }
597 switch (af) {
598 case AF_INET:
599 size = NS_INADDRSZ;
600 break;
601 case AF_INET6:
602 size = NS_IN6ADDRSZ;
603 break;
604 default:
605 errno = EAFNOSUPPORT;
606 *he = NETDB_INTERNAL;
607 return NULL;
608 }
609 if (size != len) {
610 errno = EINVAL;
611 *he = NETDB_INTERNAL;
612 return NULL;
613 }
614 info.hp = hp;
615 info.buf = buf;
616 info.buflen = buflen;
617 info.he = he;
618 *he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900619 if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
620 if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) {
621 return NULL;
622 }
623 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900624 *he = NETDB_SUCCESS;
625 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900626}
627
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900628static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
629 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
630 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900631 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
632 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900633}
634
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900635struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
636 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900637 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900638 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 char *cp, **q;
640 int af, len;
641 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900642 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900643
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900644 if (hf == NULL) {
645 *he = NETDB_INTERNAL;
646 errno = EINVAL;
647 return NULL;
648 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900649 char* p = NULL;
650 size_t maxaliases = 10;
651 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
652 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900653
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900654 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900655 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900656 * as the buffer may be used to store content for a previous hostent
657 * returned by non-reentrant functions like gethostbyname().
658 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900659 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900660 goto nospc;
661 }
662 for (;;) {
663 if (!fgets(p, line_buf_size, hf)) {
664 free(p);
665 free(aliases);
666 *he = HOST_NOT_FOUND;
667 return NULL;
668 }
669 if (*p == '#') {
670 continue;
671 }
672 if (!(cp = strpbrk(p, "#\n"))) {
673 continue;
674 }
675 *cp = '\0';
676 if (!(cp = strpbrk(p, " \t"))) continue;
677 *cp++ = '\0';
678 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
679 af = AF_INET6;
680 len = NS_IN6ADDRSZ;
681 } else {
682 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900683
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900684 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900685 if (res == NULL) goto nospc;
686 if (res->options & RES_USE_INET6) {
687 map_v4v6_address(buf, buf);
688 af = AF_INET6;
689 len = NS_IN6ADDRSZ;
690 } else {
691 af = AF_INET;
692 len = NS_INADDRSZ;
693 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900694 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900697 /* if this is not something we're looking for, skip it. */
698 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
699 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900701 while (*cp == ' ' || *cp == '\t') cp++;
702 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
703 q = aliases;
704 while (cp && *cp) {
705 if (*cp == ' ' || *cp == '\t') {
706 cp++;
707 continue;
708 }
709 addalias(q, cp, aliases, maxaliases);
710 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
711 }
712 break;
713 }
714 hent->h_length = len;
715 hent->h_addrtype = af;
716 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
717 anum = (size_t)(q - aliases);
718 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
719 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
720 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900721
nuccachen8161c992018-09-11 11:20:00 +0800722 /* Reserve space for mapping IPv4 address to IPv6 address in place */
723 if (hent->h_addrtype == AF_INET) {
724 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
725 }
726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900727 HENT_SCOPY(hent->h_name, name, buf, buflen);
728 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
729 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900731 *he = NETDB_SUCCESS;
732 free(p);
733 free(aliases);
734 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900735nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900736 free(p);
737 free(aliases);
738 errno = ENOSPC;
739 *he = NETDB_INTERNAL;
740 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900741}
742
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900743static void map_v4v6_address(const char* src, char* dst) {
744 u_char* p = (u_char*) dst;
745 char tmp[NS_INADDRSZ];
746 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900747
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900748 _DIAGASSERT(src != NULL);
749 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900750
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900751 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900752 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753 /* Mark this ipv6 addr as a mapped ipv4. */
754 for (i = 0; i < 10; i++) *p++ = 0x00;
755 *p++ = 0xff;
756 *p++ = 0xff;
757 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900758 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900759}
760
nuccachen0a511732018-09-18 13:38:48 +0800761static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
762 std::function<void(struct hostent* hp)> map_param,
763 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900764 _DIAGASSERT(hp != NULL);
765 _DIAGASSERT(bpp != NULL);
766 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800769 map_param(hp);
770 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900772
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
774 /* Out of memory. Truncate address list here. XXX */
775 *ap = NULL;
776 return;
777 }
778 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800779 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900780 *ap = *bpp;
781 *bpp += NS_IN6ADDRSZ;
782 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900783}
784
nuccachen0a511732018-09-18 13:38:48 +0800785static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
786 convert_v4v6_hostent(hp, bpp, ep,
787 [](struct hostent* hp) {
788 hp->h_addrtype = AF_INET6;
789 hp->h_length = NS_IN6ADDRSZ;
790 },
791 [](char* src, char* dst) { map_v4v6_address(src, dst); });
792}
793
794/* Reserve space for mapping IPv4 address to IPv6 address in place */
795static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
796 convert_v4v6_hostent(hp, bpp, ep,
797 [](struct hostent* hp) {
798 (void) hp; /* unused */
799 },
800 [](char* src, char* dst) {
801 memcpy(dst, src, NS_INADDRSZ);
802 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
803 });
804}
805
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900806static void addrsort(char** ap, int num, res_state res) {
807 int i, j;
808 char** p;
809 short aval[MAXADDRS];
810 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900813
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900814 p = ap;
815 for (i = 0; i < num; i++, p++) {
816 for (j = 0; (unsigned) j < res->nsort; j++)
817 if (res->sort_list[j].addr.s_addr ==
818 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
819 break;
820 aval[i] = j;
821 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
822 }
823 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825 while (needsort < num) {
826 for (j = needsort - 1; j >= 0; j--) {
827 if (aval[j] > aval[j + 1]) {
828 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900829
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900830 i = aval[j];
831 aval[j] = aval[j + 1];
832 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900833
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900834 hp = ap[j];
835 ap[j] = ap[j + 1];
836 ap[j + 1] = hp;
837 } else
838 break;
839 }
840 needsort++;
841 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900842}
843
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900844static bool _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900845 int n, type;
846 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900848
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900849 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900850
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900851 switch (info->hp->h_addrtype) {
852 case AF_INET:
853 info->hp->h_length = NS_INADDRSZ;
854 type = T_A;
855 break;
856 case AF_INET6:
857 info->hp->h_length = NS_IN6ADDRSZ;
858 type = T_AAAA;
859 break;
860 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900861 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900862 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900863 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900864 if (buf == NULL) {
865 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900866 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900867 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900868 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900869 if (res == NULL) {
870 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900871 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900872 }
873 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf));
874 if (n < 0) {
875 free(buf);
876 debugprintf("res_nsearch failed (%d)\n", res, n);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900877 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900878 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879 }
880 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
881 free(buf);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900882 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900883 if (hp == NULL) {
884 return false;
885 }
886 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900887}
888
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900889static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
890 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900891 char qbuf[MAXDNAME + 1], *qp, *ep;
892 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900893 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900894 int advance;
895 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900896
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900897 info->hp->h_length = len;
898 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900899
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900900 switch (info->hp->h_addrtype) {
901 case AF_INET:
902 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
903 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
904 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900905
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900906 case AF_INET6:
907 qp = qbuf;
908 ep = qbuf + sizeof(qbuf) - 1;
909 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
910 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
911 ((unsigned int) uaddr[n] >> 4) & 0xf);
912 if (advance > 0 && qp + advance < ep)
913 qp += advance;
914 else {
915 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900916 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900917 }
918 }
919 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
920 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900921 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900922 }
923 break;
924 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900925 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900926 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900927
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900928 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900929 if (buf == NULL) {
930 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900931 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900932 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900933 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900934 if (res == NULL) {
935 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900936 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900937 }
938 res_setnetcontext(res, netcontext);
939 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf));
940 if (n < 0) {
941 free(buf);
942 debugprintf("res_nquery failed (%d)\n", res, n);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900943 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900944 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900945 }
946 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
947 free(buf);
948 if (hp == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900949 res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900950 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900951 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900952
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900953 char* bf = (char*) (hp->h_addr_list + 2);
954 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900955 if (blen + info->hp->h_length > info->buflen) goto nospc;
956 hp->h_addr_list[0] = bf;
957 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900958 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900959 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
960 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
961 map_v4v6_address(bf, bf);
962 hp->h_addrtype = AF_INET6;
963 hp->h_length = NS_IN6ADDRSZ;
964 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900965
nuccachen8161c992018-09-11 11:20:00 +0800966 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
967 if (info->hp->h_addrtype == AF_INET) {
968 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
969 // Pad zero to the unused address space
970 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
971 }
972
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900973 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900974 *info->he = NETDB_SUCCESS;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900975 return true;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900976
Bernie Innocenti55864192018-08-30 04:05:20 +0900977nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900978 errno = ENOSPC;
979 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900980 return false;
Bernie Innocenti55864192018-08-30 04:05:20 +0900981}
982
Bernie Innocenti55864192018-08-30 04:05:20 +0900983/*
984 * Non-reentrant versions.
985 */
986
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900987struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
988 const struct android_net_context* netcontext) {
989 struct hostent* hp;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900990 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900991 if (res == NULL) return NULL;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900992 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900993 hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
994 &h_errno, netcontext);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900995 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900996 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900997}
998
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900999struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1000 const struct android_net_context* netcontext) {
1001 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001002}
1003
Bernie Innocenti0e45e2a2018-09-14 16:42:36 +09001004static struct hostent* android_gethostbyaddrfornetcontext_proxy(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001005 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001006 struct res_static* rs = res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001007 return android_gethostbyaddrfornetcontext_proxy_internal(
1008 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001009}