From 954d11f5e1c8294364ec791e34279338d4b3e82d Mon Sep 17 00:00:00 2001 From: carlosb1 Date: Tue, 3 Oct 2023 18:41:24 +0200 Subject: [PATCH 1/7] Modifie return type for ipaddr Signed-off-by: carlosb1 --- CHANGELOG.md | 7 +++++-- src/sys/socket/addr.rs | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aaab7b2d75..ba1ea93fae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Changed +- Changed function `SockaddrIn::ip()` to return `net::Ipv4Addr` + ([#])(https://github.com/nix-rust/nix/pull/2xxx)) + - The MSRV is now 1.69 ([#2144](https://github.com/nix-rust/nix/pull/2144)) @@ -31,7 +34,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). - `unistd::getpeereid` ([#2137](https://github.com/nix-rust/nix/pull/2137)) - + - Changed `openat()` and `Dir::openat()`, now take optional `dirfd`s ([#2139](https://github.com/nix-rust/nix/pull/2139)) @@ -51,7 +54,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Added `F_GETPATH` FcntlFlags entry on Apple/NetBSD/DragonflyBSD for `::nix::fcntl`. ([#2142](https://github.com/nix-rust/nix/pull/2142)) - + - Added `Ipv6HopLimit` to `::nix::sys::socket::ControlMessage` for Linux, MacOS, FreeBSD, DragonflyBSD, Android, iOS and Haiku. ([#2074](https://github.com/nix-rust/nix/pull/2074)) diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index c42f86e34f..2ab1bb043e 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -1006,8 +1006,8 @@ pub struct SockaddrIn(libc::sockaddr_in); impl SockaddrIn { /// Returns the IP address associated with this socket address, in native /// endian. - pub const fn ip(&self) -> libc::in_addr_t { - u32::from_be(self.0.sin_addr.s_addr) + pub fn ip(&self) -> net::Ipv4Addr { + net::Ipv4Addr::from(addr.0.sin_addr.s_addr.to_ne_bytes()) } /// Creates a new socket address from IPv4 octets and a port number. From 41a83b0dd5e9bd2a8146699dde99735fb02fa7d5 Mon Sep 17 00:00:00 2001 From: carlosb1 Date: Tue, 3 Oct 2023 18:43:42 +0200 Subject: [PATCH 2/7] Updated changelog with correct pull id Signed-off-by: carlosb1 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba1ea93fae..330096b8b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Changed - Changed function `SockaddrIn::ip()` to return `net::Ipv4Addr` - ([#])(https://github.com/nix-rust/nix/pull/2xxx)) + ([#2151](https://github.com/nix-rust/nix/pull/2151)) - The MSRV is now 1.69 ([#2144](https://github.com/nix-rust/nix/pull/2144)) From a28966452ee8127705443feeb352942a25190db0 Mon Sep 17 00:00:00 2001 From: carlosb1 Date: Tue, 3 Oct 2023 19:02:23 +0200 Subject: [PATCH 3/7] Fix error for getting ipv4 address Signed-off-by: carlosb1 --- src/sys/socket/addr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 2ab1bb043e..4de9eabc8a 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -1007,7 +1007,7 @@ impl SockaddrIn { /// Returns the IP address associated with this socket address, in native /// endian. pub fn ip(&self) -> net::Ipv4Addr { - net::Ipv4Addr::from(addr.0.sin_addr.s_addr.to_ne_bytes()) + net::Ipv4Addr::from(self.0.sin_addr.s_addr.to_ne_bytes()) } /// Creates a new socket address from IPv4 octets and a port number. From 870459f860480185f390dc28faf50540cbf0c759 Mon Sep 17 00:00:00 2001 From: carlosb1 Date: Tue, 3 Oct 2023 19:17:10 +0200 Subject: [PATCH 4/7] Added test for ip function Signed-off-by: carlosb1 --- src/sys/socket/addr.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 4de9eabc8a..05f431db67 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -2588,6 +2588,13 @@ mod tests { SockaddrIn::size() as usize ); } + + #[test] + fn ip() { + let s = "127.0.0.1:8080"; + let ip = SockaddrIn::from_str(s).unwrap().ip(); + assert_eq!("127.0.0.1", format!("{ip}")); + } } mod sockaddr_in6 { From a4ae6925c7a4f2c9180719183e71123c6c807000 Mon Sep 17 00:00:00 2001 From: carlosb1 Date: Wed, 4 Oct 2023 12:10:51 +0200 Subject: [PATCH 5/7] Changed as const functions SockaddrIn::ip SockaddrIn6::ip Signed-off-by: carlosb1 --- src/sys/socket/addr.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 05f431db67..8c93927b6c 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -36,6 +36,8 @@ use std::hash::{Hash, Hasher}; use std::os::unix::ffi::OsStrExt; use std::path::Path; use std::{fmt, mem, net, ptr, slice}; +use std::net::{Ipv4Addr,Ipv6Addr}; +use core::mem::transmute; /// Convert a std::net::Ipv4Addr into the libc form. #[cfg(feature = "net")] @@ -1006,8 +1008,10 @@ pub struct SockaddrIn(libc::sockaddr_in); impl SockaddrIn { /// Returns the IP address associated with this socket address, in native /// endian. - pub fn ip(&self) -> net::Ipv4Addr { - net::Ipv4Addr::from(self.0.sin_addr.s_addr.to_ne_bytes()) + pub const fn ip(&self) -> net::Ipv4Addr { + let bytes = self.0.sin_addr.s_addr.to_ne_bytes(); + let (a, b, c, d) = (bytes[0], bytes[1], bytes[2], bytes[3]); + Ipv4Addr::new(a, b, c, d) } /// Creates a new socket address from IPv4 octets and a port number. @@ -1143,8 +1147,24 @@ impl SockaddrIn6 { } /// Returns the IP address associated with this socket address. - pub fn ip(&self) -> net::Ipv6Addr { - net::Ipv6Addr::from(self.0.sin6_addr.s6_addr) + pub const fn ip(&self) -> net::Ipv6Addr { + let bytes = self.0.sin6_addr.s6_addr; + let [a, b, c, d, e, f, g, h] = + unsafe { transmute::<_, [u16; 8]>(bytes) }; + + // Ipv6Addr::new() takes segments in native endian, convert them here + let [a, b, c, d, e, f, g, h] = [ + u16::from_be(a), + u16::from_be(b), + u16::from_be(c), + u16::from_be(d), + u16::from_be(e), + u16::from_be(f), + u16::from_be(g), + u16::from_be(h), + ]; + + Ipv6Addr::new(a, b, c, d, e, f, g, h) } /// Returns the port number associated with this socket address, in native From f0951615387f183c6e64a94b626806b0ccf7bc70 Mon Sep 17 00:00:00 2001 From: carlosb1 Date: Tue, 10 Oct 2023 16:03:51 +0200 Subject: [PATCH 6/7] Refactor ip function for ipv6 Signed-off-by: carlosb1 --- src/sys/socket/addr.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 8c93927b6c..097a9d786a 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -37,7 +37,6 @@ use std::os::unix::ffi::OsStrExt; use std::path::Path; use std::{fmt, mem, net, ptr, slice}; use std::net::{Ipv4Addr,Ipv6Addr}; -use core::mem::transmute; /// Convert a std::net::Ipv4Addr into the libc form. #[cfg(feature = "net")] @@ -1149,21 +1148,15 @@ impl SockaddrIn6 { /// Returns the IP address associated with this socket address. pub const fn ip(&self) -> net::Ipv6Addr { let bytes = self.0.sin6_addr.s6_addr; - let [a, b, c, d, e, f, g, h] = - unsafe { transmute::<_, [u16; 8]>(bytes) }; - - // Ipv6Addr::new() takes segments in native endian, convert them here - let [a, b, c, d, e, f, g, h] = [ - u16::from_be(a), - u16::from_be(b), - u16::from_be(c), - u16::from_be(d), - u16::from_be(e), - u16::from_be(f), - u16::from_be(g), - u16::from_be(h), - ]; - + let (a, b, c, d, e, f, g, h) = (((bytes[0] as u16) << 8) | bytes[1] as u16, + ((bytes[2] as u16) << 8) | bytes[3] as u16, + ((bytes[4] as u16) << 8) | bytes[5] as u16, + ((bytes[6] as u16) << 8) | bytes[7] as u16, + ((bytes[8] as u16) << 8) | bytes[9] as u16, + ((bytes[10] as u16) << 8) | bytes[11] as u16, + ((bytes[12] as u16) << 8) | bytes[13] as u16, + ((bytes[14] as u16) << 8) | bytes[15] as u16 + ); Ipv6Addr::new(a, b, c, d, e, f, g, h) } @@ -2636,6 +2629,14 @@ mod tests { ); } + #[test] + fn ip() { + let s = "[1234:5678:90ab:cdef::1111:2222]:8080"; + let ip = SockaddrIn6::from_str(s).unwrap().ip(); + assert_eq!("1234:5678:90ab:cdef::1111:2222", format!("{ip}")); + + } + #[test] // Ensure that we can convert to-and-from std::net variants without change. fn to_and_from() { From 3ef68340a3e74f45806e3233a873365260f6f385 Mon Sep 17 00:00:00 2001 From: carlosb1 Date: Tue, 10 Oct 2023 16:20:03 +0200 Subject: [PATCH 7/7] Update changelog for ipv6 const function Signed-off-by: carlosb1 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 207e406aa3..515bce6d55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Changed -- Changed function `SockaddrIn::ip()` to return `net::Ipv4Addr` +- Changed function `SockaddrIn::ip()` to return `net::Ipv4Addr` and + refactored `SocketAddrV6::ip()` to use `const` ([#2151](https://github.com/nix-rust/nix/pull/2151)) - The MSRV is now 1.69