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.