@@ -40,6 +40,7 @@ use windows_sys::{
40
40
IP_ADAPTER_ADDRESSES_LH ,
41
41
} ,
42
42
Networking :: WinSock :: {
43
+ htonl,
43
44
setsockopt,
44
45
WSAGetLastError ,
45
46
WSAIoctl ,
@@ -334,20 +335,27 @@ async fn set_ip_unicast_if<S: AsRawSocket>(socket: &S, addr: &SocketAddr, iface:
334
335
unsafe {
335
336
// https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options
336
337
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
+ }
351
359
} ;
352
360
353
361
if ret == SOCKET_ERROR {
0 commit comments