2019-08-30 05:16:55 +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.
|
|
|
|
|
2019-09-06 06:49:01 +00:00
|
|
|
import sequtils, strutils, strformat
|
2019-09-09 17:33:32 +00:00
|
|
|
import chronos, chronicles
|
2019-09-06 00:16:49 +00:00
|
|
|
import connection,
|
|
|
|
varint,
|
|
|
|
vbuffer,
|
2019-09-09 17:33:32 +00:00
|
|
|
protocols/protocol
|
2019-08-30 05:16:55 +00:00
|
|
|
|
|
|
|
const MsgSize* = 64*1024
|
|
|
|
const Codec* = "/multistream/1.0.0"
|
2019-09-06 21:25:54 +00:00
|
|
|
const MSCodec* = "\x13" & Codec & "\n"
|
2019-08-30 05:16:55 +00:00
|
|
|
const Na = "\x03na\n"
|
|
|
|
const Ls = "\x03ls\n"
|
|
|
|
|
|
|
|
type
|
|
|
|
MultisteamSelectException = object of CatchableError
|
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
|
|
|
|
|
|
|
MultisteamSelect* = ref object of RootObj
|
|
|
|
handlers*: seq[HandlerHolder]
|
|
|
|
codec*: string
|
|
|
|
na: string
|
|
|
|
ls: string
|
|
|
|
|
|
|
|
proc newMultistream*(): MultisteamSelect =
|
|
|
|
new result
|
2019-09-06 21:25:54 +00:00
|
|
|
result.codec = MSCodec
|
2019-08-30 05:16:55 +00:00
|
|
|
result.ls = Ls
|
|
|
|
result.na = Na
|
|
|
|
|
|
|
|
proc select*(m: MultisteamSelect,
|
|
|
|
conn: Connection,
|
2019-09-04 20:15:55 +00:00
|
|
|
proto: seq[string]):
|
|
|
|
Future[string] {.async.} =
|
2019-09-09 23:17:45 +00:00
|
|
|
debug "select: 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-09 17:33:32 +00:00
|
|
|
debug "select: 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
|
|
|
|
2019-09-06 00:16:49 +00:00
|
|
|
result = cast[string](await conn.readLp()) # read ms header
|
|
|
|
result.removeSuffix("\n")
|
|
|
|
if result != Codec:
|
2019-09-09 23:17:45 +00:00
|
|
|
debug "select: handshake failed", codec = result
|
2019-09-04 20:15:55 +00:00
|
|
|
return ""
|
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
|
|
|
|
2019-09-06 00:16:49 +00:00
|
|
|
result = cast[string](await conn.readLp()) # read the first proto
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "select: reading first requested proto"
|
2019-09-06 00:16:49 +00:00
|
|
|
result.removeSuffix("\n")
|
|
|
|
if result == proto[0]:
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "select: 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-09 17:33:32 +00:00
|
|
|
debug "select: 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
|
2019-09-06 00:16:49 +00:00
|
|
|
result = cast[string](await conn.readLp()) # read the first proto
|
|
|
|
result.removeSuffix("\n")
|
|
|
|
if result == p:
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "select: selected protocol", protocol = result
|
2019-09-04 18:31:13 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
proc select*(m: MultisteamSelect,
|
|
|
|
conn: Connection,
|
2019-09-06 06:49:01 +00:00
|
|
|
proto: string): Future[bool] {.async.} =
|
2019-09-08 07:43:33 +00:00
|
|
|
if proto.len > 0:
|
|
|
|
result = (await m.select(conn, @[proto])) == proto
|
|
|
|
else:
|
|
|
|
result = (await m.select(conn, @[])) == Codec
|
2019-09-04 20:15:55 +00:00
|
|
|
|
2019-09-08 07:43:33 +00:00
|
|
|
proc select*(m: MultisteamSelect, conn: Connection): Future[bool] =
|
|
|
|
m.select(conn, "")
|
2019-08-30 05:16:55 +00:00
|
|
|
|
|
|
|
proc list*(m: MultisteamSelect,
|
|
|
|
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
|
|
|
|
|
2019-09-04 20:15:55 +00:00
|
|
|
await conn.write(m.ls) # send ls
|
2019-08-30 05:16:55 +00:00
|
|
|
|
|
|
|
var list = newSeq[string]()
|
|
|
|
let ms = cast[string](await conn.readLp())
|
|
|
|
for s in ms.split("\n"):
|
|
|
|
if s.len() > 0:
|
|
|
|
list.add(s)
|
|
|
|
|
|
|
|
result = list
|
|
|
|
|
|
|
|
proc handle*(m: MultisteamSelect, conn: Connection) {.async, gcsafe.} =
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "handle: starting multistream handling"
|
2019-08-30 05:16:55 +00:00
|
|
|
while not conn.closed:
|
2019-09-06 06:49:01 +00:00
|
|
|
block main:
|
|
|
|
var ms = cast[string](await conn.readLp())
|
|
|
|
ms.removeSuffix("\n")
|
2019-09-08 07:43:33 +00:00
|
|
|
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "handle: got request for ", ms
|
2019-09-06 06:49:01 +00:00
|
|
|
if ms.len() <= 0:
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "handle: invalid proto"
|
2019-09-06 06:49:01 +00:00
|
|
|
await conn.write(m.na)
|
|
|
|
|
|
|
|
if m.handlers.len() == 0:
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "handle: sending `na` for protocol ", protocol = ms
|
2019-08-30 05:16:55 +00:00
|
|
|
await conn.write(m.na)
|
2019-09-06 06:49:01 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
case ms:
|
|
|
|
of "ls":
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "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-09 17:33:32 +00:00
|
|
|
debug "handle: found handler for", protocol = ms
|
2019-09-06 06:49:01 +00:00
|
|
|
await conn.writeLp((h.proto & "\n"))
|
|
|
|
try:
|
|
|
|
await h.protocol.handler(conn, ms)
|
|
|
|
break main
|
|
|
|
except Exception as exc:
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "handle: exception while handling ", msg = exc.msg # TODO: Logging
|
|
|
|
debug "handle: no handlers for ", protocol = ms
|
2019-09-06 06:49:01 +00:00
|
|
|
await conn.write(m.na)
|
2019-08-30 05:16:55 +00:00
|
|
|
|
|
|
|
proc addHandler*[T: LPProtocol](m: MultisteamSelect,
|
2019-09-03 20:40:19 +00:00
|
|
|
codec: string,
|
2019-08-30 05:16:55 +00:00
|
|
|
protocol: T,
|
|
|
|
matcher: Matcher = nil) =
|
|
|
|
## register a handler for the protocol
|
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))
|