Skip to content

Commit fea2cb4

Browse files
authored
Add support for SO_REUSEPORT_LB
1 parent 3047737 commit fea2cb4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/sys/unix.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,35 @@ impl crate::Socket {
19831983
}
19841984
}
19851985

1986+
/// Get the value of the `SO_REUSEPORT_LB` option on this socket.
1987+
///
1988+
/// For more information about this option, see [`set_reuse_port_lb`].
1989+
///
1990+
/// [`set_reuse_port_lb`]: crate::Socket::set_reuse_port_lb
1991+
#[cfg(all(feature = "all", target_os = "freebsd"))]
1992+
pub fn reuse_port_lb(&self) -> io::Result<bool> {
1993+
unsafe {
1994+
getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_REUSEPORT_LB)
1995+
.map(|reuse| reuse != 0)
1996+
}
1997+
}
1998+
1999+
/// Set value for the `SO_REUSEPORT_LB` option on this socket.
2000+
///
2001+
/// This allows multiple programs or threads to bind to the same port and
2002+
/// incoming connections will be load balanced using a hash function.
2003+
#[cfg(all(feature = "all", target_os = "freebsd"))]
2004+
pub fn set_reuse_port_lb(&self, reuse: bool) -> io::Result<()> {
2005+
unsafe {
2006+
setsockopt(
2007+
self.as_raw(),
2008+
libc::SOL_SOCKET,
2009+
libc::SO_REUSEPORT_LB,
2010+
reuse as c_int,
2011+
)
2012+
}
2013+
}
2014+
19862015
/// Get the value of the `IP_FREEBIND` option on this socket.
19872016
///
19882017
/// For more information about this option, see [`set_freebind`].

tests/socket.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,8 @@ test!(reuse_address, set_reuse_address(true));
12281228
not(any(windows, target_os = "solaris", target_os = "illumos"))
12291229
))]
12301230
test!(reuse_port, set_reuse_port(true));
1231+
#[cfg(all(feature = "all", target_os = "freebsd"))]
1232+
test!(reuse_port_lb, set_reuse_port_lb(true));
12311233
#[cfg(all(feature = "all", unix, not(target_os = "redox")))]
12321234
test!(
12331235
#[cfg_attr(target_os = "linux", ignore = "Different value returned")]

0 commit comments

Comments
 (0)