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