summaryrefslogtreecommitdiff
path: root/src/ifaddrs.rs
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2021-12-21 06:10:50 -0700
committerAlan Somers <asomers@gmail.com>2022-03-21 20:50:24 -0600
commit76d70b4b25ef499c8bb0a322590eb86e3370b548 (patch)
treeba00a102ac5cad3a0d773270ef55f2c121748ba3 /src/ifaddrs.rs
parentb2ff9d227fd2693283fadfb908a8b7eb91e567b5 (diff)
downloadnix-76d70b4b25ef499c8bb0a322590eb86e3370b548.zip
Replace the Sockaddr enum with a union
The SockAddr enum is quite large, and the user must allocate space for the whole thing even though he usually knows what type he needs. Furthermore, thanks to the sa_family field, the sockaddr types are basically an enum even in C. So replace the ungainly enum with a SockaddrLike trait implemented by all sockaddr types and a SockaddrStorage union that has safe accessors. Also, deprecate InetAddr, which only existed to support SockAddr. Supplants #1504 Fixes #1544
Diffstat (limited to 'src/ifaddrs.rs')
-rw-r--r--src/ifaddrs.rs24
1 files changed, 12 insertions, 12 deletions
diff --git a/src/ifaddrs.rs b/src/ifaddrs.rs
index ed6328f3..f834d307 100644
--- a/src/ifaddrs.rs
+++ b/src/ifaddrs.rs
@@ -10,7 +10,7 @@ use std::mem;
use std::option::Option;
use crate::{Result, Errno};
-use crate::sys::socket::SockAddr;
+use crate::sys::socket::{SockaddrLike, SockaddrStorage};
use crate::net::if_::*;
/// Describes a single address for an interface as returned by `getifaddrs`.
@@ -21,13 +21,13 @@ pub struct InterfaceAddress {
/// Flags as from `SIOCGIFFLAGS` ioctl
pub flags: InterfaceFlags,
/// Network address of this interface
- pub address: Option<SockAddr>,
+ pub address: Option<SockaddrStorage>,
/// Netmask of this interface
- pub netmask: Option<SockAddr>,
+ pub netmask: Option<SockaddrStorage>,
/// Broadcast address of this interface, if applicable
- pub broadcast: Option<SockAddr>,
+ pub broadcast: Option<SockaddrStorage>,
/// Point-to-point destination address
- pub destination: Option<SockAddr>,
+ pub destination: Option<SockaddrStorage>,
}
cfg_if! {
@@ -46,8 +46,8 @@ impl InterfaceAddress {
/// Create an `InterfaceAddress` from the libc struct.
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
- let address = unsafe { SockAddr::from_libc_sockaddr(info.ifa_addr) };
- let netmask = unsafe { SockAddr::from_libc_sockaddr(info.ifa_netmask) };
+ let address = unsafe { SockaddrStorage::from_raw(info.ifa_addr, None) };
+ let netmask = unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
let mut addr = InterfaceAddress {
interface_name: ifname.to_string_lossy().to_string(),
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
@@ -59,9 +59,9 @@ impl InterfaceAddress {
let ifu = get_ifu_from_sockaddr(info);
if addr.flags.contains(InterfaceFlags::IFF_POINTOPOINT) {
- addr.destination = unsafe { SockAddr::from_libc_sockaddr(ifu) };
+ addr.destination = unsafe { SockaddrStorage::from_raw(ifu, None) };
} else if addr.flags.contains(InterfaceFlags::IFF_BROADCAST) {
- addr.broadcast = unsafe { SockAddr::from_libc_sockaddr(ifu) };
+ addr.broadcast = unsafe { SockaddrStorage::from_raw(ifu, None) };
}
addr
@@ -103,9 +103,9 @@ impl Iterator for InterfaceAddressIterator {
/// Note that the underlying implementation differs between OSes. Only the
/// most common address families are supported by the nix crate (due to
/// lack of time and complexity of testing). The address family is encoded
-/// in the specific variant of `SockAddr` returned for the fields `address`,
-/// `netmask`, `broadcast`, and `destination`. For any entry not supported,
-/// the returned list will contain a `None` entry.
+/// in the specific variant of `SockaddrStorage` returned for the fields
+/// `address`, `netmask`, `broadcast`, and `destination`. For any entry not
+/// supported, the returned list will contain a `None` entry.
///
/// # Example
/// ```