struct sort_result results[2];
+ size_t order[2];
results[0].dest_addr = &ai1;
results[0].got_source_addr = true;
results[0].source_addr_len = sizeof (so1);
results[0].source_addr_flags = 0;
- results[0].service_order = 0;
results[0].prefixlen = 16;
results[0].index = 0;
memcpy (&results[0].source_addr, &so1, sizeof (so1));
+ order[0] = 0;
results[1].dest_addr = &ai2;
results[1].got_source_addr = true;
results[1].source_addr_len = sizeof (so2);
results[1].source_addr_flags = 0;
- results[1].service_order = 1;
results[1].prefixlen = 16;
results[1].index = 0;
memcpy (&results[1].source_addr, &so2, sizeof (so2));
+ order[1] = 1;
struct sort_result_combo combo = { .results = results, .nresults = 2 };
- qsort_r (results, 2, sizeof (results[0]), rfc3484_sort, &combo);
+ qsort_r (order, 2, sizeof (order[0]), rfc3484_sort, &combo);
int result = 0;
- if (results[0].dest_addr->ai_family == AF_INET6)
+ if (results[order[0]].dest_addr->ai_family == AF_INET6)
{
puts ("wrong order in first test");
result |= 1;
results[1].got_source_addr = true;
results[1].source_addr_len = sizeof (so1);
results[1].source_addr_flags = 0;
- results[1].service_order = 1;
results[1].prefixlen = 16;
results[1].index = 0;
memcpy (&results[1].source_addr, &so1, sizeof (so1));
+ order[1] = 1;
results[0].dest_addr = &ai2;
results[0].got_source_addr = true;
results[0].source_addr_len = sizeof (so2);
results[0].source_addr_flags = 0;
- results[0].service_order = 0;
results[0].prefixlen = 16;
results[0].index = 0;
memcpy (&results[0].source_addr, &so2, sizeof (so2));
+ order[0] = 0;
- qsort_r (results, 2, sizeof (results[0]), rfc3484_sort, &combo);
+ qsort_r (order, 2, sizeof (order[0]), rfc3484_sort, &combo);
- if (results[0].dest_addr->ai_family == AF_INET6)
+ if (results[order[0]].dest_addr->ai_family == AF_INET6)
{
puts ("wrong order in second test");
result |= 1;
struct sort_result
{
struct addrinfo *dest_addr;
- struct sockaddr_storage source_addr;
- size_t service_order;
+ /* Using sockaddr_storage is for now overkill. We only support IPv4
+ and IPv6 so far. If this changes at some point we can adjust the
+ type here. */
+ struct sockaddr_in6 source_addr;
uint8_t source_addr_len;
bool got_source_addr;
uint8_t source_addr_flags;
static int
-get_scope (const struct sockaddr_storage *ss)
+get_scope (const struct sockaddr_in6 *in6)
{
int scope;
- if (ss->ss_family == PF_INET6)
+ if (in6->sin6_family == PF_INET6)
{
- const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) ss;
-
if (! IN6_IS_ADDR_MULTICAST (&in6->sin6_addr))
{
if (IN6_IS_ADDR_LINKLOCAL (&in6->sin6_addr))
else
scope = in6->sin6_addr.s6_addr[1] & 0xf;
}
- else if (ss->ss_family == PF_INET)
+ else if (in6->sin6_family == PF_INET)
{
- const struct sockaddr_in *in = (const struct sockaddr_in *) ss;
+ const struct sockaddr_in *in = (const struct sockaddr_in *) in6;
size_t cnt = 0;
while (1)
static int
-match_prefix (const struct sockaddr_storage *ss,
+match_prefix (const struct sockaddr_in6 *in6,
const struct prefixentry *list, int default_val)
{
int idx;
struct sockaddr_in6 in6_mem;
- const struct sockaddr_in6 *in6;
- if (ss->ss_family == PF_INET6)
- in6 = (const struct sockaddr_in6 *) ss;
- else if (ss->ss_family == PF_INET)
+ if (in6->sin6_family == PF_INET)
{
- const struct sockaddr_in *in = (const struct sockaddr_in *) ss;
+ const struct sockaddr_in *in = (const struct sockaddr_in *) in6;
/* Convert to IPv6 address. */
in6_mem.sin6_family = PF_INET6;
in6 = &in6_mem;
}
- else
+ else if (in6->sin6_family != PF_INET6)
return default_val;
for (idx = 0; ; ++idx)
static int
-get_label (const struct sockaddr_storage *ss)
+get_label (const struct sockaddr_in6 *in6)
{
/* XXX What is a good default value? */
- return match_prefix (ss, labels, INT_MAX);
+ return match_prefix (in6, labels, INT_MAX);
}
static int
-get_precedence (const struct sockaddr_storage *ss)
+get_precedence (const struct sockaddr_in6 *in6)
{
/* XXX What is a good default value? */
- return match_prefix (ss, precedence, 0);
+ return match_prefix (in6, precedence, 0);
}
static int
rfc3484_sort (const void *p1, const void *p2, void *arg)
{
- const struct sort_result *a1 = (const struct sort_result *) p1;
- const struct sort_result *a2 = (const struct sort_result *) p2;
+ const size_t idx1 = *(const size_t *) p1;
+ const size_t idx2 = *(const size_t *) p2;
struct sort_result_combo *src = (struct sort_result_combo *) arg;
+ struct sort_result *a1 = &src->results[idx1];
+ struct sort_result *a2 = &src->results[idx2];
/* Rule 1: Avoid unusable destinations.
We have the got_source_addr flag set if the destination is reachable. */
/* Rule 2: Prefer matching scope. Only interesting if both
destination addresses are IPv6. */
int a1_dst_scope
- = get_scope ((struct sockaddr_storage *) a1->dest_addr->ai_addr);
+ = get_scope ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
int a2_dst_scope
- = get_scope ((struct sockaddr_storage *) a2->dest_addr->ai_addr);
+ = get_scope ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
if (a1->got_source_addr)
{
if (a1->got_source_addr)
{
int a1_dst_label
- = get_label ((struct sockaddr_storage *) a1->dest_addr->ai_addr);
+ = get_label ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
int a1_src_label = get_label (&a1->source_addr);
int a2_dst_label
- = get_label ((struct sockaddr_storage *) a2->dest_addr->ai_addr);
+ = get_label ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
int a2_src_label = get_label (&a2->source_addr);
if (a1_dst_label == a1_src_label && a2_dst_label != a2_src_label)
/* Rule 6: Prefer higher precedence. */
int a1_prec
- = get_precedence ((struct sockaddr_storage *) a1->dest_addr->ai_addr);
+ = get_precedence ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
int a2_prec
- = get_precedence ((struct sockaddr_storage *) a2->dest_addr->ai_addr);
+ = get_precedence ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
if (a1_prec > a2_prec)
return -1;
if (a1->dest_addr->ai_family == PF_INET)
{
- assert (a1->source_addr.ss_family == PF_INET);
- assert (a2->source_addr.ss_family == PF_INET);
+ assert (a1->source_addr.sin6_family == PF_INET);
+ assert (a2->source_addr.sin6_family == PF_INET);
/* Outside of subnets, as defined by the network masks,
common address prefixes for IPv4 addresses make no sense.
}
else if (a1->dest_addr->ai_family == PF_INET6)
{
- assert (a1->source_addr.ss_family == PF_INET6);
- assert (a2->source_addr.ss_family == PF_INET6);
+ assert (a1->source_addr.sin6_family == PF_INET6);
+ assert (a2->source_addr.sin6_family == PF_INET6);
struct sockaddr_in6 *in1_dst;
struct sockaddr_in6 *in1_src;
compare with the value indicating the order in which the entries
have been received from the services. NB: no two entries can have
the same order so the test will never return zero. */
- return a1->service_order < a2->service_order ? -1 : 1;
+ return idx1 < idx2 ? -1 : 1;
}
__libc_once (once, gaiconf_init);
/* Sort results according to RFC 3484. */
struct sort_result results[nresults];
+ size_t order[nresults];
struct addrinfo *q;
struct addrinfo *last = NULL;
char *canonname = NULL;
for (i = 0, q = p; q != NULL; ++i, last = q, q = q->ai_next)
{
results[i].dest_addr = q;
- results[i].service_order = i;
results[i].native = -1;
+ order[i] = i;
/* If we just looked up the address for a different
protocol, reuse the result. */
__libc_lock_lock (lock);
if (old_once && gaiconf_reload_flag)
gaiconf_reload ();
- qsort_r (results, nresults, sizeof (results[0]), rfc3484_sort, &src);
+ qsort_r (order, nresults, sizeof (order[0]), rfc3484_sort, &src);
__libc_lock_unlock (lock);
}
else
- qsort_r (results, nresults, sizeof (results[0]), rfc3484_sort, &src);
+ qsort_r (order, nresults, sizeof (order[0]), rfc3484_sort, &src);
/* Queue the results up as they come out of sorting. */
- q = p = results[0].dest_addr;
+ q = p = results[order[0]].dest_addr;
for (i = 1; i < nresults; ++i)
- q = q->ai_next = results[i].dest_addr;
+ q = q->ai_next = results[order[i]].dest_addr;
q->ai_next = NULL;
/* Fill in the canonical name into the new first entry. */