]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/if_nameindex.3
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man3 / if_nameindex.3
CommitLineData
79bfe3d8 1.\" Copyright (c) 2012 YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
8b931bbd 2.\" and Copyright (c) 2012 Michael Kerrisk <mtk.manpages@gmail.com>
e6d86b41 3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
79bfe3d8 5.\"
45186a5d 6.TH IF_NAMEINDEX 3 2021-03-22 "Linux man-pages (unreleased)"
79bfe3d8
YH
7.SH NAME
8if_nameindex, if_freenameindex \- get network interface names and indexes
4508f8a0
AC
9.SH LIBRARY
10Standard C library
8fc3b2cf 11.RI ( libc ", " \-lc )
79bfe3d8
YH
12.SH SYNOPSIS
13.nf
14.B #include <net/if.h>
68e4db0a 15.PP
041035df 16.BI "struct if_nameindex *if_nameindex(" void );
79bfe3d8
YH
17.BI "void if_freenameindex(struct if_nameindex *" "ptr" );
18.fi
19.SH DESCRIPTION
20The
21.BR if_nameindex ()
e6d86b41 22function returns an array of
7fd06e7d
MK
23.I if_nameindex
24structures, each containing information
25about one of the network interfaces on the local system.
79bfe3d8
YH
26The
27.I if_nameindex
28structure contains at least the following entries:
51f5698d 29.PP
79bfe3d8 30.in +4n
bdd915e2
MK
31.EX
32unsigned int if_index; /* Index of interface (1, 2, ...) */
d064d41a 33char *if_name; /* Null\-terminated name ("eth0", etc.) */
bdd915e2 34.EE
79bfe3d8
YH
35.in
36.PP
37The
38.I if_index
7fd06e7d 39field contains the interface index.
79bfe3d8 40The
d0cdb0ef 41.I if_name
7fd06e7d
MK
42field points to the null-terminated interface name.
43The end of the array is indicated by entry with
44.I if_index
45set to zero and
d0cdb0ef 46.I if_name
7fd06e7d 47set to NULL.
79bfe3d8 48.PP
7fd06e7d 49The data structure returned by
79bfe3d8
YH
50.BR if_nameindex ()
51is dynamically allocated and should be freed using
52.BR if_freenameindex ()
53when no longer needed.
54.SH RETURN VALUE
55On success,
56.BR if_nameindex ()
57returns pointer to the array;
b437fdd9 58on error, NULL is returned, and
79bfe3d8 59.I errno
f6a4078b 60is set to indicate the error.
79bfe3d8
YH
61.SH ERRORS
62.BR if_nameindex ()
63may fail and set
64.I errno
65if:
66.TP
67.B ENOBUFS
ad379f3a 68Insufficient resources available.
79bfe3d8
YH
69.PP
70.BR if_nameindex ()
71may 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),
78or
79.BR malloc (3).
80.SH VERSIONS
81The
82.BR if_nameindex ()
7fd06e7d 83function first appeared in glibc 2.1, but before glibc 2.3.4,
33a0ccb2
MK
84the implementation supported only interfaces with IPv4 addresses.
85Support of interfaces that don't have IPv4 addresses is available only
79bfe3d8 86on kernels that support netlink.
147916a2
MS
87.SH ATTRIBUTES
88For an explanation of the terms used in this section, see
89.BR attributes (7).
c466875e
MK
90.ad l
91.nh
147916a2
MS
92.TS
93allbox;
c466875e 94lbx lb lb
147916a2
MS
95l l l.
96Interface Attribute Value
97T{
98.BR if_nameindex (),
147916a2
MS
99.BR if_freenameindex ()
100T} Thread safety MT-Safe
101.TE
c466875e
MK
102.hy
103.ad
847e0d88 104.sp 1
3113c7f3 105.SH STANDARDS
4d9dffba 106POSIX.1-2001, POSIX.1-2008, RFC\ 3493.
847e0d88 107.PP
7484d5a7 108This function first appeared in BSDi.
a14af333 109.SH EXAMPLES
8b931bbd
MK
110The program below demonstrates the use of the functions described
111on this page.
112An example of the output this program might produce is the following:
847e0d88 113.PP
8b931bbd 114.in +4n
b8302363 115.EX
8b931bbd
MK
116$ \fB./a.out\fI
1171: lo
1182: wlan0
1193: em1
b8302363 120.EE
8b931bbd
MK
121.in
122.SS Program source
e7d0bb47 123.EX
8b931bbd
MK
124#include <net/if.h>
125#include <stdio.h>
126#include <stdlib.h>
127#include <unistd.h>
128
129int
130main(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
e6d86b41 140 for (i = if_ni; ! (i\->if_index == 0 && i\->if_name == NULL); i++)
d1a71985 141 printf("%u: %s\en", i\->if_index, i\->if_name);
8b931bbd
MK
142
143 if_freenameindex(if_ni);
144
145 exit(EXIT_SUCCESS);
e6d86b41 146}
e7d0bb47 147.EE
79bfe3d8
YH
148.SH SEE ALSO
149.BR getsockopt (2),
150.BR setsockopt (2),
151.BR getifaddrs (3),
152.BR if_indextoname (3),
79bfe3d8
YH
153.BR if_nametoindex (3),
154.BR ifconfig (8)