rwebtransport 0.2.2 → 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 (99) hide show
  1. package/Cargo.lock +523 -140
  2. package/Cargo.toml +5 -5
  3. package/README.md +8 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +2 -0
  6. package/crates/native/src/config.rs +69 -8
  7. package/crates/native/src/driver.rs +253 -90
  8. package/crates/native/src/lib.rs +80 -57
  9. package/crates/native/src/parser_memory.rs +203 -0
  10. package/crates/native/src/server.rs +215 -183
  11. package/crates/native/src/session.rs +1341 -243
  12. package/crates/native/src/test_support.rs +119 -0
  13. package/crates/wtcore/Cargo.toml +1 -0
  14. package/crates/wtcore/src/h3.rs +250 -46
  15. package/dist/index.js +218 -75
  16. package/dist/index.js.map +7 -1
  17. package/dist/index.mjs +214 -69
  18. package/dist/index.mjs.map +7 -1
  19. package/dist/native.d.ts +10 -4
  20. package/dist/server.d.ts +1 -0
  21. package/package.json +84 -78
  22. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  24. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  25. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  26. package/scripts/build-linux-prebuilt.sh +46 -0
  27. package/scripts/build-ts.js +41 -0
  28. package/scripts/build.js +10 -3
  29. package/vendor/quiche/AGENTS.md +10 -0
  30. package/vendor/quiche/Cargo.lock +239 -248
  31. package/vendor/quiche/Cargo.toml +23 -10
  32. package/vendor/quiche/examples/Makefile +4 -4
  33. package/vendor/quiche/examples/client.rs +10 -28
  34. package/vendor/quiche/examples/http3-client.rs +20 -34
  35. package/vendor/quiche/examples/http3-server.rs +47 -78
  36. package/vendor/quiche/examples/server.rs +19 -49
  37. package/vendor/quiche/include/quiche.h +35 -4
  38. package/vendor/quiche/src/build.rs +17 -0
  39. package/vendor/quiche/src/cid.rs +129 -61
  40. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  41. package/vendor/quiche/src/crypto/mod.rs +64 -77
  42. package/vendor/quiche/src/dgram.rs +2 -3
  43. package/vendor/quiche/src/error.rs +22 -20
  44. package/vendor/quiche/src/ffi.rs +558 -280
  45. package/vendor/quiche/src/flowcontrol.rs +0 -7
  46. package/vendor/quiche/src/frame.rs +134 -135
  47. package/vendor/quiche/src/h3/ffi.rs +73 -60
  48. package/vendor/quiche/src/h3/frame.rs +70 -90
  49. package/vendor/quiche/src/h3/mod.rs +1024 -593
  50. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  51. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  52. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  53. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  54. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  55. package/vendor/quiche/src/h3/stream.rs +1009 -194
  56. package/vendor/quiche/src/lib.rs +1361 -1195
  57. package/vendor/quiche/src/minmax.rs +2 -6
  58. package/vendor/quiche/src/packet.rs +420 -552
  59. package/vendor/quiche/src/path.rs +202 -72
  60. package/vendor/quiche/src/pmtud.rs +18 -16
  61. package/vendor/quiche/src/ranges.rs +184 -82
  62. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  63. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  64. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  65. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  66. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  67. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  68. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  69. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  70. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  71. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  72. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  73. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  74. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  84. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  85. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  86. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  87. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  88. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  89. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  90. package/vendor/quiche/src/recovery/mod.rs +414 -173
  91. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  92. package/vendor/quiche/src/stream/mod.rs +864 -133
  93. package/vendor/quiche/src/stream/recv_buf.rs +476 -172
  94. package/vendor/quiche/src/stream/send_buf.rs +343 -81
  95. package/vendor/quiche/src/test_utils.rs +78 -64
  96. package/vendor/quiche/src/tests.rs +2559 -1335
  97. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  98. package/vendor/quiche/src/tls/mod.rs +146 -193
  99. 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 memory under flood.** A hostile peer cannot grow memory without limit: event delivery to the JS loop is backpressured (unread stream data stays flow-controlled in QUIC, excess datagrams are dropped), the datagram queue is bounded, HTTP/3 frame buffers are capped, and finished streams are pruned.
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
 
@@ -575,3 +575,7 @@ Certificate verification follows the WebTransport model: full PKI validation by
575
575
  <div align="center">
576
576
  <sub>Built by <a href="https://github.com/dacely-cloud">Dacely Cloud</a>.</sub>
577
577
  </div>
578
+
579
+ ### Development checks
580
+
581
+ `npm test` runs the Node suite. `npm run test:browser` runs Chrome interoperability tests against the native server; Chrome must be installed, and WebdriverIO manages the driver. `npm run test:all` runs both suites. `npm run test:coverage` runs both with V8 coverage reports in `coverage/`. These commands build the native addon, TypeScript output, and Rust test fixtures first. Native protocol regressions run with `cargo test --release -p rwebtransport-native -p rwebtransport-wtcore`.
@@ -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,3 +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.5"
25
+ url = "2.5"
@@ -12,6 +12,7 @@ pub const H3_ALPN: &[u8] = b"h3";
12
12
  #[derive(Clone, Debug)]
