summaryrefslogtreecommitdiffstats
path: root/third_party/rust/rustix/src/net/wsa.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/rustix/src/net/wsa.rs
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/rustix/src/net/wsa.rs')
-rw-r--r--third_party/rust/rustix/src/net/wsa.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/third_party/rust/rustix/src/net/wsa.rs b/third_party/rust/rustix/src/net/wsa.rs
new file mode 100644
index 0000000000..0ad4db5eb6
--- /dev/null
+++ b/third_party/rust/rustix/src/net/wsa.rs
@@ -0,0 +1,49 @@
+use crate::io;
+use core::mem::MaybeUninit;
+use windows_sys::Win32::Networking::WinSock::{WSACleanup, WSAGetLastError, WSAStartup, WSADATA};
+
+/// `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
+/// - [Winsock]
+///
+/// [Winsock]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup
+pub fn wsa_startup() -> io::Result<WSADATA> {
+ // 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
+/// - [Winsock]
+///
+/// [Winsock]: 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()))
+ }
+ }
+}