Skip to content

Commit

Permalink
Initialize networking before running connect()
Browse files Browse the repository at this point in the history
This commit adds a setup_networking function. On most platforms it does
nothing. However, on Windows it calls `wsa_startup`. This ensures that
all networking is set up before anything else is called.

Closes #182

Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Jan 26, 2024
1 parent d4218b8 commit fcc4435
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,8 @@ fn connect(
#[cfg(windows)]
use rustix::fd::AsFd;

setup_networking();

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
Expand Down Expand Up @@ -2168,6 +2170,20 @@ fn connect(
Ok(socket)
}

#[inline]
fn setup_networking() {
#[cfg(windows)]
{
// On Windows, we need to call WSAStartup before calling any networking code.
// Make sure to call it at least once.
static INIT: std::sync::Once = std::sync::Once::new();

INIT.call_once(|| {
let _ = rustix::net::wsa_startup();
});
}
}

#[inline]
fn set_nonblocking(
#[cfg(unix)] fd: BorrowedFd<'_>,
Expand Down

0 comments on commit fcc4435

Please sign in to comment.