aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
blob: 186678db076507b0e062f23dddd5d19fe68002c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::endpoint::SerializeMessage;

use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::pin::Pin;

use futures::Stream;

use log::info;

use serde::Serialize;

use tokio::sync::watch;

/// A node's identifier, which is also its public cryptographic key
pub type NodeID = sodiumoxide::crypto::sign::ed25519::PublicKey;
/// A node's secret key
pub type NodeKey = sodiumoxide::crypto::sign::ed25519::SecretKey;
/// A network key
pub type NetworkKey = sodiumoxide::crypto::auth::Key;

/// A stream of associated data.
///
/// The Stream can continue after receiving an error.
/// When sent through Netapp, the Vec may be split in smaller chunk in such a way
/// consecutive Vec may get merged, but Vec and error code may not be reordered
///
/// Error code 255 means the stream was cut before its end. Other codes have no predefined
/// meaning, it's up to your application to define their semantic.
pub type AssociatedStream = Pin<Box<dyn Stream<Item = Packet> + Send>>;

pub type Packet = Result<Vec<u8>, u8>;

/// Utility function: encodes any serializable value in MessagePack binary format
/// using the RMP library.
///
/// Field names and variant names are included in the serialization.
/// This is used internally by the netapp communication protocol.
pub fn rmp_to_vec_all_named<T>(
	val: &T,
) -> Result<(Vec<u8>, Option<AssociatedStream>), rmp_serde::encode::Error>
where
	T: SerializeMessage + ?Sized,
{
	let mut wr = Vec::with_capacity(128);
	let mut se = rmp_serde::Serializer::new(&mut wr)
		.with_struct_map()
		.with_string_variants();
	let (val, stream) = val.serialize_msg();
	val.serialize(&mut se)?;
	Ok((wr, stream))
}

/// This async function returns only when a true signal was received
/// from a watcher that tells us when to exit.
///
/// Usefull in a select statement to interrupt another
/// future:
/// ```ignore
/// select!(
///     _ = a_long_task() => Success,
///     _ = await_exit(must_exit) => Interrupted,
/// )
/// ```
pub async fn await_exit(mut must_exit: watch::Receiver<bool>) {
	while !*must_exit.borrow_and_update() {
		if must_exit.changed().await.is_err() {
			break;
		}
	}
}

/// Creates a watch that contains `false`, and that changes
/// to `true` when a Ctrl+C signal is received.
pub fn watch_ctrl_c() -> watch::Receiver<bool> {
	let (send_cancel, watch_cancel) = watch::channel(false);
	tokio::spawn(async move {
		tokio::signal::ctrl_c()
			.await
			.expect("failed to install CTRL+C signal handler");
		info!("Received CTRL+C, shutting down.");
		send_cancel.send(true).unwrap();
	});
	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))
}

/// 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<_>>();
	if hosts.is_empty() {
		return None;
	}
	Some((pubkey, hosts))
}