blob: 34df84d1270d480238122311e023ac6083cc7000 [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>
64#include <strings.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090065#include <sys/param.h>
66#include <sys/socket.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090067#include <sys/types.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090068#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090069#include <syslog.h>
70#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090071
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090072#include "resolv_cache.h"
73#include "resolv_netid.h"
74#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090075
Bernie Innocentif89b3512018-08-30 07:34:37 +090076// NetBSD uses _DIAGASSERT to null-check arguments and the like,
77// but it's clear from the number of mistakes in their assertions
78// that they don't actually test or ship with this.
79#define _DIAGASSERT(e) /* nothing */
80
Bernie Innocenti55864192018-08-30 04:05:20 +090081#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090083
84#ifndef LOG_AUTH
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#define LOG_AUTH 0
Bernie Innocenti55864192018-08-30 04:05:20 +090086#endif
87
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
Bernie Innocenti55864192018-08-30 04:05:20 +090089
Bernie Innocenti55864192018-08-30 04:05:20 +090090#include <stdlib.h>
91#include <string.h>
92
93#include "hostent.h"
94
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 +0900113static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET,
114 .app_netid = NETID_UNSET,
115 .dns_mark = MARK_UNSET,
116 .dns_netid = NETID_UNSET,
117 .uid = NET_CONTEXT_INVALID_UID};
Bernie Innocenti55864192018-08-30 04:05:20 +0900118
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900120
121typedef union {
122 HEADER hdr;
123 u_char buf[MAXPACKET];
124} querybuf;
125
126typedef union {
127 int32_t al;
128 char ac;
129} align;
130
131#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900132static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900133#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900134static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
135 char*, size_t, int*);
136static void map_v4v6_address(const char*, char*);
137static void map_v4v6_hostent(struct hostent*, char**, char*);
138static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900139
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900140struct hostent* ht_gethostbyname(char*);
141struct hostent* ht_gethostbyaddr(const char*, int, int);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900142static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
143 const android_net_context* netcontext, getnamaddr* info);
144static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900145
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900146static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*,
147 size_t, int*, const struct android_net_context*);
148static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
149 const void*, socklen_t, int, struct hostent*, char*, size_t, int*,
150 const struct android_net_context*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900151
Bernie Innocenti55864192018-08-30 04:05:20 +0900152static int h_errno_to_result(int* herrno_p) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
154 if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
155 errno = ERANGE;
156 return errno;
157 }
158 // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
159 return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900160}
161
162#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900163static void debugprintf(const char* msg, res_state res, ...) {
164 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900165
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166 if (res->options & RES_DEBUG) {
167 int save = errno;
168 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900169
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900170 va_start(ap, res);
171 vprintf(msg, ap);
172 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900173
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900174 errno = save;
175 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900176}
177#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900179#endif
180
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900181#define BOUNDED_INCR(x) \
182 do { \
183 BOUNDS_CHECK(cp, x); \
184 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900185 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900186
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900187#define BOUNDS_CHECK(ptr, count) \
188 do { \
189 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900190 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900191
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
193 res_state res, struct hostent* hent, char* buf, size_t buflen,
194 int* he) {
195 const HEADER* hp;
196 const u_char* cp;
197 int n;
198 size_t qlen;
199 const u_char *eom, *erdata;
200 char *bp, **ap, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900201 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900202 int haveanswer, had_error;
203 int toobig = 0;
204 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900205 char* addr_ptrs[MAXADDRS];
206 const char* tname;
207 int (*name_ok)(const char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900208
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900209 _DIAGASSERT(answer != NULL);
210 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900212 tname = qname;
213 hent->h_name = NULL;
214 eom = answer->buf + anslen;
215 switch (qtype) {
216 case T_A:
217 case T_AAAA:
218 name_ok = res_hnok;
219 break;
220 case T_PTR:
221 name_ok = res_dnok;
222 break;
223 default:
224 *he = NO_RECOVERY;
225 return NULL; /* XXX should be abort(); */
226 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900227
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900228 size_t maxaliases = 10;
229 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
230 if (!aliases) goto nospc;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900231 /*
232 * find first satisfactory answer
233 */
234 hp = &answer->hdr;
235 ancount = ntohs(hp->ancount);
236 qdcount = ntohs(hp->qdcount);
237 bp = buf;
238 ep = buf + buflen;
239 cp = answer->buf;
240 BOUNDED_INCR(HFIXEDSZ);
241 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900242
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900243 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
244 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900245
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900246 BOUNDED_INCR(n + QFIXEDSZ);
247 if (qtype == T_A || qtype == T_AAAA) {
248 /* res_send() has already verified that the query name is the
249 * same as the one we sent; this just gets the expanded name
250 * (i.e., with the succeeding search-domain tacked on).
251 */
252 n = (int) strlen(bp) + 1; /* for the \0 */
253 if (n >= MAXHOSTNAMELEN) goto no_recovery;
254 hent->h_name = bp;
255 bp += n;
256 /* The qname can be abbreviated, but h_name is now absolute. */
257 qname = hent->h_name;
258 }
259 hent->h_aliases = ap = aliases;
260 hent->h_addr_list = hap = addr_ptrs;
261 *ap = NULL;
262 *hap = NULL;
263 haveanswer = 0;
264 had_error = 0;
265 while (ancount-- > 0 && cp < eom && !had_error) {
266 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
267 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
268 had_error++;
269 continue;
270 }
271 cp += n; /* name */
272 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900273 int type = _getshort(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900274 cp += INT16SZ; /* type */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900275 int cl = _getshort(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276 cp += INT16SZ + INT32SZ; /* class, TTL */
277 n = _getshort(cp);
278 cp += INT16SZ; /* len */
279 BOUNDS_CHECK(cp, n);
280 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900281 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900282 /* XXX - debug? syslog? */
283 cp += n;
284 continue; /* XXX - had_error++ ? */
285 }
286 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
287 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
288 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
289 had_error++;
290 continue;
291 }
292 cp += n;
293 if (cp != erdata) goto no_recovery;
294 /* Store alias. */
295 addalias(ap, bp, aliases, maxaliases);
296 n = (int) strlen(bp) + 1; /* for the \0 */
297 if (n >= MAXHOSTNAMELEN) {
298 had_error++;
299 continue;
300 }
301 bp += n;
302 /* Get canonical name. */
303 n = (int) strlen(tbuf) + 1; /* for the \0 */
304 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
305 had_error++;
306 continue;
307 }
308 strlcpy(bp, tbuf, (size_t)(ep - bp));
309 hent->h_name = bp;
310 bp += n;
311 continue;
312 }
313 if (qtype == T_PTR && type == T_CNAME) {
314 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
315 if (n < 0 || !maybe_dnok(res, tbuf)) {
316 had_error++;
317 continue;
318 }
319 cp += n;
320 if (cp != erdata) goto no_recovery;
321 /* Get canonical name. */
322 n = (int) strlen(tbuf) + 1; /* for the \0 */
323 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
324 had_error++;
325 continue;
326 }
327 strlcpy(bp, tbuf, (size_t)(ep - bp));
328 tname = bp;
329 bp += n;
330 continue;
331 }
332 if (type != qtype) {
333 if (type != T_KEY && type != T_SIG)
334 syslog(LOG_NOTICE | LOG_AUTH,
335 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
336 p_class(C_IN), p_type(qtype), p_type(type));
337 cp += n;
338 continue; /* XXX - had_error++ ? */
339 }
340 switch (type) {
341 case T_PTR:
342 if (strcasecmp(tname, bp) != 0) {
343 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
344 cp += n;
345 continue; /* XXX - had_error++ ? */
346 }
347 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
348 if ((n < 0) || !maybe_hnok(res, bp)) {
349 had_error++;
350 break;
351 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900352#if MULTI_PTRS_ARE_ALIASES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900353 cp += n;
354 if (cp != erdata) goto no_recovery;
355 if (!haveanswer)
356 hent->h_name = bp;
357 else
358 addalias(ap, bp, aliases, maxaliases);
359 if (n != -1) {
360 n = (int) strlen(bp) + 1; /* for the \0 */
361 if (n >= MAXHOSTNAMELEN) {
362 had_error++;
363 break;
364 }
365 bp += n;
366 }
367 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900368#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900369 hent->h_name = bp;
370 if (res->options & RES_USE_INET6) {
371 n = strlen(bp) + 1; /* for the \0 */
372 if (n >= MAXHOSTNAMELEN) {
373 had_error++;
374 break;
375 }
376 bp += n;
377 map_v4v6_hostent(hent, &bp, ep);
378 }
379 goto success;
Bernie Innocenti55864192018-08-30 04:05:20 +0900380#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900381 case T_A:
382 case T_AAAA:
383 if (strcasecmp(hent->h_name, bp) != 0) {
384 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
385 cp += n;
386 continue; /* XXX - had_error++ ? */
387 }
388 if (n != hent->h_length) {
389 cp += n;
390 continue;
391 }
392 if (type == T_AAAA) {
393 struct in6_addr in6;
394 memcpy(&in6, cp, NS_IN6ADDRSZ);
395 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
396 cp += n;
397 continue;
398 }
399 }
400 if (!haveanswer) {
401 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900402
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900403 hent->h_name = bp;
404 nn = (int) strlen(bp) + 1; /* for the \0 */
405 bp += nn;
406 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900407
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900408 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900409
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900410 if (bp + n >= ep) {
411 debugprintf("size (%d) too big\n", res, n);
412 had_error++;
413 continue;
414 }
415 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
416 if (!toobig++) {
417 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
418 }
419 cp += n;
420 continue;
421 }
422 (void) memcpy(*hap++ = bp, cp, (size_t) n);
423 bp += n;
424 cp += n;
425 if (cp != erdata) goto no_recovery;
426 break;
427 default:
428 abort();
429 }
430 if (!had_error) haveanswer++;
431 }
432 if (haveanswer) {
433 *ap = NULL;
434 *hap = NULL;
435 /*
436 * Note: we sort even if host can take only one address
437 * in its return structures - should give it the "best"
438 * address in that case, not some random one
439 */
440 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
441 if (!hent->h_name) {
442 n = (int) strlen(qname) + 1; /* for the \0 */
443 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
444 strlcpy(bp, qname, (size_t)(ep - bp));
445 hent->h_name = bp;
446 bp += n;
447 }
448 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
449 goto success;
450 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900451no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900452 free(aliases);
453 *he = NO_RECOVERY;
454 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900455success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900456 bp = (char*) ALIGN(bp);
457 n = (int) (ap - aliases);
458 qlen = (n + 1) * sizeof(*hent->h_aliases);
459 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900460 hent->h_aliases = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900461 memcpy(bp, aliases, qlen);
462 free(aliases);
463 aliases = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900464
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900465 bp += qlen;
466 n = (int) (hap - addr_ptrs);
467 qlen = (n + 1) * sizeof(*hent->h_addr_list);
468 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900469 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900470 memcpy(bp, addr_ptrs, qlen);
471 *he = NETDB_SUCCESS;
472 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900473nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900474 free(aliases);
475 errno = ENOSPC;
476 *he = NETDB_INTERNAL;
477 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900478}
479
480/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900481int gethostbyname_r(const char* name, struct hostent* hp, char* buf, size_t buflen,
482 struct hostent** result, int* errorp) {
483 res_state res = __res_get_state();
484 if (res == NULL) {
485 *result = NULL;
486 *errorp = NETDB_INTERNAL;
487 return -1;
488 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900490 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900491
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492 if (res->options & RES_USE_INET6) {
493 *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
494 &NETCONTEXT_UNSET);
495 if (*result) {
496 __res_put_state(res);
497 return 0;
498 }
499 }
500 *result =
501 gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
502 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900503}
504
505/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506int gethostbyname2_r(const char* name, int af, struct hostent* hp, char* buf, size_t buflen,
507 struct hostent** result, int* errorp) {
508 res_state res = __res_get_state();
509 if (res == NULL) {
510 *result = NULL;
511 *errorp = NETDB_INTERNAL;
512 return -1;
513 }
514 *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET);
515 return h_errno_to_result(errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900516}
517
518__LIBC_HIDDEN__ FILE* android_open_proxy() {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900519 const char* cache_mode = getenv("ANDROID_DNS_MODE");
520 bool use_proxy = (cache_mode == NULL || strcmp(cache_mode, "local") != 0);
521 if (!use_proxy) {
522 return NULL;
523 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900524
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900525 int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
526 if (s == -1) {
527 return NULL;
528 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900529
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900530 const int one = 1;
531 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Bernie Innocenti55864192018-08-30 04:05:20 +0900532
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900533 struct sockaddr_un proxy_addr;
534 memset(&proxy_addr, 0, sizeof(proxy_addr));
535 proxy_addr.sun_family = AF_UNIX;
536 strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", sizeof(proxy_addr.sun_path));
Bernie Innocenti55864192018-08-30 04:05:20 +0900537
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900538 if (TEMP_FAILURE_RETRY(connect(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) !=
539 0) {
540 close(s);
541 return NULL;
542 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900543
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 return fdopen(s, "r+");
Bernie Innocenti55864192018-08-30 04:05:20 +0900545}
546
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900547static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res,
548 struct hostent* hp, char* buf, size_t buflen,
549 int* he) {
550 const char* cp;
551 struct getnamaddr info;
552 char hbuf[MAXHOSTNAMELEN];
553 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900555 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900556
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900557 switch (af) {
558 case AF_INET:
559 size = NS_INADDRSZ;
560 break;
561 case AF_INET6:
562 size = NS_IN6ADDRSZ;
563 break;
564 default:
565 *he = NETDB_INTERNAL;
566 errno = EAFNOSUPPORT;
567 return NULL;
568 }
569 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900570
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900571 hp->h_addrtype = af;
572 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900574 /*
575 * if there aren't any dots, it could be a user-level alias.
576 * this is also done in res_nquery() since we are not the only
577 * function that looks up host names.
578 */
579 if (!strchr(name, '.') && (cp = res_hostalias(res, name, hbuf, sizeof(hbuf)))) name = cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900580
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900581 /*
582 * disallow names consisting only of digits/dots, unless
583 * they end in a dot.
584 */
585 if (isdigit((u_char) name[0]))
586 for (cp = name;; ++cp) {
587 if (!*cp) {
588 if (*--cp == '.') break;
589 /*
590 * All-numeric, no dot at the end.
591 * Fake up a hostent as if we'd actually
592 * done a lookup.
593 */
594 goto fake;
595 }
596 if (!isdigit((u_char) *cp) && *cp != '.') break;
597 }
598 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':')
599 for (cp = name;; ++cp) {
600 if (!*cp) {
601 if (*--cp == '.') break;
602 /*
603 * All-IPv6-legal, no dot at the end.
604 * Fake up a hostent as if we'd actually
605 * done a lookup.
606 */
607 goto fake;
608 }
609 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
610 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900611
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900612 *he = NETDB_INTERNAL;
613 info.hp = hp;
614 info.buf = buf;
615 info.buflen = buflen;
616 info.he = he;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900617 if (!_hf_gethtbyname2(name, af, &info)) {
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900618 if (!_dns_gethtbyname(name, af, &info)) {
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900619 return NULL;
620 }
621 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900622 *he = NETDB_SUCCESS;
623 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900624nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 *he = NETDB_INTERNAL;
626 errno = ENOSPC;
627 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900628fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900629 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
630 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900631
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900632 hp->h_aliases[0] = NULL;
633 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900634
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900635 if (inet_pton(af, name, buf) <= 0) {
636 *he = HOST_NOT_FOUND;
637 return NULL;
638 }
639 hp->h_addr_list[0] = buf;
640 hp->h_addr_list[1] = NULL;
641 buf += size;
642 buflen -= size;
643 HENT_SCOPY(hp->h_name, name, buf, buflen);
644 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
645 *he = NETDB_SUCCESS;
646 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900647}
648
649// very similar in proxy-ness to android_getaddrinfo_proxy
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900650static struct hostent* gethostbyname_internal(const char* name, int af, res_state res,
651 struct hostent* hp, char* hbuf, size_t hbuflen,
652 int* errorp,
653 const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900654 res_setnetcontext(res, netcontext);
655 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900656}
657
658/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900659int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf,
660 size_t buflen, struct hostent** result, int* h_errnop) {
661 *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen,
662 h_errnop, &NETCONTEXT_UNSET);
663 return h_errno_to_result(h_errnop);
Bernie Innocenti55864192018-08-30 04:05:20 +0900664}
665
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900666static struct hostent* android_gethostbyaddrfornetcontext_real(
667 const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen,
668 int* he, const struct android_net_context* netcontext) {
669 const u_char* uaddr = (const u_char*) addr;
670 socklen_t size;
671 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900674
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900675 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
676 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
677 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
678 *he = HOST_NOT_FOUND;
679 return NULL;
680 }
681 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
682 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
683 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
684 /* Unmap. */
685 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
686 addr = uaddr;
687 af = AF_INET;
688 len = NS_INADDRSZ;
689 }
690 switch (af) {
691 case AF_INET:
692 size = NS_INADDRSZ;
693 break;
694 case AF_INET6:
695 size = NS_IN6ADDRSZ;
696 break;
697 default:
698 errno = EAFNOSUPPORT;
699 *he = NETDB_INTERNAL;
700 return NULL;
701 }
702 if (size != len) {
703 errno = EINVAL;
704 *he = NETDB_INTERNAL;
705 return NULL;
706 }
707 info.hp = hp;
708 info.buf = buf;
709 info.buflen = buflen;
710 info.he = he;
711 *he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900712 if (!_hf_gethtbyaddr(uaddr, len, af, &info)) {
713 if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) {
714 return NULL;
715 }
716 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900717 *he = NETDB_SUCCESS;
718 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900719}
720
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900721static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(
722 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
723 int* he, const struct android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900724 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he,
725 netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900726}
727
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900728struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
729 int* he) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900730 const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
731 char *name;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900732 char *cp, **q;
733 int af, len;
734 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 struct in6_addr host_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900736
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900737 if (hf == NULL) {
738 *he = NETDB_INTERNAL;
739 errno = EINVAL;
740 return NULL;
741 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900742 char* p = NULL;
743 size_t maxaliases = 10;
744 char** aliases = (char**) malloc(maxaliases * sizeof(char*));
745 if (!aliases) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900746
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900747 /* Allocate a new space to read file lines like upstream does.
748 * To keep reentrancy we cannot use __res_get_static()->hostbuf here,
749 * as the buffer may be used to store content for a previous hostent
750 * returned by non-reentrant functions like gethostbyname().
751 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900752 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753 goto nospc;
754 }
755 for (;;) {
756 if (!fgets(p, line_buf_size, hf)) {
757 free(p);
758 free(aliases);
759 *he = HOST_NOT_FOUND;
760 return NULL;
761 }
762 if (*p == '#') {
763 continue;
764 }
765 if (!(cp = strpbrk(p, "#\n"))) {
766 continue;
767 }
768 *cp = '\0';
769 if (!(cp = strpbrk(p, " \t"))) continue;
770 *cp++ = '\0';
771 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
772 af = AF_INET6;
773 len = NS_IN6ADDRSZ;
774 } else {
775 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 res_state res = __res_get_state();
778 if (res == NULL) goto nospc;
779 if (res->options & RES_USE_INET6) {
780 map_v4v6_address(buf, buf);
781 af = AF_INET6;
782 len = NS_IN6ADDRSZ;
783 } else {
784 af = AF_INET;
785 len = NS_INADDRSZ;
786 }
787 __res_put_state(res);
788 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 /* if this is not something we're looking for, skip it. */
791 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
792 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900793
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900794 while (*cp == ' ' || *cp == '\t') cp++;
795 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
796 q = aliases;
797 while (cp && *cp) {
798 if (*cp == ' ' || *cp == '\t') {
799 cp++;
800 continue;
801 }
802 addalias(q, cp, aliases, maxaliases);
803 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
804 }
805 break;
806 }
807 hent->h_length = len;
808 hent->h_addrtype = af;
809 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
810 anum = (size_t)(q - aliases);
811 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
812 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
813 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900815 HENT_SCOPY(hent->h_name, name, buf, buflen);
816 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
817 hent->h_aliases[anum] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900818
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819 *he = NETDB_SUCCESS;
820 free(p);
821 free(aliases);
822 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900823nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900824 free(p);
825 free(aliases);
826 errno = ENOSPC;
827 *he = NETDB_INTERNAL;
828 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900829}
830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831static void map_v4v6_address(const char* src, char* dst) {
832 u_char* p = (u_char*) dst;
833 char tmp[NS_INADDRSZ];
834 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900835
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 _DIAGASSERT(src != NULL);
837 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900838
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900839 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900840 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900841 /* Mark this ipv6 addr as a mapped ipv4. */
842 for (i = 0; i < 10; i++) *p++ = 0x00;
843 *p++ = 0xff;
844 *p++ = 0xff;
845 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900846 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900847}
848
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
850 char** ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900851
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900852 _DIAGASSERT(hp != NULL);
853 _DIAGASSERT(bpp != NULL);
854 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900855
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900856 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
857 hp->h_addrtype = AF_INET6;
858 hp->h_length = NS_IN6ADDRSZ;
859 for (ap = hp->h_addr_list; *ap; ap++) {
860 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900861
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900862 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
863 /* Out of memory. Truncate address list here. XXX */
864 *ap = NULL;
865 return;
866 }
867 *bpp += i;
868 map_v4v6_address(*ap, *bpp);
869 *ap = *bpp;
870 *bpp += NS_IN6ADDRSZ;
871 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900872}
873
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900874static void addrsort(char** ap, int num, res_state res) {
875 int i, j;
876 char** p;
877 short aval[MAXADDRS];
878 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900879
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900880 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900881
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900882 p = ap;
883 for (i = 0; i < num; i++, p++) {
884 for (j = 0; (unsigned) j < res->nsort; j++)
885 if (res->sort_list[j].addr.s_addr ==
886 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
887 break;
888 aval[i] = j;
889 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
890 }
891 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900892
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900893 while (needsort < num) {
894 for (j = needsort - 1; j >= 0; j--) {
895 if (aval[j] > aval[j + 1]) {
896 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900897
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900898 i = aval[j];
899 aval[j] = aval[j + 1];
900 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900901
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900902 hp = ap[j];
903 ap[j] = ap[j + 1];
904 ap[j + 1] = hp;
905 } else
906 break;
907 }
908 needsort++;
909 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900910}
911
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900912static bool _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900913 int n, type;
914 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900915 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900916
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900917 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900918
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900919 switch (info->hp->h_addrtype) {
920 case AF_INET:
921 info->hp->h_length = NS_INADDRSZ;
922 type = T_A;
923 break;
924 case AF_INET6:
925 info->hp->h_length = NS_IN6ADDRSZ;
926 type = T_AAAA;
927 break;
928 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900929 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900930 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900931 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900932 if (buf == NULL) {
933 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900934 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900935 }
936 res = __res_get_state();
937 if (res == NULL) {
938 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900939 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900940 }
941 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf));
942 if (n < 0) {
943 free(buf);
944 debugprintf("res_nsearch failed (%d)\n", res, n);
945 __res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900946 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 }
948 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he);
949 free(buf);
950 __res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900951 if (hp == NULL) {
952 return false;
953 }
954 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900955}
956
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900957static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
958 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900959 char qbuf[MAXDNAME + 1], *qp, *ep;
960 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900961 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900962 int advance;
963 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900964
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900965 info->hp->h_length = len;
966 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900967
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900968 switch (info->hp->h_addrtype) {
969 case AF_INET:
970 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
971 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
972 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900973
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900974 case AF_INET6:
975 qp = qbuf;
976 ep = qbuf + sizeof(qbuf) - 1;
977 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
978 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
979 ((unsigned int) uaddr[n] >> 4) & 0xf);
980 if (advance > 0 && qp + advance < ep)
981 qp += advance;
982 else {
983 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900984 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985 }
986 }
987 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
988 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900989 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900990 }
991 break;
992 default:
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900993 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900994 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900995
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900996 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900997 if (buf == NULL) {
998 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900999 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001000 }
1001 res = __res_get_state();
1002 if (res == NULL) {
1003 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001004 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001005 }
1006 res_setnetcontext(res, netcontext);
1007 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf));
1008 if (n < 0) {
1009 free(buf);
1010 debugprintf("res_nquery failed (%d)\n", res, n);
1011 __res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001012 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001013 }
1014 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he);
1015 free(buf);
1016 if (hp == NULL) {
1017 __res_put_state(res);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001018 return false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001019 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001020
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001021 char* bf = (char*) (hp->h_addr_list + 2);
1022 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001023 if (blen + info->hp->h_length > info->buflen) goto nospc;
1024 hp->h_addr_list[0] = bf;
1025 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001026 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001027 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
1028 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
1029 map_v4v6_address(bf, bf);
1030 hp->h_addrtype = AF_INET6;
1031 hp->h_length = NS_IN6ADDRSZ;
1032 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001033
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001034 __res_put_state(res);
1035 *info->he = NETDB_SUCCESS;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001036 return true;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001037
Bernie Innocenti55864192018-08-30 04:05:20 +09001038nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001039 errno = ENOSPC;
1040 *info->he = NETDB_INTERNAL;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +09001041 return false;
Bernie Innocenti55864192018-08-30 04:05:20 +09001042}
1043
Bernie Innocenti55864192018-08-30 04:05:20 +09001044/*
1045 * Non-reentrant versions.
1046 */
1047
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001048struct hostent* gethostbyname(const char* name) {
1049 struct hostent* result = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001050 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001051
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001052 gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1053 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001054}
1055
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001056struct hostent* gethostbyname2(const char* name, int af) {
1057 struct hostent* result = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001058 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocenti55864192018-08-30 04:05:20 +09001059
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001060 gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
1061 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +09001062}
1063
1064// android_gethostby*fornet can be called in two different contexts.
1065// - In the proxy client context (proxy != NULL), |netid| is |app_netid|.
1066// - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|.
1067// The netcontext is constructed before checking which context we are in.
1068// Therefore, we have to populate both fields, and rely on the downstream code to check whether
1069// |proxy == NULL|, and use that info to query the field that matches the caller's intent.
1070static struct android_net_context make_context(unsigned netid, unsigned mark) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001071 struct android_net_context netcontext = NETCONTEXT_UNSET;
1072 netcontext.app_netid = netid;
1073 netcontext.app_mark = mark;
1074 netcontext.dns_netid = netid;
1075 netcontext.dns_mark = mark;
1076 return netcontext;
Bernie Innocenti55864192018-08-30 04:05:20 +09001077}
1078
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001079struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid,
1080 unsigned mark) {
1081 const struct android_net_context netcontext = make_context(netid, mark);
1082 return android_gethostbynamefornetcontext(name, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001083}
1084
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001085struct hostent* android_gethostbynamefornetcontext(const char* name, int af,
1086 const struct android_net_context* netcontext) {
1087 struct hostent* hp;
1088 res_state res = __res_get_state();
1089 if (res == NULL) return NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001090 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001091 hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
1092 &h_errno, netcontext);
1093 __res_put_state(res);
1094 return hp;
Bernie Innocenti55864192018-08-30 04:05:20 +09001095}
1096
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001097struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) {
1098 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET);
Bernie Innocenti55864192018-08-30 04:05:20 +09001099}
1100
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001101struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid,
1102 unsigned mark) {
1103 const struct android_net_context netcontext = make_context(netid, mark);
1104 return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001105}
1106
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001107struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
1108 const struct android_net_context* netcontext) {
1109 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001110}
1111
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001112__LIBC_HIDDEN__ struct hostent* android_gethostbyaddrfornetcontext_proxy(
1113 const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001114 struct res_static* rs = __res_get_static(); // For thread-safety.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001115 return android_gethostbyaddrfornetcontext_proxy_internal(
1116 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +09001117}