2 .\" Copyright (c) 2012 YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
3 .\" and Copyright (c) 2012 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH if_nameindex 3 (date) "Linux man-pages (unreleased)"
9 if_nameindex, if_freenameindex \- get network interface names and indexes
12 .RI ( libc ", " \-lc )
15 .B #include <net/if.h>
17 .BI "struct if_nameindex *if_nameindex(" void );
18 .BI "void if_freenameindex(struct if_nameindex *" "ptr" );
23 function returns an array of
25 structures, each containing information
26 about one of the network interfaces on the local system.
29 structure contains at least the following entries:
33 unsigned int if_index; /* Index of interface (1, 2, ...) */
34 char *if_name; /* Null\-terminated name ("eth0", etc.) */
40 field contains the interface index.
43 field points to the null-terminated interface name.
44 The end of the array is indicated by entry with
50 The data structure returned by
52 is dynamically allocated and should be freed using
53 .BR if_freenameindex ()
54 when no longer needed.
58 returns pointer to the array;
59 on error, NULL is returned, and
61 is set to indicate the error.
69 Insufficient resources available.
72 may also fail for any of the errors specified for
82 For an explanation of the terms used in this section, see
88 Interface Attribute Value
93 .BR if_freenameindex ()
94 T} Thread safety MT-Safe
97 POSIX.1-2008, RFC\ 3493.
104 the implementation supported only interfaces with IPv4 addresses.
105 Support of interfaces that don't have IPv4 addresses is available only
106 on kernels that support netlink.
108 The program below demonstrates the use of the functions described
110 An example of the output this program might produce is the following:
121 .\" SRC BEGIN (if_nameindex.c)
131 struct if_nameindex *if_ni, *i;
133 if_ni = if_nameindex();
135 perror("if_nameindex");
139 for (i = if_ni; !(i\->if_index == 0 && i\->if_name == NULL); i++)
140 printf("%u: %s\en", i\->if_index, i\->if_name);
142 if_freenameindex(if_ni);
152 .BR if_indextoname (3),
153 .BR if_nametoindex (3),