428 lines
17 KiB
HTML
428 lines
17 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of GETIFADDRS</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>GETIFADDRS</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
getifaddrs, freeifaddrs - get interface addresses
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/ifaddrs.h">ifaddrs.h</A>></B>
|
|
|
|
<B>int getifaddrs(struct ifaddrs **</B><I>ifap</I><B>);</B>
|
|
|
|
<B>void freeifaddrs(struct ifaddrs *</B><I>ifa</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>getifaddrs</B>()
|
|
|
|
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
|
|
<I>*ifap</I>.
|
|
|
|
The list consists of
|
|
<I>ifaddrs</I>
|
|
|
|
structures, defined as follows:
|
|
<P>
|
|
|
|
|
|
|
|
struct ifaddrs {
|
|
<BR> struct ifaddrs *ifa_next; /* Next item in list */
|
|
<BR> char *ifa_name; /* Name of interface */
|
|
<BR> unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
|
|
<BR> struct sockaddr *ifa_addr; /* Address of interface */
|
|
<BR> struct sockaddr *ifa_netmask; /* Netmask of interface */
|
|
<BR> union {
|
|
<BR> struct sockaddr *ifu_broadaddr;
|
|
<BR> /* Broadcast address of interface */
|
|
<BR> struct sockaddr *ifu_dstaddr;
|
|
<BR> /* Point-to-point destination address */
|
|
<BR> } ifa_ifu;
|
|
#define ifa_broadaddr ifa_ifu.ifu_broadaddr
|
|
#define ifa_dstaddr ifa_ifu.ifu_dstaddr
|
|
<BR> void *ifa_data; /* Address-specific data */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>ifa_next</I>
|
|
|
|
field contains a pointer to the next structure on the list,
|
|
or NULL if this is the last item of the list.
|
|
<P>
|
|
|
|
The
|
|
<I>ifa_name</I>
|
|
|
|
points to the null-terminated interface name.
|
|
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>ifa_flags</I>
|
|
|
|
field contains the interface flags, as returned by the
|
|
<B>SIOCGIFFLAGS</B>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2)
|
|
|
|
operation (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+netdevice">netdevice</A></B>(7)
|
|
|
|
for a list of these flags).
|
|
<P>
|
|
|
|
The
|
|
<I>ifa_addr</I>
|
|
|
|
field points to a structure containing the interface address.
|
|
(The
|
|
<I>sa_family</I>
|
|
|
|
subfield should be consulted to determine the format of the
|
|
address structure.)
|
|
This field may contain a null pointer.
|
|
<P>
|
|
|
|
The
|
|
<I>ifa_netmask</I>
|
|
|
|
field points to a structure containing the netmask associated with
|
|
<I>ifa_addr</I>,
|
|
|
|
if applicable for the address family.
|
|
This field may contain a null pointer.
|
|
<P>
|
|
|
|
Depending on whether the bit
|
|
<B>IFF_BROADCAST</B>
|
|
|
|
or
|
|
<B>IFF_POINTOPOINT</B>
|
|
|
|
is set in
|
|
<I>ifa_flags</I>
|
|
|
|
(only one can be set at a time),
|
|
either
|
|
<I>ifa_broadaddr</I>
|
|
|
|
will contain the broadcast address associated with
|
|
<I>ifa_addr</I>
|
|
|
|
(if applicable for the address family) or
|
|
<I>ifa_dstaddr</I>
|
|
|
|
will contain the destination address of the point-to-point interface.
|
|
<P>
|
|
|
|
The
|
|
<I>ifa_data</I>
|
|
|
|
field points to a buffer containing address-family-specific data;
|
|
this field may be NULL if there is no such data for this interface.
|
|
<P>
|
|
|
|
The data returned by
|
|
<B>getifaddrs</B>()
|
|
|
|
is dynamically allocated and should be freed using
|
|
<B>freeifaddrs</B>()
|
|
|
|
when no longer needed.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success,
|
|
<B>getifaddrs</B>()
|
|
|
|
returns zero;
|
|
on error, -1 is returned, and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<B>getifaddrs</B>()
|
|
|
|
may fail and set
|
|
<I>errno</I>
|
|
|
|
for any of the errors specified for
|
|
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendto">sendto</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?3+realloc">realloc</A></B>(3).
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
The
|
|
<B>getifaddrs</B>()
|
|
|
|
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.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getifaddrs</B>(),
|
|
|
|
<B>freeifaddrs</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
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
|
|
<I>ifa_addr</I>
|
|
|
|
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
|
|
<I>ifa_broadaddr</I>
|
|
|
|
or
|
|
<I>ifa_dstaddr</I>
|
|
|
|
differs on various systems.
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
|
|
assigned to the interface, but also one
|
|
<B>AF_PACKET</B>
|
|
|
|
address per interface containing lower-level details about the interface
|
|
and its physical layer.
|
|
In this case, the
|
|
<I>ifa_data</I>
|
|
|
|
field may contain a pointer to a
|
|
<I>struct rtnl_link_stats</I>,
|
|
|
|
defined in
|
|
<I><<A HREF="file:///usr/include/linux/if_link.h">linux/if_link.h</A>></I>
|
|
|
|
(in Linux 2.4 and earlier,
|
|
<I>struct net_device_stats</I>,
|
|
|
|
defined in
|
|
<I><<A HREF="file:///usr/include/linux/netdevice.h">linux/netdevice.h</A>></I>),
|
|
|
|
which contains various interface attributes and statistics.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below demonstrates the use of
|
|
<B>getifaddrs</B>(),
|
|
|
|
<B>freeifaddrs</B>(),
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getnameinfo">getnameinfo</A></B>(3).
|
|
|
|
Here is what we see when running this program on one system:
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>./a.out</B>
|
|
lo AF_PACKET (17)
|
|
<BR> tx_packets = 524; rx_packets = 524
|
|
<BR> tx_bytes = 38788; rx_bytes = 38788
|
|
wlp3s0 AF_PACKET (17)
|
|
<BR> tx_packets = 108391; rx_packets = 130245
|
|
<BR> tx_bytes = 30420659; rx_bytes = 94230014
|
|
em1 AF_PACKET (17)
|
|
<BR> tx_packets = 0; rx_packets = 0
|
|
<BR> tx_bytes = 0; rx_bytes = 0
|
|
lo AF_INET (2)
|
|
<BR> address: <127.0.0.1>
|
|
wlp3s0 AF_INET (2)
|
|
<BR> address: <192.168.235.137>
|
|
lo AF_INET6 (10)
|
|
<BR> address: <::1>
|
|
wlp3s0 AF_INET6 (10)
|
|
<BR> address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
|
|
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
|
|
#include <<A HREF="file:///usr/include/arpa/inet.h">arpa/inet.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>>
|
|
#include <<A HREF="file:///usr/include/netdb.h">netdb.h</A>>
|
|
#include <<A HREF="file:///usr/include/ifaddrs.h">ifaddrs.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/linux/if_link.h">linux/if_link.h</A>>
|
|
<P>
|
|
int main(int argc, char *argv[])
|
|
{
|
|
<BR> struct ifaddrs *ifaddr, *ifa;
|
|
<BR> int family, s, n;
|
|
<BR> char host[NI_MAXHOST];
|
|
<P>
|
|
<BR> if (getifaddrs(&ifaddr) == -1) {
|
|
<BR> perror("getifaddrs");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Walk through linked list, maintaining head pointer so we
|
|
<BR> can free list later */
|
|
<P>
|
|
<BR> for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
|
|
<BR> if (ifa->ifa_addr == NULL)
|
|
<BR> continue;
|
|
<P>
|
|
<BR> family = ifa->ifa_addr->sa_family;
|
|
<P>
|
|
<BR> /* Display interface name and family (including symbolic
|
|
<BR> form of the latter for the common families) */
|
|
<P>
|
|
<BR> printf("%-8s %s (%d)\n",
|
|
<BR> ifa->ifa_name,
|
|
<BR> (family == AF_PACKET) ? "AF_PACKET" :
|
|
<BR> (family == AF_INET) ? "AF_INET" :
|
|
<BR> (family == AF_INET6) ? "AF_INET6" : "???",
|
|
<BR> family);
|
|
<P>
|
|
<BR> /* For an AF_INET* interface address, display the address */
|
|
<P>
|
|
<BR> if (family == AF_INET || family == AF_INET6) {
|
|
<BR> s = getnameinfo(ifa->ifa_addr,
|
|
<BR> (family == AF_INET) ? sizeof(struct sockaddr_in) :
|
|
<BR> sizeof(struct sockaddr_in6),
|
|
<BR> host, NI_MAXHOST,
|
|
<BR> NULL, 0, NI_NUMERICHOST);
|
|
<BR> if (s != 0) {
|
|
<BR> printf("getnameinfo() failed: %s\n", gai_strerror(s));
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("\t\taddress: <%s>\n", host);
|
|
<P>
|
|
<BR> } else if (family == AF_PACKET && ifa->ifa_data != NULL) {
|
|
<BR> struct rtnl_link_stats *stats = ifa->ifa_data;
|
|
<P>
|
|
<BR> printf("\t\ttx_packets = %10u; rx_packets = %10u\n"
|
|
<BR> "\t\ttx_bytes = %10u; rx_bytes = %10u\n",
|
|
<BR> stats->tx_packets, stats->rx_packets,
|
|
<BR> stats->tx_bytes, stats->rx_bytes);
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> freeifaddrs(ifaddr);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+packet">packet</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?8+ifconfig">ifconfig</A></B>(8)
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">ATTRIBUTES</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="9"><A HREF="#lbAJ">NOTES</A><DD>
|
|
<DT id="10"><A HREF="#lbAK">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="11"><A HREF="#lbAL">Program source</A><DD>
|
|
</DL>
|
|
<DT id="12"><A HREF="#lbAM">SEE ALSO</A><DD>
|
|
<DT id="13"><A HREF="#lbAN">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:44 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|