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.ts
CHANGED
|
@@ -3,18 +3,68 @@
|
|
|
3
3
|
* `nish:net` sockets on the caller's own readiness loop, and `resolveHost`,
|
|
4
4
|
* which runs a loop of its own for a program that has none.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* import { DnsClient, resolveHost } from "nish/net/dns"
|
|
7
|
+
* import { DNS_RESOLVE_MAX_ADDRESSES, DNS_RESOLVE_PENDING, DnsResolver } from "nish/net/dns-resolver"
|
|
8
|
+
* import { DnsConfig, dnsParseResolvConf } from "nish/net/dns-config"
|
|
9
|
+
* import { DNS_ADDRESS_BYTES } from "nish/net/dns-message"
|
|
10
|
+
* import { readFileBytesSync } from "nish:fs"
|
|
11
|
+
* import { pollCreate, pollWait } from "nish:net"
|
|
12
|
+
* import { monotonicNanos } from "nish:process"
|
|
11
13
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
14
|
+
* const out: u8[] = new Array<u8>(DNS_RESOLVE_MAX_ADDRESSES * DNS_ADDRESS_BYTES)
|
|
15
|
+
* const ttls: i32[] = new Array<i32>(DNS_RESOLVE_MAX_ADDRESSES)
|
|
16
|
+
*
|
|
17
|
+
* // A command-line tool: block for at most two seconds.
|
|
18
|
+
* const count: i32 = resolveHost("example.com", 2000, 443, out, ttls)
|
|
19
|
+
*
|
|
20
|
+
* // A server: the lookup rides the loop it already runs.
|
|
21
|
+
* const config: DnsConfig = new DnsConfig()
|
|
22
|
+
* const resolv: u8[] = readFileBytesSync("/etc/resolv.conf") ?? new Array<u8>(0)
|
|
23
|
+
* dnsParseResolvConf(resolv, 0, toI32(resolv.length), config)
|
|
24
|
+
* const loop: i32 = pollCreate()
|
|
25
|
+
* const client: DnsClient = new DnsClient(new DnsResolver(config, null, 4), loop, 1000)
|
|
26
|
+
* const lookup: i32 = client.start("example.com", 443, monotonicNanos())
|
|
27
|
+
* if (lookup >= 0) {
|
|
28
|
+
* const ready: i32[] = new Array<i32>(16)
|
|
29
|
+
* let status: i32 = client.result(lookup, out, ttls)
|
|
30
|
+
* while (status === DNS_RESOLVE_PENDING) {
|
|
31
|
+
* const n: i32 = pollWait(loop, ready, client.timeoutMs(monotonicNanos(), -1))
|
|
32
|
+
* const now: i64 = monotonicNanos()
|
|
33
|
+
* for (let k: i32 = 0; k < n; k++) {
|
|
34
|
+
* if (client.owns(ready[2 * k])) {
|
|
35
|
+
* client.ready(ready[2 * k], ready[2 * k + 1], now)
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* client.tick(now)
|
|
39
|
+
* status = client.result(lookup, out, ttls)
|
|
40
|
+
* }
|
|
41
|
+
* // status: the address count, 18 bytes each in out, or a DNS_RESOLVE_* code
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* **`DnsClient` is for a program with a loop.** It opens a fresh UDP socket
|
|
45
|
+
* on a kernel-chosen port for every UDP try and a TCP connection for every
|
|
46
|
+
* TCP one, watches each under a token of the range it was given, and closes
|
|
47
|
+
* it when the try ends. It never waits, never reads the clock and never
|
|
48
|
+
* reads a file: the caller's loop owns all three, so a lookup costs a server
|
|
49
|
+
* nothing while it waits. Each send's query ID is drawn from
|
|
50
|
+
* `crypto.getRandomValues`, and every buffer it uses is made in its
|
|
51
|
+
* constructor.
|
|
52
|
+
*
|
|
53
|
+
* **`resolveHost` and `dnsLookupWith` block the thread**, on a poll loop of
|
|
54
|
+
* their own, so they are for a command-line tool or a program's start-up.
|
|
55
|
+
*
|
|
56
|
+
* **The caller owns `out` and `ttls`.** Both need room for
|
|
57
|
+
* `DNS_RESOLVE_MAX_ADDRESSES` entries (18 bytes each in `out`), or the call
|
|
58
|
+
* that reads the answer panics; they are written only when it answers a
|
|
59
|
+
* count. Reading a finished lookup's answer frees its slot, so read it once.
|
|
60
|
+
*
|
|
61
|
+
* **Errors are negative numbers.** `start` answers `DNS_RESOLVE_BUSY` when
|
|
62
|
+
* every slot is in use or the `DNS_ERR_*` code of a name that is not one; a
|
|
63
|
+
* finished lookup answers `DNS_RESOLVE_NOT_FOUND`, `DNS_RESOLVE_NO_ANSWER` or
|
|
64
|
+
* `DNS_RESOLVE_CNAME`. A socket that cannot be opened, written or connected
|
|
65
|
+
* fails that one try, counted in `socketFailures`, and the query moves to
|
|
66
|
+
* the next server. A token outside the client's range, a negative token base
|
|
67
|
+
* and an `out` or `ttls` that is too short are bugs in the caller and panic.
|
|
18
68
|
*
|
|
19
69
|
* Private helpers share the importing program's flat symbol namespace
|
|
20
70
|
* (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
|
|
@@ -48,7 +98,10 @@ import {
|
|
|
48
98
|
import { readFileBytesSync } from "nish:fs"
|
|
49
99
|
import { monotonicNanos } from "nish:process"
|
|
50
100
|
|
|
51
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* The longest datagram a UDP try reads, in bytes; a longer one is cut, fails
|
|
103
|
+
* to parse and is dropped. A TCP try reads its stream in chunks of this size.
|
|
104
|
+
*/
|
|
52
105
|
export const DNS_UDP_MAX_REPLY: i32 = 4096
|
|
53
106
|
|
|
54
107
|
/**
|
|
@@ -72,25 +125,46 @@ const DNS_FD_TCP_READ: i32 = 3
|
|
|
72
125
|
export class DnsClient {
|
|
73
126
|
/** The core; `start`, `result` and `cancel` go through the client so its sockets follow. */
|
|
74
127
|
resolver: DnsResolver
|
|
75
|
-
/**
|
|
128
|
+
/**
|
|
129
|
+
* Each query's socket, or -1.
|
|
130
|
+
* @internal Used by `dnsClientClose`, `dnsClientSendUdp`, `dnsClientOpenTcp`, `dnsClientReadDatagrams` and `dnsClientReadStream` in this module.
|
|
131
|
+
*/
|
|
76
132
|
fds: i32[]
|
|
77
|
-
/**
|
|
133
|
+
/**
|
|
134
|
+
* What each query's socket is doing, a `DNS_FD_*`.
|
|
135
|
+
* @internal Used by `dnsClientClose`, `dnsClientSendUdp`, `dnsClientOpenTcp`, `dnsClientWrite` and `dnsClientReadStream` in this module.
|
|
136
|
+
*/
|
|
78
137
|
fdState: i32[]
|
|
79
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Each query's TCP bytes, length prefix first, kept until they are all written.
|
|
140
|
+
* @internal Used by `dnsClientOpenTcp` and `dnsClientWrite` in this module.
|
|
141
|
+
*/
|
|
80
142
|
outputs: u8[][]
|
|
143
|
+
/** @internal Used by `dnsClientOpenTcp` and `dnsClientWrite` in this module. */
|
|
81
144
|
outputLength: i32[]
|
|
145
|
+
/** @internal Used by `dnsClientOpenTcp` and `dnsClientWrite` in this module. */
|
|
82
146
|
outputSent: i32[]
|
|
83
|
-
/**
|
|
147
|
+
/**
|
|
148
|
+
* Scratch for `nextOutgoing`, a read, a sender's address and the ID.
|
|
149
|
+
* @internal Used by `dnsClientSendUdp` and `dnsClientOpenTcp` in this module.
|
|
150
|
+
*/
|
|
84
151
|
out: u8[]
|
|
152
|
+
/** @internal Used by `dnsClientSendUdp` and `dnsClientOpenTcp` in this module. */
|
|
85
153
|
to: u8[]
|
|
154
|
+
/** @internal Used by `dnsClientSendUdp` and `dnsClientOpenTcp` in this module. */
|
|
86
155
|
meta: i32[]
|
|
156
|
+
/** @internal Used by `dnsClientReadDatagrams` and `dnsClientReadStream` in this module. */
|
|
87
157
|
buf: u8[]
|
|
158
|
+
/** @internal Used by `dnsClientReadDatagrams` in this module. */
|
|
88
159
|
from: u8[]
|
|
160
|
+
/** @internal Used by `dnsClientReadDatagrams` in this module. */
|
|
89
161
|
recvMeta: i32[]
|
|
90
|
-
idBytes: u8[]
|
|
162
|
+
private idBytes: u8[]
|
|
91
163
|
/** Whether `idBytes` holds an ID no send has used yet. */
|
|
92
|
-
idFresh: boolean = false
|
|
164
|
+
private idFresh: boolean = false
|
|
165
|
+
/** @internal Used by `dnsClientSendUdp`, `dnsClientOpenTcp` and `dnsClientWrite` in this module. */
|
|
93
166
|
loop: i32 = 0
|
|
167
|
+
/** The first of the client's tokens; its sockets use `tokenBase` to `tokenBase + tokens() - 1`. */
|
|
94
168
|
tokenBase: i32 = 0
|
|
95
169
|
/** Sockets that could not be opened, written or connected, each a failed try. */
|
|
96
170
|
socketFailures: i32 = 0
|
|
@@ -99,7 +173,14 @@ export class DnsClient {
|
|
|
99
173
|
|
|
100
174
|
/**
|
|
101
175
|
* A client for `resolver` whose sockets join the loop `loop` under tokens
|
|
102
|
-
* from `tokenBase`.
|
|
176
|
+
* 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
|
+
*
|
|
179
|
+
* @param resolver The core the client drives; the client owns it from now on, so go through
|
|
180
|
+
* the client's `start`, `result` and `cancel` rather than the resolver's.
|
|
181
|
+
* @param loop The `pollCreate` loop the client's sockets join; the caller keeps it and closes it.
|
|
182
|
+
* @param tokenBase The first of `tokens()` consecutive tokens the client watches its sockets
|
|
183
|
+
* under; pick a range none of the caller's own sockets use.
|
|
103
184
|
*/
|
|
104
185
|
constructor(resolver: DnsResolver, loop: i32, tokenBase: i32) {
|
|
105
186
|
if (tokenBase < 0) {
|
|
@@ -129,29 +210,70 @@ export class DnsClient {
|
|
|
129
210
|
this.idBytes = new Array<u8>(2)
|
|
130
211
|
}
|
|
131
212
|
|
|
132
|
-
/**
|
|
213
|
+
/**
|
|
214
|
+
* How many tokens, from `tokenBase`, the client's sockets use: two per
|
|
215
|
+
* lookup slot, one for the A query and one for the AAAA query.
|
|
216
|
+
*
|
|
217
|
+
* @returns The width of the client's token range.
|
|
218
|
+
*/
|
|
133
219
|
tokens(): i32 {
|
|
134
220
|
return this.resolver.maxLookups * 2
|
|
135
221
|
}
|
|
136
222
|
|
|
137
|
-
/**
|
|
223
|
+
/**
|
|
224
|
+
* Whether `token` is one of the client's, so the caller knows which ready
|
|
225
|
+
* pairs from `pollWait` to hand to `ready`.
|
|
226
|
+
*
|
|
227
|
+
* @param token A token `pollWait` reported.
|
|
228
|
+
* @returns True when `token` lies in `tokenBase` to `tokenBase + tokens() - 1`.
|
|
229
|
+
*/
|
|
138
230
|
owns(token: i32): boolean {
|
|
139
231
|
return token >= this.tokenBase && token - this.tokenBase < this.tokens()
|
|
140
232
|
}
|
|
141
233
|
|
|
142
|
-
/**
|
|
234
|
+
/**
|
|
235
|
+
* `DnsResolver.start`, with its first packets sent. `localhost`, an address
|
|
236
|
+
* literal and a hosts entry finish at once, so `result` has their answer
|
|
237
|
+
* straight away.
|
|
238
|
+
*
|
|
239
|
+
* @param name The name to look up, as the search list and `ndots` will read it.
|
|
240
|
+
* @param port The port, 0 to 65535, written into every address the lookup answers; any other
|
|
241
|
+
* value panics.
|
|
242
|
+
* @param nowNanos The caller's monotonic clock now, in nanoseconds.
|
|
243
|
+
* @returns The lookup's id, 0 or more, to pass to `result` and `cancel`; or `DNS_RESOLVE_BUSY`
|
|
244
|
+
* when every slot is in use, or the negative `DNS_ERR_*` code of a name that is not one.
|
|
245
|
+
*/
|
|
143
246
|
start(name: string, port: i32, nowNanos: i64): i32 {
|
|
144
247
|
const lookup: i32 = this.resolver.start(name, port, nowNanos)
|
|
145
248
|
this.pump(nowNanos)
|
|
146
249
|
return lookup
|
|
147
250
|
}
|
|
148
251
|
|
|
149
|
-
/**
|
|
252
|
+
/**
|
|
253
|
+
* `DnsResolver.result`; a finished lookup's sockets are already closed.
|
|
254
|
+
* Answering a finished lookup frees its slot, so a second call for it, or
|
|
255
|
+
* one for an id `start` did not give out, panics.
|
|
256
|
+
*
|
|
257
|
+
* @param lookup An id `start` answered.
|
|
258
|
+
* @param out Where the addresses go, 18 bytes each from offset 0, A first, with the lookup's
|
|
259
|
+
* port; it needs room for `DNS_RESOLVE_MAX_ADDRESSES`, or this panics.
|
|
260
|
+
* @param ttls Where each address's TTL goes, in seconds; room for
|
|
261
|
+
* `DNS_RESOLVE_MAX_ADDRESSES`, or this panics.
|
|
262
|
+
* @returns `DNS_RESOLVE_PENDING` while the lookup runs; once it has finished, the count of
|
|
263
|
+
* addresses written, or a negative `DNS_RESOLVE_*` code.
|
|
264
|
+
*/
|
|
150
265
|
result(lookup: i32, out: u8[], ttls: i32[]): i32 {
|
|
151
266
|
return this.resolver.result(lookup, out, ttls)
|
|
152
267
|
}
|
|
153
268
|
|
|
154
|
-
/**
|
|
269
|
+
/**
|
|
270
|
+
* `DnsResolver.cancel`, closing the lookup's sockets now and freeing its
|
|
271
|
+
* slot, whether or not it had finished. Panics for an id that is not a
|
|
272
|
+
* lookup in progress, as `result` does.
|
|
273
|
+
*
|
|
274
|
+
* @param lookup An id `start` answered whose answer has not been read.
|
|
275
|
+
* @param nowNanos The caller's monotonic clock now, in nanoseconds.
|
|
276
|
+
*/
|
|
155
277
|
cancel(lookup: i32, nowNanos: i64): void {
|
|
156
278
|
this.resolver.cancel(lookup)
|
|
157
279
|
this.pump(nowNanos)
|
|
@@ -162,6 +284,12 @@ export class DnsClient {
|
|
|
162
284
|
* token `owns` answers true for: reads the datagrams (at most
|
|
163
285
|
* `DNS_CLIENT_MAX_DATAGRAMS`) or stream bytes that are waiting, finishes a
|
|
164
286
|
* connection or a write, and sends whatever the core asks next. Panics for a token that is not the client's.
|
|
287
|
+
* A token whose try has already ended is ignored.
|
|
288
|
+
*
|
|
289
|
+
* @param token The ready token, one `owns` answers true for.
|
|
290
|
+
* @param events The events `pollWait` reported with it: 1 readable, 2 writable, with 4 added
|
|
291
|
+
* for a hang-up or an error.
|
|
292
|
+
* @param nowNanos The caller's monotonic clock now, in nanoseconds.
|
|
165
293
|
*/
|
|
166
294
|
ready(token: i32, events: i32, nowNanos: i64): void {
|
|
167
295
|
if (!this.owns(token)) {
|
|
@@ -183,7 +311,12 @@ export class DnsClient {
|
|
|
183
311
|
this.pump(nowNanos)
|
|
184
312
|
}
|
|
185
313
|
|
|
186
|
-
/**
|
|
314
|
+
/**
|
|
315
|
+
* `DnsResolver.tick`, then whatever it asks to send: every try past its
|
|
316
|
+
* deadline moves to the next server. Call it after each wake of the loop.
|
|
317
|
+
*
|
|
318
|
+
* @param nowNanos The caller's monotonic clock now, in nanoseconds.
|
|
319
|
+
*/
|
|
187
320
|
tick(nowNanos: i64): void {
|
|
188
321
|
this.resolver.tick(nowNanos)
|
|
189
322
|
this.pump(nowNanos)
|
|
@@ -193,6 +326,12 @@ export class DnsClient {
|
|
|
193
326
|
* The `pollWait` timeout for a caller who would otherwise wait `ownMs`
|
|
194
327
|
* (negative for ever): the smaller of it and the milliseconds, rounded up,
|
|
195
328
|
* to the core's next deadline after `nowNanos`.
|
|
329
|
+
*
|
|
330
|
+
* @param nowNanos The caller's monotonic clock now, in nanoseconds.
|
|
331
|
+
* @param ownMs How long the caller would wait for its own sake, in milliseconds, or negative
|
|
332
|
+
* to wait for ever.
|
|
333
|
+
* @returns The milliseconds to pass to `pollWait`: 0 when a deadline has already passed,
|
|
334
|
+
* `ownMs` itself when the client has no try in flight.
|
|
196
335
|
*/
|
|
197
336
|
timeoutMs(nowNanos: i64, ownMs: i32): i32 {
|
|
198
337
|
const deadline: i64 = this.resolver.nextDeadlineNanos()
|
|
@@ -209,7 +348,10 @@ export class DnsClient {
|
|
|
209
348
|
return toI32(wait)
|
|
210
349
|
}
|
|
211
350
|
|
|
212
|
-
/**
|
|
351
|
+
/**
|
|
352
|
+
* Carries out everything the core has queued: opens, sends and closes sockets.
|
|
353
|
+
* @internal Used by `dnsClientReadDatagrams` and `dnsClientReadStream` in this module.
|
|
354
|
+
*/
|
|
213
355
|
pump(nowNanos: i64): void {
|
|
214
356
|
let q: i32 = this.nextOutgoing()
|
|
215
357
|
while (q >= 0 && q < toI32(this.fds.length)) {
|
|
@@ -229,7 +371,7 @@ export class DnsClient {
|
|
|
229
371
|
* drawn only after the last went out, so an empty call or a close costs no
|
|
230
372
|
* `getRandomValues`.
|
|
231
373
|
*/
|
|
232
|
-
nextOutgoing(): i32 {
|
|
374
|
+
private nextOutgoing(): i32 {
|
|
233
375
|
if (!this.idFresh) {
|
|
234
376
|
crypto.getRandomValues(this.idBytes)
|
|
235
377
|
this.idFresh = true
|
|
@@ -385,17 +527,30 @@ const dnsClientReadStream = (c: DnsClient, q: i32, fd: i32, hungUp: boolean, now
|
|
|
385
527
|
|
|
386
528
|
/**
|
|
387
529
|
* The addresses of `name`, looked up as `/etc/resolv.conf` and `/etc/hosts`
|
|
388
|
-
* say (
|
|
389
|
-
* `out` and its TTL in seconds into
|
|
390
|
-
* `
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
* not returned on its own once `timeoutMs` passes: it is `DNS_RESOLVE_TIMEOUT`.
|
|
530
|
+
* say (a missing resolv.conf means the defaults, a missing hosts file no
|
|
531
|
+
* entries), each 18 bytes with `port` into `out` and its TTL in seconds into
|
|
532
|
+
* `ttls`. It waits for both the A and the AAAA answer, so a family that
|
|
533
|
+
* answered is not returned on its own once `timeoutMs` passes: it is
|
|
534
|
+
* `DNS_RESOLVE_TIMEOUT`.
|
|
394
535
|
*
|
|
395
536
|
* **It blocks the thread** until the lookup ends, on a loop of its own, so
|
|
396
537
|
* it is for a CLI or a program's start-up; a server resolves with a
|
|
397
538
|
* `DnsClient` on its own loop. `out` and `ttls` need room for
|
|
398
|
-
* `DNS_RESOLVE_MAX_ADDRESSES`, or it panics.
|
|
539
|
+
* `DNS_RESOLVE_MAX_ADDRESSES`, or it panics. Each call reads both files and
|
|
540
|
+
* makes its own configuration, resolver and loop, closing the loop before
|
|
541
|
+
* it returns.
|
|
542
|
+
*
|
|
543
|
+
* @param name The name to look up; `localhost`, an address literal and a hosts entry answer
|
|
544
|
+
* without a packet, even with a `timeoutMs` of 0.
|
|
545
|
+
* @param timeoutMs The most the whole lookup may take, in milliseconds, on the monotonic clock.
|
|
546
|
+
* @param port The port, 0 to 65535, written into every address; any other value panics.
|
|
547
|
+
* @param out Where the addresses go, from offset 0, A first; at least
|
|
548
|
+
* `DNS_RESOLVE_MAX_ADDRESSES * 18` bytes.
|
|
549
|
+
* @param ttls Where each address's TTL goes, in seconds; at least `DNS_RESOLVE_MAX_ADDRESSES`.
|
|
550
|
+
* @returns The count of addresses written; or a negative code: `DNS_RESOLVE_TIMEOUT` once
|
|
551
|
+
* `timeoutMs` has passed, `DNS_RESOLVE_NOT_FOUND`, `DNS_RESOLVE_NO_ANSWER` or
|
|
552
|
+
* `DNS_RESOLVE_CNAME` from the lookup, the `DNS_ERR_*` code of a name that is not one, or
|
|
553
|
+
* the negative errno of a poll loop that cannot be made or waited on.
|
|
399
554
|
*/
|
|
400
555
|
export const resolveHost = (name: string, timeoutMs: i32, port: i32, out: u8[], ttls: i32[]): i32 => {
|
|
401
556
|
const config: DnsConfig = new DnsConfig()
|
|
@@ -416,7 +571,20 @@ export const resolveHost = (name: string, timeoutMs: i32, port: i32, out: u8[],
|
|
|
416
571
|
/**
|
|
417
572
|
* `resolveHost` with the configuration given rather than read: one lookup
|
|
418
573
|
* of `name` on a loop of its own, for at most `timeoutMs`. Blocks as
|
|
419
|
-
* `resolveHost` does.
|
|
574
|
+
* `resolveHost` does, and reads no file. `config` and `hosts` are only read,
|
|
575
|
+
* so one pair can serve many calls.
|
|
576
|
+
*
|
|
577
|
+
* @param config The resolv.conf to use, as `dnsParseResolvConf` filled it.
|
|
578
|
+
* @param hosts The hosts file to consult first, as `dnsParseHosts` filled it, or null for none.
|
|
579
|
+
* @param name The name to look up.
|
|
580
|
+
* @param timeoutMs The most the whole lookup may take, in milliseconds, on the monotonic clock.
|
|
581
|
+
* @param port The port, 0 to 65535, written into every address; any other value panics.
|
|
582
|
+
* @param out Where the addresses go, from offset 0, A first; at least
|
|
583
|
+
* `DNS_RESOLVE_MAX_ADDRESSES * 18` bytes, or this panics.
|
|
584
|
+
* @param ttls Where each address's TTL goes, in seconds; at least `DNS_RESOLVE_MAX_ADDRESSES`,
|
|
585
|
+
* or this panics.
|
|
586
|
+
* @returns What `resolveHost` answers: the address count, or a negative `DNS_RESOLVE_*`,
|
|
587
|
+
* `DNS_ERR_*` or errno code.
|
|
420
588
|
*/
|
|
421
589
|
export const dnsLookupWith = (
|
|
422
590
|
config: DnsConfig,
|