From 1520d7b316932f86beb32931c4aa86bdbb97e416 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Tue, 31 May 2022 10:02:35 -0600 Subject: [PATCH] Enable SockaddrStorage::{as_link_addr, as_link_addr_mut} on Linux. This was an oversight from #1684. Fixes #1728 --- CHANGELOG.md | 5 +++++ src/sys/socket/addr.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89f2b268ea..9f40e4089f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,11 @@ This project adheres to [Semantic Versioning](https://semver.org/). (#[1713](https://github.com/nix-rust/nix/pull/1713)) ### Fixed + +- Enabled `SockaddrStorage::{as_link_addr, as_link_addr_mut}` for Linux-like + operating systems. + (#[1729](https://github.com/nix-rust/nix/pull/1729)) + ### Removed - Removed support for resubmitting partially complete `lio_listio` operations. diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 6d38f26c31..8de501a413 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -1514,6 +1514,14 @@ impl SockaddrStorage { accessors!{as_alg_addr, as_alg_addr_mut, AlgAddr, AddressFamily::Alg, libc::sockaddr_alg, alg} + #[cfg(any(target_os = "android", + target_os = "fuchsia", + target_os = "linux"))] + #[cfg(feature = "net")] + accessors!{ + as_link_addr, as_link_addr_mut, LinkAddr, + AddressFamily::Packet, libc::sockaddr_ll, dl} + #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", @@ -2682,6 +2690,25 @@ mod tests { format!("{}", la); } + #[cfg(all( + any(target_os = "android", + target_os = "fuchsia", + target_os = "linux"), + target_endian = "little" + ))] + #[test] + fn linux_loopback() { + let bytes = [17u8, 0, 0, 0, 1, 0, 0, 0, 4, 3, 0, 6, 1, 2, 3, 4, 5, 6, 0, 0]; + let sa = bytes.as_ptr() as *const libc::sockaddr; + let len = None; + let sock_addr = unsafe { SockaddrStorage::from_raw(sa, len) }.unwrap(); + assert_eq!(sock_addr.family(), Some(AddressFamily::Packet)); + match sock_addr.as_link_addr() { + Some(dl) => assert_eq!(dl.addr(), Some([1, 2, 3, 4, 5, 6])), + None => panic!("Can't unwrap sockaddr storage") + } + } + #[cfg(any(target_os = "ios", target_os = "macos" ))]