nim-quic/tests/quic/helpers/ids.nim

25 lines
839 B
Nim
Raw Normal View History

import ngtcp2
2020-09-16 12:19:58 +00:00
import sysrandom
2020-09-16 12:19:58 +00:00
proc connectionId*(bytes: ptr uint8, length: uint): ngtcp2_cid =
ngtcp2_cid_init(addr result, bytes, length)
proc randomConnectionId*(): ngtcp2_cid =
var bytes: array[18, uint8]
getRandomBytes(addr bytes[0], bytes.len)
ngtcp2_cid_init(addr result, addr bytes[0], bytes.len.uint)
2020-09-16 12:19:58 +00:00
var clientSourceId* = randomConnectionId()
var clientDestinationId* = randomConnectionId()
var serverSourceId* = randomConnectionId()
var serverDestinationId* = randomConnectionId()
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
zeroMem(token, NGTCP2_STATELESS_RESET_TOKENLEN)