From 1a413eef977690870dcd5c7bfad50e4fbf62aee7 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 14 Sep 2022 15:45:05 +0200 Subject: Add async version of parse_and_resolve_peer_addr --- Cargo.toml | 2 +- src/util.rs | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4dce280..65f6c6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "netapp" -version = "0.5.0" +version = "0.5.1" authors = ["Alex Auvolat "] edition = "2018" license-file = "LICENSE" diff --git a/src/util.rs b/src/util.rs index 425d26f..4676e64 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,4 @@ use std::net::SocketAddr; -use std::net::ToSocketAddrs; use log::info; use serde::Serialize; @@ -71,6 +70,8 @@ pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> { /// Parse and resolve a peer's address including public key, written in the format: /// `@:` pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec)> { + use std::net::ToSocketAddrs; + let delim = peer.find('@')?; let (key, host) = peer.split_at(delim); let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?; @@ -80,3 +81,18 @@ pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec Option<(NodeID, Vec)> { + let delim = peer.find('@')?; + let (key, host) = peer.split_at(delim); + let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?; + let hosts = tokio::net::lookup_host(&host[1..]) + .await + .ok()? + .collect::>(); + if hosts.is_empty() { + return None; + } + Some((pubkey, hosts)) +} -- cgit v1.2.3