diff --git a/README.md b/README.md index 064e43e..87aac2f 100644 --- a/README.md +++ b/README.md @@ -50,9 +50,16 @@ chain stays in memory, so the next connection syncs from its tip. A chain below the minimum work starts again from genesis. ```sh -elo [:port] # IPv6 as [::1]:8333 +elo [:port] # IPv6 as [::1]:8333 ``` +The peer is an IP address or a name. Before each dial, elo looks up the name +with the resolver of the OS, within 30 s. It takes at most 256 of its +addresses, in a random order, and dials them in that order until one takes the +connect. So a name with many addresses, such as a DNS seed, gives a new peer +on each dial. A failed lookup is an `end`, and elo looks up the name again +after 5 s. + SIGTERM and SIGINT (Ctrl-C) stop elo at any step, also in the wait before a dial. It logs the signal and exits with 0. elo exits with 1 only on a fault of this host: its async runtime or its signal handlers do not start, a write to @@ -67,7 +74,7 @@ container runtime, reads stdout and adds the time. elo sends nothing to a log se ```text level=info event=version peer=127.0.0.1:18444 version=70016 services=0000000000000c09 user_agent=/Satoshi:31.1.0/ start_height=0 relay=true level=info event=tip peer=127.0.0.1:18444 height=0 hash=0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 work=0000000000000000000000000000000000000000000000000000000000000002 -level=warn event=end peer=127.0.0.1:18444 reason="the peer closed the connection" wait_ms=5000 +level=warn event=end destination=127.0.0.1:18444 reason="the peer closed the connection" wait_ms=5000 level=info event=stop signal=SIGTERM ``` @@ -75,7 +82,7 @@ level=info event=stop signal=SIGTERM | --- | --- | --- | | `version` | `info` | `peer` `version` `services` `user_agent` `start_height` `relay` | | `tip` | `info` | `peer` `height` `hash` `work` | -| `end` | `warn` | `peer` `reason` `wait_ms`: elo dials the peer again after `wait_ms` | +| `end` | `warn` | `destination` `reason` `wait_ms`: elo dials `destination` again after `wait_ms`. `destination` is the peer of the command line, also a name. `peer` is always the address of a connection | | `exit` | `error` | `reason`: a fault of this host, and elo exits with 1 | | `stop` | `info` | `signal`: elo exits with 0 | | `panic` | `error` | `reason`: a bug, and elo exits with 101 | diff --git a/src/chainparams.rs b/src/chainparams.rs index 446bb1d..e28219a 100644 --- a/src/chainparams.rs +++ b/src/chainparams.rs @@ -63,6 +63,12 @@ const CHAIN_WORK_MIN_SIGNET: [u8; 32] = [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x46, 0x3e, 0xa0, 0xa4, 0xb8, ]; +// `NonZeroU16::new` runs at compile time here, so a port of 0 does not build. +const PORT_MAINNET: std::num::NonZeroU16 = std::num::NonZeroU16::new(8333).expect("not 0"); +const PORT_TESTNET4: std::num::NonZeroU16 = std::num::NonZeroU16::new(48333).expect("not 0"); +const PORT_SIGNET: std::num::NonZeroU16 = std::num::NonZeroU16::new(38333).expect("not 0"); +const PORT_REGTEST: std::num::NonZeroU16 = std::num::NonZeroU16::new(18444).expect("not 0"); + impl Network { /// The bytes that start every message. pub(crate) const fn magic(self) -> [u8; MAGIC_SIZE] { @@ -75,12 +81,12 @@ impl Network { } /// The port that a node of the network listens on. - pub const fn default_port(self) -> u16 { + pub const fn default_port(self) -> std::num::NonZeroU16 { match self { - Network::Mainnet => 8333, - Network::Testnet4 => 48333, - Network::Signet => 38333, - Network::Regtest => 18444, + Network::Mainnet => PORT_MAINNET, + Network::Testnet4 => PORT_TESTNET4, + Network::Signet => PORT_SIGNET, + Network::Regtest => PORT_REGTEST, } } diff --git a/src/logging.rs b/src/logging.rs index a579d5c..95203e5 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -7,8 +7,11 @@ use std::fmt::Write; /// The most bytes of a value before its escapes. A longer value is cut, and ends with `...`. So -/// a peer that sends a long string cannot make a long line. -const VALUE_LENGTH_MAX: usize = 256; +/// a peer that sends a long string cannot make a long line. The longest destination of the +/// command line fits, so the `destination` of an `end` line is never cut. +const VALUE_LENGTH_MAX: usize = 260; + +const _: () = assert!(crate::net::DESTINATION_LENGTH_MAX <= VALUE_LENGTH_MAX); /// What `...` adds to a value that is cut. const CUT_MARK: &str = "..."; @@ -52,9 +55,11 @@ pub enum Event<'a> { peer: std::net::SocketAddr, tip: &'a crate::chain::Tip, }, - /// The connection to `peer` ended for `reason`, and elo dials it again after `wait`. + /// The connection to `destination` ended for `reason`, and elo dials it again after `wait`. + /// `destination` is the peer of the command line: an IP address, or a name that elo looks up + /// again. It has its own key, so `peer` is always the address of a connection. End { - peer: std::net::SocketAddr, + destination: &'a crate::net::Destination, reason: &'a dyn std::fmt::Display, wait: std::time::Duration, }, @@ -112,8 +117,12 @@ impl Event<'_> { line.field("hash", tip.hash_display()); line.field("work", tip.work()); } - Event::End { peer, reason, wait } => { - line.field("peer", peer); + Event::End { + destination, + reason, + wait, + } => { + line.field("destination", destination); line.field("reason", reason); line.field("wait_ms", wait.as_millis()); } @@ -302,11 +311,13 @@ mod tests { ), ( super::Event::End { - peer: PEER, + destination: &crate::net::Destination::Address( + crate::net::PeerAddress::new(PEER).unwrap(), + ), reason: &"the peer closed the connection", wait: std::time::Duration::from_secs(5), }, - "level=warn event=end peer=127.0.0.1:18444 \ + "level=warn event=end destination=127.0.0.1:18444 \ reason=\"the peer closed the connection\" wait_ms=5000", ), ( diff --git a/src/main.rs b/src/main.rs index 69aeb37..ba9dc6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,13 @@ -//! `elo [:port]`: dials one peer, does the handshake, and logs the peer's -//! `version`. Then it syncs the header chain from the peer and logs the tip. It asks the peer to -//! announce each new header, takes it on the tip, and logs the tip when it moves. It pings the -//! peer and answers its `ping` messages until the connection ends. Then it waits and dials the -//! peer again. The chain stays in memory, so the next connection syncs from its tip. SIGTERM -//! and SIGINT stop it at any of these steps. Each step writes a line of `elo::logging` to stdout. +//! `elo [:port]`: dials one peer, does the handshake, and logs the peer's +//! `version`. The host is an IP address or a name. Before each dial, elo looks up the name, and +//! dials its addresses in a random order until one takes the connect. Then it syncs the header +//! chain from the peer and logs the tip. It asks the peer to announce each new header, takes it on +//! the tip, and logs the tip when it moves. It pings the peer and answers its `ping` messages +//! until the connection ends. Then it waits and dials the peer again. The chain stays in memory, +//! so the next connection syncs from its tip. SIGTERM and SIGINT stop it at any of these steps. +//! Each step writes a line of `elo::logging` to stdout. -const USAGE: &str = "usage: elo [:port]"; +const USAGE: &str = "usage: elo [:port]"; /// The exit code for bad usage. A stop by a signal exits with 0, and a panic with 101. Every /// other exit is 1, because only a fault of this host ends elo. @@ -26,7 +28,7 @@ fn main() -> std::process::ExitCode { })); let args: Vec = std::env::args_os().skip(1).collect(); - let Some((network, peer)) = parse(&args) else { + let Some((network, destination)) = parse(&args) else { eprintln!("{USAGE}"); return std::process::ExitCode::from(EXIT_USAGE); }; @@ -40,13 +42,17 @@ fn main() -> std::process::ExitCode { Ok(runtime) => runtime, Err(error) => return exit(&format_args!("runtime: {error}")), }; - runtime.block_on(run(network, peer)) + let code = runtime.block_on(run(network, destination)); + // A lookup runs on a blocking thread of the runtime, and a stop or a lookup timeout does not + // end it. The drop of the runtime would wait for that thread, so the runtime does not wait. + runtime.shutdown_background(); + code } -/// Dials `peer` again after each end that retries, until a signal or an end that exits. +/// Dials `destination` again after each end that retries, until a signal or an end that exits. async fn run( network: elo::chainparams::Network, - peer: std::net::SocketAddr, + destination: elo::net::Destination, ) -> std::process::ExitCode { // Before the first dial, so a signal from here on stops elo. As PID 1 in a container, a // process with no handler never sees a SIGTERM: the kernel drops it, and the stop ends in @@ -63,7 +69,7 @@ async fn run( loop { // The drop of the connection closes its socket. elo keeps nothing on disk, so the stop // has nothing else to finish. - let end = match signals.unless(dial(peer, &mut chain)).await { + let end = match signals.unless(dial(&destination, &mut chain)).await { Ok(Ok(end)) => end, Ok(Err(error)) => return exit_stdout(error), Err(signal) => return stop(signal), @@ -73,8 +79,10 @@ async fn run( if !end.retries() { return exit(&end); } + // The destination of the command line, and not the address of the connection: the next + // dial looks up a name again. let logged = log(&elo::logging::Event::End { - peer, + destination: &destination, reason: &end, wait: RECONNECT_WAIT, }); @@ -188,7 +196,7 @@ impl Signals { /// How one connection ended. A failed write to stdout is no end of a connection: it ends elo. #[derive(Debug)] enum End { - /// The connect or the handshake failed. + /// The lookup, the connect or the handshake failed. Handshake(elo::net::HandshakeError), /// The connection ended in the sync or after it. Run(elo::net::RunError), @@ -203,8 +211,15 @@ impl End { match self { End::Handshake(elo::net::HandshakeError::Salt(_)) => false, End::Handshake( - elo::net::HandshakeError::Connect(_) - | elo::net::HandshakeError::ConnectTimeout + elo::net::HandshakeError::Lookup( + elo::net::LookupError::Failed(_) + | elo::net::LookupError::Timeout + | elo::net::LookupError::Empty, + ) + | elo::net::HandshakeError::Connect { + error: elo::net::ConnectError::Io(_) | elo::net::ConnectError::Timeout, + .. + } | elo::net::HandshakeError::HandshakeTimeout | elo::net::HandshakeError::Io(_) | elo::net::HandshakeError::Closed @@ -231,19 +246,20 @@ impl std::fmt::Display for End { } } -/// Dials `peer`, does the handshake and logs the `version` of the peer. Then syncs `chain` and -/// logs its tip, and logs it again each time an announcement of the peer moves it, until the -/// connection ends. `Err` is a failed write to stdout. +/// Dials `destination`, does the handshake and logs the `version` of the peer. Then syncs +/// `chain` and logs its tip, and logs it again each time an announcement of the peer moves it, +/// until the connection ends. `Err` is a failed write to stdout. async fn dial( - peer: std::net::SocketAddr, + destination: &elo::net::Destination, chain: &mut elo::chain::Chain, ) -> Result { - let (connection, version) = match elo::net::handshake(peer, chain).await { + let (connection, version) = match elo::net::handshake(destination, chain).await { Ok(handshake) => handshake, Err(error) => return Ok(End::Handshake(error)), }; + let address = connection.address(); log(&elo::logging::Event::Version { - peer, + peer: address, version: &version, })?; @@ -254,7 +270,10 @@ async fn dial( // The loop has no count bound, because the peer announces headers for as long as it is // connected. Only the end of the connection or a fault of stdout gets out. loop { - log(&elo::logging::Event::Tip { peer, tip: &tip })?; + log(&elo::logging::Event::Tip { + peer: address, + tip: &tip, + })?; (synced, tip) = match synced.next_tip().await { Ok(next) => next, Err(error) => return Ok(End::Run(error)), @@ -266,7 +285,9 @@ async fn dial( /// /// An IPv6 address needs brackets even without a port, because `::1:8333` is an IPv6 address /// and not `::1` with a port. -fn parse(args: &[std::ffi::OsString]) -> Option<(elo::chainparams::Network, std::net::SocketAddr)> { +fn parse( + args: &[std::ffi::OsString], +) -> Option<(elo::chainparams::Network, elo::net::Destination)> { let [network, peer] = args else { return None; }; @@ -279,25 +300,38 @@ fn parse(args: &[std::ffi::OsString]) -> Option<(elo::chainparams::Network, std: }; let peer = peer.to_str()?; - let peer = if let Ok(peer) = peer.parse() { - peer - } else if let Some(ip) = peer.strip_prefix('[').and_then(|ip| ip.strip_suffix(']')) { - let ip = ip.parse::().ok()?; - std::net::SocketAddr::from((ip, network.default_port())) + let destination = if let Some(address) = address(peer, network.default_port()) { + // Port 0, `0.0.0.0`, `[::]` and `[::ffff:0.0.0.0]` name no peer. + elo::net::Destination::Address(elo::net::PeerAddress::new(address)?) } else { - let ip = peer.parse::().ok()?; - std::net::SocketAddr::from((ip, network.default_port())) + // The parse of a `NonZeroU16` refuses port 0. + let (host, port) = match peer.split_once(':') { + Some((host, port)) => (host, port.parse().ok()?), + None => (peer, network.default_port()), + }; + elo::net::Destination::Name { + name: elo::net::Name::new(host)?, + port, + } }; - // Port 0, `0.0.0.0`, `[::]` and `[::ffff:0.0.0.0]` name no peer. - if peer.port() == 0 || peer.ip().to_canonical().is_unspecified() { - return None; + Some((network, destination)) +} + +/// `peer` as an IP address with an optional port, or `None` if it is no IP address. +fn address(peer: &str, default_port: std::num::NonZeroU16) -> Option { + if let Ok(address) = peer.parse() { + return Some(address); } - Some((network, peer)) + let ip = match peer.strip_prefix('[').and_then(|ip| ip.strip_suffix(']')) { + Some(ip) => std::net::IpAddr::V6(ip.parse().ok()?), + None => std::net::IpAddr::V4(peer.parse().ok()?), + }; + Some(std::net::SocketAddr::from((ip, default_port.get()))) } #[cfg(test)] mod tests { - fn parse(args: &[&str]) -> Option<(elo::chainparams::Network, std::net::SocketAddr)> { + fn parse(args: &[&str]) -> Option<(elo::chainparams::Network, elo::net::Destination)> { let args: Vec = args.iter().map(std::ffi::OsString::from).collect(); super::parse(&args) } @@ -305,7 +339,7 @@ mod tests { // The L3 tests also hold a closed connection, which retries, and a stdout fault, which // exits. A stdout fault is no `End`, so it has no row here. #[test] - fn a_fault_of_this_host_ends_elo_and_a_failed_connect_retries() { + fn a_fault_of_this_host_ends_elo_and_a_failed_connect_or_lookup_retries() { for (end, retries) in [ ( super::End::Handshake(elo::net::HandshakeError::Salt(std::io::Error::other( @@ -314,13 +348,17 @@ mod tests { false, ), ( - super::End::Handshake(elo::net::HandshakeError::Connect(std::io::Error::other( - "connect", - ))), + super::End::Handshake(elo::net::HandshakeError::Connect { + address: std::net::SocketAddr::from(([127, 0, 0, 1], 1)), + addresses_count: 1, + error: elo::net::ConnectError::Timeout, + }), true, ), ( - super::End::Handshake(elo::net::HandshakeError::ConnectTimeout), + super::End::Handshake(elo::net::HandshakeError::Lookup( + elo::net::LookupError::Timeout, + )), true, ), ] { @@ -347,36 +385,94 @@ mod tests { ("signet", elo::chainparams::Network::Signet, 38333), ("regtest", elo::chainparams::Network::Regtest, 18444), ] { - let peer = std::net::SocketAddr::from(([127, 0, 0, 1], port)); - assert_eq!(parse(&[name, "127.0.0.1"]), Some((network, peer)), "{name}"); + let address = std::net::SocketAddr::from(([127, 0, 0, 1], port)); + let destination = + elo::net::Destination::Address(elo::net::PeerAddress::new(address).unwrap()); + assert_eq!( + parse(&[name, "127.0.0.1"]), + Some((network, destination)), + "{name}" + ); + let destination = elo::net::Destination::Name { + name: elo::net::Name::new("localhost").unwrap(), + port: std::num::NonZeroU16::new(port).unwrap(), + }; + assert_eq!( + parse(&[name, "localhost"]), + Some((network, destination)), + "{name}" + ); } } #[test] - fn peer_is_an_ip_literal_with_an_optional_port() { + fn peer_is_an_ip_literal_or_a_name_with_an_optional_port() { let localhost_v6 = std::net::Ipv6Addr::LOCALHOST; + let address = |address| { + let address = elo::net::PeerAddress::new(address).unwrap(); + Some(elo::net::Destination::Address(address)) + }; + let name = |host: &str, port| { + Some(elo::net::Destination::Name { + name: elo::net::Name::new(host).unwrap(), + port: std::num::NonZeroU16::new(port).unwrap(), + }) + }; + let label = "a".repeat(63); + let long = [label.as_str(); 4].join("."); + let longest = &long[..253]; for (peer, expected) in [ ( "127.0.0.1:1", - Some(std::net::SocketAddr::from(([127, 0, 0, 1], 1))), + address(std::net::SocketAddr::from(([127, 0, 0, 1], 1))), ), ( "[::1]", - Some(std::net::SocketAddr::from((localhost_v6, 18444))), + address(std::net::SocketAddr::from((localhost_v6, 18444))), ), ( "[::1]:1", - Some(std::net::SocketAddr::from((localhost_v6, 1))), + address(std::net::SocketAddr::from((localhost_v6, 1))), ), ("::1:8333", None), ("127.0.0.1:0", None), ("0.0.0.0", None), ("[::]", None), ("[::ffff:0.0.0.0]", None), - ("localhost", None), - ("localhost:18444", None), + ("[localhost]", None), + ("localhost", name("localhost", 18444)), + ("localhost:1", name("localhost", 1)), + ("seed-1.example.org", name("seed-1.example.org", 18444)), + ("x9.Seed.example", name("x9.Seed.example", 18444)), + ("1a", name("1a", 18444)), + (longest, name(longest, 18444)), + (&long[..254], None), + (&format!("{label}a"), None), + ("localhost:0", None), + ("localhost:65536", None), + ("localhost:", None), + ("localhost.", name("localhost.", 18444)), + ("localhost..", None), + (".", None), + (&format!("{longest}."), name(&format!("{longest}."), 18444)), + (".localhost", None), + ("local..host", None), + ("-localhost", None), + ("localhost-", None), + ("local_host", None), + ("local host", None), + ("127.1", None), + ("999.0.0.1", None), + ("0x7f000001", None), + ("0X7F000001", None), + ("127.0x1", None), + ("0x0", None), + ("0x7f000001.", None), + ("0x7g", name("0x7g", 18444)), + ("0x7f.example", name("0x7f.example", 18444)), + ("", None), ] { - let parsed = parse(&["regtest", peer]).map(|(_, peer)| peer); + let parsed = parse(&["regtest", peer]).map(|(_, destination)| destination); assert_eq!(parsed, expected, "{peer}"); } } diff --git a/src/net.rs b/src/net.rs index ae2521e..639014a 100644 --- a/src/net.rs +++ b/src/net.rs @@ -17,13 +17,181 @@ const HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60 /// read can block forever. The ping timeout covers each read. const SEND_TIMEOUT: std::time::Duration = std::time::Duration::from_mins(20); -/// Why the connect or the handshake failed. +/// How long the lookup of a name may take. The resolver of the OS tries each name server again +/// after its own timeout, by default 5 s, two times for each of at most 3 servers. +const LOOKUP_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30); + +/// The most addresses of a name that one dial tries. Each try takes at most `CONNECT_TIMEOUT`, so +/// this bounds the time of a dial. +const ADDRESSES_COUNT_MAX: usize = 256; + +/// The most bytes of a name, with no `.` at the end. +const NAME_LENGTH_MAX: usize = 253; + +/// The most bytes of one label of a name. +const LABEL_LENGTH_MAX: usize = 63; + +/// The most bytes of the text of a `Destination`: a name of `NAME_LENGTH_MAX` bytes, its `.` at +/// the end, and the longest port. An IP address with its port is shorter: at most 58 bytes, an +/// IPv6 address with a scope. +pub(crate) const DESTINATION_LENGTH_MAX: usize = NAME_LENGTH_MAX + ".".len() + ":65535".len(); + +/// The peer that the command line names. +#[derive(Debug, PartialEq, Eq)] +pub enum Destination { + /// An IP address, which `handshake` dials with no lookup. + Address(PeerAddress), + /// A name, which `handshake` looks up before each dial. + Name { + name: Name, + port: std::num::NonZeroU16, + }, +} + +impl std::fmt::Display for Destination { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Destination::Address(address) => write!(f, "{}", address.0), + Destination::Name { name, port } => write!(f, "{name}:{port}"), + } + } +} + +/// An address that names a peer: its port is not 0, and its IP is not `0.0.0.0` or `::`. A +/// connect to those reaches this host. Only `PeerAddress::new` makes one, so each address that +/// `handshake` dials names a peer. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct PeerAddress(std::net::SocketAddr); + +impl PeerAddress { + /// `address` as the address of a peer, or `None` if it names no peer. An IPv4 address in + /// IPv6, such as `[::ffff:0.0.0.0]`, is checked as the IPv4 address. + pub fn new(address: std::net::SocketAddr) -> Option { + if address.port() == 0 || address.ip().to_canonical().is_unspecified() { + return None; + } + Some(PeerAddress(address)) + } +} + +/// The name of a host. Only `Name::new` makes one, so each name has the form that it checks. +#[derive(Debug, PartialEq, Eq)] +pub struct Name(String); + +impl Name { + /// `host` as a name, or `None` if it is no name. A name has labels, split by `.`, of at most + /// `NAME_LENGTH_MAX` bytes in all, and can have one more `.` at the end, as a name with all + /// its labels. The last label is no number, so the resolver cannot read the name as an IPv4 + /// address. + pub fn new(host: &str) -> Option { + let labels = host.strip_suffix('.').unwrap_or(host); + if labels.len() > NAME_LENGTH_MAX { + return None; + } + if !labels.split('.').all(is_label) { + return None; + } + let last = labels.rsplit_once('.').map_or(labels, |(_, last)| last); + if is_number(last) { + return None; + } + Some(Name(host.to_owned())) + } +} + +impl std::fmt::Display for Name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +/// Whether `label` is a label of a name: 1 to `LABEL_LENGTH_MAX` letters, digits and `-`, with no +/// `-` at the start or the end. +fn is_label(label: &str) -> bool { + if label.is_empty() { + return false; + } + if label.len() > LABEL_LENGTH_MAX { + return false; + } + if label.starts_with('-') || label.ends_with('-') { + return false; + } + label + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-') +} + +/// Whether the IPv4 parse of the resolver reads `label` as a number: only digits, or `0x` and +/// only hex digits. That parse also reads the short forms, so `127.1`, `0x7f000001` and `127.0x1` +/// are each `127.0.0.1`. It needs each part to be a number, so a name whose last label is no +/// number goes to DNS. Octal, after a `0`, has only digits. +fn is_number(label: &str) -> bool { + let (digits, radix) = match label + .strip_prefix("0x") + .or_else(|| label.strip_prefix("0X")) + { + Some(hex) => (hex, 16), + None => (label, 10), + }; + digits.chars().all(|digit| digit.is_digit(radix)) +} + +/// Why the lookup of a name gave no address to dial. +#[derive(Debug)] +pub enum LookupError { + /// The resolver of the OS failed. + Failed(std::io::Error), + /// The lookup took longer than `LOOKUP_TIMEOUT`. + Timeout, + /// The name has no address that names a peer. + Empty, +} + +impl std::fmt::Display for LookupError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + LookupError::Failed(error) => write!(f, "lookup: {error}"), + LookupError::Timeout => write!( + f, + "the lookup took longer than {} s", + LOOKUP_TIMEOUT.as_secs() + ), + LookupError::Empty => write!(f, "the lookup gave no address"), + } + } +} + +/// Why the TCP connect to one address failed. +#[derive(Debug)] +pub enum ConnectError { + /// The connect failed. + Io(std::io::Error), + /// The connect took longer than `CONNECT_TIMEOUT`. + Timeout, +} + +impl std::fmt::Display for ConnectError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ConnectError::Io(error) => write!(f, "{error}"), + ConnectError::Timeout => write!(f, "took longer than {} s", CONNECT_TIMEOUT.as_secs()), + } + } +} + +/// Why the lookup, the connect or the handshake failed. #[derive(Debug)] pub enum HandshakeError { - /// The TCP connect failed. - Connect(std::io::Error), - /// The TCP connect took longer than `CONNECT_TIMEOUT`. - ConnectTimeout, + /// The lookup of the name of the destination failed. + Lookup(LookupError), + /// The TCP connect to each address of the destination failed. `address` is the last one, + /// `error` is its error, and `addresses_count` is how many addresses `handshake` tried. + Connect { + address: std::net::SocketAddr, + addresses_count: usize, + error: ConnectError, + }, /// The handshake took longer than `HANDSHAKE_TIMEOUT`. HandshakeTimeout, /// A read or a write on the socket failed. @@ -41,14 +209,20 @@ pub enum HandshakeError { impl std::fmt::Display for HandshakeError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - HandshakeError::Connect(error) => write!(f, "connect: {error}"), - HandshakeError::ConnectTimeout => { - write!( - f, - "connect took longer than {} s", - CONNECT_TIMEOUT.as_secs() - ) - } + HandshakeError::Lookup(error) => write!(f, "{error}"), + HandshakeError::Connect { + address, + addresses_count: 1, + error, + } => write!(f, "connect to {address}: {error}"), + HandshakeError::Connect { + address, + addresses_count, + error, + } => write!( + f, + "connect to {address}, the last of {addresses_count} addresses: {error}" + ), HandshakeError::HandshakeTimeout => { write!( f, @@ -137,6 +311,8 @@ enum ReceiveError { pub struct Connection<'a> { socket: Socket, peer: crate::net_processing::Peer<'a>, + /// The address of the peer that took the connect. + address: std::net::SocketAddr, } /// One peer after the header sync. Only `Connection::sync` makes one, so `next_tip` cannot start @@ -176,32 +352,112 @@ enum PartialFrame { }, } -/// Dials `peer` on the network of `chain` and runs the handshake. Returns the connection for the -/// messages after it, and their `version`. The sync of the connection extends `chain` from its -/// tip, so the next connection with the same chain does not get those headers again. A chain -/// below the minimum work starts again from genesis. A peer whose chain does not have our tip -/// answers from the highest header of our locator on its chain, and a branch with more work takes -/// the place of ours. -pub async fn handshake( - peer: std::net::SocketAddr, - chain: &mut crate::chain::Chain, -) -> Result<(Connection<'_>, crate::protocol::Version), HandshakeError> { - let connect = tokio::net::TcpStream::connect(peer); - let stream = match tokio::time::timeout(CONNECT_TIMEOUT, connect).await { - Ok(Ok(stream)) => stream, - Ok(Err(error)) => return Err(HandshakeError::Connect(error)), - Err(_elapsed) => return Err(HandshakeError::ConnectTimeout), +/// Looks up `destination` if it is a name. Then dials the first of its addresses that takes the +/// TCP connect, in the order of `dial_order`, on the network of `chain`, and runs the handshake. +/// Returns the connection for the messages after it, and their `version`. The sync of the +/// connection extends `chain` from its tip, so the next connection with the same chain does not +/// get those headers again. A chain below the minimum work starts again from genesis. A peer +/// whose chain does not have our tip answers from the highest header of our locator on its chain, +/// and a branch with more work takes the place of ours. +pub async fn handshake<'a>( + destination: &Destination, + chain: &'a mut crate::chain::Chain, +) -> Result<(Connection<'a>, crate::protocol::Version), HandshakeError> { + let addresses = match destination { + Destination::Address(address) => Addresses { + before: Vec::new(), + last: *address, + }, + Destination::Name { name, port } => { + lookup(name, *port).await.map_err(HandshakeError::Lookup)? + } + }; + let (address, stream) = connect_first(&addresses).await?; + handshake_stream(address, stream, chain).await +} + +/// The addresses that one dial tries, in order: each one of `before`, then `last`. So a dial has +/// at least one address, with no check. +#[derive(Debug)] +struct Addresses { + before: Vec, + last: PeerAddress, +} + +/// The addresses of `name` at `port`, as `dial_order` gives them. +async fn lookup(name: &Name, port: std::num::NonZeroU16) -> Result { + let answers = tokio::net::lookup_host((name.0.as_str(), port.get())); + match tokio::time::timeout(LOOKUP_TIMEOUT, answers).await { + Ok(Ok(answers)) => dial_order(answers), + Ok(Err(error)) => Err(LookupError::Failed(error)), + Err(_elapsed) => Err(LookupError::Timeout), + } +} + +/// The `answers` of a lookup that name a peer, at most `ADDRESSES_COUNT_MAX`, each one time, in a +/// random order. So each dial can start on another address of a name that has many, such as a +/// seed. The order needs no secret, so it comes from the keys of `RandomState`. +fn dial_order( + answers: impl Iterator, +) -> Result { + // Some resolvers give `0.0.0.0` or `::` for a name that they block. + let mut before: Vec = answers + .filter_map(PeerAddress::new) + .take(ADDRESSES_COUNT_MAX) + .collect(); + // A hosts file can have the same name and address on two lines. A second try of an address + // costs up to `CONNECT_TIMEOUT` again. + before.sort_unstable_by_key(|address| address.0); + before.dedup(); + let random = std::hash::RandomState::new(); + before.sort_by_cached_key(|address| random.hash_one(address)); + let Some(last) = before.pop() else { + return Err(LookupError::Empty); }; - handshake_stream(peer, stream, chain).await + Ok(Addresses { before, last }) +} + +/// The TCP connect to the first of `addresses` that takes it. Each try takes at most +/// `CONNECT_TIMEOUT`, and `ADDRESSES_COUNT_MAX` bounds the tries, so the time of a dial is +/// bounded. When each try fails, the error has the last address, its error, and the count. +async fn connect_first( + addresses: &Addresses, +) -> Result<(std::net::SocketAddr, tokio::net::TcpStream), HandshakeError> { + let Addresses { before, last } = addresses; + // `dial_order` keeps at most `ADDRESSES_COUNT_MAX`, with `last`. + assert!(before.len() < ADDRESSES_COUNT_MAX, "{}", before.len()); + for &address in before { + if let Ok(stream) = connect(address).await { + return Ok((address.0, stream)); + } + } + match connect(*last).await { + Ok(stream) => Ok((last.0, stream)), + Err(error) => Err(HandshakeError::Connect { + address: last.0, + addresses_count: before.len() + 1, + error, + }), + } +} + +/// The TCP connect to `address`, within `CONNECT_TIMEOUT`. +async fn connect(address: PeerAddress) -> Result { + let connect = tokio::net::TcpStream::connect(address.0); + match tokio::time::timeout(CONNECT_TIMEOUT, connect).await { + Ok(Ok(stream)) => Ok(stream), + Ok(Err(error)) => Err(ConnectError::Io(error)), + Err(_elapsed) => Err(ConnectError::Timeout), + } } /// The handshake on a connected `stream`, within `HANDSHAKE_TIMEOUT`. async fn handshake_stream( - peer: std::net::SocketAddr, + address: std::net::SocketAddr, stream: tokio::net::TcpStream, chain: &mut crate::chain::Chain, ) -> Result<(Connection<'_>, crate::protocol::Version), HandshakeError> { - let exchange = handshake_stream_exchange(peer, stream, chain); + let exchange = handshake_stream_exchange(address, stream, chain); match tokio::time::timeout(HANDSHAKE_TIMEOUT, exchange).await { Ok(result) => result, Err(_elapsed) => Err(HandshakeError::HandshakeTimeout), @@ -211,7 +467,7 @@ async fn handshake_stream( /// Sends our `version`, then reads messages until the handshake is done. The handshake /// bounds the loop: it refuses a peer that sends too many messages without its `verack`. async fn handshake_stream_exchange( - peer: std::net::SocketAddr, + address: std::net::SocketAddr, stream: tokio::net::TcpStream, chain: &mut crate::chain::Chain, ) -> Result<(Connection<'_>, crate::protocol::Version), HandshakeError> { @@ -219,8 +475,14 @@ async fn handshake_stream_exchange( let mut socket = Socket::new(stream, network); let work_min = crate::pow::chain_work_min(network); let salt = salt().map_err(HandshakeError::Salt)?; - let (mut handshake, outbound) = - crate::net_processing::Handshake::start(peer, timestamp(), nonce(), chain, work_min, salt); + let (mut handshake, outbound) = crate::net_processing::Handshake::start( + address, + timestamp(), + nonce(), + chain, + work_min, + salt, + ); socket .send_handshake_outbound(&outbound) .await @@ -243,7 +505,11 @@ async fn handshake_stream_exchange( // The peer can send more frames in the same segment as its `verack`. Each read // stops at the end of a frame, so they are still in the stream, and `sync` reads // them next. - let connection = Connection { socket, peer }; + let connection = Connection { + socket, + peer, + address, + }; return Ok((connection, theirs)); } } @@ -251,6 +517,12 @@ async fn handshake_stream_exchange( } impl<'a> Connection<'a> { + /// The address of the peer: of the addresses of the destination, the one that took the + /// connect. + pub fn address(&self) -> std::net::SocketAddr { + self.address + } + /// Runs one turn after the other until a batch that is not full ends the sync. Returns the /// peer for `Synced::next_tip`, and the tip of our chain. The loop has no count bound, because /// only the chain of the peer bounds the sync. A low-work sync gets the chain twice, and the @@ -268,6 +540,7 @@ impl<'a> Connection<'a> { let Connection { mut socket, mut peer, + address: _, } = self; loop { match run_turn(&mut socket, peer).await? { @@ -718,6 +991,29 @@ mod tests { .block_on(future) } + fn destination(peer: std::net::SocketAddr) -> super::Destination { + super::Destination::Address(super::PeerAddress::new(peer).unwrap()) + } + + fn peer_addresses(addresses: &[std::net::SocketAddr]) -> Vec { + let peer_address = |address| super::PeerAddress::new(address).unwrap(); + addresses.iter().copied().map(peer_address).collect() + } + + /// `addresses` in their order, with the last one as `last`. + fn dial(addresses: &[std::net::SocketAddr]) -> super::Addresses { + let mut before = peer_addresses(addresses); + let last = before.pop().unwrap(); + super::Addresses { before, last } + } + + /// The addresses of a dial, in their order. + fn dialed(addresses: super::Addresses) -> Vec { + let super::Addresses { mut before, last } = addresses; + before.push(last); + before + } + /// Reads one frame on the peer's side of the socket. fn read_frame(stream: &mut std::net::TcpStream) -> Vec { read_frame_on(stream, crate::chainparams::Network::Regtest) @@ -820,7 +1116,7 @@ mod tests { verack }); - let result = block_on(super::handshake(peer, &mut chain)); + let result = block_on(super::handshake(&destination(peer), &mut chain)); let verack = peer_thread.join().unwrap(); assert_eq!(verack, crate::fixtures::hex(crate::fixtures::VERACK_FRAME)); assert!(result.is_ok(), "{result:?}"); @@ -849,7 +1145,7 @@ mod tests { i64::try_from(elapsed.as_secs()).unwrap() }; let before = seconds(); - let _closed = block_on(super::handshake(peer, &mut chain)); + let _closed = block_on(super::handshake(&destination(peer), &mut chain)); let after = seconds(); let message = peer_thread.join().unwrap(); assert_eq!(message.msg_type(), crate::protocol::VERSION); @@ -903,7 +1199,9 @@ mod tests { }); let (result, elapsed) = block_on(async { - let (connection, _theirs) = super::handshake(peer, &mut chain).await.unwrap(); + let (connection, _theirs) = super::handshake(&destination(peer), &mut chain) + .await + .unwrap(); tokio::time::pause(); let start = tokio::time::Instant::now(); let result = connection.sync().await; @@ -960,7 +1258,9 @@ mod tests { }); let result = block_on(async { - let (connection, _theirs) = super::handshake(peer, &mut chain).await.unwrap(); + let (connection, _theirs) = super::handshake(&destination(peer), &mut chain) + .await + .unwrap(); connection.sync().await }); peer_thread.join().unwrap(); @@ -1019,7 +1319,9 @@ mod tests { }); let result = block_on(async { - let (connection, _theirs) = super::handshake(peer, &mut chain).await.unwrap(); + let (connection, _theirs) = super::handshake(&destination(peer), &mut chain) + .await + .unwrap(); connection.sync().await }); let expected = [ @@ -1063,7 +1365,9 @@ mod tests { }); let (error, elapsed) = block_on(async { - let (connection, _theirs) = super::handshake(peer, &mut chain).await.unwrap(); + let (connection, _theirs) = super::handshake(&destination(peer), &mut chain) + .await + .unwrap(); let start = tokio::time::Instant::now(); let (synced, _tip) = connection.sync().await.unwrap(); tokio::time::pause(); @@ -1118,7 +1422,9 @@ mod tests { }); let (synced_tip, next_tip) = block_on(async { - let (connection, _theirs) = super::handshake(peer, &mut chain).await.unwrap(); + let (connection, _theirs) = super::handshake(&destination(peer), &mut chain) + .await + .unwrap(); let (synced, synced_tip) = connection.sync().await.unwrap(); let (_synced, next_tip) = synced.next_tip().await.unwrap(); (synced_tip, next_tip) @@ -1160,7 +1466,9 @@ mod tests { }); let (error, elapsed) = block_on(async { - let (connection, _theirs) = super::handshake(peer, &mut chain).await.unwrap(); + let (connection, _theirs) = super::handshake(&destination(peer), &mut chain) + .await + .unwrap(); let start = tokio::time::Instant::now(); let (synced, _tip) = connection.sync().await.unwrap(); tokio::time::pause(); @@ -1197,7 +1505,9 @@ mod tests { let peer_thread = std::thread::spawn(move || accept_handshake(&listener)); let (result, elapsed) = block_on(async { - let (connection, _theirs) = super::handshake(peer, &mut chain).await.unwrap(); + let (connection, _theirs) = super::handshake(&destination(peer), &mut chain) + .await + .unwrap(); // The peer thread ends when it has sent its `verack`, so this wait blocks nothing. // The stream stays open, and nothing reads it. let _stream = peer_thread.join().unwrap(); @@ -1239,7 +1549,7 @@ mod tests { stream.write_all(&frame[..frame.len() - 1]).unwrap(); }); - let result = block_on(super::handshake(peer, &mut chain)); + let result = block_on(super::handshake(&destination(peer), &mut chain)); peer_thread.join().unwrap(); assert!( matches!(result, Err(super::HandshakeError::Closed)), @@ -1318,7 +1628,9 @@ mod tests { }); let result = block_on(async { - let (connection, _theirs) = super::handshake(peer, &mut chain).await.unwrap(); + let (connection, _theirs) = super::handshake(&destination(peer), &mut chain) + .await + .unwrap(); connection.sync().await }); let pong = peer_thread.join().unwrap(); @@ -1340,15 +1652,125 @@ mod tests { .unwrap() .local_addr() .unwrap(); - let result = block_on(super::handshake(peer, &mut chain)); + let result = block_on(super::handshake(&destination(peer), &mut chain)); + let Err(error) = result else { + panic!("{result:?}"); + }; + let super::HandshakeError::Connect { + address, + addresses_count, + error: super::ConnectError::Io(io), + } = &error + else { + panic!("{error:?}"); + }; + assert_eq!(io.kind(), std::io::ErrorKind::ConnectionRefused); + assert_eq!(*address, peer); + assert_eq!(*addresses_count, 1); + let text = error.to_string(); + assert!(text.starts_with(&format!("connect to {peer}: ")), "{text}"); + } + + #[test] + fn connect_takes_the_first_address_that_takes_it() { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let listening = listener.local_addr().unwrap(); + // Nothing listens on a port that was bound and then freed. The listener is open while + // the bind picks the port, so the two ports differ. + let refused = std::net::TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap(); + for addresses in [[refused, listening], [listening, refused]] { + let (address, _stream) = block_on(super::connect_first(&dial(&addresses))).unwrap(); + assert_eq!(address, listening, "{addresses:?}"); + } + } + + #[test] + fn connect_to_no_address_gives_the_last_one_and_the_count() { + // Both listeners are open while the binds pick the ports, so the two ports differ. + // Nothing listens on them after the drop. + let first = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let last = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let addresses = [first.local_addr().unwrap(), last.local_addr().unwrap()]; + drop((first, last)); + + let result = block_on(super::connect_first(&dial(&addresses))); + let Err(error) = result else { + panic!("{result:?}"); + }; + let super::HandshakeError::Connect { + address, + addresses_count, + error: super::ConnectError::Io(_), + } = &error + else { + panic!("{error:?}"); + }; + assert_eq!(*address, addresses[1]); + assert_eq!(*addresses_count, 2); + let text = error.to_string(); + let expected = format!("connect to {}, the last of 2 addresses: ", addresses[1]); + assert!(text.starts_with(&expected), "{text}"); + } + + #[test] + fn dial_order_drops_the_answers_that_name_no_peer_and_keeps_at_most_the_count_max() { + let unspecified = [ + std::net::SocketAddr::from(([0, 0, 0, 0], 1)), + std::net::SocketAddr::from((std::net::Ipv6Addr::UNSPECIFIED, 1)), + std::net::SocketAddr::from((std::net::Ipv4Addr::UNSPECIFIED.to_ipv6_mapped(), 1)), + std::net::SocketAddr::from(([127, 0, 0, 1], 0)), + ]; + let result = super::dial_order(unspecified.into_iter()); assert!( - matches!( - result, - Err(super::HandshakeError::Connect(ref error)) - if error.kind() == std::io::ErrorKind::ConnectionRefused - ), + matches!(result, Err(super::LookupError::Empty)), "{result:?}" ); + + // One more than the count max, after the unspecified ones. Each port is new, so the + // ones that it keeps are the first ones. + let answers: Vec = (1..=super::ADDRESSES_COUNT_MAX + 1) + .map(|port| std::net::SocketAddr::from(([127, 0, 0, 1], u16::try_from(port).unwrap()))) + .collect(); + let kept = super::dial_order(unspecified.into_iter().chain(answers.iter().copied())); + let mut kept = dialed(kept.unwrap()); + kept.sort_unstable_by_key(|address| address.0); + assert_eq!(kept, peer_addresses(&answers[..super::ADDRESSES_COUNT_MAX])); + } + + #[test] + fn dial_order_keeps_each_address_one_time() { + let first = std::net::SocketAddr::from(([127, 0, 0, 1], 1)); + let second = std::net::SocketAddr::from(([127, 0, 0, 1], 2)); + let addresses = super::dial_order([first, second, first, first].into_iter()).unwrap(); + let mut kept = dialed(addresses); + kept.sort_unstable_by_key(|address| address.0); + assert_eq!(kept, peer_addresses(&[first, second])); + } + + // The resolver of the OS reads `localhost` from its hosts file, with no name server. + #[test] + fn lookup_gives_the_addresses_of_a_name_with_the_port() { + let name = super::Name::new("localhost").unwrap(); + let addresses = block_on(super::lookup(&name, std::num::NonZeroU16::MIN)).unwrap(); + for super::PeerAddress(address) in dialed(addresses) { + assert!(address.ip().is_loopback(), "{address}"); + assert_eq!(address.port(), 1, "{address}"); + } + } + + #[test] + fn longest_destination_has_the_length_max() { + let label = "a".repeat(super::LABEL_LENGTH_MAX); + let labels = [label.as_str(); 4].join("."); + let longest = format!("{}.", &labels[..super::NAME_LENGTH_MAX]); + let destination = super::Destination::Name { + name: super::Name::new(&longest).unwrap(), + port: std::num::NonZeroU16::MAX, + }; + assert_eq!(destination.to_string().len(), super::DESTINATION_LENGTH_MAX); } #[test] diff --git a/tests/bitcoind.rs b/tests/bitcoind.rs index 319c4b5..0d75ca8 100644 --- a/tests/bitcoind.rs +++ b/tests/bitcoind.rs @@ -167,7 +167,10 @@ fn handshake_with_bitcoind_returns_its_version() { .unwrap(); let mut chain = elo::chain::Chain::new(elo::chainparams::Network::Regtest); let (_connection, version) = runtime - .block_on(elo::net::handshake(bitcoind.peer, &mut chain)) + .block_on(elo::net::handshake( + &elo::net::Destination::Address(elo::net::PeerAddress::new(bitcoind.peer).unwrap()), + &mut chain, + )) .unwrap(); assert_eq!(version.version(), 70016); @@ -274,7 +277,7 @@ fn our_ping_gets_a_pong_from_bitcoind() { assert_eq!( end, format!( - "level=warn event=end peer={} reason=\"the peer closed the connection\" wait_ms=5000", + "level=warn event=end destination={} reason=\"the peer closed the connection\" wait_ms=5000", bitcoind.peer ) ); @@ -304,6 +307,34 @@ fn closed_connection_gets_a_new_handshake_and_sync() { assert_eq!(elo.event("tip", SYNC_TIMEOUT), tip); } +#[test] +fn peer_with_a_name_gets_a_lookup_and_a_dial_to_its_address() { + let bitcoind = Bitcoind::start(); + let named = format!("localhost:{}", bitcoind.peer.port()); + let elo = Elo::start_on(&named); + + // `localhost` can also have `::1`, where bitcoind does not listen. elo tries each address of + // the name in one dial, so the first dial gets to bitcoind, with no `end` before it. + let version = elo.event("version", SYNC_TIMEOUT); + let expected = format!( + "level=info event=version peer={} version=70016 ", + bitcoind.peer + ); + assert!(version.starts_with(&expected), "{version}"); + let ends = elo.events("end"); + assert!(ends.is_empty(), "{ends:?}"); + + // The `end` has the name, because the next dial looks it up again. elo is the only peer of + // bitcoind, so it is node 0. + elo.event("tip", SYNC_TIMEOUT); + bitcoind.cli(&["-named", "disconnectnode", "nodeid=0"]); + let end = elo.event("end", END_TIMEOUT); + assert!( + end.starts_with(&format!("level=warn event=end destination={named} ")), + "{end}" + ); +} + #[test] fn signal_in_a_connection_exits_with_0() { let bitcoind = Bitcoind::start(); @@ -640,9 +671,14 @@ struct Elo { impl Elo { fn start(bitcoind: &Bitcoind) -> Self { + Self::start_on(&bitcoind.peer.to_string()) + } + + /// elo on regtest with `peer` as the peer of its command line. + fn start_on(peer: &str) -> Self { let mut child = std::process::Command::new(env!("CARGO_BIN_EXE_elo")) .arg("regtest") - .arg(bitcoind.peer.to_string()) + .arg(peer) .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::piped()) .spawn()