Bernie Innocenti | 318ed2d | 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 | e71a28a | 2019-05-29 00:42:35 +0900 | [diff] [blame] | 54 | #include "gethnamaddr.h" |
| 55 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 56 | #include <android-base/logging.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 57 | #include <arpa/inet.h> |
| 58 | #include <arpa/nameser.h> |
Bernie Innocenti | 318ed2d | 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 | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 63 | #include <netinet/in.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 64 | #include <stdarg.h> |
| 65 | #include <stdbool.h> |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 66 | #include <stdlib.h> |
| 67 | #include <string.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 68 | #include <sys/param.h> |
| 69 | #include <sys/socket.h> |
| 70 | #include <sys/un.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 71 | #include <unistd.h> |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 72 | #include <functional> |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 73 | #include <vector> |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 74 | |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 75 | #include "hostent.h" |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 76 | #include "netd_resolv/resolv.h" |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 77 | #include "res_init.h" |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 78 | #include "resolv_cache.h" |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 79 | #include "resolv_private.h" |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 80 | #include "stats.pb.h" |
| 81 | |
| 82 | using android::net::NetworkDnsEventReported; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 83 | |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 84 | // NetBSD uses _DIAGASSERT to null-check arguments and the like, |
| 85 | // but it's clear from the number of mistakes in their assertions |
| 86 | // that they don't actually test or ship with this. |
| 87 | #define _DIAGASSERT(e) /* nothing */ |
| 88 | |
nuccachen | c28257d | 2018-09-11 11:20:00 +0800 | [diff] [blame] | 89 | // TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent |
| 90 | // buffer. |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 91 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 92 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 93 | |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 94 | #define maybe_ok(res, nm, ok) ((ok)(nm) != 0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 95 | #define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok) |
| 96 | #define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok) |
| 97 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 98 | #define MAXPACKET (8 * 1024) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 99 | |
Bernie Innocenti | bf0b3bc | 2019-10-11 19:45:56 +0900 | [diff] [blame] | 100 | constexpr int MAXADDRS = 35; |
| 101 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 102 | typedef union { |
| 103 | HEADER hdr; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 104 | uint8_t buf[MAXPACKET]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 105 | } querybuf; |
| 106 | |
| 107 | typedef union { |
| 108 | int32_t al; |
| 109 | char ac; |
| 110 | } align; |
| 111 | |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 112 | static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep, |
chenbruce | 99cbfd8 | 2019-09-05 15:08:13 +0800 | [diff] [blame] | 113 | const std::function<void(struct hostent* hp)>& mapping_param, |
| 114 | const std::function<void(char* src, char* dst)>& mapping_addr); |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 115 | static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 116 | |
waynema | 067e984 | 2019-03-08 19:13:41 +0800 | [diff] [blame] | 117 | static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 118 | const android_net_context* netcontext, getnamaddr* info, |
| 119 | NetworkDnsEventReported* event); |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 120 | static int dns_gethtbyname(ResState* res, const char* name, int af, getnamaddr* info); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 121 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 122 | #define BOUNDED_INCR(x) \ |
| 123 | do { \ |
| 124 | BOUNDS_CHECK(cp, x); \ |
| 125 | cp += (x); \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 126 | } while (0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 127 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 128 | #define BOUNDS_CHECK(ptr, count) \ |
| 129 | do { \ |
| 130 | if (eom - (ptr) < (count)) goto no_recovery; \ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 131 | } while (0) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 132 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 133 | static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype, |
Bernie Innocenti | 3479d3b | 2019-10-08 22:23:26 +0900 | [diff] [blame] | 134 | struct hostent* hent, char* buf, size_t buflen, int* he) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 135 | const HEADER* hp; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 136 | const uint8_t* cp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 137 | int n; |
| 138 | size_t qlen; |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 139 | const uint8_t *eom, *erdata; |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 140 | char *bp, **hap, *ep; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 141 | int ancount, qdcount; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 142 | int haveanswer, had_error; |
| 143 | int toobig = 0; |
| 144 | char tbuf[MAXDNAME]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 145 | char* addr_ptrs[MAXADDRS]; |
| 146 | const char* tname; |
| 147 | int (*name_ok)(const char*); |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 148 | std::vector<char*> aliases; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 149 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 150 | _DIAGASSERT(answer != NULL); |
| 151 | _DIAGASSERT(qname != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 152 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 153 | tname = qname; |
| 154 | hent->h_name = NULL; |
| 155 | eom = answer->buf + anslen; |
| 156 | switch (qtype) { |
| 157 | case T_A: |
| 158 | case T_AAAA: |
| 159 | name_ok = res_hnok; |
| 160 | break; |
| 161 | case T_PTR: |
| 162 | name_ok = res_dnok; |
| 163 | break; |
| 164 | default: |
| 165 | *he = NO_RECOVERY; |
| 166 | return NULL; /* XXX should be abort(); */ |
| 167 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 168 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 169 | /* |
| 170 | * find first satisfactory answer |
| 171 | */ |
| 172 | hp = &answer->hdr; |
| 173 | ancount = ntohs(hp->ancount); |
| 174 | qdcount = ntohs(hp->qdcount); |
| 175 | bp = buf; |
| 176 | ep = buf + buflen; |
| 177 | cp = answer->buf; |
| 178 | BOUNDED_INCR(HFIXEDSZ); |
| 179 | if (qdcount != 1) goto no_recovery; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 180 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 181 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 182 | if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 183 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 184 | BOUNDED_INCR(n + QFIXEDSZ); |
| 185 | if (qtype == T_A || qtype == T_AAAA) { |
| 186 | /* res_send() has already verified that the query name is the |
| 187 | * same as the one we sent; this just gets the expanded name |
| 188 | * (i.e., with the succeeding search-domain tacked on). |
| 189 | */ |
| 190 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 191 | if (n >= MAXHOSTNAMELEN) goto no_recovery; |
| 192 | hent->h_name = bp; |
| 193 | bp += n; |
| 194 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 195 | qname = hent->h_name; |
| 196 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 197 | hent->h_addr_list = hap = addr_ptrs; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 198 | *hap = NULL; |
| 199 | haveanswer = 0; |
| 200 | had_error = 0; |
| 201 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 202 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 203 | if ((n < 0) || !maybe_ok(res, bp, name_ok)) { |
| 204 | had_error++; |
| 205 | continue; |
| 206 | } |
| 207 | cp += n; /* name */ |
| 208 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 209 | int type = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 210 | cp += INT16SZ; /* type */ |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 211 | int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 212 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
chenbruce | 0d47042 | 2019-03-28 18:44:37 +0800 | [diff] [blame] | 213 | n = ntohs(*reinterpret_cast<const uint16_t*>(cp)); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 214 | cp += INT16SZ; /* len */ |
| 215 | BOUNDS_CHECK(cp, n); |
| 216 | erdata = cp + n; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 217 | if (cl != C_IN) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 218 | /* XXX - debug? syslog? */ |
| 219 | cp += n; |
| 220 | continue; /* XXX - had_error++ ? */ |
| 221 | } |
| 222 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
| 223 | n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf); |
| 224 | if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) { |
| 225 | had_error++; |
| 226 | continue; |
| 227 | } |
| 228 | cp += n; |
| 229 | if (cp != erdata) goto no_recovery; |
| 230 | /* Store alias. */ |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 231 | aliases.push_back(bp); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 232 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 233 | if (n >= MAXHOSTNAMELEN) { |
| 234 | had_error++; |
| 235 | continue; |
| 236 | } |
| 237 | bp += n; |
| 238 | /* Get canonical name. */ |
| 239 | n = (int) strlen(tbuf) + 1; /* for the \0 */ |
| 240 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 241 | had_error++; |
| 242 | continue; |
| 243 | } |
| 244 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 245 | hent->h_name = bp; |
| 246 | bp += n; |
| 247 | continue; |
| 248 | } |
| 249 | if (qtype == T_PTR && type == T_CNAME) { |
| 250 | n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf); |
| 251 | if (n < 0 || !maybe_dnok(res, tbuf)) { |
| 252 | had_error++; |
| 253 | continue; |
| 254 | } |
| 255 | cp += n; |
| 256 | if (cp != erdata) goto no_recovery; |
| 257 | /* Get canonical name. */ |
| 258 | n = (int) strlen(tbuf) + 1; /* for the \0 */ |
| 259 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 260 | had_error++; |
| 261 | continue; |
| 262 | } |
| 263 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 264 | tname = bp; |
| 265 | bp += n; |
| 266 | continue; |
| 267 | } |
| 268 | if (type != qtype) { |
| 269 | if (type != T_KEY && type != T_SIG) |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 270 | LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " " |
| 271 | << p_type(qtype) << "\", got type \"" << p_type(type) << "\""; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 272 | cp += n; |
| 273 | continue; /* XXX - had_error++ ? */ |
| 274 | } |
| 275 | switch (type) { |
| 276 | case T_PTR: |
| 277 | if (strcasecmp(tname, bp) != 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 278 | LOG(DEBUG) << __func__ << ": asked for \"" << qname << "\", got \"" << bp |
| 279 | << "\""; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 280 | cp += n; |
| 281 | continue; /* XXX - had_error++ ? */ |
| 282 | } |
| 283 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 284 | if ((n < 0) || !maybe_hnok(res, bp)) { |
| 285 | had_error++; |
| 286 | break; |
| 287 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 288 | cp += n; |
| 289 | if (cp != erdata) goto no_recovery; |
| 290 | if (!haveanswer) |
| 291 | hent->h_name = bp; |
| 292 | else |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 293 | aliases.push_back(bp); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 294 | if (n != -1) { |
| 295 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 296 | if (n >= MAXHOSTNAMELEN) { |
| 297 | had_error++; |
| 298 | break; |
| 299 | } |
| 300 | bp += n; |
| 301 | } |
| 302 | break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 303 | case T_A: |
| 304 | case T_AAAA: |
| 305 | if (strcasecmp(hent->h_name, bp) != 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 306 | LOG(DEBUG) << __func__ << ": asked for \"" << hent->h_name << "\", got \"" << bp |
| 307 | << "\""; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 308 | cp += n; |
| 309 | continue; /* XXX - had_error++ ? */ |
| 310 | } |
| 311 | if (n != hent->h_length) { |
| 312 | cp += n; |
| 313 | continue; |
| 314 | } |
| 315 | if (type == T_AAAA) { |
| 316 | struct in6_addr in6; |
| 317 | memcpy(&in6, cp, NS_IN6ADDRSZ); |
| 318 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 319 | cp += n; |
| 320 | continue; |
| 321 | } |
| 322 | } |
| 323 | if (!haveanswer) { |
| 324 | int nn; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 325 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 326 | hent->h_name = bp; |
| 327 | nn = (int) strlen(bp) + 1; /* for the \0 */ |
| 328 | bp += nn; |
| 329 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 330 | |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 331 | bp += sizeof(align) - (size_t)((uintptr_t)bp % sizeof(align)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 332 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 333 | if (bp + n >= ep) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 334 | LOG(DEBUG) << __func__ << ": size (" << n << ") too big"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 335 | had_error++; |
| 336 | continue; |
| 337 | } |
| 338 | if (hap >= &addr_ptrs[MAXADDRS - 1]) { |
| 339 | if (!toobig++) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 340 | LOG(DEBUG) << __func__ << ": Too many addresses (" << MAXADDRS << ")"; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 341 | } |
| 342 | cp += n; |
| 343 | continue; |
| 344 | } |
| 345 | (void) memcpy(*hap++ = bp, cp, (size_t) n); |
| 346 | bp += n; |
| 347 | cp += n; |
| 348 | if (cp != erdata) goto no_recovery; |
| 349 | break; |
| 350 | default: |
| 351 | abort(); |
| 352 | } |
| 353 | if (!had_error) haveanswer++; |
| 354 | } |
| 355 | if (haveanswer) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 356 | *hap = NULL; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 357 | if (!hent->h_name) { |
| 358 | n = (int) strlen(qname) + 1; /* for the \0 */ |
| 359 | if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery; |
| 360 | strlcpy(bp, qname, (size_t)(ep - bp)); |
| 361 | hent->h_name = bp; |
| 362 | bp += n; |
| 363 | } |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 364 | if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 365 | goto success; |
| 366 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 367 | no_recovery: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 368 | *he = NO_RECOVERY; |
| 369 | return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 370 | success: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 371 | bp = (char*) ALIGN(bp); |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 372 | aliases.push_back(nullptr); |
| 373 | qlen = aliases.size() * sizeof(*hent->h_aliases); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 374 | if ((size_t)(ep - bp) < qlen) goto nospc; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 375 | hent->h_aliases = (char**) bp; |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 376 | memcpy(bp, aliases.data(), qlen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 377 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 378 | bp += qlen; |
| 379 | n = (int) (hap - addr_ptrs); |
| 380 | qlen = (n + 1) * sizeof(*hent->h_addr_list); |
| 381 | if ((size_t)(ep - bp) < qlen) goto nospc; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 382 | hent->h_addr_list = (char**) bp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 383 | memcpy(bp, addr_ptrs, qlen); |
| 384 | *he = NETDB_SUCCESS; |
| 385 | return hent; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 386 | nospc: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 387 | errno = ENOSPC; |
| 388 | *he = NETDB_INTERNAL; |
| 389 | return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 390 | } |
| 391 | |
Bernie Innocenti | 36db773 | 2019-10-11 18:03:02 +0900 | [diff] [blame] | 392 | int resolv_gethostbyname(const char* name, int af, hostent* hp, char* buf, size_t buflen, |
| 393 | const android_net_context* netcontext, hostent** result, |
| 394 | NetworkDnsEventReported* event) { |
Mike Yu | c7d5510 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 395 | getnamaddr info; |
Bernie Innocenti | 36db773 | 2019-10-11 18:03:02 +0900 | [diff] [blame] | 396 | |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 397 | ResState res; |
| 398 | res_init(&res, netcontext, event); |
Bernie Innocenti | 36db773 | 2019-10-11 18:03:02 +0900 | [diff] [blame] | 399 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 400 | size_t size; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 401 | switch (af) { |
| 402 | case AF_INET: |
| 403 | size = NS_INADDRSZ; |
| 404 | break; |
| 405 | case AF_INET6: |
| 406 | size = NS_IN6ADDRSZ; |
| 407 | break; |
| 408 | default: |
Mike Yu | c7d5510 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 409 | return EAI_FAMILY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 410 | } |
| 411 | if (buflen < size) goto nospc; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 412 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 413 | hp->h_addrtype = af; |
| 414 | hp->h_length = (int) size; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 415 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 416 | /* |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 417 | * disallow names consisting only of digits/dots, unless |
| 418 | * they end in a dot. |
| 419 | */ |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 420 | if (isdigit((uint8_t)name[0])) { |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 421 | for (const char* cp = name;; ++cp) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 422 | if (!*cp) { |
| 423 | if (*--cp == '.') break; |
| 424 | /* |
| 425 | * All-numeric, no dot at the end. |
| 426 | * Fake up a hostent as if we'd actually |
| 427 | * done a lookup. |
| 428 | */ |
| 429 | goto fake; |
| 430 | } |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 431 | if (!isdigit((uint8_t)*cp) && *cp != '.') break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 432 | } |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 433 | } |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 434 | if ((isxdigit((uint8_t)name[0]) && strchr(name, ':') != NULL) || name[0] == ':') { |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 435 | for (const char* cp = name;; ++cp) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 436 | if (!*cp) { |
| 437 | if (*--cp == '.') break; |
| 438 | /* |
| 439 | * All-IPv6-legal, no dot at the end. |
| 440 | * Fake up a hostent as if we'd actually |
| 441 | * done a lookup. |
| 442 | */ |
| 443 | goto fake; |
| 444 | } |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 445 | if (!isxdigit((uint8_t)*cp) && *cp != ':' && *cp != '.') break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 446 | } |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 447 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 448 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 449 | info.hp = hp; |
| 450 | info.buf = buf; |
| 451 | info.buflen = buflen; |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 452 | if (_hf_gethtbyname2(name, af, &info)) { |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 453 | int error = dns_gethtbyname(&res, name, af, &info); |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 454 | if (error != 0) return error; |
Bernie Innocenti | c939de0 | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 455 | } |
Bernie Innocenti | 36db773 | 2019-10-11 18:03:02 +0900 | [diff] [blame] | 456 | *result = hp; |
Mike Yu | c7d5510 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 457 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 458 | nospc: |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 459 | return EAI_MEMORY; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 460 | fake: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 461 | HENT_ARRAY(hp->h_addr_list, 1, buf, buflen); |
| 462 | HENT_ARRAY(hp->h_aliases, 0, buf, buflen); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 463 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 464 | hp->h_aliases[0] = NULL; |
| 465 | if (size > buflen) goto nospc; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 466 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 467 | if (inet_pton(af, name, buf) <= 0) { |
Mike Yu | c7d5510 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 468 | return EAI_NODATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 469 | } |
| 470 | hp->h_addr_list[0] = buf; |
| 471 | hp->h_addr_list[1] = NULL; |
| 472 | buf += size; |
| 473 | buflen -= size; |
| 474 | HENT_SCOPY(hp->h_name, name, buf, buflen); |
Bernie Innocenti | 36db773 | 2019-10-11 18:03:02 +0900 | [diff] [blame] | 475 | *result = hp; |
Mike Yu | c7d5510 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 476 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 477 | } |
| 478 | |
Bernie Innocenti | 324f9f8 | 2019-10-11 18:50:20 +0900 | [diff] [blame] | 479 | int resolv_gethostbyaddr(const void* addr, socklen_t len, int af, hostent* hp, char* buf, |
| 480 | size_t buflen, const struct android_net_context* netcontext, |
| 481 | hostent** result, NetworkDnsEventReported* event) { |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 482 | const uint8_t* uaddr = (const uint8_t*)addr; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 483 | socklen_t size; |
| 484 | struct getnamaddr info; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 485 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 486 | _DIAGASSERT(addr != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 487 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 488 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 489 | (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) || |
| 490 | IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) { |
Hungming Chen | 598202c | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 491 | return EAI_NODATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 492 | } |
| 493 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 494 | (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) || |
| 495 | IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) { |
| 496 | /* Unmap. */ |
| 497 | uaddr += NS_IN6ADDRSZ - NS_INADDRSZ; |
| 498 | addr = uaddr; |
| 499 | af = AF_INET; |
| 500 | len = NS_INADDRSZ; |
| 501 | } |
| 502 | switch (af) { |
| 503 | case AF_INET: |
| 504 | size = NS_INADDRSZ; |
| 505 | break; |
| 506 | case AF_INET6: |
| 507 | size = NS_IN6ADDRSZ; |
| 508 | break; |
| 509 | default: |
Hungming Chen | 598202c | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 510 | return EAI_FAMILY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 511 | } |
| 512 | if (size != len) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 513 | // TODO: Consider converting to a private extended EAI_* error code. |
| 514 | // Currently, the EAI_* value has no corresponding error code for invalid argument socket |
| 515 | // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM |
| 516 | // and EINVAL, to EAI_FAIL. |
| 517 | return EAI_FAIL; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 518 | } |
| 519 | info.hp = hp; |
| 520 | info.buf = buf; |
| 521 | info.buflen = buflen; |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 522 | if (_hf_gethtbyaddr(uaddr, len, af, &info)) { |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 523 | int error = dns_gethtbyaddr(uaddr, len, af, netcontext, &info, event); |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 524 | if (error != 0) return error; |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 525 | } |
Bernie Innocenti | 324f9f8 | 2019-10-11 18:50:20 +0900 | [diff] [blame] | 526 | *result = hp; |
Hungming Chen | 598202c | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 527 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 528 | } |
| 529 | |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 530 | // TODO: Consider leaving function without returning error code as _gethtent() does because |
| 531 | // the error code of the caller does not currently return to netd. |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 532 | struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen, |
| 533 | int* he) { |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 534 | char *name; |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 535 | char* cp; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 536 | int af, len; |
| 537 | size_t anum; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 538 | struct in6_addr host_addr; |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 539 | std::vector<char*> aliases; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 540 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 541 | if (hf == NULL) { |
| 542 | *he = NETDB_INTERNAL; |
| 543 | errno = EINVAL; |
| 544 | return NULL; |
| 545 | } |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 546 | char* p = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 547 | |
Bernie Innocenti | 36db773 | 2019-10-11 18:03:02 +0900 | [diff] [blame] | 548 | // Allocate a new space to read file lines like upstream does. |
| 549 | const size_t line_buf_size = MAXPACKET; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 550 | if ((p = (char*) malloc(line_buf_size)) == NULL) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 551 | goto nospc; |
| 552 | } |
| 553 | for (;;) { |
| 554 | if (!fgets(p, line_buf_size, hf)) { |
| 555 | free(p); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 556 | *he = HOST_NOT_FOUND; |
| 557 | return NULL; |
| 558 | } |
| 559 | if (*p == '#') { |
| 560 | continue; |
| 561 | } |
| 562 | if (!(cp = strpbrk(p, "#\n"))) { |
| 563 | continue; |
| 564 | } |
| 565 | *cp = '\0'; |
| 566 | if (!(cp = strpbrk(p, " \t"))) continue; |
| 567 | *cp++ = '\0'; |
| 568 | if (inet_pton(AF_INET6, p, &host_addr) > 0) { |
| 569 | af = AF_INET6; |
| 570 | len = NS_IN6ADDRSZ; |
| 571 | } else { |
| 572 | if (inet_pton(AF_INET, p, &host_addr) <= 0) continue; |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 573 | af = AF_INET; |
| 574 | len = NS_INADDRSZ; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 575 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 576 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 577 | /* if this is not something we're looking for, skip it. */ |
| 578 | if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue; |
| 579 | if (hent->h_length != 0 && hent->h_length != len) continue; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 580 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 581 | while (*cp == ' ' || *cp == '\t') cp++; |
| 582 | if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0'; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 583 | while (cp && *cp) { |
| 584 | if (*cp == ' ' || *cp == '\t') { |
| 585 | cp++; |
| 586 | continue; |
| 587 | } |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 588 | aliases.push_back(cp); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 589 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
| 590 | } |
| 591 | break; |
| 592 | } |
| 593 | hent->h_length = len; |
| 594 | hent->h_addrtype = af; |
| 595 | HENT_ARRAY(hent->h_addr_list, 1, buf, buflen); |
lifr | be7dfbf | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 596 | anum = aliases.size(); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 597 | HENT_ARRAY(hent->h_aliases, anum, buf, buflen); |
| 598 | HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen); |
| 599 | hent->h_addr_list[1] = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 600 | |
nuccachen | c28257d | 2018-09-11 11:20:00 +0800 | [diff] [blame] | 601 | /* Reserve space for mapping IPv4 address to IPv6 address in place */ |
| 602 | if (hent->h_addrtype == AF_INET) { |
| 603 | HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen); |
| 604 | } |
| 605 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 606 | HENT_SCOPY(hent->h_name, name, buf, buflen); |
| 607 | for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen); |
| 608 | hent->h_aliases[anum] = NULL; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 609 | *he = NETDB_SUCCESS; |
| 610 | free(p); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 611 | return hent; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 612 | nospc: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 613 | free(p); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 614 | errno = ENOSPC; |
| 615 | *he = NETDB_INTERNAL; |
| 616 | return NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 617 | } |
| 618 | |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 619 | static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep, |
chenbruce | 99cbfd8 | 2019-09-05 15:08:13 +0800 | [diff] [blame] | 620 | const std::function<void(struct hostent* hp)>& map_param, |
| 621 | const std::function<void(char* src, char* dst)>& map_addr) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 622 | _DIAGASSERT(hp != NULL); |
| 623 | _DIAGASSERT(bpp != NULL); |
| 624 | _DIAGASSERT(ep != NULL); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 625 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 626 | if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return; |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 627 | map_param(hp); |
| 628 | for (char** ap = hp->h_addr_list; *ap; ap++) { |
chenbruce | c51f121 | 2019-09-12 16:59:33 +0800 | [diff] [blame] | 629 | int i = (int)(sizeof(align) - (size_t)((uintptr_t)*bpp % sizeof(align))); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 630 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 631 | if (ep - *bpp < (i + NS_IN6ADDRSZ)) { |
| 632 | /* Out of memory. Truncate address list here. XXX */ |
| 633 | *ap = NULL; |
| 634 | return; |
| 635 | } |
| 636 | *bpp += i; |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 637 | map_addr(*ap, *bpp); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 638 | *ap = *bpp; |
| 639 | *bpp += NS_IN6ADDRSZ; |
| 640 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 641 | } |
| 642 | |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 643 | /* Reserve space for mapping IPv4 address to IPv6 address in place */ |
| 644 | static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) { |
| 645 | convert_v4v6_hostent(hp, bpp, ep, |
| 646 | [](struct hostent* hp) { |
| 647 | (void) hp; /* unused */ |
| 648 | }, |
| 649 | [](char* src, char* dst) { |
| 650 | memcpy(dst, src, NS_INADDRSZ); |
| 651 | memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD)); |
| 652 | }); |
| 653 | } |
| 654 | |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 655 | static int dns_gethtbyname(ResState* res, const char* name, int addr_type, getnamaddr* info) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 656 | int n, type; |
Bernie Innocenti | c939de0 | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 657 | info->hp->h_addrtype = addr_type; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 658 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 659 | switch (info->hp->h_addrtype) { |
| 660 | case AF_INET: |
| 661 | info->hp->h_length = NS_INADDRSZ; |
| 662 | type = T_A; |
| 663 | break; |
| 664 | case AF_INET6: |
| 665 | info->hp->h_length = NS_IN6ADDRSZ; |
| 666 | type = T_AAAA; |
| 667 | break; |
| 668 | default: |
Mike Yu | c7d5510 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 669 | return EAI_FAMILY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 670 | } |
waynema | 0cac8c6 | 2019-03-08 16:58:06 +0800 | [diff] [blame] | 671 | auto buf = std::make_unique<querybuf>(); |
| 672 | |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 673 | int he; |
| 674 | n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf), &he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 675 | if (n < 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 676 | LOG(DEBUG) << __func__ << ": res_nsearch failed (" << n << ")"; |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 677 | // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA. |
| 678 | // Note that res_nsearch() doesn't set the pair NETDB_INTERNAL and errno. |
| 679 | // See also herrnoToAiErrno(). |
| 680 | return herrnoToAiErrno(he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 681 | } |
Bernie Innocenti | 3479d3b | 2019-10-08 22:23:26 +0900 | [diff] [blame] | 682 | hostent* hp = getanswer(buf.get(), n, name, type, info->hp, info->buf, info->buflen, &he); |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 683 | if (hp == NULL) return herrnoToAiErrno(he); |
| 684 | |
Mike Yu | c7d5510 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 685 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 686 | } |
| 687 | |
waynema | 067e984 | 2019-03-08 19:13:41 +0800 | [diff] [blame] | 688 | static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 689 | const android_net_context* netcontext, getnamaddr* info, |
| 690 | NetworkDnsEventReported* event) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 691 | char qbuf[MAXDNAME + 1], *qp, *ep; |
| 692 | int n; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 693 | int advance; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 694 | |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 695 | info->hp->h_length = len; |
| 696 | info->hp->h_addrtype = af; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 697 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 698 | switch (info->hp->h_addrtype) { |
| 699 | case AF_INET: |
| 700 | (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff), |
| 701 | (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff)); |
| 702 | break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 703 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 704 | case AF_INET6: |
| 705 | qp = qbuf; |
| 706 | ep = qbuf + sizeof(qbuf) - 1; |
| 707 | for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) { |
| 708 | advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf, |
| 709 | ((unsigned int) uaddr[n] >> 4) & 0xf); |
| 710 | if (advance > 0 && qp + advance < ep) |
| 711 | qp += advance; |
| 712 | else { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 713 | // TODO: Consider converting to a private extended EAI_* error code. |
| 714 | // Currently, the EAI_* value has no corresponding error code for an internal |
| 715 | // out of buffer space. In order to not rely on errno, convert the original |
| 716 | // error code EAI_SYSTEM to EAI_MEMORY. |
| 717 | return EAI_MEMORY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 721 | // TODO: Consider converting to a private extended EAI_* error code. |
| 722 | // Currently, the EAI_* value has no corresponding error code for an internal |
| 723 | // out of buffer space. In order to not rely on errno, convert the original |
| 724 | // error code EAI_SYSTEM to EAI_MEMORY. |
| 725 | return EAI_MEMORY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 726 | } |
| 727 | break; |
| 728 | default: |
Hungming Chen | 598202c | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 729 | return EAI_FAMILY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 730 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 731 | |
waynema | 067e984 | 2019-03-08 19:13:41 +0800 | [diff] [blame] | 732 | auto buf = std::make_unique<querybuf>(); |
| 733 | |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 734 | ResState res; |
| 735 | res_init(&res, netcontext, event); |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 736 | int he; |
Bernie Innocenti | 0848711 | 2019-10-11 21:14:13 +0900 | [diff] [blame] | 737 | n = res_nquery(&res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf), &he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 738 | if (n < 0) { |
Ken Chen | ffc224a | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 739 | LOG(DEBUG) << __func__ << ": res_nquery failed (" << n << ")"; |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 740 | // Note that res_nquery() doesn't set the pair NETDB_INTERNAL and errno. |
| 741 | // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA. |
| 742 | // See also herrnoToAiErrno(). |
| 743 | return herrnoToAiErrno(he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 744 | } |
Bernie Innocenti | 3479d3b | 2019-10-08 22:23:26 +0900 | [diff] [blame] | 745 | hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, info->hp, info->buf, info->buflen, &he); |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 746 | if (hp == NULL) return herrnoToAiErrno(he); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 747 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 748 | char* bf = (char*) (hp->h_addr_list + 2); |
| 749 | size_t blen = (size_t)(bf - info->buf); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 750 | if (blen + info->hp->h_length > info->buflen) goto nospc; |
| 751 | hp->h_addr_list[0] = bf; |
| 752 | hp->h_addr_list[1] = NULL; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 753 | memcpy(bf, uaddr, (size_t) info->hp->h_length); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 754 | |
nuccachen | c28257d | 2018-09-11 11:20:00 +0800 | [diff] [blame] | 755 | /* Reserve enough space for mapping IPv4 address to IPv6 address in place */ |
| 756 | if (info->hp->h_addrtype == AF_INET) { |
| 757 | if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc; |
| 758 | // Pad zero to the unused address space |
| 759 | memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD)); |
| 760 | } |
| 761 | |
Hungming Chen | 598202c | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 762 | return 0; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 763 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 764 | nospc: |
Hungming Chen | 598202c | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 765 | return EAI_MEMORY; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 766 | } |
| 767 | |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 768 | int herrnoToAiErrno(int he) { |
| 769 | switch (he) { |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 770 | // extended h_errno |
| 771 | case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT: |
| 772 | return NETD_RESOLV_TIMEOUT; |
| 773 | // legacy h_errno |
| 774 | case NETDB_SUCCESS: |
| 775 | return 0; |
| 776 | case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead |
| 777 | case NO_DATA: // NO_ADDRESS |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 778 | return EAI_NODATA; |
| 779 | case TRY_AGAIN: |
| 780 | return EAI_AGAIN; |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 781 | case NETDB_INTERNAL: |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 782 | // TODO: Remove ENOSPC and call abort() immediately whenever any allocation fails. |
| 783 | if (errno == ENOSPC) return EAI_MEMORY; |
| 784 | // Theoretically, this should not happen. Leave this here just in case. |
| 785 | // Currently, getanswer() of {gethnamaddr, getaddrinfo}.cpp, res_nsearch() and |
| 786 | // res_searchN() use this function to convert error code. Only getanswer() |
| 787 | // of gethnamaddr.cpp may return the error code pair, herrno NETDB_INTERNAL and |
| 788 | // errno ENOSPC, which has already converted to EAI_MEMORY. The remaining functions |
| 789 | // don't set the pair herrno and errno. |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 790 | return EAI_SYSTEM; // see errno for detail |
| 791 | case NO_RECOVERY: |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 792 | default: |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 793 | return EAI_FAIL; // TODO: Perhaps convert default to EAI_MAX (unknown error) instead |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 794 | } |
Bernie Innocenti | e71a28a | 2019-05-29 00:42:35 +0900 | [diff] [blame] | 795 | } |