Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2572.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `sockopt::LingerSec` for Apple targets
9 changes: 9 additions & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,15 @@ sockopt_impl!(
libc::SO_LINGER,
libc::linger
);
#[cfg(apple_targets)]
sockopt_impl!(
/// Same as `SO_LINGER`, but the duration is in seconds rather than kernel ticks.
LingerSec,
Both,
libc::SOL_SOCKET,
libc::SO_LINGER_SEC,
libc::linger
);
#[cfg(feature = "net")]
sockopt_impl!(
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
Expand Down
51 changes: 36 additions & 15 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,13 +1063,49 @@ fn test_ipv6_recv_traffic_class_opts() {
);
}

#[cfg(apple_targets)]
#[test]
fn test_linger_sec() {
let fd = socket(
AddressFamily::Inet,
SockType::Stream,
SockFlag::empty(),
None,
)
.unwrap();

let set_linger = libc::linger {
l_onoff: 1,
l_linger: 1,
};
setsockopt(&fd, sockopt::LingerSec, &set_linger).unwrap();

let get_linger = getsockopt(&fd, sockopt::Linger).unwrap();
assert_eq!(get_linger.l_linger, set_linger.l_linger * 100);
}

/// Users should be able to define their own sockopts.
mod sockopt_impl {
use nix::sys::socket::{
getsockopt, setsockopt, socket, AddressFamily, SockFlag, SockProtocol,
SockType,
};

sockopt_impl!(KeepAlive, Both, libc::SOL_SOCKET, libc::SO_KEEPALIVE, bool);

#[test]
fn test_so_tcp_keepalive() {
let fd = socket(
AddressFamily::Inet,
SockType::Stream,
SockFlag::empty(),
SockProtocol::Tcp,
)
.unwrap();
setsockopt(&fd, KeepAlive, &true).unwrap();
assert!(getsockopt(&fd, KeepAlive).unwrap());
}

sockopt_impl!(
Linger,
Both,
Expand All @@ -1096,19 +1132,4 @@ mod sockopt_impl {
let get_linger = getsockopt(&fd, Linger).unwrap();
assert_eq!(get_linger.l_linger, set_linger.l_linger);
}

sockopt_impl!(KeepAlive, Both, libc::SOL_SOCKET, libc::SO_KEEPALIVE, bool);

#[test]
fn test_so_tcp_keepalive() {
let fd = socket(
AddressFamily::Inet,
SockType::Stream,
SockFlag::empty(),
SockProtocol::Tcp,
)
.unwrap();
setsockopt(&fd, KeepAlive, &true).unwrap();
assert!(getsockopt(&fd, KeepAlive).unwrap());
}
}
Loading