Skip to content

Commit

Permalink
Modified return type for ipaddr (#2151)
Browse files Browse the repository at this point in the history
* Modifie return type for ipaddr

Signed-off-by: carlosb1 <mcflurry0@gmail.com>

* Updated changelog with correct pull id

Signed-off-by: carlosb1 <mcflurry0@gmail.com>

* Fix error for getting ipv4 address

Signed-off-by: carlosb1 <mcflurry0@gmail.com>

* Added test for ip function

Signed-off-by: carlosb1 <mcflurry0@gmail.com>

* Changed as const functions SockaddrIn::ip SockaddrIn6::ip

Signed-off-by: carlosb1 <mcflurry0@gmail.com>

* Refactor ip function for ipv6

Signed-off-by: carlosb1 <mcflurry0@gmail.com>

* Update changelog for ipv6 const function

Signed-off-by: carlosb1 <mcflurry0@gmail.com>

---------

Signed-off-by: carlosb1 <mcflurry0@gmail.com>
  • Loading branch information
carlosb1 committed Nov 5, 2023
1 parent b28132b commit ed0f7ff
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
56 changes: 56 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased] - ReleaseDate

### Fixed
- Fix `SigSet` incorrect implementation of `Eq`, `PartialEq` and `Hash`
([#1946](https://github.com/nix-rust/nix/pull/1946))

- Fixed the function signature of `recvmmsg`, potentially causing UB
([#2119](https://github.com/nix-rust/nix/issues/2119))

- Fix `SignalFd::set_mask`. In 0.27.0 it would actually close the file
descriptor.
([#2141](https://github.com/nix-rust/nix/pull/2141))

### Changed

- 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
([#2144](https://github.com/nix-rust/nix/pull/2144))

- The following APIs now take an implementation of `AsFd` rather than a
`RawFd`:

- `unistd::tcgetpgrp`
- `unistd::tcsetpgrp`
- `unistd::fpathconf`
- `unistd::ttyname`
- `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))

- `PollFd::new` now takes a `BorrowedFd` argument, with relaxed lifetime
requirements relative to the previous version.
([#2134](https://github.com/nix-rust/nix/pull/2134))

- `FdSet::{insert, remove, contains}` now take `BorrowedFd` arguments, and have
relaxed lifetime requirements relative to 0.27.1.
([#2136](https://github.com/nix-rust/nix/pull/2136))

- Simplified the function signatures of `recvmmsg` and `sendmmsg`

### Added
- Added `Icmp` and `IcmpV6` to `SockProtocol`.
(#[2103](https://github.com/nix-rust/nix/pull/2103))

- 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))
# Change Log

## [0.27.1] - 2023-08-28
Expand Down
36 changes: 32 additions & 4 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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};

/// Convert a std::net::Ipv4Addr into the libc form.
#[cfg(feature = "net")]
Expand Down Expand Up @@ -1006,8 +1007,10 @@ 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 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.
Expand Down Expand Up @@ -1143,8 +1146,18 @@ 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) = (((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)
}

/// Returns the port number associated with this socket address, in native
Expand Down Expand Up @@ -2588,6 +2601,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 All @@ -2609,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() {
Expand Down

0 comments on commit ed0f7ff

Please sign in to comment.