I want to read all the addresses(IPv4 and IPv6) using getaddrinfo(). My question is whether getaddrinfo() returns IPv6 address followed by IPv4 in the list. Assuming that I'm passing AF_UNSPEC to getaddrinfo() and using dual stack.
3 Answers
The answer is here:
https://www.rfc-editor.org/rfc/rfc6724
Operating systems should (and mostly do) follow this standard or the previous version of it. While getaddrinfo()
chooses the destination address, the kernel then selects the source address if the application doesn't bind to a specific one.
By default, global native IPv6 addresses are preferred over all IPv4 addresses.
That actually depends on the name server and OS. getaddrinfo() fetches the dns resolution info from the name server. If your name server orders IPv6 addresses before IPv4 addresses it will return IPv6 addresses before IPv4 addresses.
By default IPv6 addresses has better priority than IPv4 addresses. @Pavel Simerda wrote- "By default, global native IPv6 addresses are preferred over all IPv4 addresses."
getaddrinfo()
implementations actually follow this. The rules require predicting which source address the kernel would use to reach a particular destination address, which in turn depends on querying the kernel to get a list of source addresses that are actually available and finding out how the kernel's source address selection algorithm has been customized. Doesgetaddrinfo()
actually go to these lengths? - Celada