@amritk/nish-aarch64-linux 0.39.0 → 0.41.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.
Files changed (57) hide show
  1. package/INSTALL.md +79 -133
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-uring.c +1194 -0
  16. package/runtime/runtime-wasm.c +422 -0
  17. package/runtime/vite.mjs +1 -1
  18. package/scripts/build.sh +22 -6
  19. package/std/README.md +74 -87
  20. package/std/array.ts +36 -3
  21. package/std/collections.ts +51 -0
  22. package/std/crypto/hmac.ts +37 -10
  23. package/std/crypto/p256.ts +1388 -2664
  24. package/std/crypto/pki.ts +116 -4
  25. package/std/crypto/sha256.ts +34 -6
  26. package/std/crypto/x509.ts +7 -0
  27. package/std/host/game.ts +273 -0
  28. package/std/input.ts +56 -0
  29. package/std/json-tokenizer.ts +325 -112
  30. package/std/json-typed.ts +43 -8
  31. package/std/json-value.ts +144 -3
  32. package/std/json.ts +63 -8
  33. package/std/net/fetch.ts +22 -4
  34. package/std/net/http-server.ts +3382 -0
  35. package/std/net/http1-client-codec.ts +6 -2
  36. package/std/net/http1-codec.ts +137 -12
  37. package/std/net/http1-server.ts +83 -29
  38. package/std/net/http2.ts +47 -14
  39. package/std/net/http3-server.ts +29 -3
  40. package/std/net/http3.ts +180 -7
  41. package/std/net/prefork.ts +49 -13
  42. package/std/net/quic-recovery.ts +240 -9
  43. package/std/net/quic-stream.ts +21 -1
  44. package/std/net/quic.ts +98 -4
  45. package/std/net/router.ts +378 -31
  46. package/std/net/serve.ts +873 -65
  47. package/std/net/tls/verify.ts +19 -1
  48. package/std/net/url.ts +83 -35
  49. package/std/net/websocket.ts +74 -1
  50. package/std/net/webtransport.ts +7 -1
  51. package/std/raster.ts +837 -0
  52. package/std/release.ts +593 -0
  53. package/std/synth.ts +143 -0
  54. package/std/temporal.ts +3091 -0
  55. package/std/threads.ts +298 -0
  56. package/std/yaml-json.ts +368 -28
  57. package/std/yaml.ts +122 -27
package/std/net/http3.ts CHANGED
@@ -94,6 +94,9 @@
94
94
  * - a GOAWAY whose push ID rose, a MAX_PUSH_ID that fell, a CANCEL_PUSH past
95
95
  * the push IDs allowed (H3_ID_ERROR, §5.2, §7.2.7, §7.2.3);
96
96
  * - a control frame past `H3_CONTROL_FRAME_MAX` (H3_EXCESSIVE_LOAD);
97
+ * - a request reset past what `resetBudget` allows — by the client, or by
98
+ * this side for a stream error or a 431 the client caused, net of the
99
+ * requests this side answered (H3_EXCESSIVE_LOAD, §10.5);
97
100
  * - and QPACK's three.
98
101
  *
99
102
  * A stream error resets one request, both ways, and the connection lives: a
@@ -312,11 +315,22 @@ const H3_FLAG_WAITING: i32 = 16
312
315
  const H3_FLAG_WT: i32 = 256
313
316
  /** A request stream past this side's GOAWAY, refused unless its first bytes make it a WebTransport stream. */
314
317
  const H3_FLAG_LATE: i32 = 512
318
+ /** A request already charged to the reset budget: a client's RESET_STREAM and STOP_SENDING on one stream cost one. */
319
+ const H3_FLAG_CHARGED: i32 = 1024
315
320
 
316
321
  /** The connection's states. */
317
322
  const H3_STATE_OPEN: i32 = 0
318
323
  const H3_STATE_FAILED: i32 = 1
319
324
 
