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, or NULL if 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 your lo, your wlp3s0, your little gremlins.
  • ifa_flags โ€” the interface flags, exactly as returned by the SIOCGIFFLAGS ioctl(2) operation. (Peep netdevice(7) for the full list of these flags ๐Ÿ’–)
  • ifa_addr โ€” points to a structure holding the interface address. Consult the sa_family subfield 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 with ifa_addr, if it's applicable for the address family. Also may contain a null pointer.
  • Now the drama: depending on whether the bit IFF_BROADCAST or IFF_POINTOPOINT is set in ifa_flags (only one can be set at a time, they're not sharing that spotlight), 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.
  • ifa_data โ€” points to a buffer containing address-family-specific data. This one may be NULL if 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 errno gets 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_addr and other fields can actually be NULL if 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_broadaddr or ifa_dstaddr differs 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms