blob: 4da99b2fef5f06fb5855b83f92157ef88d8ff59f [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $NetBSD: gethnamaddr.c,v 1.70 2006/03/22 00:03:51 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
54#include <sys/cdefs.h>
55#include <sys/types.h>
56
57#include <sys/param.h>
58#include <sys/socket.h>
Mattias Falkc63e5902011-08-23 14:34:14 +020059#include <sys/un.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060#include <netinet/in.h>
61#include <arpa/inet.h>
Calin Juravle569fb982014-03-04 15:01:29 +000062#include <arpa/nameser.h>
Szymon Jakubczakea9bf672014-02-14 17:07:23 -050063#include "resolv_netid.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064#include "resolv_private.h"
65#include "resolv_cache.h"
66#include <assert.h>
67#include <ctype.h>
68#include <errno.h>
69#include <netdb.h>
70#include <stdarg.h>
71#include <stdio.h>
Carl Shapiro2cc2b2b2011-03-21 20:01:03 -070072#include <strings.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073#include <syslog.h>
Mattias Falkc63e5902011-08-23 14:34:14 +020074#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075
Calin Juravlec20de902014-03-20 15:21:32 +000076#define ALIGNBYTES (sizeof(uintptr_t) - 1)
77#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
78
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079#ifndef LOG_AUTH
80# define LOG_AUTH 0
81#endif
82
83#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
84
85#include "nsswitch.h"
86#include <stdlib.h>
87#include <string.h>
88
Mattias Falkc63e5902011-08-23 14:34:14 +020089// This should be synchronized to ResponseCode.h
90static const int DnsProxyQueryResult = 222;
91
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092static const char const AskedForGot[] =
93 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
94
95#define MAXPACKET (64*1024)
96
97typedef union {
98 HEADER hdr;
99 u_char buf[MAXPACKET];
100} querybuf;
101
102typedef union {
103 int32_t al;
104 char ac;
105} align;
106
107#ifdef DEBUG
108static void dprintf(const char *, res_state, ...)
109 __attribute__((__format__(__printf__, 1, 3)));
110#endif
111static struct hostent *getanswer(const querybuf *, int, const char *, int,
112 res_state);
113static void map_v4v6_address(const char *, char *);
114static void map_v4v6_hostent(struct hostent *, char **, char *);
115static void addrsort(char **, int, res_state);
116
Jim Huange5c35e02010-09-27 23:37:10 +0800117static void _sethtent(int);
118static void _endhtent(void);
119static struct hostent *_gethtent(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120void ht_sethostent(int);
121void ht_endhostent(void);
122struct hostent *ht_gethostbyname(char *);
123struct hostent *ht_gethostbyaddr(const char *, int, int);
124void dns_service(void);
125#undef dn_skipname
126int dn_skipname(const u_char *, const u_char *);
Jim Huange5c35e02010-09-27 23:37:10 +0800127static int _gethtbyaddr(void *, void *, va_list);
128static int _gethtbyname(void *, void *, va_list);
129static struct hostent *_gethtbyname2(const char *, int);
130static int _dns_gethtbyaddr(void *, void *, va_list);
131static int _dns_gethtbyname(void *, void *, va_list);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500133static struct hostent *gethostbyname_internal(const char *, int, res_state, unsigned, unsigned);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134
135static const ns_src default_dns_files[] = {
136 { NSSRC_FILES, NS_SUCCESS },
137 { NSSRC_DNS, NS_SUCCESS },
138 { 0, 0 }
139};
140
141
142#ifdef DEBUG
143static void
144dprintf(const char *msg, res_state res, ...)
145{
146 assert(msg != NULL);
147
148 if (res->options & RES_DEBUG) {
149 int save = errno;
150 va_list ap;
151
152 va_start (ap, res);
153 vprintf(msg, ap);
154 va_end (ap);
155
156 errno = save;
157 }
158}
159#else
160# define dprintf(msg, res, num) ((void)0) /*nada*/
161#endif
162
163#define BOUNDED_INCR(x) \
164 do { \
165 cp += (x); \
166 if (cp > eom) { \
167 h_errno = NO_RECOVERY; \
168 return NULL; \
169 } \
170 } while (/*CONSTCOND*/0)
171
172#define BOUNDS_CHECK(ptr, count) \
173 do { \
174 if ((ptr) + (count) > eom) { \
175 h_errno = NO_RECOVERY; \
176 return NULL; \
177 } \
178 } while (/*CONSTCOND*/0)
179
180static struct hostent *
181getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
182 res_state res)
183{
184 const HEADER *hp;
185 const u_char *cp;
186 int n;
187 const u_char *eom, *erdata;
188 char *bp, **ap, **hap, *ep;
189 int type, class, ancount, qdcount;
190 int haveanswer, had_error;
191 int toobig = 0;
192 char tbuf[MAXDNAME];
193 const char *tname;
194 int (*name_ok)(const char *);
195 res_static rs = __res_get_static();
196
197 assert(answer != NULL);
198 assert(qname != NULL);
199
200 tname = qname;
201 rs->host.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 return NULL; /* XXX should be abort(); */
213 }
214 /*
215 * find first satisfactory answer
216 */
217 hp = &answer->hdr;
218 ancount = ntohs(hp->ancount);
219 qdcount = ntohs(hp->qdcount);
220 bp = rs->hostbuf;
221 ep = rs->hostbuf + sizeof rs->hostbuf;
222 cp = answer->buf;
223 BOUNDED_INCR(HFIXEDSZ);
224 if (qdcount != 1) {
225 h_errno = NO_RECOVERY;
226 return NULL;
227 }
228 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
229 if ((n < 0) || !(*name_ok)(bp)) {
230 h_errno = NO_RECOVERY;
231 return NULL;
232 }
233 BOUNDED_INCR(n + QFIXEDSZ);
234 if (qtype == T_A || qtype == T_AAAA) {
235 /* res_send() has already verified that the query name is the
236 * same as the one we sent; this just gets the expanded name
237 * (i.e., with the succeeding search-domain tacked on).
238 */
239 n = strlen(bp) + 1; /* for the \0 */
240 if (n >= MAXHOSTNAMELEN) {
241 h_errno = NO_RECOVERY;
242 return NULL;
243 }
244 rs->host.h_name = bp;
245 bp += n;
246 /* The qname can be abbreviated, but h_name is now absolute. */
247 qname = rs->host.h_name;
248 }
249 ap = rs->host_aliases;
250 *ap = NULL;
251 rs->host.h_aliases = rs->host_aliases;
252 hap = rs->h_addr_ptrs;
253 *hap = NULL;
254 rs->host.h_addr_list = rs->h_addr_ptrs;
255 haveanswer = 0;
256 had_error = 0;
257 while (ancount-- > 0 && cp < eom && !had_error) {
258 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
259 if ((n < 0) || !(*name_ok)(bp)) {
260 had_error++;
261 continue;
262 }
263 cp += n; /* name */
264 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
265 type = _getshort(cp);
266 cp += INT16SZ; /* type */
267 class = _getshort(cp);
268 cp += INT16SZ + INT32SZ; /* class, TTL */
269 n = _getshort(cp);
270 cp += INT16SZ; /* len */
271 BOUNDS_CHECK(cp, n);
272 erdata = cp + n;
273 if (class != C_IN) {
274 /* XXX - debug? syslog? */
275 cp += n;
276 continue; /* XXX - had_error++ ? */
277 }
278 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
279 if (ap >= &rs->host_aliases[MAXALIASES-1])
280 continue;
281 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
282 if ((n < 0) || !(*name_ok)(tbuf)) {
283 had_error++;
284 continue;
285 }
286 cp += n;
287 if (cp != erdata) {
288 h_errno = NO_RECOVERY;
289 return NULL;
290 }
291 /* Store alias. */
292 *ap++ = bp;
293 n = strlen(bp) + 1; /* for the \0 */
294 if (n >= MAXHOSTNAMELEN) {
295 had_error++;
296 continue;
297 }
298 bp += n;
299 /* Get canonical name. */
300 n = strlen(tbuf) + 1; /* for the \0 */
301 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
302 had_error++;
303 continue;
304 }
305 strlcpy(bp, tbuf, (size_t)(ep - bp));
306 rs->host.h_name = bp;
307 bp += n;
308 continue;
309 }
310 if (qtype == T_PTR && type == T_CNAME) {
311 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
312 if (n < 0 || !res_dnok(tbuf)) {
313 had_error++;
314 continue;
315 }
316 cp += n;
317 if (cp != erdata) {
318 h_errno = NO_RECOVERY;
319 return NULL;
320 }
321 /* Get canonical name. */
322 n = 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\"",
336 qname, p_class(C_IN), p_type(qtype),
337 p_type(type));
338 cp += n;
339 continue; /* XXX - had_error++ ? */
340 }
341 switch (type) {
342 case T_PTR:
343 if (strcasecmp(tname, bp) != 0) {
344 syslog(LOG_NOTICE|LOG_AUTH,
345 AskedForGot, qname, bp);
346 cp += n;
347 continue; /* XXX - had_error++ ? */
348 }
349 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
350 if ((n < 0) || !res_hnok(bp)) {
351 had_error++;
352 break;
353 }
354#if MULTI_PTRS_ARE_ALIASES
355 cp += n;
356 if (cp != erdata) {
357 h_errno = NO_RECOVERY;
358 return NULL;
359 }
360 if (!haveanswer)
361 rs->host.h_name = bp;
362 else if (ap < &rs->host_aliases[MAXALIASES-1])
363 *ap++ = bp;
364 else
365 n = -1;
366 if (n != -1) {
367 n = strlen(bp) + 1; /* for the \0 */
368 if (n >= MAXHOSTNAMELEN) {
369 had_error++;
370 break;
371 }
372 bp += n;
373 }
374 break;
375#else
376 rs->host.h_name = bp;
377 if (res->options & RES_USE_INET6) {
378 n = strlen(bp) + 1; /* for the \0 */
379 if (n >= MAXHOSTNAMELEN) {
380 had_error++;
381 break;
382 }
383 bp += n;
384 map_v4v6_hostent(&rs->host, &bp, ep);
385 }
386 h_errno = NETDB_SUCCESS;
387 return &rs->host;
388#endif
389 case T_A:
390 case T_AAAA:
391 if (strcasecmp(rs->host.h_name, bp) != 0) {
392 syslog(LOG_NOTICE|LOG_AUTH,
393 AskedForGot, rs->host.h_name, bp);
394 cp += n;
395 continue; /* XXX - had_error++ ? */
396 }
397 if (n != rs->host.h_length) {
398 cp += n;
399 continue;
400 }
401 if (type == T_AAAA) {
402 struct in6_addr in6;
403 memcpy(&in6, cp, IN6ADDRSZ);
404 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
405 cp += n;
406 continue;
407 }
408 }
409 if (!haveanswer) {
410 int nn;
411
412 rs->host.h_name = bp;
413 nn = strlen(bp) + 1; /* for the \0 */
414 bp += nn;
415 }
416
417 bp += sizeof(align) -
418 (size_t)((u_long)bp % sizeof(align));
419
420 if (bp + n >= &rs->hostbuf[sizeof rs->hostbuf]) {
421 dprintf("size (%d) too big\n", res, n);
422 had_error++;
423 continue;
424 }
425 if (hap >= &rs->h_addr_ptrs[MAXADDRS-1]) {
426 if (!toobig++)
427 dprintf("Too many addresses (%d)\n",
428 res, MAXADDRS);
429 cp += n;
430 continue;
431 }
432 (void)memcpy(*hap++ = bp, cp, (size_t)n);
433 bp += n;
434 cp += n;
435 if (cp != erdata) {
436 h_errno = NO_RECOVERY;
437 return NULL;
438 }
439 break;
440 default:
441 abort();
442 }
443 if (!had_error)
444 haveanswer++;
445 }
446 if (haveanswer) {
447 *ap = NULL;
448 *hap = NULL;
449 /*
450 * Note: we sort even if host can take only one address
451 * in its return structures - should give it the "best"
452 * address in that case, not some random one
453 */
454 if (res->nsort && haveanswer > 1 && qtype == T_A)
455 addrsort(rs->h_addr_ptrs, haveanswer, res);
456 if (!rs->host.h_name) {
457 n = strlen(qname) + 1; /* for the \0 */
458 if (n > ep - bp || n >= MAXHOSTNAMELEN)
459 goto no_recovery;
460 strlcpy(bp, qname, (size_t)(ep - bp));
461 rs->host.h_name = bp;
462 bp += n;
463 }
464 if (res->options & RES_USE_INET6)
465 map_v4v6_hostent(&rs->host, &bp, ep);
466 h_errno = NETDB_SUCCESS;
467 return &rs->host;
468 }
469 no_recovery:
470 h_errno = NO_RECOVERY;
471 return NULL;
472}
473
474int
475gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen,
476 struct hostent**result, int *errorp)
477{
478 struct hostent *res;
479
480 res = gethostbyname(name);
481 *errorp = h_errno;
482 if (res == NULL) {
483 *result = NULL;
484 return -1;
485 }
486 memcpy(hp, res, sizeof *hp);
487 *result = hp;
488 return 0;
489}
490
491struct hostent *
492gethostbyname(const char *name)
493{
494 struct hostent *hp;
495 res_state res = __res_get_state();
496
497 if (res == NULL)
498 return NULL;
499
500 assert(name != NULL);
501
Mattias Falkc63e5902011-08-23 14:34:14 +0200502 /* try IPv6 first - if that fails do IPv4 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800503 if (res->options & RES_USE_INET6) {
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500504 hp = gethostbyname_internal(name, AF_INET6, res, NETID_UNSET, MARK_UNSET);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800505 if (hp) {
506 __res_put_state(res);
507 return hp;
508 }
509 }
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500510 hp = gethostbyname_internal(name, AF_INET, res, NETID_UNSET, MARK_UNSET);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800511 __res_put_state(res);
512 return hp;
513}
514
515struct hostent *
516gethostbyname2(const char *name, int af)
517{
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500518 return android_gethostbynamefornet(name, af, NETID_UNSET, MARK_UNSET);
Mattias Falkc63e5902011-08-23 14:34:14 +0200519}
520
521struct hostent *
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500522android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark)
Mattias Falkc63e5902011-08-23 14:34:14 +0200523{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524 struct hostent *hp;
525 res_state res = __res_get_state();
526
527 if (res == NULL)
528 return NULL;
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500529 hp = gethostbyname_internal(name, af, res, netid, mark);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800530 __res_put_state(res);
531 return hp;
532}
533
Mattias Falkc63e5902011-08-23 14:34:14 +0200534
535static FILE* android_open_proxy()
536{
537 int sock;
538 const int one = 1;
539 struct sockaddr_un proxy_addr;
540
541 sock = socket(AF_UNIX, SOCK_STREAM, 0);
542 if (sock < 0) {
543 return NULL;
544 }
545
546 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
547 memset(&proxy_addr, 0, sizeof(proxy_addr));
548 proxy_addr.sun_family = AF_UNIX;
549 strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", sizeof(proxy_addr.sun_path));
550 if (TEMP_FAILURE_RETRY(connect(sock,
551 (const struct sockaddr*) &proxy_addr,
552 sizeof(proxy_addr))) != 0) {
553 close(sock);
554 return NULL;
555 }
556
557 return fdopen(sock, "r+");
558}
559
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800560static struct hostent *
Mattias Falkc63e5902011-08-23 14:34:14 +0200561android_read_hostent(FILE* proxy)
562{
563 uint32_t size;
564 char buf[4];
565 if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) return NULL;
566
567 /* This is reading serialized data from system/netd/DnsProxyListener.cpp
568 * and changes here need to be matched there */
569 int result_code = strtol(buf, NULL, 10);
570 if (result_code != DnsProxyQueryResult) {
571 fread(&size, 1, sizeof(size), proxy);
572 return NULL;
573 }
574
575 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
576 size = ntohl(size);
577 res_static rs = __res_get_static();
578 memset(&rs->host, 0, sizeof(rs->host));
579 char *ptr = rs->hostbuf;
580
581 if (fread(ptr, 1, size, proxy) != size) return NULL;
582 ptr += size;
583 rs->host.h_name = rs->hostbuf;
584
585 char **aliases = rs->host_aliases;
586 rs->host.h_aliases = rs->host_aliases;
587 while (1) {
588 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
589 size = ntohl(size);
590
591 if (size == 0) {
592 *aliases = NULL;
593 break;
594 }
595 if (fread(ptr, 1, size, proxy) != size) return NULL;
596 *aliases++ = ptr;
597 ptr += size;
598 }
599
600 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
601 rs->host.h_addrtype = ntohl(size);
602
603 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
604 rs->host.h_length = ntohl(size);
605
606 char **addrs = rs->h_addr_ptrs;
607 rs->host.h_addr_list = rs->h_addr_ptrs;
608 while (1) {
609 if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
610 size = ntohl(size);
611 if (size == 0) {
612 *addrs = NULL;
613 break;
614 }
615 if (fread(ptr, 1, size, proxy) != size) return NULL;
616 *addrs++ = ptr;
617 ptr += size;
618 }
619
620 return &rs->host;
621}
622
623
624static struct hostent *
625gethostbyname_internal_real(const char *name, int af, res_state res)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800626{
627 const char *cp;
628 char *bp, *ep;
629 int size;
630 struct hostent *hp;
Mattias Falkc63e5902011-08-23 14:34:14 +0200631 res_static rs = __res_get_static();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800632
633 static const ns_dtab dtab[] = {
634 NS_FILES_CB(_gethtbyname, NULL)
635 { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
636 { 0, 0, 0 }
637 };
638
639 assert(name != NULL);
640
641 switch (af) {
642 case AF_INET:
643 size = INADDRSZ;
644 break;
645 case AF_INET6:
646 size = IN6ADDRSZ;
647 break;
648 default:
649 h_errno = NETDB_INTERNAL;
650 errno = EAFNOSUPPORT;
651 return NULL;
652 }
653
654 rs->host.h_addrtype = af;
655 rs->host.h_length = size;
656
657 /*
658 * if there aren't any dots, it could be a user-level alias.
659 * this is also done in res_nquery() since we are not the only
660 * function that looks up host names.
661 */
662 if (!strchr(name, '.') && (cp = __hostalias(name)))
663 name = cp;
664
665 /*
666 * disallow names consisting only of digits/dots, unless
667 * they end in a dot.
668 */
669 if (isdigit((u_char) name[0]))
670 for (cp = name;; ++cp) {
671 if (!*cp) {
672 if (*--cp == '.')
673 break;
674 /*
675 * All-numeric, no dot at the end.
676 * Fake up a hostent as if we'd actually
677 * done a lookup.
678 */
679 if (inet_pton(af, name,
680 (char *)(void *)rs->host_addr) <= 0) {
681 h_errno = HOST_NOT_FOUND;
682 return NULL;
683 }
684 strncpy(rs->hostbuf, name, MAXDNAME);
685 rs->hostbuf[MAXDNAME] = '\0';
686 bp = rs->hostbuf + MAXDNAME;
687 ep = rs->hostbuf + sizeof rs->hostbuf;
688 rs->host.h_name = rs->hostbuf;
689 rs->host.h_aliases = rs->host_aliases;
690 rs->host_aliases[0] = NULL;
691 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
692 rs->h_addr_ptrs[1] = NULL;
693 rs->host.h_addr_list = rs->h_addr_ptrs;
694 if (res->options & RES_USE_INET6)
695 map_v4v6_hostent(&rs->host, &bp, ep);
696 h_errno = NETDB_SUCCESS;
697 return &rs->host;
698 }
699 if (!isdigit((u_char) *cp) && *cp != '.')
700 break;
701 }
702 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
703 name[0] == ':')
704 for (cp = name;; ++cp) {
705 if (!*cp) {
706 if (*--cp == '.')
707 break;
708 /*
709 * All-IPv6-legal, no dot at the end.
710 * Fake up a hostent as if we'd actually
711 * done a lookup.
712 */
713 if (inet_pton(af, name,
714 (char *)(void *)rs->host_addr) <= 0) {
715 h_errno = HOST_NOT_FOUND;
716 return NULL;
717 }
718 strncpy(rs->hostbuf, name, MAXDNAME);
719 rs->hostbuf[MAXDNAME] = '\0';
720 bp = rs->hostbuf + MAXDNAME;
721 ep = rs->hostbuf + sizeof rs->hostbuf;
722 rs->host.h_name = rs->hostbuf;
723 rs->host.h_aliases = rs->host_aliases;
724 rs->host_aliases[0] = NULL;
725 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
726 rs->h_addr_ptrs[1] = NULL;
727 rs->host.h_addr_list = rs->h_addr_ptrs;
728 h_errno = NETDB_SUCCESS;
729 return &rs->host;
730 }
731 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
732 break;
733 }
734
735 hp = NULL;
736 h_errno = NETDB_INTERNAL;
737 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname",
738 default_dns_files, name, strlen(name), af) != NS_SUCCESS) {
739 return NULL;
Mattias Falkc63e5902011-08-23 14:34:14 +0200740 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800741 h_errno = NETDB_SUCCESS;
742 return hp;
743}
744
Mattias Falkc63e5902011-08-23 14:34:14 +0200745
746// very similar in proxy-ness to android_getaddrinfo_proxy
747static struct hostent *
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500748gethostbyname_internal(const char *name, int af, res_state res, unsigned netid, unsigned mark)
Mattias Falkc63e5902011-08-23 14:34:14 +0200749{
750 const char *cache_mode = getenv("ANDROID_DNS_MODE");
751 FILE* proxy = NULL;
752 struct hostent *result = NULL;
753
754 if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) {
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500755 res_setnetid(res, netid);
Chad Brubakerc39214e2013-06-20 10:36:56 -0700756 res_setmark(res, mark);
Mattias Falkc63e5902011-08-23 14:34:14 +0200757 return gethostbyname_internal_real(name, af, res);
758 }
759
760 proxy = android_open_proxy();
Nick Kralevicha6b24b72013-02-21 20:10:41 -0800761 if (proxy == NULL) goto exit;
Mattias Falkc63e5902011-08-23 14:34:14 +0200762
763 /* This is writing to system/netd/DnsProxyListener.cpp and changes
764 * here need to be matched there */
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500765 if (fprintf(proxy, "gethostbyname %u %s %d",
766 netid,
Mattias Falkc63e5902011-08-23 14:34:14 +0200767 name == NULL ? "^" : name,
768 af) < 0) {
769 goto exit;
770 }
771
772 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
773 goto exit;
774 }
775
776 result = android_read_hostent(proxy);
777
778exit:
779 if (proxy != NULL) {
780 fclose(proxy);
781 }
782 return result;
783}
784
785
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800786struct hostent *
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500787android_gethostbyaddrfornet_proxy(const void *addr,
788 socklen_t len, int af, unsigned netid)
Mattias Falkc63e5902011-08-23 14:34:14 +0200789{
790 struct hostent *result = NULL;
791 FILE* proxy = android_open_proxy();
792
793 if (proxy == NULL) goto exit;
794
795 char buf[INET6_ADDRSTRLEN]; //big enough for IPv4 and IPv6
796 const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf));
797 if (addrStr == NULL) goto exit;
798
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500799 if (fprintf(proxy, "gethostbyaddr %s %d %d %u",
800 addrStr, len, af, netid) < 0) {
Mattias Falkc63e5902011-08-23 14:34:14 +0200801 goto exit;
802 }
803
804 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
805 goto exit;
806 }
807
808 result = android_read_hostent(proxy);
809exit:
810 if (proxy != NULL) {
811 fclose(proxy);
812 }
813 return result;
814}
815
816struct hostent *
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500817android_gethostbyaddrfornet_real(const void *addr,
818 socklen_t len, int af, unsigned netid, unsigned mark)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800819{
820 const u_char *uaddr = (const u_char *)addr;
821 socklen_t size;
822 struct hostent *hp;
823 static const ns_dtab dtab[] = {
824 NS_FILES_CB(_gethtbyaddr, NULL)
825 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
826 { 0, 0, 0 }
827 };
828
829 assert(addr != NULL);
830
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700831 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
832 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) ||
833 IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800834 h_errno = HOST_NOT_FOUND;
835 return NULL;
836 }
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700837 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
838 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) ||
839 IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800840 /* Unmap. */
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700841 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
842 addr = uaddr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800843 af = AF_INET;
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700844 len = NS_INADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800845 }
846 switch (af) {
847 case AF_INET:
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700848 size = NS_INADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800849 break;
850 case AF_INET6:
Elliott Hughes3e5f0c92014-05-06 11:23:40 -0700851 size = NS_IN6ADDRSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800852 break;
853 default:
854 errno = EAFNOSUPPORT;
855 h_errno = NETDB_INTERNAL;
856 return NULL;
857 }
858 if (size != len) {
859 errno = EINVAL;
860 h_errno = NETDB_INTERNAL;
861 return NULL;
862 }
863 hp = NULL;
864 h_errno = NETDB_INTERNAL;
865 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500866 default_dns_files, uaddr, len, af, netid, mark) != NS_SUCCESS)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800867 return NULL;
868 h_errno = NETDB_SUCCESS;
869 return hp;
870}
871
Mattias Falkc63e5902011-08-23 14:34:14 +0200872struct hostent *
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500873android_gethostbyaddrfornet(const void *addr, socklen_t len, int af, unsigned netid, unsigned mark)
Mattias Falkc63e5902011-08-23 14:34:14 +0200874{
875 const char *cache_mode = getenv("ANDROID_DNS_MODE");
876
877 if (cache_mode == NULL || strcmp(cache_mode, "local") != 0) {
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500878 return android_gethostbyaddrfornet_proxy(addr, len, af, netid);
Mattias Falkc63e5902011-08-23 14:34:14 +0200879 } else {
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500880 return android_gethostbyaddrfornet_real(addr,len, af, netid, mark);
Mattias Falkc63e5902011-08-23 14:34:14 +0200881 }
882}
883
884struct hostent *
885gethostbyaddr(const void *addr, socklen_t len, int af)
886{
Szymon Jakubczakea9bf672014-02-14 17:07:23 -0500887 return android_gethostbyaddrfornet(addr, len, af, NETID_UNSET, MARK_UNSET);
Mattias Falkc63e5902011-08-23 14:34:14 +0200888}
889
890
Jim Huange5c35e02010-09-27 23:37:10 +0800891static void
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800892_sethtent(int f)
893{
894 res_static rs = __res_get_static();
895 if (rs == NULL) return;
896 if (!rs->hostf)
897 rs->hostf = fopen(_PATH_HOSTS, "r" );
898 else
899 rewind(rs->hostf);
900 rs->stayopen = f;
901}
902
Jim Huange5c35e02010-09-27 23:37:10 +0800903static void
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800904_endhtent(void)
905{
906 res_static rs = __res_get_static();
907 if (rs == NULL) return;
908
909 if (rs->hostf && !rs->stayopen) {
910 (void) fclose(rs->hostf);
911 rs->hostf = NULL;
912 }
913}
914
Jim Huange5c35e02010-09-27 23:37:10 +0800915static struct hostent *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800916_gethtent(void)
917{
918 char *p;
919 char *cp, **q;
920 int af, len;
921 res_static rs = __res_get_static();
922
923 if (!rs->hostf && !(rs->hostf = fopen(_PATH_HOSTS, "r" ))) {
924 h_errno = NETDB_INTERNAL;
925 return NULL;
926 }
927 again:
928 if (!(p = fgets(rs->hostbuf, sizeof rs->hostbuf, rs->hostf))) {
929 h_errno = HOST_NOT_FOUND;
930 return NULL;
931 }
932 if (*p == '#')
933 goto again;
934 if (!(cp = strpbrk(p, "#\n")))
935 goto again;
936 *cp = '\0';
937 if (!(cp = strpbrk(p, " \t")))
938 goto again;
939 *cp++ = '\0';
940 if (inet_pton(AF_INET6, p, (char *)(void *)rs->host_addr) > 0) {
941 af = AF_INET6;
942 len = IN6ADDRSZ;
943 } else if (inet_pton(AF_INET, p, (char *)(void *)rs->host_addr) > 0) {
944 res_state res = __res_get_state();
945 if (res == NULL)
946 return NULL;
947 if (res->options & RES_USE_INET6) {
948 map_v4v6_address((char *)(void *)rs->host_addr,
949 (char *)(void *)rs->host_addr);
950 af = AF_INET6;
951 len = IN6ADDRSZ;
952 } else {
953 af = AF_INET;
954 len = INADDRSZ;
955 }
956 __res_put_state(res);
957 } else {
958 goto again;
959 }
960 /* if this is not something we're looking for, skip it. */
961 if (rs->host.h_addrtype != 0 && rs->host.h_addrtype != af)
962 goto again;
963 if (rs->host.h_length != 0 && rs->host.h_length != len)
964 goto again;
965 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
966 rs->h_addr_ptrs[1] = NULL;
967 rs->host.h_addr_list = rs->h_addr_ptrs;
968 rs->host.h_length = len;
969 rs->host.h_addrtype = af;
970 while (*cp == ' ' || *cp == '\t')
971 cp++;
972 rs->host.h_name = cp;
973 q = rs->host.h_aliases = rs->host_aliases;
974 if ((cp = strpbrk(cp, " \t")) != NULL)
975 *cp++ = '\0';
976 while (cp && *cp) {
977 if (*cp == ' ' || *cp == '\t') {
978 cp++;
979 continue;
980 }
981 if (q < &rs->host_aliases[MAXALIASES - 1])
982 *q++ = cp;
983 if ((cp = strpbrk(cp, " \t")) != NULL)
984 *cp++ = '\0';
985 }
986 *q = NULL;
987 h_errno = NETDB_SUCCESS;
988 return &rs->host;
989}
990
991/*ARGSUSED*/
992int
993_gethtbyname(void *rv, void *cb_data, va_list ap)
994{
995 struct hostent *hp;
996 const char *name;
997 int af;
998
999 assert(rv != NULL);
1000
1001 name = va_arg(ap, char *);
1002 /* NOSTRICT skip len */(void)va_arg(ap, int);
1003 af = va_arg(ap, int);
1004
1005 hp = NULL;
1006#if 0
1007 {
1008 res_state res = __res_get_state();
1009 if (res == NULL)
1010 return NS_NOTFOUND;
1011 if (res->options & RES_USE_INET6)
1012 hp = _gethtbyname2(name, AF_INET6);
1013 if (hp==NULL)
1014 hp = _gethtbyname2(name, AF_INET);
1015 __res_put_state(res);
1016 }
1017#else
1018 hp = _gethtbyname2(name, af);
1019#endif
1020 *((struct hostent **)rv) = hp;
1021 if (hp == NULL) {
1022 h_errno = HOST_NOT_FOUND;
1023 return NS_NOTFOUND;
1024 }
1025 return NS_SUCCESS;
1026}
1027
Jim Huange5c35e02010-09-27 23:37:10 +08001028static struct hostent *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001029_gethtbyname2(const char *name, int af)
1030{
1031 struct hostent *p;
1032 char *tmpbuf, *ptr, **cp;
1033 int num;
1034 size_t len;
1035 res_static rs = __res_get_static();
1036
1037 assert(name != NULL);
1038
1039 _sethtent(rs->stayopen);
1040 ptr = tmpbuf = NULL;
1041 num = 0;
1042 while ((p = _gethtent()) != NULL && num < MAXADDRS) {
1043 if (p->h_addrtype != af)
1044 continue;
1045 if (strcasecmp(p->h_name, name) != 0) {
1046 for (cp = p->h_aliases; *cp != NULL; cp++)
1047 if (strcasecmp(*cp, name) == 0)
1048 break;
1049 if (*cp == NULL) continue;
1050 }
1051
1052 if (num == 0) {
1053 size_t bufsize;
1054 char *src;
1055
1056 bufsize = strlen(p->h_name) + 2 +
1057 MAXADDRS * p->h_length +
1058 ALIGNBYTES;
1059 for (cp = p->h_aliases; *cp != NULL; cp++)
1060 bufsize += strlen(*cp) + 1;
1061
1062 if ((tmpbuf = malloc(bufsize)) == NULL) {
1063 h_errno = NETDB_INTERNAL;
1064 return NULL;
1065 }
1066
1067 ptr = tmpbuf;
1068 src = p->h_name;
1069 while ((*ptr++ = *src++) != '\0');
1070 for (cp = p->h_aliases; *cp != NULL; cp++) {
1071 src = *cp;
1072 while ((*ptr++ = *src++) != '\0');
1073 }
1074 *ptr++ = '\0';
1075
1076 ptr = (char *)(void *)ALIGN(ptr);
1077 }
1078
1079 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
1080 ptr += p->h_length;
1081 num++;
1082 }
1083 _endhtent();
1084 if (num == 0) return NULL;
1085
1086 len = ptr - tmpbuf;
1087 if (len > (sizeof(rs->hostbuf) - ALIGNBYTES)) {
1088 free(tmpbuf);
1089 errno = ENOSPC;
1090 h_errno = NETDB_INTERNAL;
1091 return NULL;
1092 }
1093 ptr = memcpy((void *)ALIGN(rs->hostbuf), tmpbuf, len);
1094 free(tmpbuf);
1095
1096 rs->host.h_name = ptr;
1097 while (*ptr++);
1098
1099 cp = rs->host_aliases;
1100 while (*ptr) {
1101 *cp++ = ptr;
1102 while (*ptr++);
1103 }
1104 ptr++;
1105 *cp = NULL;
1106
1107 ptr = (char *)(void *)ALIGN(ptr);
1108 cp = rs->h_addr_ptrs;
1109 while (num--) {
1110 *cp++ = ptr;
1111 ptr += rs->host.h_length;
1112 }
1113 *cp = NULL;
1114
1115 return &rs->host;
1116}
1117
1118/*ARGSUSED*/
Jim Huange5c35e02010-09-27 23:37:10 +08001119static int
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001120_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1121{
1122 struct hostent *p;
1123 const unsigned char *addr;
1124 int len, af;
1125 res_static rs = __res_get_static();
1126
1127 assert(rv != NULL);
1128
1129 addr = va_arg(ap, unsigned char *);
1130 len = va_arg(ap, int);
1131 af = va_arg(ap, int);
1132
1133 rs->host.h_length = len;
1134 rs->host.h_addrtype = af;
1135
1136 _sethtent(rs->stayopen);
1137 while ((p = _gethtent()) != NULL)
1138 if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
1139 (size_t)len))
1140 break;
1141 _endhtent();
1142 *((struct hostent **)rv) = p;
1143 if (p==NULL) {
1144 h_errno = HOST_NOT_FOUND;
1145 return NS_NOTFOUND;
1146 }
1147 return NS_SUCCESS;
1148}
1149
1150static void
1151map_v4v6_address(const char *src, char *dst)
1152{
1153 u_char *p = (u_char *)dst;
1154 char tmp[INADDRSZ];
1155 int i;
1156
1157 assert(src != NULL);
1158 assert(dst != NULL);
1159
1160 /* Stash a temporary copy so our caller can update in place. */
1161 (void)memcpy(tmp, src, INADDRSZ);
1162 /* Mark this ipv6 addr as a mapped ipv4. */
1163 for (i = 0; i < 10; i++)
1164 *p++ = 0x00;
1165 *p++ = 0xff;
1166 *p++ = 0xff;
1167 /* Retrieve the saved copy and we're done. */
1168 (void)memcpy((void *)p, tmp, INADDRSZ);
1169}
1170
1171static void
1172map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
1173{
1174 char **ap;
1175
1176 assert(hp != NULL);
1177 assert(bpp != NULL);
1178 assert(ep != NULL);
1179
1180 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1181 return;
1182 hp->h_addrtype = AF_INET6;
1183 hp->h_length = IN6ADDRSZ;
1184 for (ap = hp->h_addr_list; *ap; ap++) {
1185 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
1186
1187 if (ep - *bpp < (i + IN6ADDRSZ)) {
1188 /* Out of memory. Truncate address list here. XXX */
1189 *ap = NULL;
1190 return;
1191 }
1192 *bpp += i;
1193 map_v4v6_address(*ap, *bpp);
1194 *ap = *bpp;
1195 *bpp += IN6ADDRSZ;
1196 }
1197}
1198
1199static void
1200addrsort(char **ap, int num, res_state res)
1201{
1202 int i, j;
1203 char **p;
1204 short aval[MAXADDRS];
1205 int needsort = 0;
1206
1207 assert(ap != NULL);
1208
1209 p = ap;
1210 for (i = 0; i < num; i++, p++) {
1211 for (j = 0 ; (unsigned)j < res->nsort; j++)
1212 if (res->sort_list[j].addr.s_addr ==
1213 (((struct in_addr *)(void *)(*p))->s_addr &
1214 res->sort_list[j].mask))
1215 break;
1216 aval[i] = j;
1217 if (needsort == 0 && i > 0 && j < aval[i-1])
1218 needsort = i;
1219 }
1220 if (!needsort)
1221 return;
1222
1223 while (needsort < num) {
1224 for (j = needsort - 1; j >= 0; j--) {
1225 if (aval[j] > aval[j+1]) {
1226 char *hp;
1227
1228 i = aval[j];
1229 aval[j] = aval[j+1];
1230 aval[j+1] = i;
1231
1232 hp = ap[j];
1233 ap[j] = ap[j+1];
1234 ap[j+1] = hp;
1235 } else
1236 break;
1237 }
1238 needsort++;
1239 }
1240}
1241
1242struct hostent *
1243gethostent(void)
1244{
1245 res_static rs = __res_get_static();
1246 rs->host.h_addrtype = 0;
1247 rs->host.h_length = 0;
1248 return _gethtent();
1249}
1250
1251/*ARGSUSED*/
Jim Huange5c35e02010-09-27 23:37:10 +08001252static int
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001253_dns_gethtbyname(void *rv, void *cb_data, va_list ap)
1254{
1255 querybuf *buf;
1256 int n, type;
1257 struct hostent *hp;
1258 const char *name;
1259 int af;
1260 res_state res;
1261
1262 assert(rv != NULL);
1263
1264 name = va_arg(ap, char *);
1265 /* NOSTRICT skip len */(void)va_arg(ap, int);
1266 af = va_arg(ap, int);
1267
1268 switch (af) {
1269 case AF_INET:
1270 type = T_A;
1271 break;
1272 case AF_INET6:
1273 type = T_AAAA;
1274 break;
1275 default:
1276 return NS_UNAVAIL;
1277 }
1278 buf = malloc(sizeof(*buf));
1279 if (buf == NULL) {
1280 h_errno = NETDB_INTERNAL;
1281 return NS_NOTFOUND;
1282 }
1283 res = __res_get_state();
1284 if (res == NULL) {
1285 free(buf);
1286 return NS_NOTFOUND;
1287 }
1288 n = res_nsearch(res, name, C_IN, type, buf->buf, sizeof(buf->buf));
1289 if (n < 0) {
1290 free(buf);
1291 dprintf("res_nsearch failed (%d)\n", res, n);
1292 __res_put_state(res);
1293 return NS_NOTFOUND;
1294 }
1295 hp = getanswer(buf, n, name, type, res);
1296 free(buf);
1297 __res_put_state(res);
1298 if (hp == NULL)
1299 switch (h_errno) {
1300 case HOST_NOT_FOUND:
1301 return NS_NOTFOUND;
1302 case TRY_AGAIN:
1303 return NS_TRYAGAIN;
1304 default:
1305 return NS_UNAVAIL;
1306 }
1307 *((struct hostent **)rv) = hp;
1308 return NS_SUCCESS;
1309}
1310
1311/*ARGSUSED*/
Jim Huange5c35e02010-09-27 23:37:10 +08001312static int
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001313_dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1314{
1315 char qbuf[MAXDNAME + 1], *qp, *ep;
1316 int n;
1317 querybuf *buf;
1318 struct hostent *hp;
1319 const unsigned char *uaddr;
1320 int len, af, advance;
1321 res_state res;
Szymon Jakubczakea9bf672014-02-14 17:07:23 -05001322 unsigned netid, mark;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001323 res_static rs = __res_get_static();
1324
1325 assert(rv != NULL);
1326
1327 uaddr = va_arg(ap, unsigned char *);
1328 len = va_arg(ap, int);
1329 af = va_arg(ap, int);
Szymon Jakubczakea9bf672014-02-14 17:07:23 -05001330 netid = va_arg(ap, unsigned);
1331 mark = va_arg(ap, unsigned);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001332
1333 switch (af) {
1334 case AF_INET:
1335 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1336 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1337 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1338 break;
1339
1340 case AF_INET6:
1341 qp = qbuf;
1342 ep = qbuf + sizeof(qbuf) - 1;
1343 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1344 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
1345 uaddr[n] & 0xf,
1346 ((unsigned int)uaddr[n] >> 4) & 0xf);
1347 if (advance > 0 && qp + advance < ep)
1348 qp += advance;
1349 else {
1350 h_errno = NETDB_INTERNAL;
1351 return NS_NOTFOUND;
1352 }
1353 }
1354 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
1355 h_errno = NETDB_INTERNAL;
1356 return NS_NOTFOUND;
1357 }
1358 break;
1359 default:
1360 abort();
1361 }
1362
1363 buf = malloc(sizeof(*buf));
1364 if (buf == NULL) {
1365 h_errno = NETDB_INTERNAL;
1366 return NS_NOTFOUND;
1367 }
1368 res = __res_get_state();
1369 if (res == NULL) {
1370 free(buf);
1371 return NS_NOTFOUND;
1372 }
Szymon Jakubczakea9bf672014-02-14 17:07:23 -05001373 res_setnetid(res, netid);
Chad Brubakerc39214e2013-06-20 10:36:56 -07001374 res_setmark(res, mark);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001375 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
1376 if (n < 0) {
1377 free(buf);
1378 dprintf("res_nquery failed (%d)\n", res, n);
1379 __res_put_state(res);
1380 return NS_NOTFOUND;
1381 }
1382 hp = getanswer(buf, n, qbuf, T_PTR, res);
1383 free(buf);
1384 if (hp == NULL) {
1385 __res_put_state(res);
1386 switch (h_errno) {
1387 case HOST_NOT_FOUND:
1388 return NS_NOTFOUND;
1389 case TRY_AGAIN:
1390 return NS_TRYAGAIN;
1391 default:
1392 return NS_UNAVAIL;
1393 }
1394 }
1395 hp->h_addrtype = af;
1396 hp->h_length = len;
1397 (void)memcpy(rs->host_addr, uaddr, (size_t)len);
1398 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
1399 rs->h_addr_ptrs[1] = NULL;
1400 if (af == AF_INET && (res->options & RES_USE_INET6)) {
1401 map_v4v6_address((char *)(void *)rs->host_addr,
1402 (char *)(void *)rs->host_addr);
1403 hp->h_addrtype = AF_INET6;
1404 hp->h_length = IN6ADDRSZ;
1405 }
1406
1407 __res_put_state(res);
1408 *((struct hostent **)rv) = hp;
1409 h_errno = NETDB_SUCCESS;
1410 return NS_SUCCESS;
1411}