rwebtransport 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/Cargo.lock +153 -226
  2. package/Cargo.toml +5 -5
  3. package/README.md +4 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +1 -1
  6. package/crates/native/src/config.rs +13 -1
  7. package/crates/native/src/driver.rs +249 -91
  8. package/crates/native/src/lib.rs +9 -10
  9. package/crates/native/src/parser_memory.rs +1 -0
  10. package/crates/native/src/server.rs +197 -182
  11. package/crates/native/src/session.rs +329 -177
  12. package/crates/native/src/test_support.rs +2 -2
  13. package/crates/wtcore/src/h3.rs +166 -10
  14. package/dist/index.js +92 -45
  15. package/dist/index.js.map +2 -2
  16. package/dist/index.mjs +92 -45
  17. package/dist/index.mjs.map +2 -2
  18. package/dist/native.d.ts +9 -4
  19. package/package.json +14 -11
  20. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  21. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  23. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  24. package/scripts/build-linux-prebuilt.sh +46 -0
  25. package/scripts/build.js +10 -3
  26. package/vendor/quiche/AGENTS.md +10 -0
  27. package/vendor/quiche/Cargo.lock +239 -248
  28. package/vendor/quiche/Cargo.toml +23 -10
  29. package/vendor/quiche/examples/Makefile +4 -4
  30. package/vendor/quiche/examples/client.rs +10 -28
  31. package/vendor/quiche/examples/http3-client.rs +20 -34
  32. package/vendor/quiche/examples/http3-server.rs +47 -78
  33. package/vendor/quiche/examples/server.rs +19 -49
  34. package/vendor/quiche/include/quiche.h +35 -4
  35. package/vendor/quiche/src/build.rs +17 -0
  36. package/vendor/quiche/src/cid.rs +129 -61
  37. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  38. package/vendor/quiche/src/crypto/mod.rs +64 -77
  39. package/vendor/quiche/src/dgram.rs +2 -3
  40. package/vendor/quiche/src/error.rs +22 -20
  41. package/vendor/quiche/src/ffi.rs +558 -280
  42. package/vendor/quiche/src/flowcontrol.rs +0 -7
  43. package/vendor/quiche/src/frame.rs +134 -135
  44. package/vendor/quiche/src/h3/ffi.rs +73 -60
  45. package/vendor/quiche/src/h3/frame.rs +70 -90
  46. package/vendor/quiche/src/h3/mod.rs +1024 -593
  47. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  48. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  49. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  50. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  51. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  52. package/vendor/quiche/src/h3/stream.rs +1009 -194
  53. package/vendor/quiche/src/lib.rs +1346 -1195
  54. package/vendor/quiche/src/minmax.rs +2 -6
  55. package/vendor/quiche/src/packet.rs +420 -552
  56. package/vendor/quiche/src/path.rs +202 -72
  57. package/vendor/quiche/src/pmtud.rs +18 -16
  58. package/vendor/quiche/src/ranges.rs +184 -82
  59. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  60. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  61. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  62. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  63. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  64. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  65. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  66. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  67. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  68. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  69. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  70. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  71. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  72. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  73. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  74. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  84. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  85. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  86. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  87. package/vendor/quiche/src/recovery/mod.rs +414 -173
  88. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  89. package/vendor/quiche/src/stream/mod.rs +864 -133
  90. package/vendor/quiche/src/stream/recv_buf.rs +471 -172
  91. package/vendor/quiche/src/stream/send_buf.rs +338 -81
  92. package/vendor/quiche/src/test_utils.rs +78 -64
  93. package/vendor/quiche/src/tests.rs +2559 -1335
  94. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  95. package/vendor/quiche/src/tls/mod.rs +146 -193
  96. package/vendor/quiche/src/transport_params.rs +87 -103
package/Cargo.toml CHANGED
@@ -13,13 +13,13 @@ repository = "https://github.com/felixantoinebelleau/rwebtransport"
13
13
  [workspace.dependencies]
14
14
  wtcore = { package = "rwebtransport-wtcore", path = "crates/wtcore" }
15
15
  quiche = { path = "vendor/quiche", default-features = false, features = ["boringssl-boring-crate"] }
16
- boring = "4.22"
17
- boring-sys = "4.22"
18
- octets = { version = "0.3.5", features = ["huffman_hpack"] }
16
+ boring = "5.6"
17
+ boring-sys = "5.6"
18
+ octets = { version = "0.3.7", features = ["huffman_hpack"] }
19
19
  log = "0.4"
20
20
  mio = { version = "1", features = ["net", "os-poll"] }
