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