@amritk/nish-x86_64-linux 0.18.0 → 0.20.0
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/INSTALL.md +43 -25
- package/bin/nish +0 -0
- package/package.json +2 -2
- package/runtime/nish.d.ts +55 -5
- package/runtime/nish.h +215 -18
- package/runtime/nish.mjs +16 -0
- package/runtime/runtime-aes.c +490 -0
- package/runtime/runtime-cross.c +366 -0
- package/runtime/runtime-host.c +7 -6
- package/runtime/runtime-net.c +93 -34
- package/runtime/runtime-os.c +213 -15
- package/runtime/runtime-parallel.c +590 -107
- package/runtime/runtime-simd.c +4 -4
- package/runtime/runtime-wasm.c +5 -3
- package/runtime/runtime.c +69 -13
- package/runtime/shim.mjs +71 -1
- package/scripts/build.sh +223 -22
- package/std/README.md +81 -51
- package/std/bytes.ts +163 -0
- package/std/crypto/LICENSE-libsecp256k1 +19 -0
- package/std/crypto/aes.ts +238 -76
- package/std/crypto/chacha20poly1305.ts +43 -9
- package/std/crypto/ed25519.ts +840 -0
- package/std/crypto/hkdf.ts +304 -12
- package/std/crypto/hmac.ts +245 -53
- package/std/crypto/p256.ts +932 -61
- package/std/crypto/x25519.ts +55 -37
- package/std/crypto/x509-random.ts +71 -0
- package/std/crypto/x509.ts +9 -9
- package/std/json-tokenizer.ts +1296 -0
- package/std/json-typed.ts +1014 -0
- package/std/json-value.ts +1163 -0
- package/std/json.ts +1120 -34
- package/std/net/buffer-pool.ts +151 -0
- package/std/net/hpack.ts +416 -112
- package/std/net/http-fields.ts +93 -0
- package/std/net/http1-server.ts +417 -55
- package/std/net/http1.ts +31 -6
- package/std/net/http2-tls.ts +112 -24
- package/std/net/http2.ts +333 -75
- package/std/net/http3-server.ts +211 -31
- package/std/net/http3.ts +187 -28
- package/std/net/prefork.ts +412 -0
- package/std/net/quic-listener.ts +88 -7
- package/std/net/quic-packet.ts +243 -82
- package/std/net/quic-recovery.ts +361 -30
- package/std/net/quic-stream.ts +424 -24
- package/std/net/quic.ts +519 -167
- package/std/net/router.ts +676 -0
- package/std/net/tls/codec.ts +426 -27
- package/std/net/tls/record-server.ts +478 -132
- package/std/net/tls/record.ts +133 -42
- package/std/net/tls/schedule.ts +464 -55
- package/std/net/tls/ticket.ts +610 -0
- package/std/net/tls-tcp.ts +196 -53
- package/std/net/tls.ts +1018 -260
- package/std/net/url.ts +571 -0
- package/std/random.ts +94 -0
- package/std/threads.ts +438 -14
package/std/net/http3-server.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* // quicConfig.alpn is ["h3"]; pollAdd(loop, fd, 1, 0)
|
|
10
10
|
* // each time round the loop, with `now` in milliseconds:
|
|
11
11
|
* pollWait(loop, ready, server.timeout(now));
|
|
12
|
+
* server.clock(micros); // the same instant in microseconds, for the pacer
|
|
12
13
|
* server.receive(now, key); // every datagram waiting; signs handshakes with `key`
|
|
13
14
|
* server.tick(now); // the connections' timers
|
|
14
15
|
* let slot: i32 = server.ready();
|
|
@@ -19,6 +20,8 @@
|
|
|
19
20
|
* slot = server.ready();
|
|
20
21
|
* }
|
|
21
22
|
* server.flush(now); // what every touched slot has to send
|
|
23
|
+
* // at shutdown:
|
|
24
|
+
* server.stop(); // releases every slot, wipes the static keys
|
|
22
25
|
*
|
|
23
26
|
* **Routing.** A datagram goes to the slot that owns its first packet's
|
|
24
27
|
* Destination Connection ID, found through a hash index of every slot's
|
|
@@ -38,6 +41,10 @@
|
|
|
38
41
|
* **Sending.** `flush` writes each touched slot's datagrams with
|
|
39
42
|
* `quicListenerTakeFlight`, a paced flight at a time in one `udpSendTo` with
|
|
40
43
|
* GSO where the platform has it and one datagram at a time where it does not.
|
|
44
|
+
* The pacer counts the microseconds `clock` gives it, so on a path whose
|
|
45
|
+
* round trip is under a millisecond it neither waits for the next
|
|
46
|
+
* millisecond to earn credit nor holds a slot back for a whole one; without
|
|
47
|
+
* `clock` it counts whole milliseconds.
|
|
41
48
|
* A slot whose HTTP/3 side sent GOAWAY and whose requests have all finished,
|
|
42
49
|
* every byte acknowledged, is closed with H3_NO_ERROR; a closed slot is freed
|
|
43
50
|
* once its CONNECTION_CLOSE is out.
|
|
@@ -51,11 +58,21 @@
|
|
|
51
58
|
*
|
|
52
59
|
* **Caps, fixed at start-up**: the slots, and inside each the QUIC
|
|
53
60
|
* configuration's streams and buffers and the `Http3Config`'s field section,
|
|
54
|
-
* body chunk and DATA frame.
|
|
61
|
+
* body chunk and DATA frame. A slot's connections and address are built on
|
|
62
|
+
* the first Initial accepted into it and kept, so a pool sized for its peak
|
|
63
|
+
* costs at start-up only its per-slot tables (the index, the wheel, the
|
|
64
|
+
* queues), and holds at most `size` connections once the peak has come;
|
|
55
65
|
* a datagram an established connection reads, and every datagram it sends,
|
|
56
66
|
* allocates nothing; nor does a datagram no slot owns, which the listener
|
|
57
|
-
* reads where it lies and answers into scratch made once, nor a handshake
|
|
58
|
-
* whose state is the slot's —
|
|
67
|
+
* reads where it lies and answers into scratch made once, nor a handshake
|
|
68
|
+
* into a slot built before, whose state is the slot's —
|
|
69
|
+
* `docs/security/http3.md` (H3-1, H3-3).
|
|
70
|
+
*
|
|
71
|
+
* **The Date.** Every response carries one `date` field (RFC 9110 §6.6.1)
|
|
72
|
+
* from the pool's `HttpDate`, which formats the IMF-fixdate at most once a
|
|
73
|
+
* second for every slot; trailers carry none, and a program that sends its
|
|
74
|
+
* own `date` sends that one instead. `server.date.pin(unixMs)` fixes it, for
|
|
75
|
+
* a test that compares bytes.
|
|
59
76
|
*
|
|
60
77
|
* Written from RFC 9114 §3 and RFC 9000 §5.2, not ported from another
|
|
61
78
|
* implementation. Private names carry the `http3Server` prefix
|
|
@@ -63,6 +80,8 @@
|
|
|
63
80
|
*/
|
|
64
81
|
import { udpRecvFrom, udpSendTo } from "nish:net"
|
|
65
82
|
import { Secret } from "nish:secret"
|
|
83
|
+
import { BufferPool } from "nish/net/buffer-pool"
|
|
84
|
+
import { HttpDate } from "nish/net/http1-server"
|
|
66
85
|
import { Http3Config, Http3Connection } from "nish/net/http3"
|
|
67
86
|
import { H3_NO_ERROR, h3Count } from "nish/net/http3-frame"
|
|
68
87
|
import {
|
|
@@ -70,10 +89,13 @@ import {
|
|
|
70
89
|
QUIC_CONN_DATAGRAM_SIZE,
|
|
71
90
|
QUIC_CONN_ENTROPY_SIZE,
|
|
72
91
|
QUIC_CONN_LOCAL_CIDS,
|
|
92
|
+
QUIC_CONN_MAX_STREAM_DATA,
|
|
93
|
+
QUIC_CONN_MAX_STREAMS,
|
|
73
94
|
QuicConnection,
|
|
74
95
|
QuicServerConfig,
|
|
75
96
|
} from "nish/net/quic"
|
|
76
97
|
import { QUIC_MAX_CID_LENGTH } from "nish/net/quic-packet"
|
|
98
|
+
import { QuicStreamPools, quicStreamGrowFree } from "nish/net/quic-stream"
|
|
77
99
|
import {
|
|
78
100
|
QUIC_LISTEN_ACCEPT,
|
|
79
101
|
QUIC_LISTENER_ANSWER_SIZE,
|
|
@@ -82,7 +104,7 @@ import {
|
|
|
82
104
|
QuicFlight,
|
|
83
105
|
QuicListener,
|
|
84
106
|
QuicListenerAnswer,
|
|
85
|
-
|
|
107
|
+
quicListenerFlightTime,
|
|
86
108
|
quicListenerTakeFlight,
|
|
87
109
|
} from "nish/net/quic-listener"
|
|
88
110
|
import { TlsP256Signer } from "nish/net/tls"
|
|
@@ -397,14 +419,29 @@ export class Http3Wheel {
|
|
|
397
419
|
*/
|
|
398
420
|
export class Http3Server {
|
|
399
421
|
quicConfig: QuicServerConfig
|
|
422
|
+
/** The HTTP/3 configuration each slot's `Http3Connection` is built with. */
|
|
423
|
+
config: Http3Config
|
|
400
424
|
listener: QuicListener
|
|
401
425
|
/** What the listener answers into, made once: its reply and IDs are refilled in their own room. */
|
|
402
426
|
answer: QuicListenerAnswer
|
|
427
|
+
/** The slots built so far, 0 up: each built on the first Initial accepted into it, or the first ask for it. */
|
|
403
428
|
quics: QuicConnection[]
|
|
404
429
|
conns: Http3Connection[]
|
|
430
|
+
/**
|
|
431
|
+
* The `date` every slot's responses carry, formatted once a second for all
|
|
432
|
+
* of them: each slot's `Http3Connection.date` is its bytes, refreshed by
|
|
433
|
+
* `connection` and `ready`. `date.pin` fixes its clock.
|
|
434
|
+
*/
|
|
435
|
+
date: HttpDate
|
|
405
436
|
index: Http3CidIndex
|
|
406
437
|
wheel: Http3Wheel
|
|
407
|
-
/**
|
|
438
|
+
/**
|
|
439
|
+
* Each slot's client address, as its first datagram came from. Not
|
|
440
|
+
* validated until `quic(slot).addressValidated` is set (RFC 9000 §8.1): a
|
|
441
|
+
* Retry token the listener checked, with `retry` on, or a Handshake packet
|
|
442
|
+
* from the client. Before then it is the source of one UDP datagram, which
|
|
443
|
+
* anyone can forge, so a cap or an allow-list keyed on it waits for that.
|
|
444
|
+
*/
|
|
408
445
|
addresses: u8[][]
|
|
409
446
|
/** What `receive` reads into, and where `flush` writes a flight. */
|
|
410
447
|
rx: u8[]
|
|
@@ -421,17 +458,25 @@ export class Http3Server {
|
|
|
421
458
|
/** The slots with datagrams for the program, a ring, and the slots with something to send, a stack. */
|
|
422
459
|
readyRing: i32[]
|
|
423
460
|
dirtyStack: i32[]
|
|
424
|
-
/**
|
|
461
|
+
/** Released slots, a stack: a slot never built is taken from `used` instead. */
|
|
425
462
|
free: i32[]
|
|
426
463
|
/** Per slot: 1 while on the ready ring, 2 while on the dirty stack. */
|
|
427
464
|
queued: i32[]
|
|
428
465
|
/** When the answer budget was last topped up, -1 before the first answer. */
|
|
429
466
|
answerRefilled: i64 = -1
|
|
467
|
+
/** The caller's clock in microseconds, from `clock`, or -1: what each connection's pacer counts. */
|
|
468
|
+
micros: i64 = -1
|
|
430
469
|
fd: i32 = -1
|
|
431
470
|
readyHead: i32 = 0
|
|
432
471
|
readyCount: i32 = 0
|
|
433
472
|
dirtyCount: i32 = 0
|
|
434
473
|
freeCount: i32 = 0
|
|
474
|
+
/** How many slots the pool may hold, which `size` answers. */
|
|
475
|
+
slots: i32 = 1
|
|
476
|
+
/** The slots ever accepted into, 0 up: one past it has never held a connection, so is free without being on `free`. */
|
|
477
|
+
used: i32 = 0
|
|
478
|
+
/** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
|
|
479
|
+
stopped: boolean = false
|
|
435
480
|
/** What is left of the answer budget (see `answerRefilled`). */
|
|
436
481
|
answerBudget: i32 = 0
|
|
437
482
|
/** Counters for a log or a test, saturating: connections accepted, Initials dropped for want of a slot, datagrams past the answer budget, sends that failed. */
|
|
@@ -439,34 +484,75 @@ export class Http3Server {
|
|
|
439
484
|
refused: i32 = 0
|
|
440
485
|
limited: i32 = 0
|
|
441
486
|
sendErrors: i32 = 0
|
|
487
|
+
/**
|
|
488
|
+
* The stream buffers every slot's connection borrows from, made by the
|
|
489
|
+
* constructor for the stream table `quicConfig` gives every slot: `null`
|
|
490
|
+
* when its streams have none. Each pool makes a buffer only when a stream
|
|
491
|
+
* first needs one, so an idle server holds none (QUIC-3). It is made even
|
|
492
|
+
* for a configuration `quicConnConfigFits` refuses; `build` shares it only
|
|
493
|
+
* with a connection whose stream buffers are its size.
|
|
494
|
+
*/
|
|
495
|
+
streamPools: QuicStreamPools | null = null
|
|
496
|
+
/** The `bodyChunk`-byte read buffers every slot's `Http3Connection` borrows, one at most each. */
|
|
497
|
+
dataPool: BufferPool
|
|
498
|
+
/** The `maxFieldSectionSize`-byte HEADERS buffers every slot's `Http3Connection` borrows around a decode. */
|
|
499
|
+
sectionPool: BufferPool
|
|
442
500
|
|
|
443
501
|
/**
|
|
444
502
|
* A pool of `size` slots serving HTTP/3 under `config` over QUIC under
|
|
445
503
|
* `quicConfig` on the UDP socket `fd`. `listenerEntropy` is the
|
|
446
504
|
* listener's `QUIC_LISTENER_ENTROPY_SIZE` random bytes, whose first eight
|
|
447
505
|
* also key the connection-ID index (wiped in the caller's array, as the
|
|
448
|
-
* listener does).
|
|
506
|
+
* listener does). No slot's connections are made here: each slot's are
|
|
507
|
+
* built on the first Initial accepted into it. From here on
|
|
508
|
+
* `quicConfig`'s `statelessResetKey` and `retryTokenKey` are the server's:
|
|
509
|
+
* `stop` wipes them, so the configuration is this server's alone.
|
|
449
510
|
*/
|
|
450
511
|
constructor(quicConfig: QuicServerConfig, config: Http3Config, fd: i32, size: i32, listenerEntropy: u8[]) {
|
|
451
512
|
if (size < 1 || size > 65536) {
|
|
452
513
|
panic(`Http3Server: ${size} slots, outside 1 to 65536`)
|
|
453
514
|
}
|
|
454
515
|
this.quicConfig = quicConfig
|
|
516
|
+
this.config = config
|
|
455
517
|
this.fd = fd
|
|
518
|
+
this.slots = size
|
|
456
519
|
this.index = new Http3CidIndex(size, listenerEntropy)
|
|
457
520
|
this.listener = new QuicListener(quicConfig, listenerEntropy)
|
|
458
521
|
this.answer = new QuicListenerAnswer(QUIC_LISTENER_ANSWER_SIZE)
|
|
459
522
|
this.wheel = new Http3Wheel(size)
|
|
460
523
|
this.quics = []
|
|
461
524
|
this.conns = []
|
|
525
|
+
this.date = new HttpDate()
|
|
526
|
+
// Formatted once now, so no slot ever sends the bytes before a first format.
|
|
527
|
+
this.date.current()
|
|
462
528
|
this.addresses = []
|
|
463
529
|
this.entropy = new Array<u8>(QUIC_CONN_ENTROPY_SIZE)
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
530
|
+
this.dataPool = new BufferPool("Http3Server.data", config.bodyChunk, size)
|
|
531
|
+
this.sectionPool = new BufferPool("Http3Server.section", config.maxFieldSectionSize, size)
|
|
532
|
+
// Room for every buffer each pool can lend, so that giving one back never grows the arena.
|
|
533
|
+
const none: u8[] = []
|
|
534
|
+
quicStreamGrowFree(this.dataPool.free, size, none)
|
|
535
|
+
quicStreamGrowFree(this.sectionPool.free, size, none)
|
|
536
|
+
// The stream table every slot's connection will have, read from the
|
|
537
|
+
// configuration as `QuicConnection` reads it: a shape outside its bounds
|
|
538
|
+
// gets no pools, and a connection whose configuration does not fit has no
|
|
539
|
+
// table to share them with.
|
|
540
|
+
const streamData: i64 = quicConfig.maxStreamData
|
|
541
|
+
const bidi: i64 = quicConfig.maxStreamsBidi
|
|
542
|
+
const uni: i64 = quicConfig.maxStreamsUni
|
|
543
|
+
const local: i64 = quicConfig.localStreams
|
|
544
|
+
if (
|
|
545
|
+
streamData >= 1 &&
|
|
546
|
+
streamData <= QUIC_CONN_MAX_STREAM_DATA &&
|
|
547
|
+
bidi >= 0 &&
|
|
548
|
+
bidi <= QUIC_CONN_MAX_STREAMS &&
|
|
549
|
+
uni >= 0 &&
|
|
550
|
+
uni <= QUIC_CONN_MAX_STREAMS &&
|
|
551
|
+
local >= 0 &&
|
|
552
|
+
local <= QUIC_CONN_MAX_STREAMS &&
|
|
553
|
+
bidi + uni + local > 0
|
|
554
|
+
) {
|
|
555
|
+
this.streamPools = new QuicStreamPools(toI32(streamData), toI32(bidi + uni + local), size)
|
|
470
556
|
}
|
|
471
557
|
this.rx = new Array<u8>(H3_SERVER_RECEIVE_SIZE)
|
|
472
558
|
this.tx = new Array<u8>(QUIC_LISTENER_FLIGHT_MAX * QUIC_CONN_DATAGRAM_SIZE)
|
|
@@ -478,22 +564,57 @@ export class Http3Server {
|
|
|
478
564
|
this.state = new Array<i32>(size)
|
|
479
565
|
this.readyRing = new Array<i32>(size)
|
|
480
566
|
this.dirtyStack = new Array<i32>(size)
|
|
567
|
+
// Released slots only: one never used is taken at `used`.
|
|
481
568
|
this.free = new Array<i32>(size)
|
|
482
569
|
this.queued = new Array<i32>(size)
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Builds the next slot, `quics.length`: its QUIC connection under fresh
|
|
574
|
+
* entropy, sharing `streamPools` when its stream table is the one they
|
|
575
|
+
* were made for, its HTTP/3 connection on the pool's buffers and `date`,
|
|
576
|
+
* and its address.
|
|
577
|
+
*/
|
|
578
|
+
build(): void {
|
|
579
|
+
crypto.getRandomValues(this.entropy)
|
|
580
|
+
const quic = new QuicConnection(this.quicConfig, this.entropy)
|
|
581
|
+
this.quics.push(quic)
|
|
582
|
+
const pools: QuicStreamPools | null = this.streamPools
|
|
583
|
+
if (pools !== null && pools.data.size === quic.streams.bufferSize && quic.streams.slots.length > 0) {
|
|
584
|
+
quic.streams.share(pools)
|
|
585
|
+
}
|
|
586
|
+
const conn: Http3Connection = new Http3Connection(this.config, quic)
|
|
587
|
+
conn.share(this.dataPool, this.sectionPool)
|
|
588
|
+
conn.date = this.date.current()
|
|
589
|
+
this.conns.push(conn)
|
|
590
|
+
this.addresses.push(new Array<u8>(H3_SERVER_ADDRESS))
|
|
591
|
+
if (this.stopped) {
|
|
592
|
+
conn.release()
|
|
486
593
|
}
|
|
487
594
|
}
|
|
488
595
|
|
|
489
|
-
/**
|
|
596
|
+
/** `slot` when it is one of the pool's, built with every slot before it if it was not; 0 when it is out of range. */
|
|
597
|
+
slotAt(slot: i32): i32 {
|
|
598
|
+
const at: i32 = slot >= 0 && slot < this.slots ? slot : H3_SERVER_ZERO
|
|
599
|
+
while (toI32(this.quics.length) <= at) {
|
|
600
|
+
this.build()
|
|
601
|
+
}
|
|
602
|
+
return at
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** Whether every slot holds a connection: none released, and none left never used. */
|
|
606
|
+
full(): boolean {
|
|
607
|
+
return this.freeCount <= 0 && this.used >= this.slots
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** How many slots the pool has, built or not. */
|
|
490
611
|
size(): i32 {
|
|
491
|
-
return
|
|
612
|
+
return this.slots
|
|
492
613
|
}
|
|
493
614
|
|
|
494
615
|
/** How many slots hold a connection. */
|
|
495
616
|
busy(): i32 {
|
|
496
|
-
return this.
|
|
617
|
+
return this.used - this.freeCount
|
|
497
618
|
}
|
|
498
619
|
|
|
499
620
|
/** Whether `slot` holds a connection. */
|
|
@@ -501,16 +622,20 @@ export class Http3Server {
|
|
|
501
622
|
return slot >= 0 && slot < toI32(this.state.length) && this.state[slot] === H3_SERVER_BUSY
|
|
502
623
|
}
|
|
503
624
|
|
|
504
|
-
/**
|
|
625
|
+
/**
|
|
626
|
+
* The HTTP/3 connection of `slot`, its `date` brought up to now; a slot out
|
|
627
|
+
* of range names the first. A slot not built yet is built here, with every
|
|
628
|
+
* slot before it, so a program that binds something to each slot's
|
|
629
|
+
* connection up front gets the one its slot keeps.
|
|
630
|
+
*/
|
|
505
631
|
connection(slot: i32): Http3Connection {
|
|
506
|
-
|
|
507
|
-
return this.conns[
|
|
632
|
+
this.date.current()
|
|
633
|
+
return this.conns[this.slotAt(slot)]
|
|
508
634
|
}
|
|
509
635
|
|
|
510
|
-
/** The QUIC connection of `slot
|
|
636
|
+
/** The QUIC connection of `slot`, built as `connection` builds it; a slot out of range names the first. */
|
|
511
637
|
quic(slot: i32): QuicConnection {
|
|
512
|
-
|
|
513
|
-
return this.quics[at]
|
|
638
|
+
return this.quics[this.slotAt(slot)]
|
|
514
639
|
}
|
|
515
640
|
|
|
516
641
|
/** Puts `slot` on the ready ring and the dirty stack, unless it is on them. */
|
|
@@ -538,6 +663,7 @@ export class Http3Server {
|
|
|
538
663
|
|
|
539
664
|
/** The next slot with news for the program, or -1: call its connection's `next` until it has none. */
|
|
540
665
|
ready(): i32 {
|
|
666
|
+
this.date.current()
|
|
541
667
|
while (this.readyCount > 0) {
|
|
542
668
|
const slot: i32 = this.readyRing[this.readyHead]
|
|
543
669
|
this.readyHead = (this.readyHead + 1) % toI32(this.readyRing.length)
|
|
@@ -629,11 +755,19 @@ export class Http3Server {
|
|
|
629
755
|
}
|
|
630
756
|
return
|
|
631
757
|
}
|
|
632
|
-
|
|
758
|
+
// A slot released before is taken first, then the next never used, so the
|
|
759
|
+
// first accepts take slots 0, 1, 2 ... and a pool that never fills never
|
|
760
|
+
// builds its last slots.
|
|
761
|
+
let slot: i32 = 0
|
|
762
|
+
if (this.freeCount > 0) {
|
|
763
|
+
this.freeCount = this.freeCount - 1
|
|
764
|
+
slot = this.free[this.freeCount]
|
|
765
|
+
} else if (this.used < this.slots) {
|
|
766
|
+
slot = this.slotAt(this.used)
|
|
767
|
+
this.used = this.used + 1
|
|
768
|
+
} else {
|
|
633
769
|
return
|
|
634
770
|
}
|
|
635
|
-
this.freeCount = this.freeCount - 1
|
|
636
|
-
const slot: i32 = this.free[this.freeCount]
|
|
637
771
|
crypto.getRandomValues(this.entropy)
|
|
638
772
|
const quic: QuicConnection = this.quics[slot]
|
|
639
773
|
quic.reset(this.entropy)
|
|
@@ -680,7 +814,7 @@ export class Http3Server {
|
|
|
680
814
|
if ((first & 0x30) !== 0 || toI32(buf[at + 5]) < 8) {
|
|
681
815
|
return false
|
|
682
816
|
}
|
|
683
|
-
if (this.
|
|
817
|
+
if (this.full()) {
|
|
684
818
|
this.refused = h3Count(this.refused)
|
|
685
819
|
return false
|
|
686
820
|
}
|
|
@@ -713,9 +847,21 @@ export class Http3Server {
|
|
|
713
847
|
return true
|
|
714
848
|
}
|
|
715
849
|
|
|
850
|
+
/**
|
|
851
|
+
* Gives the server the caller's monotonic clock in microseconds: the
|
|
852
|
+
* instant the `now` of the calls that follow names, `monotonicNanos() /
|
|
853
|
+
* 1000` where `now` is `monotonicNanos() / 1000000`. The pacer counts it;
|
|
854
|
+
* every timer still counts `now`. One that is not inside `now`'s
|
|
855
|
+
* millisecond is not read.
|
|
856
|
+
*/
|
|
857
|
+
clock(micros: i64): void {
|
|
858
|
+
this.micros = micros
|
|
859
|
+
}
|
|
860
|
+
|
|
716
861
|
/** Hands `slot`'s connection a datagram, signs when the handshake asks, and refiles its IDs and timer. */
|
|
717
862
|
serve(slot: i32, buf: u8[], at: i32, len: i32, now: i64, key: Secret<u8[]>): void {
|
|
718
863
|
const quic: QuicConnection = this.quics[slot]
|
|
864
|
+
quic.recovery.micros = this.micros
|
|
719
865
|
quic.receiveWindow(buf, at, len, now)
|
|
720
866
|
const input: u8[] | null = quic.signatureInput()
|
|
721
867
|
if (input !== null) {
|
|
@@ -801,6 +947,7 @@ export class Http3Server {
|
|
|
801
947
|
/** `flush` of one slot. */
|
|
802
948
|
flushSlot(slot: i32, now: i64): void {
|
|
803
949
|
const quic: QuicConnection = this.quics[slot]
|
|
950
|
+
quic.recovery.micros = this.micros
|
|
804
951
|
if (this.conns[slot].isDone() && !quic.closed()) {
|
|
805
952
|
quic.close(H3_NO_ERROR)
|
|
806
953
|
}
|
|
@@ -843,7 +990,7 @@ export class Http3Server {
|
|
|
843
990
|
// The pacer answers a time, not a delay: the slot wakes at it when it
|
|
844
991
|
// comes before the connection's own deadline.
|
|
845
992
|
let due: i64 = quic.deadline()
|
|
846
|
-
const paced: i64 =
|
|
993
|
+
const paced: i64 = quicListenerFlightTime(quic, now)
|
|
847
994
|
if (paced > now && (due < 0 || paced < due)) {
|
|
848
995
|
due = paced
|
|
849
996
|
}
|
|
@@ -862,7 +1009,7 @@ export class Http3Server {
|
|
|
862
1009
|
release(slot: i32): void {
|
|
863
1010
|
this.unindex(slot)
|
|
864
1011
|
this.wheel.unfile(slot)
|
|
865
|
-
this.
|
|
1012
|
+
this.conns[slot].release()
|
|
866
1013
|
this.state[slot] = H3_SERVER_FREE
|
|
867
1014
|
if (this.freeCount < toI32(this.free.length)) {
|
|
868
1015
|
this.free[this.freeCount] = slot
|
|
@@ -870,6 +1017,39 @@ export class Http3Server {
|
|
|
870
1017
|
}
|
|
871
1018
|
}
|
|
872
1019
|
|
|
1020
|
+
/**
|
|
1021
|
+
* Stops the server for good and wipes the keys it holds: every built slot's
|
|
1022
|
+
* connection is released (`QuicConnection.release`, which sends nothing),
|
|
1023
|
+
* as is one built after, by `connection` or `quic`; the listener is
|
|
1024
|
+
* released; and the configuration's `statelessResetKey` and
|
|
1025
|
+
* `retryTokenKey`, the index's salt, the connections' entropy scratch
|
|
1026
|
+
* and the listener's last answer (a Retry's or a reset's token) are
|
|
1027
|
+
* zeroed. From then on every datagram is dropped and no connection is
|
|
1028
|
+
* accepted. The configuration's two keys are the server's once it is made
|
|
1029
|
+
* with them: a program that keeps a copy of either, to start the next
|
|
1030
|
+
* server with, wipes its own copy. The keys are zeroed in place, so give
|
|
1031
|
+
* each server a `QuicServerConfig` of its own: any other listener,
|
|
1032
|
+
* connection or server made with the same one would go on MACing Retry
|
|
1033
|
+
* tokens and deriving stateless reset tokens under an all-zero key, which
|
|
1034
|
+
* anyone can forge.
|
|
1035
|
+
*/
|
|
1036
|
+
stop(): void {
|
|
1037
|
+
this.stopped = true
|
|
1038
|
+
for (let slot: i32 = 0; slot < toI32(this.quics.length); slot++) {
|
|
1039
|
+
if (this.holds(slot)) {
|
|
1040
|
+
this.release(slot)
|
|
1041
|
+
} else {
|
|
1042
|
+
this.conns[slot].release()
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
this.listener.release()
|
|
1046
|
+
secureZero(this.quicConfig.statelessResetKey)
|
|
1047
|
+
secureZero(this.quicConfig.retryTokenKey)
|
|
1048
|
+
secureZero(this.index.salt)
|
|
1049
|
+
secureZero(this.entropy)
|
|
1050
|
+
this.answer.wipe()
|
|
1051
|
+
}
|
|
1052
|
+
|
|
873
1053
|
/** Runs the timers due by `now`, and marks each slot it ran for `flush`. */
|
|
874
1054
|
tick(now: i64): void {
|
|
875
1055
|
const wheel: Http3Wheel = this.wheel
|