11# SPDX-License-Identifier: Apache-2.0 OR MIT
22# Copyright (c) Status Research & Development GmbH
33
4- import std/ [deques, hashes, sets]
4+ import std/ [deques, hashes, sets, strutils ]
55import boringssl
66import chronos
77import chronos/ osdefs
1717 len* : uint8
1818 bytes* : array [MAX_CID_LEN , uint8 ]
1919
20- # The generated FFI type currently aligns fields, not just the C struct.
21- # Read CIDs through the native wire layout until the binding is regenerated.
22- RawLsquicCid = object
23- buf: array [MAX_CID_LEN , uint8 ]
24- len: uint8
25- padding: array [3 , uint8 ]
26-
27- RawLsquicCidArray = UncheckedArray [RawLsquicCid ]
20+ LsquicCidArray = UncheckedArray [lsquic_cid_t]
2821
2922 QuicContext * = ref object of RootObj
3023 settings* : struct_lsquic_engine_settings
3932 running* : bool
4033 ownedCids* : HashSet [CidKey ]
4134
42- static :
43- doAssert sizeof (RawLsquicCid ) == 24
44- doAssert offsetOf (RawLsquicCid , len) == 20
45-
4635func hash * (cid: CidKey ): Hash =
4736 var h = hash (cid.len)
4837 for i in 0 ..< cid.len.int :
4938 h = h !& hash (cid.bytes[i])
5039 !$ h
5140
52- func toCidKey (cid: RawLsquicCid , key: var CidKey ): bool =
41+ func shortLog * (cid: CidKey ): string =
42+ var ret = $ cid.len & " :"
43+ for i in 0 ..< min (cid.len.int , 8 ):
44+ ret.add (toHex (cid.bytes[i], 2 ))
45+ ret
46+
47+ chronicles.formatIt (CidKey ):
48+ shortLog (it)
49+
50+ func toCidKey (cid: lsquic_cid_t, key: var CidKey ): bool =
5351 if cid.len == 0 or cid.len.int > MAX_CID_LEN :
5452 return false
5553
@@ -68,11 +66,12 @@ proc addCids*(
6866 if quicCtx.isNil or cids.isNil:
6967 return
7068
71- let cidsArr = cast [ptr RawLsquicCidArray ](cids)
69+ let cidsArr = cast [ptr LsquicCidArray ](cids)
7270 for i in 0 ..< nCids.int :
7371 var key: CidKey
7472 if toCidKey (cidsArr[i], key):
7573 quicCtx.ownedCids.incl (key)
74+ trace " Registered CID" , cid = key, cidCount = quicCtx.ownedCids.len
7675
7776proc removeCids * (
7877 ctx: pointer , peerCtxs: ptr pointer , cids: ptr lsquic_cid_t, nCids: cuint
@@ -81,15 +80,14 @@ proc removeCids*(
8180 if quicCtx.isNil or cids.isNil:
8281 return
8382
84- let cidsArr = cast [ptr RawLsquicCidArray ](cids)
83+ let cidsArr = cast [ptr LsquicCidArray ](cids)
8584 for i in 0 ..< nCids.int :
8685 var key: CidKey
8786 if toCidKey (cidsArr[i], key):
8887 quicCtx.ownedCids.excl (key)
88+ trace " Removed CID" , cid = key, cidCount = quicCtx.ownedCids.len
8989
90- proc trackConnectionCid * (
91- ctx: QuicContext , conn: ptr lsquic_conn_t
92- ) {.raises : [].} =
90+ proc trackConnectionCid * (ctx: QuicContext , conn: ptr lsquic_conn_t) {.raises : [].} =
9391 if ctx.isNil or conn.isNil:
9492 return
9593
@@ -98,8 +96,9 @@ proc trackConnectionCid*(
9896 return
9997
10098 var key: CidKey
101- if toCidKey (cast [ ptr RawLsquicCid ]( cid) [], key):
99+ if toCidKey (cid[], key):
102100 ctx.ownedCids.incl (key)
101+ trace " Tracked connection CID" , cid = key, cidCount = ctx.ownedCids.len
103102
104103proc ownsCid * (ctx: QuicContext , cid: CidKey ): bool {.raises : [].} =
105104 not ctx.isNil and cid in ctx.ownedCids
0 commit comments