1IF_NAMEINDEX(3)            Linux Programmer's Manual           IF_NAMEINDEX(3)
2
3
4

NAME

6       if_nameindex,  if_freenameindex  -  get network interface names and in‐
7       dexes
8

SYNOPSIS

10       #include <net/if.h>
11
12       struct if_nameindex *if_nameindex(void);
13       void if_freenameindex(struct if_nameindex *ptr);
14

DESCRIPTION

16       The if_nameindex() function returns an  array  of  if_nameindex  struc‐
17       tures,  each containing information about one of the network interfaces
18       on the local system.  The if_nameindex structure contains at least  the
19       following entries:
20
21           unsigned int if_index; /* Index of interface (1, 2, ...) */
22           char        *if_name;  /* Null-terminated name ("eth0", etc.) */
23
24       The  if_index  field  contains  the interface index.  The if_name field
25       points to the null-terminated interface name.  The end of the array  is
26       indicated by entry with if_index set to zero and if_name set to NULL.
27
28       The  data structure returned by if_nameindex() is dynamically allocated
29       and should be freed using if_freenameindex() when no longer needed.
30

RETURN VALUE

32       On success, if_nameindex() returns pointer to the array; on error, NULL
33       is returned, and errno is set to indicate the error.
34

ERRORS

36       if_nameindex() may fail and set errno if:
37
38       ENOBUFS
39              Insufficient resources available.
40
41       if_nameindex()  may  also  fail  for  any  of  the errors specified for
42       socket(2), bind(2), ioctl(2), getsockname(2), recvmsg(2), sendto(2), or
43       malloc(3).
44

VERSIONS

46       The  if_nameindex()  function  first  appeared in glibc 2.1, but before
47       glibc 2.3.4, the implementation supported only interfaces with IPv4 ad‐
48       dresses.   Support  of  interfaces  that  don't  have IPv4 addresses is
49       available only on kernels that support netlink.
50

ATTRIBUTES

52       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
53       tributes(7).
54
55       ┌────────────────────────────────────────────┬───────────────┬─────────┐
56Interface                                   Attribute     Value   
57       ├────────────────────────────────────────────┼───────────────┼─────────┤
58if_nameindex(), if_freenameindex()          │ Thread safety │ MT-Safe │
59       └────────────────────────────────────────────┴───────────────┴─────────┘
60

CONFORMING TO

62       POSIX.1-2001, POSIX.1-2008, RFC 3493.
63
64       This function first appeared in BSDi.
65

EXAMPLES

67       The  program  below  demonstrates the use of the functions described on
68       this page.  An example of the output this program might produce is  the
69       following:
70
71           $ ./a.out
72           1: lo
73           2: wlan0
74           3: em1
75
76   Program source
77       #include <net/if.h>
78       #include <stdio.h>
79       #include <stdlib.h>
80       #include <unistd.h>
81
82       int
83       main(int argc, char *argv[])
84       {
85           struct if_nameindex *if_ni, *i;
86
87           if_ni = if_nameindex();
88           if (if_ni == NULL) {
89               perror("if_nameindex");
90               exit(EXIT_FAILURE);
91           }
92
93           for (i = if_ni; ! (i->if_index == 0 && i->if_name == NULL); i++)
94               printf("%u: %s\n", i->if_index, i->if_name);
95
96           if_freenameindex(if_ni);
97
98           exit(EXIT_SUCCESS);
99       }
100

SEE ALSO

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

COLOPHON

106       This page is part of release 5.13 of the Linux  man-pages  project.   A
107       description  of  the project, information about reporting bugs, and the
108       latest    version    of    this    page,    can     be     found     at
109       https://www.kernel.org/doc/man-pages/.
110
111
112
113GNU                               2021-03-22                   IF_NAMEINDEX(3)
Impressum