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 e383129
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
9 changes: 2 additions & 7 deletions crates/shadowsocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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"
Expand Down
36 changes: 22 additions & 14 deletions crates/shadowsocks/src/net/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use windows_sys::{
IP_ADAPTER_ADDRESSES_LH,
},
Networking::WinSock::{
htonl,
setsockopt,
WSAGetLastError,
WSAIoctl,
Expand Down Expand Up @@ -334,20 +335,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 e383129

Please sign in to comment.