Skip to content

Commit

Permalink
g3-socket: try to fix win sock usage
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed Oct 23, 2024
1 parent c79d126 commit d5adead
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ capnpc = "0.20"
#
libc = "0.2.156"
rustix = { version = "0.38", default-features = false }
windows-sys = "0.59"
gethostname = "0.5"
#
serde = "1.0"
Expand Down
7 changes: 6 additions & 1 deletion lib/g3-socket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ rust-version = "1.80.0"
[dependencies]
tokio = { workspace = true, features = ["net"] }
socket2 = { version = "0.5", features = ["all"] }
libc.workspace = true
fastrand.workspace = true
g3-types.workspace = true

[target.'cfg(unix)'.dependencies]
libc.workspace = true

[target.'cfg(windows)'.dependencies]
windows-sys.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["macros", "rt"] }
3 changes: 1 addition & 2 deletions lib/g3-socket/src/raw/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::io::{self, IoSlice};
use std::net::SocketAddr;
use std::os::windows::io::{AsRawSocket, FromRawSocket};
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket};

use socket2::{MsgHdr, SockAddr, Socket};

Expand All @@ -37,7 +37,6 @@ impl RawSocket {
}
}

#[cfg(unix)]
impl Drop for RawSocket {
fn drop(&mut self) {
if let Some(s) = self.inner.take() {
Expand Down
24 changes: 9 additions & 15 deletions lib/g3-socket/src/sockopt/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@
use std::io;
use std::os::windows::io::AsRawSocket;

use libc::{c_char, c_int, SOCKET};
use windows_sys::Win32::Networking::WinSock;

// windows_sys::Win32::Networking::WinSock::SOL_SOCKET
const SOL_SOCKET: i32 = 65535i32;

// windows_sys::Win32::Networking::WinSock::SO_REUSE_UNICASTPORT
const SO_REUSE_UNICASTPORT: i32 = 12295i32;

unsafe fn setsockopt<T>(socket: SOCKET, level: c_int, name: c_int, value: T) -> io::Result<()>
unsafe fn setsockopt<T>(socket: WinSock::SOCKET, level: i32, name: i32, value: T) -> io::Result<()>
where
T: Copy,
{
let payload = &value as *const T as *const c_char;
let ret = libc::setsockopt(socket, level, name, payload, size_of::<T>() as c_int);
if ret == -1 {
let payload = &value as *const T as *const u8;
let ret = WinSock::setsockopt(socket, level, name, payload, size_of::<T>() as i32);
if ret == WinSock::SOCKET_ERROR {
return Err(io::Error::last_os_error());
}
Ok(())
Expand All @@ -42,10 +36,10 @@ pub(crate) fn set_reuse_unicastport<T: AsRawSocket>(socket: &T, enable: bool) ->
setsockopt(
// std::os::windows::raw::SOCKET is u64
// windows_sys::Win32::Networking::WinSock::SOCKET is usize
socket.as_raw_socket() as SOCKET,
SOL_SOCKET,
SO_REUSE_UNICASTPORT,
enable as c_int,
socket.as_raw_socket() as _,
WinSock::SOL_SOCKET,
WinSock::SO_REUSE_UNICASTPORT,
enable as i32,
)?;
Ok(())
}
Expand Down
13 changes: 9 additions & 4 deletions lib/g3-socket/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,16 @@ mod tests {
Default::default(),
)
.unwrap();
let local_addr = socket.local_addr().unwrap();
assert_eq!(local_addr.port(), 0);
let local_addr1 = socket.local_addr().unwrap();
assert_eq!(local_addr1.ip(), IpAddr::V4(Ipv4Addr::UNSPECIFIED));
#[cfg(unix)]
assert_eq!(local_addr1.port(), 0);
#[cfg(windows)]
assert_ne!(local_addr1.port(), 0);
socket.connect(peer_addr).unwrap();
let local_addr = socket.local_addr().unwrap();
assert_ne!(local_addr.port(), 0);
let local_addr2 = socket.local_addr().unwrap();
assert_ne!(local_addr2.port(), 0);
assert_ne!(local_addr1, local_addr2);
}

#[test]
Expand Down

0 comments on commit d5adead

Please sign in to comment.