Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kepner

macrumors 6502
Original poster
Sep 7, 2003
366
2
Anyone know how I could get the router's IP address into an NSString? (Not the external IP, just the local IP of the router.)

routerip.jpg


I can get it with the Terminal on Mac OS X with this command:
Code:
ipconfig getoption en1 router

But is there a way to do it on the iPhone?
 
I do believe that the api used for network communication information such as that is private.

I was afraid that might be the case. I'm currently using the following code to get the iPhone's IP address, and then just assuming that the router has the same IP except the last segment is .1. That should be true most of the time, I suppose.

Code:
- (NSString *)getIPAddress
{
    NSString *address = @"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)];
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    // Free memory
    freeifaddrs(interfaces);
    
    return address;
}

Will the above code get through the App Store review process? Can it be modified to get the router's IP instead of the iPhone's IP?
 
The code should be okay with Apple, use of the getifaddrs() call is allowed. But you can never be sure.

To get the routing information it seems that you need to open a special socket with the PR_ROUTE domain and use ioctls to query the routing table. Check the route(4) manual page. You should be able to do this without root access.
 
Converting the name to an NSString just to compare it to "en0" is kind of funny. What happened to good old strcmp()?
 
I was afraid that might be the case. I'm currently using the following code to get the iPhone's IP address, and then just assuming that the router has the same IP except the last segment is .1. That should be true most of the time, I suppose.

That is a hugely inaccurate assumption. Just because that is what many home router manufacturers use doesn't mean it's safe to assume that. Hell, on the network I am on right now the gateway IP is 192.168.0.3. Someone that is a bit savvy will always change the default IP of the router when setting up WiFi, particularly a public one to keep people from poking around on the 10.0.0.1 or 192.168.0.1 address in their web browser.
 
The code should be okay with Apple, use of the getifaddrs() call is allowed. But you can never be sure.

To get the routing information it seems that you need to open a special socket with the PR_ROUTE domain and use ioctls to query the routing table. Check the route(4) manual page. You should be able to do this without root access.

Thanks! I'll look into this.

That is a hugely inaccurate assumption. Just because that is what many home router manufacturers use doesn't mean it's safe to assume that. Hell, on the network I am on right now the gateway IP is 192.168.0.3.

I think it'll be true most of the time. And it'll have to do until I can put together something a little more accurate. I suppose if my guess fails, I'll just ask the user for the IP. (If they've changed it from the default, chances are they know what it is.) I was just hoping there was a way to not make the user have to think about it.

That is a very odd "need" for a program.

That's why I'm writing it. ;)

Why the need for the router's IP? How does this fit in with the rest of your app?

It displays some helpful information via the router's web interface and API.
 
For something like this, I might just quickly scan the /28 ip block and search for open port 80, and do a HEAD query. Of course if your app wasn't wifi only this might cause a bit of a problem.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.