325
+ /** The clock's modes. OFF: no `next` has found `h3` chosen yet, so nothing is timed. */
326
+ const H3_CLOCK_OFF: i32 = -1
327
+ /** IDLE: no request in hand; timed by `idleTimeout`. */
328
+ const H3_CLOCK_IDLE: i32 = 0
329
+ /** HEAD: a request short of its head; timed by `headTimeout` and `idleTimeout`. */
330
+ const H3_CLOCK_HEAD: i32 = 1
331
+ /** BUSY: every request in hand past its head; not timed. */
332
+ const H3_CLOCK_BUSY: i32 = 2
333
+
320
334
  /** What `requestFrame` answers when it read a frame header and the stream has more to read. */
321
335
  const H3_CONTINUE: i32 = -1
322
336
 
@@ -364,11 +378,35 @@ export class Http3Config {
364
378
  * DATAGRAM frames; at 0 it must not (RFC 9297 §2.1.1).
365
379
  */
366
380
  webtransportSessions: i32 = 0
381
+ /**
382
+ * The milliseconds a connection may go without progress while no request
383
+ * is in hand before `clock` closes it (H3-5), 1 to 2^31 - 1. Progress is
384
+ * the connection starting, a request's head arriving whole, and the last
385
+ * request in hand finishing. With no request open the close is H3_NO_ERROR;
386
+ * with one still short of its head it is H3_EXCESSIVE_LOAD.
387
+ */
388
+ idleTimeout: i32 = 60000
389
+ /**
390
+ * The milliseconds a request's head may take to arrive whole, from its
391
+ * first byte or the last head before it, before `clock` closes the
392
+ * connection with H3_EXCESSIVE_LOAD (H3-5), 1 to 2^31 - 1. A body that
393
+ * trickles after its head is the program's to time, as a long response is.
394
+ */
395
+ headTimeout: i32 = 30000
367
396
  /**
368
397
  * Whether `:protocol` is accepted (RFC 9220's extended CONNECT), and
369
398
  * SETTINGS_ENABLE_CONNECT_PROTOCOL sent. WebTransport turns it on.
370
399
  */
371
400
  extendedConnect: boolean = false
401
+ /**
402
+ * How many requests may be reset before the connection is closed with
403
+ * H3_EXCESSIVE_LOAD, net of those this side answered: each request the
404
+ * client resets or stops, and each this side resets for a stream error or
405
+ * answers with a 431, costs one; each response this side ends with a FIN
406
+ * earns one back, up to this. Rapid reset (CVE-2023-44487) and MadeYouReset
407
+ * (CVE-2025-8671) over HTTP/3.
408
+ */
409
+ resetBudget: i32 = 100
372
410
  }
373
411
 
374
412
  /** Panics unless `value` lies in `[low, high]`: a cap out of range is the program's mistake. */
