The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */ |
| 2 | /* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the project nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Issues to be discussed: |
| 35 | * - Thread safe-ness must be checked. |
| 36 | * - Return values. There are nonstandard return values defined and used |
| 37 | * in the source code. This is because RFC2553 is silent about which error |
| 38 | * code must be returned for which situation. |
| 39 | * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2 |
| 40 | * says to use inet_aton() to convert IPv4 numeric to binary (alows |
| 41 | * classful form as a result). |
| 42 | * current code - disallow classful form for IPv4 (due to use of inet_pton). |
| 43 | * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is |
| 44 | * invalid. |
| 45 | * current code - SEGV on freeaddrinfo(NULL) |
| 46 | * Note: |
| 47 | * - We use getipnodebyname() just for thread-safeness. There's no intent |
| 48 | * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to |
| 49 | * getipnodebyname(). |
| 50 | * - The code filters out AFs that are not supported by the kernel, |
| 51 | * when globbing NULL hostname (to loopback, or wildcard). Is it the right |
| 52 | * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG |
| 53 | * in ai_flags? |
| 54 | * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. |
| 55 | * (1) what should we do against numeric hostname (2) what should we do |
| 56 | * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? |
| 57 | * non-loopback address configured? global address configured? |
| 58 | * - To avoid search order issue, we have a big amount of code duplicate |
| 59 | * from gethnamaddr.c and some other places. The issues that there's no |
| 60 | * lower layer function to lookup "IPv4 or IPv6" record. Calling |
| 61 | * gethostbyname2 from getaddrinfo will end up in wrong search order, as |
| 62 | * follows: |
| 63 | * - The code makes use of following calls when asked to resolver with |
| 64 | * ai_family = PF_UNSPEC: |
| 65 | * getipnodebyname(host, AF_INET6); |
| 66 | * getipnodebyname(host, AF_INET); |
| 67 | * This will result in the following queries if the node is configure to |
| 68 | * prefer /etc/hosts than DNS: |
| 69 | * lookup /etc/hosts for IPv6 address |
| 70 | * lookup DNS for IPv6 address |
| 71 | * lookup /etc/hosts for IPv4 address |
| 72 | * lookup DNS for IPv4 address |
| 73 | * which may not meet people's requirement. |
| 74 | * The right thing to happen is to have underlying layer which does |
| 75 | * PF_UNSPEC lookup (lookup both) and return chain of addrinfos. |
| 76 | * This would result in a bit of code duplicate with _dns_ghbyname() and |
| 77 | * friends. |
| 78 | */ |
| 79 | |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 80 | #include <fcntl.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 81 | #include <sys/cdefs.h> |
| 82 | #include <sys/types.h> |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 83 | #include <sys/stat.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | #include <sys/param.h> |
| 85 | #include <sys/socket.h> |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 86 | #include <sys/un.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 87 | #include <net/if.h> |
| 88 | #include <netinet/in.h> |
| 89 | #include <arpa/inet.h> |
Calin Juravle | 569fb98 | 2014-03-04 15:01:29 +0000 | [diff] [blame] | 90 | #include <arpa/nameser.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 91 | #include <assert.h> |
| 92 | #include <ctype.h> |
| 93 | #include <errno.h> |
| 94 | #include <netdb.h> |
Sreeram Ramachandran | 57a2627 | 2014-05-19 10:21:39 -0700 | [diff] [blame] | 95 | #include "NetdClientDispatch.h" |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 96 | #include "resolv_cache.h" |
| 97 | #include "resolv_netid.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | #include "resolv_private.h" |
Robert Greenwalt | 1d8d9a3 | 2013-08-02 15:24:45 -0700 | [diff] [blame] | 99 | #include <stdbool.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 | #include <stddef.h> |
| 101 | #include <stdio.h> |
| 102 | #include <stdlib.h> |
| 103 | #include <string.h> |
Carl Shapiro | 2cc2b2b | 2011-03-21 20:01:03 -0700 | [diff] [blame] | 104 | #include <strings.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 105 | #include <unistd.h> |
| 106 | |
| 107 | #include <syslog.h> |
| 108 | #include <stdarg.h> |
| 109 | #include "nsswitch.h" |
| 110 | |
Brad Fitzpatrick | 7858564 | 2010-10-28 13:22:20 -0700 | [diff] [blame] | 111 | #ifdef ANDROID_CHANGES |
| 112 | #include <sys/system_properties.h> |
| 113 | #endif /* ANDROID_CHANGES */ |
| 114 | |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 115 | typedef union sockaddr_union { |
| 116 | struct sockaddr generic; |
| 117 | struct sockaddr_in in; |
| 118 | struct sockaddr_in6 in6; |
| 119 | } sockaddr_union; |
| 120 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 121 | #define SUCCESS 0 |
| 122 | #define ANY 0 |
| 123 | #define YES 1 |
| 124 | #define NO 0 |
| 125 | |
| 126 | static const char in_addrany[] = { 0, 0, 0, 0 }; |
| 127 | static const char in_loopback[] = { 127, 0, 0, 1 }; |
| 128 | #ifdef INET6 |
| 129 | static const char in6_addrany[] = { |
| 130 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 131 | }; |
| 132 | static const char in6_loopback[] = { |
| 133 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 |
| 134 | }; |
| 135 | #endif |
| 136 | |
Selim Gurun | 06e1831 | 2012-02-27 15:58:54 -0800 | [diff] [blame] | 137 | // This should be synchronized to ResponseCode.h |
| 138 | static const int DnsProxyQueryResult = 222; |
| 139 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 140 | static const struct afd { |
| 141 | int a_af; |
| 142 | int a_addrlen; |
| 143 | int a_socklen; |
| 144 | int a_off; |
| 145 | const char *a_addrany; |
| 146 | const char *a_loopback; |
| 147 | int a_scoped; |
| 148 | } afdl [] = { |
| 149 | #ifdef INET6 |
| 150 | {PF_INET6, sizeof(struct in6_addr), |
| 151 | sizeof(struct sockaddr_in6), |
| 152 | offsetof(struct sockaddr_in6, sin6_addr), |
| 153 | in6_addrany, in6_loopback, 1}, |
| 154 | #endif |
| 155 | {PF_INET, sizeof(struct in_addr), |
| 156 | sizeof(struct sockaddr_in), |
| 157 | offsetof(struct sockaddr_in, sin_addr), |
| 158 | in_addrany, in_loopback, 0}, |
| 159 | {0, 0, 0, 0, NULL, NULL, 0}, |
| 160 | }; |
| 161 | |
| 162 | struct explore { |
| 163 | int e_af; |
| 164 | int e_socktype; |
| 165 | int e_protocol; |
| 166 | const char *e_protostr; |
| 167 | int e_wild; |
| 168 | #define WILD_AF(ex) ((ex)->e_wild & 0x01) |
| 169 | #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) |
| 170 | #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) |
| 171 | }; |
| 172 | |
| 173 | static const struct explore explore[] = { |
| 174 | #if 0 |
| 175 | { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, |
| 176 | #endif |
| 177 | #ifdef INET6 |
| 178 | { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, |
| 179 | { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, |
| 180 | { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, |
| 181 | #endif |
| 182 | { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, |
| 183 | { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, |
| 184 | { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, |
| 185 | { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, |
| 186 | { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, |
| 187 | { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, |
| 188 | { -1, 0, 0, NULL, 0 }, |
| 189 | }; |
| 190 | |
| 191 | #ifdef INET6 |
| 192 | #define PTON_MAX 16 |
| 193 | #else |
| 194 | #define PTON_MAX 4 |
| 195 | #endif |
| 196 | |
| 197 | static const ns_src default_dns_files[] = { |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 198 | { NSSRC_FILES, NS_SUCCESS }, |
| 199 | { NSSRC_DNS, NS_SUCCESS }, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 200 | { 0, 0 } |
| 201 | }; |
| 202 | |
| 203 | #define MAXPACKET (64*1024) |
| 204 | |
| 205 | typedef union { |
| 206 | HEADER hdr; |
| 207 | u_char buf[MAXPACKET]; |
| 208 | } querybuf; |
| 209 | |
| 210 | struct res_target { |
| 211 | struct res_target *next; |
| 212 | const char *name; /* domain name */ |
| 213 | int qclass, qtype; /* class and type of query */ |
| 214 | u_char *answer; /* buffer to put answer */ |
| 215 | int anslen; /* size of answer buffer */ |
| 216 | int n; /* result length */ |
| 217 | }; |
| 218 | |
| 219 | static int str2number(const char *); |
| 220 | static int explore_fqdn(const struct addrinfo *, const char *, |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 221 | const char *, struct addrinfo **, unsigned netid, unsigned mark); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 222 | static int explore_null(const struct addrinfo *, |
| 223 | const char *, struct addrinfo **); |
| 224 | static int explore_numeric(const struct addrinfo *, const char *, |
| 225 | const char *, struct addrinfo **, const char *); |
| 226 | static int explore_numeric_scope(const struct addrinfo *, const char *, |
| 227 | const char *, struct addrinfo **); |
| 228 | static int get_canonname(const struct addrinfo *, |
| 229 | struct addrinfo *, const char *); |
| 230 | static struct addrinfo *get_ai(const struct addrinfo *, |
| 231 | const struct afd *, const char *); |
| 232 | static int get_portmatch(const struct addrinfo *, const char *); |
| 233 | static int get_port(const struct addrinfo *, const char *, int); |
| 234 | static const struct afd *find_afd(int); |
| 235 | #ifdef INET6 |
| 236 | static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); |
| 237 | #endif |
| 238 | |
| 239 | static struct addrinfo *getanswer(const querybuf *, int, const char *, int, |
| 240 | const struct addrinfo *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 241 | static int _dns_getaddrinfo(void *, void *, va_list); |
| 242 | static void _sethtent(FILE **); |
| 243 | static void _endhtent(FILE **); |
| 244 | static struct addrinfo *_gethtent(FILE **, const char *, |
| 245 | const struct addrinfo *); |
| 246 | static int _files_getaddrinfo(void *, void *, va_list); |
| 247 | |
| 248 | static int res_queryN(const char *, struct res_target *, res_state); |
| 249 | static int res_searchN(const char *, struct res_target *, res_state); |
| 250 | static int res_querydomainN(const char *, const char *, |
| 251 | struct res_target *, res_state); |
| 252 | |
| 253 | static const char * const ai_errlist[] = { |
| 254 | "Success", |
| 255 | "Address family for hostname not supported", /* EAI_ADDRFAMILY */ |
| 256 | "Temporary failure in name resolution", /* EAI_AGAIN */ |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 257 | "Invalid value for ai_flags", /* EAI_BADFLAGS */ |
| 258 | "Non-recoverable failure in name resolution", /* EAI_FAIL */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 259 | "ai_family not supported", /* EAI_FAMILY */ |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 260 | "Memory allocation failure", /* EAI_MEMORY */ |
| 261 | "No address associated with hostname", /* EAI_NODATA */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 262 | "hostname nor servname provided, or not known", /* EAI_NONAME */ |
| 263 | "servname not supported for ai_socktype", /* EAI_SERVICE */ |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 264 | "ai_socktype not supported", /* EAI_SOCKTYPE */ |
| 265 | "System error returned in errno", /* EAI_SYSTEM */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 266 | "Invalid value for hints", /* EAI_BADHINTS */ |
| 267 | "Resolved protocol is unknown", /* EAI_PROTOCOL */ |
| 268 | "Argument buffer overflow", /* EAI_OVERFLOW */ |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 269 | "Unknown error", /* EAI_MAX */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | /* XXX macros that make external reference is BAD. */ |
| 273 | |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 274 | #define GET_AI(ai, afd, addr) \ |
| 275 | do { \ |
| 276 | /* external reference: pai, error, and label free */ \ |
| 277 | (ai) = get_ai(pai, (afd), (addr)); \ |
| 278 | if ((ai) == NULL) { \ |
| 279 | error = EAI_MEMORY; \ |
| 280 | goto free; \ |
| 281 | } \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 282 | } while (/*CONSTCOND*/0) |
| 283 | |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 284 | #define GET_PORT(ai, serv) \ |
| 285 | do { \ |
| 286 | /* external reference: error and label free */ \ |
| 287 | error = get_port((ai), (serv), 0); \ |
| 288 | if (error != 0) \ |
| 289 | goto free; \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 290 | } while (/*CONSTCOND*/0) |
| 291 | |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 292 | #define GET_CANONNAME(ai, str) \ |
| 293 | do { \ |
| 294 | /* external reference: pai, error and label free */ \ |
| 295 | error = get_canonname(pai, (ai), (str)); \ |
| 296 | if (error != 0) \ |
| 297 | goto free; \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 298 | } while (/*CONSTCOND*/0) |
| 299 | |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 300 | #define ERR(err) \ |
| 301 | do { \ |
| 302 | /* external reference: error, and label bad */ \ |
| 303 | error = (err); \ |
| 304 | goto bad; \ |
| 305 | /*NOTREACHED*/ \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 306 | } while (/*CONSTCOND*/0) |
| 307 | |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 308 | #define MATCH_FAMILY(x, y, w) \ |
| 309 | ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 310 | (y) == PF_UNSPEC))) |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 311 | #define MATCH(x, y, w) \ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 312 | ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) |
| 313 | |
| 314 | const char * |
| 315 | gai_strerror(int ecode) |
| 316 | { |
| 317 | if (ecode < 0 || ecode > EAI_MAX) |
| 318 | ecode = EAI_MAX; |
| 319 | return ai_errlist[ecode]; |
| 320 | } |
| 321 | |
| 322 | void |
| 323 | freeaddrinfo(struct addrinfo *ai) |
| 324 | { |
| 325 | struct addrinfo *next; |
| 326 | |
Elliott Hughes | c62a4b5 | 2015-01-08 17:28:46 -0800 | [diff] [blame] | 327 | #if __ANDROID__ |
| 328 | if (ai == NULL) return; |
| 329 | #else |
| 330 | _DIAGASSERT(ai != NULL); |
| 331 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 332 | |
| 333 | do { |
| 334 | next = ai->ai_next; |
| 335 | if (ai->ai_canonname) |
| 336 | free(ai->ai_canonname); |
| 337 | /* no need to free(ai->ai_addr) */ |
| 338 | free(ai); |
| 339 | ai = next; |
| 340 | } while (ai); |
| 341 | } |
| 342 | |
| 343 | static int |
| 344 | str2number(const char *p) |
| 345 | { |
| 346 | char *ep; |
| 347 | unsigned long v; |
| 348 | |
| 349 | assert(p != NULL); |
| 350 | |
| 351 | if (*p == '\0') |
| 352 | return -1; |
| 353 | ep = NULL; |
| 354 | errno = 0; |
| 355 | v = strtoul(p, &ep, 10); |
| 356 | if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) |
| 357 | return v; |
| 358 | else |
| 359 | return -1; |
| 360 | } |
| 361 | |
Lorenzo Colitti | ba96e30 | 2011-01-14 12:26:05 -0800 | [diff] [blame] | 362 | /* |
| 363 | * Connect a UDP socket to a given unicast address. This will cause no network |
| 364 | * traffic, but will fail fast if the system has no or limited reachability to |
| 365 | * the destination (e.g., no IPv4 address, no IPv6 default route, ...). |
| 366 | */ |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 367 | static int |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 368 | _test_connect(int pf, struct sockaddr *addr, size_t addrlen, unsigned mark) { |
Nick Kralevich | 1781ed7 | 2014-06-29 20:46:17 -0700 | [diff] [blame] | 369 | int s = socket(pf, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 370 | if (s < 0) |
| 371 | return 0; |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 372 | if (mark != MARK_UNSET && setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) |
| 373 | return 0; |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 374 | int ret; |
| 375 | do { |
Paul Jensen | 31ad037 | 2014-05-29 16:28:30 -0400 | [diff] [blame] | 376 | ret = __connect(s, addr, addrlen); |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 377 | } while (ret < 0 && errno == EINTR); |
Lorenzo Colitti | ba96e30 | 2011-01-14 12:26:05 -0800 | [diff] [blame] | 378 | int success = (ret == 0); |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 379 | do { |
| 380 | ret = close(s); |
| 381 | } while (ret < 0 && errno == EINTR); |
Lorenzo Colitti | ba96e30 | 2011-01-14 12:26:05 -0800 | [diff] [blame] | 382 | return success; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * The following functions determine whether IPv4 or IPv6 connectivity is |
| 387 | * available in order to implement AI_ADDRCONFIG. |
| 388 | * |
| 389 | * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is |
| 390 | * available, but whether addresses of the specified family are "configured |
| 391 | * on the local system". However, bionic doesn't currently support getifaddrs, |
| 392 | * so checking for connectivity is the next best thing. |
| 393 | */ |
| 394 | static int |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 395 | _have_ipv6(unsigned mark) { |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 396 | static const struct sockaddr_in6 sin6_test = { |
| 397 | .sin6_family = AF_INET6, |
| 398 | .sin6_addr.s6_addr = { // 2000:: |
| 399 | 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
| 400 | }; |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 401 | sockaddr_union addr = { .in6 = sin6_test }; |
| 402 | return _test_connect(PF_INET6, &addr.generic, sizeof(addr.in6), mark); |
Lorenzo Colitti | ba96e30 | 2011-01-14 12:26:05 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static int |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 406 | _have_ipv4(unsigned mark) { |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 407 | static const struct sockaddr_in sin_test = { |
| 408 | .sin_family = AF_INET, |
| 409 | .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8 |
| 410 | }; |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 411 | sockaddr_union addr = { .in = sin_test }; |
| 412 | return _test_connect(PF_INET, &addr.generic, sizeof(addr.in), mark); |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 415 | bool readBE32(FILE* fp, int32_t* result) { |
| 416 | int32_t tmp; |
| 417 | if (fread(&tmp, sizeof(tmp), 1, fp) != 1) { |
| 418 | return false; |
| 419 | } |
| 420 | *result = ntohl(tmp); |
| 421 | return true; |
| 422 | } |
| 423 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 424 | // Returns 0 on success, else returns on error. |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 425 | static int |
| 426 | android_getaddrinfo_proxy( |
| 427 | const char *hostname, const char *servname, |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 428 | const struct addrinfo *hints, struct addrinfo **res, unsigned netid) |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 429 | { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 430 | int success = 0; |
| 431 | |
| 432 | // Clear this at start, as we use its non-NULLness later (in the |
| 433 | // error path) to decide if we have to free up any memory we |
| 434 | // allocated in the process (before failing). |
| 435 | *res = NULL; |
| 436 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 437 | // Bogus things we can't serialize. Don't use the proxy. These will fail - let them. |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 438 | if ((hostname != NULL && |
| 439 | strcspn(hostname, " \n\r\t^'\"") != strlen(hostname)) || |
| 440 | (servname != NULL && |
| 441 | strcspn(servname, " \n\r\t^'\"") != strlen(servname))) { |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 442 | return EAI_NODATA; |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 445 | FILE* proxy = android_open_proxy(); |
| 446 | if (proxy == NULL) { |
| 447 | return EAI_SYSTEM; |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Paul Jensen | 559c784 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 450 | netid = __netdClientDispatch.netIdForResolv(netid); |
| 451 | |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 452 | // Send the request. |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 453 | if (fprintf(proxy, "getaddrinfo %s %s %d %d %d %d %u", |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 454 | hostname == NULL ? "^" : hostname, |
| 455 | servname == NULL ? "^" : servname, |
| 456 | hints == NULL ? -1 : hints->ai_flags, |
| 457 | hints == NULL ? -1 : hints->ai_family, |
| 458 | hints == NULL ? -1 : hints->ai_socktype, |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 459 | hints == NULL ? -1 : hints->ai_protocol, |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 460 | netid) < 0) { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 461 | goto exit; |
| 462 | } |
| 463 | // literal NULL byte at end, required by FrameworkListener |
| 464 | if (fputc(0, proxy) == EOF || |
| 465 | fflush(proxy) != 0) { |
| 466 | goto exit; |
| 467 | } |
| 468 | |
Robert Greenwalt | c59ba45 | 2012-03-09 11:34:27 -0800 | [diff] [blame] | 469 | char buf[4]; |
Selim Gurun | 06e1831 | 2012-02-27 15:58:54 -0800 | [diff] [blame] | 470 | // read result code for gethostbyaddr |
| 471 | if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 472 | goto exit; |
| 473 | } |
| 474 | |
Selim Gurun | 06e1831 | 2012-02-27 15:58:54 -0800 | [diff] [blame] | 475 | int result_code = (int)strtol(buf, NULL, 10); |
| 476 | // verify the code itself |
| 477 | if (result_code != DnsProxyQueryResult ) { |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 478 | fread(buf, 1, sizeof(buf), proxy); |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 479 | goto exit; |
| 480 | } |
| 481 | |
| 482 | struct addrinfo* ai = NULL; |
| 483 | struct addrinfo** nextres = res; |
| 484 | while (1) { |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 485 | int32_t have_more; |
| 486 | if (!readBE32(proxy, &have_more)) { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 487 | break; |
| 488 | } |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 489 | if (have_more == 0) { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 490 | success = 1; |
| 491 | break; |
| 492 | } |
| 493 | |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 494 | struct addrinfo* ai = calloc(1, sizeof(struct addrinfo) + sizeof(struct sockaddr_storage)); |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 495 | if (ai == NULL) { |
| 496 | break; |
| 497 | } |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 498 | ai->ai_addr = (struct sockaddr*)(ai + 1); |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 499 | |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 500 | // struct addrinfo { |
| 501 | // int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */ |
| 502 | // int ai_family; /* PF_xxx */ |
| 503 | // int ai_socktype; /* SOCK_xxx */ |
| 504 | // int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ |
| 505 | // socklen_t ai_addrlen; /* length of ai_addr */ |
| 506 | // char *ai_canonname; /* canonical name for hostname */ |
| 507 | // struct sockaddr *ai_addr; /* binary address */ |
| 508 | // struct addrinfo *ai_next; /* next structure in linked list */ |
| 509 | // }; |
| 510 | |
| 511 | // Read the struct piece by piece because we might be a 32-bit process |
| 512 | // talking to a 64-bit netd. |
| 513 | int32_t addr_len; |
| 514 | bool success = |
| 515 | readBE32(proxy, &ai->ai_flags) && |
| 516 | readBE32(proxy, &ai->ai_family) && |
| 517 | readBE32(proxy, &ai->ai_socktype) && |
| 518 | readBE32(proxy, &ai->ai_protocol) && |
| 519 | readBE32(proxy, &addr_len); |
| 520 | if (!success) { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 524 | // Set ai_addrlen and read the ai_addr data. |
| 525 | ai->ai_addrlen = addr_len; |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 526 | if (addr_len != 0) { |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 527 | if ((size_t) addr_len > sizeof(struct sockaddr_storage)) { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 528 | // Bogus; too big. |
| 529 | break; |
| 530 | } |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 531 | if (fread(ai->ai_addr, addr_len, 1, proxy) != 1) { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 532 | break; |
| 533 | } |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Elliott Hughes | 55293c1 | 2014-11-12 17:00:30 -0800 | [diff] [blame] | 536 | // The string for ai_cannonname. |
| 537 | int32_t name_len; |
| 538 | if (!readBE32(proxy, &name_len)) { |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | if (name_len != 0) { |
| 542 | ai->ai_canonname = (char*) malloc(name_len); |
| 543 | if (fread(ai->ai_canonname, name_len, 1, proxy) != 1) { |
| 544 | break; |
| 545 | } |
| 546 | if (ai->ai_canonname[name_len - 1] != '\0') { |
| 547 | // The proxy should be returning this |
| 548 | // NULL-terminated. |
| 549 | break; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | *nextres = ai; |
| 554 | nextres = &ai->ai_next; |
| 555 | ai = NULL; |
| 556 | } |
| 557 | |
| 558 | if (ai != NULL) { |
| 559 | // Clean up partially-built addrinfo that we never ended up |
| 560 | // attaching to the response. |
| 561 | freeaddrinfo(ai); |
| 562 | } |
| 563 | exit: |
| 564 | if (proxy != NULL) { |
| 565 | fclose(proxy); |
| 566 | } |
| 567 | |
| 568 | if (success) { |
| 569 | return 0; |
| 570 | } |
| 571 | |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 572 | // Proxy failed; |
| 573 | // clean up memory we might've allocated. |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 574 | if (*res) { |
| 575 | freeaddrinfo(*res); |
| 576 | *res = NULL; |
| 577 | } |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 578 | return EAI_NODATA; |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 581 | int |
| 582 | getaddrinfo(const char *hostname, const char *servname, |
| 583 | const struct addrinfo *hints, struct addrinfo **res) |
| 584 | { |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 585 | return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res); |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | int |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 589 | android_getaddrinfofornet(const char *hostname, const char *servname, |
| 590 | const struct addrinfo *hints, unsigned netid, unsigned mark, struct addrinfo **res) |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 591 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 592 | struct addrinfo sentinel; |
| 593 | struct addrinfo *cur; |
| 594 | int error = 0; |
| 595 | struct addrinfo ai; |
| 596 | struct addrinfo ai0; |
| 597 | struct addrinfo *pai; |
| 598 | const struct explore *ex; |
| 599 | |
| 600 | /* hostname is allowed to be NULL */ |
| 601 | /* servname is allowed to be NULL */ |
| 602 | /* hints is allowed to be NULL */ |
| 603 | assert(res != NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 604 | memset(&sentinel, 0, sizeof(sentinel)); |
| 605 | cur = &sentinel; |
| 606 | pai = &ai; |
| 607 | pai->ai_flags = 0; |
| 608 | pai->ai_family = PF_UNSPEC; |
| 609 | pai->ai_socktype = ANY; |
| 610 | pai->ai_protocol = ANY; |
| 611 | pai->ai_addrlen = 0; |
| 612 | pai->ai_canonname = NULL; |
| 613 | pai->ai_addr = NULL; |
| 614 | pai->ai_next = NULL; |
| 615 | |
| 616 | if (hostname == NULL && servname == NULL) |
| 617 | return EAI_NONAME; |
| 618 | if (hints) { |
| 619 | /* error check for hints */ |
| 620 | if (hints->ai_addrlen || hints->ai_canonname || |
| 621 | hints->ai_addr || hints->ai_next) |
| 622 | ERR(EAI_BADHINTS); /* xxx */ |
| 623 | if (hints->ai_flags & ~AI_MASK) |
| 624 | ERR(EAI_BADFLAGS); |
| 625 | switch (hints->ai_family) { |
| 626 | case PF_UNSPEC: |
| 627 | case PF_INET: |
| 628 | #ifdef INET6 |
| 629 | case PF_INET6: |
| 630 | #endif |
| 631 | break; |
| 632 | default: |
| 633 | ERR(EAI_FAMILY); |
| 634 | } |
| 635 | memcpy(pai, hints, sizeof(*pai)); |
| 636 | |
| 637 | /* |
| 638 | * if both socktype/protocol are specified, check if they |
| 639 | * are meaningful combination. |
| 640 | */ |
| 641 | if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { |
| 642 | for (ex = explore; ex->e_af >= 0; ex++) { |
| 643 | if (pai->ai_family != ex->e_af) |
| 644 | continue; |
| 645 | if (ex->e_socktype == ANY) |
| 646 | continue; |
| 647 | if (ex->e_protocol == ANY) |
| 648 | continue; |
| 649 | if (pai->ai_socktype == ex->e_socktype |
| 650 | && pai->ai_protocol != ex->e_protocol) { |
| 651 | ERR(EAI_BADHINTS); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * check for special cases. (1) numeric servname is disallowed if |
| 659 | * socktype/protocol are left unspecified. (2) servname is disallowed |
| 660 | * for raw and other inet{,6} sockets. |
| 661 | */ |
| 662 | if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) |
| 663 | #ifdef PF_INET6 |
| 664 | || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) |
| 665 | #endif |
| 666 | ) { |
| 667 | ai0 = *pai; /* backup *pai */ |
| 668 | |
| 669 | if (pai->ai_family == PF_UNSPEC) { |
| 670 | #ifdef PF_INET6 |
| 671 | pai->ai_family = PF_INET6; |
| 672 | #else |
| 673 | pai->ai_family = PF_INET; |
| 674 | #endif |
| 675 | } |
| 676 | error = get_portmatch(pai, servname); |
| 677 | if (error) |
| 678 | ERR(error); |
| 679 | |
| 680 | *pai = ai0; |
| 681 | } |
| 682 | |
| 683 | ai0 = *pai; |
| 684 | |
| 685 | /* NULL hostname, or numeric hostname */ |
| 686 | for (ex = explore; ex->e_af >= 0; ex++) { |
| 687 | *pai = ai0; |
| 688 | |
| 689 | /* PF_UNSPEC entries are prepared for DNS queries only */ |
| 690 | if (ex->e_af == PF_UNSPEC) |
| 691 | continue; |
| 692 | |
| 693 | if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) |
| 694 | continue; |
| 695 | if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) |
| 696 | continue; |
| 697 | if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) |
| 698 | continue; |
| 699 | |
| 700 | if (pai->ai_family == PF_UNSPEC) |
| 701 | pai->ai_family = ex->e_af; |
| 702 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) |
| 703 | pai->ai_socktype = ex->e_socktype; |
| 704 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) |
| 705 | pai->ai_protocol = ex->e_protocol; |
| 706 | |
| 707 | if (hostname == NULL) |
| 708 | error = explore_null(pai, servname, &cur->ai_next); |
| 709 | else |
| 710 | error = explore_numeric_scope(pai, hostname, servname, |
| 711 | &cur->ai_next); |
| 712 | |
| 713 | if (error) |
| 714 | goto free; |
| 715 | |
| 716 | while (cur->ai_next) |
| 717 | cur = cur->ai_next; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * XXX |
| 722 | * If numeric representation of AF1 can be interpreted as FQDN |
| 723 | * representation of AF2, we need to think again about the code below. |
| 724 | */ |
| 725 | if (sentinel.ai_next) |
| 726 | goto good; |
| 727 | |
| 728 | if (hostname == NULL) |
| 729 | ERR(EAI_NODATA); |
| 730 | if (pai->ai_flags & AI_NUMERICHOST) |
| 731 | ERR(EAI_NONAME); |
| 732 | |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 733 | #if defined(__ANDROID__) |
| 734 | int gai_error = android_getaddrinfo_proxy(hostname, servname, hints, res, netid); |
| 735 | if (gai_error != EAI_SYSTEM) { |
| 736 | return gai_error; |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 737 | } |
Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 738 | #endif |
Brad Fitzpatrick | a1dbf0b | 2010-10-27 10:36:36 -0700 | [diff] [blame] | 739 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 740 | /* |
| 741 | * hostname as alphabetical name. |
| 742 | * we would like to prefer AF_INET6 than AF_INET, so we'll make a |
| 743 | * outer loop by AFs. |
| 744 | */ |
| 745 | for (ex = explore; ex->e_af >= 0; ex++) { |
| 746 | *pai = ai0; |
| 747 | |
| 748 | /* require exact match for family field */ |
| 749 | if (pai->ai_family != ex->e_af) |
| 750 | continue; |
| 751 | |
| 752 | if (!MATCH(pai->ai_socktype, ex->e_socktype, |
| 753 | WILD_SOCKTYPE(ex))) { |
| 754 | continue; |
| 755 | } |
| 756 | if (!MATCH(pai->ai_protocol, ex->e_protocol, |
| 757 | WILD_PROTOCOL(ex))) { |
| 758 | continue; |
| 759 | } |
| 760 | |
| 761 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) |
| 762 | pai->ai_socktype = ex->e_socktype; |
| 763 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) |
| 764 | pai->ai_protocol = ex->e_protocol; |
| 765 | |
| 766 | error = explore_fqdn(pai, hostname, servname, |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 767 | &cur->ai_next, netid, mark); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 768 | |
| 769 | while (cur && cur->ai_next) |
| 770 | cur = cur->ai_next; |
| 771 | } |
| 772 | |
| 773 | /* XXX */ |
| 774 | if (sentinel.ai_next) |
| 775 | error = 0; |
| 776 | |
| 777 | if (error) |
| 778 | goto free; |
| 779 | if (error == 0) { |
| 780 | if (sentinel.ai_next) { |
| 781 | good: |
| 782 | *res = sentinel.ai_next; |
| 783 | return SUCCESS; |
| 784 | } else |
| 785 | error = EAI_FAIL; |
| 786 | } |
| 787 | free: |
| 788 | bad: |
| 789 | if (sentinel.ai_next) |
| 790 | freeaddrinfo(sentinel.ai_next); |
| 791 | *res = NULL; |
| 792 | return error; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * FQDN hostname, DNS lookup |
| 797 | */ |
| 798 | static int |
| 799 | explore_fqdn(const struct addrinfo *pai, const char *hostname, |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 800 | const char *servname, struct addrinfo **res, unsigned netid, unsigned mark) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 801 | { |
| 802 | struct addrinfo *result; |
| 803 | struct addrinfo *cur; |
| 804 | int error = 0; |
| 805 | static const ns_dtab dtab[] = { |
| 806 | NS_FILES_CB(_files_getaddrinfo, NULL) |
| 807 | { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ |
| 808 | NS_NIS_CB(_yp_getaddrinfo, NULL) |
| 809 | { 0, 0, 0 } |
| 810 | }; |
| 811 | |
| 812 | assert(pai != NULL); |
| 813 | /* hostname may be NULL */ |
| 814 | /* servname may be NULL */ |
| 815 | assert(res != NULL); |
| 816 | |
| 817 | result = NULL; |
| 818 | |
| 819 | /* |
| 820 | * if the servname does not match socktype/protocol, ignore it. |
| 821 | */ |
| 822 | if (get_portmatch(pai, servname) != 0) |
| 823 | return 0; |
| 824 | |
| 825 | switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 826 | default_dns_files, hostname, pai, netid, mark)) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 827 | case NS_TRYAGAIN: |
| 828 | error = EAI_AGAIN; |
| 829 | goto free; |
| 830 | case NS_UNAVAIL: |
| 831 | error = EAI_FAIL; |
| 832 | goto free; |
| 833 | case NS_NOTFOUND: |
| 834 | error = EAI_NODATA; |
| 835 | goto free; |
| 836 | case NS_SUCCESS: |
| 837 | error = 0; |
| 838 | for (cur = result; cur; cur = cur->ai_next) { |
| 839 | GET_PORT(cur, servname); |
| 840 | /* canonname should be filled already */ |
| 841 | } |
| 842 | break; |
| 843 | } |
| 844 | |
| 845 | *res = result; |
| 846 | |
| 847 | return 0; |
| 848 | |
| 849 | free: |
| 850 | if (result) |
| 851 | freeaddrinfo(result); |
| 852 | return error; |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * hostname == NULL. |
| 857 | * passive socket -> anyaddr (0.0.0.0 or ::) |
| 858 | * non-passive socket -> localhost (127.0.0.1 or ::1) |
| 859 | */ |
| 860 | static int |
| 861 | explore_null(const struct addrinfo *pai, const char *servname, |
| 862 | struct addrinfo **res) |
| 863 | { |
| 864 | int s; |
| 865 | const struct afd *afd; |
| 866 | struct addrinfo *cur; |
| 867 | struct addrinfo sentinel; |
| 868 | int error; |
| 869 | |
| 870 | assert(pai != NULL); |
| 871 | /* servname may be NULL */ |
| 872 | assert(res != NULL); |
| 873 | |
| 874 | *res = NULL; |
| 875 | sentinel.ai_next = NULL; |
| 876 | cur = &sentinel; |
| 877 | |
| 878 | /* |
| 879 | * filter out AFs that are not supported by the kernel |
| 880 | * XXX errno? |
| 881 | */ |
Nick Kralevich | 1781ed7 | 2014-06-29 20:46:17 -0700 | [diff] [blame] | 882 | s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 883 | if (s < 0) { |
| 884 | if (errno != EMFILE) |
| 885 | return 0; |
| 886 | } else |
| 887 | close(s); |
| 888 | |
| 889 | /* |
| 890 | * if the servname does not match socktype/protocol, ignore it. |
| 891 | */ |
| 892 | if (get_portmatch(pai, servname) != 0) |
| 893 | return 0; |
| 894 | |
| 895 | afd = find_afd(pai->ai_family); |
| 896 | if (afd == NULL) |
| 897 | return 0; |
| 898 | |
| 899 | if (pai->ai_flags & AI_PASSIVE) { |
| 900 | GET_AI(cur->ai_next, afd, afd->a_addrany); |
| 901 | /* xxx meaningless? |
| 902 | * GET_CANONNAME(cur->ai_next, "anyaddr"); |
| 903 | */ |
| 904 | GET_PORT(cur->ai_next, servname); |
| 905 | } else { |
| 906 | GET_AI(cur->ai_next, afd, afd->a_loopback); |
| 907 | /* xxx meaningless? |
| 908 | * GET_CANONNAME(cur->ai_next, "localhost"); |
| 909 | */ |
| 910 | GET_PORT(cur->ai_next, servname); |
| 911 | } |
| 912 | cur = cur->ai_next; |
| 913 | |
| 914 | *res = sentinel.ai_next; |
| 915 | return 0; |
| 916 | |
| 917 | free: |
| 918 | if (sentinel.ai_next) |
| 919 | freeaddrinfo(sentinel.ai_next); |
| 920 | return error; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * numeric hostname |
| 925 | */ |
| 926 | static int |
| 927 | explore_numeric(const struct addrinfo *pai, const char *hostname, |
| 928 | const char *servname, struct addrinfo **res, const char *canonname) |
| 929 | { |
| 930 | const struct afd *afd; |
| 931 | struct addrinfo *cur; |
| 932 | struct addrinfo sentinel; |
| 933 | int error; |
| 934 | char pton[PTON_MAX]; |
| 935 | |
| 936 | assert(pai != NULL); |
| 937 | /* hostname may be NULL */ |
| 938 | /* servname may be NULL */ |
| 939 | assert(res != NULL); |
| 940 | |
| 941 | *res = NULL; |
| 942 | sentinel.ai_next = NULL; |
| 943 | cur = &sentinel; |
| 944 | |
| 945 | /* |
| 946 | * if the servname does not match socktype/protocol, ignore it. |
| 947 | */ |
| 948 | if (get_portmatch(pai, servname) != 0) |
| 949 | return 0; |
| 950 | |
| 951 | afd = find_afd(pai->ai_family); |
| 952 | if (afd == NULL) |
| 953 | return 0; |
| 954 | |
| 955 | switch (afd->a_af) { |
| 956 | #if 0 /*X/Open spec*/ |
| 957 | case AF_INET: |
| 958 | if (inet_aton(hostname, (struct in_addr *)pton) == 1) { |
| 959 | if (pai->ai_family == afd->a_af || |
| 960 | pai->ai_family == PF_UNSPEC /*?*/) { |
| 961 | GET_AI(cur->ai_next, afd, pton); |
| 962 | GET_PORT(cur->ai_next, servname); |
| 963 | if ((pai->ai_flags & AI_CANONNAME)) { |
| 964 | /* |
| 965 | * Set the numeric address itself as |
| 966 | * the canonical name, based on a |
| 967 | * clarification in rfc2553bis-03. |
| 968 | */ |
| 969 | GET_CANONNAME(cur->ai_next, canonname); |
| 970 | } |
| 971 | while (cur && cur->ai_next) |
| 972 | cur = cur->ai_next; |
| 973 | } else |
| 974 | ERR(EAI_FAMILY); /*xxx*/ |
| 975 | } |
| 976 | break; |
| 977 | #endif |
| 978 | default: |
| 979 | if (inet_pton(afd->a_af, hostname, pton) == 1) { |
| 980 | if (pai->ai_family == afd->a_af || |
| 981 | pai->ai_family == PF_UNSPEC /*?*/) { |
| 982 | GET_AI(cur->ai_next, afd, pton); |
| 983 | GET_PORT(cur->ai_next, servname); |
| 984 | if ((pai->ai_flags & AI_CANONNAME)) { |
| 985 | /* |
| 986 | * Set the numeric address itself as |
| 987 | * the canonical name, based on a |
| 988 | * clarification in rfc2553bis-03. |
| 989 | */ |
| 990 | GET_CANONNAME(cur->ai_next, canonname); |
| 991 | } |
| 992 | while (cur->ai_next) |
| 993 | cur = cur->ai_next; |
| 994 | } else |
| 995 | ERR(EAI_FAMILY); /*xxx*/ |
| 996 | } |
| 997 | break; |
| 998 | } |
| 999 | |
| 1000 | *res = sentinel.ai_next; |
| 1001 | return 0; |
| 1002 | |
| 1003 | free: |
| 1004 | bad: |
| 1005 | if (sentinel.ai_next) |
| 1006 | freeaddrinfo(sentinel.ai_next); |
| 1007 | return error; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * numeric hostname with scope |
| 1012 | */ |
| 1013 | static int |
| 1014 | explore_numeric_scope(const struct addrinfo *pai, const char *hostname, |
| 1015 | const char *servname, struct addrinfo **res) |
| 1016 | { |
| 1017 | #if !defined(SCOPE_DELIMITER) || !defined(INET6) |
| 1018 | return explore_numeric(pai, hostname, servname, res, hostname); |
| 1019 | #else |
| 1020 | const struct afd *afd; |
| 1021 | struct addrinfo *cur; |
| 1022 | int error; |
| 1023 | char *cp, *hostname2 = NULL, *scope, *addr; |
| 1024 | struct sockaddr_in6 *sin6; |
| 1025 | |
| 1026 | assert(pai != NULL); |
| 1027 | /* hostname may be NULL */ |
| 1028 | /* servname may be NULL */ |
| 1029 | assert(res != NULL); |
| 1030 | |
| 1031 | /* |
| 1032 | * if the servname does not match socktype/protocol, ignore it. |
| 1033 | */ |
| 1034 | if (get_portmatch(pai, servname) != 0) |
| 1035 | return 0; |
| 1036 | |
| 1037 | afd = find_afd(pai->ai_family); |
| 1038 | if (afd == NULL) |
| 1039 | return 0; |
| 1040 | |
| 1041 | if (!afd->a_scoped) |
| 1042 | return explore_numeric(pai, hostname, servname, res, hostname); |
| 1043 | |
| 1044 | cp = strchr(hostname, SCOPE_DELIMITER); |
| 1045 | if (cp == NULL) |
| 1046 | return explore_numeric(pai, hostname, servname, res, hostname); |
| 1047 | |
| 1048 | /* |
| 1049 | * Handle special case of <scoped_address><delimiter><scope id> |
| 1050 | */ |
| 1051 | hostname2 = strdup(hostname); |
| 1052 | if (hostname2 == NULL) |
| 1053 | return EAI_MEMORY; |
| 1054 | /* terminate at the delimiter */ |
| 1055 | hostname2[cp - hostname] = '\0'; |
| 1056 | addr = hostname2; |
| 1057 | scope = cp + 1; |
| 1058 | |
| 1059 | error = explore_numeric(pai, addr, servname, res, hostname); |
| 1060 | if (error == 0) { |
| 1061 | u_int32_t scopeid; |
| 1062 | |
| 1063 | for (cur = *res; cur; cur = cur->ai_next) { |
| 1064 | if (cur->ai_family != AF_INET6) |
| 1065 | continue; |
| 1066 | sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; |
| 1067 | if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { |
| 1068 | free(hostname2); |
| 1069 | return(EAI_NODATA); /* XXX: is return OK? */ |
| 1070 | } |
| 1071 | sin6->sin6_scope_id = scopeid; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | free(hostname2); |
| 1076 | |
| 1077 | return error; |
| 1078 | #endif |
| 1079 | } |
| 1080 | |
| 1081 | static int |
| 1082 | get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str) |
| 1083 | { |
| 1084 | |
| 1085 | assert(pai != NULL); |
| 1086 | assert(ai != NULL); |
| 1087 | assert(str != NULL); |
| 1088 | |
| 1089 | if ((pai->ai_flags & AI_CANONNAME) != 0) { |
| 1090 | ai->ai_canonname = strdup(str); |
| 1091 | if (ai->ai_canonname == NULL) |
| 1092 | return EAI_MEMORY; |
| 1093 | } |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | static struct addrinfo * |
| 1098 | get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr) |
| 1099 | { |
| 1100 | char *p; |
| 1101 | struct addrinfo *ai; |
| 1102 | |
| 1103 | assert(pai != NULL); |
| 1104 | assert(afd != NULL); |
| 1105 | assert(addr != NULL); |
| 1106 | |
| 1107 | ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) |
| 1108 | + (afd->a_socklen)); |
| 1109 | if (ai == NULL) |
| 1110 | return NULL; |
| 1111 | |
| 1112 | memcpy(ai, pai, sizeof(struct addrinfo)); |
| 1113 | ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); |
| 1114 | memset(ai->ai_addr, 0, (size_t)afd->a_socklen); |
| 1115 | |
| 1116 | #ifdef HAVE_SA_LEN |
| 1117 | ai->ai_addr->sa_len = afd->a_socklen; |
| 1118 | #endif |
| 1119 | |
| 1120 | ai->ai_addrlen = afd->a_socklen; |
| 1121 | #if defined (__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__) |
| 1122 | ai->__ai_pad0 = 0; |
| 1123 | #endif |
| 1124 | ai->ai_addr->sa_family = ai->ai_family = afd->a_af; |
| 1125 | p = (char *)(void *)(ai->ai_addr); |
| 1126 | memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); |
| 1127 | return ai; |
| 1128 | } |
| 1129 | |
| 1130 | static int |
| 1131 | get_portmatch(const struct addrinfo *ai, const char *servname) |
| 1132 | { |
| 1133 | |
| 1134 | assert(ai != NULL); |
| 1135 | /* servname may be NULL */ |
| 1136 | |
| 1137 | return get_port(ai, servname, 1); |
| 1138 | } |
| 1139 | |
| 1140 | static int |
| 1141 | get_port(const struct addrinfo *ai, const char *servname, int matchonly) |
| 1142 | { |
| 1143 | const char *proto; |
| 1144 | struct servent *sp; |
| 1145 | int port; |
| 1146 | int allownumeric; |
| 1147 | |
| 1148 | assert(ai != NULL); |
| 1149 | /* servname may be NULL */ |
| 1150 | |
| 1151 | if (servname == NULL) |
| 1152 | return 0; |
| 1153 | switch (ai->ai_family) { |
| 1154 | case AF_INET: |
| 1155 | #ifdef AF_INET6 |
| 1156 | case AF_INET6: |
| 1157 | #endif |
| 1158 | break; |
| 1159 | default: |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | switch (ai->ai_socktype) { |
| 1164 | case SOCK_RAW: |
| 1165 | return EAI_SERVICE; |
| 1166 | case SOCK_DGRAM: |
| 1167 | case SOCK_STREAM: |
| 1168 | allownumeric = 1; |
| 1169 | break; |
| 1170 | case ANY: |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1171 | #if 1 /* ANDROID-SPECIFIC CHANGE TO MATCH GLIBC */ |
David 'Digit' Turner | 5e56370 | 2009-05-05 15:50:24 +0200 | [diff] [blame] | 1172 | allownumeric = 1; |
| 1173 | #else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1174 | allownumeric = 0; |
David 'Digit' Turner | 5e56370 | 2009-05-05 15:50:24 +0200 | [diff] [blame] | 1175 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1176 | break; |
| 1177 | default: |
| 1178 | return EAI_SOCKTYPE; |
| 1179 | } |
| 1180 | |
| 1181 | port = str2number(servname); |
| 1182 | if (port >= 0) { |
| 1183 | if (!allownumeric) |
| 1184 | return EAI_SERVICE; |
| 1185 | if (port < 0 || port > 65535) |
| 1186 | return EAI_SERVICE; |
| 1187 | port = htons(port); |
| 1188 | } else { |
| 1189 | if (ai->ai_flags & AI_NUMERICSERV) |
| 1190 | return EAI_NONAME; |
| 1191 | |
| 1192 | switch (ai->ai_socktype) { |
| 1193 | case SOCK_DGRAM: |
| 1194 | proto = "udp"; |
| 1195 | break; |
| 1196 | case SOCK_STREAM: |
| 1197 | proto = "tcp"; |
| 1198 | break; |
| 1199 | default: |
| 1200 | proto = NULL; |
| 1201 | break; |
| 1202 | } |
| 1203 | |
| 1204 | if ((sp = getservbyname(servname, proto)) == NULL) |
| 1205 | return EAI_SERVICE; |
| 1206 | port = sp->s_port; |
| 1207 | } |
| 1208 | |
| 1209 | if (!matchonly) { |
| 1210 | switch (ai->ai_family) { |
| 1211 | case AF_INET: |
| 1212 | ((struct sockaddr_in *)(void *) |
| 1213 | ai->ai_addr)->sin_port = port; |
| 1214 | break; |
| 1215 | #ifdef INET6 |
| 1216 | case AF_INET6: |
| 1217 | ((struct sockaddr_in6 *)(void *) |
| 1218 | ai->ai_addr)->sin6_port = port; |
| 1219 | break; |
| 1220 | #endif |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | static const struct afd * |
| 1228 | find_afd(int af) |
| 1229 | { |
| 1230 | const struct afd *afd; |
| 1231 | |
| 1232 | if (af == PF_UNSPEC) |
| 1233 | return NULL; |
| 1234 | for (afd = afdl; afd->a_af; afd++) { |
| 1235 | if (afd->a_af == af) |
| 1236 | return afd; |
| 1237 | } |
| 1238 | return NULL; |
| 1239 | } |
| 1240 | |
| 1241 | #ifdef INET6 |
| 1242 | /* convert a string to a scope identifier. XXX: IPv6 specific */ |
| 1243 | static int |
| 1244 | ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid) |
| 1245 | { |
| 1246 | u_long lscopeid; |
| 1247 | struct in6_addr *a6; |
| 1248 | char *ep; |
| 1249 | |
| 1250 | assert(scope != NULL); |
| 1251 | assert(sin6 != NULL); |
| 1252 | assert(scopeid != NULL); |
| 1253 | |
| 1254 | a6 = &sin6->sin6_addr; |
| 1255 | |
| 1256 | /* empty scopeid portion is invalid */ |
| 1257 | if (*scope == '\0') |
| 1258 | return -1; |
| 1259 | |
| 1260 | if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { |
| 1261 | /* |
| 1262 | * We currently assume a one-to-one mapping between links |
| 1263 | * and interfaces, so we simply use interface indices for |
| 1264 | * like-local scopes. |
| 1265 | */ |
| 1266 | *scopeid = if_nametoindex(scope); |
| 1267 | if (*scopeid == 0) |
| 1268 | goto trynumeric; |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | /* still unclear about literal, allow numeric only - placeholder */ |
| 1273 | if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) |
| 1274 | goto trynumeric; |
| 1275 | if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) |
| 1276 | goto trynumeric; |
| 1277 | else |
| 1278 | goto trynumeric; /* global */ |
| 1279 | |
| 1280 | /* try to convert to a numeric id as a last resort */ |
| 1281 | trynumeric: |
| 1282 | errno = 0; |
| 1283 | lscopeid = strtoul(scope, &ep, 10); |
| 1284 | *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); |
| 1285 | if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) |
| 1286 | return 0; |
| 1287 | else |
| 1288 | return -1; |
| 1289 | } |
| 1290 | #endif |
| 1291 | |
| 1292 | /* code duplicate with gethnamaddr.c */ |
| 1293 | |
| 1294 | static const char AskedForGot[] = |
| 1295 | "gethostby*.getanswer: asked for \"%s\", got \"%s\""; |
| 1296 | |
| 1297 | static struct addrinfo * |
| 1298 | getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, |
| 1299 | const struct addrinfo *pai) |
| 1300 | { |
| 1301 | struct addrinfo sentinel, *cur; |
| 1302 | struct addrinfo ai; |
| 1303 | const struct afd *afd; |
| 1304 | char *canonname; |
| 1305 | const HEADER *hp; |
| 1306 | const u_char *cp; |
| 1307 | int n; |
| 1308 | const u_char *eom; |
| 1309 | char *bp, *ep; |
| 1310 | int type, class, ancount, qdcount; |
| 1311 | int haveanswer, had_error; |
| 1312 | char tbuf[MAXDNAME]; |
| 1313 | int (*name_ok) (const char *); |
| 1314 | char hostbuf[8*1024]; |
| 1315 | |
| 1316 | assert(answer != NULL); |
| 1317 | assert(qname != NULL); |
| 1318 | assert(pai != NULL); |
| 1319 | |
| 1320 | memset(&sentinel, 0, sizeof(sentinel)); |
| 1321 | cur = &sentinel; |
| 1322 | |
| 1323 | canonname = NULL; |
| 1324 | eom = answer->buf + anslen; |
| 1325 | switch (qtype) { |
| 1326 | case T_A: |
| 1327 | case T_AAAA: |
| 1328 | case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ |
| 1329 | name_ok = res_hnok; |
| 1330 | break; |
| 1331 | default: |
| 1332 | return NULL; /* XXX should be abort(); */ |
| 1333 | } |
| 1334 | /* |
| 1335 | * find first satisfactory answer |
| 1336 | */ |
| 1337 | hp = &answer->hdr; |
| 1338 | ancount = ntohs(hp->ancount); |
| 1339 | qdcount = ntohs(hp->qdcount); |
| 1340 | bp = hostbuf; |
| 1341 | ep = hostbuf + sizeof hostbuf; |
| 1342 | cp = answer->buf + HFIXEDSZ; |
| 1343 | if (qdcount != 1) { |
| 1344 | h_errno = NO_RECOVERY; |
| 1345 | return (NULL); |
| 1346 | } |
| 1347 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 1348 | if ((n < 0) || !(*name_ok)(bp)) { |
| 1349 | h_errno = NO_RECOVERY; |
| 1350 | return (NULL); |
| 1351 | } |
| 1352 | cp += n + QFIXEDSZ; |
| 1353 | if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { |
| 1354 | /* res_send() has already verified that the query name is the |
| 1355 | * same as the one we sent; this just gets the expanded name |
| 1356 | * (i.e., with the succeeding search-domain tacked on). |
| 1357 | */ |
| 1358 | n = strlen(bp) + 1; /* for the \0 */ |
| 1359 | if (n >= MAXHOSTNAMELEN) { |
| 1360 | h_errno = NO_RECOVERY; |
| 1361 | return (NULL); |
| 1362 | } |
| 1363 | canonname = bp; |
| 1364 | bp += n; |
| 1365 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 1366 | qname = canonname; |
| 1367 | } |
| 1368 | haveanswer = 0; |
| 1369 | had_error = 0; |
| 1370 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 1371 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 1372 | if ((n < 0) || !(*name_ok)(bp)) { |
| 1373 | had_error++; |
| 1374 | continue; |
| 1375 | } |
| 1376 | cp += n; /* name */ |
| 1377 | type = _getshort(cp); |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 1378 | cp += INT16SZ; /* type */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1379 | class = _getshort(cp); |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 1380 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1381 | n = _getshort(cp); |
| 1382 | cp += INT16SZ; /* len */ |
| 1383 | if (class != C_IN) { |
| 1384 | /* XXX - debug? syslog? */ |
| 1385 | cp += n; |
| 1386 | continue; /* XXX - had_error++ ? */ |
| 1387 | } |
| 1388 | if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && |
| 1389 | type == T_CNAME) { |
| 1390 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 1391 | if ((n < 0) || !(*name_ok)(tbuf)) { |
| 1392 | had_error++; |
| 1393 | continue; |
| 1394 | } |
| 1395 | cp += n; |
| 1396 | /* Get canonical name. */ |
| 1397 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 1398 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 1399 | had_error++; |
| 1400 | continue; |
| 1401 | } |
| 1402 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 1403 | canonname = bp; |
| 1404 | bp += n; |
| 1405 | continue; |
| 1406 | } |
| 1407 | if (qtype == T_ANY) { |
| 1408 | if (!(type == T_A || type == T_AAAA)) { |
| 1409 | cp += n; |
| 1410 | continue; |
| 1411 | } |
| 1412 | } else if (type != qtype) { |
| 1413 | if (type != T_KEY && type != T_SIG) |
| 1414 | syslog(LOG_NOTICE|LOG_AUTH, |
| 1415 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", |
| 1416 | qname, p_class(C_IN), p_type(qtype), |
| 1417 | p_type(type)); |
| 1418 | cp += n; |
| 1419 | continue; /* XXX - had_error++ ? */ |
| 1420 | } |
| 1421 | switch (type) { |
| 1422 | case T_A: |
| 1423 | case T_AAAA: |
| 1424 | if (strcasecmp(canonname, bp) != 0) { |
| 1425 | syslog(LOG_NOTICE|LOG_AUTH, |
| 1426 | AskedForGot, canonname, bp); |
| 1427 | cp += n; |
| 1428 | continue; /* XXX - had_error++ ? */ |
| 1429 | } |
| 1430 | if (type == T_A && n != INADDRSZ) { |
| 1431 | cp += n; |
| 1432 | continue; |
| 1433 | } |
| 1434 | if (type == T_AAAA && n != IN6ADDRSZ) { |
| 1435 | cp += n; |
| 1436 | continue; |
| 1437 | } |
| 1438 | if (type == T_AAAA) { |
| 1439 | struct in6_addr in6; |
| 1440 | memcpy(&in6, cp, IN6ADDRSZ); |
| 1441 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 1442 | cp += n; |
| 1443 | continue; |
| 1444 | } |
| 1445 | } |
| 1446 | if (!haveanswer) { |
| 1447 | int nn; |
| 1448 | |
| 1449 | canonname = bp; |
| 1450 | nn = strlen(bp) + 1; /* for the \0 */ |
| 1451 | bp += nn; |
| 1452 | } |
| 1453 | |
| 1454 | /* don't overwrite pai */ |
| 1455 | ai = *pai; |
| 1456 | ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; |
| 1457 | afd = find_afd(ai.ai_family); |
| 1458 | if (afd == NULL) { |
| 1459 | cp += n; |
| 1460 | continue; |
| 1461 | } |
| 1462 | cur->ai_next = get_ai(&ai, afd, (const char *)cp); |
| 1463 | if (cur->ai_next == NULL) |
| 1464 | had_error++; |
| 1465 | while (cur && cur->ai_next) |
| 1466 | cur = cur->ai_next; |
| 1467 | cp += n; |
| 1468 | break; |
| 1469 | default: |
| 1470 | abort(); |
| 1471 | } |
| 1472 | if (!had_error) |
| 1473 | haveanswer++; |
| 1474 | } |
| 1475 | if (haveanswer) { |
| 1476 | if (!canonname) |
| 1477 | (void)get_canonname(pai, sentinel.ai_next, qname); |
| 1478 | else |
| 1479 | (void)get_canonname(pai, sentinel.ai_next, canonname); |
| 1480 | h_errno = NETDB_SUCCESS; |
| 1481 | return sentinel.ai_next; |
| 1482 | } |
| 1483 | |
| 1484 | h_errno = NO_RECOVERY; |
| 1485 | return NULL; |
| 1486 | } |
| 1487 | |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1488 | struct addrinfo_sort_elem { |
| 1489 | struct addrinfo *ai; |
| 1490 | int has_src_addr; |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 1491 | sockaddr_union src_addr; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1492 | int original_order; |
| 1493 | }; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1494 | |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1495 | /*ARGSUSED*/ |
| 1496 | static int |
| 1497 | _get_scope(const struct sockaddr *addr) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1498 | { |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1499 | if (addr->sa_family == AF_INET6) { |
| 1500 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 1501 | if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) { |
| 1502 | return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr); |
| 1503 | } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) || |
| 1504 | IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) { |
| 1505 | /* |
| 1506 | * RFC 4291 section 2.5.3 says loopback is to be treated as having |
| 1507 | * link-local scope. |
| 1508 | */ |
| 1509 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
| 1510 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1511 | return IPV6_ADDR_SCOPE_SITELOCAL; |
| 1512 | } else { |
| 1513 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1514 | } |
| 1515 | } else if (addr->sa_family == AF_INET) { |
| 1516 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 1517 | unsigned long int na = ntohl(addr4->sin_addr.s_addr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1518 | |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1519 | if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */ |
| 1520 | (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */ |
| 1521 | return IPV6_ADDR_SCOPE_LINKLOCAL; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1522 | } else { |
Steinar H. Gunderson | d1624ad | 2010-12-20 11:15:33 +0100 | [diff] [blame] | 1523 | /* |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1524 | * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses |
| 1525 | * and shared addresses (100.64.0.0/10), are assigned global scope. |
Steinar H. Gunderson | d1624ad | 2010-12-20 11:15:33 +0100 | [diff] [blame] | 1526 | */ |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1527 | return IPV6_ADDR_SCOPE_GLOBAL; |
| 1528 | } |
| 1529 | } else { |
| 1530 | /* |
| 1531 | * This should never happen. |
| 1532 | * Return a scope with low priority as a last resort. |
| 1533 | */ |
| 1534 | return IPV6_ADDR_SCOPE_NODELOCAL; |
| 1535 | } |
| 1536 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1537 | |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1538 | /* These macros are modelled after the ones in <netinet/in6.h>. */ |
| 1539 | |
| 1540 | /* RFC 4380, section 2.6 */ |
| 1541 | #define IN6_IS_ADDR_TEREDO(a) \ |
| 1542 | ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == ntohl(0x20010000))) |
| 1543 | |
| 1544 | /* RFC 3056, section 2. */ |
| 1545 | #define IN6_IS_ADDR_6TO4(a) \ |
| 1546 | (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02)) |
| 1547 | |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1548 | /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */ |
| 1549 | #define IN6_IS_ADDR_6BONE(a) \ |
| 1550 | (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe)) |
| 1551 | |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1552 | /* |
| 1553 | * Get the label for a given IPv4/IPv6 address. |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1554 | * RFC 6724, section 2.1. |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1555 | */ |
| 1556 | |
| 1557 | /*ARGSUSED*/ |
| 1558 | static int |
| 1559 | _get_label(const struct sockaddr *addr) |
| 1560 | { |
| 1561 | if (addr->sa_family == AF_INET) { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1562 | return 4; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1563 | } else if (addr->sa_family == AF_INET6) { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1564 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1565 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1566 | return 0; |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1567 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1568 | return 4; |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1569 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
| 1570 | return 2; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1571 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
| 1572 | return 5; |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1573 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1574 | return 13; |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1575 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1576 | return 3; |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1577 | } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) { |
| 1578 | return 11; |
| 1579 | } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
| 1580 | return 12; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1581 | } else { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1582 | /* All other IPv6 addresses, including global unicast addresses. */ |
| 1583 | return 1; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1584 | } |
| 1585 | } else { |
| 1586 | /* |
| 1587 | * This should never happen. |
| 1588 | * Return a semi-random label as a last resort. |
| 1589 | */ |
| 1590 | return 1; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * Get the precedence for a given IPv4/IPv6 address. |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1596 | * RFC 6724, section 2.1. |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1597 | */ |
| 1598 | |
| 1599 | /*ARGSUSED*/ |
| 1600 | static int |
| 1601 | _get_precedence(const struct sockaddr *addr) |
| 1602 | { |
| 1603 | if (addr->sa_family == AF_INET) { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1604 | return 35; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1605 | } else if (addr->sa_family == AF_INET6) { |
| 1606 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 1607 | if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) { |
| 1608 | return 50; |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1609 | } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1610 | return 35; |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1611 | } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1612 | return 30; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1613 | } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1614 | return 5; |
| 1615 | } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) { |
| 1616 | return 3; |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1617 | } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) || |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 1618 | IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) || |
| 1619 | IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) { |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1620 | return 1; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1621 | } else { |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1622 | /* All other IPv6 addresses, including global unicast addresses. */ |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1623 | return 40; |
| 1624 | } |
| 1625 | } else { |
Steinar H. Gunderson | 2e23e29 | 2010-12-20 11:48:07 +0100 | [diff] [blame] | 1626 | return 1; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * Find number of matching initial bits between the two addresses a1 and a2. |
| 1632 | */ |
| 1633 | |
| 1634 | /*ARGSUSED*/ |
| 1635 | static int |
| 1636 | _common_prefix_len(const struct in6_addr *a1, const struct in6_addr *a2) |
| 1637 | { |
| 1638 | const char *p1 = (const char *)a1; |
| 1639 | const char *p2 = (const char *)a2; |
| 1640 | unsigned i; |
| 1641 | |
| 1642 | for (i = 0; i < sizeof(*a1); ++i) { |
| 1643 | int x, j; |
| 1644 | |
| 1645 | if (p1[i] == p2[i]) { |
| 1646 | continue; |
| 1647 | } |
| 1648 | x = p1[i] ^ p2[i]; |
| 1649 | for (j = 0; j < CHAR_BIT; ++j) { |
| 1650 | if (x & (1 << (CHAR_BIT - 1))) { |
| 1651 | return i * CHAR_BIT + j; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1652 | } |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1653 | x <<= 1; |
| 1654 | } |
| 1655 | } |
| 1656 | return sizeof(*a1) * CHAR_BIT; |
| 1657 | } |
| 1658 | |
| 1659 | /* |
| 1660 | * Compare two source/destination address pairs. |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1661 | * RFC 6724, section 6. |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1662 | */ |
| 1663 | |
| 1664 | /*ARGSUSED*/ |
| 1665 | static int |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1666 | _rfc6724_compare(const void *ptr1, const void* ptr2) |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1667 | { |
| 1668 | const struct addrinfo_sort_elem *a1 = (const struct addrinfo_sort_elem *)ptr1; |
| 1669 | const struct addrinfo_sort_elem *a2 = (const struct addrinfo_sort_elem *)ptr2; |
| 1670 | int scope_src1, scope_dst1, scope_match1; |
| 1671 | int scope_src2, scope_dst2, scope_match2; |
| 1672 | int label_src1, label_dst1, label_match1; |
| 1673 | int label_src2, label_dst2, label_match2; |
| 1674 | int precedence1, precedence2; |
| 1675 | int prefixlen1, prefixlen2; |
| 1676 | |
| 1677 | /* Rule 1: Avoid unusable destinations. */ |
| 1678 | if (a1->has_src_addr != a2->has_src_addr) { |
| 1679 | return a2->has_src_addr - a1->has_src_addr; |
| 1680 | } |
| 1681 | |
| 1682 | /* Rule 2: Prefer matching scope. */ |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 1683 | scope_src1 = _get_scope(&a1->src_addr.generic); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1684 | scope_dst1 = _get_scope(a1->ai->ai_addr); |
| 1685 | scope_match1 = (scope_src1 == scope_dst1); |
| 1686 | |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 1687 | scope_src2 = _get_scope(&a2->src_addr.generic); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1688 | scope_dst2 = _get_scope(a2->ai->ai_addr); |
| 1689 | scope_match2 = (scope_src2 == scope_dst2); |
| 1690 | |
| 1691 | if (scope_match1 != scope_match2) { |
| 1692 | return scope_match2 - scope_match1; |
| 1693 | } |
| 1694 | |
| 1695 | /* |
| 1696 | * Rule 3: Avoid deprecated addresses. |
| 1697 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1698 | */ |
| 1699 | |
| 1700 | /* |
| 1701 | * Rule 4: Prefer home addresses. |
| 1702 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1703 | */ |
| 1704 | |
| 1705 | /* Rule 5: Prefer matching label. */ |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 1706 | label_src1 = _get_label(&a1->src_addr.generic); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1707 | label_dst1 = _get_label(a1->ai->ai_addr); |
| 1708 | label_match1 = (label_src1 == label_dst1); |
| 1709 | |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 1710 | label_src2 = _get_label(&a2->src_addr.generic); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1711 | label_dst2 = _get_label(a2->ai->ai_addr); |
| 1712 | label_match2 = (label_src2 == label_dst2); |
| 1713 | |
| 1714 | if (label_match1 != label_match2) { |
| 1715 | return label_match2 - label_match1; |
| 1716 | } |
| 1717 | |
| 1718 | /* Rule 6: Prefer higher precedence. */ |
| 1719 | precedence1 = _get_precedence(a1->ai->ai_addr); |
| 1720 | precedence2 = _get_precedence(a2->ai->ai_addr); |
| 1721 | if (precedence1 != precedence2) { |
| 1722 | return precedence2 - precedence1; |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * Rule 7: Prefer native transport. |
| 1727 | * TODO(sesse): We don't currently have a good way of finding this. |
| 1728 | */ |
| 1729 | |
| 1730 | /* Rule 8: Prefer smaller scope. */ |
| 1731 | if (scope_dst1 != scope_dst2) { |
| 1732 | return scope_dst1 - scope_dst2; |
| 1733 | } |
| 1734 | |
| 1735 | /* |
| 1736 | * Rule 9: Use longest matching prefix. |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1737 | * We implement this for IPv6 only, as the rules in RFC 6724 don't seem |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1738 | * to work very well directly applied to IPv4. (glibc uses information from |
| 1739 | * the routing table for a custom IPv4 implementation here.) |
| 1740 | */ |
| 1741 | if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && |
| 1742 | a2->has_src_addr && a2->ai->ai_addr->sa_family == AF_INET6) { |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 1743 | const struct sockaddr_in6 *a1_src = &a1->src_addr.in6; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1744 | const struct sockaddr_in6 *a1_dst = (const struct sockaddr_in6 *)a1->ai->ai_addr; |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 1745 | const struct sockaddr_in6 *a2_src = &a2->src_addr.in6; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1746 | const struct sockaddr_in6 *a2_dst = (const struct sockaddr_in6 *)a2->ai->ai_addr; |
| 1747 | prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr); |
Kenny Root | 7e0bfb5 | 2010-03-24 18:06:20 -0700 | [diff] [blame] | 1748 | prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1749 | if (prefixlen1 != prefixlen2) { |
| 1750 | return prefixlen2 - prefixlen1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1751 | } |
| 1752 | } |
| 1753 | |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1754 | /* |
| 1755 | * Rule 10: Leave the order unchanged. |
| 1756 | * We need this since qsort() is not necessarily stable. |
| 1757 | */ |
| 1758 | return a1->original_order - a2->original_order; |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | * Find the source address that will be used if trying to connect to the given |
| 1763 | * address. src_addr must be large enough to hold a struct sockaddr_in6. |
| 1764 | * |
| 1765 | * Returns 1 if a source address was found, 0 if the address is unreachable, |
| 1766 | * and -1 if a fatal error occurred. If 0 or 1, the contents of src_addr are |
| 1767 | * undefined. |
| 1768 | */ |
| 1769 | |
| 1770 | /*ARGSUSED*/ |
| 1771 | static int |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1772 | _find_src_addr(const struct sockaddr *addr, struct sockaddr *src_addr, unsigned mark) |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1773 | { |
| 1774 | int sock; |
| 1775 | int ret; |
| 1776 | socklen_t len; |
| 1777 | |
| 1778 | switch (addr->sa_family) { |
| 1779 | case AF_INET: |
| 1780 | len = sizeof(struct sockaddr_in); |
| 1781 | break; |
| 1782 | case AF_INET6: |
| 1783 | len = sizeof(struct sockaddr_in6); |
| 1784 | break; |
| 1785 | default: |
| 1786 | /* No known usable source address for non-INET families. */ |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
Nick Kralevich | 1781ed7 | 2014-06-29 20:46:17 -0700 | [diff] [blame] | 1790 | sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1791 | if (sock == -1) { |
| 1792 | if (errno == EAFNOSUPPORT) { |
| 1793 | return 0; |
| 1794 | } else { |
| 1795 | return -1; |
| 1796 | } |
| 1797 | } |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1798 | if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) |
| 1799 | return 0; |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1800 | do { |
Paul Jensen | 31ad037 | 2014-05-29 16:28:30 -0400 | [diff] [blame] | 1801 | ret = __connect(sock, addr, len); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1802 | } while (ret == -1 && errno == EINTR); |
| 1803 | |
| 1804 | if (ret == -1) { |
| 1805 | close(sock); |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | if (getsockname(sock, src_addr, &len) == -1) { |
| 1810 | close(sock); |
| 1811 | return -1; |
| 1812 | } |
| 1813 | close(sock); |
| 1814 | return 1; |
| 1815 | } |
| 1816 | |
| 1817 | /* |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1818 | * Sort the linked list starting at sentinel->ai_next in RFC6724 order. |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1819 | * Will leave the list unchanged if an error occurs. |
| 1820 | */ |
| 1821 | |
| 1822 | /*ARGSUSED*/ |
| 1823 | static void |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1824 | _rfc6724_sort(struct addrinfo *list_sentinel, unsigned mark) |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1825 | { |
| 1826 | struct addrinfo *cur; |
| 1827 | int nelem = 0, i; |
| 1828 | struct addrinfo_sort_elem *elems; |
| 1829 | |
| 1830 | cur = list_sentinel->ai_next; |
| 1831 | while (cur) { |
| 1832 | ++nelem; |
| 1833 | cur = cur->ai_next; |
| 1834 | } |
| 1835 | |
| 1836 | elems = (struct addrinfo_sort_elem *)malloc(nelem * sizeof(struct addrinfo_sort_elem)); |
| 1837 | if (elems == NULL) { |
| 1838 | goto error; |
| 1839 | } |
| 1840 | |
| 1841 | /* |
| 1842 | * Convert the linked list to an array that also contains the candidate |
| 1843 | * source address for each destination address. |
| 1844 | */ |
| 1845 | for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) { |
| 1846 | int has_src_addr; |
| 1847 | assert(cur != NULL); |
| 1848 | elems[i].ai = cur; |
| 1849 | elems[i].original_order = i; |
| 1850 | |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1851 | has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1852 | if (has_src_addr == -1) { |
| 1853 | goto error; |
| 1854 | } |
| 1855 | elems[i].has_src_addr = has_src_addr; |
| 1856 | } |
| 1857 | |
| 1858 | /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */ |
Lorenzo Colitti | 378b0e1 | 2013-03-30 12:24:19 +0900 | [diff] [blame] | 1859 | qsort((void *)elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare); |
Steinar H. Gunderson | 9ab75d4 | 2010-02-11 15:44:55 +0100 | [diff] [blame] | 1860 | |
| 1861 | list_sentinel->ai_next = elems[0].ai; |
| 1862 | for (i = 0; i < nelem - 1; ++i) { |
| 1863 | elems[i].ai->ai_next = elems[i + 1].ai; |
| 1864 | } |
| 1865 | elems[nelem - 1].ai->ai_next = NULL; |
| 1866 | |
| 1867 | error: |
| 1868 | free(elems); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | /*ARGSUSED*/ |
| 1872 | static int |
| 1873 | _dns_getaddrinfo(void *rv, void *cb_data, va_list ap) |
| 1874 | { |
| 1875 | struct addrinfo *ai; |
| 1876 | querybuf *buf, *buf2; |
| 1877 | const char *name; |
| 1878 | const struct addrinfo *pai; |
| 1879 | struct addrinfo sentinel, *cur; |
| 1880 | struct res_target q, q2; |
| 1881 | res_state res; |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1882 | unsigned netid, mark; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1883 | |
| 1884 | name = va_arg(ap, char *); |
| 1885 | pai = va_arg(ap, const struct addrinfo *); |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1886 | netid = va_arg(ap, unsigned); |
| 1887 | mark = va_arg(ap, unsigned); |
David 'Digit' Turner | 5e56370 | 2009-05-05 15:50:24 +0200 | [diff] [blame] | 1888 | //fprintf(stderr, "_dns_getaddrinfo() name = '%s'\n", name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1889 | |
| 1890 | memset(&q, 0, sizeof(q)); |
| 1891 | memset(&q2, 0, sizeof(q2)); |
| 1892 | memset(&sentinel, 0, sizeof(sentinel)); |
| 1893 | cur = &sentinel; |
| 1894 | |
| 1895 | buf = malloc(sizeof(*buf)); |
| 1896 | if (buf == NULL) { |
| 1897 | h_errno = NETDB_INTERNAL; |
| 1898 | return NS_NOTFOUND; |
| 1899 | } |
| 1900 | buf2 = malloc(sizeof(*buf2)); |
| 1901 | if (buf2 == NULL) { |
| 1902 | free(buf); |
| 1903 | h_errno = NETDB_INTERNAL; |
| 1904 | return NS_NOTFOUND; |
| 1905 | } |
| 1906 | |
| 1907 | switch (pai->ai_family) { |
| 1908 | case AF_UNSPEC: |
| 1909 | /* prefer IPv6 */ |
| 1910 | q.name = name; |
| 1911 | q.qclass = C_IN; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1912 | q.answer = buf->buf; |
| 1913 | q.anslen = sizeof(buf->buf); |
Lorenzo Colitti | ba96e30 | 2011-01-14 12:26:05 -0800 | [diff] [blame] | 1914 | int query_ipv6 = 1, query_ipv4 = 1; |
| 1915 | if (pai->ai_flags & AI_ADDRCONFIG) { |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1916 | query_ipv6 = _have_ipv6(mark); |
| 1917 | query_ipv4 = _have_ipv4(mark); |
Lorenzo Colitti | ba96e30 | 2011-01-14 12:26:05 -0800 | [diff] [blame] | 1918 | } |
| 1919 | if (query_ipv6) { |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 1920 | q.qtype = T_AAAA; |
Lorenzo Colitti | ba96e30 | 2011-01-14 12:26:05 -0800 | [diff] [blame] | 1921 | if (query_ipv4) { |
| 1922 | q.next = &q2; |
| 1923 | q2.name = name; |
| 1924 | q2.qclass = C_IN; |
| 1925 | q2.qtype = T_A; |
| 1926 | q2.answer = buf2->buf; |
| 1927 | q2.anslen = sizeof(buf2->buf); |
| 1928 | } |
| 1929 | } else if (query_ipv4) { |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 1930 | q.qtype = T_A; |
Lorenzo Colitti | ba96e30 | 2011-01-14 12:26:05 -0800 | [diff] [blame] | 1931 | } else { |
| 1932 | free(buf); |
| 1933 | free(buf2); |
| 1934 | return NS_NOTFOUND; |
Lorenzo Colitti | 3d8f4ad | 2009-08-03 22:36:31 -0700 | [diff] [blame] | 1935 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1936 | break; |
| 1937 | case AF_INET: |
| 1938 | q.name = name; |
| 1939 | q.qclass = C_IN; |
| 1940 | q.qtype = T_A; |
| 1941 | q.answer = buf->buf; |
| 1942 | q.anslen = sizeof(buf->buf); |
| 1943 | break; |
| 1944 | case AF_INET6: |
| 1945 | q.name = name; |
| 1946 | q.qclass = C_IN; |
| 1947 | q.qtype = T_AAAA; |
| 1948 | q.answer = buf->buf; |
| 1949 | q.anslen = sizeof(buf->buf); |
| 1950 | break; |
| 1951 | default: |
| 1952 | free(buf); |
| 1953 | free(buf2); |
| 1954 | return NS_UNAVAIL; |
| 1955 | } |
| 1956 | |
| 1957 | res = __res_get_state(); |
| 1958 | if (res == NULL) { |
| 1959 | free(buf); |
| 1960 | free(buf2); |
| 1961 | return NS_NOTFOUND; |
| 1962 | } |
| 1963 | |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1964 | /* this just sets our netid val in the thread private data so we don't have to |
Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 1965 | * modify the api's all the way down to res_send.c's res_nsend. We could |
| 1966 | * fully populate the thread private data here, but if we get down there |
| 1967 | * and have a cache hit that would be wasted, so we do the rest there on miss |
| 1968 | */ |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 1969 | res_setnetid(res, netid); |
Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 1970 | res_setmark(res, mark); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1971 | if (res_searchN(name, &q, res) < 0) { |
| 1972 | __res_put_state(res); |
| 1973 | free(buf); |
| 1974 | free(buf2); |
| 1975 | return NS_NOTFOUND; |
| 1976 | } |
| 1977 | ai = getanswer(buf, q.n, q.name, q.qtype, pai); |
| 1978 | if (ai) { |
| 1979 | cur->ai_next = ai; |
| 1980 | while (cur && cur->ai_next) |
| 1981 | cur = cur->ai_next; |
| 1982 | } |
| 1983 | if (q.next) { |
| 1984 | ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); |
| 1985 | if (ai) |
| 1986 | cur->ai_next = ai; |
| 1987 | } |
| 1988 | free(buf); |
| 1989 | free(buf2); |
| 1990 | if (sentinel.ai_next == NULL) { |
| 1991 | __res_put_state(res); |
| 1992 | switch (h_errno) { |
| 1993 | case HOST_NOT_FOUND: |
| 1994 | return NS_NOTFOUND; |
| 1995 | case TRY_AGAIN: |
| 1996 | return NS_TRYAGAIN; |
| 1997 | default: |
| 1998 | return NS_UNAVAIL; |
| 1999 | } |
| 2000 | } |
| 2001 | |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 2002 | _rfc6724_sort(&sentinel, netid); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2003 | |
| 2004 | __res_put_state(res); |
| 2005 | |
| 2006 | *((struct addrinfo **)rv) = sentinel.ai_next; |
| 2007 | return NS_SUCCESS; |
| 2008 | } |
| 2009 | |
| 2010 | static void |
| 2011 | _sethtent(FILE **hostf) |
| 2012 | { |
| 2013 | |
| 2014 | if (!*hostf) |
Elliott Hughes | c674edb | 2014-08-26 15:56:54 -0700 | [diff] [blame] | 2015 | *hostf = fopen(_PATH_HOSTS, "re"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2016 | else |
| 2017 | rewind(*hostf); |
| 2018 | } |
| 2019 | |
| 2020 | static void |
| 2021 | _endhtent(FILE **hostf) |
| 2022 | { |
| 2023 | |
| 2024 | if (*hostf) { |
| 2025 | (void) fclose(*hostf); |
| 2026 | *hostf = NULL; |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | static struct addrinfo * |
| 2031 | _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai) |
| 2032 | { |
| 2033 | char *p; |
| 2034 | char *cp, *tname, *cname; |
| 2035 | struct addrinfo hints, *res0, *res; |
| 2036 | int error; |
| 2037 | const char *addr; |
| 2038 | char hostbuf[8*1024]; |
| 2039 | |
| 2040 | // fprintf(stderr, "_gethtent() name = '%s'\n", name); |
| 2041 | assert(name != NULL); |
| 2042 | assert(pai != NULL); |
| 2043 | |
Elliott Hughes | c674edb | 2014-08-26 15:56:54 -0700 | [diff] [blame] | 2044 | if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2045 | return (NULL); |
| 2046 | again: |
| 2047 | if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) |
| 2048 | return (NULL); |
| 2049 | if (*p == '#') |
| 2050 | goto again; |
| 2051 | if (!(cp = strpbrk(p, "#\n"))) |
| 2052 | goto again; |
| 2053 | *cp = '\0'; |
| 2054 | if (!(cp = strpbrk(p, " \t"))) |
| 2055 | goto again; |
| 2056 | *cp++ = '\0'; |
| 2057 | addr = p; |
| 2058 | /* if this is not something we're looking for, skip it. */ |
| 2059 | cname = NULL; |
| 2060 | while (cp && *cp) { |
| 2061 | if (*cp == ' ' || *cp == '\t') { |
| 2062 | cp++; |
| 2063 | continue; |
| 2064 | } |
| 2065 | if (!cname) |
| 2066 | cname = cp; |
| 2067 | tname = cp; |
| 2068 | if ((cp = strpbrk(cp, " \t")) != NULL) |
| 2069 | *cp++ = '\0'; |
| 2070 | // fprintf(stderr, "\ttname = '%s'", tname); |
| 2071 | if (strcasecmp(name, tname) == 0) |
| 2072 | goto found; |
| 2073 | } |
| 2074 | goto again; |
| 2075 | |
| 2076 | found: |
| 2077 | hints = *pai; |
| 2078 | hints.ai_flags = AI_NUMERICHOST; |
| 2079 | error = getaddrinfo(addr, NULL, &hints, &res0); |
| 2080 | if (error) |
| 2081 | goto again; |
| 2082 | for (res = res0; res; res = res->ai_next) { |
| 2083 | /* cover it up */ |
| 2084 | res->ai_flags = pai->ai_flags; |
| 2085 | |
| 2086 | if (pai->ai_flags & AI_CANONNAME) { |
| 2087 | if (get_canonname(pai, res, cname) != 0) { |
| 2088 | freeaddrinfo(res0); |
| 2089 | goto again; |
| 2090 | } |
| 2091 | } |
| 2092 | } |
| 2093 | return res0; |
| 2094 | } |
| 2095 | |
| 2096 | /*ARGSUSED*/ |
| 2097 | static int |
| 2098 | _files_getaddrinfo(void *rv, void *cb_data, va_list ap) |
| 2099 | { |
| 2100 | const char *name; |
| 2101 | const struct addrinfo *pai; |
| 2102 | struct addrinfo sentinel, *cur; |
| 2103 | struct addrinfo *p; |
| 2104 | FILE *hostf = NULL; |
| 2105 | |
| 2106 | name = va_arg(ap, char *); |
| 2107 | pai = va_arg(ap, struct addrinfo *); |
| 2108 | |
| 2109 | // fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name); |
| 2110 | memset(&sentinel, 0, sizeof(sentinel)); |
| 2111 | cur = &sentinel; |
| 2112 | |
| 2113 | _sethtent(&hostf); |
| 2114 | while ((p = _gethtent(&hostf, name, pai)) != NULL) { |
| 2115 | cur->ai_next = p; |
| 2116 | while (cur && cur->ai_next) |
| 2117 | cur = cur->ai_next; |
| 2118 | } |
| 2119 | _endhtent(&hostf); |
| 2120 | |
| 2121 | *((struct addrinfo **)rv) = sentinel.ai_next; |
| 2122 | if (sentinel.ai_next == NULL) |
| 2123 | return NS_NOTFOUND; |
| 2124 | return NS_SUCCESS; |
| 2125 | } |
| 2126 | |
| 2127 | /* resolver logic */ |
| 2128 | |
| 2129 | /* |
| 2130 | * Formulate a normal query, send, and await answer. |
| 2131 | * Returned answer is placed in supplied buffer "answer". |
| 2132 | * Perform preliminary check of answer, returning success only |
| 2133 | * if no error is indicated and the answer count is nonzero. |
| 2134 | * Return the size of the response on success, -1 on error. |
| 2135 | * Error number is left in h_errno. |
| 2136 | * |
| 2137 | * Caller must parse answer and determine whether it answers the question. |
| 2138 | */ |
| 2139 | static int |
| 2140 | res_queryN(const char *name, /* domain name */ struct res_target *target, |
| 2141 | res_state res) |
| 2142 | { |
| 2143 | u_char buf[MAXPACKET]; |
| 2144 | HEADER *hp; |
| 2145 | int n; |
| 2146 | struct res_target *t; |
| 2147 | int rcode; |
| 2148 | int ancount; |
| 2149 | |
| 2150 | assert(name != NULL); |
| 2151 | /* XXX: target may be NULL??? */ |
| 2152 | |
| 2153 | rcode = NOERROR; |
| 2154 | ancount = 0; |
| 2155 | |
| 2156 | for (t = target; t; t = t->next) { |
| 2157 | int class, type; |
| 2158 | u_char *answer; |
| 2159 | int anslen; |
| 2160 | |
| 2161 | hp = (HEADER *)(void *)t->answer; |
| 2162 | hp->rcode = NOERROR; /* default */ |
| 2163 | |
| 2164 | /* make it easier... */ |
| 2165 | class = t->qclass; |
| 2166 | type = t->qtype; |
| 2167 | answer = t->answer; |
| 2168 | anslen = t->anslen; |
| 2169 | #ifdef DEBUG |
| 2170 | if (res->options & RES_DEBUG) |
| 2171 | printf(";; res_nquery(%s, %d, %d)\n", name, class, type); |
| 2172 | #endif |
| 2173 | |
| 2174 | n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, |
| 2175 | buf, sizeof(buf)); |
| 2176 | #ifdef RES_USE_EDNS0 |
| 2177 | if (n > 0 && (res->options & RES_USE_EDNS0) != 0) |
| 2178 | n = res_nopt(res, n, buf, sizeof(buf), anslen); |
| 2179 | #endif |
| 2180 | if (n <= 0) { |
| 2181 | #ifdef DEBUG |
| 2182 | if (res->options & RES_DEBUG) |
| 2183 | printf(";; res_nquery: mkquery failed\n"); |
| 2184 | #endif |
| 2185 | h_errno = NO_RECOVERY; |
| 2186 | return n; |
| 2187 | } |
| 2188 | n = res_nsend(res, buf, n, answer, anslen); |
| 2189 | #if 0 |
| 2190 | if (n < 0) { |
| 2191 | #ifdef DEBUG |
| 2192 | if (res->options & RES_DEBUG) |
| 2193 | printf(";; res_query: send error\n"); |
| 2194 | #endif |
| 2195 | h_errno = TRY_AGAIN; |
| 2196 | return n; |
| 2197 | } |
| 2198 | #endif |
| 2199 | |
| 2200 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { |
| 2201 | rcode = hp->rcode; /* record most recent error */ |
| 2202 | #ifdef DEBUG |
| 2203 | if (res->options & RES_DEBUG) |
| 2204 | printf(";; rcode = %u, ancount=%u\n", hp->rcode, |
| 2205 | ntohs(hp->ancount)); |
| 2206 | #endif |
| 2207 | continue; |
| 2208 | } |
| 2209 | |
| 2210 | ancount += ntohs(hp->ancount); |
| 2211 | |
| 2212 | t->n = n; |
| 2213 | } |
| 2214 | |
| 2215 | if (ancount == 0) { |
| 2216 | switch (rcode) { |
| 2217 | case NXDOMAIN: |
| 2218 | h_errno = HOST_NOT_FOUND; |
| 2219 | break; |
| 2220 | case SERVFAIL: |
| 2221 | h_errno = TRY_AGAIN; |
| 2222 | break; |
| 2223 | case NOERROR: |
| 2224 | h_errno = NO_DATA; |
| 2225 | break; |
| 2226 | case FORMERR: |
| 2227 | case NOTIMP: |
| 2228 | case REFUSED: |
| 2229 | default: |
| 2230 | h_errno = NO_RECOVERY; |
| 2231 | break; |
| 2232 | } |
| 2233 | return -1; |
| 2234 | } |
| 2235 | return ancount; |
| 2236 | } |
| 2237 | |
| 2238 | /* |
| 2239 | * Formulate a normal query, send, and retrieve answer in supplied buffer. |
| 2240 | * Return the size of the response on success, -1 on error. |
| 2241 | * If enabled, implement search rules until answer or unrecoverable failure |
| 2242 | * is detected. Error code, if any, is left in h_errno. |
| 2243 | */ |
| 2244 | static int |
| 2245 | res_searchN(const char *name, struct res_target *target, res_state res) |
| 2246 | { |
| 2247 | const char *cp, * const *domain; |
| 2248 | HEADER *hp; |
| 2249 | u_int dots; |
| 2250 | int trailing_dot, ret, saved_herrno; |
| 2251 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; |
| 2252 | |
| 2253 | assert(name != NULL); |
| 2254 | assert(target != NULL); |
| 2255 | |
| 2256 | hp = (HEADER *)(void *)target->answer; /*XXX*/ |
| 2257 | |
| 2258 | errno = 0; |
| 2259 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ |
| 2260 | dots = 0; |
| 2261 | for (cp = name; *cp; cp++) |
| 2262 | dots += (*cp == '.'); |
| 2263 | trailing_dot = 0; |
| 2264 | if (cp > name && *--cp == '.') |
| 2265 | trailing_dot++; |
| 2266 | |
| 2267 | |
David 'Digit' Turner | 5e56370 | 2009-05-05 15:50:24 +0200 | [diff] [blame] | 2268 | //fprintf(stderr, "res_searchN() name = '%s'\n", name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2269 | |
| 2270 | /* |
| 2271 | * if there aren't any dots, it could be a user-level alias |
| 2272 | */ |
| 2273 | if (!dots && (cp = __hostalias(name)) != NULL) { |
| 2274 | ret = res_queryN(cp, target, res); |
| 2275 | return ret; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * If there are dots in the name already, let's just give it a try |
| 2280 | * 'as is'. The threshold can be set with the "ndots" option. |
| 2281 | */ |
| 2282 | saved_herrno = -1; |
| 2283 | if (dots >= res->ndots) { |
| 2284 | ret = res_querydomainN(name, NULL, target, res); |
| 2285 | if (ret > 0) |
| 2286 | return (ret); |
| 2287 | saved_herrno = h_errno; |
| 2288 | tried_as_is++; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * We do at least one level of search if |
| 2293 | * - there is no dot and RES_DEFNAME is set, or |
| 2294 | * - there is at least one dot, there is no trailing dot, |
| 2295 | * and RES_DNSRCH is set. |
| 2296 | */ |
| 2297 | if ((!dots && (res->options & RES_DEFNAMES)) || |
| 2298 | (dots && !trailing_dot && (res->options & RES_DNSRCH))) { |
| 2299 | int done = 0; |
| 2300 | |
Robert Greenwalt | e0805a9 | 2013-07-31 16:53:46 -0700 | [diff] [blame] | 2301 | /* Unfortunately we need to set stuff up before |
| 2302 | * the domain stuff is tried. Will have a better |
| 2303 | * fix after thread pools are used. |
| 2304 | */ |
Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 2305 | _resolv_populate_res_for_net(res); |
Robert Greenwalt | e0805a9 | 2013-07-31 16:53:46 -0700 | [diff] [blame] | 2306 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2307 | for (domain = (const char * const *)res->dnsrch; |
| 2308 | *domain && !done; |
| 2309 | domain++) { |
| 2310 | |
| 2311 | ret = res_querydomainN(name, *domain, target, res); |
| 2312 | if (ret > 0) |
| 2313 | return ret; |
| 2314 | |
| 2315 | /* |
| 2316 | * If no server present, give up. |
| 2317 | * If name isn't found in this domain, |
| 2318 | * keep trying higher domains in the search list |
| 2319 | * (if that's enabled). |
| 2320 | * On a NO_DATA error, keep trying, otherwise |
| 2321 | * a wildcard entry of another type could keep us |
| 2322 | * from finding this entry higher in the domain. |
| 2323 | * If we get some other error (negative answer or |
| 2324 | * server failure), then stop searching up, |
| 2325 | * but try the input name below in case it's |
| 2326 | * fully-qualified. |
| 2327 | */ |
| 2328 | if (errno == ECONNREFUSED) { |
| 2329 | h_errno = TRY_AGAIN; |
| 2330 | return -1; |
| 2331 | } |
| 2332 | |
| 2333 | switch (h_errno) { |
| 2334 | case NO_DATA: |
| 2335 | got_nodata++; |
| 2336 | /* FALLTHROUGH */ |
| 2337 | case HOST_NOT_FOUND: |
| 2338 | /* keep trying */ |
| 2339 | break; |
| 2340 | case TRY_AGAIN: |
| 2341 | if (hp->rcode == SERVFAIL) { |
| 2342 | /* try next search element, if any */ |
| 2343 | got_servfail++; |
| 2344 | break; |
| 2345 | } |
| 2346 | /* FALLTHROUGH */ |
| 2347 | default: |
| 2348 | /* anything else implies that we're done */ |
| 2349 | done++; |
| 2350 | } |
| 2351 | /* |
| 2352 | * if we got here for some reason other than DNSRCH, |
| 2353 | * we only wanted one iteration of the loop, so stop. |
| 2354 | */ |
| 2355 | if (!(res->options & RES_DNSRCH)) |
Lorenzo Colitti | b82532d | 2011-09-28 19:28:32 -0700 | [diff] [blame] | 2356 | done++; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | /* |
| 2361 | * if we have not already tried the name "as is", do that now. |
| 2362 | * note that we do this regardless of how many dots were in the |
| 2363 | * name or whether it ends with a dot. |
| 2364 | */ |
| 2365 | if (!tried_as_is) { |
| 2366 | ret = res_querydomainN(name, NULL, target, res); |
| 2367 | if (ret > 0) |
| 2368 | return ret; |
| 2369 | } |
| 2370 | |
| 2371 | /* |
| 2372 | * if we got here, we didn't satisfy the search. |
| 2373 | * if we did an initial full query, return that query's h_errno |
| 2374 | * (note that we wouldn't be here if that query had succeeded). |
| 2375 | * else if we ever got a nodata, send that back as the reason. |
| 2376 | * else send back meaningless h_errno, that being the one from |
| 2377 | * the last DNSRCH we did. |
| 2378 | */ |
| 2379 | if (saved_herrno != -1) |
| 2380 | h_errno = saved_herrno; |
| 2381 | else if (got_nodata) |
| 2382 | h_errno = NO_DATA; |
| 2383 | else if (got_servfail) |
| 2384 | h_errno = TRY_AGAIN; |
| 2385 | return -1; |
| 2386 | } |
| 2387 | |
| 2388 | /* |
| 2389 | * Perform a call on res_query on the concatenation of name and domain, |
| 2390 | * removing a trailing dot from name if domain is NULL. |
| 2391 | */ |
| 2392 | static int |
| 2393 | res_querydomainN(const char *name, const char *domain, |
| 2394 | struct res_target *target, res_state res) |
| 2395 | { |
| 2396 | char nbuf[MAXDNAME]; |
| 2397 | const char *longname = nbuf; |
| 2398 | size_t n, d; |
| 2399 | |
| 2400 | assert(name != NULL); |
| 2401 | /* XXX: target may be NULL??? */ |
| 2402 | |
| 2403 | #ifdef DEBUG |
| 2404 | if (res->options & RES_DEBUG) |
| 2405 | printf(";; res_querydomain(%s, %s)\n", |
| 2406 | name, domain?domain:"<Nil>"); |
| 2407 | #endif |
| 2408 | if (domain == NULL) { |
| 2409 | /* |
| 2410 | * Check for trailing '.'; |
| 2411 | * copy without '.' if present. |
| 2412 | */ |
| 2413 | n = strlen(name); |
| 2414 | if (n + 1 > sizeof(nbuf)) { |
| 2415 | h_errno = NO_RECOVERY; |
| 2416 | return -1; |
| 2417 | } |
| 2418 | if (n > 0 && name[--n] == '.') { |
| 2419 | strncpy(nbuf, name, n); |
| 2420 | nbuf[n] = '\0'; |
| 2421 | } else |
| 2422 | longname = name; |
| 2423 | } else { |
| 2424 | n = strlen(name); |
| 2425 | d = strlen(domain); |
| 2426 | if (n + 1 + d + 1 > sizeof(nbuf)) { |
| 2427 | h_errno = NO_RECOVERY; |
| 2428 | return -1; |
| 2429 | } |
| 2430 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); |
| 2431 | } |
| 2432 | return res_queryN(longname, target, res); |
| 2433 | } |