From 40e3ff236df1393e004a7cb0832bab6fd6b06749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Zettlmei=C3=9Fl?= Date: Wed, 24 Jul 2024 12:49:18 +0200 Subject: [PATCH] Fix a lingering panic by using a stricter type The functions `request_ephemeral_peer` and consecutively `new_client` accepted an `IpAddr`, but due to only ever preparing a v4 socket this lead to panic due to an `EAFNOSUPPORT` error if an IPv6 was provided. It would also have made sense to change `new_client` to create either an IPv4 or IPv6 socket depending on the type of the address, but the tuncfg service is currently not accepting IPv6 connections, therefore this was the cleaner change. --- talpid-tunnel-config-client/examples/psk-exchange.rs | 3 +-- talpid-tunnel-config-client/src/lib.rs | 9 +++++---- talpid-wireguard/src/lib.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/talpid-tunnel-config-client/examples/psk-exchange.rs b/talpid-tunnel-config-client/examples/psk-exchange.rs index bbf1e71fc782..aa6223315783 100644 --- a/talpid-tunnel-config-client/examples/psk-exchange.rs +++ b/talpid-tunnel-config-client/examples/psk-exchange.rs @@ -4,7 +4,6 @@ // Usage: ./psk-exchange // e. g. ./psk-exchange 10.64.0.1 NkECLsf+VbZUjve7RVN6sE3NYUcYUmUn8qpFugqbXFk= -use std::net::IpAddr; use talpid_types::net::wireguard::{PrivateKey, PublicKey}; #[tokio::main] @@ -24,7 +23,7 @@ async fn main() { let ephemeral_private_key = PrivateKey::new_from_random(); let ephemeral_peer = talpid_tunnel_config_client::request_ephemeral_peer( - IpAddr::V4(tuncfg_server_ip), + tuncfg_server_ip, public_key, // Parent connection's public key. ephemeral_private_key.public_key(), true, // Whether to negotiate a "PQ-safe" PSK. diff --git a/talpid-tunnel-config-client/src/lib.rs b/talpid-tunnel-config-client/src/lib.rs index 3c6abfd7a244..4172a0023a38 100644 --- a/talpid-tunnel-config-client/src/lib.rs +++ b/talpid-tunnel-config-client/src/lib.rs @@ -1,9 +1,9 @@ use proto::PostQuantumRequestV1; use std::fmt; #[cfg(not(target_os = "ios"))] -use std::net::IpAddr; -#[cfg(not(target_os = "ios"))] use std::net::SocketAddr; +#[cfg(not(target_os = "ios"))] +use std::net::{IpAddr, Ipv4Addr}; use talpid_types::net::wireguard::{PresharedKey, PublicKey}; #[cfg(not(target_os = "ios"))] use tokio::net::TcpSocket; @@ -189,7 +189,7 @@ pub async fn request_ephemeral_peer_with( /// Negotiate a short-lived peer with a PQ-safe PSK or with DAITA enabled. #[cfg(not(target_os = "ios"))] pub async fn request_ephemeral_peer( - service_address: IpAddr, + service_address: Ipv4Addr, parent_pubkey: PublicKey, ephemeral_pubkey: PublicKey, enable_post_quantum: bool, @@ -245,8 +245,9 @@ fn xor_assign(dst: &mut [u8; 32], src: &[u8; 32]) { } #[cfg(not(target_os = "ios"))] -async fn new_client(addr: IpAddr) -> Result { +async fn new_client(addr: Ipv4Addr) -> Result { let endpoint = Endpoint::from_static("tcp://0.0.0.0:0"); + let addr = IpAddr::V4(addr); let conn = endpoint .connect_with_connector(service_fn(move |_| async move { diff --git a/talpid-wireguard/src/lib.rs b/talpid-wireguard/src/lib.rs index 0ab83b326236..a477bea80bcc 100644 --- a/talpid-wireguard/src/lib.rs +++ b/talpid-wireguard/src/lib.rs @@ -728,7 +728,7 @@ impl WireguardMonitor { let ephemeral = tokio::time::timeout( timeout, talpid_tunnel_config_client::request_ephemeral_peer( - IpAddr::from(config.ipv4_gateway), + config.ipv4_gateway, config.tunnel.private_key.public_key(), wg_psk_pubkey, enable_pq,