Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified return type for ipaddr #2151

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Changed

- Changed function `SockaddrIn::ip()` to return `net::Ipv4Addr`
([#2151](https://github.com/nix-rust/nix/pull/2151))

- The MSRV is now 1.69
([#2144](https://github.com/nix-rust/nix/pull/2144))

Expand All @@ -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))

Expand All @@ -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))
Expand Down
11 changes: 9 additions & 2 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Member

@SteveLauC SteveLauC Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we can also retain the const here if we choose the following impl:

    pub const fn ip(&self) -> 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)
    }

net::Ipv4Addr::from(self.0.sin_addr.s_addr.to_ne_bytes())
}

/// Creates a new socket address from IPv4 octets and a port number.
Expand Down Expand Up @@ -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 {
Expand Down