getifaddrs() is indeed the ticket. This will print your hostname and en* IP address(es). You can modify it to do something else with them, if you'd like.
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
-(void)printIPAddresses {
struct ifaddrs *ifaddr, *ifa;
int family, s;
char host[NI_MAXHOST];
char hostname[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
}
if(!gethostname(hostname, NI_MAXHOST)) {
printf("Hostname: %s\n", hostname);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if (family == AF_INET || family == AF_INET6) {
s = getnameinfo(ifa->ifa_addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0) {
DDLogError(@"getnameinfo() failed: %s", gai_strerror(s));
}
//only print the ethernet/wifi interface (en*), not loopback, 3g or vpn
if (strncmp(ifa->ifa_name, "en", 2) == 0) {
printf("address: %s\n", host);
}
}
}
freeifaddrs(ifaddr);
}
--You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.To post to this group, send email to cocoaasy...@googlegroups.com.To unsubscribe from this group, send email to cocoaasyncsock...@googlegroups.com.For more options, visit this group at http://groups.google.com/group/cocoaasyncsocket?hl=en.