On Fri, Oct 04, 2013 at 11:46:42AM +0100, Stuart Henderson wrote: > 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).
Hmm. People missing history class again and assuming the world is flat? In the good old days netmasks did not have a family so that the same entry could be used accross multiple AFs. The network stack still produces them from time to time. This is why you need to use the AF of the address to print the netmask. One can argue that this broken and I think that you're right. > -- -- > #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); > } > -- :wq Claudio