Skip to content

Commit aeb6fac

Browse files
authored
sys::socket::sockopt, adding ExclBind for solarish systems. (#2573)
* sys::socket::sockopt, adding `ExclBind` for solarish systems. Disallow multiple sockets to bind to an address/port combination. * changelog entry
1 parent 826203a commit aeb6fac

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

changelog/2573.added.md

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

src/sys/socket/sockopt.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,18 @@ sockopt_impl!(
12571257
GetCString<[u8; libc::IFNAMSIZ]>
12581258
);
12591259

1260+
#[cfg(solarish)]
1261+
sockopt_impl!(
1262+
/// Enable/disable exclusive binding.
1263+
/// Prevent multiple sockets to bind to the same
1264+
/// address:port, neutralizing `SO_REUSEADDR` effect.
1265+
ExclBind,
1266+
Both,
1267+
libc::SOL_SOCKET,
1268+
libc::SO_EXCLBIND,
1269+
bool
1270+
);
1271+
12601272
#[allow(missing_docs)]
12611273
// Not documented by Linux!
12621274
#[cfg(linux_android)]

test/sys/test_sockopt.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1133,3 +1133,38 @@ mod sockopt_impl {
11331133
assert_eq!(get_linger.l_linger, set_linger.l_linger);
11341134
}
11351135
}
1136+
1137+
#[cfg(solarish)]
1138+
#[test]
1139+
fn test_exclbind() {
1140+
use nix::errno::Errno;
1141+
use nix::sys::socket::{
1142+
bind, socket, AddressFamily, SockFlag, SockType, SockaddrIn,
1143+
};
1144+
use std::net::SocketAddrV4;
1145+
use std::str::FromStr;
1146+
let fd1 = socket(
1147+
AddressFamily::Inet,
1148+
SockType::Stream,
1149+
SockFlag::empty(),
1150+
None,
1151+
)
1152+
.unwrap();
1153+
let addr = SocketAddrV4::from_str("127.0.0.1:8081").unwrap();
1154+
let excl = true;
1155+
1156+
setsockopt(&fd1, sockopt::ExclBind, &excl).unwrap();
1157+
bind(fd1.as_raw_fd(), &SockaddrIn::from(addr)).unwrap();
1158+
assert_eq!(getsockopt(&fd1, sockopt::ExclBind), Ok(true));
1159+
let fd2 = socket(
1160+
AddressFamily::Inet,
1161+
SockType::Stream,
1162+
SockFlag::empty(),
1163+
None,
1164+
)
1165+
.unwrap();
1166+
assert_eq!(
1167+
bind(fd2.as_raw_fd(), &SockaddrIn::from(addr)),
1168+
Err(Errno::EADDRINUSE)
1169+
);
1170+
}

0 commit comments

Comments
 (0)