std: add missing unsafe blocks

This commit is contained in:
joboet 2025-09-30 16:55:21 +02:00
parent eba1416c01
commit aa1263e768
No known key found for this signature in database
GPG key ID: 704E0149B0194B3C
6 changed files with 154 additions and 134 deletions

View file

@ -242,11 +242,11 @@ impl Socket {
None => netc::timeval { tv_sec: 0, tv_usec: 0 },
};
setsockopt(self, netc::SOL_SOCKET, kind, timeout)
unsafe { setsockopt(self, netc::SOL_SOCKET, kind, timeout) }
}
pub fn timeout(&self, kind: i32) -> io::Result<Option<Duration>> {
let raw: netc::timeval = getsockopt(self, netc::SOL_SOCKET, kind)?;
let raw: netc::timeval = unsafe { getsockopt(self, netc::SOL_SOCKET, kind)? };
if raw.tv_sec == 0 && raw.tv_usec == 0 {
Ok(None)
} else {
@ -272,22 +272,22 @@ impl Socket {
l_linger: linger.unwrap_or_default().as_secs() as libc::c_int,
};
setsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER, linger)
unsafe { setsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER, linger) }
}
pub fn linger(&self) -> io::Result<Option<Duration>> {
let val: netc::linger = getsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER)?;
let val: netc::linger = unsafe { getsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER)? };
Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
}
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
let value: i32 = if nodelay { 1 } else { 0 };
setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, value)
unsafe { setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, value) }
}
pub fn nodelay(&self) -> io::Result<bool> {
let raw: i32 = getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)?;
let raw: i32 = unsafe { getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)? };
Ok(raw != 0)
}
@ -304,7 +304,7 @@ impl Socket {
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)? };
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}

View file

