diff options
author | Alex Auvolat <alex@adnab.me> | 2021-10-14 11:58:09 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2021-10-14 11:58:09 +0200 |
commit | 7e49d0dac847cc2a502038eb512255377ac6238a (patch) | |
tree | 84684b549067b7ad6390e4d1a311d25a0b39d629 | |
parent | d62b1610405d5f02b8725862fd4a4f1368bd684b (diff) | |
download | netapp-7e49d0dac847cc2a502038eb512255377ac6238a.tar.gz netapp-7e49d0dac847cc2a502038eb512255377ac6238a.zip |
Make a public function to parse peer addresses
-rw-r--r-- | examples/basalt.rs | 9 | ||||
-rw-r--r-- | examples/fullmesh.rs | 10 | ||||
-rw-r--r-- | src/util.rs | 12 |
3 files changed, 16 insertions, 15 deletions
diff --git a/examples/basalt.rs b/examples/basalt.rs index 85fbb58..752583d 100644 --- a/examples/basalt.rs +++ b/examples/basalt.rs @@ -1,5 +1,4 @@ use std::io::Write; -use std::net::SocketAddr; use std::sync::Arc; use std::time::Duration; @@ -17,6 +16,7 @@ use tokio::sync::watch; use netapp::endpoint::*; use netapp::peering::basalt::*; use netapp::proto::*; +use netapp::util::parse_peer_addr; use netapp::{NetApp, NodeID}; #[derive(StructOpt, Debug)] @@ -98,12 +98,7 @@ async fn main() { let mut bootstrap_peers = vec![]; for peer in opt.bootstrap_peers.iter() { - if let Some(delim) = peer.find('@') { - let (key, ip) = peer.split_at(delim); - let pubkey = ed25519::PublicKey::from_slice(&hex::decode(&key).unwrap()).unwrap(); - let ip = ip[1..].parse::<SocketAddr>().unwrap(); - bootstrap_peers.push((pubkey, ip)); - } + bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address")); } let basalt_params = BasaltParams { diff --git a/examples/fullmesh.rs b/examples/fullmesh.rs index 67861a7..f5b307b 100644 --- a/examples/fullmesh.rs +++ b/examples/fullmesh.rs @@ -1,5 +1,4 @@ use std::io::Write; -use std::net::SocketAddr; use log::info; @@ -9,8 +8,8 @@ use sodiumoxide::crypto::auth; use sodiumoxide::crypto::sign::ed25519; use netapp::peering::fullmesh::*; +use netapp::util::*; use netapp::NetApp; -use netapp::NodeID; #[derive(StructOpt, Debug)] #[structopt(name = "netapp")] @@ -70,12 +69,7 @@ async fn main() { let mut bootstrap_peers = vec![]; for peer in opt.bootstrap_peers.iter() { - if let Some(delim) = peer.find('@') { - let (key, ip) = peer.split_at(delim); - let pubkey = NodeID::from_slice(&hex::decode(&key).unwrap()).unwrap(); - let ip = ip[1..].parse::<SocketAddr>().unwrap(); - bootstrap_peers.push((pubkey, ip)); - } + bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address")); } let peering = FullMeshPeeringStrategy::new(netapp.clone(), bootstrap_peers); 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)) +} |