From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/rustix/src/net/wsa.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 vendor/rustix/src/net/wsa.rs (limited to 'vendor/rustix/src/net/wsa.rs') diff --git a/vendor/rustix/src/net/wsa.rs b/vendor/rustix/src/net/wsa.rs new file mode 100644 index 000000000..e2a70c97b --- /dev/null +++ b/vendor/rustix/src/net/wsa.rs @@ -0,0 +1,49 @@ +use crate::io; +use core::mem::MaybeUninit; +use windows_sys::Win32::Networking::WinSock::{WSACleanup, WSAData, WSAGetLastError, WSAStartup}; + +/// `WSAStartup()`—Initialize process-wide Windows support for sockets. +/// +/// On Windows, it's necessary to initialize the sockets subsystem before +/// using sockets APIs. The function performs the necessary initialization. +/// +/// # References +/// - [Winsock2] +/// +/// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup +pub fn wsa_startup() -> io::Result { + // Request version 2.2, which has been the latest version since far older + // versions of Windows than we support here. For more information about + // the version, see [here]. + // + // [here]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup#remarks + let version = 0x202; + let mut data = MaybeUninit::uninit(); + unsafe { + let ret = WSAStartup(version, data.as_mut_ptr()); + if ret == 0 { + Ok(data.assume_init()) + } else { + Err(io::Errno::from_raw_os_error(WSAGetLastError())) + } + } +} + +/// `WSACleanup()`—Clean up process-wide Windows support for sockets. +/// +/// In a program where `init` is called, if sockets are no longer necessary, +/// this function releases associated resources. +/// +/// # References +/// - [Winsock2] +/// +/// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsacleanup +pub fn wsa_cleanup() -> io::Result<()> { + unsafe { + if WSACleanup() == 0 { + Ok(()) + } else { + Err(io::Errno::from_raw_os_error(WSAGetLastError())) + } + } +} -- cgit v1.2.3