Skip to content

Instantly share code, notes, and snippets.

@trojanfoe
Last active January 4, 2016 00:29
Show Gist options
  • Save trojanfoe/8541571 to your computer and use it in GitHub Desktop.
Save trojanfoe/8541571 to your computer and use it in GitHub Desktop.
#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;
}
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