]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/if_nameindex.3
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man3 / if_nameindex.3
1 .\" Copyright (c) 2012 YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
2 .\" and Copyright (c) 2012 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH IF_NAMEINDEX 3 2021-03-22 "Linux man-pages (unreleased)"
7 .SH NAME
8 if_nameindex, if_freenameindex \- get network interface names and indexes
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ", " \-lc )
12 .SH SYNOPSIS
13 .nf
14 .B #include <net/if.h>
15 .PP
16 .BI "struct if_nameindex *if_nameindex(" void );
17 .BI "void if_freenameindex(struct if_nameindex *" "ptr" );
18 .fi
19 .SH DESCRIPTION
20 The
21 .BR if_nameindex ()
22 function returns an array of
23 .I if_nameindex
24 structures, each containing information
25 about one of the network interfaces on the local system.
26 The
27 .I if_nameindex
28 structure contains at least the following entries:
29 .PP
30 .in +4n
31 .EX
32 unsigned int if_index; /* Index of interface (1, 2, ...) */
33 char *if_name; /* Null\-terminated name ("eth0", etc.) */
34 .EE
35 .in
36 .PP
37 The
38 .I if_index
39 field contains the interface index.
40 The
41 .I if_name
42 field points to the null-terminated interface name.
43 The end of the array is indicated by entry with
44 .I if_index
45 set to zero and
46 .I if_name
47 set to NULL.
48 .PP
49 The data structure returned by
50 .BR if_nameindex ()
51 is dynamically allocated and should be freed using
52 .BR if_freenameindex ()
53 when no longer needed.
54 .SH RETURN VALUE
55 On success,
56 .BR if_nameindex ()
57 returns pointer to the array;
58 on error, NULL is returned, and
59 .I errno
60 is set to indicate the error.
61 .SH ERRORS
62 .BR if_nameindex ()
63 may fail and set
64 .I errno
65 if:
66 .TP
67 .B ENOBUFS
68 Insufficient resources available.
69 .PP
70 .BR if_nameindex ()
71 may also fail for any of the errors specified for
72 .BR socket (2),
73 .BR bind (2),
74 .BR ioctl (2),
75 .BR getsockname (2),
76 .BR recvmsg (2),
77 .BR sendto (2),
78 or
79 .BR malloc (3).
80 .SH VERSIONS
81 The
82 .BR if_nameindex ()
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.
87 .SH ATTRIBUTES
88 For an explanation of the terms used in this section, see
89 .BR attributes (7).
90 .ad l
91 .nh
92 .TS
93 allbox;
94 lbx lb lb
95 l l l.
96 Interface Attribute Value
97 T{
98 .BR if_nameindex (),
99 .BR if_freenameindex ()
100 T} Thread safety MT-Safe
101 .TE
102 .hy
103 .ad
104 .sp 1
105 .SH STANDARDS
106 POSIX.1-2001, POSIX.1-2008, RFC\ 3493.
107 .PP
108 This function first appeared in BSDi.
109 .SH EXAMPLES
110 The program below demonstrates the use of the functions described
111 on this page.
112 An example of the output this program might produce is the following:
113 .PP
114 .in +4n
115 .EX
116 $ \fB./a.out\fI
117 1: lo
118 2: wlan0
119 3: em1
120 .EE
121 .in
122 .SS Program source
123 .EX
124 #include <net/if.h>
125 #include <stdio.h>
126 #include <stdlib.h>
127 #include <unistd.h>
128
129 int
130 main(int argc, char *argv[])
131 {
132 struct if_nameindex *if_ni, *i;
133
134 if_ni = if_nameindex();
135 if (if_ni == NULL) {
136 perror("if_nameindex");
137 exit(EXIT_FAILURE);
138 }
139
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);
142
143 if_freenameindex(if_ni);
144
145 exit(EXIT_SUCCESS);
146 }
147 .EE
148 .SH SEE ALSO
149 .BR getsockopt (2),
150 .BR setsockopt (2),
151 .BR getifaddrs (3),
152 .BR if_indextoname (3),
153 .BR if_nametoindex (3),
154 .BR ifconfig (8)