Skip to content

Commit 31bc937

Browse files
committed
fix(autonat): close dial-back connections once probes complete
1 parent cb1450d commit 31bc937

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

protocols/autonat/CHANGELOG.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 0.16.0
22

3+
- Close server dial-back connections once the probe completes instead of leaving them open.
4+
See [PR 6528](https://github.com/libp2p/rust-libp2p/pull/6528).
5+
36
- Use `futures-timer` instead of tokio's timer for stream timeouts so the bounded `Delay` works on
47
`wasm32`; tokio's timer has no driver in the browser and panics at runtime.
58
See [PR 6488](https://github.com/libp2p/rust-libp2p/pull/6488).
@@ -32,19 +35,20 @@
3235
## 0.13.0
3336

3437
- Due to the refactor of `Transport` it's no longer required to create a separate transport for
35-
AutoNAT where port reuse is disabled. This information is now passed by the behaviour.
38+
AutoNAT where port reuse is disabled. This information is now passed by the behaviour.
3639
See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568).
3740
- Introduce the new AutoNATv2 protocol.
3841
It's split into a client and a server part, represented in their respective modules
3942
Features:
4043
- The server now always dials back over a newly allocated port.
4144
This more accurately reflects the reachability state for other peers and avoids accidental hole punching.
4245
- The server can now test addresses different from the observed address (i.e., the connection to the server was made through a `p2p-circuit`). To mitigate against DDoS attacks, the client has to send more data to the server than the dial-back costs.
43-
See [PR 5526](https://github.com/libp2p/rust-libp2p/pull/5526).
46+
See [PR 5526](https://github.com/libp2p/rust-libp2p/pull/5526).
4447

4548
<!-- Update to libp2p-swarm v0.45.0 -->
4649

4750
## 0.12.1
51+
4852
- Use `web-time` instead of `instant`.
4953
See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347).
5054

@@ -92,7 +96,6 @@ AutoNAT where port reuse is disabled. This information is now passed by the beha
9296

9397
[PR 3351]: https://github.com/libp2p/rust-libp2p/pull/3351
9498

95-
9699
## 0.9.0
97100

98101
- Update to `libp2p-core` `v0.38.0`.
@@ -177,7 +180,7 @@ AutoNAT where port reuse is disabled. This information is now passed by the beha
177180

178181
- Update to `libp2p-request-response` `v0.16.0`.
179182

180-
- Merge NetworkBehaviour's inject_\* paired methods (see PR 2445).
183+
- Merge NetworkBehaviour's inject\_\* paired methods (see PR 2445).
181184

182185
[PR 2445]: https://github.com/libp2p/rust-libp2p/pull/2445
183186

protocols/autonat/src/v1/behaviour.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use libp2p_request_response::{
3939
self as request_response, InboundRequestId, OutboundRequestId, ProtocolSupport, ResponseChannel,
4040
};
4141
use libp2p_swarm::{
42-
ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, THandler, THandlerInEvent,
43-
THandlerOutEvent, ToSwarm,
42+
CloseConnection, ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, THandler,
43+
THandlerInEvent, THandlerOutEvent, ToSwarm,
4444
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
4545
};
4646
use web_time::Instant;
@@ -349,6 +349,15 @@ impl Behaviour {
349349
if let Some(event) = self.as_server().on_outbound_connection(&peer, address) {
350350
self.pending_actions
351351
.push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event)));
352+
// This connection was dialed solely to probe the peer's reachability;
353+
// the probe is resolved and the response is sent over the connection
354+
// the peer dialed us on. Close the probe connection so it does not
355+
// linger alongside the peer's primary connection and hold on to
356+
// transport resources.
357+
self.pending_actions.push_back(ToSwarm::CloseConnection {
358+
peer_id: peer,
359+
connection: CloseConnection::One(conn),
360+
});
352361
}
353362
}
354363
ConnectedPoint::Dialer {

protocols/autonat/src/v2/server/behaviour.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use either::Either;
88
use libp2p_core::{Endpoint, Multiaddr, transport::PortUse};
99
use libp2p_identity::PeerId;
1010
use libp2p_swarm::{
11-
ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, FromSwarm, NetworkBehaviour,
12-
ToSwarm,
11+
CloseConnection, ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, FromSwarm,
12+
NetworkBehaviour, ToSwarm,
1313
dial_opts::{DialOpts, PeerCondition},
1414
dummy,
1515
};
@@ -102,13 +102,23 @@ where
102102
fn on_connection_handler_event(
103103
&mut self,
104104
peer_id: PeerId,
105-
_connection_id: ConnectionId,
105+
connection_id: ConnectionId,
106106
event: <Handler<R> as ConnectionHandler>::ToBehaviour,
107107
) {
108108
match event {
109-
Either::Left(Either::Left(Ok(_))) => {}
110-
Either::Left(Either::Left(Err(e))) => {
111-
tracing::debug!("dial back error: {e:?}");
109+
Either::Left(Either::Left(result)) => {
110+
if let Err(e) = result {
111+
tracing::debug!("dial back error: {e:?}");
112+
}
113+
// This connection was dialed solely to perform the dial-back, which
114+
// has now run to completion; the outcome reaches the client over the
115+
// connection it dialed us on. Close the dial-back connection so it
116+
// does not linger alongside the client's primary connection and hold
117+
// on to transport resources.
118+
self.pending_events.push_back(ToSwarm::CloseConnection {
119+
peer_id,
120+
connection: CloseConnection::One(connection_id),
121+
});
112122
}
113123
Either::Left(Either::Right(v)) => libp2p_core::util::unreachable(v),
114124
Either::Right(Either::Left(cmd)) => {

0 commit comments

Comments
 (0)