1 /* $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>
59 #include <sys/un.h>
60 #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>
71 #include <strings.h>
72 #include <syslog.h>
73 #include <unistd.h>
74
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
85 // This should be synchronized to ResponseCode.h
86 static const int DnsProxyQueryResult = 222;
87
88 static const char const AskedForGot[] =
89 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
90
91 #define MAXPACKET (64*1024)
92
93 typedef union {
94 HEADER hdr;
95 u_char buf[MAXPACKET];
96 } querybuf;
97
98 typedef union {
99 int32_t al;
100 char ac;
101 } align;
102
103 #ifdef DEBUG
104 static void dprintf(const char *, res_state, ...)
105 __attribute__((__format__(__printf__, 1, 3)));
106 #endif
107 static struct hostent *getanswer(const querybuf *, int, const char *, int,
108 res_state);
109 static void map_v4v6_address(const char *, char *);
110 static void map_v4v6_hostent(struct hostent *, char **, char *);
111 static void addrsort(char **, int, res_state);
112
113 static void _sethtent(int);
114 static void _endhtent(void);
115 static struct hostent *_gethtent(void);
116 void ht_sethostent(int);
117 void ht_endhostent(void);
118 struct hostent *ht_gethostbyname(char *);
119 struct hostent *ht_gethostbyaddr(const char *, int, int);
120 void dns_service(void);
121 #undef dn_skipname
122 int dn_skipname(const u_char *, const u_char *);
123 static int _gethtbyaddr(void *, void *, va_list);
124 static int _gethtbyname(void *, void *, va_list);
125 static struct hostent *_gethtbyname2(const char *, int);
126 static int _dns_gethtbyaddr(void *, void *, va_list);
127 static int _dns_gethtbyname(void *, void *, va_list);
128
129 static struct hostent *gethostbyname_internal(const char *, int, res_state, const char *);
130
131 static 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
139 static void
dprintf(const char * msg,res_state res,...)140 dprintf(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
176 static struct hostent *
getanswer(const querybuf * answer,int anslen,const char * qname,int qtype,res_state res)177 getanswer(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
470 int
gethostbyname_r(const char * name,struct hostent * hp,char * buf,size_t buflen,struct hostent ** result,int * errorp)471 gethostbyname_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
487 struct hostent *
gethostbyname(const char * name)488 gethostbyname(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
498 /* try IPv6 first - if that fails do IPv4 */
499 if (res->options & RES_USE_INET6) {
500 hp = gethostbyname_internal(name, AF_INET6, res, NULL);
501 if (hp) {
502 __res_put_state(res);
503 return hp;
504 }
505 }
506 hp = gethostbyname_internal(name, AF_INET, res, NULL);
507 __res_put_state(res);
508 return hp;
509 }
510
511 struct hostent *
gethostbyname2(const char * name,int af)512 gethostbyname2(const char *name, int af)
513 {
514 return android_gethostbynameforiface(name, af, NULL);
515 }
516
517 struct hostent *
android_gethostbynameforiface(const char * name,int af,const char * iface)518 android_gethostbynameforiface(const char *name, int af, const char *iface)
519 {
520 struct hostent *hp;
521 res_state res = __res_get_state();
522
523 if (res == NULL)
524 return NULL;
525 hp = gethostbyname_internal(name, af, res, iface);
526 __res_put_state(res);
527 return hp;
528 }
529
530
android_open_proxy()531 static 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
556 static struct hostent *
android_read_hostent(FILE * proxy)557 android_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
620 static struct hostent *
gethostbyname_internal_real(const char * name,int af,res_state res)621 gethostbyname_internal_real(const char *name, int af, res_state res)
622 {
623 const char *cp;
624 char *bp, *ep;
625 int size;
626 struct hostent *hp;
627 res_static rs = __res_get_static();
628
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;
736 }
737 h_errno = NETDB_SUCCESS;
738 return hp;
739 }
740
741
742 // very similar in proxy-ness to android_getaddrinfo_proxy
743 static struct hostent *
gethostbyname_internal(const char * name,int af,res_state res,const char * iface)744 gethostbyname_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 if (proxy == NULL) goto exit;
757
758 /* This is writing to system/netd/DnsProxyListener.cpp and changes
759 * here need to be matched there */
760 if (fprintf(proxy, "gethostbyname %s %s %d",
761 iface == NULL ? "^" : iface,
762 name == NULL ? "^" : name,
763 af) < 0) {
764 goto exit;
765 }
766
767 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
768 goto exit;
769 }
770
771 result = android_read_hostent(proxy);
772
773 exit:
774 if (proxy != NULL) {
775 fclose(proxy);
776 }
777 return result;
778 }
779
780
781 struct hostent *
android_gethostbyaddrforiface_proxy(const void * addr,socklen_t len,int af,const char * iface)782 android_gethostbyaddrforiface_proxy(const void *addr,
783 socklen_t len, int af, const char* iface)
784 {
785 struct hostent *result = NULL;
786 FILE* proxy = android_open_proxy();
787
788 if (proxy == NULL) goto exit;
789
790 char buf[INET6_ADDRSTRLEN]; //big enough for IPv4 and IPv6
791 const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf));
792 if (addrStr == NULL) goto exit;
793
794 if (fprintf(proxy, "gethostbyaddr %s %d %d %s",
795 addrStr, len, af, iface == NULL ? "^" : iface) < 0) {
796 goto exit;
797 }
798
799 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
800 goto exit;
801 }
802
803 result = android_read_hostent(proxy);
804 exit:
805 if (proxy != NULL) {
806 fclose(proxy);
807 }
808 return result;
809 }
810
811 struct hostent *
android_gethostbyaddrforiface_real(const void * addr,socklen_t len,int af,const char * iface)812 android_gethostbyaddrforiface_real(const void *addr,
813 socklen_t len, int af, const char* iface)
814 {
815 const u_char *uaddr = (const u_char *)addr;
816 socklen_t size;
817 struct hostent *hp;
818 static const ns_dtab dtab[] = {
819 NS_FILES_CB(_gethtbyaddr, NULL)
820 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
821 { 0, 0, 0 }
822 };
823
824 assert(addr != NULL);
825
826 if (af == AF_INET6 && len == IN6ADDRSZ &&
827 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) ||
828 IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) {
829 h_errno = HOST_NOT_FOUND;
830 return NULL;
831 }
832 if (af == AF_INET6 && len == IN6ADDRSZ &&
833 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) ||
834 IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) {
835 /* Unmap. */
836 addr += IN6ADDRSZ - INADDRSZ;
837 uaddr += IN6ADDRSZ - INADDRSZ;
838 af = AF_INET;
839 len = INADDRSZ;
840 }
841 switch (af) {
842 case AF_INET:
843 size = INADDRSZ;
844 break;
845 case AF_INET6:
846 size = IN6ADDRSZ;
847 break;
848 default:
849 errno = EAFNOSUPPORT;
850 h_errno = NETDB_INTERNAL;
851 return NULL;
852 }
853 if (size != len) {
854 errno = EINVAL;
855 h_errno = NETDB_INTERNAL;
856 return NULL;
857 }
858 hp = NULL;
859 h_errno = NETDB_INTERNAL;
860 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
861 default_dns_files, uaddr, len, af, iface) != NS_SUCCESS)
862 return NULL;
863 h_errno = NETDB_SUCCESS;
864 return hp;
865 }
866
867 struct hostent *
android_gethostbyaddrforiface(const void * addr,socklen_t len,int af,const char * iface)868 android_gethostbyaddrforiface(const void *addr, socklen_t len, int af, const char* iface)
869 {
870 const char *cache_mode = getenv("ANDROID_DNS_MODE");
871
872 if (cache_mode == NULL || strcmp(cache_mode, "local") != 0) {
873 return android_gethostbyaddrforiface_proxy(addr, len, af, iface);
874 } else {
875 return android_gethostbyaddrforiface_real(addr,len, af,iface);
876 }
877 }
878
879 struct hostent *
gethostbyaddr(const void * addr,socklen_t len,int af)880 gethostbyaddr(const void *addr, socklen_t len, int af)
881 {
882 return android_gethostbyaddrforiface(addr, len, af, NULL);
883 }
884
885
886 static void
_sethtent(int f)887 _sethtent(int f)
888 {
889 res_static rs = __res_get_static();
890 if (rs == NULL) return;
891 if (!rs->hostf)
892 rs->hostf = fopen(_PATH_HOSTS, "r" );
893 else
894 rewind(rs->hostf);
895 rs->stayopen = f;
896 }
897
898 static void
_endhtent(void)899 _endhtent(void)
900 {
901 res_static rs = __res_get_static();
902 if (rs == NULL) return;
903
904 if (rs->hostf && !rs->stayopen) {
905 (void) fclose(rs->hostf);
906 rs->hostf = NULL;
907 }
908 }
909
910 static struct hostent *
_gethtent(void)911 _gethtent(void)
912 {
913 char *p;
914 char *cp, **q;
915 int af, len;
916 res_static rs = __res_get_static();
917
918 if (!rs->hostf && !(rs->hostf = fopen(_PATH_HOSTS, "r" ))) {
919 h_errno = NETDB_INTERNAL;
920 return NULL;
921 }
922 again:
923 if (!(p = fgets(rs->hostbuf, sizeof rs->hostbuf, rs->hostf))) {
924 h_errno = HOST_NOT_FOUND;
925 return NULL;
926 }
927 if (*p == '#')
928 goto again;
929 if (!(cp = strpbrk(p, "#\n")))
930 goto again;
931 *cp = '\0';
932 if (!(cp = strpbrk(p, " \t")))
933 goto again;
934 *cp++ = '\0';
935 if (inet_pton(AF_INET6, p, (char *)(void *)rs->host_addr) > 0) {
936 af = AF_INET6;
937 len = IN6ADDRSZ;
938 } else if (inet_pton(AF_INET, p, (char *)(void *)rs->host_addr) > 0) {
939 res_state res = __res_get_state();
940 if (res == NULL)
941 return NULL;
942 if (res->options & RES_USE_INET6) {
943 map_v4v6_address((char *)(void *)rs->host_addr,
944 (char *)(void *)rs->host_addr);
945 af = AF_INET6;
946 len = IN6ADDRSZ;
947 } else {
948 af = AF_INET;
949 len = INADDRSZ;
950 }
951 __res_put_state(res);
952 } else {
953 goto again;
954 }
955 /* if this is not something we're looking for, skip it. */
956 if (rs->host.h_addrtype != 0 && rs->host.h_addrtype != af)
957 goto again;
958 if (rs->host.h_length != 0 && rs->host.h_length != len)
959 goto again;
960 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
961 rs->h_addr_ptrs[1] = NULL;
962 rs->host.h_addr_list = rs->h_addr_ptrs;
963 rs->host.h_length = len;
964 rs->host.h_addrtype = af;
965 while (*cp == ' ' || *cp == '\t')
966 cp++;
967 rs->host.h_name = cp;
968 q = rs->host.h_aliases = rs->host_aliases;
969 if ((cp = strpbrk(cp, " \t")) != NULL)
970 *cp++ = '\0';
971 while (cp && *cp) {
972 if (*cp == ' ' || *cp == '\t') {
973 cp++;
974 continue;
975 }
976 if (q < &rs->host_aliases[MAXALIASES - 1])
977 *q++ = cp;
978 if ((cp = strpbrk(cp, " \t")) != NULL)
979 *cp++ = '\0';
980 }
981 *q = NULL;
982 h_errno = NETDB_SUCCESS;
983 return &rs->host;
984 }
985
986 /*ARGSUSED*/
987 int
_gethtbyname(void * rv,void * cb_data,va_list ap)988 _gethtbyname(void *rv, void *cb_data, va_list ap)
989 {
990 struct hostent *hp;
991 const char *name;
992 int af;
993
994 assert(rv != NULL);
995
996 name = va_arg(ap, char *);
997 /* NOSTRICT skip len */(void)va_arg(ap, int);
998 af = va_arg(ap, int);
999
1000 hp = NULL;
1001 #if 0
1002 {
1003 res_state res = __res_get_state();
1004 if (res == NULL)
1005 return NS_NOTFOUND;
1006 if (res->options & RES_USE_INET6)
1007 hp = _gethtbyname2(name, AF_INET6);
1008 if (hp==NULL)
1009 hp = _gethtbyname2(name, AF_INET);
1010 __res_put_state(res);
1011 }
1012 #else
1013 hp = _gethtbyname2(name, af);
1014 #endif
1015 *((struct hostent **)rv) = hp;
1016 if (hp == NULL) {
1017 h_errno = HOST_NOT_FOUND;
1018 return NS_NOTFOUND;
1019 }
1020 return NS_SUCCESS;
1021 }
1022
1023 static struct hostent *
_gethtbyname2(const char * name,int af)1024 _gethtbyname2(const char *name, int af)
1025 {
1026 struct hostent *p;
1027 char *tmpbuf, *ptr, **cp;
1028 int num;
1029 size_t len;
1030 res_static rs = __res_get_static();
1031
1032 assert(name != NULL);
1033
1034 _sethtent(rs->stayopen);
1035 ptr = tmpbuf = NULL;
1036 num = 0;
1037 while ((p = _gethtent()) != NULL && num < MAXADDRS) {
1038 if (p->h_addrtype != af)
1039 continue;
1040 if (strcasecmp(p->h_name, name) != 0) {
1041 for (cp = p->h_aliases; *cp != NULL; cp++)
1042 if (strcasecmp(*cp, name) == 0)
1043 break;
1044 if (*cp == NULL) continue;
1045 }
1046
1047 if (num == 0) {
1048 size_t bufsize;
1049 char *src;
1050
1051 bufsize = strlen(p->h_name) + 2 +
1052 MAXADDRS * p->h_length +
1053 ALIGNBYTES;
1054 for (cp = p->h_aliases; *cp != NULL; cp++)
1055 bufsize += strlen(*cp) + 1;
1056
1057 if ((tmpbuf = malloc(bufsize)) == NULL) {
1058 h_errno = NETDB_INTERNAL;
1059 return NULL;
1060 }
1061
1062 ptr = tmpbuf;
1063 src = p->h_name;
1064 while ((*ptr++ = *src++) != '\0');
1065 for (cp = p->h_aliases; *cp != NULL; cp++) {
1066 src = *cp;
1067 while ((*ptr++ = *src++) != '\0');
1068 }
1069 *ptr++ = '\0';
1070
1071 ptr = (char *)(void *)ALIGN(ptr);
1072 }
1073
1074 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
1075 ptr += p->h_length;
1076 num++;
1077 }
1078 _endhtent();
1079 if (num == 0) return NULL;
1080
1081 len = ptr - tmpbuf;
1082 if (len > (sizeof(rs->hostbuf) - ALIGNBYTES)) {
1083 free(tmpbuf);
1084 errno = ENOSPC;
1085 h_errno = NETDB_INTERNAL;
1086 return NULL;
1087 }
1088 ptr = memcpy((void *)ALIGN(rs->hostbuf), tmpbuf, len);
1089 free(tmpbuf);
1090
1091 rs->host.h_name = ptr;
1092 while (*ptr++);
1093
1094 cp = rs->host_aliases;
1095 while (*ptr) {
1096 *cp++ = ptr;
1097 while (*ptr++);
1098 }
1099 ptr++;
1100 *cp = NULL;
1101
1102 ptr = (char *)(void *)ALIGN(ptr);
1103 cp = rs->h_addr_ptrs;
1104 while (num--) {
1105 *cp++ = ptr;
1106 ptr += rs->host.h_length;
1107 }
1108 *cp = NULL;
1109
1110 return &rs->host;
1111 }
1112
1113 /*ARGSUSED*/
1114 static int
_gethtbyaddr(void * rv,void * cb_data,va_list ap)1115 _gethtbyaddr(void *rv, void *cb_data, va_list ap)
1116 {
1117 struct hostent *p;
1118 const unsigned char *addr;
1119 int len, af;
1120 res_static rs = __res_get_static();
1121
1122 assert(rv != NULL);
1123
1124 addr = va_arg(ap, unsigned char *);
1125 len = va_arg(ap, int);
1126 af = va_arg(ap, int);
1127
1128 rs->host.h_length = len;
1129 rs->host.h_addrtype = af;
1130
1131 _sethtent(rs->stayopen);
1132 while ((p = _gethtent()) != NULL)
1133 if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
1134 (size_t)len))
1135 break;
1136 _endhtent();
1137 *((struct hostent **)rv) = p;
1138 if (p==NULL) {
1139 h_errno = HOST_NOT_FOUND;
1140 return NS_NOTFOUND;
1141 }
1142 return NS_SUCCESS;
1143 }
1144
1145 static void
map_v4v6_address(const char * src,char * dst)1146 map_v4v6_address(const char *src, char *dst)
1147 {
1148 u_char *p = (u_char *)dst;
1149 char tmp[INADDRSZ];
1150 int i;
1151
1152 assert(src != NULL);
1153 assert(dst != NULL);
1154
1155 /* Stash a temporary copy so our caller can update in place. */
1156 (void)memcpy(tmp, src, INADDRSZ);
1157 /* Mark this ipv6 addr as a mapped ipv4. */
1158 for (i = 0; i < 10; i++)
1159 *p++ = 0x00;
1160 *p++ = 0xff;
1161 *p++ = 0xff;
1162 /* Retrieve the saved copy and we're done. */
1163 (void)memcpy((void *)p, tmp, INADDRSZ);
1164 }
1165
1166 static void
map_v4v6_hostent(struct hostent * hp,char ** bpp,char * ep)1167 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
1168 {
1169 char **ap;
1170
1171 assert(hp != NULL);
1172 assert(bpp != NULL);
1173 assert(ep != NULL);
1174
1175 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1176 return;
1177 hp->h_addrtype = AF_INET6;
1178 hp->h_length = IN6ADDRSZ;
1179 for (ap = hp->h_addr_list; *ap; ap++) {
1180 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
1181
1182 if (ep - *bpp < (i + IN6ADDRSZ)) {
1183 /* Out of memory. Truncate address list here. XXX */
1184 *ap = NULL;
1185 return;
1186 }
1187 *bpp += i;
1188 map_v4v6_address(*ap, *bpp);
1189 *ap = *bpp;
1190 *bpp += IN6ADDRSZ;
1191 }
1192 }
1193
1194 static void
addrsort(char ** ap,int num,res_state res)1195 addrsort(char **ap, int num, res_state res)
1196 {
1197 int i, j;
1198 char **p;
1199 short aval[MAXADDRS];
1200 int needsort = 0;
1201
1202 assert(ap != NULL);
1203
1204 p = ap;
1205 for (i = 0; i < num; i++, p++) {
1206 for (j = 0 ; (unsigned)j < res->nsort; j++)
1207 if (res->sort_list[j].addr.s_addr ==
1208 (((struct in_addr *)(void *)(*p))->s_addr &
1209 res->sort_list[j].mask))
1210 break;
1211 aval[i] = j;
1212 if (needsort == 0 && i > 0 && j < aval[i-1])
1213 needsort = i;
1214 }
1215 if (!needsort)
1216 return;
1217
1218 while (needsort < num) {
1219 for (j = needsort - 1; j >= 0; j--) {
1220 if (aval[j] > aval[j+1]) {
1221 char *hp;
1222
1223 i = aval[j];
1224 aval[j] = aval[j+1];
1225 aval[j+1] = i;
1226
1227 hp = ap[j];
1228 ap[j] = ap[j+1];
1229 ap[j+1] = hp;
1230 } else
1231 break;
1232 }
1233 needsort++;
1234 }
1235 }
1236
1237 struct hostent *
gethostent(void)1238 gethostent(void)
1239 {
1240 res_static rs = __res_get_static();
1241 rs->host.h_addrtype = 0;
1242 rs->host.h_length = 0;
1243 return _gethtent();
1244 }
1245
1246 /*ARGSUSED*/
1247 static int
_dns_gethtbyname(void * rv,void * cb_data,va_list ap)1248 _dns_gethtbyname(void *rv, void *cb_data, va_list ap)
1249 {
1250 querybuf *buf;
1251 int n, type;
1252 struct hostent *hp;
1253 const char *name;
1254 int af;
1255 res_state res;
1256
1257 assert(rv != NULL);
1258
1259 name = va_arg(ap, char *);
1260 /* NOSTRICT skip len */(void)va_arg(ap, int);
1261 af = va_arg(ap, int);
1262
1263 switch (af) {
1264 case AF_INET:
1265 type = T_A;
1266 break;
1267 case AF_INET6:
1268 type = T_AAAA;
1269 break;
1270 default:
1271 return NS_UNAVAIL;
1272 }
1273 buf = malloc(sizeof(*buf));
1274 if (buf == NULL) {
1275 h_errno = NETDB_INTERNAL;
1276 return NS_NOTFOUND;
1277 }
1278 res = __res_get_state();
1279 if (res == NULL) {
1280 free(buf);
1281 return NS_NOTFOUND;
1282 }
1283 n = res_nsearch(res, name, C_IN, type, buf->buf, sizeof(buf->buf));
1284 if (n < 0) {
1285 free(buf);
1286 dprintf("res_nsearch failed (%d)\n", res, n);
1287 __res_put_state(res);
1288 return NS_NOTFOUND;
1289 }
1290 hp = getanswer(buf, n, name, type, res);
1291 free(buf);
1292 __res_put_state(res);
1293 if (hp == NULL)
1294 switch (h_errno) {
1295 case HOST_NOT_FOUND:
1296 return NS_NOTFOUND;
1297 case TRY_AGAIN:
1298 return NS_TRYAGAIN;
1299 default:
1300 return NS_UNAVAIL;
1301 }
1302 *((struct hostent **)rv) = hp;
1303 return NS_SUCCESS;
1304 }
1305
1306 /*ARGSUSED*/
1307 static int
_dns_gethtbyaddr(void * rv,void * cb_data,va_list ap)1308 _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1309 {
1310 char qbuf[MAXDNAME + 1], *qp, *ep;
1311 int n;
1312 querybuf *buf;
1313 struct hostent *hp;
1314 const unsigned char *uaddr;
1315 int len, af, advance;
1316 res_state res;
1317 const char* iface;
1318 res_static rs = __res_get_static();
1319
1320 assert(rv != NULL);
1321
1322 uaddr = va_arg(ap, unsigned char *);
1323 len = va_arg(ap, int);
1324 af = va_arg(ap, int);
1325 iface = va_arg(ap, char *);
1326
1327 switch (af) {
1328 case AF_INET:
1329 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1330 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1331 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1332 break;
1333
1334 case AF_INET6:
1335 qp = qbuf;
1336 ep = qbuf + sizeof(qbuf) - 1;
1337 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1338 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
1339 uaddr[n] & 0xf,
1340 ((unsigned int)uaddr[n] >> 4) & 0xf);
1341 if (advance > 0 && qp + advance < ep)
1342 qp += advance;
1343 else {
1344 h_errno = NETDB_INTERNAL;
1345 return NS_NOTFOUND;
1346 }
1347 }
1348 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
1349 h_errno = NETDB_INTERNAL;
1350 return NS_NOTFOUND;
1351 }
1352 break;
1353 default:
1354 abort();
1355 }
1356
1357 buf = malloc(sizeof(*buf));
1358 if (buf == NULL) {
1359 h_errno = NETDB_INTERNAL;
1360 return NS_NOTFOUND;
1361 }
1362 res = __res_get_state();
1363 if (res == NULL) {
1364 free(buf);
1365 return NS_NOTFOUND;
1366 }
1367 res_setiface(res, iface);
1368 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
1369 if (n < 0) {
1370 free(buf);
1371 dprintf("res_nquery failed (%d)\n", res, n);
1372 __res_put_state(res);
1373 return NS_NOTFOUND;
1374 }
1375 hp = getanswer(buf, n, qbuf, T_PTR, res);
1376 free(buf);
1377 if (hp == NULL) {
1378 __res_put_state(res);
1379 switch (h_errno) {
1380 case HOST_NOT_FOUND:
1381 return NS_NOTFOUND;
1382 case TRY_AGAIN:
1383 return NS_TRYAGAIN;
1384 default:
1385 return NS_UNAVAIL;
1386 }
1387 }
1388 hp->h_addrtype = af;
1389 hp->h_length = len;
1390 (void)memcpy(rs->host_addr, uaddr, (size_t)len);
1391 rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr;
1392 rs->h_addr_ptrs[1] = NULL;
1393 if (af == AF_INET && (res->options & RES_USE_INET6)) {
1394 map_v4v6_address((char *)(void *)rs->host_addr,
1395 (char *)(void *)rs->host_addr);
1396 hp->h_addrtype = AF_INET6;
1397 hp->h_length = IN6ADDRSZ;
1398 }
1399
1400 __res_put_state(res);
1401 *((struct hostent **)rv) = hp;
1402 h_errno = NETDB_SUCCESS;
1403 return NS_SUCCESS;
1404 }
1405