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?
Asked
Active
Viewed 1.1k times
11
-
Right now I am looking at ICMP (router solicitation) or ICMP (echo request, 1 hop, see who replies). I don't think my router responds to router solicitation ICMP requests; the second method is how traceroute works (I believe). Another option would be a DHCP API (cannot find one on Iphone) or sending DHCP packets for info (because in my case there will always be DHCP and the router is always the DHCP server as well). – Bill Feb 22 '10 at 22:58
-
FYI - The iPhone application is designed to control security/access policies on DLink Home NAT routers (the Web interface is a pain to use); I am simply trying to avoid making the user type in the router's address. They will have to provide password, so no big deal if I cannot find a good solution here. – Bill Feb 22 '10 at 23:01
-
@bvanderw - can you post a link to your code or the documentation to determine the MAC address of the WiFi connection? I haven't been able to track it down. – Ciaran Mar 25 '10 at 02:57
2 Answers
13
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!
3
NSString *address = @"error";
NSString *netmask = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
netmask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
NSLog(@"address %@", address);
NSLog(@"netmask %@", netmask);

vualoaithu
- 936
- 10
- 9