rwebtransport 0.1.0 → 0.1.2

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 (113) hide show
  1. package/Cargo.lock +666 -666
  2. package/Cargo.toml +31 -31
  3. package/LICENSE +202 -202
  4. package/NOTICE +41 -41
  5. package/README.md +550 -550
  6. package/crates/echo-server/Cargo.toml +20 -20
  7. package/crates/echo-server/src/main.rs +666 -666
  8. package/crates/native/Cargo.toml +23 -23
  9. package/crates/native/src/config.rs +129 -129
  10. package/crates/native/src/driver.rs +463 -463
  11. package/crates/native/src/lib.rs +821 -821
  12. package/crates/native/src/server.rs +718 -718
  13. package/crates/native/src/session.rs +1191 -1191
  14. package/crates/native/src/tls.rs +96 -96
  15. package/crates/wtcore/Cargo.toml +15 -15
  16. package/crates/wtcore/src/h3.rs +226 -226
  17. package/crates/wtcore/src/lib.rs +9 -9
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs.map +1 -1
  20. package/package.json +80 -79
  21. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  24. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  25. package/scripts/bench.mjs +180 -180
  26. package/scripts/build.js +67 -67
  27. package/scripts/postinstall.js +67 -67
  28. package/scripts/setup-unix.sh +129 -129
  29. package/scripts/setup-windows.ps1 +229 -229
  30. package/vendor/quiche/AGENTS.md +75 -75
  31. package/vendor/quiche/COPYING +23 -23
  32. package/vendor/quiche/Cargo.lock +1212 -1212
  33. package/vendor/quiche/Cargo.toml +215 -215
  34. package/vendor/quiche/README.md +466 -466
  35. package/vendor/quiche/examples/Makefile +46 -46
  36. package/vendor/quiche/examples/README.md +20 -20
  37. package/vendor/quiche/examples/cert-big.crt +90 -90
  38. package/vendor/quiche/examples/cert.crt +18 -18
  39. package/vendor/quiche/examples/cert.key +28 -28
  40. package/vendor/quiche/examples/client.c +343 -343
  41. package/vendor/quiche/examples/client.rs +281 -281
  42. package/vendor/quiche/examples/gen-certs.sh +15 -15
  43. package/vendor/quiche/examples/http3-client.c +473 -473
  44. package/vendor/quiche/examples/http3-client.rs +349 -349
  45. package/vendor/quiche/examples/http3-server.c +614 -614
  46. package/vendor/quiche/examples/http3-server.rs +680 -680
  47. package/vendor/quiche/examples/qpack-decode.rs +87 -87
  48. package/vendor/quiche/examples/qpack-encode.rs +88 -88
  49. package/vendor/quiche/examples/rootca.crt +21 -21
  50. package/vendor/quiche/examples/server.c +529 -529
  51. package/vendor/quiche/examples/server.rs +529 -529
  52. package/vendor/quiche/include/quiche.h +1282 -1282
  53. package/vendor/quiche/src/buffers.rs +105 -105
  54. package/vendor/quiche/src/build.rs +66 -66
  55. package/vendor/quiche/src/cid.rs +1079 -1079
  56. package/vendor/quiche/src/crypto/boringssl.rs +373 -373
  57. package/vendor/quiche/src/crypto/mod.rs +671 -671
  58. package/vendor/quiche/src/dgram.rs +118 -118
  59. package/vendor/quiche/src/error.rs +263 -263
  60. package/vendor/quiche/src/ffi.rs +2290 -2290
  61. package/vendor/quiche/src/flowcontrol.rs +246 -246
  62. package/vendor/quiche/src/frame.rs +2172 -2172
  63. package/vendor/quiche/src/h3/AGENTS.md +54 -54
  64. package/vendor/quiche/src/h3/ffi.rs +442 -442
  65. package/vendor/quiche/src/h3/frame.rs +1404 -1404
  66. package/vendor/quiche/src/h3/mod.rs +8223 -8223
  67. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
  68. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
  69. package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
  70. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
  71. package/vendor/quiche/src/h3/stream.rs +1595 -1595
  72. package/vendor/quiche/src/lib.rs +9556 -9556
  73. package/vendor/quiche/src/minmax.rs +446 -446
  74. package/vendor/quiche/src/packet.rs +2334 -2334
  75. package/vendor/quiche/src/path.rs +1374 -1374
  76. package/vendor/quiche/src/pmtud.rs +652 -652
  77. package/vendor/quiche/src/rand.rs +64 -64
  78. package/vendor/quiche/src/range_buf.rs +186 -186
  79. package/vendor/quiche/src/ranges.rs +755 -755
  80. package/vendor/quiche/src/recovery/AGENTS.md +80 -80
  81. package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
  82. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
  83. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
  84. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
  85. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
  86. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
  87. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
  88. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
  89. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
  90. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
  91. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
  92. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
  93. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
  94. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
  95. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
  96. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
  97. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
  98. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
  99. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
  100. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
  101. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
  102. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
  103. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
  104. package/vendor/quiche/src/recovery/mod.rs +3086 -3086
  105. package/vendor/quiche/src/recovery/rtt.rs +152 -152
  106. package/vendor/quiche/src/stream/mod.rs +2210 -2210
  107. package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
  108. package/vendor/quiche/src/stream/send_buf.rs +849 -849
  109. package/vendor/quiche/src/test_utils.rs +681 -681
  110. package/vendor/quiche/src/tests.rs +12922 -12922
  111. package/vendor/quiche/src/tls/boringssl.rs +375 -375
  112. package/vendor/quiche/src/tls/mod.rs +1245 -1245
  113. package/vendor/quiche/src/transport_params.rs +628 -628
