Skip to content

Commit cb11f07

Browse files
feat/606-enable-nat-traversal-via-hole-punching (#668)
* feat: base implementation of dcutr for hole-punching * chore: removed circuit-relay imports from __init__ * feat: implemented dcutr protocol * added test suite with mock setup * Fix pre-commit hook issues in DCUtR implementation * usages of CONNECT_TYPE and SYNC_TYPE have been replaced with HolePunch.Type.CONNECT and HolePunch.Type.SYNC * added unit tests for dcutr and nat module and * added multiaddr.get_peer_id() with proper DNS address handling and fixed method signature inconsistencies * added assertions to verify DCUtR hole punch result in integration test --------- Co-authored-by: Manu Sheel Gupta <[email protected]>
1 parent 9ed44f5 commit cb11f07

File tree

14 files changed

+2099
-1
lines changed

14 files changed

+2099
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ PB = libp2p/crypto/pb/crypto.proto \
6060
libp2p/identity/identify/pb/identify.proto \
6161
libp2p/host/autonat/pb/autonat.proto \
6262
libp2p/relay/circuit_v2/pb/circuit.proto \
63+
libp2p/relay/circuit_v2/pb/dcutr.proto \
6364
libp2p/kad_dht/pb/kademlia.proto
6465

6566
PY = $(PB:.proto=_pb2.py)

libp2p/abc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ def get_streams(self) -> tuple[INetStream, ...]:
357357
:return: A tuple containing instances of INetStream.
358358
"""
359359

360+
@abstractmethod
361+
def get_transport_addresses(self) -> list[Multiaddr]:
362+
"""
363+
Retrieve the transport addresses used by this connection.
364+
365+
:return: A list of multiaddresses used by the transport.
366+
"""
367+
360368

361369
# -------------------------- peermetadata interface.py --------------------------
362370

libp2p/network/connection/swarm_connection.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
TYPE_CHECKING,
44
)
55

6+
from multiaddr import Multiaddr
67
import trio
78

89
from libp2p.abc import (
@@ -147,6 +148,24 @@ async def new_stream(self) -> NetStream:
147148
def get_streams(self) -> tuple[NetStream, ...]:
148149
return tuple(self.streams)
149150

151+
def get_transport_addresses(self) -> list[Multiaddr]:
152+
"""
153+
Retrieve the transport addresses used by this connection.
154+
155+
Returns
156+
-------
157+
list[Multiaddr]
158+
A list of multiaddresses used by the transport.
159+
160+
"""
161+
# Return the addresses from the peerstore for this peer
162+
try:
163+
peer_id = self.muxed_conn.peer_id
164+
return self.swarm.peerstore.addrs(peer_id)
165+
except Exception as e:
166+
logging.warning(f"Error getting transport addresses: {e}")
167+
return []
168+
150169
def remove_stream(self, stream: NetStream) -> None:
151170
if stream not in self.streams:
152171
return

libp2p/relay/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
RelayLimits,
1616
RelayResourceManager,
1717
Reservation,
18+
DCUTR_PROTOCOL_ID,
19+
DCUtRProtocol,
20+
ReachabilityChecker,
21+
is_private_ip,
1822
)
1923

2024
__all__ = [
@@ -25,4 +29,9 @@
2529
"RelayLimits",
2630
"RelayResourceManager",
2731
"Reservation",
32+
"DCUtRProtocol",
33+
"DCUTR_PROTOCOL_ID",
34+
"ReachabilityChecker",
35+
"is_private_ip"
36+
2837
]

libp2p/relay/circuit_v2/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
https://github.com/libp2p/specs/blob/master/relay/circuit-v2.md
66
"""
77

8+
from .dcutr import (
9+
DCUtRProtocol,
10+
)
11+
from .dcutr import PROTOCOL_ID as DCUTR_PROTOCOL_ID
12+
13+
from .nat import (
14+
ReachabilityChecker,
15+
is_private_ip,
16+
)
17+
818
from .discovery import (
919
RelayDiscovery,
1020
)
@@ -29,4 +39,8 @@
2939
"RelayResourceManager",
3040
"CircuitV2Transport",
3141
"RelayDiscovery",
42+
"DCUtRProtocol",
43+
"DCUTR_PROTOCOL_ID",
44+
"ReachabilityChecker",
45+
"is_private_ip",
3246
]

0 commit comments

Comments
 (0)