@ -442,11 +442,11 @@ impl TcpStream {
}
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
sockname(|buf, len| unsafe { c::getpeername(self.inner.as_raw(), buf, len) })
unsafe { sockname(|buf, len| c::getpeername(self.inner.as_raw(), buf, len)) }
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
sockname(|buf, len| unsafe { c::getsockname(self.inner.as_raw(), buf, len) })
unsafe { sockname(|buf, len| c::getsockname(self.inner.as_raw(), buf, len)) }
}
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
@ -474,11 +474,11 @@ impl TcpStream {
}
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int) }
}
pub fn ttl(&self) -> io::Result<u32> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
let raw: c_int = unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)? };
Ok(raw as u32)
}
@ -545,7 +545,9 @@ impl TcpListener {
// which allows “socket hijacking”, so we explicitly don't set it here.
// https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
#[cfg(not(windows))]
setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1 as c_int)?;
unsafe {
setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1 as c_int)?
};
// Bind our new socket
let (addr, len) = socket_addr_to_c(addr);
@ -581,7 +583,7 @@ impl TcpListener {
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
sockname(|buf, len| unsafe { c::getsockname(self.inner.as_raw(), buf, len) })
unsafe { sockname(|buf, len| c::getsockname(self.inner.as_raw(), buf, len)) }
}
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
@ -600,20 +602,20 @@ impl TcpListener {
}
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int) }
}
pub fn ttl(&self) -> io::Result<u32> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
let raw: c_int = unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)? };
Ok(raw as u32)
}
pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int)
unsafe { setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int) }
}
pub fn only_v6(&self) -> io::Result<bool> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY)?;
let raw: c_int = unsafe { getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY)? };
Ok(raw != 0)
}
@ -676,11 +678,11 @@ impl UdpSocket {
}
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
sockname(|buf, len| unsafe { c::getpeername(self.inner.as_raw(), buf, len) })
unsafe { sockname(|buf, len| c::getpeername(self.inner.as_raw(), buf, len)) }
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
sockname(|buf, len| unsafe { c::getsockname(self.inner.as_raw(), buf, len) })
unsafe { sockname(|buf, len| c::getsockname(self.inner.as_raw(), buf, len)) }
}
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
@ -728,48 +730,62 @@ impl UdpSocket {
}
pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
setsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST, broadcast as c_int)
unsafe { setsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST, broadcast as c_int) }
}
pub fn broadcast(&self) -> io::Result<bool> {
let raw: c_int = getsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST)?;
let raw: c_int = unsafe { getsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST)? };
Ok(raw != 0)
}
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
setsockopt(
&self.inner,
c::IPPROTO_IP,
c::IP_MULTICAST_LOOP,
multicast_loop_v4 as IpV4MultiCastType,
)
unsafe {
setsockopt(
&self.inner,
c::IPPROTO_IP,
c::IP_MULTICAST_LOOP,
multicast_loop_v4 as IpV4MultiCastType,
)
}
}
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
let raw: IpV4MultiCastType = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
let raw: IpV4MultiCastType =
unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)? };
Ok(raw != 0)
}
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
setsockopt(
&self.inner,
c::IPPROTO_IP,
c::IP_MULTICAST_TTL,
multicast_ttl_v4 as IpV4MultiCastType,
)
unsafe {
setsockopt(
&self.inner,
c::IPPROTO_IP,
c::IP_MULTICAST_TTL,
multicast_ttl_v4 as IpV4MultiCastType,
)
}
}
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
let raw: IpV4MultiCastType = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
let raw: IpV4MultiCastType =
unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)? };
Ok(raw as u32)
}
pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int)
unsafe {
setsockopt(
&self.inner,
c::IPPROTO_IPV6,
c::IPV6_MULTICAST_LOOP,
multicast_loop_v6 as c_int,
)
}
}
pub fn multicast_loop_v6(&self) -> io::Result<bool> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP)?;
let raw: c_int =
unsafe { getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP)? };
Ok(raw != 0)
}
@ -778,7 +794,7 @@ impl UdpSocket {
imr_multiaddr: ip_v4_addr_to_c(multiaddr),
imr_interface: ip_v4_addr_to_c(interface),
};
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_ADD_MEMBERSHIP, mreq)
unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_ADD_MEMBERSHIP, mreq) }
}
pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
@ -786,7 +802,7 @@ impl UdpSocket {
ipv6mr_multiaddr: ip_v6_addr_to_c(multiaddr),
ipv6mr_interface: to_ipv6mr_interface(interface),
};
setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq)
unsafe { setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq) }
}
pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
@ -794,7 +810,7 @@ impl UdpSocket {
imr_multiaddr: ip_v4_addr_to_c(multiaddr),
imr_interface: ip_v4_addr_to_c(interface),
};
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_DROP_MEMBERSHIP, mreq)
unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_DROP_MEMBERSHIP, mreq) }
}
pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
@ -802,15 +818,15 @@ impl UdpSocket {
ipv6mr_multiaddr: ip_v6_addr_to_c(multiaddr),
ipv6mr_interface: to_ipv6mr_interface(interface),
};
setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, mreq)
unsafe { setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, mreq) }
}
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int) }
}
pub fn ttl(&self) -> io::Result<u32> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
let raw: c_int = unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)? };
Ok(raw as u32)
}

View file

@ -303,11 +303,11 @@ impl Socket {
}
None => netc::timeval { tv_sec: 0, tv_usec: 0 },
};
setsockopt(self, netc::SOL_SOCKET, kind, timeout)
unsafe { setsockopt(self, netc::SOL_SOCKET, kind, timeout) }
}
pub fn timeout(&self, kind: c_int) -> io::Result<Option<Duration>> {
let raw: netc::timeval = getsockopt(self, netc::SOL_SOCKET, kind)?;
let raw: netc::timeval = unsafe { getsockopt(self, netc::SOL_SOCKET, kind)? };
if raw.tv_sec == 0 && raw.tv_usec == 0 {
Ok(None)
} else {
@ -333,21 +333,21 @@ impl Socket {
l_linger: linger.unwrap_or_default().as_secs() as netc::c_int,
};
setsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER, linger)
unsafe { setsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER, linger) }
}
pub fn linger(&self) -> io::Result<Option<Duration>> {
let val: netc::linger = getsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER)?;
let val: netc::linger = unsafe { getsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER)? };
Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
}
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, nodelay as c_int)
unsafe { setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, nodelay as c_int) }
}
pub fn nodelay(&self) -> io::Result<bool> {
let raw: c_int = getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)?;
let raw: c_int = unsafe { getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)? };
Ok(raw != 0)
}
@ -360,7 +360,7 @@ impl Socket {
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
let raw: c_int = getsockopt(self, netc::SOL_SOCKET, netc::SO_ERROR)?;
let raw: c_int = unsafe { getsockopt(self, netc::SOL_SOCKET, netc::SO_ERROR)? };
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}

