2022-07-01 20:19:57 +02:00
|
|
|
# Nim-LibP2P
|
|
|
|
# Copyright (c) 2022 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.
|
2020-11-01 16:23:26 -06:00
|
|
|
##
|
2019-08-20 10:18:15 -06:00
|
|
|
|
2021-05-21 10:27:01 -06:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-07-14 02:02:16 +02:00
|
|
|
import sequtils
|
2019-09-28 13:56:13 -06:00
|
|
|
import chronos, chronicles
|
2020-06-19 11:29:43 -06:00
|
|
|
import ../stream/connection,
|
2019-09-23 16:35:23 -06:00
|
|
|
../multiaddress,
|
2021-03-18 09:20:36 -06:00
|
|
|
../multicodec,
|
|
|
|
../upgrademngrs/upgrade
|
2019-08-20 10:18:15 -06:00
|
|
|
|
2020-12-01 11:34:27 -06:00
|
|
|
logScope:
|
|
|
|
topics = "libp2p transport"
|
|
|
|
|
2019-08-20 10:18:15 -06:00
|
|
|
type
|
2021-06-02 07:39:10 -06:00
|
|
|
TransportError* = object of LPError
|
2021-11-24 14:01:12 -06:00
|
|
|
TransportInvalidAddrError* = object of TransportError
|
2021-06-02 07:39:10 -06:00
|
|
|
TransportClosedError* = object of TransportError
|
2019-08-20 10:18:15 -06:00
|
|
|
|
|
|
|
Transport* = ref object of RootObj
|
2021-12-16 11:05:20 +01:00
|
|
|
addrs*: seq[MultiAddress]
|
2020-11-18 20:06:42 -06:00
|
|
|
running*: bool
|
2021-03-18 09:20:36 -06:00
|
|
|
upgrader*: Upgrade
|
2020-11-18 20:06:42 -06:00
|
|
|
|
2021-06-02 07:39:10 -06:00
|
|
|
proc newTransportClosedError*(parent: ref Exception = nil): ref LPError =
|
2020-11-18 20:06:42 -06:00
|
|
|
newException(TransportClosedError,
|
|
|
|
"Transport closed, no more connections!", parent)
|
2020-05-08 18:08:38 +02:00
|
|
|
|
2021-03-18 09:20:36 -06:00
|
|
|
method start*(
|
|
|
|
self: Transport,
|
2021-11-24 14:01:12 -06:00
|
|
|
addrs: seq[MultiAddress]) {.base, async.} =
|
2020-11-18 20:06:42 -06:00
|
|
|
## start the transport
|
|
|
|
##
|
|
|
|
|
2021-11-24 14:01:12 -06:00
|
|
|
trace "starting transport on addrs", address = $addrs
|
|
|
|
self.addrs = addrs
|
2021-06-30 10:59:30 +02:00
|
|
|
self.running = true
|
2020-11-18 20:06:42 -06:00
|
|
|
|
2021-11-24 14:01:12 -06:00
|
|
|
method stop*(self: Transport) {.base, async.} =
|
2019-08-21 16:53:16 -06:00
|
|
|
## stop and cleanup the transport
|
|
|
|
## including all outstanding connections
|
2020-11-18 20:06:42 -06:00
|
|
|
##
|
|
|
|
|
2021-11-24 14:01:12 -06:00
|
|
|
trace "stopping transport", address = $self.addrs
|
2021-06-30 10:59:30 +02:00
|
|
|
self.running = false
|
2019-08-20 10:18:15 -06:00
|
|
|
|
2021-03-18 09:20:36 -06:00
|
|
|
method accept*(self: Transport): Future[Connection]
|
|
|
|
{.base, gcsafe.} =
|
2020-11-18 20:06:42 -06:00
|
|
|
## accept incoming connections
|
|
|
|
##
|
|
|
|
|
2021-06-30 10:59:30 +02:00
|
|
|
doAssert(false, "Not implemented!")
|
2019-08-20 10:18:15 -06:00
|
|
|
|
2021-03-18 09:20:36 -06:00
|
|
|
method dial*(
|
|
|
|
self: Transport,
|
2021-11-08 13:02:03 +01:00
|
|
|
hostname: string,
|
2021-03-18 09:20:36 -06:00
|
|
|
address: MultiAddress): Future[Connection] {.base, gcsafe.} =
|
2019-08-20 10:18:15 -06:00
|
|
|
## dial a peer
|
2020-11-18 20:06:42 -06:00
|
|
|
##
|
|
|
|
|
2021-06-30 10:59:30 +02:00
|
|
|
doAssert(false, "Not implemented!")
|
2019-08-20 10:18:15 -06:00
|
|
|
|
2021-11-08 13:02:03 +01:00
|
|
|
proc dial*(
|
|
|
|
self: Transport,
|
|
|
|
address: MultiAddress): Future[Connection] {.gcsafe.} =
|
|
|
|
self.dial("", address)
|
|
|
|
|
2021-03-18 09:20:36 -06:00
|
|
|
method upgradeIncoming*(
|
|
|
|
self: Transport,
|
|
|
|
conn: Connection): Future[void] {.base, gcsafe.} =
|
2019-08-30 16:16:37 -06:00
|
|
|
## base upgrade method that the transport uses to perform
|
|
|
|
## transport specific upgrades
|
2020-11-18 20:06:42 -06:00
|
|
|
##
|
|
|
|
|
2021-06-30 10:59:30 +02:00
|
|
|
self.upgrader.upgradeIncoming(conn)
|
2021-03-18 09:20:36 -06:00
|
|
|
|
|
|
|
method upgradeOutgoing*(
|
|
|
|
self: Transport,
|
|
|
|
conn: Connection): Future[Connection] {.base, gcsafe.} =
|
|
|
|
## base upgrade method that the transport uses to perform
|
|
|
|
## transport specific upgrades
|
|
|
|
##
|
|
|
|
|
2021-06-30 10:59:30 +02:00
|
|
|
self.upgrader.upgradeOutgoing(conn)
|
2019-08-30 16:16:37 -06:00
|
|
|
|
2021-03-18 09:20:36 -06:00
|
|
|
method handles*(
|
|
|
|
self: Transport,
|
|
|
|
address: MultiAddress): bool {.base, gcsafe.} =
|
2020-11-18 20:06:42 -06:00
|
|
|
## check if transport supports the multiaddress
|
|
|
|
##
|
2019-12-03 22:44:54 -06:00
|
|
|
|
2020-03-27 08:25:52 -06:00
|
|
|
# by default we skip circuit addresses to avoid
|
2019-12-03 22:44:54 -06:00
|
|
|
# having to repeat the check in every transport
|
2021-05-21 10:27:01 -06:00
|
|
|
if address.protocols.isOk:
|
2021-08-03 15:48:03 +02:00
|
|
|
return address.protocols
|
|
|
|
.get()
|
|
|
|
.filterIt(
|
|
|
|
it == multiCodec("p2p-circuit")
|
|
|
|
).len == 0
|