Skip to content

Commit

Permalink
sys::socket::sockopt, adding ExclBind for solarish systems. (#2573)
Browse files Browse the repository at this point in the history
* sys::socket::sockopt, adding `ExclBind` for solarish systems.

Disallow multiple sockets to bind to an address/port combination.

* changelog entry
  • Loading branch information
devnexen authored Dec 31, 2024
1 parent 826203a commit aeb6fac
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2573.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `sockopt::EsclBind` for solarish targets
12 changes: 12 additions & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
35 changes: 35 additions & 0 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

0 comments on commit aeb6fac

Please sign in to comment.