getifaddrs (3) - Linux Manuals
getifaddrs: get interface addresses
NAME
getifaddrs, freeifaddrs - get interface addresses
SYNOPSIS
#include <sys/types.h> #include <ifaddrs.h> int getifaddrs(struct ifaddrs **ifap); void freeifaddrs(struct ifaddrs *ifa);
DESCRIPTION
The getifaddrs() function creates a linked list of structures describing 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 {
The
ifa_next
field contains a pointer to the next structure on the list,
or NULL if this is the last item of the list.
The
ifa_name
points to the null-terminated interface name.
The
ifa_flags
field contains the interface flags, as returned by the
SIOCGIFFLAGS
ioctl(2)
operation (see
netdevice(7)
for a list of these flags).
The
ifa_addr
field points to a structure containing the interface address.
(The
sa_family
subfield should be consulted to determine the format of the
address structure.)
This field may contain a null pointer.
The
ifa_netmask
field points to a structure containing the netmask associated with
ifa_addr,
if applicable for the address family.
This field may contain a null pointer.
Depending on whether the bit
IFF_BROADCAST
or
IFF_POINTOPOINT
is set in
ifa_flags
(only one can be set at a time),
either
ifa_broadaddr
will contain the broadcast address associated with
ifa_addr
(if applicable for the address family) or
ifa_dstaddr
will contain the destination address of the point-to-point interface.
The
ifa_data
field points to a buffer containing address-family-specific 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.
$ ./a.out
lo AF_PACKET (17)
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.
ATTRIBUTES
For an explanation of the terms used in this section, see
attributes(7).
Interface Attribute Value
getifaddrs(),
freeifaddrs()
Thread safety MT-Safe CONFORMING TO
Not in POSIX.1.
This function first appeared in BSDi and is
present on the BSD systems, but with slightly different
semantics documented---returning one entry per interface,
not per address.
This means
ifa_addr
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
ifa_broadaddr
or
ifa_dstaddr
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
AF_PACKET
address per interface containing lower-level details about the interface
and its physical layer.
In this case, the
ifa_data
field may contain a pointer to a
struct rtnl_link_stats,
defined in
<linux/if_link.h>
(in Linux 2.4 and earlier,
struct net_device_stats,
defined in
<linux/netdevice.h>),
which contains various interface attributes and statistics.
EXAMPLES
The program below demonstrates the use of
getifaddrs(),
freeifaddrs(),
and
getnameinfo(3).
Here is what we see when running this program on one system: