1IF_NAMEINDEX(3) Linux Programmer's Manual IF_NAMEINDEX(3)
2
3
4
6 if_nameindex, if_freenameindex - get network interface names and
7 indexes
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 ifa_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 ifa_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, a
33 NULL pointer 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
48 addresses. Support of interfaces that don't have IPv4 addresses is
49 available only on kernels that support netlink.
50
52 RFC 3493, POSIX.1-2001.
53
54 This function first appeared in BSDi.
55
57 The program below demonstrates the use of the functions described on
58 this page. An example of the output this program might produce is the
59 following:
60 $ ./a.out
61 1: lo
62 2: wlan0
63 3: em1
64
65 Program source
66 #include <net/if.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <unistd.h>
70
71 int
72 main(int argc, char *argv[])
73 {
74 struct if_nameindex *if_ni, *i;
75
76 if_ni = if_nameindex();
77 if (if_ni == NULL) {
78 perror("if_nameindex");
79 exit(EXIT_FAILURE);
80 }
81
82 for (i = if_ni; ! (i->if_index == 0 && i->if_name == NULL); i++)
83 printf("%u: %s\n", i->if_index, i->if_name);
84
85 if_freenameindex(if_ni);
86
87 exit(EXIT_SUCCESS);
88 }
89
91 getsockopt(2), setsockopt(2), getifaddrs(3), if_indextoname(3),
92 if_nametoindex(3), ifconfig(8)
93
95 This page is part of release 3.53 of the Linux man-pages project. A
96 description of the project, and information about reporting bugs, can
97 be found at http://www.kernel.org/doc/man-pages/.
98
99
100
101GNU 2012-11-21 IF_NAMEINDEX(3)