From daaa9afdd0fba80799c3c8b6b306bdd6f3dfe9cd Mon Sep 17 00:00:00 2001 From: Biraj Parikh Date: Tue, 18 Mar 2025 21:42:36 -0700 Subject: [PATCH 1/4] feat: Get `tcp_info` structure Implement TCP_INFO option for getsockopt. --- src/sys/socket/sockopt.rs | 11 +++++++++++ test/sys/test_sockopt.rs | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index 11c3be9e13..08ae2cb1a4 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -1268,6 +1268,17 @@ sockopt_impl!( libc::SO_EXCLBIND, bool ); +#[cfg(target_os = "linux")] +#[cfg(feature = "net")] +sockopt_impl!( + #[cfg_attr(docsrs, doc(cfg(feature = "net")))] + /// Get tcp_info structure. + TcpInfo, + GetOnly, + libc::SOL_TCP, + libc::TCP_INFO, + libc::tcp_info +); #[allow(missing_docs)] // Not documented by Linux! diff --git a/test/sys/test_sockopt.rs b/test/sys/test_sockopt.rs index 42697ffa24..2dc304fba1 100644 --- a/test/sys/test_sockopt.rs +++ b/test/sys/test_sockopt.rs @@ -568,6 +568,23 @@ fn test_ipv6_tclass() { assert_eq!(getsockopt(&fd, sockopt::Ipv6TClass).unwrap(), class); } +#[test] +#[cfg(target_os = "linux")] +fn test_tcp_info() { + let fd = socket( + AddressFamily::Inet6, + SockType::Stream, + SockFlag::empty(), + SockProtocol::Tcp, + ) + .unwrap(); + let tcp_info = getsockopt(&fd, sockopt::TcpInfo).unwrap(); + // Silly assert for the sake of having one in the test. + // There should be no retransmits because nothing is sent through the + // socket in the first place. + assert_eq!(tcp_info.tcpi_retransmits, 0); +} + #[test] #[cfg(target_os = "freebsd")] fn test_receive_timestamp() { From 7e6cd7ebd1ec3be0cbf898423ffaf92adbc9ee63 Mon Sep 17 00:00:00 2001 From: Biraj Parikh Date: Tue, 18 Mar 2025 21:48:49 -0700 Subject: [PATCH 2/4] chore: CHANGELOG entry for TCP_INFO sockopt addition --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b673cac562..1f65a65be1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1960,6 +1960,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Added `SIGINFO` in `::nix::sys::signal` for the _macos_ target as well as `SIGPWR` and `SIGSTKFLT` in `::nix::sys::signal` for non-_macos_ targets. ([#361](https://github.com/nix-rust/nix/pull/361)) +- Added `TCP_INFO` sockopt to read TCP socket information on _linux_. + ([#2615](https://github.com/nix-rust/nix/pull/2615)) ### Changed - Changed the structure `IoVec` in `::nix::sys::uio`. From 46c7bb031408925e64cd7134181ce1dee5fee3af Mon Sep 17 00:00:00 2001 From: Biraj Parikh Date: Sun, 23 Mar 2025 21:40:41 -0700 Subject: [PATCH 3/4] Update changelog with pull request 2615 --- changelog/2615.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2615.added.md diff --git a/changelog/2615.added.md b/changelog/2615.added.md new file mode 100644 index 0000000000..86080626c0 --- /dev/null +++ b/changelog/2615.added.md @@ -0,0 +1 @@ + Added `TCP_INFO` sockopt to read TCP socket information on _linux_. From 7cb442625246953eab7f424f0b7d169e2ab98b32 Mon Sep 17 00:00:00 2001 From: Biraj Parikh Date: Sat, 29 Mar 2025 22:48:57 -0700 Subject: [PATCH 4/4] Revert "chore: CHANGELOG entry for TCP_INFO sockopt addition" This reverts commit 7e6cd7ebd1ec3be0cbf898423ffaf92adbc9ee63. --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f65a65be1..b673cac562 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1960,8 +1960,6 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Added `SIGINFO` in `::nix::sys::signal` for the _macos_ target as well as `SIGPWR` and `SIGSTKFLT` in `::nix::sys::signal` for non-_macos_ targets. ([#361](https://github.com/nix-rust/nix/pull/361)) -- Added `TCP_INFO` sockopt to read TCP socket information on _linux_. - ([#2615](https://github.com/nix-rust/nix/pull/2615)) ### Changed - Changed the structure `IoVec` in `::nix::sys::uio`.