@amritk/nish-x86_64-linux 0.42.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/net/dns.ts
CHANGED
|
@@ -78,6 +78,7 @@ import {
|
|
|
78
78
|
DNS_RESOLVE_MAX_ADDRESSES,
|
|
79
79
|
DNS_RESOLVE_PENDING,
|
|
80
80
|
DNS_RESOLVE_TIMEOUT,
|
|
81
|
+
DNS_RESOLVER_MAX_LOOKUPS,
|
|
81
82
|
DNS_SEND_TCP,
|
|
82
83
|
DnsResolver,
|
|
83
84
|
} from "nish/net/dns-resolver"
|
|
@@ -174,7 +175,8 @@ export class DnsClient {
|
|
|
174
175
|
/**
|
|
175
176
|
* A client for `resolver` whose sockets join the loop `loop` under tokens
|
|
176
177
|
* from `tokenBase`. Every buffer the client uses is made here, one TCP
|
|
177
|
-
* output buffer per query among them. Panics when `tokenBase` is negative
|
|
178
|
+
* output buffer per query among them. Panics when `tokenBase` is negative,
|
|
179
|
+
* or when `resolver.maxLookups` was written outside 1 to `DNS_RESOLVER_MAX_LOOKUPS`.
|
|
178
180
|
*
|
|
179
181
|
* @param resolver The core the client drives; the client owns it from now on, so go through
|
|
180
182
|
* the client's `start`, `result` and `cancel` rather than the resolver's.
|
|
@@ -186,21 +188,29 @@ export class DnsClient {
|
|
|
186
188
|
if (tokenBase < 0) {
|
|
187
189
|
panic(`DnsClient: a token base of ${tokenBase} is negative`)
|
|
188
190
|
}
|
|
189
|
-
|
|
191
|
+
// `DnsResolver`'s constructor holds `maxLookups` to this range; only a
|
|
192
|
+
// write to the field leaves it, and the resolver's own arrays are then the
|
|
193
|
+
// wrong size too. Inside it the `u16` count of slots, at most 128, is exact.
|
|
194
|
+
const lookups: i32 = resolver.maxLookups
|
|
195
|
+
if (lookups < 1 || lookups > DNS_RESOLVER_MAX_LOOKUPS) {
|
|
196
|
+
panic(`DnsClient: ${lookups} lookups is outside 1 to ${DNS_RESOLVER_MAX_LOOKUPS}`)
|
|
197
|
+
}
|
|
198
|
+
const slots: u16 = toU16(lookups) * 2
|
|
199
|
+
const n: i32 = toI32(slots)
|
|
190
200
|
this.resolver = resolver
|
|
191
201
|
this.loop = loop
|
|
192
202
|
this.tokenBase = tokenBase
|
|
193
|
-
this.fds = new Array<i32>(
|
|
203
|
+
this.fds = new Array<i32>(slots)
|
|
194
204
|
this.fds.fill(-1)
|
|
195
|
-
this.fdState = new Array<i32>(
|
|
205
|
+
this.fdState = new Array<i32>(slots)
|
|
196
206
|
this.outputs = []
|
|
197
207
|
let q: i32 = 0
|
|
198
208
|
while (q < n) {
|
|
199
209
|
this.outputs.push(new Array<u8>(DNS_OUTGOING_MAX_BYTES))
|
|
200
210
|
q += 1
|
|
201
211
|
}
|
|
202
|
-
this.outputLength = new Array<i32>(
|
|
203
|
-
this.outputSent = new Array<i32>(
|
|
212
|
+
this.outputLength = new Array<i32>(slots)
|
|
213
|
+
this.outputSent = new Array<i32>(slots)
|
|
204
214
|
this.out = new Array<u8>(DNS_OUTGOING_MAX_BYTES)
|
|
205
215
|
this.to = new Array<u8>(DNS_ADDRESS_BYTES)
|
|
206
216
|
this.meta = new Array<i32>(2)
|
|
@@ -217,7 +227,9 @@ export class DnsClient {
|
|
|
217
227
|
* @returns The width of the client's token range.
|
|
218
228
|
*/
|
|
219
229
|
tokens(): i32 {
|
|
220
|
-
|
|
230
|
+
// The constructor made one socket slot per query, so this is
|
|
231
|
+
// `maxLookups * 2` without the multiply.
|
|
232
|
+
return toI32(this.fds.length)
|
|
221
233
|
}
|
|
222
234
|
|
|
223
235
|
/**
|
|
@@ -228,7 +240,9 @@ export class DnsClient {
|
|
|
228
240
|
* @returns True when `token` lies in `tokenBase` to `tokenBase + tokens() - 1`.
|
|
229
241
|
*/
|
|
230
242
|
owns(token: i32): boolean {
|
|
231
|
-
|
|
243
|
+
// In i64, where the difference of two i32s cannot overflow.
|
|
244
|
+
const q: i64 = toI64(token) - toI64(this.tokenBase)
|
|
245
|
+
return q >= 0 && q < toI64(this.tokens())
|
|
232
246
|
}
|
|
233
247
|
|
|
234
248
|
/**
|
|
@@ -308,15 +322,20 @@ export class DnsClient {
|
|
|
308
322
|
* @param nowNanos The caller's monotonic clock now, in nanoseconds.
|
|
309
323
|
*/
|
|
310
324
|
ready(token: i32, events: i32, nowNanos: i64): void {
|
|
311
|
-
|
|
325
|
+
// `owns`, inline, so the slot it finds is the one indexed below. The
|
|
326
|
+
// per-query arrays are all made `tokens()` long.
|
|
327
|
+
const fds: i32[] = this.fds
|
|
328
|
+
const fdState: i32[] = this.fdState
|
|
329
|
+
const slot: i64 = toI64(token) - toI64(this.tokenBase)
|
|
330
|
+
const q: i32 = slot >= 0 && slot < toI64(fds.length) ? toI32(slot) : -1
|
|
331
|
+
if (q < 0 || q >= toI32(fds.length) || q >= toI32(fdState.length)) {
|
|
312
332
|
panic(`DnsClient.ready: the token ${token} is not the client's`)
|
|
313
333
|
}
|
|
314
|
-
const
|
|
315
|
-
const fd: i32 = this.fds[q]
|
|
334
|
+
const fd: i32 = fds[q]
|
|
316
335
|
if (fd < 0) {
|
|
317
336
|
return
|
|
318
337
|
}
|
|
319
|
-
const state: i32 =
|
|
338
|
+
const state: i32 = fdState[q]
|
|
320
339
|
if (state === DNS_FD_UDP) {
|
|
321
340
|
dnsClientReadDatagrams(this, q, fd, nowNanos)
|
|
322
341
|
} else if (state === DNS_FD_TCP_WRITE) {
|
|
@@ -356,7 +375,10 @@ export class DnsClient {
|
|
|
356
375
|
}
|
|
357
376
|
let wait: i64 = 0
|
|
358
377
|
if (deadline > nowNanos) {
|
|
359
|
-
|
|
378
|
+
// The gap is above 0 and below 2^64, so `u64` holds it exactly where an
|
|
379
|
+
// `i64` difference could overflow; rounded up, it is below 2^45 ms.
|
|
380
|
+
const gap: u64 = toU64(deadline) - toU64(nowNanos)
|
|
381
|
+
wait = toI64((gap - 1) / 1000000 + 1)
|
|
360
382
|
}
|
|
361
383
|
if (ownMs >= 0 && toI64(ownMs) < wait) {
|
|
362
384
|
return ownMs
|
|
@@ -372,11 +394,16 @@ export class DnsClient {
|
|
|
372
394
|
let q: i32 = this.nextOutgoing()
|
|
373
395
|
while (q >= 0 && q < toI32(this.fds.length)) {
|
|
374
396
|
dnsClientClose(this, q)
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
397
|
+
// `meta` is made two long and the core has just written it; a shorter
|
|
398
|
+
// one, never seen, would leave only the close done.
|
|
399
|
+
const meta: i32[] = this.meta
|
|
400
|
+
if (meta.length > 0) {
|
|
401
|
+
const kind: i32 = meta[0]
|
|
402
|
+
if (kind === DNS_SEND_TCP) {
|
|
403
|
+
dnsClientOpenTcp(this, q, nowNanos)
|
|
404
|
+
} else if (kind !== DNS_CLOSE) {
|
|
405
|
+
dnsClientSendUdp(this, q, nowNanos)
|
|
406
|
+
}
|
|
380
407
|
}
|
|
381
408
|
q = this.nextOutgoing()
|
|
382
409
|
}
|
|
@@ -392,7 +419,11 @@ export class DnsClient {
|
|
|
392
419
|
crypto.getRandomValues(this.idBytes)
|
|
393
420
|
this.idFresh = true
|
|
394
421
|
}
|
|
395
|
-
const
|
|
422
|
+
const idBytes: u8[] = this.idBytes
|
|
423
|
+
if (idBytes.length < 2) {
|
|
424
|
+
return -1 // never taken: the constructor makes `idBytes` two long and nothing replaces it
|
|
425
|
+
}
|
|
426
|
+
const id: i32 = (toI32(idBytes[0]) << 8) | toI32(idBytes[1])
|
|
396
427
|
const q: i32 = this.resolver.nextOutgoing(this.out, this.to, this.meta, id)
|
|
397
428
|
if (q >= 0 && this.meta.length > 0 && this.meta[0] !== DNS_CLOSE) {
|
|
398
429
|
this.idFresh = false
|
|
@@ -401,13 +432,22 @@ export class DnsClient {
|
|
|
401
432
|
}
|
|
402
433
|
}
|
|
403
434
|
|
|
404
|
-
/**
|
|
435
|
+
/**
|
|
436
|
+
* Closes query `q`'s socket, if it has one. Every `q` here is one `pump`
|
|
437
|
+
* bounded by `fds`, and the per-query arrays are all one length, so the
|
|
438
|
+
* range test is never taken.
|
|
439
|
+
*/
|
|
405
440
|
const dnsClientClose = (c: DnsClient, q: i32): void => {
|
|
406
|
-
const
|
|
441
|
+
const fds: i32[] = c.fds
|
|
442
|
+
const fdState: i32[] = c.fdState
|
|
443
|
+
if (q < 0 || q >= toI32(fds.length) || q >= toI32(fdState.length)) {
|
|
444
|
+
return
|
|
445
|
+
}
|
|
446
|
+
const fd: i32 = fds[q]
|
|
407
447
|
if (fd >= 0) {
|
|
408
448
|
netClose(fd)
|
|
409
|
-
|
|
410
|
-
|
|
449
|
+
fds[q] = -1
|
|
450
|
+
fdState[q] = DNS_FD_NONE
|
|
411
451
|
}
|
|
412
452
|
}
|
|
413
453
|
|
|
@@ -433,6 +473,37 @@ const dnsClientIsV4 = (to: u8[]): boolean => {
|
|
|
433
473
|
/** `n` plus one, held at the i32 maximum, so a counter never overflows. */
|
|
434
474
|
const dnsClientCount = (n: i32): i32 => (n < 2147483647 ? n + 1 : n)
|
|
435
475
|
|
|
476
|
+
/**
|
|
477
|
+
* Records `fd` as query `q`'s socket in `state`, or answers false for a `q`
|
|
478
|
+
* outside the per-query arrays, which `pump` never passes.
|
|
479
|
+
*/
|
|
480
|
+
const dnsClientTake = (c: DnsClient, q: i32, fd: i32, state: i32): boolean => {
|
|
481
|
+
const fds: i32[] = c.fds
|
|
482
|
+
const fdState: i32[] = c.fdState
|
|
483
|
+
if (q < 0 || q >= toI32(fds.length) || q >= toI32(fdState.length)) {
|
|
484
|
+
return false
|
|
485
|
+
}
|
|
486
|
+
fds[q] = fd
|
|
487
|
+
fdState[q] = state
|
|
488
|
+
return true
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Watches `fd` on the client's loop for `events` under query `q`'s token,
|
|
493
|
+
* adding it or, with `modify`, changing its events. False when that fails,
|
|
494
|
+
* as for a token past the i32 maximum, which a `tokenBase` too near it gives.
|
|
495
|
+
*/
|
|
496
|
+
const dnsClientWatch = (c: DnsClient, q: i32, fd: i32, events: i32, modify: boolean): boolean => {
|
|
497
|
+
const token: i64 = toI64(c.tokenBase) + toI64(q)
|
|
498
|
+
if (token > 2147483647) {
|
|
499
|
+
return false
|
|
500
|
+
}
|
|
501
|
+
if (modify) {
|
|
502
|
+
return pollModify(c.loop, fd, events, toI32(token)) === 0
|
|
503
|
+
}
|
|
504
|
+
return pollAdd(c.loop, fd, events, toI32(token)) === 0
|
|
505
|
+
}
|
|
506
|
+
|
|
436
507
|
/** Sends `c.out` to `c.to` from a new socket on a port the kernel picks, watched for reading. */
|
|
437
508
|
const dnsClientSendUdp = (c: DnsClient, q: i32, nowNanos: i64): void => {
|
|
438
509
|
const fd: i32 = udpBind(dnsClientIsV4(c.to) ? "0.0.0.0" : "::", 0, 0)
|
|
@@ -441,12 +512,20 @@ const dnsClientSendUdp = (c: DnsClient, q: i32, nowNanos: i64): void => {
|
|
|
441
512
|
return
|
|
442
513
|
}
|
|
443
514
|
c.socketsOpened = dnsClientCount(c.socketsOpened)
|
|
444
|
-
c
|
|
445
|
-
|
|
446
|
-
|
|
515
|
+
if (!dnsClientTake(c, q, fd, DNS_FD_UDP)) {
|
|
516
|
+
netClose(fd)
|
|
517
|
+
return
|
|
518
|
+
}
|
|
519
|
+
// The core writes `meta[1]` with every send; a `meta` too short for it, or
|
|
520
|
+
// a length outside `out`, never seen, fails the try as a short send would.
|
|
521
|
+
const meta: i32[] = c.meta
|
|
522
|
+
const out: u8[] = c.out
|
|
523
|
+
const length: i32 = meta.length > 1 ? meta[1] : -1
|
|
447
524
|
if (
|
|
448
|
-
|
|
449
|
-
|
|
525
|
+
length < 0 ||
|
|
526
|
+
length > toI32(out.length) ||
|
|
527
|
+
udpSendTo(fd, out, 0, length, c.to, 0, 0) !== length ||
|
|
528
|
+
!dnsClientWatch(c, q, fd, 1, false)
|
|
450
529
|
) {
|
|
451
530
|
dnsClientFail(c, q, nowNanos)
|
|
452
531
|
}
|
|
@@ -460,18 +539,33 @@ const dnsClientOpenTcp = (c: DnsClient, q: i32, nowNanos: i64): void => {
|
|
|
460
539
|
return
|
|
461
540
|
}
|
|
462
541
|
c.socketsOpened = dnsClientCount(c.socketsOpened)
|
|
463
|
-
c
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
542
|
+
if (!dnsClientTake(c, q, fd, DNS_FD_TCP_WRITE)) {
|
|
543
|
+
netClose(fd)
|
|
544
|
+
return
|
|
545
|
+
}
|
|
546
|
+
const outputs: u8[][] = c.outputs
|
|
547
|
+
const outputLength: i32[] = c.outputLength
|
|
548
|
+
const outputSent: i32[] = c.outputSent
|
|
549
|
+
if (q >= toI32(outputs.length) || q >= toI32(outputLength.length) || q >= toI32(outputSent.length)) {
|
|
550
|
+
dnsClientFail(c, q, nowNanos) // never taken: every per-query array is `tokens()` long
|
|
551
|
+
return
|
|
552
|
+
}
|
|
553
|
+
const meta: i32[] = c.meta
|
|
554
|
+
if (meta.length < 2) {
|
|
555
|
+
dnsClientFail(c, q, nowNanos) // never taken, as in `dnsClientSendUdp`
|
|
556
|
+
return
|
|
557
|
+
}
|
|
558
|
+
const length: i32 = meta[1]
|
|
559
|
+
const out: u8[] = c.out
|
|
560
|
+
const output: u8[] = outputs[q]
|
|
467
561
|
let i: i32 = 0
|
|
468
|
-
while (i < length && i < toI32(output.length) && i < toI32(
|
|
469
|
-
output[i] =
|
|
562
|
+
while (i < length && i < toI32(output.length) && i < toI32(out.length)) {
|
|
563
|
+
output[i] = out[i]
|
|
470
564
|
i += 1
|
|
471
565
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
if (
|
|
566
|
+
outputLength[q] = length
|
|
567
|
+
outputSent[q] = 0
|
|
568
|
+
if (!dnsClientWatch(c, q, fd, 2, false)) {
|
|
475
569
|
dnsClientFail(c, q, nowNanos)
|
|
476
570
|
}
|
|
477
571
|
}
|
|
@@ -482,7 +576,12 @@ const dnsClientOpenTcp = (c: DnsClient, q: i32, nowNanos: i64): void => {
|
|
|
482
576
|
*/
|
|
483
577
|
const dnsClientReadDatagrams = (c: DnsClient, q: i32, fd: i32, nowNanos: i64): void => {
|
|
484
578
|
let read: i32 = 0
|
|
485
|
-
while (
|
|
579
|
+
while (read < DNS_CLIENT_MAX_DATAGRAMS) {
|
|
580
|
+
// Read again each time: the core may end the try, or another open its socket.
|
|
581
|
+
const fds: i32[] = c.fds
|
|
582
|
+
if (q < 0 || q >= toI32(fds.length) || fds[q] !== fd) {
|
|
583
|
+
return
|
|
584
|
+
}
|
|
486
585
|
const n: i32 = udpRecvFrom(fd, c.buf, 0, DNS_UDP_MAX_REPLY, c.from, c.recvMeta)
|
|
487
586
|
if (n < 0) {
|
|
488
587
|
return
|
|
@@ -499,23 +598,55 @@ const dnsClientReadDatagrams = (c: DnsClient, q: i32, fd: i32, nowNanos: i64): v
|
|
|
499
598
|
* is written, the socket watched for reading instead.
|
|
500
599
|
*/
|
|
501
600
|
const dnsClientWrite = (c: DnsClient, q: i32, fd: i32, nowNanos: i64): void => {
|
|
502
|
-
|
|
601
|
+
const first: i32[] = c.outputSent
|
|
602
|
+
if (q < 0 || q >= toI32(first.length)) {
|
|
603
|
+
return // never taken: `ready` bounds `q` by the per-query arrays
|
|
604
|
+
}
|
|
605
|
+
if (first[q] === 0 && connectResult(fd) !== 0) {
|
|
503
606
|
dnsClientFail(c, q, nowNanos)
|
|
504
607
|
return
|
|
505
608
|
}
|
|
506
|
-
while (
|
|
507
|
-
|
|
609
|
+
while (true) {
|
|
610
|
+
// Each call below may change what the fields hold, so each turn reads
|
|
611
|
+
// them again.
|
|
612
|
+
const outputs: u8[][] = c.outputs
|
|
613
|
+
const outputLength: i32[] = c.outputLength
|
|
614
|
+
const outputSent: i32[] = c.outputSent
|
|
615
|
+
if (q >= toI32(outputs.length) || q >= toI32(outputLength.length) || q >= toI32(outputSent.length)) {
|
|
616
|
+
return // never taken, as above
|
|
617
|
+
}
|
|
618
|
+
const output: u8[] = outputs[q]
|
|
619
|
+
const length: i32 = outputLength[q]
|
|
620
|
+
const sent: i32 = outputSent[q]
|
|
621
|
+
if (sent >= length) {
|
|
622
|
+
break
|
|
623
|
+
}
|
|
624
|
+
// `dnsClientOpenTcp` keeps a length the core wrote, at most
|
|
625
|
+
// `DNS_OUTGOING_MAX_BYTES`, the size of `output`; one outside it, never
|
|
626
|
+
// seen, fails the try as a failed write does.
|
|
627
|
+
if (sent < 0 || length < 0 || length > toI32(output.length)) {
|
|
628
|
+
dnsClientFail(c, q, nowNanos)
|
|
629
|
+
return
|
|
630
|
+
}
|
|
631
|
+
const n: i32 = netWrite(fd, output, sent, length - sent)
|
|
508
632
|
if (n === -11) {
|
|
509
633
|
return
|
|
510
634
|
}
|
|
511
|
-
|
|
635
|
+
// A write never takes more than it is given; in i64 the sum cannot overflow.
|
|
636
|
+
const next: i64 = toI64(sent) + toI64(n)
|
|
637
|
+
if (n <= 0 || next > toI64(length)) {
|
|
512
638
|
dnsClientFail(c, q, nowNanos)
|
|
513
639
|
return
|
|
514
640
|
}
|
|
515
|
-
|
|
641
|
+
if (q < toI32(outputSent.length)) {
|
|
642
|
+
outputSent[q] = toI32(next)
|
|
643
|
+
}
|
|
516
644
|
}
|
|
517
|
-
|
|
518
|
-
if (
|
|
645
|
+
const fdState: i32[] = c.fdState
|
|
646
|
+
if (q < toI32(fdState.length)) {
|
|
647
|
+
fdState[q] = DNS_FD_TCP_READ
|
|
648
|
+
}
|
|
649
|
+
if (!dnsClientWatch(c, q, fd, 1, true)) {
|
|
519
650
|
dnsClientFail(c, q, nowNanos)
|
|
520
651
|
}
|
|
521
652
|
}
|
|
@@ -527,7 +658,15 @@ const dnsClientWrite = (c: DnsClient, q: i32, fd: i32, nowNanos: i64): void => {
|
|
|
527
658
|
* the reading, though the next one may open its socket on the same fd.
|
|
528
659
|
*/
|
|
529
660
|
const dnsClientReadStream = (c: DnsClient, q: i32, fd: i32, hungUp: boolean, nowNanos: i64): void => {
|
|
530
|
-
while (
|
|
661
|
+
while (true) {
|
|
662
|
+
const fds: i32[] = c.fds
|
|
663
|
+
const fdState: i32[] = c.fdState
|
|
664
|
+
if (q < 0 || q >= toI32(fds.length) || q >= toI32(fdState.length)) {
|
|
665
|
+
return // never taken: `ready` bounds `q` by the per-query arrays
|
|
666
|
+
}
|
|
667
|
+
if (fds[q] !== fd || fdState[q] !== DNS_FD_TCP_READ) {
|
|
668
|
+
return
|
|
669
|
+
}
|
|
531
670
|
const n: i32 = netRead(fd, c.buf, 0, DNS_UDP_MAX_REPLY)
|
|
532
671
|
if (n === -11 && !hungUp) {
|
|
533
672
|
return
|
|
@@ -640,7 +779,10 @@ export const dnsLookupWith = (
|
|
|
640
779
|
}
|
|
641
780
|
const client: DnsClient = new DnsClient(new DnsResolver(config, hosts, 1), loop, 0)
|
|
642
781
|
const begun: i64 = monotonicNanos()
|
|
643
|
-
|
|
782
|
+
// The budget and the time spent are kept apart, where `begun` plus the
|
|
783
|
+
// budget could overflow: `now >= end` is `spent >= budget`. The clock only
|
|
784
|
+
// moves on, so `spent` is its true value, which `u64` holds exactly.
|
|
785
|
+
const budget: i64 = toI64(timeoutMs) * 1000000
|
|
644
786
|
const lookup: i32 = client.start(name, port, begun)
|
|
645
787
|
if (lookup < 0) {
|
|
646
788
|
netClose(loop)
|
|
@@ -650,7 +792,8 @@ export const dnsLookupWith = (
|
|
|
650
792
|
let status: i32 = client.result(lookup, out, ttls)
|
|
651
793
|
while (status === DNS_RESOLVE_PENDING) {
|
|
652
794
|
const now: i64 = monotonicNanos()
|
|
653
|
-
|
|
795
|
+
const spent: i64 = toI64(toU64(now) - toU64(begun))
|
|
796
|
+
if (spent >= budget) {
|
|
654
797
|
if (client.settle(lookup, now)) {
|
|
655
798
|
status = client.result(lookup, out, ttls)
|
|
656
799
|
} else {
|
|
@@ -659,7 +802,9 @@ export const dnsLookupWith = (
|
|
|
659
802
|
}
|
|
660
803
|
break
|
|
661
804
|
}
|
|
662
|
-
|
|
805
|
+
// `end - now`, above 0 here, in milliseconds rounded up.
|
|
806
|
+
const left: u64 = toU64(budget) - toU64(spent)
|
|
807
|
+
const own: i32 = toI32(toI64((left - 1) / 1000000 + 1))
|
|
663
808
|
const count: i32 = pollWait(loop, ready, client.timeoutMs(now, own))
|
|
664
809
|
if (count < 0) {
|
|
665
810
|
// A signal answers 0, so a negative count is the loop failing: waiting again would spin.
|
|
@@ -668,10 +813,18 @@ export const dnsLookupWith = (
|
|
|
668
813
|
break
|
|
669
814
|
}
|
|
670
815
|
const after: i64 = monotonicNanos()
|
|
816
|
+
// `ready` holds four pairs, so `k < 4` is the old `2 * k + 1 < ready.length`.
|
|
671
817
|
let k: i32 = 0
|
|
672
|
-
while (k < count &&
|
|
673
|
-
|
|
674
|
-
|
|
818
|
+
while (k < count && k < 4) {
|
|
819
|
+
const at: i32 = 2 * k
|
|
820
|
+
const next: i32 = at + 1
|
|
821
|
+
if (at >= toI32(ready.length) || next >= toI32(ready.length)) {
|
|
822
|
+
break
|
|
823
|
+
}
|
|
824
|
+
const token: i32 = ready[at]
|
|
825
|
+
const events: i32 = ready[next]
|
|
826
|
+
if (client.owns(token)) {
|
|
827
|
+
client.ready(token, events, after)
|
|
675
828
|
}
|
|
676
829
|
k += 1
|
|
677
830
|
}
|