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 | |
waynema | 4c6fd92 | 2019-03-08 19:13:41 +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) |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame^] | 280 | LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " " |
| 281 | << p_type(qtype) << "\", got type \"" << p_type(type) << "\""; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 282 | cp += n; |
| 283 | continue; /* XXX - had_error++ ? */ |
| 284 | } |
| 285 | switch (type) { |
| 286 | case T_PTR: |
| 287 | if (strcasecmp(tname, bp) != 0) { |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame^] | 288 | LOG(DEBUG) << __func__ << ": asked for \"" << qname << "\", got \"" << bp |
| 289 | << "\""; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 290 | cp += n; |
| 291 | continue; /* XXX - had_error++ ? */ |
| 292 | } |
| 293 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 294 | if ((n < 0) || !maybe_hnok(res, bp)) { |
| 295 | had_error++; |
| 296 | break; |
| 297 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 298 | cp += n; |
| 299 | if (cp != erdata) goto no_recovery; |
| 300 | if (!haveanswer) |
| 301 | hent->h_name = bp; |
| 302 | else |
lifr | 1bebdb5 | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 303 | aliases.push_back(bp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 304 | if (n != -1) { |
| 305 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 306 | if (n >= MAXHOSTNAMELEN) { |
| 307 | had_error++; |
| 308 | break; |
| 309 | } |
| 310 | bp += n; |
| 311 | } |
| 312 | break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 313 | case T_A: |
| 314 | case T_AAAA: |
| 315 | if (strcasecmp(hent->h_name, bp) != 0) { |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame^] | 316 | LOG(DEBUG) << __func__ << ": asked for \"" << hent->h_name << "\", got \"" << bp |
| 317 | << "\""; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 318 | cp += n; |
| 319 | continue; /* XXX - had_error++ ? */ |
| 320 | } |
| 321 | if (n != hent->h_length) { |
| 322 | cp += n; |
| 323 | continue; |
| 324 | } |
| 325 | if (type == T_AAAA) { |
| 326 | struct in6_addr in6; |
| 327 | memcpy(&in6, cp, NS_IN6ADDRSZ); |
| 328 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 329 | cp += n; |
| 330 | continue; |
| 331 | } |
| 332 | } |
| 333 | if (!haveanswer) { |
| 334 | int nn; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 335 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 336 | hent->h_name = bp; |
| 337 | nn = (int) strlen(bp) + 1; /* for the \0 */ |
| 338 | bp += nn; |
| 339 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 340 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 341 | bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 342 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 343 | if (bp + n >= ep) { |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame^] | 344 | LOG(DEBUG) << __func__ << ": size (" << n << ") too big"; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 345 | had_error++; |
| 346 | continue; |
| 347 | } |
| 348 | if (hap >= &addr_ptrs[MAXADDRS - 1]) { |
| 349 | if (!toobig++) { |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame^] | 350 | LOG(DEBUG) << __func__ << ": Too many addresses (" << MAXADDRS << ")"; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 351 | } |
| 352 | cp += n; |
| 353 | continue; |
| 354 | } |
| 355 | (void) memcpy(*hap++ = bp, cp, (size_t) n); |
| 356 | bp += n; |
| 357 | cp += n; |
| 358 | if (cp != erdata) goto no_recovery; |
| 359 | break; |
| 360 | default: |
| 361 | abort(); |
| 362 | } |
| 363 | if (!had_error) haveanswer++; |
| 364 | } |
| 365 | if (haveanswer) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 366 | *hap = NULL; |
| 367 | /* |
| 368 | * Note: we sort even if host can take only one address |
| 369 | * in its return structures - should give it the "best" |
| 370 | * address in that case, not some random one |
| 371 | */ |
| 372 | if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res); |
| 373 | if (!hent->h_name) { |
| 374 | n = (int) strlen(qname) + 1; /* for the \0 */ |
| 375 | if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery; |
| 376 | strlcpy(bp, qname, (size_t)(ep - bp)); |
| 377 | hent->h_name = bp; |
| 378 | bp += n; |
| 379 | } |
| 380 | if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep); |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 381 | if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 382 | goto success; |
| 383 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 384 | no_recovery: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 385 | *he = NO_RECOVERY; |
| 386 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 387 | success: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 388 | bp = (char*) ALIGN(bp); |
lifr | 1bebdb5 | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 389 | aliases.push_back(nullptr); |
| 390 | qlen = aliases.size() * sizeof(*hent->h_aliases); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 391 | if ((size_t)(ep - bp) < qlen) goto nospc; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 392 | hent->h_aliases = (char**) bp; |
lifr | 1bebdb5 | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 393 | memcpy(bp, aliases.data(), qlen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 394 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 395 | bp += qlen; |
| 396 | n = (int) (hap - addr_ptrs); |
| 397 | qlen = (n + 1) * sizeof(*hent->h_addr_list); |
| 398 | if ((size_t)(ep - bp) < qlen) goto nospc; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 399 | hent->h_addr_list = (char**) bp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 400 | memcpy(bp, addr_ptrs, qlen); |
| 401 | *he = NETDB_SUCCESS; |
| 402 | return hent; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 403 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 404 | errno = ENOSPC; |
| 405 | *he = NETDB_INTERNAL; |
| 406 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 407 | } |
| 408 | |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 409 | 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] | 410 | char* buf, size_t buflen) { |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 411 | getnamaddr info; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 412 | size_t size; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 413 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 414 | _DIAGASSERT(name != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 415 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 416 | switch (af) { |
| 417 | case AF_INET: |
| 418 | size = NS_INADDRSZ; |
| 419 | break; |
| 420 | case AF_INET6: |
| 421 | size = NS_IN6ADDRSZ; |
| 422 | break; |
| 423 | default: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 424 | errno = EAFNOSUPPORT; |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 425 | return EAI_FAMILY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 426 | } |
| 427 | if (buflen < size) goto nospc; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 428 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 429 | hp->h_addrtype = af; |
| 430 | hp->h_length = (int) size; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 431 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 432 | /* |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 433 | * disallow names consisting only of digits/dots, unless |
| 434 | * they end in a dot. |
| 435 | */ |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 436 | if (isdigit((u_char) name[0])) { |
| 437 | for (const char* cp = name;; ++cp) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 438 | if (!*cp) { |
| 439 | if (*--cp == '.') break; |
| 440 | /* |
| 441 | * All-numeric, no dot at the end. |
| 442 | * Fake up a hostent as if we'd actually |
| 443 | * done a lookup. |
| 444 | */ |
| 445 | goto fake; |
| 446 | } |
| 447 | if (!isdigit((u_char) *cp) && *cp != '.') break; |
| 448 | } |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 449 | } |
| 450 | if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') { |
| 451 | for (const char* cp = name;; ++cp) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 452 | if (!*cp) { |
| 453 | if (*--cp == '.') break; |
| 454 | /* |
| 455 | * All-IPv6-legal, no dot at the end. |
| 456 | * Fake up a hostent as if we'd actually |
| 457 | * done a lookup. |
| 458 | */ |
| 459 | goto fake; |
| 460 | } |
| 461 | if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break; |
| 462 | } |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 463 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 464 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 465 | info.hp = hp; |
| 466 | info.buf = buf; |
| 467 | info.buflen = buflen; |
Hungming Chen | 56273d7 | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 468 | if (_hf_gethtbyname2(name, af, &info)) { |
waynema | 1a1d8b6 | 2019-03-08 16:58:06 +0800 | [diff] [blame] | 469 | int error = dns_gethtbyname(name, af, &info); |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 470 | if (error != 0) { |
| 471 | return error; |
Bernie Innocenti | 9bf0e1d | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 472 | } |
| 473 | } |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 474 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 475 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 476 | errno = ENOSPC; |
Hungming Chen | 56273d7 | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 477 | return EAI_MEMORY; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 478 | fake: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 479 | HENT_ARRAY(hp->h_addr_list, 1, buf, buflen); |
| 480 | HENT_ARRAY(hp->h_aliases, 0, buf, buflen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 481 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 482 | hp->h_aliases[0] = NULL; |
| 483 | if (size > buflen) goto nospc; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 484 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 485 | if (inet_pton(af, name, buf) <= 0) { |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 486 | return EAI_NODATA; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 487 | } |
| 488 | hp->h_addr_list[0] = buf; |
| 489 | hp->h_addr_list[1] = NULL; |
| 490 | buf += size; |
| 491 | buflen -= size; |
| 492 | HENT_SCOPY(hp->h_name, name, buf, buflen); |
| 493 | 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] | 494 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // very similar in proxy-ness to android_getaddrinfo_proxy |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 498 | 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] | 499 | size_t hbuflen, const android_net_context* netcontext) { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 500 | res_setnetcontext(res, netcontext); |
Hungming Chen | 56273d7 | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 501 | return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 502 | } |
| 503 | |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 504 | static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t len, int af, |
| 505 | struct hostent* hp, char* buf, size_t buflen, |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 506 | const struct android_net_context* netcontext) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 507 | const u_char* uaddr = (const u_char*) addr; |
| 508 | socklen_t size; |
| 509 | struct getnamaddr info; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 510 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 511 | _DIAGASSERT(addr != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 512 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 513 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 514 | (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) || |
| 515 | IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) { |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 516 | return EAI_NODATA; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 517 | } |
| 518 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 519 | (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) || |
| 520 | IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) { |
| 521 | /* Unmap. */ |
| 522 | uaddr += NS_IN6ADDRSZ - NS_INADDRSZ; |
| 523 | addr = uaddr; |
| 524 | af = AF_INET; |
| 525 | len = NS_INADDRSZ; |
| 526 | } |
| 527 | switch (af) { |
| 528 | case AF_INET: |
| 529 | size = NS_INADDRSZ; |
| 530 | break; |
| 531 | case AF_INET6: |
| 532 | size = NS_IN6ADDRSZ; |
| 533 | break; |
| 534 | default: |
| 535 | errno = EAFNOSUPPORT; |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 536 | return EAI_FAMILY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 537 | } |
| 538 | if (size != len) { |
| 539 | errno = EINVAL; |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 540 | // TODO: Consider to remap error code without relying on errno. |
| 541 | return EAI_SYSTEM; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 542 | } |
| 543 | info.hp = hp; |
| 544 | info.buf = buf; |
| 545 | info.buflen = buflen; |
Hungming Chen | 56273d7 | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 546 | if (_hf_gethtbyaddr(uaddr, len, af, &info)) { |
waynema | 4c6fd92 | 2019-03-08 19:13:41 +0800 | [diff] [blame] | 547 | int error = dns_gethtbyaddr(uaddr, len, af, netcontext, &info); |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 548 | if (error != 0) { |
| 549 | return error; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 550 | } |
| 551 | } |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 552 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 553 | } |
| 554 | |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 555 | static int android_gethostbyaddrfornetcontext_proxy_internal( |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 556 | 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] | 557 | const struct android_net_context* netcontext) { |
| 558 | return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 559 | } |
| 560 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 561 | struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen, |
| 562 | int* he) { |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 563 | const size_t line_buf_size = sizeof(res_get_static()->hostbuf); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 564 | char *name; |
lifr | 1bebdb5 | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 565 | char* cp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 566 | int af, len; |
| 567 | size_t anum; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 568 | struct in6_addr host_addr; |
lifr | 1bebdb5 | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 569 | std::vector<char*> aliases; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 570 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 571 | if (hf == NULL) { |
| 572 | *he = NETDB_INTERNAL; |
| 573 | errno = EINVAL; |
| 574 | return NULL; |
| 575 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 576 | char* p = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 577 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 578 | /* Allocate a new space to read file lines like upstream does. |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 579 | * To keep reentrancy we cannot use res_get_static()->hostbuf here, |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 580 | * as the buffer may be used to store content for a previous hostent |
| 581 | * returned by non-reentrant functions like gethostbyname(). |
| 582 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 583 | if ((p = (char*) malloc(line_buf_size)) == NULL) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 584 | goto nospc; |
| 585 | } |
| 586 | for (;;) { |
| 587 | if (!fgets(p, line_buf_size, hf)) { |
| 588 | free(p); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 589 | *he = HOST_NOT_FOUND; |
| 590 | return NULL; |
| 591 | } |
| 592 | if (*p == '#') { |
| 593 | continue; |
| 594 | } |
| 595 | if (!(cp = strpbrk(p, "#\n"))) { |
| 596 | continue; |
| 597 | } |
| 598 | *cp = '\0'; |
| 599 | if (!(cp = strpbrk(p, " \t"))) continue; |
| 600 | *cp++ = '\0'; |
| 601 | if (inet_pton(AF_INET6, p, &host_addr) > 0) { |
| 602 | af = AF_INET6; |
| 603 | len = NS_IN6ADDRSZ; |
| 604 | } else { |
| 605 | if (inet_pton(AF_INET, p, &host_addr) <= 0) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 606 | |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 607 | res_state res = res_get_state(); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 608 | if (res == NULL) goto nospc; |
| 609 | if (res->options & RES_USE_INET6) { |
| 610 | map_v4v6_address(buf, buf); |
| 611 | af = AF_INET6; |
| 612 | len = NS_IN6ADDRSZ; |
| 613 | } else { |
| 614 | af = AF_INET; |
| 615 | len = NS_INADDRSZ; |
| 616 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 617 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 618 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 619 | /* if this is not something we're looking for, skip it. */ |
| 620 | if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue; |
| 621 | if (hent->h_length != 0 && hent->h_length != len) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 622 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 623 | while (*cp == ' ' || *cp == '\t') cp++; |
| 624 | if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0'; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 625 | while (cp && *cp) { |
| 626 | if (*cp == ' ' || *cp == '\t') { |
| 627 | cp++; |
| 628 | continue; |
| 629 | } |
lifr | 1bebdb5 | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 630 | aliases.push_back(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 631 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
| 632 | } |
| 633 | break; |
| 634 | } |
| 635 | hent->h_length = len; |
| 636 | hent->h_addrtype = af; |
| 637 | HENT_ARRAY(hent->h_addr_list, 1, buf, buflen); |
lifr | 1bebdb5 | 2019-01-09 14:41:15 +0800 | [diff] [blame] | 638 | anum = aliases.size(); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 639 | HENT_ARRAY(hent->h_aliases, anum, buf, buflen); |
| 640 | HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen); |
| 641 | hent->h_addr_list[1] = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 642 | |
nuccachen | 8161c99 | 2018-09-11 11:20:00 +0800 | [diff] [blame] | 643 | /* Reserve space for mapping IPv4 address to IPv6 address in place */ |
| 644 | if (hent->h_addrtype == AF_INET) { |
| 645 | HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen); |
| 646 | } |
| 647 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 648 | HENT_SCOPY(hent->h_name, name, buf, buflen); |
| 649 | for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen); |
| 650 | hent->h_aliases[anum] = NULL; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 651 | *he = NETDB_SUCCESS; |
| 652 | free(p); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 653 | return hent; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 654 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 655 | free(p); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 656 | errno = ENOSPC; |
| 657 | *he = NETDB_INTERNAL; |
| 658 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 659 | } |
| 660 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 661 | static void map_v4v6_address(const char* src, char* dst) { |
| 662 | u_char* p = (u_char*) dst; |
| 663 | char tmp[NS_INADDRSZ]; |
| 664 | int i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 665 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 666 | _DIAGASSERT(src != NULL); |
| 667 | _DIAGASSERT(dst != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 668 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 669 | /* Stash a temporary copy so our caller can update in place. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 670 | memcpy(tmp, src, NS_INADDRSZ); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 671 | /* Mark this ipv6 addr as a mapped ipv4. */ |
| 672 | for (i = 0; i < 10; i++) *p++ = 0x00; |
| 673 | *p++ = 0xff; |
| 674 | *p++ = 0xff; |
| 675 | /* Retrieve the saved copy and we're done. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 676 | memcpy(p, tmp, NS_INADDRSZ); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 677 | } |
| 678 | |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 679 | static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep, |
| 680 | std::function<void(struct hostent* hp)> map_param, |
| 681 | std::function<void(char* src, char* dst)> map_addr) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 682 | _DIAGASSERT(hp != NULL); |
| 683 | _DIAGASSERT(bpp != NULL); |
| 684 | _DIAGASSERT(ep != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 685 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 686 | if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return; |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 687 | map_param(hp); |
| 688 | for (char** ap = hp->h_addr_list; *ap; ap++) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 689 | int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align))); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 690 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 691 | if (ep - *bpp < (i + NS_IN6ADDRSZ)) { |
| 692 | /* Out of memory. Truncate address list here. XXX */ |
| 693 | *ap = NULL; |
| 694 | return; |
| 695 | } |
| 696 | *bpp += i; |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 697 | map_addr(*ap, *bpp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 698 | *ap = *bpp; |
| 699 | *bpp += NS_IN6ADDRSZ; |
| 700 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 701 | } |
| 702 | |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 703 | static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) { |
| 704 | convert_v4v6_hostent(hp, bpp, ep, |
| 705 | [](struct hostent* hp) { |
| 706 | hp->h_addrtype = AF_INET6; |
| 707 | hp->h_length = NS_IN6ADDRSZ; |
| 708 | }, |
| 709 | [](char* src, char* dst) { map_v4v6_address(src, dst); }); |
| 710 | } |
| 711 | |
| 712 | /* Reserve space for mapping IPv4 address to IPv6 address in place */ |
| 713 | static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) { |
| 714 | convert_v4v6_hostent(hp, bpp, ep, |
| 715 | [](struct hostent* hp) { |
| 716 | (void) hp; /* unused */ |
| 717 | }, |
| 718 | [](char* src, char* dst) { |
| 719 | memcpy(dst, src, NS_INADDRSZ); |
| 720 | memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD)); |
| 721 | }); |
| 722 | } |
| 723 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 724 | static void addrsort(char** ap, int num, res_state res) { |
| 725 | int i, j; |
| 726 | char** p; |
| 727 | short aval[MAXADDRS]; |
| 728 | int needsort = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 729 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 730 | _DIAGASSERT(ap != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 731 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 732 | p = ap; |
| 733 | for (i = 0; i < num; i++, p++) { |
| 734 | for (j = 0; (unsigned) j < res->nsort; j++) |
| 735 | if (res->sort_list[j].addr.s_addr == |
| 736 | (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask)) |
| 737 | break; |
| 738 | aval[i] = j; |
| 739 | if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i; |
| 740 | } |
| 741 | if (!needsort) return; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 742 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 743 | while (needsort < num) { |
| 744 | for (j = needsort - 1; j >= 0; j--) { |
| 745 | if (aval[j] > aval[j + 1]) { |
| 746 | char* hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 747 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 748 | i = aval[j]; |
| 749 | aval[j] = aval[j + 1]; |
| 750 | aval[j + 1] = i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 751 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 752 | hp = ap[j]; |
| 753 | ap[j] = ap[j + 1]; |
| 754 | ap[j + 1] = hp; |
| 755 | } else |
| 756 | break; |
| 757 | } |
| 758 | needsort++; |
| 759 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 760 | } |
| 761 | |
waynema | 1a1d8b6 | 2019-03-08 16:58:06 +0800 | [diff] [blame] | 762 | static int dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 763 | int n, type; |
Bernie Innocenti | 9bf0e1d | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 764 | info->hp->h_addrtype = addr_type; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 765 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 766 | switch (info->hp->h_addrtype) { |
| 767 | case AF_INET: |
| 768 | info->hp->h_length = NS_INADDRSZ; |
| 769 | type = T_A; |
| 770 | break; |
| 771 | case AF_INET6: |
| 772 | info->hp->h_length = NS_IN6ADDRSZ; |
| 773 | type = T_AAAA; |
| 774 | break; |
| 775 | default: |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 776 | return EAI_FAMILY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 777 | } |
waynema | 1a1d8b6 | 2019-03-08 16:58:06 +0800 | [diff] [blame] | 778 | auto buf = std::make_unique<querybuf>(); |
| 779 | |
| 780 | res_state res = res_get_state(); |
| 781 | if (!res) return EAI_MEMORY; |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 782 | |
Hungming Chen | 56273d7 | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 783 | int herrno = NETDB_INTERNAL; |
Hungming Chen | 7f0d329 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 784 | 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] | 785 | if (n < 0) { |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame^] | 786 | LOG(DEBUG) << __func__ << ": res_nsearch failed (" << n << ")"; |
Hungming Chen | 7f0d329 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 787 | // Pass herrno to catch more detailed errors rather than EAI_NODATA. |
| 788 | return herrnoToAiErrno(herrno); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 789 | } |
waynema | 1a1d8b6 | 2019-03-08 16:58:06 +0800 | [diff] [blame] | 790 | hostent* hp = |
| 791 | 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] | 792 | if (hp == NULL) { |
Hungming Chen | 7f0d329 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 793 | return herrnoToAiErrno(herrno); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 794 | } |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 795 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 796 | } |
| 797 | |
waynema | 4c6fd92 | 2019-03-08 19:13:41 +0800 | [diff] [blame] | 798 | static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, |
| 799 | const android_net_context* netcontext, getnamaddr* info) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 800 | char qbuf[MAXDNAME + 1], *qp, *ep; |
| 801 | int n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 802 | int advance; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 803 | |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 804 | info->hp->h_length = len; |
| 805 | info->hp->h_addrtype = af; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 806 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 807 | switch (info->hp->h_addrtype) { |
| 808 | case AF_INET: |
| 809 | (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff), |
| 810 | (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff)); |
| 811 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 812 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 813 | case AF_INET6: |
| 814 | qp = qbuf; |
| 815 | ep = qbuf + sizeof(qbuf) - 1; |
| 816 | for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) { |
| 817 | advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf, |
| 818 | ((unsigned int) uaddr[n] >> 4) & 0xf); |
| 819 | if (advance > 0 && qp + advance < ep) |
| 820 | qp += advance; |
| 821 | else { |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 822 | // TODO: Consider to remap error code without relying on errno. |
| 823 | return EAI_SYSTEM; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 827 | // TODO: Consider to remap error code without relying on errno. |
| 828 | return EAI_SYSTEM; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 829 | } |
| 830 | break; |
| 831 | default: |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 832 | return EAI_FAMILY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 833 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 834 | |
waynema | 4c6fd92 | 2019-03-08 19:13:41 +0800 | [diff] [blame] | 835 | auto buf = std::make_unique<querybuf>(); |
| 836 | |
| 837 | res_state res = res_get_state(); |
| 838 | if (!res) return EAI_MEMORY; |
| 839 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 840 | res_setnetcontext(res, netcontext); |
Hungming Chen | 56273d7 | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 841 | int herrno = NETDB_INTERNAL; |
Hungming Chen | 7f0d329 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 842 | 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] | 843 | if (n < 0) { |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame^] | 844 | LOG(DEBUG) << __func__ << ": res_nquery failed (" << n << ")"; |
Hungming Chen | 7f0d329 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 845 | return herrnoToAiErrno(herrno); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 846 | } |
waynema | 4c6fd92 | 2019-03-08 19:13:41 +0800 | [diff] [blame] | 847 | hostent* hp = |
| 848 | getanswer(buf.get(), n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &herrno); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 849 | if (hp == NULL) { |
Hungming Chen | 7f0d329 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 850 | return herrnoToAiErrno(herrno); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 851 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 852 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 853 | char* bf = (char*) (hp->h_addr_list + 2); |
| 854 | size_t blen = (size_t)(bf - info->buf); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 855 | if (blen + info->hp->h_length > info->buflen) goto nospc; |
| 856 | hp->h_addr_list[0] = bf; |
| 857 | hp->h_addr_list[1] = NULL; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 858 | memcpy(bf, uaddr, (size_t) info->hp->h_length); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 859 | if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) { |
| 860 | if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc; |
| 861 | map_v4v6_address(bf, bf); |
| 862 | hp->h_addrtype = AF_INET6; |
| 863 | hp->h_length = NS_IN6ADDRSZ; |
| 864 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 865 | |
nuccachen | 8161c99 | 2018-09-11 11:20:00 +0800 | [diff] [blame] | 866 | /* Reserve enough space for mapping IPv4 address to IPv6 address in place */ |
| 867 | if (info->hp->h_addrtype == AF_INET) { |
| 868 | if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc; |
| 869 | // Pad zero to the unused address space |
| 870 | memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD)); |
| 871 | } |
| 872 | |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 873 | return 0; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 874 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 875 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 876 | errno = ENOSPC; |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 877 | return EAI_MEMORY; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 878 | } |
| 879 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 880 | /* |
| 881 | * Non-reentrant versions. |
| 882 | */ |
| 883 | |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 884 | int android_gethostbynamefornetcontext(const char* name, int af, |
| 885 | const struct android_net_context* netcontext, hostent** hp) { |
| 886 | int error; |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 887 | res_state res = res_get_state(); |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 888 | if (res == NULL) return EAI_MEMORY; |
| 889 | res_static* rs = res_get_static(); // For thread-safety. |
| 890 | 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] | 891 | netcontext); |
Mike Yu | cac05e4 | 2018-11-06 19:20:07 +0800 | [diff] [blame] | 892 | if (error == 0) { |
| 893 | *hp = &rs->host; |
| 894 | } |
| 895 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 896 | } |
| 897 | |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 898 | int android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af, |
| 899 | const struct android_net_context* netcontext, hostent** hp) { |
| 900 | return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext, hp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 901 | } |
| 902 | |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 903 | static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af, |
| 904 | const struct android_net_context* netcontext, |
| 905 | hostent** hp) { |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 906 | struct res_static* rs = res_get_static(); // For thread-safety. |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 907 | int error = android_gethostbyaddrfornetcontext_proxy_internal( |
Hungming Chen | 56273d7 | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 908 | addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), netcontext); |
Hungming Chen | 806d446 | 2018-12-26 17:04:43 +0800 | [diff] [blame] | 909 | if (error == 0) *hp = &rs->host; |
| 910 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 911 | } |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 912 | |
Hungming Chen | 7f0d329 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 913 | int herrnoToAiErrno(int herrno) { |
| 914 | switch (herrno) { |
| 915 | // extended h_errno |
| 916 | case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT: |
| 917 | return NETD_RESOLV_TIMEOUT; |
| 918 | // legacy h_errno |
| 919 | case NETDB_SUCCESS: |
| 920 | return 0; |
| 921 | case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead |
| 922 | case NO_DATA: // NO_ADDRESS |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 923 | return EAI_NODATA; |
| 924 | case TRY_AGAIN: |
| 925 | return EAI_AGAIN; |
Hungming Chen | 7f0d329 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 926 | case NETDB_INTERNAL: |
| 927 | return EAI_SYSTEM; // see errno for detail |
| 928 | case NO_RECOVERY: |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 929 | default: |
| 930 | return EAI_FAIL; |
| 931 | } |
chenbruce | 16adee4 | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 932 | } |