mirror of
https://github.com/shadowsocks/shadowsocks-rust.git
synced 2026-11-17 08:16:12 +00:00
[#26] Switched to socket2 for much simpler solution
This commit is contained in:
@@ -73,6 +73,7 @@ lru_time_cache = "0.9"
|
||||
hyper = "0.13"
|
||||
tower = "0.3"
|
||||
pin-project = "0.4"
|
||||
socket2 = "0.3"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = { version = "0.3", features = ["mswsock", "winsock2"] }
|
||||
|
||||
+34
-52
@@ -1,62 +1,44 @@
|
||||
//! Utility functions
|
||||
|
||||
use std::{
|
||||
io,
|
||||
net::{SocketAddr, TcpStream as StdTcpStream},
|
||||
};
|
||||
use std::{io, net::SocketAddr};
|
||||
|
||||
use tokio::net::TcpStream as TokioTcpStream;
|
||||
#[cfg(windows)]
|
||||
use winapi::um::winsock2::SOCKET;
|
||||
use socket2::{Domain, SockAddr, Socket, Type};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use crate::relay::utils::{bind_socket, close_socket, create_socket_nonblock};
|
||||
/// Connecting to a specific target with TCP protocol
|
||||
///
|
||||
/// Optionally we can bind to a local address for connecting
|
||||
pub async fn connect_tcp_stream(addr: &SocketAddr, outbound_addr: &Option<SocketAddr>) -> io::Result<TcpStream> {
|
||||
match *outbound_addr {
|
||||
None => {
|
||||
// Connect with tokio's default API directly
|
||||
TcpStream::connect(addr).await
|
||||
}
|
||||
Some(ref bind_addr) => {
|
||||
// Create TcpStream manually from socket
|
||||
// These functions may not behave exactly the same as tokio's TcpStream::connect
|
||||
|
||||
#[cfg(unix)]
|
||||
fn new_socket(addr: &SocketAddr) -> io::Result<libc::c_int> {
|
||||
let domain = match *addr {
|
||||
SocketAddr::V4(..) => libc::AF_INET,
|
||||
SocketAddr::V6(..) => libc::AF_INET6,
|
||||
};
|
||||
let socket = match *addr {
|
||||
SocketAddr::V4(..) => Socket::new(Domain::ipv4(), Type::stream(), None)?,
|
||||
SocketAddr::V6(..) => Socket::new(Domain::ipv6(), Type::stream(), None)?,
|
||||
};
|
||||
|
||||
create_socket_nonblock(domain, libc::SOCK_STREAM)
|
||||
}
|
||||
// Bind to local outbound address
|
||||
//
|
||||
// Common failure: EADDRINUSE
|
||||
let bind_addr = SockAddr::from(*bind_addr);
|
||||
socket.bind(&bind_addr)?;
|
||||
|
||||
#[cfg(windows)]
|
||||
fn new_socket(addr: &SocketAddr) -> io::Result<SOCKET> {
|
||||
use winapi::um::winsock2::{PF_INET, PF_INET6, SOCK_STREAM};
|
||||
|
||||
let af = match *addr {
|
||||
SocketAddr::V4(..) => PF_INET,
|
||||
SocketAddr::V6(..) => PF_INET6,
|
||||
};
|
||||
|
||||
create_socket_nonblock(af, SOCK_STREAM)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn create_tcp_stream(socket: libc::c_int) -> StdTcpStream {
|
||||
use std::os::unix::io::FromRawFd;
|
||||
unsafe { StdTcpStream::from_raw_fd(socket) }
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn create_tcp_stream(socket: SOCKET) -> StdTcpStream {
|
||||
use std::os::windows::io::{FromRawSocket, RawSocket};
|
||||
unsafe { StdTcpStream::from_raw_socket(socket as RawSocket) }
|
||||
}
|
||||
|
||||
pub async fn connect_tcp_stream(addr: &SocketAddr, outbound_addr: &Option<SocketAddr>) -> io::Result<TokioTcpStream> {
|
||||
let socket = new_socket(addr)?;
|
||||
|
||||
if let Some(ref bind_addr) = outbound_addr {
|
||||
if let Err(err) = bind_socket(socket, bind_addr) {
|
||||
let _ = close_socket(socket);
|
||||
return Err(err);
|
||||
// Connect to the target
|
||||
//
|
||||
// FIXME: This function is not documented as it may be deleted in the future
|
||||
//
|
||||
// mio 0.6.x (tokio 0.2.x is depending on it) will set stream into non-block mode
|
||||
// unix: https://github.com/tokio-rs/mio/blob/v0.6.x/src/sys/unix/tcp.rs#L28
|
||||
// windows: https://github.com/tokio-rs/mio/blob/v0.6.x/src/sys/windows/tcp.rs#L118
|
||||
//
|
||||
// We have to let tokio calls connect for us. Because we don't have a chance to wait until the socket is actually connected
|
||||
TcpStream::connect_std(socket.into_tcp_stream(), addr).await
|
||||
}
|
||||
}
|
||||
|
||||
let stream = create_tcp_stream(socket);
|
||||
|
||||
// FIXME: connect_std is marked as not recommended
|
||||
TokioTcpStream::connect_std(stream, addr).await
|
||||
}
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
use std::{
|
||||
future::Future,
|
||||
io::{self, Error},
|
||||
mem,
|
||||
net::SocketAddr,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use tokio::time;
|
||||
#[cfg(windows)]
|
||||
use winapi::{
|
||||
ctypes,
|
||||
shared::ws2def::SOCKADDR,
|
||||
um::winsock2::{bind, closesocket, ioctlsocket, socket, WSAGetLastError, FIONBIO, INVALID_SOCKET, SOCKET},
|
||||
};
|
||||
|
||||
pub async fn try_timeout<T, E, F>(fut: F, timeout: Option<Duration>) -> io::Result<T>
|
||||
where
|
||||
@@ -25,137 +17,3 @@ where
|
||||
}
|
||||
.map_err(From::from)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub fn addr2raw(addr: &SocketAddr) -> (*const libc::sockaddr, libc::socklen_t) {
|
||||
match *addr {
|
||||
SocketAddr::V4(ref a) => (a as *const _ as *const _, mem::size_of_val(a) as libc::socklen_t),
|
||||
SocketAddr::V6(ref a) => (a as *const _ as *const _, mem::size_of_val(a) as libc::socklen_t),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn addr2raw(addr: &SocketAddr) -> (*const SOCKADDR, ctypes::c_int) {
|
||||
match *addr {
|
||||
SocketAddr::V4(ref a) => (a as *const _ as *const _, mem::size_of_val(a) as ctypes::c_int),
|
||||
SocketAddr::V6(ref a) => (a as *const _ as *const _, mem::size_of_val(a) as ctypes::c_int),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new socket with O_NONBLOCK flag set
|
||||
///
|
||||
/// Borrowed from mio: src/sys/unix/net.rs
|
||||
#[cfg(unix)]
|
||||
pub fn create_socket_nonblock(domain: libc::c_int, socket_type: libc::c_int) -> io::Result<libc::c_int> {
|
||||
unsafe {
|
||||
#[cfg(any(
|
||||
target_os = "android",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "linux",
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd"
|
||||
))]
|
||||
let socket_type = socket_type | libc::SOCK_NONBLOCK | libc::SOCK_CLOEXEC;
|
||||
|
||||
// Gives a warning for platforms without SOCK_NONBLOCK.
|
||||
#[allow(clippy::let_and_return)]
|
||||
let socket = libc::socket(domain, socket_type, 0);
|
||||
if socket < 0 {
|
||||
return Err(Error::last_os_error());
|
||||
}
|
||||
// Darwin doesn't have SOCK_NONBLOCK or SOCK_CLOEXEC. Not sure about Solaris, couldn't find anything online.
|
||||
if cfg!(any(target_os = "ios", target_os = "macos", target_os = "solaris")) {
|
||||
// For platforms that don't support flags in socket, we need to set the flags ourselves.
|
||||
let ret = libc::fcntl(
|
||||
socket,
|
||||
libc::F_SETFL,
|
||||
libc::fcntl(socket, libc::F_GETFL) | libc::O_NONBLOCK,
|
||||
);
|
||||
|
||||
if ret == -1 {
|
||||
let err = Error::last_os_error();
|
||||
libc::close(socket);
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
let ret = libc::fcntl(
|
||||
socket,
|
||||
libc::F_SETFD,
|
||||
libc::fcntl(socket, libc::F_GETFD) | libc::FD_CLOEXEC,
|
||||
);
|
||||
|
||||
if ret == -1 {
|
||||
let err = Error::last_os_error();
|
||||
libc::close(socket);
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(socket)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new socket with FIONBIO flag set
|
||||
///
|
||||
/// Borrowed from mio: src/sys/windows/net.rs
|
||||
#[cfg(windows)]
|
||||
pub fn create_socket_nonblock(address_family: ctypes::c_int, socket_type: ctypes::c_int) -> io::Result<SOCKET> {
|
||||
let handle = socket(address_family, socket_type);
|
||||
if handle == INVALID_SOCKET {
|
||||
return Err(Error::from_raw_os_error(WSAGetLastError()));
|
||||
}
|
||||
|
||||
let ret = ioctlsocket(handle, FIONBIO, &mut 1);
|
||||
if ret != 0 {
|
||||
return Err(Error::from_raw_os_error(WSAGetLastError()));
|
||||
}
|
||||
|
||||
Ok(handle)
|
||||
}
|
||||
|
||||
/// Bind a socket with addr
|
||||
#[cfg(unix)]
|
||||
pub fn bind_socket(socket: libc::c_int, addr: &SocketAddr) -> io::Result<()> {
|
||||
unsafe {
|
||||
let (saddr, saddr_len) = addr2raw(addr);
|
||||
if libc::bind(socket, saddr, saddr_len) != 0 {
|
||||
return Err(Error::last_os_error());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Bind a socket with addr
|
||||
#[cfg(windows)]
|
||||
pub fn bind_socket(socket: SOCKET, addr: &SocketAddr) -> io::Result<()> {
|
||||
unsafe {
|
||||
let (saddr, saddr_len) = addr2raw(addr);
|
||||
if bind(socket, saddr, saddr_len) != 0 {
|
||||
return Err(Error::from_raw_os_error(WSAGetLastError()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub fn close_socket(socket: libc::c_int) -> io::Result<()> {
|
||||
unsafe {
|
||||
if libc::close(socket) != 0 {
|
||||
Err(Error::last_os_error())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn close_socket(socket: SOCKET) -> io::Result<()> {
|
||||
unsafe {
|
||||
if closesocket(socket) != 0 {
|
||||
Err(Error::from_raw_os_error(WSAGetLastError()))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user