From 54ba468c47db5f01f98f77d47328bfca0201bace Mon Sep 17 00:00:00 2001 From: fi3 Date: Tue, 7 May 2024 11:09:26 +0200 Subject: [PATCH] Fix network helper panic if peer close connection When the remote peer try to open an encrypted sv2 connection and then immediatly close the tcp connection, the helper for async std and tokio it panic, mostly of the time we don't want to panic in this situation. This commit add an error for this case and return it. --- roles/roles-utils/network-helpers/src/lib.rs | 2 ++ .../network-helpers/src/noise_connection_async_std.rs | 2 +- roles/roles-utils/network-helpers/src/noise_connection_tokio.rs | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/roles/roles-utils/network-helpers/src/lib.rs b/roles/roles-utils/network-helpers/src/lib.rs index 2879e3ad6..3555c2056 100644 --- a/roles/roles-utils/network-helpers/src/lib.rs +++ b/roles/roles-utils/network-helpers/src/lib.rs @@ -30,6 +30,8 @@ pub enum Error { CodecError(CodecError), RecvError, SendError, + // This means that a socket that was supposed to be opened have been closed, likley by the peer + SocketClosed, } impl From for Error { diff --git a/roles/roles-utils/network-helpers/src/noise_connection_async_std.rs b/roles/roles-utils/network-helpers/src/noise_connection_async_std.rs index 0bfad7e9d..ae4052903 100644 --- a/roles/roles-utils/network-helpers/src/noise_connection_async_std.rs +++ b/roles/roles-utils/network-helpers/src/noise_connection_async_std.rs @@ -47,7 +47,7 @@ impl Connection { ), Error, > { - let address = stream.peer_addr().unwrap(); + let address = stream.peer_addr().map_err(|_| Error::SocketClosed)?; let (mut reader, writer) = (stream.clone(), stream.clone()); let (sender_incoming, receiver_incoming): ( diff --git a/roles/roles-utils/network-helpers/src/noise_connection_tokio.rs b/roles/roles-utils/network-helpers/src/noise_connection_tokio.rs index 06a4961be..2e2996365 100644 --- a/roles/roles-utils/network-helpers/src/noise_connection_tokio.rs +++ b/roles/roles-utils/network-helpers/src/noise_connection_tokio.rs @@ -48,7 +48,7 @@ impl Connection { ), Error, > { - let address = stream.peer_addr().unwrap(); + let address = stream.peer_addr().map_err(|_| Error::SocketClosed)?; let (mut reader, mut writer) = stream.into_split();