From 202e4083242b49fd8b9e36bc614a8dcd2935d81c Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Mon, 7 Jun 2021 23:07:45 -0500 Subject: [PATCH] Expose SockAddr::from_raw_sockaddr --- CHANGELOG.md | 2 ++ src/ifaddrs.rs | 8 ++++---- src/sys/socket/addr.rs | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ccbe81bb6..4c2fb35fcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). (#[1422](https://github.com/nix-rust/nix/pull/1422)) - Added `IP6T_SO_ORIGINAL_DST` sockopt. (#[1490](https://github.com/nix-rust/nix/pull/1490)) +- Exposed `SockAddr::from_raw_sockaddr` + (#[1447](https://github.com/nix-rust/nix/pull/1447)) ### Changed diff --git a/src/ifaddrs.rs b/src/ifaddrs.rs index ed6328f3ef..74f340505e 100644 --- a/src/ifaddrs.rs +++ b/src/ifaddrs.rs @@ -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 { SockAddr::from_raw_sockaddr(info.ifa_addr) }; + let netmask = unsafe { SockAddr::from_raw_sockaddr(info.ifa_netmask) }; 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 { SockAddr::from_raw_sockaddr(ifu) }; } else if addr.flags.contains(InterfaceFlags::IFF_BROADCAST) { - addr.broadcast = unsafe { SockAddr::from_libc_sockaddr(ifu) }; + addr.broadcast = unsafe { SockAddr::from_raw_sockaddr(ifu) }; } addr diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index a964d61bdb..2b0b3aeed3 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -736,7 +736,7 @@ impl SockAddr { /// unsafe because it takes a raw pointer as argument. The caller must /// ensure that the pointer is valid. #[cfg(not(target_os = "fuchsia"))] - pub(crate) unsafe fn from_libc_sockaddr(addr: *const libc::sockaddr) -> Option { + pub unsafe fn from_raw_sockaddr(addr: *const libc::sockaddr) -> Option { if addr.is_null() { None } else { @@ -1315,7 +1315,7 @@ mod tests { fn test_macos_loopback_datalink_addr() { let bytes = [20i8, 18, 1, 0, 24, 3, 0, 0, 108, 111, 48, 0, 0, 0, 0, 0]; let sa = bytes.as_ptr() as *const libc::sockaddr; - let _sock_addr = unsafe { SockAddr::from_libc_sockaddr(sa) }; + let _sock_addr = unsafe { SockAddr::from_raw_sockaddr(sa) }; assert!(_sock_addr.is_none()); } @@ -1330,7 +1330,7 @@ mod tests { let bytes = [20i8, 18, 7, 0, 6, 3, 6, 0, 101, 110, 48, 24, 101, -112, -35, 76, -80]; let ptr = bytes.as_ptr(); let sa = ptr as *const libc::sockaddr; - let _sock_addr = unsafe { SockAddr::from_libc_sockaddr(sa) }; + let _sock_addr = unsafe { SockAddr::from_raw_sockaddr(sa) }; assert!(_sock_addr.is_some()); @@ -1352,7 +1352,7 @@ mod tests { let bytes = [25u8, 0, 0, 0, 6, 0, 6, 0, 24, 101, 144, 221, 76, 176]; let ptr = bytes.as_ptr(); let sa = ptr as *const libc::sockaddr; - let _sock_addr = unsafe { SockAddr::from_libc_sockaddr(sa) }; + let _sock_addr = unsafe { SockAddr::from_raw_sockaddr(sa) }; assert!(_sock_addr.is_some());