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

Replace winapi with windows-sys #303

Merged
merged 2 commits into from Apr 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions Cargo.toml
Expand Up @@ -35,8 +35,14 @@ features = ["all"]
[target."cfg(unix)".dependencies]
libc = "0.2.124"

[target."cfg(windows)".dependencies]
winapi = { version = "0.3.9", features = ["handleapi", "ws2ipdef", "ws2tcpip"] }
[target.'cfg(windows)'.dependencies.windows-sys]
version = "=0.36"
features = [
"Win32_Foundation",
"Win32_Networking_WinSock",
"Win32_System_IO",
"Win32_System_WindowsProgramming",
]

[features]
# Enable all API, even ones not available on all OSs.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -78,7 +78,7 @@ macro_rules! impl_debug {
$(#[$target: meta])*
// The flag(s) to check.
// Need to specific the libc crate because Windows doesn't use
// `libc` but `winapi`.
// `libc` but `windows_sys`.
$libc: ident :: $flag: ident
),+ $(,)*
) => {
Expand Down
18 changes: 7 additions & 11 deletions src/sockaddr.rs
Expand Up @@ -2,12 +2,13 @@ use std::mem::{self, size_of, MaybeUninit};
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use std::{fmt, io};

#[cfg(windows)]
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6_0;

use crate::sys::{
sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, AF_INET,
AF_INET6,
};
#[cfg(windows)]
use winapi::shared::ws2ipdef::SOCKADDR_IN6_LH_u;

/// The address of a socket.
///
Expand Down Expand Up @@ -183,7 +184,7 @@ impl SockAddr {
addr.sin6_scope_id,
#[cfg(windows)]
unsafe {
*addr.u.sin6_scope_id()
addr.Anonymous.sin6_scope_id
},
)))
} else {
Expand Down Expand Up @@ -249,13 +250,6 @@ impl From<SocketAddrV4> for SockAddr {

impl From<SocketAddrV6> for SockAddr {
fn from(addr: SocketAddrV6) -> SockAddr {
#[cfg(windows)]
let u = unsafe {
let mut u = mem::zeroed::<SOCKADDR_IN6_LH_u>();
*u.sin6_scope_id_mut() = addr.scope_id();
u
};

let sockaddr_in6 = sockaddr_in6 {
sin6_family: AF_INET6 as sa_family_t,
sin6_port: addr.port().to_be(),
Expand All @@ -264,7 +258,9 @@ impl From<SocketAddrV6> for SockAddr {
#[cfg(unix)]
sin6_scope_id: addr.scope_id(),
#[cfg(windows)]
u,
Anonymous: SOCKADDR_IN6_0 {
sin6_scope_id: addr.scope_id(),
},
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
Expand Down
2 changes: 0 additions & 2 deletions src/socket.rs
Expand Up @@ -1400,7 +1400,6 @@ impl Socket {
target_os = "netbsd",
target_os = "redox",
target_os = "solaris",
target_os = "windows",
)))]
pub fn set_recv_tos(&self, recv_tos: bool) -> io::Result<()> {
let recv_tos = if recv_tos { 1 } else { 0 };
Expand All @@ -1426,7 +1425,6 @@ impl Socket {
target_os = "netbsd",
target_os = "redox",
target_os = "solaris",
target_os = "windows",
)))]
pub fn recv_tos(&self) -> io::Result<bool> {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion src/sockref.rs
Expand Up @@ -128,7 +128,7 @@ where
/// See the `From<&impl AsRawFd>` implementation.
fn from(socket: &'s S) -> Self {
let socket = socket.as_raw_socket();
assert!(socket != winapi::um::winsock2::INVALID_SOCKET as _);
assert!(socket != windows_sys::Win32::Networking::WinSock::INVALID_SOCKET as _);
SockRef {
socket: ManuallyDrop::new(unsafe { Socket::from_raw_socket(socket) }),
_lifetime: PhantomData,
Expand Down
10 changes: 5 additions & 5 deletions src/sys/unix.rs
Expand Up @@ -174,7 +174,7 @@ macro_rules! syscall {

/// Maximum size of a buffer passed to system call like `recv` and `send`.
#[cfg(not(target_vendor = "apple"))]
const MAX_BUF_LEN: usize = <ssize_t>::max_value() as usize;
const MAX_BUF_LEN: usize = ssize_t::MAX as usize;

// The maximum read limit on most posix-like systems is `SSIZE_MAX`, with the
// man page quoting that if the count of bytes to read is greater than
Expand All @@ -185,7 +185,7 @@ const MAX_BUF_LEN: usize = <ssize_t>::max_value() as usize;
// than or equal to INT_MAX. To handle both of these the read size is capped on
// both platforms.
#[cfg(target_vendor = "apple")]
const MAX_BUF_LEN: usize = <c_int>::max_value() as usize - 1;
const MAX_BUF_LEN: usize = c_int::MAX as usize - 1;

#[cfg(any(
all(
Expand Down Expand Up @@ -622,7 +622,7 @@ pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Res
}

let timeout = (timeout - elapsed).as_millis();
let timeout = clamp(timeout, 1, c_int::max_value() as u128) as c_int;
let timeout = clamp(timeout, 1, c_int::MAX as u128) as c_int;

match syscall!(poll(&mut pollfd, 1, timeout)) {
Ok(0) => return Err(io::ErrorKind::TimedOut.into()),
Expand Down Expand Up @@ -878,7 +878,7 @@ fn into_timeval(duration: Option<Duration>) -> libc::timeval {
// https://github.com/rust-lang/libc/issues/1848
#[cfg_attr(target_env = "musl", allow(deprecated))]
Some(duration) => libc::timeval {
tv_sec: min(duration.as_secs(), libc::time_t::max_value() as u64) as libc::time_t,
tv_sec: min(duration.as_secs(), libc::time_t::MAX as u64) as libc::time_t,
tv_usec: duration.subsec_micros() as libc::suseconds_t,
},
None => libc::timeval {
Expand Down Expand Up @@ -931,7 +931,7 @@ pub(crate) fn set_tcp_keepalive(fd: Socket, keepalive: &TcpKeepalive) -> io::Res

#[cfg(not(any(target_os = "haiku", target_os = "openbsd")))]
fn into_secs(duration: Duration) -> c_int {
min(duration.as_secs(), c_int::max_value() as u64) as c_int
min(duration.as_secs(), c_int::MAX as u64) as c_int
}

/// Add `flag` to the current set flags of `F_GETFD`.
Expand Down