@@ -160,10 +160,15 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
160160 CM :: Target : ChannelMessageHandler ,
161161 RM :: Target : RoutingMessageHandler {
162162 /// A message handler which handles messages specific to channels. Usually this is just a
163- /// ChannelManager object or a ErroringMessageHandler.
163+ /// [`ChannelManager`] object or a [`ErroringMessageHandler`].
164+ ///
165+ /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
164166 pub chan_handler : CM ,
165167 /// A message handler which handles messages updating our knowledge of the network channel
166- /// graph. Usually this is just a NetGraphMsgHandlerMonitor object or an IgnoringMessageHandler.
168+ /// graph. Usually this is just a [`NetGraphMsgHandler`] object or an
169+ /// [`IgnoringMessageHandler`].
170+ ///
171+ /// [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
167172 pub route_handler : RM ,
168173}
169174
@@ -173,26 +178,34 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
173178///
174179/// For efficiency, Clone should be relatively cheap for this type.
175180///
176- /// You probably want to just extend an int and put a file descriptor in a struct and implement
177- /// send_data. Note that if you are using a higher-level net library that may call close() itself,
178- /// be careful to ensure you don't have races whereby you might register a new connection with an
179- /// fd which is the same as a previous one which has yet to be removed via
180- /// PeerManager::socket_disconnected().
181+ /// If applicable in your language, you probably want to just extend an int and put a file
182+ /// descriptor in a struct and implement this trait on it. Note, of course, that if you do so and
183+ /// have multiple threads interacting with the [`PeerManager`], you must not call `close()` on the
184+ /// file descriptor until all threads are guaranteed to not call any [`PeerManager`] functions with
185+ /// the same descriptor. Otherwise, file descriptor re-use could imply that you call a
186+ /// [`PeerManager`] function with a file descriptor representing a different connection.
187+ ///
188+ /// Note that if you are using a higher-level net library that may call close() itself when you
189+ /// close a socket, be careful to ensure you don't directly use the file descriptor for your [`Eq`]
190+ /// and [`Hash`] implementations.
181191pub trait SocketDescriptor : cmp:: Eq + hash:: Hash + Clone {
182192 /// Attempts to send some data from the given slice to the peer.
183193 ///
184194 /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
185- /// Note that in the disconnected case, socket_disconnected must still fire and further write
186- /// attempts may occur until that time.
195+ /// Note that in the disconnected case, [`PeerManager:: socket_disconnected`] must still be
196+ /// called and further write attempts may occur until that time.
187197 ///
188- /// If the returned size is smaller than data.len(), a write_available event must
189- /// trigger the next time more data can be written. Additionally, until the a send_data event
190- /// completes fully, no further read_events should trigger on the same peer!
198+ /// If the returned size is smaller than data.len(), a
199+ /// [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
200+ /// written. Additionally, until a send_data event completes fully, no further
201+ /// [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
202+ /// prevent denial-of-service issues, you should not read or buffer any data from the socket
203+ /// until then.
191204 ///
192- /// If a read_event on this descriptor had previously returned true (indicating that read
193- /// events should be paused to prevent DoS in the send buffer), resume_read may be set
194- /// indicating that read events on this descriptor should resume. A resume_read of false does
195- /// *not* imply that further read events should be paused.
205+ /// If a [`PeerManager:: read_event`] call on this descriptor had previously returned true
206+ /// (indicating that read events should be paused to prevent DoS in the send buffer),
207+ /// resume_read may be set indicating that read events on this descriptor should resume. A
208+ /// resume_read of false does *not* imply on its own that further read events should be paused.
196209 fn send_data ( & mut self , data : & [ u8 ] , resume_read : bool ) -> usize ;
197210 /// Disconnect the socket pointed to by this SocketDescriptor.
198211 /// No [`PeerManager::socket_disconnected`] call need be generated as a result of this call.
@@ -309,8 +322,17 @@ pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArc
309322/// helps with issues such as long function definitions.
310323pub type SimpleRefPeerManager < ' a , ' b , ' c , ' d , ' e , ' f , ' g , SD , M , T , F , C , L > = PeerManager < SD , SimpleRefChannelManager < ' a , ' b , ' c , ' d , ' e , M , T , F , L > , & ' e NetGraphMsgHandler < & ' g C , & ' f L > , & ' f L > ;
311324
312- /// A PeerManager manages a set of peers, described by their SocketDescriptor and marshalls socket
313- /// events into messages which it passes on to its MessageHandlers.
325+ /// A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
326+ /// socket events into messages which it passes on to its MessageHandlers.
327+ ///
328+ /// Locks are taken internally, so you must never assume that reentrancy from a
329+ /// [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
330+ ///
331+ /// Calls to [`read_event`] will decode relevant messages and pass them to the
332+ /// [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
333+ /// parallelism in Rust-Lightning is parallelism in calls to [`read_event`]. Note, however, that
334+ /// calls to any [`PeerManager`] functions related to the same connection must occur only in
335+ /// serial, making new calls only after previous ones have returned.
314336///
315337/// Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
316338/// a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
@@ -397,8 +419,6 @@ impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor,
397419 }
398420}
399421
400- /// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
401- /// PeerIds may repeat, but only after socket_disconnected() has been called.
402422impl < Descriptor : SocketDescriptor , CM : Deref , RM : Deref , L : Deref > PeerManager < Descriptor , CM , RM , L > where
403423 CM :: Target : ChannelMessageHandler ,
404424 RM :: Target : RoutingMessageHandler ,
@@ -458,8 +478,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
458478 ///
459479 /// Returns a small number of bytes to send to the remote node (currently always 50).
460480 ///
461- /// Panics if descriptor is duplicative with some other descriptor which has not yet had a
462- /// socket_disconnected().
481+ /// Panics if descriptor is duplicative with some other descriptor which has not yet been
482+ /// [`socket_disconnected()`].
483+ ///
484+ /// [`socket_disconnected()`]: PeerManager::socket_disconnected
463485 pub fn new_outbound_connection ( & self , their_node_id : PublicKey , descriptor : Descriptor ) -> Result < Vec < u8 > , PeerHandleError > {
464486 let mut peer_encryptor = PeerChannelEncryptor :: new_outbound ( their_node_id. clone ( ) , self . get_ephemeral_key ( ) ) ;
465487 let res = peer_encryptor. get_act_one ( ) . to_vec ( ) ;
@@ -495,8 +517,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
495517 /// call socket_disconnected for the new descriptor but must disconnect the connection
496518 /// immediately.
497519 ///
498- /// Panics if descriptor is duplicative with some other descriptor which has not yet had
499- /// socket_disconnected called.
520+ /// Panics if descriptor is duplicative with some other descriptor which has not yet been
521+ /// [`socket_disconnected()`].
522+ ///
523+ /// [`socket_disconnected()`]: PeerManager::socket_disconnected
500524 pub fn new_inbound_connection ( & self , descriptor : Descriptor ) -> Result < ( ) , PeerHandleError > {
501525 let peer_encryptor = PeerChannelEncryptor :: new_inbound ( & self . our_node_secret ) ;
502526 let pending_read_buffer = [ 0 ; 50 ] . to_vec ( ) ; // Noise act one is 50 bytes
@@ -604,12 +628,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
604628 ///
605629 /// May return an Err to indicate that the connection should be closed.
606630 ///
607- /// Will most likely call send_data on the descriptor passed in (or the descriptor handed into
608- /// new_*\_connection) before returning. Thus, be very careful with reentrancy issues! The
609- /// invariants around calling write_buffer_space_avail in case a write did not fully complete
610- /// must still hold - be ready to call write_buffer_space_avail again if a write call generated
611- /// here isn't sufficient! Panics if the descriptor was not previously registered in a
612- /// new_\*_connection event.
631+ /// Will most likely call [`send_data`] on the descriptor passed in (or the descriptor handed
632+ /// into new_*\_connection) before returning. Thus, be very careful with reentrancy issues! The
633+ /// invariants around calling [`write_buffer_space_avail`] in case a write did not fully
634+ /// complete must still hold - be ready to call `[write_buffer_space_avail`] again if a write
635+ /// call generated here isn't sufficient!
636+ ///
637+ /// [`send_data`]: SocketDescriptor::send_data
638+ /// [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
613639 pub fn write_buffer_space_avail ( & self , descriptor : & mut Descriptor ) -> Result < ( ) , PeerHandleError > {
614640 let mut peers = self . peers . lock ( ) . unwrap ( ) ;
615641 match peers. peers . get_mut ( descriptor) {
@@ -631,13 +657,15 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
631657 ///
632658 /// May return an Err to indicate that the connection should be closed.
633659 ///
634- /// Will *not* call back into send_data on any descriptors to avoid reentrancy complexity.
635- /// Thus, however, you almost certainly want to call process_events() after any read_event to
636- /// generate send_data calls to handle responses.
660+ /// Will *not* call back into [` send_data`] on any descriptors to avoid reentrancy complexity.
661+ /// Thus, however, you almost certainly want to call [` process_events`] after any read_event
662+ /// to generate [` send_data`] calls to handle responses.
637663 ///
638- /// If Ok(true) is returned, further read_events should not be triggered until a send_data call
639- /// on this file descriptor has resume_read set (preventing DoS issues in the send buffer).
664+ /// If Ok(true) is returned, further read_events should not be triggered until a [` send_data`]
665+ /// call on this descriptor has resume_read set (preventing DoS issues in the send buffer).
640666 ///
667+ /// [`send_data`]: SocketDescriptor::send_data
668+ /// [`process_events`]: PeerManager::process_events
641669 pub fn read_event ( & self , peer_descriptor : & mut Descriptor , data : & [ u8 ] ) -> Result < bool , PeerHandleError > {
642670 match self . do_read_event ( peer_descriptor, data) {
643671 Ok ( res) => Ok ( res) ,
@@ -1085,7 +1113,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
10851113
10861114 /// Checks for any events generated by our handlers and processes them. Includes sending most
10871115 /// response messages as well as messages generated by calls to handler functions directly (eg
1088- /// functions like ChannelManager::process_pending_htlc_forward or send_payment).
1116+ /// functions like [`ChannelManager::process_pending_htlc_forward`] or [`send_payment`]).
1117+ ///
1118+ /// Will most likely call [`send_data`] on descriptors previously provided to
1119+ /// new_*\_connection. Thus, be very careful with reentrancy issues!
1120+ ///
1121+ /// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
1122+ /// [`ChannelManager::process_pending_htlc_forward`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forward
1123+ /// [`send_data`]: SocketDescriptor::send_data
10891124 pub fn process_events ( & self ) {
10901125 {
10911126 // TODO: There are some DoS attacks here where you can flood someone's outbound send
@@ -1297,10 +1332,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
12971332 }
12981333
12991334 /// Indicates that the given socket descriptor's connection is now closed.
1300- ///
1301- /// This need only be called if the socket has been disconnected by the peer or your own
1302- /// decision to disconnect it and may be skipped in any case where other parts of this library
1303- /// (eg PeerHandleError, explicit disconnect_socket calls) instruct you to disconnect the peer.
13041335 pub fn socket_disconnected ( & self , descriptor : & Descriptor ) {
13051336 self . disconnect_event_internal ( descriptor, false ) ;
13061337 }
@@ -1331,8 +1362,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
13311362 /// Set no_connection_possible to true to prevent any further connection with this peer,
13321363 /// force-closing any channels we have with it.
13331364 ///
1334- /// If a peer is connected, this will call `disconnect_socket` on the descriptor for the peer,
1335- /// so be careful about reentrancy issues.
1365+ /// If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
1366+ /// peer. Thus, be very careful about reentrancy issues.
1367+ ///
1368+ /// [`disconnect_socket`]: SocketDescriptor::disconnect_socket
13361369 pub fn disconnect_by_node_id ( & self , node_id : PublicKey , no_connection_possible : bool ) {
13371370 let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
13381371 if let Some ( mut descriptor) = peers_lock. node_id_to_descriptor . remove ( & node_id) {
@@ -1345,8 +1378,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
13451378
13461379 /// This function should be called roughly once every 30 seconds.
13471380 /// It will send pings to each peer and disconnect those which did not respond to the last round of pings.
1348-
1349- /// Will most likely call send_data on all of the registered descriptors, thus, be very careful with reentrancy issues!
1381+ ///
1382+ /// Will most likely call [`send_data`] all descriptors previously provided to
1383+ /// new_*\_connection. Thus, be very careful with reentrancy issues!
13501384 pub fn timer_tick_occurred ( & self ) {
13511385 let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
13521386 {
0 commit comments