Getting the private IP
Getting the private IP
- Subject: Getting the private IP
- From: "Mr. Gecko" <email@hidden>
- Date: Fri, 8 May 2009 09:46:31 -0500
Hello, I'm trying to find out how to get the private IP address.
This is my quick way of finding out.
- (NSString *)localIP {
NSString *IPAddress = @"";
struct ifaddrs *myaddrs, *ifa;
struct sockaddr_in *s4;
int status;
char buf[64];
status = getifaddrs(&myaddrs);
if (status != 0) {
perror("getifaddrs");
exit(1);
}
for (ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) continue;
if ((ifa->ifa_flags & IFF_UP) == 0) continue;
if (ifa->ifa_addr->sa_family == AF_INET) {
s4 = (struct sockaddr_in *)(ifa->ifa_addr);
if (inet_ntop(ifa->ifa_addr->sa_family, (void *)&(s4->sin_addr),
buf, sizeof(buf)) != NULL) {
if (![[NSString stringWithCString:ifa->ifa_name] hasPrefix:@"lo"]) {
IPAddress = [NSString stringWithCString:buf];
break;
}
}
}
}
freeifaddrs(myaddrs);
if (![IPAddress isEqualToString:@""]) {
return IPAddress;
}
return @"127.0.0.1";
}
is there a better way?
Also when I have a firewire connected and it's self-assigned IP I get
the self-assigned IP address not the one which isn't self signed.
Thanks,
Mr. Gecko
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden