2019-08-30 05:16:55 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-30 05:16:55 +00:00
|
|
|
## 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.
|
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
import std/[strutils]
|
2020-06-03 02:21:11 +00:00
|
|
|
import chronos, chronicles, stew/byteutils
|
2020-06-19 17:29:43 +00:00
|
|
|
import stream/connection,
|
2019-10-04 16:04:21 +00:00
|
|
|
vbuffer,
|
2019-12-06 02:16:18 +00:00
|
|
|
protocols/protocol
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2019-09-11 21:06:10 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p multistream"
|
2019-09-11 21:06:10 +00:00
|
|
|
|
2020-01-07 08:02:37 +00:00
|
|
|
const
|
2019-10-04 16:04:21 +00:00
|
|
|
MsgSize* = 64*1024
|
|
|
|
Codec* = "/multistream/1.0.0"
|
|
|
|
|
|
|
|
MSCodec* = "\x13" & Codec & "\n"
|
2019-10-04 20:10:01 +00:00
|
|
|
Na* = "\x03na\n"
|
|
|
|
Ls* = "\x03ls\n"
|
2019-08-30 05:16:55 +00:00
|
|
|
|
|
|
|
type
|
2019-08-30 22:15:47 +00:00
|
|
|
Matcher* = proc (proto: string): bool {.gcsafe.}
|
2019-08-30 05:16:55 +00:00
|
|
|
|
|
|
|
HandlerHolder* = object
|
2020-09-21 09:16:29 +00:00
|
|
|
protos*: seq[string]
|
2019-09-06 00:16:49 +00:00
|
|
|
protocol*: LPProtocol
|
|
|
|
match*: Matcher
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
MultistreamSelect* = ref object of RootObj
|
2019-08-30 05:16:55 +00:00
|
|
|
handlers*: seq[HandlerHolder]
|
|
|
|
codec*: string
|
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
proc newMultistream*(): MultistreamSelect =
|
2019-08-30 05:16:55 +00:00
|
|
|
new result
|
2019-09-06 21:25:54 +00:00
|
|
|
result.codec = MSCodec
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2020-07-28 14:03:22 +00:00
|
|
|
template validateSuffix(str: string): untyped =
|
|
|
|
if str.endsWith("\n"):
|
|
|
|
str.removeSuffix("\n")
|
|
|
|
else:
|
|
|
|
raise newException(CatchableError, "MultistreamSelect failed, malformed message")
|
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
proc select*(m: MultistreamSelect,
|
2019-08-30 05:16:55 +00:00
|
|
|
conn: Connection,
|
2019-09-14 13:55:52 +00:00
|
|
|
proto: seq[string]):
|
2019-09-04 20:15:55 +00:00
|
|
|
Future[string] {.async.} =
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "initiating handshake", conn, codec = m.codec
|
2019-08-30 05:16:55 +00:00
|
|
|
## select a remote protocol
|
2019-09-05 15:19:15 +00:00
|
|
|
await conn.write(m.codec) # write handshake
|
2019-08-30 05:16:55 +00:00
|
|
|
if proto.len() > 0:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "selecting proto", conn, proto = proto[0]
|
2019-09-05 15:19:15 +00:00
|
|
|
await conn.writeLp((proto[0] & "\n")) # select proto
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2020-09-09 17:12:08 +00:00
|
|
|
var s = string.fromBytes((await conn.readLp(MsgSize))) # read ms header
|
2020-07-28 14:03:22 +00:00
|
|
|
validateSuffix(s)
|
|
|
|
|
2020-06-22 21:38:48 +00:00
|
|
|
if s != Codec:
|
2020-09-09 17:12:08 +00:00
|
|
|
notice "handshake failed", conn, codec = s
|
2020-07-28 14:03:22 +00:00
|
|
|
raise newException(CatchableError, "MultistreamSelect handshake failed")
|
|
|
|
else:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "multistream handshake success", conn
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2019-09-04 18:31:13 +00:00
|
|
|
if proto.len() == 0: # no protocols, must be a handshake call
|
2020-06-22 21:38:48 +00:00
|
|
|
return Codec
|
|
|
|
else:
|
2020-09-09 17:12:08 +00:00
|
|
|
s = string.fromBytes(await conn.readLp(MsgSize)) # read the first proto
|
2020-07-28 14:03:22 +00:00
|
|
|
validateSuffix(s)
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "reading first requested proto", conn
|
2020-06-22 21:38:48 +00:00
|
|
|
if s == proto[0]:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "successfully selected ", conn, proto = proto[0]
|
2020-06-22 21:38:48 +00:00
|
|
|
return proto[0]
|
|
|
|
elif proto.len > 1:
|
|
|
|
# Try to negotiate alternatives
|
|
|
|
let protos = proto[1..<proto.len()]
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "selecting one of several protos", conn, protos = protos
|
2020-06-22 21:38:48 +00:00
|
|
|
for p in protos:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "selecting proto", conn, proto = p
|
2020-06-22 21:38:48 +00:00
|
|
|
await conn.writeLp((p & "\n")) # select proto
|
2020-09-09 17:12:08 +00:00
|
|
|
s = string.fromBytes(await conn.readLp(MsgSize)) # read the first proto
|
2020-07-28 14:03:22 +00:00
|
|
|
validateSuffix(s)
|
2020-06-22 21:38:48 +00:00
|
|
|
if s == p:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "selected protocol", conn, protocol = s
|
2020-06-22 21:38:48 +00:00
|
|
|
return s
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
# No alternatives, fail
|
|
|
|
return ""
|
2019-09-04 18:31:13 +00:00
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
proc select*(m: MultistreamSelect,
|
2019-09-04 18:31:13 +00:00
|
|
|
conn: Connection,
|
2020-01-07 08:02:37 +00:00
|
|
|
proto: string): Future[bool] {.async.} =
|
|
|
|
if proto.len > 0:
|
2020-06-22 21:38:48 +00:00
|
|
|
return (await m.select(conn, @[proto])) == proto
|
2020-01-07 08:02:37 +00:00
|
|
|
else:
|
2020-06-22 21:38:48 +00:00
|
|
|
return (await m.select(conn, @[])) == Codec
|
2019-09-04 20:15:55 +00:00
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
proc select*(m: MultistreamSelect, conn: Connection): Future[bool] =
|
2019-09-08 07:43:33 +00:00
|
|
|
m.select(conn, "")
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
proc list*(m: MultistreamSelect,
|
2019-08-30 05:16:55 +00:00
|
|
|
conn: Connection): Future[seq[string]] {.async.} =
|
|
|
|
## list remote protos requests on connection
|
2019-09-06 06:49:01 +00:00
|
|
|
if not await m.select(conn):
|
2019-08-30 05:16:55 +00:00
|
|
|
return
|
|
|
|
|
2020-05-06 16:31:47 +00:00
|
|
|
await conn.write(Ls) # send ls
|
2019-08-30 05:16:55 +00:00
|
|
|
|
|
|
|
var list = newSeq[string]()
|
2020-09-09 17:12:08 +00:00
|
|
|
let ms = string.fromBytes(await conn.readLp(MsgSize))
|
2019-08-30 05:16:55 +00:00
|
|
|
for s in ms.split("\n"):
|
|
|
|
if s.len() > 0:
|
|
|
|
list.add(s)
|
|
|
|
|
|
|
|
result = list
|
|
|
|
|
2020-07-28 14:03:22 +00:00
|
|
|
proc handle*(m: MultistreamSelect, conn: Connection, active: bool = false) {.async, gcsafe.} =
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "Starting multistream handler", conn, handshaked = active
|
2020-07-28 14:03:22 +00:00
|
|
|
var handshaked = active
|
2020-05-23 17:10:22 +00:00
|
|
|
try:
|
2020-08-03 05:20:11 +00:00
|
|
|
while not conn.atEof:
|
2020-09-09 17:12:08 +00:00
|
|
|
var ms = string.fromBytes(await conn.readLp(MsgSize))
|
2020-07-28 14:03:22 +00:00
|
|
|
validateSuffix(ms)
|
|
|
|
|
|
|
|
if not handshaked and ms != Codec:
|
2020-09-09 17:12:08 +00:00
|
|
|
notice "expected handshake message", conn, instead=ms
|
2020-08-02 10:22:49 +00:00
|
|
|
raise newException(CatchableError,
|
2020-07-28 14:03:22 +00:00
|
|
|
"MultistreamSelect handling failed, invalid first message")
|
2020-01-07 08:02:37 +00:00
|
|
|
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "handle: got request", conn, ms
|
2019-09-06 06:49:01 +00:00
|
|
|
if ms.len() <= 0:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "handle: invalid proto", conn
|
2020-05-06 16:31:47 +00:00
|
|
|
await conn.write(Na)
|
2019-09-06 06:49:01 +00:00
|
|
|
|
|
|
|
if m.handlers.len() == 0:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "handle: sending `na` for protocol", conn, protocol = ms
|
2020-05-06 16:31:47 +00:00
|
|
|
await conn.write(Na)
|
2019-09-06 06:49:01 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
case ms:
|
2020-07-28 14:03:22 +00:00
|
|
|
of "ls":
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "handle: listing protos", conn
|
2020-07-28 14:03:22 +00:00
|
|
|
var protos = ""
|
|
|
|
for h in m.handlers:
|
2020-09-21 09:16:29 +00:00
|
|
|
for proto in h.protos:
|
|
|
|
protos &= (proto & "\n")
|
2020-07-28 14:03:22 +00:00
|
|
|
await conn.writeLp(protos)
|
|
|
|
of Codec:
|
|
|
|
if not handshaked:
|
2019-09-06 06:49:01 +00:00
|
|
|
await conn.write(m.codec)
|
2020-07-28 14:03:22 +00:00
|
|
|
handshaked = true
|
2019-09-06 06:49:01 +00:00
|
|
|
else:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "handle: sending `na` for duplicate handshake while handshaked",
|
|
|
|
conn
|
2020-07-31 05:02:03 +00:00
|
|
|
await conn.write(Na)
|
2020-07-28 14:03:22 +00:00
|
|
|
else:
|
|
|
|
for h in m.handlers:
|
2020-09-21 09:16:29 +00:00
|
|
|
if (not isNil(h.match) and h.match(ms)) or h.protos.contains(ms):
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "found handler", conn, protocol = ms
|
2020-09-21 09:16:29 +00:00
|
|
|
await conn.writeLp(ms & "\n")
|
2020-07-28 14:03:22 +00:00
|
|
|
await h.protocol.handler(conn, ms)
|
|
|
|
return
|
2020-09-09 17:12:08 +00:00
|
|
|
debug "no handlers", conn, protocol = ms
|
2020-07-28 14:03:22 +00:00
|
|
|
await conn.write(Na)
|
2020-06-29 15:15:31 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-05-23 17:10:22 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-09 17:12:08 +00:00
|
|
|
trace "Exception in multistream", conn, msg = exc.msg
|
2020-05-23 17:10:22 +00:00
|
|
|
finally:
|
2020-09-09 17:12:08 +00:00
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
trace "Stopped multistream handler", conn
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2020-08-15 19:50:31 +00:00
|
|
|
proc addHandler*(m: MultistreamSelect,
|
2020-09-21 09:16:29 +00:00
|
|
|
codecs: seq[string],
|
2020-08-15 19:50:31 +00:00
|
|
|
protocol: LPProtocol,
|
|
|
|
matcher: Matcher = nil) =
|
2020-09-21 09:16:29 +00:00
|
|
|
trace "registering protocols", protos = codecs
|
|
|
|
m.handlers.add(HandlerHolder(protos: codecs,
|
2019-09-14 13:55:52 +00:00
|
|
|
protocol: protocol,
|
|
|
|
match: matcher))
|
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
proc addHandler*(m: MultistreamSelect,
|
|
|
|
codec: string,
|
|
|
|
protocol: LPProtocol,
|
|
|
|
matcher: Matcher = nil) =
|
|
|
|
addHandler(m, @[codec], protocol, matcher)
|
|
|
|
|
2020-08-15 19:50:31 +00:00
|
|
|
proc addHandler*(m: MultistreamSelect,
|
|
|
|
codec: string,
|
|
|
|
handler: LPProtoHandler,
|
|
|
|
matcher: Matcher = nil) =
|
2019-09-14 13:55:52 +00:00
|
|
|
## helper to allow registering pure handlers
|
2020-09-21 09:16:29 +00:00
|
|
|
trace "registering proto handler", proto = codec
|
2019-09-14 13:55:52 +00:00
|
|
|
let protocol = new LPProtocol
|
|
|
|
protocol.codec = codec
|
|
|
|
protocol.handler = handler
|
|
|
|
|
2020-09-21 09:16:29 +00:00
|
|
|
m.handlers.add(HandlerHolder(protos: @[codec],
|
2019-08-30 05:16:55 +00:00
|
|
|
protocol: protocol,
|
|
|
|
match: matcher))
|