Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys::socket::sockopt, adding ExclBind for solarish systems. #2573

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
);
}
Loading