]>
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 | .\" |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
79bfe3d8 YH |
5 | .\" Permission is granted to make and distribute verbatim copies of this |
6 | .\" manual provided the copyright notice and this permission notice are | |
7 | .\" preserved on all copies. | |
8 | .\" | |
9 | .\" Permission is granted to copy and distribute modified versions of | |
10 | .\" this manual under the conditions for verbatim copying, provided that | |
11 | .\" the entire resulting derived work is distributed under the terms of | |
12 | .\" a permission notice identical to this one. | |
13 | .\" | |
14 | .\" Since the Linux kernel and libraries are constantly changing, this | |
62290181 SP |
15 | .\" manual page may be incorrect or out-of-date. The author(s) assume |
16 | .\" no responsibility for errors or omissions, or for damages resulting | |
17 | .\" from the use of the information contained herein. The author(s) may | |
18 | .\" not have taken the same level of care in the production of this | |
19 | .\" manual, which is licensed free of charge, as they might when working | |
79bfe3d8 YH |
20 | .\" professionally. |
21 | .\" | |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by | |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
79bfe3d8 | 25 | .\" |
4b8c67d9 | 26 | .TH IF_NAMEINDEX 3 2017-09-15 "GNU" "Linux Programmer's Manual" |
79bfe3d8 YH |
27 | .SH NAME |
28 | if_nameindex, if_freenameindex \- get network interface names and indexes | |
29 | .SH SYNOPSIS | |
30 | .nf | |
31 | .B #include <net/if.h> | |
68e4db0a | 32 | .PP |
79bfe3d8 YH |
33 | .BI "struct if_nameindex *if_nameindex(void); |
34 | .BI "void if_freenameindex(struct if_nameindex *" "ptr" ); | |
35 | .fi | |
36 | .SH DESCRIPTION | |
37 | The | |
38 | .BR if_nameindex () | |
e6d86b41 | 39 | function returns an array of |
7fd06e7d MK |
40 | .I if_nameindex |
41 | structures, each containing information | |
42 | about one of the network interfaces on the local system. | |
79bfe3d8 YH |
43 | The |
44 | .I if_nameindex | |
45 | structure contains at least the following entries: | |
51f5698d | 46 | .PP |
79bfe3d8 | 47 | .in +4n |
bdd915e2 MK |
48 | .EX |
49 | unsigned int if_index; /* Index of interface (1, 2, ...) */ | |
50 | char *if_name; /* Null-terminated name ("eth0", etc.) */ | |
51 | .EE | |
79bfe3d8 YH |
52 | .in |
53 | .PP | |
54 | The | |
55 | .I if_index | |
7fd06e7d | 56 | field contains the interface index. |
79bfe3d8 | 57 | The |
d0cdb0ef | 58 | .I if_name |
7fd06e7d MK |
59 | field points to the null-terminated interface name. |
60 | The end of the array is indicated by entry with | |
61 | .I if_index | |
62 | set to zero and | |
d0cdb0ef | 63 | .I if_name |
7fd06e7d | 64 | set to NULL. |
79bfe3d8 | 65 | .PP |
7fd06e7d | 66 | The data structure returned by |
79bfe3d8 YH |
67 | .BR if_nameindex () |
68 | is dynamically allocated and should be freed using | |
69 | .BR if_freenameindex () | |
70 | when no longer needed. | |
71 | .SH RETURN VALUE | |
72 | On success, | |
73 | .BR if_nameindex () | |
74 | returns pointer to the array; | |
b437fdd9 | 75 | on error, NULL is returned, and |
79bfe3d8 YH |
76 | .I errno |
77 | is set appropriately. | |
78 | .SH ERRORS | |
79 | .BR if_nameindex () | |
80 | may fail and set | |
81 | .I errno | |
82 | if: | |
83 | .TP | |
84 | .B ENOBUFS | |
ad379f3a | 85 | Insufficient resources available. |
79bfe3d8 YH |
86 | .PP |
87 | .BR if_nameindex () | |
88 | may also fail for any of the errors specified for | |
89 | .BR socket (2), | |
90 | .BR bind (2), | |
91 | .BR ioctl (2), | |
92 | .BR getsockname (2), | |
93 | .BR recvmsg (2), | |
94 | .BR sendto (2), | |
95 | or | |
96 | .BR malloc (3). | |
97 | .SH VERSIONS | |
98 | The | |
99 | .BR if_nameindex () | |
7fd06e7d | 100 | function first appeared in glibc 2.1, but before glibc 2.3.4, |
33a0ccb2 MK |
101 | the implementation supported only interfaces with IPv4 addresses. |
102 | Support of interfaces that don't have IPv4 addresses is available only | |
79bfe3d8 | 103 | on kernels that support netlink. |
147916a2 MS |
104 | .SH ATTRIBUTES |
105 | For an explanation of the terms used in this section, see | |
106 | .BR attributes (7). | |
107 | .TS | |
108 | allbox; | |
109 | lb lb lb | |
110 | l l l. | |
111 | Interface Attribute Value | |
112 | T{ | |
113 | .BR if_nameindex (), | |
114 | .br | |
115 | .BR if_freenameindex () | |
116 | T} Thread safety MT-Safe | |
117 | .TE | |
847e0d88 | 118 | .sp 1 |
7484d5a7 | 119 | .SH CONFORMING TO |
4d9dffba | 120 | POSIX.1-2001, POSIX.1-2008, RFC\ 3493. |
847e0d88 | 121 | .PP |
7484d5a7 | 122 | This function first appeared in BSDi. |
8b931bbd MK |
123 | .SH EXAMPLE |
124 | The program below demonstrates the use of the functions described | |
125 | on this page. | |
126 | An example of the output this program might produce is the following: | |
847e0d88 | 127 | .PP |
8b931bbd | 128 | .in +4n |
b8302363 | 129 | .EX |
8b931bbd MK |
130 | $ \fB./a.out\fI |
131 | 1: lo | |
132 | 2: wlan0 | |
133 | 3: em1 | |
b8302363 | 134 | .EE |
8b931bbd MK |
135 | .in |
136 | .SS Program source | |
e7d0bb47 | 137 | .EX |
8b931bbd MK |
138 | #include <net/if.h> |
139 | #include <stdio.h> | |
140 | #include <stdlib.h> | |
141 | #include <unistd.h> | |
142 | ||
143 | int | |
144 | main(int argc, char *argv[]) | |
145 | { | |
146 | struct if_nameindex *if_ni, *i; | |
147 | ||
148 | if_ni = if_nameindex(); | |
149 | if (if_ni == NULL) { | |
150 | perror("if_nameindex"); | |
151 | exit(EXIT_FAILURE); | |
152 | } | |
153 | ||
e6d86b41 | 154 | for (i = if_ni; ! (i\->if_index == 0 && i\->if_name == NULL); i++) |
f2b5a969 | 155 | printf("%u: %s\\n", i\->if_index, i\->if_name); |
8b931bbd MK |
156 | |
157 | if_freenameindex(if_ni); | |
158 | ||
159 | exit(EXIT_SUCCESS); | |
e6d86b41 | 160 | } |
e7d0bb47 | 161 | .EE |
79bfe3d8 YH |
162 | .SH SEE ALSO |
163 | .BR getsockopt (2), | |
164 | .BR setsockopt (2), | |
165 | .BR getifaddrs (3), | |
166 | .BR if_indextoname (3), | |
79bfe3d8 YH |
167 | .BR if_nametoindex (3), |
168 | .BR ifconfig (8) |