From bb5b2eafcf0eba9c97968521bfa1612f681ffc93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Wed, 5 Mar 2025 15:02:07 +0100 Subject: [PATCH] Allow IPR reconnect to session (#5562) --- .../src/clients/connected_clients.rs | 35 ++++++++++--------- .../ip-packet-router/src/messages/response.rs | 3 -- .../src/messages/response/v6.rs | 3 -- .../src/messages/response/v7.rs | 3 -- .../src/messages/response/v8.rs | 3 -- .../ip-packet-router/src/mixnet_listener.rs | 5 +-- 6 files changed, 21 insertions(+), 31 deletions(-) diff --git a/service-providers/ip-packet-router/src/clients/connected_clients.rs b/service-providers/ip-packet-router/src/clients/connected_clients.rs index 1957ddaa15..853c83f884 100644 --- a/service-providers/ip-packet-router/src/clients/connected_clients.rs +++ b/service-providers/ip-packet-router/src/clients/connected_clients.rs @@ -59,23 +59,24 @@ impl ConnectedClients { .any(|client| client.client_id == *client_id) } - //fn lookup_ip_from_client_id(&self, client_id: &ConnectedClientId) -> Option { - // self.clients_ipv4_mapping - // .iter() - // .find_map(|(ipv4, connected_client)| { - // if connected_client.client_id == *client_id { - // Some(IpPair::new(*ipv4, connected_client.ipv6)) - // } else { - // None - // } - // }) - //} - // - //fn lookup_client(&self, client_id: &ConnectedClientId) -> Option<&ConnectedClient> { - // self.clients_ipv4_mapping - // .values() - // .find(|connected_client| connected_client.client_id == *client_id) - //} + pub(crate) fn lookup_ip_from_client_id(&self, client_id: &ConnectedClientId) -> Option { + self.clients_ipv4_mapping + .iter() + .find_map(|(ipv4, connected_client)| { + if connected_client.client_id == *client_id { + Some(IpPair::new(*ipv4, connected_client.ipv6)) + } else { + None + } + }) + } + + #[allow(unused)] + fn lookup_client(&self, client_id: &ConnectedClientId) -> Option<&ConnectedClient> { + self.clients_ipv4_mapping + .values() + .find(|connected_client| connected_client.client_id == *client_id) + } pub(crate) fn connect( &mut self, diff --git a/service-providers/ip-packet-router/src/messages/response.rs b/service-providers/ip-packet-router/src/messages/response.rs index 6e60d41433..d1bf0d08b4 100644 --- a/service-providers/ip-packet-router/src/messages/response.rs +++ b/service-providers/ip-packet-router/src/messages/response.rs @@ -90,9 +90,6 @@ pub(crate) struct DynamicConnectSuccess { #[derive(Clone, Debug, thiserror::Error)] pub(crate) enum DynamicConnectFailureReason { - #[error("client already connected")] - ClientAlreadyConnected, - #[error("no available ip address")] NoAvailableIp, diff --git a/service-providers/ip-packet-router/src/messages/response/v6.rs b/service-providers/ip-packet-router/src/messages/response/v6.rs index 290f78971b..bf7fc9dbdf 100644 --- a/service-providers/ip-packet-router/src/messages/response/v6.rs +++ b/service-providers/ip-packet-router/src/messages/response/v6.rs @@ -119,9 +119,6 @@ impl From for DynamicConnectResponseReplyV6 { impl From for DynamicConnectFailureReasonV6 { fn from(reason: DynamicConnectFailureReason) -> Self { match reason { - DynamicConnectFailureReason::ClientAlreadyConnected => { - DynamicConnectFailureReasonV6::RequestedNymAddressAlreadyInUse - } DynamicConnectFailureReason::NoAvailableIp => { DynamicConnectFailureReasonV6::NoAvailableIp } diff --git a/service-providers/ip-packet-router/src/messages/response/v7.rs b/service-providers/ip-packet-router/src/messages/response/v7.rs index 60a3aa6d8a..f2ad4e849f 100644 --- a/service-providers/ip-packet-router/src/messages/response/v7.rs +++ b/service-providers/ip-packet-router/src/messages/response/v7.rs @@ -119,9 +119,6 @@ impl From for DynamicConnectResponseReplyV7 { impl From for DynamicConnectFailureReasonV7 { fn from(reason: DynamicConnectFailureReason) -> Self { match reason { - DynamicConnectFailureReason::ClientAlreadyConnected => { - DynamicConnectFailureReasonV7::RequestedNymAddressAlreadyInUse - } DynamicConnectFailureReason::NoAvailableIp => { DynamicConnectFailureReasonV7::NoAvailableIp } diff --git a/service-providers/ip-packet-router/src/messages/response/v8.rs b/service-providers/ip-packet-router/src/messages/response/v8.rs index 3f73479a05..99b87075c8 100644 --- a/service-providers/ip-packet-router/src/messages/response/v8.rs +++ b/service-providers/ip-packet-router/src/messages/response/v8.rs @@ -82,9 +82,6 @@ impl From for ConnectResponseReplyV8 { impl From for ConnectFailureReasonV8 { fn from(reason: DynamicConnectFailureReason) -> Self { match reason { - DynamicConnectFailureReason::ClientAlreadyConnected => { - ConnectFailureReasonV8::ClientAlreadyConnected - } DynamicConnectFailureReason::NoAvailableIp => ConnectFailureReasonV8::NoAvailableIp, DynamicConnectFailureReason::Other(err) => ConnectFailureReasonV8::Other(err), } diff --git a/service-providers/ip-packet-router/src/mixnet_listener.rs b/service-providers/ip-packet-router/src/mixnet_listener.rs index 293718831c..31d4b2e5fe 100644 --- a/service-providers/ip-packet-router/src/mixnet_listener.rs +++ b/service-providers/ip-packet-router/src/mixnet_listener.rs @@ -242,13 +242,14 @@ impl MixnetListener { .map(Duration::from_millis) .unwrap_or(nym_ip_packet_requests::codec::BUFFER_TIMEOUT); - if self.connected_clients.is_client_connected(&reply_to) { + if let Some(ips) = self.connected_clients.lookup_ip_from_client_id(&reply_to) { + log::debug!("Reconnecting to the previous session"); return Ok(Some(VersionedResponse { version, reply_to, response: Response::DynamicConnect { request_id, - reply: DynamicConnectFailureReason::ClientAlreadyConnected.into(), + reply: DynamicConnectSuccess { ips }.into(), }, })); }