1

How can I get my default Gateway IP and MAC Address in Objective-C?

Right now, I was able to make a function to get the IP that uses arpa/inet.h it works fine, but I've no idea how to get the default gateway IP and MAC Address.

NSString* GetMyIP(NSString* inf) {
    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!
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:inf])
                {
                    // 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;
        }
    }

    freeifaddrs(interfaces);
    return address;
}

I used arp and netstat for a while but they don't always work as expected and as you might understand are risky solutions...

Is there another more "Apple-way" to get this kind of info? Without using arpa/inet.h? Examples?

Thank you.

TCB13
  • 3,067
  • 2
  • 39
  • 68

1 Answers1

1

This answer explains how to get the MAC address for an interface on OSX.

The default gateway is registered in the routing table - not directly associated with a network interface. Note that the default gateway (i.e. the gateway for 0.0.0.0) is probably not the only one you need to care about, there might be routers set for more constrained address ranges. (also, there may be multiple gateways, default or otherwise, with different priorities)

The routing table is documented in the route(4) manpage - that should hopefully give you enough information for retrieving the routing table in your app.

Community
  • 1
  • 1
pmdj
  • 22,018
  • 3
  • 52
  • 103
  • I'm looking for my Default Gateway MAC not my own... Thank you anyway. – TCB13 Jun 12 '12 at 18:46
  • Ah, I see. Once you have the gateway's IP, you will indeed need to go via ARP then. – pmdj Jun 12 '12 at 18:47
  • I notice that on the terminal the arp give's me a different MAC from the one reported by the router... strange :S. – TCB13 Jun 13 '12 at 19:18
  • The router will have at least two different MAC addresses, one for each interface. Wifi routers often have 3: one for the WAN interface, one for the wired LAN interface and one for the Wifi access point. Make sure you're looking at the correct one. – pmdj Jun 13 '12 at 19:29
  • I'm using dd-wrt, and none of the MAC addresses on the `System Information` page are the one reported by arp on Terminal. I guess it's an issue of the router maybe... But I'll try with others soon. Thank you! – TCB13 Jun 13 '12 at 19:39