13
13
  pub struct ClientConfigParams {
14
14
  pub verify: CertVerification,
15
+ pub expected_ip: Option<std::net::IpAddr>,
15
16
  pub max_idle_timeout_ms: u64,
16
17
  pub max_udp_payload_size: usize,
17
18
  pub initial_max_data: u64,
@@ -26,6 +27,7 @@ impl Default for ClientConfigParams {
26
27
  fn default() -> Self {
27
28
  Self {
28
29
  verify: CertVerification::PkiDefault,
30
+ expected_ip: None,
29
31
  max_idle_timeout_ms: 30_000,
30
32
  max_udp_payload_size: 1350,
31
33
  initial_max_data: 10 * 1024 * 1024,
@@ -42,7 +44,36 @@ impl Default for ClientConfigParams {
42
44
 
43
45
  /// Build a ready-to-use quiche client [`quiche::Config`].
44
46
  pub fn build_config(p: &ClientConfigParams) -> Result<quiche::Config, String> {
45
- let builder = build_client_tls(&p.verify)?;
47
+ let mut builder = build_client_tls(&p.verify)?;
48
+ if matches!(p.verify, CertVerification::PkiDefault) {
49
+ let roots = rustls_native_certs::load_native_certs();
50
+ let mut loaded = 0;
51
+ for der in roots.certs {
52
+ if let Ok(cert) = boring::x509::X509::from_der(der.as_ref()) {
53
+ if builder.cert_store_mut().add_cert(cert).is_ok() {
54
+ loaded += 1;
55
+ }
56
+ }
57
+ }
58
+ if loaded == 0 {
59
+ return Err("system certificate store contains no usable roots".into());
60
+ }
61
+ if let Some(ip) = p.expected_ip {
62
+ let bytes: Vec<u8> = match ip {
63
+ std::net::IpAddr::V4(ip) => ip.octets().to_vec(),
64
+ std::net::IpAddr::V6(ip) => ip.octets().to_vec(),
65
+ };
66
+ // The context owns the verification parameter; BoringSSL copies it
67
+ // into the connection and copies the address supplied here.
68
+ let ok = unsafe {
69
+ let param = boring_sys::SSL_CTX_get0_param(builder.as_ptr());
70
+ boring_sys::X509_VERIFY_PARAM_set1_ip(param, bytes.as_ptr(), bytes.len())
71
+ };
72
+ if ok != 1 {
73
+ return Err("failed to configure certificate IP verification".into());
74
+ }
75
+ }
76
+ }
46
77
  let mut config = quiche::Config::with_boring_ssl_ctx_builder(quiche::PROTOCOL_VERSION, builder)
47
78
  .map_err(|e| format!("quiche config: {e:?}"))?;
48
79
 
@@ -56,12 +87,6 @@ pub fn build_config(p: &ClientConfigParams) -> Result<quiche::Config, String> {
56
87
  // it a trust anchor set. Hostname verification is wired by quiche's
57
88
  // `set_host_name` (X509_VERIFY_PARAM_set1_host).
58
89
  config.verify_peer(true);
59
- #[cfg(unix)]
60
- {
61
- // Best-effort system roots. If neither path exists the handshake
62
- // will fail closed, which is the correct default.
63
- let _ = config.load_verify_locations_from_directory("/etc/ssl/certs");
64
- }
65
90
  }
66
91
  CertVerification::Hashes(_) | CertVerification::Insecure => {
67
92
  // Verification is fully handled on the SSL context (custom callback /
@@ -81,6 +106,8 @@ pub fn build_config(p: &ClientConfigParams) -> Result<quiche::Config, String> {
81
106
  config.set_initial_max_streams_uni(p.initial_max_streams_uni);
82
107
  config.set_disable_active_migration(true);
83
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);
84
111
 
85
112
  Ok(config)
86
113
  }
@@ -112,7 +139,13 @@ pub fn build_server_config(cert_path: &str, key_path: &str) -> Result<quiche::Co
112
139
  .set_application_protos(&[H3_ALPN])
113
140
  .map_err(|e| format!("set_application_protos: {e:?}"))?;
114
141
 
115
- 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
+ };
116
149
  config.set_max_idle_timeout(p.max_idle_timeout_ms);
117
150
  config.set_max_recv_udp_payload_size(p.max_udp_payload_size);
118
151
  config.set_max_send_udp_payload_size(p.max_udp_payload_size);
@@ -124,6 +157,34 @@ pub fn build_server_config(cert_path: &str, key_path: &str) -> Result<quiche::Co
124
157
  config.set_initial_max_streams_uni(p.initial_max_streams_uni);
125
158
  config.set_disable_active_migration(true);
126
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);
127
164
 
128
165
  Ok(config)
129
166
  }
167
+
168
+ #[cfg(test)]
169
+ mod tests {
170
+ #[test]
171
+ fn certificate_identity_checks_dns_and_ip_sans() {
172
+ use crate::test_support::{exchange, pair};
173
+ for (expected_ip, name, ip_san, accepted) in [
174
+ (Some("127.0.0.1".parse().unwrap()), None, false, false),
175
+ (Some("127.0.0.1".parse().unwrap()), None, true, true),
176
+ (Some("::1".parse().unwrap()), None, false, false),
177
+ (Some("::1".parse().unwrap()), None, true, true),
178
+ (None, Some("localhost"), false, false),
179
+ (None, Some("wrong.example"), false, true),
180
+ ] {
181
+ let (mut client, mut server) = pair(expected_ip, name, ip_san);
182
+ exchange(&mut client, &mut server);
183
+ assert_eq!(
184
+ client.is_established(),
185
+ accepted,
186
+ "IP={expected_ip:?}, DNS={name:?}, ip_san={ip_san}"
187
+ );
188
+ }
189
+ }
190
+ }
@@ -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,12 +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
- got_packet = true;
447
+ // UDP permits empty datagrams. They carry no QUIC packet
448
+ // and must not become fatal errors before authentication.
449
+ if len == 0 || len > MAX_RECV_UDP_PAYLOAD {
450
+ continue;
451
+ }
312
452
  if debug {
313
453
  eprintln!("[rwt-driver] recv {len} bytes from {from}");
314
454
  }
@@ -323,25 +463,31 @@ fn run_inner(
323
463
  if debug {
324
464
  eprintln!("[rwt-driver] recv err {e:?}");
325
465
  }
326
- evs.push(Ev::Error(format!("quic recv: {e:?}")));
466
+ // quiche records fatal transport errors on the
467
+ // connection. Invalid UDP input is otherwise dropped.
327
468
  }
328
469
  }
329
470
  }
330
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
+ }
331
478
  Err(e) => {
332
479
  evs.push(Ev::Error(format!("socket recv: {e}")));
333
480
  break;
334
481
  }
