@@ -28,6 +28,11 @@ pub type ToUnderlay = (PeerId, Vec<PacketMut>);
2828/// Packet batches received from an underlay.
2929pub type FromUnderlay = Vec < PacketMut > ;
3030
31+ /// A batch of disco packets received from an underlay transport.
32+ pub type DiscoBatch = Vec < PacketMut > ;
33+ /// A batch of stun packets received from an underlay transport.
34+ pub type StunBatch = Vec < PacketMut > ;
35+
3136/// Shorthand for a sender channel.
3237pub type Tx < T > = mpsc:: UnboundedSender < T > ;
3338
@@ -58,6 +63,11 @@ struct CoreState {
5863 overlay_transports : HashMap < OverlayTransportId , Tx < ToOverlay > > ,
5964 /// Queues to write packets to underlay transports.
6065 underlay_transports : HashMap < UnderlayTransportId , Tx < ToUnderlay > > ,
66+
67+ /// Send handle for disco packets received from underlays.
68+ disco_out : Tx < DiscoBatch > ,
69+ /// Send handle for stun packets received from underlays.
70+ stun_out : Tx < StunBatch > ,
6171}
6272
6373/// State that must be held during async polling.
@@ -73,13 +83,19 @@ impl DataPlane {
7383 ///
7484 /// The caller must configure overlay/underlay output queues for the data plane to be useful,
7585 /// otherwise all it can do is drop packets.
76- pub fn new ( my_key : NodeKeyPair ) -> Self {
86+ ///
87+ /// The second and third elements of the return tuple are output queues for disco and
88+ /// STUN messages, respectively.
89+ pub fn new ( my_key : NodeKeyPair ) -> ( Self , Rx < DiscoBatch > , Rx < StunBatch > ) {
7790 let ( overlay_up, overlay_down) = mpsc:: unbounded_channel ( ) ;
7891 let ( underlay_down, underlay_up) = mpsc:: unbounded_channel ( ) ;
7992
93+ let ( disco_tx, disco_rx) = mpsc:: unbounded_channel ( ) ;
94+ let ( stun_tx, stun_rx) = mpsc:: unbounded_channel ( ) ;
95+
8096 let sync = crate :: DataPlane :: new ( my_key) ;
8197
82- Self {
98+ let dp = Self {
8399 underlay_down,
84100 overlay_up,
85101
@@ -90,6 +106,8 @@ impl DataPlane {
90106
91107 core_state : Mutex :: new ( CoreState {
92108 sync,
109+ stun_out : stun_tx,
110+ disco_out : disco_tx,
93111 overlay_transports : Default :: default ( ) ,
94112 underlay_transports : Default :: default ( ) ,
95113 } ) ,
@@ -98,7 +116,9 @@ impl DataPlane {
98116 from_overlay : overlay_down,
99117 from_underlay : underlay_up,
100118 } ) ,
101- }
119+ } ;
120+
121+ ( dp, disco_rx, stun_rx)
102122 }
103123
104124 /// Allocate a new underlay transport.
@@ -230,7 +250,20 @@ impl DataPlane {
230250 ( Some ( to_peers) , Some ( loopback) )
231251 }
232252 SelectResult :: UnderlayUp ( underlay_up) => {
233- let InboundResult { to_local, to_peers } = core. sync . process_inbound ( underlay_up) ;
253+ let InboundResult {
254+ to_local,
255+ to_peers,
256+ disco,
257+ stun,
258+ } = core. sync . process_inbound ( underlay_up) ;
259+
260+ if !disco. is_empty ( ) && core. disco_out . send ( disco) . is_err ( ) {
261+ tracing:: warn!( "disco packets dropped: no receiver" ) ;
262+ }
263+
264+ if !stun. is_empty ( ) && core. stun_out . send ( stun) . is_err ( ) {
265+ tracing:: warn!( "stun packets dropped: no receiver" ) ;
266+ }
234267
235268 ( Some ( to_peers) , Some ( to_local) )
236269 }
0 commit comments