Skip to content

Commit 768e988

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

File tree

1 file changed

+22
-14
lines changed
  • crates/shadowsocks/src/net/sys/windows

1 file changed

+22
-14
lines changed

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)