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
84 function first appeared in glibc 2.1, but before glibc 2.3.4,
85 the implementation supported only interfaces with IPv4 addresses.
86 Support of interfaces that don't have IPv4 addresses is available only
87 on kernels that support netlink.
89 For an explanation of the terms used in this section, see
97 Interface Attribute Value
100 .BR if_freenameindex ()
101 T} Thread safety MT-Safe
107 POSIX.1-2001, POSIX.1-2008, RFC\ 3493.
109 This function first appeared in BSDi.
111 The program below demonstrates the use of the functions described
113 An example of the output this program might produce is the following:
124 .\" SRC BEGIN (if_nameindex.c)
134 struct if_nameindex *if_ni, *i;
136 if_ni = if_nameindex();
138 perror("if_nameindex");
142 for (i = if_ni; !(i\->if_index == 0 && i\->if_name == NULL); i++)
143 printf("%u: %s\en", i\->if_index, i\->if_name);
145 if_freenameindex(if_ni);
155 .BR if_indextoname (3),
156 .BR if_nametoindex (3),