Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 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 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 54 | #include <arpa/inet.h> |
| 55 | #include <arpa/nameser.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 56 | #include <assert.h> |
| 57 | #include <ctype.h> |
| 58 | #include <errno.h> |
| 59 | #include <netdb.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 60 | #include <netinet/in.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 61 | #include <stdarg.h> |
| 62 | #include <stdbool.h> |
| 63 | #include <stdio.h> |
| 64 | #include <strings.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 65 | #include <sys/param.h> |
| 66 | #include <sys/socket.h> |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 67 | #include <sys/types.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 68 | #include <sys/un.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 69 | #include <syslog.h> |
| 70 | #include <unistd.h> |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 71 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 72 | #include "resolv_cache.h" |
| 73 | #include "resolv_netid.h" |
| 74 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 75 | |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 76 | // NetBSD uses _DIAGASSERT to null-check arguments and the like, |
| 77 | // but it's clear from the number of mistakes in their assertions |
| 78 | // that they don't actually test or ship with this. |
| 79 | #define _DIAGASSERT(e) /* nothing */ |
| 80 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 81 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 82 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 83 | |
| 84 | #ifndef LOG_AUTH |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 85 | #define LOG_AUTH 0 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 86 | #endif |
| 87 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 88 | #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 89 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 90 | #include <stdlib.h> |
| 91 | #include <string.h> |
| 92 | |
| 93 | #include "hostent.h" |
| 94 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 95 | #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 96 | #define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok) |
| 97 | #define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok) |
| 98 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 99 | #define addalias(d, s, arr, siz) \ |
| 100 | do { \ |
| 101 | if (d >= &arr[siz]) { \ |
| 102 | char** xptr = (char**) realloc(arr, (siz + 10) * sizeof(*arr)); \ |
| 103 | if (xptr == NULL) goto nospc; \ |
| 104 | d = xptr + (d - arr); \ |
| 105 | arr = xptr; \ |
| 106 | siz += 10; \ |
| 107 | } \ |
| 108 | *d++ = s; \ |
| 109 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 110 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 111 | static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\""; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 112 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 113 | static const struct android_net_context NETCONTEXT_UNSET = {.app_mark = MARK_UNSET, |
| 114 | .app_netid = NETID_UNSET, |
| 115 | .dns_mark = MARK_UNSET, |
| 116 | .dns_netid = NETID_UNSET, |
| 117 | .uid = NET_CONTEXT_INVALID_UID}; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 118 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | #define MAXPACKET (8 * 1024) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 120 | |
| 121 | typedef union { |
| 122 | HEADER hdr; |
| 123 | u_char buf[MAXPACKET]; |
| 124 | } querybuf; |
| 125 | |
| 126 | typedef union { |
| 127 | int32_t al; |
| 128 | char ac; |
| 129 | } align; |
| 130 | |
| 131 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 132 | static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3))); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 133 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 134 | static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*, |
| 135 | char*, size_t, int*); |
| 136 | static void map_v4v6_address(const char*, char*); |
| 137 | static void map_v4v6_hostent(struct hostent*, char**, char*); |
| 138 | static void addrsort(char**, int, res_state); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 139 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 140 | struct hostent* ht_gethostbyname(char*); |
| 141 | struct hostent* ht_gethostbyaddr(const char*, int, int); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 142 | static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, |
| 143 | const android_net_context* netcontext, getnamaddr* info); |
| 144 | static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 145 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 146 | static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*, |
| 147 | size_t, int*, const struct android_net_context*); |
| 148 | static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal( |
| 149 | const void*, socklen_t, int, struct hostent*, char*, size_t, int*, |
| 150 | const struct android_net_context*); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 151 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 152 | static int h_errno_to_result(int* herrno_p) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 153 | // glibc considers ERANGE a special case (and BSD uses ENOSPC instead). |
| 154 | if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) { |
| 155 | errno = ERANGE; |
| 156 | return errno; |
| 157 | } |
| 158 | // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value. |
| 159 | return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 163 | static void debugprintf(const char* msg, res_state res, ...) { |
| 164 | _DIAGASSERT(msg != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 165 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 166 | if (res->options & RES_DEBUG) { |
| 167 | int save = errno; |
| 168 | va_list ap; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 169 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 170 | va_start(ap, res); |
| 171 | vprintf(msg, ap); |
| 172 | va_end(ap); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 173 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 174 | errno = save; |
| 175 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 176 | } |
| 177 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 178 | #define debugprintf(msg, res, num) /*nada*/ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 179 | #endif |
| 180 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 181 | #define BOUNDED_INCR(x) \ |
| 182 | do { \ |
| 183 | BOUNDS_CHECK(cp, x); \ |
| 184 | cp += (x); \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 185 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 186 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 187 | #define BOUNDS_CHECK(ptr, count) \ |
| 188 | do { \ |
| 189 | if (eom - (ptr) < (count)) goto no_recovery; \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 190 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 191 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 192 | static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype, |
| 193 | res_state res, struct hostent* hent, char* buf, size_t buflen, |
| 194 | int* he) { |
| 195 | const HEADER* hp; |
| 196 | const u_char* cp; |
| 197 | int n; |
| 198 | size_t qlen; |
| 199 | const u_char *eom, *erdata; |
| 200 | char *bp, **ap, **hap, *ep; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 201 | int ancount, qdcount; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 202 | int haveanswer, had_error; |
| 203 | int toobig = 0; |
| 204 | char tbuf[MAXDNAME]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 205 | char* addr_ptrs[MAXADDRS]; |
| 206 | const char* tname; |
| 207 | int (*name_ok)(const char*); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 208 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 209 | _DIAGASSERT(answer != NULL); |
| 210 | _DIAGASSERT(qname != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 211 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 212 | tname = qname; |
| 213 | hent->h_name = NULL; |
| 214 | eom = answer->buf + anslen; |
| 215 | switch (qtype) { |
| 216 | case T_A: |
| 217 | case T_AAAA: |
| 218 | name_ok = res_hnok; |
| 219 | break; |
| 220 | case T_PTR: |
| 221 | name_ok = res_dnok; |
| 222 | break; |
| 223 | default: |
| 224 | *he = NO_RECOVERY; |
| 225 | return NULL; /* XXX should be abort(); */ |
| 226 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 227 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 228 | size_t maxaliases = 10; |
| 229 | char** aliases = (char**) malloc(maxaliases * sizeof(char*)); |
| 230 | if (!aliases) goto nospc; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 231 | /* |
| 232 | * find first satisfactory answer |
| 233 | */ |
| 234 | hp = &answer->hdr; |
| 235 | ancount = ntohs(hp->ancount); |
| 236 | qdcount = ntohs(hp->qdcount); |
| 237 | bp = buf; |
| 238 | ep = buf + buflen; |
| 239 | cp = answer->buf; |
| 240 | BOUNDED_INCR(HFIXEDSZ); |
| 241 | if (qdcount != 1) goto no_recovery; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 242 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 243 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 244 | if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 245 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 246 | BOUNDED_INCR(n + QFIXEDSZ); |
| 247 | if (qtype == T_A || qtype == T_AAAA) { |
| 248 | /* res_send() has already verified that the query name is the |
| 249 | * same as the one we sent; this just gets the expanded name |
| 250 | * (i.e., with the succeeding search-domain tacked on). |
| 251 | */ |
| 252 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 253 | if (n >= MAXHOSTNAMELEN) goto no_recovery; |
| 254 | hent->h_name = bp; |
| 255 | bp += n; |
| 256 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 257 | qname = hent->h_name; |
| 258 | } |
| 259 | hent->h_aliases = ap = aliases; |
| 260 | hent->h_addr_list = hap = addr_ptrs; |
| 261 | *ap = NULL; |
| 262 | *hap = NULL; |
| 263 | haveanswer = 0; |
| 264 | had_error = 0; |
| 265 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 266 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 267 | if ((n < 0) || !maybe_ok(res, bp, name_ok)) { |
| 268 | had_error++; |
| 269 | continue; |
| 270 | } |
| 271 | cp += n; /* name */ |
| 272 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 273 | int type = _getshort(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 274 | cp += INT16SZ; /* type */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 275 | int cl = _getshort(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 276 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
| 277 | n = _getshort(cp); |
| 278 | cp += INT16SZ; /* len */ |
| 279 | BOUNDS_CHECK(cp, n); |
| 280 | erdata = cp + n; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 281 | if (cl != C_IN) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 282 | /* XXX - debug? syslog? */ |
| 283 | cp += n; |
| 284 | continue; /* XXX - had_error++ ? */ |
| 285 | } |
| 286 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
| 287 | n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf); |
| 288 | if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) { |
| 289 | had_error++; |
| 290 | continue; |
| 291 | } |
| 292 | cp += n; |
| 293 | if (cp != erdata) goto no_recovery; |
| 294 | /* Store alias. */ |
| 295 | addalias(ap, bp, aliases, maxaliases); |
| 296 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 297 | if (n >= MAXHOSTNAMELEN) { |
| 298 | had_error++; |
| 299 | continue; |
| 300 | } |
| 301 | bp += n; |
| 302 | /* Get canonical name. */ |
| 303 | n = (int) strlen(tbuf) + 1; /* for the \0 */ |
| 304 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 305 | had_error++; |
| 306 | continue; |
| 307 | } |
| 308 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 309 | hent->h_name = bp; |
| 310 | bp += n; |
| 311 | continue; |
| 312 | } |
| 313 | if (qtype == T_PTR && type == T_CNAME) { |
| 314 | n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf); |
| 315 | if (n < 0 || !maybe_dnok(res, tbuf)) { |
| 316 | had_error++; |
| 317 | continue; |
| 318 | } |
| 319 | cp += n; |
| 320 | if (cp != erdata) goto no_recovery; |
| 321 | /* Get canonical name. */ |
| 322 | n = (int) strlen(tbuf) + 1; /* for the \0 */ |
| 323 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 324 | had_error++; |
| 325 | continue; |
| 326 | } |
| 327 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 328 | tname = bp; |
| 329 | bp += n; |
| 330 | continue; |
| 331 | } |
| 332 | if (type != qtype) { |
| 333 | if (type != T_KEY && type != T_SIG) |
| 334 | syslog(LOG_NOTICE | LOG_AUTH, |
| 335 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname, |
| 336 | p_class(C_IN), p_type(qtype), p_type(type)); |
| 337 | cp += n; |
| 338 | continue; /* XXX - had_error++ ? */ |
| 339 | } |
| 340 | switch (type) { |
| 341 | case T_PTR: |
| 342 | if (strcasecmp(tname, bp) != 0) { |
| 343 | syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp); |
| 344 | cp += n; |
| 345 | continue; /* XXX - had_error++ ? */ |
| 346 | } |
| 347 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 348 | if ((n < 0) || !maybe_hnok(res, bp)) { |
| 349 | had_error++; |
| 350 | break; |
| 351 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 352 | #if MULTI_PTRS_ARE_ALIASES |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 353 | cp += n; |
| 354 | if (cp != erdata) goto no_recovery; |
| 355 | if (!haveanswer) |
| 356 | hent->h_name = bp; |
| 357 | else |
| 358 | addalias(ap, bp, aliases, maxaliases); |
| 359 | if (n != -1) { |
| 360 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 361 | if (n >= MAXHOSTNAMELEN) { |
| 362 | had_error++; |
| 363 | break; |
| 364 | } |
| 365 | bp += n; |
| 366 | } |
| 367 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 368 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 369 | hent->h_name = bp; |
| 370 | if (res->options & RES_USE_INET6) { |
| 371 | n = strlen(bp) + 1; /* for the \0 */ |
| 372 | if (n >= MAXHOSTNAMELEN) { |
| 373 | had_error++; |
| 374 | break; |
| 375 | } |
| 376 | bp += n; |
| 377 | map_v4v6_hostent(hent, &bp, ep); |
| 378 | } |
| 379 | goto success; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 380 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 381 | case T_A: |
| 382 | case T_AAAA: |
| 383 | if (strcasecmp(hent->h_name, bp) != 0) { |
| 384 | syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp); |
| 385 | cp += n; |
| 386 | continue; /* XXX - had_error++ ? */ |
| 387 | } |
| 388 | if (n != hent->h_length) { |
| 389 | cp += n; |
| 390 | continue; |
| 391 | } |
| 392 | if (type == T_AAAA) { |
| 393 | struct in6_addr in6; |
| 394 | memcpy(&in6, cp, NS_IN6ADDRSZ); |
| 395 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 396 | cp += n; |
| 397 | continue; |
| 398 | } |
| 399 | } |
| 400 | if (!haveanswer) { |
| 401 | int nn; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 402 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 403 | hent->h_name = bp; |
| 404 | nn = (int) strlen(bp) + 1; /* for the \0 */ |
| 405 | bp += nn; |
| 406 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 407 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 408 | bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 409 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 410 | if (bp + n >= ep) { |
| 411 | debugprintf("size (%d) too big\n", res, n); |
| 412 | had_error++; |
| 413 | continue; |
| 414 | } |
| 415 | if (hap >= &addr_ptrs[MAXADDRS - 1]) { |
| 416 | if (!toobig++) { |
| 417 | debugprintf("Too many addresses (%d)\n", res, MAXADDRS); |
| 418 | } |
| 419 | cp += n; |
| 420 | continue; |
| 421 | } |
| 422 | (void) memcpy(*hap++ = bp, cp, (size_t) n); |
| 423 | bp += n; |
| 424 | cp += n; |
| 425 | if (cp != erdata) goto no_recovery; |
| 426 | break; |
| 427 | default: |
| 428 | abort(); |
| 429 | } |
| 430 | if (!had_error) haveanswer++; |
| 431 | } |
| 432 | if (haveanswer) { |
| 433 | *ap = NULL; |
| 434 | *hap = NULL; |
| 435 | /* |
| 436 | * Note: we sort even if host can take only one address |
| 437 | * in its return structures - should give it the "best" |
| 438 | * address in that case, not some random one |
| 439 | */ |
| 440 | if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res); |
| 441 | if (!hent->h_name) { |
| 442 | n = (int) strlen(qname) + 1; /* for the \0 */ |
| 443 | if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery; |
| 444 | strlcpy(bp, qname, (size_t)(ep - bp)); |
| 445 | hent->h_name = bp; |
| 446 | bp += n; |
| 447 | } |
| 448 | if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep); |
| 449 | goto success; |
| 450 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 451 | no_recovery: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 452 | free(aliases); |
| 453 | *he = NO_RECOVERY; |
| 454 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 455 | success: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 456 | bp = (char*) ALIGN(bp); |
| 457 | n = (int) (ap - aliases); |
| 458 | qlen = (n + 1) * sizeof(*hent->h_aliases); |
| 459 | if ((size_t)(ep - bp) < qlen) goto nospc; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 460 | hent->h_aliases = (char**) bp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 461 | memcpy(bp, aliases, qlen); |
| 462 | free(aliases); |
| 463 | aliases = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 464 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 465 | bp += qlen; |
| 466 | n = (int) (hap - addr_ptrs); |
| 467 | qlen = (n + 1) * sizeof(*hent->h_addr_list); |
| 468 | if ((size_t)(ep - bp) < qlen) goto nospc; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 469 | hent->h_addr_list = (char**) bp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 470 | memcpy(bp, addr_ptrs, qlen); |
| 471 | *he = NETDB_SUCCESS; |
| 472 | return hent; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 473 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 474 | free(aliases); |
| 475 | errno = ENOSPC; |
| 476 | *he = NETDB_INTERNAL; |
| 477 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* The prototype of gethostbyname_r is from glibc, not that in netbsd. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 481 | int gethostbyname_r(const char* name, struct hostent* hp, char* buf, size_t buflen, |
| 482 | struct hostent** result, int* errorp) { |
| 483 | res_state res = __res_get_state(); |
| 484 | if (res == NULL) { |
| 485 | *result = NULL; |
| 486 | *errorp = NETDB_INTERNAL; |
| 487 | return -1; |
| 488 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 489 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 490 | _DIAGASSERT(name != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 491 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 492 | if (res->options & RES_USE_INET6) { |
| 493 | *result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp, |
| 494 | &NETCONTEXT_UNSET); |
| 495 | if (*result) { |
| 496 | __res_put_state(res); |
| 497 | return 0; |
| 498 | } |
| 499 | } |
| 500 | *result = |
| 501 | gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET); |
| 502 | return h_errno_to_result(errorp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 506 | int gethostbyname2_r(const char* name, int af, struct hostent* hp, char* buf, size_t buflen, |
| 507 | struct hostent** result, int* errorp) { |
| 508 | res_state res = __res_get_state(); |
| 509 | if (res == NULL) { |
| 510 | *result = NULL; |
| 511 | *errorp = NETDB_INTERNAL; |
| 512 | return -1; |
| 513 | } |
| 514 | *result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, &NETCONTEXT_UNSET); |
| 515 | return h_errno_to_result(errorp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | __LIBC_HIDDEN__ FILE* android_open_proxy() { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 519 | const char* cache_mode = getenv("ANDROID_DNS_MODE"); |
| 520 | bool use_proxy = (cache_mode == NULL || strcmp(cache_mode, "local") != 0); |
| 521 | if (!use_proxy) { |
| 522 | return NULL; |
| 523 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 524 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 525 | int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 526 | if (s == -1) { |
| 527 | return NULL; |
| 528 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 529 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 530 | const int one = 1; |
| 531 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 532 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 533 | struct sockaddr_un proxy_addr; |
| 534 | memset(&proxy_addr, 0, sizeof(proxy_addr)); |
| 535 | proxy_addr.sun_family = AF_UNIX; |
| 536 | strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", sizeof(proxy_addr.sun_path)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 537 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 538 | if (TEMP_FAILURE_RETRY(connect(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) != |
| 539 | 0) { |
| 540 | close(s); |
| 541 | return NULL; |
| 542 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 543 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 544 | return fdopen(s, "r+"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 545 | } |
| 546 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 547 | static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res, |
| 548 | struct hostent* hp, char* buf, size_t buflen, |
| 549 | int* he) { |
| 550 | const char* cp; |
| 551 | struct getnamaddr info; |
| 552 | char hbuf[MAXHOSTNAMELEN]; |
| 553 | size_t size; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 554 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 555 | _DIAGASSERT(name != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 556 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 557 | switch (af) { |
| 558 | case AF_INET: |
| 559 | size = NS_INADDRSZ; |
| 560 | break; |
| 561 | case AF_INET6: |
| 562 | size = NS_IN6ADDRSZ; |
| 563 | break; |
| 564 | default: |
| 565 | *he = NETDB_INTERNAL; |
| 566 | errno = EAFNOSUPPORT; |
| 567 | return NULL; |
| 568 | } |
| 569 | if (buflen < size) goto nospc; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 570 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 571 | hp->h_addrtype = af; |
| 572 | hp->h_length = (int) size; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 573 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 574 | /* |
| 575 | * if there aren't any dots, it could be a user-level alias. |
| 576 | * this is also done in res_nquery() since we are not the only |
| 577 | * function that looks up host names. |
| 578 | */ |
| 579 | if (!strchr(name, '.') && (cp = res_hostalias(res, name, hbuf, sizeof(hbuf)))) name = cp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 580 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 581 | /* |
| 582 | * disallow names consisting only of digits/dots, unless |
| 583 | * they end in a dot. |
| 584 | */ |
| 585 | if (isdigit((u_char) name[0])) |
| 586 | for (cp = name;; ++cp) { |
| 587 | if (!*cp) { |
| 588 | if (*--cp == '.') break; |
| 589 | /* |
| 590 | * All-numeric, no dot at the end. |
| 591 | * Fake up a hostent as if we'd actually |
| 592 | * done a lookup. |
| 593 | */ |
| 594 | goto fake; |
| 595 | } |
| 596 | if (!isdigit((u_char) *cp) && *cp != '.') break; |
| 597 | } |
| 598 | if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') |
| 599 | for (cp = name;; ++cp) { |
| 600 | if (!*cp) { |
| 601 | if (*--cp == '.') break; |
| 602 | /* |
| 603 | * All-IPv6-legal, no dot at the end. |
| 604 | * Fake up a hostent as if we'd actually |
| 605 | * done a lookup. |
| 606 | */ |
| 607 | goto fake; |
| 608 | } |
| 609 | if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break; |
| 610 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 611 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 612 | *he = NETDB_INTERNAL; |
| 613 | info.hp = hp; |
| 614 | info.buf = buf; |
| 615 | info.buflen = buflen; |
| 616 | info.he = he; |
Bernie Innocenti | 9bf0e1d | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 617 | if (!_hf_gethtbyname2(name, af, &info)) { |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 618 | if (!_dns_gethtbyname(name, af, &info)) { |
Bernie Innocenti | 9bf0e1d | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 619 | return NULL; |
| 620 | } |
| 621 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 622 | *he = NETDB_SUCCESS; |
| 623 | return hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 624 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 625 | *he = NETDB_INTERNAL; |
| 626 | errno = ENOSPC; |
| 627 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 628 | fake: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 629 | HENT_ARRAY(hp->h_addr_list, 1, buf, buflen); |
| 630 | HENT_ARRAY(hp->h_aliases, 0, buf, buflen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 631 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 632 | hp->h_aliases[0] = NULL; |
| 633 | if (size > buflen) goto nospc; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 634 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 635 | if (inet_pton(af, name, buf) <= 0) { |
| 636 | *he = HOST_NOT_FOUND; |
| 637 | return NULL; |
| 638 | } |
| 639 | hp->h_addr_list[0] = buf; |
| 640 | hp->h_addr_list[1] = NULL; |
| 641 | buf += size; |
| 642 | buflen -= size; |
| 643 | HENT_SCOPY(hp->h_name, name, buf, buflen); |
| 644 | if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen); |
| 645 | *he = NETDB_SUCCESS; |
| 646 | return hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // very similar in proxy-ness to android_getaddrinfo_proxy |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 650 | static struct hostent* gethostbyname_internal(const char* name, int af, res_state res, |
| 651 | struct hostent* hp, char* hbuf, size_t hbuflen, |
| 652 | int* errorp, |
| 653 | const struct android_net_context* netcontext) { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 654 | res_setnetcontext(res, netcontext); |
| 655 | return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 659 | int gethostbyaddr_r(const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, |
| 660 | size_t buflen, struct hostent** result, int* h_errnop) { |
| 661 | *result = android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, hp, buf, buflen, |
| 662 | h_errnop, &NETCONTEXT_UNSET); |
| 663 | return h_errno_to_result(h_errnop); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 664 | } |
| 665 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 666 | static struct hostent* android_gethostbyaddrfornetcontext_real( |
| 667 | const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen, |
| 668 | int* he, const struct android_net_context* netcontext) { |
| 669 | const u_char* uaddr = (const u_char*) addr; |
| 670 | socklen_t size; |
| 671 | struct getnamaddr info; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 672 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 673 | _DIAGASSERT(addr != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 674 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 675 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 676 | (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) || |
| 677 | IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) { |
| 678 | *he = HOST_NOT_FOUND; |
| 679 | return NULL; |
| 680 | } |
| 681 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 682 | (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) || |
| 683 | IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) { |
| 684 | /* Unmap. */ |
| 685 | uaddr += NS_IN6ADDRSZ - NS_INADDRSZ; |
| 686 | addr = uaddr; |
| 687 | af = AF_INET; |
| 688 | len = NS_INADDRSZ; |
| 689 | } |
| 690 | switch (af) { |
| 691 | case AF_INET: |
| 692 | size = NS_INADDRSZ; |
| 693 | break; |
| 694 | case AF_INET6: |
| 695 | size = NS_IN6ADDRSZ; |
| 696 | break; |
| 697 | default: |
| 698 | errno = EAFNOSUPPORT; |
| 699 | *he = NETDB_INTERNAL; |
| 700 | return NULL; |
| 701 | } |
| 702 | if (size != len) { |
| 703 | errno = EINVAL; |
| 704 | *he = NETDB_INTERNAL; |
| 705 | return NULL; |
| 706 | } |
| 707 | info.hp = hp; |
| 708 | info.buf = buf; |
| 709 | info.buflen = buflen; |
| 710 | info.he = he; |
| 711 | *he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 712 | if (!_hf_gethtbyaddr(uaddr, len, af, &info)) { |
| 713 | if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) { |
| 714 | return NULL; |
| 715 | } |
| 716 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 717 | *he = NETDB_SUCCESS; |
| 718 | return hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 719 | } |
| 720 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 721 | static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal( |
| 722 | const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen, |
| 723 | int* he, const struct android_net_context* netcontext) { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 724 | return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he, |
| 725 | netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 726 | } |
| 727 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 728 | struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen, |
| 729 | int* he) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 730 | const size_t line_buf_size = sizeof(__res_get_static()->hostbuf); |
| 731 | char *name; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 732 | char *cp, **q; |
| 733 | int af, len; |
| 734 | size_t anum; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 735 | struct in6_addr host_addr; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 736 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 737 | if (hf == NULL) { |
| 738 | *he = NETDB_INTERNAL; |
| 739 | errno = EINVAL; |
| 740 | return NULL; |
| 741 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 742 | char* p = NULL; |
| 743 | size_t maxaliases = 10; |
| 744 | char** aliases = (char**) malloc(maxaliases * sizeof(char*)); |
| 745 | if (!aliases) goto nospc; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 746 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 747 | /* Allocate a new space to read file lines like upstream does. |
| 748 | * To keep reentrancy we cannot use __res_get_static()->hostbuf here, |
| 749 | * as the buffer may be used to store content for a previous hostent |
| 750 | * returned by non-reentrant functions like gethostbyname(). |
| 751 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 752 | if ((p = (char*) malloc(line_buf_size)) == NULL) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 753 | goto nospc; |
| 754 | } |
| 755 | for (;;) { |
| 756 | if (!fgets(p, line_buf_size, hf)) { |
| 757 | free(p); |
| 758 | free(aliases); |
| 759 | *he = HOST_NOT_FOUND; |
| 760 | return NULL; |
| 761 | } |
| 762 | if (*p == '#') { |
| 763 | continue; |
| 764 | } |
| 765 | if (!(cp = strpbrk(p, "#\n"))) { |
| 766 | continue; |
| 767 | } |
| 768 | *cp = '\0'; |
| 769 | if (!(cp = strpbrk(p, " \t"))) continue; |
| 770 | *cp++ = '\0'; |
| 771 | if (inet_pton(AF_INET6, p, &host_addr) > 0) { |
| 772 | af = AF_INET6; |
| 773 | len = NS_IN6ADDRSZ; |
| 774 | } else { |
| 775 | if (inet_pton(AF_INET, p, &host_addr) <= 0) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 776 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 777 | res_state res = __res_get_state(); |
| 778 | if (res == NULL) goto nospc; |
| 779 | if (res->options & RES_USE_INET6) { |
| 780 | map_v4v6_address(buf, buf); |
| 781 | af = AF_INET6; |
| 782 | len = NS_IN6ADDRSZ; |
| 783 | } else { |
| 784 | af = AF_INET; |
| 785 | len = NS_INADDRSZ; |
| 786 | } |
| 787 | __res_put_state(res); |
| 788 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 789 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 790 | /* if this is not something we're looking for, skip it. */ |
| 791 | if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue; |
| 792 | if (hent->h_length != 0 && hent->h_length != len) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 793 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 794 | while (*cp == ' ' || *cp == '\t') cp++; |
| 795 | if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0'; |
| 796 | q = aliases; |
| 797 | while (cp && *cp) { |
| 798 | if (*cp == ' ' || *cp == '\t') { |
| 799 | cp++; |
| 800 | continue; |
| 801 | } |
| 802 | addalias(q, cp, aliases, maxaliases); |
| 803 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
| 804 | } |
| 805 | break; |
| 806 | } |
| 807 | hent->h_length = len; |
| 808 | hent->h_addrtype = af; |
| 809 | HENT_ARRAY(hent->h_addr_list, 1, buf, buflen); |
| 810 | anum = (size_t)(q - aliases); |
| 811 | HENT_ARRAY(hent->h_aliases, anum, buf, buflen); |
| 812 | HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen); |
| 813 | hent->h_addr_list[1] = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 814 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 815 | HENT_SCOPY(hent->h_name, name, buf, buflen); |
| 816 | for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen); |
| 817 | hent->h_aliases[anum] = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 818 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 819 | *he = NETDB_SUCCESS; |
| 820 | free(p); |
| 821 | free(aliases); |
| 822 | return hent; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 823 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 824 | free(p); |
| 825 | free(aliases); |
| 826 | errno = ENOSPC; |
| 827 | *he = NETDB_INTERNAL; |
| 828 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 829 | } |
| 830 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 831 | static void map_v4v6_address(const char* src, char* dst) { |
| 832 | u_char* p = (u_char*) dst; |
| 833 | char tmp[NS_INADDRSZ]; |
| 834 | int i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 835 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 836 | _DIAGASSERT(src != NULL); |
| 837 | _DIAGASSERT(dst != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 838 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 839 | /* Stash a temporary copy so our caller can update in place. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 840 | memcpy(tmp, src, NS_INADDRSZ); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 841 | /* Mark this ipv6 addr as a mapped ipv4. */ |
| 842 | for (i = 0; i < 10; i++) *p++ = 0x00; |
| 843 | *p++ = 0xff; |
| 844 | *p++ = 0xff; |
| 845 | /* Retrieve the saved copy and we're done. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 846 | memcpy(p, tmp, NS_INADDRSZ); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 847 | } |
| 848 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 849 | static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) { |
| 850 | char** ap; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 851 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 852 | _DIAGASSERT(hp != NULL); |
| 853 | _DIAGASSERT(bpp != NULL); |
| 854 | _DIAGASSERT(ep != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 855 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 856 | if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return; |
| 857 | hp->h_addrtype = AF_INET6; |
| 858 | hp->h_length = NS_IN6ADDRSZ; |
| 859 | for (ap = hp->h_addr_list; *ap; ap++) { |
| 860 | int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align))); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 861 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 862 | if (ep - *bpp < (i + NS_IN6ADDRSZ)) { |
| 863 | /* Out of memory. Truncate address list here. XXX */ |
| 864 | *ap = NULL; |
| 865 | return; |
| 866 | } |
| 867 | *bpp += i; |
| 868 | map_v4v6_address(*ap, *bpp); |
| 869 | *ap = *bpp; |
| 870 | *bpp += NS_IN6ADDRSZ; |
| 871 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 872 | } |
| 873 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 874 | static void addrsort(char** ap, int num, res_state res) { |
| 875 | int i, j; |
| 876 | char** p; |
| 877 | short aval[MAXADDRS]; |
| 878 | int needsort = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 879 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 880 | _DIAGASSERT(ap != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 881 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 882 | p = ap; |
| 883 | for (i = 0; i < num; i++, p++) { |
| 884 | for (j = 0; (unsigned) j < res->nsort; j++) |
| 885 | if (res->sort_list[j].addr.s_addr == |
| 886 | (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask)) |
| 887 | break; |
| 888 | aval[i] = j; |
| 889 | if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i; |
| 890 | } |
| 891 | if (!needsort) return; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 892 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 893 | while (needsort < num) { |
| 894 | for (j = needsort - 1; j >= 0; j--) { |
| 895 | if (aval[j] > aval[j + 1]) { |
| 896 | char* hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 897 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 898 | i = aval[j]; |
| 899 | aval[j] = aval[j + 1]; |
| 900 | aval[j + 1] = i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 901 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 902 | hp = ap[j]; |
| 903 | ap[j] = ap[j + 1]; |
| 904 | ap[j + 1] = hp; |
| 905 | } else |
| 906 | break; |
| 907 | } |
| 908 | needsort++; |
| 909 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 910 | } |
| 911 | |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 912 | static bool _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 913 | int n, type; |
| 914 | struct hostent* hp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 915 | res_state res; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 916 | |
Bernie Innocenti | 9bf0e1d | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 917 | info->hp->h_addrtype = addr_type; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 918 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 919 | switch (info->hp->h_addrtype) { |
| 920 | case AF_INET: |
| 921 | info->hp->h_length = NS_INADDRSZ; |
| 922 | type = T_A; |
| 923 | break; |
| 924 | case AF_INET6: |
| 925 | info->hp->h_length = NS_IN6ADDRSZ; |
| 926 | type = T_AAAA; |
| 927 | break; |
| 928 | default: |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 929 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 930 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 931 | querybuf* buf = (querybuf*) malloc(sizeof(querybuf)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 932 | if (buf == NULL) { |
| 933 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 934 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 935 | } |
| 936 | res = __res_get_state(); |
| 937 | if (res == NULL) { |
| 938 | free(buf); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 939 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 940 | } |
| 941 | n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf)); |
| 942 | if (n < 0) { |
| 943 | free(buf); |
| 944 | debugprintf("res_nsearch failed (%d)\n", res, n); |
| 945 | __res_put_state(res); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 946 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 947 | } |
| 948 | hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he); |
| 949 | free(buf); |
| 950 | __res_put_state(res); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 951 | if (hp == NULL) { |
| 952 | return false; |
| 953 | } |
| 954 | return true; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 955 | } |
| 956 | |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 957 | static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, |
| 958 | const android_net_context* netcontext, getnamaddr* info) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 959 | char qbuf[MAXDNAME + 1], *qp, *ep; |
| 960 | int n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 961 | struct hostent* hp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 962 | int advance; |
| 963 | res_state res; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 964 | |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 965 | info->hp->h_length = len; |
| 966 | info->hp->h_addrtype = af; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 967 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 968 | switch (info->hp->h_addrtype) { |
| 969 | case AF_INET: |
| 970 | (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff), |
| 971 | (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff)); |
| 972 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 973 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 974 | case AF_INET6: |
| 975 | qp = qbuf; |
| 976 | ep = qbuf + sizeof(qbuf) - 1; |
| 977 | for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) { |
| 978 | advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf, |
| 979 | ((unsigned int) uaddr[n] >> 4) & 0xf); |
| 980 | if (advance > 0 && qp + advance < ep) |
| 981 | qp += advance; |
| 982 | else { |
| 983 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 984 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { |
| 988 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 989 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 990 | } |
| 991 | break; |
| 992 | default: |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 993 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 994 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 995 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 996 | querybuf* buf = (querybuf*) malloc(sizeof(querybuf)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 997 | if (buf == NULL) { |
| 998 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 999 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1000 | } |
| 1001 | res = __res_get_state(); |
| 1002 | if (res == NULL) { |
| 1003 | free(buf); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 1004 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1005 | } |
| 1006 | res_setnetcontext(res, netcontext); |
| 1007 | n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf)); |
| 1008 | if (n < 0) { |
| 1009 | free(buf); |
| 1010 | debugprintf("res_nquery failed (%d)\n", res, n); |
| 1011 | __res_put_state(res); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 1012 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1013 | } |
| 1014 | hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he); |
| 1015 | free(buf); |
| 1016 | if (hp == NULL) { |
| 1017 | __res_put_state(res); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 1018 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1019 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1020 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1021 | char* bf = (char*) (hp->h_addr_list + 2); |
| 1022 | size_t blen = (size_t)(bf - info->buf); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1023 | if (blen + info->hp->h_length > info->buflen) goto nospc; |
| 1024 | hp->h_addr_list[0] = bf; |
| 1025 | hp->h_addr_list[1] = NULL; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1026 | memcpy(bf, uaddr, (size_t) info->hp->h_length); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1027 | if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) { |
| 1028 | if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc; |
| 1029 | map_v4v6_address(bf, bf); |
| 1030 | hp->h_addrtype = AF_INET6; |
| 1031 | hp->h_length = NS_IN6ADDRSZ; |
| 1032 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1033 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1034 | __res_put_state(res); |
| 1035 | *info->he = NETDB_SUCCESS; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 1036 | return true; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1037 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1038 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1039 | errno = ENOSPC; |
| 1040 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 1041 | return false; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1042 | } |
| 1043 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1044 | /* |
| 1045 | * Non-reentrant versions. |
| 1046 | */ |
| 1047 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1048 | struct hostent* gethostbyname(const char* name) { |
| 1049 | struct hostent* result = NULL; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1050 | struct res_static* rs = __res_get_static(); // For thread-safety. |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1051 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1052 | gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno); |
| 1053 | return result; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1054 | } |
| 1055 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1056 | struct hostent* gethostbyname2(const char* name, int af) { |
| 1057 | struct hostent* result = NULL; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1058 | struct res_static* rs = __res_get_static(); // For thread-safety. |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1059 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1060 | gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno); |
| 1061 | return result; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | // android_gethostby*fornet can be called in two different contexts. |
| 1065 | // - In the proxy client context (proxy != NULL), |netid| is |app_netid|. |
| 1066 | // - In the proxy listener context (proxy == NULL), |netid| is |dns_netid|. |
| 1067 | // The netcontext is constructed before checking which context we are in. |
| 1068 | // Therefore, we have to populate both fields, and rely on the downstream code to check whether |
| 1069 | // |proxy == NULL|, and use that info to query the field that matches the caller's intent. |
| 1070 | static struct android_net_context make_context(unsigned netid, unsigned mark) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1071 | struct android_net_context netcontext = NETCONTEXT_UNSET; |
| 1072 | netcontext.app_netid = netid; |
| 1073 | netcontext.app_mark = mark; |
| 1074 | netcontext.dns_netid = netid; |
| 1075 | netcontext.dns_mark = mark; |
| 1076 | return netcontext; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1077 | } |
| 1078 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1079 | struct hostent* android_gethostbynamefornet(const char* name, int af, unsigned netid, |
| 1080 | unsigned mark) { |
| 1081 | const struct android_net_context netcontext = make_context(netid, mark); |
| 1082 | return android_gethostbynamefornetcontext(name, af, &netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1083 | } |
| 1084 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1085 | struct hostent* android_gethostbynamefornetcontext(const char* name, int af, |
| 1086 | const struct android_net_context* netcontext) { |
| 1087 | struct hostent* hp; |
| 1088 | res_state res = __res_get_state(); |
| 1089 | if (res == NULL) return NULL; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1090 | struct res_static* rs = __res_get_static(); // For thread-safety. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1091 | hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), |
| 1092 | &h_errno, netcontext); |
| 1093 | __res_put_state(res); |
| 1094 | return hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1095 | } |
| 1096 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1097 | struct hostent* gethostbyaddr(const void* addr, socklen_t len, int af) { |
| 1098 | return android_gethostbyaddrfornetcontext_proxy(addr, len, af, &NETCONTEXT_UNSET); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1099 | } |
| 1100 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1101 | struct hostent* android_gethostbyaddrfornet(const void* addr, socklen_t len, int af, unsigned netid, |
| 1102 | unsigned mark) { |
| 1103 | const struct android_net_context netcontext = make_context(netid, mark); |
| 1104 | return android_gethostbyaddrfornetcontext(addr, len, af, &netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1105 | } |
| 1106 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1107 | struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af, |
| 1108 | const struct android_net_context* netcontext) { |
| 1109 | return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1110 | } |
| 1111 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1112 | __LIBC_HIDDEN__ struct hostent* android_gethostbyaddrfornetcontext_proxy( |
| 1113 | const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1114 | struct res_static* rs = __res_get_static(); // For thread-safety. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1115 | return android_gethostbyaddrfornetcontext_proxy_internal( |
| 1116 | addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1117 | } |