It seems that for some interface types (I noticed vlan and lo), a netmask
with af==0 is returned by getifaddrs().

Not sure if this was always broken or introduced more recently; happens
on at least 5.3 and -current so it's not anything particularly recent.

Demonstration code (yes I know it's crappy but it does show the problem -
other code also hits it, I noticed this when investigating problems
reported with 'samenet' in postgresql).

-- --
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <stdio.h>

void ntop(struct sockaddr *sa) {
        char buf[128];

        switch (sa->sa_family) {
                case AF_INET:
                        printf("AF_INET ");
                        break;
                case AF_LINK:
                        printf("AF_LINK ");
                        break;
                case AF_INET6:
                        printf("AF_INET6 ");
                        break;
                default:
                        printf("AF[%u] ", sa->sa_family);
                }
        inet_ntop(sa->sa_family, sa->sa_data+2, buf, sizeof(buf));
        if (buf && (sa->sa_family != AF_LINK))
                printf("%s", buf);
}

main() {
        struct ifaddrs *ifa, *l;
        if (getifaddrs(&ifa) < 0)
                return 1;
        for (l = ifa; l; l = l->ifa_next) {
                printf("%s: ", l->ifa_name);
                ntop(l->ifa_addr);
                if (l->ifa_netmask) {
                        printf(" mask ");
                        ntop(l->ifa_netmask);
                }
                printf("\n");
        }
        freeifaddrs(ifa);
}

Reply via email to