@amritk/nish-x86_64-linux 0.49.0 → 0.50.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 +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +233 -159
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +12 -9
- package/std/README.md +8 -5
- package/std/base64.ts +74 -1
- package/std/bytes.ts +201 -93
- package/std/compress/deflate.ts +20 -15
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/hmac.ts +1 -1
- package/std/crypto/jwt.ts +32 -19
- package/std/crypto/p256.ts +9 -12
- package/std/crypto/pki-roots.ts +7 -4
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1632 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +135 -68
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +29 -12
- package/std/json-value.ts +222 -104
- package/std/json.ts +81 -46
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +120 -38
- package/std/net/handler.ts +178 -66
- package/std/net/hpack.ts +192 -98
- package/std/net/http-compress.ts +83 -30
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +317 -20
- package/std/net/http1-client-codec.ts +137 -40
- package/std/net/http1-codec.ts +391 -202
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +12 -15
- package/std/net/http3.ts +41 -29
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +92 -54
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +66 -25
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +19 -12
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +7 -5
- package/std/net/quic.ts +36 -31
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +81 -33
- package/std/net/tls/record.ts +26 -13
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +152 -38
- package/std/net/upstream.ts +65 -19
- package/std/net/url.ts +97 -78
- package/std/net/websocket.ts +33 -15
- package/std/net/webtransport.ts +28 -12
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +158 -22
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +33 -11
- package/std/threads.ts +52 -8
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
package/std/json.ts
CHANGED
|
@@ -225,6 +225,25 @@ const jsonClosingQuote = (text: string, start: i32): i32 => {
|
|
|
225
225
|
return -1
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Where the `count` bytes from `at` end, `at + count`, when they end by
|
|
230
|
+
* `length`; `-1` when any of the three is negative or they run past it. Written
|
|
231
|
+
* as differences, each of which `src/bounds.ts` proves, so the end needs no
|
|
232
|
+
* overflow check and no cast through `u32`. A `-1` means only a span that
|
|
233
|
+
* runs past `length` (a full writer); no sum of in-range values wraps to it.
|
|
234
|
+
*/
|
|
235
|
+
const jsonSpanEnd = (length: i32, at: i32, count: i32): i32 => {
|
|
236
|
+
if (length < 0 || at < 0 || count < 0) {
|
|
237
|
+
return -1
|
|
238
|
+
}
|
|
239
|
+
const room: i32 = length - at
|
|
240
|
+
if (room < 0) {
|
|
241
|
+
return -1
|
|
242
|
+
}
|
|
243
|
+
const gap: i32 = room - count
|
|
244
|
+
return gap < 0 ? -1 : length - gap
|
|
245
|
+
}
|
|
246
|
+
|
|
228
247
|
/**
|
|
229
248
|
* The index just past the string literal whose opening quote is at `at`, or `-1`
|
|
230
249
|
* when the quote is never closed. A backslash takes the byte after it with it,
|
|
@@ -240,8 +259,10 @@ const jsonEndOfString = (text: string, at: i32): i32 => {
|
|
|
240
259
|
return -1
|
|
241
260
|
}
|
|
242
261
|
let i: i32 = at + 1
|
|
243
|
-
// The
|
|
244
|
-
|
|
262
|
+
// The first `JSON_INLINE_SCAN` bytes after the quote, or all of them when
|
|
263
|
+
// fewer are left.
|
|
264
|
+
const ahead: i32 = jsonSpanEnd(length, i, JSON_INLINE_SCAN)
|
|
265
|
+
const stop: i32 = ahead < 0 ? length : ahead
|
|
245
266
|
// `i < length` follows from `i < stop`, and is there for the bounds proof.
|
|
246
267
|
while (i < stop && i < length) {
|
|
247
268
|
const code: i32 = toI32(text.charCodeAt(i))
|
|
@@ -262,8 +283,9 @@ const jsonEndOfString = (text: string, at: i32): i32 => {
|
|
|
262
283
|
return -1
|
|
263
284
|
}
|
|
264
285
|
const quote: i32 = jsonClosingQuote(text, i)
|
|
265
|
-
//
|
|
266
|
-
|
|
286
|
+
// A quote found is an index of `text`, so the second test never holds; it
|
|
287
|
+
// is the bound one past the quote is proved by.
|
|
288
|
+
return quote < 0 || quote >= length ? -1 : quote + 1
|
|
267
289
|
}
|
|
268
290
|
|
|
269
291
|
/**
|
|
@@ -305,8 +327,9 @@ const jsonEndOfValue = (text: string, at: i32): i32 => {
|
|
|
305
327
|
const code: i32 = toI32(text.charCodeAt(i))
|
|
306
328
|
if (code === JSON_QUOTE) {
|
|
307
329
|
let j: i32 = i + 1
|
|
308
|
-
//
|
|
309
|
-
const
|
|
330
|
+
// As `jsonEndOfString` bounds its inline scan.
|
|
331
|
+
const ahead: i32 = jsonSpanEnd(length, j, JSON_INLINE_SCAN)
|
|
332
|
+
const stop: i32 = ahead < 0 ? length : ahead
|
|
310
333
|
let close: i32 = -1
|
|
311
334
|
while (j < stop && j < length) {
|
|
312
335
|
const inner: i32 = toI32(text.charCodeAt(j))
|
|
@@ -333,8 +356,12 @@ const jsonEndOfValue = (text: string, at: i32): i32 => {
|
|
|
333
356
|
return -1
|
|
334
357
|
}
|
|
335
358
|
}
|
|
336
|
-
//
|
|
337
|
-
|
|
359
|
+
// A quote found is an index of `text`, so this never returns; it is
|
|
360
|
+
// the bound one past the quote is proved by.
|
|
361
|
+
if (close >= length) {
|
|
362
|
+
return -1
|
|
363
|
+
}
|
|
364
|
+
const after: i32 = close + 1
|
|
338
365
|
if (depth === 0) {
|
|
339
366
|
return after
|
|
340
367
|
}
|
|
@@ -762,12 +789,12 @@ const jsonValueText = (object: string, member: JsonMember): string => {
|
|
|
762
789
|
if (valueAt < 0 || valueEnd > toI32(object.length)) {
|
|
763
790
|
return ""
|
|
764
791
|
}
|
|
765
|
-
if (valueAt >= valueEnd) {
|
|
792
|
+
if (valueEnd <= 0 || valueAt >= valueEnd) {
|
|
766
793
|
return ""
|
|
767
794
|
}
|
|
768
795
|
if (toI32(object.charCodeAt(valueAt)) === JSON_QUOTE) {
|
|
769
|
-
// `valueEnd >
|
|
770
|
-
return jsonUnescape(object, valueAt + 1,
|
|
796
|
+
// `valueEnd > 0`, so one before it is a position in `object`.
|
|
797
|
+
return jsonUnescape(object, valueAt + 1, valueEnd - 1)
|
|
771
798
|
}
|
|
772
799
|
return object.substring(valueAt, valueEnd)
|
|
773
800
|
}
|
|
@@ -1143,9 +1170,11 @@ export class JsonWriter {
|
|
|
1143
1170
|
* @returns the byte count, `end - start`.
|
|
1144
1171
|
*/
|
|
1145
1172
|
written(): i32 {
|
|
1146
|
-
//
|
|
1147
|
-
//
|
|
1148
|
-
|
|
1173
|
+
// `end` never falls below `start`, nor `start` below 0, so the test never
|
|
1174
|
+
// holds; it is the bound the difference is proved by.
|
|
1175
|
+
const start: i32 = this.start
|
|
1176
|
+
const end: i32 = this.end
|
|
1177
|
+
return start < 0 || end < start ? 0 : end - start
|
|
1149
1178
|
}
|
|
1150
1179
|
|
|
1151
1180
|
/**
|
|
@@ -1486,7 +1515,8 @@ export class JsonWriter {
|
|
|
1486
1515
|
}
|
|
1487
1516
|
const buf: u8[] = this.buf
|
|
1488
1517
|
const at: i32 = this.end
|
|
1489
|
-
|
|
1518
|
+
const end: i32 = jsonSpanEnd(toI32(buf.length), at, len)
|
|
1519
|
+
if (end < 0) {
|
|
1490
1520
|
this.failed = JSON_WRITE_FULL
|
|
1491
1521
|
return
|
|
1492
1522
|
}
|
|
@@ -1496,8 +1526,7 @@ export class JsonWriter {
|
|
|
1496
1526
|
this.failed = JSON_WRITE_FULL
|
|
1497
1527
|
return
|
|
1498
1528
|
}
|
|
1499
|
-
|
|
1500
|
-
this.end = toI32(toU32(at) + toU32(len))
|
|
1529
|
+
this.end = end
|
|
1501
1530
|
jsonWriterValueDone(this)
|
|
1502
1531
|
}
|
|
1503
1532
|
}
|
|
@@ -1870,8 +1899,9 @@ const jsonWriterPutBytes = (w: JsonWriter, bytes: u8[], off: i32, len: i32): boo
|
|
|
1870
1899
|
}
|
|
1871
1900
|
buf[at] = toU8(JSON_QUOTE)
|
|
1872
1901
|
at += 1
|
|
1873
|
-
//
|
|
1874
|
-
|
|
1902
|
+
// The caller checked the window lies inside `bytes`; were it not, `stop` is
|
|
1903
|
+
// -1 and nothing is read.
|
|
1904
|
+
const stop: i32 = jsonSpanEnd(toI32(bytes.length), off, len)
|
|
1875
1905
|
for (let i: i32 = off; i >= 0 && i < stop && i < toI32(bytes.length); i += 1) {
|
|
1876
1906
|
const code: i32 = toI32(bytes[i])
|
|
1877
1907
|
if (jsonIsPlainByte(code)) {
|
|
@@ -1932,16 +1962,18 @@ const jsonWriterPutRuns = (w: JsonWriter, bytes: u8[], off: i32, len: i32): bool
|
|
|
1932
1962
|
}
|
|
1933
1963
|
buf[at] = toU8(JSON_QUOTE)
|
|
1934
1964
|
at += 1
|
|
1935
|
-
//
|
|
1936
|
-
|
|
1965
|
+
// The caller checked the window lies inside `bytes`; were it not, `stop` is
|
|
1966
|
+
// -1 and nothing is read.
|
|
1967
|
+
const stop: i32 = jsonSpanEnd(toI32(bytes.length), off, len)
|
|
1937
1968
|
let i: i32 = off
|
|
1938
1969
|
while (i >= 0 && i < stop && i < toI32(bytes.length)) {
|
|
1939
1970
|
const run: i32 = skipJsonPlain(bytes, i, stop)
|
|
1940
|
-
//
|
|
1941
|
-
//
|
|
1942
|
-
const count: i32 =
|
|
1971
|
+
// `skipJsonPlain` answers an index in `[i, stop]`, so the count is the
|
|
1972
|
+
// run's length; the test is the bound the difference is proved by.
|
|
1973
|
+
const count: i32 = run > i ? run - i : 0
|
|
1943
1974
|
if (count > 0) {
|
|
1944
|
-
|
|
1975
|
+
const end: i32 = jsonSpanEnd(cap, at, count)
|
|
1976
|
+
if (end < 0) {
|
|
1945
1977
|
w.failed = JSON_WRITE_FULL
|
|
1946
1978
|
return false
|
|
1947
1979
|
}
|
|
@@ -1949,8 +1981,7 @@ const jsonWriterPutRuns = (w: JsonWriter, bytes: u8[], off: i32, len: i32): bool
|
|
|
1949
1981
|
w.failed = JSON_WRITE_FULL
|
|
1950
1982
|
return false
|
|
1951
1983
|
}
|
|
1952
|
-
|
|
1953
|
-
at = toI32(toU32(at) + toU32(count))
|
|
1984
|
+
at = end
|
|
1954
1985
|
i = run
|
|
1955
1986
|
}
|
|
1956
1987
|
if (i < 0 || i >= stop || i >= toI32(bytes.length)) {
|
|
@@ -2016,15 +2047,18 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
|
2016
2047
|
bound = bound * ten
|
|
2017
2048
|
}
|
|
2018
2049
|
}
|
|
2019
|
-
const sign:
|
|
2020
|
-
const extra:
|
|
2050
|
+
const sign: integer<0, 1> = v < 0 ? 1 : 0
|
|
2051
|
+
const extra: integer<0, 1> = suffix >= 0 ? 1 : 0
|
|
2021
2052
|
const buf: u8[] = w.buf
|
|
2022
2053
|
const cap: i32 = toI32(buf.length)
|
|
2023
2054
|
const at: i32 = w.end
|
|
2024
|
-
//
|
|
2025
|
-
// at most 12 bytes
|
|
2026
|
-
const
|
|
2027
|
-
|
|
2055
|
+
// The loop leaves `digits` from 1 to `JSON_I32_DIGITS`, so the test always
|
|
2056
|
+
// holds; it is the bound the width, at most 12 bytes, is proved by.
|
|
2057
|
+
const found: i32 = digits
|
|
2058
|
+
const count: integer<0, 10> = found >= 0 && found <= 10 ? found : 1
|
|
2059
|
+
const width: i32 = sign + count + extra
|
|
2060
|
+
const end: i32 = jsonSpanEnd(cap, at, width)
|
|
2061
|
+
if (at < 0 || at >= cap || end < 0) {
|
|
2028
2062
|
w.failed = JSON_WRITE_FULL
|
|
2029
2063
|
return false
|
|
2030
2064
|
}
|
|
@@ -2034,7 +2068,7 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
|
2034
2068
|
// The last byte of the number. The test above keeps every byte of it
|
|
2035
2069
|
// inside `buf`, so each `k` below is too, and the tests of `k` in the loop
|
|
2036
2070
|
// always hold.
|
|
2037
|
-
let k: i32 =
|
|
2071
|
+
let k: i32 = end - 1
|
|
2038
2072
|
if (extra === 1 && k >= 0 && k < toI32(buf.length)) {
|
|
2039
2073
|
buf[k] = toU8(toU32(JSON_DIGIT_ZERO) + toU32(suffix))
|
|
2040
2074
|
k -= 1
|
|
@@ -2045,7 +2079,7 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
|
2045
2079
|
rest = (rest - digit) / ten
|
|
2046
2080
|
k -= 1
|
|
2047
2081
|
}
|
|
2048
|
-
w.end =
|
|
2082
|
+
w.end = end
|
|
2049
2083
|
return true
|
|
2050
2084
|
}
|
|
2051
2085
|
|
|
@@ -2066,13 +2100,17 @@ const jsonWriterPutDigits64 = (w: JsonWriter, v: i64): boolean => {
|
|
|
2066
2100
|
bound = bound * ten
|
|
2067
2101
|
}
|
|
2068
2102
|
}
|
|
2069
|
-
const sign:
|
|
2103
|
+
const sign: integer<0, 1> = v < 0 ? 1 : 0
|
|
2070
2104
|
const buf: u8[] = w.buf
|
|
2071
2105
|
const cap: i32 = toI32(buf.length)
|
|
2072
2106
|
const at: i32 = w.end
|
|
2073
|
-
//
|
|
2074
|
-
|
|
2075
|
-
|
|
2107
|
+
// From 1 to `JSON_I64_DIGITS` digits, as `jsonWriterPutDigits` bounds them,
|
|
2108
|
+
// so at most 20 bytes, tested against `cap` as there.
|
|
2109
|
+
const found: i32 = digits
|
|
2110
|
+
const count: integer<0, 19> = found >= 0 && found <= 19 ? found : 1
|
|
2111
|
+
const width: i32 = sign + count
|
|
2112
|
+
const end: i32 = jsonSpanEnd(cap, at, width)
|
|
2113
|
+
if (at < 0 || at >= cap || end < 0) {
|
|
2076
2114
|
w.failed = JSON_WRITE_FULL
|
|
2077
2115
|
return false
|
|
2078
2116
|
}
|
|
@@ -2080,14 +2118,14 @@ const jsonWriterPutDigits64 = (w: JsonWriter, v: i64): boolean => {
|
|
|
2080
2118
|
buf[at] = toU8(JSON_MINUS)
|
|
2081
2119
|
}
|
|
2082
2120
|
// The last byte of the number, as `jsonWriterPutDigits` finds it.
|
|
2083
|
-
let k: i32 =
|
|
2121
|
+
let k: i32 = end - 1
|
|
2084
2122
|
for (let left: i32 = digits; left > 0 && k >= 0 && k < toI32(buf.length); left -= 1) {
|
|
2085
2123
|
const digit: u64 = rest % ten
|
|
2086
2124
|
buf[k] = toU8(toU32(JSON_DIGIT_ZERO) + toU32(digit))
|
|
2087
2125
|
rest = (rest - digit) / ten
|
|
2088
2126
|
k -= 1
|
|
2089
2127
|
}
|
|
2090
|
-
w.end =
|
|
2128
|
+
w.end = end
|
|
2091
2129
|
return true
|
|
2092
2130
|
}
|
|
2093
2131
|
|
|
@@ -2200,11 +2238,8 @@ const jsonPutStringWalk = (dst: u8[], at: i32, src: readonly u8[], from: i32, to
|
|
|
2200
2238
|
* writes again from `at`.
|
|
2201
2239
|
*/
|
|
2202
2240
|
export const jsonPutString = (dst: u8[], at: i32, src: readonly u8[], from: i32, len: i32): i32 => {
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
}
|
|
2206
|
-
// In u32, which cannot wrap: the window was checked to lie inside `src`.
|
|
2207
|
-
return jsonPutStringWalk(dst, at, src, from, toI32(toU32(from) + toU32(len)))
|
|
2241
|
+
const to: i32 = jsonSpanEnd(toI32(src.length), from, len)
|
|
2242
|
+
return to < 0 ? -1 : jsonPutStringWalk(dst, at, src, from, to)
|
|
2208
2243
|
}
|
|
2209
2244
|
|
|
2210
2245
|
/** The digits `${v}` spells for a finite `v`, at the cursor; all of them or, with `JSON_WRITE_FULL`, none. */
|
package/std/net/dns-config.ts
CHANGED
|
@@ -445,7 +445,14 @@ const dnsParseAddressVia = (
|
|
|
445
445
|
p += 1
|
|
446
446
|
k += 1
|
|
447
447
|
}
|
|
448
|
-
|
|
448
|
+
// Never taken (`at <= out.length - 18` was checked above): the port's window is
|
|
449
|
+
// tested again here, after the last call, because that is where `setU16BE` proves it from.
|
|
450
|
+
const portAt: i32 = at + 16
|
|
451
|
+
const portBits: u16 = toU16(port)
|
|
452
|
+
if (portAt < 0 || portAt > toI32(out.length) - 2) {
|
|
453
|
+
return false
|
|
454
|
+
}
|
|
455
|
+
setU16BE(out, portAt, portBits)
|
|
449
456
|
return true
|
|
450
457
|
}
|
|
451
458
|
|
|
@@ -736,9 +743,11 @@ const dnsFallbackServers = (servers: u8[]): void => {
|
|
|
736
743
|
servers[11] = toU8(255)
|
|
737
744
|
servers[12] = toU8(127)
|
|
738
745
|
servers[15] = toU8(1)
|
|
746
|
+
servers[16] = toU8(DNS_PORT >> 8)
|
|
747
|
+
servers[17] = toU8(DNS_PORT & 255)
|
|
739
748
|
servers[33] = toU8(1)
|
|
740
|
-
|
|
741
|
-
|
|
749
|
+
servers[34] = toU8(DNS_PORT >> 8)
|
|
750
|
+
servers[35] = toU8(DNS_PORT & 255)
|
|
742
751
|
}
|
|
743
752
|
|
|
744
753
|
/** Adds `b[from, to)` as a name of entry `entry`, lowercase, one trailing dot dropped, or counts it dropped. */
|
|
@@ -988,7 +997,11 @@ export const dnsHostsFind = (
|
|
|
988
997
|
}
|
|
989
998
|
const base: i32 = found * DNS_ADDRESS_BYTES
|
|
990
999
|
const outRoom: i32 = toI32(out.length) - DNS_ADDRESS_BYTES
|
|
991
|
-
|
|
1000
|
+
// The port's two bytes, `out[base + 16, base + 18)`, tested as the window
|
|
1001
|
+
// `setU16BE` proves from: nothing between here and that call calls anything.
|
|
1002
|
+
const portAt: i32 = base + 16
|
|
1003
|
+
const portBits: u16 = toU16(port)
|
|
1004
|
+
if (base > outRoom || portAt > toI32(out.length) - 2) {
|
|
992
1005
|
panic("dnsHostsFind: out has no room for max addresses")
|
|
993
1006
|
}
|
|
994
1007
|
let q: i32 = base
|
|
@@ -1000,7 +1013,7 @@ export const dnsHostsFind = (
|
|
|
1000
1013
|
r += 1
|
|
1001
1014
|
k += 1
|
|
1002
1015
|
}
|
|
1003
|
-
setU16BE(out,
|
|
1016
|
+
setU16BE(out, portAt, portBits)
|
|
1004
1017
|
found += 1
|
|
1005
1018
|
last = entry
|
|
1006
1019
|
}
|
package/std/net/dns-message.ts
CHANGED
|
@@ -55,7 +55,6 @@
|
|
|
55
55
|
*
|
|
56
56
|
* The record of what this module refuses and why is `docs/security/dns.md`.
|
|
57
57
|
*/
|
|
58
|
-
import { getU16BE, getU32BE, setU16BE } from "nish/bytes"
|
|
59
58
|
|
|
60
59
|
/** The record type of an IPv4 address (RFC 1035 §3.2.2). */
|
|
61
60
|
export const DNS_TYPE_A: i32 = 1
|
|
@@ -195,6 +194,14 @@ const dnsCheckLength = (bytes: u8[], len: i32, who: string): void => {
|
|
|
195
194
|
}
|
|
196
195
|
}
|
|
197
196
|
|
|
197
|
+
/**
|
|
198
|
+
* The big-endian u16 whose high byte is `hi` and low byte `lo`. The message
|
|
199
|
+
* reads index their bytes in place, where the checks before them prove them,
|
|
200
|
+
* rather than call `getU16BE`, whose window check would need a test after
|
|
201
|
+
* every call before it.
|
|
202
|
+
*/
|
|
203
|
+
const dnsBe16 = (hi: u8, lo: u8): u16 => (toU16(hi) << 8) | toU16(lo)
|
|
204
|
+
|
|
198
205
|
/** `b` with A to Z read as a to z, and every other byte as it stands. */
|
|
199
206
|
const dnsLower = (b: i32): i32 => (b >= 65 && b <= 90 ? b + 32 : b)
|
|
200
207
|
|
|
@@ -240,15 +247,23 @@ export const dnsBuildQuery = (out: u8[], id: i32, name: string, qtype: i32): i32
|
|
|
240
247
|
return DNS_ERR_NAME_LONG
|
|
241
248
|
}
|
|
242
249
|
const total: i32 = DNS_HEADER_BYTES + n + 2 + 4
|
|
243
|
-
if (total > toI32(out.length)) {
|
|
250
|
+
if (total > toI32(out.length) || toI32(out.length) < DNS_HEADER_BYTES) {
|
|
244
251
|
return DNS_ERR_SPACE
|
|
245
252
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
253
|
+
// The header's six big-endian u16s, `total` (at least 19) bytes having been
|
|
254
|
+
// checked: ID, flags (RD), QDCOUNT 1 and three zero counts.
|
|
255
|
+
out[0] = toU8(id >> 8)
|
|
256
|
+
out[1] = toU8(id & 255)
|
|
257
|
+
out[2] = toU8(DNS_FLAG_RD >> 8)
|
|
258
|
+
out[3] = toU8(DNS_FLAG_RD & 255)
|
|
259
|
+
out[4] = toU8(0)
|
|
260
|
+
out[5] = toU8(1)
|
|
261
|
+
out[6] = toU8(0)
|
|
262
|
+
out[7] = toU8(0)
|
|
263
|
+
out[8] = toU8(0)
|
|
264
|
+
out[9] = toU8(0)
|
|
265
|
+
out[10] = toU8(0)
|
|
266
|
+
out[11] = toU8(0)
|
|
252
267
|
// The bytes of the label being written so far, held at 64 once it is past
|
|
253
268
|
// `DNS_MAX_LABEL_BYTES`: its length byte goes just before them.
|
|
254
269
|
let label: i32 = 0
|
|
@@ -289,8 +304,15 @@ export const dnsBuildQuery = (out: u8[], id: i32, name: string, qtype: i32): i32
|
|
|
289
304
|
return DNS_ERR_SPACE
|
|
290
305
|
}
|
|
291
306
|
out[rootAt] = toU8(0)
|
|
292
|
-
|
|
293
|
-
|
|
307
|
+
// QTYPE then QCLASS IN, the question's last four bytes.
|
|
308
|
+
const typeAt: i32 = rootAt + 1
|
|
309
|
+
if (typeAt > toI32(out.length) - 4) {
|
|
310
|
+
return DNS_ERR_SPACE
|
|
311
|
+
}
|
|
312
|
+
out[typeAt] = toU8(qtype >> 8)
|
|
313
|
+
out[typeAt + 1] = toU8(qtype & 255)
|
|
314
|
+
out[typeAt + 2] = toU8(DNS_CLASS_IN >> 8)
|
|
315
|
+
out[typeAt + 3] = toU8(DNS_CLASS_IN & 255)
|
|
294
316
|
return total
|
|
295
317
|
}
|
|
296
318
|
|
|
@@ -479,7 +501,9 @@ const dnsKeepAddress = (msg: u8[], at: i32, size: i32, port: i32, ttl: i32, repl
|
|
|
479
501
|
addresses[to] = msg[from]
|
|
480
502
|
k += 1
|
|
481
503
|
}
|
|
482
|
-
|
|
504
|
+
// The port, big-endian, in the address's last two bytes.
|
|
505
|
+
addresses[base + 16] = toU8((port >> 8) & 255)
|
|
506
|
+
addresses[base + 17] = toU8(port & 255)
|
|
483
507
|
reply.count = count + 1
|
|
484
508
|
return true
|
|
485
509
|
}
|
|
@@ -534,25 +558,27 @@ export const dnsParseReply = (
|
|
|
534
558
|
reply.dropped = 0
|
|
535
559
|
reply.rcode = 0
|
|
536
560
|
reply.truncated = false
|
|
537
|
-
|
|
561
|
+
// `len` lies in `msg` (checked above), so the second test is never taken: it
|
|
562
|
+
// is the one the reads below index from.
|
|
563
|
+
if (len < 12 || toI32(msg.length) < 12) {
|
|
538
564
|
return DNS_ERR_HEADER
|
|
539
565
|
}
|
|
540
|
-
const flags: i32 = toI32(
|
|
566
|
+
const flags: i32 = toI32(dnsBe16(msg[2], msg[3]))
|
|
541
567
|
if ((flags & DNS_FLAG_QR) === 0) {
|
|
542
568
|
return DNS_ERR_NOT_REPLY
|
|
543
569
|
}
|
|
544
570
|
if (((flags >> 11) & 15) !== 0) {
|
|
545
571
|
return DNS_ERR_OPCODE
|
|
546
572
|
}
|
|
547
|
-
if (queryLen < 12 ||
|
|
573
|
+
if (queryLen < 12 || toI32(query.length) < 12 || dnsBe16(msg[0], msg[1]) !== dnsBe16(query[0], query[1])) {
|
|
548
574
|
return DNS_ERR_ID
|
|
549
575
|
}
|
|
550
|
-
if (
|
|
576
|
+
if (dnsBe16(msg[4], msg[5]) !== toU16(1)) {
|
|
551
577
|
return DNS_ERR_QUESTION
|
|
552
578
|
}
|
|
553
579
|
const asked: i32 = dnsReadName(query, queryLen, DNS_HEADER_BYTES, reply.asked)
|
|
554
580
|
// `DNS_QUERY_MAX_BYTES` (271) holds any query dnsBuildQuery writes.
|
|
555
|
-
if (asked < 0 || asked > queryLen - 4 || asked > 267) {
|
|
581
|
+
if (asked < 0 || asked > queryLen - 4 || asked > toI32(query.length) - 4 || asked > 267) {
|
|
556
582
|
panic("dnsParseReply: the query is not one dnsBuildQuery wrote")
|
|
557
583
|
}
|
|
558
584
|
const named: i32 = dnsReadName(msg, len, DNS_HEADER_BYTES, reply.chain)
|
|
@@ -560,17 +586,19 @@ export const dnsParseReply = (
|
|
|
560
586
|
return named
|
|
561
587
|
}
|
|
562
588
|
// dnsReadName reads no name past 65535, the longest a message can be.
|
|
563
|
-
if (named > len - 4 || named > 65535) {
|
|
589
|
+
if (named > len - 4 || named > toI32(msg.length) - 4 || named > 65535) {
|
|
564
590
|
return DNS_ERR_SHORT
|
|
565
591
|
}
|
|
566
592
|
if (
|
|
567
593
|
!dnsSameName(reply.chain, reply.asked) ||
|
|
568
|
-
|
|
569
|
-
|
|
594
|
+
msg[named] !== query[asked] ||
|
|
595
|
+
msg[named + 1] !== query[asked + 1] ||
|
|
596
|
+
msg[named + 2] !== query[asked + 2] ||
|
|
597
|
+
msg[named + 3] !== query[asked + 3]
|
|
570
598
|
) {
|
|
571
599
|
return DNS_ERR_QUESTION
|
|
572
600
|
}
|
|
573
|
-
const qtype: i32 = toI32(
|
|
601
|
+
const qtype: i32 = toI32(dnsBe16(query[asked], query[asked + 1]))
|
|
574
602
|
reply.truncated = (flags & DNS_FLAG_TC) !== 0
|
|
575
603
|
const rcode: i32 = flags & 15
|
|
576
604
|
reply.rcode = rcode
|
|
@@ -583,14 +611,14 @@ export const dnsParseReply = (
|
|
|
583
611
|
return rcode
|
|
584
612
|
}
|
|
585
613
|
let pos: i32 = named + 4
|
|
586
|
-
const
|
|
614
|
+
const answers: i32 = toI32(dnsBe16(msg[6], msg[7]))
|
|
615
|
+
const records: i32 = answers + toI32(dnsBe16(msg[8], msg[9])) + toI32(dnsBe16(msg[10], msg[11]))
|
|
587
616
|
if (records > (len - pos) / DNS_MIN_RECORD_BYTES) {
|
|
588
617
|
return DNS_ERR_COUNT
|
|
589
618
|
}
|
|
590
619
|
if (rcode !== DNS_NOERROR) {
|
|
591
620
|
return rcode
|
|
592
621
|
}
|
|
593
|
-
const answers: i32 = toI32(getU16BE(msg, 6))
|
|
594
622
|
let dropped: i32 = 0
|
|
595
623
|
let i: i32 = 0
|
|
596
624
|
while (i < answers) {
|
|
@@ -598,13 +626,18 @@ export const dnsParseReply = (
|
|
|
598
626
|
if (fixed < 0) {
|
|
599
627
|
return fixed
|
|
600
628
|
}
|
|
601
|
-
if (fixed > len - 10 || fixed > 65535) {
|
|
629
|
+
if (fixed > len - 10 || fixed > toI32(msg.length) - 10 || fixed > 65535) {
|
|
602
630
|
return DNS_ERR_SHORT
|
|
603
631
|
}
|
|
604
|
-
const type: i32 = toI32(
|
|
605
|
-
const klass: i32 = toI32(
|
|
606
|
-
const
|
|
607
|
-
|
|
632
|
+
const type: i32 = toI32(dnsBe16(msg[fixed], msg[fixed + 1]))
|
|
633
|
+
const klass: i32 = toI32(dnsBe16(msg[fixed + 2], msg[fixed + 3]))
|
|
634
|
+
const ttlBits: u32 =
|
|
635
|
+
(toU32(msg[fixed + 4]) << 24) |
|
|
636
|
+
(toU32(msg[fixed + 5]) << 16) |
|
|
637
|
+
(toU32(msg[fixed + 6]) << 8) |
|
|
638
|
+
toU32(msg[fixed + 7])
|
|
639
|
+
const rawTtl: i32 = toI32(ttlBits)
|
|
640
|
+
const size: i32 = toI32(dnsBe16(msg[fixed + 8], msg[fixed + 9]))
|
|
608
641
|
const data: i32 = fixed + 10
|
|
609
642
|
if (size > len - data) {
|
|
610
643
|
return DNS_ERR_RDATA
|
package/std/net/dns-resolver.ts
CHANGED
|
@@ -90,7 +90,6 @@ import {
|
|
|
90
90
|
dnsBuildQuery,
|
|
91
91
|
dnsParseReply,
|
|
92
92
|
} from "nish/net/dns-message"
|
|
93
|
-
import { setU16BE } from "nish/bytes"
|
|
94
93
|
import { DnsConfig, DnsHosts, dnsHostsFind, dnsParseAddress } from "nish/net/dns-config"
|
|
95
94
|
|
|
96
95
|
/** `result` for a lookup that has not finished. */
|
|
@@ -560,6 +559,12 @@ export class DnsResolver {
|
|
|
560
559
|
) {
|
|
561
560
|
return -1
|
|
562
561
|
}
|
|
562
|
+
// Every query buffer holds `DNS_QUERY_MAX_BYTES`, so this is never taken:
|
|
563
|
+
// it is the test the ID's two bytes below are written under.
|
|
564
|
+
const query: u8[] = queries[q]
|
|
565
|
+
if (toI32(query.length) < 2) {
|
|
566
|
+
return -1
|
|
567
|
+
}
|
|
563
568
|
const kind: i32 = pending[q]
|
|
564
569
|
pending[q] = 0
|
|
565
570
|
this.cursor = q + 1 < n ? q + 1 : 0
|
|
@@ -568,12 +573,13 @@ export class DnsResolver {
|
|
|
568
573
|
if (kind === DNS_CLOSE) {
|
|
569
574
|
return q
|
|
570
575
|
}
|
|
571
|
-
|
|
572
|
-
|
|
576
|
+
query[0] = toU8(id >> 8)
|
|
577
|
+
query[1] = toU8(id)
|
|
573
578
|
const length: i32 = queryLength[q]
|
|
574
579
|
const skip: i32 = kind === DNS_SEND_TCP ? 2 : 0
|
|
575
580
|
if (skip === 2) {
|
|
576
|
-
|
|
581
|
+
out[0] = toU8(length >> 8)
|
|
582
|
+
out[1] = toU8(length)
|
|
577
583
|
}
|
|
578
584
|
// A query's length is at most its buffer's, and `out` holds the longest
|
|
579
585
|
// with its two-byte prefix, so `o` ends at `skip + length`.
|
|
@@ -1033,9 +1039,11 @@ const dnsResolverLoopback = (out: u8[], port: i32): void => {
|
|
|
1033
1039
|
out[11] = toU8(255)
|
|
1034
1040
|
out[12] = toU8(127)
|
|
1035
1041
|
out[15] = toU8(1)
|
|
1036
|
-
|
|
1042
|
+
out[16] = toU8(port >> 8)
|
|
1043
|
+
out[17] = toU8(port)
|
|
1037
1044
|
out[DNS_ADDRESS_BYTES + 15] = toU8(1)
|
|
1038
|
-
|
|
1045
|
+
out[DNS_ADDRESS_BYTES + 16] = toU8(port >> 8)
|
|
1046
|
+
out[DNS_ADDRESS_BYTES + 17] = toU8(port)
|
|
1039
1047
|
}
|
|
1040
1048
|
|
|
1041
1049
|
/**
|
|
@@ -1445,17 +1453,25 @@ const dnsResolverAsk = (r: DnsResolver, q: i32, qtype: i32, nowNanos: i64): void
|
|
|
1445
1453
|
const query: u8[] = queries[q]
|
|
1446
1454
|
const name: u8[] = names[q]
|
|
1447
1455
|
const length: i32 = nameLength[q]
|
|
1448
|
-
// `255` is DNS_MAX_NAME_BYTES: every name written to `names` fits it,
|
|
1449
|
-
|
|
1456
|
+
// `255` is DNS_MAX_NAME_BYTES: every name written to `names` fits it, and
|
|
1457
|
+
// every query buffer holds `DNS_QUERY_MAX_BYTES` (`16 + 255`), so this never
|
|
1458
|
+
// returns.
|
|
1459
|
+
if (length < 0 || length > 255 || toI32(query.length) < DNS_QUERY_MAX_BYTES) {
|
|
1450
1460
|
return
|
|
1451
1461
|
}
|
|
1452
1462
|
// The bytes are dnsBuildQuery's: the ID, RD, one question, then the name, type and class IN.
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1463
|
+
query[0] = toU8(0)
|
|
1464
|
+
query[1] = toU8(0)
|
|
1465
|
+
query[2] = toU8(0x01)
|
|
1466
|
+
query[3] = toU8(0)
|
|
1467
|
+
query[4] = toU8(0)
|
|
1468
|
+
query[5] = toU8(1)
|
|
1469
|
+
query[6] = toU8(0)
|
|
1470
|
+
query[7] = toU8(0)
|
|
1471
|
+
query[8] = toU8(0)
|
|
1472
|
+
query[9] = toU8(0)
|
|
1473
|
+
query[10] = toU8(0)
|
|
1474
|
+
query[11] = toU8(0)
|
|
1459
1475
|
let i: i32 = 0
|
|
1460
1476
|
let o: i32 = 12
|
|
1461
1477
|
while (i < length && i < toI32(name.length) && o >= 0 && o < toI32(query.length)) {
|
|
@@ -1463,8 +1479,11 @@ const dnsResolverAsk = (r: DnsResolver, q: i32, qtype: i32, nowNanos: i64): void
|
|
|
1463
1479
|
i += 1
|
|
1464
1480
|
o += 1
|
|
1465
1481
|
}
|
|
1466
|
-
|
|
1467
|
-
|
|
1482
|
+
const typeAt: i32 = 12 + length
|
|
1483
|
+
query[typeAt] = toU8(qtype >> 8)
|
|
1484
|
+
query[typeAt + 1] = toU8(qtype)
|
|
1485
|
+
query[typeAt + 2] = toU8(0)
|
|
1486
|
+
query[typeAt + 3] = toU8(1)
|
|
1468
1487
|
queryLength[q] = 16 + length
|
|
1469
1488
|
// Only this name's reply may say NXDOMAIN, not an earlier candidate's or lookup's.
|
|
1470
1489
|
nxdomain[q] = false
|
package/std/net/dns.ts
CHANGED
|
@@ -781,7 +781,7 @@ export const dnsLookupWith = (
|
|
|
781
781
|
const begun: i64 = monotonicNanos()
|
|
782
782
|
// The budget and the time spent are kept apart, where `begun` plus the
|
|
783
783
|
// budget could overflow: `now >= end` is `spent >= budget`. The clock only
|
|
784
|
-
// moves on, so `spent` is its true value
|
|
784
|
+
// moves on, so `spent` is its true value.
|
|
785
785
|
const budget: i64 = toI64(timeoutMs) * 1000000
|
|
786
786
|
const lookup: i32 = client.start(name, port, begun)
|
|
787
787
|
if (lookup < 0) {
|
|
@@ -792,7 +792,8 @@ export const dnsLookupWith = (
|
|
|
792
792
|
let status: i32 = client.result(lookup, out, ttls)
|
|
793
793
|
while (status === DNS_RESOLVE_PENDING) {
|
|
794
794
|
const now: i64 = monotonicNanos()
|
|
795
|
-
|
|
795
|
+
// The clock counts up from boot; a reading behind `begun` has spent none.
|
|
796
|
+
const spent: i64 = begun >= 0 && now >= 0 && now >= begun ? now - begun : 0
|
|
796
797
|
if (spent >= budget) {
|
|
797
798
|
if (client.settle(lookup, now)) {
|
|
798
799
|
status = client.result(lookup, out, ttls)
|