Hi I'm trying to get interfaces' list and infos with getifaddrs(). The ifa_data struct should contain all needed information, but this is a NULL pointer for IPv4 interfaces. Why ? a sample output for my ethernet card : ed0 ( 6.3.6.0) : UP, AF_INET6 MTU=1500 (struct ifa_data) ed0 ( 10.0.0.1) : UP MTU=1500 (struct ifa_data is a null pointer) BTW, there is always 2 nodes for each interface, even if INET6 is not enabled in the kernel. is it normal ? thanks (see the source attached) -- ecureuil <[EMAIL PROTECTED]>
/* ecureuil <[EMAIL PROTECTED]> get interface information */ #include <stdio.h> #include <string.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <net/if.h> #include <unistd.h> #include <ifaddrs.h> int main(int argc, char **argv) { struct ifaddrs *ifap, *ifa; struct sockaddr_in *sa; int fd; struct ifreq ifr; char ifstraddr[16]; sa_family_t fam; if (getifaddrs(&ifap) < 0) { perror("getifaddrs"); return -1; } for (ifa = ifap; ifa; ifa = ifa->ifa_next) { printf("%6s ", ifa->ifa_name); /* get if address */ sa = (struct sockaddr_in *) ifa->ifa_addr; strncpy(ifstraddr, inet_ntoa(sa->sin_addr), 16); printf("(%15s) : ",ifstraddr); if (!(ifa->ifa_flags & IFF_UP)) printf("DOWN"); else printf("UP"); if (ifa->ifa_flags & IFF_PROMISC) printf(", PROMISCUOUS"); if (ifa->ifa_addr->sa_family & AF_INET6) printf(", AF_INET6"); /* get if mtu */ fam = ifa->ifa_addr->sa_family == AF_LINK ? AF_INET : ifa->ifa_addr->sa_family; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { perror("[get_if_name] socket(AF_INET, SOCK_DGRAM, 0)"); return -1; } strncpy(ifr.ifr_name,ifa->ifa_name,IFNAMSIZ); if (ioctl(fd, SIOCGIFMTU, &ifr) < 0 ) { perror("ioctl"); return -1; } else { printf(" MTU=%d ",ifr.ifr_mtu); } close(fd); if(ifa->ifa_data) printf("(struct ifa_data)\n"); else printf("(struct ifa_data is a null pointer)\n"); } freeifaddrs(ifap); return 0; }