OSDN Git Service

(split) LDP: Update original to LDP v3.65
[linuxjm/LDP_man-pages.git] / original / man3 / getifaddrs.3
index d2cf120..02d0e6d 100644 (file)
@@ -2,6 +2,7 @@
 .\" and copyright (c) 2009, Linux Foundation, written by Michael Kerrisk
 .\"     <mtk.manpages@gmail.com>
 .\"
+.\" %%%LICENSE_START(VERBATIM)
 .\" Permission is granted to make and distribute verbatim copies of this
 .\" manual provided the copyright notice and this permission notice are
 .\" preserved on all copies.
@@ -21,6 +22,8 @@
 .\"
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
+.\"
 .\" Redistribution and use in source and binary forms, with or without
 .\" modification, are permitted provided that the following conditions
 .\" are met:
@@ -30,7 +33,7 @@
 .\"    for glibc specificities, provide an example.
 .\" 2009-01-14 mtk, many edits and changes, rewrote example program.
 .\"
-.TH GETIFADDRS 3 2010-10-06 "GNU" "Linux Programmer's Manual"
+.TH GETIFADDRS 3 2014-02-26 "GNU" "Linux Programmer's Manual"
 .SH NAME
 getifaddrs, freeifaddrs \- get interface addresses
 .SH SYNOPSIS
@@ -102,12 +105,14 @@ field points to a structure containing the interface address.
 .I sa_family
 subfield should be consulted to determine the format of the
 address structure.)
+This field may contain a null pointer.
 .PP
 The
 .I ifa_netmask
 field points to a structure containing the netmask associated with
 .IR ifa_addr ,
 if applicable for the address family.
+This field may contain a null pointer.
 .PP
 Depending on whether the bit
 .B IFF_BROADCAST
@@ -158,9 +163,9 @@ or
 The
 .BR getifaddrs ()
 function first appeared in glibc 2.3, but before glibc 2.3.3,
-the implementation only supported IPv4 addresses;
+the implementation supported only IPv4 addresses;
 IPv6 support was added in glibc 2.3.3.
-Support of address families other than IPv4 is only available
+Support of address families other than IPv4 is available only
 on kernels that support netlink.
 .SH CONFORMING TO
 Not in POSIX.1-2001.
@@ -191,9 +196,13 @@ and its physical layer.
 In this case, the
 .I ifa_data
 field may contain a pointer to a
+.IR "struct rtnl_link_stats" ,
+defined in
+.IR <linux/if_link.h>
+(in Linux 2.4 and earlier,
 .IR "struct net_device_stats" ,
 defined in
-.IR <linux/netdevice.h> ,
+.IR <linux/netdevice.h> ),
 which contains various interface attributes and statistics.
 .SH EXAMPLE
 The program below demonstrates the use of
@@ -206,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>
@@ -228,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) {
@@ -244,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;
 
@@ -253,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 */
 
@@ -265,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);
         }
     }
 
@@ -284,3 +312,11 @@ main(int argc, char *argv[])
 .BR socket (2),
 .BR packet (7),
 .BR ifconfig (8)
+.SH COLOPHON
+This page is part of release 3.65 of the Linux
+.I man-pages
+project.
+A description of the project,
+and information about reporting bugs,
+can be found at
+\%http://www.kernel.org/doc/man\-pages/.