mirror of https://github.com/vacp2p/nim-quic.git
Implement new connection id callback using randomConnectionId()
This commit is contained in:
parent
0fa6790520
commit
a5f5e39cae
|
@ -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)))
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue