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-05-06 16:31:47 +00:00
|
|
|
import strutils, stew/byteutils
|
2019-09-09 17:33:32 +00:00
|
|
|
import chronos, chronicles
|
2019-10-04 16:04:21 +00:00
|
|
|
import connection,
|
|
|
|
vbuffer,
|
2020-04-21 01:24:42 +00:00
|
|
|
errors,
|
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:
|
|
|
|
topic = "Multistream"
|
|
|
|
|
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
|
2019-09-06 00:16:49 +00:00
|
|
|
proto*: string
|
|
|
|
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-12 14:40:15 +00:00
|
|
|
MultistreamHandshakeException* = object of CatchableError
|
|
|
|
|
2020-05-23 17:10:22 +00:00
|
|
|
proc newMultistreamHandshakeException*(): ref CatchableError {.inline.} =
|
2020-02-12 14:40:15 +00:00
|
|
|
result = newException(MultistreamHandshakeException,
|
|
|
|
"could not perform multistream handshake")
|
|
|
|
|
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-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.} =
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "initiating handshake", 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:
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "selecting proto", proto = proto
|
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-05-08 20:58:23 +00:00
|
|
|
result = cast[string]((await conn.readLp(1024))) # read ms header
|
2019-09-06 00:16:49 +00:00
|
|
|
result.removeSuffix("\n")
|
|
|
|
if result != Codec:
|
2020-05-15 03:56:56 +00:00
|
|
|
notice "handshake failed", codec = result.toHex()
|
2020-02-12 14:40:15 +00:00
|
|
|
raise newMultistreamHandshakeException()
|
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
|
2019-09-06 00:16:49 +00:00
|
|
|
return
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2020-05-08 20:58:23 +00:00
|
|
|
result = string.fromBytes(await conn.readLp(1024)) # read the first proto
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "reading first requested proto"
|
2019-09-06 00:16:49 +00:00
|
|
|
result.removeSuffix("\n")
|
|
|
|
if result == proto[0]:
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "succesfully selected ", proto = proto
|
2019-09-06 00:16:49 +00:00
|
|
|
return
|
2019-09-04 18:31:13 +00:00
|
|
|
|
2019-09-04 20:15:55 +00:00
|
|
|
if not result.len > 0:
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "selecting one of several protos"
|
2019-09-04 18:31:13 +00:00
|
|
|
for p in proto[1..<proto.len()]:
|
2019-09-05 15:19:15 +00:00
|
|
|
await conn.writeLp((p & "\n")) # select proto
|
2020-05-08 20:58:23 +00:00
|
|
|
result = string.fromBytes(await conn.readLp(1024)) # read the first proto
|
2019-09-06 00:16:49 +00:00
|
|
|
result.removeSuffix("\n")
|
|
|
|
if result == p:
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "selected protocol", protocol = result
|
2019-09-04 18:31:13 +00:00
|
|
|
break
|
|
|
|
|
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:
|
|
|
|
result = (await m.select(conn, @[proto])) == proto
|
|
|
|
else:
|
2019-09-08 07:43:33 +00:00
|
|
|
result = (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-05-08 20:58:23 +00:00
|
|
|
let ms = string.fromBytes(await conn.readLp(1024))
|
2019-08-30 05:16:55 +00:00
|
|
|
for s in ms.split("\n"):
|
|
|
|
if s.len() > 0:
|
|
|
|
list.add(s)
|
|
|
|
|
|
|
|
result = list
|
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
proc handle*(m: MultistreamSelect, conn: Connection) {.async, gcsafe.} =
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "handle: starting multistream handling"
|
2020-05-23 17:10:22 +00:00
|
|
|
try:
|
2019-12-04 04:44:54 +00:00
|
|
|
while not conn.closed:
|
2020-05-08 20:58:23 +00:00
|
|
|
var ms = string.fromBytes(await conn.readLp(1024))
|
2019-09-06 06:49:01 +00:00
|
|
|
ms.removeSuffix("\n")
|
2020-01-07 08:02:37 +00:00
|
|
|
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "handle: got request for ", ms
|
2019-09-06 06:49:01 +00:00
|
|
|
if ms.len() <= 0:
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "handle: invalid proto"
|
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:
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "handle: sending `na` for protocol ", 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:
|
|
|
|
of "ls":
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "handle: listing protos"
|
2019-09-06 06:49:01 +00:00
|
|
|
var protos = ""
|
|
|
|
for h in m.handlers:
|
|
|
|
protos &= (h.proto & "\n")
|
|
|
|
await conn.writeLp(protos)
|
|
|
|
of Codec:
|
|
|
|
await conn.write(m.codec)
|
|
|
|
else:
|
|
|
|
for h in m.handlers:
|
|
|
|
if (not isNil(h.match) and h.match(ms)) or ms == h.proto:
|
2019-09-28 19:54:16 +00:00
|
|
|
trace "found handler for", protocol = ms
|
2019-09-06 06:49:01 +00:00
|
|
|
await conn.writeLp((h.proto & "\n"))
|
2020-05-23 17:10:22 +00:00
|
|
|
await h.protocol.handler(conn, ms)
|
|
|
|
return
|
2019-09-14 13:55:52 +00:00
|
|
|
warn "no handlers for ", protocol = ms
|
2020-05-06 16:31:47 +00:00
|
|
|
await conn.write(Na)
|
2020-05-23 17:10:22 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception in multistream", exc = exc.msg
|
|
|
|
finally:
|
|
|
|
trace "leaving multistream loop"
|
2019-08-30 05:16:55 +00:00
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
proc addHandler*[T: LPProtocol](m: MultistreamSelect,
|
2019-09-03 20:40:19 +00:00
|
|
|
codec: string,
|
2019-08-30 05:16:55 +00:00
|
|
|
protocol: T,
|
|
|
|
matcher: Matcher = nil) =
|
2019-09-14 13:55:52 +00:00
|
|
|
## register a protocol
|
2020-01-07 08:02:37 +00:00
|
|
|
# TODO: This is a bug in chronicles,
|
2019-09-14 13:55:52 +00:00
|
|
|
# it break if I uncoment this line.
|
2020-01-07 08:02:37 +00:00
|
|
|
# Which is almost the same as the
|
2019-09-14 13:55:52 +00:00
|
|
|
# one on the next override of addHandler
|
|
|
|
#
|
2019-09-14 15:55:58 +00:00
|
|
|
# trace "registering protocol", codec = codec
|
2019-09-14 13:55:52 +00:00
|
|
|
m.handlers.add(HandlerHolder(proto: codec,
|
|
|
|
protocol: protocol,
|
|
|
|
match: matcher))
|
|
|
|
|
2020-02-04 16:16:21 +00:00
|
|
|
proc addHandler*[T: LPProtoHandler](m: MultistreamSelect,
|
2019-09-14 13:55:52 +00:00
|
|
|
codec: string,
|
|
|
|
handler: T,
|
|
|
|
matcher: Matcher = nil) =
|
|
|
|
## helper to allow registering pure handlers
|
|
|
|
|
2019-09-14 15:55:58 +00:00
|
|
|
trace "registering proto handler", codec = codec
|
2019-09-14 13:55:52 +00:00
|
|
|
let protocol = new LPProtocol
|
|
|
|
protocol.codec = codec
|
|
|
|
protocol.handler = handler
|
|
|
|
|
2019-09-03 20:40:19 +00:00
|
|
|
m.handlers.add(HandlerHolder(proto: codec,
|
2019-08-30 05:16:55 +00:00
|
|
|
protocol: protocol,
|
|
|
|
match: matcher))
|