Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(autonatv2): Implement autonat v2 #1

Open
wants to merge 248 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
248 commits
Select commit Hold shift + click to select a range
326a2b9
Proposed API design
umgefahren Sep 27, 2023
25e6ccf
Implement proposed changes to the Transport trait.
umgefahren Sep 27, 2023
85ea10a
Merge branch 'master' into transport-redesign
umgefahren Sep 27, 2023
b90eac7
Merge branch 'master' into transport-redesign
umgefahren Sep 28, 2023
c9e33e1
Reworked proposed API changes
umgefahren Sep 28, 2023
dc47b08
Merge branch 'master' into transport-redesign
umgefahren Sep 28, 2023
461494b
Introduced listener presence structure. Allows for efficient detectio…
umgefahren Sep 29, 2023
309fad6
Merge remote-tracking branch 'origin/transport-redesign' into transpo…
umgefahren Sep 29, 2023
d6ea5bc
Continous updates and BTree
umgefahren Sep 29, 2023
9f6ffa5
Merge branch 'master' into transport-redesign
umgefahren Sep 29, 2023
9cc3ff5
Make for better testability
umgefahren Sep 29, 2023
5570b60
Nevermind let's use HashMap
umgefahren Sep 29, 2023
cfd8786
Resolved conflict
umgefahren Sep 29, 2023
31ad325
Little error
umgefahren Sep 29, 2023
dbd3277
Write more comments and switch to u16.
umgefahren Sep 29, 2023
6c11a8b
extremly reduced complexity
umgefahren Oct 4, 2023
9aa1df2
extremly reduced complexity and removed the now unnecessary files
umgefahren Oct 4, 2023
092c3ba
Merge branch 'master' into transport-redesign
umgefahren Oct 4, 2023
0fbe7e7
extremly reduced complexity and removed the now unnecessary files
umgefahren Oct 4, 2023
3a9e865
Merge branch 'master' into transport-redesign
umgefahren Oct 5, 2023
d7a0fb4
Merge into single function
umgefahren Oct 5, 2023
a33cb6b
Add first tests
umgefahren Oct 6, 2023
35b8002
I wrote so many tests i think i'm gonna be insane
umgefahren Oct 6, 2023
283c4e6
Make tests pass
umgefahren Oct 7, 2023
b7dec26
Remove major mistake
umgefahren Oct 7, 2023
842c8a0
Merge branch 'master' into transport-redesign
umgefahren Oct 9, 2023
9053dc7
Implement discussed changes
umgefahren Oct 11, 2023
1df8949
Merge branch 'master' into transport-redesign
umgefahren Oct 11, 2023
cb0558c
Merge branch 'master' into transport-redesign
umgefahren Oct 12, 2023
3487ae3
Implement port use propagation
umgefahren Oct 17, 2023
d9385de
Fix the behaviours
umgefahren Oct 17, 2023
7747bc1
Merge branch 'master' into transport-redesign
umgefahren Oct 18, 2023
2755696
Make the tests pass sometimes
umgefahren Oct 19, 2023
c4796ac
Transition TCP
umgefahren Oct 19, 2023
2856b6f
Format files
umgefahren Oct 19, 2023
3ccd2f7
Merge branch 'master' into transport-redesign
umgefahren Oct 19, 2023
ccd9025
Merge branch 'master' into transport-redesign
umgefahren Oct 20, 2023
99df328
Apply suggestions from code review
umgefahren Oct 20, 2023
0c2000e
Implement all suggestions
umgefahren Oct 20, 2023
49a0d3e
Remove some stuff, make tests pass
umgefahren Oct 21, 2023
a735f64
Merge branch 'master' into transport-redesign
umgefahren Oct 21, 2023
23207bd
Address another issue
umgefahren Oct 22, 2023
9597d53
Apply suggestions from code review
umgefahren Oct 23, 2023
6352e65
Implemented really the lateset review
umgefahren Oct 23, 2023
3f7249a
Merge branch 'master' into transport-redesign
umgefahren Oct 23, 2023
8e9d6ce
Merge branch 'master' into transport-redesign
umgefahren Oct 28, 2023
f33c3be
Merge branch 'master' into transport-redesign
umgefahren Oct 30, 2023
6cc5d7c
Implement the request response part of AutoNATv2
umgefahren Oct 30, 2023
6d44a1e
First steps of implementing the client
umgefahren Nov 7, 2023
81cd442
Extract ip-global and implemented lot of behaviour
umgefahren Nov 8, 2023
c0fd889
Intermediate commit
umgefahren Nov 18, 2023
4ac520d
Resolved conflict
umgefahren Nov 18, 2023
fadf904
WIP client
umgefahren Nov 19, 2023
60b4ccc
Run cargo fix
umgefahren Nov 19, 2023
71487fc
Correct minor things
umgefahren Nov 20, 2023
0da3753
Resolving merge conflict
umgefahren Nov 20, 2023
63b31e9
Implement suggestions
umgefahren Nov 20, 2023
546d2b2
Merged master
umgefahren Dec 1, 2023
c16235f
Remove gloabl only
umgefahren Dec 1, 2023
3bc107e
Initial server implementation
umgefahren Dec 1, 2023
50fb5e4
Pass the basic test for the first time.
umgefahren Dec 6, 2023
d34782e
Move forwards to fewer bug fixes
umgefahren Dec 6, 2023
5b489c6
Merge branch 'master' into transport-redesign
thomaseizinger Dec 6, 2023
d1bd57d
Resolve merge conflict
umgefahren Dec 6, 2023
f9075dc
Merge branch 'libp2p:master' into transport-redesign
umgefahren Dec 12, 2023
fee9de1
Make the things actually expire
umgefahren Dec 12, 2023
faa694a
Tests are working
umgefahren Dec 12, 2023
7d361ff
Merge branch 'master' into transport-redesign
umgefahren Dec 19, 2023
b01bae5
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Dec 19, 2023
6282cae
Implement in call suggested changes
umgefahren Dec 22, 2023
d711fe1
Merge branch 'master' into transport-redesign
umgefahren Dec 22, 2023
dab99d2
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Dec 22, 2023
da2a790
Implement all suggestions from the PR review
umgefahren Dec 22, 2023
b2bb8f3
Merge branch 'master' into transport-redesign
umgefahren Dec 27, 2023
be35e80
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Dec 27, 2023
101a325
Implement latest suggestions
umgefahren Dec 27, 2023
bf2662a
Implement old suggestions i overlooked
umgefahren Dec 27, 2023
efdbe35
Resolve conflict
umgefahren Dec 27, 2023
c7350f9
Port dns transport
umgefahren Dec 27, 2023
3079aa9
Port request response
umgefahren Dec 27, 2023
596bb06
Port websocket
umgefahren Dec 27, 2023
37cd309
Port allow block list and connection limits
umgefahren Dec 27, 2023
9eea564
Address more concerns
umgefahren Dec 29, 2023
9a85209
Move autonat down
umgefahren Dec 29, 2023
4399bab
Move autonatv2 to autonat
umgefahren Dec 29, 2023
663055e
Format code
thomaseizinger Dec 30, 2023
ba232cb
Alphabetically sort members list
thomaseizinger Dec 30, 2023
22b96b3
Undo changes to global-only transport
thomaseizinger Dec 30, 2023
a19d7c8
Make deprecation warning work
thomaseizinger Dec 30, 2023
46b8e84
Avoid diff in v1
thomaseizinger Dec 30, 2023
037aa99
Undo behaviour change in v1
thomaseizinger Dec 30, 2023
230ad08
Rename client config fields
thomaseizinger Dec 30, 2023
7cbf413
Streamline creation of `peer_info` state
thomaseizinger Dec 30, 2023
16b4d7c
Rewrite pending nonce handling
thomaseizinger Dec 30, 2023
fdc0c2d
A server might not support autonat on every connection
thomaseizinger Dec 30, 2023
a78c265
Prefer early-exit over `else`
thomaseizinger Dec 30, 2023
8f6c27a
Use shorthand field logging syntax
thomaseizinger Dec 30, 2023
170270a
Fix variable name
thomaseizinger Dec 30, 2023
589cf6c
Improve log message
thomaseizinger Dec 30, 2023
feea82f
Log error using `fmt::Display`
thomaseizinger Dec 30, 2023
8b0a544
Remove `server` field from `InternalStatusUpdate`
thomaseizinger Dec 30, 2023
86849e5
Loop instead of repeating code
thomaseizinger Dec 30, 2023
372d88d
Remove set that we never write to
thomaseizinger Dec 30, 2023
a2fcadd
Remove superflous tests
thomaseizinger Dec 30, 2023
90d7d07
Handle tick reset next to polling
thomaseizinger Dec 30, 2023
2a6ad94
Rewrite generation of dial requests
thomaseizinger Dec 30, 2023
b2d147f
Add docs and additional log statements for no candidates
thomaseizinger Dec 30, 2023
7e3da61
Add more docs and rename protocol name constants
thomaseizinger Dec 30, 2023
e2fe41d
Inline `DialBack` struct
thomaseizinger Dec 30, 2023
1e1aeca
Simplify handling of closed connection
thomaseizinger Dec 30, 2023
d5724c1
ConnectionIds are unique, meaning we can always just insert
thomaseizinger Dec 30, 2023
de91c55
Remove handling of `DialFailure`
thomaseizinger Dec 30, 2023
3719440
Disable connection for future autonat requests on error
thomaseizinger Dec 30, 2023
7b6d354
Remove unnecessary trait implementations
thomaseizinger Dec 30, 2023
8ce2a01
Add comment
thomaseizinger Dec 30, 2023
42ac03c
Rewrite dial-request to always index by nonce
thomaseizinger Dec 30, 2023
3181113
Reset TestStatus on errors
thomaseizinger Dec 30, 2023
0641b4b
Merge branch 'master' into transport-redesign
umgefahren Dec 30, 2023
c51f8cc
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Dec 30, 2023
88b1092
Merge pull request #2 from libp2p/autonatv2-thomas-followup
umgefahren Dec 30, 2023
4d96c33
Delay and use Result
umgefahren Dec 30, 2023
8a135ea
Perform sleep correct
umgefahren Dec 30, 2023
fac1196
Remove is_local
umgefahren Dec 30, 2023
9ed44d9
Remove the unnecessary check for confirmation
umgefahren Dec 30, 2023
a974707
Fix deprecation
umgefahren Dec 30, 2023
b04e9d5
Merge branch 'master' into transport-redesign
umgefahren Jan 28, 2024
117075f
Merge completed
umgefahren Jan 28, 2024
3f2476a
Implement first jan suggestions
umgefahren Jan 28, 2024
9254faa
Implement an example and a public tester
umgefahren Jan 30, 2024
4dd8db2
Merge branch 'master' into transport-redesign
umgefahren Feb 4, 2024
54e4c1f
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Feb 4, 2024
d723c50
Port quic
umgefahren Feb 4, 2024
daf2a0b
Make it compile
umgefahren Feb 5, 2024
6b85b82
Allow deprecated items
umgefahren Feb 5, 2024
e29e1c3
Merge branch 'master' into transport-redesign
umgefahren Feb 5, 2024
e6afb5a
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Feb 5, 2024
0b94a01
Make the tests run
umgefahren Feb 5, 2024
a09e26d
Implement the latest changes to the protocol
umgefahren Feb 14, 2024
61ae32e
Fix minor nits
umgefahren Feb 14, 2024
ad6ad8f
Implement DialBackOk correctly
umgefahren Feb 14, 2024
fbdc85f
Format
umgefahren Feb 14, 2024
553511c
Fix issues raised by clippy
umgefahren Feb 14, 2024
9bf52d4
Update protocols/autonat/src/v2/client/behaviour.rs
umgefahren Feb 19, 2024
70f41f3
Update protocols/autonat/src/v2/client/handler/dial_back.rs
umgefahren Feb 19, 2024
0773694
Update protocols/autonat/src/v2/client/handler/dial_back.rs
umgefahren Feb 19, 2024
89acd28
Make error richer and trace it at a different point
umgefahren Feb 19, 2024
d571e51
Avoid stream close
umgefahren Feb 19, 2024
10e4cee
Better error handling
umgefahren Feb 19, 2024
d4d671e
Correct one failing CI and change the license
umgefahren Feb 19, 2024
7617c5d
Merge branch 'master' into transport-redesign
umgefahren Feb 19, 2024
2dc574e
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Feb 19, 2024
330fc51
Correct minor things
umgefahren Mar 6, 2024
952cf54
Merge branch 'master' into transport-redesign
umgefahren Mar 6, 2024
5f6f2f6
Resolve merge conflict
umgefahren Mar 6, 2024
367273a
Add quic and dns to the example
umgefahren Mar 7, 2024
d79bca3
Remove unused import
umgefahren Mar 7, 2024
8df0183
Change dial opts to make wasm tests pass
umgefahren Mar 7, 2024
5d24d03
Remove futures-time to compile on wasm
umgefahren Mar 7, 2024
41804be
Fix concerns by clippy
umgefahren Mar 7, 2024
bd495f9
Format
umgefahren Mar 7, 2024
38be2b4
Use workaround to make generated code CI pass
umgefahren Mar 7, 2024
ce7596c
Correct webtransport test
umgefahren Mar 7, 2024
0506c7b
Handle strange variable unused on windows
umgefahren Mar 7, 2024
0b6dbb1
Remove clippy nit
umgefahren Mar 7, 2024
d2fddd5
Simplify a little bit and get rid of every performance opt
umgefahren Mar 7, 2024
8f1fcbb
Version bumps and added author
umgefahren Mar 8, 2024
8095e84
Upstream transport changes
umgefahren Mar 8, 2024
0d5dcba
Merge branch 'master' into transport-redesign
umgefahren Apr 30, 2024
a1ae4e9
Update protocols/autonat/src/v2/client/handler/dial_request.rs
umgefahren Apr 30, 2024
e640920
Resolve merge conflict
umgefahren Apr 30, 2024
480337e
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Apr 30, 2024
a84c998
Format
umgefahren Apr 30, 2024
1d6727f
Merge branch 'master' into transport-redesign
umgefahren May 8, 2024
0dc5003
Resolve concern
umgefahren May 8, 2024
0918ea8
Remove deprecated
umgefahren May 8, 2024
b07a463
Merge from master
umgefahren May 8, 2024
0724086
Merge branch 'transport-redesign' into autonat-v2-transport-changes
umgefahren May 8, 2024
45863c3
Formatting
umgefahren May 8, 2024
e7dd2ec
Merge branch 'master' into implement-autonat-v2
umgefahren May 8, 2024
7f389b6
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren May 8, 2024
a38d35f
Merge pull request #3 from umgefahren/autonat-v2-transport-changes
umgefahren May 14, 2024
86650bd
Don't close stream
umgefahren May 14, 2024
5c2658a
Merge master
umgefahren May 14, 2024
dd39a16
Merge master
umgefahren May 14, 2024
e41f667
identify detects if address translation is required
stormshield-ebzh May 16, 2024
235447b
homogeneize fully-p2p-qualified addresses to avoid double probe
stormshield-ebzh May 16, 2024
6103cd8
feat(nat): get rid of Transport::address_translation
stormshield-ebzh May 22, 2024
4b16e42
Thomas remarks
stormshield-ebzh May 23, 2024
0f538f3
removed Transport::address_translation and moved generic address_tran…
stormshield-ebzh May 23, 2024
7bea1c2
Merge pull request #4 from stormshield-ebzh/transport-redesign-addres…
umgefahren Jun 1, 2024
efd5882
Update lock file
umgefahren Jun 4, 2024
0c750ef
Update transport to always use a new endpoint when dialing without reuse
umgefahren Jun 4, 2024
7c78fee
Merge master
umgefahren Jun 4, 2024
aa7af5b
No reference in observed
umgefahren Jun 4, 2024
51b1333
Remove now defunct test
umgefahren Jun 4, 2024
4c3382f
Add changelog to core
umgefahren Jun 4, 2024
3764d10
Remove expected output /p2p/
umgefahren Jun 4, 2024
f506edd
Port reuse test
umgefahren Jun 4, 2024
64b0136
Merge branch 'master' into transport-redesign
umgefahren Jun 4, 2024
9458749
I don't need Protocol anymore
umgefahren Jun 4, 2024
20319cf
Restore doc test
umgefahren Jun 4, 2024
8b136cf
Add CHANGELOG entries
umgefahren Jun 4, 2024
aaa8c7c
Update versions in root Cargo.toml
umgefahren Jun 4, 2024
725c564
Bump autonat version
umgefahren Jun 4, 2024
1253ee3
Update versions (again)
umgefahren Jun 4, 2024
9d84b7c
Merge latest changes
umgefahren Jun 4, 2024
df2592b
Correct bad merge
umgefahren Jun 4, 2024
75e8011
Apply suggestions from code review
umgefahren Jun 6, 2024
6e0839d
Address review comments
umgefahren Jun 9, 2024
f266619
Merge master
umgefahren Jun 9, 2024
158c017
Remove HEAD
umgefahren Jun 9, 2024
1dc9f4b
Format
umgefahren Jun 9, 2024
3dbc6c9
Make compilable
umgefahren Jun 9, 2024
0b20c21
Correct the webrtc-utils version
umgefahren Jun 9, 2024
269783c
Merge branch 'master' into transport-redesign
umgefahren Jun 11, 2024
7fc5546
Address @thomaseizinger comments
umgefahren Jun 11, 2024
cba6fb9
Revert bad changes from #9d84b7c
umgefahren Jun 11, 2024
09204d4
Revert bad changes from #9d84b7c
umgefahren Jun 11, 2024
cba4113
Fix clippy issues
umgefahren Jun 11, 2024
6a632b5
Don't format
umgefahren Jun 14, 2024
0be96af
Merge branch 'master' into transport-redesign
umgefahren Jun 14, 2024
3359e60
Correct bad merge
umgefahren Jun 14, 2024
fc14ba8
Delete unused files
umgefahren Jun 14, 2024
73eddc7
Fix lock
umgefahren Jun 14, 2024
03c2717
Detect if local host supports ipv6
umgefahren Jun 14, 2024
236b356
Revert "Detect if local host supports ipv6"
umgefahren Jun 14, 2024
b0adbd7
Detect reachability through catching the error
umgefahren Jun 14, 2024
4fac6da
Compile on wasm and make clippy happy
umgefahren Jun 14, 2024
ff806a3
Switch to protobuf version 3
umgefahren Jun 14, 2024
75a86c2
Revert network unreachable detection
umgefahren Jun 16, 2024
73584cd
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 3, 2024
5fb6715
Fix issues after merge
umgefahren Aug 3, 2024
0a566d4
Add changelog entry
umgefahren Aug 4, 2024
3e85ffb
Add documentation
umgefahren Aug 4, 2024
531e8df
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 5, 2024
921f5f1
Undo a bad merge and update changelog for silent roll out
umgefahren Aug 5, 2024
bd76259
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 5, 2024
bf45de8
Update protocols/autonat/CHANGELOG.md
umgefahren Aug 6, 2024
ea419f7
Update protocols/autonat/CHANGELOG.md
umgefahren Aug 6, 2024
6d1b431
this file is useless
umgefahren Aug 6, 2024
431d7b6
Trigger CI
umgefahren Aug 6, 2024
475c76b
Include doc cfg
umgefahren Aug 6, 2024
80981d9
Trigger CI
umgefahren Aug 6, 2024
c5a21f3
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 6, 2024
61b1f2d
Correct bad merge
umgefahren Aug 6, 2024
75646ca
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 7, 2024
123c3a4
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 8, 2024
ec56cbc
Merge branch 'master' into implement-autonat-v2
mergify[bot] Aug 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"misc/allow-block-list",
"misc/connection-limits",
"misc/futures-bounded",
"misc/ip-global",
"misc/keygen",
"misc/memory-connection-limits",
"misc/metrics",
Expand All @@ -34,6 +35,7 @@ members = [
"muxers/test-harness",
"muxers/yamux",
"protocols/autonat",
"protocols/autonatv2",
"protocols/dcutr",
"protocols/floodsub",
"protocols/gossipsub",
Expand Down Expand Up @@ -87,20 +89,16 @@ libp2p-mdns = { version = "0.45.0", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.14.0", path = "misc/metrics" }
libp2p-mplex = { version = "0.41.0", path = "muxers/mplex" }
libp2p-muxer-test-harness = { path = "muxers/test-harness" }
libp2p-noise = { version = "0.44.0", path = "transports/noise" }
libp2p-perf = { version = "0.3.0", path = "protocols/perf" }
libp2p-ping = { version = "0.44.0", path = "protocols/ping" }
libp2p-plaintext = { version = "0.41.0", path = "transports/plaintext" }
libp2p-pnet = { version = "0.24.0", path = "transports/pnet" }
libp2p-quic = { version = "0.10.0", path = "transports/quic" }
libp2p-relay = { version = "0.17.0", path = "protocols/relay" }
libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.0", path = "protocols/request-response" }
libp2p-server = { version = "0.12.3", path = "misc/server" }
libp2p-swarm = { version = "0.44.0", path = "swarm" }
libp2p-swarm-derive = { version = "0.34.0", path = "swarm-derive" }
libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" }
libp2p-tcp = { version = "0.41.0", path = "transports/tcp" }
libp2p-tls = { version = "0.3.0", path = "transports/tls" }
libp2p-uds = { version = "0.40.0", path = "transports/uds" }
Expand All @@ -119,6 +117,7 @@ prometheus-client = "0.22.0"
quick-protobuf-codec = { version = "0.2.0", path = "misc/quick-protobuf-codec" }
quickcheck = { package = "quickcheck-ext", path = "misc/quickcheck-ext" }
rw-stream-sink = { version = "0.4.0", path = "misc/rw-stream-sink" }
ip-global = { version = "0.1.0", path = "misc/ip-global" }

[patch.crates-io]

Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fnv = "1.0"
futures = { version = "0.3.29", features = ["executor", "thread-pool"] }
futures-timer = "3"
instant = "0.1.12"
ip-global = { workspace = true }
libp2p-identity = { workspace = true, features = ["peerid", "ed25519"] }
log = "0.4"
multiaddr = { workspace = true }
Expand Down
13 changes: 12 additions & 1 deletion core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::multiaddr::{Multiaddr, Protocol};
use crate::{
multiaddr::{Multiaddr, Protocol},
transport::PortUse,
};

/// The endpoint roles associated with a peer-to-peer communication channel.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -80,6 +83,13 @@ pub enum ConnectedPoint {
/// connection as a dialer and one peer dial the other and upgrade the
/// connection _as a listener_ overriding its role.
role_override: Endpoint,
/// Whether the port for the outgoing connection was reused from a listener
/// or a new port was allocated. This is useful for address translation.
///
/// The port use is implemented on a best-effort basis. It is not guaranteed
/// that [`PortUse::Reuse`] actually reused a port. A good example is the case
/// where there is no listener available to reuse a port from.
port_use: PortUse,
},
/// We received the node.
Listener {
Expand Down Expand Up @@ -133,6 +143,7 @@ impl ConnectedPoint {
ConnectedPoint::Dialer {
address,
role_override: _,
port_use: _,
} => address,
ConnectedPoint::Listener { local_addr, .. } => local_addr,
}
Expand Down
29 changes: 6 additions & 23 deletions core/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// DEALINGS IN THE SOFTWARE.

use crate::muxing::StreamMuxerEvent;
use crate::transport::DialOpts;
use crate::{
muxing::StreamMuxer,
transport::{ListenerId, Transport, TransportError, TransportEvent},
Expand Down Expand Up @@ -172,37 +173,19 @@ where
}
}

fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
use TransportError::*;
match self {
Either::Left(a) => match a.dial(addr) {
Ok(connec) => Ok(EitherFuture::First(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(Either::Left(err))),
},
Either::Right(b) => match b.dial(addr) {
Ok(connec) => Ok(EitherFuture::Second(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(Either::Right(err))),
},
}
}

fn dial_as_listener(
fn dial(
&mut self,
addr: Multiaddr,
) -> Result<Self::Dial, TransportError<Self::Error>>
where
Self: Sized,
{
opts: DialOpts,
) -> Result<Self::Dial, TransportError<Self::Error>> {
use TransportError::*;
match self {
Either::Left(a) => match a.dial_as_listener(addr) {
Either::Left(a) => match a.dial(addr, opts) {
Ok(connec) => Ok(EitherFuture::First(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(Either::Left(err))),
},
Either::Right(b) => match b.dial_as_listener(addr) {
Either::Right(b) => match b.dial(addr, opts) {
Ok(connec) => Ok(EitherFuture::Second(connec)),
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
Err(Other(err)) => Err(Other(Either::Right(err))),
Expand Down
36 changes: 27 additions & 9 deletions core/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub mod upgrade;
mod boxed;
mod optional;

use crate::ConnectedPoint;
use crate::{ConnectedPoint, Endpoint};

pub use self::boxed::Boxed;
pub use self::choice::OrTransport;
Expand All @@ -58,6 +58,30 @@ pub use self::upgrade::Upgrade;

static NEXT_LISTENER_ID: AtomicUsize = AtomicUsize::new(1);

/// The port use policy for a new connection.
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash)]
pub enum PortUse {
/// Always allocate a new port for the dial.
New,
/// Best effor reusing of an existing port.
///
/// If there is no listener present that can be used to dial, a new port is allocated.
#[default]
Reuse,
}

/// Options to customize the behaviour during dialing.
#[derive(Debug, Copy, Clone)]
pub struct DialOpts {
/// The endpoint establishing a new connection.
///
/// When attempting a hole-punch, both parties simultaneously "dial" each other but one party has to be the "listener" on the final connection.
/// This option specifies the role of this node in the final connection.
pub role: Endpoint,
/// The port use policy for a new connection.
pub port_use: PortUse,
}

/// A transport provides connection-oriented communication between two peers
/// through ordered streams of data (i.e. connections).
///
Expand Down Expand Up @@ -129,16 +153,10 @@ pub trait Transport {
///
/// If [`TransportError::MultiaddrNotSupported`] is returned, it may be desirable to
/// try an alternative [`Transport`], if available.
fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>>;

/// As [`Transport::dial`] but has the local node act as a listener on the outgoing connection.
///
/// This option is needed for NAT and firewall hole punching.
///
/// See [`ConnectedPoint::Dialer`] for related option.
fn dial_as_listener(
fn dial(
&mut self,
addr: Multiaddr,
opts: DialOpts,
) -> Result<Self::Dial, TransportError<Self::Error>>;

/// Poll for [`TransportEvent`]s.
Expand Down
31 changes: 7 additions & 24 deletions core/src/transport/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

Check failure on line 20 in core/src/transport/and_then.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/core/src/transport/and_then.rs
use crate::{
connection::{ConnectedPoint, Endpoint},
transport::{ListenerId, Transport, TransportError, TransportEvent},
connection::ConnectedPoint,
transport::{ListenerId, Transport, TransportError, TransportEvent, DialOpts},
};
use either::Either;
use futures::prelude::*;
Expand Down Expand Up @@ -68,40 +68,23 @@
self.transport.remove_listener(id)
}

fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
let dialed_fut = self
.transport
.dial(addr.clone())
.map_err(|err| err.map(Either::Left))?;
let future = AndThenFuture {
inner: Either::Left(Box::pin(dialed_fut)),
args: Some((
self.fun.clone(),
ConnectedPoint::Dialer {
address: addr,
role_override: Endpoint::Dialer,
},
)),
_marker: PhantomPinned,
};
Ok(future)
}

fn dial_as_listener(
fn dial(
&mut self,
addr: Multiaddr,
opts: DialOpts,
) -> Result<Self::Dial, TransportError<Self::Error>> {
let dialed_fut = self
.transport
.dial_as_listener(addr.clone())
.dial(addr.clone(), opts)
.map_err(|err| err.map(Either::Left))?;
let future = AndThenFuture {
inner: Either::Left(Box::pin(dialed_fut)),
args: Some((
self.fun.clone(),
ConnectedPoint::Dialer {
address: addr,
role_override: Endpoint::Listener,
role_override: opts.role,
port_use: opts.port_use,
},
)),
_marker: PhantomPinned,
Expand Down
33 changes: 15 additions & 18 deletions core/src/transport/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::transport::{ListenerId, Transport, TransportError, TransportEvent};
use crate::transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent};
use futures::{prelude::*, stream::FusedStream};
use multiaddr::Multiaddr;
use std::{
Expand Down Expand Up @@ -58,8 +58,11 @@ trait Abstract<O> {
addr: Multiaddr,
) -> Result<(), TransportError<io::Error>>;
fn remove_listener(&mut self, id: ListenerId) -> bool;
fn dial(&mut self, addr: Multiaddr) -> Result<Dial<O>, TransportError<io::Error>>;
fn dial_as_listener(&mut self, addr: Multiaddr) -> Result<Dial<O>, TransportError<io::Error>>;
fn dial(
&mut self,
addr: Multiaddr,
opts: DialOpts,
) -> Result<Dial<O>, TransportError<io::Error>>;
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>;
fn poll(
self: Pin<&mut Self>,
Expand All @@ -86,15 +89,12 @@ where
Transport::remove_listener(self, id)
}

fn dial(&mut self, addr: Multiaddr) -> Result<Dial<O>, TransportError<io::Error>> {
let fut = Transport::dial(self, addr)
.map(|r| r.map_err(box_err))
.map_err(|e| e.map(box_err))?;
Ok(Box::pin(fut) as Dial<_>)
}

fn dial_as_listener(&mut self, addr: Multiaddr) -> Result<Dial<O>, TransportError<io::Error>> {
let fut = Transport::dial_as_listener(self, addr)
fn dial(
&mut self,
addr: Multiaddr,
opts: DialOpts,
) -> Result<Dial<O>, TransportError<io::Error>> {
let fut = Transport::dial(self, addr, opts)
.map(|r| r.map_err(box_err))
.map_err(|e| e.map(box_err))?;
Ok(Box::pin(fut) as Dial<_>)
Expand Down Expand Up @@ -143,15 +143,12 @@ impl<O> Transport for Boxed<O> {
self.inner.remove_listener(id)
}

fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
self.inner.dial(addr)
}

fn dial_as_listener(
fn dial(
&mut self,
addr: Multiaddr,
opts: DialOpts,
) -> Result<Self::Dial, TransportError<Self::Error>> {
self.inner.dial_as_listener(addr)
self.inner.dial(addr, opts)
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
Expand Down
Loading
Loading