Skip to content

Commit 826203a

Browse files
authored
sys::socket adding sockopt::LingerSec for Apple targets. (#2572)
1 parent e789a7c commit 826203a

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

changelog/2572.added.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `sockopt::LingerSec` for Apple targets

src/sys/socket/sockopt.rs

+9
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ sockopt_impl!(
372372
libc::SO_LINGER,
373373
libc::linger
374374
);
375+
#[cfg(apple_targets)]
376+
sockopt_impl!(
377+
/// Same as `SO_LINGER`, but the duration is in seconds rather than kernel ticks.
378+
LingerSec,
379+
Both,
380+
libc::SOL_SOCKET,
381+
libc::SO_LINGER_SEC,
382+
libc::linger
383+
);
375384
#[cfg(feature = "net")]
376385
sockopt_impl!(
377386
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]

test/sys/test_sockopt.rs

+36-15
Original file line numberDiff line numberDiff line change
@@ -1063,13 +1063,49 @@ fn test_ipv6_recv_traffic_class_opts() {
10631063
);
10641064
}
10651065

1066+
#[cfg(apple_targets)]
1067+
#[test]
1068+
fn test_linger_sec() {
1069+
let fd = socket(
1070+
AddressFamily::Inet,
1071+
SockType::Stream,
1072+
SockFlag::empty(),
1073+
None,
1074+
)
1075+
.unwrap();
1076+
1077+
let set_linger = libc::linger {
1078+
l_onoff: 1,
1079+
l_linger: 1,
1080+
};
1081+
setsockopt(&fd, sockopt::LingerSec, &set_linger).unwrap();
1082+
1083+
let get_linger = getsockopt(&fd, sockopt::Linger).unwrap();
1084+
assert_eq!(get_linger.l_linger, set_linger.l_linger * 100);
1085+
}
1086+
10661087
/// Users should be able to define their own sockopts.
10671088
mod sockopt_impl {
10681089
use nix::sys::socket::{
10691090
getsockopt, setsockopt, socket, AddressFamily, SockFlag, SockProtocol,
10701091
SockType,
10711092
};
10721093

1094+
sockopt_impl!(KeepAlive, Both, libc::SOL_SOCKET, libc::SO_KEEPALIVE, bool);
1095+
1096+
#[test]
1097+
fn test_so_tcp_keepalive() {
1098+
let fd = socket(
1099+
AddressFamily::Inet,
1100+
SockType::Stream,
1101+
SockFlag::empty(),
1102+
SockProtocol::Tcp,
1103+
)
1104+
.unwrap();
1105+
setsockopt(&fd, KeepAlive, &true).unwrap();
1106+
assert!(getsockopt(&fd, KeepAlive).unwrap());
1107+
}
1108+
10731109
sockopt_impl!(
10741110
Linger,
10751111
Both,
@@ -1096,19 +1132,4 @@ mod sockopt_impl {
10961132
let get_linger = getsockopt(&fd, Linger).unwrap();
10971133
assert_eq!(get_linger.l_linger, set_linger.l_linger);
10981134
}
1099-
1100-
sockopt_impl!(KeepAlive, Both, libc::SOL_SOCKET, libc::SO_KEEPALIVE, bool);
1101-
1102-
#[test]
1103-
fn test_so_tcp_keepalive() {
1104-
let fd = socket(
1105-
AddressFamily::Inet,
1106-
SockType::Stream,
1107-
SockFlag::empty(),
1108-
SockProtocol::Tcp,
1109-
)
1110-
.unwrap();
1111-
setsockopt(&fd, KeepAlive, &true).unwrap();
1112-
assert!(getsockopt(&fd, KeepAlive).unwrap());
1113-
}
11141135
}

0 commit comments

Comments
 (0)