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