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