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