Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1 | /* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 91 | #include "hostent.h" |
| 92 | |
Lev Rumyantsev | 814f38f | 2020-10-12 17:33:06 -0700 | [diff] [blame^] | 93 | #include "private/bionic_defs.h" |
| 94 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 95 | #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \ |
| 96 | (ok)(nm) != 0) |
| 97 | #define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok) |
| 98 | #define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok) |
| 99 | |
| 100 | #define addalias(d, s, arr, siz) do { \ |
| 101 | if (d >= &arr[siz]) { \ |
| 102 | char **xptr = realloc(arr, (siz + 10) * sizeof(*arr)); \ |
| 103 | if (xptr == NULL) \ |
| 104 | goto nospc; \ |
| 105 | d = xptr + (d - arr); \ |
| 106 | arr = xptr; \ |
| 107 | siz += 10; \ |
| 108 | } \ |
| 109 | *d++ = s; \ |
| 110 | } while (/*CONSTCOND*/0) |
| 111 | |
| 112 | #define setup(arr, siz) do { \ |
| 113 | arr = malloc((siz = 10) * sizeof(*arr)); \ |
| 114 | if (arr == NULL) \ |
| 115 | goto nospc; \ |
| 116 | } while (/*CONSTCOND*/0) |
| 117 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 118 | // This should be synchronized to ResponseCode.h |
| 119 | static const int DnsProxyQueryResult = 222; |
| 120 | |
Elliott Hughes | 68c2755 | 2014-07-07 09:44:17 -0700 | [diff] [blame] | 121 | static const char AskedForGot[] = |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | "gethostby*.getanswer: asked for \"%s\", got \"%s\""; |
| 123 | |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 124 | static const struct android_net_context NETCONTEXT_UNSET = { |
| 125 | .app_mark = MARK_UNSET, |
| 126 | .app_netid = NETID_UNSET, |
| 127 | .dns_mark = MARK_UNSET, |
| 128 | .dns_netid = NETID_UNSET, |
| 129 | .uid = NET_CONTEXT_INVALID_UID |
| 130 | }; |
| 131 | |
Ben Schwartz | 47fb0e8 | 2018-01-31 13:35:03 -0500 | [diff] [blame] | 132 | #define MAXPACKET (8*1024) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 133 | |
| 134 | typedef union { |
| 135 | HEADER hdr; |
| 136 | u_char buf[MAXPACKET]; |
| 137 | } querybuf; |
| 138 | |
| 139 | typedef union { |
| 140 | int32_t al; |
| 141 | char ac; |
| 142 | } align; |
| 143 | |
| 144 | #ifdef DEBUG |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 145 | static void debugprintf(const char *, res_state, ...) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 146 | __attribute__((__format__(__printf__, 1, 3))); |
| 147 | #endif |
| 148 | static struct hostent *getanswer(const querybuf *, int, const char *, int, |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 149 | res_state, struct hostent *, char *, size_t, int *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 150 | static void map_v4v6_address(const char *, char *); |
| 151 | static void map_v4v6_hostent(struct hostent *, char **, char *); |
| 152 | static void addrsort(char **, int, res_state); |
| 153 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 154 | void ht_sethostent(int); |
| 155 | void ht_endhostent(void); |
| 156 | struct hostent *ht_gethostbyname(char *); |
| 157 | struct hostent *ht_gethostbyaddr(const char *, int, int); |
| 158 | void dns_service(void); |
| 159 | #undef dn_skipname |
| 160 | int dn_skipname(const u_char *, const u_char *); |
Jim Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 161 | static int _dns_gethtbyaddr(void *, void *, va_list); |
| 162 | static int _dns_gethtbyname(void *, void *, va_list); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 163 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 164 | static struct hostent *gethostbyname_internal(const char *, int, res_state, |
Ben Schwartz | dd878fe | 2017-05-22 10:19:25 -0400 | [diff] [blame] | 165 | struct hostent *, char *, size_t, int *, const struct android_net_context *); |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 166 | static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t, |
| 167 | int, struct hostent *, char *, size_t, int *, const struct android_net_context *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 168 | |
| 169 | static const ns_src default_dns_files[] = { |
| 170 | { NSSRC_FILES, NS_SUCCESS }, |
| 171 | { NSSRC_DNS, NS_SUCCESS }, |
| 172 | { 0, 0 } |
| 173 | }; |
| 174 | |
Elliott Hughes | bb7d9fb | 2017-10-23 17:38:35 -0700 | [diff] [blame] | 175 | static int h_errno_to_result(int* herrno_p) { |
| 176 | // glibc considers ERANGE a special case (and BSD uses ENOSPC instead). |
| 177 | if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) { |
| 178 | errno = ERANGE; |
| 179 | return errno; |
| 180 | } |
| 181 | // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value. |
| 182 | return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0; |
| 183 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 184 | |
| 185 | #ifdef DEBUG |
| 186 | static void |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 187 | debugprintf(const char *msg, res_state res, ...) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 188 | { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 189 | _DIAGASSERT(msg != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 190 | |
| 191 | if (res->options & RES_DEBUG) { |
| 192 | int save = errno; |
| 193 | va_list ap; |
| 194 | |
| 195 | va_start (ap, res); |
| 196 | vprintf(msg, ap); |
| 197 | va_end (ap); |
| 198 | |
| 199 | errno = save; |
| 200 | } |
| 201 | } |
| 202 | #else |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 203 | # define debugprintf(msg, res, num) /*nada*/ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 204 | #endif |
| 205 | |
| 206 | #define BOUNDED_INCR(x) \ |
| 207 | do { \ |
Elliott Hughes | 87c0dba | 2016-11-14 13:56:32 -0800 | [diff] [blame] | 208 | BOUNDS_CHECK(cp, x); \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 209 | cp += (x); \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 210 | } while (/*CONSTCOND*/0) |
| 211 | |
| 212 | #define BOUNDS_CHECK(ptr, count) \ |
| 213 | do { \ |
Elliott Hughes | 87c0dba | 2016-11-14 13:56:32 -0800 | [diff] [blame] | 214 | if (eom - (ptr) < (count)) \ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 215 | goto no_recovery; \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 216 | } while (/*CONSTCOND*/0) |
| 217 | |
| 218 | static struct hostent * |
| 219 | getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 220 | res_state res, struct hostent *hent, char *buf, size_t buflen, int *he) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 221 | { |
| 222 | const HEADER *hp; |
| 223 | const u_char *cp; |
| 224 | int n; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 225 | size_t qlen; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 | const u_char *eom, *erdata; |
| 227 | char *bp, **ap, **hap, *ep; |
| 228 | int type, class, ancount, qdcount; |
| 229 | int haveanswer, had_error; |
| 230 | int toobig = 0; |
| 231 | char tbuf[MAXDNAME]; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 232 | char **aliases; |
| 233 | size_t maxaliases; |
| 234 | char *addr_ptrs[MAXADDRS]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 235 | const char *tname; |
| 236 | int (*name_ok)(const char *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 237 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 238 | _DIAGASSERT(answer != NULL); |
| 239 | _DIAGASSERT(qname != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 240 | |
| 241 | tname = qname; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 242 | hent->h_name = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 243 | eom = answer->buf + anslen; |
| 244 | switch (qtype) { |
| 245 | case T_A: |
| 246 | case T_AAAA: |
| 247 | name_ok = res_hnok; |
| 248 | break; |
| 249 | case T_PTR: |
| 250 | name_ok = res_dnok; |
| 251 | break; |
| 252 | default: |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 253 | *he = NO_RECOVERY; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 254 | return NULL; /* XXX should be abort(); */ |
| 255 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 256 | |
| 257 | setup(aliases, maxaliases); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 258 | /* |
| 259 | * find first satisfactory answer |
| 260 | */ |
| 261 | hp = &answer->hdr; |
| 262 | ancount = ntohs(hp->ancount); |
| 263 | qdcount = ntohs(hp->qdcount); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 264 | bp = buf; |
| 265 | ep = buf + buflen; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 266 | cp = answer->buf; |
| 267 | BOUNDED_INCR(HFIXEDSZ); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 268 | if (qdcount != 1) |
| 269 | goto no_recovery; |
| 270 | |
| 271 | n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); |
| 272 | if ((n < 0) || !maybe_ok(res, bp, name_ok)) |
| 273 | goto no_recovery; |
| 274 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 275 | BOUNDED_INCR(n + QFIXEDSZ); |
| 276 | if (qtype == T_A || qtype == T_AAAA) { |
| 277 | /* res_send() has already verified that the query name is the |
| 278 | * same as the one we sent; this just gets the expanded name |
| 279 | * (i.e., with the succeeding search-domain tacked on). |
| 280 | */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 281 | n = (int)strlen(bp) + 1; /* for the \0 */ |
| 282 | if (n >= MAXHOSTNAMELEN) |
| 283 | goto no_recovery; |
| 284 | hent->h_name = bp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 285 | bp += n; |
| 286 | /* The qname can be abbreviated, but h_name is now absolute. */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 287 | qname = hent->h_name; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 288 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 289 | hent->h_aliases = ap = aliases; |
| 290 | hent->h_addr_list = hap = addr_ptrs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 291 | *ap = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 292 | *hap = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 293 | haveanswer = 0; |
| 294 | had_error = 0; |
| 295 | while (ancount-- > 0 && cp < eom && !had_error) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 296 | n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); |
| 297 | if ((n < 0) || !maybe_ok(res, bp, name_ok)) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 298 | had_error++; |
| 299 | continue; |
| 300 | } |
| 301 | cp += n; /* name */ |
| 302 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
| 303 | type = _getshort(cp); |
| 304 | cp += INT16SZ; /* type */ |
| 305 | class = _getshort(cp); |
| 306 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
| 307 | n = _getshort(cp); |
| 308 | cp += INT16SZ; /* len */ |
| 309 | BOUNDS_CHECK(cp, n); |
| 310 | erdata = cp + n; |
| 311 | if (class != C_IN) { |
| 312 | /* XXX - debug? syslog? */ |
| 313 | cp += n; |
| 314 | continue; /* XXX - had_error++ ? */ |
| 315 | } |
| 316 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 317 | n = dn_expand(answer->buf, eom, cp, tbuf, |
| 318 | (int)sizeof tbuf); |
| 319 | if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 320 | had_error++; |
| 321 | continue; |
| 322 | } |
| 323 | cp += n; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 324 | if (cp != erdata) |
| 325 | goto no_recovery; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 326 | /* Store alias. */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 327 | addalias(ap, bp, aliases, maxaliases); |
| 328 | n = (int)strlen(bp) + 1; /* for the \0 */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 329 | if (n >= MAXHOSTNAMELEN) { |
| 330 | had_error++; |
| 331 | continue; |
| 332 | } |
| 333 | bp += n; |
| 334 | /* Get canonical name. */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 335 | n = (int)strlen(tbuf) + 1; /* for the \0 */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 336 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 337 | had_error++; |
| 338 | continue; |
| 339 | } |
| 340 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 341 | hent->h_name = bp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 342 | bp += n; |
| 343 | continue; |
| 344 | } |
| 345 | if (qtype == T_PTR && type == T_CNAME) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 346 | n = dn_expand(answer->buf, eom, cp, tbuf, |
| 347 | (int)sizeof tbuf); |
| 348 | if (n < 0 || !maybe_dnok(res, tbuf)) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 349 | had_error++; |
| 350 | continue; |
| 351 | } |
| 352 | cp += n; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 353 | if (cp != erdata) |
| 354 | goto no_recovery; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 355 | /* Get canonical name. */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 356 | n = (int)strlen(tbuf) + 1; /* for the \0 */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 357 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 358 | had_error++; |
| 359 | continue; |
| 360 | } |
| 361 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 362 | tname = bp; |
| 363 | bp += n; |
| 364 | continue; |
| 365 | } |
| 366 | if (type != qtype) { |
| 367 | if (type != T_KEY && type != T_SIG) |
| 368 | syslog(LOG_NOTICE|LOG_AUTH, |
| 369 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", |
| 370 | qname, p_class(C_IN), p_type(qtype), |
| 371 | p_type(type)); |
| 372 | cp += n; |
| 373 | continue; /* XXX - had_error++ ? */ |
| 374 | } |
| 375 | switch (type) { |
| 376 | case T_PTR: |
| 377 | if (strcasecmp(tname, bp) != 0) { |
| 378 | syslog(LOG_NOTICE|LOG_AUTH, |
| 379 | AskedForGot, qname, bp); |
| 380 | cp += n; |
| 381 | continue; /* XXX - had_error++ ? */ |
| 382 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 383 | n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); |
| 384 | if ((n < 0) || !maybe_hnok(res, bp)) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 385 | had_error++; |
| 386 | break; |
| 387 | } |
| 388 | #if MULTI_PTRS_ARE_ALIASES |
| 389 | cp += n; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 390 | if (cp != erdata) |
| 391 | goto no_recovery; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 392 | if (!haveanswer) |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 393 | hent->h_name = bp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 394 | else |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 395 | addalias(ap, bp, aliases, maxaliases); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 396 | if (n != -1) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 397 | n = (int)strlen(bp) + 1; /* for the \0 */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 398 | if (n >= MAXHOSTNAMELEN) { |
| 399 | had_error++; |
| 400 | break; |
| 401 | } |
| 402 | bp += n; |
| 403 | } |
| 404 | break; |
| 405 | #else |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 406 | hent->h_name = bp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 407 | if (res->options & RES_USE_INET6) { |
| 408 | n = strlen(bp) + 1; /* for the \0 */ |
| 409 | if (n >= MAXHOSTNAMELEN) { |
| 410 | had_error++; |
| 411 | break; |
| 412 | } |
| 413 | bp += n; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 414 | map_v4v6_hostent(hent, &bp, ep); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 415 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 416 | goto success; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 417 | #endif |
| 418 | case T_A: |
| 419 | case T_AAAA: |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 420 | if (strcasecmp(hent->h_name, bp) != 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 421 | syslog(LOG_NOTICE|LOG_AUTH, |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 422 | AskedForGot, hent->h_name, bp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 423 | cp += n; |
| 424 | continue; /* XXX - had_error++ ? */ |
| 425 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 426 | if (n != hent->h_length) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 427 | cp += n; |
| 428 | continue; |
| 429 | } |
| 430 | if (type == T_AAAA) { |
| 431 | struct in6_addr in6; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 432 | memcpy(&in6, cp, NS_IN6ADDRSZ); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 433 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 434 | cp += n; |
| 435 | continue; |
| 436 | } |
| 437 | } |
| 438 | if (!haveanswer) { |
| 439 | int nn; |
| 440 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 441 | hent->h_name = bp; |
| 442 | nn = (int)strlen(bp) + 1; /* for the \0 */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 443 | bp += nn; |
| 444 | } |
| 445 | |
| 446 | bp += sizeof(align) - |
| 447 | (size_t)((u_long)bp % sizeof(align)); |
| 448 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 449 | if (bp + n >= ep) { |
| 450 | debugprintf("size (%d) too big\n", res, n); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 451 | had_error++; |
| 452 | continue; |
| 453 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 454 | if (hap >= &addr_ptrs[MAXADDRS - 1]) { |
| 455 | if (!toobig++) { |
| 456 | debugprintf("Too many addresses (%d)\n", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 457 | res, MAXADDRS); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 458 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 459 | cp += n; |
| 460 | continue; |
| 461 | } |
| 462 | (void)memcpy(*hap++ = bp, cp, (size_t)n); |
| 463 | bp += n; |
| 464 | cp += n; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 465 | if (cp != erdata) |
| 466 | goto no_recovery; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 467 | break; |
| 468 | default: |
| 469 | abort(); |
| 470 | } |
| 471 | if (!had_error) |
| 472 | haveanswer++; |
| 473 | } |
| 474 | if (haveanswer) { |
| 475 | *ap = NULL; |
| 476 | *hap = NULL; |
| 477 | /* |
| 478 | * Note: we sort even if host can take only one address |
| 479 | * in its return structures - should give it the "best" |
| 480 | * address in that case, not some random one |
| 481 | */ |
| 482 | if (res->nsort && haveanswer > 1 && qtype == T_A) |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 483 | addrsort(addr_ptrs, haveanswer, res); |
| 484 | if (!hent->h_name) { |
| 485 | n = (int)strlen(qname) + 1; /* for the \0 */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 486 | if (n > ep - bp || n >= MAXHOSTNAMELEN) |
| 487 | goto no_recovery; |
| 488 | strlcpy(bp, qname, (size_t)(ep - bp)); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 489 | hent->h_name = bp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 490 | bp += n; |
| 491 | } |
| 492 | if (res->options & RES_USE_INET6) |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 493 | map_v4v6_hostent(hent, &bp, ep); |
| 494 | goto success; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 495 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 496 | no_recovery: |
| 497 | free(aliases); |
| 498 | *he = NO_RECOVERY; |
| 499 | return NULL; |
| 500 | success: |
| 501 | bp = (char *)ALIGN(bp); |
| 502 | n = (int)(ap - aliases); |
| 503 | qlen = (n + 1) * sizeof(*hent->h_aliases); |
| 504 | if ((size_t)(ep - bp) < qlen) |
| 505 | goto nospc; |
| 506 | hent->h_aliases = (void *)bp; |
| 507 | memcpy(bp, aliases, qlen); |
| 508 | free(aliases); |
| 509 | aliases = NULL; |
| 510 | |
| 511 | bp += qlen; |
| 512 | n = (int)(hap - addr_ptrs); |
| 513 | qlen = (n + 1) * sizeof(*hent->h_addr_list); |
| 514 | if ((size_t)(ep - bp) < qlen) |
| 515 | goto nospc; |
| 516 | hent->h_addr_list = (void *)bp; |
| 517 | memcpy(bp, addr_ptrs, qlen); |
| 518 | *he = NETDB_SUCCESS; |
| 519 | return hent; |
| 520 | nospc: |
| 521 | free(aliases); |
| 522 | errno = ENOSPC; |
| 523 | *he = NETDB_INTERNAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
| 526 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 527 | /* The prototype of gethostbyname_r is from glibc, not that in netbsd. */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 528 | int |
| 529 | gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen, |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 530 | struct hostent **result, int *errorp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 531 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 532 | res_state res = __res_get_state(); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 533 | if (res == NULL) { |
Elliott Hughes | bb7d9fb | 2017-10-23 17:38:35 -0700 | [diff] [blame] | 534 | *result = NULL; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 535 | *errorp = NETDB_INTERNAL; |
| 536 | return -1; |
| 537 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 538 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 539 | _DIAGASSERT(name != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 540 | |
| 541 | if (res->options & RES_USE_INET6) { |
Ben Schwartz | dd878fe | 2017-05-22 10:19:25 -0400 | [diff] [blame] | 542 | *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp, |
| 543 | &NETCONTEXT_UNSET); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 544 | if (*result) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 545 | __res_put_state(res); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 546 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
Ben Schwartz | dd878fe | 2017-05-22 10:19:25 -0400 | [diff] [blame] | 549 | *result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, |
| 550 | &NETCONTEXT_UNSET); |
Elliott Hughes | bb7d9fb | 2017-10-23 17:38:35 -0700 | [diff] [blame] | 551 | return h_errno_to_result(errorp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 554 | /* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */ |
| 555 | int |
| 556 | gethostbyname2_r(const char *name, int af, struct hostent *hp, char *buf, |
| 557 | size_t buflen, struct hostent **result, int *errorp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 558 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 559 | res_state res = __res_get_state(); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 560 | if (res == NULL) { |
| 561 | *result = NULL; |
| 562 | *errorp = NETDB_INTERNAL; |
| 563 | return -1; |
| 564 | } |
Ben Schwartz | dd878fe | 2017-05-22 10:19:25 -0400 | [diff] [blame] | 565 | *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, |
| 566 | &NETCONTEXT_UNSET); |
Elliott Hughes | bb7d9fb | 2017-10-23 17:38:35 -0700 | [diff] [blame] | 567 | return h_errno_to_result(errorp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static struct hostent * |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 571 | android_read_hostent(FILE* proxy, struct hostent* hp, char* hbuf, size_t hbuflen, int *he) |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 572 | { |
| 573 | uint32_t size; |
| 574 | char buf[4]; |
| 575 | if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) return NULL; |
| 576 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 577 | // This is reading serialized data from system/netd/server/DnsProxyListener.cpp |
| 578 | // and changes here need to be matched there. |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 579 | int result_code = strtol(buf, NULL, 10); |
| 580 | if (result_code != DnsProxyQueryResult) { |
| 581 | fread(&size, 1, sizeof(size), proxy); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 582 | *he = HOST_NOT_FOUND; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 587 | size = ntohl(size); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 588 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 589 | memset(hp, 0, sizeof(*hp)); |
| 590 | char *ptr = hbuf; |
| 591 | char *hbuf_end = hbuf + hbuflen; |
| 592 | |
| 593 | if (ptr + size > hbuf_end) { |
| 594 | goto nospc; |
| 595 | } |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 596 | if (fread(ptr, 1, size, proxy) != size) return NULL; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 597 | hp->h_name = ptr; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 598 | ptr += size; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 599 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 600 | char *aliases_ptrs[MAXALIASES]; |
| 601 | char **aliases = &aliases_ptrs[0]; |
| 602 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 603 | while (1) { |
| 604 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 605 | size = ntohl(size); |
| 606 | |
| 607 | if (size == 0) { |
| 608 | *aliases = NULL; |
| 609 | break; |
| 610 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 611 | if (ptr + size > hbuf_end) { |
| 612 | goto nospc; |
| 613 | } |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 614 | if (fread(ptr, 1, size, proxy) != size) return NULL; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 615 | if (aliases < &aliases_ptrs[MAXALIASES - 1]) { |
| 616 | *aliases++ = ptr; |
| 617 | } |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 618 | ptr += size; |
| 619 | } |
| 620 | |
Elliott Hughes | 65dd858 | 2015-07-06 14:16:12 -0700 | [diff] [blame] | 621 | // Fix alignment after variable-length data. |
| 622 | ptr = (char*)ALIGN(ptr); |
| 623 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 624 | int aliases_len = ((int)(aliases - aliases_ptrs) + 1) * sizeof(*hp->h_aliases); |
| 625 | if (ptr + aliases_len > hbuf_end) { |
| 626 | goto nospc; |
| 627 | } |
| 628 | hp->h_aliases = (void*)ptr; |
| 629 | memcpy(ptr, aliases_ptrs, aliases_len); |
| 630 | ptr += aliases_len; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 631 | |
| 632 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 633 | hp->h_addrtype = ntohl(size); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 634 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 635 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 636 | hp->h_length = ntohl(size); |
| 637 | |
| 638 | char *addr_ptrs[MAXADDRS]; |
| 639 | char **addr_p = &addr_ptrs[0]; |
| 640 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 641 | while (1) { |
| 642 | if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL; |
| 643 | size = ntohl(size); |
| 644 | if (size == 0) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 645 | *addr_p = NULL; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 646 | break; |
| 647 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 648 | if (ptr + size > hbuf_end) { |
| 649 | goto nospc; |
| 650 | } |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 651 | if (fread(ptr, 1, size, proxy) != size) return NULL; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 652 | if (addr_p < &addr_ptrs[MAXADDRS - 1]) { |
| 653 | *addr_p++ = ptr; |
| 654 | } |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 655 | ptr += size; |
| 656 | } |
| 657 | |
Elliott Hughes | 65dd858 | 2015-07-06 14:16:12 -0700 | [diff] [blame] | 658 | // Fix alignment after variable-length data. |
| 659 | ptr = (char*)ALIGN(ptr); |
| 660 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 661 | int addrs_len = ((int)(addr_p - addr_ptrs) + 1) * sizeof(*hp->h_addr_list); |
| 662 | if (ptr + addrs_len > hbuf_end) { |
| 663 | goto nospc; |
| 664 | } |
| 665 | hp->h_addr_list = (void*)ptr; |
| 666 | memcpy(ptr, addr_ptrs, addrs_len); |
| 667 | *he = NETDB_SUCCESS; |
| 668 | return hp; |
| 669 | |
| 670 | nospc: |
| 671 | *he = NETDB_INTERNAL; |
| 672 | errno = ENOSPC; |
| 673 | return NULL; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 676 | static struct hostent * |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 677 | gethostbyname_internal_real(const char *name, int af, res_state res, struct hostent *hp, char *buf, |
| 678 | size_t buflen, int *he) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 679 | { |
| 680 | const char *cp; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 681 | struct getnamaddr info; |
| 682 | char hbuf[MAXHOSTNAMELEN]; |
| 683 | size_t size; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 684 | static const ns_dtab dtab[] = { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 685 | NS_FILES_CB(_hf_gethtbyname, NULL) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 686 | { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 687 | NS_NIS_CB(_yp_gethtbyname, NULL) |
| 688 | NS_NULL_CB |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 689 | }; |
| 690 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 691 | _DIAGASSERT(name != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 692 | |
| 693 | switch (af) { |
| 694 | case AF_INET: |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 695 | size = NS_INADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 696 | break; |
| 697 | case AF_INET6: |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 698 | size = NS_IN6ADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 699 | break; |
| 700 | default: |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 701 | *he = NETDB_INTERNAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 702 | errno = EAFNOSUPPORT; |
| 703 | return NULL; |
| 704 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 705 | if (buflen < size) |
| 706 | goto nospc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 707 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 708 | hp->h_addrtype = af; |
| 709 | hp->h_length = (int)size; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 710 | |
| 711 | /* |
| 712 | * if there aren't any dots, it could be a user-level alias. |
| 713 | * this is also done in res_nquery() since we are not the only |
| 714 | * function that looks up host names. |
| 715 | */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 716 | if (!strchr(name, '.') && (cp = res_hostalias(res, name, |
| 717 | hbuf, sizeof(hbuf)))) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 718 | name = cp; |
| 719 | |
| 720 | /* |
| 721 | * disallow names consisting only of digits/dots, unless |
| 722 | * they end in a dot. |
| 723 | */ |
| 724 | if (isdigit((u_char) name[0])) |
| 725 | for (cp = name;; ++cp) { |
| 726 | if (!*cp) { |
| 727 | if (*--cp == '.') |
| 728 | break; |
| 729 | /* |
| 730 | * All-numeric, no dot at the end. |
| 731 | * Fake up a hostent as if we'd actually |
| 732 | * done a lookup. |
| 733 | */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 734 | goto fake; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 735 | } |
| 736 | if (!isdigit((u_char) *cp) && *cp != '.') |
| 737 | break; |
| 738 | } |
| 739 | if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || |
| 740 | name[0] == ':') |
| 741 | for (cp = name;; ++cp) { |
| 742 | if (!*cp) { |
| 743 | if (*--cp == '.') |
| 744 | break; |
| 745 | /* |
| 746 | * All-IPv6-legal, no dot at the end. |
| 747 | * Fake up a hostent as if we'd actually |
| 748 | * done a lookup. |
| 749 | */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 750 | goto fake; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 751 | } |
| 752 | if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') |
| 753 | break; |
| 754 | } |
| 755 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 756 | *he = NETDB_INTERNAL; |
| 757 | info.hp = hp; |
| 758 | info.buf = buf; |
| 759 | info.buflen = buflen; |
| 760 | info.he = he; |
| 761 | if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname", |
| 762 | default_dns_files, name, strlen(name), af) != NS_SUCCESS) |
| 763 | return NULL; |
| 764 | *he = NETDB_SUCCESS; |
| 765 | return hp; |
| 766 | nospc: |
| 767 | *he = NETDB_INTERNAL; |
| 768 | errno = ENOSPC; |
| 769 | return NULL; |
| 770 | fake: |
| 771 | HENT_ARRAY(hp->h_addr_list, 1, buf, buflen); |
| 772 | HENT_ARRAY(hp->h_aliases, 0, buf, buflen); |
| 773 | |
| 774 | hp->h_aliases[0] = NULL; |
| 775 | if (size > buflen) |
| 776 | goto nospc; |
| 777 | |
| 778 | if (inet_pton(af, name, buf) <= 0) { |
| 779 | *he = HOST_NOT_FOUND; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 780 | return NULL; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 781 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 782 | hp->h_addr_list[0] = buf; |
| 783 | hp->h_addr_list[1] = NULL; |
| 784 | buf += size; |
| 785 | buflen -= size; |
| 786 | HENT_SCOPY(hp->h_name, name, buf, buflen); |
| 787 | if (res->options & RES_USE_INET6) |
| 788 | map_v4v6_hostent(hp, &buf, buf + buflen); |
| 789 | *he = NETDB_SUCCESS; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 790 | return hp; |
| 791 | } |
| 792 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 793 | static struct hostent * |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 794 | gethostbyname_internal(const char *name, int af, res_state res, struct hostent *hp, char *hbuf, |
Ben Schwartz | dd878fe | 2017-05-22 10:19:25 -0400 | [diff] [blame] | 795 | size_t hbuflen, int *errorp, const struct android_net_context *netcontext) |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 796 | { |
Luke Huang | e3ed892 | 2018-11-19 16:59:08 +0800 | [diff] [blame] | 797 | FILE* proxy = fdopen(__netdClientDispatch.dnsOpenProxy(), "r+"); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 798 | if (proxy == NULL) { |
| 799 | // Either we're not supposed to be using the proxy or the proxy is unavailable. |
Ben Schwartz | 90a83be | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 800 | res_setnetcontext(res, netcontext); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 801 | return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 802 | } |
Ben Schwartz | dd878fe | 2017-05-22 10:19:25 -0400 | [diff] [blame] | 803 | unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid); |
Paul Jensen | 5240b56 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 804 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 805 | // This is writing to system/netd/server/DnsProxyListener.cpp and changes |
| 806 | // here need to be matched there. |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 807 | if (fprintf(proxy, "gethostbyname %u %s %d", |
| 808 | netid, |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 809 | name == NULL ? "^" : name, |
| 810 | af) < 0) { |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 811 | fclose(proxy); |
| 812 | return NULL; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | if (fputc(0, proxy) == EOF || fflush(proxy) != 0) { |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 816 | fclose(proxy); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 817 | return NULL; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 818 | } |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 819 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 820 | struct hostent* result = android_read_hostent(proxy, hp, hbuf, hbuflen, errorp); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 821 | fclose(proxy); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 822 | return result; |
| 823 | } |
| 824 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 825 | /* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */ |
| 826 | int gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp, char *buf, |
| 827 | size_t buflen, struct hostent **result, int *h_errnop) |
| 828 | { |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 829 | *result = android_gethostbyaddrfornetcontext_proxy_internal( |
| 830 | addr, len, af, hp, buf, buflen, h_errnop, &NETCONTEXT_UNSET); |
Elliott Hughes | bb7d9fb | 2017-10-23 17:38:35 -0700 | [diff] [blame] | 831 | return h_errno_to_result(h_errnop); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 832 | } |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 833 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 834 | static struct hostent * |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 835 | android_gethostbyaddrfornetcontext_real(const void *addr, socklen_t len, int af, struct hostent *hp, |
| 836 | char *buf, size_t buflen, int *he, |
| 837 | const struct android_net_context *netcontext) |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 838 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 839 | const u_char *uaddr = (const u_char *)addr; |
| 840 | socklen_t size; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 841 | struct getnamaddr info; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 842 | static const ns_dtab dtab[] = { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 843 | NS_FILES_CB(_hf_gethtbyaddr, NULL) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 844 | { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 845 | NS_NIS_CB(_yp_gethtbyaddr, NULL) |
| 846 | NS_NULL_CB |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 847 | }; |
| 848 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 849 | _DIAGASSERT(addr != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 850 | |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 851 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 852 | (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) || |
| 853 | IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 854 | *he = HOST_NOT_FOUND; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 855 | return NULL; |
| 856 | } |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 857 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 858 | (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) || |
| 859 | IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 860 | /* Unmap. */ |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 861 | uaddr += NS_IN6ADDRSZ - NS_INADDRSZ; |
| 862 | addr = uaddr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 863 | af = AF_INET; |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 864 | len = NS_INADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 865 | } |
| 866 | switch (af) { |
| 867 | case AF_INET: |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 868 | size = NS_INADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 869 | break; |
| 870 | case AF_INET6: |
Elliott Hughes | 3e5f0c9 | 2014-05-06 11:23:40 -0700 | [diff] [blame] | 871 | size = NS_IN6ADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 872 | break; |
| 873 | default: |
| 874 | errno = EAFNOSUPPORT; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 875 | *he = NETDB_INTERNAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 876 | return NULL; |
| 877 | } |
| 878 | if (size != len) { |
| 879 | errno = EINVAL; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 880 | *he = NETDB_INTERNAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 881 | return NULL; |
| 882 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 883 | info.hp = hp; |
| 884 | info.buf = buf; |
| 885 | info.buflen = buflen; |
| 886 | info.he = he; |
| 887 | *he = NETDB_INTERNAL; |
| 888 | if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr", |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 889 | default_dns_files, uaddr, len, af, netcontext) != NS_SUCCESS) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 890 | return NULL; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 891 | *he = NETDB_SUCCESS; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 892 | return hp; |
| 893 | } |
| 894 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 895 | static struct hostent* |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 896 | android_gethostbyaddrfornetcontext_proxy_internal(const void* addr, socklen_t len, int af, |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 897 | struct hostent *hp, char *hbuf, size_t hbuflen, int *he, |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 898 | const struct android_net_context *netcontext) |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 899 | { |
Luke Huang | e3ed892 | 2018-11-19 16:59:08 +0800 | [diff] [blame] | 900 | FILE* proxy = fdopen(__netdClientDispatch.dnsOpenProxy(), "r+"); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 901 | if (proxy == NULL) { |
| 902 | // Either we're not supposed to be using the proxy or the proxy is unavailable. |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 903 | return android_gethostbyaddrfornetcontext_real(addr,len, af, hp, hbuf, hbuflen, he, netcontext); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 904 | } |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 905 | char buf[INET6_ADDRSTRLEN]; //big enough for IPv4 and IPv6 |
| 906 | const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf)); |
| 907 | if (addrStr == NULL) { |
| 908 | fclose(proxy); |
| 909 | return NULL; |
| 910 | } |
| 911 | |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 912 | unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 913 | |
| 914 | if (fprintf(proxy, "gethostbyaddr %s %d %d %u", |
| 915 | addrStr, len, af, netid) < 0) { |
| 916 | fclose(proxy); |
| 917 | return NULL; |
| 918 | } |
| 919 | |
| 920 | if (fputc(0, proxy) == EOF || fflush(proxy) != 0) { |
| 921 | fclose(proxy); |
| 922 | return NULL; |
| 923 | } |
| 924 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 925 | struct hostent *result = android_read_hostent(proxy, hp, hbuf, hbuflen, he); |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 926 | fclose(proxy); |
| 927 | return result; |
| 928 | } |
| 929 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 930 | struct hostent* |
| 931 | netbsd_gethostent_r(FILE *hf, struct hostent *hent, char *buf, size_t buflen, int *he) |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 932 | { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 933 | char *p, *name; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 934 | char *cp, **q; |
| 935 | int af, len; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 936 | size_t anum; |
| 937 | char **aliases; |
| 938 | size_t maxaliases; |
| 939 | struct in6_addr host_addr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 940 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 941 | if (hf == NULL) { |
| 942 | *he = NETDB_INTERNAL; |
| 943 | errno = EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 944 | return NULL; |
| 945 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 946 | p = NULL; |
| 947 | setup(aliases, maxaliases); |
| 948 | |
| 949 | /* Allocate a new space to read file lines like upstream does. |
| 950 | * To keep reentrancy we cannot use __res_get_static()->hostbuf here, |
| 951 | * as the buffer may be used to store content for a previous hostent |
| 952 | * returned by non-reentrant functions like gethostbyname(). |
| 953 | */ |
| 954 | const size_t line_buf_size = sizeof(__res_get_static()->hostbuf); |
| 955 | if ((p = malloc(line_buf_size)) == NULL) { |
| 956 | goto nospc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 957 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 958 | for (;;) { |
| 959 | if (!fgets(p, line_buf_size, hf)) { |
| 960 | free(p); |
| 961 | free(aliases); |
| 962 | *he = HOST_NOT_FOUND; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 963 | return NULL; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 964 | } |
| 965 | if (*p == '#') { |
| 966 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 967 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 968 | if (!(cp = strpbrk(p, "#\n"))) { |
| 969 | continue; |
| 970 | } |
| 971 | *cp = '\0'; |
| 972 | if (!(cp = strpbrk(p, " \t"))) |
| 973 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 974 | *cp++ = '\0'; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 975 | if (inet_pton(AF_INET6, p, &host_addr) > 0) { |
| 976 | af = AF_INET6; |
| 977 | len = NS_IN6ADDRSZ; |
| 978 | } else { |
| 979 | if (inet_pton(AF_INET, p, &host_addr) <= 0) |
| 980 | continue; |
| 981 | |
| 982 | res_state res = __res_get_state(); |
| 983 | if (res == NULL) |
| 984 | goto nospc; |
| 985 | if (res->options & RES_USE_INET6) { |
| 986 | map_v4v6_address(buf, buf); |
| 987 | af = AF_INET6; |
| 988 | len = NS_IN6ADDRSZ; |
| 989 | } else { |
| 990 | af = AF_INET; |
| 991 | len = NS_INADDRSZ; |
| 992 | } |
| 993 | __res_put_state(res); |
| 994 | } |
| 995 | |
| 996 | /* if this is not something we're looking for, skip it. */ |
| 997 | if (hent->h_addrtype != 0 && hent->h_addrtype != af) |
| 998 | continue; |
| 999 | if (hent->h_length != 0 && hent->h_length != len) |
| 1000 | continue; |
| 1001 | |
| 1002 | while (*cp == ' ' || *cp == '\t') |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1003 | cp++; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1004 | if ((cp = strpbrk(name = cp, " \t")) != NULL) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1005 | *cp++ = '\0'; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1006 | q = aliases; |
| 1007 | while (cp && *cp) { |
| 1008 | if (*cp == ' ' || *cp == '\t') { |
| 1009 | cp++; |
| 1010 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1011 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1012 | addalias(q, cp, aliases, maxaliases); |
| 1013 | if ((cp = strpbrk(cp, " \t")) != NULL) |
| 1014 | *cp++ = '\0'; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1015 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1016 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1017 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1018 | hent->h_length = len; |
| 1019 | hent->h_addrtype = af; |
| 1020 | HENT_ARRAY(hent->h_addr_list, 1, buf, buflen); |
| 1021 | anum = (size_t)(q - aliases); |
| 1022 | HENT_ARRAY(hent->h_aliases, anum, buf, buflen); |
| 1023 | HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, |
| 1024 | buflen); |
| 1025 | hent->h_addr_list[1] = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1026 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1027 | HENT_SCOPY(hent->h_name, name, buf, buflen); |
| 1028 | for (size_t i = 0; i < anum; i++) |
| 1029 | HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen); |
| 1030 | hent->h_aliases[anum] = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1031 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1032 | *he = NETDB_SUCCESS; |
| 1033 | free(p); |
| 1034 | free(aliases); |
| 1035 | return hent; |
| 1036 | nospc: |
| 1037 | free(p); |
| 1038 | free(aliases); |
| 1039 | errno = ENOSPC; |
| 1040 | *he = NETDB_INTERNAL; |
| 1041 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static void |
| 1045 | map_v4v6_address(const char *src, char *dst) |
| 1046 | { |
| 1047 | u_char *p = (u_char *)dst; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1048 | char tmp[NS_INADDRSZ]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1049 | int i; |
| 1050 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1051 | _DIAGASSERT(src != NULL); |
| 1052 | _DIAGASSERT(dst != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1053 | |
| 1054 | /* Stash a temporary copy so our caller can update in place. */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1055 | (void)memcpy(tmp, src, NS_INADDRSZ); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1056 | /* Mark this ipv6 addr as a mapped ipv4. */ |
| 1057 | for (i = 0; i < 10; i++) |
| 1058 | *p++ = 0x00; |
| 1059 | *p++ = 0xff; |
| 1060 | *p++ = 0xff; |
| 1061 | /* Retrieve the saved copy and we're done. */ |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1062 | (void)memcpy(p, tmp, NS_INADDRSZ); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | static void |
| 1066 | map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) |
| 1067 | { |
| 1068 | char **ap; |
| 1069 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1070 | _DIAGASSERT(hp != NULL); |
| 1071 | _DIAGASSERT(bpp != NULL); |
| 1072 | _DIAGASSERT(ep != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1073 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1074 | if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1075 | return; |
| 1076 | hp->h_addrtype = AF_INET6; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1077 | hp->h_length = NS_IN6ADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1078 | for (ap = hp->h_addr_list; *ap; ap++) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1079 | int i = (int)(sizeof(align) - |
| 1080 | (size_t)((u_long)*bpp % sizeof(align))); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1081 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1082 | if (ep - *bpp < (i + NS_IN6ADDRSZ)) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1083 | /* Out of memory. Truncate address list here. XXX */ |
| 1084 | *ap = NULL; |
| 1085 | return; |
| 1086 | } |
| 1087 | *bpp += i; |
| 1088 | map_v4v6_address(*ap, *bpp); |
| 1089 | *ap = *bpp; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1090 | *bpp += NS_IN6ADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | static void |
| 1095 | addrsort(char **ap, int num, res_state res) |
| 1096 | { |
| 1097 | int i, j; |
| 1098 | char **p; |
| 1099 | short aval[MAXADDRS]; |
| 1100 | int needsort = 0; |
| 1101 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1102 | _DIAGASSERT(ap != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1103 | |
| 1104 | p = ap; |
| 1105 | for (i = 0; i < num; i++, p++) { |
| 1106 | for (j = 0 ; (unsigned)j < res->nsort; j++) |
| 1107 | if (res->sort_list[j].addr.s_addr == |
| 1108 | (((struct in_addr *)(void *)(*p))->s_addr & |
| 1109 | res->sort_list[j].mask)) |
| 1110 | break; |
| 1111 | aval[i] = j; |
| 1112 | if (needsort == 0 && i > 0 && j < aval[i-1]) |
| 1113 | needsort = i; |
| 1114 | } |
| 1115 | if (!needsort) |
| 1116 | return; |
| 1117 | |
| 1118 | while (needsort < num) { |
| 1119 | for (j = needsort - 1; j >= 0; j--) { |
| 1120 | if (aval[j] > aval[j+1]) { |
| 1121 | char *hp; |
| 1122 | |
| 1123 | i = aval[j]; |
| 1124 | aval[j] = aval[j+1]; |
| 1125 | aval[j+1] = i; |
| 1126 | |
| 1127 | hp = ap[j]; |
| 1128 | ap[j] = ap[j+1]; |
| 1129 | ap[j+1] = hp; |
| 1130 | } else |
| 1131 | break; |
| 1132 | } |
| 1133 | needsort++; |
| 1134 | } |
| 1135 | } |
| 1136 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1137 | /*ARGSUSED*/ |
Jim Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 1138 | static int |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1139 | _dns_gethtbyname(void *rv, void *cb_data, va_list ap) |
| 1140 | { |
| 1141 | querybuf *buf; |
| 1142 | int n, type; |
| 1143 | struct hostent *hp; |
| 1144 | const char *name; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1145 | res_state res; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1146 | struct getnamaddr *info = rv; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1147 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1148 | _DIAGASSERT(rv != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1149 | |
| 1150 | name = va_arg(ap, char *); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1151 | /* NOSTRICT skip string len */(void)va_arg(ap, int); |
| 1152 | info->hp->h_addrtype = va_arg(ap, int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1153 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1154 | switch (info->hp->h_addrtype) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1155 | case AF_INET: |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1156 | info->hp->h_length = NS_INADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1157 | type = T_A; |
| 1158 | break; |
| 1159 | case AF_INET6: |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1160 | info->hp->h_length = NS_IN6ADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1161 | type = T_AAAA; |
| 1162 | break; |
| 1163 | default: |
| 1164 | return NS_UNAVAIL; |
| 1165 | } |
| 1166 | buf = malloc(sizeof(*buf)); |
| 1167 | if (buf == NULL) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1168 | *info->he = NETDB_INTERNAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1169 | return NS_NOTFOUND; |
| 1170 | } |
| 1171 | res = __res_get_state(); |
| 1172 | if (res == NULL) { |
| 1173 | free(buf); |
| 1174 | return NS_NOTFOUND; |
| 1175 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1176 | n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1177 | if (n < 0) { |
| 1178 | free(buf); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1179 | debugprintf("res_nsearch failed (%d)\n", res, n); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1180 | __res_put_state(res); |
| 1181 | return NS_NOTFOUND; |
| 1182 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1183 | hp = getanswer(buf, n, name, type, res, info->hp, info->buf, |
| 1184 | info->buflen, info->he); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1185 | free(buf); |
| 1186 | __res_put_state(res); |
| 1187 | if (hp == NULL) |
Yabin Cui | 7069256 | 2014-12-19 10:10:04 -0800 | [diff] [blame] | 1188 | switch (*info->he) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1189 | case HOST_NOT_FOUND: |
| 1190 | return NS_NOTFOUND; |
| 1191 | case TRY_AGAIN: |
| 1192 | return NS_TRYAGAIN; |
| 1193 | default: |
| 1194 | return NS_UNAVAIL; |
| 1195 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1196 | return NS_SUCCESS; |
| 1197 | } |
| 1198 | |
| 1199 | /*ARGSUSED*/ |
Jim Huang | e5c35e0 | 2010-09-27 23:37:10 +0800 | [diff] [blame] | 1200 | static int |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1201 | _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap) |
| 1202 | { |
| 1203 | char qbuf[MAXDNAME + 1], *qp, *ep; |
| 1204 | int n; |
| 1205 | querybuf *buf; |
| 1206 | struct hostent *hp; |
| 1207 | const unsigned char *uaddr; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1208 | int advance; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1209 | res_state res; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1210 | char *bf; |
| 1211 | size_t blen; |
| 1212 | struct getnamaddr *info = rv; |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 1213 | const struct android_net_context *netcontext; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1214 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1215 | _DIAGASSERT(rv != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1216 | |
| 1217 | uaddr = va_arg(ap, unsigned char *); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1218 | info->hp->h_length = va_arg(ap, int); |
| 1219 | info->hp->h_addrtype = va_arg(ap, int); |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 1220 | netcontext = va_arg(ap, const struct android_net_context *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1221 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1222 | switch (info->hp->h_addrtype) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1223 | case AF_INET: |
| 1224 | (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", |
| 1225 | (uaddr[3] & 0xff), (uaddr[2] & 0xff), |
| 1226 | (uaddr[1] & 0xff), (uaddr[0] & 0xff)); |
| 1227 | break; |
| 1228 | |
| 1229 | case AF_INET6: |
| 1230 | qp = qbuf; |
| 1231 | ep = qbuf + sizeof(qbuf) - 1; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1232 | for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1233 | advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", |
| 1234 | uaddr[n] & 0xf, |
| 1235 | ((unsigned int)uaddr[n] >> 4) & 0xf); |
| 1236 | if (advance > 0 && qp + advance < ep) |
| 1237 | qp += advance; |
| 1238 | else { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1239 | *info->he = NETDB_INTERNAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1240 | return NS_NOTFOUND; |
| 1241 | } |
| 1242 | } |
| 1243 | if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1244 | *info->he = NETDB_INTERNAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1245 | return NS_NOTFOUND; |
| 1246 | } |
| 1247 | break; |
| 1248 | default: |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1249 | return NS_UNAVAIL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | buf = malloc(sizeof(*buf)); |
| 1253 | if (buf == NULL) { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1254 | *info->he = NETDB_INTERNAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1255 | return NS_NOTFOUND; |
| 1256 | } |
| 1257 | res = __res_get_state(); |
| 1258 | if (res == NULL) { |
| 1259 | free(buf); |
| 1260 | return NS_NOTFOUND; |
| 1261 | } |
Ben Schwartz | 90a83be | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 1262 | res_setnetcontext(res, netcontext); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1263 | n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1264 | if (n < 0) { |
| 1265 | free(buf); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1266 | debugprintf("res_nquery failed (%d)\n", res, n); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1267 | __res_put_state(res); |
| 1268 | return NS_NOTFOUND; |
| 1269 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1270 | hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, |
| 1271 | info->buflen, info->he); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1272 | free(buf); |
| 1273 | if (hp == NULL) { |
| 1274 | __res_put_state(res); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1275 | switch (*info->he) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1276 | case HOST_NOT_FOUND: |
| 1277 | return NS_NOTFOUND; |
| 1278 | case TRY_AGAIN: |
| 1279 | return NS_TRYAGAIN; |
| 1280 | default: |
| 1281 | return NS_UNAVAIL; |
| 1282 | } |
| 1283 | } |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1284 | |
| 1285 | bf = (void *)(hp->h_addr_list + 2); |
| 1286 | blen = (size_t)(bf - info->buf); |
| 1287 | if (blen + info->hp->h_length > info->buflen) |
| 1288 | goto nospc; |
| 1289 | hp->h_addr_list[0] = bf; |
| 1290 | hp->h_addr_list[1] = NULL; |
| 1291 | (void)memcpy(bf, uaddr, (size_t)info->hp->h_length); |
| 1292 | if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) { |
| 1293 | if (blen + NS_IN6ADDRSZ > info->buflen) |
| 1294 | goto nospc; |
| 1295 | map_v4v6_address(bf, bf); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1296 | hp->h_addrtype = AF_INET6; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1297 | hp->h_length = NS_IN6ADDRSZ; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | __res_put_state(res); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1301 | *info->he = NETDB_SUCCESS; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1302 | return NS_SUCCESS; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1303 | nospc: |
Yabin Cui | 7069256 | 2014-12-19 10:10:04 -0800 | [diff] [blame] | 1304 | errno = ENOSPC; |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1305 | *info->he = NETDB_INTERNAL; |
| 1306 | return NS_UNAVAIL; |
| 1307 | } |
| 1308 | |
| 1309 | #ifdef YP |
| 1310 | /*ARGSUSED*/ |
| 1311 | static struct hostent * |
| 1312 | _yp_hostent(char *line, int af, struct getnamaddr *info) |
| 1313 | { |
| 1314 | struct in6_addr host_addrs[MAXADDRS]; |
| 1315 | char **aliases; |
| 1316 | size_t maxaliases; |
| 1317 | char *p = line; |
| 1318 | char *cp, **q, *ptr; |
| 1319 | size_t len, anum, i; |
| 1320 | int addrok; |
| 1321 | int more; |
| 1322 | size_t naddrs; |
| 1323 | struct hostent *hp = info->hp; |
| 1324 | |
| 1325 | _DIAGASSERT(line != NULL); |
| 1326 | |
| 1327 | hp->h_name = NULL; |
| 1328 | hp->h_addrtype = af; |
| 1329 | switch (af) { |
| 1330 | case AF_INET: |
| 1331 | hp->h_length = NS_INADDRSZ; |
| 1332 | break; |
| 1333 | case AF_INET6: |
| 1334 | hp->h_length = NS_IN6ADDRSZ; |
| 1335 | break; |
| 1336 | default: |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | setup(aliases, maxaliases); |
| 1340 | naddrs = 0; |
| 1341 | q = aliases; |
| 1342 | |
| 1343 | nextline: |
| 1344 | /* check for host_addrs overflow */ |
| 1345 | if (naddrs >= __arraycount(host_addrs)) |
| 1346 | goto done; |
| 1347 | |
| 1348 | more = 0; |
| 1349 | cp = strpbrk(p, " \t"); |
| 1350 | if (cp == NULL) |
| 1351 | goto done; |
| 1352 | *cp++ = '\0'; |
| 1353 | |
| 1354 | /* p has should have an address */ |
| 1355 | addrok = inet_pton(af, p, &host_addrs[naddrs]); |
| 1356 | if (addrok != 1) { |
| 1357 | /* skip to the next line */ |
| 1358 | while (cp && *cp) { |
| 1359 | if (*cp == '\n') { |
| 1360 | cp++; |
| 1361 | goto nextline; |
| 1362 | } |
| 1363 | cp++; |
| 1364 | } |
| 1365 | goto done; |
| 1366 | } |
| 1367 | naddrs++; |
| 1368 | |
| 1369 | while (*cp == ' ' || *cp == '\t') |
| 1370 | cp++; |
| 1371 | p = cp; |
| 1372 | cp = strpbrk(p, " \t\n"); |
| 1373 | if (cp != NULL) { |
| 1374 | if (*cp == '\n') |
| 1375 | more = 1; |
| 1376 | *cp++ = '\0'; |
| 1377 | } |
| 1378 | if (!hp->h_name) |
| 1379 | hp->h_name = p; |
| 1380 | else if (strcmp(hp->h_name, p) == 0) |
| 1381 | ; |
| 1382 | else |
| 1383 | addalias(q, p, aliases, maxaliases); |
| 1384 | p = cp; |
| 1385 | if (more) |
| 1386 | goto nextline; |
| 1387 | |
| 1388 | while (cp && *cp) { |
| 1389 | if (*cp == ' ' || *cp == '\t') { |
| 1390 | cp++; |
| 1391 | continue; |
| 1392 | } |
| 1393 | if (*cp == '\n') { |
| 1394 | cp++; |
| 1395 | goto nextline; |
| 1396 | } |
| 1397 | addalias(q, cp, aliases, maxaliases); |
| 1398 | cp = strpbrk(cp, " \t"); |
| 1399 | if (cp != NULL) |
| 1400 | *cp++ = '\0'; |
| 1401 | } |
| 1402 | |
| 1403 | done: |
| 1404 | if (hp->h_name == NULL) { |
| 1405 | free(aliases); |
| 1406 | return NULL; |
| 1407 | } |
| 1408 | |
| 1409 | ptr = info->buf; |
| 1410 | len = info->buflen; |
| 1411 | |
| 1412 | anum = (size_t)(q - aliases); |
| 1413 | HENT_ARRAY(hp->h_addr_list, naddrs, ptr, len); |
| 1414 | HENT_ARRAY(hp->h_aliases, anum, ptr, len); |
| 1415 | |
| 1416 | for (i = 0; i < naddrs; i++) |
| 1417 | HENT_COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length, |
| 1418 | ptr, len); |
| 1419 | hp->h_addr_list[naddrs] = NULL; |
| 1420 | |
| 1421 | HENT_SCOPY(hp->h_name, hp->h_name, ptr, len); |
| 1422 | |
| 1423 | for (i = 0; i < anum; i++) |
| 1424 | HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len); |
| 1425 | hp->h_aliases[anum] = NULL; |
| 1426 | free(aliases); |
| 1427 | |
| 1428 | return hp; |
| 1429 | nospc: |
| 1430 | free(aliases); |
| 1431 | *info->he = NETDB_INTERNAL; |
| 1432 | errno = ENOSPC; |
| 1433 | return NULL; |
| 1434 | } |
| 1435 | |
| 1436 | /*ARGSUSED*/ |
| 1437 | int |
| 1438 | _yp_gethtbyaddr(void *rv, void *cb_data, va_list ap) |
| 1439 | { |
| 1440 | struct hostent *hp = NULL; |
| 1441 | char *ypcurrent; |
| 1442 | int ypcurrentlen, r; |
| 1443 | char name[INET6_ADDRSTRLEN]; /* XXX enough? */ |
| 1444 | const unsigned char *uaddr; |
| 1445 | int af; |
| 1446 | const char *map; |
| 1447 | struct getnamaddr *info = rv; |
| 1448 | |
| 1449 | _DIAGASSERT(rv != NULL); |
| 1450 | |
| 1451 | uaddr = va_arg(ap, unsigned char *); |
| 1452 | /* NOSTRICT skip len */(void)va_arg(ap, int); |
| 1453 | af = va_arg(ap, int); |
| 1454 | |
| 1455 | if (!__ypdomain) { |
| 1456 | if (_yp_check(&__ypdomain) == 0) |
| 1457 | return NS_UNAVAIL; |
| 1458 | } |
| 1459 | /* |
| 1460 | * XXX unfortunately, we cannot support IPv6 extended scoped address |
| 1461 | * notation here. gethostbyaddr() is not scope-aware. too bad. |
| 1462 | */ |
| 1463 | if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL) |
| 1464 | return NS_UNAVAIL; |
| 1465 | switch (af) { |
| 1466 | case AF_INET: |
| 1467 | map = "hosts.byaddr"; |
| 1468 | break; |
| 1469 | default: |
| 1470 | map = "ipnodes.byaddr"; |
| 1471 | break; |
| 1472 | } |
| 1473 | ypcurrent = NULL; |
| 1474 | r = yp_match(__ypdomain, map, name, |
| 1475 | (int)strlen(name), &ypcurrent, &ypcurrentlen); |
| 1476 | if (r == 0) |
| 1477 | hp = _yp_hostent(ypcurrent, af, info); |
| 1478 | else |
| 1479 | hp = NULL; |
| 1480 | free(ypcurrent); |
| 1481 | if (hp == NULL) { |
| 1482 | *info->he = HOST_NOT_FOUND; |
| 1483 | return NS_NOTFOUND; |
| 1484 | } |
| 1485 | return NS_SUCCESS; |
| 1486 | } |
| 1487 | |
| 1488 | /*ARGSUSED*/ |
| 1489 | int |
| 1490 | _yp_gethtbyname(void *rv, void *cb_data, va_list ap) |
| 1491 | { |
| 1492 | struct hostent *hp; |
| 1493 | char *ypcurrent; |
| 1494 | int ypcurrentlen, r; |
| 1495 | const char *name; |
| 1496 | int af; |
| 1497 | const char *map; |
| 1498 | struct getnamaddr *info = rv; |
| 1499 | |
| 1500 | _DIAGASSERT(rv != NULL); |
| 1501 | |
| 1502 | name = va_arg(ap, char *); |
| 1503 | /* NOSTRICT skip string len */(void)va_arg(ap, int); |
| 1504 | af = va_arg(ap, int); |
| 1505 | |
| 1506 | if (!__ypdomain) { |
| 1507 | if (_yp_check(&__ypdomain) == 0) |
| 1508 | return NS_UNAVAIL; |
| 1509 | } |
| 1510 | switch (af) { |
| 1511 | case AF_INET: |
| 1512 | map = "hosts.byname"; |
| 1513 | break; |
| 1514 | default: |
| 1515 | map = "ipnodes.byname"; |
| 1516 | break; |
| 1517 | } |
| 1518 | ypcurrent = NULL; |
| 1519 | r = yp_match(__ypdomain, map, name, |
| 1520 | (int)strlen(name), &ypcurrent, &ypcurrentlen); |
| 1521 | if (r == 0) |
| 1522 | hp = _yp_hostent(ypcurrent, af, info); |
| 1523 | else |
| 1524 | hp = NULL; |
| 1525 | free(ypcurrent); |
| 1526 | if (hp == NULL) { |
| 1527 | *info->he = HOST_NOT_FOUND; |
| 1528 | return NS_NOTFOUND; |
| 1529 | } |
| 1530 | return NS_SUCCESS; |
| 1531 | } |
| 1532 | #endif |
| 1533 | |
| 1534 | /* |
| 1535 | * Non-reentrant versions. |
| 1536 | */ |
| 1537 | |
Lev Rumyantsev | 814f38f | 2020-10-12 17:33:06 -0700 | [diff] [blame^] | 1538 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1539 | struct hostent * |
| 1540 | gethostbyname(const char *name) |
| 1541 | { |
| 1542 | struct hostent *result = NULL; |
| 1543 | res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */ |
| 1544 | |
| 1545 | gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno); |
| 1546 | return result; |
| 1547 | } |
| 1548 | |
| 1549 | struct hostent * |
| 1550 | gethostbyname2(const char *name, int af) |
| 1551 | { |
| 1552 | struct hostent *result = NULL; |
| 1553 | res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */ |
| 1554 | |
| 1555 | gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno); |
| 1556 | return result; |
| 1557 | } |
| 1558 | |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 1559 | // android_gethostby*fornet can be called in two different contexts. |
| 1560 | // - In the proxy client context (proxy != NULL), |netid| is |app_netid|. |
| 1561 | // - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|. |
| 1562 | // The netcontext is constructed before checking which context we are in. |
| 1563 | // Therefore, we have to populate both fields, and rely on the downstream code to check whether |
| 1564 | // |proxy == NULL|, and use that info to query the field that matches the caller's intent. |
| 1565 | static struct android_net_context make_context(unsigned netid, unsigned mark) { |
| 1566 | struct android_net_context netcontext = NETCONTEXT_UNSET; |
| 1567 | netcontext.app_netid = netid; |
| 1568 | netcontext.app_mark = mark; |
| 1569 | netcontext.dns_netid = netid; |
| 1570 | netcontext.dns_mark = mark; |
| 1571 | return netcontext; |
| 1572 | } |
| 1573 | |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1574 | struct hostent * |
| 1575 | android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark) |
| 1576 | { |
Ben Schwartz | dd878fe | 2017-05-22 10:19:25 -0400 | [diff] [blame] | 1577 | const struct android_net_context netcontext = make_context(netid, mark); |
| 1578 | return android_gethostbynamefornetcontext(name, af, &netcontext); |
| 1579 | } |
| 1580 | |
| 1581 | struct hostent * |
| 1582 | android_gethostbynamefornetcontext(const char *name, int af, |
| 1583 | const struct android_net_context *netcontext) |
| 1584 | { |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1585 | struct hostent *hp; |
| 1586 | res_state res = __res_get_state(); |
| 1587 | if (res == NULL) |
| 1588 | return NULL; |
| 1589 | res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */ |
| 1590 | hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), |
Ben Schwartz | dd878fe | 2017-05-22 10:19:25 -0400 | [diff] [blame] | 1591 | &h_errno, netcontext); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1592 | __res_put_state(res); |
| 1593 | return hp; |
| 1594 | } |
| 1595 | |
Lev Rumyantsev | 814f38f | 2020-10-12 17:33:06 -0700 | [diff] [blame^] | 1596 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1597 | struct hostent * |
| 1598 | gethostbyaddr(const void *addr, socklen_t len, int af) |
| 1599 | { |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 1600 | return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | struct hostent * |
| 1604 | android_gethostbyaddrfornet(const void *addr, socklen_t len, int af, unsigned netid, unsigned mark) |
| 1605 | { |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 1606 | const struct android_net_context netcontext = make_context(netid, mark); |
| 1607 | return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext); |
| 1608 | } |
| 1609 | |
| 1610 | struct hostent * |
| 1611 | android_gethostbyaddrfornetcontext(const void *addr, socklen_t len, int af, |
| 1612 | const struct android_net_context *netcontext) |
| 1613 | { |
| 1614 | return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | __LIBC_HIDDEN__ struct hostent* |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 1618 | android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af, |
| 1619 | const struct android_net_context *netcontext) |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1620 | { |
| 1621 | res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */ |
Ben Schwartz | 5017805 | 2017-04-24 17:57:11 -0400 | [diff] [blame] | 1622 | return android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, &rs->host, rs->hostbuf, |
| 1623 | sizeof(rs->hostbuf), &h_errno, netcontext); |
Yabin Cui | 58d33a5 | 2014-12-16 17:03:44 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | struct hostent * |
| 1627 | gethostent(void) |
| 1628 | { |
| 1629 | res_static rs = __res_get_static(); |
| 1630 | if (!rs->hostf) { |
| 1631 | sethostent_r(&rs->hostf); |
| 1632 | if (!rs->hostf) { |
| 1633 | h_errno = NETDB_INTERNAL; |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | } |
| 1637 | memset(&rs->host, 0, sizeof(rs->host)); |
| 1638 | return netbsd_gethostent_r(rs->hostf, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1639 | } |