Skip navigation.
 
mlRe: Finding IP Addresses?
FROM : Jeff Thompson
DATE : Sat Nov 23 07:52:46 2002

This should help you. Returns a dictionary of IP addresses, but ignores
127.0.0.1 and 10.64.64.64 (unitialized modem). Just enumerate through
the dictionary entries to find interface name and IP. Obviously you can
strip out the 127.0.0.1 and 10.64.64.64 check if you like.

#include <sys/socket.h>
#include <sys/sockio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <unistd.h>

#define IFCONF_BUFFERSIZE  4000
#define max(a,b)    ((a) > (b) ? (a) : (b))

+ (NSDictionary *)getIPAddresses
{
    NSMutableDictionary *ip_addrs = [NSMutableDictionary dictionary];
    char buffer[IFCONF_BUFFERSIZE];
    char *ptr = buffer;
    struct ifconf ifc = { IFCONF_BUFFERSIZE, };
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    if (sockfd == -1)
        return nil;
    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
        return nil;
    while (ptr < buffer + ifc.ifc_len)
    {
        struct ifreq *ifr = (struct ifreq *)ptr;
        struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
        NSString *ipaddr = [NSString
stringWithCString:inet_ntoa(sin->sin_addr)]
;
        char *cptr;

        // for next one in buffer
        ptr += sizeof(ifr->ifr_name) + max(sizeof(struct sockaddr),
                                            ifr->ifr_addr.sa_len);
        if (ifr->ifr_addr.sa_family != AF_INET)
        {
            continue;  // ignore if not desired address family
        }
        if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)
        {
            *cptr = 0;      // replace colon with null
        }

        ioctl(sockfd, SIOCGIFFLAGS, ifr);
        if ((ifr->ifr_flags & IFF_UP) == 0)
        {
            continue;  // ignore if interface not up
        }

    if(![ipaddr isEqualToString:@"10.64.64.64"] && ![ipaddr
isEqualToString:@"12
7.0.0.1"])
    {
            [ip_addrs setObject:ipaddr forKey:[NSString
stringWithCString:ifr->i
fr_name]];
    }
    }
    close(sockfd);
    return ip_addrs;
}

I also use the follow to determine if the network is up. By ignoring
10.64.64.64 we don't get a false positive by having a modem that is not
  connected.

+ (BOOL)isNetworkUp
{
    if([[self getIPAddresses] count] > 1)
        return YES;
    else
        return NO;
}


Enjoy,

Jeff

Jeff Thompson
CTO, CodeTek Studios, Inc.
CodeTek VirtualDesktop - http://www.codetek.com/


On Saturday, November 23, 2002, at 09:33 AM, Parmadil Istarion wrote:

> Hi,
> I've written a program which needs to locate a user's current IP
> address. I tried using NSHost, and it only returned
> localhost/127.0.0.1, even in the arrays. This was while connected to
> the Net, by the way.
>
> Is there any way short of using Open Transport to do this? Am I doing
> something stupid with NSHost? Any help will be appreciated.
>
> Thanks
>
> _______________________________________________
> MacOSX-dev mailing list
> <email_removed>
> http://www.omnigroup.com/mailman/listinfo/macosx-dev



Related mailsAuthorDate
mlFinding IP Addresses? Parmadil Istarion Nov 23, 07:31
mlRe: Finding IP Addresses? Jeff Thompson Nov 23, 07:52
mlRe: Finding IP Addresses? Stéphane Sudre Nov 25, 02:32
mlRe: Finding IP Addresses? Jeff Thompson Nov 25, 14:02
mlRe: Finding IP Addresses? Avi Drissman Nov 25, 14:16
mlRe: Finding IP Addresses? Parmadil Istarion Nov 25, 16:26