nibkeep-net 0.1.0

Connections between NibKeep replicas on the home network, through iroh
Documentation
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! Connections between NibKeep replicas on the home network: iroh's QUIC with TLS, no
//! relays and nothing published to DNS, and replicas found by mDNS on the interfaces of home
//! networks alone, which announces an endpoint's key and addresses and no names. The API
//! blocks, so that the engine stays synchronous: a [`Network`] runs its own Tokio runtime,
//! and a [`Connection`] carries the messages of `nibkeep-sync` as its [`Transport`].
//!
//! The connections are specified in `docs/spec/sync.md`, and the reasons for them are in
//! `docs/Technical-design.md`, section 10.

mod discovery;

use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write as _;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};

use iroh::address_lookup::MemoryLookup;
use iroh::endpoint::{
	ConnectError, ConnectingError, Connection as Quic, ConnectionError, RecvStream, SendStream,
	TransportErrorCode, presets,
};
use iroh::{Endpoint, EndpointAddr, EndpointId, SecretKey, TransportAddr};
use nibkeep_sync::{MAX_MESSAGE_LEN, Transport, TransportError};
use tokio::runtime::Runtime;

/// The ALPN of pairing.
const PAIR: &[u8] = b"nibkeep/pair/1";

/// The ALPN of syncing.
const SYNC: &[u8] = b"nibkeep/sync/1";

/// The ALPN of invitations to a space.
const INVITE: &[u8] = b"nibkeep/invite/1";

/// The label of the bytes exported from TLS that bind what is signed to one connection.
const BINDING: &[u8] = b"EXPORTER-nibkeep-2026-10";

/// How long a side waits for the next message: long enough for the other to save what it
/// received before it answers.
const RECEIVE_TIMEOUT: Duration = Duration::from_mins(5);

/// How long a side that was connected to waits for the other to open its stream.
const STREAM_TIMEOUT: Duration = Duration::from_secs(10);

/// How long closing waits for the other side to read what was sent.
const CLOSE_TIMEOUT: Duration = Duration::from_secs(3);

/// How often waiting for a device to appear looks again.
const LOOK_EVERY: Duration = Duration::from_millis(100);

/// Which protocol a connection speaks.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Protocol {
	/// Pairing a new replica.
	Pair,
	/// Syncing replicas.
	Sync,
	/// Inviting a user to a space.
	Invite,
}

impl Protocol {
	const fn alpn(self) -> &'static [u8] {
		match self {
			Self::Pair => PAIR,
			Self::Sync => SYNC,
			Self::Invite => INVITE,
		}
	}
}

/// The UDP port that replicas take connections on unless told otherwise, so that a
/// firewall can let them through with one rule.
pub const DEFAULT_PORT: u16 = 53_300;

/// How many ports from the one asked for an endpoint tries, before it takes any: from
/// 53300 to 53309 by default, one for each library of a computer that waits at once.
pub const PORTS: u16 = 10;

/// How an endpoint starts.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Options {
	/// Whether it announces itself on the home network by mDNS and finds the others there;
	/// without, as in tests, it connects only to addresses it is given.
	pub discovery: bool,
	/// The UDP port it takes connections on, or the next free one of [`PORTS`] from it, or
	/// any free one if they are all taken; `None` for any free port.
	pub port: Option<u16>,
	/// Whether it takes connections; one that only connects refuses them at once, so that
	/// a peer that connects to it learns so, rather than waiting for an answer.
	pub accept: bool,
}

impl Options {
	/// On the home network, at the default port, taking connections.
	pub const HOME: Self = Self {
		discovery: true,
		port: Some(DEFAULT_PORT),
		accept: true,
	};

	/// For tests: no discovery, any free port, taking connections.
	pub const LOCAL: Self = Self {
		discovery: false,
		port: None,
		accept: true,
	};
}

/// A connection that another endpoint opened, with its stream, once its handshake is done.
type Incoming = (Quic, SendStream, RecvStream, Protocol);

/// Why networking failed.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum NetError {
	/// The endpoint could not start.
	#[error("cannot start networking: {0}")]
	Start(String),
	/// Connecting failed.
	#[error("cannot connect: {0}")]
	Connect(String),
	/// The other endpoint takes no connections now, as one that only connects refuses them:
	/// a program there syncs once, or does something else that needs no connection.
	#[error("the other device takes no connections now, as while it syncs by itself")]
	Refused,
	/// Nothing came in time.
	#[error("no device answered in time")]
	Timeout,
}

