tags:

views:

847

answers:

1

I have code that allows me to determine the MAC address and the IP address of the WiFi connection on the iPhone, but I can't figure out how to get the Subnet Mask and Router address for the connection. Can anyone point me in the right direction here?

+5  A: 

You can get that information by calling getifaddrs. (I use this function in an app of mine to figure out the iPhone's IP address.)

struct ifaddrs *ifa = NULL, *ifList;
getifaddrs(&ifList); // should check for errors
for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next) {
   ifa->ifa_addr // interface address
   ifa->ifa_netmask // subnet mask
   ifa->ifa_dstaddr // broadcast address, NOT router address
}
freeifaddrs(ifList); // clean up after yourself

This gets you the subnet mask; for the router address, see this question.

This is all old-school UNIX networking stuff, you'll have to pick out which of the interfaces is the WiFi connection (other stuff like a loopback interface will be in there too). Then you might have to use functions like inet_ntoa() depending on what format you want to read the IP addresses. It's not bad, just tedious and ugly. Have fun!

benzado
ifa->ifa_dstaddr is the broadcast address, not the router address.
Bill
@Bill: fixed my answer, please reconsider your downvote.
benzado