From fa21a30ac2e157510e4eb1a24992951d23db854d Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Mon, 9 Sep 2024 22:56:05 +0800 Subject: [PATCH] fix(shadowsocks): check mptcp socket() return value --- crates/shadowsocks/src/net/sys/unix/bsd/macos.rs | 4 ++++ crates/shadowsocks/src/net/sys/unix/linux/mod.rs | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/crates/shadowsocks/src/net/sys/unix/bsd/macos.rs b/crates/shadowsocks/src/net/sys/unix/bsd/macos.rs index 2c27417afc95..de3717ee0a44 100644 --- a/crates/shadowsocks/src/net/sys/unix/bsd/macos.rs +++ b/crates/shadowsocks/src/net/sys/unix/bsd/macos.rs @@ -54,6 +54,10 @@ impl TcpStream { let socket = unsafe { let fd = libc::socket(AF_MULTIPATH, libc::SOCK_STREAM, libc::IPPROTO_TCP); + if fd < 0 { + let err = io::Error::last_os_error(); + return Err(err); + } let socket = Socket::from_raw_fd(fd); socket.set_nonblocking(true)?; TcpSocket::from_raw_fd(socket.into_raw_fd()) diff --git a/crates/shadowsocks/src/net/sys/unix/linux/mod.rs b/crates/shadowsocks/src/net/sys/unix/linux/mod.rs index 1122f041d128..f007255a0f67 100644 --- a/crates/shadowsocks/src/net/sys/unix/linux/mod.rs +++ b/crates/shadowsocks/src/net/sys/unix/linux/mod.rs @@ -213,12 +213,18 @@ pub fn set_tcp_fastopen(socket: &S) -> io::Result<()> { } fn create_mptcp_socket(bind_addr: &SocketAddr) -> io::Result { + // https://www.kernel.org/doc/html/next/networking/mptcp.html + unsafe { let family = match bind_addr { SocketAddr::V4(..) => libc::AF_INET, SocketAddr::V6(..) => libc::AF_INET6, }; let fd = libc::socket(family, libc::SOCK_STREAM, libc::IPPROTO_MPTCP); + if fd < 0 { + let err = io::Error::last_os_error(); + return Err(err); + } let socket = Socket::from_raw_fd(fd); socket.set_nonblocking(true)?; Ok(TcpSocket::from_raw_fd(socket.into_raw_fd()))