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