1 .\" Copyright (c) 2012 YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
2 .\" and Copyright (c) 2012 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of
10 .\" this manual under the conditions for verbatim copying, provided that
11 .\" the entire resulting derived work is distributed under the terms of
12 .\" a permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume
16 .\" no responsibility for errors or omissions, or for damages resulting
17 .\" from the use of the information contained herein. The author(s) may
18 .\" not have taken the same level of care in the production of this
19 .\" manual, which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH IF_NAMEINDEX 3 2017-09-15 "GNU" "Linux Programmer's Manual"
28 if_nameindex, if_freenameindex \- get network interface names and indexes
31 .B #include <net/if.h>
33 .BI "struct if_nameindex *if_nameindex(void);
34 .BI "void if_freenameindex(struct if_nameindex *" "ptr" );
39 function returns an array of
41 structures, each containing information
42 about one of the network interfaces on the local system.
45 structure contains at least the following entries:
49 unsigned int if_index; /* Index of interface (1, 2, ...) */
50 char *if_name; /* Null-terminated name ("eth0", etc.) */
56 field contains the interface index.
59 field points to the null-terminated interface name.
60 The end of the array is indicated by entry with
66 The data structure returned by
68 is dynamically allocated and should be freed using
69 .BR if_freenameindex ()
70 when no longer needed.
74 returns pointer to the array;
75 on error, NULL is returned, and
85 Insufficient resources available.
88 may also fail for any of the errors specified for
100 function first appeared in glibc 2.1, but before glibc 2.3.4,
101 the implementation supported only interfaces with IPv4 addresses.
102 Support of interfaces that don't have IPv4 addresses is available only
103 on kernels that support netlink.
105 For an explanation of the terms used in this section, see
111 Interface Attribute Value
115 .BR if_freenameindex ()
116 T} Thread safety MT-Safe
120 POSIX.1-2001, POSIX.1-2008, RFC\ 3493.
122 This function first appeared in BSDi.
124 The program below demonstrates the use of the functions described
126 An example of the output this program might produce is the following:
144 main(int argc, char *argv[])
146 struct if_nameindex *if_ni, *i;
148 if_ni = if_nameindex();
150 perror("if_nameindex");
154 for (i = if_ni; ! (i\->if_index == 0 && i\->if_name == NULL); i++)
155 printf("%u: %s\\n", i\->if_index, i\->if_name);
157 if_freenameindex(if_ni);
166 .BR if_indextoname (3),
167 .BR if_nametoindex (3),