21
- getrandom = "0.2"
22
- socket2 = { version = "0.5", features = ["all"] }
21
+ getrandom = "0.4"
22
+ socket2 = { version = "0.6", features = ["all"] }
23
23
 
24
24
  # Release profile tuned for a shippable native addon without pathological compile times.
25
25
  # NOTE: panic must stay "unwind" (the default) so neon can catch Rust panics at the
package/README.md CHANGED
@@ -18,7 +18,7 @@
18
18
  [![ci](https://github.com/dacely-cloud/rwebtransport/actions/workflows/ci.yml/badge.svg)](https://github.com/dacely-cloud/rwebtransport/actions/workflows/ci.yml)
19
19
  [![node](https://img.shields.io/badge/node-24%20%7C%2026-22e3ab.svg?labelColor=0e1520)](#requirements)
20
20
  [![types](https://img.shields.io/badge/types-included-2563ff.svg?labelColor=0e1520)](https://www.typescriptlang.org/)
21
- [![quic](https://img.shields.io/badge/QUIC-quiche%200.29-7c3aed.svg?labelColor=0e1520)](https://github.com/cloudflare/quiche)
21
+ [![quic](https://img.shields.io/badge/QUIC-quiche%200.30-7c3aed.svg?labelColor=0e1520)](https://github.com/cloudflare/quiche)
22
22
  [![tls](https://img.shields.io/badge/TLS-BoringSSL-654ff0.svg?labelColor=0e1520)](https://boringssl.googlesource.com/boringssl/)
23
23
  [![license](https://img.shields.io/badge/license-Apache--2.0-8b9ab4.svg?labelColor=0e1520)](./LICENSE)
24
24
 
@@ -66,7 +66,7 @@
66
66
 
67
67
  **WebTransport is the modern transport for realtime apps:** lower latency than WebSocket, multiple independent streams over one connection with no head-of-line blocking, and unreliable datagrams for the data you would rather drop than delay. Browsers have shipped it for years. Node.js has not.
68
68
 
69
- `rwebtransport` closes that gap. It is not an emulation over HTTP/2 or a WebSocket shim. It is a genuine HTTP/3 endpoint speaking QUIC over UDP, built on the same [Cloudflare quiche](https://github.com/cloudflare/quiche) engine that powers WebTransport at scale, exposed through the exact `WebTransport` API you already know from the browser, plus a matching `WebTransportServer`.
69
+ `rwebtransport` is an HTTP/3 endpoint speaking QUIC over UDP, built on [Cloudflare quiche](https://github.com/cloudflare/quiche). It exposes the browser-style `WebTransport` API and a matching `WebTransportServer`. It prefers the current `webtransport-h3` protocol with reliable stream resets and falls back to the older `webtransport` protocol for peers that advertise legacy support. Each session uses its own QUIC connection.
70
70
 
71
71
  QUIC + HTTP/3 + WebTransport, implemented natively in Rust. It ships with prebuilt binaries. No round-trips through a browser, no polyfills, no HTTP/2 emulation, just an actual HTTP/3 datagram on the wire. And it provides the full standard surface: `WebTransport`, `WebTransportBidirectionalStream`, `WebTransportSendStream` and `WebTransportReceiveStream` as WHATWG streams, and `WebTransportDatagramDuplexStream`, all matching the [W3C spec](https://w3c.github.io/webtransport/).
72
72
 
@@ -473,7 +473,7 @@ flowchart TB
473
473
  end
474
474
 
475
475
  subgraph ENGINE["vendored engine"]
476
- QUICHE["Cloudflare quiche 0.29, QUIC transport and HTTP/3"]
476
+ QUICHE["Cloudflare quiche 0.30, QUIC transport and HTTP/3"]
477
477
  BSSL["BoringSSL for the TLS 1.3 handshake"]
478
478
  end
479
479
 
@@ -554,7 +554,7 @@ The suite covers the QUIC handshake, Extended CONNECT, bidirectional and unidire
554
554
  The native core treats the peer and the caller as untrusted, on both the client and the server:
555
555
 
556
556
  - **A panic can never crash Node.** The entire driver thread runs inside a panic boundary; a panic (or a fatal setup error) is surfaced as an `error` event that rejects `ready`/`closed`. It never aborts the process and never silently hangs the session.
557
- - **Bounded receive queues and parsers.** Event delivery to the JS loop is backpressured (unread stream data stays flow-controlled in QUIC, excess datagrams are dropped), servers queue at most 128 unread sessions, each incoming stream direction queues at most 128 unread streams, and finished native streams are pruned. HTTP/3 and capsule parser allocations are limited to 8 MiB per connection and 64 MiB across a server. Exceeding a queue or parser budget terminates the affected session.
557
+ - **Bounded receive queues and parsers.** Stream payload stays in QUIC until the application pulls it. Native receive batches have size limits, excess datagrams can be dropped, and each listener caps live connections and retained receive memory. See [server resource limits](./docs/server.md#server-initiated-streams-and-datagrams).
558
558
  - **No blocking on the event loop.** DNS resolution, the socket bind, and the QUIC handshake all run on the driver thread, so the constructor never blocks Node.
559
559
  - **Hostile input is fail-closed.** Malformed HTTP/3, bad QPACK, oversized frames, unexpected resets, and out-of-range numbers from JS are handled by rejecting or closing cleanly rather than panicking.
560
560
 
@@ -72,7 +72,7 @@ fn main() {
72
72
  .ip("::1")
73
73
  .build(&b.x509v3_context(None, None))
74
74
  .expect("SAN");
75
- b.append_extension(san).expect("append SAN");
75
+ b.append_extension(&san).expect("append SAN");
76
76
 
77
77
  b.sign(&pkey, MessageDigest::sha256()).expect("sign");
78
78
  let cert = b.build();
@@ -21,5 +21,5 @@ log.workspace = true
21
21
  mio.workspace = true
22
22
  getrandom.workspace = true
23
23
  socket2.workspace = true
24
- rustls-native-certs = "0.8.4"
24
+ rustls-native-certs = "0.8.5"
25
25
  url = "2.5"
@@ -106,6 +106,8 @@ pub fn build_config(p: &ClientConfigParams) -> Result<quiche::Config, String> {
106
106
  config.set_initial_max_streams_uni(p.initial_max_streams_uni);
107
107
  config.set_disable_active_migration(true);
108
108
  config.enable_dgram(true, p.dgram_recv_queue, p.dgram_send_queue);
109
+ config.enable_reliable_stream_reset();
110
+ config.set_stream_recv_memory_limits(32 * 1024 * 1024, 32 * 1024 * 1024);
109
111
 
110
112
  Ok(config)
111
113
  }
@@ -137,7 +139,13 @@ pub fn build_server_config(cert_path: &str, key_path: &str) -> Result<quiche::Co
137
139
  .set_application_protos(&[H3_ALPN])
138
140
  .map_err(|e| format!("set_application_protos: {e:?}"))?;
139
141
 
140
- let p = ClientConfigParams::default();
142
+ let p = ClientConfigParams {
143
+ initial_max_data: 2 * 1024 * 1024,
144
+ initial_max_stream_data: 256 * 1024,
145
+ dgram_recv_queue: 128,
146
+ dgram_send_queue: 128,
147
+ ..ClientConfigParams::default()
148
+ };
141
149
  config.set_max_idle_timeout(p.max_idle_timeout_ms);
142
150
  config.set_max_recv_udp_payload_size(p.max_udp_payload_size);
143
151
  config.set_max_send_udp_payload_size(p.max_udp_payload_size);
@@ -149,6 +157,10 @@ pub fn build_server_config(cert_path: &str, key_path: &str) -> Result<quiche::Co
149
157
  config.set_initial_max_streams_uni(p.initial_max_streams_uni);
150
158
  config.set_disable_active_migration(true);
151
159
  config.enable_dgram(true, p.dgram_recv_queue, p.dgram_send_queue);
160
+ config.enable_reliable_stream_reset();
161
+ config.set_max_connection_window(4 * 1024 * 1024);
162
+ config.set_max_stream_window(1024 * 1024);
163
+ config.set_stream_recv_memory_limits(6 * 1024 * 1024, 96 * 1024 * 1024);
152
164
 
153
165
  Ok(config)
154
166
  }
@@ -3,10 +3,12 @@
3
3
  //! the quiche connection, feeds packets to [`crate::session::WtSession`], and
4
4
  //! ferries commands in and events out.
5
5
 
6
+ use std::collections::{HashSet, VecDeque};
6
7
  use std::net::{SocketAddr, ToSocketAddrs};
7
8
  use std::sync::atomic::{AtomicBool, AtomicI64, AtomicUsize, Ordering};
8
9
  use std::sync::mpsc::Receiver;
9
10
  use std::sync::Arc;
11
+ use std::time::{Duration, Instant};
10
12
 
11
13
  use mio::net::UdpSocket;
12
14
  use mio::{Events, Poll, Token};
@@ -28,7 +30,15 @@ pub const WAKE_TOKEN: Token = Token(1);
28
30
  /// back below this cap: back-pressure is set on the driver thread but cleared on the JS thread, so
29
31
  /// without that wake the driver would keep withholding data (including a terminal frame) until an
30
32
  /// unrelated event happened to run another loop iteration.
31
- pub const MAX_INFLIGHT_BATCHES: i64 = 128;
33
+ pub const MAX_INFLIGHT_BATCHES: i64 = 16;
34
+
35
+ /// Yield to commands and timers even if the UDP socket stays readable.
36
+ pub const MAX_RECV_PER_WAKE: usize = 1024;
37
+ /// Allow Initial packets larger than our advertised 1350-byte payload without
38
+ /// handing oversized datagrams to QUIC and its receive queues.
39
+ pub const MAX_RECV_UDP_PAYLOAD: usize = 2048;
40
+ const ADDRESS_ATTEMPT_TIMEOUT: Duration = Duration::from_secs(2);
41
+ const MAX_RESOLVED_ADDRESSES: usize = 16;
32
42
 
33
43
  /// State shared between the driver thread and the JS-facing handle so that
34
44
  /// synchronous property reads (e.g. `datagrams.maxDatagramSize`) don't require a
@@ -164,18 +174,6 @@ fn emit_fatal(sink: &dyn EventSink, shared: &Arc<DriverShared>, message: String)
164
174
  ]);
165
175
  }
166
176
 
167
- fn random_scid() -> [u8; 16] {
168
- let mut b = [0u8; 16];
169
- if getrandom::getrandom(&mut b).is_err() {
170
- let seed: u128 = std::time::SystemTime::now()
171
- .duration_since(std::time::UNIX_EPOCH)
172
- .map(|d| d.as_nanos())
173
- .unwrap_or(0);
174
- b.copy_from_slice(&seed.to_le_bytes());
175
- }
176
- b
177
- }
178
-
179
177
  /// Resolve DNS, bind the UDP socket, build the QUIC config, perform the handshake
180
178
  /// handshake, then run the event loop. Any setup failure is surfaced to JS.
181
179
  fn setup_and_run(
@@ -185,43 +183,22 @@ fn setup_and_run(
185
183
  sink: &dyn EventSink,
186
184
  shared: &Arc<DriverShared>,
187
185
  ) {
188
- // DNS resolution, potentially blocking, so it runs here (driver thread),
189
- // never on the JS event loop.
190
- let peer = match (setup.host.as_str(), setup.port)
186
+ let mut seen = HashSet::new();
187
+ let peers: Vec<_> = (setup.host.as_str(), setup.port)
191
188
  .to_socket_addrs()
192
- .ok()
193
- .and_then(|mut it| it.next())
194
- {
195
- Some(p) => p,
196
- None => {
197
- emit_fatal(
198
- sink,
199
- shared,
200
- format!("failed to resolve {}:{}", setup.host, setup.port),
201
- );
202
- return;
203
- }
204
- };
205
-
206
- let bind: SocketAddr = if peer.is_ipv4() {
207
- "0.0.0.0:0".parse().unwrap()
208
- } else {
209
- "[::]:0".parse().unwrap()
210
- };
211
- let socket = match UdpSocket::bind(bind) {
212
- Ok(s) => s,
213
- Err(e) => {
214
- emit_fatal(sink, shared, format!("failed to bind UDP socket: {e}"));
215
- return;
216
- }
217
- };
218
- let local_addr = match socket.local_addr() {
219
- Ok(a) => a,
220
- Err(e) => {
221
- emit_fatal(sink, shared, format!("failed to read local addr: {e}"));
222
- return;
223
- }
224
- };
189
+ .into_iter()
190
+ .flatten()
191
+ .filter(|address| seen.insert(*address))
192
+ .take(MAX_RESOLVED_ADDRESSES)
193
+ .collect();
194
+ if peers.is_empty() {
195
+ emit_fatal(
196
+ sink,
197
+ shared,
198
+ format!("failed to resolve {}:{}", setup.host, setup.port),
199
+ );
200
+ return;
201
+ }
225
202
 
226
203
  let mut config = match build_config(&setup.config) {
227
204
  Ok(c) => c,
@@ -231,25 +208,174 @@ fn setup_and_run(
231
208
  }
232
209
  };
233
210
 
234
- let scid = random_scid();
235
- let scid = quiche::ConnectionId::from_ref(&scid);
236
- let conn = match quiche::connect(setup.sni.as_deref(), &scid, local_addr, peer, &mut config) {
237
- Ok(c) => c,
238
- Err(e) => {
239
- emit_fatal(sink, shared, format!("quiche connect: {e:?}"));
240
- return;
211
+ let mut poll = poll;
212
+ let mut pending = VecDeque::new();
213
+ let mut bind_error = String::new();
214
+ for (index, peer) in peers.iter().copied().enumerate() {
215
+ let (mut socket, local_addr, mut conn) =
216
+ match dial_peer(peer, setup.sni.as_deref(), &mut config) {
217
+ Ok(connection) => connection,
218
+ Err(e) => {
219
+ bind_error = e;
220
+ continue;
221
+ }
222
+ };
223
+ if index + 1 < peers.len() {
224
+ match probe_address(
225
+ &mut poll,
226
+ &mut socket,
227
+ local_addr,
228
+ &mut conn,
229
+ &rx,
230
+ &mut pending,
231
+ ) {
232
+ Ok(Probe::Established) => {}
233
+ Ok(Probe::NextAddress) => continue,
234
+ Ok(Probe::Closed) => {
235
+ shared.closed.store(true, Ordering::Relaxed);
236
+ sink.emit(vec![Ev::Closed {
237
+ code: 0,
238
+ reason: Vec::new(),
239
+ remote: false,
240
+ }]);
241
+ return;
242
+ }
243
+ Err(e) => {
244
+ bind_error = format!("probing {peer}: {e}");
245
+ continue;
246
+ }
247
+ }
241
248
  }
242
- };
243
249
 
244
- let session = WtSession::new_client(
245
- setup.authority,
246
- setup.path,
247
- setup.origin,
248
- setup.extra_headers,
249
- setup.protocols,
250
+ let session = WtSession::new_client(
251
+ setup.authority,
252
+ setup.path,
253
+ setup.origin,
254
+ setup.extra_headers,
255
+ setup.protocols,
256
+ );
257
+ run_inner(
258
+ poll, socket, local_addr, conn, session, rx, pending, sink, shared,
259
+ );
260
+ return;
261
+ }
262
+
263
+ emit_fatal(
264
+ sink,
265
+ shared,
266
+ format!(
267
+ "could not connect to any address for {}: {bind_error}",
268
+ setup.host
269
+ ),
250
270
  );
271
+ }
251
272
 
252
- run_inner(poll, socket, local_addr, conn, session, rx, sink, shared);
273
+ fn dial_peer(
274
+ peer: SocketAddr,
275
+ sni: Option<&str>,
276
+ config: &mut quiche::Config,
277
+ ) -> Result<(UdpSocket, SocketAddr, quiche::Connection), String> {
278
+ let bind = if peer.is_ipv4() {
279
+ SocketAddr::from((std::net::Ipv4Addr::UNSPECIFIED, 0))
280
+ } else {
281
+ SocketAddr::from((std::net::Ipv6Addr::UNSPECIFIED, 0))
282
+ };
283
+ let socket = UdpSocket::bind(bind).map_err(|e| format!("binding for {peer}: {e}"))?;
284
+ let local = socket
285
+ .local_addr()
286
+ .map_err(|e| format!("local address: {e}"))?;
287
+ let mut scid = [0; 16];
288
+ getrandom::fill(&mut scid).map_err(|e| format!("generating QUIC connection ID: {e}"))?;
289
+ let scid = quiche::ConnectionId::from_ref(&scid);
290
+ let conn = quiche::connect(sni, &scid, local, peer, config)
291
+ .map_err(|e| format!("QUIC connect to {peer}: {e:?}"))?;
292
+ Ok((socket, local, conn))
293
+ }
294
+
295
+ #[derive(Debug, PartialEq, Eq)]
296
+ enum Probe {
297
+ Established,
298
+ NextAddress,
299
+ Closed,
300
+ }
301
+
302
+ fn probe_address(
303
+ poll: &mut Poll,
304
+ socket: &mut UdpSocket,
305
+ local: SocketAddr,
306
+ conn: &mut quiche::Connection,
307
+ rx: &Receiver<Command>,
308
+ pending: &mut VecDeque<Command>,
309
+ ) -> std::io::Result<Probe> {
310
+ poll.registry()
311
+ .register(socket, SOCKET_TOKEN, mio::Interest::READABLE)?;
312
+ let result = probe_registered(poll, socket, local, conn, rx, pending);
313
+ poll.registry().deregister(socket)?;
314
+ result
315
+ }
316
+
317
+ fn probe_registered(
318
+ poll: &mut Poll,
319
+ socket: &UdpSocket,
320
+ local: SocketAddr,
321
+ conn: &mut quiche::Connection,
322
+ rx: &Receiver<Command>,
323
+ pending: &mut VecDeque<Command>,
324
+ ) -> std::io::Result<Probe> {
325
+ let deadline = Instant::now() + ADDRESS_ATTEMPT_TIMEOUT;
326
+ let mut events = Events::with_capacity(8);
327
+ let mut packet = vec![0; 65_535];
328
+ let mut out = [0; 1350];
329
+ loop {
330
+ while let Ok(cmd) = rx.try_recv() {
331
+ if matches!(cmd, Command::Close { .. } | Command::Shutdown) {
332
+ return Ok(Probe::Closed);
333
+ }
334
+ pending.push_back(cmd);
335
+ }
336
+ if conn.is_established() && conn.local_error().is_none() && !conn.is_draining() {
337
+ return Ok(Probe::Established);
338
+ }
339
+ let remaining = deadline.saturating_duration_since(Instant::now());
340
+ if remaining.is_zero()
341
+ || conn.is_closed()
342
+ || conn.local_error().is_some()
343
+ || conn.is_draining()
344
+ {
345
+ return Ok(Probe::NextAddress);
346
+ }
347
+ conn.on_timeout();
348
+ flush_send(conn, socket, &mut out, false);
349
+ let mut drained = false;
350
+ let mut received = false;
351
+ for _ in 0..MAX_RECV_PER_WAKE {
352
+ match socket.recv_from(&mut packet) {
353
+ Ok((len, from)) if len != 0 && len <= MAX_RECV_UDP_PAYLOAD => {
354
+ let _ = conn.recv(&mut packet[..len], quiche::RecvInfo { from, to: local });
355
+ received = true;
356
+ }
357
+ Ok(_) => {}
358
+ Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
359
+ drained = true;
360
+ break;
361
+ }
362
+ Err(e) if is_udp_port_unreachable(&e) => return Ok(Probe::NextAddress),
363
+ Err(e) => return Err(e),
364
+ }
365
+ }
366
+ if received || !drained {
367
+ continue;
368
+ }
369
+ let remaining = deadline.saturating_duration_since(Instant::now());
370
+ let timeout = conn
371
+ .timeout()
372
+ .map_or(remaining, |timeout| timeout.min(remaining));
373
+ if let Err(e) = poll.poll(&mut events, Some(timeout)) {
374
+ if e.kind() != std::io::ErrorKind::Interrupted {
375
+ return Err(e);
376
+ }
377
+ }
378
+ }
253
379
  }
254
380
 
255
381
  /// The actual event loop. Runs until the connection closes or a fatal error
@@ -263,6 +389,7 @@ fn run_inner(
263
389
  mut conn: quiche::Connection,
264
390
  mut session: WtSession,
265
391
  rx: Receiver<Command>,
392
+ mut pending: VecDeque<Command>,
266
393
  sink: &dyn EventSink,
267
394
  shared: &Arc<DriverShared>,
268
395
  ) {
@@ -290,8 +417,15 @@ fn run_inner(
290
417
  // Prime the handshake (send the client Initial).
291
418
  flush_send(&mut conn, &socket, &mut send_buf, debug);
292
419
 
420
+ let mut udp_pending = conn.is_established() || !pending.is_empty();
421
+ let mut read_pending = false;
293
422
  loop {
294
- let timeout = conn.timeout();
423
+ let can_read = shared.inflight.load(Ordering::Relaxed) < MAX_INFLIGHT_BATCHES;
424
+ let timeout = if udp_pending || (read_pending && can_read) {
425
+ Some(std::time::Duration::ZERO)
426
+ } else {
427
+ conn.timeout()
428
+ };
295
429
  if let Err(e) = poll.poll(&mut events, timeout) {
296
430
  if e.kind() == std::io::ErrorKind::Interrupted {
297
431
  continue;
@@ -303,17 +437,18 @@ fn run_inner(
303
437
  let mut evs: Vec<Ev> = Vec::new();
304
438
  let mut shutdown = false;
305
439
 
306
- // 1. Read all datagrams the socket has.
307
- let mut got_packet = false;
308
- loop {
440
+ // mio may use edge-triggered readiness. If we stop before WouldBlock,
441
+ // poll again without waiting for a new readiness edge.
442
+ let mut udp_budget = MAX_RECV_PER_WAKE;
443
+ while udp_budget > 0 {
444
+ udp_budget -= 1;
309
445
  match socket.recv_from(&mut recv_buf) {
310
446
  Ok((len, from)) => {
311
447
  // UDP permits empty datagrams. They carry no QUIC packet
312
448
  // and must not become fatal errors before authentication.
313
- if len == 0 {
449
+ if len == 0 || len > MAX_RECV_UDP_PAYLOAD {
314
450
  continue;
315
451
  }
316
- got_packet = true;
317
452
  if debug {
318
453
  eprintln!("[rwt-driver] recv {len} bytes from {from}");
319
454
  }
@@ -328,25 +463,31 @@ fn run_inner(
328
463
  if debug {
329
464
  eprintln!("[rwt-driver] recv err {e:?}");
330
465
  }
331
- evs.push(Ev::Error(format!("quic recv: {e:?}")));
466
+ // quiche records fatal transport errors on the
467
+ // connection. Invalid UDP input is otherwise dropped.
332
468
  }
333
469
  }
334
470
  }
335
471
  Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
472
+ Err(ref e) if is_udp_port_unreachable(e) => {
473
+ if debug {
474
+ eprintln!("[rwt-driver] UDP port unreachable: {e}");
475
+ }
476
+ continue;
477
+ }
336
478
  Err(e) => {
337
479
  evs.push(Ev::Error(format!("socket recv: {e}")));
338
480
  break;
339
481
  }
340
482
  }
341
483
  }
484
+ udp_pending = udp_budget == 0;
342
485
 
343
- // 2. Fire the timeout if nothing else woke us.
344
- if !got_packet && events.is_empty() {
345
- conn.on_timeout();
346
- }
486
+ // quiche checks whether a timer is due.
487
+ conn.on_timeout();
347
488
 
348
489
  // 3. Drain commands.
349
- while let Ok(cmd) = rx.try_recv() {
490
+ while let Some(cmd) = pending.pop_front().or_else(|| rx.try_recv().ok()) {
350
491
  match cmd {
351
492
  Command::OpenStream { request_id, bidi } => {
352
493
  session.open_stream(bidi, request_id, &mut evs)
@@ -375,9 +516,9 @@ fn run_inner(
375
516
  label,
376
517
  context,
377
518
  length,
378
- } => evs.push(crate::session::build_keying_material(
379
- &mut conn, request_id, &label, &context, length,
380
- )),
519
+ } => evs.push(
520
+ session.build_keying_material(&mut conn, request_id, &label, &context, length),
521
+ ),
381
522
  Command::Shutdown => {
382
523
  shutdown = true;
383
524
  break;
@@ -406,24 +547,18 @@ fn run_inner(
406
547
  conn.application_proto()
407
548
  );
408
549
  }
409
- session.on_established();
550
+ session.on_established(conn.reliable_stream_reset_negotiated());
410
551
  }
411
552
 
412
553
  // Backpressure: if the JS thread is behind on processing events, stop
413
554
  // pulling high-volume data out of quiche this iteration. Unread stream
414
555
  // bytes stay flow-controlled in quiche and excess datagrams are dropped
415
556
  // by quiche's bounded recv queue, so the event queue cannot grow without
416
- // bound (hostile-flood OOM protection). Control-plane processing (the
417
- // CONNECT response, SETTINGS, session close) always runs.
418
- let backpressured = shared.inflight.load(Ordering::Relaxed) >= MAX_INFLIGHT_BATCHES;
419
-
420
- if !backpressured {
421
- session.on_datagrams(&mut conn, &mut evs);
422
- }
423
-
424
- let readable: Vec<u64> = conn.readable().collect();
425
- for id in readable {
426
- session.on_readable(&mut conn, id, backpressured, &mut evs);
557
+ // bound. QUIC packet processing and closure continue under pressure.
558
+ if shared.inflight.load(Ordering::Relaxed) < MAX_INFLIGHT_BATCHES {
559
+ let mut budget = crate::session::ReceiveBudget::new();
560
+ session.read_pending(&mut conn, &mut budget, &mut evs);
561
+ read_pending = budget.exhausted();
427
562
  }
428
563
 
429
564
  session.flush(&mut conn, &mut evs);
@@ -466,6 +601,13 @@ fn run_inner(
466
601
  }
467
602
  }
468
603
 
604
+ /// Winsock may report an ICMP Port Unreachable for an earlier UDP send. Let QUIC
605
+ /// determine connection liveness after loss rather than retiring the session from recv_from.
606
+ pub(crate) fn is_udp_port_unreachable(error: &std::io::Error) -> bool {
607
+ const WSAECONNRESET: i32 = 10054;
608
+ cfg!(windows) && error.raw_os_error() == Some(WSAECONNRESET)
609
+ }
610
+
469
611
  /// Drain quiche's send queue to the socket.
470
612
  fn flush_send(conn: &mut quiche::Connection, socket: &UdpSocket, out: &mut [u8], debug: bool) {
471
613
  loop {
@@ -549,3 +691,19 @@ fn closure_info(conn: &quiche::Connection) -> (u32, Vec<u8>, bool, Option<String
549
691
  Some("connection closed abnormally".to_string()),
550
692
  )
551
693
  }
694
+
695
+ #[cfg(test)]
696
+ mod receive_error_tests {
697
+ use super::is_udp_port_unreachable;
698
+ use std::io::Error;
699
+
700
+ #[test]
701
+ fn the_winsock_udp_notification_keeps_the_quic_session_alive() {
702
+ assert_eq!(
703
+ is_udp_port_unreachable(&Error::from_raw_os_error(10054)),
704
+ cfg!(windows)
705
+ );
706
+ assert!(!is_udp_port_unreachable(&Error::from_raw_os_error(10053)));
707
+ assert!(!is_udp_port_unreachable(&Error::from_raw_os_error(10038)));
708
+ }
709
+ }
@@ -451,7 +451,6 @@ use server::{ServerCommand, ServerEventSink, ServerMsg, ServerSetup};
451
451
  struct ServerHandle {
452
452
  tx: Mutex<Option<Sender<ServerCommand>>>,
453
453
  waker: Arc<mio::Waker>,
454
- shared: Arc<DriverShared>,
455
454
  thread: Mutex<Option<std::thread::JoinHandle<()>>>,
456
455
  }
457
456
 
@@ -601,7 +600,6 @@ fn server_listen(mut cx: FunctionContext) -> JsResult<JsBox<ServerHandle>> {
601
600
  Ok(cx.boxed(ServerHandle {
602
601
  tx: Mutex::new(Some(tx)),
603
602
  waker,
604
- shared,
605
603
  thread: Mutex::new(Some(thread)),
606
604
  }))
607
605
  }
@@ -686,12 +684,6 @@ fn server_send_datagram(mut cx: FunctionContext) -> JsResult<JsUndefined> {
686
684
  Ok(cx.undefined())
687
685
  }
688
686
 
689
- fn server_max_datagram_size(mut cx: FunctionContext) -> JsResult<JsNumber> {
690
- let handle = cx.argument::<JsBox<ServerHandle>>(0)?;
691
- let n = handle.shared.max_datagram_size.load(Ordering::Relaxed);
692
- Ok(cx.number(n as f64))
693
- }
694
-
695
687
  fn server_close_session(mut cx: FunctionContext) -> JsResult<JsUndefined> {
696
688
  let handle = cx.argument::<JsBox<ServerHandle>>(0)?;
697
689
  let session = to_stream_id(cx.argument::<JsNumber>(1)?.value(&mut cx));
@@ -886,6 +878,10 @@ fn ev_to_js<'a>(cx: &mut TaskContext<'a>, ev: &Ev) -> JsResult<'a, JsObject> {
886
878
  set_str(cx, &obj, "type", "datagram")?;
887
879
  set_buf(cx, &obj, "data", data)?;
888
880
  }
881
+ Ev::DatagramSize(size) => {
882
+ set_str(cx, &obj, "type", "datagramSize")?;
883
+ set_num(cx, &obj, "size", *size as f64)?;
884
+ }
889
885
  Ev::IncomingStream { id, bidi } => {
890
886
  set_str(cx, &obj, "type", "stream")?;
891
887
  set_num(cx, &obj, "streamId", *id as f64)?;
@@ -915,9 +911,13 @@ fn ev_to_js<'a>(cx: &mut TaskContext<'a>, ev: &Ev) -> JsResult<'a, JsObject> {
915
911
  set_num(cx, &obj, "requestId", *request_id as f64)?;
916
912
  set_num(cx, &obj, "streamId", *id as f64)?;
917
913
  }
918
- Ev::WriteAck { request_id } => {
914
+ Ev::WriteAck {
915
+ request_id,
916
+ accepted,
917
+ } => {
919
918
  set_str(cx, &obj, "type", "writeAck")?;
920
919
  set_num(cx, &obj, "requestId", *request_id as f64)?;
920
+ set_bool(cx, &obj, "accepted", *accepted)?;
921
921
  }
922
922
  Ev::DatagramAck { request_id, sent } => {
923
923
  set_str(cx, &obj, "type", "datagramAck")?;
@@ -1013,7 +1013,6 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
1013
1013
  cx.export_function("serverStopSending", server_stop_sending)?;
1014
1014
  cx.export_function("serverSetPaused", server_set_paused)?;
1015
1015
  cx.export_function("serverSendDatagram", server_send_datagram)?;
1016
- cx.export_function("serverMaxDatagramSize", server_max_datagram_size)?;
1017
1016
  cx.export_function("serverCloseSession", server_close_session)?;
1018
1017
  cx.export_function("serverDrain", server_drain)?;
1019
1018
  cx.export_function("serverGetStats", server_get_stats)?;
@@ -28,6 +28,7 @@ impl ParserMemory {
28
28
  })
29
29
  }
30
30
 
31
+ #[allow(deprecated)] // Use the atomic API available on older Rust toolchains.
31
32
  fn reserve(&self, bytes: usize) -> bool {
32
33
  let reserved = self
33
34
  .used