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