diff --git a/storage/nat.nim b/storage/nat.nim index 9facf336..0189480f 100644 --- a/storage/nat.nim +++ b/storage/nat.nim @@ -49,6 +49,19 @@ type NatPortMapper* = ref object of RootObj plumInitialized: bool closed: bool +proc resetMappings(m: NatPortMapper) = + if m.tcpMappingId.isSome: + destroyMapping(m.tcpMappingId.get) + m.tcpMappingId = none(cint) + + if m.udpMappingId.isSome: + destroyMapping(m.udpMappingId.get) + m.udpMappingId = none(cint) + + m.activeMappingProtocol = none(MappingProtocol) + m.activeTcpPort = none(Port) + m.activeUdpPort = none(Port) + method mapNatPorts*( m: NatPortMapper ): Future[Option[(Port, Port, MappingProtocol)]] {. @@ -79,17 +92,7 @@ method mapNatPorts*( # If there is only one mapping, something went wrong somewhere # so we delete the mappings to recreate them. - if m.tcpMappingId.isSome: - destroyMapping(m.tcpMappingId.get) - m.tcpMappingId = none(cint) - - if m.udpMappingId.isSome: - destroyMapping(m.udpMappingId.get) - m.udpMappingId = none(cint) - - m.activeMappingProtocol = none(MappingProtocol) - m.activeTcpPort = none(Port) - m.activeUdpPort = none(Port) + m.resetMappings() let tcpRes = await createMapping(TCP, m.tcpPort.uint16, m.tcpPort.uint16) if tcpRes.isErr: @@ -111,17 +114,7 @@ method mapNatPorts*( some((m.activeTcpPort.get, m.activeUdpPort.get, m.activeMappingProtocol.get)) proc close*(m: NatPortMapper) = - if m.tcpMappingId.isSome: - destroyMapping(m.tcpMappingId.get) - m.tcpMappingId = none(cint) - - if m.udpMappingId.isSome: - destroyMapping(m.udpMappingId.get) - m.udpMappingId = none(cint) - - m.activeMappingProtocol = none(MappingProtocol) - m.activeTcpPort = none(Port) - m.activeUdpPort = none(Port) + m.resetMappings() if m.plumInitialized: discard cleanup() diff --git a/storage/storage.nim b/storage/storage.nim index ac32f0f2..20c1e018 100644 --- a/storage/storage.nim +++ b/storage/storage.nim @@ -61,7 +61,6 @@ type autonatService*: Option[AutonatV2Service] autoRelayService*: Option[AutoRelayService] natMapper*: Option[NatPortMapper] - natRouter*: Option[NatRouter] holePunchHandler: Option[connmanager.PeerEventHandler] peerInfoObserver: Option[PeerInfoObserver] bootstrapNodes: seq[SignedPeerRecord] @@ -539,7 +538,6 @@ proc new*( autonatService: autonatService, autoRelayService: autoRelayService, natMapper: natMapper, - natRouter: natRouter, holePunchHandler: holePunchHandler, peerInfoObserver: peerInfoObserver, bootstrapNodes: bootstrapNodes, diff --git a/storage/utils/addrutils.nim b/storage/utils/addrutils.nim index abc3de07..600a38f5 100644 --- a/storage/utils/addrutils.nim +++ b/storage/utils/addrutils.nim @@ -14,6 +14,7 @@ import std/strutils import std/options import pkg/libp2p +import pkg/stew/endians2 func remapAddr*( address: MultiAddress, @@ -47,28 +48,15 @@ func remapAddr*( MultiAddress.init(parts.join("/")).expect("Should construct multiaddress") -proc getMultiAddrWithIPAndUDPPort*(ip: IpAddress, port: Port): MultiAddress = - ## Creates a MultiAddress with the specified IP address and UDP port - ## - ## Parameters: - ## - ip: A valid IP address (IPv4 or IPv6) - ## - port: The UDP port number - ## - ## Returns: - ## A MultiAddress in the format "/ip4/
/udp/" or "/ip6/
/udp/" - - let ipFamily = if ip.family == IpAddressFamily.IPv4: "/ip4/" else: "/ip6/" - return MultiAddress.init(ipFamily & $ip & "/udp/" & $port).expect("valid multiaddr") - func getTcpPort*(ma: MultiAddress): Option[Port] = - let parts = ($ma).split("/") - for i, part in parts: - if part == "tcp" and i + 1 < parts.len: - try: - return some(Port(parseInt(parts[i + 1]))) - except ValueError: - return Port.none - Port.none + ## Extracts the TCP port from a multiaddress; none when there is no TCP part. + let tcpPart = ma[multiCodec("tcp")] + if tcpPart.isErr: + return Port.none + let portBytes = tcpPart.get().protoArgument() + if portBytes.isErr or portBytes.get().len != 2: + return Port.none + some(Port(fromBytesBE(uint16, portBytes.get()))) proc getMultiAddrWithIpAndTcpPort*(ip: IpAddress, port: Port): MultiAddress = ## Creates a MultiAddress with the specified IP address and TCP port diff --git a/storage/utils/natutils.nim b/storage/utils/natutils.nim index f96d4579..82d0edc8 100644 --- a/storage/utils/natutils.nim +++ b/storage/utils/natutils.nim @@ -1,15 +1,11 @@ {.push raises: [].} import std/[options, net] -import pkg/chronicles import results import libplum/plum import libplum/libplum export plum, libplum, results, options, net -logScope: - topics = "nat" - type NatStrategy* = enum NatAuto