Skip to content

fix only_v6() panic on windows #603

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1932,10 +1932,13 @@ impl Socket {
///
/// [`set_only_v6`]: Socket::set_only_v6
pub fn only_v6(&self) -> io::Result<bool> {
#[cfg(not(windows))]
unsafe {
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_V6ONLY)
getsockopt::<Bool>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_V6ONLY)
.map(|only_v6| only_v6 != 0)
}
#[cfg(windows)]
sys::only_v6(self.as_raw())
}

/// Set the value for the `IPV6_V6ONLY` option on this socket.
Expand Down
20 changes: 20 additions & 0 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,26 @@ pub(crate) fn to_mreqn(
}
}

pub(crate) fn only_v6(socket: RawSocket) -> io::Result<bool> {
let mut optval: c_int = 0; // input must be 4 bytes (DWORD)
let mut optlen = mem::size_of_val(&optval) as c_int;
syscall!(
getsockopt(
socket,
IPPROTO_IPV6 as i32,
IPV6_V6ONLY,
ptr::from_mut(&mut optval).cast(),
&mut optlen,
),
PartialEq::eq,
SOCKET_ERROR
)
.map(|_| {
// optlen may be 1, which won't match the input size of optval
optval != 0
})
}

#[cfg(feature = "all")]
pub(crate) fn original_dst_v4(socket: RawSocket) -> io::Result<SockAddr> {
unsafe {
Expand Down