@amritk/nish-x86_64-linux 0.42.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/net/quic-conn-ack.ts
CHANGED
|
@@ -23,19 +23,32 @@
|
|
|
23
23
|
*
|
|
24
24
|
* **When to acknowledge.** That is the connection's choice; this module records
|
|
25
25
|
* whether something ack-eliciting arrived since the last ACK it wrote
|
|
26
|
-
* (`ackPending`). ACK frames carry an ACK Delay the caller supplies
|
|
27
|
-
* ECN counts.
|
|
26
|
+
* (`ackPending`). ACK frames carry an ACK Delay the caller supplies.
|
|
28
27
|
*
|
|
29
|
-
* **
|
|
28
|
+
* **ECN counts.** `countEcn` adds a packet's ECN codepoint to the space's
|
|
29
|
+
* ECT(0), ECT(1) or ECN-CE count (RFC 9000 §13.4.1); the caller counts a
|
|
30
|
+
* packet only once `record` has accepted it, so a duplicate is not counted.
|
|
31
|
+
* Once any count is above zero every ACK written is an ACK_ECN (type 0x03)
|
|
32
|
+
* carrying all three, and before that a plain ACK (type 0x02).
|
|
33
|
+
*
|
|
34
|
+
* **Provenance.** Written from RFC 9000 §12.3, §13.2, §13.4.1 and §19.3, not ported from another
|
|
30
35
|
* implementation. Private names carry the `quicAck` prefix
|
|
31
36
|
* (`docs/wp26-stdlib.md` §3e).
|
|
32
37
|
*/
|
|
33
38
|
import { QUIC_MAX_VARINT } from "nish/net/quic-packet"
|
|
34
|
-
import { quicPushAck, quicPutAck } from "nish/net/quic-frame"
|
|
39
|
+
import { quicPushAck, quicPushAckEcn, quicPutAck, quicPutAckEcn } from "nish/net/quic-frame"
|
|
35
40
|
|
|
36
41
|
/** The most disjoint ranges a `QuicAckRanges` keeps, and so the most an ACK frame it writes lists. */
|
|
37
42
|
export const QUIC_ACK_MAX_RANGES: i32 = 32
|
|
38
43
|
|
|
44
|
+
/**
|
|
45
|
+
* One more than the ECN count `n`, or `n` once it is the largest a varint
|
|
46
|
+
* carries, since an ACK_ECN frame could not write a larger one.
|
|
47
|
+
*/
|
|
48
|
+
const quicAckCountUp = (n: i64): i64 =>
|
|
49
|
+
// Below `QUIC_MAX_VARINT`, so the unsigned sum cannot wrap.
|
|
50
|
+
n < QUIC_MAX_VARINT ? toI64(toU64(n) + 1) : n
|
|
51
|
+
|
|
39
52
|
/**
|
|
40
53
|
* The packet numbers received in one space. `ranges` holds `count` pairs
|
|
41
54
|
* `[smallest, largest]`, highest first and never touching; `largest` is the
|
|
@@ -51,13 +64,20 @@ export class QuicAckRanges {
|
|
|
51
64
|
count: i32 = 0
|
|
52
65
|
/** Whether an ack-eliciting packet arrived since the last `pushAck`. */
|
|
53
66
|
ackPending: boolean = false
|
|
67
|
+
/** Packets of this space that arrived marked ECT(0) (codepoint 2). */
|
|
68
|
+
ect0: i64 = 0
|
|
69
|
+
/** Packets of this space that arrived marked ECT(1) (codepoint 1). */
|
|
70
|
+
ect1: i64 = 0
|
|
71
|
+
/** Packets of this space that arrived marked ECN-CE (codepoint 3). */
|
|
72
|
+
ce: i64 = 0
|
|
54
73
|
|
|
55
74
|
/**
|
|
56
75
|
* Makes an empty set, nothing received yet, with room for
|
|
57
76
|
* `QUIC_ACK_MAX_RANGES` ranges.
|
|
58
77
|
*/
|
|
59
78
|
constructor() {
|
|
60
|
-
|
|
79
|
+
// 64 slots, which a u16 length holds.
|
|
80
|
+
this.ranges = new Array<i64>(toU16(QUIC_ACK_MAX_RANGES * 2))
|
|
61
81
|
}
|
|
62
82
|
|
|
63
83
|
/**
|
|
@@ -67,8 +87,13 @@ export class QuicAckRanges {
|
|
|
67
87
|
* @returns The range's smallest packet number, or -1 for an index outside the storage.
|
|
68
88
|
*/
|
|
69
89
|
low(k: i32): i64 {
|
|
90
|
+
// Below half the storage, `k * 2` cannot wrap.
|
|
91
|
+
const half: i32 = toI32(this.ranges.length) >>> 1
|
|
92
|
+
if (k < 0 || k >= half) {
|
|
93
|
+
return -1
|
|
94
|
+
}
|
|
70
95
|
const at: i32 = k * 2
|
|
71
|
-
if (at
|
|
96
|
+
if (at >= toI32(this.ranges.length)) {
|
|
72
97
|
return -1
|
|
73
98
|
}
|
|
74
99
|
return this.ranges[at]
|
|
@@ -81,8 +106,13 @@ export class QuicAckRanges {
|
|
|
81
106
|
* @returns The range's largest packet number, or -1 for an index outside the storage.
|
|
82
107
|
*/
|
|
83
108
|
high(k: i32): i64 {
|
|
109
|
+
// Below half the storage, `k * 2 + 1` cannot wrap.
|
|
110
|
+
const half: i32 = toI32(this.ranges.length) >>> 1
|
|
111
|
+
if (k < 0 || k >= half) {
|
|
112
|
+
return -1
|
|
113
|
+
}
|
|
84
114
|
const at: i32 = k * 2 + 1
|
|
85
|
-
if (at
|
|
115
|
+
if (at >= toI32(this.ranges.length)) {
|
|
86
116
|
return -1
|
|
87
117
|
}
|
|
88
118
|
return this.ranges[at]
|
|
@@ -97,9 +127,14 @@ export class QuicAckRanges {
|
|
|
97
127
|
* @param high The range's largest packet number, at least `low`.
|
|
98
128
|
*/
|
|
99
129
|
set(k: i32, low: i64, high: i64): void {
|
|
130
|
+
// Below half the storage, `k * 2 + 1` cannot wrap.
|
|
131
|
+
const half: i32 = toI32(this.ranges.length) >>> 1
|
|
132
|
+
if (k < 0 || k >= half) {
|
|
133
|
+
return
|
|
134
|
+
}
|
|
100
135
|
const at: i32 = k * 2
|
|
101
136
|
const next: i32 = at + 1
|
|
102
|
-
if (at
|
|
137
|
+
if (at >= toI32(this.ranges.length) || next >= toI32(this.ranges.length)) {
|
|
103
138
|
return
|
|
104
139
|
}
|
|
105
140
|
this.ranges[at] = low
|
|
@@ -153,7 +188,8 @@ export class QuicAckRanges {
|
|
|
153
188
|
while (k < this.count && this.low(k) > pn) {
|
|
154
189
|
k += 1
|
|
155
190
|
}
|
|
156
|
-
|
|
191
|
+
// `pn` is at most 2^62 - 1 here, so one past it cannot wrap.
|
|
192
|
+
const joinsAbove: boolean = k > 0 && this.low(k - 1) === toI64(toU64(pn) + 1)
|
|
157
193
|
const joinsBelow: boolean = k < this.count && this.high(k) === pn - 1
|
|
158
194
|
if (joinsAbove && joinsBelow) {
|
|
159
195
|
// `pn` closes the hole between two ranges: merge them into the upper one.
|
|
@@ -173,12 +209,43 @@ export class QuicAckRanges {
|
|
|
173
209
|
return true
|
|
174
210
|
}
|
|
175
211
|
|
|
176
|
-
/**
|
|
212
|
+
/**
|
|
213
|
+
* Counts one packet's ECN codepoint, the low two bits of the IP header's
|
|
214
|
+
* traffic class, in this space (RFC 9000 §13.4.1). The caller counts a
|
|
215
|
+
* packet once, after `record` accepted it; Not-ECT (0) counts nothing.
|
|
216
|
+
*
|
|
217
|
+
* @param codepoint The datagram's ECN bits: 0 Not-ECT, 1 ECT(1), 2 ECT(0), 3 CE; higher bits are ignored.
|
|
218
|
+
*/
|
|
219
|
+
countEcn(codepoint: i32): void {
|
|
220
|
+
const bits: i32 = codepoint & 3
|
|
221
|
+
if (bits === 2) {
|
|
222
|
+
this.ect0 = quicAckCountUp(this.ect0)
|
|
223
|
+
} else if (bits === 1) {
|
|
224
|
+
this.ect1 = quicAckCountUp(this.ect1)
|
|
225
|
+
} else if (bits === 3) {
|
|
226
|
+
this.ce = quicAckCountUp(this.ce)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Whether an ACK written now carries ECN counts.
|
|
232
|
+
*
|
|
233
|
+
* @returns True once any of `ect0`, `ect1` and `ce` is above zero.
|
|
234
|
+
*/
|
|
235
|
+
hasEcn(): boolean {
|
|
236
|
+
return this.ect0 > 0 || this.ect1 > 0 || this.ce > 0
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Removes range `k`, moving the lower ones up; `k` is a held range, below `count`. */
|
|
177
240
|
private remove(k: i32): void {
|
|
178
|
-
|
|
179
|
-
|
|
241
|
+
const count: i32 = this.count
|
|
242
|
+
if (count <= 0 || k < 0 || k >= count) {
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
for (let j: i32 = k + 1; j < count; j += 1) {
|
|
246
|
+
this.set(j - 1, this.low(j), this.high(j))
|
|
180
247
|
}
|
|
181
|
-
this.count =
|
|
248
|
+
this.count = count - 1
|
|
182
249
|
}
|
|
183
250
|
|
|
184
251
|
/**
|
|
@@ -187,24 +254,28 @@ export class QuicAckRanges {
|
|
|
187
254
|
* if `pn` itself would be that lowest range, it is the one dropped.
|
|
188
255
|
*/
|
|
189
256
|
private insert(k: i32, pn: i64): void {
|
|
190
|
-
|
|
191
|
-
|
|
257
|
+
let count: i32 = this.count
|
|
258
|
+
if (count === QUIC_ACK_MAX_RANGES) {
|
|
259
|
+
if (k === count) {
|
|
192
260
|
this.floor = pn
|
|
193
261
|
return
|
|
194
262
|
}
|
|
195
|
-
this.floor = this.high(
|
|
196
|
-
|
|
263
|
+
this.floor = this.high(QUIC_ACK_MAX_RANGES - 1)
|
|
264
|
+
count = QUIC_ACK_MAX_RANGES - 1
|
|
197
265
|
}
|
|
198
|
-
|
|
266
|
+
// `k` is 0 or more, so `j > 0` adds nothing but keeps `j - 1` from wrapping.
|
|
267
|
+
for (let j: i32 = count; j > 0 && j > k; j -= 1) {
|
|
199
268
|
this.set(j, this.low(j - 1), this.high(j - 1))
|
|
200
269
|
}
|
|
201
270
|
this.set(k, pn, pn)
|
|
202
|
-
|
|
271
|
+
// `count` is below `QUIC_ACK_MAX_RANGES` here, so one more cannot wrap.
|
|
272
|
+
this.count = toI32(toU32(count) + 1)
|
|
203
273
|
}
|
|
204
274
|
|
|
205
275
|
/**
|
|
206
276
|
* Appends an ACK frame for every range kept, with `ackDelay` (already
|
|
207
277
|
* scaled by this endpoint's ACK delay exponent), and clears `ackPending`.
|
|
278
|
+
* The frame is an ACK_ECN carrying the three counts once `hasEcn` holds.
|
|
208
279
|
* Answers `false`, appending nothing, when nothing has been received.
|
|
209
280
|
*
|
|
210
281
|
* @param out The packet payload being built; the frame is appended to its end.
|
|
@@ -213,7 +284,13 @@ export class QuicAckRanges {
|
|
|
213
284
|
* @returns True when the frame was appended; false, leaving `out` and `ackPending` alone, otherwise.
|
|
214
285
|
*/
|
|
215
286
|
pushAck(out: u8[], ackDelay: i64): boolean {
|
|
216
|
-
if (this.count === 0
|
|
287
|
+
if (this.count === 0) {
|
|
288
|
+
return false
|
|
289
|
+
}
|
|
290
|
+
const pushed: boolean = this.hasEcn()
|
|
291
|
+
? quicPushAckEcn(out, this.ranges, this.count, ackDelay, this.ect0, this.ect1, this.ce)
|
|
292
|
+
: quicPushAck(out, this.ranges, this.count, ackDelay)
|
|
293
|
+
if (!pushed) {
|
|
217
294
|
return false
|
|
218
295
|
}
|
|
219
296
|
this.ackPending = false
|
|
@@ -237,18 +314,23 @@ export class QuicAckRanges {
|
|
|
237
314
|
if (this.count === 0) {
|
|
238
315
|
return -1
|
|
239
316
|
}
|
|
240
|
-
const next: i32 =
|
|
317
|
+
const next: i32 = this.hasEcn()
|
|
318
|
+
? quicPutAckEcn(buf, at, end, this.ranges, this.count, ackDelay, this.ect0, this.ect1, this.ce)
|
|
319
|
+
: quicPutAck(buf, at, end, this.ranges, this.count, ackDelay)
|
|
241
320
|
if (next >= 0) {
|
|
242
321
|
this.ackPending = false
|
|
243
322
|
}
|
|
244
323
|
return next
|
|
245
324
|
}
|
|
246
325
|
|
|
247
|
-
/** Forgets every packet number, for a connection slot reused for another peer. */
|
|
326
|
+
/** Forgets every packet number and ECN count, for a connection slot reused for another peer. */
|
|
248
327
|
clear(): void {
|
|
249
328
|
this.largest = -1
|
|
250
329
|
this.floor = -1
|
|
251
330
|
this.count = 0
|
|
252
331
|
this.ackPending = false
|
|
332
|
+
this.ect0 = 0
|
|
333
|
+
this.ect1 = 0
|
|
334
|
+
this.ce = 0
|
|
253
335
|
}
|
|
254
336
|
}
|
package/std/net/quic-conn-cid.ts
CHANGED
|
@@ -49,7 +49,7 @@ import {
|
|
|
49
49
|
QUIC_ERROR_PROTOCOL_VIOLATION,
|
|
50
50
|
QUIC_RESET_TOKEN_SIZE,
|
|
51
51
|
} from "nish/net/quic-frame"
|
|
52
|
-
import { QUIC_MAX_CID_LENGTH } from "nish/net/quic-packet"
|
|
52
|
+
import { QUIC_MAX_CID_LENGTH, QUIC_MAX_VARINT } from "nish/net/quic-packet"
|
|
53
53
|
|
|
54
54
|
/** A typed zero: a bare literal is an `f64` under `--number-mode f64`. */
|
|
55
55
|
const QUIC_CID_ZERO: i32 = 0
|
|
@@ -95,7 +95,10 @@ export class QuicCidEntry {
|
|
|
95
95
|
*/
|
|
96
96
|
export const quicCidCopy = (to: u8[], from: u8[], at: i32, length: i32): void => {
|
|
97
97
|
for (let k: i32 = 0; k < toI32(to.length); k += 1) {
|
|
98
|
-
|
|
98
|
+
// Widened, so a sum past 2^31 - 1 lands outside `from` and reads as a missing byte.
|
|
99
|
+
const j: i64 = toI64(at) + toI64(k)
|
|
100
|
+
const i: i32 = j >= 0 && j < 2147483648 ? toI32(j) : -1
|
|
101
|
+
to[k] = k < length && i >= 0 && i < toI32(from.length) ? from[i] : toU8(0)
|
|
99
102
|
}
|
|
100
103
|
}
|
|
101
104
|
|
|
@@ -139,6 +142,19 @@ const quicCidClear = (entry: QuicCidEntry): void => {
|
|
|
139
142
|
secureZero(entry.resetToken)
|
|
140
143
|
}
|
|
141
144
|
|
|
145
|
+
/** `count` zeroed sequence numbers, none when `count` is below 1; `count` is below 2^32. */
|
|
146
|
+
const quicCidSequences = (count: i64): i64[] => {
|
|
147
|
+
if (count >= 0 && count <= 65535) {
|
|
148
|
+
return new Array<i64>(toU16(count))
|
|
149
|
+
}
|
|
150
|
+
const out: i64[] = []
|
|
151
|
+
// `count` is twice an i32 at most, below 2^32, so the unsigned counter reaches it before it wraps.
|
|
152
|
+
for (let k: u32 = 0; toI64(k) < count; k += 1) {
|
|
153
|
+
out.push(0)
|
|
154
|
+
}
|
|
155
|
+
return out
|
|
156
|
+
}
|
|
157
|
+
|
|
142
158
|
/** `count` empty slots. */
|
|
143
159
|
const quicCidSlots = (count: i32): QuicCidEntry[] => {
|
|
144
160
|
const out: QuicCidEntry[] = []
|
|
@@ -184,7 +200,8 @@ export class QuicCidTable {
|
|
|
184
200
|
const peerSlots: i32 = limit < 1 ? 1 : toI32(limit)
|
|
185
201
|
this.local = quicCidSlots(localCapacity)
|
|
186
202
|
this.peer = quicCidSlots(peerSlots)
|
|
187
|
-
|
|
203
|
+
// Widened, so twice a large `limit` cannot wrap.
|
|
204
|
+
this.retirePending = quicCidSequences(toI64(peerSlots) * 2)
|
|
188
205
|
this.limit = limit
|
|
189
206
|
}
|
|
190
207
|
|
|
@@ -206,21 +223,23 @@ export class QuicCidTable {
|
|
|
206
223
|
/**
|
|
207
224
|
* Issues `cid`, up to its first 20 bytes, as the next local ID, with its
|
|
208
225
|
* 16-byte reset token (or none, empty, for sequence 0), and answers its
|
|
209
|
-
* sequence number; -1 when every local slot is taken
|
|
226
|
+
* sequence number; -1 when every local slot is taken or sequence
|
|
227
|
+
* numbers reach 2^62 - 1, the largest a varint carries.
|
|
210
228
|
*
|
|
211
229
|
* @param cid The ID's bytes, copied into the slot; past 20 bytes (`QUIC_MAX_CID_LENGTH`) the rest is
|
|
212
230
|
* dropped.
|
|
213
231
|
* @param resetToken The 16-byte stateless reset token, copied; any other length, empty included, means
|
|
214
232
|
* none.
|
|
215
|
-
* @returns The ID's sequence number, counting up from 0, or -1 when every local slot is taken
|
|
233
|
+
* @returns The ID's sequence number, counting up from 0, or -1 when every local slot is taken or the
|
|
234
|
+
* sequence numbers are used up.
|
|
216
235
|
*/
|
|
217
236
|
addLocal(cid: u8[], resetToken: u8[]): i64 {
|
|
218
237
|
const k: i32 = quicCidFree(this.local)
|
|
219
|
-
|
|
238
|
+
const sequence: i64 = this.nextLocal
|
|
239
|
+
if (k < 0 || k >= toI32(this.local.length) || sequence < 0 || sequence >= QUIC_MAX_VARINT) {
|
|
220
240
|
return -1
|
|
221
241
|
}
|
|
222
242
|
const entry: QuicCidEntry = this.local[k]
|
|
223
|
-
const sequence: i64 = this.nextLocal
|
|
224
243
|
const length: i32 = toI32(cid.length) > QUIC_MAX_CID_LENGTH ? QUIC_MAX_CID_LENGTH : toI32(cid.length)
|
|
225
244
|
quicCidCopy(entry.cid, cid, 0, length)
|
|
226
245
|
entry.length = length
|
|
@@ -230,7 +249,8 @@ export class QuicCidTable {
|
|
|
230
249
|
entry.used = true
|
|
231
250
|
// Sequence 0 travels in the long header's SCID, never in a frame.
|
|
232
251
|
entry.announced = sequence === 0
|
|
233
|
-
|
|
252
|
+
// Below `QUIC_MAX_VARINT`, so one more cannot wrap.
|
|
253
|
+
this.nextLocal = toI64(toU64(sequence) + 1)
|
|
234
254
|
return sequence
|
|
235
255
|
}
|
|
236
256
|
|
|
@@ -240,13 +260,14 @@ export class QuicCidTable {
|
|
|
240
260
|
* @returns The count of issued, not yet retired local IDs, from 0 to `localCapacity`.
|
|
241
261
|
*/
|
|
242
262
|
activeLocal(): i32 {
|
|
243
|
-
|
|
263
|
+
// At most one per slot, so the unsigned count cannot wrap.
|
|
264
|
+
let n: u32 = 0
|
|
244
265
|
for (const entry of this.local) {
|
|
245
266
|
if (entry.used) {
|
|
246
267
|
n += 1
|
|
247
268
|
}
|
|
248
269
|
}
|
|
249
|
-
return n
|
|
270
|
+
return toI32(n)
|
|
250
271
|
}
|
|
251
272
|
|
|
252
273
|
/**
|
|
@@ -314,13 +335,14 @@ export class QuicCidTable {
|
|
|
314
335
|
* @returns The count of the peer's IDs not yet retired, from 0 to `limit`.
|
|
315
336
|
*/
|
|
316
337
|
activePeer(): i32 {
|
|
317
|
-
|
|
338
|
+
// At most one per slot, so the unsigned count cannot wrap.
|
|
339
|
+
let n: u32 = 0
|
|
318
340
|
for (const entry of this.peer) {
|
|
319
341
|
if (entry.used) {
|
|
320
342
|
n += 1
|
|
321
343
|
}
|
|
322
344
|
}
|
|
323
|
-
return n
|
|
345
|
+
return toI32(n)
|
|
324
346
|
}
|
|
325
347
|
|
|
326
348
|
/**
|
|
@@ -399,8 +421,10 @@ export class QuicCidTable {
|
|
|
399
421
|
// Count what the frame would leave before changing anything, so a frame
|
|
400
422
|
// refused for a limit leaves the table as it was.
|
|
401
423
|
const priorTo: i64 = retirePriorTo > this.peerRetirePriorTo ? retirePriorTo : this.peerRetirePriorTo
|
|
402
|
-
|
|
403
|
-
|
|
424
|
+
// Each count is at most one per peer slot and queued retirement plus this frame's ID, far below
|
|
425
|
+
// 2^32, so the unsigned sums cannot wrap.
|
|
426
|
+
let active: u32 = sequence < priorTo ? 0 : 1
|
|
427
|
+
let pending: u32 = toU32(this.retireCount) + (sequence < priorTo ? 1 : 0)
|
|
404
428
|
for (const old of this.peer) {
|
|
405
429
|
if (!old.used) {
|
|
406
430
|
continue
|
|
@@ -412,9 +436,10 @@ export class QuicCidTable {
|
|
|
412
436
|
}
|
|
413
437
|
}
|
|
414
438
|
if (
|
|
415
|
-
active > this.limit ||
|
|
416
|
-
pending >
|
|
417
|
-
pending >
|
|
439
|
+
toI64(active) > this.limit ||
|
|
440
|
+
// `pending > limit * 2` for whole numbers, without doubling `limit`.
|
|
441
|
+
toI64((pending + 1) >>> 1) > this.limit ||
|
|
442
|
+
toI64(pending) > toI64(toI32(this.retirePending.length))
|
|
418
443
|
) {
|
|
419
444
|
return QUIC_ERROR_CONNECTION_ID_LIMIT
|
|
420
445
|
}
|
|
@@ -472,7 +497,8 @@ export class QuicCidTable {
|
|
|
472
497
|
currentPeer(): u8[] {
|
|
473
498
|
const entry: QuicCidEntry | null = this.currentPeerEntry()
|
|
474
499
|
const length: i32 = entry !== null ? entry.length : 0
|
|
475
|
-
|
|
500
|
+
// An entry's length is 0 to 20 (`QUIC_MAX_CID_LENGTH`), so it fits a u16.
|
|
501
|
+
const out: u8[] = new Array<u8>(toU16(length))
|
|
476
502
|
if (entry !== null) {
|
|
477
503
|
quicCidCopy(out, entry.cid, 0, length)
|
|
478
504
|
}
|
|
@@ -487,20 +513,33 @@ export class QuicCidTable {
|
|
|
487
513
|
*/
|
|
488
514
|
queueRetire(sequence: i64): void {
|
|
489
515
|
const size: i32 = toI32(this.retirePending.length)
|
|
490
|
-
|
|
491
|
-
|
|
516
|
+
const count: i32 = this.retireCount
|
|
517
|
+
for (let k: i32 = 0; k < count; k += 1) {
|
|
518
|
+
const at: i32 = this.retireSlot(k)
|
|
492
519
|
if (at >= 0 && at < size && this.retirePending[at] === sequence) {
|
|
493
520
|
return
|
|
494
521
|
}
|
|
495
522
|
}
|
|
496
|
-
if (
|
|
523
|
+
if (count < 0 || count >= size) {
|
|
497
524
|
return
|
|
498
525
|
}
|
|
499
|
-
const at: i32 =
|
|
526
|
+
const at: i32 = this.retireSlot(count)
|
|
500
527
|
if (at >= 0 && at < size) {
|
|
501
528
|
this.retirePending[at] = sequence
|
|
502
|
-
this.retireCount =
|
|
529
|
+
this.retireCount = count + 1
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* The ring slot `k` places after `retireHead`, or 0 for an empty ring. Unsigned, because the head and
|
|
535
|
+
* `k` are each below 2^31, so the sum cannot wrap.
|
|
536
|
+
*/
|
|
537
|
+
private retireSlot(k: i32): i32 {
|
|
538
|
+
const size: u32 = toU32(this.retirePending.length)
|
|
539
|
+
if (size === 0) {
|
|
540
|
+
return 0
|
|
503
541
|
}
|
|
542
|
+
return toI32((toU32(this.retireHead) + toU32(k)) % size)
|
|
504
543
|
}
|
|
505
544
|
|
|
506
545
|
/**
|
|
@@ -510,13 +549,14 @@ export class QuicCidTable {
|
|
|
510
549
|
*/
|
|
511
550
|
takeRetire(): i64 {
|
|
512
551
|
const size: i32 = toI32(this.retirePending.length)
|
|
513
|
-
|
|
552
|
+
const count: i32 = this.retireCount
|
|
553
|
+
if (count <= 0 || size === 0) {
|
|
514
554
|
return -1
|
|
515
555
|
}
|
|
516
556
|
const at: i32 = this.retireHead
|
|
517
557
|
const first: i64 = at >= 0 && at < size ? this.retirePending[at] : -1
|
|
518
|
-
this.retireHead = (
|
|
519
|
-
this.retireCount =
|
|
558
|
+
this.retireHead = this.retireSlot(toI32(1))
|
|
559
|
+
this.retireCount = count - 1
|
|
520
560
|
return first
|
|
521
561
|
}
|
|
522
562
|
|
|
@@ -313,8 +313,11 @@ export const quicEncodeTransportParametersInto = (p: QuicTransportParameters, ou
|
|
|
313
313
|
const quicParamsCopyInto = (to: u8[], data: u8[], from: i32, length: i32): void => {
|
|
314
314
|
quicParamsEmpty(to)
|
|
315
315
|
for (let k: i32 = 0; k < length; k += 1) {
|
|
316
|
-
|
|
317
|
-
|
|
316
|
+
// `from` and `k` are both i32, so their sum fits an i64; a byte outside `data` is not copied.
|
|
317
|
+
const j: i64 = toI64(from) + toI64(k)
|
|
318
|
+
const i: i32 = j >= 0 && j < 2147483648 ? toI32(j) : -1
|
|
319
|
+
if (i >= 0 && i < toI32(data.length)) {
|
|
320
|
+
to.push(data[i])
|
|
318
321
|
}
|
|
319
322
|
}
|
|
320
323
|
}
|
|
@@ -332,7 +335,8 @@ const quicParamsRefuse = (p: QuicTransportParameters, id: i64): QuicTransportPar
|
|
|
332
335
|
* holds, otherwise.
|
|
333
336
|
*/
|
|
334
337
|
const quicParamsVarintValue = (bytes: u8[], at: i32, length: i32): i64 => {
|
|
335
|
-
|
|
338
|
+
// The caller's value window lies inside `bytes`, so its end fits an i32.
|
|
339
|
+
const value: i64 = quicVarintRead(bytes, at, toI32(toI64(at) + toI64(length)))
|
|
336
340
|
if (value < 0 || quicVarintLength(bytes, at) !== length) {
|
|
337
341
|
return -1
|
|
338
342
|
}
|
|
@@ -504,18 +508,20 @@ export const quicParseTransportParametersInto = (
|
|
|
504
508
|
if (id < 0) {
|
|
505
509
|
return quicParamsRefuse(p, -1)
|
|
506
510
|
}
|
|
507
|
-
|
|
511
|
+
// The varint just read ends inside `end`, so the advance fits an i32.
|
|
512
|
+
at = toI32(toI64(at) + toI64(quicVarintLength(bytes, at)))
|
|
508
513
|
const length: i64 = quicVarintRead(bytes, at, end)
|
|
509
514
|
if (length < 0) {
|
|
510
515
|
return quicParamsRefuse(p, id)
|
|
511
516
|
}
|
|
512
|
-
at = at + quicVarintLength(bytes, at)
|
|
513
|
-
if (length > toI64(end - at)) {
|
|
517
|
+
at = toI32(toI64(at) + toI64(quicVarintLength(bytes, at)))
|
|
518
|
+
if (length > toI64(end) - toI64(at)) {
|
|
514
519
|
return quicParamsRefuse(p, id)
|
|
515
520
|
}
|
|
516
521
|
const valueAt: i32 = at
|
|
517
522
|
const valueLength: i32 = toI32(length)
|
|
518
|
-
|
|
523
|
+
// `length` fits in what is left before `end`, so the advance fits an i32.
|
|
524
|
+
at = toI32(toI64(at) + toI64(valueLength))
|
|
519
525
|
if (id === toI64(QUIC_TP_MAX_DATAGRAM_FRAME_SIZE)) {
|
|
520
526
|
const value: i64 = quicParamsVarintValue(bytes, valueAt, valueLength)
|
|
521
527
|
if (seenDatagram || value < 0) {
|