From 0d3793403385e4b6e88afbd58889571df66dfd27 Mon Sep 17 00:00:00 2001 From: sukun Date: Mon, 11 Aug 2025 19:21:00 +0530 Subject: [PATCH] network: rename NAT Types (#3331) --- core/event/nattype.go | 2 +- core/network/nattype.go | 49 +++++++++++++++++++-------- p2p/protocol/identify/obsaddr.go | 8 ++--- p2p/protocol/identify/obsaddr_test.go | 8 ++--- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/core/event/nattype.go b/core/event/nattype.go index 21ecdebd5..5ac4c525d 100644 --- a/core/event/nattype.go +++ b/core/event/nattype.go @@ -11,7 +11,7 @@ type EvtNATDeviceTypeChanged struct { // TransportProtocol is the Transport Protocol for which the NAT Device Type has been determined. TransportProtocol network.NATTransportProtocol // NatDeviceType indicates the type of the NAT Device for the Transport Protocol. - // Currently, it can be either a `Cone NAT` or a `Symmetric NAT`. Please see the detailed documentation + // Currently, it can be either a `EndpointIndependent NAT` or a `EndpointDependent NAT`. Please see the detailed documentation // on `network.NATDeviceType` enumerations for a better understanding of what these types mean and // how they impact Connectivity and Hole Punching. NatDeviceType network.NATDeviceType diff --git a/core/network/nattype.go b/core/network/nattype.go index bc95d6870..ab62886af 100644 --- a/core/network/nattype.go +++ b/core/network/nattype.go @@ -7,20 +7,39 @@ const ( // NATDeviceTypeUnknown indicates that the type of the NAT device is unknown. NATDeviceTypeUnknown NATDeviceType = iota - // NATDeviceTypeCone indicates that the NAT device is a Cone NAT. - // A Cone NAT is a NAT where all outgoing connections from the same source IP address and port are mapped by the NAT device - // to the same IP address and port irrespective of the destination address. - // With regards to RFC 3489, this could be either a Full Cone NAT, a Restricted Cone NAT or a - // Port Restricted Cone NAT. However, we do NOT differentiate between them here and simply classify all such NATs as a Cone NAT. - // NAT traversal with hole punching is possible with a Cone NAT ONLY if the remote peer is ALSO behind a Cone NAT. - // If the remote peer is behind a Symmetric NAT, hole punching will fail. - NATDeviceTypeCone + // NATDeviceTypeEndpointIndependent is a NAT device that maps addresses + // independent of the destination address. An EndpointIndependent NAT is + // a NAT where all outgoing connections from the same source IP address + // and port are mapped by the NAT device to the same IP address and port + // irrespective of the destination endpoint. + // + // NAT traversal with hole punching is possible with an + // EndpointIndependent NAT ONLY if the remote peer is ALSO behind an + // EndpointIndependent NAT. If the remote peer is behind an + // EndpointDependent NAT, hole punching will fail. + NATDeviceTypeEndpointIndependent - // NATDeviceTypeSymmetric indicates that the NAT device is a Symmetric NAT. - // A Symmetric NAT maps outgoing connections with different destination addresses to different IP addresses and ports, - // even if they originate from the same source IP address and port. - // NAT traversal with hole-punching is currently NOT possible in libp2p with Symmetric NATs irrespective of the remote peer's NAT type. - NATDeviceTypeSymmetric + // NATDeviceTypeEndpointDependent is a NAT device that maps addresses + // depending on the destination address. An EndpointDependent NAT maps + // outgoing connections with different destination addresses to + // different IP addresses and ports, even if they originate from the + // same source IP address and port. + // + // NAT traversal with hole-punching is currently NOT possible in libp2p + // with EndpointDependent NATs irrespective of the remote peer's NAT + // type. + NATDeviceTypeEndpointDependent +) + +const ( + // NATDeviceTypeCone is the same as endpoint independent + // + // Deprecated: Use NATDeviceTypeEndpointIndependent + NATDeviceTypeCone = NATDeviceTypeEndpointIndependent + // NATDeviceTypeSymmetric is the same as endpoint dependent + // + // Deprecated: Use NATDeviceTypeEndpointDependent + NATDeviceTypeSymmetric = NATDeviceTypeEndpointDependent ) func (r NATDeviceType) String() string { @@ -28,9 +47,9 @@ func (r NATDeviceType) String() string { case 0: return "Unknown" case 1: - return "Cone" + return "Endpoint Independent" case 2: - return "Symmetric" + return "Endpoint Dependent" default: return "unrecognized" } diff --git a/p2p/protocol/identify/obsaddr.go b/p2p/protocol/identify/obsaddr.go index 06e54bf5f..9c572dda1 100644 --- a/p2p/protocol/identify/obsaddr.go +++ b/p2p/protocol/identify/obsaddr.go @@ -580,16 +580,16 @@ func (o *ObservedAddrManager) getNATType() (tcpNATType, udpNATType network.NATDe // hole punching based on outputs of observed address manager will succeed if tcpTotal >= 3*maxExternalThinWaistAddrsPerLocalAddr { if tcpTopCounts >= tcpTotal/2 { - tcpNATType = network.NATDeviceTypeCone + tcpNATType = network.NATDeviceTypeEndpointIndependent } else { - tcpNATType = network.NATDeviceTypeSymmetric + tcpNATType = network.NATDeviceTypeEndpointDependent } } if udpTotal >= 3*maxExternalThinWaistAddrsPerLocalAddr { if udpTopCounts >= udpTotal/2 { - udpNATType = network.NATDeviceTypeCone + udpNATType = network.NATDeviceTypeEndpointIndependent } else { - udpNATType = network.NATDeviceTypeSymmetric + udpNATType = network.NATDeviceTypeEndpointDependent } } return diff --git a/p2p/protocol/identify/obsaddr_test.go b/p2p/protocol/identify/obsaddr_test.go index f2261c012..d405f3c7d 100644 --- a/p2p/protocol/identify/obsaddr_test.go +++ b/p2p/protocol/identify/obsaddr_test.go @@ -428,7 +428,7 @@ func TestObservedAddrManager(t *testing.T) { tcpNAT, udpNAT := o.getNATType() require.Equal(t, tcpNAT, network.NATDeviceTypeUnknown) - require.Equal(t, udpNAT, network.NATDeviceTypeCone) + require.Equal(t, udpNAT, network.NATDeviceTypeEndpointIndependent) }) t.Run("NATTypeSymmetric", func(t *testing.T) { o := newObservedAddrMgr() @@ -455,8 +455,8 @@ func TestObservedAddrManager(t *testing.T) { }, 1*time.Second, 100*time.Millisecond) tcpNAT, udpNAT := o.getNATType() - require.Equal(t, tcpNAT, network.NATDeviceTypeSymmetric) - require.Equal(t, udpNAT, network.NATDeviceTypeSymmetric) + require.Equal(t, tcpNAT, network.NATDeviceTypeEndpointDependent) + require.Equal(t, udpNAT, network.NATDeviceTypeEndpointDependent) for i := 0; i < N; i++ { o.removeConn(tcpConns[i]) @@ -518,7 +518,7 @@ func TestObservedAddrManager(t *testing.T) { } evt := e.(event.EvtNATDeviceTypeChanged) require.Equal(t, evt.TransportProtocol, network.NATTransportUDP) - require.Equal(t, evt.NatDeviceType, network.NATDeviceTypeCone) + require.Equal(t, evt.NatDeviceType, network.NATDeviceTypeEndpointIndependent) }) t.Run("Many connection many observations IP4 And IP6", func(t *testing.T) { o := newObservedAddrMgr()