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