README: Update links
[man-pages.git] / man3 / if_nameindex.3
blob32afb1763ef4776952cc153c30fece781f38062a
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>
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:
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
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.
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.
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 ATTRIBUTES
82 For an explanation of the terms used in this section, see
83 .BR attributes (7).
84 .TS
85 allbox;
86 lbx lb lb
87 l l l.
88 Interface       Attribute       Value
90 .na
91 .nh
92 .BR if_nameindex (),
93 .BR if_freenameindex ()
94 T}      Thread safety   MT-Safe
95 .TE
96 .SH STANDARDS
97 POSIX.1-2008, RFC\ 3493.
98 .SH HISTORY
99 glibc 2.1.
100 POSIX.1-2001.
101 BSDi.
103 Before glibc 2.3.4,
104 the implementation supported only interfaces with IPv4 addresses.
105 Support of interfaces that don't have IPv4 addresses is available only
106 on kernels that support netlink.
107 .SH EXAMPLES
108 The program below demonstrates the use of the functions described
109 on this page.
110 An example of the output this program might produce is the following:
112 .in +4n
114 $ \fB./a.out\fI
115 1: lo
116 2: wlan0
117 3: em1
120 .SS Program source
121 .\" SRC BEGIN (if_nameindex.c)
123 #include <net/if.h>
124 #include <stdio.h>
125 #include <stdlib.h>
126 #include <unistd.h>
129 main(void)
131     struct if_nameindex *if_ni, *i;
133     if_ni = if_nameindex();
134     if (if_ni == NULL) {
135         perror("if_nameindex");
136         exit(EXIT_FAILURE);
137     }
139     for (i = if_ni; !(i\->if_index == 0 && i\->if_name == NULL); i++)
140         printf("%u: %s\en", i\->if_index, i\->if_name);
142     if_freenameindex(if_ni);
144     exit(EXIT_SUCCESS);
147 .\" SRC END
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)