Debian Codegraph @

.cargo/vendor/rustix/src/net/wsa.rs

raw
1 use crate::io;
2 use core::mem::MaybeUninit;
3 use windows_sys::Win32::Networking::WinSock::{WSACleanup, WSAGetLastError, WSAStartup, WSADATA};
4
5 /// `WSAStartup()`—Initialize process-wide Windows support for sockets.
6 ///
7 /// On Windows, it's necessary to initialize the sockets subsystem before
8 /// using sockets APIs. The function performs the necessary initialization.
9 ///
10 /// # References
11 /// - [Winsock]
12 ///
13 /// [Winsock]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup
14 pub fn wsa_startup() -> io::Result<WSADATA> {
15 // Request version 2.2, which has been the latest version since far older
16 // versions of Windows than we support here. For more information about
17 // the version, see [here].
18 //
19 // [here]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup#remarks
20 let version = 0x202;
21 let mut data = MaybeUninit::uninit();
22 unsafe {
23 let ret = WSAStartup(version, data.as_mut_ptr());
24 if ret == 0 {
25 Ok(data.assume_init())
26 } else {
27 Err(io::Errno::from_raw_os_error(WSAGetLastError()))
28 }
29 }
30 }
31
32 /// `WSACleanup()`—Clean up process-wide Windows support for sockets.
33 ///
34 /// In a program where `init` is called, if sockets are no longer necessary,
35 /// this function releases associated resources.
36 ///
37 /// # References
38 /// - [Winsock]
39 ///
40 /// [Winsock]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsacleanup
41 pub fn wsa_cleanup() -> io::Result<()> {
42 unsafe {
43 if WSACleanup() == 0 {
44 Ok(())
45 } else {
46 Err(io::Errno::from_raw_os_error(WSAGetLastError()))
47 }
48 }
49 }