Write payload of short packets

This commit is contained in:
Mark Spanbroek 2020-09-28 14:03:52 +02:00 committed by markspanbroek
parent c9534fcd61
commit 10b69064ee
4 changed files with 17 additions and 5 deletions

View File

@ -46,6 +46,7 @@ proc write*(datagram: var Datagram, packet: Packet) =
writer.writeKeyPhase(datagram)
writer.writeShortDestination(datagram)
writer.writePacketNumber(datagram)
writer.writePayload(datagram)
of formLong:
writer.writeKind(datagram)
writer.writeVersion(datagram)

View File

@ -19,6 +19,7 @@ type
spinBit*: bool
keyPhase*: bool
packetnumber*: PacketNumber
payload*: seq[byte]
PacketInitial* = object
version*: uint32
token*: seq[byte]

View File

@ -55,11 +55,15 @@ proc writeIntegrity*(writer: var PacketWriter, datagram: var Datagram) =
writer.write(datagram, writer.packet.retry.integrity)
proc payload(packet: Packet): seq[byte] =
case packet.kind
of packetHandshake: packet.handshake.payload
of packet0RTT: packet.rtt.payload
of packetInitial: packet.initial.payload
else: @[]
case packet.form
of formShort:
packet.short.payload
of formLong:
case packet.kind
of packetHandshake: packet.handshake.payload
of packet0RTT: packet.rtt.payload
of packetInitial: packet.initial.payload
else: @[]
proc packetNumber(packet: Packet): PacketNumber =
case packet.form

View File

@ -158,6 +158,12 @@ suite "packet writing":
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
check datagram[1..2] == @[0xAA'u8, 0xBB'u8]
test "writes payload for short packet":
const payload = repeat(0xAB'u8, 1024)
var packet = Packet(form: formShort, short: PacketShort(payload: payload))
datagram.write(packet)
check datagram[2..1025] == payload
suite "packet reading":
var datagram: seq[byte]