pub(crate) fn start_error(error: impl std::fmt::Display) -> NetError {
	NetError::Start(error.to_string())
}

fn connect_error(error: impl std::fmt::Display) -> NetError {
	NetError::Connect(error.to_string())
}

/// What connecting failed with: [`NetError::Refused`] where the other endpoint refused the
/// connection, as one that only connects does.
fn connecting_error(error: ConnectError) -> NetError {
	let closed = match &error {
		ConnectError::Connecting {
			source: ConnectingError::ConnectionError { source, .. },
			..
		}
		| ConnectError::Connection { source, .. } => Some(source),
		_ => None,
	};
	if closed.is_some_and(refused) {
		NetError::Refused
	} else {
		connect_error(error)
	}
}

/// Whether a connection ended as the other endpoint refused it.
fn refused(error: &ConnectionError) -> bool {
	matches!(error, ConnectionError::ConnectionClosed(close)
		if close.error_code == TransportErrorCode::CONNECTION_REFUSED)
}

/// What mDNS found: each endpoint seen on the network, with what it announces.
pub(crate) type Seen = Arc<Mutex<BTreeMap<[u8; 32], Option<String>>>>;

/// This replica's endpoint on the network, which connects to others and takes their
/// connections.
pub struct Network {
	runtime: Arc<Runtime>,
	endpoint: Endpoint,
	seen: Seen,
	/// The addresses of endpoints it was told of, rather than found.
	known: MemoryLookup,
	/// The connections that others opened, each taken in once its handshake is done, so
	/// that one that stalls holds up no other; `None` for one that takes no connections.
	incoming: Option<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<Incoming>>>,
	/// What announces this endpoint on the home network, and finds the others; `None`
	/// without discovery.
	discovery: Option<discovery::Discovery>,
	/// The IPv4 port it takes connections on.
	port: Option<u16>,
	/// The port asked for, and why it could not be had, if the endpoint took another.
	port_refused: Option<(u16, String)>,
}

/// How many connections that others opened wait to be taken.
const WAITING_CONNECTIONS: usize = 16;

/// Takes in the connections that others open, doing each handshake on its own, until the
/// endpoint closes; or refuses them all, without `queue`.
async fn take_in(endpoint: Endpoint, queue: Option<tokio::sync::mpsc::Sender<Incoming>>) {
	while let Some(incoming) = endpoint.accept().await {
		let Some(queue) = queue.clone() else {
			incoming.refuse();
			continue;
		};
		tokio::spawn(async move {
			let Ok(accepting) = incoming.accept() else {
				return;
			};
			let Ok(quic) = accepting.await else {
				return;
			};
			let protocol = match quic.alpn() {
				PAIR => Protocol::Pair,
				SYNC => Protocol::Sync,
				INVITE => Protocol::Invite,
				_ => return,
			};
			if let Ok(Ok((send, receive))) =
				tokio::time::timeout(STREAM_TIMEOUT, quic.accept_bi()).await
			{
				let _ = queue.send((quic, send, receive, protocol)).await;
			}
		});
	}
}

