How to know if iOS device is connected to IPV4 or IPV6 network

Hi,


Our VoIP app ( targeting iOS 9.2+ ) uses an existing SIP library which we use on different platforms not just iOS. It uses POSIX sockets.

If the device is connected to IPV6 network we need to create IPV6 transport rather than IPV4. We dont feel

we need to have both transports up simulatanoeusly as the device is only connected on WiFi to one or the

other.


What would be the best way to determine which of IPV6 or IPV4 network the device is connected to before we

create sockets etc.


Thanks

Are you using SIP over UDP or TCP?

Are you making outgoing ‘connections’ to a remote peer? Or listening for incoming ‘connections’? [Yeah, connections doesn’t strictly make sense when you’re using UDP, but most UDP protocols do have that notion.]

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi, Yes the app supports SIP over both UDP and TCP so we create transports for both . The app will make

outgoing connections and also will be listening for incoming connections.


Thanks

For outgoing connections you should do the standard DNS dance:

  1. resolve the DNS name

  2. for each address you get back…

  3. try to connect (using IPv4 or IPv6 depending on the address)

  4. if that fails, move on to the next one

Once the connection is in place you:

  • know whether IPv4 or IPv6 was used

  • have the correct local address (always with TCP; with UDP, use a connected socket to get this)

  • can monitor the reachability of that local address use

    SCNetworkReachabilityCreateWithAddressPair

For TCP you might consider using NSStream and then extracting the file descriptor from that object once the connection is in place. NSStream uses the system’s connect-by-name infrastructure, which contains smarts that are hard to replicate otherwise.

For UDP you don’t have that option, in which case you’ll want to read up on Happy Eyeballs. Be aware that the system’s connect-by-name support is a step beyond that.

For incoming connections, bring up listening sockets (well, for UDP that means a bound socket) on both

INADDR_ANY
(IPv4) and
in6addr_any
(IPv6). Any other approach requires you to monitor the address list and that’s quite painful.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
How to know if iOS device is connected to IPV4 or IPV6 network
 
 
Q