2021-09-13 12:54:06 +00:00
|
|
|
# Copyright (c) 2020-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2021-10-25 07:58:13 +00:00
|
|
|
std/[tables, options, hashes, sugar, math],
|
2021-09-13 12:54:06 +00:00
|
|
|
chronos, chronicles, bearssl,
|
2021-10-28 09:41:43 +00:00
|
|
|
./utp_router,
|
2021-09-13 12:54:06 +00:00
|
|
|
../keys
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "utp"
|
|
|
|
|
|
|
|
type
|
|
|
|
# For now utp protocol is tied to udp transport, but ultimatly we would like to
|
|
|
|
# abstract underlying transport to be able to run utp over udp, discoveryv5 or
|
|
|
|
# maybe some test transport
|
|
|
|
UtpProtocol* = ref object
|
|
|
|
transport: DatagramTransport
|
2021-10-28 09:41:43 +00:00
|
|
|
utpRouter: UtpRouter[TransportAddress]
|
2021-10-25 07:58:13 +00:00
|
|
|
|
2021-10-11 12:16:06 +00:00
|
|
|
# This should probably be defined in TransportAddress module, as hash function should
|
|
|
|
# be consitent with equality function
|
|
|
|
# in nim zero arrays always have hash equal to 0, irrespectively of array size, to
|
|
|
|
# avoid clashes betweend different types of addresses, each type have mixed different
|
|
|
|
# magic number
|
|
|
|
proc hash(x: TransportAddress): Hash =
|
|
|
|
var h: Hash = 0
|
|
|
|
case x.family
|
|
|
|
of AddressFamily.None:
|
|
|
|
h = h !& 31
|
|
|
|
!$h
|
|
|
|
of AddressFamily.IPv4:
|
|
|
|
h = h !& x.address_v4.hash
|
|
|
|
h = h !& x.port.hash
|
|
|
|
h = h !& 37
|
|
|
|
!$h
|
|
|
|
of AddressFamily.IPv6:
|
|
|
|
h = h !& x.address_v6.hash
|
|
|
|
h = h !& x.port.hash
|
|
|
|
h = h !& 41
|
|
|
|
!$h
|
|
|
|
of AddressFamily.Unix:
|
|
|
|
h = h !& x.address_un.hash
|
|
|
|
h = h !& 43
|
|
|
|
!$h
|
|
|
|
|
|
|
|
# Required to use socketKey as key in hashtable
|
2021-10-28 09:41:43 +00:00
|
|
|
proc hash(x: UtpSocketKey[TransportAddress]): Hash =
|
2021-10-11 12:16:06 +00:00
|
|
|
var h = 0
|
|
|
|
h = h !& x.remoteAddress.hash
|
|
|
|
h = h !& x.rcvId.hash
|
|
|
|
!$h
|
|
|
|
|
2021-09-13 12:54:06 +00:00
|
|
|
proc processDatagram(transp: DatagramTransport, raddr: TransportAddress):
|
|
|
|
Future[void] {.async.} =
|
2021-10-28 09:41:43 +00:00
|
|
|
let router = getUserData[UtpRouter[TransportAddress]](transp)
|
2021-09-13 12:54:06 +00:00
|
|
|
# TODO: should we use `peekMessage()` to avoid allocation?
|
|
|
|
let buf = try: transp.getMessage()
|
|
|
|
except TransportOsError as e:
|
|
|
|
# This is likely to be local network connection issues.
|
|
|
|
return
|
2021-10-28 09:41:43 +00:00
|
|
|
await processIncomingBytes[TransportAddress](router, buf, raddr)
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
proc initSendCallback(t: DatagramTransport): SendCallback[TransportAddress] =
|
|
|
|
return (
|
|
|
|
proc (to: TransportAddress, data: seq[byte]): Future[void] =
|
|
|
|
t.sendTo(to, data)
|
|
|
|
)
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
proc new*(
|
|
|
|
T: type UtpProtocol,
|
2021-10-28 09:41:43 +00:00
|
|
|
acceptConnectionCb: AcceptConnectionCallback[TransportAddress],
|
2021-10-25 07:58:13 +00:00
|
|
|
address: TransportAddress,
|
|
|
|
socketConfig: SocketConfig = SocketConfig.init(),
|
2021-11-18 09:05:56 +00:00
|
|
|
allowConnectionCb: AllowConnectionCallback[TransportAddress] = nil,
|
2021-10-19 11:36:57 +00:00
|
|
|
rng = newRng()): UtpProtocol {.raises: [Defect, CatchableError].} =
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
doAssert(not(isNil(acceptConnectionCb)))
|
2021-10-28 09:41:43 +00:00
|
|
|
|
|
|
|
let router = UtpRouter[TransportAddress].new(
|
|
|
|
acceptConnectionCb,
|
2021-11-18 09:05:56 +00:00
|
|
|
allowConnectionCb,
|
2021-10-28 09:41:43 +00:00
|
|
|
socketConfig,
|
|
|
|
rng
|
2021-10-25 07:58:13 +00:00
|
|
|
)
|
2021-10-28 09:41:43 +00:00
|
|
|
|
|
|
|
let ta = newDatagramTransport(processDatagram, udata = router, local = address)
|
|
|
|
router.sendCb = initSendCallback(ta)
|
|
|
|
UtpProtocol(transport: ta, utpRouter: router)
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
proc shutdownWait*(p: UtpProtocol): Future[void] {.async.} =
|
|
|
|
## closes all managed utp sockets and then underlying transport
|
|
|
|
await p.utpRouter.shutdownWait()
|
2021-10-25 07:58:13 +00:00
|
|
|
await p.transport.closeWait()
|
2021-11-09 14:29:59 +00:00
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
proc connectTo*(r: UtpProtocol, address: TransportAddress): Future[ConnectionResult[TransportAddress]] =
|
2021-10-28 09:41:43 +00:00
|
|
|
return r.utpRouter.connectTo(address)
|
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
proc connectTo*(r: UtpProtocol, address: TransportAddress, connectionId: uint16): Future[ConnectionResult[TransportAddress]] =
|
|
|
|
return r.utpRouter.connectTo(address, connectionId)
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
proc openSockets*(r: UtpProtocol): int =
|
|
|
|
len(r.utpRouter)
|