nish 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/README.md +11 -5
- package/docs/AI.md +287 -129
- package/package.json +9 -9
- 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/skills/writing-nish/SKILL.md +12 -4
- 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/http3-server.ts
CHANGED
|
@@ -133,7 +133,6 @@ import {
|
|
|
133
133
|
QUIC_CONN_ENTROPY_SIZE,
|
|
134
134
|
QUIC_CONN_LOCAL_CIDS,
|
|
135
135
|
QUIC_CONN_MAX_STREAM_DATA,
|
|
136
|
-
QUIC_CONN_MAX_STREAMS,
|
|
137
136
|
QuicConnection,
|
|
138
137
|
QuicServerConfig,
|
|
139
138
|
} from "nish/net/quic"
|
|
@@ -175,9 +174,21 @@ const H3_SERVER_UNSUPPORTED: i32 = -95
|
|
|
175
174
|
/** The largest datagram read: a GRO receive of up to 64 KB. */
|
|
176
175
|
const H3_SERVER_RECEIVE_SIZE: i32 = 65536
|
|
177
176
|
|
|
177
|
+
/** The bytes of the largest flight `flush` writes at once: `QUIC_LISTENER_FLIGHT_MAX` datagrams. */
|
|
178
|
+
const H3_SERVER_FLIGHT_BYTES: i32 = QUIC_LISTENER_FLIGHT_MAX * QUIC_CONN_DATAGRAM_SIZE
|
|
179
|
+
|
|
178
180
|
/** Index entries per slot: its local connection IDs, then the client's original DCID. */
|
|
179
181
|
const H3_SERVER_KEYS: i32 = QUIC_CONN_LOCAL_CIDS + 1
|
|
180
182
|
|
|
183
|
+
/** The ID bytes an index keeps per slot: its entries, `QUIC_MAX_CID_LENGTH` bytes each. */
|
|
184
|
+
const H3_SERVER_SLOT_KEY_BYTES: i32 = H3_SERVER_KEYS * QUIC_MAX_CID_LENGTH
|
|
185
|
+
|
|
186
|
+
/** The most slots an index files: past it, one slot's ID bytes would end beyond an `i32` offset. */
|
|
187
|
+
const H3_SERVER_SLOT_LIMIT: i32 = 21474836
|
|
188
|
+
|
|
189
|
+
/** The index's largest table, in places: 2^30, more than twice `H3_SERVER_SLOT_LIMIT`'s entries. */
|
|
190
|
+
const H3_SERVER_TABLE_LIMIT: i32 = 1073741824
|
|
191
|
+
|
|
181
192
|
/** Typed constants, since a bare literal is an `f64` under `--number-mode f64`. */
|
|
182
193
|
const H3_SERVER_ZERO: i32 = 0
|
|
183
194
|
const H3_SERVER_NONE: i64 = -1
|
|
@@ -190,8 +201,66 @@ const H3_SERVER_MASK: i64 = 0x7fffffffffff
|
|
|
190
201
|
const H3_SERVER_FREE: i32 = 0
|
|
191
202
|
const H3_SERVER_BUSY: i32 = 1
|
|
192
203
|
|
|
193
|
-
/**
|
|
194
|
-
|
|
204
|
+
/**
|
|
205
|
+
* The bucket of `time` on the wheel: 2048 is `H3_SERVER_WHEEL`, written out
|
|
206
|
+
* so the prover knows the divisor. Negative for a negative time not a multiple of it.
|
|
207
|
+
*/
|
|
208
|
+
const http3ServerBucket = (time: i64): i32 => toI32(time % toI64(2048))
|
|
209
|
+
|
|
210
|
+
/** The words of the wheel's bitmap: `H3_SERVER_WHEEL / 64`. */
|
|
211
|
+
const H3_SERVER_WHEEL_WORDS: i32 = 32
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* `count` copies of `value`, built by `push`, so a negative `count` gives an
|
|
215
|
+
* empty array and no length is unchecked.
|
|
216
|
+
*/
|
|
217
|
+
const http3ServerFilled = (count: i32, value: i32): i32[] => {
|
|
218
|
+
const out: i32[] = []
|
|
219
|
+
let i: i32 = 0
|
|
220
|
+
while (i < count) {
|
|
221
|
+
out.push(value)
|
|
222
|
+
i = i + 1
|
|
223
|
+
}
|
|
224
|
+
return out
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** `count` zero times, built as `http3ServerFilled` builds. */
|
|
228
|
+
const http3ServerZeroTimes = (count: i32): i64[] => {
|
|
229
|
+
const out: i64[] = []
|
|
230
|
+
let i: i32 = 0
|
|
231
|
+
while (i < count) {
|
|
232
|
+
out.push(toI64(0))
|
|
233
|
+
i = i + 1
|
|
234
|
+
}
|
|
235
|
+
return out
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Milliseconds from `now` to `from + offset`, 0 when that is at or before
|
|
240
|
+
* `now`; a wait past the `i32` range keeps its low 32 bits, as `toI32` gives.
|
|
241
|
+
* The sum is never formed, since it can pass the `i64` range.
|
|
242
|
+
* @param from A millisecond; a negative one only with an `offset` of 0.
|
|
243
|
+
* @param offset Milliseconds after `from`, 0 to 2047; one outside gives -1.
|
|
244
|
+
* @param now The current millisecond.
|
|
245
|
+
* @returns The wait, or -1 for an `offset` out of range.
|
|
246
|
+
*/
|
|
247
|
+
const http3ServerWait = (from: i64, offset: i32, now: i64): i32 => {
|
|
248
|
+
if (offset < 0 || offset >= H3_SERVER_WHEEL) {
|
|
249
|
+
// Never taken: `next` passes an offset below `H3_SERVER_WHEEL`.
|
|
250
|
+
return -1
|
|
251
|
+
}
|
|
252
|
+
let due: boolean = false
|
|
253
|
+
if (from < 0) {
|
|
254
|
+
due = from + toI64(offset) <= now
|
|
255
|
+
} else if (now >= 0) {
|
|
256
|
+
due = from - now <= toI64(0 - offset)
|
|
257
|
+
}
|
|
258
|
+
if (due) {
|
|
259
|
+
return H3_SERVER_ZERO
|
|
260
|
+
}
|
|
261
|
+
// The low 32 bits of `from + offset - now`, from the low 32 bits of each.
|
|
262
|
+
return toI32(toI64(toI32(from)) - toI64(toI32(now)) + toI64(offset))
|
|
263
|
+
}
|
|
195
264
|
|
|
196
265
|
/**
|
|
197
266
|
* The connection IDs of every slot, by hash: an open-addressed table keyed
|
|
@@ -211,18 +280,37 @@ export class Http3CidIndex {
|
|
|
211
280
|
|
|
212
281
|
/**
|
|
213
282
|
* An empty index for `slots` slots.
|
|
214
|
-
* @param slots How many slots it files IDs for, each `QUIC_CONN_LOCAL_CIDS + 1` entries
|
|
283
|
+
* @param slots How many slots it files IDs for, each `QUIC_CONN_LOCAL_CIDS + 1` entries; 0 or fewer
|
|
284
|
+
* files none.
|
|
215
285
|
* @param salt The hash's key: its first eight bytes are copied (zeros past a shorter one); not kept.
|
|
216
286
|
*/
|
|
217
287
|
constructor(slots: i32, salt: u8[]) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
288
|
+
// Each table is built by push, one after another, so it grows at the
|
|
289
|
+
// top of the arena; seeded with one slot's worth, its capacity is exact
|
|
290
|
+
// at a power-of-two slot count.
|
|
291
|
+
const lengths: i32[] = slots > 0 ? new Array<i32>(H3_SERVER_KEYS) : []
|
|
292
|
+
for (let s: i32 = 1; s < slots && s < H3_SERVER_SLOT_LIMIT; s++) {
|
|
293
|
+
for (let j: i32 = 0; j < H3_SERVER_KEYS; j++) {
|
|
294
|
+
lengths.push(0)
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const keys: u8[] = slots > 0 ? new Array<u8>(H3_SERVER_SLOT_KEY_BYTES) : []
|
|
298
|
+
for (let s: i32 = 1; s < slots && s < H3_SERVER_SLOT_LIMIT; s++) {
|
|
299
|
+
for (let k: i32 = 0; k < H3_SERVER_SLOT_KEY_BYTES; k++) {
|
|
300
|
+
keys.push(0)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const entries: i32 = toI32(lengths.length)
|
|
304
|
+
const table: i32[] = new Array<i32>(8)
|
|
305
|
+
while (toI32(table.length) < H3_SERVER_TABLE_LIMIT && toI32(table.length) / 2 < entries) {
|
|
306
|
+
const size: i32 = toI32(table.length)
|
|
307
|
+
for (let h: i32 = 0; h < size; h++) {
|
|
308
|
+
table.push(0)
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
this.table = table
|
|
312
|
+
this.keys = keys
|
|
313
|
+
this.lengths = lengths
|
|
226
314
|
this.salt = new Array<u8>(8)
|
|
227
315
|
for (let k: i32 = 0; k < 8 && k < toI32(salt.length); k++) {
|
|
228
316
|
this.salt[k] = salt[k]
|
|
@@ -231,11 +319,19 @@ export class Http3CidIndex {
|
|
|
231
319
|
|
|
232
320
|
/** The bucket the ID `buf[at .. at + length)` starts its probe at. */
|
|
233
321
|
private bucket(buf: u8[], at: i32, length: i32): i32 {
|
|
322
|
+
const salt: u8[] = this.salt
|
|
234
323
|
let h: i64 = toI64(length)
|
|
235
|
-
|
|
324
|
+
// `i` is `at + k`, the byte read.
|
|
325
|
+
let i: i32 = at
|
|
326
|
+
for (let k: i32 = 0; k < length && i >= 0 && i < toI32(buf.length); k++) {
|
|
236
327
|
const j: i32 = k & 7
|
|
237
|
-
|
|
328
|
+
if (j >= toI32(salt.length)) {
|
|
329
|
+
// Never taken: the constructor gives `salt` eight bytes.
|
|
330
|
+
break
|
|
331
|
+
}
|
|
332
|
+
const mixed: i64 = toI64(toI32(buf[i]) ^ toI32(salt[j]))
|
|
238
333
|
h = (((h << 7) & H3_SERVER_MASK_LOW) ^ (h >> 29) ^ mixed) & H3_SERVER_MASK
|
|
334
|
+
i++
|
|
239
335
|
}
|
|
240
336
|
h = h ^ (h >> 17) ^ (h >> 31)
|
|
241
337
|
return toI32(h & toI64(toI32(this.table.length) - 1))
|
|
@@ -243,14 +339,21 @@ export class Http3CidIndex {
|
|
|
243
339
|
|
|
244
340
|
/** Whether entry `e` holds the ID `buf[at .. at + length)`. */
|
|
245
341
|
private holds(e: i32, buf: u8[], at: i32, length: i32): boolean {
|
|
246
|
-
|
|
342
|
+
const keys: u8[] = this.keys
|
|
343
|
+
const lengths: i32[] = this.lengths
|
|
344
|
+
// 107374182 is `I32_MAX / QUIC_MAX_CID_LENGTH`, past `H3_SERVER_SLOT_LIMIT`'s entries.
|
|
345
|
+
if (e < 0 || e >= 107374182 || e >= toI32(lengths.length) || lengths[e] !== length) {
|
|
247
346
|
return false
|
|
248
347
|
}
|
|
249
|
-
|
|
348
|
+
// `p` and `i` are `e * QUIC_MAX_CID_LENGTH + k` and `at + k`, the bytes compared.
|
|
349
|
+
let p: i32 = e * QUIC_MAX_CID_LENGTH
|
|
350
|
+
let i: i32 = at
|
|
250
351
|
for (let k: i32 = 0; k < length; k++) {
|
|
251
|
-
if (
|
|
352
|
+
if (i < 0 || i >= toI32(buf.length) || p < 0 || p >= toI32(keys.length) || keys[p] !== buf[i]) {
|
|
252
353
|
return false
|
|
253
354
|
}
|
|
355
|
+
p++
|
|
356
|
+
i++
|
|
254
357
|
}
|
|
255
358
|
return true
|
|
256
359
|
}
|
|
@@ -266,10 +369,11 @@ export class Http3CidIndex {
|
|
|
266
369
|
const size: i32 = toI32(this.table.length)
|
|
267
370
|
let h: i32 = this.bucket(buf, at, length)
|
|
268
371
|
for (let probe: i32 = 0; probe < size && h >= 0 && h < size; probe++) {
|
|
269
|
-
const
|
|
270
|
-
if (
|
|
372
|
+
const filed: i32 = this.table[h]
|
|
373
|
+
if (filed <= 0) {
|
|
271
374
|
return -1
|
|
272
375
|
}
|
|
376
|
+
const e: i32 = filed - 1
|
|
273
377
|
if (this.holds(e, buf, at, length)) {
|
|
274
378
|
return e / H3_SERVER_KEYS
|
|
275
379
|
}
|
|
@@ -288,17 +392,34 @@ export class Http3CidIndex {
|
|
|
288
392
|
* @param length The ID's length in bytes, 1 to `QUIC_MAX_CID_LENGTH`; any other files nothing.
|
|
289
393
|
*/
|
|
290
394
|
insert(e: i32, buf: u8[], at: i32, length: i32): void {
|
|
291
|
-
|
|
395
|
+
const keys: u8[] = this.keys
|
|
396
|
+
const lengths: i32[] = this.lengths
|
|
397
|
+
// 107374182 is `I32_MAX / QUIC_MAX_CID_LENGTH`, past `H3_SERVER_SLOT_LIMIT`'s entries.
|
|
398
|
+
if (
|
|
399
|
+
length <= 0 ||
|
|
400
|
+
length > QUIC_MAX_CID_LENGTH ||
|
|
401
|
+
e < 0 ||
|
|
402
|
+
e >= toI32(lengths.length) ||
|
|
403
|
+
e >= 107374182
|
|
404
|
+
) {
|
|
292
405
|
return
|
|
293
406
|
}
|
|
294
407
|
if (this.find(buf, at, length) >= 0) {
|
|
295
408
|
return
|
|
296
409
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
410
|
+
// `p` and `i` are `e * QUIC_MAX_CID_LENGTH + k` and `at + k`, the bytes copied.
|
|
411
|
+
let p: i32 = e * QUIC_MAX_CID_LENGTH
|
|
412
|
+
let i: i32 = at
|
|
413
|
+
for (let k: i32 = 0; k < length && i >= 0 && i < toI32(buf.length); k++) {
|
|
414
|
+
if (p < 0 || p >= toI32(keys.length)) {
|
|
415
|
+
// Never taken: each entry has `QUIC_MAX_CID_LENGTH` bytes of `keys`.
|
|
416
|
+
return
|
|
417
|
+
}
|
|
418
|
+
keys[p] = buf[i]
|
|
419
|
+
p++
|
|
420
|
+
i++
|
|
300
421
|
}
|
|
301
|
-
|
|
422
|
+
lengths[e] = length
|
|
302
423
|
const size: i32 = toI32(this.table.length)
|
|
303
424
|
let h: i32 = this.bucket(buf, at, length)
|
|
304
425
|
for (let probe: i32 = 0; probe < size && h >= 0 && h < size; probe++) {
|
|
@@ -315,13 +436,14 @@ export class Http3CidIndex {
|
|
|
315
436
|
* @param e The entry, numbered as `insert` numbers it; one out of range or not filed is ignored.
|
|
316
437
|
*/
|
|
317
438
|
remove(e: i32): void {
|
|
318
|
-
|
|
439
|
+
const lengths: i32[] = this.lengths
|
|
440
|
+
// 107374182 is `I32_MAX / QUIC_MAX_CID_LENGTH`, past `H3_SERVER_SLOT_LIMIT`'s entries.
|
|
441
|
+
if (e < 0 || e >= toI32(lengths.length) || e >= 107374182 || lengths[e] === 0) {
|
|
319
442
|
return
|
|
320
443
|
}
|
|
321
444
|
const size: i32 = toI32(this.table.length)
|
|
322
445
|
const mask: i32 = size - 1
|
|
323
|
-
|
|
324
|
-
let h: i32 = this.bucket(this.keys, base, this.lengths[e])
|
|
446
|
+
let h: i32 = this.bucket(this.keys, e * QUIC_MAX_CID_LENGTH, lengths[e])
|
|
325
447
|
let hole: i32 = -1
|
|
326
448
|
for (let probe: i32 = 0; probe < size && h >= 0 && h < size; probe++) {
|
|
327
449
|
if (this.table[h] === 0) {
|
|
@@ -333,17 +455,27 @@ export class Http3CidIndex {
|
|
|
333
455
|
}
|
|
334
456
|
h = (h + 1) & mask
|
|
335
457
|
}
|
|
336
|
-
|
|
337
|
-
|
|
458
|
+
lengths[e] = 0
|
|
459
|
+
// `hole` is -1 or a place the probe found, below `size`.
|
|
460
|
+
if (hole < 0 || hole >= size) {
|
|
338
461
|
return
|
|
339
462
|
}
|
|
340
463
|
let next: i32 = (hole + 1) & mask
|
|
341
464
|
for (let probe: i32 = 0; probe < size && next >= 0 && next < size && hole >= 0 && hole < size; probe++) {
|
|
342
|
-
const
|
|
343
|
-
if (
|
|
465
|
+
const filed: i32 = this.table[next]
|
|
466
|
+
if (filed <= 0) {
|
|
467
|
+
break
|
|
468
|
+
}
|
|
469
|
+
const other: i32 = filed - 1
|
|
470
|
+
if (other >= toI32(lengths.length) || other >= 107374182) {
|
|
471
|
+
// Never taken: only `insert` files an entry, and only one it numbers.
|
|
472
|
+
break
|
|
473
|
+
}
|
|
474
|
+
const home: i32 = this.bucket(this.keys, other * QUIC_MAX_CID_LENGTH, lengths[other])
|
|
475
|
+
if (home < 0 || home >= size) {
|
|
476
|
+
// Never taken: `bucket` answers a place of the table.
|
|
344
477
|
break
|
|
345
478
|
}
|
|
346
|
-
const home: i32 = this.bucket(this.keys, other * QUIC_MAX_CID_LENGTH, this.lengths[other])
|
|
347
479
|
// The entry may move into the hole when its home is not between the hole and it.
|
|
348
480
|
if (((hole - home) & mask) < ((next - home) & mask)) {
|
|
349
481
|
this.table[hole] = this.table[next]
|
|
@@ -377,16 +509,15 @@ export class Http3Wheel {
|
|
|
377
509
|
|
|
378
510
|
/**
|
|
379
511
|
* An empty wheel whose cursor is not started.
|
|
380
|
-
* @param slots How many slots it files, 0 to `slots - 1
|
|
512
|
+
* @param slots How many slots it files, 0 to `slots - 1`; 0 or fewer files none.
|
|
381
513
|
*/
|
|
382
514
|
constructor(slots: i32) {
|
|
383
515
|
this.head = new Array<i32>(H3_SERVER_WHEEL)
|
|
384
|
-
this.nextOf =
|
|
385
|
-
this.prevOf =
|
|
386
|
-
this.bucketOf =
|
|
387
|
-
this.
|
|
388
|
-
this.
|
|
389
|
-
this.bits = new Array<i64>(H3_SERVER_WHEEL / 64)
|
|
516
|
+
this.nextOf = http3ServerFilled(slots, H3_SERVER_ZERO)
|
|
517
|
+
this.prevOf = http3ServerFilled(slots, H3_SERVER_ZERO)
|
|
518
|
+
this.bucketOf = http3ServerFilled(slots, -1)
|
|
519
|
+
this.dueAt = http3ServerZeroTimes(slots)
|
|
520
|
+
this.bits = new Array<i64>(H3_SERVER_WHEEL_WORDS)
|
|
390
521
|
}
|
|
391
522
|
|
|
392
523
|
/** Marks bucket `b` filled or not. */
|
|
@@ -404,27 +535,41 @@ export class Http3Wheel {
|
|
|
404
535
|
* @param slot The slot; one out of range or not filed is ignored.
|
|
405
536
|
*/
|
|
406
537
|
unfile(slot: i32): void {
|
|
407
|
-
|
|
538
|
+
const head = this.head
|
|
539
|
+
const nextOf = this.nextOf
|
|
540
|
+
const prevOf = this.prevOf
|
|
541
|
+
const bucketOf = this.bucketOf
|
|
542
|
+
if (
|
|
543
|
+
slot < 0 ||
|
|
544
|
+
slot >= toI32(bucketOf.length) ||
|
|
545
|
+
slot >= toI32(nextOf.length) ||
|
|
546
|
+
slot >= toI32(prevOf.length)
|
|
547
|
+
) {
|
|
548
|
+
// Past the first two, never taken: the constructor gives each per-slot array `slots` entries.
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
const b: i32 = bucketOf[slot]
|
|
552
|
+
if (b < 0 || b >= toI32(head.length)) {
|
|
408
553
|
return
|
|
409
554
|
}
|
|
410
|
-
const
|
|
411
|
-
|
|
555
|
+
const prev: i32 = prevOf[slot]
|
|
556
|
+
const next: i32 = nextOf[slot]
|
|
557
|
+
if (next >= toI32(prevOf.length)) {
|
|
558
|
+
// Never taken: `file` links a slot only to a slot or -1.
|
|
412
559
|
return
|
|
413
560
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
if (prev >= 0 && prev < toI32(this.nextOf.length)) {
|
|
417
|
-
this.nextOf[prev] = next
|
|
561
|
+
if (prev >= 0 && prev < toI32(nextOf.length)) {
|
|
562
|
+
nextOf[prev] = next
|
|
418
563
|
} else {
|
|
419
|
-
|
|
564
|
+
head[b] = next + 1
|
|
420
565
|
}
|
|
421
|
-
if (next >= 0
|
|
422
|
-
|
|
566
|
+
if (next >= 0) {
|
|
567
|
+
prevOf[next] = prev
|
|
423
568
|
}
|
|
424
|
-
if (
|
|
569
|
+
if (head[b] === 0) {
|
|
425
570
|
this.flag(b, false)
|
|
426
571
|
}
|
|
427
|
-
|
|
572
|
+
bucketOf[slot] = -1
|
|
428
573
|
}
|
|
429
574
|
|
|
430
575
|
/**
|
|
@@ -437,30 +582,52 @@ export class Http3Wheel {
|
|
|
437
582
|
*/
|
|
438
583
|
file(slot: i32, due: i64, now: i64): void {
|
|
439
584
|
this.unfile(slot)
|
|
440
|
-
|
|
585
|
+
const head = this.head
|
|
586
|
+
const nextOf = this.nextOf
|
|
587
|
+
const prevOf = this.prevOf
|
|
588
|
+
const bucketOf = this.bucketOf
|
|
589
|
+
const dueAt = this.dueAt
|
|
590
|
+
if (
|
|
591
|
+
due < 0 ||
|
|
592
|
+
slot < 0 ||
|
|
593
|
+
slot >= toI32(bucketOf.length) ||
|
|
594
|
+
slot >= toI32(nextOf.length) ||
|
|
595
|
+
slot >= toI32(prevOf.length) ||
|
|
596
|
+
slot >= toI32(dueAt.length)
|
|
597
|
+
) {
|
|
598
|
+
// Past the first three, never taken: the constructor gives each per-slot array `slots` entries.
|
|
441
599
|
return
|
|
442
600
|
}
|
|
443
601
|
if (this.cursor < 0) {
|
|
444
602
|
this.cursor = now
|
|
445
603
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
if (
|
|
449
|
-
|
|
604
|
+
const cursor: i64 = this.cursor
|
|
605
|
+
let b: i32 = -1
|
|
606
|
+
if (cursor < 0) {
|
|
607
|
+
// A cursor before the clock's zero: its horizon is below the wheel, so the sum cannot overflow.
|
|
608
|
+
const horizon: i64 = cursor + toI64(H3_SERVER_WHEEL - 1)
|
|
609
|
+
b = http3ServerBucket(due > horizon ? horizon : due)
|
|
610
|
+
} else if (due - cursor >= toI64(H3_SERVER_WHEEL)) {
|
|
611
|
+
// Past the horizon, `cursor + H3_SERVER_WHEEL - 1`, whose bucket is the one before the
|
|
612
|
+
// cursor's; found from that bucket, since the sum can overflow.
|
|
613
|
+
const at: i32 = http3ServerBucket(cursor)
|
|
614
|
+
b = at > 0 ? at - 1 : H3_SERVER_WHEEL - 1
|
|
615
|
+
} else {
|
|
616
|
+
b = http3ServerBucket(due < cursor ? cursor : due)
|
|
450
617
|
}
|
|
451
|
-
|
|
452
|
-
if (b < 0 || b >= H3_SERVER_WHEEL) {
|
|
618
|
+
if (b < 0 || b >= toI32(head.length)) {
|
|
453
619
|
return
|
|
454
620
|
}
|
|
455
|
-
const
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
621
|
+
const filed: i32 = head[b]
|
|
622
|
+
const first: i32 = filed > 0 ? filed - 1 : -1
|
|
623
|
+
nextOf[slot] = first
|
|
624
|
+
prevOf[slot] = -1
|
|
625
|
+
if (first >= 0 && first < toI32(prevOf.length)) {
|
|
626
|
+
prevOf[first] = slot
|
|
460
627
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
628
|
+
head[b] = slot + 1
|
|
629
|
+
bucketOf[slot] = b
|
|
630
|
+
dueAt[slot] = due
|
|
464
631
|
this.flag(b, true)
|
|
465
632
|
}
|
|
466
633
|
|
|
@@ -471,11 +638,17 @@ export class Http3Wheel {
|
|
|
471
638
|
* @returns The slot taken, or -1 when the bucket is empty.
|
|
472
639
|
*/
|
|
473
640
|
take(time: i64): i32 {
|
|
641
|
+
const head = this.head
|
|
474
642
|
const b: i32 = http3ServerBucket(time)
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
643
|
+
if (b < 0 || b >= toI32(head.length)) {
|
|
644
|
+
return -1
|
|
645
|
+
}
|
|
646
|
+
const filed: i32 = head[b]
|
|
647
|
+
if (filed <= 0) {
|
|
648
|
+
return -1
|
|
478
649
|
}
|
|
650
|
+
const slot: i32 = filed - 1
|
|
651
|
+
this.unfile(slot)
|
|
479
652
|
return slot
|
|
480
653
|
}
|
|
481
654
|
|
|
@@ -486,25 +659,38 @@ export class Http3Wheel {
|
|
|
486
659
|
* when every bucket is empty.
|
|
487
660
|
*/
|
|
488
661
|
next(now: i64): i32 {
|
|
662
|
+
const head = this.head
|
|
663
|
+
const bits = this.bits
|
|
489
664
|
// From the cursor: a bucket before it has run, and one at `now` after a
|
|
490
665
|
// tick is the horizon's, a full turn of the wheel away.
|
|
491
666
|
const from: i64 = this.cursor < 0 ? now : this.cursor
|
|
492
|
-
const
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
667
|
+
const start: i32 = http3ServerBucket(from)
|
|
668
|
+
if (start < 0 || start > 2047) {
|
|
669
|
+
// Below 0 only for a time before the clock's zero, which has no bucket; never above 2047.
|
|
670
|
+
return -1
|
|
671
|
+
}
|
|
672
|
+
// The milliseconds past `from` searched, to `H3_SERVER_WHEEL`. 2048 and 2047 are it and its
|
|
673
|
+
// mask, written out so the prover bounds the sums below.
|
|
674
|
+
let offset: i32 = 0
|
|
675
|
+
while (offset < 2048) {
|
|
676
|
+
if (from < 0 && offset > 0) {
|
|
677
|
+
// Past a negative `from`, every time up to the clock's zero has no bucket.
|
|
678
|
+
return -1
|
|
679
|
+
}
|
|
680
|
+
const b: i32 = (start + offset) & 2047
|
|
496
681
|
const word: i32 = b >> 6
|
|
497
|
-
if (word
|
|
682
|
+
if (word >= toI32(bits.length) || b >= toI32(head.length)) {
|
|
683
|
+
// Never taken: the constructor gives `head` a place per bucket and `bits` a bit.
|
|
498
684
|
return -1
|
|
499
685
|
}
|
|
500
|
-
if (
|
|
686
|
+
if (bits[word] === 0) {
|
|
501
687
|
// An empty word: on to the next one.
|
|
502
688
|
const word64: i32 = 64
|
|
503
|
-
|
|
504
|
-
} else if (
|
|
505
|
-
return
|
|
689
|
+
offset = offset + (word64 - (b & 63))
|
|
690
|
+
} else if (head[b] !== 0) {
|
|
691
|
+
return http3ServerWait(from, offset, now)
|
|
506
692
|
} else {
|
|
507
|
-
|
|
693
|
+
offset = offset + 1
|
|
508
694
|
}
|
|
509
695
|
}
|
|
510
696
|
return -1
|
|
@@ -574,6 +760,19 @@ export class Http3Server {
|
|
|
574
760
|
private free: i32[]
|
|
575
761
|
/** Per slot: 1 while on the ready ring, 2 while on the dirty stack. */
|
|
576
762
|
private queued: i32[]
|
|
763
|
+
/**
|
|
764
|
+
* The stream buffers every slot's connection borrows from, made by the
|
|
765
|
+
* constructor for the stream table `quicConfig` gives every slot: `null`
|
|
766
|
+
* when its streams have none. Each pool makes a buffer only when a stream
|
|
767
|
+
* first needs one, so an idle server holds none (QUIC-3). It is made even
|
|
768
|
+
* for a configuration `quicConnConfigFits` refuses; a slot's connection
|
|
769
|
+
* shares it only when its stream buffers are the pools' size.
|
|
770
|
+
*/
|
|
771
|
+
streamPools: QuicStreamPools | null = null
|
|
772
|
+
/** The `bodyChunk`-byte read buffers every slot's `Http3Connection` borrows, one at most each. */
|
|
773
|
+
dataPool: BufferPool
|
|
774
|
+
/** The `maxFieldSectionSize`-byte HEADERS buffers every slot's `Http3Connection` borrows around a decode. */
|
|
775
|
+
sectionPool: BufferPool
|
|
577
776
|
/** When the answer budget was last topped up, -1 before the first answer. */
|
|
578
777
|
private answerRefilled: i64 = -1
|
|
579
778
|
/** The caller's clock in microseconds, from `clock`, or -1: what each connection's pacer counts. */
|
|
@@ -588,8 +787,6 @@ export class Http3Server {
|
|
|
588
787
|
private slots: i32 = 1
|
|
589
788
|
/** The slots ever accepted into, 0 up: one past it has never held a connection, so is free without being on `free`. */
|
|
590
789
|
private used: i32 = 0
|
|
591
|
-
/** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
|
|
592
|
-
private stopped: boolean = false
|
|
593
790
|
/** What is left of the answer budget (see `answerRefilled`). */
|
|
594
791
|
private answerBudget: i32 = 0
|
|
595
792
|
/** Connections accepted into a slot, for a log or a test; saturating at 2^31 - 1. */
|
|
@@ -599,21 +796,18 @@ export class Http3Server {
|
|
|
599
796
|
/** Datagrams no slot owns dropped past the answer budget before the listener read them, saturating. */
|
|
600
797
|
limited: i32 = 0
|
|
601
798
|
private sendErrors: i32 = 0
|
|
602
|
-
/** Whether the platform sends ECN marks: true until a marked send answers -95, and every slot's `ecnMarking` with it. */
|
|
603
|
-
ecn: boolean = true
|
|
604
799
|
/**
|
|
605
|
-
* The
|
|
606
|
-
*
|
|
607
|
-
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
* shares it only when its stream buffers are the pools' size.
|
|
800
|
+
* The ECN bits of the datagram being handed on (`udpRecvFrom`'s second
|
|
801
|
+
* metadata word, 0 where the platform does not report them), which `serve`
|
|
802
|
+
* gives the slot's connection as its `arrivalEcn`, so that the server's
|
|
803
|
+
* ACKs echo the counts (RFC 9000 §13.4.1). `receive` sets it for every
|
|
804
|
+
* datagram it reads; a caller of `datagram` sets it before the call.
|
|
611
805
|
*/
|
|
612
|
-
|
|
613
|
-
/**
|
|
614
|
-
|
|
615
|
-
/**
|
|
616
|
-
|
|
806
|
+
arrivalEcn: i32 = 0
|
|
807
|
+
/** Whether the platform sends ECN marks: true until a marked send answers -95, and every slot's `ecnMarking` with it. */
|
|
808
|
+
ecn: boolean = true
|
|
809
|
+
/** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
|
|
810
|
+
private stopped: boolean = false
|
|
617
811
|
|
|
618
812
|
/**
|
|
619
813
|
* A pool of `size` slots serving HTTP/3 under `config` over QUIC under
|
|
@@ -672,32 +866,34 @@ export class Http3Server {
|
|
|
672
866
|
const bidi: i64 = quicConfig.maxStreamsBidi
|
|
673
867
|
const uni: i64 = quicConfig.maxStreamsUni
|
|
674
868
|
const local: i64 = quicConfig.localStreams
|
|
869
|
+
// `QUIC_CONN_MAX_STREAMS`, written out so the prover knows each count's bound in the sum below.
|
|
870
|
+
const most: i64 = 1024
|
|
675
871
|
if (
|
|
676
872
|
streamData >= 1 &&
|
|
677
873
|
streamData <= QUIC_CONN_MAX_STREAM_DATA &&
|
|
678
874
|
bidi >= 0 &&
|
|
679
|
-
bidi <=
|
|
875
|
+
bidi <= most &&
|
|
680
876
|
uni >= 0 &&
|
|
681
|
-
uni <=
|
|
877
|
+
uni <= most &&
|
|
682
878
|
local >= 0 &&
|
|
683
|
-
local <=
|
|
684
|
-
bidi
|
|
879
|
+
local <= most &&
|
|
880
|
+
(bidi > 0 || uni > 0 || local > 0)
|
|
685
881
|
) {
|
|
686
882
|
this.streamPools = new QuicStreamPools(toI32(streamData), toI32(bidi + uni + local), size)
|
|
687
883
|
}
|
|
688
884
|
this.rx = new Array<u8>(H3_SERVER_RECEIVE_SIZE)
|
|
689
|
-
this.tx = new Array<u8>(
|
|
885
|
+
this.tx = new Array<u8>(H3_SERVER_FLIGHT_BYTES)
|
|
690
886
|
this.from = new Array<u8>(H3_SERVER_ADDRESS)
|
|
691
887
|
this.meta = [H3_SERVER_ZERO, H3_SERVER_ZERO]
|
|
692
888
|
this.flight = new QuicFlight()
|
|
693
889
|
this.signer = new TlsP256Signer()
|
|
694
|
-
this.signatures =
|
|
695
|
-
this.state =
|
|
696
|
-
this.readyRing =
|
|
697
|
-
this.dirtyStack =
|
|
890
|
+
this.signatures = http3ServerZeroTimes(size)
|
|
891
|
+
this.state = http3ServerFilled(size, H3_SERVER_ZERO)
|
|
892
|
+
this.readyRing = http3ServerFilled(size, H3_SERVER_ZERO)
|
|
893
|
+
this.dirtyStack = http3ServerFilled(size, H3_SERVER_ZERO)
|
|
698
894
|
// Released slots only: one never used is taken at `used`.
|
|
699
|
-
this.free =
|
|
700
|
-
this.queued =
|
|
895
|
+
this.free = http3ServerFilled(size, H3_SERVER_ZERO)
|
|
896
|
+
this.queued = http3ServerFilled(size, H3_SERVER_ZERO)
|
|
701
897
|
}
|
|
702
898
|
|
|
703
899
|
/**
|
|
@@ -752,7 +948,13 @@ export class Http3Server {
|
|
|
752
948
|
* @returns 0 to `size()`.
|
|
753
949
|
*/
|
|
754
950
|
busy(): i32 {
|
|
755
|
-
|
|
951
|
+
const used: i32 = this.used
|
|
952
|
+
const freed: i32 = this.freeCount
|
|
953
|
+
// Never taken: both count up from 0.
|
|
954
|
+
if (used < 0 || freed < 0) {
|
|
955
|
+
return H3_SERVER_ZERO
|
|
956
|
+
}
|
|
957
|
+
return used - freed
|
|
756
958
|
}
|
|
757
959
|
|
|
758
960
|
/**
|
|
@@ -774,7 +976,15 @@ export class Http3Server {
|
|
|
774
976
|
*/
|
|
775
977
|
connection(slot: i32): Http3Connection {
|
|
776
978
|
this.date.current()
|
|
777
|
-
|
|
979
|
+
const at: i32 = slot >= 0 && slot < this.slots ? slot : H3_SERVER_ZERO
|
|
980
|
+
const conns: Http3Connection[] = this.conns
|
|
981
|
+
// `build` grows `conns` and `quics` together, so this builds as `slotAt` does.
|
|
982
|
+
for (;;) {
|
|
983
|
+
if (at >= 0 && at < toI32(conns.length)) {
|
|
984
|
+
return conns[at]
|
|
985
|
+
}
|
|
986
|
+
this.build()
|
|
987
|
+
}
|
|
778
988
|
}
|
|
779
989
|
|
|
780
990
|
/**
|
|
@@ -783,7 +993,14 @@ export class Http3Server {
|
|
|
783
993
|
* @returns The slot's QUIC connection, kept by the pool and reset for each connection the slot holds.
|
|
784
994
|
*/
|
|
785
995
|
quic(slot: i32): QuicConnection {
|
|
786
|
-
|
|
996
|
+
const at: i32 = slot >= 0 && slot < this.slots ? slot : H3_SERVER_ZERO
|
|
997
|
+
const quics: QuicConnection[] = this.quics
|
|
998
|
+
for (;;) {
|
|
999
|
+
if (at >= 0 && at < toI32(quics.length)) {
|
|
1000
|
+
return quics[at]
|
|
1001
|
+
}
|
|
1002
|
+
this.build()
|
|
1003
|
+
}
|
|
787
1004
|
}
|
|
788
1005
|
|
|
789
1006
|
/**
|
|
@@ -793,24 +1010,48 @@ export class Http3Server {
|
|
|
793
1010
|
* @param slot The slot; one that holds no connection is ignored.
|
|
794
1011
|
*/
|
|
795
1012
|
touch(slot: i32): void {
|
|
796
|
-
|
|
1013
|
+
const queued: i32[] = this.queued
|
|
1014
|
+
const ring: i32[] = this.readyRing
|
|
1015
|
+
// Past `holds`, never taken: `queued` is `size` long, as `state` is.
|
|
1016
|
+
if (!this.holds(slot) || slot < 0 || slot >= toI32(queued.length)) {
|
|
797
1017
|
return
|
|
798
1018
|
}
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
1019
|
+
const head: i32 = this.readyHead
|
|
1020
|
+
const count: i32 = this.readyCount
|
|
1021
|
+
const length: i32 = toI32(ring.length)
|
|
1022
|
+
// Never false past its first two terms: `ready` keeps `head` in the ring, which is at most 65536 long.
|
|
1023
|
+
if (
|
|
1024
|
+
(queued[slot] & 1) === 0 &&
|
|
1025
|
+
count < length &&
|
|
1026
|
+
head >= 0 &&
|
|
1027
|
+
head <= 65535 &&
|
|
1028
|
+
count >= 0 &&
|
|
1029
|
+
count <= 65535
|
|
1030
|
+
) {
|
|
1031
|
+
// `(head + count) % length`, both below `length`.
|
|
1032
|
+
const sum: i32 = head + count
|
|
1033
|
+
const at: i32 = sum >= length ? sum - length : sum
|
|
1034
|
+
if (at >= 0 && at < length) {
|
|
1035
|
+
ring[at] = slot
|
|
1036
|
+
this.readyCount = count + 1
|
|
1037
|
+
queued[slot] = queued[slot] | 1
|
|
1038
|
+
}
|
|
804
1039
|
}
|
|
805
1040
|
this.dirty(slot)
|
|
806
1041
|
}
|
|
807
1042
|
|
|
808
1043
|
/** Puts `slot` on the dirty stack, for `flush`. */
|
|
809
1044
|
private dirty(slot: i32): void {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
1045
|
+
const queued: i32[] = this.queued
|
|
1046
|
+
const stack: i32[] = this.dirtyStack
|
|
1047
|
+
const count: i32 = this.dirtyCount
|
|
1048
|
+
// Never false past its second term: every caller passes a slot `holds`, and the count starts at 0.
|
|
1049
|
+
if (slot >= 0 && slot < toI32(queued.length) && count >= 0) {
|
|
1050
|
+
if ((queued[slot] & 2) === 0 && count < toI32(stack.length)) {
|
|
1051
|
+
stack[count] = slot
|
|
1052
|
+
this.dirtyCount = count + 1
|
|
1053
|
+
queued[slot] = queued[slot] | 2
|
|
1054
|
+
}
|
|
814
1055
|
}
|
|
815
1056
|
}
|
|
816
1057
|
|
|
@@ -821,16 +1062,31 @@ export class Http3Server {
|
|
|
821
1062
|
*/
|
|
822
1063
|
ready(): i32 {
|
|
823
1064
|
this.date.current()
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
1065
|
+
const ring: i32[] = this.readyRing
|
|
1066
|
+
const queued: i32[] = this.queued
|
|
1067
|
+
for (;;) {
|
|
1068
|
+
const count: i32 = this.readyCount
|
|
1069
|
+
if (count <= 0) {
|
|
1070
|
+
return -1
|
|
1071
|
+
}
|
|
1072
|
+
const head: i32 = this.readyHead
|
|
1073
|
+
const length: i32 = toI32(ring.length)
|
|
1074
|
+
// Never taken: `touch` and this loop keep `head` in the ring.
|
|
1075
|
+
if (head < 0 || head >= length) {
|
|
1076
|
+
return -1
|
|
1077
|
+
}
|
|
1078
|
+
const slot: i32 = ring[head]
|
|
1079
|
+
// `(head + 1) % length`.
|
|
1080
|
+
this.readyHead = head + 1 < length ? head + 1 : H3_SERVER_ZERO
|
|
1081
|
+
this.readyCount = count - 1
|
|
1082
|
+
// Never false: `touch` puts only a slot it holds on the ring.
|
|
1083
|
+
if (slot >= 0 && slot < toI32(queued.length)) {
|
|
1084
|
+
queued[slot] = queued[slot] & ~1
|
|
1085
|
+
}
|
|
829
1086
|
if (this.holds(slot)) {
|
|
830
1087
|
return slot
|
|
831
1088
|
}
|
|
832
1089
|
}
|
|
833
|
-
return -1
|
|
834
1090
|
}
|
|
835
1091
|
|
|
836
1092
|
/**
|
|
@@ -855,16 +1111,26 @@ export class Http3Server {
|
|
|
855
1111
|
this.from,
|
|
856
1112
|
this.meta
|
|
857
1113
|
)
|
|
858
|
-
|
|
1114
|
+
const meta: i32[] = this.meta
|
|
1115
|
+
// Past the first term, never taken: `udpRecvFrom` reads at most the
|
|
1116
|
+
// `H3_SERVER_RECEIVE_SIZE` it is given, and `meta` is the constructor's two words.
|
|
1117
|
+
if (n < 0 || n > 65536 || toI32(meta.length) < 2) {
|
|
859
1118
|
return count
|
|
860
1119
|
}
|
|
861
|
-
const segment: i32 =
|
|
1120
|
+
const segment: i32 = meta[0] > 0 ? meta[0] : n
|
|
1121
|
+
// One receive is one ECN codepoint for every GRO segment of it.
|
|
1122
|
+
this.arrivalEcn = meta[1] & 3
|
|
862
1123
|
let at: i32 = 0
|
|
863
|
-
while (at < n && segment > 0) {
|
|
864
|
-
const
|
|
1124
|
+
while (at >= 0 && at < n && segment > 0) {
|
|
1125
|
+
const left: i32 = n - at
|
|
1126
|
+
const length: i32 = left < segment ? left : segment
|
|
865
1127
|
this.datagram(this.rx, at, length, this.from, now, key)
|
|
866
|
-
count = count
|
|
867
|
-
|
|
1128
|
+
count = h3Count(count)
|
|
1129
|
+
if (length < left) {
|
|
1130
|
+
at = at + length
|
|
1131
|
+
} else {
|
|
1132
|
+
at = n
|
|
1133
|
+
}
|
|
868
1134
|
}
|
|
869
1135
|
}
|
|
870
1136
|
return count
|
|
@@ -883,20 +1149,27 @@ export class Http3Server {
|
|
|
883
1149
|
* @param key The certificate's private key, as `receive` takes it.
|
|
884
1150
|
*/
|
|
885
1151
|
datagram(buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
|
|
886
|
-
|
|
1152
|
+
const end: i64 = toI64(at) + toI64(len)
|
|
1153
|
+
// The fourth term never decides: a window of at least a byte inside `buf` starts in it.
|
|
1154
|
+
if (len < 1 || at < 0 || end > toI64(buf.length) || at >= toI32(buf.length)) {
|
|
887
1155
|
return
|
|
888
1156
|
}
|
|
889
1157
|
// RFC 8999: a long header carries the DCID's length; a short one is ours, of our length.
|
|
890
1158
|
let dcidAt: i32 = at + 1
|
|
891
1159
|
let dcidLength: i32 = QUIC_CONN_CID_LENGTH
|
|
892
1160
|
if ((toI32(buf[at]) & 0x80) !== 0) {
|
|
893
|
-
if (len < 6) {
|
|
1161
|
+
if (len < 6 || at >= toI32(buf.length) - 5) {
|
|
1162
|
+
return
|
|
1163
|
+
}
|
|
1164
|
+
const lengthAt: i32 = at + 5
|
|
1165
|
+
// Never taken, nor the second term above: a window of six bytes or more holds its sixth.
|
|
1166
|
+
if (lengthAt >= toI32(buf.length)) {
|
|
894
1167
|
return
|
|
895
1168
|
}
|
|
896
|
-
dcidLength = toI32(buf[
|
|
897
|
-
dcidAt =
|
|
1169
|
+
dcidLength = toI32(buf[lengthAt])
|
|
1170
|
+
dcidAt = lengthAt + 1
|
|
898
1171
|
}
|
|
899
|
-
if (dcidLength > QUIC_MAX_CID_LENGTH || dcidAt + dcidLength >
|
|
1172
|
+
if (dcidLength > QUIC_MAX_CID_LENGTH || toI64(dcidAt) + toI64(dcidLength) > end) {
|
|
900
1173
|
return
|
|
901
1174
|
}
|
|
902
1175
|
const slot: i32 = this.index.find(buf, dcidAt, dcidLength)
|
|
@@ -927,29 +1200,62 @@ export class Http3Server {
|
|
|
927
1200
|
// A slot released before is taken first, then the next never used, so the
|
|
928
1201
|
// first accepts take slots 0, 1, 2 ... and a pool that never fills never
|
|
929
1202
|
// builds its last slots.
|
|
1203
|
+
// The slot is chosen, checked, and only then taken, so a check that fails leaves it where it was.
|
|
930
1204
|
let slot: i32 = 0
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
1205
|
+
const free: i32[] = this.free
|
|
1206
|
+
const freed: i32 = this.freeCount
|
|
1207
|
+
const used: i32 = this.used
|
|
1208
|
+
// `freeCount` and `used` once the slot is taken.
|
|
1209
|
+
let freeAfter: i32 = freed
|
|
1210
|
+
let usedAfter: i32 = used
|
|
1211
|
+
if (freed > 0) {
|
|
1212
|
+
// Never taken: `release` pushes at most one entry per slot onto `free`, `size` long.
|
|
1213
|
+
if (freed > toI32(free.length)) {
|
|
1214
|
+
return
|
|
1215
|
+
}
|
|
1216
|
+
slot = free[freed - 1]
|
|
1217
|
+
freeAfter = freed - 1
|
|
1218
|
+
} else if (used < this.slots) {
|
|
1219
|
+
// Builds the slot if it is not yet; building it again on a later call is a no-op.
|
|
1220
|
+
slot = this.slotAt(used)
|
|
1221
|
+
usedAfter = used + 1
|
|
937
1222
|
} else {
|
|
938
1223
|
return
|
|
939
1224
|
}
|
|
1225
|
+
const quics: QuicConnection[] = this.quics
|
|
1226
|
+
const conns: Http3Connection[] = this.conns
|
|
1227
|
+
const addresses: u8[][] = this.addresses
|
|
1228
|
+
const state: i32[] = this.state
|
|
1229
|
+
const signatures: i64[] = this.signatures
|
|
940
1230
|
crypto.getRandomValues(this.entropy)
|
|
941
|
-
|
|
1231
|
+
// Never taken: a released slot was built, `slotAt` builds the one it answers, and
|
|
1232
|
+
// `state` and `signatures` are `size` long.
|
|
1233
|
+
if (
|
|
1234
|
+
slot < 0 ||
|
|
1235
|
+
slot >= toI32(quics.length) ||
|
|
1236
|
+
slot >= toI32(conns.length) ||
|
|
1237
|
+
slot >= toI32(addresses.length) ||
|
|
1238
|
+
slot >= toI32(state.length) ||
|
|
1239
|
+
slot >= toI32(signatures.length)
|
|
1240
|
+
) {
|
|
1241
|
+
return
|
|
1242
|
+
}
|
|
1243
|
+
this.freeCount = freeAfter
|
|
1244
|
+
this.used = usedAfter
|
|
1245
|
+
const quic: QuicConnection = quics[slot]
|
|
1246
|
+
const conn: Http3Connection = conns[slot]
|
|
1247
|
+
const to: u8[] = addresses[slot]
|
|
1248
|
+
// Marked busy before its connections restart, which read neither array.
|
|
1249
|
+
state[slot] = H3_SERVER_BUSY
|
|
1250
|
+
signatures[slot] = H3_SERVER_NONE
|
|
942
1251
|
quic.reset(this.entropy)
|
|
943
|
-
|
|
1252
|
+
conn.restart()
|
|
944
1253
|
if (answer.retried) {
|
|
945
1254
|
quic.acceptRetry(answer.originalDcid, answer.retryScid)
|
|
946
1255
|
}
|
|
947
|
-
const to: u8[] = this.addresses[slot]
|
|
948
1256
|
for (let k: i32 = 0; k < toI32(to.length) && k < toI32(address.length); k++) {
|
|
949
1257
|
to[k] = address[k]
|
|
950
1258
|
}
|
|
951
|
-
this.state[slot] = H3_SERVER_BUSY
|
|
952
|
-
this.signatures[slot] = H3_SERVER_NONE
|
|
953
1259
|
this.accepted = h3Count(this.accepted)
|
|
954
1260
|
this.serve(slot, buf, at, len, address, now, key)
|
|
955
1261
|
}
|
|
@@ -966,6 +1272,10 @@ export class Http3Server {
|
|
|
966
1272
|
* not start a connection, are also held to `takeAnswer`'s budget.
|
|
967
1273
|
*/
|
|
968
1274
|
private worthListening(buf: u8[], at: i32, len: i32, now: i64): boolean {
|
|
1275
|
+
// Never taken: `datagram` passes a window of at least a byte inside `buf`.
|
|
1276
|
+
if (at < 0 || at >= toI32(buf.length)) {
|
|
1277
|
+
return false
|
|
1278
|
+
}
|
|
969
1279
|
const first: i32 = toI32(buf[at])
|
|
970
1280
|
if ((first & 0x80) === 0) {
|
|
971
1281
|
return len > QUIC_LISTENER_MIN_RESET && this.takeAnswer(now)
|
|
@@ -973,14 +1283,26 @@ export class Http3Server {
|
|
|
973
1283
|
if (len < QUIC_CONN_DATAGRAM_SIZE) {
|
|
974
1284
|
return false
|
|
975
1285
|
}
|
|
1286
|
+
// Never taken, nor the test in the loop: the window, `QUIC_CONN_DATAGRAM_SIZE` bytes or more,
|
|
1287
|
+
// holds its first six.
|
|
1288
|
+
if (at >= toI32(buf.length) - 5) {
|
|
1289
|
+
return false
|
|
1290
|
+
}
|
|
1291
|
+
const lengthAt: i32 = at + 5
|
|
1292
|
+
if (lengthAt >= toI32(buf.length)) {
|
|
1293
|
+
return false
|
|
1294
|
+
}
|
|
976
1295
|
let version: i32 = 0
|
|
977
1296
|
for (let k: i32 = 1; k <= 4; k++) {
|
|
978
|
-
|
|
1297
|
+
const byteAt: i32 = at + k
|
|
1298
|
+
if (byteAt < toI32(buf.length)) {
|
|
1299
|
+
version = (version << 8) | toI32(buf[byteAt])
|
|
1300
|
+
}
|
|
979
1301
|
}
|
|
980
1302
|
if (version !== 1) {
|
|
981
1303
|
return version !== 0 && this.takeAnswer(now)
|
|
982
1304
|
}
|
|
983
|
-
if ((first & 0x30) !== 0 || toI32(buf[
|
|
1305
|
+
if ((first & 0x30) !== 0 || toI32(buf[lengthAt]) < 8) {
|
|
984
1306
|
return false
|
|
985
1307
|
}
|
|
986
1308
|
if (this.full()) {
|
|
@@ -998,21 +1320,42 @@ export class Http3Server {
|
|
|
998
1320
|
* garbage costs no answer and no MAC past the budget (H3-3); counted in `limited`.
|
|
999
1321
|
*/
|
|
1000
1322
|
private takeAnswer(now: i64): boolean {
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
if (
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1323
|
+
let refilled: i64 = this.answerRefilled
|
|
1324
|
+
let budget: i32 = this.answerBudget
|
|
1325
|
+
if (refilled < 0 || now < refilled) {
|
|
1326
|
+
refilled = now
|
|
1327
|
+
budget = H3_SERVER_ANSWER_BURST
|
|
1328
|
+
} else if (now >= 0) {
|
|
1329
|
+
// Always taken: `now` is at least `refilled`, which is not negative here.
|
|
1330
|
+
const elapsed: i64 = now - refilled
|
|
1331
|
+
const earned: i64 = elapsed / H3_SERVER_ANSWER_INTERVAL
|
|
1332
|
+
// The second term never decides: an interval earned is `H3_SERVER_ANSWER_INTERVAL` elapsed.
|
|
1333
|
+
if (earned > 0 && elapsed > 0) {
|
|
1334
|
+
// The budget is 0 to `H3_SERVER_ANSWER_BURST`, 16, so 16 intervals or more always fill it;
|
|
1335
|
+
// written out so the prover bounds the sum below.
|
|
1336
|
+
const gain: i32 = earned < 16 ? toI32(earned) : 16
|
|
1337
|
+
if (gain < 16 && gain >= 0 && budget >= 0 && budget < 16) {
|
|
1338
|
+
const sum: i32 = budget + gain
|
|
1339
|
+
budget = sum < H3_SERVER_ANSWER_BURST ? sum : H3_SERVER_ANSWER_BURST
|
|
1340
|
+
} else {
|
|
1341
|
+
budget = H3_SERVER_ANSWER_BURST
|
|
1342
|
+
}
|
|
1343
|
+
// `refilled + earned * H3_SERVER_ANSWER_INTERVAL`: the part of an interval left over counts
|
|
1344
|
+
// toward the next answer.
|
|
1345
|
+
const rest: i64 = elapsed % H3_SERVER_ANSWER_INTERVAL
|
|
1346
|
+
// Always taken: `elapsed` is positive, so its remainder is under the interval.
|
|
1347
|
+
if (rest >= 0 && rest < H3_SERVER_ANSWER_INTERVAL) {
|
|
1348
|
+
refilled = now - rest
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1010
1351
|
}
|
|
1011
|
-
|
|
1352
|
+
this.answerRefilled = refilled
|
|
1353
|
+
if (budget <= 0) {
|
|
1354
|
+
this.answerBudget = budget
|
|
1012
1355
|
this.limited = h3Count(this.limited)
|
|
1013
1356
|
return false
|
|
1014
1357
|
}
|
|
1015
|
-
this.answerBudget =
|
|
1358
|
+
this.answerBudget = budget - 1
|
|
1016
1359
|
return true
|
|
1017
1360
|
}
|
|
1018
1361
|
|
|
@@ -1034,7 +1377,7 @@ export class Http3Server {
|
|
|
1034
1377
|
* handshake asks, and refiles its IDs and timer. The connection decides
|
|
1035
1378
|
* from the address whether the client moved (RFC 9000 §9.3); an address
|
|
1036
1379
|
* that is not 18 bytes long leaves it where it is.
|
|
1037
|
-
* @param slot A slot that holds a connection, as `datagram` finds it; the
|
|
1380
|
+
* @param slot A slot that holds a connection, as `datagram` finds it; one outside the pool is ignored.
|
|
1038
1381
|
* @param buf The bytes holding the datagram, read in place.
|
|
1039
1382
|
* @param at Where the datagram starts in `buf`.
|
|
1040
1383
|
* @param len Its length in bytes.
|
|
@@ -1043,8 +1386,13 @@ export class Http3Server {
|
|
|
1043
1386
|
* @param key The certificate's private key, as `receive` takes it.
|
|
1044
1387
|
*/
|
|
1045
1388
|
serve(slot: i32, buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
|
|
1046
|
-
const
|
|
1389
|
+
const quics: QuicConnection[] = this.quics
|
|
1390
|
+
if (slot < 0 || slot >= toI32(quics.length)) {
|
|
1391
|
+
return
|
|
1392
|
+
}
|
|
1393
|
+
const quic: QuicConnection = quics[slot]
|
|
1047
1394
|
quic.recovery.micros = this.micros
|
|
1395
|
+
quic.arrivalEcn = this.arrivalEcn
|
|
1048
1396
|
quic.receiveWindowFrom(buf, at, len, address, now)
|
|
1049
1397
|
const input: u8[] | null = quic.signatureInput()
|
|
1050
1398
|
if (input !== null) {
|
|
@@ -1059,31 +1407,41 @@ export class Http3Server {
|
|
|
1059
1407
|
|
|
1060
1408
|
/** Files `slot`'s connection IDs in the index again when they changed. */
|
|
1061
1409
|
private refile(slot: i32): void {
|
|
1062
|
-
const
|
|
1410
|
+
const quics: QuicConnection[] = this.quics
|
|
1411
|
+
if (slot < 0 || slot >= toI32(quics.length)) {
|
|
1412
|
+
// Never taken: every caller passes a slot of the pool.
|
|
1413
|
+
return
|
|
1414
|
+
}
|
|
1415
|
+
const quic: QuicConnection = quics[slot]
|
|
1063
1416
|
const signature: i64 =
|
|
1064
1417
|
(quic.cids.nextLocal << 16) |
|
|
1065
1418
|
(toI64(quic.cids.activeLocal()) << 8) |
|
|
1066
1419
|
(toI64(quic.originalDcidLength) << 1) |
|
|
1067
1420
|
(quic.handshakeComplete ? toI64(1) : toI64(0))
|
|
1068
|
-
|
|
1421
|
+
const signatures: i64[] = this.signatures
|
|
1422
|
+
// `slot` is under 65536, the most slots the constructor takes, so its first entry below fits.
|
|
1423
|
+
if (slot >= toI32(signatures.length) || slot > 65535) {
|
|
1424
|
+
// Never taken: `signatures` has an entry per slot.
|
|
1425
|
+
return
|
|
1426
|
+
}
|
|
1427
|
+
if (signature === signatures[slot]) {
|
|
1069
1428
|
return
|
|
1070
1429
|
}
|
|
1071
|
-
|
|
1430
|
+
signatures[slot] = signature
|
|
1072
1431
|
this.unindex(slot)
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1432
|
+
// 5 is `H3_SERVER_KEYS` and 4 `QUIC_CONN_LOCAL_CIDS`, written out so the entries' bound is known.
|
|
1433
|
+
const keys: i32 = 5
|
|
1434
|
+
const cids: i32 = 4
|
|
1435
|
+
const first: i32 = slot * keys
|
|
1436
|
+
const local = quic.cids.local
|
|
1437
|
+
for (let j: i32 = 0; j < cids && j < toI32(local.length); j++) {
|
|
1438
|
+
const entry = local[j]
|
|
1076
1439
|
if (entry.used) {
|
|
1077
1440
|
this.index.insert(first + j, entry.cid, H3_SERVER_ZERO, entry.length)
|
|
1078
1441
|
}
|
|
1079
1442
|
}
|
|
1080
1443
|
if (!quic.handshakeComplete && quic.originalDcidLength > 0) {
|
|
1081
|
-
this.index.insert(
|
|
1082
|
-
first + QUIC_CONN_LOCAL_CIDS,
|
|
1083
|
-
quic.originalDcid,
|
|
1084
|
-
H3_SERVER_ZERO,
|
|
1085
|
-
quic.originalDcidLength
|
|
1086
|
-
)
|
|
1444
|
+
this.index.insert(first + cids, quic.originalDcid, H3_SERVER_ZERO, quic.originalDcidLength)
|
|
1087
1445
|
}
|
|
1088
1446
|
}
|
|
1089
1447
|
|
|
@@ -1108,9 +1466,14 @@ export class Http3Server {
|
|
|
1108
1466
|
mark = H3_SERVER_ZERO
|
|
1109
1467
|
}
|
|
1110
1468
|
const step: i32 = segment > 0 ? segment : len
|
|
1469
|
+
if (at < 0 || len < 0) {
|
|
1470
|
+
// Never taken: `udpSendTo` above took `buf[at .. at + len)`.
|
|
1471
|
+
return false
|
|
1472
|
+
}
|
|
1111
1473
|
let p: i32 = at
|
|
1112
|
-
|
|
1113
|
-
|
|
1474
|
+
let left: i32 = len
|
|
1475
|
+
while (left > 0) {
|
|
1476
|
+
const n: i32 = left < step ? left : step
|
|
1114
1477
|
let one: i32 = udpSendTo(this.fd, buf, p, n, to, H3_SERVER_ZERO, mark)
|
|
1115
1478
|
if (one === H3_SERVER_UNSUPPORTED && mark !== 0) {
|
|
1116
1479
|
// No ECN here: this datagram and the rest go unmarked and no slot
|
|
@@ -1124,7 +1487,13 @@ export class Http3Server {
|
|
|
1124
1487
|
this.sendErrors = h3Count(this.sendErrors)
|
|
1125
1488
|
return false
|
|
1126
1489
|
}
|
|
1127
|
-
|
|
1490
|
+
if (n < 0 || n > left) {
|
|
1491
|
+
// Never taken: `n` is the smaller of `left` and a positive `step`.
|
|
1492
|
+
return false
|
|
1493
|
+
}
|
|
1494
|
+
left = left - n
|
|
1495
|
+
// `p + n`, at most `at + len`, which `udpSendTo` took, so it fits an i32.
|
|
1496
|
+
p = toI32(toI64(p) + toI64(n))
|
|
1128
1497
|
}
|
|
1129
1498
|
return true
|
|
1130
1499
|
}
|
|
@@ -1145,10 +1514,21 @@ export class Http3Server {
|
|
|
1145
1514
|
* @param now The current millisecond on the caller's monotonic clock.
|
|
1146
1515
|
*/
|
|
1147
1516
|
flush(now: i64): void {
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1517
|
+
const stack: i32[] = this.dirtyStack
|
|
1518
|
+
const queued: i32[] = this.queued
|
|
1519
|
+
for (;;) {
|
|
1520
|
+
const count: i32 = this.dirtyCount
|
|
1521
|
+
// Past the first term, never taken: `dirty` pushes only below the stack's length.
|
|
1522
|
+
if (count <= 0 || count > toI32(stack.length)) {
|
|
1523
|
+
return
|
|
1524
|
+
}
|
|
1525
|
+
const top: i32 = count - 1
|
|
1526
|
+
this.dirtyCount = top
|
|
1527
|
+
const slot: i32 = stack[top]
|
|
1528
|
+
// Always taken: the stack holds slots of the pool.
|
|
1529
|
+
if (slot >= 0 && slot < toI32(queued.length)) {
|
|
1530
|
+
queued[slot] = queued[slot] & ~2
|
|
1531
|
+
}
|
|
1152
1532
|
if (this.holds(slot)) {
|
|
1153
1533
|
this.flushSlot(slot, now)
|
|
1154
1534
|
}
|
|
@@ -1157,9 +1537,22 @@ export class Http3Server {
|
|
|
1157
1537
|
|
|
1158
1538
|
/** `flush` of one slot. */
|
|
1159
1539
|
private flushSlot(slot: i32, now: i64): void {
|
|
1160
|
-
const
|
|
1540
|
+
const quics: QuicConnection[] = this.quics
|
|
1541
|
+
const conns: Http3Connection[] = this.conns
|
|
1542
|
+
const addresses: u8[][] = this.addresses
|
|
1543
|
+
if (
|
|
1544
|
+
slot < 0 ||
|
|
1545
|
+
slot >= toI32(quics.length) ||
|
|
1546
|
+
slot >= toI32(conns.length) ||
|
|
1547
|
+
slot >= toI32(addresses.length)
|
|
1548
|
+
) {
|
|
1549
|
+
// Never taken: `flush` passes a slot that holds a connection, which `build` made in all three.
|
|
1550
|
+
return
|
|
1551
|
+
}
|
|
1552
|
+
const quic: QuicConnection = quics[slot]
|
|
1553
|
+
const conn: Http3Connection = conns[slot]
|
|
1554
|
+
const to: u8[] = addresses[slot]
|
|
1161
1555
|
quic.recovery.micros = this.micros
|
|
1162
|
-
const conn: Http3Connection = this.conns[slot]
|
|
1163
1556
|
if (conn.isDone() && !quic.closed()) {
|
|
1164
1557
|
quic.close(H3_NO_ERROR)
|
|
1165
1558
|
}
|
|
@@ -1173,7 +1566,6 @@ export class Http3Server {
|
|
|
1173
1566
|
if (probe > 0) {
|
|
1174
1567
|
this.send(this.tx, H3_SERVER_ZERO, probe, quic.probeAddress, H3_SERVER_ZERO, H3_SERVER_ZERO)
|
|
1175
1568
|
}
|
|
1176
|
-
const to: u8[] = this.addresses[slot]
|
|
1177
1569
|
if (quic.pathKnown) {
|
|
1178
1570
|
const current: u8[] = quic.sendAddress()
|
|
1179
1571
|
for (let k: i32 = 0; k < toI32(to.length) && k < toI32(current.length); k++) {
|
|
@@ -1230,8 +1622,14 @@ export class Http3Server {
|
|
|
1230
1622
|
|
|
1231
1623
|
/** Takes every one of `slot`'s connection IDs out of the index. */
|
|
1232
1624
|
private unindex(slot: i32): void {
|
|
1233
|
-
|
|
1234
|
-
|
|
1625
|
+
// Never taken: a slot is 0 to 65535, under the most slots the constructor takes.
|
|
1626
|
+
if (slot < 0 || slot > 65535) {
|
|
1627
|
+
return
|
|
1628
|
+
}
|
|
1629
|
+
// 5 is `H3_SERVER_KEYS`, written out so the entries' bound is known.
|
|
1630
|
+
const keys: i32 = 5
|
|
1631
|
+
const first: i32 = slot * keys
|
|
1632
|
+
for (let j: i32 = 0; j < keys; j++) {
|
|
1235
1633
|
this.index.remove(first + j)
|
|
1236
1634
|
}
|
|
1237
1635
|
}
|
|
@@ -1240,11 +1638,21 @@ export class Http3Server {
|
|
|
1240
1638
|
private release(slot: i32): void {
|
|
1241
1639
|
this.unindex(slot)
|
|
1242
1640
|
this.wheel.unfile(slot)
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
if (
|
|
1246
|
-
|
|
1247
|
-
|
|
1641
|
+
const conns: Http3Connection[] = this.conns
|
|
1642
|
+
const state: i32[] = this.state
|
|
1643
|
+
if (slot < 0 || slot >= toI32(conns.length) || slot >= toI32(state.length)) {
|
|
1644
|
+
// Never taken: every caller passes a slot that holds a connection.
|
|
1645
|
+
return
|
|
1646
|
+
}
|
|
1647
|
+
// Marked free before the connection lets go of its buffers, which read
|
|
1648
|
+
// nothing of the server's.
|
|
1649
|
+
state[slot] = H3_SERVER_FREE
|
|
1650
|
+
conns[slot].release()
|
|
1651
|
+
const free: i32[] = this.free
|
|
1652
|
+
const count: i32 = this.freeCount
|
|
1653
|
+
if (count >= 0 && count < toI32(free.length)) {
|
|
1654
|
+
free[count] = slot
|
|
1655
|
+
this.freeCount = count + 1
|
|
1248
1656
|
}
|
|
1249
1657
|
}
|
|
1250
1658
|
|
|
@@ -1266,11 +1674,13 @@ export class Http3Server {
|
|
|
1266
1674
|
*/
|
|
1267
1675
|
stop(): void {
|
|
1268
1676
|
this.stopped = true
|
|
1677
|
+
const conns: Http3Connection[] = this.conns
|
|
1269
1678
|
for (let slot: i32 = 0; slot < toI32(this.quics.length); slot++) {
|
|
1270
1679
|
if (this.holds(slot)) {
|
|
1271
1680
|
this.release(slot)
|
|
1272
|
-
} else {
|
|
1273
|
-
|
|
1681
|
+
} else if (slot < toI32(conns.length)) {
|
|
1682
|
+
// Always taken: `build` grows `conns` and `quics` together.
|
|
1683
|
+
conns[slot].release()
|
|
1274
1684
|
}
|
|
1275
1685
|
}
|
|
1276
1686
|
this.listener.release()
|
|
@@ -1286,35 +1696,91 @@ export class Http3Server {
|
|
|
1286
1696
|
* @param now The current millisecond on the caller's monotonic clock.
|
|
1287
1697
|
*/
|
|
1288
1698
|
tick(now: i64): void {
|
|
1699
|
+
// `now + 1`, where the cursor ends once `now` has run. Below zero it fits;
|
|
1700
|
+
// from zero it is put back together from the millisecond's high and low
|
|
1701
|
+
// parts, whose ranges show the sum fits.
|
|
1702
|
+
let after: i64 = 0
|
|
1703
|
+
if (now < 0) {
|
|
1704
|
+
after = now + 1
|
|
1705
|
+
} else {
|
|
1706
|
+
const high: i64 = now / 1048576
|
|
1707
|
+
const low: i64 = now % 1048576
|
|
1708
|
+
if (high >= 1073741824) {
|
|
1709
|
+
// Never taken: a millisecond of `monotonicNanos() / 1000000` is under
|
|
1710
|
+
// 2^44, so `high` is under 2^24.
|
|
1711
|
+
return
|
|
1712
|
+
}
|
|
1713
|
+
after = high * 1048576 + low + 1
|
|
1714
|
+
}
|
|
1289
1715
|
const wheel: Http3Wheel = this.wheel
|
|
1290
|
-
|
|
1291
|
-
|
|
1716
|
+
let cursor: i64 = wheel.cursor
|
|
1717
|
+
if (cursor < 0) {
|
|
1718
|
+
cursor = now
|
|
1292
1719
|
}
|
|
1293
1720
|
// Late by more than a turn: every bucket is run once, from a turn ago, so a
|
|
1294
|
-
// slot filed again lands after `now` and not behind the cursor.
|
|
1295
|
-
|
|
1296
|
-
|
|
1721
|
+
// slot filed again lands after `now` and not behind the cursor. 2048 is
|
|
1722
|
+
// `H3_SERVER_WHEEL`, written out so the prover bounds the sums, and compared
|
|
1723
|
+
// on each side of zero so no difference overflows.
|
|
1724
|
+
if (now >= 0) {
|
|
1725
|
+
if (now - 2048 >= cursor) {
|
|
1726
|
+
cursor = now - 2047
|
|
1727
|
+
}
|
|
1728
|
+
} else if (cursor < 0) {
|
|
1729
|
+
const behind: i64 = now - cursor
|
|
1730
|
+
if (behind >= 2048) {
|
|
1731
|
+
// `now - 2047`.
|
|
1732
|
+
cursor = cursor + (behind - 2047)
|
|
1733
|
+
}
|
|
1297
1734
|
}
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1735
|
+
wheel.cursor = cursor
|
|
1736
|
+
for (let steps: i32 = 0; steps < H3_SERVER_WHEEL; steps++) {
|
|
1737
|
+
const at: i64 = wheel.cursor
|
|
1738
|
+
if (at > now) {
|
|
1739
|
+
break
|
|
1740
|
+
}
|
|
1741
|
+
let slot: i32 = wheel.take(at)
|
|
1301
1742
|
while (slot >= 0) {
|
|
1302
1743
|
if (this.holds(slot)) {
|
|
1303
|
-
|
|
1744
|
+
const dueAt: i64[] = wheel.dueAt
|
|
1745
|
+
const quics: QuicConnection[] = this.quics
|
|
1746
|
+
if (slot >= toI32(dueAt.length) || slot >= toI32(quics.length)) {
|
|
1747
|
+
// Never taken: `holds` is true only of a slot of the pool.
|
|
1748
|
+
} else if (dueAt[slot] > now) {
|
|
1304
1749
|
// Filed at the horizon: not due yet, so filed again.
|
|
1305
|
-
wheel.file(slot,
|
|
1750
|
+
wheel.file(slot, dueAt[slot], now)
|
|
1306
1751
|
} else {
|
|
1307
|
-
|
|
1752
|
+
quics[slot].handleTimer(now)
|
|
1308
1753
|
this.dirty(slot)
|
|
1309
1754
|
}
|
|
1310
1755
|
}
|
|
1311
1756
|
slot = wheel.take(wheel.cursor)
|
|
1312
1757
|
}
|
|
1313
|
-
|
|
1314
|
-
|
|
1758
|
+
// On to the next millisecond, `next + 1`, written so that no sum can
|
|
1759
|
+
// overflow. At `now` the cursor ends at `after`, and the loop with it.
|
|
1760
|
+
const next: i64 = wheel.cursor
|
|
1761
|
+
if (next === now) {
|
|
1762
|
+
wheel.cursor = after
|
|
1763
|
+
break
|
|
1764
|
+
}
|
|
1765
|
+
if (next < 0 && next < now) {
|
|
1766
|
+
wheel.cursor = next + 1
|
|
1767
|
+
} else if (next >= 0 && now >= 0) {
|
|
1768
|
+
// Both from zero, so the distance left fits.
|
|
1769
|
+
const ahead: i64 = now - next
|
|
1770
|
+
if (ahead >= 1) {
|
|
1771
|
+
wheel.cursor = now - (ahead - 1)
|
|
1772
|
+
} else {
|
|
1773
|
+
// Never taken: the bucket just run was at `now` or before.
|
|
1774
|
+
break
|
|
1775
|
+
}
|
|
1776
|
+
} else {
|
|
1777
|
+
// Never taken: the bucket just run was at `now` or before, and running
|
|
1778
|
+
// it moves the cursor only from before zero to `now`.
|
|
1779
|
+
break
|
|
1780
|
+
}
|
|
1315
1781
|
}
|
|
1316
1782
|
if (wheel.cursor <= now) {
|
|
1317
|
-
wheel.cursor =
|
|
1783
|
+
wheel.cursor = after
|
|
1318
1784
|
}
|
|
1319
1785
|
}
|
|
1320
1786
|
|