From 75d910ef7fd799057804b517ff5cb8511513d981 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 30 Dec 2024 15:31:49 +0000 Subject: [PATCH 1/2] sys::socket::sockopt, adding `ExclBind` for solarish systems. Disallow multiple sockets to bind to an address/port combination. --- src/sys/socket/sockopt.rs | 12 ++++++++++++ test/sys/test_sockopt.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index cfa407de00..1b61e976f0 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -1257,6 +1257,18 @@ sockopt_impl!( GetCString<[u8; libc::IFNAMSIZ]> ); +#[cfg(solarish)] +sockopt_impl!( + /// Enable/disable exclusive binding. + /// Prevent multiple sockets to bind to the same + /// address:port, neutralizing `SO_REUSEADDR` effect. + ExclBind, + Both, + libc::SOL_SOCKET, + libc::SO_EXCLBIND, + bool +); + #[allow(missing_docs)] // Not documented by Linux! #[cfg(linux_android)] diff --git a/test/sys/test_sockopt.rs b/test/sys/test_sockopt.rs index 83f796e44c..42697ffa24 100644 --- a/test/sys/test_sockopt.rs +++ b/test/sys/test_sockopt.rs @@ -1133,3 +1133,38 @@ mod sockopt_impl { assert_eq!(get_linger.l_linger, set_linger.l_linger); } } + +#[cfg(solarish)] +#[test] +fn test_exclbind() { + use nix::errno::Errno; + use nix::sys::socket::{ + bind, socket, AddressFamily, SockFlag, SockType, SockaddrIn, + }; + use std::net::SocketAddrV4; + use std::str::FromStr; + let fd1 = socket( + AddressFamily::Inet, + SockType::Stream, + SockFlag::empty(), + None, + ) + .unwrap(); + let addr = SocketAddrV4::from_str("127.0.0.1:8081").unwrap(); + let excl = true; + + setsockopt(&fd1, sockopt::ExclBind, &excl).unwrap(); + bind(fd1.as_raw_fd(), &SockaddrIn::from(addr)).unwrap(); + assert_eq!(getsockopt(&fd1, sockopt::ExclBind), Ok(true)); + let fd2 = socket( + AddressFamily::Inet, + SockType::Stream, + SockFlag::empty(), + None, + ) + .unwrap(); + assert_eq!( + bind(fd2.as_raw_fd(), &SockaddrIn::from(addr)), + Err(Errno::EADDRINUSE) + ); +} From ca756aefcf740e476191ad76b68a525f7c14c39c Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 30 Dec 2024 15:45:24 +0000 Subject: [PATCH 2/2] changelog entry --- changelog/2573.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2573.added.md diff --git a/changelog/2573.added.md b/changelog/2573.added.md new file mode 100644 index 0000000000..1b312aa861 --- /dev/null +++ b/changelog/2573.added.md @@ -0,0 +1 @@ +Added `sockopt::EsclBind` for solarish targets