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