From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 2C13761626 for ; Mon, 17 Jan 2022 11:48:28 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1BD1918B7E for ; Mon, 17 Jan 2022 11:48:28 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id F313B18B69 for ; Mon, 17 Jan 2022 11:48:26 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id CB721460C3 for ; Mon, 17 Jan 2022 11:48:26 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Mon, 17 Jan 2022 11:48:18 +0100 Message-Id: <20220117104825.2409598-4-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220117104825.2409598-1-d.csapak@proxmox.com> References: <20220117104825.2409598-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.163 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox v4 3/4] proxmox-async: add connect_to_udp helper X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Jan 2022 10:48:28 -0000 so that we do not have to always check the target ipaddr family manually Signed-off-by: Dominik Csapak --- proxmox-async/Cargo.toml | 2 +- proxmox-async/src/io/mod.rs | 3 +++ proxmox-async/src/io/udp_connect.rs | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 proxmox-async/src/io/udp_connect.rs diff --git a/proxmox-async/Cargo.toml b/proxmox-async/Cargo.toml index f9a5b01..9756a23 100644 --- a/proxmox-async/Cargo.toml +++ b/proxmox-async/Cargo.toml @@ -17,7 +17,7 @@ flate2 = "1.0" futures = "0.3" lazy_static = "1.4" pin-utils = "0.1.0" -tokio = { version = "1.0", features = ["fs", "rt", "rt-multi-thread", "sync"] } +tokio = { version = "1.0", features = ["fs", "net", "rt", "rt-multi-thread", "sync"] } walkdir = "2" proxmox-sys = { path = "../proxmox-sys", version = "0.2.0" } diff --git a/proxmox-async/src/io/mod.rs b/proxmox-async/src/io/mod.rs index 9a6d8a6..2dd49d4 100644 --- a/proxmox-async/src/io/mod.rs +++ b/proxmox-async/src/io/mod.rs @@ -2,3 +2,6 @@ mod async_channel_writer; pub use async_channel_writer::AsyncChannelWriter; + +mod udp_connect; +pub use udp_connect::connect_to_udp; diff --git a/proxmox-async/src/io/udp_connect.rs b/proxmox-async/src/io/udp_connect.rs new file mode 100644 index 0000000..878b150 --- /dev/null +++ b/proxmox-async/src/io/udp_connect.rs @@ -0,0 +1,18 @@ +use std::io; +use std::net::SocketAddr; + +use tokio::net::{ToSocketAddrs, UdpSocket}; + +/// Helper to connect to UDP addresses without having to manually bind to the correct ip address +pub async fn connect_to_udp( + addr: A, +) -> io::Result { + let socket = match tokio::net::lookup_host(&addr).await?.next() { + Some(SocketAddr::V4(_)) => UdpSocket::bind("0.0.0.0:0").await?, + Some(SocketAddr::V6(_)) => UdpSocket::bind("[::]:0").await?, + None => proxmox_sys::io_bail!("could not resolve address family {}", addr), + }; + + socket.connect(addr).await?; + Ok(socket) +} -- 2.30.2