mirror of https://github.com/vacp2p/nim-quic.git
Length of version negotiation packet
This commit is contained in:
parent
89491c1f08
commit
f54f7a4f3b
|
@ -56,16 +56,22 @@ proc `kind=`*(header: var PacketHeader, kind: PacketKind) =
|
||||||
header.bytes[0].bits[2] = kind.uint8.bits[6]
|
header.bytes[0].bits[2] = kind.uint8.bits[6]
|
||||||
header.bytes[0].bits[3] = kind.uint8.bits[7]
|
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 =
|
proc destination*(header: PacketHeader): ConnectionId =
|
||||||
let length = header.bytes[5]
|
result = ConnectionId(header.bytes[header.destinationSlice])
|
||||||
result = ConnectionId(header.bytes[6..<6+length])
|
|
||||||
|
|
||||||
proc source*(header: PacketHeader): ConnectionId =
|
proc source*(header: PacketHeader): ConnectionId =
|
||||||
let destinationLength = header.bytes[5]
|
result = ConnectionId(header.bytes[header.sourceSlice])
|
||||||
let destinationEnd = 6+destinationLength
|
|
||||||
let sourceLength = header.bytes[destinationEnd]
|
|
||||||
let sourceStart = destinationEnd + 1
|
|
||||||
result = ConnectionId(header.bytes[sourceStart..<sourceStart+sourceLength])
|
|
||||||
|
|
||||||
proc `$`*(id: ConnectionId): string =
|
proc `$`*(id: ConnectionId): string =
|
||||||
"0x" & cast[string](id).toHex
|
"0x" & cast[string](id).toHex
|
||||||
|
@ -81,3 +87,10 @@ proc `$`*(header: PacketHeader): string =
|
||||||
")"
|
")"
|
||||||
|
|
||||||
proc `==`*(x: ConnectionId, y: ConnectionId): bool {.borrow.}
|
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
|
||||||
|
|
|
@ -103,6 +103,24 @@ suite "long headers":
|
||||||
"source: 0xDDEEFF" &
|
"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":
|
suite "packet numbers":
|
||||||
|
|
||||||
test "packet numbers are in the range 0 to 2^62-1":
|
test "packet numbers are in the range 0 to 2^62-1":
|
||||||
|
|
Loading…
Reference in New Issue