all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v5 1/2] proxmox-async: add udp::connect() helper
Date: Wed,  2 Feb 2022 10:50:10 +0100	[thread overview]
Message-ID: <20220202095019.1799843-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20220202095019.1799843-1-d.csapak@proxmox.com>

so that we do not have to always check the target ipaddr family manually

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 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<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> {
+    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





  reply	other threads:[~2022-02-02  9:50 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-02  9:50 [pbs-devel] [PATCH proxmox/proxmox-backup v5] add metrics server capability Dominik Csapak
2022-02-02  9:50 ` Dominik Csapak [this message]
2022-02-02 12:22   ` [pbs-devel] applied: [PATCH proxmox v5 1/2] proxmox-async: add udp::connect() helper Wolfgang Bumiller
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox v5 2/2] proxmox-metrics: implement metrics server client code Dominik Csapak
2022-02-02 12:25   ` [pbs-devel] applied: " Wolfgang Bumiller
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox-backup v5 1/8] use 'fs_info' from proxmox-sys Dominik Csapak
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox-backup v5 2/8] pbs-api-types: add metrics api types Dominik Csapak
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox-backup v5 3/8] pbs-config: add metrics config class Dominik Csapak
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox-backup v5 4/8] backup-proxy: decouple stats gathering from rrd update Dominik Csapak
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox-backup v5 5/8] proxmox-backup-proxy: send metrics to configured metrics server Dominik Csapak
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox-backup v5 6/8] api: add metricserver endpoints Dominik Csapak
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox-backup v5 7/8] ui: add window/InfluxDbEdit Dominik Csapak
2022-02-02  9:50 ` [pbs-devel] [PATCH proxmox-backup v5 8/8] ui: add MetricServerView and use it Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220202095019.1799843-2-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal