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.writeKeyPhase(datagram)
writer.writeShortDestination(datagram) writer.writeShortDestination(datagram)
writer.writePacketNumber(datagram) writer.writePacketNumber(datagram)
writer.writePayload(datagram)
of formLong: of formLong:
writer.writeKind(datagram) writer.writeKind(datagram)
writer.writeVersion(datagram) writer.writeVersion(datagram)

View File

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

View File

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

View File

@ -158,6 +158,12 @@ suite "packet writing":
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber) check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
check datagram[1..2] == @[0xAA'u8, 0xBB'u8] 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": suite "packet reading":
var datagram: seq[byte] var datagram: seq[byte]