Skip to content

Commit 0fe6682

Browse files
bors[bot]asomers
andauthored
Merge #1684
1684: Replace the Sockaddr enum with a union r=rtzoeller a=asomers 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 Co-authored-by: Alan Somers <[email protected]>
2 parents b2ff9d2 + 76d70b4 commit 0fe6682

File tree

7 files changed

+1402
-390
lines changed

7 files changed

+1402
-390
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
7474
- Changed `getrlimit` and `setrlimit` to use `rlim_t` directly
7575
instead of `Option<rlim_t>`.
7676
(#[1668](https://github.com/nix-rust/nix/pull/1668))
77+
- Deprecated `InetAddr` and `SockAddr` in favor of `SockaddrIn`, `SockaddrIn6`,
78+
and `SockaddrStorage`.
79+
(#[1684](https://github.com/nix-rust/nix/pull/1684))
7780

7881
### Fixed
7982

src/ifaddrs.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::mem;
1010
use std::option::Option;
1111

1212
use crate::{Result, Errno};
13-
use crate::sys::socket::SockAddr;
13+
use crate::sys::socket::{SockaddrLike, SockaddrStorage};
1414
use crate::net::if_::*;
1515

1616
/// Describes a single address for an interface as returned by `getifaddrs`.
@@ -21,13 +21,13 @@ pub struct InterfaceAddress {
2121
/// Flags as from `SIOCGIFFLAGS` ioctl
2222
pub flags: InterfaceFlags,
2323
/// Network address of this interface
24-
pub address: Option<SockAddr>,
24+
pub address: Option<SockaddrStorage>,
2525
/// Netmask of this interface
26-
pub netmask: Option<SockAddr>,
26+
pub netmask: Option<SockaddrStorage>,
2727
/// Broadcast address of this interface, if applicable
28-
pub broadcast: Option<SockAddr>,
28+
pub broadcast: Option<SockaddrStorage>,
2929
/// Point-to-point destination address
30-
pub destination: Option<SockAddr>,
30+
pub destination: Option<SockaddrStorage>,
3131
}
3232

3333
cfg_if! {
@@ -46,8 +46,8 @@ impl InterfaceAddress {
4646
/// Create an `InterfaceAddress` from the libc struct.
4747
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
4848
let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
49-
let address = unsafe { SockAddr::from_libc_sockaddr(info.ifa_addr) };
50-
let netmask = unsafe { SockAddr::from_libc_sockaddr(info.ifa_netmask) };
49+
let address = unsafe { SockaddrStorage::from_raw(info.ifa_addr, None) };
50+
let netmask = unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
5151
let mut addr = InterfaceAddress {
5252
interface_name: ifname.to_string_lossy().to_string(),
5353
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
@@ -59,9 +59,9 @@ impl InterfaceAddress {
5959

6060
let ifu = get_ifu_from_sockaddr(info);
6161
if addr.flags.contains(InterfaceFlags::IFF_POINTOPOINT) {
62-
addr.destination = unsafe { SockAddr::from_libc_sockaddr(ifu) };
62+
addr.destination = unsafe { SockaddrStorage::from_raw(ifu, None) };
6363
} else if addr.flags.contains(InterfaceFlags::IFF_BROADCAST) {
64-
addr.broadcast = unsafe { SockAddr::from_libc_sockaddr(ifu) };
64+
addr.broadcast = unsafe { SockaddrStorage::from_raw(ifu, None) };
6565
}
6666

6767
addr
@@ -103,9 +103,9 @@ impl Iterator for InterfaceAddressIterator {
103103
/// Note that the underlying implementation differs between OSes. Only the
104104
/// most common address families are supported by the nix crate (due to
105105
/// lack of time and complexity of testing). The address family is encoded
106-
/// in the specific variant of `SockAddr` returned for the fields `address`,
107-
/// `netmask`, `broadcast`, and `destination`. For any entry not supported,
108-
/// the returned list will contain a `None` entry.
106+
/// in the specific variant of `SockaddrStorage` returned for the fields
107+
/// `address`, `netmask`, `broadcast`, and `destination`. For any entry not
108+
/// supported, the returned list will contain a `None` entry.
109109
///
110110
/// # Example
111111
/// ```

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
//! * `reboot` - Reboot the system
3232
//! * `resource` - Process resource limits
3333
//! * `sched` - Manipulate process's scheduling
34+
//! * `socket` - Sockets, whether for networking or local use
3435
//! * `signal` - Send and receive signals to processes
3536
//! * `term` - Terminal control APIs
3637
//! * `time` - Query the operating system's clocks

0 commit comments

Comments
 (0)