2021-12-10 10:12:24 +00:00
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
2021-09-13 12:54:06 +00:00
|
|
|
# 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
|
2022-07-09 08:55:15 +00:00
|
|
|
std/[tables, options, hashes, math],
|
2022-06-17 20:45:37 +00:00
|
|
|
chronos, chronicles,
|
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
|
|
|
|
2022-02-10 07:05:44 +00:00
|
|
|
SendCallbackBuilder* = proc (d: DatagramTransport): SendCallback[TransportAddress] {.gcsafe, raises: [Defect].}
|
|
|
|
|
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 (
|
2021-12-20 12:14:50 +00:00
|
|
|
proc (to: TransportAddress, data: seq[byte]): Future[void] =
|
2021-10-28 09:41:43 +00:00
|
|
|
t.sendTo(to, data)
|
|
|
|
)
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-10-19 11:36:57 +00:00
|
|
|
proc new*(
|
2022-08-12 13:09:59 +00:00
|
|
|
T: type UtpProtocol,
|
|
|
|
acceptConnectionCb: AcceptConnectionCallback[TransportAddress],
|
|
|
|
address: TransportAddress,
|
|
|
|
udata: pointer = nil,
|
|
|
|
socketConfig: SocketConfig = SocketConfig.init(),
|
|
|
|
allowConnectionCb: AllowConnectionCallback[TransportAddress] = nil,
|
|
|
|
sendCallbackBuilder: SendCallbackBuilder = nil,
|
|
|
|
rng = newRng()): UtpProtocol {.raises: [Defect, CatchableError].} =
|
2021-12-20 12:14:50 +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,
|
2022-08-12 13:09:59 +00:00
|
|
|
udata,
|
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)
|
2022-02-10 07:05:44 +00:00
|
|
|
|
|
|
|
if (sendCallbackBuilder == nil):
|
|
|
|
router.sendCb = initSendCallback(ta)
|
|
|
|
else:
|
|
|
|
router.sendCb = sendCallbackBuilder(ta)
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
UtpProtocol(transport: ta, utpRouter: router)
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2022-08-12 13:09:59 +00:00
|
|
|
proc new*(
|
|
|
|
T: type UtpProtocol,
|
|
|
|
acceptConnectionCb: AcceptConnectionCallback[TransportAddress],
|
|
|
|
address: TransportAddress,
|
|
|
|
udata: ref,
|
|
|
|
socketConfig: SocketConfig = SocketConfig.init(),
|
|
|
|
allowConnectionCb: AllowConnectionCallback[TransportAddress] = nil,
|
|
|
|
sendCallbackBuilder: SendCallbackBuilder = nil,
|
|
|
|
rng = newRng()): UtpProtocol {.raises: [Defect, CatchableError].} =
|
|
|
|
GC_ref(udata)
|
|
|
|
UtpProtocol.new(
|
|
|
|
acceptConnectionCb,
|
|
|
|
address,
|
|
|
|
cast[pointer](udata),
|
|
|
|
socketConfig,
|
|
|
|
allowConnectionCb,
|
|
|
|
sendCallbackBuilder,
|
|
|
|
rng
|
|
|
|
)
|
|
|
|
|
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-12-20 12:14:50 +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)
|