1IF_NAMEINDEX(3) Linux Programmer's Manual IF_NAMEINDEX(3)
2
3
4
6 if_nameindex, if_freenameindex - get network interface names and in‐
7 dexes
8
10 #include <net/if.h>
11
12 struct if_nameindex *if_nameindex(void);
13 void if_freenameindex(struct if_nameindex *ptr);
14
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
32 On success, if_nameindex() returns pointer to the array; on error, NULL
33 is returned, and errno is set appropriately.
34
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
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
52 For an explanation of the terms used in this section, see at‐
53 tributes(7).
54
55 ┌───────────────────┬───────────────┬─────────┐
56 │Interface │ Attribute │ Value │
57 ├───────────────────┼───────────────┼─────────┤
58 │if_nameindex(), │ Thread safety │ MT-Safe │
59 │if_freenameindex() │ │ │
60 └───────────────────┴───────────────┴─────────┘
61
63 POSIX.1-2001, POSIX.1-2008, RFC 3493.
64
65 This function first appeared in BSDi.
66
68 The program below demonstrates the use of the functions described on
69 this page. An example of the output this program might produce is the
70 following:
71
72 $ ./a.out
73 1: lo
74 2: wlan0
75 3: em1
76
77 Program source
78 #include <net/if.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <unistd.h>
82
83 int
84 main(int argc, char *argv[])
85 {
86 struct if_nameindex *if_ni, *i;
87
88 if_ni = if_nameindex();
89 if (if_ni == NULL) {
90 perror("if_nameindex");
91 exit(EXIT_FAILURE);
92 }
93
94 for (i = if_ni; ! (i->if_index == 0 && i->if_name == NULL); i++)
95 printf("%u: %s\n", i->if_index, i->if_name);
96
97 if_freenameindex(if_ni);
98
99 exit(EXIT_SUCCESS);
100 }
101
103 getsockopt(2), setsockopt(2), getifaddrs(3), if_indextoname(3),
104 if_nametoindex(3), ifconfig(8)
105
107 This page is part of release 5.10 of the Linux man-pages project. A
108 description of the project, information about reporting bugs, and the
109 latest version of this page, can be found at
110 https://www.kernel.org/doc/man-pages/.
111
112
113
114GNU 2020-06-09 IF_NAMEINDEX(3)