Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */ |
| 2 | /* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the project nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Issues to be discussed: |
| 35 | * - Thread safe-ness must be checked. |
| 36 | * - Return values. There are nonstandard return values defined and used |
| 37 | * in the source code. This is because RFC2553 is silent about which error |
| 38 | * code must be returned for which situation. |
| 39 | * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2 |
| 40 | * says to use inet_aton() to convert IPv4 numeric to binary (alows |
| 41 | * classful form as a result). |
| 42 | * current code - disallow classful form for IPv4 (due to use of inet_pton). |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 43 | * Note: |
| 44 | * - We use getipnodebyname() just for thread-safeness. There's no intent |
| 45 | * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to |
| 46 | * getipnodebyname(). |
| 47 | * - The code filters out AFs that are not supported by the kernel, |
| 48 | * when globbing NULL hostname (to loopback, or wildcard). Is it the right |
| 49 | * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG |
| 50 | * in ai_flags? |
| 51 | * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. |
| 52 | * (1) what should we do against numeric hostname (2) what should we do |
| 53 | * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? |
| 54 | * non-loopback address configured? global address configured? |
| 55 | * - To avoid search order issue, we have a big amount of code duplicate |
| 56 | * from gethnamaddr.c and some other places. The issues that there's no |
| 57 | * lower layer function to lookup "IPv4 or IPv6" record. Calling |
| 58 | * gethostbyname2 from getaddrinfo will end up in wrong search order, as |
| 59 | * follows: |
| 60 | * - The code makes use of following calls when asked to resolver with |
| 61 | * ai_family = PF_UNSPEC: |
| 62 | * getipnodebyname(host, AF_INET6); |
| 63 | * getipnodebyname(host, AF_INET); |
| 64 | * This will result in the following queries if the node is configure to |
| 65 | * prefer /etc/hosts than DNS: |
| 66 | * lookup /etc/hosts for IPv6 address |
| 67 | * lookup DNS for IPv6 address |
| 68 | * lookup /etc/hosts for IPv4 address |
| 69 | * lookup DNS for IPv4 address |
| 70 | * which may not meet people's requirement. |
| 71 | * The right thing to happen is to have underlying layer which does |
| 72 | * PF_UNSPEC lookup (lookup both) and return chain of addrinfos. |
| 73 | * This would result in a bit of code duplicate with _dns_ghbyname() and |
| 74 | * friends. |
| 75 | */ |
| 76 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 77 | #include <arpa/inet.h> |
| 78 | #include <arpa/nameser.h> |
| 79 | #include <assert.h> |
| 80 | #include <ctype.h> |
| 81 | #include <errno.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 82 | #include <fcntl.h> |
| 83 | #include <net/if.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 84 | #include <netdb.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 85 | #include <netinet/in.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 86 | #include <stdbool.h> |
| 87 | #include <stddef.h> |
| 88 | #include <stdio.h> |
| 89 | #include <stdlib.h> |
| 90 | #include <string.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 91 | #include <sys/param.h> |
| 92 | #include <sys/socket.h> |
| 93 | #include <sys/stat.h> |
| 94 | #include <sys/types.h> |
| 95 | #include <sys/un.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 96 | #include <syslog.h> |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 97 | #include <unistd.h> |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 98 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 99 | #include "netd_resolv/resolv.h" |
| 100 | #include "resolv_cache.h" |
| 101 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 102 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 103 | #define ANY 0 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 104 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 105 | static const char in_addrany[] = {0, 0, 0, 0}; |
| 106 | static const char in_loopback[] = {127, 0, 0, 1}; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 107 | static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 108 | static const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 109 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 110 | static const struct afd { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 111 | int a_af; |
| 112 | int a_addrlen; |
| 113 | int a_socklen; |
| 114 | int a_off; |
| 115 | const char* a_addrany; |
| 116 | const char* a_loopback; |
| 117 | int a_scoped; |
| 118 | } afdl[] = { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), |
| 120 | offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1}, |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 121 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), |
| 122 | offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0}, |
| 123 | {0, 0, 0, 0, NULL, NULL, 0}, |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | struct explore { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 127 | int e_af; |
| 128 | int e_socktype; |
| 129 | int e_protocol; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 130 | int e_wild; |
| 131 | #define WILD_AF(ex) ((ex)->e_wild & 0x01) |
| 132 | #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) |
| 133 | #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 134 | }; |
| 135 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 136 | static const struct explore explore_options[] = { |
| 137 | {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07}, |
| 138 | {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07}, |
| 139 | {PF_INET6, SOCK_RAW, ANY, 0x05}, |
| 140 | {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07}, |
| 141 | {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07}, |
| 142 | {PF_INET, SOCK_RAW, ANY, 0x05}, |
| 143 | {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07}, |
| 144 | {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07}, |
| 145 | {PF_UNSPEC, SOCK_RAW, ANY, 0x05}, |
| 146 | {-1, 0, 0, 0}, |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 147 | }; |
| 148 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 149 | #define PTON_MAX 16 |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 150 | #define MAXPACKET (8 * 1024) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 151 | |
| 152 | typedef union { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 153 | HEADER hdr; |
| 154 | u_char buf[MAXPACKET]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 155 | } querybuf; |
| 156 | |
| 157 | struct res_target { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 158 | struct res_target* next; |
| 159 | const char* name; /* domain name */ |
| 160 | int qclass, qtype; /* class and type of query */ |
| 161 | u_char* answer; /* buffer to put answer */ |
| 162 | int anslen; /* size of answer buffer */ |
| 163 | int n; /* result length */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 164 | }; |
| 165 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 166 | static int str2number(const char*); |
| 167 | static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**, |
| 168 | const struct android_net_context*); |
| 169 | static int explore_null(const struct addrinfo*, const char*, struct addrinfo**); |
| 170 | static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**, |
| 171 | const char*); |
| 172 | static int explore_numeric_scope(const struct addrinfo*, const char*, const char*, |
| 173 | struct addrinfo**); |
| 174 | static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*); |
| 175 | static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*); |
| 176 | static int get_portmatch(const struct addrinfo*, const char*); |
| 177 | static int get_port(const struct addrinfo*, const char*, int); |
| 178 | static const struct afd* find_afd(int); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 179 | static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 180 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 181 | static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*); |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 182 | static int dns_getaddrinfo(const char* name, const addrinfo* pai, |
| 183 | const android_net_context* netcontext, addrinfo** rv); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 184 | static void _sethtent(FILE**); |
| 185 | static void _endhtent(FILE**); |
| 186 | static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*); |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 187 | static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 188 | static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 189 | |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 190 | static int res_queryN(const char* name, res_target* target, res_state res, int* ai_error); |
| 191 | static int res_searchN(const char* name, res_target* target, res_state res, int* ai_error); |
| 192 | static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res, |
| 193 | int* ai_error); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 194 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 195 | static const char* const ai_errlist[] = { |
| 196 | "Success", |
| 197 | "Address family for hostname not supported", /* EAI_ADDRFAMILY */ |
| 198 | "Temporary failure in name resolution", /* EAI_AGAIN */ |
| 199 | "Invalid value for ai_flags", /* EAI_BADFLAGS */ |
| 200 | "Non-recoverable failure in name resolution", /* EAI_FAIL */ |
| 201 | "ai_family not supported", /* EAI_FAMILY */ |
| 202 | "Memory allocation failure", /* EAI_MEMORY */ |
| 203 | "No address associated with hostname", /* EAI_NODATA */ |
| 204 | "hostname nor servname provided, or not known", /* EAI_NONAME */ |
| 205 | "servname not supported for ai_socktype", /* EAI_SERVICE */ |
| 206 | "ai_socktype not supported", /* EAI_SOCKTYPE */ |
| 207 | "System error returned in errno", /* EAI_SYSTEM */ |
| 208 | "Invalid value for hints", /* EAI_BADHINTS */ |
| 209 | "Resolved protocol is unknown", /* EAI_PROTOCOL */ |
| 210 | "Argument buffer overflow", /* EAI_OVERFLOW */ |
| 211 | "Unknown error", /* EAI_MAX */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | /* XXX macros that make external reference is BAD. */ |
| 215 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 216 | #define GET_AI(ai, afd, addr) \ |
| 217 | do { \ |
| 218 | /* external reference: pai, error, and label free */ \ |
| 219 | (ai) = get_ai(pai, (afd), (addr)); \ |
| 220 | if ((ai) == NULL) { \ |
| 221 | error = EAI_MEMORY; \ |
| 222 | goto free; \ |
| 223 | } \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 224 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 225 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 226 | #define GET_PORT(ai, serv) \ |
| 227 | do { \ |
| 228 | /* external reference: error and label free */ \ |
| 229 | error = get_port((ai), (serv), 0); \ |
| 230 | if (error != 0) goto free; \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 231 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 232 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 233 | #define MATCH_FAMILY(x, y, w) \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 234 | ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) |
| 235 | #define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY))) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 236 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 237 | const char* gai_strerror(int ecode) { |
| 238 | if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX; |
| 239 | return ai_errlist[ecode]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 242 | void freeaddrinfo(struct addrinfo* ai) { |
Bernie Innocenti | b050f9b | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 243 | while (ai) { |
| 244 | struct addrinfo* next = ai->ai_next; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 245 | if (ai->ai_canonname) free(ai->ai_canonname); |
Bernie Innocenti | b050f9b | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 246 | // Also frees ai->ai_addr which points to extra space beyond addrinfo |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 247 | free(ai); |
| 248 | ai = next; |
Bernie Innocenti | b050f9b | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 249 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 250 | } |
| 251 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 252 | static int str2number(const char* p) { |
| 253 | char* ep; |
| 254 | unsigned long v; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 255 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 256 | assert(p != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 257 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 258 | if (*p == '\0') return -1; |
| 259 | ep = NULL; |
| 260 | errno = 0; |
| 261 | v = strtoul(p, &ep, 10); |
| 262 | if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) |
| 263 | return v; |
| 264 | else |
| 265 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * The following functions determine whether IPv4 or IPv6 connectivity is |
| 270 | * available in order to implement AI_ADDRCONFIG. |
| 271 | * |
| 272 | * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is |
| 273 | * available, but whether addresses of the specified family are "configured |
| 274 | * on the local system". However, bionic doesn't currently support getifaddrs, |
| 275 | * so checking for connectivity is the next best thing. |
| 276 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 277 | static int _have_ipv6(unsigned mark, uid_t uid) { |
| 278 | static const struct sockaddr_in6 sin6_test = { |
| 279 | .sin6_family = AF_INET6, |
| 280 | .sin6_addr.s6_addr = {// 2000:: |
| 281 | 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 282 | sockaddr_union addr = {.sin6 = sin6_test}; |
| 283 | return _find_src_addr(&addr.sa, NULL, mark, uid) == 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 284 | } |
| 285 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 286 | static int _have_ipv4(unsigned mark, uid_t uid) { |
| 287 | static const struct sockaddr_in sin_test = { |
| 288 | .sin_family = AF_INET, |
| 289 | .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8 |
| 290 | }; |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 291 | sockaddr_union addr = {.sin = sin_test}; |
| 292 | return _find_src_addr(&addr.sa, NULL, mark, uid) == 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | bool readBE32(FILE* fp, int32_t* result) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 296 | int32_t tmp; |
| 297 | if (fread(&tmp, sizeof(tmp), 1, fp) != 1) { |
| 298 | return false; |
| 299 | } |
| 300 | *result = ntohl(tmp); |
| 301 | return true; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 302 | } |
| 303 | |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 304 | // Internal version of getaddrinfo(), but limited to AI_NUMERICHOST. |
| 305 | // NOTE: also called by resolv_set_nameservers_for_net(). |
| 306 | int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints, |
| 307 | addrinfo** result) { |
| 308 | hints.ai_flags = AI_NUMERICHOST; |
| 309 | const android_net_context netcontext = { |
| 310 | .app_netid = NETID_UNSET, |
| 311 | .app_mark = MARK_UNSET, |
| 312 | .dns_netid = NETID_UNSET, |
| 313 | .dns_mark = MARK_UNSET, |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 314 | .uid = NET_CONTEXT_INVALID_UID, |
| 315 | }; |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 316 | return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 317 | } |
| 318 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 319 | int android_getaddrinfofornetcontext(const char* hostname, const char* servname, |
| 320 | const struct addrinfo* hints, |
| 321 | const struct android_net_context* netcontext, |
| 322 | struct addrinfo** res) { |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 323 | struct addrinfo sentinel = {}; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 324 | struct addrinfo* cur; |
| 325 | int error = 0; |
| 326 | struct addrinfo ai; |
| 327 | struct addrinfo ai0; |
| 328 | struct addrinfo* pai; |
| 329 | const struct explore* ex; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 330 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 331 | /* hostname is allowed to be NULL */ |
| 332 | /* servname is allowed to be NULL */ |
| 333 | /* hints is allowed to be NULL */ |
| 334 | assert(res != NULL); |
| 335 | assert(netcontext != NULL); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 336 | cur = &sentinel; |
| 337 | pai = &ai; |
| 338 | pai->ai_flags = 0; |
| 339 | pai->ai_family = PF_UNSPEC; |
| 340 | pai->ai_socktype = ANY; |
| 341 | pai->ai_protocol = ANY; |
| 342 | pai->ai_addrlen = 0; |
| 343 | pai->ai_canonname = NULL; |
| 344 | pai->ai_addr = NULL; |
| 345 | pai->ai_next = NULL; |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 346 | do { |
| 347 | if (hostname == NULL && servname == NULL) { |
| 348 | error = EAI_NONAME; |
| 349 | break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 350 | } |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 351 | if (hints) { |
| 352 | /* error check for hints */ |
| 353 | if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) { |
| 354 | error = EAI_BADHINTS; |
| 355 | break; |
| 356 | } |
| 357 | if (hints->ai_flags & ~AI_MASK) { |
| 358 | error = EAI_BADFLAGS; |
| 359 | break; |
| 360 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 361 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 362 | if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET || |
| 363 | hints->ai_family == PF_INET6)) { |
| 364 | error = EAI_FAMILY; |
| 365 | break; |
| 366 | } |
| 367 | *pai = *hints; |
| 368 | |
| 369 | /* |
| 370 | * if both socktype/protocol are specified, check if they |
| 371 | * are meaningful combination. |
| 372 | */ |
| 373 | if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { |
| 374 | for (ex = explore_options; ex->e_af >= 0; ex++) { |
| 375 | if (pai->ai_family != ex->e_af) continue; |
| 376 | if (ex->e_socktype == ANY) continue; |
| 377 | if (ex->e_protocol == ANY) continue; |
| 378 | if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) { |
| 379 | error = EAI_BADHINTS; |
| 380 | break; |
| 381 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 382 | } |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 383 | if (error) break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 384 | } |
| 385 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 386 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 387 | /* |
| 388 | * check for special cases. (1) numeric servname is disallowed if |
| 389 | * socktype/protocol are left unspecified. (2) servname is disallowed |
| 390 | * for raw and other inet{,6} sockets. |
| 391 | */ |
| 392 | if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) |
| 393 | || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) |
| 394 | ) { |
| 395 | ai0 = *pai; /* backup *pai */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 396 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 397 | if (pai->ai_family == PF_UNSPEC) { |
| 398 | pai->ai_family = PF_INET6; |
| 399 | } |
| 400 | error = get_portmatch(pai, servname); |
| 401 | if (error) break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 402 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 403 | *pai = ai0; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 404 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 405 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 406 | ai0 = *pai; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 407 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 408 | /* NULL hostname, or numeric hostname */ |
| 409 | for (ex = explore_options; ex->e_af >= 0; ex++) { |
| 410 | *pai = ai0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 411 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 412 | /* PF_UNSPEC entries are prepared for DNS queries only */ |
| 413 | if (ex->e_af == PF_UNSPEC) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 414 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 415 | if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue; |
| 416 | if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue; |
| 417 | if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 418 | |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 419 | if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af; |
| 420 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype; |
| 421 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol; |
| 422 | |
| 423 | if (hostname == NULL) |
| 424 | error = explore_null(pai, servname, &cur->ai_next); |
| 425 | else |
| 426 | error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next); |
| 427 | |
| 428 | if (error) break; |
| 429 | |
| 430 | while (cur->ai_next) cur = cur->ai_next; |
| 431 | } |
| 432 | if (error) break; |
| 433 | |
| 434 | /* |
| 435 | * XXX |
| 436 | * If numeric representation of AF1 can be interpreted as FQDN |
| 437 | * representation of AF2, we need to think again about the code below. |
| 438 | */ |
| 439 | if (sentinel.ai_next) break; |
| 440 | |
| 441 | if (hostname == NULL) { |
| 442 | error = EAI_NODATA; |
| 443 | break; |
| 444 | } |
| 445 | if (pai->ai_flags & AI_NUMERICHOST) { |
| 446 | error = EAI_NONAME; |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * hostname as alphabetical name. |
| 452 | * we would like to prefer AF_INET6 than AF_INET, so we'll make a |
| 453 | * outer loop by AFs. |
| 454 | */ |
| 455 | for (ex = explore_options; ex->e_af >= 0; ex++) { |
| 456 | *pai = ai0; |
| 457 | |
| 458 | /* require exact match for family field */ |
| 459 | if (pai->ai_family != ex->e_af) continue; |
| 460 | |
| 461 | if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) { |
| 462 | continue; |
| 463 | } |
| 464 | if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) { |
| 465 | continue; |
| 466 | } |
| 467 | |
| 468 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype; |
| 469 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol; |
| 470 | |
| 471 | error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext); |
| 472 | |
| 473 | while (cur->ai_next) cur = cur->ai_next; |
| 474 | } |
| 475 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 476 | if (sentinel.ai_next) { |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 477 | error = 0; |
| 478 | } else if (error == 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 479 | error = EAI_FAIL; |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 480 | } |
| 481 | } while (0); |
| 482 | |
| 483 | if (error) { |
| 484 | freeaddrinfo(sentinel.ai_next); |
| 485 | *res = NULL; |
| 486 | } else { |
| 487 | *res = sentinel.ai_next; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 488 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 489 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 490 | } |
| 491 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 492 | // FQDN hostname, DNS lookup |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 493 | static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname, |
| 494 | struct addrinfo** res, const struct android_net_context* netcontext) { |
| 495 | struct addrinfo* result; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 496 | int error = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 497 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 498 | assert(pai != NULL); |
| 499 | /* hostname may be NULL */ |
| 500 | /* servname may be NULL */ |
| 501 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 502 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 503 | result = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 504 | |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 505 | // If the servname does not match socktype/protocol, ignore it. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 506 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 507 | |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 508 | if (!files_getaddrinfo(hostname, pai, &result)) { |
| 509 | error = dns_getaddrinfo(hostname, pai, netcontext, &result); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 510 | } |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 511 | if (!error) { |
| 512 | struct addrinfo* cur; |
| 513 | for (cur = result; cur; cur = cur->ai_next) { |
| 514 | GET_PORT(cur, servname); |
| 515 | /* canonname should be filled already */ |
| 516 | } |
| 517 | *res = result; |
| 518 | return 0; |
| 519 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 520 | |
| 521 | free: |
Bernie Innocenti | b050f9b | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 522 | freeaddrinfo(result); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 523 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /* |
| 527 | * hostname == NULL. |
| 528 | * passive socket -> anyaddr (0.0.0.0 or ::) |
| 529 | * non-passive socket -> localhost (127.0.0.1 or ::1) |
| 530 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 531 | static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) { |
| 532 | int s; |
| 533 | const struct afd* afd; |
| 534 | struct addrinfo* cur; |
| 535 | struct addrinfo sentinel; |
| 536 | int error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 537 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 538 | assert(pai != NULL); |
| 539 | /* servname may be NULL */ |
| 540 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 541 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 542 | *res = NULL; |
| 543 | sentinel.ai_next = NULL; |
| 544 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 545 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 546 | /* |
| 547 | * filter out AFs that are not supported by the kernel |
| 548 | * XXX errno? |
| 549 | */ |
| 550 | s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 551 | if (s < 0) { |
| 552 | if (errno != EMFILE) return 0; |
| 553 | } else |
| 554 | close(s); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 555 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 556 | /* |
| 557 | * if the servname does not match socktype/protocol, ignore it. |
| 558 | */ |
| 559 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 560 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 561 | afd = find_afd(pai->ai_family); |
| 562 | if (afd == NULL) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 563 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 564 | if (pai->ai_flags & AI_PASSIVE) { |
| 565 | GET_AI(cur->ai_next, afd, afd->a_addrany); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 566 | GET_PORT(cur->ai_next, servname); |
| 567 | } else { |
| 568 | GET_AI(cur->ai_next, afd, afd->a_loopback); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 569 | GET_PORT(cur->ai_next, servname); |
| 570 | } |
| 571 | cur = cur->ai_next; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 572 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 573 | *res = sentinel.ai_next; |
| 574 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 575 | |
| 576 | free: |
Bernie Innocenti | b050f9b | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 577 | freeaddrinfo(sentinel.ai_next); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 578 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
| 582 | * numeric hostname |
| 583 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 584 | static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname, |
| 585 | struct addrinfo** res, const char* canonname) { |
| 586 | const struct afd* afd; |
| 587 | struct addrinfo* cur; |
| 588 | struct addrinfo sentinel; |
| 589 | int error; |
| 590 | char pton[PTON_MAX]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 591 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 592 | assert(pai != NULL); |
| 593 | /* hostname may be NULL */ |
| 594 | /* servname may be NULL */ |
| 595 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 596 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 597 | *res = NULL; |
| 598 | sentinel.ai_next = NULL; |
| 599 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 600 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 601 | /* |
| 602 | * if the servname does not match socktype/protocol, ignore it. |
| 603 | */ |
| 604 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 605 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 606 | afd = find_afd(pai->ai_family); |
| 607 | if (afd == NULL) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 608 | |
Ken Chen | 15c805a | 2018-10-17 00:19:59 +0800 | [diff] [blame] | 609 | if (inet_pton(afd->a_af, hostname, pton) == 1) { |
| 610 | if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) { |
| 611 | GET_AI(cur->ai_next, afd, pton); |
| 612 | GET_PORT(cur->ai_next, servname); |
| 613 | if ((pai->ai_flags & AI_CANONNAME)) { |
| 614 | /* |
| 615 | * Set the numeric address itself as |
| 616 | * the canonical name, based on a |
| 617 | * clarification in rfc2553bis-03. |
| 618 | */ |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 619 | error = get_canonname(pai, cur->ai_next, canonname); |
| 620 | if (error != 0) { |
| 621 | freeaddrinfo(sentinel.ai_next); |
| 622 | return error; |
| 623 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 624 | } |
Ken Chen | 15c805a | 2018-10-17 00:19:59 +0800 | [diff] [blame] | 625 | while (cur->ai_next) cur = cur->ai_next; |
| 626 | } else |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 627 | return EAI_FAMILY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 628 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 629 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 630 | *res = sentinel.ai_next; |
| 631 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 632 | |
| 633 | free: |
Bernie Innocenti | b050f9b | 2018-10-02 12:53:39 +0900 | [diff] [blame] | 634 | freeaddrinfo(sentinel.ai_next); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 635 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
| 639 | * numeric hostname with scope |
| 640 | */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 641 | static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname, |
| 642 | const char* servname, struct addrinfo** res) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 643 | const struct afd* afd; |
| 644 | struct addrinfo* cur; |
| 645 | int error; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 646 | const char *cp, *scope, *addr; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 647 | struct sockaddr_in6* sin6; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 648 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 649 | assert(pai != NULL); |
| 650 | /* hostname may be NULL */ |
| 651 | /* servname may be NULL */ |
| 652 | assert(res != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 653 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 654 | /* |
| 655 | * if the servname does not match socktype/protocol, ignore it. |
| 656 | */ |
| 657 | if (get_portmatch(pai, servname) != 0) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 658 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 659 | afd = find_afd(pai->ai_family); |
| 660 | if (afd == NULL) return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 661 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 662 | if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 663 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 664 | cp = strchr(hostname, SCOPE_DELIMITER); |
| 665 | if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 666 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 667 | /* |
| 668 | * Handle special case of <scoped_address><delimiter><scope id> |
| 669 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 670 | char* hostname2 = strdup(hostname); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 671 | if (hostname2 == NULL) return EAI_MEMORY; |
| 672 | /* terminate at the delimiter */ |
| 673 | hostname2[cp - hostname] = '\0'; |
| 674 | addr = hostname2; |
| 675 | scope = cp + 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 676 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 677 | error = explore_numeric(pai, addr, servname, res, hostname); |
| 678 | if (error == 0) { |
| 679 | u_int32_t scopeid; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 680 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 681 | for (cur = *res; cur; cur = cur->ai_next) { |
| 682 | if (cur->ai_family != AF_INET6) continue; |
| 683 | sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr; |
| 684 | if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { |
| 685 | free(hostname2); |
| 686 | return (EAI_NODATA); /* XXX: is return OK? */ |
| 687 | } |
| 688 | sin6->sin6_scope_id = scopeid; |
| 689 | } |
| 690 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 691 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 692 | free(hostname2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 693 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 694 | return error; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 695 | } |
| 696 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 697 | static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) { |
| 698 | assert(pai != NULL); |
| 699 | assert(ai != NULL); |
| 700 | assert(str != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 701 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 702 | if ((pai->ai_flags & AI_CANONNAME) != 0) { |
| 703 | ai->ai_canonname = strdup(str); |
| 704 | if (ai->ai_canonname == NULL) return EAI_MEMORY; |
| 705 | } |
| 706 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 707 | } |
| 708 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 709 | static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd, |
| 710 | const char* addr) { |
| 711 | char* p; |
| 712 | struct addrinfo* ai; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 713 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 714 | assert(pai != NULL); |
| 715 | assert(afd != NULL); |
| 716 | assert(addr != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 717 | |
nuccachen | e21023a | 2018-09-11 11:13:44 +0800 | [diff] [blame] | 718 | ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 719 | if (ai == NULL) return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 720 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 721 | memcpy(ai, pai, sizeof(struct addrinfo)); |
| 722 | ai->ai_addr = (struct sockaddr*) (void*) (ai + 1); |
nuccachen | e21023a | 2018-09-11 11:13:44 +0800 | [diff] [blame] | 723 | memset(ai->ai_addr, 0, sizeof(sockaddr_union)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 724 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 725 | ai->ai_addrlen = afd->a_socklen; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 726 | ai->ai_addr->sa_family = ai->ai_family = afd->a_af; |
| 727 | p = (char*) (void*) (ai->ai_addr); |
| 728 | memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen); |
| 729 | return ai; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 730 | } |
| 731 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 732 | static int get_portmatch(const struct addrinfo* ai, const char* servname) { |
| 733 | assert(ai != NULL); |
| 734 | /* servname may be NULL */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 735 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 736 | return get_port(ai, servname, 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 737 | } |
| 738 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 739 | static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) { |
| 740 | const char* proto; |
| 741 | struct servent* sp; |
| 742 | int port; |
| 743 | int allownumeric; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 744 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 745 | assert(ai != NULL); |
| 746 | /* servname may be NULL */ |
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 | if (servname == NULL) return 0; |
| 749 | switch (ai->ai_family) { |
| 750 | case AF_INET: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 751 | case AF_INET6: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 752 | break; |
| 753 | default: |
| 754 | return 0; |
| 755 | } |
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 | switch (ai->ai_socktype) { |
| 758 | case SOCK_RAW: |
| 759 | return EAI_SERVICE; |
| 760 | case SOCK_DGRAM: |
| 761 | case SOCK_STREAM: |
| 762 | allownumeric = 1; |
| 763 | break; |
| 764 | case ANY: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 765 | allownumeric = 1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 766 | break; |
| 767 | default: |
| 768 | return EAI_SOCKTYPE; |
| 769 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 770 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 771 | port = str2number(servname); |
| 772 | if (port >= 0) { |
| 773 | if (!allownumeric) return EAI_SERVICE; |
| 774 | if (port < 0 || port > 65535) return EAI_SERVICE; |
| 775 | port = htons(port); |
| 776 | } else { |
| 777 | if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME; |
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 | switch (ai->ai_socktype) { |
| 780 | case SOCK_DGRAM: |
| 781 | proto = "udp"; |
| 782 | break; |
| 783 | case SOCK_STREAM: |
| 784 | proto = "tcp"; |
| 785 | break; |
| 786 | default: |
| 787 | proto = NULL; |
| 788 | break; |
| 789 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 790 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 791 | if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE; |
| 792 | port = sp->s_port; |
| 793 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 794 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 795 | if (!matchonly) { |
| 796 | switch (ai->ai_family) { |
| 797 | case AF_INET: |
| 798 | ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port; |
| 799 | break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 800 | case AF_INET6: |
| 801 | ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port; |
| 802 | break; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 803 | } |
| 804 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 805 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 806 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 807 | } |
| 808 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 809 | static const struct afd* find_afd(int af) { |
| 810 | const struct afd* afd; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 811 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 812 | if (af == PF_UNSPEC) return NULL; |
| 813 | for (afd = afdl; afd->a_af; afd++) { |
| 814 | if (afd->a_af == af) return afd; |
| 815 | } |
| 816 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 817 | } |
| 818 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 819 | // Convert a string to a scope identifier. |
| 820 | static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 821 | u_long lscopeid; |
| 822 | struct in6_addr* a6; |
| 823 | char* ep; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 824 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 825 | assert(scope != NULL); |
| 826 | assert(sin6 != NULL); |
| 827 | assert(scopeid != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 828 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 829 | a6 = &sin6->sin6_addr; |
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 | /* empty scopeid portion is invalid */ |
| 832 | if (*scope == '\0') return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 833 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 834 | if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { |
| 835 | /* |
| 836 | * We currently assume a one-to-one mapping between links |
| 837 | * and interfaces, so we simply use interface indices for |
| 838 | * like-local scopes. |
| 839 | */ |
| 840 | *scopeid = if_nametoindex(scope); |
| 841 | if (*scopeid == 0) goto trynumeric; |
| 842 | return 0; |
| 843 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 844 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 845 | /* still unclear about literal, allow numeric only - placeholder */ |
| 846 | if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric; |
| 847 | if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) |
| 848 | goto trynumeric; |
| 849 | else |
| 850 | goto trynumeric; /* global */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 851 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 852 | /* try to convert to a numeric id as a last resort */ |
| 853 | trynumeric: |
| 854 | errno = 0; |
| 855 | lscopeid = strtoul(scope, &ep, 10); |
| 856 | *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); |
| 857 | if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) |
| 858 | return 0; |
| 859 | else |
| 860 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 861 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 862 | |
| 863 | /* code duplicate with gethnamaddr.c */ |
| 864 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 865 | static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\""; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 866 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 867 | #define BOUNDED_INCR(x) \ |
| 868 | do { \ |
| 869 | BOUNDS_CHECK(cp, x); \ |
| 870 | cp += (x); \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 871 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 872 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 873 | #define BOUNDS_CHECK(ptr, count) \ |
| 874 | do { \ |
| 875 | if (eom - (ptr) < (count)) { \ |
| 876 | h_errno = NO_RECOVERY; \ |
| 877 | return NULL; \ |
| 878 | } \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 879 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 880 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 881 | static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype, |
| 882 | const struct addrinfo* pai) { |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 883 | struct addrinfo sentinel = {}; |
| 884 | struct addrinfo *cur; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 885 | struct addrinfo ai; |
| 886 | const struct afd* afd; |
| 887 | char* canonname; |
| 888 | const HEADER* hp; |
| 889 | const u_char* cp; |
| 890 | int n; |
| 891 | const u_char* eom; |
| 892 | char *bp, *ep; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 893 | int type, ancount, qdcount; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 894 | int haveanswer, had_error; |
| 895 | char tbuf[MAXDNAME]; |
| 896 | int (*name_ok)(const char*); |
| 897 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 898 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 899 | assert(answer != NULL); |
| 900 | assert(qname != NULL); |
| 901 | assert(pai != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 902 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 903 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 904 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 905 | canonname = NULL; |
| 906 | eom = answer->buf + anslen; |
| 907 | switch (qtype) { |
| 908 | case T_A: |
| 909 | case T_AAAA: |
| 910 | case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ |
| 911 | name_ok = res_hnok; |
| 912 | break; |
| 913 | default: |
| 914 | return NULL; /* XXX should be abort(); */ |
| 915 | } |
| 916 | /* |
| 917 | * find first satisfactory answer |
| 918 | */ |
| 919 | hp = &answer->hdr; |
| 920 | ancount = ntohs(hp->ancount); |
| 921 | qdcount = ntohs(hp->qdcount); |
| 922 | bp = hostbuf; |
| 923 | ep = hostbuf + sizeof hostbuf; |
| 924 | cp = answer->buf; |
| 925 | BOUNDED_INCR(HFIXEDSZ); |
| 926 | if (qdcount != 1) { |
| 927 | h_errno = NO_RECOVERY; |
| 928 | return (NULL); |
| 929 | } |
| 930 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 931 | if ((n < 0) || !(*name_ok)(bp)) { |
| 932 | h_errno = NO_RECOVERY; |
| 933 | return (NULL); |
| 934 | } |
| 935 | BOUNDED_INCR(n + QFIXEDSZ); |
| 936 | if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { |
| 937 | /* res_send() has already verified that the query name is the |
| 938 | * same as the one we sent; this just gets the expanded name |
| 939 | * (i.e., with the succeeding search-domain tacked on). |
| 940 | */ |
| 941 | n = strlen(bp) + 1; /* for the \0 */ |
| 942 | if (n >= MAXHOSTNAMELEN) { |
| 943 | h_errno = NO_RECOVERY; |
| 944 | return (NULL); |
| 945 | } |
| 946 | canonname = bp; |
| 947 | bp += n; |
| 948 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 949 | qname = canonname; |
| 950 | } |
| 951 | haveanswer = 0; |
| 952 | had_error = 0; |
| 953 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 954 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 955 | if ((n < 0) || !(*name_ok)(bp)) { |
| 956 | had_error++; |
| 957 | continue; |
| 958 | } |
| 959 | cp += n; /* name */ |
| 960 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 961 | type = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 962 | cp += INT16SZ; /* type */ |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 963 | int cl = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 964 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 965 | n = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 966 | cp += INT16SZ; /* len */ |
| 967 | BOUNDS_CHECK(cp, n); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 968 | if (cl != C_IN) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 969 | /* XXX - debug? syslog? */ |
| 970 | cp += n; |
| 971 | continue; /* XXX - had_error++ ? */ |
| 972 | } |
| 973 | if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) { |
| 974 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 975 | if ((n < 0) || !(*name_ok)(tbuf)) { |
| 976 | had_error++; |
| 977 | continue; |
| 978 | } |
| 979 | cp += n; |
| 980 | /* Get canonical name. */ |
| 981 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 982 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 983 | had_error++; |
| 984 | continue; |
| 985 | } |
| 986 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 987 | canonname = bp; |
| 988 | bp += n; |
| 989 | continue; |
| 990 | } |
| 991 | if (qtype == T_ANY) { |
| 992 | if (!(type == T_A || type == T_AAAA)) { |
| 993 | cp += n; |
| 994 | continue; |
| 995 | } |
| 996 | } else if (type != qtype) { |
| 997 | if (type != T_KEY && type != T_SIG) |
| 998 | syslog(LOG_NOTICE | LOG_AUTH, |
| 999 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname, |
| 1000 | p_class(C_IN), p_type(qtype), p_type(type)); |
| 1001 | cp += n; |
| 1002 | continue; /* XXX - had_error++ ? */ |
| 1003 | } |
| 1004 | switch (type) { |
| 1005 | case T_A: |
| 1006 | case T_AAAA: |
| 1007 | if (strcasecmp(canonname, bp) != 0) { |
| 1008 | syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp); |
| 1009 | cp += n; |
| 1010 | continue; /* XXX - had_error++ ? */ |
| 1011 | } |
| 1012 | if (type == T_A && n != INADDRSZ) { |
| 1013 | cp += n; |
| 1014 | continue; |
| 1015 | } |
| 1016 | if (type == T_AAAA && n != IN6ADDRSZ) { |
| 1017 | cp += n; |
| 1018 | continue; |
| 1019 | } |
| 1020 | if (type == T_AAAA) { |
| 1021 | struct in6_addr in6; |
| 1022 | memcpy(&in6, cp, IN6ADDRSZ); |
| 1023 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 1024 | cp += n; |
| 1025 | continue; |
| 1026 | } |
| 1027 | } |
| 1028 | if (!haveanswer) { |
| 1029 | int nn; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1030 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1031 | canonname = bp; |
| 1032 | nn = strlen(bp) + 1; /* for the \0 */ |
| 1033 | bp += nn; |
| 1034 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1035 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1036 | /* don't overwrite pai */ |
| 1037 | ai = *pai; |
| 1038 | ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; |
| 1039 | afd = find_afd(ai.ai_family); |
| 1040 | if (afd == NULL) { |
| 1041 | cp += n; |
| 1042 | continue; |
| 1043 | } |
| 1044 | cur->ai_next = get_ai(&ai, afd, (const char*) cp); |
| 1045 | if (cur->ai_next == NULL) had_error++; |
| 1046 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1047 | cp += n; |
| 1048 | break; |
| 1049 | default: |
| 1050 | abort(); |
| 1051 | } |
| 1052 | if (!had_error) haveanswer++; |
| 1053 | } |
| 1054 | if (haveanswer) { |
| 1055 | if (!canonname) |
| 1056 | (void) get_canonname(pai, sentinel.ai_next, qname); |
| 1057 | else |
| 1058 | (void) get_canonname(pai, sentinel.ai_next, canonname); |
| 1059 | h_errno = NETDB_SUCCESS; |
| 1060 | return sentinel.ai_next; |
| 1061 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1062 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1063 | h_errno = NO_RECOVERY; |
| 1064 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | struct addrinfo_sort_elem { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1068 | struct addrinfo* ai; |
| 1069 | int has_src_addr; |
| 1070 | sockaddr_union src_addr; |
| 1071 | int original_order; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1072 | }; |
| 1073 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1074 | static int _get_scope(const struct sockaddr* addr) { |
| 1075 | if (addr->sa_family == AF_INET6) { |
| 1076 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1077 | if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) { |
| 1078 | return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr); |
| 1079 | } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) || |
| 1080 | IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) { |
| 1081 | /* |
| 1082 | * RFC 4291 section 2.5.3 says loopback is to be treated as having |
| 1083 | * link-local scope. |
| 1084 | */ |
| 1085 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1086 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1087 | return IPV6_ADDR_SCOPE_SITELOCAL; |
| 1088 | } else { |
| 1089 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1090 | } |
| 1091 | } else if (addr->sa_family == AF_INET) { |
| 1092 | const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr; |
| 1093 | unsigned long int na = ntohl(addr4->sin_addr.s_addr); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1094 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1095 | if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */ |
| 1096 | (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */ |
| 1097 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1098 | } else { |
| 1099 | /* |
| 1100 | * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses |
| 1101 | * and shared addresses (100.64.0.0/10), are assigned global scope. |
| 1102 | */ |
| 1103 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1104 | } |
| 1105 | } else { |
| 1106 | /* |
| 1107 | * This should never happen. |
| 1108 | * Return a scope with low priority as a last resort. |
| 1109 | */ |
| 1110 | return IPV6_ADDR_SCOPE_NODELOCAL; |
| 1111 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /* These macros are modelled after the ones in <netinet/in6.h>. */ |
| 1115 | |
| 1116 | /* RFC 4380, section 2.6 */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1117 | #define IN6_IS_ADDR_TEREDO(a) \ |
| 1118 | ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000))) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1119 | |
| 1120 | /* RFC 3056, section 2. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1121 | #define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02)) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1122 | |
| 1123 | /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1124 | #define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe)) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1125 | |
| 1126 | /* |
| 1127 | * Get the label for a given IPv4/IPv6 address. |
| 1128 | * RFC 6724, section 2.1. |
| 1129 | */ |
| 1130 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1131 | static int _get_label(const struct sockaddr* addr) { |
| 1132 | if (addr->sa_family == AF_INET) { |
| 1133 | return 4; |
| 1134 | } else if (addr->sa_family == AF_INET6) { |
| 1135 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1136 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1137 | return 0; |
| 1138 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
| 1139 | return 4; |
| 1140 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1141 | return 2; |
| 1142 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1143 | return 5; |
| 1144 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1145 | return 13; |
| 1146 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) { |
| 1147 | return 3; |
| 1148 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1149 | return 11; |
| 1150 | } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1151 | return 12; |
| 1152 | } else { |
| 1153 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1154 | return 1; |
| 1155 | } |
| 1156 | } else { |
| 1157 | /* |
| 1158 | * This should never happen. |
| 1159 | * Return a semi-random label as a last resort. |
| 1160 | */ |
| 1161 | return 1; |
| 1162 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * Get the precedence for a given IPv4/IPv6 address. |
| 1167 | * RFC 6724, section 2.1. |
| 1168 | */ |
| 1169 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1170 | static int _get_precedence(const struct sockaddr* addr) { |
| 1171 | if (addr->sa_family == AF_INET) { |
| 1172 | return 35; |
| 1173 | } else if (addr->sa_family == AF_INET6) { |
| 1174 | const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr; |
| 1175 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1176 | return 50; |
| 1177 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
| 1178 | return 35; |
| 1179 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1180 | return 30; |
| 1181 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1182 | return 5; |
| 1183 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1184 | return 3; |
| 1185 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) || |
| 1186 | IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) || |
| 1187 | IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1188 | return 1; |
| 1189 | } else { |
| 1190 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1191 | return 40; |
| 1192 | } |
| 1193 | } else { |
| 1194 | return 1; |
| 1195 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * Find number of matching initial bits between the two addresses a1 and a2. |
| 1200 | */ |
| 1201 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1202 | static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) { |
| 1203 | const char* p1 = (const char*) a1; |
| 1204 | const char* p2 = (const char*) a2; |
| 1205 | unsigned i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1206 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1207 | for (i = 0; i < sizeof(*a1); ++i) { |
| 1208 | int x, j; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1209 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1210 | if (p1[i] == p2[i]) { |
| 1211 | continue; |
| 1212 | } |
| 1213 | x = p1[i] ^ p2[i]; |
| 1214 | for (j = 0; j < CHAR_BIT; ++j) { |
| 1215 | if (x & (1 << (CHAR_BIT - 1))) { |
| 1216 | return i * CHAR_BIT + j; |
| 1217 | } |
| 1218 | x <<= 1; |
| 1219 | } |
| 1220 | } |
| 1221 | return sizeof(*a1) * CHAR_BIT; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * Compare two source/destination address pairs. |
| 1226 | * RFC 6724, section 6. |
| 1227 | */ |
| 1228 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1229 | static int _rfc6724_compare(const void* ptr1, const void* ptr2) { |
| 1230 | const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1; |
| 1231 | const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2; |
| 1232 | int scope_src1, scope_dst1, scope_match1; |
| 1233 | int scope_src2, scope_dst2, scope_match2; |
| 1234 | int label_src1, label_dst1, label_match1; |
| 1235 | int label_src2, label_dst2, label_match2; |
| 1236 | int precedence1, precedence2; |
| 1237 | int prefixlen1, prefixlen2; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1238 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1239 | /* Rule 1: Avoid unusable destinations. */ |
| 1240 | if (a1->has_src_addr != a2->has_src_addr) { |
| 1241 | return a2->has_src_addr - a1->has_src_addr; |
| 1242 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1243 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1244 | /* Rule 2: Prefer matching scope. */ |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1245 | scope_src1 = _get_scope(&a1->src_addr.sa); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1246 | scope_dst1 = _get_scope(a1->ai->ai_addr); |
| 1247 | scope_match1 = (scope_src1 == scope_dst1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1248 | |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1249 | scope_src2 = _get_scope(&a2->src_addr.sa); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1250 | scope_dst2 = _get_scope(a2->ai->ai_addr); |
| 1251 | scope_match2 = (scope_src2 == scope_dst2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1252 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1253 | if (scope_match1 != scope_match2) { |
| 1254 | return scope_match2 - scope_match1; |
| 1255 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1256 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1257 | /* |
| 1258 | * Rule 3: Avoid deprecated addresses. |
| 1259 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1260 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1261 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1262 | /* |
| 1263 | * Rule 4: Prefer home addresses. |
| 1264 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1265 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1266 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1267 | /* Rule 5: Prefer matching label. */ |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1268 | label_src1 = _get_label(&a1->src_addr.sa); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1269 | label_dst1 = _get_label(a1->ai->ai_addr); |
| 1270 | label_match1 = (label_src1 == label_dst1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1271 | |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1272 | label_src2 = _get_label(&a2->src_addr.sa); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1273 | label_dst2 = _get_label(a2->ai->ai_addr); |
| 1274 | label_match2 = (label_src2 == label_dst2); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1275 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1276 | if (label_match1 != label_match2) { |
| 1277 | return label_match2 - label_match1; |
| 1278 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1279 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1280 | /* Rule 6: Prefer higher precedence. */ |
| 1281 | precedence1 = _get_precedence(a1->ai->ai_addr); |
| 1282 | precedence2 = _get_precedence(a2->ai->ai_addr); |
| 1283 | if (precedence1 != precedence2) { |
| 1284 | return precedence2 - precedence1; |
| 1285 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1286 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1287 | /* |
| 1288 | * Rule 7: Prefer native transport. |
| 1289 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1290 | */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1291 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1292 | /* Rule 8: Prefer smaller scope. */ |
| 1293 | if (scope_dst1 != scope_dst2) { |
| 1294 | return scope_dst1 - scope_dst2; |
| 1295 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1296 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1297 | /* |
| 1298 | * Rule 9: Use longest matching prefix. |
| 1299 | * We implement this for IPv6 only, as the rules in RFC 6724 don't seem |
| 1300 | * to work very well directly applied to IPv4. (glibc uses information from |
| 1301 | * the routing table for a custom IPv4 implementation here.) |
| 1302 | */ |
| 1303 | if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr && |
| 1304 | a2->ai->ai_addr->sa_family == AF_INET6) { |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1305 | const struct sockaddr_in6* a1_src = &a1->src_addr.sin6; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1306 | const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr; |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1307 | const struct sockaddr_in6* a2_src = &a2->src_addr.sin6; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1308 | const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr; |
| 1309 | prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr); |
| 1310 | prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr); |
| 1311 | if (prefixlen1 != prefixlen2) { |
| 1312 | return prefixlen2 - prefixlen1; |
| 1313 | } |
| 1314 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1315 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1316 | /* |
| 1317 | * Rule 10: Leave the order unchanged. |
| 1318 | * We need this since qsort() is not necessarily stable. |
| 1319 | */ |
| 1320 | return a1->original_order - a2->original_order; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * Find the source address that will be used if trying to connect to the given |
| 1325 | * address. src_addr must be large enough to hold a struct sockaddr_in6. |
| 1326 | * |
| 1327 | * Returns 1 if a source address was found, 0 if the address is unreachable, |
| 1328 | * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are |
| 1329 | * undefined. |
| 1330 | */ |
| 1331 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1332 | static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark, |
| 1333 | uid_t uid) { |
| 1334 | int sock; |
| 1335 | int ret; |
| 1336 | socklen_t len; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1337 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1338 | switch (addr->sa_family) { |
| 1339 | case AF_INET: |
| 1340 | len = sizeof(struct sockaddr_in); |
| 1341 | break; |
| 1342 | case AF_INET6: |
| 1343 | len = sizeof(struct sockaddr_in6); |
| 1344 | break; |
| 1345 | default: |
| 1346 | /* No known usable source address for non-INET families. */ |
| 1347 | return 0; |
| 1348 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1349 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1350 | sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); |
| 1351 | if (sock == -1) { |
| 1352 | if (errno == EAFNOSUPPORT) { |
| 1353 | return 0; |
| 1354 | } else { |
| 1355 | return -1; |
| 1356 | } |
| 1357 | } |
| 1358 | if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) { |
| 1359 | close(sock); |
| 1360 | return 0; |
| 1361 | } |
| 1362 | if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) { |
| 1363 | close(sock); |
| 1364 | return 0; |
| 1365 | } |
| 1366 | do { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 1367 | ret = connect(sock, addr, len); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1368 | } while (ret == -1 && errno == EINTR); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1369 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1370 | if (ret == -1) { |
| 1371 | close(sock); |
| 1372 | return 0; |
| 1373 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1374 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1375 | if (src_addr && getsockname(sock, src_addr, &len) == -1) { |
| 1376 | close(sock); |
| 1377 | return -1; |
| 1378 | } |
| 1379 | close(sock); |
| 1380 | return 1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Sort the linked list starting at sentinel->ai_next in RFC6724 order. |
| 1385 | * Will leave the list unchanged if an error occurs. |
| 1386 | */ |
| 1387 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1388 | static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) { |
| 1389 | struct addrinfo* cur; |
| 1390 | int nelem = 0, i; |
| 1391 | struct addrinfo_sort_elem* elems; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1392 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1393 | cur = list_sentinel->ai_next; |
| 1394 | while (cur) { |
| 1395 | ++nelem; |
| 1396 | cur = cur->ai_next; |
| 1397 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1398 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1399 | elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem)); |
| 1400 | if (elems == NULL) { |
| 1401 | goto error; |
| 1402 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1403 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1404 | /* |
| 1405 | * Convert the linked list to an array that also contains the candidate |
| 1406 | * source address for each destination address. |
| 1407 | */ |
| 1408 | for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) { |
| 1409 | int has_src_addr; |
| 1410 | assert(cur != NULL); |
| 1411 | elems[i].ai = cur; |
| 1412 | elems[i].original_order = i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1413 | |
nuccachen | e172a4e | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 1414 | has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1415 | if (has_src_addr == -1) { |
| 1416 | goto error; |
| 1417 | } |
| 1418 | elems[i].has_src_addr = has_src_addr; |
| 1419 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1420 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1421 | /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */ |
| 1422 | qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1423 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1424 | list_sentinel->ai_next = elems[0].ai; |
| 1425 | for (i = 0; i < nelem - 1; ++i) { |
| 1426 | elems[i].ai->ai_next = elems[i + 1].ai; |
| 1427 | } |
| 1428 | elems[nelem - 1].ai->ai_next = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1429 | |
| 1430 | error: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1431 | free(elems); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1432 | } |
| 1433 | |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1434 | static int dns_getaddrinfo(const char* name, const addrinfo* pai, |
| 1435 | const android_net_context* netcontext, addrinfo** rv) { |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 1436 | struct addrinfo *ai, *cur; |
| 1437 | struct addrinfo sentinel = {}; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1438 | struct res_target q, q2; |
| 1439 | res_state res; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1440 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1441 | memset(&q, 0, sizeof(q)); |
| 1442 | memset(&q2, 0, sizeof(q2)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1443 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1444 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1445 | querybuf* buf = (querybuf*) malloc(sizeof(*buf)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1446 | if (buf == NULL) { |
| 1447 | h_errno = NETDB_INTERNAL; |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1448 | return EAI_MEMORY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1449 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1450 | querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1451 | if (buf2 == NULL) { |
| 1452 | free(buf); |
| 1453 | h_errno = NETDB_INTERNAL; |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1454 | return EAI_MEMORY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1455 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1456 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1457 | switch (pai->ai_family) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1458 | case AF_UNSPEC: { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1459 | /* prefer IPv6 */ |
| 1460 | q.name = name; |
| 1461 | q.qclass = C_IN; |
| 1462 | q.answer = buf->buf; |
| 1463 | q.anslen = sizeof(buf->buf); |
| 1464 | int query_ipv6 = 1, query_ipv4 = 1; |
| 1465 | if (pai->ai_flags & AI_ADDRCONFIG) { |
| 1466 | query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid); |
| 1467 | query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid); |
| 1468 | } |
| 1469 | if (query_ipv6) { |
| 1470 | q.qtype = T_AAAA; |
| 1471 | if (query_ipv4) { |
| 1472 | q.next = &q2; |
| 1473 | q2.name = name; |
| 1474 | q2.qclass = C_IN; |
| 1475 | q2.qtype = T_A; |
| 1476 | q2.answer = buf2->buf; |
| 1477 | q2.anslen = sizeof(buf2->buf); |
| 1478 | } |
| 1479 | } else if (query_ipv4) { |
| 1480 | q.qtype = T_A; |
| 1481 | } else { |
| 1482 | free(buf); |
| 1483 | free(buf2); |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1484 | return EAI_NODATA; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1485 | } |
| 1486 | break; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1487 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1488 | case AF_INET: |
| 1489 | q.name = name; |
| 1490 | q.qclass = C_IN; |
| 1491 | q.qtype = T_A; |
| 1492 | q.answer = buf->buf; |
| 1493 | q.anslen = sizeof(buf->buf); |
| 1494 | break; |
| 1495 | case AF_INET6: |
| 1496 | q.name = name; |
| 1497 | q.qclass = C_IN; |
| 1498 | q.qtype = T_AAAA; |
| 1499 | q.answer = buf->buf; |
| 1500 | q.anslen = sizeof(buf->buf); |
| 1501 | break; |
| 1502 | default: |
| 1503 | free(buf); |
| 1504 | free(buf2); |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1505 | return EAI_FAMILY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1506 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1507 | |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 1508 | res = res_get_state(); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1509 | if (res == NULL) { |
| 1510 | free(buf); |
| 1511 | free(buf2); |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1512 | return EAI_MEMORY; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1513 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1514 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1515 | /* this just sets our netid val in the thread private data so we don't have to |
| 1516 | * modify the api's all the way down to res_send.c's res_nsend. We could |
| 1517 | * fully populate the thread private data here, but if we get down there |
| 1518 | * and have a cache hit that would be wasted, so we do the rest there on miss |
| 1519 | */ |
| 1520 | res_setnetcontext(res, netcontext); |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1521 | |
| 1522 | // Pass ai_error to catch more detailed errors rather than EAI_NODATA. |
| 1523 | int ai_error = EAI_NODATA; |
| 1524 | if (res_searchN(name, &q, res, &ai_error) < 0) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1525 | free(buf); |
| 1526 | free(buf2); |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1527 | return ai_error; // TODO: Decode error from h_errno like we do below |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1528 | } |
| 1529 | ai = getanswer(buf, q.n, q.name, q.qtype, pai); |
| 1530 | if (ai) { |
| 1531 | cur->ai_next = ai; |
| 1532 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1533 | } |
| 1534 | if (q.next) { |
| 1535 | ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); |
| 1536 | if (ai) cur->ai_next = ai; |
| 1537 | } |
| 1538 | free(buf); |
| 1539 | free(buf2); |
| 1540 | if (sentinel.ai_next == NULL) { |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1541 | return herrnoToAiError(h_errno); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1542 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1543 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1544 | _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1545 | |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1546 | *rv = sentinel.ai_next; |
| 1547 | return 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1548 | } |
| 1549 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1550 | static void _sethtent(FILE** hostf) { |
| 1551 | if (!*hostf) |
| 1552 | *hostf = fopen(_PATH_HOSTS, "re"); |
| 1553 | else |
| 1554 | rewind(*hostf); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1555 | } |
| 1556 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1557 | static void _endhtent(FILE** hostf) { |
| 1558 | if (*hostf) { |
| 1559 | (void) fclose(*hostf); |
| 1560 | *hostf = NULL; |
| 1561 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1562 | } |
| 1563 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1564 | static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) { |
| 1565 | char* p; |
| 1566 | char *cp, *tname, *cname; |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1567 | struct addrinfo *res0, *res; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1568 | int error; |
| 1569 | const char* addr; |
| 1570 | char hostbuf[8 * 1024]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1571 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1572 | assert(name != NULL); |
| 1573 | assert(pai != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1574 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1575 | if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL); |
| 1576 | again: |
| 1577 | if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL); |
| 1578 | if (*p == '#') goto again; |
| 1579 | if (!(cp = strpbrk(p, "#\n"))) goto again; |
| 1580 | *cp = '\0'; |
| 1581 | if (!(cp = strpbrk(p, " \t"))) goto again; |
| 1582 | *cp++ = '\0'; |
| 1583 | addr = p; |
| 1584 | /* if this is not something we're looking for, skip it. */ |
| 1585 | cname = NULL; |
| 1586 | while (cp && *cp) { |
| 1587 | if (*cp == ' ' || *cp == '\t') { |
| 1588 | cp++; |
| 1589 | continue; |
| 1590 | } |
| 1591 | if (!cname) cname = cp; |
| 1592 | tname = cp; |
| 1593 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
| 1594 | // fprintf(stderr, "\ttname = '%s'", tname); |
| 1595 | if (strcasecmp(name, tname) == 0) goto found; |
| 1596 | } |
| 1597 | goto again; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1598 | |
| 1599 | found: |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 1600 | error = getaddrinfo_numeric(addr, nullptr, *pai, &res0); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1601 | if (error) goto again; |
| 1602 | for (res = res0; res; res = res->ai_next) { |
| 1603 | /* cover it up */ |
| 1604 | res->ai_flags = pai->ai_flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1605 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1606 | if (pai->ai_flags & AI_CANONNAME) { |
| 1607 | if (get_canonname(pai, res, cname) != 0) { |
| 1608 | freeaddrinfo(res0); |
| 1609 | goto again; |
| 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | return res0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1614 | } |
| 1615 | |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1616 | static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) { |
Ken Chen | 3270cf5 | 2018-11-07 01:20:48 +0800 | [diff] [blame] | 1617 | struct addrinfo sentinel = {}; |
| 1618 | struct addrinfo *p, *cur; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1619 | FILE* hostf = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1620 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1621 | cur = &sentinel; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1622 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1623 | _sethtent(&hostf); |
| 1624 | while ((p = _gethtent(&hostf, name, pai)) != NULL) { |
| 1625 | cur->ai_next = p; |
| 1626 | while (cur && cur->ai_next) cur = cur->ai_next; |
| 1627 | } |
| 1628 | _endhtent(&hostf); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1629 | |
Bernie Innocenti | 948f657 | 2018-09-12 21:32:42 +0900 | [diff] [blame] | 1630 | *res = sentinel.ai_next; |
| 1631 | return sentinel.ai_next != NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /* resolver logic */ |
| 1635 | |
| 1636 | /* |
| 1637 | * Formulate a normal query, send, and await answer. |
| 1638 | * Returned answer is placed in supplied buffer "answer". |
| 1639 | * Perform preliminary check of answer, returning success only |
| 1640 | * if no error is indicated and the answer count is nonzero. |
| 1641 | * Return the size of the response on success, -1 on error. |
| 1642 | * Error number is left in h_errno. |
| 1643 | * |
| 1644 | * Caller must parse answer and determine whether it answers the question. |
| 1645 | */ |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1646 | static int res_queryN(const char* name, res_target* target, res_state res, int* ai_error) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1647 | u_char buf[MAXPACKET]; |
| 1648 | HEADER* hp; |
| 1649 | int n; |
| 1650 | struct res_target* t; |
| 1651 | int rcode; |
| 1652 | int ancount; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1653 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1654 | assert(name != NULL); |
| 1655 | /* XXX: target may be NULL??? */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1656 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1657 | rcode = NOERROR; |
| 1658 | ancount = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1659 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1660 | for (t = target; t; t = t->next) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1661 | u_char* answer; |
| 1662 | int anslen; |
| 1663 | u_int oflags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1664 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1665 | hp = (HEADER*) (void*) t->answer; |
| 1666 | oflags = res->_flags; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1667 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1668 | again: |
| 1669 | hp->rcode = NOERROR; /* default */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1670 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1671 | /* make it easier... */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1672 | int cl = t->qclass; |
| 1673 | int type = t->qtype; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1674 | answer = t->answer; |
| 1675 | anslen = t->anslen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1676 | #ifdef DEBUG |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1677 | if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1678 | #endif |
| 1679 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 1680 | n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1681 | if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 && |
| 1682 | (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0) |
| 1683 | n = res_nopt(res, n, buf, sizeof(buf), anslen); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1684 | if (n <= 0) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1685 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1686 | if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1687 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1688 | h_errno = NO_RECOVERY; |
| 1689 | return n; |
| 1690 | } |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1691 | |
| 1692 | n = res_nsend(res, buf, n, answer, anslen, &rcode); |
| 1693 | *ai_error = rcodeToAiError(rcode); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1694 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1695 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
| 1696 | rcode = hp->rcode; /* record most recent error */ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1697 | /* if the query choked with EDNS0, retry without EDNS0 */ |
| 1698 | if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && |
| 1699 | ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) { |
| 1700 | res->_flags |= RES_F_EDNS0ERR; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1701 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1702 | if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1703 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1704 | goto again; |
| 1705 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1706 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1707 | if (res->options & RES_DEBUG) |
| 1708 | printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1709 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1710 | continue; |
| 1711 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1712 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1713 | ancount += ntohs(hp->ancount); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1714 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1715 | t->n = n; |
| 1716 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1717 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1718 | if (ancount == 0) { |
| 1719 | switch (rcode) { |
| 1720 | case NXDOMAIN: |
| 1721 | h_errno = HOST_NOT_FOUND; |
| 1722 | break; |
| 1723 | case SERVFAIL: |
| 1724 | h_errno = TRY_AGAIN; |
| 1725 | break; |
| 1726 | case NOERROR: |
| 1727 | h_errno = NO_DATA; |
| 1728 | break; |
| 1729 | case FORMERR: |
| 1730 | case NOTIMP: |
| 1731 | case REFUSED: |
| 1732 | default: |
| 1733 | h_errno = NO_RECOVERY; |
| 1734 | break; |
| 1735 | } |
| 1736 | return -1; |
| 1737 | } |
| 1738 | return ancount; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
| 1743 | * Return the size of the response on success, -1 on error. |
| 1744 | * If enabled, implement search rules until answer or unrecoverable failure |
| 1745 | * is detected. Error code, if any, is left in h_errno. |
| 1746 | */ |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1747 | static int res_searchN(const char* name, res_target* target, res_state res, int* ai_error) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1748 | const char *cp, *const *domain; |
| 1749 | HEADER* hp; |
| 1750 | u_int dots; |
| 1751 | int trailing_dot, ret, saved_herrno; |
| 1752 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1753 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1754 | assert(name != NULL); |
| 1755 | assert(target != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1756 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1757 | hp = (HEADER*) (void*) target->answer; /*XXX*/ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1758 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1759 | errno = 0; |
| 1760 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ |
| 1761 | dots = 0; |
| 1762 | for (cp = name; *cp; cp++) dots += (*cp == '.'); |
| 1763 | trailing_dot = 0; |
| 1764 | if (cp > name && *--cp == '.') trailing_dot++; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1765 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1766 | /* |
| 1767 | * If there are dots in the name already, let's just give it a try |
| 1768 | * 'as is'. The threshold can be set with the "ndots" option. |
| 1769 | */ |
| 1770 | saved_herrno = -1; |
| 1771 | if (dots >= res->ndots) { |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1772 | ret = res_querydomainN(name, NULL, target, res, ai_error); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1773 | if (ret > 0) return (ret); |
| 1774 | saved_herrno = h_errno; |
| 1775 | tried_as_is++; |
| 1776 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1777 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1778 | /* |
| 1779 | * We do at least one level of search if |
| 1780 | * - there is no dot and RES_DEFNAME is set, or |
| 1781 | * - there is at least one dot, there is no trailing dot, |
| 1782 | * and RES_DNSRCH is set. |
| 1783 | */ |
| 1784 | if ((!dots && (res->options & RES_DEFNAMES)) || |
| 1785 | (dots && !trailing_dot && (res->options & RES_DNSRCH))) { |
| 1786 | int done = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1787 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1788 | /* Unfortunately we need to set stuff up before |
| 1789 | * the domain stuff is tried. Will have a better |
| 1790 | * fix after thread pools are used. |
| 1791 | */ |
| 1792 | _resolv_populate_res_for_net(res); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1793 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1794 | for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) { |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1795 | ret = res_querydomainN(name, *domain, target, res, ai_error); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1796 | if (ret > 0) return ret; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1797 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1798 | /* |
| 1799 | * If no server present, give up. |
| 1800 | * If name isn't found in this domain, |
| 1801 | * keep trying higher domains in the search list |
| 1802 | * (if that's enabled). |
| 1803 | * On a NO_DATA error, keep trying, otherwise |
| 1804 | * a wildcard entry of another type could keep us |
| 1805 | * from finding this entry higher in the domain. |
| 1806 | * If we get some other error (negative answer or |
| 1807 | * server failure), then stop searching up, |
| 1808 | * but try the input name below in case it's |
| 1809 | * fully-qualified. |
| 1810 | */ |
| 1811 | if (errno == ECONNREFUSED) { |
| 1812 | h_errno = TRY_AGAIN; |
| 1813 | return -1; |
| 1814 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1815 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1816 | switch (h_errno) { |
| 1817 | case NO_DATA: |
| 1818 | got_nodata++; |
Bernie Innocenti | 8bb94ba | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 1819 | [[fallthrough]]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1820 | case HOST_NOT_FOUND: |
| 1821 | /* keep trying */ |
| 1822 | break; |
| 1823 | case TRY_AGAIN: |
| 1824 | if (hp->rcode == SERVFAIL) { |
| 1825 | /* try next search element, if any */ |
| 1826 | got_servfail++; |
| 1827 | break; |
| 1828 | } |
Bernie Innocenti | 8bb94ba | 2018-10-10 22:30:12 +0900 | [diff] [blame] | 1829 | [[fallthrough]]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1830 | default: |
| 1831 | /* anything else implies that we're done */ |
| 1832 | done++; |
| 1833 | } |
| 1834 | /* |
| 1835 | * if we got here for some reason other than DNSRCH, |
| 1836 | * we only wanted one iteration of the loop, so stop. |
| 1837 | */ |
| 1838 | if (!(res->options & RES_DNSRCH)) done++; |
| 1839 | } |
| 1840 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1841 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1842 | /* |
| 1843 | * if we have not already tried the name "as is", do that now. |
| 1844 | * note that we do this regardless of how many dots were in the |
| 1845 | * name or whether it ends with a dot. |
| 1846 | */ |
| 1847 | if (!tried_as_is) { |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1848 | ret = res_querydomainN(name, NULL, target, res, ai_error); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1849 | if (ret > 0) return ret; |
| 1850 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1851 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1852 | /* |
| 1853 | * if we got here, we didn't satisfy the search. |
| 1854 | * if we did an initial full query, return that query's h_errno |
| 1855 | * (note that we wouldn't be here if that query had succeeded). |
| 1856 | * else if we ever got a nodata, send that back as the reason. |
| 1857 | * else send back meaningless h_errno, that being the one from |
| 1858 | * the last DNSRCH we did. |
| 1859 | */ |
| 1860 | if (saved_herrno != -1) |
| 1861 | h_errno = saved_herrno; |
| 1862 | else if (got_nodata) |
| 1863 | h_errno = NO_DATA; |
| 1864 | else if (got_servfail) |
| 1865 | h_errno = TRY_AGAIN; |
| 1866 | return -1; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * Perform a call on res_query on the concatenation of name and domain, |
| 1871 | * removing a trailing dot from name if domain is NULL. |
| 1872 | */ |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1873 | static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res, |
| 1874 | int* ai_error) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1875 | char nbuf[MAXDNAME]; |
| 1876 | const char* longname = nbuf; |
| 1877 | size_t n, d; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1878 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1879 | assert(name != NULL); |
| 1880 | /* XXX: target may be NULL??? */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1881 | |
| 1882 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1883 | if (res->options & RES_DEBUG) |
| 1884 | printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1885 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1886 | if (domain == NULL) { |
| 1887 | /* |
| 1888 | * Check for trailing '.'; |
| 1889 | * copy without '.' if present. |
| 1890 | */ |
| 1891 | n = strlen(name); |
| 1892 | if (n + 1 > sizeof(nbuf)) { |
| 1893 | h_errno = NO_RECOVERY; |
| 1894 | return -1; |
| 1895 | } |
| 1896 | if (n > 0 && name[--n] == '.') { |
| 1897 | strncpy(nbuf, name, n); |
| 1898 | nbuf[n] = '\0'; |
| 1899 | } else |
| 1900 | longname = name; |
| 1901 | } else { |
| 1902 | n = strlen(name); |
| 1903 | d = strlen(domain); |
| 1904 | if (n + 1 + d + 1 > sizeof(nbuf)) { |
| 1905 | h_errno = NO_RECOVERY; |
| 1906 | return -1; |
| 1907 | } |
| 1908 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); |
| 1909 | } |
Mike Yu | 69615f6 | 2018-11-06 15:42:36 +0800 | [diff] [blame^] | 1910 | return res_queryN(longname, target, res, ai_error); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1911 | } |