Length of version negotiation packet

This commit is contained in:
Mark Spanbroek 2020-09-21 10:52:02 +02:00 committed by markspanbroek
parent 89491c1f08
commit f54f7a4f3b
2 changed files with 38 additions and 7 deletions

View File

@ -56,16 +56,22 @@ proc `kind=`*(header: var PacketHeader, kind: PacketKind) =
header.bytes[0].bits[2] = kind.uint8.bits[6]
header.bytes[0].bits[3] = kind.uint8.bits[7]
proc destinationSlice(header: PacketHeader): Slice[int] =
let start = 6
let length = header.bytes[5].int
result = start..<start+length
proc sourceSlice(header: PacketHeader): Slice[int] =
let destinationEnd = header.destinationSlice.b + 1
let start = destinationEnd + 1
let length = header.bytes[destinationEnd].int
result = start..<start+length
proc destination*(header: PacketHeader): ConnectionId =
let length = header.bytes[5]
result = ConnectionId(header.bytes[6..<6+length])
result = ConnectionId(header.bytes[header.destinationSlice])
proc source*(header: PacketHeader): ConnectionId =
let destinationLength = header.bytes[5]
let destinationEnd = 6+destinationLength
let sourceLength = header.bytes[destinationEnd]
let sourceStart = destinationEnd + 1
result = ConnectionId(header.bytes[sourceStart..<sourceStart+sourceLength])
result = ConnectionId(header.bytes[header.sourceSlice])
proc `$`*(id: ConnectionId): string =
"0x" & cast[string](id).toHex
@ -81,3 +87,10 @@ proc `$`*(header: PacketHeader): string =
")"
proc `==`*(x: ConnectionId, y: ConnectionId): bool {.borrow.}
proc packetLength*(header: PacketHeader): int =
case header.kind:
of packetVersionNegotiation:
return header.sourceSlice.b + 1 + 4
else:
return 0

View File

@ -103,6 +103,24 @@ suite "long headers":
"source: 0xDDEEFF" &
")"
suite "version negotiation packet":
test "has a fixed length":
let header = newPacketHeader(
type0 &
version0 &
destination.len.uint8 & destination &
source.len.uint8 & source &
version1 &
@[byte('r'), byte('e'), byte('s'), byte('t')]
)
check header.packetLength ==
type0.len +
version0.len +
destination.len + 1 +
source.len + 1 +
version1.len
suite "packet numbers":
test "packet numbers are in the range 0 to 2^62-1":