2023-03-14 16:17:39 +00:00
|
|
|
# Copyright (c) 2020-2023 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.
|
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
# uTP packet format as specified in:
|
|
|
|
# https://www.bittorrent.org/beps/bep_0029.html
|
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
{.push raises: [].}
|
2021-09-13 12:54:06 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
faststreams,
|
2021-12-10 15:28:00 +00:00
|
|
|
chronos,
|
2022-07-09 08:55:15 +00:00
|
|
|
stew/[endians2, results, objects],
|
2021-09-13 12:54:06 +00:00
|
|
|
../p2p/discoveryv5/random2
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
export results, random2
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
const
|
2021-12-10 15:28:00 +00:00
|
|
|
minimalHeaderSize = 20
|
2021-12-15 12:35:17 +00:00
|
|
|
minimalHeaderSizeWithSelectiveAck = 26
|
2021-12-10 15:28:00 +00:00
|
|
|
protocolVersion = 1
|
|
|
|
zeroMoment = Moment.init(0, Nanosecond)
|
2021-12-15 12:35:17 +00:00
|
|
|
acksArrayLength: uint8 = 4
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
type
|
2021-09-13 12:54:06 +00:00
|
|
|
PacketType* = enum
|
|
|
|
ST_DATA = 0,
|
|
|
|
ST_FIN = 1,
|
|
|
|
ST_STATE = 2,
|
|
|
|
ST_RESET = 3,
|
|
|
|
ST_SYN = 4
|
|
|
|
|
|
|
|
MicroSeconds = uint32
|
|
|
|
|
2022-01-07 09:38:36 +00:00
|
|
|
PacketHeaderV1* = object
|
2021-09-13 12:54:06 +00:00
|
|
|
pType*: PacketType
|
|
|
|
version*: uint8
|
|
|
|
extension*: uint8
|
|
|
|
connectionId*: uint16
|
|
|
|
timestamp*: MicroSeconds
|
|
|
|
timestampDiff*: MicroSeconds
|
|
|
|
wndSize*: uint32
|
|
|
|
seqNr*: uint16
|
|
|
|
ackNr*: uint16
|
|
|
|
|
2022-01-04 08:52:38 +00:00
|
|
|
SelectiveAckExtension* = object
|
|
|
|
acks*: array[4, byte]
|
2021-12-15 12:35:17 +00:00
|
|
|
|
2021-09-13 12:54:06 +00:00
|
|
|
Packet* = object
|
|
|
|
header*: PacketHeaderV1
|
2021-12-15 12:35:17 +00:00
|
|
|
eack*: Option[SelectiveAckExtension]
|
2021-09-13 12:54:06 +00:00
|
|
|
payload*: seq[uint8]
|
|
|
|
|
2021-12-10 15:28:00 +00:00
|
|
|
TimeStampInfo* = object
|
|
|
|
moment*: Moment
|
|
|
|
timestamp*: uint32
|
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
# Important timing assumptions for uTP protocol here:
|
2021-09-13 12:54:06 +00:00
|
|
|
# 1. Microsecond precisions
|
|
|
|
# 2. Monotonicity
|
2023-03-14 16:17:39 +00:00
|
|
|
# Reference lib has a lot of checks to assume that this is monotonic on
|
2022-11-16 16:44:00 +00:00
|
|
|
# every system, and warnings when monotonic clock is not available.
|
2021-12-20 12:14:50 +00:00
|
|
|
proc getMonoTimestamp*(): TimeStampInfo =
|
2021-12-10 15:28:00 +00:00
|
|
|
let currentMoment = Moment.now()
|
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
# Casting this value from int64 to uint32, may lead to some sudden spikes in
|
|
|
|
# timestamp numeric values, i.e it is possible that the timestamp will
|
|
|
|
# suddenly change from 4294967296 to for example 10. This may lead to sudden
|
|
|
|
# spikes in calculated delays.
|
|
|
|
# The uTP implementation is resistant to those spikes are as it keeps a
|
|
|
|
# history of several last delays and uses the smallest one for calculating
|
|
|
|
# the ledbat window, thus any outlier value will be ignored.
|
2021-12-10 15:28:00 +00:00
|
|
|
let timestamp = uint32((currentMoment - zeroMoment).microseconds())
|
|
|
|
TimeStampInfo(moment: currentMoment, timestamp: timestamp)
|
2021-09-13 12:54:06 +00:00
|
|
|
|
|
|
|
# Simple generator, not useful for cryptography
|
2022-06-17 20:45:37 +00:00
|
|
|
proc randUint16*(rng: var HmacDrbgContext): uint16 =
|
2021-09-13 12:54:06 +00:00
|
|
|
uint16(rand(rng, int(high(uint16))))
|
|
|
|
|
|
|
|
# Simple generator, not useful for cryptography
|
2022-06-17 20:45:37 +00:00
|
|
|
proc randUint32*(rng: var HmacDrbgContext): uint32 =
|
2021-09-13 12:54:06 +00:00
|
|
|
uint32(rand(rng, int(high(uint32))))
|
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
func encodeTypeVer(h: PacketHeaderV1): uint8 =
|
2021-09-13 12:54:06 +00:00
|
|
|
var typeVer = 0'u8
|
|
|
|
let typeOrd = uint8(ord(h.pType))
|
|
|
|
typeVer = (typeVer and 0xf0) or (h.version and 0xf)
|
|
|
|
typeVer = (typeVer and 0xf) or (typeOrd shl 4)
|
|
|
|
typeVer
|
|
|
|
|
2021-10-06 09:36:37 +00:00
|
|
|
proc encodeHeaderStream(s: var OutputStream, h: PacketHeaderV1) =
|
2021-09-13 12:54:06 +00:00
|
|
|
try:
|
2021-10-06 09:36:37 +00:00
|
|
|
s.write(encodeTypeVer(h))
|
|
|
|
s.write(h.extension)
|
|
|
|
s.write(h.connectionId.toBytesBE())
|
|
|
|
s.write(h.timestamp.toBytesBE())
|
|
|
|
s.write(h.timestampDiff.toBytesBE())
|
|
|
|
s.write(h.wndSize.toBytesBE())
|
|
|
|
s.write(h.seqNr.toBytesBE())
|
|
|
|
s.write(h.ackNr.toBytesBE())
|
2021-09-13 12:54:06 +00:00
|
|
|
except IOError as e:
|
2021-10-06 09:36:37 +00:00
|
|
|
# This should not happen in case of in-memory streams
|
2021-09-13 12:54:06 +00:00
|
|
|
raiseAssert e.msg
|
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
proc encodeExtensionStream(s: var OutputStream, e: SelectiveAckExtension) =
|
|
|
|
try:
|
2023-03-14 16:17:39 +00:00
|
|
|
# writing always 0 as there are no other extensions (only selective ack)
|
2021-12-15 12:35:17 +00:00
|
|
|
s.write(0'u8)
|
|
|
|
s.write(acksArrayLength)
|
|
|
|
s.write(e.acks)
|
|
|
|
except IOError as e:
|
|
|
|
# This should not happen in case of in-memory streams
|
|
|
|
raiseAssert e.msg
|
|
|
|
|
2021-09-13 12:54:06 +00:00
|
|
|
proc encodePacket*(p: Packet): seq[byte] =
|
2021-10-06 09:36:37 +00:00
|
|
|
var s = memoryOutput().s
|
2021-09-13 12:54:06 +00:00
|
|
|
try:
|
2021-10-06 09:36:37 +00:00
|
|
|
encodeHeaderStream(s, p.header)
|
2021-12-15 12:35:17 +00:00
|
|
|
if (p.eack.isSome()):
|
|
|
|
encodeExtensionStream(s, p.eack.unsafeGet())
|
2021-09-13 12:54:06 +00:00
|
|
|
if (len(p.payload) > 0):
|
2021-10-06 09:36:37 +00:00
|
|
|
s.write(p.payload)
|
|
|
|
s.getOutput()
|
2021-09-13 12:54:06 +00:00
|
|
|
except IOError as e:
|
2021-10-06 09:36:37 +00:00
|
|
|
# This should not happen in case of in-memory streams
|
2021-09-13 12:54:06 +00:00
|
|
|
raiseAssert e.msg
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
func decodePacket*(bytes: openArray[byte]): Result[Packet, string] =
|
2021-12-15 12:35:17 +00:00
|
|
|
let receivedBytesLength = len(bytes)
|
|
|
|
if receivedBytesLength < minimalHeaderSize:
|
2023-03-14 16:17:39 +00:00
|
|
|
return err("Invalid header size")
|
2021-09-13 12:54:06 +00:00
|
|
|
|
|
|
|
let version = bytes[0] and 0xf
|
|
|
|
if version != protocolVersion:
|
2023-03-14 16:17:39 +00:00
|
|
|
return err("Invalid packet version")
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-09-13 12:54:06 +00:00
|
|
|
var kind: PacketType
|
|
|
|
if not checkedEnumAssign(kind, (bytes[0] shr 4)):
|
|
|
|
return err("Invalid message type")
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
let extensionByte = bytes[1]
|
|
|
|
|
|
|
|
if (not (extensionByte == 0 or extensionByte == 1)):
|
|
|
|
return err("Invalid extension type")
|
|
|
|
|
2021-09-13 12:54:06 +00:00
|
|
|
let header =
|
|
|
|
PacketHeaderV1(
|
|
|
|
pType: kind,
|
|
|
|
version: version,
|
2021-12-15 12:35:17 +00:00
|
|
|
extension: extensionByte,
|
2021-10-06 09:36:37 +00:00
|
|
|
connection_id: fromBytesBE(uint16, bytes.toOpenArray(2, 3)),
|
|
|
|
timestamp: fromBytesBE(uint32, bytes.toOpenArray(4, 7)),
|
|
|
|
timestamp_diff: fromBytesBE(uint32, bytes.toOpenArray(8, 11)),
|
|
|
|
wnd_size: fromBytesBE(uint32, bytes.toOpenArray(12, 15)),
|
|
|
|
seq_nr: fromBytesBE(uint16, bytes.toOpenArray(16, 17)),
|
|
|
|
ack_nr: fromBytesBE(uint16, bytes.toOpenArray(18, 19)),
|
2021-09-13 12:54:06 +00:00
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
if extensionByte == 0:
|
2023-03-14 16:17:39 +00:00
|
|
|
# packet without extensions
|
2021-12-15 12:35:17 +00:00
|
|
|
let payload =
|
|
|
|
if (receivedBytesLength == minimalHeaderSize):
|
|
|
|
@[]
|
|
|
|
else:
|
2021-12-28 07:08:05 +00:00
|
|
|
bytes[minimalHeaderSize..^1]
|
2021-12-15 12:35:17 +00:00
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
return ok(Packet(
|
|
|
|
header: header,
|
|
|
|
eack: none[SelectiveAckExtension](),
|
|
|
|
payload: payload))
|
2021-12-15 12:35:17 +00:00
|
|
|
else:
|
2023-03-14 16:17:39 +00:00
|
|
|
# packet with the selective ack extension
|
2021-12-15 12:35:17 +00:00
|
|
|
if (receivedBytesLength < minimalHeaderSizeWithSelectiveAck):
|
2023-03-14 16:17:39 +00:00
|
|
|
return err("Packet too short for selective ack extension: " &
|
|
|
|
"len = " & $receivedBytesLength)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
let nextExtension = bytes[20]
|
|
|
|
let extLength = bytes[21]
|
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
# The byte for nextExtension must be 0 as selective ack is currently the
|
|
|
|
# only supported extension.
|
|
|
|
# For extLength, the specification states that it must be at least 4, and
|
|
|
|
# in multiples of 4. However, the reference implementation always uses a 4
|
|
|
|
# bytes bit mask, which makes sense as a 4 byte bit mask is able to ack
|
|
|
|
# 32 packets in the future, which is sounds more than enough.
|
2021-12-15 12:35:17 +00:00
|
|
|
if (nextExtension != 0 or extLength != 4):
|
2023-03-14 16:17:39 +00:00
|
|
|
return err("Bad format of selective ack extension: " &
|
|
|
|
"extension = " & $nextExtension & " len = " & $extLength)
|
2021-12-15 12:35:17 +00:00
|
|
|
|
|
|
|
let extension = SelectiveAckExtension(
|
2021-12-28 07:08:05 +00:00
|
|
|
acks: toArray(4, bytes.toOpenArray(22, 25))
|
2021-12-20 12:14:50 +00:00
|
|
|
)
|
2021-12-15 12:35:17 +00:00
|
|
|
|
|
|
|
let payload =
|
|
|
|
if (receivedBytesLength == minimalHeaderSizeWithSelectiveAck):
|
|
|
|
@[]
|
|
|
|
else:
|
2021-12-28 07:08:05 +00:00
|
|
|
bytes[minimalHeaderSizeWithSelectiveAck..^1]
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
return ok(Packet(header: header, eack: some(extension), payload: payload))
|
2021-09-13 12:54:06 +00:00
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
proc modifyTimeStampAndAckNr*(
|
|
|
|
packetBytes: var seq[byte], newTimestamp: uint32, newAckNr: uint16) =
|
|
|
|
## Modifies timestamp and ack nr of already encoded packets. These fields
|
|
|
|
## must be filled right before sending, so when re-sending the packet they
|
|
|
|
## can be updated without decoding and re-encoding the packet.
|
2021-12-10 09:18:00 +00:00
|
|
|
doAssert(len(packetBytes) >= minimalHeaderSize)
|
|
|
|
packetBytes[4..7] = toBytesBE(newTimestamp)
|
|
|
|
packetBytes[18..19] = toBytesBE(newAckNr)
|
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
proc synPacket*(
|
|
|
|
seqNr: uint16, rcvConnectionId: uint16, bufferSize: uint32): Packet =
|
|
|
|
# rcvConnectionId - should be a random, not already used, number
|
|
|
|
# bufferSize - should be a pre-configured initial buffer size for the socket
|
|
|
|
# SYN packets are special and have the conn_id_recv in the conn_id field,
|
|
|
|
# instead of conn_id_send.
|
2021-09-13 12:54:06 +00:00
|
|
|
let h = PacketHeaderV1(
|
|
|
|
pType: ST_SYN,
|
|
|
|
version: protocolVersion,
|
|
|
|
extension: 0'u8,
|
2021-10-11 12:16:06 +00:00
|
|
|
connectionId: rcvConnectionId,
|
2021-12-10 15:28:00 +00:00
|
|
|
timestamp: getMonoTimestamp().timestamp,
|
2021-09-13 12:54:06 +00:00
|
|
|
timestampDiff: 0'u32,
|
|
|
|
wndSize: bufferSize,
|
2021-10-11 12:16:06 +00:00
|
|
|
seqNr: seqNr,
|
2023-03-14 16:17:39 +00:00
|
|
|
ackNr: 0'u16 # At start, no acks have been received
|
2021-09-13 12:54:06 +00:00
|
|
|
)
|
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
Packet(header: h, eack: none[SelectiveAckExtension](), payload: @[])
|
|
|
|
|
|
|
|
proc ackPacket*(
|
|
|
|
seqNr: uint16,
|
|
|
|
sndConnectionId: uint16,
|
|
|
|
ackNr: uint16,
|
|
|
|
bufferSize: uint32,
|
|
|
|
timestampDiff: uint32,
|
|
|
|
acksBitmask: Option[array[4, byte]] = none[array[4, byte]]()
|
2021-12-20 12:14:50 +00:00
|
|
|
): Packet =
|
2021-12-15 12:35:17 +00:00
|
|
|
|
|
|
|
let (extensionByte, extensionData) =
|
|
|
|
if acksBitmask.isSome():
|
|
|
|
(1'u8, some(SelectiveAckExtension(acks: acksBitmask.unsafeGet())))
|
|
|
|
else:
|
|
|
|
(0'u8, none[SelectiveAckExtension]())
|
2021-10-11 12:16:06 +00:00
|
|
|
|
|
|
|
let h = PacketHeaderV1(
|
|
|
|
pType: ST_STATE,
|
|
|
|
version: protocolVersion,
|
2021-12-15 12:35:17 +00:00
|
|
|
extension: extensionByte,
|
2021-10-11 12:16:06 +00:00
|
|
|
connectionId: sndConnectionId,
|
2021-12-10 15:28:00 +00:00
|
|
|
timestamp: getMonoTimestamp().timestamp,
|
2021-12-09 09:52:21 +00:00
|
|
|
timestampDiff: timestampDiff,
|
2021-10-11 12:16:06 +00:00
|
|
|
wndSize: bufferSize,
|
|
|
|
seqNr: seqNr,
|
|
|
|
ackNr: ackNr
|
|
|
|
)
|
2021-12-15 12:35:17 +00:00
|
|
|
|
|
|
|
Packet(header: h, eack: extensionData, payload: @[])
|
2021-10-19 11:36:57 +00:00
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
proc dataPacket*(
|
|
|
|
seqNr: uint16,
|
|
|
|
sndConnectionId: uint16,
|
|
|
|
ackNr: uint16,
|
|
|
|
bufferSize: uint32,
|
|
|
|
payload: seq[byte],
|
|
|
|
timestampDiff: uint32
|
2021-12-20 12:14:50 +00:00
|
|
|
): Packet =
|
2021-10-19 11:36:57 +00:00
|
|
|
let h = PacketHeaderV1(
|
|
|
|
pType: ST_DATA,
|
|
|
|
version: protocolVersion,
|
2023-03-14 16:17:39 +00:00
|
|
|
extension: 0'u8, # data packets always have extension field set to 0
|
2021-10-19 11:36:57 +00:00
|
|
|
connectionId: sndConnectionId,
|
2021-12-10 15:28:00 +00:00
|
|
|
timestamp: getMonoTimestamp().timestamp,
|
2021-12-09 09:52:21 +00:00
|
|
|
timestampDiff: timestampDiff,
|
2021-10-19 11:36:57 +00:00
|
|
|
wndSize: bufferSize,
|
|
|
|
seqNr: seqNr,
|
|
|
|
ackNr: ackNr
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
Packet(header: h, eack: none[SelectiveAckExtension](), payload: payload)
|
2021-11-05 08:41:41 +00:00
|
|
|
|
2023-03-14 16:17:39 +00:00
|
|
|
proc resetPacket*(
|
|
|
|
seqNr: uint16, sndConnectionId: uint16, ackNr: uint16): Packet =
|
2021-11-05 08:41:41 +00:00
|
|
|
let h = PacketHeaderV1(
|
|
|
|
pType: ST_RESET,
|
|
|
|
version: protocolVersion,
|
2023-03-14 16:17:39 +00:00
|
|
|
extension: 0'u8, # reset packets always have extension field set to 0
|
2021-11-05 08:41:41 +00:00
|
|
|
connectionId: sndConnectionId,
|
2021-12-10 15:28:00 +00:00
|
|
|
timestamp: getMonoTimestamp().timestamp,
|
2023-03-14 16:17:39 +00:00
|
|
|
# reset packet informs remote about lack of state for given connection,
|
|
|
|
# therefore the remote is not informed about its delay.
|
2021-12-09 09:52:21 +00:00
|
|
|
timestampDiff: 0,
|
2021-11-05 08:41:41 +00:00
|
|
|
wndSize: 0,
|
|
|
|
seqNr: seqNr,
|
|
|
|
ackNr: ackNr
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
Packet(header: h, eack: none[SelectiveAckExtension](), payload: @[])
|
2021-11-09 14:29:59 +00:00
|
|
|
|
2021-12-09 09:52:21 +00:00
|
|
|
proc finPacket*(
|
|
|
|
seqNr: uint16,
|
|
|
|
sndConnectionId: uint16,
|
|
|
|
ackNr: uint16,
|
|
|
|
bufferSize: uint32,
|
|
|
|
timestampDiff: uint32
|
2021-12-20 12:14:50 +00:00
|
|
|
): Packet =
|
2021-11-09 14:29:59 +00:00
|
|
|
let h = PacketHeaderV1(
|
|
|
|
pType: ST_FIN,
|
|
|
|
version: protocolVersion,
|
2023-03-14 16:17:39 +00:00
|
|
|
extension: 0'u8, # fin packets always have extension field set to 0
|
2021-11-09 14:29:59 +00:00
|
|
|
connectionId: sndConnectionId,
|
2021-12-10 15:28:00 +00:00
|
|
|
timestamp: getMonoTimestamp().timestamp,
|
2021-12-09 09:52:21 +00:00
|
|
|
timestampDiff: timestampDiff,
|
2021-11-09 14:29:59 +00:00
|
|
|
wndSize: bufferSize,
|
|
|
|
seqNr: seqNr,
|
|
|
|
ackNr: ackNr
|
|
|
|
)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-12-15 12:35:17 +00:00
|
|
|
Packet(header: h, eack: none[SelectiveAckExtension](), payload: @[])
|