Skip to content

Commit b221712

Browse files
committed
Windows: move BSD socket shims to netc
1 parent 0968298 commit b221712

File tree

4 files changed

+110
-111
lines changed

4 files changed

+110
-111
lines changed

library/std/src/sys/pal/windows/c.rs

+1-98
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::ffi::CStr;
99
use crate::mem;
1010
use crate::num::NonZero;
1111
pub use crate::os::raw::c_int;
12-
use crate::os::raw::{c_char, c_long, c_longlong, c_uint, c_ulong, c_ushort, c_void};
12+
use crate::os::raw::{c_long, c_longlong, c_uint, c_ulong, c_ushort, c_void};
1313
use crate::os::windows::io::{AsRawHandle, BorrowedHandle};
1414
use crate::ptr;
1515

@@ -25,9 +25,7 @@ pub type LARGE_INTEGER = c_longlong;
2525
pub type LONG = c_long;
2626
pub type UINT = c_uint;
2727
pub type WCHAR = u16;
28-
pub type USHORT = c_ushort;
2928
pub type SIZE_T = usize;
30-
pub type CHAR = c_char;
3129
pub type ULONG = c_ulong;
3230

3331
pub type LPCVOID = *const c_void;
@@ -40,12 +38,6 @@ pub type LPWSTR = *mut WCHAR;
4038
#[cfg(target_vendor = "win7")]
4139
pub type PSRWLOCK = *mut SRWLOCK;
4240

43-
pub type socklen_t = c_int;
44-
pub type ADDRESS_FAMILY = USHORT;
45-
pub use FD_SET as fd_set;
46-
pub use LINGER as linger;
47-
pub use TIMEVAL as timeval;
48-
4941
pub const INVALID_HANDLE_VALUE: HANDLE = ::core::ptr::without_provenance_mut(-1i32 as _);
5042

5143
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/exit-success-exit-failure?view=msvc-170
@@ -63,20 +55,6 @@ pub const INIT_ONCE_STATIC_INIT: INIT_ONCE = INIT_ONCE { Ptr: ptr::null_mut() };
6355
pub const OBJ_DONT_REPARSE: u32 = windows_sys::OBJ_DONT_REPARSE as u32;
6456
pub const FRS_ERR_SYSVOL_POPULATE_TIMEOUT: u32 =
6557
windows_sys::FRS_ERR_SYSVOL_POPULATE_TIMEOUT as u32;
66-
pub const AF_INET: c_int = windows_sys::AF_INET as c_int;
67-
pub const AF_INET6: c_int = windows_sys::AF_INET6 as c_int;
68-
69-
#[repr(C)]
70-
pub struct ip_mreq {
71-
pub imr_multiaddr: in_addr,
72-
pub imr_interface: in_addr,
73-
}
74-
75-
#[repr(C)]
76-
pub struct ipv6_mreq {
77-
pub ipv6mr_multiaddr: in6_addr,
78-
pub ipv6mr_interface: c_uint,
79-
}
8058

8159
// Equivalent to the `NT_SUCCESS` C preprocessor macro.
8260
// See: https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/using-ntstatus-values
@@ -148,45 +126,6 @@ pub struct MOUNT_POINT_REPARSE_BUFFER {
148126
pub PathBuffer: WCHAR,
149127
}
150128

