Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 412dbb3

Browse files
Event for user's NAT Device Type: Tell user if the node is behind an Easy or Hard NAT (#173)
* event for NAT device type Co-authored-by: Marten Seemann <[email protected]>
1 parent faf23c3 commit 412dbb3

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

event/nattype.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package event
2+
3+
import "github.com/libp2p/go-libp2p-core/network"
4+
5+
// EvtNATDeviceTypeChanged is an event struct to be emitted when the type of the NAT device changes for a Transport Protocol.
6+
//
7+
// Note: This event is meaningful ONLY if the AutoNAT Reachability is Private.
8+
// Consumers of this event should ALSO consume the `EvtLocalReachabilityChanged` event and interpret
9+
// this event ONLY if the Reachability on the `EvtLocalReachabilityChanged` is Private.
10+
type EvtNATDeviceTypeChanged struct {
11+
// TransportProtocol is the Transport Protocol for which the NAT Device Type has been determined.
12+
TransportProtocol network.NATTransportProtocol
13+
// NatDeviceType indicates the type of the NAT Device for the Transport Protocol.
14+
// Currently, it can be either a `Cone NAT` or a `Symmetric NAT`. Please see the detailed documentation
15+
// on `network.NATDeviceType` enumerations for a better understanding of what these types mean and
16+
// how they impact Connectivity and Hole Punching.
17+
NatDeviceType network.NATDeviceType
18+
}

network/nattype.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package network
2+
3+
// NATDeviceType indicates the type of the NAT device.
4+
type NATDeviceType int
5+
6+
const (
7+
// NATDeviceTypeUnknown indicates that the type of the NAT device is unknown.
8+
NATDeviceTypeUnknown NATDeviceType = iota
9+
10+
// NATDeviceTypeCone indicates that the NAT device is a Cone NAT.
11+
// A Cone NAT is a NAT where all outgoing connections from the same source IP address and port are mapped by the NAT device
12+
// to the same IP address and port irrespective of the destination address.
13+
// With regards to RFC 3489, this could be either a Full Cone NAT, a Restricted Cone NAT or a
14+
// Port Restricted Cone NAT. However, we do NOT differentiate between them here and simply classify all such NATs as a Cone NAT.
15+
// NAT traversal with hole punching is possible with a Cone NAT ONLY if the remote peer is ALSO behind a Cone NAT.
16+
// If the remote peer is behind a Symmetric NAT, hole punching will fail.
17+
NATDeviceTypeCone
18+
19+
// NATDeviceTypeSymmetric indicates that the NAT device is a Symmetric NAT.
20+
// A Symmetric NAT maps outgoing connections with different destination addresses to different IP addresses and ports,
21+
// even if they originate from the same source IP address and port.
22+
// NAT traversal with hole-punching is currently NOT possible in libp2p with Symmetric NATs irrespective of the remote peer's NAT type.
23+
NATDeviceTypeSymmetric
24+
)
25+
26+
func (r NATDeviceType) String() string {
27+
switch r {
28+
case 0:
29+
return "Unknown"
30+
case 1:
31+
return "Cone"
32+
case 2:
33+
return "Symmetric"
34+
default:
35+
return "unrecognized"
36+
}
37+
}
38+
39+
// NATTransportProtocol is the transport protocol for which the NAT Device Type has been determined.
40+
type NATTransportProtocol int
41+
42+
const (
43+
// NATTransportUDP means that the NAT Device Type has been determined for the UDP Protocol.
44+
NATTransportUDP NATTransportProtocol = iota
45+
// NATTransportTCP means that the NAT Device Type has been determined for the TCP Protocol.
46+
NATTransportTCP
47+
)
48+
49+
func (n NATTransportProtocol) String() string {
50+
switch n {
51+
case 0:
52+
return "UDP"
53+
case 1:
54+
return "TCP"
55+
default:
56+
return "unrecognized"
57+
}
58+
}

0 commit comments

Comments
 (0)