Combine HeaderForm and PacketKind enums into a single enum

This commit is contained in:
Mark Spanbroek 2020-09-21 12:17:06 +02:00 committed by markspanbroek
parent 9ebba09f91
commit 66f2a44545
2 changed files with 27 additions and 40 deletions

View File

@ -4,14 +4,12 @@ import strformat
import bits
type
HeaderForm* = enum
headerShort
headerLong
PacketKind* = enum
packetInitial
packet0RTT
packetHandshake
packetRetry
packetShort
packetVersionNegotiation
PacketHeader* = object
bytes: seq[byte]
@ -25,12 +23,6 @@ proc newPacketHeader*(bytes: seq[byte]): PacketHeader =
proc bytes*(header: PacketHeader): seq[byte] =
header.bytes
proc form*(header: PacketHeader): HeaderForm =
HeaderForm(header.bytes[0].bits[0])
proc `form=`*(header: var PacketHeader, form: HeaderForm) =
header.bytes[0].bits[0] = Bit(form)
proc version*(header: PacketHeader): uint32 =
result.bytes[0] = header.bytes[1]
result.bytes[1] = header.bytes[2]
@ -44,7 +36,9 @@ proc `version=`*(header: var PacketHeader, version: uint32) =
header.bytes[4] = version.bytes[3]
proc kind*(header: PacketHeader): PacketKind =
if header.version == 0:
if header.bytes[0].bits[0] == 0:
result = packetShort
elif header.version == 0:
result = packetVersionNegotiation
else:
var kind: uint8
@ -88,25 +82,22 @@ proc `$`*(id: ConnectionId): string =
"0x" & cast[string](id).toHex
proc `$`*(header: PacketHeader): string =
case header.form:
of headerShort: fmt"(form: {header.form})"
of headerLong:
case header.kind:
of packetVersionNegotiation:
"(" &
fmt"form: {header.form}, " &
fmt"kind: {header.kind}, " &
fmt"destination: {header.destination}, " &
fmt"source: {header.source}, " &
fmt"supportedVersion: {header.supportedVersion}" &
")"
else:
"(" &
fmt"form: {header.form}, " &
fmt"kind: {header.kind}, " &
fmt"destination: {header.destination}, " &
fmt"source: {header.source}" &
")"
case header.kind:
of packetShort:
fmt"(kind: {header.kind})"
of packetVersionNegotiation:
"(" &
fmt"kind: {header.kind}, " &
fmt"destination: {header.destination}, " &
fmt"source: {header.source}, " &
fmt"supportedVersion: {header.supportedVersion}" &
")"
else:
"(" &
fmt"kind: {header.kind}, " &
fmt"destination: {header.destination}, " &
fmt"source: {header.source}" &
")"
proc `==`*(x: ConnectionId, y: ConnectionId): bool {.borrow.}

View File

@ -5,14 +5,12 @@ import quic/bits
suite "packet header":
test "first bit of the header indicates its form":
check newPacketHeader(@[0b01000000'u8]).form == headerShort
check newPacketHeader(@[0b11000000'u8]).form == headerLong
test "header form can be set":
var header = newPacketHeader(@[0b01000000'u8])
header.form = headerLong
check header.bytes[0].bits[0] == 1
test "first bit of the header indicates short/long form":
var datagram = newSeq[byte](4096)
datagram[0] = 0b01000000'u8
check newPacketHeader(datagram).kind == packetShort
datagram[0] = 0b11000000'u8
check newPacketHeader(datagram).kind != packetShort
test "second bit of the header should always be 1":
expect Exception:
@ -21,7 +19,7 @@ suite "packet header":
suite "short headers":
test "conversion to string":
check $newPacketHeader(@[0b01000000'u8]) == "(form: headerShort)"
check $newPacketHeader(@[0b01000000'u8]) == "(kind: packetShort)"
suite "long headers":
@ -97,7 +95,6 @@ suite "long headers":
source.len.uint8 & source
)
check $header == "(" &
"form: headerLong, " &
"kind: packetInitial, " &
"destination: 0xAABBCC, " &
"source: 0xDDEEFF" &
@ -140,7 +137,6 @@ suite "long headers":
version1
)
check $header == "(" &
"form: headerLong, " &
"kind: packetVersionNegotiation, " &
"destination: 0xAABBCC, " &
"source: 0xDDEEFF, " &