Skip to content

Commit ed0f7ff

Browse files
authored
Modified return type for ipaddr (#2151)
* Modifie return type for ipaddr Signed-off-by: carlosb1 <[email protected]> * Updated changelog with correct pull id Signed-off-by: carlosb1 <[email protected]> * Fix error for getting ipv4 address Signed-off-by: carlosb1 <[email protected]> * Added test for ip function Signed-off-by: carlosb1 <[email protected]> * Changed as const functions SockaddrIn::ip SockaddrIn6::ip Signed-off-by: carlosb1 <[email protected]> * Refactor ip function for ipv6 Signed-off-by: carlosb1 <[email protected]> * Update changelog for ipv6 const function Signed-off-by: carlosb1 <[email protected]> --------- Signed-off-by: carlosb1 <[email protected]>
1 parent b28132b commit ed0f7ff

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

CHANGELOG.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,62 @@
11
All notable changes to this project will be documented in this file.
22
This project adheres to [Semantic Versioning](https://semver.org/).
33

4+
## [Unreleased] - ReleaseDate
5+
6+
### Fixed
7+
- Fix `SigSet` incorrect implementation of `Eq`, `PartialEq` and `Hash`
8+
([#1946](https://github.com/nix-rust/nix/pull/1946))
9+
10+
- Fixed the function signature of `recvmmsg`, potentially causing UB
11+
([#2119](https://github.com/nix-rust/nix/issues/2119))
12+
13+
- Fix `SignalFd::set_mask`. In 0.27.0 it would actually close the file
14+
descriptor.
15+
([#2141](https://github.com/nix-rust/nix/pull/2141))
16+
17+
### Changed
18+
19+
- Changed function `SockaddrIn::ip()` to return `net::Ipv4Addr` and
20+
refactored `SocketAddrV6::ip()` to use `const`
21+
([#2151](https://github.com/nix-rust/nix/pull/2151))
22+
23+
- The MSRV is now 1.69
24+
([#2144](https://github.com/nix-rust/nix/pull/2144))
25+
26+
- The following APIs now take an implementation of `AsFd` rather than a
27+
`RawFd`:
28+
29+
- `unistd::tcgetpgrp`
30+
- `unistd::tcsetpgrp`
31+
- `unistd::fpathconf`
32+
- `unistd::ttyname`
33+
- `unistd::getpeereid`
34+
35+
([#2137](https://github.com/nix-rust/nix/pull/2137))
36+
37+
- Changed `openat()` and `Dir::openat()`, now take optional `dirfd`s
38+
([#2139](https://github.com/nix-rust/nix/pull/2139))
39+
40+
- `PollFd::new` now takes a `BorrowedFd` argument, with relaxed lifetime
41+
requirements relative to the previous version.
42+
([#2134](https://github.com/nix-rust/nix/pull/2134))
43+
44+
- `FdSet::{insert, remove, contains}` now take `BorrowedFd` arguments, and have
45+
relaxed lifetime requirements relative to 0.27.1.
46+
([#2136](https://github.com/nix-rust/nix/pull/2136))
47+
48+
- Simplified the function signatures of `recvmmsg` and `sendmmsg`
49+
50+
### Added
51+
- Added `Icmp` and `IcmpV6` to `SockProtocol`.
52+
(#[2103](https://github.com/nix-rust/nix/pull/2103))
53+
54+
- Added `F_GETPATH` FcntlFlags entry on Apple/NetBSD/DragonflyBSD for `::nix::fcntl`.
55+
([#2142](https://github.com/nix-rust/nix/pull/2142))
56+
57+
- Added `Ipv6HopLimit` to `::nix::sys::socket::ControlMessage` for Linux,
58+
MacOS, FreeBSD, DragonflyBSD, Android, iOS and Haiku.
59+
([#2074](https://github.com/nix-rust/nix/pull/2074))
460
# Change Log
561

662
## [0.27.1] - 2023-08-28

src/sys/socket/addr.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use std::hash::{Hash, Hasher};
3636
use std::os::unix::ffi::OsStrExt;
3737
use std::path::Path;
3838
use std::{fmt, mem, net, ptr, slice};
39+
use std::net::{Ipv4Addr,Ipv6Addr};
3940

4041
/// Convert a std::net::Ipv4Addr into the libc form.
4142
#[cfg(feature = "net")]
@@ -1006,8 +1007,10 @@ pub struct SockaddrIn(libc::sockaddr_in);
10061007
impl SockaddrIn {
10071008
/// Returns the IP address associated with this socket address, in native
10081009
/// endian.
1009-
pub const fn ip(&self) -> libc::in_addr_t {
1010-
u32::from_be(self.0.sin_addr.s_addr)
1010+
pub const fn ip(&self) -> net::Ipv4Addr {
1011+
let bytes = self.0.sin_addr.s_addr.to_ne_bytes();
1012+
let (a, b, c, d) = (bytes[0], bytes[1], bytes[2], bytes[3]);
1013+
Ipv4Addr::new(a, b, c, d)
10111014
}
10121015

10131016
/// Creates a new socket address from IPv4 octets and a port number.
@@ -1143,8 +1146,18 @@ impl SockaddrIn6 {
11431146
}
11441147

11451148
/// Returns the IP address associated with this socket address.
1146-
pub fn ip(&self) -> net::Ipv6Addr {
1147-
net::Ipv6Addr::from(self.0.sin6_addr.s6_addr)
1149+
pub const fn ip(&self) -> net::Ipv6Addr {
1150+
let bytes = self.0.sin6_addr.s6_addr;
1151+
let (a, b, c, d, e, f, g, h) = (((bytes[0] as u16) << 8) | bytes[1] as u16,
1152+
((bytes[2] as u16) << 8) | bytes[3] as u16,
1153+
((bytes[4] as u16) << 8) | bytes[5] as u16,
1154+
((bytes[6] as u16) << 8) | bytes[7] as u16,
1155+
((bytes[8] as u16) << 8) | bytes[9] as u16,
1156+
((bytes[10] as u16) << 8) | bytes[11] as u16,
1157+
((bytes[12] as u16) << 8) | bytes[13] as u16,
1158+
((bytes[14] as u16) << 8) | bytes[15] as u16
1159+
);
1160+
Ipv6Addr::new(a, b, c, d, e, f, g, h)
11481161
}
11491162

11501163
/// Returns the port number associated with this socket address, in native
@@ -2588,6 +2601,13 @@ mod tests {
25882601
SockaddrIn::size() as usize
25892602
);
25902603
}
2604+
2605+
#[test]
2606+
fn ip() {
2607+
let s = "127.0.0.1:8080";
2608+
let ip = SockaddrIn::from_str(s).unwrap().ip();
2609+
assert_eq!("127.0.0.1", format!("{ip}"));
2610+
}
25912611
}
25922612

25932613
mod sockaddr_in6 {
@@ -2609,6 +2629,14 @@ mod tests {
26092629
);
26102630
}
26112631

2632+
#[test]
2633+
fn ip() {
2634+
let s = "[1234:5678:90ab:cdef::1111:2222]:8080";
2635+
let ip = SockaddrIn6::from_str(s).unwrap().ip();
2636+
assert_eq!("1234:5678:90ab:cdef::1111:2222", format!("{ip}"));
2637+
2638+
}
2639+
26122640
#[test]
26132641
// Ensure that we can convert to-and-from std::net variants without change.
26142642
fn to_and_from() {

0 commit comments

Comments
 (0)