diff --git a/changelog/2572.added.md b/changelog/2572.added.md new file mode 100644 index 0000000000..950ab1443c --- /dev/null +++ b/changelog/2572.added.md @@ -0,0 +1 @@ +Added `sockopt::LingerSec` for Apple targets diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index 7ec4f68f07..cfa407de00 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -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")))] diff --git a/test/sys/test_sockopt.rs b/test/sys/test_sockopt.rs index b40fb08d08..83f796e44c 100644 --- a/test/sys/test_sockopt.rs +++ b/test/sys/test_sockopt.rs @@ -1063,6 +1063,27 @@ 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::{ @@ -1070,6 +1091,21 @@ mod sockopt_impl { 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, @@ -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()); - } }