1if_nameindex(3) Library Functions Manual if_nameindex(3)
2
3
4
6 if_nameindex, if_freenameindex - get network interface names and in‐
7 dexes
8
10 Standard C library (libc, -lc)
11
13 #include <net/if.h>
14
15 struct if_nameindex *if_nameindex(void);
16 void if_freenameindex(struct if_nameindex *ptr);
17
19 The if_nameindex() function returns an array of if_nameindex struc‐
20 tures, each containing information about one of the network interfaces
21 on the local system. The if_nameindex structure contains at least the
22 following entries:
23
24 unsigned int if_index; /* Index of interface (1, 2, ...) */
25 char *if_name; /* Null-terminated name ("eth0", etc.) */
26
27 The if_index field contains the interface index. The if_name field
28 points to the null-terminated interface name. The end of the array is
29 indicated by entry with if_index set to zero and if_name set to NULL.
30
31 The data structure returned by if_nameindex() is dynamically allocated
32 and should be freed using if_freenameindex() when no longer needed.
33
35 On success, if_nameindex() returns pointer to the array; on error, NULL
36 is returned, and errno is set to indicate the error.
37
39 if_nameindex() may fail and set errno if:
40
41 ENOBUFS
42 Insufficient resources available.
43
44 if_nameindex() may also fail for any of the errors specified for
45 socket(2), bind(2), ioctl(2), getsockname(2), recvmsg(2), sendto(2), or
46 malloc(3).
47
49 For an explanation of the terms used in this section, see at‐
50 tributes(7).
51
52 ┌────────────────────────────────────────────┬───────────────┬─────────┐
53 │Interface │ Attribute │ Value │
54 ├────────────────────────────────────────────┼───────────────┼─────────┤
55 │if_nameindex(), if_freenameindex() │ Thread safety │ MT-Safe │
56 └────────────────────────────────────────────┴───────────────┴─────────┘
57
59 POSIX.1-2008, RFC 3493.
60
62 glibc 2.1. POSIX.1-2001. BSDi.
63
64 Before glibc 2.3.4, the implementation supported only interfaces with
65 IPv4 addresses. Support of interfaces that don't have IPv4 addresses
66 is available only on kernels that support netlink.
67
69 The program below demonstrates the use of the functions described on
70 this page. An example of the output this program might produce is the
71 following:
72
73 $ ./a.out
74 1: lo
75 2: wlan0
76 3: em1
77
78 Program source
79 #include <net/if.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <unistd.h>
83
84 int
85 main(void)
86 {
87 struct if_nameindex *if_ni, *i;
88
89 if_ni = if_nameindex();
90 if (if_ni == NULL) {
91 perror("if_nameindex");
92 exit(EXIT_FAILURE);
93 }
94
95 for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++)
96 printf("%u: %s\n", i->if_index, i->if_name);
97
98 if_freenameindex(if_ni);
99
100 exit(EXIT_SUCCESS);
101 }
102
104 getsockopt(2), setsockopt(2), getifaddrs(3), if_indextoname(3),
105 if_nametoindex(3), ifconfig(8)
106
107
108
109Linux man-pages 6.04 2023-03-30 if_nameindex(3)