1 //! Network interface name resolution.
2 //!
3 //! Uses Linux and/or POSIX functions to resolve interface names like "eth0"
4 //! or "socan1" into device numbers.
5
6 use libc::c_uint;
7 use crate::{Result, Error, NixPath};
8
9 /// Resolve an interface into a interface number.
if_nametoindex<P: ?Sized + NixPath>(name: &P) -> Result<c_uint>10 pub fn if_nametoindex<P: ?Sized + NixPath>(name: &P) -> Result<c_uint> {
11 let if_index = name.with_nix_path(|name| unsafe { libc::if_nametoindex(name.as_ptr()) })?;
12
13 if if_index == 0 {
14 Err(Error::last())
15 } else {
16 Ok(if_index)
17 }
18 }
19
20 libc_bitflags!(
21 /// Standard interface flags, used by `getifaddrs`
22 pub struct InterfaceFlags: libc::c_int {
23 /// Interface is running. (see
24 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
25 IFF_UP;
26 /// Valid broadcast address set. (see
27 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
28 IFF_BROADCAST;
29 /// Internal debugging flag. (see
30 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
31 IFF_DEBUG;
32 /// Interface is a loopback interface. (see
33 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
34 IFF_LOOPBACK;
35 /// Interface is a point-to-point link. (see
36 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
37 IFF_POINTOPOINT;
38 /// Avoid use of trailers. (see
39 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
40 #[cfg(any(target_os = "android",
41 target_os = "fuchsia",
42 target_os = "ios",
43 target_os = "linux",
44 target_os = "macos",
45 target_os = "netbsd",
46 target_os = "solaris"))]
47 IFF_NOTRAILERS;
48 /// Interface manages own routes.
49 #[cfg(any(target_os = "dragonfly"))]
50 IFF_SMART;
51 /// Resources allocated. (see
52 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
53 #[cfg(any(target_os = "android",
54 target_os = "dragonfly",
55 target_os = "freebsd",
56 target_os = "fuchsia",
57 target_os = "ios",
58 target_os = "linux",
59 target_os = "macos",
60 target_os = "netbsd",
61 target_os = "openbsd",
62 target_os = "solaris"))]
63 IFF_RUNNING;
64 /// No arp protocol, L2 destination address not set. (see
65 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
66 IFF_NOARP;
67 /// Interface is in promiscuous mode. (see
68 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
69 IFF_PROMISC;
70 /// Receive all multicast packets. (see
71 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
72 IFF_ALLMULTI;
73 /// Master of a load balancing bundle. (see
74 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
75 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
76 IFF_MASTER;
77 /// transmission in progress, tx hardware queue is full
78 #[cfg(any(target_os = "freebsd",
79 target_os = "macos",
80 target_os = "netbsd",
81 target_os = "openbsd",
82 target_os = "ios"))]
83 IFF_OACTIVE;
84 /// Protocol code on board.
85 #[cfg(target_os = "solaris")]
86 IFF_INTELLIGENT;
87 /// Slave of a load balancing bundle. (see
88 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
89 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
90 IFF_SLAVE;
91 /// Can't hear own transmissions.
92 #[cfg(any(target_os = "dragonfly",
93 target_os = "freebsd",
94 target_os = "macos",
95 target_os = "netbsd",
96 target_os = "openbsd",
97 target_os = "osx"))]
98 IFF_SIMPLEX;
99 /// Supports multicast. (see
100 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
101 IFF_MULTICAST;
102 /// Per link layer defined bit.
103 #[cfg(any(target_os = "dragonfly",
104 target_os = "freebsd",
105 target_os = "macos",
106 target_os = "netbsd",
107 target_os = "openbsd",
108 target_os = "ios"))]
109 IFF_LINK0;
110 /// Multicast using broadcast.
111 #[cfg(any(target_os = "solaris"))]
112 IFF_MULTI_BCAST;
113 /// Is able to select media type via ifmap. (see
114 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
115 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
116 IFF_PORTSEL;
117 /// Per link layer defined bit.
118 #[cfg(any(target_os = "dragonfly",
119 target_os = "freebsd",
120 target_os = "macos",
121 target_os = "netbsd",
122 target_os = "openbsd",
123 target_os = "ios"))]
124 IFF_LINK1;
125 /// Non-unique address.
126 #[cfg(any(target_os = "solaris"))]
127 IFF_UNNUMBERED;
128 /// Auto media selection active. (see
129 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
130 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
131 IFF_AUTOMEDIA;
132 /// Per link layer defined bit.
133 #[cfg(any(target_os = "dragonfly",
134 target_os = "freebsd",
135 target_os = "macos",
136 target_os = "netbsd",
137 target_os = "openbsd",
138 target_os = "ios"))]
139 IFF_LINK2;
140 /// Use alternate physical connection.
141 #[cfg(any(target_os = "dragonfly",
142 target_os = "freebsd",
143 target_os = "macos",
144 target_os = "ios"))]
145 IFF_ALTPHYS;
146 /// DHCP controlls interface.
147 #[cfg(any(target_os = "solaris"))]
148 IFF_DHCPRUNNING;
149 /// The addresses are lost when the interface goes down. (see
150 /// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
151 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
152 IFF_DYNAMIC;
153 /// Do not advertise.
154 #[cfg(any(target_os = "solaris"))]
155 IFF_PRIVATE;
156 /// Driver signals L1 up. Volatile.
157 #[cfg(any(target_os = "fuchsia", target_os = "linux"))]
158 IFF_LOWER_UP;
159 /// Interface is in polling mode.
160 #[cfg(any(target_os = "dragonfly"))]
161 IFF_POLLING_COMPAT;
162 /// Unconfigurable using ioctl(2).
163 #[cfg(any(target_os = "freebsd"))]
164 IFF_CANTCONFIG;
165 /// Do not transmit packets.
166 #[cfg(any(target_os = "solaris"))]
167 IFF_NOXMIT;
168 /// Driver signals dormant. Volatile.
169 #[cfg(any(target_os = "fuchsia", target_os = "linux"))]
170 IFF_DORMANT;
171 /// User-requested promisc mode.
172 #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
173 IFF_PPROMISC;
174 /// Just on-link subnet.
175 #[cfg(any(target_os = "solaris"))]
176 IFF_NOLOCAL;
177 /// Echo sent packets. Volatile.
178 #[cfg(any(target_os = "fuchsia", target_os = "linux"))]
179 IFF_ECHO;
180 /// User-requested monitor mode.
181 #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
182 IFF_MONITOR;
183 /// Address is deprecated.
184 #[cfg(any(target_os = "solaris"))]
185 IFF_DEPRECATED;
186 /// Static ARP.
187 #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
188 IFF_STATICARP;
189 /// Address from stateless addrconf.
190 #[cfg(any(target_os = "solaris"))]
191 IFF_ADDRCONF;
192 /// Interface is in polling mode.
193 #[cfg(any(target_os = "dragonfly"))]
194 IFF_NPOLLING;
195 /// Router on interface.
196 #[cfg(any(target_os = "solaris"))]
197 IFF_ROUTER;
198 /// Interface is in polling mode.
199 #[cfg(any(target_os = "dragonfly"))]
200 IFF_IDIRECT;
201 /// Interface is winding down
202 #[cfg(any(target_os = "freebsd"))]
203 IFF_DYING;
204 /// No NUD on interface.
205 #[cfg(any(target_os = "solaris"))]
206 IFF_NONUD;
207 /// Interface is being renamed
208 #[cfg(any(target_os = "freebsd"))]
209 IFF_RENAMING;
210 /// Anycast address.
211 #[cfg(any(target_os = "solaris"))]
212 IFF_ANYCAST;
213 /// Don't exchange routing info.
214 #[cfg(any(target_os = "solaris"))]
215 IFF_NORTEXCH;
216 /// Do not provide packet information
217 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
218 IFF_NO_PI as libc::c_int;
219 /// TUN device (no Ethernet headers)
220 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
221 IFF_TUN as libc::c_int;
222 /// TAP device
223 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
224 IFF_TAP as libc::c_int;
225 /// IPv4 interface.
226 #[cfg(any(target_os = "solaris"))]
227 IFF_IPV4;
228 /// IPv6 interface.
229 #[cfg(any(target_os = "solaris"))]
230 IFF_IPV6;
231 /// in.mpathd test address
232 #[cfg(any(target_os = "solaris"))]
233 IFF_NOFAILOVER;
234 /// Interface has failed
235 #[cfg(any(target_os = "solaris"))]
236 IFF_FAILED;
237 /// Interface is a hot-spare
238 #[cfg(any(target_os = "solaris"))]
239 IFF_STANDBY;
240 /// Functioning but not used
241 #[cfg(any(target_os = "solaris"))]
242 IFF_INACTIVE;
243 /// Interface is offline
244 #[cfg(any(target_os = "solaris"))]
245 IFF_OFFLINE;
246 #[cfg(any(target_os = "solaris"))]
247 IFF_COS_ENABLED;
248 /// Prefer as source addr.
249 #[cfg(any(target_os = "solaris"))]
250 IFF_PREFERRED;
251 /// RFC3041
252 #[cfg(any(target_os = "solaris"))]
253 IFF_TEMPORARY;
254 /// MTU set with SIOCSLIFMTU
255 #[cfg(any(target_os = "solaris"))]
256 IFF_FIXEDMTU;
257 /// Cannot send / receive packets
258 #[cfg(any(target_os = "solaris"))]
259 IFF_VIRTUAL;
260 /// Local address in use
261 #[cfg(any(target_os = "solaris"))]
262 IFF_DUPLICATE;
263 /// IPMP IP interface
264 #[cfg(any(target_os = "solaris"))]
265 IFF_IPMP;
266 }
267 );
268