• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Getting the private IP
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Getting the private IP
      • From: Alastair Houghton <email@hidden>
    • Re: Getting the private IP
      • From: Michael Ash <email@hidden>
  • Prev by Date: Re: quick and dirty NSData implosion
  • Next by Date: Re: quick and dirty NSData implosion
  • Previous by thread: RE: Distributed Objects via Bonjour?
  • Next by thread: Re: Getting the private IP
  • Index(es):
    • Date
    • Thread