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