How to find whether connected network is IPv4 or IPv6 or dual stack in iOS code?

Hi


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?


In dual stack network, how CFStreamCreatePairWithSocketToHost API works, meaning API works on IPv4 or IPv6?


Thanks in advance.

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:

  1. it resolves the DNS name to get a set of

    A
    and
    AAAA
    records
  2. it filters those based on the current network configuration

  3. it sorts them based on the likelihood of a fast connection

  4. it tries to connect to the first one

  5. if that connection attempt takes too long, it simultaneously tries to connect to the others

  6. 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/

Thanks for information.


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.


Please provide if you have any reference links for writing address independent code to find connected network type.

Please provide if you have any reference links for writing address independent code to find connected network type.

That request doesn’t make sense. By definition, code can’t be “address independent” if the goal is to “find [the] connected network type”.

As I said before, trying to work out what type of network environment you’re working in is almost always the wrong approach. If you explain the high-level goal, why you need this information, I may be able point you down a better path.

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/

The goal is to use the services or call APIs from the library linked in the application based on the network environment. If network is IPv4 or dual, make use of library services. If network is IPv6 then skip calling services from the library and use other functionality in the application.

What service does this library provide? Why does it care about the specific type of IP connectivity in play?

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/

Library provides audio related transactions between client and server which is not working in IPv6 network (Tested in the IPv6 network as Apple suggested setup). So it is require to idenify the network environment before calling the library. If network environment is IPv6 then client makes alternative functionality.

Where does this library go wrong? That’s important because it might radically simplify this problem.

As I mentioned earlier, determining the network environment of the device as a whole is tricky. OTOH, determining the address family used by a particular connection is easy. For example, if this library is UDP based then it’ll probably use the standard two step connection setup process:

  1. resolve a DNS name to a set of IP addresses

  2. try to set up the audio stream to each IP address in turn

If, in step 1, you get back all IPv6 addresses, then you can bail out early and fall back to your alternative strategy.

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/

How to find whether connected network is IPv4 or IPv6 or dual stack in iOS code?
 
 
Q