if_nameindex(3)

✨ if_nameindex(3) ✨

okay bestie, gather round, because today we're reading a page from the Library Functions Manual and it is lowkey about to change how you feel about network interfaces πŸ’– (yes the man page name is literally there in the title, we're being fancy today, it's the whole vibe).


🌸 NAME 🌸

if_nameindex, if_freenameindex β€” get network interface names and indexes

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc) 🫢

πŸŽ€ SYNOPSIS πŸŽ€

#include <net/if.h>

struct if_nameindex *if_nameindex(void);
void if_freenameindex(struct if_nameindex *ptr);

πŸ’… DESCRIPTION πŸ’…

okay so if_nameindex() hands you back an array of if_nameindex structs, and each one is giving juicy gossip about exactly one of the network interfaces chilling on your local system. iconic behaviour honestly 🌈

The if_nameindex structure contains at least these entries:

unsigned int if_index;  /* Index of interface (1, 2, ...) */
char        *if_name;   /* Null-terminated name ("eth0", etc.) */

The if_index field is where the interface index lives. The if_name field points at the null-terminated interface name. And how do you know you've hit the end of the array? The entry where if_index is zero and if_name is NULL β€” that's the little "we're done here" marker, periodt πŸ’€βœ¨

The data structure that if_nameindex() returns is dynamically allocated, and you must free it with if_freenameindex() once you're done with it. No ghost memory leaks in this house, bestie, we do not leave the party without cleaning up πŸ§ΉπŸ’–

πŸ‘‘ RETURN VALUE πŸ‘‘

On success, if_nameindex() returns a pointer to the array. On error, NULL is returned, and errno gets set to say what went wrong 😭

🚨 ERRORS 🚨

if_nameindex() may fail and set errno if:

  • ENOBUFS β€” Insufficient resources available. (she's out of resources, tragic, no cap)

if_nameindex() may also fail for any of the errors specified for socket(2), bind(2), ioctl(2), getsockname(2), recvmsg(2), sendto(2), or malloc(3). so like, it's not just doing its own thing, it's borrowing drama from all of those too πŸ™ˆ

πŸ¦‹ ATTRIBUTES πŸ¦‹

For an explanation of the terms used in this section, see attributes(7).

  • Interface: if_nameindex(), if_freenameindex()
  • Attribute: Thread safety
  • Value: MT-Safe

so yes bestie, both of them are MT-Safe, she's safe, she's stable, she's multitasking-friendly πŸ’…

⭐ STANDARDS ⭐

POSIX.1-2008, RFC 3493.

🌈 HISTORY 🌈

glibc 2.1. POSIX.1-2001. BSDi.

Before glibc 2.3.4, the implementation supported only interfaces with IPv4 addresses. Support of interfaces that don't have IPv4 addresses is available only on kernels that support netlink. (iykyk β€” old glibc was not it, and if your kernel can't do netlink, sorry babe 😭)

πŸ”₯ EXAMPLES πŸ”₯

The program below demonstrates the use of the functions described on this page. An example of the output this program might produce is the following:

$ ./a.out;
1: lo
2: wlan0
3: em1

🌸 Program source 🌸

#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(void)
{
    struct if_nameindex *if_ni, *i;

    if_ni = if_nameindex();
    if (if_ni == NULL) {
        perror("if_nameindex");
        exit(EXIT_FAILURE);
    }

    for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++)
        printf("%u: %s\n", i->if_index, i->if_name);

    if_freenameindex(if_ni);

    exit(EXIT_SUCCESS);
}

look at that loop condition β€” !(i->if_index == 0 && i->if_name == NULL) β€” she walks the array until the sentinel shows up, then frees, then exits. main character energy, honestly 🫢 and yes, if_freenameindex(if_ni) at the end, because we always clean up ✨

πŸ’œ SEE ALSO πŸ’œ

getsockopt(2), setsockopt(2), getifaddrs(3), if_indextoname(3), if_nametoindex(3), ifconfig(8)


and that's the whole page bestie πŸ’– this one came from Linux man-pages 6.20, dated 2026-02-08, and it's if_nameindex(3) β€” go forth, grab your interface names, free your memory, slay πŸ’…πŸŒΈβœ¨

πŸ“¦ 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