From e383129ffee06dfd6c46c0b1d0a9f3a3bb5463fe Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Sat, 12 Aug 2023 22:07:14 +0800 Subject: [PATCH] fix(shadowsocks): IP_UNICAST_IF requires index to be network endianess ref #1266 --- crates/shadowsocks/Cargo.toml | 9 ++--- crates/shadowsocks/src/net/sys/windows/mod.rs | 36 +++++++++++-------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/crates/shadowsocks/Cargo.toml b/crates/shadowsocks/Cargo.toml index fb0d5687..bb70b792 100644 --- a/crates/shadowsocks/Cargo.toml +++ b/crates/shadowsocks/Cargo.toml @@ -30,12 +30,7 @@ stream-cipher = ["shadowsocks-crypto/v1-stream"] aead-cipher-extra = ["shadowsocks-crypto/v1-aead-extra"] # Enable AEAD 2022 -aead-cipher-2022 = [ - "shadowsocks-crypto/v2", - "rand/small_rng", - "aes", - "lru_time_cache", -] +aead-cipher-2022 = ["shadowsocks-crypto/v2", "rand/small_rng", "aes"] # Enable AEAD 2022 with extra ciphers aead-cipher-2022-extra = ["aead-cipher-2022", "shadowsocks-crypto/v2-extra"] @@ -57,7 +52,7 @@ pin-project = "1.1" bloomfilter = { version = "1.0.8", optional = true } thiserror = "1.0" rand = { version = "0.8", optional = true } -lru_time_cache = { version = "0.11", optional = true } +lru_time_cache = "0.11" serde = { version = "1.0", features = ["derive"] } serde_urlencoded = "0.7" diff --git a/crates/shadowsocks/src/net/sys/windows/mod.rs b/crates/shadowsocks/src/net/sys/windows/mod.rs index cad81836..56fd13d1 100644 --- a/crates/shadowsocks/src/net/sys/windows/mod.rs +++ b/crates/shadowsocks/src/net/sys/windows/mod.rs @@ -40,6 +40,7 @@ use windows_sys::{ IP_ADAPTER_ADDRESSES_LH, }, Networking::WinSock::{ + htonl, setsockopt, WSAGetLastError, WSAIoctl, @@ -334,20 +335,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 {