@@ -1,614 +1,614 @@
1
- // Copyright (C) 2018-2019, Cloudflare, Inc.
2
- // All rights reserved.
3
- //
4
- // Redistribution and use in source and binary forms, with or without
5
- // modification, are permitted provided that the following conditions are
6
- // met:
7
- //
8
- // * Redistributions of source code must retain the above copyright
9
- // notice, this list of conditions and the following disclaimer.
10
- //
11
- // * Redistributions in binary form must reproduce the above copyright
12
- // notice, this list of conditions and the following disclaimer in the
13
- // documentation and/or other materials provided with the distribution.
14
- //
15
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
- // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
- // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
- // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
- // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
- // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
- // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
- // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
- // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
- // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
- // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
-
27
- #include <inttypes.h>
28
- #include <stdio.h>
29
- #include <stdlib.h>
30
- #include <stdint.h>
31
- #include <stdbool.h>
32
- #include <unistd.h>
33
-
34
- #include <fcntl.h>
35
- #include <errno.h>
36
-
37
- #include <sys/types.h>
38
- #include <sys/socket.h>
39
- #include <netdb.h>
40
-
41
- #include <ev.h>
42
- #include <uthash.h>
43
-
44
- #include <quiche.h>
45
-
46
- #define LOCAL_CONN_ID_LEN 16
47
-
48
- #define MAX_DATAGRAM_SIZE 1350
49
-
50
- #define MAX_TOKEN_LEN \
51
- sizeof("quiche") - 1 + \
52
- sizeof(struct sockaddr_storage) + \
53
- QUICHE_MAX_CONN_ID_LEN
54
-
55
- struct connections {
56
- int sock;
57
-
58
- struct sockaddr *local_addr;
59
- socklen_t local_addr_len;
60
-
61
- struct conn_io *h;
62
- };
63
-
64
- struct conn_io {
65
- ev_timer timer;
66
-
67
- int sock;
68
-
69
- uint8_t cid[LOCAL_CONN_ID_LEN];
70
-
71
- quiche_conn *conn;
72
- quiche_h3_conn *http3;
73
-
74
- struct sockaddr_storage peer_addr;
75
- socklen_t peer_addr_len;
76
-
77
- UT_hash_handle hh;
78
- };
79
-
80
- static quiche_config *config = NULL;
81
-
82
- static quiche_h3_config *http3_config = NULL;
83
-
84
- static struct connections *conns = NULL;
85
-
86
- static void timeout_cb(EV_P_ ev_timer *w, int revents);
87
-
88
- static void debug_log(const char *line, void *argp) {
89
- fprintf(stderr, "%s\n", line);
90
- }
91
-
92
- static void flush_egress(struct ev_loop *loop, struct conn_io *conn_io) {
93
- static uint8_t out[MAX_DATAGRAM_SIZE];
94
-
95
- quiche_send_info send_info;
96
-
97
- while (1) {
98
- ssize_t written = quiche_conn_send(conn_io->conn, out, sizeof(out),
99
- &send_info);
100
-
101
- if (written == QUICHE_ERR_DONE) {
102
- fprintf(stderr, "done writing\n");
103
- break;
104
- }
105
-
106
- if (written < 0) {
107
- fprintf(stderr, "failed to create packet: %zd\n", written);
108
- return;
109
- }
110
-
111
- ssize_t sent = sendto(conn_io->sock, out, written, 0,
112
- (struct sockaddr *) &conn_io->peer_addr,
113
- conn_io->peer_addr_len);
114
- if (sent != written) {
115
- perror("failed to send");
116
- return;
117
- }
118
-
119
- fprintf(stderr, "sent %zd bytes\n", sent);
120
- }
121
-
122
- double t = quiche_conn_timeout_as_nanos(conn_io->conn) / 1e9f;
123
- conn_io->timer.repeat = t;
124
- ev_timer_again(loop, &conn_io->timer);
125
- }
126
-
127
- static void mint_token(const uint8_t *dcid, size_t dcid_len,
128
- struct sockaddr_storage *addr, socklen_t addr_len,
129
- uint8_t *token, size_t *token_len) {
130
- memcpy(token, "quiche", sizeof("quiche") - 1);
131
- memcpy(token + sizeof("quiche") - 1, addr, addr_len);
132
- memcpy(token + sizeof("quiche") - 1 + addr_len, dcid, dcid_len);
133
-
134
- *token_len = sizeof("quiche") - 1 + addr_len + dcid_len;
135
- }
136
-
137
- static bool validate_token(const uint8_t *token, size_t token_len,
138
- struct sockaddr_storage *addr, socklen_t addr_len,
139
- uint8_t *odcid, size_t *odcid_len) {
140
- if ((token_len < sizeof("quiche") - 1) ||
141
- memcmp(token, "quiche", sizeof("quiche") - 1)) {
142
- return false;
143
- }
144
-
145
- token += sizeof("quiche") - 1;
146
- token_len -= sizeof("quiche") - 1;
147
-
148
- if ((token_len < addr_len) || memcmp(token, addr, addr_len)) {
149
- return false;
150
- }
151
-
152
- token += addr_len;
153
- token_len -= addr_len;
154
-
155
- if (*odcid_len < token_len) {
156
- return false;
157
- }
158
-
159
- memcpy(odcid, token, token_len);
160
- *odcid_len = token_len;
161
-
162
- return true;
163
- }
164
-
165
- static uint8_t *gen_cid(uint8_t *cid, size_t cid_len) {
166
- int rng = open("/dev/urandom", O_RDONLY);
167
- if (rng < 0) {
168
- perror("failed to open /dev/urandom");
169
- return NULL;
170
- }
171
-
172
- ssize_t rand_len = read(rng, cid, cid_len);
173
- if (rand_len < 0) {
174
- perror("failed to create connection ID");
175
- return NULL;
176
- }
177
-
178
- return cid;
179
- }
180
-
181
- static struct conn_io *create_conn(uint8_t *scid, size_t scid_len,
182
- uint8_t *odcid, size_t odcid_len,
183
- struct sockaddr *local_addr,
184
- socklen_t local_addr_len,
185
- struct sockaddr_storage *peer_addr,
186
- socklen_t peer_addr_len)
187
- {
188
- struct conn_io *conn_io = calloc(1, sizeof(*conn_io));
189
- if (conn_io == NULL) {
190
- fprintf(stderr, "failed to allocate connection IO\n");
191
- return NULL;
192
- }
193
-
194
- if (scid_len != LOCAL_CONN_ID_LEN) {
195
- fprintf(stderr, "failed, scid length too short\n");
196
- }
197
-
198
- memcpy(conn_io->cid, scid, LOCAL_CONN_ID_LEN);
199
-
200
- quiche_conn *conn = quiche_accept(conn_io->cid, LOCAL_CONN_ID_LEN,
201
- odcid, odcid_len,
202
- local_addr,
203
- local_addr_len,
204
- (struct sockaddr *) peer_addr,
205
- peer_addr_len,
206
- config);
207
-
208
- if (conn == NULL) {
209
- fprintf(stderr, "failed to create connection\n");
210
- return NULL;
211
- }
212
-
213
- conn_io->sock = conns->sock;
214
- conn_io->conn = conn;
215
-
216
- memcpy(&conn_io->peer_addr, peer_addr, peer_addr_len);
217
- conn_io->peer_addr_len = peer_addr_len;
218
-
219
- ev_init(&conn_io->timer, timeout_cb);
220
- conn_io->timer.data = conn_io;
221
-
222
- HASH_ADD(hh, conns->h, cid, LOCAL_CONN_ID_LEN, conn_io);
223
-
224
- fprintf(stderr, "new connection\n");
225
-
226
- return conn_io;
227
- }
228
-
229
- static int for_each_header(uint8_t *name, size_t name_len,
230
- uint8_t *value, size_t value_len,
231
- void *argp) {
232
- fprintf(stderr, "got HTTP header: %.*s=%.*s\n",
233
- (int) name_len, name, (int) value_len, value);
234
-
235
- return 0;
236
- }
237
-
238
- static void recv_cb(EV_P_ ev_io *w, int revents) {
239
- struct conn_io *tmp, *conn_io = NULL;
240
-
241
- static uint8_t buf[65535];
242
- static uint8_t out[MAX_DATAGRAM_SIZE];
243
-
244
- while (1) {
245
- struct sockaddr_storage peer_addr;
246
- socklen_t peer_addr_len = sizeof(peer_addr);
247
- memset(&peer_addr, 0, peer_addr_len);
248
-
249
- ssize_t read = recvfrom(conns->sock, buf, sizeof(buf), 0,
250
- (struct sockaddr *) &peer_addr,
251
- &peer_addr_len);
252
-
253
- if (read < 0) {
254
- if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
255
- fprintf(stderr, "recv would block\n");
256
- break;
257
- }
258
-
259
- perror("failed to read");
260
- return;
261
- }
262
-
263
- uint8_t type;
264
- uint32_t version;
265
-
266
- uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
267
- size_t scid_len = sizeof(scid);
268
-
269
- uint8_t dcid[QUICHE_MAX_CONN_ID_LEN];
270
- size_t dcid_len = sizeof(dcid);
271
-
272
- uint8_t odcid[QUICHE_MAX_CONN_ID_LEN];
273
- size_t odcid_len = sizeof(odcid);
274
-
275
- uint8_t token[MAX_TOKEN_LEN];
276
- size_t token_len = sizeof(token);
277
-
278
- int rc = quiche_header_info(buf, read, LOCAL_CONN_ID_LEN, &version,
279
- &type, scid, &scid_len, dcid, &dcid_len,
280
- token, &token_len);
281
- if (rc < 0) {
282
- fprintf(stderr, "failed to parse header: %d\n", rc);
283
- return;
284
- }
285
-
286
- HASH_FIND(hh, conns->h, dcid, dcid_len, conn_io);
287
-
288
- if (conn_io == NULL) {
289
- if (!quiche_version_is_supported(version)) {
290
- fprintf(stderr, "version negotiation\n");
291
-
292
- ssize_t written = quiche_negotiate_version(scid, scid_len,
293
- dcid, dcid_len,
294
- out, sizeof(out));
295
-
296
- if (written < 0) {
297
- fprintf(stderr, "failed to create vneg packet: %zd\n",
298
- written);
299
- continue;
300
- }
301
-
302
- ssize_t sent = sendto(conns->sock, out, written, 0,
303
- (struct sockaddr *) &peer_addr,
304
- peer_addr_len);
305
- if (sent != written) {
306
- perror("failed to send");
307
- continue;
308
- }
309
-
310
- fprintf(stderr, "sent %zd bytes\n", sent);
311
- continue;
312
- }
313
-
314
- if (token_len == 0) {
315
- fprintf(stderr, "stateless retry\n");
316
-
317
- mint_token(dcid, dcid_len, &peer_addr, peer_addr_len,
318
- token, &token_len);
319
-
320
- uint8_t new_cid[LOCAL_CONN_ID_LEN];
321
-
322
- if (gen_cid(new_cid, LOCAL_CONN_ID_LEN) == NULL) {
323
- continue;
324
- }
325
-
326
- ssize_t written = quiche_retry(scid, scid_len,
327
- dcid, dcid_len,
328
- new_cid, LOCAL_CONN_ID_LEN,
329
- token, token_len,
330
- version, out, sizeof(out));
331
-
332
- if (written < 0) {
333
- fprintf(stderr, "failed to create retry packet: %zd\n",
334
- written);
335
- continue;
336
- }
337
-
338
- ssize_t sent = sendto(conns->sock, out, written, 0,
339
- (struct sockaddr *) &peer_addr,
340
- peer_addr_len);
341
- if (sent != written) {
342
- perror("failed to send");
343
- continue;
344
- }
345
-
346
- fprintf(stderr, "sent %zd bytes\n", sent);
347
- continue;
348
- }
349
-
350
-
351
- if (!validate_token(token, token_len, &peer_addr, peer_addr_len,
352
- odcid, &odcid_len)) {
353
- fprintf(stderr, "invalid address validation token\n");
354
- continue;
355
- }
356
-
357
- conn_io = create_conn(dcid, dcid_len, odcid, odcid_len,
358
- conns->local_addr, conns->local_addr_len,
359
- &peer_addr, peer_addr_len);
360
-
361
- if (conn_io == NULL) {
362
- continue;
363
- }
364
- }
365
-
366
- quiche_recv_info recv_info = {
367
- (struct sockaddr *)&peer_addr,
368
- peer_addr_len,
369
-
370
- conns->local_addr,
371
- conns->local_addr_len,
372
- };
373
-
374
- ssize_t done = quiche_conn_recv(conn_io->conn, buf, read, &recv_info);
375
-
376
- if (done < 0) {
377
- fprintf(stderr, "failed to process packet: %zd\n", done);
378
- continue;
379
- }
380
-
381
- fprintf(stderr, "recv %zd bytes\n", done);
382
-
383
- if (quiche_conn_is_established(conn_io->conn)) {
384
- quiche_h3_event *ev;
385
-
386
- if (conn_io->http3 == NULL) {
387
- conn_io->http3 = quiche_h3_conn_new_with_transport(conn_io->conn,
388
- http3_config);
389
- if (conn_io->http3 == NULL) {
390
- fprintf(stderr, "failed to create HTTP/3 connection\n");
391
- continue;
392
- }
393
- }
394
-
395
- while (1) {
396
- int64_t s = quiche_h3_conn_poll(conn_io->http3,
397
- conn_io->conn,
398
- &ev);
399
-
400
- if (s < 0) {
401
- break;
402
- }
403
-
404
- switch (quiche_h3_event_type(ev)) {
405
- case QUICHE_H3_EVENT_HEADERS: {
406
- int rc = quiche_h3_event_for_each_header(ev,
407
- for_each_header,
408
- NULL);
409
-
410
- if (rc != 0) {
411
- fprintf(stderr, "failed to process headers\n");
412
- }
413
-
414
- quiche_h3_header headers[] = {
415
- {
416
- .name = (const uint8_t *) ":status",
417
- .name_len = sizeof(":status") - 1,
418
-
419
- .value = (const uint8_t *) "200",
420
- .value_len = sizeof("200") - 1,
421
- },
422
-
423
- {
424
- .name = (const uint8_t *) "server",
425
- .name_len = sizeof("server") - 1,
426
-
427
- .value = (const uint8_t *) "quiche",
428
- .value_len = sizeof("quiche") - 1,
429
- },
430
-
431
- {
432
- .name = (const uint8_t *) "content-length",
433
- .name_len = sizeof("content-length") - 1,
434
-
435
- .value = (const uint8_t *) "5",
436
- .value_len = sizeof("5") - 1,
437
- },
438
- };
439
-
440
- quiche_h3_send_response(conn_io->http3, conn_io->conn,
441
- s, headers, 3, false);
442
-
443
- quiche_h3_send_body(conn_io->http3, conn_io->conn,
444
- s, (uint8_t *) "byez\n", 5, true);
445
- break;
446
- }
447
-
448
- case QUICHE_H3_EVENT_DATA: {
449
- fprintf(stderr, "got HTTP data\n");
450
- break;
451
- }
452
-
453
- case QUICHE_H3_EVENT_FINISHED:
454
- break;
455
-
456
- case QUICHE_H3_EVENT_RESET:
457
- break;
458
-
459
- case QUICHE_H3_EVENT_PRIORITY_UPDATE:
460
- break;
461
-
462
- case QUICHE_H3_EVENT_GOAWAY: {
463
- fprintf(stderr, "got GOAWAY\n");
464
- break;
465
- }
466
- }
467
-
468
- quiche_h3_event_free(ev);
469
- }
470
- }
471
- }
472
-
473
- HASH_ITER(hh, conns->h, conn_io, tmp) {
474
- flush_egress(loop, conn_io);
475
-
476
- if (quiche_conn_is_closed(conn_io->conn)) {
477
- quiche_stats stats;
478
- quiche_path_stats path_stats;
479
-
480
- quiche_conn_stats(conn_io->conn, &stats);
481
- quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
482
-
483
- fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns cwnd=%zu\n",
484
- stats.recv, stats.sent, stats.lost, path_stats.rtt, path_stats.cwnd);
485
-
486
- HASH_DELETE(hh, conns->h, conn_io);
487
-
488
- ev_timer_stop(loop, &conn_io->timer);
489
-
490
- quiche_conn_free(conn_io->conn);
491
- free(conn_io);
492
- }
493
- }
494
- }
495
-
496
- static void timeout_cb(EV_P_ ev_timer *w, int revents) {
497
- struct conn_io *conn_io = w->data;
498
- quiche_conn_on_timeout(conn_io->conn);
499
-
500
- fprintf(stderr, "timeout\n");
501
-
502
- flush_egress(loop, conn_io);
503
-
504
- if (quiche_conn_is_closed(conn_io->conn)) {
505
- quiche_stats stats;
506
- quiche_path_stats path_stats;
507
-
508
- quiche_conn_stats(conn_io->conn, &stats);
509
- quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
510
-
511
- fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns cwnd=%zu\n",
512
- stats.recv, stats.sent, stats.lost, path_stats.rtt, path_stats.cwnd);
513
-
514
- HASH_DELETE(hh, conns->h, conn_io);
515
-
516
- ev_timer_stop(loop, &conn_io->timer);
517
- quiche_conn_free(conn_io->conn);
518
- free(conn_io);
519
-
520
- return;
521
- }
522
- }
523
-
524
- int main(int argc, char *argv[]) {
525
- const char *host = argv[1];
526
- const char *port = argv[2];
527
-
528
- const struct addrinfo hints = {
529
- .ai_family = PF_UNSPEC,
530
- .ai_socktype = SOCK_DGRAM,
531
- .ai_protocol = IPPROTO_UDP
532
- };
533
-
534
- quiche_enable_debug_logging(debug_log, NULL);
535
-
536
- struct addrinfo *local;
537
- if (getaddrinfo(host, port, &hints, &local) != 0) {
538
- perror("failed to resolve host");
539
- return -1;
540
- }
541
-
542
- int sock = socket(local->ai_family, SOCK_DGRAM, 0);
543
- if (sock < 0) {
544
- perror("failed to create socket");
545
- return -1;
546
- }
547
-
548
- if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
549
- perror("failed to make socket non-blocking");
550
- return -1;
551
- }
552
-
553
- if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
554
- perror("failed to connect socket");
555
- return -1;
556
- }
557
-
558
- config = quiche_config_new(QUICHE_PROTOCOL_VERSION);
559
- if (config == NULL) {
560
- fprintf(stderr, "failed to create config\n");
561
- return -1;
562
- }
563
-
564
- quiche_config_load_cert_chain_from_pem_file(config, "./cert.crt");
565
- quiche_config_load_priv_key_from_pem_file(config, "./cert.key");
566
-
567
- quiche_config_set_application_protos(config,
568
- (uint8_t *) QUICHE_H3_APPLICATION_PROTOCOL,
569
- sizeof(QUICHE_H3_APPLICATION_PROTOCOL) - 1);
570
-
571
- quiche_config_set_max_idle_timeout(config, 5000);
572
- quiche_config_set_max_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
573
- quiche_config_set_max_send_udp_payload_size(config, MAX_DATAGRAM_SIZE);
574
- quiche_config_set_initial_max_data(config, 10000000);
575
- quiche_config_set_initial_max_stream_data_bidi_local(config, 1000000);
576
- quiche_config_set_initial_max_stream_data_bidi_remote(config, 1000000);
577
- quiche_config_set_initial_max_stream_data_uni(config, 1000000);
578
- quiche_config_set_initial_max_streams_bidi(config, 100);
579
- quiche_config_set_initial_max_streams_uni(config, 100);
580
- quiche_config_set_disable_active_migration(config, true);
581
- quiche_config_set_cc_algorithm(config, QUICHE_CC_RENO);
582
-
583
- http3_config = quiche_h3_config_new();
584
- if (http3_config == NULL) {
585
- fprintf(stderr, "failed to create HTTP/3 config\n");
586
- return -1;
587
- }
588
-
589
- struct connections c;
590
- c.sock = sock;
591
- c.h = NULL;
592
- c.local_addr = local->ai_addr;
593
- c.local_addr_len = local->ai_addrlen;
594
-
595
- conns = &c;
596
-
597
- ev_io watcher;
598
-
599
- struct ev_loop *loop = ev_default_loop(0);
600
-
601
- ev_io_init(&watcher, recv_cb, sock, EV_READ);
602
- ev_io_start(loop, &watcher);
603
- watcher.data = &c;
604
-
605
- ev_loop(loop, 0);
606
-
607
- freeaddrinfo(local);
608
-
609
- quiche_h3_config_free(http3_config);
610
-
611
- quiche_config_free(config);
612
-
613
- return 0;
614
- }
1
+ // Copyright (C) 2018-2019, Cloudflare, Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright
9
+ // notice, this list of conditions and the following disclaimer.
10
+ //
11
+ // * Redistributions in binary form must reproduce the above copyright
12
+ // notice, this list of conditions and the following disclaimer in the
13
+ // documentation and/or other materials provided with the distribution.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ #include <inttypes.h>
28
+ #include <stdio.h>
29
+ #include <stdlib.h>
30
+ #include <stdint.h>
31
+ #include <stdbool.h>
32
+ #include <unistd.h>
33
+
34
+ #include <fcntl.h>
35
+ #include <errno.h>
36
+
37
+ #include <sys/types.h>
38
+ #include <sys/socket.h>
39
+ #include <netdb.h>
40
+
41
+ #include <ev.h>
42
+ #include <uthash.h>
43
+
44
+ #include <quiche.h>
45
+
46
+ #define LOCAL_CONN_ID_LEN 16
47
+
48
+ #define MAX_DATAGRAM_SIZE 1350
49
+
50
+ #define MAX_TOKEN_LEN \
51
+ sizeof("quiche") - 1 + \
52
+ sizeof(struct sockaddr_storage) + \
53
+ QUICHE_MAX_CONN_ID_LEN
54
+
55
+ struct connections {
56
+ int sock;
57
+
58
+ struct sockaddr *local_addr;
59
+ socklen_t local_addr_len;
60
+
61
+ struct conn_io *h;
62
+ };
63
+
64
+ struct conn_io {
65
+ ev_timer timer;
66
+
67
+ int sock;
68
+
69
+ uint8_t cid[LOCAL_CONN_ID_LEN];
70
+
71
+ quiche_conn *conn;
72
+ quiche_h3_conn *http3;
73
+
74
+ struct sockaddr_storage peer_addr;
75
+ socklen_t peer_addr_len;
76
+
77
+ UT_hash_handle hh;
78
+ };
79
+
80
+ static quiche_config *config = NULL;
81
+
82
+ static quiche_h3_config *http3_config = NULL;
83
+
84
+ static struct connections *conns = NULL;
85
+
86
+ static void timeout_cb(EV_P_ ev_timer *w, int revents);
87
+
88
+ static void debug_log(const char *line, void *argp) {
89
+ fprintf(stderr, "%s\n", line);
90
+ }
91
+
92
+ static void flush_egress(struct ev_loop *loop, struct conn_io *conn_io) {
93
+ static uint8_t out[MAX_DATAGRAM_SIZE];
94
+
95
+ quiche_send_info send_info;
96
+
97
+ while (1) {
98
+ ssize_t written = quiche_conn_send(conn_io->conn, out, sizeof(out),
99
+ &send_info);
100
+
101
+ if (written == QUICHE_ERR_DONE) {
102
+ fprintf(stderr, "done writing\n");
103
+ break;
104
+ }
105
+
106
+ if (written < 0) {
107
+ fprintf(stderr, "failed to create packet: %zd\n", written);
108
+ return;
109
+ }
110
+
111
+ ssize_t sent = sendto(conn_io->sock, out, written, 0,
112
+ (struct sockaddr *) &conn_io->peer_addr,
113
+ conn_io->peer_addr_len);
114
+ if (sent != written) {
115
+ perror("failed to send");
116
+ return;
117
+ }
118
+
119
+ fprintf(stderr, "sent %zd bytes\n", sent);
120
+ }
121
+
122
+ double t = quiche_conn_timeout_as_nanos(conn_io->conn) / 1e9f;
123
+ conn_io->timer.repeat = t;
124
+ ev_timer_again(loop, &conn_io->timer);
125
+ }
126
+
127
+ static void mint_token(const uint8_t *dcid, size_t dcid_len,
128
+ struct sockaddr_storage *addr, socklen_t addr_len,
129
+ uint8_t *token, size_t *token_len) {
130
+ memcpy(token, "quiche", sizeof("quiche") - 1);
131
+ memcpy(token + sizeof("quiche") - 1, addr, addr_len);
132
+ memcpy(token + sizeof("quiche") - 1 + addr_len, dcid, dcid_len);
133
+
134
+ *token_len = sizeof("quiche") - 1 + addr_len + dcid_len;
135
+ }
136
+
137
+ static bool validate_token(const uint8_t *token, size_t token_len,
138
+ struct sockaddr_storage *addr, socklen_t addr_len,
139
+ uint8_t *odcid, size_t *odcid_len) {
140
+ if ((token_len < sizeof("quiche") - 1) ||
141
+ memcmp(token, "quiche", sizeof("quiche") - 1)) {
142
+ return false;
143
+ }
144
+
145
+ token += sizeof("quiche") - 1;
146
+ token_len -= sizeof("quiche") - 1;
147
+
148
+ if ((token_len < addr_len) || memcmp(token, addr, addr_len)) {
149
+ return false;
150
+ }
151
+
152
+ token += addr_len;
153
+ token_len -= addr_len;
154
+
155
+ if (*odcid_len < token_len) {
156
+ return false;
157
+ }
158
+
159
+ memcpy(odcid, token, token_len);
160
+ *odcid_len = token_len;
161
+
162
+ return true;
163
+ }
164
+
165
+ static uint8_t *gen_cid(uint8_t *cid, size_t cid_len) {
166
+ int rng = open("/dev/urandom", O_RDONLY);
167
+ if (rng < 0) {
168
+ perror("failed to open /dev/urandom");
169
+ return NULL;
170
+ }
171
+
172
+ ssize_t rand_len = read(rng, cid, cid_len);
173
+ if (rand_len < 0) {
174
+ perror("failed to create connection ID");
175
+ return NULL;
176
+ }
177
+
178
+ return cid;
179
+ }
180
+
181
+ static struct conn_io *create_conn(uint8_t *scid, size_t scid_len,
182
+ uint8_t *odcid, size_t odcid_len,
183
+ struct sockaddr *local_addr,
184
+ socklen_t local_addr_len,
185
+ struct sockaddr_storage *peer_addr,
186
+ socklen_t peer_addr_len)
187
+ {
188
+ struct conn_io *conn_io = calloc(1, sizeof(*conn_io));
189
+ if (conn_io == NULL) {
190
+ fprintf(stderr, "failed to allocate connection IO\n");
191
+ return NULL;
192
+ }
193
+
194
+ if (scid_len != LOCAL_CONN_ID_LEN) {
195
+ fprintf(stderr, "failed, scid length too short\n");
196
+ }
197
+
198
+ memcpy(conn_io->cid, scid, LOCAL_CONN_ID_LEN);
199
+
200
+ quiche_conn *conn = quiche_accept(conn_io->cid, LOCAL_CONN_ID_LEN,
201
+ odcid, odcid_len,
202
+ local_addr,
203
+ local_addr_len,
204
+ (struct sockaddr *) peer_addr,
205
+ peer_addr_len,
206
+ config);
207
+
208
+ if (conn == NULL) {
209
+ fprintf(stderr, "failed to create connection\n");
210
+ return NULL;
211
+ }
212
+
213
+ conn_io->sock = conns->sock;
214
+ conn_io->conn = conn;
215
+
216
+ memcpy(&conn_io->peer_addr, peer_addr, peer_addr_len);
217
+ conn_io->peer_addr_len = peer_addr_len;
218
+
219
+ ev_init(&conn_io->timer, timeout_cb);
220
+ conn_io->timer.data = conn_io;
221
+
222
+ HASH_ADD(hh, conns->h, cid, LOCAL_CONN_ID_LEN, conn_io);
223
+
224
+ fprintf(stderr, "new connection\n");
225
+
226
+ return conn_io;
227
+ }
228
+
229
+ static int for_each_header(uint8_t *name, size_t name_len,
230
+ uint8_t *value, size_t value_len,
231
+ void *argp) {
232
+ fprintf(stderr, "got HTTP header: %.*s=%.*s\n",
233
+ (int) name_len, name, (int) value_len, value);
234
+
235
+ return 0;
236
+ }
237
+
238
+ static void recv_cb(EV_P_ ev_io *w, int revents) {
239
+ struct conn_io *tmp, *conn_io = NULL;
240
+
241
+ static uint8_t buf[65535];
242
+ static uint8_t out[MAX_DATAGRAM_SIZE];
243
+
244
+ while (1) {
245
+ struct sockaddr_storage peer_addr;
246
+ socklen_t peer_addr_len = sizeof(peer_addr);
247
+ memset(&peer_addr, 0, peer_addr_len);
248
+
249
+ ssize_t read = recvfrom(conns->sock, buf, sizeof(buf), 0,
250
+ (struct sockaddr *) &peer_addr,
251
+ &peer_addr_len);
252
+
253
+ if (read < 0) {
254
+ if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
255
+ fprintf(stderr, "recv would block\n");
256
+ break;
257
+ }
258
+
259
+ perror("failed to read");
260
+ return;
261
+ }
262
+
263
+ uint8_t type;
264
+ uint32_t version;
265
+
266
+ uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
267
+ size_t scid_len = sizeof(scid);
268
+
269
+ uint8_t dcid[QUICHE_MAX_CONN_ID_LEN];
270
+ size_t dcid_len = sizeof(dcid);
271
+
272
+ uint8_t odcid[QUICHE_MAX_CONN_ID_LEN];
273
+ size_t odcid_len = sizeof(odcid);
274
+
275
+ uint8_t token[MAX_TOKEN_LEN];
276
+ size_t token_len = sizeof(token);
277
+
278
+ int rc = quiche_header_info(buf, read, LOCAL_CONN_ID_LEN, &version,
279
+ &type, scid, &scid_len, dcid, &dcid_len,
280
+ token, &token_len);
281
+ if (rc < 0) {
282
+ fprintf(stderr, "failed to parse header: %d\n", rc);
283
+ return;
284
+ }
285
+
286
+ HASH_FIND(hh, conns->h, dcid, dcid_len, conn_io);
287
+
288
+ if (conn_io == NULL) {
289
+ if (!quiche_version_is_supported(version)) {
290
+ fprintf(stderr, "version negotiation\n");
291
+
292
+ ssize_t written = quiche_negotiate_version(scid, scid_len,
293
+ dcid, dcid_len,
294
+ out, sizeof(out));
295
+
296
+ if (written < 0) {
297
+ fprintf(stderr, "failed to create vneg packet: %zd\n",
298
+ written);
299
+ continue;
300
+ }
301
+
302
+ ssize_t sent = sendto(conns->sock, out, written, 0,
303
+ (struct sockaddr *) &peer_addr,
304
+ peer_addr_len);
305
+ if (sent != written) {
306
+ perror("failed to send");
307
+ continue;
308
+ }
309
+
310
+ fprintf(stderr, "sent %zd bytes\n", sent);
311
+ continue;
312
+ }
313
+
314
+ if (token_len == 0) {
315
+ fprintf(stderr, "stateless retry\n");
316
+
317
+ mint_token(dcid, dcid_len, &peer_addr, peer_addr_len,
318
+ token, &token_len);
319
+
320
+ uint8_t new_cid[LOCAL_CONN_ID_LEN];
321
+
322
+ if (gen_cid(new_cid, LOCAL_CONN_ID_LEN) == NULL) {
323
+ continue;
324
+ }
325
+
326
+ ssize_t written = quiche_retry(scid, scid_len,
327
+ dcid, dcid_len,
328
+ new_cid, LOCAL_CONN_ID_LEN,
329
+ token, token_len,
330
+ version, out, sizeof(out));
331
+
332
+ if (written < 0) {
333
+ fprintf(stderr, "failed to create retry packet: %zd\n",
334
+ written);
335
+ continue;
336
+ }
337
+
338
+ ssize_t sent = sendto(conns->sock, out, written, 0,
339
+ (struct sockaddr *) &peer_addr,
340
+ peer_addr_len);
341
+ if (sent != written) {
342
+ perror("failed to send");
343
+ continue;
344
+ }
345
+
346
+ fprintf(stderr, "sent %zd bytes\n", sent);
347
+ continue;
348
+ }
349
+
350
+
351
+ if (!validate_token(token, token_len, &peer_addr, peer_addr_len,
352
+ odcid, &odcid_len)) {
353
+ fprintf(stderr, "invalid address validation token\n");
354
+ continue;
355
+ }
356
+
357
+ conn_io = create_conn(dcid, dcid_len, odcid, odcid_len,
358
+ conns->local_addr, conns->local_addr_len,
359
+ &peer_addr, peer_addr_len);
360
+
361
+ if (conn_io == NULL) {
362
+ continue;
363
+ }
364
+ }
365
+
366
+ quiche_recv_info recv_info = {
367
+ (struct sockaddr *)&peer_addr,
368
+ peer_addr_len,
369
+
370
+ conns->local_addr,
371
+ conns->local_addr_len,
372
+ };
373
+
374
+ ssize_t done = quiche_conn_recv(conn_io->conn, buf, read, &recv_info);
375
+
376
+ if (done < 0) {
377
+ fprintf(stderr, "failed to process packet: %zd\n", done);
378
+ continue;
379
+ }
380
+
381
+ fprintf(stderr, "recv %zd bytes\n", done);
382
+
383
+ if (quiche_conn_is_established(conn_io->conn)) {
384
+ quiche_h3_event *ev;
385
+
386
+ if (conn_io->http3 == NULL) {
387
+ conn_io->http3 = quiche_h3_conn_new_with_transport(conn_io->conn,
388
+ http3_config);
389
+ if (conn_io->http3 == NULL) {
390
+ fprintf(stderr, "failed to create HTTP/3 connection\n");
391
+ continue;
392
+ }
393
+ }
394
+
395
+ while (1) {
396
+ int64_t s = quiche_h3_conn_poll(conn_io->http3,
397
+ conn_io->conn,
398
+ &ev);
399
+
400
+ if (s < 0) {
401
+ break;
402
+ }
403
+
404
+ switch (quiche_h3_event_type(ev)) {
405
+ case QUICHE_H3_EVENT_HEADERS: {
406
+ int rc = quiche_h3_event_for_each_header(ev,
407
+ for_each_header,
408
+ NULL);
409
+
410
+ if (rc != 0) {
411
+ fprintf(stderr, "failed to process headers\n");
412
+ }
413
+
414
+ quiche_h3_header headers[] = {
415
+ {
416
+ .name = (const uint8_t *) ":status",
417
+ .name_len = sizeof(":status") - 1,
418
+
419
+ .value = (const uint8_t *) "200",
420
+ .value_len = sizeof("200") - 1,
421
+ },
422
+
423
+ {
424
+ .name = (const uint8_t *) "server",
425
+ .name_len = sizeof("server") - 1,
426
+
427
+ .value = (const uint8_t *) "quiche",
428
+ .value_len = sizeof("quiche") - 1,
429
+ },
430
+
431
+ {
432
+ .name = (const uint8_t *) "content-length",
433
+ .name_len = sizeof("content-length") - 1,
434
+
435
+ .value = (const uint8_t *) "5",
436
+ .value_len = sizeof("5") - 1,
437
+ },
438
+ };
439
+
440
+ quiche_h3_send_response(conn_io->http3, conn_io->conn,
441
+ s, headers, 3, false);
442
+
443
+ quiche_h3_send_body(conn_io->http3, conn_io->conn,
444
+ s, (uint8_t *) "byez\n", 5, true);
445
+ break;
446
+ }
447
+
448
+ case QUICHE_H3_EVENT_DATA: {
449
+ fprintf(stderr, "got HTTP data\n");
450
+ break;
451
+ }
452
+
453
+ case QUICHE_H3_EVENT_FINISHED:
454
+ break;
455
+
456
+ case QUICHE_H3_EVENT_RESET:
457
+ break;
458
+
459
+ case QUICHE_H3_EVENT_PRIORITY_UPDATE:
460
+ break;
461
+
462
+ case QUICHE_H3_EVENT_GOAWAY: {
463
+ fprintf(stderr, "got GOAWAY\n");
464
+ break;
465
+ }
466
+ }
467
+
468
+ quiche_h3_event_free(ev);
469
+ }
470
+ }
471
+ }
472
+
473
+ HASH_ITER(hh, conns->h, conn_io, tmp) {
474
+ flush_egress(loop, conn_io);
475
+
476
+ if (quiche_conn_is_closed(conn_io->conn)) {
477
+ quiche_stats stats;
478
+ quiche_path_stats path_stats;
479
+
480
+ quiche_conn_stats(conn_io->conn, &stats);
481
+ quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
482
+
483
+ fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns cwnd=%zu\n",
484
+ stats.recv, stats.sent, stats.lost, path_stats.rtt, path_stats.cwnd);
485
+
486
+ HASH_DELETE(hh, conns->h, conn_io);
487
+
488
+ ev_timer_stop(loop, &conn_io->timer);
489
+
490
+ quiche_conn_free(conn_io->conn);
491
+ free(conn_io);
492
+ }
493
+ }
494
+ }
495
+
496
+ static void timeout_cb(EV_P_ ev_timer *w, int revents) {
497
+ struct conn_io *conn_io = w->data;
498
+ quiche_conn_on_timeout(conn_io->conn);
499
+
500
+ fprintf(stderr, "timeout\n");
501
+
502
+ flush_egress(loop, conn_io);
503
+
504
+ if (quiche_conn_is_closed(conn_io->conn)) {
505
+ quiche_stats stats;
506
+ quiche_path_stats path_stats;
507
+
508
+ quiche_conn_stats(conn_io->conn, &stats);
509
+ quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
510
+
511
+ fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns cwnd=%zu\n",
512
+ stats.recv, stats.sent, stats.lost, path_stats.rtt, path_stats.cwnd);
513
+
514
+ HASH_DELETE(hh, conns->h, conn_io);
515
+
516
+ ev_timer_stop(loop, &conn_io->timer);
517
+ quiche_conn_free(conn_io->conn);
518
+ free(conn_io);
519
+
520
+ return;
521
+ }
522
+ }
523
+
524
+ int main(int argc, char *argv[]) {
525
+ const char *host = argv[1];
526
+ const char *port = argv[2];
527
+
528
+ const struct addrinfo hints = {
529
+ .ai_family = PF_UNSPEC,
530
+ .ai_socktype = SOCK_DGRAM,
531
+ .ai_protocol = IPPROTO_UDP
532
+ };
533
+
534
+ quiche_enable_debug_logging(debug_log, NULL);
535
+
536
+ struct addrinfo *local;
537
+ if (getaddrinfo(host, port, &hints, &local) != 0) {
538
+ perror("failed to resolve host");
539
+ return -1;
540
+ }
541
+
542
+ int sock = socket(local->ai_family, SOCK_DGRAM, 0);
543
+ if (sock < 0) {
544
+ perror("failed to create socket");
545
+ return -1;
546
+ }
547
+
548
+ if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
549
+ perror("failed to make socket non-blocking");
550
+ return -1;
551
+ }
552
+
553
+ if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
554
+ perror("failed to connect socket");
555
+ return -1;
556
+ }
557
+
558
+ config = quiche_config_new(QUICHE_PROTOCOL_VERSION);
559
+ if (config == NULL) {
560
+ fprintf(stderr, "failed to create config\n");
561
+ return -1;
562
+ }
563
+
564
+ quiche_config_load_cert_chain_from_pem_file(config, "./cert.crt");
565
+ quiche_config_load_priv_key_from_pem_file(config, "./cert.key");
566
+
567
+ quiche_config_set_application_protos(config,
568
+ (uint8_t *) QUICHE_H3_APPLICATION_PROTOCOL,
569
+ sizeof(QUICHE_H3_APPLICATION_PROTOCOL) - 1);
570
+
571
+ quiche_config_set_max_idle_timeout(config, 5000);
572
+ quiche_config_set_max_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
573
+ quiche_config_set_max_send_udp_payload_size(config, MAX_DATAGRAM_SIZE);
574
+ quiche_config_set_initial_max_data(config, 10000000);
575
+ quiche_config_set_initial_max_stream_data_bidi_local(config, 1000000);
576
+ quiche_config_set_initial_max_stream_data_bidi_remote(config, 1000000);
577
+ quiche_config_set_initial_max_stream_data_uni(config, 1000000);
578
+ quiche_config_set_initial_max_streams_bidi(config, 100);
579
+ quiche_config_set_initial_max_streams_uni(config, 100);
580
+ quiche_config_set_disable_active_migration(config, true);
581
+ quiche_config_set_cc_algorithm(config, QUICHE_CC_RENO);
582
+
583
+ http3_config = quiche_h3_config_new();
584
+ if (http3_config == NULL) {
585
+ fprintf(stderr, "failed to create HTTP/3 config\n");
586
+ return -1;
587
+ }
588
+
589
+ struct connections c;
590
+ c.sock = sock;
591
+ c.h = NULL;
592
+ c.local_addr = local->ai_addr;
593
+ c.local_addr_len = local->ai_addrlen;
594
+
595
+ conns = &c;
596
+
597
+ ev_io watcher;
598
+
599
+ struct ev_loop *loop = ev_default_loop(0);
600
+
601
+ ev_io_init(&watcher, recv_cb, sock, EV_READ);
602
+ ev_io_start(loop, &watcher);
603
+ watcher.data = &c;
604
+
605
+ ev_loop(loop, 0);
606
+
607
+ freeaddrinfo(local);
608
+
609
+ quiche_h3_config_free(http3_config);
610
+
611
+ quiche_config_free(config);
612
+
613
+ return 0;
614
+ }