getifaddrs(3)
โจ NAME โจ
getifaddrs, freeifaddrs โ bestie, we're getting the interface addresses ๐
This one's a section 3 page (library functions, iykyk), affectionately known as getifaddrs(3).
๐ซถ LIBRARY ๐ซถ
Standard C library โ that's libc, and you link it with -lc. Iconic, lives in every single program you've ever written fr fr.
๐ธ SYNOPSIS ๐ธ
#include <sys/types.h>
#include <ifaddrs.h>
int getifaddrs(struct ifaddrs **ifap);
void freeifaddrs(struct ifaddrs *ifa);
๐ DESCRIPTION ๐
okay so. getifaddrs() builds you a whole linked list of structures describing every network interface on the local system, and it drops the address of the very first item into *ifap. Main character energy from the first node onward ๐
The list is made of ifaddrs structures, and they look like this:
struct ifaddrs {
struct ifaddrs *ifa_next; /* Next item in list */
char *ifa_name; /* Name of interface */
unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
struct sockaddr *ifa_addr; /* Address of interface */
struct sockaddr *ifa_netmask; /* Netmask of interface */
union {
struct sockaddr *ifu_broadaddr;
/* Broadcast address of interface */
struct sockaddr *ifu_dstaddr;
/* Point-to-point destination address */
} ifa_ifu;
#define ifa_broadaddr ifa_ifu.ifu_broadaddr
#define ifa_dstaddr ifa_ifu.ifu_dstaddr
void *ifa_data; /* Address-specific data */
};
Let me walk you through every field because we do NOT skip details in this house:
ifa_nextโ a pointer to the next structure in the list, orNULLif this is the last item. That's your "am I done yet" light, bestie.ifa_nameโ points to the null-terminated interface name. That's yourlo, yourwlp3s0, your little gremlins.ifa_flagsโ the interface flags, exactly as returned by theSIOCGIFFLAGSioctl(2)operation. (Peepnetdevice(7)for the full list of these flags ๐)ifa_addrโ points to a structure holding the interface address. Consult thesa_familysubfield to figure out the format of that address structure โ it's giving shapeshifter. This field may contain a null pointer, so check it, okay?ifa_netmaskโ points to a structure holding the netmask associated withifa_addr, if it's applicable for the address family. Also may contain a null pointer.- Now the drama: depending on whether the bit
IFF_BROADCASTorIFF_POINTOPOINTis set inifa_flags(only one can be set at a time, they're not sharing that spotlight), eitherifa_broadaddrwill contain the broadcast address associated withifa_addr(if applicable for the address family) orifa_dstaddrwill contain the destination address of the point-to-point interface. ifa_dataโ points to a buffer containing address-family-specific data. This one may beNULLif there's just no such data for this interface. She's optional.
Everything getifaddrs() hands back is dynamically allocated, so you must free it with freeifaddrs() once you're done with it. Do not leave it hanging, that's so not the vibe ๐ฎ
๐ RETURN VALUE ๐
- Success?
getifaddrs()returns zero. Slay. - Error? It returns -1, and
errnogets set to tell you what went wrong ๐ญ
๐ญ ERRORS ๐ญ
getifaddrs() can 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). Yeah. That's the whole list. Go read those pages if you need the deets.
๐ ATTRIBUTES ๐
For an explanation of the terms used in this section, go see attributes(7).
getifaddrs(),freeifaddrs()โ Thread safety โ MT-Safe ๐งตโจ
๐ STANDARDS ๐
None. ๐ Not standardized. It's just out here living its life.
๐ฆ HISTORY ๐ฆ
This function first appeared in BSDi and is present on the BSD systems too โ but with slightly different semantics documented, specifically: they return one entry per interface, not per address. That's a big deal, bestie, because it means:
ifa_addrand other fields can actually beNULLif the interface has no address.- No link-level address is returned if the interface has an IP address assigned.
- Also, the way of choosing either
ifa_broadaddrorifa_dstaddrdiffers on various systems. Chaos, honestly.
getifaddrs() 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. And support of address families other than IPv4 is available only on kernels that support netlink.
๐ NOTES ๐
The addresses you get back on Linux will usually be the IPv4 and IPv6 addresses assigned to the interface โ but surprise! You also get one AF_PACKET address per interface carrying lower-level details about the interface and its physical layer. Extra, but useful.
In that case, the ifa_data field may contain a pointer to a struct rtnl_link_stats, defined in <linux/if_link.h>. And for the historians: in Linux 2.4 and earlier that was struct net_device_stats, defined in <linux/netdevice.h>. Either way it holds various interface attributes and statistics ๐
โญ EXAMPLES โญ
The program below demonstrates the use of getifaddrs(), freeifaddrs(), and getnameinfo(3). And here's what we see when running this program on one system:
$ ./a.out;
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>
๐ป Program source ๐ป
#define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/if_link.h>
int main(int argc, char *argv[])
{
struct ifaddrs *ifaddr;
int family, s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
/* Walk through linked list, maintaining head pointer so we
can free list later. */
for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
/* Display interface name and family (including symbolic
form of the latter for the common families). */
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. */
if (family == AF_INET || family == AF_INET6) {
s = getnameinfo(ifa->ifa_addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
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);
}
}
freeifaddrs(ifaddr);
exit(EXIT_SUCCESS);
}
bestie reminder: that freeifaddrs(ifaddr) at the end is not optional ๐ do not forget it.
๐ฅ SEE ALSO ๐ฅ
bind(2), getsockname(2), socket(2), packet(7), ifconfig(8)
and that's the whole page, no cap ๐ this was Linux man-pages 6.20, dated 2026-02-08, getifaddrs(3) โ go forth and enumerate your interfaces, queen ๐โจ๐ฆ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2008, Petr Baudis <pasky@ucw.cz>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms