From cfe689a11e963fb07ee08bf8277cece9d9ea382a Mon Sep 17 00:00:00 2001 From: Aaron Drew Date: Tue, 10 Jan 2023 13:53:16 +1100 Subject: Fix endian swap on SocketAddrV6. flowinfo and scope_id should not be byte swapped. --- CHANGELOG.md | 3 +++ src/sys/socket/addr.rs | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ff4176c..2bb1de9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). ([#1921](https://github.com/nix-rust/nix/pull/1921)) ### Fixed +- Fix `SockaddrIn6` bug that was swapping flowinfo and scope_id byte ordering. + ([#1964](https://github.com/nix-rust/nix/pull/1964)) + ### Removed - Removed deprecated IoVec API. diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 4e36ca47..aad407a7 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -1182,8 +1182,8 @@ impl From for net::SocketAddrV6 { net::SocketAddrV6::new( net::Ipv6Addr::from(addr.0.sin6_addr.s6_addr), u16::from_be(addr.0.sin6_port), - u32::from_be(addr.0.sin6_flowinfo), - u32::from_be(addr.0.sin6_scope_id), + addr.0.sin6_flowinfo, + addr.0.sin6_scope_id, ) } } @@ -2525,6 +2525,18 @@ mod tests { SockaddrIn6::size() as usize ); } + + #[test] + // Ensure that we can convert to-and-from std::net variants without change. + fn to_and_from() { + let s = "[1234:5678:90ab:cdef::1111:2222]:8080"; + let mut nix_sin6 = SockaddrIn6::from_str(s).unwrap(); + nix_sin6.0.sin6_flowinfo = 0x12345678; + nix_sin6.0.sin6_scope_id = 0x9abcdef0; + + let std_sin6 : std::net::SocketAddrV6 = nix_sin6.into(); + assert_eq!(nix_sin6, std_sin6.into()); + } } mod sockaddr_storage { -- cgit v1.2.3