Default destination address selection
Resolver APIs can return multiple IP addresses as a result of a host name query; however, many applications use only the first address returned to attempt a connection or to send a UDP datagram. Therefore, sorting of these IP addresses is performed by the default destination address selection algorithm.
Establishing connectivity can depend on whether an IPv6 address or an IPv4 address is selected, which makes this sorting function even more important.
Default destination address selection occurs only when the system is enabled for IPv6 and the application is using the getaddrinfo() API to retrieve IPv6 addresses, IPv4 addresses, or both.
The default destination address selection algorithm sorts a list of destination addresses and generates a new list. The algorithm sorts together both IPv6 and IPv4 addresses by a set of rules. Rules are applied, in order, to the first and second address, choosing a best address. Rules are then applied to this best address and the third address. This continues until rules have been applied to all addresses and the entire list of addresses has been sorted. If one of the rules is able to select the best address between two addresses, remaining rules are bypassed for those two addresses. Subsequent rules act as tie-breakers for earlier rules.
The destination address selection algorithm attempts to predict what source address is selected by TCP/IP when the application initiates an outbound connection or sends a datagram by using the destination address. This source address is used for some of the destination address selection criteria rules. Source address prediction processing assumes that the application does not explicitly specify a source IP address (by using bind or ipv6_pktinfo) when it is initiating a connection or sending a datagram. If the application explicitly specifies a source address, then the destination address that this algorithm selects might not be optimal. The decision the algorithm makes might assume that a different source address is used.
- Avoid unusable destinations.
If one address is reachable (the stack has a route to the particular address) and the other is unreachable, then place the reachable destination address before the unreachable address.
- Prefer matching scope.
If the scope of one address matches the scope of its source address and the other address does not meet this criteria, then the address with the matching scope is placed before the other destination address.
The scopes of the destination addresses and their associated source addresses are determined by the high-order bits of the address. The destination address can be a multicast or unicast address. To compare scope, unicast link-local addresses are mapped to multicast link-local addresses and unicast global addresses are mapped to multicast global addresses.
- Avoid deprecated addresses.
If one address is deprecated and the other is non-deprecated, then the non-deprecated address is placed before the other address.
- Prefer matching label.
If the label of one destination address matches the label of its associated source address and the label of the other destination address does not match the label of its associated source address, then the destination with the matching label is placed before the other address.
See Policy table for IPv6 default address selection and Configuring the policy table for default address selection for information about how labels are associated with destination addresses.
- Prefer higher precedence.
If the precedence of one address is higher than the precedence of the other address, then the address with the higher precedence is placed before the other destination address.
See Policy table for IPv6 default address selection and Configuring the policy table for default address selection for more information about how precedence values are associated with destination addresses.
- Prefer smaller scope.
If the scope of one address is smaller than the scope of the other address, the address with the smaller scope is placed before the other destination address.
- Use the longest matching prefix.
If one destination address has a longer CommonPrefixLength with its associated source address than the other destination address has with its source address, then the address with the longer CommonPrefixLength is placed before the other address.
- Leave the order unchanged.
No rule selected a better address of these two addresses; they are equally good. Choose the first address as the better address of these two and the order is not changed.