impl Network {
	/// Starts the endpoint with the 32-byte secret of its key, as `options` say.
	///
	/// # Errors
	///
	/// Returns an error if the runtime or the endpoint cannot start.
	pub fn start(secret: &[u8; 32], options: Options) -> Result<Self, NetError> {
		let runtime = tokio::runtime::Builder::new_multi_thread()
			.worker_threads(2)
			.thread_name("nibkeep-net")
			.enable_all()
			.build()
			.map_err(start_error)?;
		let seen: Seen = Arc::default();
		let known = MemoryLookup::new();
		let endpoint = runtime.block_on(async {
			let builder = || {
				Endpoint::builder(presets::Minimal)
					.secret_key(SecretKey::from_bytes(secret))
					.alpns(vec![PAIR.to_vec(), SYNC.to_vec(), INVITE.to_vec()])
					.address_lookup(known.clone())
			};
			let Some(first) = options.port else {
				return Ok::<_, NetError>((builder().bind().await.map_err(start_error)?, None));
			};
			// The first free of the ports from the one asked for, as other libraries of the
			// computer may have taken some, so that one firewall rule lets them all through.
			let mut refused = None;
			for port in (first..=first.saturating_add(PORTS - 1)).take(usize::from(PORTS)) {
				match at(builder(), port).map_err(start_error)?.bind().await {
					Ok(endpoint) => return Ok((endpoint, refused)),
					Err(error) => {
						refused.get_or_insert((first, error.to_string()));
					}
				}
			}
			// All are taken: any other will do.
			Ok((builder().bind().await.map_err(start_error)?, refused))
		})?;
		let (endpoint, port_refused) = endpoint;
		let port = endpoint
			.bound_sockets()
			.iter()
			.find(|address| address.is_ipv4())
			.map(SocketAddr::port);
		let (queue, incoming) = if options.accept {
			let (queue, incoming) = tokio::sync::mpsc::channel(WAITING_CONNECTIONS);
			(Some(queue), Some(tokio::sync::Mutex::new(incoming)))
		} else {
			(None, None)
		};
		runtime.spawn(take_in(endpoint.clone(), queue));
		let discovery = if options.discovery {
			// The port it took, which is another than the one asked for if that was taken.
			let port =
				port.ok_or_else(|| NetError::Start("no IPv4 socket to announce".to_owned()))?;
			Some(discovery::start(
				endpoint.id(),
				port,
				seen.clone(),
				known.clone(),
			)?)
		} else {
			None
		};
		Ok(Self {
			runtime: Arc::new(runtime),
			endpoint,
			seen,
			known,
			incoming,
			discovery,
			port,
			port_refused,
		})
	}

	/// The IPv4 port it takes connections on.
	#[must_use]
	pub const fn port(&self) -> Option<u16> {
		self.port
	}

	/// The port it asked for, and why it could not have it, if it took another: as when
	/// another program of NibKeep has it. A firewall that lets only the ports from that one
	/// through, [`PORTS`] of them, lets this endpoint's connections through only if it took
	/// one of them.
	#[must_use]
	pub fn port_refused(&self) -> Option<(u16, &str)> {
		self.port_refused
			.as_ref()
			.map(|(port, why)| (*port, why.as_str()))
	}

	/// Tells the endpoint where another is, and what it announces, as if it had found it on
	/// the network: for tests, and for networks where discovery does not work.
	pub fn remember(&self, peer: [u8; 32], addresses: &[SocketAddr], announced: Option<&str>) {
		if let Ok(id) = EndpointId::from_bytes(&peer) {
			let addresses = addresses.iter().copied().map(TransportAddr::Ip);
			self.known
				.add_endpoint_info(EndpointAddr::from_parts(id, addresses));
		}
		self.seen
			.lock()
			.unwrap_or_else(PoisonError::into_inner)
			.insert(peer, announced.map(str::to_owned));
	}

	/// This endpoint's ID: the public half of its key.
	#[must_use]
	pub fn endpoint(&self) -> [u8; 32] {
		*self.endpoint.id().as_bytes()
	}

	/// The addresses this endpoint takes connections on, as this computer reaches them, for
	/// connecting without discovery.
	#[must_use]
	pub fn local_addresses(&self) -> Vec<SocketAddr> {
		self.endpoint
			.bound_sockets()
			.into_iter()
			.map(|mut address| {
				if address.ip().is_unspecified() {
					address.set_ip(match address.ip() {
						IpAddr::V4(_) => IpAddr::V4(Ipv4Addr::LOCALHOST),
						IpAddr::V6(_) => IpAddr::V6(Ipv6Addr::LOCALHOST),
					});
				}
				address
			})
			.collect()
	}

	/// Adds `text` to what the endpoint announces on the network, or stops announcing any.
	///
	/// # Errors
	///
	/// Returns an error if the text is too long to announce.
	pub fn announce(&self, text: Option<&str>) -> Result<(), NetError> {
		match &self.discovery {
			Some(discovery) => discovery.announce(text),
			// Without discovery, only those told of it find this endpoint.
			None => discovery::check_announced(text),
		}
	}

