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