Skip to content

Commit

Permalink
fixed bug, TFO bind dummy IPv6 address should use SOCKADDR_IN6
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Sep 26, 2021
1 parent 1525a24 commit 94ba553
Showing 1 changed file with 4 additions and 25 deletions.
29 changes: 4 additions & 25 deletions crates/shadowsocks/src/net/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,13 @@ use winapi::{
shared::{
minwindef::{BOOL, DWORD, FALSE, LPDWORD, LPVOID, TRUE},
winerror::ERROR_IO_PENDING,
ws2def::{
ADDRESS_FAMILY,
AF_INET,
AF_INET6,
IPPROTO_TCP,
SIO_GET_EXTENSION_FUNCTION_POINTER,
SOCKADDR,
SOCKADDR_IN,
},
ws2def::{AF_INET, IPPROTO_TCP, SIO_GET_EXTENSION_FUNCTION_POINTER},
},
um::{
minwinbase::{LPOVERLAPPED, OVERLAPPED},
mswsock::{LPFN_CONNECTEX, SIO_UDP_CONNRESET, SO_UPDATE_CONNECT_CONTEXT, WSAID_CONNECTEX},
winnt::PVOID,
winsock2::{
bind,
closesocket,
setsockopt,
socket,
Expand Down Expand Up @@ -163,22 +154,10 @@ impl TcpStream {
}

if opts.bind_local_addr.is_none() {
// Bind to a dummy address (required)
let mut dummy_addr: SOCKADDR_IN = mem::zeroed();
// Bind to a dummy address (required for TFO socket)
match addr.ip() {
IpAddr::V4(..) => dummy_addr.sin_family = AF_INET as ADDRESS_FAMILY,
IpAddr::V6(..) => dummy_addr.sin_family = AF_INET6 as ADDRESS_FAMILY,
}

let ret = bind(
sock,
&dummy_addr as *const _ as *const SOCKADDR,
mem::size_of_val(&dummy_addr) as c_int,
);

if ret == SOCKET_ERROR {
let err = WSAGetLastError();
return Err(io::Error::from_raw_os_error(err));
IpAddr::V4(..) => socket.bind(SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0))?,
IpAddr::V6(..) => socket.bind(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0))?,
}
}
}
Expand Down

0 comments on commit 94ba553

Please sign in to comment.