title: Objective-C : How to fetch the router address?
body: <p>I tried to fetch the router address this way.</p>
  
  <pre><code>- (NSString *) routerIp {
  
    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(&amp;interfaces);
    if (success == 0)
    {
      // Loop through linked list of interfaces
      temp_addr = interfaces;
      while(temp_addr != NULL)
      {
        if(temp_addr-&gt;ifa_addr-&gt;sa_family == AF_INET)
        {
          // Check if interface is en0 which is the wifi connection on the iPhone
          if([[NSString stringWithUTF8String:temp_addr-&gt;ifa_name] isEqualToString:@"en0"])
          {
            // Get NSString from C String //ifa_addr
            address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr-&gt;ifa_dstaddr)-&gt;sin_addr)];
          }
        }
  
        temp_addr = temp_addr-&gt;ifa_next;
      }
    }
  
    // Free memory
    freeifaddrs(interfaces);
  
    return address;
  }
  </code></pre>
  
  <p>The router address always looks like xxx.xxx.255.255 but it is supposed to look like xxx.xxx.0.1 or something this way...</p>
  
  <p>Is there anything to do to get the valid address?</p>
  
  <p>Thanks for your help!</p>