Skip to content

Commit 8fc62a8

Browse files
committed
Improve documentation, order target_os lists and use whitelisting.
1 parent 37636cf commit 8fc62a8

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

src/ifaddrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Query network interface addresses
22
//!
3-
//! Uses the Linux and/or BSD specific function getifaddrs to query the list
3+
//! Uses the Linux and/or BSD specific function `getifaddrs` to query the list
44
//! of interfaces and their associated addresses.
55
66
use std::ffi;
@@ -121,7 +121,7 @@ impl Iterator for InterfaceAddressIterator {
121121
/// }
122122
/// ```
123123
pub fn getifaddrs() -> Result<InterfaceAddressIterator> {
124-
let mut addrs: *mut libc::ifaddrs = unsafe { mem::zeroed() };
124+
let mut addrs: *mut libc::ifaddrs = unsafe { mem::uninitialized() };
125125
Errno::result(unsafe { libc::getifaddrs(&mut addrs) }).map(|_| {
126126
InterfaceAddressIterator {
127127
base: addrs,
@@ -134,7 +134,7 @@ pub fn getifaddrs() -> Result<InterfaceAddressIterator> {
134134
mod tests {
135135
use super::*;
136136

137-
// Only checks if getifaddrs can be invoked without panicking.
137+
// Only checks if `getifaddrs` can be invoked without panicking.
138138
#[test]
139139
fn test_getifaddrs() {
140140
let _ = getifaddrs();

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ pub mod poll;
4343

4444
pub mod net;
4545

46-
#[cfg(not(any(target_os = "android", target_os = "haiku")))]
46+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd",
47+
target_os = "ios", target_os = "macos", target_os = "openbsd", target_os = "linux", ))]
4748
pub mod ifaddrs;
4849

4950
#[cfg(any(target_os = "linux", target_os = "android"))]

src/net/if_.rs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,64 @@ pub fn if_nametoindex<P: ?Sized + NixPath>(name: &P) -> Result<c_uint> {
2121
libc_bitflags!(
2222
/// Standard interface flags, used by `getifaddrs`
2323
pub struct InterfaceFlags: libc::c_int {
24+
/// Interface is running. (see
25+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
2426
IFF_UP;
27+
/// Valid broadcast address set. (see
28+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
2529
IFF_BROADCAST;
30+
/// Internal debugging flag. (see
31+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
2632
IFF_DEBUG;
33+
/// Interface is a loopback interface. (see
34+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
2735
IFF_LOOPBACK;
36+
/// Interface is a point-to-point link. (see
37+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
2838
IFF_POINTOPOINT;
29-
#[cfg(not(any(target_os = "freebsd", target_os = "dragonfly", target_os = "haiku")))]
39+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "ios",
40+
target_os = "linux", target_os = "macos", target_os = "netbsd",
41+
target_os = "openbsd"))]
42+
/// Avoid use of trailers. (see
43+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
3044
IFF_NOTRAILERS;
31-
#[cfg(not(any(target_os = "freebsd", target_os = "haiku")))]
45+
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "fuchsia",
46+
target_os = "ios", target_os = "linux", target_os = "macos",
47+
target_os = "netbsd", target_os = "openbsd"))]
48+
/// Resources allocated. (see
49+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
3250
IFF_RUNNING;
51+
/// No arp protocol, L2 destination address not set. (see
52+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
3353
IFF_NOARP;
54+
/// Interface is in promiscuous mode. (see
55+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
3456
IFF_PROMISC;
57+
/// Receive all multicast packets. (see
58+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
3559
IFF_ALLMULTI;
36-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "fuchsia"))]
60+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
61+
/// Master of a load balancing bundle. (see
62+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
3763
IFF_MASTER;
38-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "fuchsia"))]
64+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
65+
/// Slave of a load balancing bundle. (see
66+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
3967
IFF_SLAVE;
68+
/// Supports multicast. (see
69+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
4070
IFF_MULTICAST;
41-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "fuchsia"))]
71+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
72+
/// Is able to select media type via ifmap. (see
73+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
4274
IFF_PORTSEL;
43-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "fuchsia"))]
75+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
76+
/// Auto media selection active. (see
77+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
4478
IFF_AUTOMEDIA;
45-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "fuchsia"))]
79+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
80+
/// The addresses are lost when the interface goes down. (see
81+
/// [`netdevice(7)`](http://man7.org/linux/man-pages/man7/netdevice.7.html))
4682
IFF_DYNAMIC;
4783
}
4884
);

