how to sort the ip adresses returning from getaddrinfo() like /etc/gai.conf in linux

  1. This is happening Mac M1 Monterey OS .Environment supports both IPv4 and IPV6.
  2. When a http client calls gettaddrinfo() it is returning both IPv6,IPv4 IPs . first v6 IPs and then v4 IPs.
  3. We need to have a way to sort gettaddrinfo() output to get v4 ip first and then v6.
  4. We tried changing DNS order with scutil by putting v4 DNS first , but still getaddrInfo() listing v6 IPs first .
  5. In linux there is a way to control gettaddrinfo() o/p with /etc/gai.conf https://man7.org/linux/man-pages/man5/gai.conf.5.html . In Mac I did not find any option like this , scutil changing order DNS is not effective . can you tell us what is way to do this in MAC OSx ?

When a http client calls gettaddrinfo

Is this an HTTP client that you wrote?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I would not rely on getaddrinfo giving me back entries in any particular order. Even if I could, the code would be prone to breaking soon...

I have made a couple utility swift extensions that examine an address by looking into sockaddr_t and return whether the address is IPv6 or IPv5, whether it is link local, v4 mapped to v6, etc… and then sort entries into any particular order I need for a given case.

Set ai_family to AF_INET for IPv4 addresses, or to AF_INET6 for only IPv6, and try your tests again?

I would not rely on getaddrinfo giving me back entries in any particular order.

getaddrinfo, at least on Apple platforms, goes out of its way to return IP addresses in a reasonable order. However, this assumes you’re using a Happy Eyeballs client, so a bogus or unusable address won’t slow things down. If your client doesn’t implement Happy Eyeballs, you will run into problems on Apple platforms and elsewhere

Best practice on Apple platforms is to use a connect-by-name API, such as NWConnection from the Network framework. This implements Happy Eyeballs v2, per RFC 8305. It also implements a bunch of other smarts, such as VPN On Demand.

And that’s why I started out asking about the origin of the HTTP client. Because if this is code that skandukuru controls, the best option is to update that client to not use the old school resolve-then-connect approach that’s implicit in getaddrinfo.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi Quinn

getaddrinfo, at least on Apple platforms, goes out of its way to return IP addresses in a reasonable order.

Does this mean there is no option on Apple platforms to change order of getaddrinfo results? Is there any source code for getaddrinfo implementation we can take a look?

Does this mean there is no option on Apple platforms to change order of getaddrinfo results?

Not in the way that you expect.

Is there any source code for getaddrinfo implementation we can take a look?

Sure. I don’t have the exactly link handy but it’s somewhere in Libinfo. IIRC all the heavy lifting is done by DNSServiceGetAddrInfo, which you can find in mDNSResponder.

To reiterate, you need a Happy Eyeballs implementation to work reliably in the wide range of network environments that Apple devices find themselves in. And once you have that, the order of results returned by getaddrinfo is largely irrelevant. I explore this in more detail in TN3151 Choosing the right networking API.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

how to sort the ip adresses returning from getaddrinfo() like /etc/gai.conf in linux
 
 
Q