Manual Pages for Linux CentOS command on man freeifaddrs
MyWebUniversity

Manual Pages for Linux CentOS command on man freeifaddrs

GETIFADDRS(3) Linux Programmer's Manual GETIFADDRS(3)

NAME

getifaddrs, freeifaddrs - get interface addresses SYNOPSIS

#include

#include int getifaddrs(struct ifaddrs **ifap); void freeifaddrs(struct ifaddrs *ifa); DESCRIPTION The getifaddrs() function creates a linked list of structures describ‐ ing the network interfaces of the local system, and stores the address of the first item of the list in *ifap. The list consists of ifaddrs structures, defined as follows: struct ifaddrs { struct ifaddrs *ifanext; /* Next item in list */ char *ifaname; /* Name of interface */ unsigned int ifaflags; /* Flags from SIOCGIFFLAGS */ struct sockaddr *ifaaddr; /* Address of interface */ struct sockaddr *ifanetmask; /* Netmask of interface */ union { struct sockaddr *ifubroadaddr; /* Broadcast address of interface */ struct sockaddr *ifudstaddr;

/* Point-to-point destination address */ } ifaifu;

#define ifabroadaddr ifaifu.ifubroadaddr

#define ifadstaddr ifaifu.ifudstaddr

void *ifadata; /* Address-specific data */ }; The ifanext field contains a pointer to the next structure on the list, or NULL if this is the last item of the list.

The ifaname points to the null-terminated interface name. The ifaflags field contains the interface flags, as returned by the SIOCGIFFLAGS ioctl(2) operation (see netdevice(7) for a list of these flags). The ifaaddr field points to a structure containing the interface address. (The safamily subfield should be consulted to determine the format of the address structure.) This field may contain a NULL pointer. The ifanetmask field points to a structure containing the netmask associated with ifaaddr, if applicable for the address family. This field may contain a NULL pointer. Depending on whether the bit IFFBROADCAST or IFFPOINTOPOINT is set in ifaflags (only one can be set at a time), either ifabroadaddr will contain the broadcast address associated with ifaaddr (if applicable for the address family) or ifadstaddr will contain the destination

address of the point-to-point interface.

The ifadata field points to a buffer containing address-family-spe‐ cific data; this field may be NULL if there is no such data for this interface. The data returned by getifaddrs() is dynamically allocated and should be freed using freeifaddrs() when no longer needed. RETURN VALUE

On success, getifaddrs() returns zero; on error, -1 is returned, and errno is set appropriately. ERRORS getifaddrs() may fail and set errno for any of the errors specified for socket(2), bind(2), getsockname(2), recvmsg(2), sendto(2), malloc(3), or realloc(3). VERSIONS The getifaddrs() function first appeared in glibc 2.3, but before glibc 2.3.3, the implementation supported only IPv4 addresses; IPv6 support was added in glibc 2.3.3. Support of address families other than IPv4 is available only on kernels that support netlink. CONFORMING TO

Not in POSIX.1-2001. This function first appeared in BSDi and is present on the BSD systems, but with slightly different semantics docu‐ mented—returning one entry per interface, not per address. This means ifaaddr and other fields can actually be NULL if the interface has no

address, and no link-level address is returned if the interface has an IP address assigned. Also, the way of choosing either ifabroadaddr or ifadstaddr differs on various systems. NOTES The addresses returned on Linux will usually be the IPv4 and IPv6 addresses assigned to the interface, but also one AFPACKET address per

interface containing lower-level details about the interface and its physical layer. In this case, the ifadata field may contain a pointer to a struct rtnllinkstats, defined in (in Linux 2.4 and earlier, struct netdevicestats, defined in ), which contains various interface attributes and statistics. EXAMPLE The program below demonstrates the use of getifaddrs(), freeifaddrs(), and getnameinfo(3). Here is what we see when running this program on one system:

$ ./a.out lo address family: 17 (AFPACKET) eth0 address family: 17 (AFPACKET) lo address family: 2 (AFINET) address: <127.0.0.1> eth0 address family: 2 (AFINET) address: <10.1.1.4> lo address family: 10 (AFINET6) address: <::1> eth0 address family: 10 (AFINET6)

address: Program source

#include

#include

#include

#include

#include

#include

#include int main(int argc, char *argv[]) { struct ifaddrs *ifaddr, *ifa; int family, s; char host[NIMAXHOST];

if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); exit(EXITFAILURE); } /* Walk through linked list, maintaining head pointer so we can free list later */

for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifanext) {

if (ifa->ifaaddr == NULL) continue;

family = ifa->ifaaddr->safamily; /* Display interface name and family (including symbolic form of the latter for the common families) */

printf("%s address family: %d%s\n",

ifa->ifaname, family, (family == AFPACKET) ? " (AFPACKET)" : (family == AFINET) ? " (AFINET)" : (family == AFINET6) ? " (AFINET6)" : ""); /* For an AFINET* interface address, display the address */ if (family == AFINET || family == AFINET6) {

s = getnameinfo(ifa->ifaaddr, (family == AFINET) ? sizeof(struct sockaddrin) : sizeof(struct sockaddrin6), host, NIMAXHOST, NULL, 0, NINUMERICHOST); if (s != 0) {

printf("getnameinfo() failed: %s\n", gaistrerror(s)); exit(EXITFAILURE); }

printf("\taddress: <%s>\n", host); } } freeifaddrs(ifaddr); exit(EXITSUCCESS); } SEE ALSO bind(2), getsockname(2), socket(2), packet(7), ifconfig(8) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

GNU 2012-11-11 GETIFADDRS(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™