2018-11-27 12:16:04 +00:00
|
|
|
## Nim-Libp2p
|
|
|
|
## Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
## This module implements MultiAddress.
|
2019-10-08 21:21:54 +00:00
|
|
|
import nativesockets
|
2018-11-27 12:16:04 +00:00
|
|
|
import tables, strutils, net
|
2019-10-10 04:16:02 +00:00
|
|
|
import chronos
|
2019-12-13 09:42:47 +00:00
|
|
|
import multicodec, multihash, multibase, transcoder, vbuffer
|
|
|
|
import stew/[base58, base32]
|
2019-03-20 09:41:37 +00:00
|
|
|
from peer import PeerID
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
{.deadCodeElim:on.}
|
|
|
|
|
|
|
|
type
|
|
|
|
MAKind* = enum
|
|
|
|
None, Fixed, Length, Path, Marker
|
|
|
|
|
|
|
|
MAProtocol* = object
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec*: MultiCodec
|
2018-11-27 12:16:04 +00:00
|
|
|
size*: int
|
|
|
|
kind: MAKind
|
|
|
|
coder*: Transcoder
|
|
|
|
|
|
|
|
MultiAddress* = object
|
|
|
|
data*: VBuffer
|
|
|
|
|
2019-03-20 09:41:37 +00:00
|
|
|
MaPatternOp* = enum
|
|
|
|
Eq, Or, And
|
|
|
|
|
|
|
|
MaPattern* = object
|
|
|
|
operator*: MaPatternOp
|
|
|
|
args*: seq[MaPattern]
|
|
|
|
value*: MultiCodec
|
|
|
|
|
|
|
|
MaPatResult* = object
|
|
|
|
flag*: bool
|
|
|
|
rem*: seq[MultiCodec]
|
|
|
|
|
2019-06-10 20:33:36 +00:00
|
|
|
MultiAddressError* = object of CatchableError
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
proc ip4StB(s: string, vb: var VBuffer): bool =
|
|
|
|
## IPv4 stringToBuffer() implementation.
|
|
|
|
try:
|
|
|
|
var a = parseIpAddress(s)
|
|
|
|
if a.family == IpAddressFamily.IPv4:
|
|
|
|
vb.writeArray(a.address_v4)
|
|
|
|
result = true
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc ip4BtS(vb: var VBuffer, s: var string): bool =
|
|
|
|
## IPv4 bufferToString() implementation.
|
|
|
|
var a = IpAddress(family: IpAddressFamily.IPv4)
|
|
|
|
if vb.readArray(a.address_v4) == 4:
|
|
|
|
s = $a
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc ip4VB(vb: var VBuffer): bool =
|
|
|
|
## IPv4 validateBuffer() implementation.
|
|
|
|
var a = IpAddress(family: IpAddressFamily.IPv4)
|
|
|
|
if vb.readArray(a.address_v4) == 4:
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc ip6StB(s: string, vb: var VBuffer): bool =
|
|
|
|
## IPv6 stringToBuffer() implementation.
|
|
|
|
try:
|
|
|
|
var a = parseIpAddress(s)
|
|
|
|
if a.family == IpAddressFamily.IPv6:
|
|
|
|
vb.writeArray(a.address_v6)
|
|
|
|
result = true
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc ip6BtS(vb: var VBuffer, s: var string): bool =
|
|
|
|
## IPv6 bufferToString() implementation.
|
|
|
|
var a = IpAddress(family: IpAddressFamily.IPv6)
|
|
|
|
if vb.readArray(a.address_v6) == 16:
|
|
|
|
s = $a
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc ip6VB(vb: var VBuffer): bool =
|
|
|
|
## IPv6 validateBuffer() implementation.
|
|
|
|
var a = IpAddress(family: IpAddressFamily.IPv6)
|
|
|
|
if vb.readArray(a.address_v6) == 16:
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc ip6zoneStB(s: string, vb: var VBuffer): bool =
|
|
|
|
## IPv6 stringToBuffer() implementation.
|
|
|
|
if len(s) > 0:
|
|
|
|
vb.writeSeq(s)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc ip6zoneBtS(vb: var VBuffer, s: var string): bool =
|
|
|
|
## IPv6 bufferToString() implementation.
|
|
|
|
if vb.readSeq(s) > 0:
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc ip6zoneVB(vb: var VBuffer): bool =
|
|
|
|
## IPv6 validateBuffer() implementation.
|
|
|
|
var s = ""
|
|
|
|
if vb.readSeq(s) > 0:
|
|
|
|
if s.find('/') == -1:
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc portStB(s: string, vb: var VBuffer): bool =
|
|
|
|
## Port number stringToBuffer() implementation.
|
|
|
|
var port: array[2, byte]
|
|
|
|
try:
|
|
|
|
var nport = parseInt(s)
|
2018-12-05 00:30:50 +00:00
|
|
|
if (nport >= 0) and (nport < 65536):
|
2018-11-27 12:16:04 +00:00
|
|
|
port[0] = cast[byte]((nport shr 8) and 0xFF)
|
|
|
|
port[1] = cast[byte](nport and 0xFF)
|
|
|
|
vb.writeArray(port)
|
|
|
|
result = true
|
|
|
|
except:
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc portBtS(vb: var VBuffer, s: var string): bool =
|
|
|
|
## Port number bufferToString() implementation.
|
|
|
|
var port: array[2, byte]
|
|
|
|
if vb.readArray(port) == 2:
|
|
|
|
var nport = (cast[uint16](port[0]) shl 8) or cast[uint16](port[1])
|
|
|
|
s = $nport
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc portVB(vb: var VBuffer): bool =
|
|
|
|
## Port number validateBuffer() implementation.
|
|
|
|
var port: array[2, byte]
|
|
|
|
if vb.readArray(port) == 2:
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc p2pStB(s: string, vb: var VBuffer): bool =
|
|
|
|
## P2P address stringToBuffer() implementation.
|
|
|
|
try:
|
|
|
|
var data = Base58.decode(s)
|
2018-12-05 00:30:50 +00:00
|
|
|
var mh: MultiHash
|
|
|
|
if MultiHash.decode(data, mh) >= 0:
|
|
|
|
vb.writeSeq(data)
|
|
|
|
result = true
|
2018-11-27 12:16:04 +00:00
|
|
|
except:
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc p2pBtS(vb: var VBuffer, s: var string): bool =
|
|
|
|
## P2P address bufferToString() implementation.
|
|
|
|
var address = newSeq[byte]()
|
|
|
|
if vb.readSeq(address) > 0:
|
2018-12-05 00:30:50 +00:00
|
|
|
var mh: MultiHash
|
|
|
|
if MultiHash.decode(address, mh) >= 0:
|
|
|
|
s = Base58.encode(address)
|
|
|
|
result = true
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
proc p2pVB(vb: var VBuffer): bool =
|
|
|
|
## P2P address validateBuffer() implementation.
|
|
|
|
var address = newSeq[byte]()
|
|
|
|
if vb.readSeq(address) > 0:
|
2018-12-05 00:30:50 +00:00
|
|
|
var mh: MultiHash
|
|
|
|
if MultiHash.decode(address, mh) >= 0:
|
|
|
|
result = true
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
proc onionStB(s: string, vb: var VBuffer): bool =
|
2018-12-05 00:30:50 +00:00
|
|
|
try:
|
|
|
|
var parts = s.split(':')
|
|
|
|
if len(parts) != 2:
|
|
|
|
return false
|
|
|
|
if len(parts[0]) != 16:
|
|
|
|
return false
|
|
|
|
var address = Base32Lower.decode(parts[0].toLowerAscii())
|
|
|
|
var nport = parseInt(parts[1])
|
|
|
|
if (nport > 0 and nport < 65536) and len(address) == 10:
|
|
|
|
address.setLen(12)
|
|
|
|
address[10] = cast[byte]((nport shr 8) and 0xFF)
|
|
|
|
address[11] = cast[byte](nport and 0xFF)
|
|
|
|
vb.writeArray(address)
|
|
|
|
result = true
|
|
|
|
except:
|
|
|
|
discard
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
proc onionBtS(vb: var VBuffer, s: var string): bool =
|
2018-12-05 00:30:50 +00:00
|
|
|
## ONION address bufferToString() implementation.
|
|
|
|
var buf: array[12, byte]
|
|
|
|
if vb.readArray(buf) == 12:
|
|
|
|
var nport = (cast[uint16](buf[10]) shl 8) or cast[uint16](buf[11])
|
|
|
|
s = Base32Lower.encode(buf.toOpenArray(0, 9))
|
|
|
|
s.add(":")
|
|
|
|
s.add($nport)
|
|
|
|
result = true
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
proc onionVB(vb: var VBuffer): bool =
|
2018-12-05 00:30:50 +00:00
|
|
|
## ONION address validateBuffer() implementation.
|
|
|
|
var buf: array[12, byte]
|
|
|
|
if vb.readArray(buf) == 12:
|
|
|
|
result = true
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
proc unixStB(s: string, vb: var VBuffer): bool =
|
|
|
|
## Unix socket name stringToBuffer() implementation.
|
|
|
|
if len(s) > 0:
|
|
|
|
vb.writeSeq(s)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc unixBtS(vb: var VBuffer, s: var string): bool =
|
|
|
|
## Unix socket name bufferToString() implementation.
|
|
|
|
s = ""
|
|
|
|
if vb.readSeq(s) > 0:
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc unixVB(vb: var VBuffer): bool =
|
|
|
|
## Unix socket name validateBuffer() implementation.
|
|
|
|
var s = ""
|
|
|
|
if vb.readSeq(s) > 0:
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc dnsStB(s: string, vb: var VBuffer): bool =
|
|
|
|
## DNS name stringToBuffer() implementation.
|
|
|
|
if len(s) > 0:
|
|
|
|
vb.writeSeq(s)
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc dnsBtS(vb: var VBuffer, s: var string): bool =
|
|
|
|
## DNS name bufferToString() implementation.
|
|
|
|
s = ""
|
|
|
|
if vb.readSeq(s) > 0:
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc dnsVB(vb: var VBuffer): bool =
|
|
|
|
## DNS name validateBuffer() implementation.
|
|
|
|
var s = ""
|
|
|
|
if vb.readSeq(s) > 0:
|
|
|
|
if s.find('/') == -1:
|
|
|
|
result = true
|
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
proc mapEq*(codec: string): MaPattern =
|
2019-03-20 09:41:37 +00:00
|
|
|
## ``Equal`` operator for pattern
|
|
|
|
result.operator = Eq
|
|
|
|
result.value = multiCodec(codec)
|
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
proc mapOr*(args: varargs[MaPattern]): MaPattern =
|
2019-03-20 09:41:37 +00:00
|
|
|
## ``Or`` operator for pattern
|
|
|
|
result.operator = Or
|
|
|
|
result.args = @args
|
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
proc mapAnd*(args: varargs[MaPattern]): MaPattern =
|
2019-03-20 09:41:37 +00:00
|
|
|
## ``And`` operator for pattern
|
|
|
|
result.operator = And
|
|
|
|
result.args = @args
|
|
|
|
|
2018-11-27 12:16:04 +00:00
|
|
|
const
|
|
|
|
TranscoderIP4* = Transcoder(
|
|
|
|
stringToBuffer: ip4StB,
|
|
|
|
bufferToString: ip4BtS,
|
|
|
|
validateBuffer: ip4VB
|
|
|
|
)
|
|
|
|
TranscoderIP6* = Transcoder(
|
|
|
|
stringToBuffer: ip6StB,
|
|
|
|
bufferToString: ip6BtS,
|
|
|
|
validateBuffer: ip6VB
|
|
|
|
)
|
|
|
|
TranscoderIP6Zone* = Transcoder(
|
|
|
|
stringToBuffer: ip6zoneStB,
|
|
|
|
bufferToString: ip6zoneBtS,
|
|
|
|
validateBuffer: ip6zoneVB
|
|
|
|
)
|
|
|
|
TranscoderUnix* = Transcoder(
|
|
|
|
stringToBuffer: unixStB,
|
|
|
|
bufferToString: unixBtS,
|
|
|
|
validateBuffer: unixVB
|
|
|
|
)
|
|
|
|
TranscoderP2P* = Transcoder(
|
|
|
|
stringToBuffer: p2pStB,
|
|
|
|
bufferToString: p2pBtS,
|
|
|
|
validateBuffer: p2pVB
|
|
|
|
)
|
|
|
|
TranscoderPort* = Transcoder(
|
|
|
|
stringToBuffer: portStB,
|
|
|
|
bufferToString: portBtS,
|
|
|
|
validateBuffer: portVB
|
|
|
|
)
|
|
|
|
TranscoderOnion* = Transcoder(
|
|
|
|
stringToBuffer: onionStB,
|
|
|
|
bufferToString: onionBtS,
|
|
|
|
validateBuffer: onionVB
|
|
|
|
)
|
|
|
|
TranscoderDNS* = Transcoder(
|
|
|
|
stringToBuffer: dnsStB,
|
|
|
|
bufferToString: dnsBtS,
|
|
|
|
validateBuffer: dnsVB
|
|
|
|
)
|
|
|
|
ProtocolsList = [
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("ip4"), kind: Fixed, size: 4,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderIP4
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("tcp"), kind: Fixed, size: 2,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderPort
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("udp"), kind: Fixed, size: 2,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderPort
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("ip6"), kind: Fixed, size: 16,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderIP6
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("dccp"), kind: Fixed, size: 2,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderPort
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("sctp"), kind: Fixed, size: 2,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderPort
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("udt"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("utp"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("http"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("https"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("quic"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("ip6zone"), kind: Length, size: 0,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderIP6Zone
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("onion"), kind: Fixed, size: 10,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderOnion
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("ws"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("ipfs"), kind: Length, size: 0,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderP2P
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("p2p"), kind: Length, size: 0,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderP2P
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("unix"), kind: Path, size: 0,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderUnix
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("dns4"), kind: Length, size: 0,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderDNS
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("dns6"), kind: Length, size: 0,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderDNS
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("dnsaddr"), kind: Length, size: 0,
|
2018-11-27 12:16:04 +00:00
|
|
|
coder: TranscoderDNS
|
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("p2p-circuit"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("p2p-websocket-star"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("p2p-webrtc-star"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
),
|
|
|
|
MAProtocol(
|
2018-12-05 15:24:07 +00:00
|
|
|
mcodec: multiCodec("p2p-webrtc-direct"), kind: Marker, size: 0
|
2018-11-27 12:16:04 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
DNS4* = mapEq("dns4")
|
|
|
|
DNS6* = mapEq("dns6")
|
|
|
|
IP4* = mapEq("ip4")
|
|
|
|
IP6* = mapEq("ip6")
|
|
|
|
DNS* = mapOr(mapEq("dnsaddr"), DNS4, DNS6)
|
|
|
|
IP* = mapOr(IP4, IP6)
|
|
|
|
TCP* = mapOr(mapAnd(DNS, mapEq("tcp")), mapAnd(IP, mapEq("tcp")))
|
|
|
|
UDP* = mapOr(mapAnd(DNS, mapEq("udp")), mapAnd(IP, mapEq("udp")))
|
|
|
|
UTP* = mapAnd(UDP, mapEq("utp"))
|
|
|
|
QUIC* = mapAnd(UDP, mapEq("quic"))
|
|
|
|
UNIX* = mapEq("unix")
|
2019-03-20 09:41:37 +00:00
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
Unreliable* = mapOr(UDP)
|
2019-03-20 09:41:37 +00:00
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
Reliable* = mapOr(TCP, UTP, QUIC)
|
2019-03-20 09:41:37 +00:00
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
IPFS* = mapAnd(Reliable, mapEq("p2p"))
|
2019-03-20 09:41:37 +00:00
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
HTTP* = mapOr(
|
|
|
|
mapAnd(TCP, mapEq("http")),
|
|
|
|
mapAnd(IP, mapEq("http")),
|
|
|
|
mapAnd(DNS, mapEq("http"))
|
2019-03-20 09:41:37 +00:00
|
|
|
)
|
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
HTTPS* = mapOr(
|
|
|
|
mapAnd(TCP, mapEq("https")),
|
|
|
|
mapAnd(IP, mapEq("https")),
|
|
|
|
mapAnd(DNS, mapEq("https"))
|
2019-03-20 09:41:37 +00:00
|
|
|
)
|
|
|
|
|
2020-02-12 10:04:12 +00:00
|
|
|
WebRTCDirect* = mapOr(
|
|
|
|
mapAnd(HTTP, mapEq("p2p-webrtc-direct")),
|
|
|
|
mapAnd(HTTPS, mapEq("p2p-webrtc-direct"))
|
2019-03-20 09:41:37 +00:00
|
|
|
)
|
|
|
|
|
2018-12-05 15:24:07 +00:00
|
|
|
proc initMultiAddressCodeTable(): Table[MultiCodec,
|
|
|
|
MAProtocol] {.compileTime.} =
|
|
|
|
result = initTable[MultiCodec, MAProtocol]()
|
2018-11-27 12:16:04 +00:00
|
|
|
for item in ProtocolsList:
|
2018-12-05 15:24:07 +00:00
|
|
|
result[item.mcodec] = item
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
CodeAddresses = initMultiAddressCodeTable()
|
|
|
|
|
|
|
|
proc trimRight(s: string, ch: char): string =
|
|
|
|
## Consume trailing characters ``ch`` from string ``s`` and return result.
|
|
|
|
var m = 0
|
|
|
|
for i in countdown(len(s) - 1, 0):
|
|
|
|
if s[i] == ch:
|
|
|
|
inc(m)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
result = s[0..(len(s) - 1 - m)]
|
|
|
|
|
2018-11-27 20:58:16 +00:00
|
|
|
proc shcopy*(m1: var MultiAddress, m2: MultiAddress) =
|
|
|
|
shallowCopy(m1.data.buffer, m2.data.buffer)
|
|
|
|
m1.data.offset = m2.data.offset
|
|
|
|
m1.data.length = m2.data.length
|
|
|
|
|
2018-12-05 15:24:07 +00:00
|
|
|
proc protoCode*(ma: MultiAddress): MultiCodec =
|
2018-11-27 12:16:04 +00:00
|
|
|
## Returns MultiAddress ``ma`` protocol code.
|
2018-11-27 20:58:16 +00:00
|
|
|
var header: uint64
|
|
|
|
var vb: MultiAddress
|
|
|
|
shcopy(vb, ma)
|
|
|
|
if vb.data.readVarint(header) == -1:
|
|
|
|
raise newException(MultiAddressError, "Malformed binary address!")
|
2018-12-05 15:24:07 +00:00
|
|
|
let proto = CodeAddresses.getOrDefault(MultiCodec(header))
|
2018-11-27 20:58:16 +00:00
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Unsupported protocol '" & $header & "'")
|
2018-12-05 15:24:07 +00:00
|
|
|
result = proto.mcodec
|
2018-11-27 12:16:04 +00:00
|
|
|
|
|
|
|
proc protoName*(ma: MultiAddress): string =
|
|
|
|
## Returns MultiAddress ``ma`` protocol name.
|
2018-11-27 20:58:16 +00:00
|
|
|
var header: uint64
|
|
|
|
var vb: MultiAddress
|
|
|
|
shcopy(vb, ma)
|
|
|
|
if vb.data.readVarint(header) == -1:
|
|
|
|
raise newException(MultiAddressError, "Malformed binary address!")
|
2018-12-05 15:24:07 +00:00
|
|
|
let proto = CodeAddresses.getOrDefault(MultiCodec(header))
|
2018-11-27 20:58:16 +00:00
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Unsupported protocol '" & $header & "'")
|
2018-12-05 15:24:07 +00:00
|
|
|
result = $(proto.mcodec)
|
2018-11-27 12:16:04 +00:00
|
|
|
|
2018-11-27 20:58:16 +00:00
|
|
|
proc protoArgument*(ma: MultiAddress, value: var openarray[byte]): int =
|
|
|
|
## Returns MultiAddress ``ma`` protocol argument value.
|
2018-12-29 12:44:27 +00:00
|
|
|
##
|
2018-11-27 20:58:16 +00:00
|
|
|
## If current MultiAddress do not have argument value, then result will be
|
|
|
|
## ``0``.
|
|
|
|
var header: uint64
|
|
|
|
var vb: MultiAddress
|
|
|
|
var buffer: seq[byte]
|
|
|
|
shcopy(vb, ma)
|
|
|
|
if vb.data.readVarint(header) == -1:
|
|
|
|
raise newException(MultiAddressError, "Malformed binary address!")
|
2018-12-05 15:24:07 +00:00
|
|
|
let proto = CodeAddresses.getOrDefault(MultiCodec(header))
|
2018-11-27 20:58:16 +00:00
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Unsupported protocol '" & $header & "'")
|
|
|
|
if proto.kind == Fixed:
|
|
|
|
result = proto.size
|
|
|
|
if len(value) >= result:
|
|
|
|
if vb.data.readArray(value) != proto.size:
|
|
|
|
raise newException(MultiAddressError, "Decoding protocol error")
|
|
|
|
elif proto.kind in {Length, Path}:
|
|
|
|
if vb.data.readSeq(buffer) == -1:
|
|
|
|
raise newException(MultiAddressError, "Decoding protocol error")
|
2019-01-09 17:12:15 +00:00
|
|
|
result = len(buffer)
|
2018-11-27 20:58:16 +00:00
|
|
|
if len(value) >= result:
|
2019-01-09 17:12:15 +00:00
|
|
|
copyMem(addr value[0], addr buffer[0], result)
|
2018-11-27 12:16:04 +00:00
|
|
|
|
2019-10-29 13:38:37 +00:00
|
|
|
proc protoAddress*(ma: MultiAddress): seq[byte] =
|
|
|
|
## Returns MultiAddress ``ma`` protocol address binary blob.
|
|
|
|
##
|
|
|
|
## If current MultiAddress do not have argument value, then result array will
|
|
|
|
## be empty.
|
|
|
|
result = newSeq[byte](len(ma.data.buffer))
|
|
|
|
let res = protoArgument(ma, result)
|
|
|
|
result.setLen(res)
|
|
|
|
|
2018-11-27 12:16:04 +00:00
|
|
|
proc getPart(ma: MultiAddress, index: int): MultiAddress =
|
|
|
|
var header: uint64
|
|
|
|
var data = newSeq[byte]()
|
|
|
|
var offset = 0
|
|
|
|
var vb = ma
|
|
|
|
result.data = initVBuffer()
|
|
|
|
while offset <= index:
|
|
|
|
if vb.data.readVarint(header) == -1:
|
|
|
|
raise newException(MultiAddressError, "Malformed binary address!")
|
2018-12-05 15:24:07 +00:00
|
|
|
let proto = CodeAddresses.getOrDefault(MultiCodec(header))
|
2018-11-27 12:16:04 +00:00
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Unsupported protocol '" & $header & "'")
|
|
|
|
elif proto.kind == Fixed:
|
|
|
|
data.setLen(proto.size)
|
|
|
|
if vb.data.readArray(data) != proto.size:
|
|
|
|
raise newException(MultiAddressError, "Decoding protocol error")
|
|
|
|
if offset == index:
|
|
|
|
result.data.writeVarint(header)
|
|
|
|
result.data.writeArray(data)
|
|
|
|
result.data.finish()
|
|
|
|
elif proto.kind in {Length, Path}:
|
|
|
|
if vb.data.readSeq(data) == -1:
|
|
|
|
raise newException(MultiAddressError, "Decoding protocol error")
|
|
|
|
if offset == index:
|
|
|
|
result.data.writeVarint(header)
|
|
|
|
result.data.writeSeq(data)
|
|
|
|
result.data.finish()
|
|
|
|
elif proto.kind == Marker:
|
|
|
|
if offset == index:
|
|
|
|
result.data.writeVarint(header)
|
|
|
|
result.data.finish()
|
|
|
|
inc(offset)
|
|
|
|
|
|
|
|
proc `[]`*(ma: MultiAddress, i: int): MultiAddress {.inline.} =
|
2018-12-16 02:54:07 +00:00
|
|
|
## Returns part with index ``i`` of MultiAddress ``ma``.
|
2018-11-27 12:16:04 +00:00
|
|
|
result = ma.getPart(i)
|
|
|
|
|
|
|
|
iterator items*(ma: MultiAddress): MultiAddress =
|
|
|
|
## Iterates over all addresses inside of MultiAddress ``ma``.
|
|
|
|
var header: uint64
|
|
|
|
var data = newSeq[byte]()
|
|
|
|
var vb = ma
|
|
|
|
while true:
|
|
|
|
if vb.data.isEmpty():
|
|
|
|
break
|
|
|
|
var res = MultiAddress(data: initVBuffer())
|
|
|
|
if vb.data.readVarint(header) == -1:
|
|
|
|
raise newException(MultiAddressError, "Malformed binary address!")
|
2018-12-05 15:24:07 +00:00
|
|
|
let proto = CodeAddresses.getOrDefault(MultiCodec(header))
|
2018-11-27 12:16:04 +00:00
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Unsupported protocol '" & $header & "'")
|
|
|
|
elif proto.kind == Fixed:
|
|
|
|
data.setLen(proto.size)
|
|
|
|
if vb.data.readArray(data) != proto.size:
|
|
|
|
raise newException(MultiAddressError, "Decoding protocol error")
|
|
|
|
res.data.writeVarint(header)
|
|
|
|
res.data.writeArray(data)
|
|
|
|
elif proto.kind in {Length, Path}:
|
|
|
|
if vb.data.readSeq(data) == -1:
|
|
|
|
raise newException(MultiAddressError, "Decoding protocol error")
|
|
|
|
res.data.writeVarint(header)
|
|
|
|
res.data.writeSeq(data)
|
|
|
|
elif proto.kind == Marker:
|
|
|
|
res.data.writeVarint(header)
|
|
|
|
res.data.finish()
|
|
|
|
yield res
|
|
|
|
|
|
|
|
proc `$`*(value: MultiAddress): string =
|
|
|
|
## Return string representation of MultiAddress ``value``.
|
|
|
|
var header: uint64
|
|
|
|
var vb = value
|
|
|
|
var parts = newSeq[string]()
|
|
|
|
var part: string
|
|
|
|
while true:
|
|
|
|
if vb.data.isEmpty():
|
|
|
|
break
|
|
|
|
if vb.data.readVarint(header) == -1:
|
|
|
|
raise newException(MultiAddressError, "Malformed binary address!")
|
2018-12-05 15:24:07 +00:00
|
|
|
let proto = CodeAddresses.getOrDefault(MultiCodec(header))
|
2018-11-27 12:16:04 +00:00
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Unsupported protocol '" & $header & "'")
|
|
|
|
if proto.kind in {Fixed, Length, Path}:
|
|
|
|
if isNil(proto.coder.bufferToString):
|
|
|
|
raise newException(MultiAddressError,
|
2018-12-05 15:24:07 +00:00
|
|
|
"Missing protocol '" & $(proto.mcodec) & "' coder")
|
2018-11-27 12:16:04 +00:00
|
|
|
if not proto.coder.bufferToString(vb.data, part):
|
|
|
|
raise newException(MultiAddressError, "Decoding protocol error")
|
2018-12-05 15:24:07 +00:00
|
|
|
parts.add($(proto.mcodec))
|
2019-01-09 17:12:15 +00:00
|
|
|
if proto.kind == Path and part[0] == '/':
|
|
|
|
parts.add(part[1..^1])
|
|
|
|
else:
|
|
|
|
parts.add(part)
|
2018-11-27 12:16:04 +00:00
|
|
|
elif proto.kind == Marker:
|
2018-12-05 15:24:07 +00:00
|
|
|
parts.add($(proto.mcodec))
|
2018-11-27 12:16:04 +00:00
|
|
|
if len(parts) > 0:
|
|
|
|
result = "/" & parts.join("/")
|
|
|
|
|
2019-03-20 09:41:37 +00:00
|
|
|
proc protocols*(value: MultiAddress): seq[MultiCodec] =
|
|
|
|
## Returns list of protocol codecs inside of MultiAddress ``value``.
|
|
|
|
result = newSeq[MultiCodec]()
|
|
|
|
for item in value.items():
|
|
|
|
result.add(item.protoCode())
|
|
|
|
|
2018-11-27 12:16:04 +00:00
|
|
|
proc hex*(value: MultiAddress): string =
|
|
|
|
## Return hexadecimal string representation of MultiAddress ``value``.
|
|
|
|
result = $(value.data)
|
|
|
|
|
2018-12-16 02:54:07 +00:00
|
|
|
proc write*(vb: var VBuffer, ma: MultiAddress) {.inline.} =
|
|
|
|
## Write MultiAddress value ``ma`` to buffer ``vb``.
|
|
|
|
vb.writeArray(ma.data.buffer)
|
|
|
|
|
|
|
|
proc encode*(mbtype: typedesc[MultiBase], encoding: string,
|
|
|
|
ma: MultiAddress): string {.inline.} =
|
|
|
|
## Get MultiBase encoded representation of ``ma`` using encoding
|
|
|
|
## ``encoding``.
|
|
|
|
result = MultiBase.encode(encoding, ma.data.buffer)
|
|
|
|
|
2018-11-27 12:16:04 +00:00
|
|
|
proc validate*(ma: MultiAddress): bool =
|
|
|
|
## Returns ``true`` if MultiAddress ``ma`` is valid.
|
|
|
|
var header: uint64
|
2018-11-27 20:58:16 +00:00
|
|
|
var vb: MultiAddress
|
|
|
|
shcopy(vb, ma)
|
2018-11-27 12:16:04 +00:00
|
|
|
while true:
|
|
|
|
if vb.data.isEmpty():
|
|
|
|
break
|
|
|
|
if vb.data.readVarint(header) == -1:
|
|
|
|
return false
|
2018-12-05 15:24:07 +00:00
|
|
|
let proto = CodeAddresses.getOrDefault(MultiCodec(header))
|
2018-11-27 12:16:04 +00:00
|
|
|
if proto.kind == None:
|
|
|
|
return false
|
|
|
|
if proto.kind in {Fixed, Length, Path}:
|
|
|
|
if isNil(proto.coder.validateBuffer):
|
|
|
|
return false
|
|
|
|
if not proto.coder.validateBuffer(vb.data):
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
result = true
|
|
|
|
|
2019-03-05 12:09:26 +00:00
|
|
|
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec,
|
2018-11-27 12:16:04 +00:00
|
|
|
value: openarray[byte]): MultiAddress =
|
|
|
|
## Initialize MultiAddress object from protocol id ``protocol`` and array
|
|
|
|
## of bytes ``value``.
|
|
|
|
let proto = CodeAddresses.getOrDefault(protocol)
|
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError, "Protocol not found")
|
|
|
|
result.data = initVBuffer()
|
2019-03-05 20:28:46 +00:00
|
|
|
result.data.writeVarint(cast[uint64](proto.mcodec))
|
2018-11-27 12:16:04 +00:00
|
|
|
if proto.kind in {Fixed, Length, Path}:
|
|
|
|
if len(value) == 0:
|
|
|
|
raise newException(MultiAddressError, "Value must not be empty array")
|
|
|
|
if proto.kind == Fixed:
|
|
|
|
result.data.writeArray(value)
|
|
|
|
else:
|
|
|
|
var data = newSeq[byte](len(value))
|
|
|
|
copyMem(addr data[0], unsafeAddr value[0], len(value))
|
|
|
|
result.data.writeSeq(data)
|
|
|
|
result.data.finish()
|
|
|
|
|
2019-03-05 12:09:26 +00:00
|
|
|
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec,
|
|
|
|
value: PeerID): MultiAddress {.inline.} =
|
|
|
|
## Initialize MultiAddress object from protocol id ``protocol`` and peer id
|
|
|
|
## ``value``.
|
2019-11-04 18:57:17 +00:00
|
|
|
init(mtype, protocol, value.data)
|
2019-03-05 12:09:26 +00:00
|
|
|
|
|
|
|
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec): MultiAddress =
|
2018-11-27 12:16:04 +00:00
|
|
|
## Initialize MultiAddress object from protocol id ``protocol``.
|
|
|
|
let proto = CodeAddresses.getOrDefault(protocol)
|
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError, "Protocol not found")
|
|
|
|
result.data = initVBuffer()
|
|
|
|
if proto.kind != Marker:
|
|
|
|
raise newException(MultiAddressError, "Protocol missing value")
|
2019-03-05 12:09:26 +00:00
|
|
|
result.data.writeVarint(cast[uint64](proto.mcodec))
|
2018-11-27 12:16:04 +00:00
|
|
|
result.data.finish()
|
|
|
|
|
2018-12-05 15:24:07 +00:00
|
|
|
proc getProtocol(name: string): MAProtocol {.inline.} =
|
|
|
|
let mc = MultiCodec.codec(name)
|
|
|
|
if mc != InvalidMultiCodec:
|
|
|
|
result = CodeAddresses.getOrDefault(mc)
|
|
|
|
|
2018-11-27 12:16:04 +00:00
|
|
|
proc init*(mtype: typedesc[MultiAddress], value: string): MultiAddress =
|
|
|
|
## Initialize MultiAddress object from string representation ``value``.
|
|
|
|
var parts = value.trimRight('/').split('/')
|
|
|
|
if len(parts[0]) != 0:
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Invalid MultiAddress, must start with `/`")
|
|
|
|
var offset = 1
|
|
|
|
result.data = initVBuffer()
|
|
|
|
while offset < len(parts):
|
|
|
|
let part = parts[offset]
|
2018-12-05 15:24:07 +00:00
|
|
|
let proto = getProtocol(part)
|
2018-11-27 12:16:04 +00:00
|
|
|
if proto.kind == None:
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Unsupported protocol '" & part & "'")
|
|
|
|
if proto.kind in {Fixed, Length, Path}:
|
|
|
|
if isNil(proto.coder.stringToBuffer):
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Missing protocol '" & part & "' transcoder")
|
|
|
|
if offset + 1 >= len(parts):
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Missing protocol '" & part & "' argument")
|
|
|
|
|
|
|
|
if proto.kind in {Fixed, Length}:
|
2018-12-14 10:28:22 +00:00
|
|
|
result.data.write(proto.mcodec)
|
2018-12-05 15:24:07 +00:00
|
|
|
let res = proto.coder.stringToBuffer(parts[offset + 1], result.data)
|
|
|
|
if not res:
|
2018-11-27 12:16:04 +00:00
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Error encoding `$1/$2`" % [part, parts[offset + 1]])
|
|
|
|
offset += 2
|
|
|
|
elif proto.kind == Path:
|
|
|
|
var path = "/" & (parts[(offset + 1)..^1].join("/"))
|
2018-12-14 10:28:22 +00:00
|
|
|
result.data.write(proto.mcodec)
|
2018-11-27 12:16:04 +00:00
|
|
|
if not proto.coder.stringToBuffer(path, result.data):
|
|
|
|
raise newException(MultiAddressError,
|
|
|
|
"Error encoding `$1/$2`" % [part, path])
|
|
|
|
break
|
|
|
|
elif proto.kind == Marker:
|
2018-12-14 10:28:22 +00:00
|
|
|
result.data.write(proto.mcodec)
|
2018-11-27 12:16:04 +00:00
|
|
|
offset += 1
|
|
|
|
result.data.finish()
|
|
|
|
|
|
|
|
proc init*(mtype: typedesc[MultiAddress], data: openarray[byte]): MultiAddress =
|
|
|
|
## Initialize MultiAddress with array of bytes ``data``.
|
|
|
|
if len(data) == 0:
|
|
|
|
raise newException(MultiAddressError, "Address could not be empty!")
|
|
|
|
result.data = initVBuffer()
|
|
|
|
result.data.buffer.setLen(len(data))
|
|
|
|
copyMem(addr result.data.buffer[0], unsafeAddr data[0], len(data))
|
|
|
|
if not result.validate():
|
|
|
|
raise newException(MultiAddressError, "Incorrect MultiAddress!")
|
|
|
|
|
|
|
|
proc init*(mtype: typedesc[MultiAddress]): MultiAddress =
|
|
|
|
## Initialize empty MultiAddress.
|
|
|
|
result.data = initVBuffer()
|
|
|
|
|
2019-06-17 11:45:14 +00:00
|
|
|
proc init*(mtype: typedesc[MultiAddress],
|
|
|
|
address: IpAddress, protocol: Protocol, port: Port): MultiAddress =
|
2019-08-01 05:56:59 +00:00
|
|
|
## Initialize MultiAddress using stdlib's net.IpAddress (IPv4/IPv6) and
|
|
|
|
## net.Protocol (UDP/TCP) information.
|
|
|
|
result.data = initVBuffer()
|
|
|
|
let familyProto = case address.family
|
|
|
|
of IpAddressFamily.IPv4: getProtocol("ip4")
|
|
|
|
of IpAddressFamily.IPv6: getProtocol("ip6")
|
|
|
|
let protoProto = case protocol
|
|
|
|
of IPPROTO_TCP: getProtocol("tcp")
|
|
|
|
of IPPROTO_UDP: getProtocol("udp")
|
|
|
|
else: raise newException(AssertionError,
|
|
|
|
"protocol should be either TCP or UDP")
|
|
|
|
result.data.write(familyProto.mcodec)
|
|
|
|
if not familyProto.coder.stringToBuffer($address, result.data):
|
|
|
|
raise newException(MultiAddressError, "Error encoding IPv4/IPv6 address")
|
|
|
|
result.data.write(protoProto.mcodec)
|
|
|
|
if not protoProto.coder.stringToBuffer($port, result.data):
|
|
|
|
raise newException(MultiAddressError, "Error encoding port number")
|
|
|
|
result.data.finish()
|
2019-06-12 18:18:08 +00:00
|
|
|
|
2019-10-10 04:16:02 +00:00
|
|
|
proc init*(mtype: typedesc[MultiAddress], address: TransportAddress,
|
|
|
|
protocol: Protocol = IPPROTO_TCP): MultiAddress =
|
|
|
|
## Initialize MultiAddress using chronos.TransportAddress (IPv4/IPv6/Unix)
|
|
|
|
## and protocol information (UDP/TCP).
|
|
|
|
result.data = initVBuffer()
|
|
|
|
let protoProto = case protocol
|
|
|
|
of IPPROTO_TCP: getProtocol("tcp")
|
|
|
|
of IPPROTO_UDP: getProtocol("udp")
|
|
|
|
else: raise newException(AssertionError,
|
|
|
|
"protocol should be either TCP or UDP")
|
|
|
|
if address.family == AddressFamily.IPv4:
|
|
|
|
result.data.write(getProtocol("ip4").mcodec)
|
|
|
|
result.data.writeArray(address.address_v4)
|
|
|
|
result.data.write(protoProto.mcodec)
|
|
|
|
discard protoProto.coder.stringToBuffer($address.port, result.data)
|
|
|
|
elif address.family == AddressFamily.IPv6:
|
|
|
|
result.data.write(getProtocol("ip6").mcodec)
|
|
|
|
result.data.writeArray(address.address_v6)
|
|
|
|
result.data.write(protoProto.mcodec)
|
|
|
|
discard protoProto.coder.stringToBuffer($address.port, result.data)
|
|
|
|
elif address.family == AddressFamily.Unix:
|
|
|
|
result.data.write(getProtocol("unix").mcodec)
|
|
|
|
result.data.writeSeq(address.address_un)
|
|
|
|
result.data.finish()
|
|
|
|
|
2018-11-27 12:16:04 +00:00
|
|
|
proc isEmpty*(ma: MultiAddress): bool =
|
|
|
|
## Returns ``true``, if MultiAddress ``ma`` is empty or non initialized.
|
|
|
|
result = len(ma.data) == 0
|
|
|
|
|
|
|
|
proc `&`*(m1, m2: MultiAddress): MultiAddress =
|
|
|
|
## Concatenates two addresses ``m1`` and ``m2``, and returns result.
|
|
|
|
##
|
|
|
|
## This procedure performs validation of concatenated result and can raise
|
|
|
|
## exception on error.
|
|
|
|
result.data = initVBuffer()
|
|
|
|
result.data.buffer = m1.data.buffer & m2.data.buffer
|
|
|
|
if not result.validate():
|
|
|
|
raise newException(MultiAddressError, "Incorrect MultiAddress!")
|
|
|
|
|
|
|
|
proc `&=`*(m1: var MultiAddress, m2: MultiAddress) =
|
|
|
|
## Concatenates two addresses ``m1`` and ``m2``.
|
|
|
|
##
|
|
|
|
## This procedure performs validation of concatenated result and can raise
|
|
|
|
## exception on error.
|
|
|
|
m1.data.buffer &= m2.data.buffer
|
|
|
|
if not m1.validate():
|
|
|
|
raise newException(MultiAddressError, "Incorrect MultiAddress!")
|
2018-12-29 12:44:27 +00:00
|
|
|
|
|
|
|
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
|
2019-03-20 09:41:37 +00:00
|
|
|
|
|
|
|
proc matchPart(pat: MaPattern, protos: seq[MultiCodec]): MaPatResult =
|
|
|
|
var empty: seq[MultiCodec]
|
|
|
|
var pcs = protos
|
|
|
|
if pat.operator == Or:
|
|
|
|
for a in pat.args:
|
|
|
|
let res = a.matchPart(pcs)
|
|
|
|
if res.flag:
|
|
|
|
return MaPatResult(flag: true, rem: res.rem)
|
|
|
|
result = MaPatResult(flag: false, rem: empty)
|
|
|
|
elif pat.operator == And:
|
|
|
|
if len(pcs) < len(pat.args):
|
|
|
|
return MaPatResult(flag: false, rem: empty)
|
|
|
|
for i in 0..<len(pat.args):
|
|
|
|
let res = pat.args[i].matchPart(pcs)
|
|
|
|
if not res.flag:
|
|
|
|
return MaPatResult(flag: false, rem: res.rem)
|
|
|
|
pcs = res.rem
|
|
|
|
result = MaPatResult(flag: true, rem: pcs)
|
|
|
|
elif pat.operator == Eq:
|
|
|
|
if len(pcs) == 0:
|
|
|
|
return MaPatResult(flag: false, rem: empty)
|
|
|
|
if pcs[0] == pat.value:
|
|
|
|
return MaPatResult(flag: true, rem: pcs[1..^1])
|
|
|
|
result = MaPatResult(flag: false, rem: empty)
|
|
|
|
|
|
|
|
proc match*(pat: MaPattern, address: MultiAddress): bool =
|
|
|
|
## Match full ``address`` using pattern ``pat`` and return ``true`` if
|
|
|
|
## ``address`` satisfies pattern.
|
|
|
|
var protos = address.protocols()
|
|
|
|
let res = matchPart(pat, protos)
|
|
|
|
result = res.flag and (len(res.rem) == 0)
|
|
|
|
|
|
|
|
proc matchPartial*(pat: MaPattern, address: MultiAddress): bool =
|
|
|
|
## Match prefix part of ``address`` using pattern ``pat`` and return
|
|
|
|
## ``true`` if ``address`` starts with pattern.
|
|
|
|
var protos = address.protocols()
|
|
|
|
let res = matchPart(pat, protos)
|
|
|
|
result = res.flag
|
|
|
|
|
|
|
|
proc `$`*(pat: MaPattern): string =
|
|
|
|
## Return pattern ``pat`` as string.
|
|
|
|
var sub = newSeq[string]()
|
|
|
|
for a in pat.args:
|
|
|
|
sub.add($a)
|
|
|
|
if pat.operator == And:
|
|
|
|
result = sub.join("/")
|
|
|
|
elif pat.operator == Or:
|
|
|
|
result = "(" & sub.join("|") & ")"
|
|
|
|
elif pat.operator == Eq:
|
|
|
|
result = $pat.value
|