View file

@ -72,47 +72,45 @@ impl Socket {
}
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
unsafe {
cfg_select! {
any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "illumos",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "nto",
target_os = "solaris",
) => {
// On platforms that support it we pass the SOCK_CLOEXEC
// flag to atomically create the socket and set it as
// CLOEXEC. On Linux this was added in 2.6.27.
let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
let socket = Socket(FileDesc::from_raw_fd(fd));
cfg_select! {
any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "illumos",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "nto",
target_os = "solaris",
) => {
// On platforms that support it we pass the SOCK_CLOEXEC
// flag to atomically create the socket and set it as
// CLOEXEC. On Linux this was added in 2.6.27.
let fd = cvt(unsafe { libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0) })?;
let socket = Socket(unsafe { FileDesc::from_raw_fd(fd) });
// DragonFlyBSD, FreeBSD and NetBSD use `SO_NOSIGPIPE` as a `setsockopt`
// flag to disable `SIGPIPE` emission on socket.
#[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "dragonfly"))]
setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)?;
// DragonFlyBSD, FreeBSD and NetBSD use `SO_NOSIGPIPE` as a `setsockopt`
// flag to disable `SIGPIPE` emission on socket.
#[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "dragonfly"))]
unsafe { setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)? };
Ok(socket)
}
_ => {
let fd = cvt(libc::socket(fam, ty, 0))?;
let fd = FileDesc::from_raw_fd(fd);
fd.set_cloexec()?;
let socket = Socket(fd);
Ok(socket)
}
_ => {
let fd = cvt(unsafe { libc::socket(fam, ty, 0) })?;
let fd = unsafe { FileDesc::from_raw_fd(fd) };
fd.set_cloexec()?;
let socket = Socket(fd);
// macOS and iOS use `SO_NOSIGPIPE` as a `setsockopt`
// flag to disable `SIGPIPE` emission on socket.
#[cfg(target_vendor = "apple")]
setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)?;
// macOS and iOS use `SO_NOSIGPIPE` as a `setsockopt`
// flag to disable `SIGPIPE` emission on socket.
#[cfg(target_vendor = "apple")]
unsafe { setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)? };
Ok(socket)
}
Ok(socket)
}
}
}
@ -413,11 +411,11 @@ impl Socket {
}
None => libc::timeval { tv_sec: 0, tv_usec: 0 },
};
setsockopt(self, libc::SOL_SOCKET, kind, timeout)
unsafe { setsockopt(self, libc::SOL_SOCKET, kind, timeout) }
}
pub fn timeout(&self, kind: libc::c_int) -> io::Result<Option<Duration>> {
let raw: libc::timeval = getsockopt(self, libc::SOL_SOCKET, kind)?;
let raw: libc::timeval = unsafe { getsockopt(self, libc::SOL_SOCKET, kind)? };
if raw.tv_sec == 0 && raw.tv_usec == 0 {
Ok(None)
} else {
@ -444,7 +442,7 @@ impl Socket {
l_linger: linger.unwrap_or_default().as_secs() as libc::c_int,
};
setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger)
unsafe { setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger) }
}
#[cfg(target_os = "cygwin")]
@ -454,32 +452,32 @@ impl Socket {
l_linger: linger.unwrap_or_default().as_secs() as libc::c_ushort,
};
setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger)
unsafe { setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger) }
}
pub fn linger(&self) -> io::Result<Option<Duration>> {
let val: libc::linger = getsockopt(self, libc::SOL_SOCKET, SO_LINGER)?;
let val: libc::linger = unsafe { getsockopt(self, libc::SOL_SOCKET, SO_LINGER)? };
Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
}
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
setsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY, nodelay as c_int)
unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY, nodelay as c_int) }
}
pub fn nodelay(&self) -> io::Result<bool> {
let raw: c_int = getsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY)?;
let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY)? };
Ok(raw != 0)
}
#[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
pub fn set_quickack(&self, quickack: bool) -> io::Result<()> {
setsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK, quickack as c_int)
unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK, quickack as c_int) }
}
#[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
pub fn quickack(&self) -> io::Result<bool> {
let raw: c_int = getsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK)?;
let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK)? };
Ok(raw != 0)
}
@ -487,12 +485,12 @@ impl Socket {
#[cfg(target_os = "linux")]
pub fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
let val = cmp::min(accept.as_secs(), c_int::MAX as u64) as c_int;
setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, val)
unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, val) }
}
#[cfg(target_os = "linux")]
pub fn deferaccept(&self) -> io::Result<Duration> {
let raw: c_int = getsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT)?;
let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT)? };
Ok(Duration::from_secs(raw as _))
}
@ -506,21 +504,23 @@ impl Socket {
}
let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
arg.af_name = buf;
setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg)
unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg) }
} else {
setsockopt(
self,
libc::SOL_SOCKET,
libc::SO_ACCEPTFILTER,
core::ptr::null_mut() as *mut c_void,
)
unsafe {
setsockopt(
self,
libc::SOL_SOCKET,
libc::SO_ACCEPTFILTER,
core::ptr::null_mut() as *mut c_void,
)
}
}
}
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
pub fn acceptfilter(&self) -> io::Result<&CStr> {
let arg: libc::accept_filter_arg =
getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)?;
unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)? };
let s: &[u8] =
unsafe { core::slice::from_raw_parts(arg.af_name.as_ptr() as *const u8, 16) };
let name = CStr::from_bytes_with_nul(s).unwrap();
@ -531,53 +531,57 @@ impl Socket {
pub fn set_exclbind(&self, excl: bool) -> io::Result<()> {
// not yet on libc crate
const SO_EXCLBIND: i32 = 0x1015;
setsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND, excl)
unsafe { setsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND, excl) }
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
pub fn exclbind(&self) -> io::Result<bool> {
// not yet on libc crate
const SO_EXCLBIND: i32 = 0x1015;
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND)?;
let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND)? };
Ok(raw != 0)
}
#[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)
unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int) }
}
#[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
pub fn passcred(&self) -> io::Result<bool> {
let passcred: libc::c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)?;
let passcred: libc::c_int =
unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)? };
Ok(passcred != 0)
}
#[cfg(target_os = "netbsd")]
pub fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, local_creds as libc::c_int)
unsafe { setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, local_creds as libc::c_int) }
}
#[cfg(target_os = "netbsd")]
pub fn local_creds(&self) -> io::Result<bool> {
let local_creds: libc::c_int = getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)?;
let local_creds: libc::c_int =
unsafe { getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)? };
Ok(local_creds != 0)
}
#[cfg(target_os = "freebsd")]
pub fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
setsockopt(
self,
libc::AF_LOCAL,
libc::LOCAL_CREDS_PERSISTENT,
local_creds_persistent as libc::c_int,
)
unsafe {
setsockopt(
self,
libc::AF_LOCAL,
libc::LOCAL_CREDS_PERSISTENT,
local_creds_persistent as libc::c_int,
)
}
}
#[cfg(target_os = "freebsd")]
pub fn local_creds_persistent(&self) -> io::Result<bool> {
let local_creds_persistent: libc::c_int =
getsockopt(self, libc::AF_LOCAL, libc::LOCAL_CREDS_PERSISTENT)?;
unsafe { getsockopt(self, libc::AF_LOCAL, libc::LOCAL_CREDS_PERSISTENT)? };
Ok(local_creds_persistent != 0)
}
@ -590,7 +594,7 @@ impl Socket {
#[cfg(target_os = "vita")]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
let option = nonblocking as libc::c_int;
setsockopt(self, libc::SOL_SOCKET, libc::SO_NONBLOCK, option)
unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_NONBLOCK, option) }
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
@ -608,11 +612,11 @@ impl Socket {
let option = libc::SO_USER_COOKIE;
#[cfg(target_os = "openbsd")]
let option = libc::SO_RTABLE;
setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int)
unsafe { setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int) }
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)? };
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}