	/// Waits up to `timeout` for an endpoint that announces `text`, other than those in
	/// `except`, and returns the one with the lowest ID among those found, so that one that
	/// tries them in turn tries each once.
	///
	/// # Errors
	///
	/// Returns [`NetError::Timeout`] if none appears in time.
	pub fn find_announcing(
		&self,
		text: &str,
		except: &BTreeSet<[u8; 32]>,
		timeout: Duration,
	) -> Result<[u8; 32], NetError> {
		self.wait(timeout, |seen| {
			seen.iter()
				.find(|(endpoint, announced)| {
					announced.as_deref() == Some(text) && !except.contains(*endpoint)
				})
				.map(|(endpoint, _)| *endpoint)
		})
		.ok_or(NetError::Timeout)
	}

	/// Waits up to `timeout` until every one of `endpoints` is seen on the network, and
	/// returns those seen by then.
	#[must_use]
	pub fn find(&self, endpoints: &BTreeSet<[u8; 32]>, timeout: Duration) -> BTreeSet<[u8; 32]> {
		let all = self.wait(timeout, |seen| {
			endpoints
				.iter()
				.all(|endpoint| seen.contains_key(endpoint))
				.then(|| endpoints.clone())
		});
		all.unwrap_or_else(|| {
			let seen = self.seen.lock().unwrap_or_else(PoisonError::into_inner);
			endpoints
				.iter()
				.filter(|endpoint| seen.contains_key(*endpoint))
				.copied()
				.collect()
		})
	}

	/// Looks at what mDNS found until `found` finds something, for up to `timeout`.
	fn wait<T>(
		&self,
		timeout: Duration,
		found: impl Fn(&BTreeMap<[u8; 32], Option<String>>) -> Option<T>,
	) -> Option<T> {
		let started = Instant::now();
		loop {
			let seen = self.seen.lock().unwrap_or_else(PoisonError::into_inner);
			if let Some(found) = found(&seen) {
				return Some(found);
			}
			drop(seen);
			if started.elapsed() >= timeout {
				return None;
			}
			std::thread::sleep(LOOK_EVERY);
		}
	}

	/// Connects to an endpoint found on the network.
	///
	/// # Errors
	///
	/// Returns an error if the endpoint cannot be reached, or refuses, within `timeout`.
	pub fn connect(
		&self,
		peer: [u8; 32],
		protocol: Protocol,
		timeout: Duration,
	) -> Result<Connection, NetError> {
		let id = EndpointId::from_bytes(&peer).map_err(connect_error)?;
		self.connect_to(EndpointAddr::new(id), protocol, timeout)
	}

	/// Connects to an endpoint at these addresses.
	///
	/// # Errors
	///
	/// As [`Network::connect`].
	pub fn connect_at(
		&self,
		peer: [u8; 32],
		addresses: &[SocketAddr],
		protocol: Protocol,
		timeout: Duration,
	) -> Result<Connection, NetError> {
		let id = EndpointId::from_bytes(&peer).map_err(connect_error)?;
		let addresses = addresses.iter().copied().map(TransportAddr::Ip);
		self.connect_to(EndpointAddr::from_parts(id, addresses), protocol, timeout)
	}

	fn connect_to(
		&self,
		address: EndpointAddr,
		protocol: Protocol,
		timeout: Duration,
	) -> Result<Connection, NetError> {
		let (quic, send, receive) = self.runtime.block_on(async {
			tokio::time::timeout(timeout, async {
				let quic = self
					.endpoint
					.connect(address, protocol.alpn())
					.await
					.map_err(connecting_error)?;
				let (send, receive) = quic.open_bi().await.map_err(|error| {
					if refused(&error) {
						NetError::Refused
					} else {
						connect_error(error)
					}
				})?;
				Ok::<_, NetError>((quic, send, receive))
			})
			.await
			.map_err(|_| NetError::Timeout)?
		})?;
		Connection::new(
			self.runtime.clone(),
			self.endpoint(),
			(quic, send, receive),
			protocol,
		)
	}

