mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-09-26 20:21:26 +08:00
network: rename NAT Types (#3331)
This commit is contained in:
@@ -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
|
||||
|
@@ -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"
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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()
|
||||
|
Reference in New Issue
Block a user