Skip to content

Commit

Permalink
fix(shadowsocks): IP_UNICAST_IF requires index to be network endianess
Browse files Browse the repository at this point in the history
ref #1266
  • Loading branch information
zonyitoo committed Aug 12, 2023
1 parent fddf50a commit b9483e2
Showing 1 changed file with 28 additions and 41 deletions.
69 changes: 28 additions & 41 deletions crates/shadowsocks/src/net/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use std::{
io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket},
},
pin::Pin,
ptr,
slice,
ptr, slice,
task::{self, Poll},
time::{Duration, Instant},
};
Expand All @@ -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,
},
},
Expand All @@ -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)
Expand Down Expand Up @@ -334,20 +314,27 @@ async fn set_ip_unicast_if<S: AsRawSocket>(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 {
Expand Down

0 comments on commit b9483e2

Please sign in to comment.