	/// Waits for another endpoint to connect, for up to `timeout`, or for as long as it
	/// takes; `None` if none did in time. Connections that fail before they speak a
	/// protocol of NibKeep are passed by.
	///
	/// # Errors
	///
	/// Returns an error if the endpoint was closed, or takes no connections.
	pub fn accept(&self, timeout: Option<Duration>) -> Result<Option<Connection>, NetError> {
		let Some(incoming) = &self.incoming else {
			return Err(NetError::Connect(
				"this endpoint takes no connections".to_owned(),
			));
		};
		let accepted = self.runtime.block_on(async {
			let mut incoming = incoming.lock().await;
			let next = match timeout {
				Some(timeout) => match tokio::time::timeout(timeout, incoming.recv()).await {
					Ok(next) => next,
					Err(_) => return Ok(None),
				},
				None => incoming.recv().await,
			};
			next.map(Some)
				.ok_or_else(|| NetError::Connect("the endpoint was closed".to_owned()))
		})?;
		accepted
			.map(|(quic, send, receive, protocol)| {
				Connection::new(
					self.runtime.clone(),
					self.endpoint(),
					(quic, send, receive),
					protocol,
				)
			})
			.transpose()
	}
}

/// A builder that binds `port` on every interface, IPv6 too where there is IPv6.
fn at(
	builder: iroh::endpoint::Builder,
	port: u16,
) -> Result<iroh::endpoint::Builder, iroh::endpoint::InvalidSocketAddr> {
	builder
		.clear_ip_transports()
		.bind_addr(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port))?
		.bind_addr_with_opts(
			SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), port),
			iroh::endpoint::BindOpts::default().set_is_required(false),
		)
}

impl Drop for Network {
	fn drop(&mut self) {
		let endpoint = self.endpoint.clone();
		self.runtime.block_on(async {
			let _ = tokio::time::timeout(CLOSE_TIMEOUT, endpoint.close()).await;
		});
	}
}

/// A connection to another replica: one stream of messages each way.
pub struct Connection {
	runtime: Arc<Runtime>,
	quic: Quic,
	send: SendStream,
	/// The messages that came, read off the stream as soon as they come, so that the
	/// connection holds no data unread while this side saves what came before; each with
	/// its share of the room it takes.
	received: tokio::sync::mpsc::UnboundedReceiver<Result<Received, String>>,
	/// What reads the messages off the stream.
	reader: tokio::task::JoinHandle<()>,
	protocol: Protocol,
	binding: [u8; 32],
	own: [u8; 32],
	peer: [u8; 32],
	/// The remote addresses of the paths the connection has used, to say which when it is
	/// lost.
	paths: BTreeSet<String>,
	/// Why reading failed, once it did, for every later read.
	failure: Option<String>,
}

/// How many bytes of messages that came wait to be taken, at most: room for the longest.
const RECEIVED_ROOM: u32 = 32 * 1024 * 1024;

const _: () = assert!(MAX_MESSAGE_LEN <= RECEIVED_ROOM as usize);

/// A message that came, and the room it takes until it is taken.
type Received = (Vec<u8>, tokio::sync::OwnedSemaphorePermit);

/// Reads messages off `stream` as they come, into `received`, while they take no more than
/// `room`, until the stream ends or fails.
async fn read_messages(
	mut stream: RecvStream,
	room: Arc<tokio::sync::Semaphore>,
	received: tokio::sync::mpsc::UnboundedSender<Result<Received, String>>,
) {
	loop {
		let mut length = [0; 4];
		match stream.read_exact(&mut length).await {
			Ok(()) => {}
			// The end of the stream, where a message may start, is no failure.
			Err(iroh::endpoint::ReadExactError::FinishedEarly(0)) => return,
			Err(error) => {
				let _ = received.send(Err(error.to_string()));
				return;
			}
		}
		let length = u32::from_be_bytes(length);
		if usize::try_from(length).map_or(true, |length| length > MAX_MESSAGE_LEN) {
			let _ = received.send(Err("a message too long".to_owned()));
			return;
		}
		// The room a message takes is its size, within the room there is, so that one as
		// large as the room still fits.
		let Ok(share) = room
			.clone()
			.acquire_many_owned(length.clamp(1, RECEIVED_ROOM))
			.await
		else {
			return;
		};
		let mut message = vec![0; length as usize];
		if let Err(error) = stream.read_exact(&mut message).await {
			let _ = received.send(Err(error.to_string()));
			return;
		}
		if received.send(Ok((message, share))).is_err() {
			return;
		}
	}
}