View file

@ -270,11 +270,11 @@ impl Socket {
}
None => netc::timeval { tv_sec: 0, tv_usec: 0 },
};
setsockopt(self, netc::SOL_SOCKET, kind, timeout)
unsafe { setsockopt(self, netc::SOL_SOCKET, kind, timeout) }
}
pub fn timeout(&self, kind: c_int) -> io::Result<Option<Duration>> {
let raw: netc::timeval = getsockopt(self, netc::SOL_SOCKET, kind)?;
let raw: netc::timeval = unsafe { getsockopt(self, netc::SOL_SOCKET, kind)? };
if raw.tv_sec == 0 && raw.tv_usec == 0 {
Ok(None)
} else {
@ -303,11 +303,11 @@ impl Socket {
}
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, nodelay as c_int)
unsafe { setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, nodelay as c_int) }
}
pub fn nodelay(&self) -> io::Result<bool> {
let raw: c_int = getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)?;
let raw: c_int = unsafe { getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)? };
Ok(raw != 0)
}
@ -317,7 +317,7 @@ impl Socket {
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
let raw: c_int = getsockopt(self, netc::SOL_SOCKET, netc::SO_ERROR)?;
let raw: c_int = unsafe { getsockopt(self, netc::SOL_SOCKET, netc::SO_ERROR)? };
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}

