Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion core/event/nattype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 35 additions & 16 deletions core/network/nattype.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,49 @@ 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

// 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
// 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

// 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 {
switch r {
case 0:
return "Unknown"
case 1:
return "Cone"
return "Endpoint Independent"
case 2:
return "Symmetric"
return "Endpoint Dependent"
default:
return "unrecognized"
}
Expand Down
8 changes: 4 additions & 4 deletions p2p/protocol/identify/obsaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions p2p/protocol/identify/obsaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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])
Expand Down Expand Up @@ -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()
Expand Down
Loading