iroh/socket/transports/
custom.rs

1// The items in this module are exported from [`crate::endpoint::transports`] only if
2// the "unstable-custom-transports" feature is enabled
3#![cfg_attr(not(feature = "unstable-custom-transports"), allow(unreachable_pub))]
4
5use std::{
6    io,
7    num::NonZeroUsize,
8    sync::Arc,
9    task::{Context, Poll},
10};
11
12use iroh_base::CustomAddr;
13
14use super::{RecvInfo, Transmit};
15
16/// Custom transport.
17///
18/// Usually a transport will only deal with a single custom address type, but
19/// the signature allows for dealing with multiple custom address types.
20///
21/// A transport is a factory for custom endpoints. Whenever an iroh endpoint is
22/// created using [crate::endpoint::Builder::bind], a new custom endpoint will
23/// be created using [CustomTransport::bind].
24pub trait CustomTransport: std::fmt::Debug + Send + Sync + 'static {
25    /// Create a custom endpoint
26    ///
27    /// Analogously to [std::net::UdpSocket::bind], this is where the actual
28    /// underlying hardware resource is created.
29    fn bind(&self) -> io::Result<Box<dyn CustomEndpoint>>;
30}
31
32/// Custom endpoint created by a [CustomTransport].
33///
34/// An endpoint has a local address (or multiple local addresses), can receive
35/// packets, and can create senders to send packets.
36pub trait CustomEndpoint: std::fmt::Debug + Send + Sync + 'static {
37    /// A watcher for local addresses for this custom endpoint.
38    fn watch_local_addrs(&self) -> n0_watcher::Direct<Vec<CustomAddr>>;
39    /// Create a custom sender for this custom endpoint.
40    fn create_sender(&self) -> Arc<dyn CustomSender>;
41    /// poll receiving a packet on this custom endpoint.
42    ///
43    /// This will be called with `bufs`, `metas` and `recv_infos` of the same length.
44    /// It is acceptable to panic if this is not the case.
45    ///
46    /// The maximum length of the slices is [`noq_udp::BATCH_SIZE`].
47    /// It is acceptable to panic if this is exceeded.
48    ///
49    /// On success, all three slices must be filled up to the returned length,
50    /// and the returned length must be less than or equal to the length of the slices.
51    ///
52    /// It does not make much sense to return addresses unrelated to this transport.
53    ///
54    /// For each filled slot, write a [`RecvInfo::new`] carrying the remote
55    /// custom address and, if the transport can identify it, the local custom
56    /// address that received the packet. The latter surfaces via
57    /// [`crate::endpoint::Incoming::local_addr`].
58    fn poll_recv(
59        &mut self,
60        cx: &mut Context,
61        bufs: &mut [io::IoSliceMut<'_>],
62        metas: &mut [noq_udp::RecvMeta],
63        recv_infos: &mut [RecvInfo],
64    ) -> Poll<io::Result<usize>>;
65
66    /// Maximum number of segments to transmit (GSO).
67    ///
68    /// This controls how many datagrams Noq will batch into a single transmit.
69    /// The default is 1 (no batching). Custom transports that support batching
70    /// can override this to allow more efficient transmission.
71    fn max_transmit_segments(&self) -> NonZeroUsize {
72        NonZeroUsize::MIN
73    }
74}
75
76impl std::fmt::Display for Box<dyn CustomEndpoint> {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        write!(f, "CustomTransport")
79    }
80}
81
82/// Custom sender
83///
84/// A sender provides a poll based interface to send packets to custom addresses.
85/// It can decide whether it wants to send to a given custom address type.
86///
87/// This is not enforced at type level, but [CustomSender::poll_send] should
88/// only be called with addresses for which [CustomSender::is_valid_send_addr]
89/// returns true.
90pub trait CustomSender: std::fmt::Debug + Send + Sync + 'static {
91    /// True if this sender can send to the given address.
92    fn is_valid_send_addr(&self, addr: &CustomAddr) -> bool;
93    /// poll sending a packet on this sender.
94    ///
95    /// This will only be called from iroh with addresses for which [CustomSender::is_valid_send_addr] returns true.
96    ///
97    /// You should handle invalid addresses by returning an error.
98    fn poll_send(
99        &self,
100        cx: &mut std::task::Context,
101        dst: &CustomAddr,
102        src: Option<&CustomAddr>,
103        transmit: &Transmit<'_>,
104    ) -> Poll<io::Result<()>>;
105}