Skip to content

Commit

Permalink
Replace unsafe code with socket2
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Sep 24, 2024
1 parent cfd2319 commit 4502ba7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion talpid-tunnel-config-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pqc_kyber = { version = "0.4.0", features = ["std", "kyber1024", "zeroize"] }
zeroize = "1.5.7"

[target.'cfg(unix)'.dependencies]
libc = "0.2"
socket2 = { version = "0.5.3", features = ["all"] }

[target.'cfg(windows)'.dependencies.windows-sys]
workspace = true
Expand Down
39 changes: 13 additions & 26 deletions talpid-tunnel-config-client/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@

use std::io;
use std::net::SocketAddr;
use tokio::net::TcpSocket as StdTcpSocket;
use tokio::net::TcpSocket as TokioTcpSocket;
use tokio::net::TcpStream;

#[cfg(unix)]
mod sys {
use super::*;

pub use libc::{setsockopt, socklen_t, IPPROTO_TCP, TCP_MAXSEG};
pub use std::os::fd::{AsRawFd, RawFd};
use socket2::{Domain, Protocol, Socket, Type};

/// MTU to set on the tunnel config client socket. We want a low value to prevent fragmentation.
/// Especially on Android, we've found that the real MTU is often lower than the default MTU, and
Expand All @@ -21,39 +19,28 @@ mod sys {
const CONFIG_CLIENT_MTU: u16 = 576;

pub struct TcpSocket {
socket: StdTcpSocket,
socket: Socket,
}

impl TcpSocket {
pub fn new() -> io::Result<Self> {
let socket = StdTcpSocket::new_v4()?;
try_set_tcp_sock_mtu(socket.as_raw_fd());
let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?;
try_set_tcp_sock_mtu(&socket);
socket.set_nonblocking(true)?;
Ok(Self { socket })
}

pub async fn connect(self, addr: SocketAddr) -> io::Result<TcpStream> {
self.socket.connect(addr).await
let socket = TokioTcpSocket::from_std_stream(self.socket.into());
socket.connect(addr).await
}
}

fn try_set_tcp_sock_mtu(sock: RawFd) {
fn try_set_tcp_sock_mtu(sock: &Socket) {
let mss = desired_mss();
log::debug!("Tunnel config TCP socket MSS: {mss}");

let result = unsafe {
setsockopt(
sock,
IPPROTO_TCP,
TCP_MAXSEG,
&mss as *const _ as _,
socklen_t::try_from(std::mem::size_of_val(&mss)).unwrap(),
)
};
if result != 0 {
log::error!(
"Failed to set MSS on tunnel config TCP socket: {}",
std::io::Error::last_os_error()
);
if let Err(error) = sock.set_mss(mss) {
log::error!("Failed to set MSS on tunnel config TCP socket: {}", error);
}
}

Expand All @@ -70,13 +57,13 @@ mod sys {
use super::*;

pub struct TcpSocket {
socket: StdTcpSocket,
socket: TokioTcpSocket,
}

impl TcpSocket {
pub fn new() -> io::Result<Self> {
Ok(Self {
socket: StdTcpSocket::new_v4()?,
socket: TokioTcpSocket::new_v4()?,
})
}

Expand Down

0 comments on commit 4502ba7

Please sign in to comment.