src/sys/socket/addr.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,21 @@ pub enum AddressFamily {
202202
}
203203

204204
impl AddressFamily {
205+
/// Create a new `AddressFamily` from an integer value retrieved from `libc`, usually from
206+
/// the `sa_family` field of a `sockaddr`.
207+
///
208+
/// Currently only supports these address families: Unix, Inet (v4 & v6), Packet, Netlink
209+
/// and System. Returns None for unsupported or unknown address families.
205210
pub fn from_i32(family: i32) -> Option<AddressFamily> {
206211
match family {
207212
libc::AF_UNIX => Some(AddressFamily::Unix),
208213
libc::AF_INET => Some(AddressFamily::Inet),
209214
libc::AF_INET6 => Some(AddressFamily::Inet6),
210-
#[cfg(any(target_os = "linux", target_os = "android"))]
215+
#[cfg(any(target_os = "android", target_os = "linux"))]
211216
libc::AF_NETLINK => Some(AddressFamily::Netlink),
212-
#[cfg(any(target_os = "linux", target_os = "android"))]
217+
#[cfg(any(target_os = "android", target_os = "linux"))]
213218
libc::AF_PACKET => Some(AddressFamily::Packet),
214-
#[cfg(any(target_os = "macos", target_os = "ios"))]
219+
#[cfg(any(target_os = "macos", target_os = "macos"))]
215220
libc::AF_SYSTEM => Some(AddressFamily::System),
216221
_ => None
217222
}
@@ -706,23 +711,27 @@ impl SockAddr {
706711
}
707712

708713
/// Creates a `SockAddr` struct from libc's sockaddr.
714+
///
715+
/// Supports only the following address families: Unix, Inet (v4 & v6), Netlink and System.
716+
/// Returns None for unsupported families.
709717
pub unsafe fn from_libc_sockaddr(addr: *const libc::sockaddr) -> Option<SockAddr> {
710718
if addr.is_null() {
711719
None
712720
} else {
713721
match AddressFamily::from_i32((*addr).sa_family as i32) {
714722
Some(AddressFamily::Unix) => None,
715723
Some(AddressFamily::Inet) => Some(SockAddr::Inet(
716-
InetAddr::V4(*(addr as *mut libc::sockaddr_in)))),
724+
InetAddr::V4(*(addr as *const libc::sockaddr_in)))),
717725
Some(AddressFamily::Inet6) => Some(SockAddr::Inet(
718-
InetAddr::V6(*(addr as *mut libc::sockaddr_in6)))),
726+
InetAddr::V6(*(addr as *const libc::sockaddr_in6)))),
719727
#[cfg(any(target_os = "android", target_os = "linux"))]
720728
Some(AddressFamily::Netlink) => Some(SockAddr::Netlink(
721-
NetlinkAddr(*(addr as *mut libc::sockaddr_nl)))),
729+
NetlinkAddr(*(addr as *const libc::sockaddr_nl)))),
722730
#[cfg(any(target_os = "ios", target_os = "macos"))]
723731
Some(AddressFamily::System) => Some(SockAddr::SysControl(
724-
SysControlAddr(*(addr as *mut sys_control::sockaddr_ctl)))),
725-
// No other address families are supported, including netlink and syscontrol.
732+
SysControlAddr(*(addr as *const sys_control::sockaddr_ctl)))),
733+
// Other address families are currently not supported and simply yield a None
734+
// entry instead of a proper conversion to a `SockAddr`.
726735
Some(_) => None,
727736
None => None,
728737
}

0 commit comments

Comments
 (0)