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