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 677A5607D7 for ; Wed, 2 Feb 2022 13:23:27 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 54FCB1BB82 for ; Wed, 2 Feb 2022 13:22:57 +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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id D9F7F1BB77 for ; Wed, 2 Feb 2022 13:22:55 +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 A0675461E9 for ; Wed, 2 Feb 2022 13:22:55 +0100 (CET) Date: Wed, 2 Feb 2022 13:22:54 +0100 From: Wolfgang Bumiller To: Dominik Csapak Cc: pbs-devel@lists.proxmox.com Message-ID: <20220202122254.bobwyd5aqc3juayl@olga.proxmox.com> References: <20220202095019.1799843-1-d.csapak@proxmox.com> <20220202095019.1799843-2-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220202095019.1799843-2-d.csapak@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.400 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] applied: [PATCH proxmox v5 1/2] proxmox-async: add udp::connect() 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: Wed, 02 Feb 2022 12:23:27 -0000 applied but moved from `io::udp` to `net::udp`. On Wed, Feb 02, 2022 at 10:50:10AM +0100, Dominik Csapak wrote: > 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 | 2 ++ > proxmox-async/src/io/udp.rs | 36 ++++++++++++++++++++++++++++++++++++ > 3 files changed, 39 insertions(+), 1 deletion(-) > create mode 100644 proxmox-async/src/io/udp.rs > > diff --git a/proxmox-async/Cargo.toml b/proxmox-async/Cargo.toml > index 9e38303..c1a41f1 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..32081cf 100644 > --- a/proxmox-async/src/io/mod.rs > +++ b/proxmox-async/src/io/mod.rs > @@ -2,3 +2,5 @@ > > mod async_channel_writer; > pub use async_channel_writer::AsyncChannelWriter; > + > +pub mod udp; > diff --git a/proxmox-async/src/io/udp.rs b/proxmox-async/src/io/udp.rs > new file mode 100644 > index 0000000..a517869 > --- /dev/null > +++ b/proxmox-async/src/io/udp.rs > @@ -0,0 +1,36 @@ > +use std::io; > +use std::net::{Ipv4Addr, Ipv6Addr, 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(addr: A) -> io::Result { > + let mut last_err = None; > + for address in tokio::net::lookup_host(&addr).await? { > + let bind_address = match address { > + SocketAddr::V4(_) => SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0), > + SocketAddr::V6(_) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0), > + }; > + let socket = match UdpSocket::bind(bind_address).await { > + Ok(sock) => sock, > + Err(err) => { > + last_err = Some(err); > + continue; > + } > + }; > + match socket.connect(address).await { > + Ok(()) => return Ok(socket), > + Err(err) => { > + last_err = Some(err); > + continue; > + } > + } > + } > + > + Err(last_err.unwrap_or_else(|| { > + io::Error::new( > + io::ErrorKind::InvalidInput, > + "could not resolve to any addresses", > + ) > + })) > +} > -- > 2.30.2