Skip to content

Commit e383129

Browse files
committed
fix(shadowsocks): IP_UNICAST_IF requires index to be network endianess
ref #1266
1 parent fddf50a commit e383129

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

crates/shadowsocks/Cargo.toml

+2-7
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ stream-cipher = ["shadowsocks-crypto/v1-stream"]
3030
aead-cipher-extra = ["shadowsocks-crypto/v1-aead-extra"]
3131

3232
# Enable AEAD 2022
33-
aead-cipher-2022 = [
34-
"shadowsocks-crypto/v2",
35-
"rand/small_rng",
36-
"aes",
37-
"lru_time_cache",
38-
]
33+
aead-cipher-2022 = ["shadowsocks-crypto/v2", "rand/small_rng", "aes"]
3934
# Enable AEAD 2022 with extra ciphers
4035
aead-cipher-2022-extra = ["aead-cipher-2022", "shadowsocks-crypto/v2-extra"]
4136

@@ -57,7 +52,7 @@ pin-project = "1.1"
5752
bloomfilter = { version = "1.0.8", optional = true }
5853
thiserror = "1.0"
5954
rand = { version = "0.8", optional = true }
60-
lru_time_cache = { version = "0.11", optional = true }
55+
lru_time_cache = "0.11"
6156

6257
serde = { version = "1.0", features = ["derive"] }
6358
serde_urlencoded = "0.7"

crates/shadowsocks/src/net/sys/windows/mod.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use windows_sys::{
4040
IP_ADAPTER_ADDRESSES_LH,
4141
},
4242
Networking::WinSock::{
43+
htonl,
4344
setsockopt,
4445
WSAGetLastError,
4546
WSAIoctl,
@@ -334,20 +335,27 @@ async fn set_ip_unicast_if<S: AsRawSocket>(socket: &S, addr: &SocketAddr, iface:
334335
unsafe {
335336
// https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options
336337
let ret = match addr {
337-
SocketAddr::V4(..) => setsockopt(
338-
handle,
339-
IPPROTO_IP as i32,
340-
IP_UNICAST_IF as i32,
341-
&if_index as *const _ as PCSTR,
342-
mem::size_of_val(&if_index) as i32,
343-
),
344-
SocketAddr::V6(..) => setsockopt(
345-
handle,
346-
IPPROTO_IPV6 as i32,
347-
IPV6_UNICAST_IF as i32,
348-
&if_index as *const _ as PCSTR,
349-
mem::size_of_val(&if_index) as i32,
350-
),
338+
SocketAddr::V4(..) => {
339+
// Interface index is in network byte order for IPPROTO_IP.
340+
let if_index = htonl(if_index);
341+
setsockopt(
342+
handle,
343+
IPPROTO_IP as i32,
344+
IP_UNICAST_IF as i32,
345+
&if_index as *const _ as PCSTR,
346+
mem::size_of_val(&if_index) as i32,
347+
)
348+
}
349+
SocketAddr::V6(..) => {
350+
// Interface index is in host byte order for IPPROTO_IPV6.
351+
setsockopt(
352+
handle,
353+
IPPROTO_IPV6 as i32,
354+
IPV6_UNICAST_IF as i32,
355+
&if_index as *const _ as PCSTR,
356+
mem::size_of_val(&if_index) as i32,
357+
)
358+
}
351359
};
352360

353361
if ret == SOCKET_ERROR {

0 commit comments

Comments
 (0)