getifaddrs.3: Enhance example program

Print statistics for AF_PACKET interfaces.
Add missing feature test macro definition.
Reformat output.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2014-02-25 08:56:27 +01:00
parent 754356dbcf
commit 83a1c50ffe
1 changed files with 40 additions and 21 deletions

View File

@ -215,21 +215,29 @@ Here is what we see when running this program on one system:
.nf
$ \fB./a.out\fP
lo address family: 17 (AF_PACKET)
eth0 address family: 17 (AF_PACKET)
lo address family: 2 (AF_INET)
address: <127.0.0.1>
eth0 address family: 2 (AF_INET)
address: <10.1.1.4>
lo address family: 10 (AF_INET6)
address: <::1>
eth0 address family: 10 (AF_INET6)
address: <fe80::2d0:59ff:feda:eb51%eth0>
lo AF_PACKET (17)
tx_packets = 524; rx_packets = 524
tx_bytes = 38788; rx_bytes = 38788
wlp3s0 AF_PACKET (17)
tx_packets = 108391; rx_packets = 130245
tx_bytes = 30420659; rx_bytes = 94230014
em1 AF_PACKET (17)
tx_packets = 0; rx_packets = 0
tx_bytes = 0; rx_bytes = 0
lo AF_INET (2)
address: <127.0.0.1>
wlp3s0 AF_INET (2)
address: <192.168.235.137>
lo AF_INET6 (10)
address: <::1>
wlp3s0 AF_INET6 (10)
address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
.fi
.in
.SS Program source
\&
.nf
#define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
@ -237,12 +245,12 @@ eth0 address family: 10 (AF_INET6)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/if_link.h>
int
main(int argc, char *argv[])
int main(int argc, char *argv[])
{
struct ifaddrs *ifaddr, *ifa;
int family, s;
int family, s, n;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == \-1) {
@ -253,7 +261,7 @@ main(int argc, char *argv[])
/* Walk through linked list, maintaining head pointer so we
can free list later */
for (ifa = ifaddr; ifa != NULL; ifa = ifa\->ifa_next) {
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa\->ifa_next, n++) {
if (ifa\->ifa_addr == NULL)
continue;
@ -262,11 +270,12 @@ main(int argc, char *argv[])
/* Display interface name and family (including symbolic
form of the latter for the common families) */
printf("%s\t address family: %d%s\\n",
ifa\->ifa_name, family,
(family == AF_PACKET) ? " (AF_PACKET)" :
(family == AF_INET) ? " (AF_INET)" :
(family == AF_INET6) ? " (AF_INET6)" : "");
printf("%\-8s %s (%d)\\n",
ifa\->ifa_name,
(family == AF_PACKET) ? "AF_PACKET" :
(family == AF_INET) ? "AF_INET" :
(family == AF_INET6) ? "AF_INET6" : "???",
family);
/* For an AF_INET* interface address, display the address */
@ -274,12 +283,22 @@ main(int argc, char *argv[])
s = getnameinfo(ifa\->ifa_addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
host, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("\\taddress: <%s>\\n", host);
printf("\\t\\taddress: <%s>\\n", host);
} else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
struct rtnl_link_stats *stats = ifa\->ifa_data;
printf("\\t\\ttx_packets = %10u; rx_packets = %10u\\n"
"\\t\\ttx_bytes = %10u; rx_bytes = %10u\\n",
stats\->tx_packets, stats\->rx_packets,
stats\->tx_bytes, stats\->rx_bytes);
}
}