nish 0.35.0 → 0.37.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/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/net/dns-config.ts
CHANGED
|
@@ -9,14 +9,26 @@
|
|
|
9
9
|
* and counts what did not fit rather than growing:
|
|
10
10
|
*
|
|
11
11
|
* import { DnsConfig, DnsHosts, dnsHostsFind, dnsParseHosts, dnsParseResolvConf } from "nish/net/dns-config"
|
|
12
|
-
* import { DNS_TYPE_A } from "nish/net/dns-message"
|
|
12
|
+
* import { DNS_ADDRESS_BYTES, DNS_TYPE_A } from "nish/net/dns-message"
|
|
13
|
+
* import { readFileBytesSync } from "nish:fs"
|
|
13
14
|
*
|
|
14
15
|
* const config: DnsConfig = new DnsConfig()
|
|
15
|
-
*
|
|
16
|
+
* const resolv: u8[] | null = readFileBytesSync("/etc/resolv.conf")
|
|
17
|
+
* if (resolv !== null) {
|
|
18
|
+
* dnsParseResolvConf(resolv, 0, toI32(resolv.length), config)
|
|
19
|
+
* } else {
|
|
20
|
+
* // An empty file still gives the defaults; a fresh DnsConfig has no servers.
|
|
21
|
+
* dnsParseResolvConf(new Array<u8>(0), 0, 0, config)
|
|
22
|
+
* }
|
|
16
23
|
* // config.servers[i * 18, +18) for i < config.serverCount, on port 53
|
|
17
24
|
* const hosts: DnsHosts = new DnsHosts()
|
|
18
|
-
*
|
|
25
|
+
* const file: u8[] | null = readFileBytesSync("/etc/hosts")
|
|
26
|
+
* if (file !== null) {
|
|
27
|
+
* dnsParseHosts(file, 0, toI32(file.length), hosts)
|
|
28
|
+
* }
|
|
29
|
+
* const out: u8[] = new Array<u8>(8 * DNS_ADDRESS_BYTES)
|
|
19
30
|
* const found: i32 = dnsHostsFind(hosts, "localhost", DNS_TYPE_A, 80, out, 8)
|
|
31
|
+
* // out[0, found * 18): the A addresses /etc/hosts gives localhost, on port 80
|
|
20
32
|
*
|
|
21
33
|
* **resolv.conf** is read as glibc reads it, minus what a stub resolver does
|
|
22
34
|
* not use. `nameserver` takes one IPv4 or IPv6 literal (no zone), and the first
|
|
@@ -24,8 +36,8 @@
|
|
|
24
36
|
* so the last one wins, and it holds at most `DNS_MAX_SEARCH` (6) domains and
|
|
25
37
|
* `DNS_MAX_SEARCH_BYTES` (256) bytes; `options` reads `ndots:n` (clamped to 0
|
|
26
38
|
* to 15), `timeout:n` (1 to 30 seconds) and `attempts:n` (1 to 5) and ignores
|
|
27
|
-
* every other option. With no nameserver line
|
|
28
|
-
* ::1, as glibc's are.
|
|
39
|
+
* every other option. With no nameserver line that holds an address, the
|
|
40
|
+
* servers are 127.0.0.1 and ::1, as glibc's are.
|
|
29
41
|
*
|
|
30
42
|
* **hosts** is an address and then its names, one entry a line. At most
|
|
31
43
|
* `DNS_HOSTS_MAX_ENTRIES` entries and `DNS_HOSTS_MAX_NAMES` names are kept,
|
|
@@ -62,20 +74,20 @@ export const DNS_PORT: i32 = 53
|
|
|
62
74
|
export const DNS_DEFAULT_NDOTS: i32 = 1
|
|
63
75
|
/** The highest `ndots:n` kept; a higher one is clamped to it, as glibc clamps. */
|
|
64
76
|
export const DNS_MAX_NDOTS: i32 = 15
|
|
65
|
-
/** The `timeout` without an option, in seconds. */
|
|
77
|
+
/** The `timeout` without an option, in seconds: how long one server's reply is waited for. */
|
|
66
78
|
export const DNS_DEFAULT_TIMEOUT: i32 = 5
|
|
67
79
|
/** The highest `timeout:n` kept, in seconds; the lowest is 1. */
|
|
68
80
|
export const DNS_MAX_TIMEOUT: i32 = 30
|
|
69
|
-
/** The `attempts` without an option. */
|
|
81
|
+
/** The `attempts` without an option: rounds through the server list before a lookup gives up. */
|
|
70
82
|
export const DNS_DEFAULT_ATTEMPTS: i32 = 2
|
|
71
83
|
/** The highest `attempts:n` kept; the lowest is 1. */
|
|
72
84
|
export const DNS_MAX_ATTEMPTS: i32 = 5
|
|
73
85
|
|
|
74
|
-
/** How many hosts lines are kept. */
|
|
86
|
+
/** How many hosts lines, each an address with its names, are kept. */
|
|
75
87
|
export const DNS_HOSTS_MAX_ENTRIES: i32 = 256
|
|
76
88
|
/** How many names all hosts entries may hold together. */
|
|
77
89
|
export const DNS_HOSTS_MAX_NAMES: i32 = 1024
|
|
78
|
-
/** How many bytes those names may take together. */
|
|
90
|
+
/** How many bytes those names may take together, 16 KiB. */
|
|
79
91
|
export const DNS_HOSTS_MAX_NAME_BYTES: i32 = 16384
|
|
80
92
|
/** The longest hosts name kept, the longest a DNS name can be. */
|
|
81
93
|
export const DNS_HOSTS_MAX_NAME: i32 = 253
|
|
@@ -93,8 +105,12 @@ const DNS_SEMICOLON: i32 = 59
|
|
|
93
105
|
export class DnsConfig {
|
|
94
106
|
/** `serverCount` addresses of `DNS_ADDRESS_BYTES` each, on `DNS_PORT`. */
|
|
95
107
|
servers: u8[]
|
|
96
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* Sixteen bytes a literal is parsed into before it is kept, so a parse allocates nothing.
|
|
110
|
+
* @internal Used by `dnsResolvLine` in this module.
|
|
111
|
+
*/
|
|
97
112
|
scratch: u8[]
|
|
113
|
+
/** How many addresses `servers` holds, 1 to `DNS_MAX_SERVERS` once a file is read. */
|
|
98
114
|
serverCount: i32 = 0
|
|
99
115
|
/** Nameserver lines past `DNS_MAX_SERVERS`, read and not kept. */
|
|
100
116
|
droppedServers: i32 = 0
|
|
@@ -102,19 +118,29 @@ export class DnsConfig {
|
|
|
102
118
|
search: u8[]
|
|
103
119
|
/** Where each of the `searchCount` domains starts in `search`, and its length. */
|
|
104
120
|
searchStart: i32[]
|
|
121
|
+
/** Each search domain's length in bytes, by the same index as `searchStart`. */
|
|
105
122
|
searchLength: i32[]
|
|
123
|
+
/** How many search domains are kept, 0 to `DNS_MAX_SEARCH`. */
|
|
106
124
|
searchCount: i32 = 0
|
|
107
|
-
/**
|
|
125
|
+
/**
|
|
126
|
+
* Search domains past `DNS_MAX_SEARCH` or `DNS_MAX_SEARCH_BYTES`, or empty (a bare `.`),
|
|
127
|
+
* read and not kept.
|
|
128
|
+
*/
|
|
108
129
|
droppedSearch: i32 = 0
|
|
109
|
-
/** Dots a name needs to be tried as it stands before the search list
|
|
130
|
+
/** Dots a name needs to be tried as it stands before the search list, 0 to `DNS_MAX_NDOTS`. */
|
|
110
131
|
ndots: i32
|
|
111
|
-
/** Seconds to wait for one server's reply
|
|
132
|
+
/** Seconds to wait for one server's reply, 1 to `DNS_MAX_TIMEOUT`. */
|
|
112
133
|
timeout: i32
|
|
113
|
-
/** Rounds through the servers before giving up
|
|
134
|
+
/** Rounds through the servers before giving up, 1 to `DNS_MAX_ATTEMPTS`. */
|
|
114
135
|
attempts: i32
|
|
115
136
|
/** Lines longer than `DNS_CONFIG_MAX_LINE`, skipped whole. */
|
|
116
137
|
skippedLines: i32 = 0
|
|
117
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Makes an empty configuration with no servers or search domains, and
|
|
141
|
+
* `ndots`, `timeout` and `attempts` at resolv.conf's defaults (1, 5
|
|
142
|
+
* seconds, 2).
|
|
143
|
+
*/
|
|
118
144
|
constructor() {
|
|
119
145
|
this.servers = new Array<u8>(DNS_MAX_SERVERS * DNS_ADDRESS_BYTES)
|
|
120
146
|
this.scratch = new Array<u8>(16)
|
|
@@ -129,26 +155,50 @@ export class DnsConfig {
|
|
|
129
155
|
|
|
130
156
|
/** One hosts file, read by `dnsParseHosts`, made once and reusable. */
|
|
131
157
|
export class DnsHosts {
|
|
132
|
-
/**
|
|
158
|
+
/**
|
|
159
|
+
* `entryCount` addresses of `DNS_ADDRESS_BYTES` each, port 0.
|
|
160
|
+
* @internal Used by `dnsHostsLine` and `dnsHostsFind` in this module.
|
|
161
|
+
*/
|
|
133
162
|
addresses: u8[]
|
|
134
|
-
/**
|
|
163
|
+
/**
|
|
164
|
+
* Every kept name, back to back, lowercase, without a trailing dot.
|
|
165
|
+
* @internal Used by `dnsAddHostName` and `dnsHostNameIs` in this module.
|
|
166
|
+
*/
|
|
135
167
|
names: u8[]
|
|
136
|
-
/**
|
|
168
|
+
/**
|
|
169
|
+
* Sixteen bytes a literal is parsed into before it is kept, so a parse allocates nothing.
|
|
170
|
+
* @internal Used by `dnsHostsLine` in this module.
|
|
171
|
+
*/
|
|
137
172
|
scratch: u8[]
|
|
138
|
-
/**
|
|
173
|
+
/**
|
|
174
|
+
* Where each of the `nameCount` names starts in `names`, its length, and its entry.
|
|
175
|
+
* @internal Used by `dnsAddHostName` and `dnsHostNameIs` in this module.
|
|
176
|
+
*/
|
|
139
177
|
nameStart: i32[]
|
|
178
|
+
/** @internal Used by `dnsAddHostName` and `dnsHostNameIs` in this module. */
|
|
140
179
|
nameLength: i32[]
|
|
180
|
+
/** @internal Used by `dnsAddHostName` and `dnsHostsFind` in this module. */
|
|
141
181
|
nameEntry: i32[]
|
|
182
|
+
/** How many entries, each an address and its names, are kept, 0 to `DNS_HOSTS_MAX_ENTRIES`. */
|
|
142
183
|
entryCount: i32 = 0
|
|
184
|
+
/** How many names all the entries hold together, 0 to `DNS_HOSTS_MAX_NAMES`. */
|
|
143
185
|
nameCount: i32 = 0
|
|
186
|
+
/** How many bytes those names take together, 0 to `DNS_HOSTS_MAX_NAME_BYTES`. */
|
|
144
187
|
nameBytes: i32 = 0
|
|
145
|
-
/**
|
|
188
|
+
/**
|
|
189
|
+
* Lines with an address word and a name past the first `DNS_HOSTS_MAX_ENTRIES` entries,
|
|
190
|
+
* read and not kept; their address is not parsed.
|
|
191
|
+
*/
|
|
146
192
|
droppedEntries: i32 = 0
|
|
147
193
|
/** Names past a cap, or longer than `DNS_HOSTS_MAX_NAME`, read and not kept. */
|
|
148
194
|
droppedNames: i32 = 0
|
|
149
195
|
/** Lines longer than `DNS_CONFIG_MAX_LINE`, skipped whole. */
|
|
150
196
|
skippedLines: i32 = 0
|
|
151
197
|
|
|
198
|
+
/**
|
|
199
|
+
* Makes an empty table, sized for `DNS_HOSTS_MAX_ENTRIES` addresses and
|
|
200
|
+
* `DNS_HOSTS_MAX_NAMES` names, for `dnsParseHosts` to fill.
|
|
201
|
+
*/
|
|
152
202
|
constructor() {
|
|
153
203
|
this.addresses = new Array<u8>(DNS_HOSTS_MAX_ENTRIES * DNS_ADDRESS_BYTES)
|
|
154
204
|
this.names = new Array<u8>(DNS_HOSTS_MAX_NAME_BYTES)
|
|
@@ -302,10 +352,20 @@ const dnsParseIpv6 = (b: u8[], from: i32, to: i32, out: u8[], at: i32): boolean
|
|
|
302
352
|
* Writes the IPv4 or IPv6 literal `b[off, off + len)` at `out[at, at + 18)`,
|
|
303
353
|
* IPv4 as `::ffff:a.b.c.d`, with `port` big-endian after it, and answers
|
|
304
354
|
* whether it was one. `out` is written only when it was. A zone (`%eth0`),
|
|
305
|
-
* brackets and a port are not part of a literal
|
|
355
|
+
* brackets and a port are not part of a literal, so a window holding one is
|
|
356
|
+
* refused; so is an IPv4 part with a leading zero, never read as octal.
|
|
306
357
|
*
|
|
307
358
|
* Each call allocates the sixteen bytes it parses into, released when it
|
|
308
359
|
* returns; the two file parsers reuse a buffer of their own instead.
|
|
360
|
+
*
|
|
361
|
+
* @param b The bytes the literal is in.
|
|
362
|
+
* @param off Where the literal starts in `b`.
|
|
363
|
+
* @param len The literal's length in bytes; the whole window must be the literal, with no blank.
|
|
364
|
+
* A window outside `b` panics.
|
|
365
|
+
* @param out Where the 18-byte address goes.
|
|
366
|
+
* @param at Where in `out` it starts; `out[at, at + 18)` must lie inside `out`, or this panics.
|
|
367
|
+
* @param port The port written after the address, 0 to 65535; any other value panics.
|
|
368
|
+
* @returns True when the window was an IPv4 or IPv6 literal and `out` now holds it.
|
|
309
369
|
*/
|
|
310
370
|
export const dnsParseAddress = (b: u8[], off: i32, len: i32, out: u8[], at: i32, port: i32): boolean =>
|
|
311
371
|
dnsParseAddressVia(b, off, len, out, at, port, new Array<u8>(16))
|
|
@@ -540,7 +600,13 @@ const dnsResolvLine = (b: u8[], from: i32, to: i32, config: DnsConfig): void =>
|
|
|
540
600
|
/**
|
|
541
601
|
* Reads the resolv.conf `b[off, off + len)` into `config`, which starts over
|
|
542
602
|
* from the defaults, and gives it 127.0.0.1 and ::1 on port 53 when no
|
|
543
|
-
* nameserver line held an address.
|
|
603
|
+
* nameserver line held an address. It allocates nothing, and nothing in the
|
|
604
|
+
* bytes makes it panic or fail: what it cannot use is ignored or counted.
|
|
605
|
+
*
|
|
606
|
+
* @param b The file's bytes, read by the caller.
|
|
607
|
+
* @param off Where the file starts in `b`.
|
|
608
|
+
* @param len The file's length in bytes; a window outside `b` panics.
|
|
609
|
+
* @param config The table to fill; its counts and options are reset first, so one can be reused.
|
|
544
610
|
*/
|
|
545
611
|
export const dnsParseResolvConf = (b: u8[], off: i32, len: i32, config: DnsConfig): void => {
|
|
546
612
|
dnsConfigCheckWindow(b, off, len, "dnsParseResolvConf")
|
|
@@ -642,7 +708,16 @@ const dnsHostsLine = (b: u8[], from: i32, to: i32, hosts: DnsHosts): void => {
|
|
|
642
708
|
}
|
|
643
709
|
}
|
|
644
710
|
|
|
645
|
-
/**
|
|
711
|
+
/**
|
|
712
|
+
* Reads the hosts file `b[off, off + len)` into `hosts`, which starts over
|
|
713
|
+
* empty. It allocates nothing; a line whose address is not a literal, or
|
|
714
|
+
* that names nothing, is ignored, and what does not fit is counted.
|
|
715
|
+
*
|
|
716
|
+
* @param b The file's bytes, read by the caller.
|
|
717
|
+
* @param off Where the file starts in `b`.
|
|
718
|
+
* @param len The file's length in bytes; a window outside `b` panics.
|
|
719
|
+
* @param hosts The table to fill; it is emptied first, so one can be reused.
|
|
720
|
+
*/
|
|
646
721
|
export const dnsParseHosts = (b: u8[], off: i32, len: i32, hosts: DnsHosts): void => {
|
|
647
722
|
dnsConfigCheckWindow(b, off, len, "dnsParseHosts")
|
|
648
723
|
hosts.entryCount = 0
|
|
@@ -685,7 +760,18 @@ const dnsHostNameIs = (hosts: DnsHosts, index: i32, name: string, n: i32): boole
|
|
|
685
760
|
* Writes the addresses `hosts` gives `name`, of `qtype` (`DNS_TYPE_A` or
|
|
686
761
|
* `DNS_TYPE_AAAA`), into `out` from offset 0 with `port`, in file order and at
|
|
687
762
|
* most `max` of them, and answers how many it wrote. An entry that names
|
|
688
|
-
* `name` twice is written once
|
|
763
|
+
* `name` twice is written once; two entries with the same address are each
|
|
764
|
+
* written. It allocates nothing.
|
|
765
|
+
*
|
|
766
|
+
* @param hosts The table `dnsParseHosts` filled.
|
|
767
|
+
* @param name The name to find, without case, one trailing `.` ignored.
|
|
768
|
+
* @param qtype `DNS_TYPE_A` for the IPv4 entries or `DNS_TYPE_AAAA` for the IPv6 ones; any
|
|
769
|
+
* other type panics.
|
|
770
|
+
* @param port The port written into each address, 0 to 65535; any other value panics.
|
|
771
|
+
* @param out Where the 18-byte addresses go, back to back from offset 0.
|
|
772
|
+
* @param max The most addresses to write, 0 or more; `out` must have room for `max` of them,
|
|
773
|
+
* or this panics.
|
|
774
|
+
* @returns How many addresses were written, 0 when `hosts` does not name `name`.
|
|
689
775
|
*/
|
|
690
776
|
export const dnsHostsFind = (
|
|
691
777
|
hosts: DnsHosts,
|
package/std/net/dns-message.ts
CHANGED
|
@@ -8,9 +8,14 @@
|
|
|
8
8
|
* record five bytes long. This module reads a reply against the query it
|
|
9
9
|
* answers and refuses each of those with its own code:
|
|
10
10
|
*
|
|
11
|
-
* import {
|
|
11
|
+
* import {
|
|
12
|
+
* DnsReply, DNS_ADDRESS_BYTES, DNS_NOERROR, DNS_QUERY_MAX_BYTES, DNS_TYPE_A,
|
|
13
|
+
* dnsBuildQuery, dnsParseReply,
|
|
14
|
+
* } from "nish/net/dns-message"
|
|
12
15
|
*
|
|
13
16
|
* const query: u8[] = new Array<u8>(DNS_QUERY_MAX_BYTES)
|
|
17
|
+
* // A sketch: `id` is a fresh random 16-bit number for each query, and
|
|
18
|
+
* // `msg` a u8[] the program reads `mlen` bytes of reply into.
|
|
14
19
|
* const qlen: i32 = dnsBuildQuery(query, id, "example.com", DNS_TYPE_A) // < 0: a DNS_ERR_* code
|
|
15
20
|
* // ... send query[0, qlen), read the datagram into msg[0, mlen) ...
|
|
16
21
|
* const reply: DnsReply = new DnsReply()
|
|
@@ -145,17 +150,33 @@ export class DnsReply {
|
|
|
145
150
|
count: i32 = 0
|
|
146
151
|
/** How many addresses past `DNS_MAX_ANSWERS` were read and not kept. */
|
|
147
152
|
dropped: i32 = 0
|
|
148
|
-
/**
|
|
153
|
+
/**
|
|
154
|
+
* The reply's RCODE (0 to 15), once the reply was found to answer the query;
|
|
155
|
+
* 0 when it was refused before that.
|
|
156
|
+
*/
|
|
149
157
|
rcode: i32 = 0
|
|
150
158
|
/** The TC bit: the reply was cut to fit a datagram, and no record of it was read. */
|
|
151
159
|
truncated: boolean = false
|
|
152
|
-
/**
|
|
160
|
+
/**
|
|
161
|
+
* The last record's owner name, lowercase, in wire form.
|
|
162
|
+
* @internal Used by `dnsParseReply` in this module.
|
|
163
|
+
*/
|
|
153
164
|
owner: u8[]
|
|
154
|
-
/**
|
|
165
|
+
/**
|
|
166
|
+
* The end of the alias chain, lowercase, in wire form: the question's name until a CNAME moves it.
|
|
167
|
+
* @internal Used by `dnsParseReply` in this module; `dnsResolverAnswer` in std/net/dns-resolver.ts.
|
|
168
|
+
*/
|
|
155
169
|
chain: u8[]
|
|
156
|
-
/**
|
|
170
|
+
/**
|
|
171
|
+
* The question's name as the query asked it, lowercase, in wire form.
|
|
172
|
+
* @internal Used by `dnsParseReply` in this module; `dnsResolverAnswer` in std/net/dns-resolver.ts.
|
|
173
|
+
*/
|
|
157
174
|
asked: u8[]
|
|
158
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Makes an empty reply with every array sized for `DNS_MAX_ANSWERS` answers
|
|
178
|
+
* and `DNS_MAX_NAME_BYTES`-byte names, for `dnsParseReply` to fill.
|
|
179
|
+
*/
|
|
159
180
|
constructor() {
|
|
160
181
|
this.addresses = new Array<u8>(DNS_MAX_ANSWERS * DNS_ADDRESS_BYTES)
|
|
161
182
|
this.ttls = new Array<i32>(DNS_MAX_ANSWERS)
|
|
@@ -183,6 +204,19 @@ const dnsLower = (b: i32): i32 => (b >= 65 && b <= 90 ? b + 32 : b)
|
|
|
183
204
|
* `name` is ASCII, its labels split on `.`, and one trailing `.` is read as the
|
|
184
205
|
* root it names. `id` is the caller's: a resolver draws it at random, so that a
|
|
185
206
|
* spoofed reply must guess it, and passes the same query to `dnsParseReply`.
|
|
207
|
+
* Nothing is allocated, and nothing is written past `out.length`.
|
|
208
|
+
*
|
|
209
|
+
* @param out Where the query goes, from offset 0; `DNS_QUERY_MAX_BYTES` (271)
|
|
210
|
+
* bytes always have room for any name that is one.
|
|
211
|
+
* @param id The query ID, 0 to 0xFFFF; any other value panics.
|
|
212
|
+
* @param name The name to ask, such as `"example.com"` or `"example.com."`:
|
|
213
|
+
* bytes 33 to 126 (printable ASCII without the space), labels of 1 to 63
|
|
214
|
+
* bytes, at most 253 bytes without the trailing dot.
|
|
215
|
+
* @param qtype `DNS_TYPE_A` or `DNS_TYPE_AAAA`; any other is `DNS_ERR_QTYPE`.
|
|
216
|
+
* @returns The query's length in bytes, written to `out[0, length)`, or a
|
|
217
|
+
* negative `DNS_ERR_*` code: `DNS_ERR_QTYPE`, `DNS_ERR_EMPTY_LABEL`,
|
|
218
|
+
* `DNS_ERR_LABEL_LONG`, `DNS_ERR_NAME_LONG`, `DNS_ERR_NAME_BYTE` or
|
|
219
|
+
* `DNS_ERR_SPACE`.
|
|
186
220
|
*/
|
|
187
221
|
export const dnsBuildQuery = (out: u8[], id: i32, name: string, qtype: i32): i32 => {
|
|
188
222
|
if (id < 0 || id > 0xffff) {
|
|
@@ -393,8 +427,29 @@ const dnsKeepAddress = (msg: u8[], at: i32, size: i32, port: i32, ttl: i32, repl
|
|
|
393
427
|
* malformed or answers another. The answer records are read only under
|
|
394
428
|
* NOERROR and with TC clear; with TC set, `reply.truncated` says so,
|
|
395
429
|
* `reply.count` is 0 whatever the counts claim, and the caller asks again
|
|
396
|
-
* over TCP. The authority and
|
|
397
|
-
*
|
|
430
|
+
* over TCP. The authority and additional sections are counted against the
|
|
431
|
+
* bytes left and never read. `reply` is reset first and written in place, so
|
|
432
|
+
* nothing is allocated; it holds this reply's answer until the next call.
|
|
433
|
+
*
|
|
434
|
+
* @param msg The datagram (or TCP message, without its length) that came back.
|
|
435
|
+
* @param len How many bytes of `msg`, from offset 0, are the reply; a length
|
|
436
|
+
* outside `msg` panics.
|
|
437
|
+
* @param query The query this reply should answer, as `dnsBuildQuery` wrote
|
|
438
|
+
* it: the reply's ID and question are compared with its bytes. A query under
|
|
439
|
+
* 12 bytes, or with another ID, answers `DNS_ERR_ID`; once the reply passes
|
|
440
|
+
* the ID, QR, opcode and QDCOUNT checks, a query whose question cannot be
|
|
441
|
+
* read back panics.
|
|
442
|
+
* @param queryLen The query's length, as `dnsBuildQuery` answered it; a length
|
|
443
|
+
* outside `query` panics.
|
|
444
|
+
* @param port The port, 0 to 0xFFFF, written big-endian into the last two bytes
|
|
445
|
+
* of every kept address; any other value panics.
|
|
446
|
+
* @param reply The caller's reused `DnsReply`, reset and filled in.
|
|
447
|
+
* @returns The reply's RCODE when it answers the query (`DNS_NOERROR`,
|
|
448
|
+
* `DNS_NXDOMAIN`, `DNS_SERVFAIL` or `DNS_REFUSED`), with the addresses in
|
|
449
|
+
* `reply` only under `DNS_NOERROR` with `reply.truncated` clear; or a
|
|
450
|
+
* negative `DNS_ERR_*` code: `DNS_ERR_RCODE` for a reply to this query
|
|
451
|
+
* with any other RCODE (FORMERR, NOTIMP or one undefined), and the others
|
|
452
|
+
* for a reply that is malformed or answers another query.
|
|
398
453
|
*/
|
|
399
454
|
export const dnsParseReply = (
|
|
400
455
|
msg: u8[],
|