diff --git a/crates/shadowsocks/src/net/sys/windows/mod.rs b/crates/shadowsocks/src/net/sys/windows/mod.rs index cad81836aac3..346f4c95b3a5 100644 --- a/crates/shadowsocks/src/net/sys/windows/mod.rs +++ b/crates/shadowsocks/src/net/sys/windows/mod.rs @@ -8,8 +8,7 @@ use std::{ io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket}, }, pin::Pin, - ptr, - slice, + ptr, slice, task::{self, Poll}, time::{Duration, Instant}, }; @@ -26,35 +25,18 @@ use tokio::{ sync::Mutex, }; use tokio_tfo::TfoStream; +use windows_sys::Win32::Networking::WinSock::htonl; use windows_sys::{ core::PCSTR, Win32::{ Foundation::{BOOL, ERROR_BUFFER_OVERFLOW, ERROR_NO_DATA, ERROR_SUCCESS}, NetworkManagement::IpHelper::{ - if_nametoindex, - GetAdaptersAddresses, - GAA_FLAG_SKIP_ANYCAST, - GAA_FLAG_SKIP_DNS_SERVER, - GAA_FLAG_SKIP_MULTICAST, - GAA_FLAG_SKIP_UNICAST, - IP_ADAPTER_ADDRESSES_LH, + if_nametoindex, GetAdaptersAddresses, GAA_FLAG_SKIP_ANYCAST, GAA_FLAG_SKIP_DNS_SERVER, + GAA_FLAG_SKIP_MULTICAST, GAA_FLAG_SKIP_UNICAST, IP_ADAPTER_ADDRESSES_LH, }, Networking::WinSock::{ - setsockopt, - WSAGetLastError, - WSAIoctl, - AF_UNSPEC, - IPPROTO_IP, - IPPROTO_IPV6, - IPPROTO_TCP, - IPV6_MTU_DISCOVER, - IPV6_UNICAST_IF, - IP_MTU_DISCOVER, - IP_PMTUDISC_DO, - IP_UNICAST_IF, - SIO_UDP_CONNRESET, - SOCKET, - SOCKET_ERROR, + setsockopt, WSAGetLastError, WSAIoctl, AF_UNSPEC, IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP, IPV6_MTU_DISCOVER, + IPV6_UNICAST_IF, IP_MTU_DISCOVER, IP_PMTUDISC_DO, IP_UNICAST_IF, SIO_UDP_CONNRESET, SOCKET, SOCKET_ERROR, TCP_FASTOPEN, }, }, @@ -66,9 +48,7 @@ const FALSE: BOOL = 0; use crate::net::{ is_dual_stack_addr, sys::{set_common_sockopt_for_connect, socket_bind_dual_stack}, - AcceptOpts, - AddrFamily, - ConnectOpts, + AcceptOpts, AddrFamily, ConnectOpts, }; /// A `TcpStream` that supports TFO (TCP Fast Open) @@ -334,20 +314,27 @@ async fn set_ip_unicast_if(socket: &S, addr: &SocketAddr, iface: unsafe { // https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options let ret = match addr { - SocketAddr::V4(..) => setsockopt( - handle, - IPPROTO_IP as i32, - IP_UNICAST_IF as i32, - &if_index as *const _ as PCSTR, - mem::size_of_val(&if_index) as i32, - ), - SocketAddr::V6(..) => setsockopt( - handle, - IPPROTO_IPV6 as i32, - IPV6_UNICAST_IF as i32, - &if_index as *const _ as PCSTR, - mem::size_of_val(&if_index) as i32, - ), + SocketAddr::V4(..) => { + // Interface index is in network byte order for IPPROTO_IP. + let if_index = htonl(if_index); + setsockopt( + handle, + IPPROTO_IP as i32, + IP_UNICAST_IF as i32, + &if_index as *const _ as PCSTR, + mem::size_of_val(&if_index) as i32, + ) + } + SocketAddr::V6(..) => { + // Interface index is in host byte order for IPPROTO_IPV6. + setsockopt( + handle, + IPPROTO_IPV6 as i32, + IPV6_UNICAST_IF as i32, + &if_index as *const _ as PCSTR, + mem::size_of_val(&if_index) as i32, + ) + } }; if ret == SOCKET_ERROR {