Debian Codegraph @

debian/rust-vendor/mio/src/sys/windows/net.rs

raw
1 use std::io;
2 use std::mem;
3 use std::net::SocketAddr;
4 use std::sync::Once;
5
6 use windows_sys::Win32::Networking::WinSock::{
7 closesocket, ioctlsocket, WSASocketW, AF_INET, AF_INET6, FIONBIO, IN6_ADDR, IN6_ADDR_0,
8 INVALID_SOCKET, IN_ADDR, IN_ADDR_0, SOCKADDR, SOCKADDR_IN, SOCKADDR_IN6, SOCKADDR_IN6_0,
9 SOCKET, WSA_FLAG_NO_HANDLE_INHERIT, WSA_FLAG_OVERLAPPED,
10 };
11
12 /// Initialise the network stack for Windows.
13 fn init() {
14 static INIT: Once = Once::new();
15 INIT.call_once(|| {
16 // Let standard library call `WSAStartup` for us, we can't do it
17 // ourselves because otherwise using any type in `std::net` would panic
18 // when it tries to call `WSAStartup` a second time.
19 drop(std::net::UdpSocket::bind("127.0.0.1:0"));
20 });
21 }
22
23 /// Create a new non-blocking socket.
24 pub(crate) fn new_ip_socket(addr: SocketAddr, socket_type: i32) -> io::Result<SOCKET> {
25 let domain = match addr {
26 SocketAddr::V4(..) => AF_INET,
27 SocketAddr::V6(..) => AF_INET6,
28 };
29
30 new_socket(domain.into(), socket_type)
31 }
32
33 pub(crate) fn new_socket(domain: u32, socket_type: i32) -> io::Result<SOCKET> {
34 init();
35
36 let flags = WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT;
37 let socket = syscall!(
38 WSASocketW(domain as i32, socket_type, 0, std::ptr::null(), 0, flags),
39 PartialEq::eq,
40 INVALID_SOCKET
41 )?;
42
43 if let Err(err) = syscall!(ioctlsocket(socket, FIONBIO, &mut 1), PartialEq::ne, 0) {
44 let _ = unsafe { closesocket(socket) };
45 return Err(err);
46 }
47
48 Ok(socket as SOCKET)
49 }
50
51 /// A type with the same memory layout as `SOCKADDR`. Used in converting Rust level
52 /// SocketAddr* types into their system representation. The benefit of this specific
53 /// type over using `SOCKADDR_STORAGE` is that this type is exactly as large as it
54 /// needs to be and not a lot larger. And it can be initialized cleaner from Rust.
55 #[repr(C)]
56 pub(crate) union SocketAddrCRepr {
57 v4: SOCKADDR_IN,
58 v6: SOCKADDR_IN6,
59 }
60
61 impl SocketAddrCRepr {
62 pub(crate) fn as_ptr(&self) -> *const SOCKADDR {
63 self as *const _ as *const SOCKADDR
64 }
65 }
66
67 pub(crate) fn socket_addr(addr: &SocketAddr) -> (SocketAddrCRepr, i32) {
68 match addr {
69 SocketAddr::V4(ref addr) => {
70 // `s_addr` is stored as BE on all machine and the array is in BE order.
71 // So the native endian conversion method is used so that it's never swapped.
72 let sin_addr = unsafe {
73 let mut s_un = mem::zeroed::<IN_ADDR_0>();
74 s_un.S_addr = u32::from_ne_bytes(addr.ip().octets());
75 IN_ADDR { S_un: s_un }
76 };
77
78 let sockaddr_in = SOCKADDR_IN {
79 sin_family: AF_INET as u16, // 1
80 sin_port: addr.port().to_be(),
81 sin_addr,
82 sin_zero: [0; 8],
83 };
84
85 let sockaddr = SocketAddrCRepr { v4: sockaddr_in };
86 (sockaddr, mem::size_of::<SOCKADDR_IN>() as i32)
87 }
88 SocketAddr::V6(ref addr) => {
89 let sin6_addr = unsafe {
90 let mut u = mem::zeroed::<IN6_ADDR_0>();
91 u.Byte = addr.ip().octets();
92 IN6_ADDR { u }
93 };
94 let u = unsafe {
95 let mut u = mem::zeroed::<SOCKADDR_IN6_0>();
96 u.sin6_scope_id = addr.scope_id();
97 u
98 };
99
100 let sockaddr_in6 = SOCKADDR_IN6 {
101 sin6_family: AF_INET6 as u16, // 23
102 sin6_port: addr.port().to_be(),
103 sin6_addr,
104 sin6_flowinfo: addr.flowinfo(),
105 Anonymous: u,
106 };
107
108 let sockaddr = SocketAddrCRepr { v6: sockaddr_in6 };
109 (sockaddr, mem::size_of::<SOCKADDR_IN6>() as i32)
110 }
111 }
112 }