+ message_router: MR,
+ offers_handler: OMH,
+ custom_handler: CMH,
+}
+
+/// [`OnionMessage`]s buffered to be sent.
+enum OnionMessageRecipient {
+ /// Messages for a node connected as a peer.
+ ConnectedPeer(VecDeque<OnionMessage>),
+
+ /// Messages for a node that is not yet connected, which are dropped after [`MAX_TIMER_TICKS`]
+ /// and tracked here.
+ PendingConnection(VecDeque<OnionMessage>, Option<Vec<SocketAddress>>, usize),
+}
+
+impl OnionMessageRecipient {
+ fn pending_connection(addresses: Vec<SocketAddress>) -> Self {
+ Self::PendingConnection(VecDeque::new(), Some(addresses), 0)
+ }
+
+ fn pending_messages(&self) -> &VecDeque<OnionMessage> {
+ match self {
+ OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
+ }
+ }
+
+ fn enqueue_message(&mut self, message: OnionMessage) {
+ let pending_messages = match self {
+ OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
+ };
+
+ pending_messages.push_back(message);
+ }
+
+ fn dequeue_message(&mut self) -> Option<OnionMessage> {
+ let pending_messages = match self {
+ OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageRecipient::PendingConnection(pending_messages, _, _) => {
+ debug_assert!(false);
+ pending_messages
+ },
+ };
+
+ pending_messages.pop_front()
+ }
+
+ #[cfg(test)]
+ fn release_pending_messages(&mut self) -> VecDeque<OnionMessage> {
+ let pending_messages = match self {
+ OnionMessageRecipient::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageRecipient::PendingConnection(pending_messages, _, _) => pending_messages,
+ };
+
+ core::mem::take(pending_messages)
+ }
+
+ fn mark_connected(&mut self) {
+ if let OnionMessageRecipient::PendingConnection(pending_messages, _, _) = self {
+ let mut new_pending_messages = VecDeque::new();
+ core::mem::swap(pending_messages, &mut new_pending_messages);
+ *self = OnionMessageRecipient::ConnectedPeer(new_pending_messages);
+ }
+ }
+}
+
+/// An [`OnionMessage`] for [`OnionMessenger`] to send.
+///
+/// These are obtained when released from [`OnionMessenger`]'s handlers after which they are
+/// enqueued for sending.
+#[cfg(not(c_bindings))]
+pub struct PendingOnionMessage<T: OnionMessageContents> {
+ /// The message contents to send in an [`OnionMessage`].
+ pub contents: T,
+
+ /// The destination of the message.
+ pub destination: Destination,
+
+ /// A reply path to include in the [`OnionMessage`] for a response.
+ pub reply_path: Option<BlindedPath>,
+}
+
+#[cfg(c_bindings)]
+/// An [`OnionMessage`] for [`OnionMessenger`] to send.
+///
+/// These are obtained when released from [`OnionMessenger`]'s handlers after which they are
+/// enqueued for sending.
+pub type PendingOnionMessage<T: OnionMessageContents> = (T, Destination, Option<BlindedPath>);
+
+pub(crate) fn new_pending_onion_message<T: OnionMessageContents>(
+ contents: T, destination: Destination, reply_path: Option<BlindedPath>
+) -> PendingOnionMessage<T> {
+ #[cfg(not(c_bindings))]
+ return PendingOnionMessage { contents, destination, reply_path };
+ #[cfg(c_bindings)]
+ return (contents, destination, reply_path);
+}
+
+/// A trait defining behavior for routing an [`OnionMessage`].
+pub trait MessageRouter {
+ /// Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
+ fn find_path(
+ &self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
+ ) -> Result<OnionMessagePath, ()>;
+}
+
+/// A [`MessageRouter`] that can only route to a directly connected [`Destination`].
+pub struct DefaultMessageRouter<G: Deref<Target=NetworkGraph<L>>, L: Deref>
+where
+ L::Target: Logger,
+{
+ network_graph: G,
+}
+
+impl<G: Deref<Target=NetworkGraph<L>>, L: Deref> DefaultMessageRouter<G, L>
+where
+ L::Target: Logger,
+{
+ /// Creates a [`DefaultMessageRouter`] using the given [`NetworkGraph`].
+ pub fn new(network_graph: G) -> Self {
+ Self { network_graph }
+ }
+}
+
+impl<G: Deref<Target=NetworkGraph<L>>, L: Deref> MessageRouter for DefaultMessageRouter<G, L>
+where
+ L::Target: Logger,
+{
+ fn find_path(
+ &self, _sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
+ ) -> Result<OnionMessagePath, ()> {
+ let first_node = destination.first_node();
+ if peers.contains(&first_node) {
+ Ok(OnionMessagePath { intermediate_nodes: vec![], destination, addresses: None })
+ } else {
+ let network_graph = self.network_graph.deref().read_only();
+ let node_announcement = network_graph
+ .node(&NodeId::from_pubkey(&first_node))
+ .and_then(|node_info| node_info.announcement_info.as_ref())
+ .and_then(|announcement_info| announcement_info.announcement_message.as_ref())
+ .map(|node_announcement| &node_announcement.contents);
+
+ match node_announcement {
+ Some(node_announcement) if node_announcement.features.supports_onion_messages() => {
+ let addresses = Some(node_announcement.addresses.clone());
+ Ok(OnionMessagePath { intermediate_nodes: vec![], destination, addresses })
+ },
+ _ => Err(()),
+ }
+ }
+ }
+}
+
+/// A path for sending an [`OnionMessage`].
+#[derive(Clone)]
+pub struct OnionMessagePath {
+ /// Nodes on the path between the sender and the destination.
+ pub intermediate_nodes: Vec<PublicKey>,
+
+ /// The recipient of the message.
+ pub destination: Destination,
+
+ /// Addresses that may be used to connect to [`OnionMessagePath::first_node`].
+ ///
+ /// Only needs to be set if a connection to the node is required. [`OnionMessenger`] may use
+ /// this to initiate such a connection.
+ pub addresses: Option<Vec<SocketAddress>>,
+}
+
+impl OnionMessagePath {
+ /// Returns the first node in the path.
+ pub fn first_node(&self) -> PublicKey {
+ self.intermediate_nodes
+ .first()
+ .copied()
+ .unwrap_or_else(|| self.destination.first_node())
+ }