Last active
January 4, 2016 00:29
-
-
Save trojanfoe/8541571 to your computer and use it in GitHub Desktop.
Enumerate all IP addresses on OSX. See: http://stackoverflow.com/questions/21260004/how-to-get-the-ipaddress-of-an-iphone-device-in-universal-application-in-ios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <ifaddrs.h> | |
#import <arpa/inet.h> | |
int main(int argc, const char **argv) { | |
@autoreleasepool { | |
struct ifaddrs *interfaces = NULL; | |
if (getifaddrs(&interfaces) == 0) { | |
for (struct ifaddrs *ifa = interfaces; ifa; ifa = ifa->ifa_next) { | |
char buf[128]; | |
if (ifa->ifa_addr->sa_family == AF_INET) { | |
inet_ntop(AF_INET, (void *)&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, | |
buf, sizeof(buf)); | |
} else if (ifa->ifa_addr->sa_family == AF_INET6) { | |
inet_ntop(AF_INET6, (void *)&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, | |
buf, sizeof(buf)); | |
} else { | |
continue; | |
} | |
NSLog(@"name=%s, family=%u, address=%s", | |
ifa->ifa_name, (unsigned)ifa->ifa_addr->sa_family, buf); | |
} | |
} | |
freeifaddrs(interfaces); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2014-01-21 14:57:39.317 getifaddrs[31705:707] name=lo0, family=30, address=fe80::1 | |
2014-01-21 14:57:39.319 getifaddrs[31705:707] name=lo0, family=2, address=127.0.0.1 | |
2014-01-21 14:57:39.319 getifaddrs[31705:707] name=lo0, family=30, address=::1 | |
2014-01-21 14:57:39.320 getifaddrs[31705:707] name=en1, family=30, address=fe80::fa1a:67ff:fe0e:30eb | |
2014-01-21 14:57:39.320 getifaddrs[31705:707] name=en1, family=2, address=192.168.1.100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment