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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to