Implement new connection id callback using randomConnectionId()

This commit is contained in:
Mark Spanbroek 2020-10-12 11:05:05 +02:00 committed by markspanbroek
parent 0fa6790520
commit a5f5e39cae
3 changed files with 6 additions and 8 deletions

View File

@ -11,5 +11,5 @@ proc `len`*(x: ConnectionId): int {.borrow.}
proc `$`*(id: ConnectionId): string =
"0x" & cast[string](id).toHex
proc randomConnectionId*: ConnectionId =
ConnectionId(@(getRandomBytes(DefaultConnectionIdLength)))
proc randomConnectionId*(len = DefaultConnectionIdLength): ConnectionId =
ConnectionId(@(getRandomBytes(len)))

View File

@ -8,11 +8,6 @@ proc toCid*(id: ConnectionId): ngtcp2_cid =
let bytes = seq[byte](id)
toCid(unsafeAddr bytes[0], bytes.len.uint)
var nextConnectionId = 1'u8
proc getNewConnectionId*(connection: ptr ngtcp2_conn, id: ptr ngtcp2_cid, token: ptr uint8, cidlen: uint, userData: pointer): cint {.cdecl.} =
zeroMem(addr id.data[0], cidlen)
id.data[0] = nextConnectionId
inc(nextConnectionId)
id.datalen = cidlen
id[] = randomConnectionId(cidlen.int).toCid
zeroMem(token, NGTCP2_STATELESS_RESET_TOKENLEN)

View File

@ -8,3 +8,6 @@ suite "Connection Ids":
test "random ids are of the correct length":
check randomConnectionId().len == DefaultConnectionIdLength
test "random ids can have custom length":
check randomConnectionId(5).len == 5