netapp/src/util.rs

123 lines
3.4 KiB
Rust
Raw Normal View History

everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
use std::net;
use std::path::PathBuf;
use std::str::FromStr;
use log::info;
use serde::Serialize;
use tokio::sync::watch;
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
use tokio_unix_tcp::{NamedSocketAddr, UnixSocketAddr};
2022-07-22 11:06:10 +00:00
use crate::netapp::*;
2020-12-02 19:12:24 +00:00
/// 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>, rmp_serde::encode::Error>
2020-12-02 12:30:47 +00:00
where
T: Serialize + ?Sized,
2020-12-02 12:30:47 +00:00
{
let mut wr = Vec::with_capacity(128);
let mut se = rmp_serde::Serializer::new(&mut wr).with_struct_map();
val.serialize(&mut se)?;
Ok(wr)
2020-12-02 12:30:47 +00:00
}
/// This async function returns only when a true signal was received
/// from a watcher that tells us when to exit.
2021-10-14 09:35:05 +00:00
///
/// Usefull in a select statement to interrupt another
/// future:
2021-10-13 16:05:49 +00:00
/// ```ignore
/// select!(
/// _ = a_long_task() => Success,
/// _ = await_exit(must_exit) => Interrupted,
/// )
/// ```
pub async fn await_exit(mut must_exit: watch::Receiver<bool>) {
2021-10-12 11:07:34 +00:00
while !*must_exit.borrow_and_update() {
if must_exit.changed().await.is_err() {
break;
}
}
}
2021-10-14 09:35:05 +00:00
/// 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:
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
/// `<public key hex>@<ip>:<port>` or
/// `<public key hex>@<path>` for unix domain sockets
pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, NamedSocketAddr)> {
let delim = peer.find('@')?;
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
let (key, host) = peer.split_at(delim);
2023-01-31 22:57:33 +00:00
let pubkey = NodeID::from_slice(&hex::decode(key).ok()?)?;
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
let host = NamedSocketAddr::from_str(&host[1..]).ok()?;
Some((pubkey, host))
}
2021-10-18 10:39:19 +00:00
/// Parse and resolve a peer's address including public key, written in the format:
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
/// `<public key hex>@<ip or hostname>:<port>` or
/// `<public key hex>@<path>` for unix domain sockets
pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<NamedSocketAddr>)> {
2021-10-18 10:39:19 +00:00
let delim = peer.find('@')?;
let (key, host) = peer.split_at(delim);
2023-01-31 22:57:33 +00:00
let pubkey = NodeID::from_slice(&hex::decode(key).ok()?)?;
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
let host = &host[1..];
let hosts = if UnixSocketAddr::is_pathname(host) {
vec![PathBuf::from_str(host).unwrap().into()]
} else {
use std::net::ToSocketAddrs;
host.parse::<net::SocketAddr>()
.ok()?
.to_socket_addrs()
.ok()?
.map(NamedSocketAddr::Inet)
.collect::<Vec<_>>()
};
if hosts.is_empty() {
return None;
}
2021-10-18 10:39:19 +00:00
Some((pubkey, hosts))
}
/// async version of parse_and_resolve_peer_addr
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
pub async fn parse_and_resolve_peer_addr_async(
peer: &str,
) -> Option<(NodeID, Vec<NamedSocketAddr>)> {
let delim = peer.find('@')?;
let (key, host) = peer.split_at(delim);
2023-01-31 22:57:33 +00:00
let pubkey = NodeID::from_slice(&hex::decode(key).ok()?)?;
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
let host = &host[1..];
let hosts = if UnixSocketAddr::is_pathname(host) {
vec![PathBuf::from_str(host).unwrap().into()]
} else {
tokio::net::lookup_host(host)
.await
.ok()?
.map(NamedSocketAddr::Inet)
.collect::<Vec<_>>()
};
if hosts.is_empty() {
return None;
}
Some((pubkey, hosts))
}