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