Convert 'supportedVersion' to a field in PacketHeader object

This commit is contained in:
Mark Spanbroek 2020-09-21 18:00:25 +02:00 committed by markspanbroek
parent 3bea5a879e
commit d1b461dca1
2 changed files with 15 additions and 10 deletions

View File

@ -23,7 +23,7 @@ type
of packetInitial, packet0RTT, packetHandshake, packetRetry:
version*: uint32
of packetVersionNegotiation:
discard
supportedVersion*: uint32
destination*: ConnectionId
source*: ConnectionId
bytes: seq[byte]
@ -94,6 +94,17 @@ proc findSource(datagram: seq[byte]): Slice[int] =
proc readSource(datagram: seq[byte]): ConnectionId =
result = ConnectionId(datagram[datagram.findSource()])
proc findSupportedVersion(datagram: seq[byte]): Slice[int] =
let start = datagram.findSource().b + 1
result = start..<start+4
proc readSupportedVersion*(datagram: seq[byte]): uint32 =
let versionBytes = datagram[datagram.findSupportedVersion()]
result.bytes[0] = versionBytes[0]
result.bytes[1] = versionBytes[1]
result.bytes[2] = versionBytes[2]
result.bytes[3] = versionBytes[3]
proc newPacketHeader*(datagram: seq[byte]): PacketHeader =
let form = datagram.readForm()
datagram.readFixedBit()
@ -106,7 +117,8 @@ proc newPacketHeader*(datagram: seq[byte]): PacketHeader =
let source = datagram.readSource()
case kind
of packetVersionNegotiation:
result = PacketHeader(form: form, kind: kind, destination: destination, source: source, bytes: datagram)
let supportedVersion = datagram.readSupportedVersion()
result = PacketHeader(form: form, kind: kind, destination: destination, source: source, supportedVersion: supportedVersion, bytes: datagram)
else:
let version = datagram.readVersion()
result = PacketHeader(form: form, kind:kind, version: version, destination: destination, source: source, bytes: datagram)
@ -137,13 +149,6 @@ proc supportedVersionSlice(header: PacketHeader): Slice[int] =
let start = header.sourceSlice().b + 1
result = start..<start+4
proc supportedVersion*(header: PacketHeader): uint32 =
let versionBytes = header.bytes[header.supportedVersionSlice]
result.bytes[0] = versionBytes[0]
result.bytes[1] = versionBytes[1]
result.bytes[2] = versionBytes[2]
result.bytes[3] = versionBytes[3]
proc `$`*(id: ConnectionId): string =
"0x" & cast[string](id).toHex

View File

@ -72,7 +72,7 @@ suite "long headers":
check datagram[1..4] == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
test "version negotiation packet is a packet with version 0":
let header = newPacketHeader(type0 & version0 & destination.len.uint8 & destination & source.len.uint8 & source)
let header = newPacketHeader(type0 & version0 & destination.len.uint8 & destination & source.len.uint8 & source & version1)
check header.kind == packetVersionNegotiation
test "initial packet is a long packet of type 0":