151-
#[repr(C)]
152-
pub struct SOCKADDR_STORAGE_LH {
153-
pub ss_family: ADDRESS_FAMILY,
154-
pub __ss_pad1: [CHAR; 6],
155-
pub __ss_align: i64,
156-
pub __ss_pad2: [CHAR; 112],
157-
}
158-
159-
#[repr(C)]
160-
#[derive(Copy, Clone)]
161-
pub struct sockaddr_in {
162-
pub sin_family: ADDRESS_FAMILY,
163-
pub sin_port: USHORT,
164-
pub sin_addr: in_addr,
165-
pub sin_zero: [CHAR; 8],
166-
}
167-
168-
#[repr(C)]
169-
#[derive(Copy, Clone)]
170-
pub struct sockaddr_in6 {
171-
pub sin6_family: ADDRESS_FAMILY,
172-
pub sin6_port: USHORT,
173-
pub sin6_flowinfo: c_ulong,
174-
pub sin6_addr: in6_addr,
175-
pub sin6_scope_id: c_ulong,
176-
}
177-
178-
#[repr(C)]
179-
#[derive(Copy, Clone)]
180-
pub struct in_addr {
181-
pub s_addr: u32,
182-
}
183-
184-
#[repr(C)]
185-
#[derive(Copy, Clone)]
186-
pub struct in6_addr {
187-
pub s6_addr: [u8; 16],
188-
}
189-
190129
// Desktop specific functions & types
191130
cfg_if::cfg_if! {
192131
if #[cfg(not(target_vendor = "uwp"))] {
@@ -226,42 +165,6 @@ pub unsafe extern "system" fn ReadFileEx(
226165
)
227166
}
228167