@@ -539,6 +577,9 @@ export class Http3Connection {
539
577
  lastStreamId: i64 = 0
540
578
  /** H3_STREAM's session ID. */
541
579
  session: i64 = -1
580
+ /** When `clock` last saw progress, and when it first saw a head waiting since; -1 for not yet. */
581
+ private clockSince: i64 = -1
582
+ private headSince: i64 = -1
542
583
  /**
543
584
  * The QUIC stream slot of the last event's stream: still right when the read that made the event freed it.
544
585
  * @internal Read by `webtransport.ts` to find its own per-slot state; not for callers.
@@ -575,9 +616,18 @@ export class Http3Connection {
575
616
  * with `content-length`, a stream that ends before its HEADERS, a trailer section too large.
576
617
  */
577
618
  streamErrors: i32 = 0
619
+ /** What is left of `config.resetBudget`. */
620
+ private resetCredit: i32 = 0
578
621
  private againHead: i32 = 0
579
622
  private againCount: i32 = 0
580
623
  private waitCount: i32 = 0
624
+ /**
625
+ * The heads delivered, wrapping, and the count and `H3_CLOCK_*` mode `clock` last saw. The mode is
626
+ * OFF (-1) from construction and `restart` until `next` finds `h3` chosen and sets it to IDLE.
627
+ */
628
+ private heads: i32 = 0
629
+ private clockHeads: i32 = 0
630
+ private clockMode: i32 = -1
581
631
  /** How many times `restart` ran: a layer above that keeps state per connection compares it to know when to forget its own. */
582
632
  generation: i32 = 0
583
633
  /** Whether the client's SETTINGS have arrived. */
@@ -620,6 +670,9 @@ export class Http3Connection {
620
670
  http3CheckCap("the QUIC unidirectional stream limit", toI32(quic.streams.maxUni), 3, 1024)
621
671
  http3CheckCap("the QUIC local stream limit", toI32(quic.streams.localStreams), 3, 1024)
622
672
  http3CheckCap("webtransportSessions", config.webtransportSessions, 0, H3_MAX_SESSIONS)
673
+ http3CheckCap("resetBudget", config.resetBudget, 0, 2147483647)
674
+ http3CheckCap("idleTimeout", config.idleTimeout, 1, 2147483647)
675
+ http3CheckCap("headTimeout", config.headTimeout, 1, 2147483647)
623
676
  // RFC 9297 §2.1.1: QUIC datagrams carry HTTP datagrams, which only WebTransport uses here.
624
677
  const datagrams: boolean = quic.datagramsIn.capacity() > 0
625
678
  if (config.webtransportSessions > 0 && !datagrams) {
@@ -737,10 +790,15 @@ export class Http3Connection {
737
790
  this.errorCode = 0
738
791
  this.lastStreamId = 0
739
792
  this.session = -1
793
+ this.clockSince = -1
794
+ this.headSince = -1
740
795
  this.slot = -1
741
796
  this.againHead = 0
742
797
  this.againCount = 0
743
798
  this.waitCount = 0
799
+ this.heads = 0
800
+ this.clockHeads = 0
801
+ this.clockMode = H3_CLOCK_OFF
744
802
  this.generation = this.generation < 2147483647 ? this.generation + 1 : H3_ONE
745
803
  this.dataStart = 0
746
804
  this.dataLength = 0
@@ -750,6 +808,7 @@ export class Http3Connection {
750
808
  this.rejected = 0
751
809
  this.tooLarge = 0
752
810
  this.streamErrors = 0
811
+ this.resetCredit = this.config.resetBudget
753
812
  this.settingsSeen = false
754
813
  this.resetByPeer = false
755
814
  }
@@ -782,6 +841,75 @@ export class Http3Connection {
782
841
  return this.goawayId >= 0 && this.live === 0 && streams.peerBidiClosed >= streams.peerBidiOpened
783
842
  }
784
843
 
844
+ /**
845
+ * Runs the connection's clock (H3-5): closes the QUIC connection once it
846
+ * has gone `idleTimeout` without progress while no request is in hand, or a
847
+ * request's head has waited `headTimeout`, and otherwise answers when to
848
+ * look again. `Http3Server` calls it on every flush of the slot; a program
849
+ * driving a connection itself calls it after each `next` and when the
850
+ * time it answered comes. It allocates nothing. The clock starts on the
851
+ * first `next` after the handshake chose `h3`, whether or not the client's
852
+ * MAX_STREAMS has let this side's own streams open yet: a connection the
853
+ * program runs another protocol on, and never asks for an event, is left
854
+ * to QUIC's own idle timeout.
855
+ *
856
+ * @param now The current millisecond on the caller's monotonic clock.
857
+ * @returns The millisecond the clock next needs a look, or -1 when it needs none: no `next` has
858
+ * found `h3` chosen yet, every request in hand is past its head, or the connection is closed (by this call too).
859
+ */
860
+ clock(now: i64): i64 {
861
+ if (this.clockMode === H3_CLOCK_OFF || this.quic.closed()) {
862
+ return -1
863
+ }
864
+ let mode: i32 = H3_CLOCK_BUSY
865
+ if (this.live === 0) {
866
+ mode = H3_CLOCK_IDLE
867
+ } else if (this.awaitsHead()) {
868
+ mode = H3_CLOCK_HEAD
869
+ }
870
+ const finished: boolean = this.clockMode === H3_CLOCK_BUSY && mode !== H3_CLOCK_BUSY
871
+ if (this.clockSince < 0 || this.heads !== this.clockHeads || finished) {
872
+ this.clockSince = now
873
+ this.clockHeads = this.heads
874
+ this.headSince = -1
875
+ }
876
+ if (mode !== H3_CLOCK_HEAD) {
877
+ this.headSince = -1
878
+ } else if (this.headSince < 0) {
879
+ this.headSince = now
880
+ }
881
+ this.clockMode = mode
882
+ if (mode === H3_CLOCK_BUSY) {
883
+ return -1
884
+ }
885
+ let due: i64 = this.clockSince + toI64(this.config.idleTimeout)
886
+ if (mode === H3_CLOCK_HEAD) {
887
+ due = http3Min(due, this.headSince + toI64(this.config.headTimeout))
888
+ }
889
+ if (now < due) {
890
+ return due
891
+ }
892
+ this.quic.close(mode === H3_CLOCK_HEAD ? H3_EXCESSIVE_LOAD : H3_NO_ERROR)
893
+ return -1
894
+ }
895
+
896
+ /** Whether a request stream the client opened, counted in `live`, has not yet given its head. */
897
+ private awaitsHead(): boolean {
898
+ const skip: i32 = H3_FLAG_DELIVERED | H3_FLAG_WT
899
+ for (let k: i32 = 0; k < toI32(this.slotId.length); k++) {
900
+ const flags: i32 = this.flags[k]
901
+ if (
902
+ this.phase[k] === H3_PHASE_HEADERS &&
903
+ (flags & H3_FLAG_COUNTED) !== 0 &&
904
+ (flags & skip) === 0 &&
905
+ http3IsRequest(this.slotId[k])
906
+ ) {
907
+ return true
908
+ }
909
+ }
910
+ return false
911
+ }
912
+
785
913
  /** Writes `buf[0 .. n)` to this side's new stream `id` whole, as its first bytes; answers whether it took them. */
786
914
  private writeNew(id: i64, buf: u8[], n: i32): boolean {
787
915
  return n > 0 && this.quic.streamWrite(id, buf, H3_ZERO, n, false) === n
@@ -955,6 +1083,12 @@ export class Http3Connection {
955
1083
  this.quic.fail(H3_NO_APPLICATION_PROTOCOL, toI64(0))
956
1084
  return this.fail(H3_NO_APPLICATION_PROTOCOL)
957
1085
  }
1086
+ // The clock runs from here, not from start() finishing: a client
1087
+ // whose MAX_STREAMS keeps this side's unidirectional streams unopened
1088
+ // is still served its requests, so it is still timed.
1089
+ if (this.clockMode === H3_CLOCK_OFF) {
1090
+ this.clockMode = H3_CLOCK_IDLE
1091
+ }
958
1092
  this.start()
959
1093
  }
960
1094
  while (this.state !== H3_STATE_FAILED) {
@@ -1643,6 +1777,10 @@ export class Http3Connection {
1643
1777
  // The client stopped the response (QUIC answered with RESET_STREAM, §3.5 of RFC 9000).
1644
1778
  const told: boolean = (this.flags[k] & H3_FLAG_DELIVERED) !== 0
1645
1779
  this.mark(k, H3_FLAG_SEND_DONE)
1780
+ const charged: i32 = this.chargeReset(k)
1781
+ if (charged !== H3_NEED_MORE) {
1782
+ return charged
1783
+ }
1646
1784
  if (told) {
1647
1785
  if (phase !== H3_PHASE_DONE) {
1648
1786
  this.phase[k] = H3_PHASE_DISCARD
@@ -1863,6 +2001,7 @@ export class Http3Connection {
1863
2001
  this.contentLength[k] = this.fields.contentLength
1864
2002
  this.phase[k] = H3_PHASE_BODY
1865
2003
  this.flags[k] = this.flags[k] | H3_FLAG_DELIVERED
2004
+ this.heads = this.heads < 2147483647 ? this.heads + 1 : H3_ZERO
1866
2005
  this.stream = id
1867
2006
  return H3_REQUEST
1868
2007
  }
@@ -1922,6 +2061,10 @@ export class Http3Connection {
1922
2061
  if (n === QUIC_STREAM_ERR_RESET) {
1923
2062
  // §4.1.1: a response to a request that will not finish is abandoned too.
1924
2063
  this.abandon(k, id, resetCode === H3_REQUEST_CANCELLED ? H3_REQUEST_CANCELLED : H3_REQUEST_INCOMPLETE)
2064
+ const charged: i32 = this.chargeReset(k)
2065
+ if (charged !== H3_NEED_MORE) {
2066
+ return charged
2067
+ }
1925
2068
  return delivered ? this.resetEvent(id, resetCode, true) : this.dropped(id)
1926
2069
  }
1927
2070
  if (n !== QUIC_STREAM_END) {
@@ -1931,13 +2074,15 @@ export class Http3Connection {
1931
2074
  // §4.1.1: the stream ended before a whole request.
1932
2075
  this.abandon(k, id, H3_REQUEST_INCOMPLETE)
1933
2076
  this.streamErrors = h3Count(this.streamErrors)
1934
- return this.dropped(id)
2077
+ const charged: i32 = this.chargeReset(k)
2078
+ return charged !== H3_NEED_MORE ? charged : this.dropped(id)
1935
2079
  }
1936
2080
  if (this.contentLength[k] >= 0 && this.dataTotal[k] !== this.contentLength[k]) {
1937
2081
  // §4.1.2: the body is shorter than its content-length.
1938
2082
  this.abandon(k, id, H3_MESSAGE_ERROR)
1939
2083
  this.streamErrors = h3Count(this.streamErrors)
1940
- return this.resetEvent(id, H3_MESSAGE_ERROR, false)
2084
+ const charged: i32 = this.chargeReset(k)
2085
+ return charged !== H3_NEED_MORE ? charged : this.resetEvent(id, H3_MESSAGE_ERROR, false)
1941
2086
  }
1942
2087
  this.stream = id
1943
2088
  return H3_END
@@ -1957,12 +2102,39 @@ export class Http3Connection {
1957
2102
  this.abandon(k, id, code)
1958
2103
  this.quic.streamStopSending(id, code)
1959
2104
  this.phase[k] = H3_PHASE_DISCARD
2105
+ const charged: i32 = this.chargeReset(k)
2106
+ if (charged !== H3_NEED_MORE) {
2107
+ return charged
2108
+ }
1960
2109
  if ((this.flags[k] & H3_FLAG_DELIVERED) !== 0) {
1961
2110
  return this.resetEvent(id, code, false)
1962
2111
  }
1963
2112
  return this.dropped(id)
1964
2113
  }
1965
2114
 
2115
+ /**
2116
+ * Charges request `k` to the reset budget, once whatever reset it: a client
2117
+ * that cannot reset many requests itself provokes this side into it
2118
+ * (MadeYouReset, CVE-2025-8671). Answers H3_NEED_MORE, or once the budget
2119
+ * is spent the connection error H3_EXCESSIVE_LOAD (§10.5).
2120
+ */
2121
+ private chargeReset(k: i32): i32 {
2122
+ if ((this.flags[k] & H3_FLAG_CHARGED) !== 0) {
2123
+ return H3_NEED_MORE
2124
+ }
2125
+ this.flags[k] = this.flags[k] | H3_FLAG_CHARGED
2126
+ this.resetCredit = this.resetCredit - 1
2127
+ return this.resetCredit < 0 ? this.fail(H3_EXCESSIVE_LOAD) : H3_NEED_MORE
2128
+ }
2129
+
2130
+ /** This side ended request `k`'s response with a FIN: its send side is done, and a request the program answered earns the reset budget one back. */
2131
+ private answered(k: i32): void {
2132
+ this.mark(k, H3_FLAG_SEND_DONE)
2133
+ if ((this.flags[k] & H3_FLAG_DELIVERED) !== 0 && this.resetCredit < this.config.resetBudget) {
2134
+ this.resetCredit = this.resetCredit + 1
2135
+ }
2136
+ }
2137
+
1966
2138
  /** H3_DROPPED for request stream `id` with WebTransport on, which waits on such a stream; H3_NEED_MORE without. */
1967
2139
  private dropped(id: i64): i32 {
1968
2140
  if (!this.webtransport) {
@@ -2000,7 +2172,8 @@ export class Http3Connection {
2000
2172
  }
2001
2173
  this.quic.streamStopSending(id, H3_NO_ERROR)
2002
2174
  this.phase[k] = H3_PHASE_DISCARD
2003
- return this.dropped(id)
2175
+ const charged: i32 = this.chargeReset(k)
2176
+ return charged !== H3_NEED_MORE ? charged : this.dropped(id)
2004
2177
  }
2005
2178
 
2006
2179
  /**
@@ -2191,7 +2364,7 @@ export class Http3Connection {
2191
2364
  this.quic.streamWrite(id, out, H3_ZERO, toI32(length), fin)
2192
2365
  this.flags[k] = this.flags[k] | H3_FLAG_HEADERS_SENT
2193
2366
  if (fin) {
2194
- this.mark(k, H3_FLAG_SEND_DONE)
2367
+ this.answered(k)
2195
2368
  }
2196
2369
  return 0
2197
2370
  }
@@ -2299,7 +2472,7 @@ export class Http3Connection {
2299
2472
  if (len === 0) {
2300
2473
  if (endStream && this.dataLeft[k] === 0) {
2301
2474
  this.quic.streamWrite(id, this.zeros, H3_ZERO, H3_ZERO, true)
2302
- this.mark(k, H3_FLAG_SEND_DONE)
2475
+ this.answered(k)
2303
2476
  }
2304
2477
  return 0
2305
2478
  }
@@ -2336,7 +2509,7 @@ export class Http3Connection {
2336
2509
  break
2337
2510
  }
2338
2511
  if (fin) {
2339
- this.mark(k, H3_FLAG_SEND_DONE)
2512
+ this.answered(k)
2340
2513
  }
2341
2514
  }
2342
2515
  return total > 0 ? total : H3_AGAIN
@@ -2388,7 +2561,7 @@ export class Http3Connection {
2388
2561
  this.quic.streamWrite(id, this.scratch, H3_ZERO, hp, false)
2389
2562
  this.quic.streamWrite(id, buf, off, len, fin)
2390
2563
  if (fin) {
2391
- this.mark(k, H3_FLAG_SEND_DONE)
2564
+ this.answered(k)
2392
2565
  }
2393
2566
  return 0
2394
2567
  }
@@ -83,10 +83,12 @@
83
83
  * `preforkUdpCount(args)` (`--nish-prefork-udp=<fd>,<N>`, before the listener
84
84
  * flag), and sets `QuicServerConfig.workerIndex` to k and `.workerCount` to N
85
85
  * so every ID it issues starts with k. A connection then stays with its
86
- * worker when the client's address or port changes. More than 255 workers
87
- * panics; where the kernel refuses the steering (off Linux, or under a
88
- * seccomp profile that refuses it) the supervisor says so on stderr and runs
89
- * one worker.
86
+ * worker when the client's address or port changes. Port 0 is a port no UDP
87
+ * or TCP socket holds when the group binds, never another reuseport group's,
88
+ * which a port-0 bind with `SO_REUSEPORT` can be given on Linux. More than
89
+ * 255 workers panics; where the kernel refuses the steering (off Linux, or
90
+ * under a seccomp profile that refuses it) the supervisor says so on stderr
91
+ * and runs one worker.
90
92
  *
91
93
  * **How many.** `NISH_WORKERS` when it is a positive whole number, else
92
94
  * `PreforkConfig.workers` when that is positive, else
@@ -454,6 +456,40 @@ export const preforkUdpCount = (args: string[]): i32 => {
454
456
  return count > 0 ? count : 0
455
457
  }
456
458
 
459
+ /**
460
+ * A port on `host` that no UDP or TCP socket holds, for a group asked for port
461
+ * 0; a negative errno when a plain UDP socket cannot bind, -98 (`EADDRINUSE`)
462
+ * after 16 ports whose TCP side was taken. The port of a plain UDP bind,
463
+ * closed again: Linux gives a port-0 bind with `SO_REUSEPORT` a port another
464
+ * reuseport socket of the same user may hold, and the two groups would become
465
+ * one, each handed the other's datagrams. Between this close and the group's
466
+ * bind another process can still take the port; a plain bind there makes the
467
+ * group's fail, and only a reuseport bind at that instant to that one port
468
+ * joins it.
469
+ */
470
+ const preforkFreePort = (host: string): i32 => {
471
+ for (let k: i32 = 0; k < 16; k++) {
472
+ const probe: i32 = udpBind(host, 0, 0)
473
+ if (probe < 0) {
474
+ return probe
475
+ }
476
+ const port: i32 = netLocalPort(probe)
477
+ // Flags 0, not SO_REUSEPORT, which would succeed beside a same-user listener.
478
+ const listener: i32 = port > 0 ? tcpListenFlags(host, port, 1, 0) : -1
479
+ if (listener >= 0) {
480
+ netClose(listener)
481
+ }
482
+ netClose(probe)
483
+ if (port <= 0) {
484
+ return port < 0 ? port : -22
485
+ }
486
+ if (listener >= 0) {
487
+ return port
488
+ }
489
+ }
490
+ return -98
491
+ }
492
+
457
493
  /**
458
494
  * The UDP group of `listenUdp`, socket i for worker i, bound before anything
459
495
  * else on the port: steered by connection ID (`udpBind` flag 4 with the count
@@ -467,9 +503,14 @@ const preforkBindUdp = (config: PreforkConfig, count: i32): i32[] => {
467
503
  const out: i32[] = []
468
504
  // Flag 1 (SO_REUSEPORT) and 4 (steer), the group's size in bits 8 to 15.
469
505
  const steered: i32 = (count << 8) | 4 | 1
470
- const first: i32 = udpBind(host, config.listenPort, count > 1 ? steered : 1)
506
+ const asked: i32 = config.listenPort === 0 ? preforkFreePort(host) : config.listenPort
507
+ if (asked < 0) {
508
+ console.error(`nish/net/prefork: cannot find a free UDP and TCP port on ${host} (${asked})`)
509
+ return out
510
+ }
511
+ const first: i32 = udpBind(host, asked, count > 1 ? steered : 1)
471
512
  if (first < 0 && count > 1) {
472
- const plain: i32 = udpBind(host, config.listenPort, 1)
513
+ const plain: i32 = udpBind(host, asked, 1)
473
514
  if (plain >= 0) {
474
515
  console.error(`nish/net/prefork: QUIC steering refused (${first}), running one worker`)
475
516
  out.push(plain)
@@ -477,16 +518,11 @@ const preforkBindUdp = (config: PreforkConfig, count: i32): i32[] => {
477
518
  }
478
519
  }
479
520
  if (first < 0) {
480
- console.error(`nish/net/prefork: cannot bind UDP ${host}:${config.listenPort} (${first})`)
521
+ console.error(`nish/net/prefork: cannot bind UDP ${host}:${asked} (${first})`)
481
522
  return out
482
523
  }
483
524
  out.push(first)
484
- const port: i32 = config.listenPort === 0 ? netLocalPort(first) : config.listenPort
485
- if (port <= 0) {
486
- console.error(`nish/net/prefork: cannot read the UDP port bound (${port})`)
487
- netClose(first)
488
- return []
489
- }
525
+ const port: i32 = asked
490
526
  for (let i: i32 = 1; i < count; i++) {
491
527
  const fd: i32 = udpBind(host, port, steered)
492
528
  if (fd < 0) {