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