From 7e2d3e213fcd69cd50c4d87170fec4fc66b26ecf Mon Sep 17 00:00:00 2001 From: cheatfate Date: Sat, 29 Dec 2018 14:44:27 +0200 Subject: [PATCH] Add isWire() procedure for MultiAddress. --- libp2p/multiaddress.nim | 35 ++++++++++++++++++++++++++++++++++- tests/testmultiaddress.nim | 31 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 27e016304..9b859297a 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -400,7 +400,7 @@ proc protoName*(ma: MultiAddress): string = proc protoArgument*(ma: MultiAddress, value: var openarray[byte]): int = ## Returns MultiAddress ``ma`` protocol argument value. - ## + ## ## If current MultiAddress do not have argument value, then result will be ## ``0``. var header: uint64 @@ -672,3 +672,36 @@ proc `&=`*(m1: var MultiAddress, m2: MultiAddress) = m1.data.buffer &= m2.data.buffer if not m1.validate(): raise newException(MultiAddressError, "Incorrect MultiAddress!") + +proc isWire*(ma: MultiAddress): bool = + ## Returns ``true`` if MultiAddress ``ma`` is one of: + ## - {IP4}/{TCP, UDP} + ## - {IP6}/{TCP, UDP} + ## - {UNIX}/{PATH} + var state = 0 + try: + for part in ma.items(): + if state == 0: + let code = part.protoCode() + if code == multiCodec("ip4") or code == multiCodec("ip6"): + inc(state) + continue + elif code == multiCodec("unix"): + result = true + break + else: + result = false + break + elif state == 1: + if part.protoCode == multiCodec("tcp") or + part.protoCode == multiCodec("udp"): + inc(state) + result = true + else: + result = false + break + else: + result = false + break + except: + result = false diff --git a/tests/testmultiaddress.nim b/tests/testmultiaddress.nim index 0f6611b82..d2cd16837 100644 --- a/tests/testmultiaddress.nim +++ b/tests/testmultiaddress.nim @@ -170,6 +170,29 @@ const "/p2p-circuit/50" ] + UtilitySuccessVectors = [ + "/ip4/127.0.0.1/tcp/1024", + "/ip4/127.0.0.1/udp/1024", + "/ip4/0.0.0.0/tcp/1024", + "/ip4/0.0.0.0/udp/1024", + "/ip4/255.255.255.255/tcp/65535", + "/ip4/255.255.255.255/udp/65535", + "/ip6/::1/tcp/1024", + "/ip6/::1/udp/1024", + "/ip6/::/tcp/65535", + "/ip6/::/udp/65535", + "/ip6/::/udp/65535", + "/unix/tmp/test.socket" + ] + + UtilityFailVectors = [ + "/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", + "/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234", + "/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", + "/p2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", + "/ip4/127.0.0.1/udp/1234/quic" + ] + suite "MultiAddress test suite": test "go-multiaddr success test vectors": @@ -216,3 +239,11 @@ suite "MultiAddress test suite": check: $cma == "/ip4/127.0.0.1/udp/30000/p2p-circuit" $ma2 == "/ip4/127.0.0.1/udp/30000/p2p-circuit" + + test "isWire() test": + for item in UtilitySuccessVectors: + var a = MultiAddress.init(item) + check a.isWire() == true + for item in UtilityFailVectors: + var a = MultiAddress.init(item) + check a.isWire() == false