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