mapping -> allocation

This commit is contained in:
Christian Ulrich 2020-11-20 19:28:53 +01:00
parent cb9a43ea65
commit 45cc3e0e34
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 8 additions and 8 deletions

View File

@ -56,13 +56,13 @@ proc getNatProperties*(localPort: uint16, probedPorts: seq[uint16]):
NatProperties = NatProperties =
if probedPorts.len == 0: if probedPorts.len == 0:
# No probed ports, so our only guess can be that the NAT is a cone-type NAT # No probed ports, so our only guess can be that the NAT is a cone-type NAT
# and the port mapping preserves the local Port. # and the port allocation preserves the local Port.
return NatProperties(natType: Unknown, guess: @[localPort]) return NatProperties(natType: Unknown, guess: @[localPort])
if probedPorts.len == 1: if probedPorts.len == 1:
# Only one server was used for probing, so we cannot know if the NAT is # Only one server was used for probing, so we cannot know if the NAT is
# symmetric or not. We are trying the probed port (assuming cone-type NAT) # symmetric or not. We are trying the probed port (assuming cone-type NAT)
# and the next port in a progressive sequence if applicable (assuming # and the next port in a progressive sequence if applicable (assuming
# symmetric NAT with progressive port mapping). # symmetric NAT with progressive port allocation).
result = NatProperties(natType: Unknown, guess: @[probedPorts[0]]) result = NatProperties(natType: Unknown, guess: @[probedPorts[0]])
if probedPorts[0] > localPort: if probedPorts[0] > localPort:
let offset = probedPorts[0] - localPort let offset = probedPorts[0] - localPort
@ -87,16 +87,16 @@ proc getNatProperties*(localPort: uint16, probedPorts: seq[uint16]):
maxDistance = max(maxDistance, distance) maxDistance = max(maxDistance, distance)
if maxDistance < 10: if maxDistance < 10:
if probedPorts.isSorted(Ascending): if probedPorts.isSorted(Ascending):
# assume symmetric NAT with positive-progressive port mapping # assume symmetric NAT with positive-progressive port allocation
return NatProperties(natType: SymmetricProgressive, return NatProperties(natType: SymmetricProgressive,
order: Ascending, previousPort: maxPort, order: Ascending, previousPort: maxPort,
minDistance: minDistance, maxDistance: maxDistance) minDistance: minDistance, maxDistance: maxDistance)
if probedPorts.isSorted(Descending): if probedPorts.isSorted(Descending):
# assume symmetric NAT with negative-progressive port mapping # assume symmetric NAT with negative-progressive port allocation
return NatProperties(natType: SymmetricProgressive, return NatProperties(natType: SymmetricProgressive,
order: Descending, previousPort: minPort, order: Descending, previousPort: minPort,
minDistance: minDistance, maxDistance: maxDistance) minDistance: minDistance, maxDistance: maxDistance)
# assume symmetric NAT with random port mapping # assume symmetric NAT with random port allocation
return NatProperties(natType: SymmetricRandom, minPort: minPort, return NatProperties(natType: SymmetricRandom, minPort: minPort,
maxPort: maxPort) maxPort: maxPort)
@ -272,7 +272,7 @@ suite "port prediction tests":
let predicted = predictPortRange(props) let predicted = predictPortRange(props)
check(predicted == @[Port(65533)]) check(predicted == @[Port(65533)])
test "random mapping": test "random port allocation":
let props = getNatProperties(1234'u16, @[20000'u16, 24000'u16]) let props = getNatProperties(1234'u16, @[20000'u16, 24000'u16])
check(props.natType == SymmetricRandom) check(props.natType == SymmetricRandom)
check(props.minPort == 20000'u16) check(props.minPort == 20000'u16)
@ -281,7 +281,7 @@ suite "port prediction tests":
let half = RandomPortCount div 2'u16 let half = RandomPortCount div 2'u16
check(predicted == toSeq(countup(22000'u16 - half, 22000'u16 + half - 1)).map(toPort)) check(predicted == toSeq(countup(22000'u16 - half, 22000'u16 + half - 1)).map(toPort))
test "random mapping, low": test "random port allocation":
let props = getNatProperties(1234'u16, @[1200'u16, 1600'u16]) let props = getNatProperties(1234'u16, @[1200'u16, 1600'u16])
check(props.natType == SymmetricRandom) check(props.natType == SymmetricRandom)
check(props.minPort == 1200'u16) check(props.minPort == 1200'u16)
@ -290,7 +290,7 @@ suite "port prediction tests":
check(predicted.len == RandomPortCount.int) check(predicted.len == RandomPortCount.int)
check(predicted == toSeq(countup(1024'u16, 1024'u16 + RandomPortCount - 1)).map(toPort)) check(predicted == toSeq(countup(1024'u16, 1024'u16 + RandomPortCount - 1)).map(toPort))
test "random mapping, high": test "random port allocation, high":
let props = getNatProperties(1234'u16, @[65000'u16, 65400'u16]) let props = getNatProperties(1234'u16, @[65000'u16, 65400'u16])
check(props.natType == SymmetricRandom) check(props.natType == SymmetricRandom)
check(props.minPort == 65000'u16) check(props.minPort == 65000'u16)