In code, I want to know type of network (IPv4 or IPv6 or dual) connected, so that based on type of network need to handle functionality. Is there a way to achieve this?
You can call
getifaddrs
to get all the IP addresses associated with the machine, then group them by interface, then sort through them to decide which ones are link local and which ones are ‘real’.
IMPORTANT This is not fun code to write and I strongly recommend that you not go down this path. It’s much better to write address independent code.
In dual stack network, how CFStreamCreatePairWithSocketToHost API works, meaning API works on IPv4 or IPv6?
It’s quite complex. The basic idea is:
it resolves the DNS name to get a set of
A
and AAAA
recordsit filters those based on the current network configuration
it sorts them based on the likelihood of a fast connection
it tries to connect to the first one
if that connection attempt takes too long, it simultaneously tries to connect to the others
the first one that connects becomes the TCP connection that you see; the remaining are closed
I don’t understand this enough to describe the process in detail. It’s a combination of Happy Eyeballs (RFC 6555, which in turn references RFC 3484) and lots of Apple-specific tweaks. You can get some idea of those tweaks by reading this post from 2011.
IMPORTANT As you might expect, things have changed substantially since 2011. Don’t read this post as a description of the current system behaviour, but rather an indication of the lengths that our connect-by-name APIs go through to do the right thing here.
I would not try to replicate this technique in your own code. If you’re connecting to a DNS name, use one of our connect-by-name APIs. If you’re working at a level that makes that hard (for example, BSD Sockets) let me know what the road block is and I’ll follow up from there.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
WWDC runs Mon, 13 Jun through to Fri, 17 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/