2 * Original file name getaddrinfo.c
3 * Lifted from the 'Android Bionic' project with the BSD license.
7 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Copyright (C) 2018 The Android Open Source Project
9 * Copyright (C) 2019 by Andrew Selivanov
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the project nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "ares_setup.h"
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
53 #include "ares_private.h"
55 struct addrinfo_sort_elem
57 struct ares_addrinfo_node *ai;
59 ares_sockaddr src_addr;
63 #define ARES_IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
65 #define ARES_IPV6_ADDR_SCOPE_NODELOCAL 0x01
66 #define ARES_IPV6_ADDR_SCOPE_INTFACELOCAL 0x01
67 #define ARES_IPV6_ADDR_SCOPE_LINKLOCAL 0x02
68 #define ARES_IPV6_ADDR_SCOPE_SITELOCAL 0x05
69 #define ARES_IPV6_ADDR_SCOPE_ORGLOCAL 0x08
70 #define ARES_IPV6_ADDR_SCOPE_GLOBAL 0x0e
72 #define ARES_IN_LOOPBACK(a) ((((long int)(a)) & 0xff000000) == 0x7f000000)
75 #define ARES_IN6_IS_ADDR_ULA(a) (((a)->s6_addr[0] & 0xfe) == 0xfc)
77 /* These macros are modelled after the ones in <netinet/in6.h>. */
78 /* RFC 4380, section 2.6 */
79 #define ARES_IN6_IS_ADDR_TEREDO(a) \
80 ((*(const unsigned int *)(const void *)(&(a)->s6_addr[0]) == ntohl(0x20010000)))
81 /* RFC 3056, section 2. */
82 #define ARES_IN6_IS_ADDR_6TO4(a) \
83 (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
84 /* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
85 #define ARES_IN6_IS_ADDR_6BONE(a) \
86 (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
89 static int get_scope(const struct sockaddr *addr)
91 if (addr->sa_family == AF_INET6)
93 const struct sockaddr_in6 *addr6 = CARES_INADDR_CAST(const struct sockaddr_in6 *, addr);
94 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr))
96 return ARES_IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
98 else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
99 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr))
102 * RFC 4291 section 2.5.3 says loopback is to be treated as having
105 return ARES_IPV6_ADDR_SCOPE_LINKLOCAL;
107 else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr))
109 return ARES_IPV6_ADDR_SCOPE_SITELOCAL;
113 return ARES_IPV6_ADDR_SCOPE_GLOBAL;
116 else if (addr->sa_family == AF_INET)
118 const struct sockaddr_in *addr4 = CARES_INADDR_CAST(const struct sockaddr_in *, addr);
119 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
120 if (ARES_IN_LOOPBACK(na) || /* 127.0.0.0/8 */
121 (na & 0xffff0000) == 0xa9fe0000) /* 169.254.0.0/16 */
123 return ARES_IPV6_ADDR_SCOPE_LINKLOCAL;
128 * RFC 6724 section 3.2. Other IPv4 addresses, including private
129 * addresses and shared addresses (100.64.0.0/10), are assigned global
132 return ARES_IPV6_ADDR_SCOPE_GLOBAL;
138 * This should never happen.
139 * Return a scope with low priority as a last resort.
141 return ARES_IPV6_ADDR_SCOPE_NODELOCAL;
145 static int get_label(const struct sockaddr *addr)
147 if (addr->sa_family == AF_INET)
151 else if (addr->sa_family == AF_INET6)
153 const struct sockaddr_in6 *addr6 = CARES_INADDR_CAST(const struct sockaddr_in6 *, addr);
154 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr))
158 else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr))
162 else if (ARES_IN6_IS_ADDR_6TO4(&addr6->sin6_addr))
166 else if (ARES_IN6_IS_ADDR_TEREDO(&addr6->sin6_addr))
170 else if (ARES_IN6_IS_ADDR_ULA(&addr6->sin6_addr))
174 else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr))
178 else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr))
182 else if (ARES_IN6_IS_ADDR_6BONE(&addr6->sin6_addr))
188 /* All other IPv6 addresses, including global unicast addresses. */
195 * This should never happen.
196 * Return a semi-random label as a last resort.
203 * Get the precedence for a given IPv4/IPv6 address.
204 * RFC 6724, section 2.1.
206 static int get_precedence(const struct sockaddr *addr)
208 if (addr->sa_family == AF_INET)
212 else if (addr->sa_family == AF_INET6)
214 const struct sockaddr_in6 *addr6 = CARES_INADDR_CAST(const struct sockaddr_in6 *, addr);
215 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr))
219 else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr))
223 else if (ARES_IN6_IS_ADDR_6TO4(&addr6->sin6_addr))
227 else if (ARES_IN6_IS_ADDR_TEREDO(&addr6->sin6_addr))
231 else if (ARES_IN6_IS_ADDR_ULA(&addr6->sin6_addr))
235 else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
236 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
237 ARES_IN6_IS_ADDR_6BONE(&addr6->sin6_addr))
243 /* All other IPv6 addresses, including global unicast addresses. */
254 * Find number of matching initial bits between the two addresses a1 and a2.
256 static int common_prefix_len(const struct in6_addr *a1,
257 const struct in6_addr *a2)
259 const char *p1 = (const char *)a1;
260 const char *p2 = (const char *)a2;
262 for (i = 0; i < sizeof(*a1); ++i)
270 for (j = 0; j < CHAR_BIT; ++j)
272 if (x & (1 << (CHAR_BIT - 1)))
274 return i * CHAR_BIT + j;
279 return sizeof(*a1) * CHAR_BIT;
283 * Compare two source/destination address pairs.
284 * RFC 6724, section 6.
286 static int rfc6724_compare(const void *ptr1, const void *ptr2)
288 const struct addrinfo_sort_elem *a1 = (const struct addrinfo_sort_elem *)ptr1;
289 const struct addrinfo_sort_elem *a2 = (const struct addrinfo_sort_elem *)ptr2;
290 int scope_src1, scope_dst1, scope_match1;
291 int scope_src2, scope_dst2, scope_match2;
292 int label_src1, label_dst1, label_match1;
293 int label_src2, label_dst2, label_match2;
294 int precedence1, precedence2;
295 int prefixlen1, prefixlen2;
297 /* Rule 1: Avoid unusable destinations. */
298 if (a1->has_src_addr != a2->has_src_addr)
300 return a2->has_src_addr - a1->has_src_addr;
303 /* Rule 2: Prefer matching scope. */
304 scope_src1 = ARES_IPV6_ADDR_SCOPE_NODELOCAL;
305 if (a1->has_src_addr)
306 scope_src1 = get_scope(&a1->src_addr.sa);
307 scope_dst1 = get_scope(a1->ai->ai_addr);
308 scope_match1 = (scope_src1 == scope_dst1);
310 scope_src2 = ARES_IPV6_ADDR_SCOPE_NODELOCAL;
311 if (a2->has_src_addr)
312 scope_src2 = get_scope(&a2->src_addr.sa);
313 scope_dst2 = get_scope(a2->ai->ai_addr);
314 scope_match2 = (scope_src2 == scope_dst2);
316 if (scope_match1 != scope_match2)
318 return scope_match2 - scope_match1;
321 /* Rule 3: Avoid deprecated addresses. */
323 /* Rule 4: Prefer home addresses. */
325 /* Rule 5: Prefer matching label. */
327 if (a1->has_src_addr)
328 label_src1 = get_label(&a1->src_addr.sa);
329 label_dst1 = get_label(a1->ai->ai_addr);
330 label_match1 = (label_src1 == label_dst1);
333 if (a2->has_src_addr)
334 label_src2 = get_label(&a2->src_addr.sa);
335 label_dst2 = get_label(a2->ai->ai_addr);
336 label_match2 = (label_src2 == label_dst2);
338 if (label_match1 != label_match2)
340 return label_match2 - label_match1;
343 /* Rule 6: Prefer higher precedence. */
344 precedence1 = get_precedence(a1->ai->ai_addr);
345 precedence2 = get_precedence(a2->ai->ai_addr);
346 if (precedence1 != precedence2)
348 return precedence2 - precedence1;
351 /* Rule 7: Prefer native transport. */
353 /* Rule 8: Prefer smaller scope. */
354 if (scope_dst1 != scope_dst2)
356 return scope_dst1 - scope_dst2;
359 /* Rule 9: Use longest matching prefix. */
360 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 &&
361 a2->has_src_addr && a2->ai->ai_addr->sa_family == AF_INET6)
363 const struct sockaddr_in6 *a1_src = &a1->src_addr.sa6;
364 const struct sockaddr_in6 *a1_dst =
365 CARES_INADDR_CAST(const struct sockaddr_in6 *, a1->ai->ai_addr);
366 const struct sockaddr_in6 *a2_src = &a2->src_addr.sa6;
367 const struct sockaddr_in6 *a2_dst =
368 CARES_INADDR_CAST(const struct sockaddr_in6 *, a2->ai->ai_addr);
369 prefixlen1 = common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
370 prefixlen2 = common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
371 if (prefixlen1 != prefixlen2)
373 return prefixlen2 - prefixlen1;
378 * Rule 10: Leave the order unchanged.
379 * We need this since qsort() is not necessarily stable.
381 return a1->original_order - a2->original_order;
385 * Find the source address that will be used if trying to connect to the given
388 * Returns 1 if a source address was found, 0 if the address is unreachable,
389 * and -1 if a fatal error occurred. If 0 or 1, the contents of src_addr are
392 static int find_src_addr(ares_channel channel,
393 const struct sockaddr *addr,
394 struct sockaddr *src_addr)
400 switch (addr->sa_family)
403 len = sizeof(struct sockaddr_in);
406 len = sizeof(struct sockaddr_in6);
409 /* No known usable source address for non-INET families. */
413 sock = ares__open_socket(channel, addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
414 if (sock == ARES_SOCKET_BAD)
416 if (errno == EAFNOSUPPORT)
428 ret = ares__connect_socket(channel, sock, addr, len);
430 while (ret == -1 && errno == EINTR);
434 ares__close_socket(channel, sock);
438 if (getsockname(sock, src_addr, &len) != 0)
440 ares__close_socket(channel, sock);
443 ares__close_socket(channel, sock);
448 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
449 * Will leave the list unchanged if an error occurs.
451 int ares__sortaddrinfo(ares_channel channel, struct ares_addrinfo_node *list_sentinel)
453 struct ares_addrinfo_node *cur;
456 struct addrinfo_sort_elem *elems;
458 cur = list_sentinel->ai_next;
468 elems = (struct addrinfo_sort_elem *)ares_malloc(
469 nelem * sizeof(struct addrinfo_sort_elem));
476 * Convert the linked list to an array that also contains the candidate
477 * source address for each destination address.
479 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next)
483 elems[i].original_order = i;
484 has_src_addr = find_src_addr(channel, cur->ai_addr, &elems[i].src_addr.sa);
485 if (has_src_addr == -1)
488 return ARES_ENOTFOUND;
490 elems[i].has_src_addr = has_src_addr;
493 /* Sort the addresses, and rearrange the linked list so it matches the sorted
495 qsort((void *)elems, nelem, sizeof(struct addrinfo_sort_elem),
498 list_sentinel->ai_next = elems[0].ai;
499 for (i = 0; i < nelem - 1; ++i)
501 elems[i].ai->ai_next = elems[i + 1].ai;
503 elems[nelem - 1].ai->ai_next = NULL;