A few months ago I posted a description of how to use SIOCGIFCONF to retrieve information about interfaces. SIOCGIFCONF is somewhat clunky in that you use an ioctl to find out how many interfaces are present, allocate enough memory to retrieve them all, and then issue another ioctl to actually get the information. To handle the vanishingly small chance that more interfaces will be added during the time you spend allocating memory, a fudge factor of 2x is added to the memory allocation. Because, you know, its not likely the number of interfaces would double.
That was all very silly, and as it turns out in Linux there is a much better API for retrieving information about interfaces: getifaddr(). The call handles memory allocation so you don't have to pass in a buffer of sufficient size, though you do have to call freeifaddrs() afterwards to release the memory. getifaddrs allows each protocol family in the kernel to export information about an interface. The caller has to check the address family of each returned interface to know how to interpret it. For example, AF_INET/AF_INET6 contain the interface address, while AF_PACKET has statistics. Example code for these three families is shown here.
#include <arpa/inet.h> #include <sys/socket.h> #include <netdb.h> #include <ifaddrs.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <linux/if_link.h> int main(int argc, char *argv[]) { struct ifaddrs *ifaddr; int family, s; if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); exit(1); } struct ifaddrs *ifa = ifaddr; for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr != NULL) { int family = ifa->ifa_addr->sa_family; if (family == AF_INET || family == AF_INET6) { char ip_addr[NI_MAXHOST]; int s = getnameinfo(ifa->ifa_addr, ((family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6)), ip_addr, sizeof(ip_addr), NULL, 0, NI_NUMERICHOST); if (s != 0) { printf("getnameinfo() failed: %s\n", gai_strerror(s)); exit(1); } else { printf("%-7s: %s\n", ifa->ifa_name, ip_addr); } } else if (family == AF_PACKET) { struct rtnl_link_stats *stats = ifa->ifa_data; printf("%-7s:\n" "\ttx_packets = %12u, rx_packets = %12u\n" "\ttx_bytes = %12u, rx_bytes = %12u\n", ifa->ifa_name, stats->tx_packets, stats->rx_packets, stats->tx_bytes, stats->rx_bytes); } else { printf("%-7s: family=%d\n", ifa->ifa_name, family); } } } freeifaddrs(ifaddr); exit(0); }
On my system the output is as follows (though I've obscured the addresses):
lo : tx_packets = 16714641, rx_packets = 16714641 tx_bytes = 1943837629, rx_bytes = 1943837629 eth0 : tx_packets = 102862634, rx_packets = 118537985 tx_bytes = 3472339330, rx_bytes = 698859563 gre0 : tx_packets = 0, rx_packets = 0 tx_bytes = 0, rx_bytes = 0 lo : 127.0.0.1 eth0 : 10.0.0.1 lo : ::1 eth0 : 1111:1111:1111:1111:a800:1ff:fe00:1111 eth0 : fe80::a800:1ff:fe00:1111%eth0