]>
Commit | Line | Data |
---|---|---|
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 |
8 | if_nameindex, if_freenameindex \- get network interface names and indexes | |
4508f8a0 AC |
9 | .SH LIBRARY |
10 | Standard 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 | |
20 | The | |
21 | .BR if_nameindex () | |
e6d86b41 | 22 | function returns an array of |
7fd06e7d MK |
23 | .I if_nameindex |
24 | structures, each containing information | |
25 | about one of the network interfaces on the local system. | |
79bfe3d8 YH |
26 | The |
27 | .I if_nameindex | |
28 | structure contains at least the following entries: | |
51f5698d | 29 | .PP |
79bfe3d8 | 30 | .in +4n |
bdd915e2 MK |
31 | .EX |
32 | unsigned int if_index; /* Index of interface (1, 2, ...) */ | |
d064d41a | 33 | char *if_name; /* Null\-terminated name ("eth0", etc.) */ |
bdd915e2 | 34 | .EE |
79bfe3d8 YH |
35 | .in |
36 | .PP | |
37 | The | |
38 | .I if_index | |
7fd06e7d | 39 | field contains the interface index. |
79bfe3d8 | 40 | The |
d0cdb0ef | 41 | .I if_name |
7fd06e7d MK |
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 | |
d0cdb0ef | 46 | .I if_name |
7fd06e7d | 47 | set to NULL. |
79bfe3d8 | 48 | .PP |
7fd06e7d | 49 | The data structure returned by |
79bfe3d8 YH |
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; | |
b437fdd9 | 58 | on error, NULL is returned, and |
79bfe3d8 | 59 | .I errno |
f6a4078b | 60 | is set to indicate the error. |
79bfe3d8 YH |
61 | .SH ERRORS |
62 | .BR if_nameindex () | |
63 | may fail and set | |
64 | .I errno | |
65 | if: | |
66 | .TP | |
67 | .B ENOBUFS | |
ad379f3a | 68 | Insufficient resources available. |
79bfe3d8 YH |
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 () | |
7fd06e7d | 83 | function first appeared in glibc 2.1, but before glibc 2.3.4, |
33a0ccb2 MK |
84 | the implementation supported only interfaces with IPv4 addresses. |
85 | Support of interfaces that don't have IPv4 addresses is available only | |
79bfe3d8 | 86 | on kernels that support netlink. |
147916a2 MS |
87 | .SH ATTRIBUTES |
88 | For 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 |
93 | allbox; | |
c466875e | 94 | lbx lb lb |
147916a2 MS |
95 | l l l. |
96 | Interface Attribute Value | |
97 | T{ | |
98 | .BR if_nameindex (), | |
147916a2 MS |
99 | .BR if_freenameindex () |
100 | T} Thread safety MT-Safe | |
101 | .TE | |
c466875e MK |
102 | .hy |
103 | .ad | |
847e0d88 | 104 | .sp 1 |
3113c7f3 | 105 | .SH STANDARDS |
4d9dffba | 106 | POSIX.1-2001, POSIX.1-2008, RFC\ 3493. |
847e0d88 | 107 | .PP |
7484d5a7 | 108 | This function first appeared in BSDi. |
a14af333 | 109 | .SH EXAMPLES |
8b931bbd MK |
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: | |
847e0d88 | 113 | .PP |
8b931bbd | 114 | .in +4n |
b8302363 | 115 | .EX |
8b931bbd MK |
116 | $ \fB./a.out\fI |
117 | 1: lo | |
118 | 2: wlan0 | |
119 | 3: 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 | ||
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 | ||
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) |