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