tags:

views:

49

answers:

1

Hi,

I need some help with finding network interfaces based on a provided IP address. I need a cross-platform way to reliably find the local interface that has the given address in both IPv4 and IPv6 formats. I created the attached program to take an IP address as a string and search through the results of getifaddrs.

The reason is that I eventually want to pass this in_addr or in6_addr structure to the IP_MULTICAST_IF ioctl on a socket for specifying which interface should be used to send a multicast message. I want to check that it is a known IP before proceeding.

While it seems to work fine on my Linux machine and on a MacBook, it fails on my lab's server. (Oh my, this eventually has to work on Windows too..) Here's an example run on Linux:

$ ifconfig wlan0 | grep inet6
          inet6 addr: fe80::1e4b:d6ff:fe6e:f846/64 Scope:Link

$ ifconfig wlan0 | grep inet
          inet addr:192.168.0.13  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::1e4b:d6ff:fe6e:f846/64 Scope:Link

$ ./findif 192.168.0.13

found: wlan0

$ ./findif fe80::1e4b:d6ff:fe6e:f846

name: lo
ifa:      00000000000000000000000000000001
provided: fe800000000000001e4bd6fffe6ef846

name: wlan0
ifa:      fe800000000000001e4bd6fffe6ef846
provided: fe800000000000001e4bd6fffe6ef846

found: wlan0

So far so good. I get similar results on a MacBook running OS X 10.6. However, when I do something similar on our server, which is an older PPC-based OS X machine running 10.4.11, I get the following:

$ ifconfig en0 | grep inet
  inet6 fe80::20d:XXXX:XXXX:XXXX%en0 prefixlen 64 scopeid 0x4 
  inet 132.XXX.XX.XX netmask 0xffffff00 broadcast 132.XXX.XX.255

$ ./findif 132.XXX.XX.XX

found: en0

$ ./findif fe80::XXX:XXXX:XXXX:XXXX

name: lo0
ifa:      00000000000000000000000000000001
provided: fe800000000000000XXXXXXXXXXXXXXX

name: lo0
ifa:      fe800001000000000000000000000001
provided: fe800000000000000XXXXXXXXXXXXXXX

name: en0
ifa:      fe800004000000000XXXXXXXXXXXXXXX
provided: fe800000000000000XXXXXXXXXXXXXXX

name: en1
ifa:      fe800005000000000YYYYYYYYYYYYYYY
provided: fe800000000000000YYYYYYYYYYYYYYY

Apologies for X'ing out some of the address digits, I thought it best not to expose real IPs for a live server here. Rest assured that wherever you see X or Y it is a matching hex digit.

Anyways, notice that the IPv6 returned for en0 has exactly 1 bit that is different than reported by ifconfig. Can anyone tell me why that is, and how I should adjust my code? Is a simple memcmp not the correct way to compare IPv6 addresses?

Thanks for any suggestions.

Here is the code for findif.c:

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <string.h>

void printipv6(const void* ipv6)
{
    int i;
    for (i=0; i<16; i++)
        printf("%02x", ((unsigned char*)ipv6)[i]);
}

int find_iface(const char *ip)
{
    union {
        struct in_addr addr;
        struct in6_addr addr6;
    } a;

    int rc = inet_pton(AF_INET6, ip, &a.addr6);
 int fam = AF_INET6;
    if (rc==0) {
        rc = inet_pton(AF_INET, ip, &a.addr);
  fam = AF_INET;
 }
    if (rc < 0) {
        perror("inet_pton");
        return 1;
    }

    struct ifaddrs *ifa, *ifa_list;
    if (getifaddrs(&ifa_list)==-1) {
        perror("getifaddrs");
        return 1;
    }
    ifa = ifa_list;

    int i=0;
    while (ifa) {
        if (ifa->ifa_addr) {
            if (ifa->ifa_addr->sa_family == AF_INET && fam == AF_INET)
            {
                if (memcmp(&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr,
                           &a.addr, sizeof(struct in_addr))==0) {
                    printf("\nfound: %s\n", ifa->ifa_name);
                    break;
                }
            }
            else if (ifa->ifa_addr->sa_family == AF_INET6 && fam == AF_INET6)
            {
                struct in6_addr *p = &((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr;
                printf("\nname: %s\n", ifa->ifa_name);
                printf("ifa:      "); printipv6(p);        printf("\n");
                printf("provided: "); printipv6(&a.addr6); printf("\n");
                if (memcmp(p, &a.addr6, sizeof(struct in6_addr))==0) {
                    printf("\nfound: %s\n", ifa->ifa_name);
                    break;
                }
            }
        }
        ifa = ifa->ifa_next;
        i++;
    }
    freeifaddrs(ifa_list);
    return 0;
}

int main(int argc, char *argv[])
{
    if (argc > 1)
        return find_iface(argv[1]);
    return 0;
}
+1  A: 

Your code looks correct - IPv6 link-local addresses are supposed to have 54 zero bits following the first 10 bits, so the addresses being returned by getifaddrs() just seem wrong. It looks like that version of OS X might be incorrectly munging the scope id into the address.

caf
Do you think it would be correct to purposely not compare those 54 zero bits then, or would it be better to leave it and say that the OS was broken? (I'll try to do more testing on more recent versions of OS X to verify that it will at least be reliable.)
Steve
@Steve: If you do, I would only do that on that particular version of OS X as a workaround. Alternatively you could just specify a minimum supported version of OS X.
caf