2019-08-20 16:18:15 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-20 16:18:15 +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-19 02:06:42 +00:00
|
|
|
import std/[oids, sequtils]
|
|
|
|
import chronos, chronicles
|
2019-09-24 16:16:39 +00:00
|
|
|
import transport,
|
2020-04-21 01:24:42 +00:00
|
|
|
../errors,
|
2019-09-24 16:16:39 +00:00
|
|
|
../wire,
|
|
|
|
../multiaddress,
|
|
|
|
../multicodec,
|
2020-06-19 17:29:43 +00:00
|
|
|
../stream/connection,
|
2019-09-24 16:16:39 +00:00
|
|
|
../stream/chronosstream
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-09-12 00:15:04 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p tcptransport"
|
2019-09-12 00:15:04 +00:00
|
|
|
|
2020-11-28 08:05:12 +00:00
|
|
|
export transport
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
const
|
|
|
|
TcpTransportTrackerName* = "libp2p.tcptransport"
|
|
|
|
|
|
|
|
type
|
|
|
|
TcpTransport* = ref object of Transport
|
|
|
|
server*: StreamServer
|
2020-11-19 02:06:42 +00:00
|
|
|
clients: array[Direction, seq[StreamTransport]]
|
2020-05-18 19:04:05 +00:00
|
|
|
flags: set[ServerFlags]
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
TcpTransportTracker* = ref object of TrackerBase
|
|
|
|
opened*: uint64
|
|
|
|
closed*: uint64
|
|
|
|
|
2021-03-23 06:45:25 +00:00
|
|
|
proc setupTcpTransportTracker(): TcpTransportTracker {.gcsafe, raises: [Defect].}
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
proc getTcpTransportTracker(): TcpTransportTracker {.gcsafe.} =
|
|
|
|
result = cast[TcpTransportTracker](getTracker(TcpTransportTrackerName))
|
|
|
|
if isNil(result):
|
|
|
|
result = setupTcpTransportTracker()
|
|
|
|
|
|
|
|
proc dumpTracking(): string {.gcsafe.} =
|
|
|
|
var tracker = getTcpTransportTracker()
|
2020-05-20 00:14:15 +00:00
|
|
|
result = "Opened tcp transports: " & $tracker.opened & "\n" &
|
|
|
|
"Closed tcp transports: " & $tracker.closed
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
proc leakTransport(): bool {.gcsafe.} =
|
|
|
|
var tracker = getTcpTransportTracker()
|
|
|
|
result = (tracker.opened != tracker.closed)
|
|
|
|
|
|
|
|
proc setupTcpTransportTracker(): TcpTransportTracker =
|
|
|
|
result = new TcpTransportTracker
|
|
|
|
result.opened = 0
|
|
|
|
result.closed = 0
|
|
|
|
result.dump = dumpTracking
|
|
|
|
result.isLeaked = leakTransport
|
|
|
|
addTracker(TcpTransportTrackerName, result)
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
proc connHandler*(t: TcpTransport,
|
2019-09-24 16:16:39 +00:00
|
|
|
client: StreamTransport,
|
2020-11-19 02:06:42 +00:00
|
|
|
dir: Direction): Future[Connection] {.async.} =
|
2020-11-25 19:34:48 +00:00
|
|
|
var observedAddr: MultiAddress = MultiAddress()
|
|
|
|
try:
|
|
|
|
observedAddr = MultiAddress.init(client.remoteAddress).tryGet()
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Connection setup failed", exc = exc.msg
|
|
|
|
if not(isNil(client) and client.closed):
|
|
|
|
await client.closeWait()
|
|
|
|
raise exc
|
|
|
|
|
2021-01-04 18:59:05 +00:00
|
|
|
trace "Handling tcp connection", address = $observedAddr,
|
2020-11-19 02:06:42 +00:00
|
|
|
dir = $dir,
|
|
|
|
clients = t.clients[Direction.In].len +
|
|
|
|
t.clients[Direction.Out].len
|
2020-11-01 22:23:26 +00:00
|
|
|
|
|
|
|
let conn = Connection(
|
|
|
|
ChronosStream.init(
|
2020-11-25 19:34:48 +00:00
|
|
|
client = client,
|
|
|
|
dir = dir,
|
|
|
|
observedAddr = observedAddr
|
2020-11-19 02:06:42 +00:00
|
|
|
))
|
2020-11-01 22:23:26 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
proc onClose() {.async.} =
|
2020-05-23 17:10:01 +00:00
|
|
|
try:
|
2020-11-25 13:35:25 +00:00
|
|
|
let futs = @[client.join(), conn.join()]
|
|
|
|
await futs[0] or futs[1]
|
|
|
|
for f in futs:
|
2020-11-28 15:48:06 +00:00
|
|
|
if not f.finished: await f.cancelAndWait() # cancel outstanding join()
|
2020-11-25 13:35:25 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
trace "Cleaning up client", addrs = $client.remoteAddress,
|
|
|
|
conn
|
|
|
|
|
|
|
|
t.clients[dir].keepItIf( it != client )
|
2020-11-24 00:22:15 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
conn.close(), client.closeWait())
|
2020-11-19 02:06:42 +00:00
|
|
|
|
|
|
|
trace "Cleaned up client", addrs = $client.remoteAddress,
|
|
|
|
conn
|
|
|
|
|
2020-05-23 17:10:01 +00:00
|
|
|
except CatchableError as exc:
|
2020-11-19 02:06:42 +00:00
|
|
|
let useExc {.used.} = exc
|
|
|
|
debug "Error cleaning up client", errMsg = exc.msg, conn
|
2020-05-23 17:10:01 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
t.clients[dir].add(client)
|
|
|
|
asyncSpawn onClose()
|
2020-11-02 20:35:26 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
return conn
|
|
|
|
|
|
|
|
proc init*(T: type TcpTransport,
|
|
|
|
flags: set[ServerFlags] = {}): T =
|
2020-05-18 19:04:05 +00:00
|
|
|
result = T(flags: flags)
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
result.initTransport()
|
|
|
|
|
|
|
|
method initTransport*(t: TcpTransport) =
|
2019-08-20 16:18:15 +00:00
|
|
|
t.multicodec = multiCodec("tcp")
|
2020-04-21 01:24:42 +00:00
|
|
|
inc getTcpTransportTracker().opened
|
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
method start*(t: TcpTransport, ma: MultiAddress) {.async.} =
|
|
|
|
## listen on the transport
|
|
|
|
##
|
2020-05-27 20:46:25 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
if t.running:
|
|
|
|
trace "TCP transport already running"
|
|
|
|
return
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
await procCall Transport(t).start(ma)
|
|
|
|
trace "Starting TCP transport"
|
2020-05-18 17:05:34 +00:00
|
|
|
|
2021-01-04 18:59:05 +00:00
|
|
|
t.server = createStreamServer(
|
|
|
|
ma = t.ma,
|
|
|
|
flags = t.flags,
|
|
|
|
udata = t)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
# always get the resolved address in case we're bound to 0.0.0.0:0
|
|
|
|
t.ma = MultiAddress.init(t.server.sock.getLocalAddress()).tryGet()
|
|
|
|
t.running = true
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
trace "Listening on", address = t.ma
|
|
|
|
|
|
|
|
method stop*(t: TcpTransport) {.async, gcsafe.} =
|
|
|
|
## stop the transport
|
|
|
|
##
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2021-01-04 18:59:05 +00:00
|
|
|
t.running = false # mark stopped as soon as possible
|
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
try:
|
|
|
|
trace "Stopping TCP transport"
|
|
|
|
await procCall Transport(t).stop() # call base
|
2020-04-11 04:08:25 +00:00
|
|
|
|
2020-05-27 20:46:25 +00:00
|
|
|
checkFutures(
|
2020-11-19 02:06:42 +00:00
|
|
|
await allFinished(
|
|
|
|
t.clients[Direction.In].mapIt(it.closeWait()) &
|
|
|
|
t.clients[Direction.Out].mapIt(it.closeWait())))
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
# server can be nil
|
|
|
|
if not isNil(t.server):
|
|
|
|
await t.server.closeWait()
|
|
|
|
|
|
|
|
t.server = nil
|
|
|
|
trace "Transport stopped"
|
2020-05-27 20:46:25 +00:00
|
|
|
inc getTcpTransportTracker().closed
|
|
|
|
except CatchableError as exc:
|
2020-11-19 02:06:42 +00:00
|
|
|
trace "Error shutting down tcp transport", exc = exc.msg
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
method accept*(t: TcpTransport): Future[Connection] {.async, gcsafe.} =
|
|
|
|
## accept a new TCP connection
|
|
|
|
##
|
2019-09-25 17:36:39 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
if not t.running:
|
|
|
|
raise newTransportClosedError()
|
|
|
|
|
2020-11-19 15:10:25 +00:00
|
|
|
try:
|
2020-11-19 02:06:42 +00:00
|
|
|
let transp = await t.server.accept()
|
|
|
|
return await t.connHandler(transp, Direction.In)
|
2020-11-19 15:10:25 +00:00
|
|
|
except TransportOsError as exc:
|
|
|
|
# TODO: it doesn't sound like all OS errors
|
|
|
|
# can be ignored, we should re-raise those
|
|
|
|
# that can't.
|
|
|
|
debug "OS Error", exc = exc.msg
|
|
|
|
except TransportTooManyError as exc:
|
2021-01-04 18:59:05 +00:00
|
|
|
debug "Too many files opened", exc = exc.msg
|
2020-11-19 15:10:25 +00:00
|
|
|
except TransportUseClosedError as exc:
|
2021-01-04 18:59:05 +00:00
|
|
|
debug "Server was closed", exc = exc.msg
|
2020-11-19 15:10:25 +00:00
|
|
|
raise newTransportClosedError(exc)
|
|
|
|
except CatchableError as exc:
|
2021-01-04 18:59:05 +00:00
|
|
|
warn "Unexpected error creating connection", exc = exc.msg
|
2020-11-19 15:10:25 +00:00
|
|
|
raise exc
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
method dial*(t: TcpTransport,
|
2019-09-24 16:16:39 +00:00
|
|
|
address: MultiAddress):
|
2019-08-30 22:16:37 +00:00
|
|
|
Future[Connection] {.async, gcsafe.} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## dial a peer
|
2020-11-19 02:06:42 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
trace "Dialing remote peer", address = $address
|
|
|
|
|
2020-11-19 15:10:25 +00:00
|
|
|
let transp = await connect(address)
|
|
|
|
return await t.connHandler(transp, Direction.Out)
|
2019-08-22 00:23:13 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method handles*(t: TcpTransport, address: MultiAddress): bool {.gcsafe.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
if procCall Transport(t).handles(address):
|
2020-11-19 02:06:42 +00:00
|
|
|
return address.protocols
|
|
|
|
.tryGet()
|
|
|
|
.filterIt( it == multiCodec("tcp") )
|
|
|
|
.len > 0
|