How to get IP address of interface a packet will sent on on iOS

Hi,

We are developing a VoIP application on iOS using BSD sockets. We need to populate the SIP Via and Contact headers with the IP address that the SIP packet will be sent on to a particular destination. getifaddrs will present a list of interfaces and IP addresses but doesn't tell us which interface will be used.

Anyone know which APIs we should be using to get this information

Thanks

  • These BSD APIs were neutered years ago by apple for privacy reasons to avoid using an IP or mac address tracking a user as a replacement to the once available unique device identifier. You will have to find the IP address by some other means maybe having the server detect the incoming IP address.

Add a Comment

Replies

We need to populate the SIP Via and Contact headers with the IP address that the SIP packet will be sent on to a particular destination.

The standard way to do this in BSD Sockets is with a connected UDP socket. That is:

  1. Create your socket.

  2. Call connect with your destination IP address.

  3. Call getsockname to read back the source IP address.

Make sure you monitor these IP addresses for changes otherwise thing’ll fail in various common scenarios like:

  • Switch from Wi-Fi to WWAN or back

  • DHCP lease expiry

To monitor for changes in this specific case, use SCNetworkReachabilityCreateWithAddressPair.

Also, be aware that connecting a UDP socket means that an ICMP response can be reflected as errors on the socket. I’m not sure where that’s covered in Apple’s documentation; for this sort of thing I usually reach for my trusty copy of UNIX Network Programming.

http://www.unpbook.com

Finally, make sure your BSD Sockets code works in an IPv6-only environment.


MobileTen wrote:

These BSD APIs were neutered years ago by apple for privacy reasons to avoid using an IP or mac address tracking a user as a replacement to the once available unique device identifier.

This is only half right. It’s true that recent versions of iOS prevent apps from accessing hardware MAC addresses. However, that change does not apply to local IP addresses.

Share and Enjoy

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