229-
// POSIX compatibility shims.
230-
pub unsafe fn recv(socket: SOCKET, buf: *mut c_void, len: c_int, flags: c_int) -> c_int {
231-
windows_sys::recv(socket, buf.cast::<u8>(), len, flags)
232-
}
233-
pub unsafe fn send(socket: SOCKET, buf: *const c_void, len: c_int, flags: c_int) -> c_int {
234-
windows_sys::send(socket, buf.cast::<u8>(), len, flags)
235-
}
236-
pub unsafe fn recvfrom(
237-
socket: SOCKET,
238-
buf: *mut c_void,
239-
len: c_int,
240-
flags: c_int,
241-
addr: *mut SOCKADDR,
242-
addrlen: *mut c_int,
243-
) -> c_int {
244-
windows_sys::recvfrom(socket, buf.cast::<u8>(), len, flags, addr, addrlen)
245-
}
246-
pub unsafe fn sendto(
247-
socket: SOCKET,
248-
buf: *const c_void,
249-
len: c_int,
250-
flags: c_int,
251-
addr: *const SOCKADDR,
252-
addrlen: c_int,
253-
) -> c_int {
254-
windows_sys::sendto(socket, buf.cast::<u8>(), len, flags, addr, addrlen)
255-
}
256-
pub unsafe fn getaddrinfo(
257-
node: *const c_char,
258-
service: *const c_char,
259-
hints: *const ADDRINFOA,
260-
res: *mut *mut ADDRINFOA,
261-
) -> c_int {
262-
windows_sys::getaddrinfo(node.cast::<u8>(), service.cast::<u8>(), hints, res)
263-
}
264-
265168
cfg_if::cfg_if! {
266169
if #[cfg(not(target_vendor = "uwp"))] {
267170
pub unsafe fn NtReadFile(

library/std/src/sys/pal/windows/c/bindings.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,7 @@ Windows.Win32.Networking.WinSock.SOCK_RDM
20592059
Windows.Win32.Networking.WinSock.SOCK_SEQPACKET
20602060
Windows.Win32.Networking.WinSock.SOCK_STREAM
20612061
Windows.Win32.Networking.WinSock.SOCKADDR
2062+
Windows.Win32.Networking.WinSock.SOCKADDR_STORAGE
20622063
Windows.Win32.Networking.WinSock.SOCKADDR_UN
20632064
Windows.Win32.Networking.WinSock.SOCKET
20642065
Windows.Win32.Networking.WinSock.SOCKET_ERROR

library/std/src/sys/pal/windows/c/windows_sys.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,14 @@ pub struct SOCKADDR {
28902890
}
28912891
#[repr(C)]
28922892
#[derive(Clone, Copy)]
2893+
pub struct SOCKADDR_STORAGE {
2894+
pub ss_family: ADDRESS_FAMILY,
2895+
pub __ss_pad1: [i8; 6],
2896+
pub __ss_align: i64,
2897+
pub __ss_pad2: [i8; 112],
2898+
}
2899+
#[repr(C)]
2900+
#[derive(Clone, Copy)]
28932901
pub struct SOCKADDR_UN {
28942902
pub sun_family: ADDRESS_FAMILY,
28952903
pub sun_path: [i8; 108],

library/std/src/sys/pal/windows/net.rs

+100-13
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,101 @@ use crate::time::Duration;
1717

1818
use core::ffi::{c_int, c_long, c_ulong, c_ushort};
1919

20+
#[allow(non_camel_case_types)]
2021
pub type wrlen_t = i32;
2122

2223
pub mod netc {
23-
pub use crate::sys::c::ADDRESS_FAMILY as sa_family_t;
24-
pub use crate::sys::c::ADDRINFOA as addrinfo;
25-
pub use crate::sys::c::SOCKADDR as sockaddr;
26-
pub use crate::sys::c::SOCKADDR_STORAGE_LH as sockaddr_storage;
27-
pub use crate::sys::c::*;
24+
//! BSD socket compatibility shim
25+
//!
26+
//! Some Windows API types are not quite what's expected by our cross-platform
27+
//! net code. E.g. naming differences or different pointer types.
28+
29+
use crate::sys::c::{self, ADDRESS_FAMILY, ADDRINFOA, SOCKADDR, SOCKET};
30+
use core::ffi::{c_char, c_int, c_uint, c_ulong, c_ushort, c_void};
31+
32+
// re-exports from Windows API bindings.
33+
pub use crate::sys::c::{
34+
bind, connect, freeaddrinfo, getpeername, getsockname, getsockopt, listen, setsockopt,
35+
ADDRESS_FAMILY as sa_family_t, ADDRINFOA as addrinfo, IPPROTO_IP, IPPROTO_IPV6,
36+
IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_MULTICAST_LOOP, IPV6_V6ONLY,
37+
IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_TTL,
38+
SOCKADDR as sockaddr, SOCKADDR_STORAGE as sockaddr_storage, SOCK_DGRAM, SOCK_STREAM,
39+
SOL_SOCKET, SO_BROADCAST, SO_RCVTIMEO, SO_SNDTIMEO,
40+
};
41+
42+
#[allow(non_camel_case_types)]
43+
pub type socklen_t = c_int;
44+
45+
pub const AF_INET: i32 = c::AF_INET as i32;
46+
pub const AF_INET6: i32 = c::AF_INET6 as i32;
47+
48+
// The following two structs use a union in the generated bindings but
49+
// our cross-platform code expects a normal field so it's redefined here.
50+
// As a consequence, we also need to redefine other structs that use this struct.
51+
#[repr(C)]
52+
#[derive(Copy, Clone)]
53+
pub struct in_addr {
54+
pub s_addr: u32,
55+
}
56+
57+
#[repr(C)]
58+
#[derive(Copy, Clone)]
59+
pub struct in6_addr {
60+
pub s6_addr: [u8; 16],
61+
}
62+
63+
#[repr(C)]
64+
pub struct ip_mreq {
65+
pub imr_multiaddr: in_addr,
66+
pub imr_interface: in_addr,
67+
}
68+
69+
#[repr(C)]
70+
pub struct ipv6_mreq {
71+
pub ipv6mr_multiaddr: in6_addr,
72+
pub ipv6mr_interface: c_uint,
73+
}
74+
75+
#[repr(C)]
76+
#[derive(Copy, Clone)]
77+
pub struct sockaddr_in {
78+
pub sin_family: ADDRESS_FAMILY,
79+
pub sin_port: c_ushort,
80+
pub sin_addr: in_addr,
81+
pub sin_zero: [c_char; 8],
82+
}
83+
84+
#[repr(C)]
85+
#[derive(Copy, Clone)]
86+
pub struct sockaddr_in6 {
87+
pub sin6_family: ADDRESS_FAMILY,
88+
pub sin6_port: c_ushort,
89+
pub sin6_flowinfo: c_ulong,
90+
pub sin6_addr: in6_addr,
91+
pub sin6_scope_id: c_ulong,
92+
}
93+
94+
pub unsafe fn send(socket: SOCKET, buf: *const c_void, len: c_int, flags: c_int) -> c_int {
95+
c::send(socket, buf.cast::<u8>(), len, flags)
96+
}
97+
pub unsafe fn sendto(
98+
socket: SOCKET,
99+
buf: *const c_void,
100+
len: c_int,
101+
flags: c_int,
102+
addr: *const SOCKADDR,
103+
addrlen: c_int,
104+
) -> c_int {
105+
c::sendto(socket, buf.cast::<u8>(), len, flags, addr, addrlen)
106+
}
107+
pub unsafe fn getaddrinfo(
108+
node: *const c_char,
109+
service: *const c_char,
110+
hints: *const ADDRINFOA,
111+
res: *mut *mut ADDRINFOA,
112+
) -> c_int {
113+
c::getaddrinfo(node.cast::<u8>(), service.cast::<u8>(), hints, res)
114+
}
28115
}
29116

30117
pub struct Socket(OwnedSocket);
@@ -102,8 +189,8 @@ where
102189
impl Socket {
103190
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
104191
let family = match *addr {
105-
SocketAddr::V4(..) => c::AF_INET,
106-
SocketAddr::V6(..) => c::AF_INET6,
192+
SocketAddr::V4(..) => netc::AF_INET,
193+
SocketAddr::V6(..) => netc::AF_INET6,
107194
};
108195
let socket = unsafe {
109196
c::WSASocketW(
@@ -157,7 +244,7 @@ impl Socket {
157244
return Err(io::Error::ZERO_TIMEOUT);
158245
}
159246

160-
let mut timeout = c::timeval {
247+
let mut timeout = c::TIMEVAL {
161248
tv_sec: cmp::min(timeout.as_secs(), c_long::MAX as u64) as c_long,
162249
tv_usec: timeout.subsec_micros() as c_long,
163250
};
@@ -167,7 +254,7 @@ impl Socket {
167254
}
168255

169256
let fds = {
170-
let mut fds = unsafe { mem::zeroed::<c::fd_set>() };
257+
let mut fds = unsafe { mem::zeroed::<c::FD_SET>() };
171258
fds.fd_count = 1;
172259
fds.fd_array[0] = self.as_raw();
173260
fds
@@ -295,8 +382,8 @@ impl Socket {
295382
buf: &mut [u8],
296383
flags: c_int,
297384
) -> io::Result<(usize, SocketAddr)> {
298-
let mut storage = unsafe { mem::zeroed::<c::SOCKADDR_STORAGE_LH>() };
299-
let mut addrlen = mem::size_of_val(&storage) as c::socklen_t;
385+
let mut storage = unsafe { mem::zeroed::<c::SOCKADDR_STORAGE>() };
386+
let mut addrlen = mem::size_of_val(&storage) as netc::socklen_t;
300387
let length = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
301388

302389
// On unix when a socket is shut down all further reads return 0, so we
@@ -399,7 +486,7 @@ impl Socket {
399486
}
400487

401488
pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
402-
let linger = c::linger {
489+
let linger = c::LINGER {
403490
l_onoff: linger.is_some() as c_ushort,
404491
l_linger: linger.unwrap_or_default().as_secs() as c_ushort,
405492
};
@@ -408,7 +495,7 @@ impl Socket {
408495
}
409496

410497
pub fn linger(&self) -> io::Result<Option<Duration>> {
411-
let val: c::linger = net::getsockopt(self, c::SOL_SOCKET, c::SO_LINGER)?;
498+
let val: c::LINGER = net::getsockopt(self, c::SOL_SOCKET, c::SO_LINGER)?;
412499

413500
Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
414501
}

0 commit comments

Comments
 (0)