View file

@ -384,11 +384,11 @@ impl Socket {
}
None => 0,
};
setsockopt(self, c::SOL_SOCKET, kind, timeout)
unsafe { setsockopt(self, c::SOL_SOCKET, kind, timeout) }
}
pub fn timeout(&self, kind: c_int) -> io::Result<Option<Duration>> {
let raw: u32 = getsockopt(self, c::SOL_SOCKET, kind)?;
let raw: u32 = unsafe { getsockopt(self, c::SOL_SOCKET, kind)? };
if raw == 0 {
Ok(None)
} else {
@ -421,26 +421,26 @@ impl Socket {
l_linger: linger.unwrap_or_default().as_secs() as c_ushort,
};
setsockopt(self, c::SOL_SOCKET, c::SO_LINGER, linger)
unsafe { setsockopt(self, c::SOL_SOCKET, c::SO_LINGER, linger) }
}
pub fn linger(&self) -> io::Result<Option<Duration>> {
let val: c::LINGER = getsockopt(self, c::SOL_SOCKET, c::SO_LINGER)?;
let val: c::LINGER = unsafe { getsockopt(self, c::SOL_SOCKET, c::SO_LINGER)? };
Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
}
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
setsockopt(self, c::IPPROTO_TCP, c::TCP_NODELAY, nodelay as c::BOOL)
unsafe { setsockopt(self, c::IPPROTO_TCP, c::TCP_NODELAY, nodelay as c::BOOL) }
}
pub fn nodelay(&self) -> io::Result<bool> {
let raw: c::BOOL = getsockopt(self, c::IPPROTO_TCP, c::TCP_NODELAY)?;
let raw: c::BOOL = unsafe { getsockopt(self, c::IPPROTO_TCP, c::TCP_NODELAY)? };
Ok(raw != 0)
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
let raw: c_int = getsockopt(self, c::SOL_SOCKET, c::SO_ERROR)?;
let raw: c_int = unsafe { getsockopt(self, c::SOL_SOCKET, c::SO_ERROR)? };
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}