impl Connection {
	fn new(
		runtime: Arc<Runtime>,
		own: [u8; 32],
		(quic, send, receive): (Quic, SendStream, RecvStream),
		protocol: Protocol,
	) -> Result<Self, NetError> {
		let mut binding = [0; 32];
		quic.export_keying_material(&mut binding, BINDING, b"")
			.map_err(|_| NetError::Connect("cannot bind to the connection".to_owned()))?;
		let peer = *quic.remote_id().as_bytes();
		let (sender, received) = tokio::sync::mpsc::unbounded_channel();
		let room = Arc::new(tokio::sync::Semaphore::new(RECEIVED_ROOM as usize));
		let reader = runtime.spawn(read_messages(receive, room, sender));
		let mut connection = Self {
			runtime,
			quic,
			send,
			received,
			reader,
			protocol,
			binding,
			own,
			peer,
			paths: BTreeSet::new(),
			failure: None,
		};
		connection.note_paths();
		Ok(connection)
	}

	/// Notes the paths the connection uses now.
	fn note_paths(&mut self) {
		for path in self.quic.paths().iter() {
			self.paths.insert(format!("{:?}", path.remote_addr()));
		}
	}

	/// What went wrong with the connection, with why it closed, if it did, and the paths it
	/// used, so that a lost connection can be told apart from a refused one.
	fn failed(&self, error: impl std::fmt::Display) -> TransportError {
		let mut text = error.to_string();
		if let Some(reason) = self.quic.close_reason() {
			let _ = write!(text, " ({reason})");
		}
		if !self.paths.is_empty() {
			let paths: Vec<&str> = self.paths.iter().map(String::as_str).collect();
			let _ = write!(text, "; paths used: {}", paths.join(", "));
		}
		TransportError(text)
	}

	/// The protocol it speaks.
	#[must_use]
	pub const fn protocol(&self) -> Protocol {
		self.protocol
	}

	/// The other side's endpoint ID.
	#[must_use]
	pub const fn peer(&self) -> [u8; 32] {
		self.peer
	}

	/// Ends the connection, once the other side has read what this side sent, or after a
	/// few seconds.
	pub fn close(mut self) {
		let runtime = self.runtime.clone();
		runtime.block_on(async {
			let _ = self.send.finish();
			// What still comes is read and dropped, which frees its room, until the other
			// side ends its stream, once it has read this one, and the reader ends.
			let _ = tokio::time::timeout(CLOSE_TIMEOUT, async {
				while self.received.recv().await.is_some() {}
			})
			.await;
			self.quic.close(0_u32.into(), b"done");
		});
	}
}

impl Drop for Connection {
	fn drop(&mut self) {
		// A connection dropped unclosed, as on an error, stops reading, which lets it close.
		self.reader.abort();
	}
}

impl Transport for Connection {
	fn send(&mut self, message: &[u8]) -> Result<(), TransportError> {
		let length = u32::try_from(message.len())
			.ok()
			.filter(|_| message.len() <= MAX_MESSAGE_LEN)
			.ok_or_else(|| TransportError("a message too long to send".to_owned()))?;
		let runtime = self.runtime.clone();
		let sent = runtime.block_on(async {
			self.send.write_all(&length.to_be_bytes()).await?;
			self.send.write_all(message).await
		});
		match sent {
			Ok(()) => Ok(()),
			Err(error) => Err(self.failed(error)),
		}
	}

	fn receive(&mut self) -> Result<Vec<u8>, TransportError> {
		if let Some(failure) = &self.failure {
			return Err(self.failed(failure));
		}
		let runtime = self.runtime.clone();
		let received = runtime.block_on(async {
			match tokio::time::timeout(RECEIVE_TIMEOUT, self.received.recv()).await {
				Ok(Some(Ok((message, _share)))) => Ok(message),
				Ok(Some(Err(error))) => Err(error),
				Ok(None) => Err("the other device closed the connection".to_owned()),
				Err(_) => Err("the other device stopped answering".to_owned()),
			}
		});
		self.note_paths();
		received.map_err(|error| {
			let failed = self.failed(&error);
			self.failure = Some(error);
			failed
		})
	}

	fn has_message(&mut self) -> bool {
		// The reader passes on whole messages, or why it stopped.
		self.failure.is_some() || !self.received.is_empty()
	}

	fn binding(&self) -> [u8; 32] {
		self.binding
	}

	fn own_endpoint(&self) -> [u8; 32] {
		self.own
	}

	fn peer_endpoint(&self) -> [u8; 32] {
		self.peer
	}
}