diff options
author | Alex Auvolat <alex@adnab.me> | 2022-09-14 15:45:05 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-09-14 15:45:05 +0200 |
commit | 1a413eef977690870dcd5c7bfad50e4fbf62aee7 (patch) | |
tree | 5c6e0c40296107ccd1edb7bcb0e9b67d8f906fa0 | |
parent | 8ac109e3a84bd34550d66baf65fe59b86b63bca2 (diff) | |
download | netapp-1a413eef977690870dcd5c7bfad50e4fbf62aee7.tar.gz netapp-1a413eef977690870dcd5c7bfad50e4fbf62aee7.zip |
Add async version of parse_and_resolve_peer_addrv0.5.1
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/util.rs | 18 |
2 files changed, 18 insertions, 2 deletions
@@ -1,6 +1,6 @@ [package] name = "netapp" -version = "0.5.0" +version = "0.5.1" authors = ["Alex Auvolat <alex@adnab.me>"] 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: /// `<public key hex>@<ip or hostname>:<port>` pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<SocketAddr>)> { + 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<SocketAddr } Some((pubkey, hosts)) } + +/// async version of parse_and_resolve_peer_addr +pub async fn parse_and_resolve_peer_addr_async(peer: &str) -> Option<(NodeID, Vec<SocketAddr>)> { + 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::<Vec<_>>(); + if hosts.is_empty() { + return None; + } + Some((pubkey, hosts)) +} |