diff options
author | Alex Auvolat <alex@adnab.me> | 2021-10-18 12:39:19 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2021-10-18 12:39:19 +0200 |
commit | 238c0162c0036f767dbf5672f8856def754625a3 (patch) | |
tree | d45e06a548f91af7fb44047c96cc335f5a8871cb /src/util.rs | |
parent | e621ba49ded953b3258201278edea8b8ecadc19f (diff) | |
download | netapp-238c0162c0036f767dbf5672f8856def754625a3.tar.gz netapp-238c0162c0036f767dbf5672f8856def754625a3.zip |
Add parse_and_resolve_peer_addr
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index 81ee89c..4f6e352 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,5 @@ use std::net::SocketAddr; +use std::net::ToSocketAddrs; use serde::Serialize; @@ -72,3 +73,16 @@ pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> { let ip = ip[1..].parse::<SocketAddr>().ok()?; Some((pubkey, ip)) } + +/// 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>)> { + let delim = peer.find('@')?; + let (key, host) = peer.split_at(delim); + let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?; + let hosts = host[1..] + .to_socket_addrs() + .ok()? + .collect::<Vec<_>>(); + Some((pubkey, hosts)) +} |