Router IP into NSString?

Discussion in 'iOS Programming' started by kepner, Sep 28, 2010.

  1. kepner macrumors 6502

    Joined:
    Sep 7, 2003
    #1
    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.)

    [​IMG]

    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?
     
  2. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #2
    I do believe that the api used for network communication information such as that is private.
     
  3. kepner thread starter macrumors 6502

    Joined:
    Sep 7, 2003
    #3
    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?
     
  4. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #4
    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.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Converting the name to an NSString just to compare it to "en0" is kind of funny. What happened to good old strcmp()?
     
  6. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #6
    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.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Why the need for the router's IP? How does this fit in with the rest of your app?
     
  8. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #8
    +1 to this.. That is a very odd "need" for a program.
     
  9. kepner thread starter macrumors 6502

    Joined:
    Sep 7, 2003
    #9
    Thanks! I'll look into this.

    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's why I'm writing it. ;)

    It displays some helpful information via the router's web interface and API.
     
  10. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #10
    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.
     

Share This Page