1 .\" Copyright (c) 2012 YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
2 .\" and Copyright (c) 2012 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH IF_NAMEINDEX 3 2021-03-22 "Linux man-pages (unreleased)"
8 if_nameindex, if_freenameindex \- get network interface names and indexes
11 .RI ( libc ", " \-lc )
14 .B #include <net/if.h>
16 .BI "struct if_nameindex *if_nameindex(" void );
17 .BI "void if_freenameindex(struct if_nameindex *" "ptr" );
22 function returns an array of
24 structures, each containing information
25 about one of the network interfaces on the local system.
28 structure contains at least the following entries:
32 unsigned int if_index; /* Index of interface (1, 2, ...) */
33 char *if_name; /* Null\-terminated name ("eth0", etc.) */
39 field contains the interface index.
42 field points to the null-terminated interface name.
43 The end of the array is indicated by entry with
49 The data structure returned by
51 is dynamically allocated and should be freed using
52 .BR if_freenameindex ()
53 when no longer needed.
57 returns pointer to the array;
58 on error, NULL is returned, and
60 is set to indicate the error.
68 Insufficient resources available.
71 may also fail for any of the errors specified for
83 function first appeared in glibc 2.1, but before glibc 2.3.4,
84 the implementation supported only interfaces with IPv4 addresses.
85 Support of interfaces that don't have IPv4 addresses is available only
86 on kernels that support netlink.
88 For an explanation of the terms used in this section, see
96 Interface Attribute Value
99 .BR if_freenameindex ()
100 T} Thread safety MT-Safe
106 POSIX.1-2001, POSIX.1-2008, RFC\ 3493.
108 This function first appeared in BSDi.
110 The program below demonstrates the use of the functions described
112 An example of the output this program might produce is the following:
130 main(int argc, char *argv[])
132 struct if_nameindex *if_ni, *i;
134 if_ni = if_nameindex();
136 perror("if_nameindex");
140 for (i = if_ni; ! (i\->if_index == 0 && i\->if_name == NULL); i++)
141 printf("%u: %s\en", i\->if_index, i\->if_name);
143 if_freenameindex(if_ni);
152 .BR if_indextoname (3),
153 .BR if_nametoindex (3),