rwebtransport 0.1.0 → 0.1.1
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/Cargo.lock +666 -666
- package/Cargo.toml +31 -31
- package/LICENSE +202 -202
- package/NOTICE +41 -41
- package/README.md +550 -550
- package/crates/echo-server/Cargo.toml +20 -20
- package/crates/echo-server/src/main.rs +666 -666
- package/crates/native/Cargo.toml +23 -23
- package/crates/native/src/config.rs +129 -129
- package/crates/native/src/driver.rs +463 -463
- package/crates/native/src/lib.rs +821 -821
- package/crates/native/src/server.rs +718 -718
- package/crates/native/src/session.rs +1191 -1191
- package/crates/native/src/tls.rs +96 -96
- package/crates/wtcore/Cargo.toml +15 -15
- package/crates/wtcore/src/h3.rs +226 -226
- package/crates/wtcore/src/lib.rs +9 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +80 -79
- package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-x64/rwebtransport.node +0 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/bench.mjs +180 -180
- package/scripts/build.js +67 -67
- package/scripts/postinstall.js +67 -67
- package/scripts/setup-unix.sh +129 -129
- package/scripts/setup-windows.ps1 +229 -229
- package/vendor/quiche/AGENTS.md +75 -75
- package/vendor/quiche/COPYING +23 -23
- package/vendor/quiche/Cargo.lock +1212 -1212
- package/vendor/quiche/Cargo.toml +215 -215
- package/vendor/quiche/README.md +466 -466
- package/vendor/quiche/examples/Makefile +46 -46
- package/vendor/quiche/examples/README.md +20 -20
- package/vendor/quiche/examples/cert-big.crt +90 -90
- package/vendor/quiche/examples/cert.crt +18 -18
- package/vendor/quiche/examples/cert.key +28 -28
- package/vendor/quiche/examples/client.c +343 -343
- package/vendor/quiche/examples/client.rs +281 -281
- package/vendor/quiche/examples/gen-certs.sh +15 -15
- package/vendor/quiche/examples/http3-client.c +473 -473
- package/vendor/quiche/examples/http3-client.rs +349 -349
- package/vendor/quiche/examples/http3-server.c +614 -614
- package/vendor/quiche/examples/http3-server.rs +680 -680
- package/vendor/quiche/examples/qpack-decode.rs +87 -87
- package/vendor/quiche/examples/qpack-encode.rs +88 -88
- package/vendor/quiche/examples/rootca.crt +21 -21
- package/vendor/quiche/examples/server.c +529 -529
- package/vendor/quiche/examples/server.rs +529 -529
- package/vendor/quiche/include/quiche.h +1282 -1282
- package/vendor/quiche/src/buffers.rs +105 -105
- package/vendor/quiche/src/build.rs +66 -66
- package/vendor/quiche/src/cid.rs +1079 -1079
- package/vendor/quiche/src/crypto/boringssl.rs +373 -373
- package/vendor/quiche/src/crypto/mod.rs +671 -671
- package/vendor/quiche/src/dgram.rs +118 -118
- package/vendor/quiche/src/error.rs +263 -263
- package/vendor/quiche/src/ffi.rs +2290 -2290
- package/vendor/quiche/src/flowcontrol.rs +246 -246
- package/vendor/quiche/src/frame.rs +2172 -2172
- package/vendor/quiche/src/h3/AGENTS.md +54 -54
- package/vendor/quiche/src/h3/ffi.rs +442 -442
- package/vendor/quiche/src/h3/frame.rs +1404 -1404
- package/vendor/quiche/src/h3/mod.rs +8223 -8223
- package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
- package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
- package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
- package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
- package/vendor/quiche/src/h3/stream.rs +1595 -1595
- package/vendor/quiche/src/lib.rs +9556 -9556
- package/vendor/quiche/src/minmax.rs +446 -446
- package/vendor/quiche/src/packet.rs +2334 -2334
- package/vendor/quiche/src/path.rs +1374 -1374
- package/vendor/quiche/src/pmtud.rs +652 -652
- package/vendor/quiche/src/rand.rs +64 -64
- package/vendor/quiche/src/range_buf.rs +186 -186
- package/vendor/quiche/src/ranges.rs +755 -755
- package/vendor/quiche/src/recovery/AGENTS.md +80 -80
- package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
- package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
- package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
- package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
- package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
- package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
- package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
- package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
- package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
- package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
- package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
- package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
- package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
- package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
- package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
- package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
- package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
- package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
- package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
- package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
- package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
- package/vendor/quiche/src/recovery/mod.rs +3086 -3086
- package/vendor/quiche/src/recovery/rtt.rs +152 -152
- package/vendor/quiche/src/stream/mod.rs +2210 -2210
- package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
- package/vendor/quiche/src/stream/send_buf.rs +849 -849
- package/vendor/quiche/src/test_utils.rs +681 -681
- package/vendor/quiche/src/tests.rs +12922 -12922
- package/vendor/quiche/src/tls/boringssl.rs +375 -375
- package/vendor/quiche/src/tls/mod.rs +1245 -1245
- package/vendor/quiche/src/transport_params.rs +628 -628
|
@@ -1,666 +1,666 @@
|
|
|
1
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
//! WebTransport **echo** server — a test fixture for the rwebtransport client.
|
|
3
|
-
//!
|
|
4
|
-
//! It speaks real QUIC/HTTP-3/WebTransport (Cloudflare quiche + BoringSSL,
|
|
5
|
-
//! server role) and echoes everything a client sends:
|
|
6
|
-
//! * bidirectional streams — bytes are written straight back on the same stream;
|
|
7
|
-
//! * unidirectional streams — the server opens a matching uni stream and echoes;
|
|
8
|
-
//! * datagrams — sent back verbatim.
|
|
9
|
-
//!
|
|
10
|
-
//! Usage: `wt-echo-server --cert <pem> --key <pem> [--host 127.0.0.1] [--port 0]`.
|
|
11
|
-
//! On startup it prints two machine-readable lines to stdout:
|
|
12
|
-
//! `PORT <n>` (the actually-bound UDP port) and `READY`.
|
|
13
|
-
|
|
14
|
-
use std::collections::HashMap;
|
|
15
|
-
use std::net::SocketAddr;
|
|
16
|
-
|
|
17
|
-
use mio::net::UdpSocket;
|
|
18
|
-
use mio::{Events, Interest, Poll, Token};
|
|
19
|
-
use wtcore::h3;
|
|
20
|
-
|
|
21
|
-
const LOCAL_CONN_ID_LEN: usize = 16;
|
|
22
|
-
const SOCKET: Token = Token(0);
|
|
23
|
-
|
|
24
|
-
/// Server behaviour, selectable via `--mode` for adversarial client tests.
|
|
25
|
-
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
26
|
-
enum Mode {
|
|
27
|
-
/// Echo everything (default).
|
|
28
|
-
Echo,
|
|
29
|
-
/// Reject the Extended CONNECT with `:status 404`.
|
|
30
|
-
Reject,
|
|
31
|
-
/// Answer CONNECT with a HEADERS frame carrying a malformed QPACK block.
|
|
32
|
-
MalformedHeaders,
|
|
33
|
-
/// After a normal CONNECT, dump random non-frame bytes on the CONNECT stream.
|
|
34
|
-
Garbage,
|
|
35
|
-
/// Echo bidi streams but RESET them instead of finishing cleanly.
|
|
36
|
-
ResetStreams,
|
|
37
|
-
/// Accept CONNECT, then immediately close the WebTransport session (FIN).
|
|
38
|
-
CloseSession,
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
impl Mode {
|
|
42
|
-
fn parse(s: &str) -> Mode {
|
|
43
|
-
match s {
|
|
44
|
-
"reject" => Mode::Reject,
|
|
45
|
-
"malformed-headers" => Mode::MalformedHeaders,
|
|
46
|
-
"garbage" => Mode::Garbage,
|
|
47
|
-
"reset" => Mode::ResetStreams,
|
|
48
|
-
"close" => Mode::CloseSession,
|
|
49
|
-
_ => Mode::Echo,
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const CONNECT_ID: u64 = 0;
|
|
55
|
-
|
|
56
|
-
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
57
|
-
enum Role {
|
|
58
|
-
LocalControlPlane,
|
|
59
|
-
PeerControlPlane, // client control/qpack uni streams — drain
|
|
60
|
-
Connect, // client CONNECT bidi (id 0)
|
|
61
|
-
WtBidi, // client bidi WT stream — echo on the same stream
|
|
62
|
-
WtUniIn, // client uni WT stream — echo via a new server uni stream
|
|
63
|
-
PendingUni, // client uni, type varint not yet read
|
|
64
|
-
Ignored,
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
struct Stream {
|
|
68
|
-
role: Role,
|
|
69
|
-
class_buf: Vec<u8>,
|
|
70
|
-
frames: FrameBuf,
|
|
71
|
-
out: Vec<u8>,
|
|
72
|
-
out_off: usize,
|
|
73
|
-
fin_queued: bool,
|
|
74
|
-
fin_sent: bool,
|
|
75
|
-
prefix_done: bool,
|
|
76
|
-
reset_queued: bool,
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
impl Stream {
|
|
80
|
-
fn new(role: Role) -> Self {
|
|
81
|
-
Self {
|
|
82
|
-
role,
|
|
83
|
-
class_buf: Vec::new(),
|
|
84
|
-
frames: FrameBuf::default(),
|
|
85
|
-
out: Vec::new(),
|
|
86
|
-
out_off: 0,
|
|
87
|
-
fin_queued: false,
|
|
88
|
-
fin_sent: false,
|
|
89
|
-
prefix_done: false,
|
|
90
|
-
reset_queued: false,
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
fn with_prefix(role: Role, prefix: Vec<u8>) -> Self {
|
|
94
|
-
let mut s = Self::new(role);
|
|
95
|
-
s.out = prefix;
|
|
96
|
-
s
|
|
97
|
-
}
|
|
98
|
-
fn queue(&mut self, data: &[u8]) {
|
|
99
|
-
self.out.extend_from_slice(data);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
#[derive(Default)]
|
|
104
|
-
struct FrameBuf {
|
|
105
|
-
buf: Vec<u8>,
|
|
106
|
-
}
|
|
107
|
-
impl FrameBuf {
|
|
108
|
-
fn push(&mut self, d: &[u8]) {
|
|
109
|
-
self.buf.extend_from_slice(d);
|
|
110
|
-
}
|
|
111
|
-
fn next(&mut self) -> Option<(u64, Vec<u8>)> {
|
|
112
|
-
let (ty, n1) = h3::read_varint(&self.buf)?;
|
|
113
|
-
let (len, n2) = h3::read_varint(&self.buf[n1..])?;
|
|
114
|
-
let len = len as usize;
|
|
115
|
-
if len > (1 << 20) {
|
|
116
|
-
return None;
|
|
117
|
-
}
|
|
118
|
-
let total = n1 + n2 + len;
|
|
119
|
-
if self.buf.len() < total {
|
|
120
|
-
return None;
|
|
121
|
-
}
|
|
122
|
-
let payload = self.buf[n1 + n2..total].to_vec();
|
|
123
|
-
self.buf.drain(..total);
|
|
124
|
-
Some((ty, payload))
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
struct Server {
|
|
129
|
-
conn: quiche::Connection,
|
|
130
|
-
streams: HashMap<u64, Stream>,
|
|
131
|
-
setup_done: bool,
|
|
132
|
-
connected: bool,
|
|
133
|
-
next_server_uni: u64,
|
|
134
|
-
established_logged: bool,
|
|
135
|
-
mode: Mode,
|
|
136
|
-
adversary_done: bool,
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
impl Server {
|
|
140
|
-
fn new(conn: quiche::Connection, mode: Mode) -> Self {
|
|
141
|
-
Self {
|
|
142
|
-
conn,
|
|
143
|
-
streams: HashMap::new(),
|
|
144
|
-
setup_done: false,
|
|
145
|
-
connected: false,
|
|
146
|
-
next_server_uni: 15, // 3/7/11 are control/qpack; WT uni starts at 15
|
|
147
|
-
established_logged: false,
|
|
148
|
-
mode,
|
|
149
|
-
adversary_done: false,
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
fn on_established(&mut self) {
|
|
154
|
-
if self.setup_done {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
self.setup_done = true;
|
|
158
|
-
self.streams.insert(
|
|
159
|
-
3,
|
|
160
|
-
Stream::with_prefix(Role::LocalControlPlane, h3::control_stream_prefix()),
|
|
161
|
-
);
|
|
162
|
-
let mut enc = Vec::new();
|
|
163
|
-
h3::put_varint(h3::QPACK_ENCODER_STREAM_TYPE, &mut enc);
|
|
164
|
-
self.streams
|
|
165
|
-
.insert(7, Stream::with_prefix(Role::LocalControlPlane, enc));
|
|
166
|
-
let mut dec = Vec::new();
|
|
167
|
-
h3::put_varint(h3::QPACK_DECODER_STREAM_TYPE, &mut dec);
|
|
168
|
-
self.streams
|
|
169
|
-
.insert(11, Stream::with_prefix(Role::LocalControlPlane, dec));
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
fn drive(&mut self) {
|
|
173
|
-
if self.conn.is_established() {
|
|
174
|
-
self.on_established();
|
|
175
|
-
}
|
|
176
|
-
// Datagrams: echo back verbatim.
|
|
177
|
-
let mut dbuf = [0u8; 65_536];
|
|
178
|
-
while let Ok(n) = self.conn.dgram_recv(&mut dbuf) {
|
|
179
|
-
let data = dbuf[..n].to_vec();
|
|
180
|
-
let _ = self.conn.dgram_send(&data);
|
|
181
|
-
}
|
|
182
|
-
// Readable streams.
|
|
183
|
-
let readable: Vec<u64> = self.conn.readable().collect();
|
|
184
|
-
if std::env::var("RWT_DEBUG").is_ok() && !readable.is_empty() {
|
|
185
|
-
eprintln!("[echo] readable streams: {readable:?}");
|
|
186
|
-
}
|
|
187
|
-
for id in readable {
|
|
188
|
-
self.on_readable(id);
|
|
189
|
-
}
|
|
190
|
-
self.adversary();
|
|
191
|
-
self.flush();
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
fn on_readable(&mut self, id: u64) {
|
|
195
|
-
self.streams
|
|
196
|
-
.entry(id)
|
|
197
|
-
.or_insert_with(|| Stream::new(classify(id)));
|
|
198
|
-
let mut chunk = Vec::new();
|
|
199
|
-
let mut fin = false;
|
|
200
|
-
let mut buf = [0u8; 16 * 1024];
|
|
201
|
-
loop {
|
|
202
|
-
match self.conn.stream_recv(id, &mut buf) {
|
|
203
|
-
Ok((n, f)) => {
|
|
204
|
-
chunk.extend_from_slice(&buf[..n]);
|
|
205
|
-
if f {
|
|
206
|
-
fin = true;
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
if n == 0 {
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
Err(quiche::Error::Done) => break,
|
|
214
|
-
Err(_) => break,
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
if std::env::var("RWT_DEBUG").is_ok() {
|
|
218
|
-
eprintln!("[echo] readable id={id} chunk={} fin={fin}", chunk.len());
|
|
219
|
-
}
|
|
220
|
-
self.process(id, chunk, fin);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
fn process(&mut self, id: u64, chunk: Vec<u8>, fin: bool) {
|
|
224
|
-
let role = self.streams.get(&id).map(|s| s.role);
|
|
225
|
-
match role {
|
|
226
|
-
Some(Role::PeerControlPlane)
|
|
227
|
-
| Some(Role::LocalControlPlane)
|
|
228
|
-
| Some(Role::Ignored)
|
|
229
|
-
| None => {}
|
|
230
|
-
Some(Role::Connect) => {
|
|
231
|
-
if let Some(s) = self.streams.get_mut(&id) {
|
|
232
|
-
s.frames.push(&chunk);
|
|
233
|
-
}
|
|
234
|
-
self.handle_connect(id);
|
|
235
|
-
}
|
|
236
|
-
Some(Role::WtBidi) => {
|
|
237
|
-
// Strip the 0x41 + session prefix once, then echo the rest.
|
|
238
|
-
let data = self.strip_prefix(id, chunk, h3::WT_BIDI_FRAME_TYPE);
|
|
239
|
-
let reset = self.mode == Mode::ResetStreams;
|
|
240
|
-
if let Some(s) = self.streams.get_mut(&id) {
|
|
241
|
-
if !data.is_empty() {
|
|
242
|
-
s.queue(&data);
|
|
243
|
-
}
|
|
244
|
-
if fin {
|
|
245
|
-
if reset {
|
|
246
|
-
s.reset_queued = true;
|
|
247
|
-
} else {
|
|
248
|
-
s.fin_queued = true;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
Some(Role::WtUniIn) => {
|
|
254
|
-
let data = self.strip_prefix(id, chunk, h3::WT_UNI_STREAM_TYPE);
|
|
255
|
-
if !data.is_empty() || fin {
|
|
256
|
-
self.echo_uni(&data, fin);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
Some(Role::PendingUni) => {
|
|
260
|
-
self.resolve_uni(id, chunk, fin);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/// Consume the `signal + session-id` prefix on a WT stream the first time,
|
|
266
|
-
/// returning the application bytes that follow.
|
|
267
|
-
fn strip_prefix(&mut self, id: u64, chunk: Vec<u8>, _signal: u64) -> Vec<u8> {
|
|
268
|
-
let s = self.streams.get_mut(&id).unwrap();
|
|
269
|
-
if s.prefix_done {
|
|
270
|
-
return chunk;
|
|
271
|
-
}
|
|
272
|
-
s.class_buf.extend_from_slice(&chunk);
|
|
273
|
-
let buf = s.class_buf.clone();
|
|
274
|
-
let Some((_sig, n1)) = h3::read_varint(&buf) else {
|
|
275
|
-
return Vec::new();
|
|
276
|
-
};
|
|
277
|
-
let Some((_sess, n2)) = h3::read_varint(&buf[n1..]) else {
|
|
278
|
-
return Vec::new();
|
|
279
|
-
};
|
|
280
|
-
s.prefix_done = true;
|
|
281
|
-
let rest = buf[n1 + n2..].to_vec();
|
|
282
|
-
s.class_buf.clear();
|
|
283
|
-
rest
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
fn resolve_uni(&mut self, id: u64, chunk: Vec<u8>, fin: bool) {
|
|
287
|
-
let buf = {
|
|
288
|
-
let s = self.streams.get_mut(&id).unwrap();
|
|
289
|
-
s.class_buf.extend_from_slice(&chunk);
|
|
290
|
-
s.class_buf.clone()
|
|
291
|
-
};
|
|
292
|
-
let Some((ty, n)) = h3::read_varint(&buf) else {
|
|
293
|
-
return;
|
|
294
|
-
};
|
|
295
|
-
match ty {
|
|
296
|
-
t if t == h3::H3_CONTROL_STREAM_TYPE
|
|
297
|
-
|| t == h3::QPACK_ENCODER_STREAM_TYPE
|
|
298
|
-
|| t == h3::QPACK_DECODER_STREAM_TYPE =>
|
|
299
|
-
{
|
|
300
|
-
let s = self.streams.get_mut(&id).unwrap();
|
|
301
|
-
s.role = Role::PeerControlPlane;
|
|
302
|
-
s.class_buf.clear();
|
|
303
|
-
}
|
|
304
|
-
t if t == h3::WT_UNI_STREAM_TYPE => {
|
|
305
|
-
// Read session id, then treat remaining as echo payload.
|
|
306
|
-
let Some((_sess, n2)) = h3::read_varint(&buf[n..]) else {
|
|
307
|
-
return;
|
|
308
|
-
};
|
|
309
|
-
let data = buf[n + n2..].to_vec();
|
|
310
|
-
{
|
|
311
|
-
let s = self.streams.get_mut(&id).unwrap();
|
|
312
|
-
s.role = Role::WtUniIn;
|
|
313
|
-
s.prefix_done = true;
|
|
314
|
-
s.class_buf.clear();
|
|
315
|
-
}
|
|
316
|
-
if !data.is_empty() || fin {
|
|
317
|
-
self.echo_uni(&data, fin);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
_ => {
|
|
321
|
-
let s = self.streams.get_mut(&id).unwrap();
|
|
322
|
-
s.role = Role::Ignored;
|
|
323
|
-
s.class_buf.clear();
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
fn echo_uni(&mut self, data: &[u8], fin: bool) {
|
|
329
|
-
// Open (or reuse) a server uni stream carrying the echoed bytes.
|
|
330
|
-
let id = self.next_server_uni;
|
|
331
|
-
self.streams.entry(id).or_insert_with(|| {
|
|
332
|
-
let mut prefix = Vec::new();
|
|
333
|
-
h3::put_varint(h3::WT_UNI_STREAM_TYPE, &mut prefix);
|
|
334
|
-
h3::put_varint(CONNECT_ID, &mut prefix);
|
|
335
|
-
Stream::with_prefix(Role::LocalControlPlane, prefix)
|
|
336
|
-
});
|
|
337
|
-
let s = self.streams.get_mut(&id).unwrap();
|
|
338
|
-
s.queue(data);
|
|
339
|
-
if fin {
|
|
340
|
-
s.fin_queued = true;
|
|
341
|
-
self.next_server_uni += 4; // finish this uni stream, next echo uses a new one
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
fn handle_connect(&mut self, id: u64) {
|
|
346
|
-
loop {
|
|
347
|
-
let frame = self.streams.get_mut(&id).and_then(|s| s.frames.next());
|
|
348
|
-
let Some((ty, payload)) = frame else { break };
|
|
349
|
-
if std::env::var("RWT_DEBUG").is_ok() {
|
|
350
|
-
eprintln!(
|
|
351
|
-
"[echo] connect stream {id} frame ty={ty} len={}",
|
|
352
|
-
payload.len()
|
|
353
|
-
);
|
|
354
|
-
}
|
|
355
|
-
if ty == h3::FRAME_HEADERS {
|
|
356
|
-
let valid = match h3::decode_header_block(&payload) {
|
|
357
|
-
Ok(headers) => headers
|
|
358
|
-
.iter()
|
|
359
|
-
.any(|hh| hh.name() == b":protocol" && hh.value() == b"webtransport"),
|
|
360
|
-
Err(_) => false,
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
// Adversarial: a deliberately malformed QPACK response block.
|
|
364
|
-
if self.mode == Mode::MalformedHeaders {
|
|
365
|
-
let mut framed = Vec::new();
|
|
366
|
-
h3::put_varint(h3::FRAME_HEADERS, &mut framed);
|
|
367
|
-
let garbage = [0xffu8, 0xff, 0xff, 0xff, 0xff, 0xff];
|
|
368
|
-
h3::put_varint(garbage.len() as u64, &mut framed);
|
|
369
|
-
framed.extend_from_slice(&garbage);
|
|
370
|
-
if let Some(s) = self.streams.get_mut(&id) {
|
|
371
|
-
s.queue(&framed);
|
|
372
|
-
s.fin_queued = true;
|
|
373
|
-
}
|
|
374
|
-
self.connected = false;
|
|
375
|
-
continue;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
let ok = valid && self.mode != Mode::Reject;
|
|
379
|
-
let status: &[u8] = if ok { b"200" } else { b"404" };
|
|
380
|
-
let resp = vec![quiche::h3::Header::new(b":status", status)];
|
|
381
|
-
if let Ok(frame) = h3::encode_headers_frame(&resp) {
|
|
382
|
-
if let Some(s) = self.streams.get_mut(&id) {
|
|
383
|
-
s.queue(&frame);
|
|
384
|
-
if !ok {
|
|
385
|
-
s.fin_queued = true;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
self.connected = ok;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
fn flush(&mut self) {
|
|
395
|
-
let ids: Vec<u64> = self.streams.keys().copied().collect();
|
|
396
|
-
for id in ids {
|
|
397
|
-
let s = self.streams.get_mut(&id).unwrap();
|
|
398
|
-
// Call stream_send directly (a stream doesn't exist until its first
|
|
399
|
-
// send, so stream_capacity would error on a not-yet-created stream).
|
|
400
|
-
while s.out_off < s.out.len() {
|
|
401
|
-
let remaining_len = s.out.len() - s.out_off;
|
|
402
|
-
match self.conn.stream_send(id, &s.out[s.out_off..], false) {
|
|
403
|
-
Ok(sent) => {
|
|
404
|
-
s.out_off += sent;
|
|
405
|
-
if sent < remaining_len {
|
|
406
|
-
break;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
Err(_) => break,
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
if s.reset_queued && !s.fin_sent {
|
|
413
|
-
let _ = self.conn.stream_shutdown(id, quiche::Shutdown::Write, 7);
|
|
414
|
-
s.fin_sent = true;
|
|
415
|
-
} else if s.out_off >= s.out.len()
|
|
416
|
-
&& s.fin_queued
|
|
417
|
-
&& !s.fin_sent
|
|
418
|
-
&& self.conn.stream_send(id, &[], true).is_ok()
|
|
419
|
-
{
|
|
420
|
-
s.fin_sent = true;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/// Fire the mode-specific hostile action once, after CONNECT succeeds.
|
|
426
|
-
fn adversary(&mut self) {
|
|
427
|
-
if self.adversary_done || !self.connected {
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
match self.mode {
|
|
431
|
-
Mode::Garbage => {
|
|
432
|
-
// Dump non-frame bytes on the CONNECT (session) stream. The client
|
|
433
|
-
// must survive this without crashing or hanging.
|
|
434
|
-
let garbage = [
|
|
435
|
-
0xffu8, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03,
|
|
436
|
-
];
|
|
437
|
-
if let Some(s) = self.streams.get_mut(&CONNECT_ID) {
|
|
438
|
-
s.queue(&garbage);
|
|
439
|
-
}
|
|
440
|
-
self.adversary_done = true;
|
|
441
|
-
}
|
|
442
|
-
Mode::CloseSession => {
|
|
443
|
-
// FIN the CONNECT stream: the WebTransport session ends.
|
|
444
|
-
if let Some(s) = self.streams.get_mut(&CONNECT_ID) {
|
|
445
|
-
s.fin_queued = true;
|
|
446
|
-
}
|
|
447
|
-
self.adversary_done = true;
|
|
448
|
-
}
|
|
449
|
-
_ => {}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
use quiche::h3::NameValue;
|
|
455
|
-
|
|
456
|
-
fn classify(id: u64) -> Role {
|
|
457
|
-
match id & 0x3 {
|
|
458
|
-
0x0 => {
|
|
459
|
-
if id == CONNECT_ID {
|
|
460
|
-
Role::Connect
|
|
461
|
-
} else {
|
|
462
|
-
Role::WtBidi
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
0x2 => Role::PendingUni, // client-initiated uni
|
|
466
|
-
_ => Role::Ignored,
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
fn main() {
|
|
471
|
-
let mut cert = String::new();
|
|
472
|
-
let mut key = String::new();
|
|
473
|
-
let mut host = "127.0.0.1".to_string();
|
|
474
|
-
let mut port: u16 = 0;
|
|
475
|
-
let mut mode = Mode::Echo;
|
|
476
|
-
|
|
477
|
-
let mut args = std::env::args().skip(1);
|
|
478
|
-
while let Some(a) = args.next() {
|
|
479
|
-
match a.as_str() {
|
|
480
|
-
"--cert" => cert = args.next().unwrap_or_default(),
|
|
481
|
-
"--key" => key = args.next().unwrap_or_default(),
|
|
482
|
-
"--host" => host = args.next().unwrap_or_else(|| "127.0.0.1".into()),
|
|
483
|
-
"--port" => port = args.next().and_then(|p| p.parse().ok()).unwrap_or(0),
|
|
484
|
-
"--mode" => mode = Mode::parse(&args.next().unwrap_or_default()),
|
|
485
|
-
_ => {}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
if cert.is_empty() || key.is_empty() {
|
|
489
|
-
eprintln!("usage: wt-echo-server --cert <pem> --key <pem> [--host H] [--port P]");
|
|
490
|
-
std::process::exit(2);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
let bind: SocketAddr = format!("{host}:{port}").parse().expect("bind addr");
|
|
494
|
-
let mut socket = UdpSocket::bind(bind).expect("bind udp");
|
|
495
|
-
let local_addr = socket.local_addr().expect("local addr");
|
|
496
|
-
|
|
497
|
-
let mut config = build_server_config(&cert, &key).expect("server config");
|
|
498
|
-
|
|
499
|
-
let mut poll = Poll::new().unwrap();
|
|
500
|
-
poll.registry()
|
|
501
|
-
.register(&mut socket, SOCKET, Interest::READABLE)
|
|
502
|
-
.unwrap();
|
|
503
|
-
let mut events = Events::with_capacity(1024);
|
|
504
|
-
|
|
505
|
-
// Machine-readable handshake for the test harness.
|
|
506
|
-
println!("PORT {}", local_addr.port());
|
|
507
|
-
println!("READY");
|
|
508
|
-
use std::io::Write;
|
|
509
|
-
std::io::stdout().flush().ok();
|
|
510
|
-
|
|
511
|
-
let debug = std::env::var("RWT_DEBUG").is_ok();
|
|
512
|
-
let mut clients: HashMap<Vec<u8>, Server> = HashMap::new();
|
|
513
|
-
let mut routing: HashMap<Vec<u8>, Vec<u8>> = HashMap::new();
|
|
514
|
-
let mut recv_buf = vec![0u8; 65_535];
|
|
515
|
-
let mut send_buf = vec![0u8; 1350];
|
|
516
|
-
|
|
517
|
-
loop {
|
|
518
|
-
let timeout = clients.values().filter_map(|c| c.conn.timeout()).min();
|
|
519
|
-
poll.poll(&mut events, timeout).ok();
|
|
520
|
-
|
|
521
|
-
// Read all datagrams.
|
|
522
|
-
loop {
|
|
523
|
-
let (len, from) = match socket.recv_from(&mut recv_buf) {
|
|
524
|
-
Ok(v) => v,
|
|
525
|
-
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
|
|
526
|
-
Err(_) => break,
|
|
527
|
-
};
|
|
528
|
-
let pkt = &mut recv_buf[..len];
|
|
529
|
-
let hdr = match quiche::Header::from_slice(pkt, LOCAL_CONN_ID_LEN) {
|
|
530
|
-
Ok(h) => h,
|
|
531
|
-
Err(_) => continue,
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
let key_scid = routing.get(&hdr.dcid.to_vec()).cloned();
|
|
535
|
-
let scid = match key_scid {
|
|
536
|
-
Some(s) => s,
|
|
537
|
-
None => {
|
|
538
|
-
if hdr.ty != quiche::Type::Initial {
|
|
539
|
-
continue;
|
|
540
|
-
}
|
|
541
|
-
let mut scid = [0u8; LOCAL_CONN_ID_LEN];
|
|
542
|
-
getrandom_fill(&mut scid);
|
|
543
|
-
let scid_id = quiche::ConnectionId::from_ref(&scid);
|
|
544
|
-
let conn = match quiche::accept(&scid_id, None, local_addr, from, &mut config) {
|
|
545
|
-
Ok(c) => c,
|
|
546
|
-
Err(_) => continue,
|
|
547
|
-
};
|
|
548
|
-
let key = scid.to_vec();
|
|
549
|
-
routing.insert(hdr.dcid.to_vec(), key.clone());
|
|
550
|
-
routing.insert(key.clone(), key.clone());
|
|
551
|
-
clients.insert(key.clone(), Server::new(conn, mode));
|
|
552
|
-
if debug {
|
|
553
|
-
eprintln!("[echo] new connection from {from} (type={:?})", hdr.ty);
|
|
554
|
-
}
|
|
555
|
-
key
|
|
556
|
-
}
|
|
557
|
-
};
|
|
558
|
-
|
|
559
|
-
if let Some(server) = clients.get_mut(&scid) {
|
|
560
|
-
let info = quiche::RecvInfo {
|
|
561
|
-
from,
|
|
562
|
-
to: local_addr,
|
|
563
|
-
};
|
|
564
|
-
match server.conn.recv(pkt, info) {
|
|
565
|
-
Ok(n) => {
|
|
566
|
-
if debug {
|
|
567
|
-
eprintln!("[echo] recv {len} bytes -> processed {n}");
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
Err(e) => {
|
|
571
|
-
if debug {
|
|
572
|
-
eprintln!("[echo] recv err {e:?}");
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
// Fire timeouts only for connections whose timer has actually elapsed.
|
|
580
|
-
for server in clients.values_mut() {
|
|
581
|
-
if server.conn.timeout() == Some(std::time::Duration::ZERO) {
|
|
582
|
-
server.conn.on_timeout();
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
// Drive each connection and flush.
|
|
587
|
-
for server in clients.values_mut() {
|
|
588
|
-
let est_before = server.established_logged;
|
|
589
|
-
server.drive();
|
|
590
|
-
if debug && !est_before && server.conn.is_established() {
|
|
591
|
-
server.established_logged = true;
|
|
592
|
-
eprintln!("[echo] connection established");
|
|
593
|
-
}
|
|
594
|
-
let mut sent = 0;
|
|
595
|
-
loop {
|
|
596
|
-
match server.conn.send(&mut send_buf) {
|
|
597
|
-
Ok((write, info)) => {
|
|
598
|
-
let _ = socket.send_to(&send_buf[..write], info.to);
|
|
599
|
-
sent += write;
|
|
600
|
-
}
|
|
601
|
-
Err(quiche::Error::Done) => break,
|
|
602
|
-
Err(_) => break,
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
if debug && sent > 0 {
|
|
606
|
-
eprintln!("[echo] sent {sent} bytes");
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
// Reap closed connections.
|
|
611
|
-
let before = clients.len();
|
|
612
|
-
clients.retain(|_, s| !s.conn.is_closed());
|
|
613
|
-
if debug && clients.len() < before {
|
|
614
|
-
eprintln!("[echo] reaped a closed connection");
|
|
615
|
-
}
|
|
616
|
-
routing.retain(|_, scid| clients.contains_key(scid));
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
fn build_server_config(cert: &str, key: &str) -> Result<quiche::Config, String> {
|
|
621
|
-
use boring::ssl::{SslContextBuilder, SslMethod, SslVersion};
|
|
622
|
-
let mut b = SslContextBuilder::new(SslMethod::tls()).map_err(|e| e.to_string())?;
|
|
623
|
-
b.set_min_proto_version(Some(SslVersion::TLS1_3))
|
|
624
|
-
.map_err(|e| e.to_string())?;
|
|
625
|
-
b.set_max_proto_version(Some(SslVersion::TLS1_3))
|
|
626
|
-
.map_err(|e| e.to_string())?;
|
|
627
|
-
|
|
628
|
-
let mut config = quiche::Config::with_boring_ssl_ctx_builder(quiche::PROTOCOL_VERSION, b)
|
|
629
|
-
.map_err(|e| format!("{e:?}"))?;
|
|
630
|
-
config
|
|
631
|
-
.load_cert_chain_from_pem_file(cert)
|
|
632
|
-
.map_err(|e| format!("cert: {e:?}"))?;
|
|
633
|
-
config
|
|
634
|
-
.load_priv_key_from_pem_file(key)
|
|
635
|
-
.map_err(|e| format!("key: {e:?}"))?;
|
|
636
|
-
config
|
|
637
|
-
.set_application_protos(&[b"h3"])
|
|
638
|
-
.map_err(|e| format!("{e:?}"))?;
|
|
639
|
-
config.set_max_idle_timeout(30_000);
|
|
640
|
-
config.set_max_recv_udp_payload_size(1350);
|
|
641
|
-
config.set_max_send_udp_payload_size(1350);
|
|
642
|
-
config.set_initial_max_data(10 * 1024 * 1024);
|
|
643
|
-
config.set_initial_max_stream_data_bidi_local(1024 * 1024);
|
|
644
|
-
config.set_initial_max_stream_data_bidi_remote(1024 * 1024);
|
|
645
|
-
config.set_initial_max_stream_data_uni(1024 * 1024);
|
|
646
|
-
config.set_initial_max_streams_bidi(256);
|
|
647
|
-
config.set_initial_max_streams_uni(256);
|
|
648
|
-
config.set_disable_active_migration(true);
|
|
649
|
-
config.enable_dgram(true, 65_536, 65_536);
|
|
650
|
-
Ok(config)
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
fn getrandom_fill(buf: &mut [u8]) {
|
|
654
|
-
// Cheap unique-ish CID source for a localhost test server.
|
|
655
|
-
use std::time::{SystemTime, UNIX_EPOCH};
|
|
656
|
-
let mut seed = SystemTime::now()
|
|
657
|
-
.duration_since(UNIX_EPOCH)
|
|
658
|
-
.unwrap()
|
|
659
|
-
.as_nanos() as u64;
|
|
660
|
-
for b in buf.iter_mut() {
|
|
661
|
-
seed = seed
|
|
662
|
-
.wrapping_mul(6364136223846793005)
|
|
663
|
-
.wrapping_add(1442695040888963407);
|
|
664
|
-
*b = (seed >> 33) as u8;
|
|
665
|
-
}
|
|
666
|
-
}
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
//! WebTransport **echo** server — a test fixture for the rwebtransport client.
|
|
3
|
+
//!
|
|
4
|
+
//! It speaks real QUIC/HTTP-3/WebTransport (Cloudflare quiche + BoringSSL,
|
|
5
|
+
//! server role) and echoes everything a client sends:
|
|
6
|
+
//! * bidirectional streams — bytes are written straight back on the same stream;
|
|
7
|
+
//! * unidirectional streams — the server opens a matching uni stream and echoes;
|
|
8
|
+
//! * datagrams — sent back verbatim.
|
|
9
|
+
//!
|
|
10
|
+
//! Usage: `wt-echo-server --cert <pem> --key <pem> [--host 127.0.0.1] [--port 0]`.
|
|
11
|
+
//! On startup it prints two machine-readable lines to stdout:
|
|
12
|
+
//! `PORT <n>` (the actually-bound UDP port) and `READY`.
|
|
13
|
+
|
|
14
|
+
use std::collections::HashMap;
|
|
15
|
+
use std::net::SocketAddr;
|
|
16
|
+
|
|
17
|
+
use mio::net::UdpSocket;
|
|
18
|
+
use mio::{Events, Interest, Poll, Token};
|
|
19
|
+
use wtcore::h3;
|
|
20
|
+
|
|
21
|
+
const LOCAL_CONN_ID_LEN: usize = 16;
|
|
22
|
+
const SOCKET: Token = Token(0);
|
|
23
|
+
|
|
24
|
+
/// Server behaviour, selectable via `--mode` for adversarial client tests.
|
|
25
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
26
|
+
enum Mode {
|
|
27
|
+
/// Echo everything (default).
|
|
28
|
+
Echo,
|
|
29
|
+
/// Reject the Extended CONNECT with `:status 404`.
|
|
30
|
+
Reject,
|
|
31
|
+
/// Answer CONNECT with a HEADERS frame carrying a malformed QPACK block.
|
|
32
|
+
MalformedHeaders,
|
|
33
|
+
/// After a normal CONNECT, dump random non-frame bytes on the CONNECT stream.
|
|
34
|
+
Garbage,
|
|
35
|
+
/// Echo bidi streams but RESET them instead of finishing cleanly.
|
|
36
|
+
ResetStreams,
|
|
37
|
+
/// Accept CONNECT, then immediately close the WebTransport session (FIN).
|
|
38
|
+
CloseSession,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
impl Mode {
|
|
42
|
+
fn parse(s: &str) -> Mode {
|
|
43
|
+
match s {
|
|
44
|
+
"reject" => Mode::Reject,
|
|
45
|
+
"malformed-headers" => Mode::MalformedHeaders,
|
|
46
|
+
"garbage" => Mode::Garbage,
|
|
47
|
+
"reset" => Mode::ResetStreams,
|
|
48
|
+
"close" => Mode::CloseSession,
|
|
49
|
+
_ => Mode::Echo,
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const CONNECT_ID: u64 = 0;
|
|
55
|
+
|
|
56
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
57
|
+
enum Role {
|
|
58
|
+
LocalControlPlane,
|
|
59
|
+
PeerControlPlane, // client control/qpack uni streams — drain
|
|
60
|
+
Connect, // client CONNECT bidi (id 0)
|
|
61
|
+
WtBidi, // client bidi WT stream — echo on the same stream
|
|
62
|
+
WtUniIn, // client uni WT stream — echo via a new server uni stream
|
|
63
|
+
PendingUni, // client uni, type varint not yet read
|
|
64
|
+
Ignored,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
struct Stream {
|
|
68
|
+
role: Role,
|
|
69
|
+
class_buf: Vec<u8>,
|
|
70
|
+
frames: FrameBuf,
|
|
71
|
+
out: Vec<u8>,
|
|
72
|
+
out_off: usize,
|
|
73
|
+
fin_queued: bool,
|
|
74
|
+
fin_sent: bool,
|
|
75
|
+
prefix_done: bool,
|
|
76
|
+
reset_queued: bool,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
impl Stream {
|
|
80
|
+
fn new(role: Role) -> Self {
|
|
81
|
+
Self {
|
|
82
|
+
role,
|
|
83
|
+
class_buf: Vec::new(),
|
|
84
|
+
frames: FrameBuf::default(),
|
|
85
|
+
out: Vec::new(),
|
|
86
|
+
out_off: 0,
|
|
87
|
+
fin_queued: false,
|
|
88
|
+
fin_sent: false,
|
|
89
|
+
prefix_done: false,
|
|
90
|
+
reset_queued: false,
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
fn with_prefix(role: Role, prefix: Vec<u8>) -> Self {
|
|
94
|
+
let mut s = Self::new(role);
|
|
95
|
+
s.out = prefix;
|
|
96
|
+
s
|
|
97
|
+
}
|
|
98
|
+
fn queue(&mut self, data: &[u8]) {
|
|
99
|
+
self.out.extend_from_slice(data);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#[derive(Default)]
|
|
104
|
+
struct FrameBuf {
|
|
105
|
+
buf: Vec<u8>,
|
|
106
|
+
}
|
|
107
|
+
impl FrameBuf {
|
|
108
|
+
fn push(&mut self, d: &[u8]) {
|
|
109
|
+
self.buf.extend_from_slice(d);
|
|
110
|
+
}
|
|
111
|
+
fn next(&mut self) -> Option<(u64, Vec<u8>)> {
|
|
112
|
+
let (ty, n1) = h3::read_varint(&self.buf)?;
|
|
113
|
+
let (len, n2) = h3::read_varint(&self.buf[n1..])?;
|
|
114
|
+
let len = len as usize;
|
|
115
|
+
if len > (1 << 20) {
|
|
116
|
+
return None;
|
|
117
|
+
}
|
|
118
|
+
let total = n1 + n2 + len;
|
|
119
|
+
if self.buf.len() < total {
|
|
120
|
+
return None;
|
|
121
|
+
}
|
|
122
|
+
let payload = self.buf[n1 + n2..total].to_vec();
|
|
123
|
+
self.buf.drain(..total);
|
|
124
|
+
Some((ty, payload))
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
struct Server {
|
|
129
|
+
conn: quiche::Connection,
|
|
130
|
+
streams: HashMap<u64, Stream>,
|
|
131
|
+
setup_done: bool,
|
|
132
|
+
connected: bool,
|
|
133
|
+
next_server_uni: u64,
|
|
134
|
+
established_logged: bool,
|
|
135
|
+
mode: Mode,
|
|
136
|
+
adversary_done: bool,
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
impl Server {
|
|
140
|
+
fn new(conn: quiche::Connection, mode: Mode) -> Self {
|
|
141
|
+
Self {
|
|
142
|
+
conn,
|
|
143
|
+
streams: HashMap::new(),
|
|
144
|
+
setup_done: false,
|
|
145
|
+
connected: false,
|
|
146
|
+
next_server_uni: 15, // 3/7/11 are control/qpack; WT uni starts at 15
|
|
147
|
+
established_logged: false,
|
|
148
|
+
mode,
|
|
149
|
+
adversary_done: false,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
fn on_established(&mut self) {
|
|
154
|
+
if self.setup_done {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
self.setup_done = true;
|
|
158
|
+
self.streams.insert(
|
|
159
|
+
3,
|
|
160
|
+
Stream::with_prefix(Role::LocalControlPlane, h3::control_stream_prefix()),
|
|
161
|
+
);
|
|
162
|
+
let mut enc = Vec::new();
|
|
163
|
+
h3::put_varint(h3::QPACK_ENCODER_STREAM_TYPE, &mut enc);
|
|
164
|
+
self.streams
|
|
165
|
+
.insert(7, Stream::with_prefix(Role::LocalControlPlane, enc));
|
|
166
|
+
let mut dec = Vec::new();
|
|
167
|
+
h3::put_varint(h3::QPACK_DECODER_STREAM_TYPE, &mut dec);
|
|
168
|
+
self.streams
|
|
169
|
+
.insert(11, Stream::with_prefix(Role::LocalControlPlane, dec));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
fn drive(&mut self) {
|
|
173
|
+
if self.conn.is_established() {
|
|
174
|
+
self.on_established();
|
|
175
|
+
}
|
|
176
|
+
// Datagrams: echo back verbatim.
|
|
177
|
+
let mut dbuf = [0u8; 65_536];
|
|
178
|
+
while let Ok(n) = self.conn.dgram_recv(&mut dbuf) {
|
|
179
|
+
let data = dbuf[..n].to_vec();
|
|
180
|
+
let _ = self.conn.dgram_send(&data);
|
|
181
|
+
}
|
|
182
|
+
// Readable streams.
|
|
183
|
+
let readable: Vec<u64> = self.conn.readable().collect();
|
|
184
|
+
if std::env::var("RWT_DEBUG").is_ok() && !readable.is_empty() {
|
|
185
|
+
eprintln!("[echo] readable streams: {readable:?}");
|
|
186
|
+
}
|
|
187
|
+
for id in readable {
|
|
188
|
+
self.on_readable(id);
|
|
189
|
+
}
|
|
190
|
+
self.adversary();
|
|
191
|
+
self.flush();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
fn on_readable(&mut self, id: u64) {
|
|
195
|
+
self.streams
|
|
196
|
+
.entry(id)
|
|
197
|
+
.or_insert_with(|| Stream::new(classify(id)));
|
|
198
|
+
let mut chunk = Vec::new();
|
|
199
|
+
let mut fin = false;
|
|
200
|
+
let mut buf = [0u8; 16 * 1024];
|
|
201
|
+
loop {
|
|
202
|
+
match self.conn.stream_recv(id, &mut buf) {
|
|
203
|
+
Ok((n, f)) => {
|
|
204
|
+
chunk.extend_from_slice(&buf[..n]);
|
|
205
|
+
if f {
|
|
206
|
+
fin = true;
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
if n == 0 {
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
Err(quiche::Error::Done) => break,
|
|
214
|
+
Err(_) => break,
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if std::env::var("RWT_DEBUG").is_ok() {
|
|
218
|
+
eprintln!("[echo] readable id={id} chunk={} fin={fin}", chunk.len());
|
|
219
|
+
}
|
|
220
|
+
self.process(id, chunk, fin);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
fn process(&mut self, id: u64, chunk: Vec<u8>, fin: bool) {
|
|
224
|
+
let role = self.streams.get(&id).map(|s| s.role);
|
|
225
|
+
match role {
|
|
226
|
+
Some(Role::PeerControlPlane)
|
|
227
|
+
| Some(Role::LocalControlPlane)
|
|
228
|
+
| Some(Role::Ignored)
|
|
229
|
+
| None => {}
|
|
230
|
+
Some(Role::Connect) => {
|
|
231
|
+
if let Some(s) = self.streams.get_mut(&id) {
|
|
232
|
+
s.frames.push(&chunk);
|
|
233
|
+
}
|
|
234
|
+
self.handle_connect(id);
|
|
235
|
+
}
|
|
236
|
+
Some(Role::WtBidi) => {
|
|
237
|
+
// Strip the 0x41 + session prefix once, then echo the rest.
|
|
238
|
+
let data = self.strip_prefix(id, chunk, h3::WT_BIDI_FRAME_TYPE);
|
|
239
|
+
let reset = self.mode == Mode::ResetStreams;
|
|
240
|
+
if let Some(s) = self.streams.get_mut(&id) {
|
|
241
|
+
if !data.is_empty() {
|
|
242
|
+
s.queue(&data);
|
|
243
|
+
}
|
|
244
|
+
if fin {
|
|
245
|
+
if reset {
|
|
246
|
+
s.reset_queued = true;
|
|
247
|
+
} else {
|
|
248
|
+
s.fin_queued = true;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
Some(Role::WtUniIn) => {
|
|
254
|
+
let data = self.strip_prefix(id, chunk, h3::WT_UNI_STREAM_TYPE);
|
|
255
|
+
if !data.is_empty() || fin {
|
|
256
|
+
self.echo_uni(&data, fin);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
Some(Role::PendingUni) => {
|
|
260
|
+
self.resolve_uni(id, chunk, fin);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/// Consume the `signal + session-id` prefix on a WT stream the first time,
|
|
266
|
+
/// returning the application bytes that follow.
|
|
267
|
+
fn strip_prefix(&mut self, id: u64, chunk: Vec<u8>, _signal: u64) -> Vec<u8> {
|
|
268
|
+
let s = self.streams.get_mut(&id).unwrap();
|
|
269
|
+
if s.prefix_done {
|
|
270
|
+
return chunk;
|
|
271
|
+
}
|
|
272
|
+
s.class_buf.extend_from_slice(&chunk);
|
|
273
|
+
let buf = s.class_buf.clone();
|
|
274
|
+
let Some((_sig, n1)) = h3::read_varint(&buf) else {
|
|
275
|
+
return Vec::new();
|
|
276
|
+
};
|
|
277
|
+
let Some((_sess, n2)) = h3::read_varint(&buf[n1..]) else {
|
|
278
|
+
return Vec::new();
|
|
279
|
+
};
|
|
280
|
+
s.prefix_done = true;
|
|
281
|
+
let rest = buf[n1 + n2..].to_vec();
|
|
282
|
+
s.class_buf.clear();
|
|
283
|
+
rest
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
fn resolve_uni(&mut self, id: u64, chunk: Vec<u8>, fin: bool) {
|
|
287
|
+
let buf = {
|
|
288
|
+
let s = self.streams.get_mut(&id).unwrap();
|
|
289
|
+
s.class_buf.extend_from_slice(&chunk);
|
|
290
|
+
s.class_buf.clone()
|
|
291
|
+
};
|
|
292
|
+
let Some((ty, n)) = h3::read_varint(&buf) else {
|
|
293
|
+
return;
|
|
294
|
+
};
|
|
295
|
+
match ty {
|
|
296
|
+
t if t == h3::H3_CONTROL_STREAM_TYPE
|
|
297
|
+
|| t == h3::QPACK_ENCODER_STREAM_TYPE
|
|
298
|
+
|| t == h3::QPACK_DECODER_STREAM_TYPE =>
|
|
299
|
+
{
|
|
300
|
+
let s = self.streams.get_mut(&id).unwrap();
|
|
301
|
+
s.role = Role::PeerControlPlane;
|
|
302
|
+
s.class_buf.clear();
|
|
303
|
+
}
|
|
304
|
+
t if t == h3::WT_UNI_STREAM_TYPE => {
|
|
305
|
+
// Read session id, then treat remaining as echo payload.
|
|
306
|
+
let Some((_sess, n2)) = h3::read_varint(&buf[n..]) else {
|
|
307
|
+
return;
|
|
308
|
+
};
|
|
309
|
+
let data = buf[n + n2..].to_vec();
|
|
310
|
+
{
|
|
311
|
+
let s = self.streams.get_mut(&id).unwrap();
|
|
312
|
+
s.role = Role::WtUniIn;
|
|
313
|
+
s.prefix_done = true;
|
|
314
|
+
s.class_buf.clear();
|
|
315
|
+
}
|
|
316
|
+
if !data.is_empty() || fin {
|
|
317
|
+
self.echo_uni(&data, fin);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
_ => {
|
|
321
|
+
let s = self.streams.get_mut(&id).unwrap();
|
|
322
|
+
s.role = Role::Ignored;
|
|
323
|
+
s.class_buf.clear();
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
fn echo_uni(&mut self, data: &[u8], fin: bool) {
|
|
329
|
+
// Open (or reuse) a server uni stream carrying the echoed bytes.
|
|
330
|
+
let id = self.next_server_uni;
|
|
331
|
+
self.streams.entry(id).or_insert_with(|| {
|
|
332
|
+
let mut prefix = Vec::new();
|
|
333
|
+
h3::put_varint(h3::WT_UNI_STREAM_TYPE, &mut prefix);
|
|
334
|
+
h3::put_varint(CONNECT_ID, &mut prefix);
|
|
335
|
+
Stream::with_prefix(Role::LocalControlPlane, prefix)
|
|
336
|
+
});
|
|
337
|
+
let s = self.streams.get_mut(&id).unwrap();
|
|
338
|
+
s.queue(data);
|
|
339
|
+
if fin {
|
|
340
|
+
s.fin_queued = true;
|
|
341
|
+
self.next_server_uni += 4; // finish this uni stream, next echo uses a new one
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
fn handle_connect(&mut self, id: u64) {
|
|
346
|
+
loop {
|
|
347
|
+
let frame = self.streams.get_mut(&id).and_then(|s| s.frames.next());
|
|
348
|
+
let Some((ty, payload)) = frame else { break };
|
|
349
|
+
if std::env::var("RWT_DEBUG").is_ok() {
|
|
350
|
+
eprintln!(
|
|
351
|
+
"[echo] connect stream {id} frame ty={ty} len={}",
|
|
352
|
+
payload.len()
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
if ty == h3::FRAME_HEADERS {
|
|
356
|
+
let valid = match h3::decode_header_block(&payload) {
|
|
357
|
+
Ok(headers) => headers
|
|
358
|
+
.iter()
|
|
359
|
+
.any(|hh| hh.name() == b":protocol" && hh.value() == b"webtransport"),
|
|
360
|
+
Err(_) => false,
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
// Adversarial: a deliberately malformed QPACK response block.
|
|
364
|
+
if self.mode == Mode::MalformedHeaders {
|
|
365
|
+
let mut framed = Vec::new();
|
|
366
|
+
h3::put_varint(h3::FRAME_HEADERS, &mut framed);
|
|
367
|
+
let garbage = [0xffu8, 0xff, 0xff, 0xff, 0xff, 0xff];
|
|
368
|
+
h3::put_varint(garbage.len() as u64, &mut framed);
|
|
369
|
+
framed.extend_from_slice(&garbage);
|
|
370
|
+
if let Some(s) = self.streams.get_mut(&id) {
|
|
371
|
+
s.queue(&framed);
|
|
372
|
+
s.fin_queued = true;
|
|
373
|
+
}
|
|
374
|
+
self.connected = false;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
let ok = valid && self.mode != Mode::Reject;
|
|
379
|
+
let status: &[u8] = if ok { b"200" } else { b"404" };
|
|
380
|
+
let resp = vec![quiche::h3::Header::new(b":status", status)];
|
|
381
|
+
if let Ok(frame) = h3::encode_headers_frame(&resp) {
|
|
382
|
+
if let Some(s) = self.streams.get_mut(&id) {
|
|
383
|
+
s.queue(&frame);
|
|
384
|
+
if !ok {
|
|
385
|
+
s.fin_queued = true;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
self.connected = ok;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
fn flush(&mut self) {
|
|
395
|
+
let ids: Vec<u64> = self.streams.keys().copied().collect();
|
|
396
|
+
for id in ids {
|
|
397
|
+
let s = self.streams.get_mut(&id).unwrap();
|
|
398
|
+
// Call stream_send directly (a stream doesn't exist until its first
|
|
399
|
+
// send, so stream_capacity would error on a not-yet-created stream).
|
|
400
|
+
while s.out_off < s.out.len() {
|
|
401
|
+
let remaining_len = s.out.len() - s.out_off;
|
|
402
|
+
match self.conn.stream_send(id, &s.out[s.out_off..], false) {
|
|
403
|
+
Ok(sent) => {
|
|
404
|
+
s.out_off += sent;
|
|
405
|
+
if sent < remaining_len {
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
Err(_) => break,
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if s.reset_queued && !s.fin_sent {
|
|
413
|
+
let _ = self.conn.stream_shutdown(id, quiche::Shutdown::Write, 7);
|
|
414
|
+
s.fin_sent = true;
|
|
415
|
+
} else if s.out_off >= s.out.len()
|
|
416
|
+
&& s.fin_queued
|
|
417
|
+
&& !s.fin_sent
|
|
418
|
+
&& self.conn.stream_send(id, &[], true).is_ok()
|
|
419
|
+
{
|
|
420
|
+
s.fin_sent = true;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/// Fire the mode-specific hostile action once, after CONNECT succeeds.
|
|
426
|
+
fn adversary(&mut self) {
|
|
427
|
+
if self.adversary_done || !self.connected {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
match self.mode {
|
|
431
|
+
Mode::Garbage => {
|
|
432
|
+
// Dump non-frame bytes on the CONNECT (session) stream. The client
|
|
433
|
+
// must survive this without crashing or hanging.
|
|
434
|
+
let garbage = [
|
|
435
|
+
0xffu8, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03,
|
|
436
|
+
];
|
|
437
|
+
if let Some(s) = self.streams.get_mut(&CONNECT_ID) {
|
|
438
|
+
s.queue(&garbage);
|
|
439
|
+
}
|
|
440
|
+
self.adversary_done = true;
|
|
441
|
+
}
|
|
442
|
+
Mode::CloseSession => {
|
|
443
|
+
// FIN the CONNECT stream: the WebTransport session ends.
|
|
444
|
+
if let Some(s) = self.streams.get_mut(&CONNECT_ID) {
|
|
445
|
+
s.fin_queued = true;
|
|
446
|
+
}
|
|
447
|
+
self.adversary_done = true;
|
|
448
|
+
}
|
|
449
|
+
_ => {}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
use quiche::h3::NameValue;
|
|
455
|
+
|
|
456
|
+
fn classify(id: u64) -> Role {
|
|
457
|
+
match id & 0x3 {
|
|
458
|
+
0x0 => {
|
|
459
|
+
if id == CONNECT_ID {
|
|
460
|
+
Role::Connect
|
|
461
|
+
} else {
|
|
462
|
+
Role::WtBidi
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
0x2 => Role::PendingUni, // client-initiated uni
|
|
466
|
+
_ => Role::Ignored,
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
fn main() {
|
|
471
|
+
let mut cert = String::new();
|
|
472
|
+
let mut key = String::new();
|
|
473
|
+
let mut host = "127.0.0.1".to_string();
|
|
474
|
+
let mut port: u16 = 0;
|
|
475
|
+
let mut mode = Mode::Echo;
|
|
476
|
+
|
|
477
|
+
let mut args = std::env::args().skip(1);
|
|
478
|
+
while let Some(a) = args.next() {
|
|
479
|
+
match a.as_str() {
|
|
480
|
+
"--cert" => cert = args.next().unwrap_or_default(),
|
|
481
|
+
"--key" => key = args.next().unwrap_or_default(),
|
|
482
|
+
"--host" => host = args.next().unwrap_or_else(|| "127.0.0.1".into()),
|
|
483
|
+
"--port" => port = args.next().and_then(|p| p.parse().ok()).unwrap_or(0),
|
|
484
|
+
"--mode" => mode = Mode::parse(&args.next().unwrap_or_default()),
|
|
485
|
+
_ => {}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if cert.is_empty() || key.is_empty() {
|
|
489
|
+
eprintln!("usage: wt-echo-server --cert <pem> --key <pem> [--host H] [--port P]");
|
|
490
|
+
std::process::exit(2);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
let bind: SocketAddr = format!("{host}:{port}").parse().expect("bind addr");
|
|
494
|
+
let mut socket = UdpSocket::bind(bind).expect("bind udp");
|
|
495
|
+
let local_addr = socket.local_addr().expect("local addr");
|
|
496
|
+
|
|
497
|
+
let mut config = build_server_config(&cert, &key).expect("server config");
|
|
498
|
+
|
|
499
|
+
let mut poll = Poll::new().unwrap();
|
|
500
|
+
poll.registry()
|
|
501
|
+
.register(&mut socket, SOCKET, Interest::READABLE)
|
|
502
|
+
.unwrap();
|
|
503
|
+
let mut events = Events::with_capacity(1024);
|
|
504
|
+
|
|
505
|
+
// Machine-readable handshake for the test harness.
|
|
506
|
+
println!("PORT {}", local_addr.port());
|
|
507
|
+
println!("READY");
|
|
508
|
+
use std::io::Write;
|
|
509
|
+
std::io::stdout().flush().ok();
|
|
510
|
+
|
|
511
|
+
let debug = std::env::var("RWT_DEBUG").is_ok();
|
|
512
|
+
let mut clients: HashMap<Vec<u8>, Server> = HashMap::new();
|
|
513
|
+
let mut routing: HashMap<Vec<u8>, Vec<u8>> = HashMap::new();
|
|
514
|
+
let mut recv_buf = vec![0u8; 65_535];
|
|
515
|
+
let mut send_buf = vec![0u8; 1350];
|
|
516
|
+
|
|
517
|
+
loop {
|
|
518
|
+
let timeout = clients.values().filter_map(|c| c.conn.timeout()).min();
|
|
519
|
+
poll.poll(&mut events, timeout).ok();
|
|
520
|
+
|
|
521
|
+
// Read all datagrams.
|
|
522
|
+
loop {
|
|
523
|
+
let (len, from) = match socket.recv_from(&mut recv_buf) {
|
|
524
|
+
Ok(v) => v,
|
|
525
|
+
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
|
|
526
|
+
Err(_) => break,
|
|
527
|
+
};
|
|
528
|
+
let pkt = &mut recv_buf[..len];
|
|
529
|
+
let hdr = match quiche::Header::from_slice(pkt, LOCAL_CONN_ID_LEN) {
|
|
530
|
+
Ok(h) => h,
|
|
531
|
+
Err(_) => continue,
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
let key_scid = routing.get(&hdr.dcid.to_vec()).cloned();
|
|
535
|
+
let scid = match key_scid {
|
|
536
|
+
Some(s) => s,
|
|
537
|
+
None => {
|
|
538
|
+
if hdr.ty != quiche::Type::Initial {
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
let mut scid = [0u8; LOCAL_CONN_ID_LEN];
|
|
542
|
+
getrandom_fill(&mut scid);
|
|
543
|
+
let scid_id = quiche::ConnectionId::from_ref(&scid);
|
|
544
|
+
let conn = match quiche::accept(&scid_id, None, local_addr, from, &mut config) {
|
|
545
|
+
Ok(c) => c,
|
|
546
|
+
Err(_) => continue,
|
|
547
|
+
};
|
|
548
|
+
let key = scid.to_vec();
|
|
549
|
+
routing.insert(hdr.dcid.to_vec(), key.clone());
|
|
550
|
+
routing.insert(key.clone(), key.clone());
|
|
551
|
+
clients.insert(key.clone(), Server::new(conn, mode));
|
|
552
|
+
if debug {
|
|
553
|
+
eprintln!("[echo] new connection from {from} (type={:?})", hdr.ty);
|
|
554
|
+
}
|
|
555
|
+
key
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
if let Some(server) = clients.get_mut(&scid) {
|
|
560
|
+
let info = quiche::RecvInfo {
|
|
561
|
+
from,
|
|
562
|
+
to: local_addr,
|
|
563
|
+
};
|
|
564
|
+
match server.conn.recv(pkt, info) {
|
|
565
|
+
Ok(n) => {
|
|
566
|
+
if debug {
|
|
567
|
+
eprintln!("[echo] recv {len} bytes -> processed {n}");
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
Err(e) => {
|
|
571
|
+
if debug {
|
|
572
|
+
eprintln!("[echo] recv err {e:?}");
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// Fire timeouts only for connections whose timer has actually elapsed.
|
|
580
|
+
for server in clients.values_mut() {
|
|
581
|
+
if server.conn.timeout() == Some(std::time::Duration::ZERO) {
|
|
582
|
+
server.conn.on_timeout();
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Drive each connection and flush.
|
|
587
|
+
for server in clients.values_mut() {
|
|
588
|
+
let est_before = server.established_logged;
|
|
589
|
+
server.drive();
|
|
590
|
+
if debug && !est_before && server.conn.is_established() {
|
|
591
|
+
server.established_logged = true;
|
|
592
|
+
eprintln!("[echo] connection established");
|
|
593
|
+
}
|
|
594
|
+
let mut sent = 0;
|
|
595
|
+
loop {
|
|
596
|
+
match server.conn.send(&mut send_buf) {
|
|
597
|
+
Ok((write, info)) => {
|
|
598
|
+
let _ = socket.send_to(&send_buf[..write], info.to);
|
|
599
|
+
sent += write;
|
|
600
|
+
}
|
|
601
|
+
Err(quiche::Error::Done) => break,
|
|
602
|
+
Err(_) => break,
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
if debug && sent > 0 {
|
|
606
|
+
eprintln!("[echo] sent {sent} bytes");
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// Reap closed connections.
|
|
611
|
+
let before = clients.len();
|
|
612
|
+
clients.retain(|_, s| !s.conn.is_closed());
|
|
613
|
+
if debug && clients.len() < before {
|
|
614
|
+
eprintln!("[echo] reaped a closed connection");
|
|
615
|
+
}
|
|
616
|
+
routing.retain(|_, scid| clients.contains_key(scid));
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
fn build_server_config(cert: &str, key: &str) -> Result<quiche::Config, String> {
|
|
621
|
+
use boring::ssl::{SslContextBuilder, SslMethod, SslVersion};
|
|
622
|
+
let mut b = SslContextBuilder::new(SslMethod::tls()).map_err(|e| e.to_string())?;
|
|
623
|
+
b.set_min_proto_version(Some(SslVersion::TLS1_3))
|
|
624
|
+
.map_err(|e| e.to_string())?;
|
|
625
|
+
b.set_max_proto_version(Some(SslVersion::TLS1_3))
|
|
626
|
+
.map_err(|e| e.to_string())?;
|
|
627
|
+
|
|
628
|
+
let mut config = quiche::Config::with_boring_ssl_ctx_builder(quiche::PROTOCOL_VERSION, b)
|
|
629
|
+
.map_err(|e| format!("{e:?}"))?;
|
|
630
|
+
config
|
|
631
|
+
.load_cert_chain_from_pem_file(cert)
|
|
632
|
+
.map_err(|e| format!("cert: {e:?}"))?;
|
|
633
|
+
config
|
|
634
|
+
.load_priv_key_from_pem_file(key)
|
|
635
|
+
.map_err(|e| format!("key: {e:?}"))?;
|
|
636
|
+
config
|
|
637
|
+
.set_application_protos(&[b"h3"])
|
|
638
|
+
.map_err(|e| format!("{e:?}"))?;
|
|
639
|
+
config.set_max_idle_timeout(30_000);
|
|
640
|
+
config.set_max_recv_udp_payload_size(1350);
|
|
641
|
+
config.set_max_send_udp_payload_size(1350);
|
|
642
|
+
config.set_initial_max_data(10 * 1024 * 1024);
|
|
643
|
+
config.set_initial_max_stream_data_bidi_local(1024 * 1024);
|
|
644
|
+
config.set_initial_max_stream_data_bidi_remote(1024 * 1024);
|
|
645
|
+
config.set_initial_max_stream_data_uni(1024 * 1024);
|
|
646
|
+
config.set_initial_max_streams_bidi(256);
|
|
647
|
+
config.set_initial_max_streams_uni(256);
|
|
648
|
+
config.set_disable_active_migration(true);
|
|
649
|
+
config.enable_dgram(true, 65_536, 65_536);
|
|
650
|
+
Ok(config)
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
fn getrandom_fill(buf: &mut [u8]) {
|
|
654
|
+
// Cheap unique-ish CID source for a localhost test server.
|
|
655
|
+
use std::time::{SystemTime, UNIX_EPOCH};
|
|
656
|
+
let mut seed = SystemTime::now()
|
|
657
|
+
.duration_since(UNIX_EPOCH)
|
|
658
|
+
.unwrap()
|
|
659
|
+
.as_nanos() as u64;
|
|
660
|
+
for b in buf.iter_mut() {
|
|
661
|
+
seed = seed
|
|
662
|
+
.wrapping_mul(6364136223846793005)
|
|
663
|
+
.wrapping_add(1442695040888963407);
|
|
664
|
+
*b = (seed >> 33) as u8;
|
|
665
|
+
}
|
|
666
|
+
}
|