The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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> |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 59 | #include <sys/un.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 60 | #include <netinet/in.h> |
| 61 | #include <arpa/inet.h> |
Calin Juravle | 569fb98 | 2014-03-04 15:01:29 +0000 | [diff] [blame] | 62 | #include <arpa/nameser.h> |
Paul Jensen | 5240b56 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 63 | #include "NetdClientDispatch.h" |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 64 | #include "resolv_netid.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 65 | #include "resolv_private.h" |
| 66 | #include "resolv_cache.h" |
| 67 | #include <assert.h> |
| 68 | #include <ctype.h> |
| 69 | #include <errno.h> |
| 70 | #include <netdb.h> |
| 71 | #include <stdarg.h> |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 72 | #include <stdbool.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 73 | #include <stdio.h> |
Carl Shapiro | 2cc2b2b | 2011-03-21 20:01:03 -0700 | [diff] [blame] | 74 | #include <strings.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 75 | #include <syslog.h> |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 76 | #include <unistd.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 77 | |
Calin Juravle | c20de90 | 2014-03-20 15:21:32 +0000 | [diff] [blame] | 78 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
| 79 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) |
| 80 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 81 | #ifndef LOG_AUTH |
| 82 | # define LOG_AUTH 0 |
| 83 | #endif |
| 84 | |
| 85 | #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ |
| 86 | |
| 87 | #include "nsswitch.h" |
| 88 | #include <stdlib.h> |
| 89 | #include <string.h> |
| 90 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 91 | // This should be synchronized to ResponseCode.h |
| 92 | static const int DnsProxyQueryResult = 222; |
| 93 | |
Elliott Hughes | 68c2755 | 2014-07-07 09:44:17 -0700 | [diff] [blame] | 94 | static const char AskedForGot[] = |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 95 | "gethostby*.getanswer: asked for \"%s\", got \"%s\""; |
| 96 | |
| 97 | #define MAXPACKET (64*1024) |
| 98 | |
| 99 | typedef union { |
| 100 | HEADER hdr; |
| 101 | u_char buf[MAXPACKET]; |
| 102 | } querybuf; |
| 103 | |
| 104 | typedef union { |
| 105 | int32_t al; |
| 106 | char ac; |
| 107 | } align; |
| 108 | |
| 109 | #ifdef DEBUG |
| 110 | static void dprintf(const char *, res_state, ...) |
| 111 | __attribute__((__format__(__printf__, 1, 3))); |
| 112 | #endif |
| 113 | static struct hostent *getanswer(const querybuf *, int, const char *, int, |
| 114 | res_state); |
| 115 | static void map_v4v6_address(const char *, char *); |
| 116 | static void map_v4v6_hostent(struct hostent *, char **, char *); |
| 117 | static void addrsort(char **, int, res_state); |
| 118 | |
Jim Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 119 | static void _sethtent(int); |
| 120 | static void _endhtent(void); |
| 121 | static struct hostent *_gethtent(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | void ht_sethostent(int); |
| 123 | void ht_endhostent(void); |
| 124 | struct hostent *ht_gethostbyname(char *); |
| 125 | struct hostent *ht_gethostbyaddr(const char *, int, int); |
| 126 | void dns_service(void); |
| 127 | #undef dn_skipname |
| 128 | int dn_skipname(const u_char *, const u_char *); |
Jim Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 129 | static int _gethtbyaddr(void *, void *, va_list); |
| 130 | static int _gethtbyname(void *, void *, va_list); |
| 131 | static struct hostent *_gethtbyname2(const char *, int); |
| 132 | static int _dns_gethtbyaddr(void *, void *, va_list); |
| 133 | static int _dns_gethtbyname(void *, void *, va_list); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 134 | |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 135 | static struct hostent *gethostbyname_internal(const char *, int, res_state, unsigned, unsigned); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 136 | |
| 137 | static const ns_src default_dns_files[] = { |
| 138 | { NSSRC_FILES, NS_SUCCESS }, |
| 139 | { NSSRC_DNS, NS_SUCCESS }, |
| 140 | { 0, 0 } |
| 141 | }; |
| 142 | |
| 143 | |
| 144 | #ifdef DEBUG |
| 145 | static void |
| 146 | dprintf(const char *msg, res_state res, ...) |
| 147 | { |
| 148 | assert(msg != NULL); |
| 149 | |
| 150 | if (res->options & RES_DEBUG) { |
| 151 | int save = errno; |
| 152 | va_list ap; |
| 153 | |
| 154 | va_start (ap, res); |
| 155 | vprintf(msg, ap); |
| 156 | va_end (ap); |
| 157 | |
| 158 | errno = save; |
| 159 | } |
| 160 | } |
| 161 | #else |
| 162 | # define dprintf(msg, res, num) ((void)0) /*nada*/ |
| 163 | #endif |
| 164 | |
| 165 | #define BOUNDED_INCR(x) \ |
| 166 | do { \ |
| 167 | cp += (x); \ |
| 168 | if (cp > eom) { \ |
| 169 | h_errno = NO_RECOVERY; \ |
| 170 | return NULL; \ |
| 171 | } \ |
| 172 | } while (/*CONSTCOND*/0) |
| 173 | |
| 174 | #define BOUNDS_CHECK(ptr, count) \ |
| 175 | do { \ |
| 176 | if ((ptr) + (count) > eom) { \ |
| 177 | h_errno = NO_RECOVERY; \ |
| 178 | return NULL; \ |
| 179 | } \ |
| 180 | } while (/*CONSTCOND*/0) |
| 181 | |
| 182 | static struct hostent * |
| 183 | getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, |
| 184 | res_state res) |
| 185 | { |
| 186 | const HEADER *hp; |
| 187 | const u_char *cp; |
| 188 | int n; |
| 189 | const u_char *eom, *erdata; |
| 190 | char *bp, **ap, **hap, *ep; |
| 191 | int type, class, ancount, qdcount; |
| 192 | int haveanswer, had_error; |
| 193 | int toobig = 0; |
| 194 | char tbuf[MAXDNAME]; |
| 195 | const char *tname; |
| 196 | int (*name_ok)(const char *); |
| 197 | res_static rs = __res_get_static(); |
| 198 | |
| 199 | assert(answer != NULL); |
| 200 | assert(qname != NULL); |
| 201 | |
| 202 | tname = qname; |
| 203 | rs->host.h_name = NULL; |
| 204 | eom = answer->buf + anslen; |
| 205 | switch (qtype) { |
| 206 | case T_A: |
| 207 | case T_AAAA: |
| 208 | name_ok = res_hnok; |
| 209 | break; |
| 210 | case T_PTR: |
| 211 | name_ok = res_dnok; |
| 212 | break; |
| 213 | default: |
| 214 | return NULL; /* XXX should be abort(); */ |
| 215 | } |
| 216 | /* |
| 217 | * find first satisfactory answer |
| 218 | */ |
| 219 | hp = &answer->hdr; |
| 220 | ancount = ntohs(hp->ancount); |
| 221 | qdcount = ntohs(hp->qdcount); |
| 222 | bp = rs->hostbuf; |
| 223 | ep = rs->hostbuf + sizeof rs->hostbuf; |
| 224 | cp = answer->buf; |
| 225 | BOUNDED_INCR(HFIXEDSZ); |
| 226 | if (qdcount != 1) { |
| 227 | h_errno = NO_RECOVERY; |
| 228 | return NULL; |
| 229 | } |
| 230 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 231 | if ((n < 0) || !(*name_ok)(bp)) { |
| 232 | h_errno = NO_RECOVERY; |
| 233 | return NULL; |
| 234 | } |
| 235 | BOUNDED_INCR(n + QFIXEDSZ); |
| 236 | if (qtype == T_A || qtype == T_AAAA) { |
| 237 | /* res_send() has already verified that the query name is the |
| 238 | * same as the one we sent; this just gets the expanded name |
| 239 | * (i.e., with the succeeding search-domain tacked on). |
| 240 | */ |
| 241 | n = strlen(bp) + 1; /* for the \0 */ |
| 242 | if (n >= MAXHOSTNAMELEN) { |
| 243 | h_errno = NO_RECOVERY; |
| 244 | return NULL; |
| 245 | } |
| 246 | rs->host.h_name = bp; |
| 247 | bp += n; |
| 248 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 249 | qname = rs->host.h_name; |
| 250 | } |
| 251 | ap = rs->host_aliases; |
| 252 | *ap = NULL; |
| 253 | rs->host.h_aliases = rs->host_aliases; |
| 254 | hap = rs->h_addr_ptrs; |
| 255 | *hap = NULL; |
| 256 | rs->host.h_addr_list = rs->h_addr_ptrs; |
| 257 | haveanswer = 0; |
| 258 | had_error = 0; |
| 259 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 260 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 261 | if ((n < 0) || !(*name_ok)(bp)) { |
| 262 | had_error++; |
| 263 | continue; |
| 264 | } |
| 265 | cp += n; /* name */ |
| 266 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
| 267 | type = _getshort(cp); |
| 268 | cp += INT16SZ; /* type */ |
| 269 | class = _getshort(cp); |
| 270 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
| 271 | n = _getshort(cp); |
| 272 | cp += INT16SZ; /* len */ |
| 273 | BOUNDS_CHECK(cp, n); |
| 274 | erdata = cp + n; |
| 275 | if (class != C_IN) { |
| 276 | /* XXX - debug? syslog? */ |
| 277 | cp += n; |
| 278 | continue; /* XXX - had_error++ ? */ |
| 279 | } |
| 280 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
| 281 | if (ap >= &rs->host_aliases[MAXALIASES-1]) |
| 282 | continue; |
| 283 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 284 | if ((n < 0) || !(*name_ok)(tbuf)) { |
| 285 | had_error++; |
| 286 | continue; |
| 287 | } |
| 288 | cp += n; |
| 289 | if (cp != erdata) { |
| 290 | h_errno = NO_RECOVERY; |
| 291 | return NULL; |
| 292 | } |
| 293 | /* Store alias. */ |
| 294 | *ap++ = bp; |
| 295 | n = strlen(bp) + 1; /* for the \0 */ |
| 296 | if (n >= MAXHOSTNAMELEN) { |
| 297 | had_error++; |
| 298 | continue; |
| 299 | } |
| 300 | bp += n; |
| 301 | /* Get canonical name. */ |
| 302 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 303 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 304 | had_error++; |
| 305 | continue; |
| 306 | } |
| 307 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 308 | rs->host.h_name = bp; |
| 309 | bp += n; |
| 310 | continue; |
| 311 | } |
| 312 | if (qtype == T_PTR && type == T_CNAME) { |
| 313 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 314 | if (n < 0 || !res_dnok(tbuf)) { |
| 315 | had_error++; |
| 316 | continue; |
| 317 | } |
| 318 | cp += n; |
| 319 | if (cp != erdata) { |
| 320 | h_errno = NO_RECOVERY; |
| 321 | return NULL; |
| 322 | } |
| 323 | /* Get canonical name. */ |
| 324 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 325 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 326 | had_error++; |
| 327 | continue; |
| 328 | } |
| 329 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 330 | tname = bp; |
| 331 | bp += n; |
| 332 | continue; |
| 333 | } |
| 334 | if (type != qtype) { |
| 335 | if (type != T_KEY && type != T_SIG) |
| 336 | syslog(LOG_NOTICE|LOG_AUTH, |
| 337 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", |
| 338 | qname, p_class(C_IN), p_type(qtype), |
| 339 | p_type(type)); |
| 340 | cp += n; |
| 341 | continue; /* XXX - had_error++ ? */ |
| 342 | } |
| 343 | switch (type) { |
| 344 | case T_PTR: |
| 345 | if (strcasecmp(tname, bp) != 0) { |
| 346 | syslog(LOG_NOTICE|LOG_AUTH, |
| 347 | AskedForGot, qname, bp); |
| 348 | cp += n; |
| 349 | continue; /* XXX - had_error++ ? */ |
| 350 | } |
| 351 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 352 | if ((n < 0) || !res_hnok(bp)) { |
| 353 | had_error++; |
| 354 | break; |
| 355 | } |
| 356 | #if MULTI_PTRS_ARE_ALIASES |
| 357 | cp += n; |
| 358 | if (cp != erdata) { |
| 359 | h_errno = NO_RECOVERY; |
| 360 | return NULL; |
| 361 | } |
| 362 | if (!haveanswer) |
| 363 | rs->host.h_name = bp; |
| 364 | else if (ap < &rs->host_aliases[MAXALIASES-1]) |
| 365 | *ap++ = bp; |
| 366 | else |
| 367 | n = -1; |
| 368 | if (n != -1) { |
| 369 | n = strlen(bp) + 1; /* for the \0 */ |
| 370 | if (n >= MAXHOSTNAMELEN) { |
| 371 | had_error++; |
| 372 | break; |
| 373 | } |
| 374 | bp += n; |
| 375 | } |
| 376 | break; |
| 377 | #else |
| 378 | rs->host.h_name = bp; |
| 379 | if (res->options & RES_USE_INET6) { |
| 380 | n = strlen(bp) + 1; /* for the \0 */ |
| 381 | if (n >= MAXHOSTNAMELEN) { |
| 382 | had_error++; |
| 383 | break; |
| 384 | } |
| 385 | bp += n; |
| 386 | map_v4v6_hostent(&rs->host, &bp, ep); |
| 387 | } |
| 388 | h_errno = NETDB_SUCCESS; |
| 389 | return &rs->host; |
| 390 | #endif |
| 391 | case T_A: |
| 392 | case T_AAAA: |
| 393 | if (strcasecmp(rs->host.h_name, bp) != 0) { |
| 394 | syslog(LOG_NOTICE|LOG_AUTH, |
| 395 | AskedForGot, rs->host.h_name, bp); |
| 396 | cp += n; |
| 397 | continue; /* XXX - had_error++ ? */ |
| 398 | } |
| 399 | if (n != rs->host.h_length) { |
| 400 | cp += n; |
| 401 | continue; |
| 402 | } |
| 403 | if (type == T_AAAA) { |
| 404 | struct in6_addr in6; |
| 405 | memcpy(&in6, cp, IN6ADDRSZ); |
| 406 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 407 | cp += n; |
| 408 | continue; |
| 409 | } |
| 410 | } |
| 411 | if (!haveanswer) { |
| 412 | int nn; |
| 413 | |
| 414 | rs->host.h_name = bp; |
| 415 | nn = strlen(bp) + 1; /* for the \0 */ |
| 416 | bp += nn; |
| 417 | } |
| 418 | |
| 419 | bp += sizeof(align) - |
| 420 | (size_t)((u_long)bp % sizeof(align)); |
| 421 | |
| 422 | if (bp + n >= &rs->hostbuf[sizeof rs->hostbuf]) { |
| 423 | dprintf("size (%d) too big\n", res, n); |
| 424 | had_error++; |
| 425 | continue; |
| 426 | } |
| 427 | if (hap >= &rs->h_addr_ptrs[MAXADDRS-1]) { |
| 428 | if (!toobig++) |
| 429 | dprintf("Too many addresses (%d)\n", |
| 430 | res, MAXADDRS); |
| 431 | cp += n; |
| 432 | continue; |
| 433 | } |
| 434 | (void)memcpy(*hap++ = bp, cp, (size_t)n); |
| 435 | bp += n; |
| 436 | cp += n; |
| 437 | if (cp != erdata) { |
| 438 | h_errno = NO_RECOVERY; |
| 439 | return NULL; |
| 440 | } |
| 441 | break; |
| 442 | default: |
| 443 | abort(); |
| 444 | } |
| 445 | if (!had_error) |
| 446 | haveanswer++; |
| 447 | } |
| 448 | if (haveanswer) { |
| 449 | *ap = NULL; |
| 450 | *hap = NULL; |
| 451 | /* |
| 452 | * Note: we sort even if host can take only one address |
| 453 | * in its return structures - should give it the "best" |
| 454 | * address in that case, not some random one |
| 455 | */ |
| 456 | if (res->nsort && haveanswer > 1 && qtype == T_A) |
| 457 | addrsort(rs->h_addr_ptrs, haveanswer, res); |
| 458 | if (!rs->host.h_name) { |
| 459 | n = strlen(qname) + 1; /* for the \0 */ |
| 460 | if (n > ep - bp || n >= MAXHOSTNAMELEN) |
| 461 | goto no_recovery; |
| 462 | strlcpy(bp, qname, (size_t)(ep - bp)); |
| 463 | rs->host.h_name = bp; |
| 464 | bp += n; |
| 465 | } |
| 466 | if (res->options & RES_USE_INET6) |
| 467 | map_v4v6_hostent(&rs->host, &bp, ep); |
| 468 | h_errno = NETDB_SUCCESS; |
| 469 | return &rs->host; |
| 470 | } |
| 471 | no_recovery: |
| 472 | h_errno = NO_RECOVERY; |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | int |
| 477 | gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen, |
| 478 | struct hostent**result, int *errorp) |
| 479 | { |
| 480 | struct hostent *res; |
| 481 | |
| 482 | res = gethostbyname(name); |
| 483 | *errorp = h_errno; |
| 484 | if (res == NULL) { |
| 485 | *result = NULL; |
| 486 | return -1; |
| 487 | } |
| 488 | memcpy(hp, res, sizeof *hp); |
| 489 | *result = hp; |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | struct hostent * |
| 494 | gethostbyname(const char *name) |
| 495 | { |
| 496 | struct hostent *hp; |
| 497 | res_state res = __res_get_state(); |
| 498 | |
| 499 | if (res == NULL) |
| 500 | return NULL; |
| 501 | |
| 502 | assert(name != NULL); |
| 503 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 504 | /* try IPv6 first - if that fails do IPv4 */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 505 | if (res->options & RES_USE_INET6) { |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 506 | hp = gethostbyname_internal(name, AF_INET6, res, NETID_UNSET, MARK_UNSET); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 507 | if (hp) { |
| 508 | __res_put_state(res); |
| 509 | return hp; |
| 510 | } |
| 511 | } |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 512 | hp = gethostbyname_internal(name, AF_INET, res, NETID_UNSET, MARK_UNSET); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 513 | __res_put_state(res); |
| 514 | return hp; |
| 515 | } |
| 516 | |
| 517 | struct hostent * |
| 518 | gethostbyname2(const char *name, int af) |
| 519 | { |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 520 | return android_gethostbynamefornet(name, af, NETID_UNSET, MARK_UNSET); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | struct hostent * |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 524 | android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark) |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 525 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 526 | struct hostent *hp; |
| 527 | res_state res = __res_get_state(); |
| 528 | |
| 529 | if (res == NULL) |
| 530 | return NULL; |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 531 | hp = gethostbyname_internal(name, af, res, netid, mark); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 532 | __res_put_state(res); |
| 533 | return hp; |
| 534 | } |
| 535 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 536 | __LIBC_HIDDEN__ FILE* android_open_proxy() { |
| 537 | const char* cache_mode = getenv("ANDROID_DNS_MODE"); |
| 538 | bool use_proxy = (cache_mode == NULL || strcmp(cache_mode, "local") != 0); |
| 539 | if (!use_proxy) { |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 540 | return NULL; |
| 541 | } |
| 542 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 543 | int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 544 | if (s == -1) { |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | const int one = 1; |
| 549 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
| 550 | |
| 551 | struct sockaddr_un proxy_addr; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 552 | memset(&proxy_addr, 0, sizeof(proxy_addr)); |
| 553 | proxy_addr.sun_family = AF_UNIX; |
| 554 | strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", sizeof(proxy_addr.sun_path)); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 555 | |
| 556 | if (TEMP_FAILURE_RETRY(connect(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) != 0) { |
| 557 | close(s); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 558 | return NULL; |
| 559 | } |
| 560 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 561 | return fdopen(s, "r+"); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 562 | } |
| 563 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 564 | static struct hostent * |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 565 | android_read_hostent(FILE* proxy) |
| 566 | { |
| 567 | uint32_t size; |
| 568 | char buf[4]; |
| 569 | if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) return NULL; |
| 570 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 571 | // This is reading serialized data from system/netd/server/DnsProxyListener.cpp |
| 572 | // and changes here need to be matched there. |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 573 | int result_code = strtol(buf, NULL, 10); |
| 574 | if (result_code != DnsProxyQueryResult) { |
| 575 | fread(&size, 1, sizeof(size), proxy); |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 580 | size = ntohl(size); |
| 581 | res_static rs = __res_get_static(); |
| 582 | memset(&rs->host, 0, sizeof(rs->host)); |
| 583 | char *ptr = rs->hostbuf; |
| 584 | |
| 585 | if (fread(ptr, 1, size, proxy) != size) return NULL; |
| 586 | ptr += size; |
| 587 | rs->host.h_name = rs->hostbuf; |
| 588 | |
| 589 | char **aliases = rs->host_aliases; |
| 590 | rs->host.h_aliases = rs->host_aliases; |
| 591 | while (1) { |
| 592 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 593 | size = ntohl(size); |
| 594 | |
| 595 | if (size == 0) { |
| 596 | *aliases = NULL; |
| 597 | break; |
| 598 | } |
| 599 | if (fread(ptr, 1, size, proxy) != size) return NULL; |
| 600 | *aliases++ = ptr; |
| 601 | ptr += size; |
| 602 | } |
| 603 | |
| 604 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 605 | rs->host.h_addrtype = ntohl(size); |
| 606 | |
| 607 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 608 | rs->host.h_length = ntohl(size); |
| 609 | |
| 610 | char **addrs = rs->h_addr_ptrs; |
| 611 | rs->host.h_addr_list = rs->h_addr_ptrs; |
| 612 | while (1) { |
| 613 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 614 | size = ntohl(size); |
| 615 | if (size == 0) { |
| 616 | *addrs = NULL; |
| 617 | break; |
| 618 | } |
| 619 | if (fread(ptr, 1, size, proxy) != size) return NULL; |
| 620 | *addrs++ = ptr; |
| 621 | ptr += size; |
| 622 | } |
| 623 | |
| 624 | return &rs->host; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | static struct hostent * |
| 629 | gethostbyname_internal_real(const char *name, int af, res_state res) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 630 | { |
| 631 | const char *cp; |
| 632 | char *bp, *ep; |
| 633 | int size; |
| 634 | struct hostent *hp; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 635 | res_static rs = __res_get_static(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 636 | |
| 637 | static const ns_dtab dtab[] = { |
| 638 | NS_FILES_CB(_gethtbyname, NULL) |
| 639 | { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */ |
| 640 | { 0, 0, 0 } |
| 641 | }; |
| 642 | |
| 643 | assert(name != NULL); |
| 644 | |
| 645 | switch (af) { |
| 646 | case AF_INET: |
| 647 | size = INADDRSZ; |
| 648 | break; |
| 649 | case AF_INET6: |
| 650 | size = IN6ADDRSZ; |
| 651 | break; |
| 652 | default: |
| 653 | h_errno = NETDB_INTERNAL; |
| 654 | errno = EAFNOSUPPORT; |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | rs->host.h_addrtype = af; |
| 659 | rs->host.h_length = size; |
| 660 | |
| 661 | /* |
| 662 | * if there aren't any dots, it could be a user-level alias. |
| 663 | * this is also done in res_nquery() since we are not the only |
| 664 | * function that looks up host names. |
| 665 | */ |
| 666 | if (!strchr(name, '.') && (cp = __hostalias(name))) |
| 667 | name = cp; |
| 668 | |
| 669 | /* |
| 670 | * disallow names consisting only of digits/dots, unless |
| 671 | * they end in a dot. |
| 672 | */ |
| 673 | if (isdigit((u_char) name[0])) |
| 674 | for (cp = name;; ++cp) { |
| 675 | if (!*cp) { |
| 676 | if (*--cp == '.') |
| 677 | break; |
| 678 | /* |
| 679 | * All-numeric, no dot at the end. |
| 680 | * Fake up a hostent as if we'd actually |
| 681 | * done a lookup. |
| 682 | */ |
| 683 | if (inet_pton(af, name, |
| 684 | (char *)(void *)rs->host_addr) <= 0) { |
| 685 | h_errno = HOST_NOT_FOUND; |
| 686 | return NULL; |
| 687 | } |
| 688 | strncpy(rs->hostbuf, name, MAXDNAME); |
| 689 | rs->hostbuf[MAXDNAME] = '\0'; |
| 690 | bp = rs->hostbuf + MAXDNAME; |
| 691 | ep = rs->hostbuf + sizeof rs->hostbuf; |
| 692 | rs->host.h_name = rs->hostbuf; |
| 693 | rs->host.h_aliases = rs->host_aliases; |
| 694 | rs->host_aliases[0] = NULL; |
| 695 | rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr; |
| 696 | rs->h_addr_ptrs[1] = NULL; |
| 697 | rs->host.h_addr_list = rs->h_addr_ptrs; |
| 698 | if (res->options & RES_USE_INET6) |
| 699 | map_v4v6_hostent(&rs->host, &bp, ep); |
| 700 | h_errno = NETDB_SUCCESS; |
| 701 | return &rs->host; |
| 702 | } |
| 703 | if (!isdigit((u_char) *cp) && *cp != '.') |
| 704 | break; |
| 705 | } |
| 706 | if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || |
| 707 | name[0] == ':') |
| 708 | for (cp = name;; ++cp) { |
| 709 | if (!*cp) { |
| 710 | if (*--cp == '.') |
| 711 | break; |
| 712 | /* |
| 713 | * All-IPv6-legal, no dot at the end. |
| 714 | * Fake up a hostent as if we'd actually |
| 715 | * done a lookup. |
| 716 | */ |
| 717 | if (inet_pton(af, name, |
| 718 | (char *)(void *)rs->host_addr) <= 0) { |
| 719 | h_errno = HOST_NOT_FOUND; |
| 720 | return NULL; |
| 721 | } |
| 722 | strncpy(rs->hostbuf, name, MAXDNAME); |
| 723 | rs->hostbuf[MAXDNAME] = '\0'; |
| 724 | bp = rs->hostbuf + MAXDNAME; |
| 725 | ep = rs->hostbuf + sizeof rs->hostbuf; |
| 726 | rs->host.h_name = rs->hostbuf; |
| 727 | rs->host.h_aliases = rs->host_aliases; |
| 728 | rs->host_aliases[0] = NULL; |
| 729 | rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr; |
| 730 | rs->h_addr_ptrs[1] = NULL; |
| 731 | rs->host.h_addr_list = rs->h_addr_ptrs; |
| 732 | h_errno = NETDB_SUCCESS; |
| 733 | return &rs->host; |
| 734 | } |
| 735 | if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') |
| 736 | break; |
| 737 | } |
| 738 | |
| 739 | hp = NULL; |
| 740 | h_errno = NETDB_INTERNAL; |
| 741 | if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname", |
| 742 | default_dns_files, name, strlen(name), af) != NS_SUCCESS) { |
| 743 | return NULL; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 744 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 745 | h_errno = NETDB_SUCCESS; |
| 746 | return hp; |
| 747 | } |
| 748 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 749 | |
| 750 | // very similar in proxy-ness to android_getaddrinfo_proxy |
| 751 | static struct hostent * |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 752 | gethostbyname_internal(const char *name, int af, res_state res, unsigned netid, unsigned mark) |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 753 | { |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 754 | FILE* proxy = android_open_proxy(); |
| 755 | if (proxy == NULL) { |
| 756 | // Either we're not supposed to be using the proxy or the proxy is unavailable. |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 757 | res_setnetid(res, netid); |
Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 758 | res_setmark(res, mark); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 759 | return gethostbyname_internal_real(name, af, res); |
| 760 | } |
| 761 | |
Paul Jensen | 5240b56 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 762 | netid = __netdClientDispatch.netIdForResolv(netid); |
| 763 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 764 | // This is writing to system/netd/server/DnsProxyListener.cpp and changes |
| 765 | // here need to be matched there. |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 766 | if (fprintf(proxy, "gethostbyname %u %s %d", |
| 767 | netid, |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 768 | name == NULL ? "^" : name, |
| 769 | af) < 0) { |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 770 | fclose(proxy); |
| 771 | return NULL; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | if (fputc(0, proxy) == EOF || fflush(proxy) != 0) { |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 775 | fclose(proxy); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 776 | return NULL; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 777 | } |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 778 | |
| 779 | struct hostent* result = android_read_hostent(proxy); |
| 780 | fclose(proxy); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 781 | return result; |
| 782 | } |
| 783 | |
| 784 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 785 | static struct hostent * |
| 786 | android_gethostbyaddrfornet_real(const void *addr, socklen_t len, int af, unsigned netid, unsigned mark) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 787 | const u_char *uaddr = (const u_char *)addr; |
| 788 | socklen_t size; |
| 789 | struct hostent *hp; |
| 790 | static const ns_dtab dtab[] = { |
| 791 | NS_FILES_CB(_gethtbyaddr, NULL) |
| 792 | { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */ |
| 793 | { 0, 0, 0 } |
| 794 | }; |
| 795 | |
| 796 | assert(addr != NULL); |
| 797 | |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 798 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 799 | (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) || |
| 800 | IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 801 | h_errno = HOST_NOT_FOUND; |
| 802 | return NULL; |
| 803 | } |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 804 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 805 | (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) || |
| 806 | IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 807 | /* Unmap. */ |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 808 | uaddr += NS_IN6ADDRSZ - NS_INADDRSZ; |
| 809 | addr = uaddr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 810 | af = AF_INET; |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 811 | len = NS_INADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 812 | } |
| 813 | switch (af) { |
| 814 | case AF_INET: |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 815 | size = NS_INADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 816 | break; |
| 817 | case AF_INET6: |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 818 | size = NS_IN6ADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 819 | break; |
| 820 | default: |
| 821 | errno = EAFNOSUPPORT; |
| 822 | h_errno = NETDB_INTERNAL; |
| 823 | return NULL; |
| 824 | } |
| 825 | if (size != len) { |
| 826 | errno = EINVAL; |
| 827 | h_errno = NETDB_INTERNAL; |
| 828 | return NULL; |
| 829 | } |
| 830 | hp = NULL; |
| 831 | h_errno = NETDB_INTERNAL; |
| 832 | if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr", |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 833 | default_dns_files, uaddr, len, af, netid, mark) != NS_SUCCESS) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 834 | return NULL; |
| 835 | h_errno = NETDB_SUCCESS; |
| 836 | return hp; |
| 837 | } |
| 838 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 839 | __LIBC_HIDDEN__ struct hostent* |
| 840 | android_gethostbyaddrfornet_proxy(const void* addr, socklen_t len, int af, unsigned netid, unsigned mark) { |
| 841 | FILE* proxy = android_open_proxy(); |
| 842 | if (proxy == NULL) { |
| 843 | // Either we're not supposed to be using the proxy or the proxy is unavailable. |
| 844 | return android_gethostbyaddrfornet_real(addr,len, af, netid, mark); |
| 845 | } |
| 846 | |
| 847 | char buf[INET6_ADDRSTRLEN]; //big enough for IPv4 and IPv6 |
| 848 | const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf)); |
| 849 | if (addrStr == NULL) { |
| 850 | fclose(proxy); |
| 851 | return NULL; |
| 852 | } |
| 853 | |
| 854 | netid = __netdClientDispatch.netIdForResolv(netid); |
| 855 | |
| 856 | if (fprintf(proxy, "gethostbyaddr %s %d %d %u", |
| 857 | addrStr, len, af, netid) < 0) { |
| 858 | fclose(proxy); |
| 859 | return NULL; |
| 860 | } |
| 861 | |
| 862 | if (fputc(0, proxy) == EOF || fflush(proxy) != 0) { |
| 863 | fclose(proxy); |
| 864 | return NULL; |
| 865 | } |
| 866 | |
| 867 | struct hostent *result = android_read_hostent(proxy); |
| 868 | fclose(proxy); |
| 869 | return result; |
| 870 | } |
| 871 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 872 | struct hostent * |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 873 | android_gethostbyaddrfornet(const void *addr, socklen_t len, int af, unsigned netid, unsigned mark) |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 874 | { |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 875 | return android_gethostbyaddrfornet_proxy(addr, len, af, netid, mark); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | struct hostent * |
| 879 | gethostbyaddr(const void *addr, socklen_t len, int af) |
| 880 | { |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 881 | return android_gethostbyaddrfornet(addr, len, af, NETID_UNSET, MARK_UNSET); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | |
Jim Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 885 | static void |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 886 | _sethtent(int f) |
| 887 | { |
| 888 | res_static rs = __res_get_static(); |
| 889 | if (rs == NULL) return; |
| 890 | if (!rs->hostf) |
Elliott Hughes | c674edb | 2014-08-26 15:56:54 -0700 | [diff] [blame] | 891 | rs->hostf = fopen(_PATH_HOSTS, "re" ); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 892 | else |
| 893 | rewind(rs->hostf); |
| 894 | rs->stayopen = f; |
| 895 | } |
| 896 | |
Jim Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 897 | static void |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 898 | _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 Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 909 | static struct hostent * |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 910 | _gethtent(void) |
| 911 | { |
| 912 | char *p; |
| 913 | char *cp, **q; |
| 914 | int af, len; |
| 915 | res_static rs = __res_get_static(); |
| 916 | |
Elliott Hughes | c674edb | 2014-08-26 15:56:54 -0700 | [diff] [blame] | 917 | if (!rs->hostf && !(rs->hostf = fopen(_PATH_HOSTS, "re" ))) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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*/ |
| 986 | int |
| 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 Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 1022 | static struct hostent * |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1023 | _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 Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 1113 | static int |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1114 | _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 | |
| 1144 | static void |
| 1145 | map_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 | |
| 1165 | static void |
| 1166 | map_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 | |
| 1193 | static void |
| 1194 | addrsort(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 | |
| 1236 | struct hostent * |
| 1237 | gethostent(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 Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 1246 | static int |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1247 | _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 Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 1306 | static int |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1307 | _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; |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1316 | unsigned netid, mark; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1317 | 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); |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1324 | netid = va_arg(ap, unsigned); |
| 1325 | mark = va_arg(ap, unsigned); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | } |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1367 | res_setnetid(res, netid); |
Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 1368 | res_setmark(res, mark); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1369 | n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf)); |
| 1370 | if (n < 0) { |
| 1371 | free(buf); |
| 1372 | dprintf("res_nquery failed (%d)\n", res, n); |
| 1373 | __res_put_state(res); |
| 1374 | return NS_NOTFOUND; |
| 1375 | } |
| 1376 | hp = getanswer(buf, n, qbuf, T_PTR, res); |
| 1377 | free(buf); |
| 1378 | if (hp == NULL) { |
| 1379 | __res_put_state(res); |
| 1380 | switch (h_errno) { |
| 1381 | case HOST_NOT_FOUND: |
| 1382 | return NS_NOTFOUND; |
| 1383 | case TRY_AGAIN: |
| 1384 | return NS_TRYAGAIN; |
| 1385 | default: |
| 1386 | return NS_UNAVAIL; |
| 1387 | } |
| 1388 | } |
| 1389 | hp->h_addrtype = af; |
| 1390 | hp->h_length = len; |
| 1391 | (void)memcpy(rs->host_addr, uaddr, (size_t)len); |
| 1392 | rs->h_addr_ptrs[0] = (char *)(void *)rs->host_addr; |
| 1393 | rs->h_addr_ptrs[1] = NULL; |
| 1394 | if (af == AF_INET && (res->options & RES_USE_INET6)) { |
| 1395 | map_v4v6_address((char *)(void *)rs->host_addr, |
| 1396 | (char *)(void *)rs->host_addr); |
| 1397 | hp->h_addrtype = AF_INET6; |
| 1398 | hp->h_length = IN6ADDRSZ; |
| 1399 | } |
| 1400 | |
| 1401 | __res_put_state(res); |
| 1402 | *((struct hostent **)rv) = hp; |
| 1403 | h_errno = NETDB_SUCCESS; |
| 1404 | return NS_SUCCESS; |
| 1405 | } |