335
482
  }
336
483
  }
484
+ udp_pending = udp_budget == 0;
337
485
 
338
- // 2. Fire the timeout if nothing else woke us.
339
- if !got_packet && events.is_empty() {
340
- conn.on_timeout();
341
- }
486
+ // quiche checks whether a timer is due.
487
+ conn.on_timeout();
342
488
 
343
489
  // 3. Drain commands.
344
- while let Ok(cmd) = rx.try_recv() {
490
+ while let Some(cmd) = pending.pop_front().or_else(|| rx.try_recv().ok()) {
345
491
  match cmd {
346
492
  Command::OpenStream { request_id, bidi } => {
347
493
  session.open_stream(bidi, request_id, &mut evs)
@@ -370,9 +516,9 @@ fn run_inner(
370
516
  label,
371
517
  context,
372
518
  length,
373
- } => evs.push(crate::session::build_keying_material(
374
- &mut conn, request_id, &label, &context, length,
375
- )),
519
+ } => evs.push(
520
+ session.build_keying_material(&mut conn, request_id, &label, &context, length),
521
+ ),
376
522
  Command::Shutdown => {
377
523
  shutdown = true;
378
524
  break;
@@ -401,24 +547,18 @@ fn run_inner(
401
547
  conn.application_proto()
402
548
  );
403
549
  }
404
- session.on_established();
550
+ session.on_established(conn.reliable_stream_reset_negotiated());
405
551
  }
406
552
 
407
553
  // Backpressure: if the JS thread is behind on processing events, stop
408
554
  // pulling high-volume data out of quiche this iteration. Unread stream
409
555
  // bytes stay flow-controlled in quiche and excess datagrams are dropped
410
556
  // by quiche's bounded recv queue, so the event queue cannot grow without
411
- // bound (hostile-flood OOM protection). Control-plane processing (the
412
- // CONNECT response, SETTINGS, session close) always runs.
413
- let backpressured = shared.inflight.load(Ordering::Relaxed) >= MAX_INFLIGHT_BATCHES;
414
-
415
- if !backpressured {
416
- session.on_datagrams(&mut conn, &mut evs);
417
- }
418
-
419
- let readable: Vec<u64> = conn.readable().collect();
420
- for id in readable {
421
- 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();
422
562
  }
423
563
 
424
564
  session.flush(&mut conn, &mut evs);
@@ -461,6 +601,13 @@ fn run_inner(
461
601
  }
462
602
  }
463
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
+
464
611
  /// Drain quiche's send queue to the socket.
465
612
  fn flush_send(conn: &mut quiche::Connection, socket: &UdpSocket, out: &mut [u8], debug: bool) {
466
613
  loop {
@@ -544,3 +691,19 @@ fn closure_info(conn: &quiche::Connection) -> (u32, Vec<u8>, bool, Option<String
544
691
  Some("connection closed abnormally".to_string()),
545
692
  )
546
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
+ }