@@ -160,10 +160,15 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
160
160
CM :: Target : ChannelMessageHandler ,
161
161
RM :: Target : RoutingMessageHandler {
162
162
/// 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
164
166
pub chan_handler : CM ,
165
167
/// 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
167
172
pub route_handler : RM ,
168
173
}
169
174
@@ -173,26 +178,34 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
173
178
///
174
179
/// For efficiency, Clone should be relatively cheap for this type.
175
180
///
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.
181
191
pub trait SocketDescriptor : cmp:: Eq + hash:: Hash + Clone {
182
192
/// Attempts to send some data from the given slice to the peer.
183
193
///
184
194
/// 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.
187
197
///
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.
191
204
///
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.
196
209
fn send_data ( & mut self , data : & [ u8 ] , resume_read : bool ) -> usize ;
197
210
/// Disconnect the socket pointed to by this SocketDescriptor.
198
211
/// 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
309
322
/// helps with issues such as long function definitions.
310
323
pub 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 > ;
311
324
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.
314
336
///
315
337
/// Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
316
338
/// a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
@@ -397,8 +419,6 @@ impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor,
397
419
}
398
420
}
399
421
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.
402
422
impl < Descriptor : SocketDescriptor , CM : Deref , RM : Deref , L : Deref > PeerManager < Descriptor , CM , RM , L > where
403
423
CM :: Target : ChannelMessageHandler ,
404
424
RM :: Target : RoutingMessageHandler ,
@@ -458,8 +478,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
458
478
///
459
479
/// Returns a small number of bytes to send to the remote node (currently always 50).
460
480
///
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
463
485
pub fn new_outbound_connection ( & self , their_node_id : PublicKey , descriptor : Descriptor ) -> Result < Vec < u8 > , PeerHandleError > {
464
486
let mut peer_encryptor = PeerChannelEncryptor :: new_outbound ( their_node_id. clone ( ) , self . get_ephemeral_key ( ) ) ;
465
487
let res = peer_encryptor. get_act_one ( ) . to_vec ( ) ;
@@ -495,8 +517,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
495
517
/// call socket_disconnected for the new descriptor but must disconnect the connection
496
518
/// immediately.
497
519
///
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
500
524
pub fn new_inbound_connection ( & self , descriptor : Descriptor ) -> Result < ( ) , PeerHandleError > {
501
525
let peer_encryptor = PeerChannelEncryptor :: new_inbound ( & self . our_node_secret ) ;
502
526
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
604
628
///
605
629
/// May return an Err to indicate that the connection should be closed.
606
630
///
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
613
639
pub fn write_buffer_space_avail ( & self , descriptor : & mut Descriptor ) -> Result < ( ) , PeerHandleError > {
614
640
let mut peers = self . peers . lock ( ) . unwrap ( ) ;
615
641
match peers. peers . get_mut ( descriptor) {
@@ -631,13 +657,15 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
631
657
///
632
658
/// May return an Err to indicate that the connection should be closed.
633
659
///
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.
637
663
///
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).
640
666
///
667
+ /// [`send_data`]: SocketDescriptor::send_data
668
+ /// [`process_events`]: PeerManager::process_events
641
669
pub fn read_event ( & self , peer_descriptor : & mut Descriptor , data : & [ u8 ] ) -> Result < bool , PeerHandleError > {
642
670
match self . do_read_event ( peer_descriptor, data) {
643
671
Ok ( res) => Ok ( res) ,
@@ -1085,7 +1113,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
1085
1113
1086
1114
/// Checks for any events generated by our handlers and processes them. Includes sending most
1087
1115
/// 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
1089
1124
pub fn process_events ( & self ) {
1090
1125
{
1091
1126
// 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
1297
1332
}
1298
1333
1299
1334
/// 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.
1304
1335
pub fn socket_disconnected ( & self , descriptor : & Descriptor ) {
1305
1336
self . disconnect_event_internal ( descriptor, false ) ;
1306
1337
}
@@ -1331,8 +1362,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
1331
1362
/// Set no_connection_possible to true to prevent any further connection with this peer,
1332
1363
/// force-closing any channels we have with it.
1333
1364
///
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
1336
1369
pub fn disconnect_by_node_id ( & self , node_id : PublicKey , no_connection_possible : bool ) {
1337
1370
let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
1338
1371
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
1345
1378
1346
1379
/// This function should be called roughly once every 30 seconds.
1347
1380
/// 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!
1350
1384
pub fn timer_tick_occurred ( & self ) {
1351
1385
let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
1352
1386
{
0 commit comments