aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-10-14 11:58:09 +0200
committerAlex Auvolat <alex@adnab.me>2021-10-14 11:58:09 +0200
commit7e49d0dac847cc2a502038eb512255377ac6238a (patch)
tree84684b549067b7ad6390e4d1a311d25a0b39d629 /src/util.rs
parentd62b1610405d5f02b8725862fd4a4f1368bd684b (diff)
downloadnetapp-7e49d0dac847cc2a502038eb512255377ac6238a.tar.gz
netapp-7e49d0dac847cc2a502038eb512255377ac6238a.zip
Make a public function to parse peer addresses
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
index bf8f4cb..fa2f37a 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,3 +1,5 @@
+use std::net::SocketAddr;
+
use serde::Serialize;
use log::info;
@@ -56,3 +58,13 @@ pub fn watch_ctrl_c() -> watch::Receiver<bool> {
});
watch_cancel
}
+
+/// Parse a peer's address including public key, written in the format:
+/// `<public key hex>@<ip>:<port>`
+pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> {
+ let delim = peer.find('@')?;
+ let (key, ip) = peer.split_at(delim);
+ let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?;
+ let ip = ip[1..].parse::<SocketAddr>().ok()?;
+ Some((pubkey, ip))
+}