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.
- 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,473 +1,473 @@
|
|
|
1
|
-
// Copyright (C) 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
|
-
|
|
43
|
-
#include <quiche.h>
|
|
44
|
-
|
|
45
|
-
#define LOCAL_CONN_ID_LEN 16
|
|
46
|
-
|
|
47
|
-
#define MAX_DATAGRAM_SIZE 1350
|
|
48
|
-
|
|
49
|
-
struct conn_io {
|
|
50
|
-
ev_timer timer;
|
|
51
|
-
|
|
52
|
-
const char *host;
|
|
53
|
-
|
|
54
|
-
int sock;
|
|
55
|
-
|
|
56
|
-
struct sockaddr_storage local_addr;
|
|
57
|
-
socklen_t local_addr_len;
|
|
58
|
-
|
|
59
|
-
quiche_conn *conn;
|
|
60
|
-
|
|
61
|
-
quiche_h3_conn *http3;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
static void debug_log(const char *line, void *argp) {
|
|
65
|
-
fprintf(stderr, "%s\n", line);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
static void flush_egress(struct ev_loop *loop, struct conn_io *conn_io) {
|
|
69
|
-
static uint8_t out[MAX_DATAGRAM_SIZE];
|
|
70
|
-
|
|
71
|
-
quiche_send_info send_info;
|
|
72
|
-
|
|
73
|
-
while (1) {
|
|
74
|
-
ssize_t written = quiche_conn_send(conn_io->conn, out, sizeof(out),
|
|
75
|
-
&send_info);
|
|
76
|
-
|
|
77
|
-
if (written == QUICHE_ERR_DONE) {
|
|
78
|
-
fprintf(stderr, "done writing\n");
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (written < 0) {
|
|
83
|
-
fprintf(stderr, "failed to create packet: %zd\n", written);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
ssize_t sent = sendto(conn_io->sock, out, written, 0,
|
|
88
|
-
(struct sockaddr *) &send_info.to,
|
|
89
|
-
send_info.to_len);
|
|
90
|
-
|
|
91
|
-
if (sent != written) {
|
|
92
|
-
perror("failed to send");
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
fprintf(stderr, "sent %zd bytes\n", sent);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
double t = quiche_conn_timeout_as_nanos(conn_io->conn) / 1e9f;
|
|
100
|
-
conn_io->timer.repeat = t;
|
|
101
|
-
ev_timer_again(loop, &conn_io->timer);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
static int for_each_setting(uint64_t identifier, uint64_t value,
|
|
105
|
-
void *argp) {
|
|
106
|
-
fprintf(stderr, "got HTTP/3 SETTING: %" PRIu64 "=%" PRIu64 "\n",
|
|
107
|
-
identifier, value);
|
|
108
|
-
|
|
109
|
-
return 0;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
static int for_each_header(uint8_t *name, size_t name_len,
|
|
113
|
-
uint8_t *value, size_t value_len,
|
|
114
|
-
void *argp) {
|
|
115
|
-
fprintf(stderr, "got HTTP header: %.*s=%.*s\n",
|
|
116
|
-
(int) name_len, name, (int) value_len, value);
|
|
117
|
-
|
|
118
|
-
return 0;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
static void recv_cb(EV_P_ ev_io *w, int revents) {
|
|
122
|
-
static bool req_sent = false;
|
|
123
|
-
static bool settings_received = false;
|
|
124
|
-
|
|
125
|
-
struct conn_io *conn_io = w->data;
|
|
126
|
-
|
|
127
|
-
static uint8_t buf[65535];
|
|
128
|
-
|
|
129
|
-
while (1) {
|
|
130
|
-
struct sockaddr_storage peer_addr;
|
|
131
|
-
socklen_t peer_addr_len = sizeof(peer_addr);
|
|
132
|
-
memset(&peer_addr, 0, peer_addr_len);
|
|
133
|
-
|
|
134
|
-
ssize_t read = recvfrom(conn_io->sock, buf, sizeof(buf), 0,
|
|
135
|
-
(struct sockaddr *) &peer_addr,
|
|
136
|
-
&peer_addr_len);
|
|
137
|
-
|
|
138
|
-
if (read < 0) {
|
|
139
|
-
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
|
|
140
|
-
fprintf(stderr, "recv would block\n");
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
perror("failed to read");
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
quiche_recv_info recv_info = {
|
|
149
|
-
(struct sockaddr *) &peer_addr,
|
|
150
|
-
peer_addr_len,
|
|
151
|
-
|
|
152
|
-
(struct sockaddr *) &conn_io->local_addr,
|
|
153
|
-
conn_io->local_addr_len,
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
ssize_t done = quiche_conn_recv(conn_io->conn, buf, read, &recv_info);
|
|
157
|
-
|
|
158
|
-
if (done < 0) {
|
|
159
|
-
fprintf(stderr, "failed to process packet: %zd\n", done);
|
|
160
|
-
continue;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
fprintf(stderr, "recv %zd bytes\n", done);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
fprintf(stderr, "done reading\n");
|
|
167
|
-
|
|
168
|
-
if (quiche_conn_is_closed(conn_io->conn)) {
|
|
169
|
-
fprintf(stderr, "connection closed\n");
|
|
170
|
-
|
|
171
|
-
ev_break(EV_A_ EVBREAK_ONE);
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (quiche_conn_is_established(conn_io->conn) && !req_sent) {
|
|
176
|
-
const uint8_t *app_proto;
|
|
177
|
-
size_t app_proto_len;
|
|
178
|
-
|
|
179
|
-
quiche_conn_application_proto(conn_io->conn, &app_proto, &app_proto_len);
|
|
180
|
-
|
|
181
|
-
fprintf(stderr, "connection established: %.*s\n",
|
|
182
|
-
(int) app_proto_len, app_proto);
|
|
183
|
-
|
|
184
|
-
quiche_h3_config *config = quiche_h3_config_new();
|
|
185
|
-
if (config == NULL) {
|
|
186
|
-
fprintf(stderr, "failed to create HTTP/3 config\n");
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
conn_io->http3 = quiche_h3_conn_new_with_transport(conn_io->conn, config);
|
|
191
|
-
if (conn_io->http3 == NULL) {
|
|
192
|
-
fprintf(stderr, "failed to create HTTP/3 connection\n");
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
quiche_h3_config_free(config);
|
|
197
|
-
|
|
198
|
-
quiche_h3_header headers[] = {
|
|
199
|
-
{
|
|
200
|
-
.name = (const uint8_t *) ":method",
|
|
201
|
-
.name_len = sizeof(":method") - 1,
|
|
202
|
-
|
|
203
|
-
.value = (const uint8_t *) "GET",
|
|
204
|
-
.value_len = sizeof("GET") - 1,
|
|
205
|
-
},
|
|
206
|
-
|
|
207
|
-
{
|
|
208
|
-
.name = (const uint8_t *) ":scheme",
|
|
209
|
-
.name_len = sizeof(":scheme") - 1,
|
|
210
|
-
|
|
211
|
-
.value = (const uint8_t *) "https",
|
|
212
|
-
.value_len = sizeof("https") - 1,
|
|
213
|
-
},
|
|
214
|
-
|
|
215
|
-
{
|
|
216
|
-
.name = (const uint8_t *) ":authority",
|
|
217
|
-
.name_len = sizeof(":authority") - 1,
|
|
218
|
-
|
|
219
|
-
.value = (const uint8_t *) conn_io->host,
|
|
220
|
-
.value_len = strlen(conn_io->host),
|
|
221
|
-
},
|
|
222
|
-
|
|
223
|
-
{
|
|
224
|
-
.name = (const uint8_t *) ":path",
|
|
225
|
-
.name_len = sizeof(":path") - 1,
|
|
226
|
-
|
|
227
|
-
.value = (const uint8_t *) "/",
|
|
228
|
-
.value_len = sizeof("/") - 1,
|
|
229
|
-
},
|
|
230
|
-
|
|
231
|
-
{
|
|
232
|
-
.name = (const uint8_t *) "user-agent",
|
|
233
|
-
.name_len = sizeof("user-agent") - 1,
|
|
234
|
-
|
|
235
|
-
.value = (const uint8_t *) "quiche",
|
|
236
|
-
.value_len = sizeof("quiche") - 1,
|
|
237
|
-
},
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
int64_t stream_id = quiche_h3_send_request(conn_io->http3,
|
|
241
|
-
conn_io->conn,
|
|
242
|
-
headers, 5, true);
|
|
243
|
-
|
|
244
|
-
fprintf(stderr, "sent HTTP request %" PRId64 "\n", stream_id);
|
|
245
|
-
|
|
246
|
-
req_sent = true;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (quiche_conn_is_established(conn_io->conn)) {
|
|
250
|
-
quiche_h3_event *ev;
|
|
251
|
-
|
|
252
|
-
while (1) {
|
|
253
|
-
int64_t s = quiche_h3_conn_poll(conn_io->http3,
|
|
254
|
-
conn_io->conn,
|
|
255
|
-
&ev);
|
|
256
|
-
|
|
257
|
-
if (s < 0) {
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if (!settings_received) {
|
|
262
|
-
int rc = quiche_h3_for_each_setting(conn_io->http3,
|
|
263
|
-
for_each_setting,
|
|
264
|
-
NULL);
|
|
265
|
-
|
|
266
|
-
if (rc == 0) {
|
|
267
|
-
settings_received = true;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
switch (quiche_h3_event_type(ev)) {
|
|
272
|
-
case QUICHE_H3_EVENT_HEADERS: {
|
|
273
|
-
int rc = quiche_h3_event_for_each_header(ev, for_each_header,
|
|
274
|
-
NULL);
|
|
275
|
-
|
|
276
|
-
if (rc != 0) {
|
|
277
|
-
fprintf(stderr, "failed to process headers");
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
break;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
case QUICHE_H3_EVENT_DATA: {
|
|
284
|
-
for (;;) {
|
|
285
|
-
ssize_t len = quiche_h3_recv_body(conn_io->http3,
|
|
286
|
-
conn_io->conn, s,
|
|
287
|
-
buf, sizeof(buf));
|
|
288
|
-
|
|
289
|
-
if (len <= 0) {
|
|
290
|
-
break;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
printf("%.*s", (int) len, buf);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
break;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
case QUICHE_H3_EVENT_FINISHED:
|
|
300
|
-
if (quiche_conn_close(conn_io->conn, true, 0, NULL, 0) < 0) {
|
|
301
|
-
fprintf(stderr, "failed to close connection\n");
|
|
302
|
-
}
|
|
303
|
-
break;
|
|
304
|
-
|
|
305
|
-
case QUICHE_H3_EVENT_RESET:
|
|
306
|
-
fprintf(stderr, "request was reset\n");
|
|
307
|
-
|
|
308
|
-
if (quiche_conn_close(conn_io->conn, true, 0, NULL, 0) < 0) {
|
|
309
|
-
fprintf(stderr, "failed to close connection\n");
|
|
310
|
-
}
|
|
311
|
-
break;
|
|
312
|
-
|
|
313
|
-
case QUICHE_H3_EVENT_PRIORITY_UPDATE:
|
|
314
|
-
break;
|
|
315
|
-
|
|
316
|
-
case QUICHE_H3_EVENT_GOAWAY: {
|
|
317
|
-
fprintf(stderr, "got GOAWAY\n");
|
|
318
|
-
break;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
quiche_h3_event_free(ev);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
flush_egress(loop, conn_io);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
static void timeout_cb(EV_P_ ev_timer *w, int revents) {
|
|
330
|
-
struct conn_io *conn_io = w->data;
|
|
331
|
-
quiche_conn_on_timeout(conn_io->conn);
|
|
332
|
-
|
|
333
|
-
fprintf(stderr, "timeout\n");
|
|
334
|
-
|
|
335
|
-
flush_egress(loop, conn_io);
|
|
336
|
-
|
|
337
|
-
if (quiche_conn_is_closed(conn_io->conn)) {
|
|
338
|
-
quiche_stats stats;
|
|
339
|
-
quiche_path_stats path_stats;
|
|
340
|
-
|
|
341
|
-
quiche_conn_stats(conn_io->conn, &stats);
|
|
342
|
-
quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
|
|
343
|
-
|
|
344
|
-
fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns\n",
|
|
345
|
-
stats.recv, stats.sent, stats.lost, path_stats.rtt);
|
|
346
|
-
|
|
347
|
-
ev_break(EV_A_ EVBREAK_ONE);
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
int main(int argc, char *argv[]) {
|
|
353
|
-
const char *host = argv[1];
|
|
354
|
-
const char *port = argv[2];
|
|
355
|
-
|
|
356
|
-
const struct addrinfo hints = {
|
|
357
|
-
.ai_family = PF_UNSPEC,
|
|
358
|
-
.ai_socktype = SOCK_DGRAM,
|
|
359
|
-
.ai_protocol = IPPROTO_UDP
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
quiche_enable_debug_logging(debug_log, NULL);
|
|
363
|
-
|
|
364
|
-
struct addrinfo *peer;
|
|
365
|
-
if (getaddrinfo(host, port, &hints, &peer) != 0) {
|
|
366
|
-
perror("failed to resolve host");
|
|
367
|
-
return -1;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
int sock = socket(peer->ai_family, SOCK_DGRAM, 0);
|
|
371
|
-
if (sock < 0) {
|
|
372
|
-
perror("failed to create socket");
|
|
373
|
-
return -1;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
|
|
377
|
-
perror("failed to make socket non-blocking");
|
|
378
|
-
return -1;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
quiche_config *config = quiche_config_new(0xbabababa);
|
|
382
|
-
if (config == NULL) {
|
|
383
|
-
fprintf(stderr, "failed to create config\n");
|
|
384
|
-
return -1;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
quiche_config_set_application_protos(config,
|
|
388
|
-
(uint8_t *) QUICHE_H3_APPLICATION_PROTOCOL,
|
|
389
|
-
sizeof(QUICHE_H3_APPLICATION_PROTOCOL) - 1);
|
|
390
|
-
|
|
391
|
-
quiche_config_set_max_idle_timeout(config, 5000);
|
|
392
|
-
quiche_config_set_max_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
|
393
|
-
quiche_config_set_max_send_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
|
394
|
-
quiche_config_set_initial_max_data(config, 10000000);
|
|
395
|
-
quiche_config_set_initial_max_stream_data_bidi_local(config, 1000000);
|
|
396
|
-
quiche_config_set_initial_max_stream_data_bidi_remote(config, 1000000);
|
|
397
|
-
quiche_config_set_initial_max_stream_data_uni(config, 1000000);
|
|
398
|
-
quiche_config_set_initial_max_streams_bidi(config, 100);
|
|
399
|
-
quiche_config_set_initial_max_streams_uni(config, 100);
|
|
400
|
-
quiche_config_set_disable_active_migration(config, true);
|
|
401
|
-
|
|
402
|
-
if (getenv("SSLKEYLOGFILE")) {
|
|
403
|
-
quiche_config_log_keys(config);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// ABC: old config creation here
|
|
407
|
-
|
|
408
|
-
uint8_t scid[LOCAL_CONN_ID_LEN];
|
|
409
|
-
int rng = open("/dev/urandom", O_RDONLY);
|
|
410
|
-
if (rng < 0) {
|
|
411
|
-
perror("failed to open /dev/urandom");
|
|
412
|
-
return -1;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
ssize_t rand_len = read(rng, &scid, sizeof(scid));
|
|
416
|
-
if (rand_len < 0) {
|
|
417
|
-
perror("failed to create connection ID");
|
|
418
|
-
return -1;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
struct conn_io *conn_io = malloc(sizeof(*conn_io));
|
|
422
|
-
if (conn_io == NULL) {
|
|
423
|
-
fprintf(stderr, "failed to allocate connection IO\n");
|
|
424
|
-
return -1;
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
conn_io->local_addr_len = sizeof(conn_io->local_addr);
|
|
428
|
-
if (getsockname(sock, (struct sockaddr *)&conn_io->local_addr,
|
|
429
|
-
&conn_io->local_addr_len) != 0)
|
|
430
|
-
{
|
|
431
|
-
perror("failed to get local address of socket");
|
|
432
|
-
return -1;
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
quiche_conn *conn = quiche_connect(host, (const uint8_t *) scid, sizeof(scid),
|
|
436
|
-
(struct sockaddr *) &conn_io->local_addr,
|
|
437
|
-
conn_io->local_addr_len,
|
|
438
|
-
peer->ai_addr, peer->ai_addrlen, config);
|
|
439
|
-
|
|
440
|
-
if (conn == NULL) {
|
|
441
|
-
fprintf(stderr, "failed to create connection\n");
|
|
442
|
-
return -1;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
conn_io->sock = sock;
|
|
446
|
-
conn_io->conn = conn;
|
|
447
|
-
conn_io->host = host;
|
|
448
|
-
|
|
449
|
-
ev_io watcher;
|
|
450
|
-
|
|
451
|
-
struct ev_loop *loop = ev_default_loop(0);
|
|
452
|
-
|
|
453
|
-
ev_io_init(&watcher, recv_cb, conn_io->sock, EV_READ);
|
|
454
|
-
ev_io_start(loop, &watcher);
|
|
455
|
-
watcher.data = conn_io;
|
|
456
|
-
|
|
457
|
-
ev_init(&conn_io->timer, timeout_cb);
|
|
458
|
-
conn_io->timer.data = conn_io;
|
|
459
|
-
|
|
460
|
-
flush_egress(loop, conn_io);
|
|
461
|
-
|
|
462
|
-
ev_loop(loop, 0);
|
|
463
|
-
|
|
464
|
-
freeaddrinfo(peer);
|
|
465
|
-
|
|
466
|
-
quiche_h3_conn_free(conn_io->http3);
|
|
467
|
-
|
|
468
|
-
quiche_conn_free(conn);
|
|
469
|
-
|
|
470
|
-
quiche_config_free(config);
|
|
471
|
-
|
|
472
|
-
return 0;
|
|
473
|
-
}
|
|
1
|
+
// Copyright (C) 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
|
+
|
|
43
|
+
#include <quiche.h>
|
|
44
|
+
|
|
45
|
+
#define LOCAL_CONN_ID_LEN 16
|
|
46
|
+
|
|
47
|
+
#define MAX_DATAGRAM_SIZE 1350
|
|
48
|
+
|
|
49
|
+
struct conn_io {
|
|
50
|
+
ev_timer timer;
|
|
51
|
+
|
|
52
|
+
const char *host;
|
|
53
|
+
|
|
54
|
+
int sock;
|
|
55
|
+
|
|
56
|
+
struct sockaddr_storage local_addr;
|
|
57
|
+
socklen_t local_addr_len;
|
|
58
|
+
|
|
59
|
+
quiche_conn *conn;
|
|
60
|
+
|
|
61
|
+
quiche_h3_conn *http3;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
static void debug_log(const char *line, void *argp) {
|
|
65
|
+
fprintf(stderr, "%s\n", line);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static void flush_egress(struct ev_loop *loop, struct conn_io *conn_io) {
|
|
69
|
+
static uint8_t out[MAX_DATAGRAM_SIZE];
|
|
70
|
+
|
|
71
|
+
quiche_send_info send_info;
|
|
72
|
+
|
|
73
|
+
while (1) {
|
|
74
|
+
ssize_t written = quiche_conn_send(conn_io->conn, out, sizeof(out),
|
|
75
|
+
&send_info);
|
|
76
|
+
|
|
77
|
+
if (written == QUICHE_ERR_DONE) {
|
|
78
|
+
fprintf(stderr, "done writing\n");
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (written < 0) {
|
|
83
|
+
fprintf(stderr, "failed to create packet: %zd\n", written);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
ssize_t sent = sendto(conn_io->sock, out, written, 0,
|
|
88
|
+
(struct sockaddr *) &send_info.to,
|
|
89
|
+
send_info.to_len);
|
|
90
|
+
|
|
91
|
+
if (sent != written) {
|
|
92
|
+
perror("failed to send");
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fprintf(stderr, "sent %zd bytes\n", sent);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
double t = quiche_conn_timeout_as_nanos(conn_io->conn) / 1e9f;
|
|
100
|
+
conn_io->timer.repeat = t;
|
|
101
|
+
ev_timer_again(loop, &conn_io->timer);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static int for_each_setting(uint64_t identifier, uint64_t value,
|
|
105
|
+
void *argp) {
|
|
106
|
+
fprintf(stderr, "got HTTP/3 SETTING: %" PRIu64 "=%" PRIu64 "\n",
|
|
107
|
+
identifier, value);
|
|
108
|
+
|
|
109
|
+
return 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static int for_each_header(uint8_t *name, size_t name_len,
|
|
113
|
+
uint8_t *value, size_t value_len,
|
|
114
|
+
void *argp) {
|
|
115
|
+
fprintf(stderr, "got HTTP header: %.*s=%.*s\n",
|
|
116
|
+
(int) name_len, name, (int) value_len, value);
|
|
117
|
+
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static void recv_cb(EV_P_ ev_io *w, int revents) {
|
|
122
|
+
static bool req_sent = false;
|
|
123
|
+
static bool settings_received = false;
|
|
124
|
+
|
|
125
|
+
struct conn_io *conn_io = w->data;
|
|
126
|
+
|
|
127
|
+
static uint8_t buf[65535];
|
|
128
|
+
|
|
129
|
+
while (1) {
|
|
130
|
+
struct sockaddr_storage peer_addr;
|
|
131
|
+
socklen_t peer_addr_len = sizeof(peer_addr);
|
|
132
|
+
memset(&peer_addr, 0, peer_addr_len);
|
|
133
|
+
|
|
134
|
+
ssize_t read = recvfrom(conn_io->sock, buf, sizeof(buf), 0,
|
|
135
|
+
(struct sockaddr *) &peer_addr,
|
|
136
|
+
&peer_addr_len);
|
|
137
|
+
|
|
138
|
+
if (read < 0) {
|
|
139
|
+
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
|
|
140
|
+
fprintf(stderr, "recv would block\n");
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
perror("failed to read");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
quiche_recv_info recv_info = {
|
|
149
|
+
(struct sockaddr *) &peer_addr,
|
|
150
|
+
peer_addr_len,
|
|
151
|
+
|
|
152
|
+
(struct sockaddr *) &conn_io->local_addr,
|
|
153
|
+
conn_io->local_addr_len,
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
ssize_t done = quiche_conn_recv(conn_io->conn, buf, read, &recv_info);
|
|
157
|
+
|
|
158
|
+
if (done < 0) {
|
|
159
|
+
fprintf(stderr, "failed to process packet: %zd\n", done);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
fprintf(stderr, "recv %zd bytes\n", done);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
fprintf(stderr, "done reading\n");
|
|
167
|
+
|
|
168
|
+
if (quiche_conn_is_closed(conn_io->conn)) {
|
|
169
|
+
fprintf(stderr, "connection closed\n");
|
|
170
|
+
|
|
171
|
+
ev_break(EV_A_ EVBREAK_ONE);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (quiche_conn_is_established(conn_io->conn) && !req_sent) {
|
|
176
|
+
const uint8_t *app_proto;
|
|
177
|
+
size_t app_proto_len;
|
|
178
|
+
|
|
179
|
+
quiche_conn_application_proto(conn_io->conn, &app_proto, &app_proto_len);
|
|
180
|
+
|
|
181
|
+
fprintf(stderr, "connection established: %.*s\n",
|
|
182
|
+
(int) app_proto_len, app_proto);
|
|
183
|
+
|
|
184
|
+
quiche_h3_config *config = quiche_h3_config_new();
|
|
185
|
+
if (config == NULL) {
|
|
186
|
+
fprintf(stderr, "failed to create HTTP/3 config\n");
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
conn_io->http3 = quiche_h3_conn_new_with_transport(conn_io->conn, config);
|
|
191
|
+
if (conn_io->http3 == NULL) {
|
|
192
|
+
fprintf(stderr, "failed to create HTTP/3 connection\n");
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
quiche_h3_config_free(config);
|
|
197
|
+
|
|
198
|
+
quiche_h3_header headers[] = {
|
|
199
|
+
{
|
|
200
|
+
.name = (const uint8_t *) ":method",
|
|
201
|
+
.name_len = sizeof(":method") - 1,
|
|
202
|
+
|
|
203
|
+
.value = (const uint8_t *) "GET",
|
|
204
|
+
.value_len = sizeof("GET") - 1,
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
{
|
|
208
|
+
.name = (const uint8_t *) ":scheme",
|
|
209
|
+
.name_len = sizeof(":scheme") - 1,
|
|
210
|
+
|
|
211
|
+
.value = (const uint8_t *) "https",
|
|
212
|
+
.value_len = sizeof("https") - 1,
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
{
|
|
216
|
+
.name = (const uint8_t *) ":authority",
|
|
217
|
+
.name_len = sizeof(":authority") - 1,
|
|
218
|
+
|
|
219
|
+
.value = (const uint8_t *) conn_io->host,
|
|
220
|
+
.value_len = strlen(conn_io->host),
|
|
221
|
+
},
|
|
222
|
+
|
|
223
|
+
{
|
|
224
|
+
.name = (const uint8_t *) ":path",
|
|
225
|
+
.name_len = sizeof(":path") - 1,
|
|
226
|
+
|
|
227
|
+
.value = (const uint8_t *) "/",
|
|
228
|
+
.value_len = sizeof("/") - 1,
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
{
|
|
232
|
+
.name = (const uint8_t *) "user-agent",
|
|
233
|
+
.name_len = sizeof("user-agent") - 1,
|
|
234
|
+
|
|
235
|
+
.value = (const uint8_t *) "quiche",
|
|
236
|
+
.value_len = sizeof("quiche") - 1,
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
int64_t stream_id = quiche_h3_send_request(conn_io->http3,
|
|
241
|
+
conn_io->conn,
|
|
242
|
+
headers, 5, true);
|
|
243
|
+
|
|
244
|
+
fprintf(stderr, "sent HTTP request %" PRId64 "\n", stream_id);
|
|
245
|
+
|
|
246
|
+
req_sent = true;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (quiche_conn_is_established(conn_io->conn)) {
|
|
250
|
+
quiche_h3_event *ev;
|
|
251
|
+
|
|
252
|
+
while (1) {
|
|
253
|
+
int64_t s = quiche_h3_conn_poll(conn_io->http3,
|
|
254
|
+
conn_io->conn,
|
|
255
|
+
&ev);
|
|
256
|
+
|
|
257
|
+
if (s < 0) {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (!settings_received) {
|
|
262
|
+
int rc = quiche_h3_for_each_setting(conn_io->http3,
|
|
263
|
+
for_each_setting,
|
|
264
|
+
NULL);
|
|
265
|
+
|
|
266
|
+
if (rc == 0) {
|
|
267
|
+
settings_received = true;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
switch (quiche_h3_event_type(ev)) {
|
|
272
|
+
case QUICHE_H3_EVENT_HEADERS: {
|
|
273
|
+
int rc = quiche_h3_event_for_each_header(ev, for_each_header,
|
|
274
|
+
NULL);
|
|
275
|
+
|
|
276
|
+
if (rc != 0) {
|
|
277
|
+
fprintf(stderr, "failed to process headers");
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
case QUICHE_H3_EVENT_DATA: {
|
|
284
|
+
for (;;) {
|
|
285
|
+
ssize_t len = quiche_h3_recv_body(conn_io->http3,
|
|
286
|
+
conn_io->conn, s,
|
|
287
|
+
buf, sizeof(buf));
|
|
288
|
+
|
|
289
|
+
if (len <= 0) {
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
printf("%.*s", (int) len, buf);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
case QUICHE_H3_EVENT_FINISHED:
|
|
300
|
+
if (quiche_conn_close(conn_io->conn, true, 0, NULL, 0) < 0) {
|
|
301
|
+
fprintf(stderr, "failed to close connection\n");
|
|
302
|
+
}
|
|
303
|
+
break;
|
|
304
|
+
|
|
305
|
+
case QUICHE_H3_EVENT_RESET:
|
|
306
|
+
fprintf(stderr, "request was reset\n");
|
|
307
|
+
|
|
308
|
+
if (quiche_conn_close(conn_io->conn, true, 0, NULL, 0) < 0) {
|
|
309
|
+
fprintf(stderr, "failed to close connection\n");
|
|
310
|
+
}
|
|
311
|
+
break;
|
|
312
|
+
|
|
313
|
+
case QUICHE_H3_EVENT_PRIORITY_UPDATE:
|
|
314
|
+
break;
|
|
315
|
+
|
|
316
|
+
case QUICHE_H3_EVENT_GOAWAY: {
|
|
317
|
+
fprintf(stderr, "got GOAWAY\n");
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
quiche_h3_event_free(ev);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
flush_egress(loop, conn_io);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
static void timeout_cb(EV_P_ ev_timer *w, int revents) {
|
|
330
|
+
struct conn_io *conn_io = w->data;
|
|
331
|
+
quiche_conn_on_timeout(conn_io->conn);
|
|
332
|
+
|
|
333
|
+
fprintf(stderr, "timeout\n");
|
|
334
|
+
|
|
335
|
+
flush_egress(loop, conn_io);
|
|
336
|
+
|
|
337
|
+
if (quiche_conn_is_closed(conn_io->conn)) {
|
|
338
|
+
quiche_stats stats;
|
|
339
|
+
quiche_path_stats path_stats;
|
|
340
|
+
|
|
341
|
+
quiche_conn_stats(conn_io->conn, &stats);
|
|
342
|
+
quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
|
|
343
|
+
|
|
344
|
+
fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns\n",
|
|
345
|
+
stats.recv, stats.sent, stats.lost, path_stats.rtt);
|
|
346
|
+
|
|
347
|
+
ev_break(EV_A_ EVBREAK_ONE);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
int main(int argc, char *argv[]) {
|
|
353
|
+
const char *host = argv[1];
|
|
354
|
+
const char *port = argv[2];
|
|
355
|
+
|
|
356
|
+
const struct addrinfo hints = {
|
|
357
|
+
.ai_family = PF_UNSPEC,
|
|
358
|
+
.ai_socktype = SOCK_DGRAM,
|
|
359
|
+
.ai_protocol = IPPROTO_UDP
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
quiche_enable_debug_logging(debug_log, NULL);
|
|
363
|
+
|
|
364
|
+
struct addrinfo *peer;
|
|
365
|
+
if (getaddrinfo(host, port, &hints, &peer) != 0) {
|
|
366
|
+
perror("failed to resolve host");
|
|
367
|
+
return -1;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
int sock = socket(peer->ai_family, SOCK_DGRAM, 0);
|
|
371
|
+
if (sock < 0) {
|
|
372
|
+
perror("failed to create socket");
|
|
373
|
+
return -1;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
|
|
377
|
+
perror("failed to make socket non-blocking");
|
|
378
|
+
return -1;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
quiche_config *config = quiche_config_new(0xbabababa);
|
|
382
|
+
if (config == NULL) {
|
|
383
|
+
fprintf(stderr, "failed to create config\n");
|
|
384
|
+
return -1;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
quiche_config_set_application_protos(config,
|
|
388
|
+
(uint8_t *) QUICHE_H3_APPLICATION_PROTOCOL,
|
|
389
|
+
sizeof(QUICHE_H3_APPLICATION_PROTOCOL) - 1);
|
|
390
|
+
|
|
391
|
+
quiche_config_set_max_idle_timeout(config, 5000);
|
|
392
|
+
quiche_config_set_max_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
|
393
|
+
quiche_config_set_max_send_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
|
394
|
+
quiche_config_set_initial_max_data(config, 10000000);
|
|
395
|
+
quiche_config_set_initial_max_stream_data_bidi_local(config, 1000000);
|
|
396
|
+
quiche_config_set_initial_max_stream_data_bidi_remote(config, 1000000);
|
|
397
|
+
quiche_config_set_initial_max_stream_data_uni(config, 1000000);
|
|
398
|
+
quiche_config_set_initial_max_streams_bidi(config, 100);
|
|
399
|
+
quiche_config_set_initial_max_streams_uni(config, 100);
|
|
400
|
+
quiche_config_set_disable_active_migration(config, true);
|
|
401
|
+
|
|
402
|
+
if (getenv("SSLKEYLOGFILE")) {
|
|
403
|
+
quiche_config_log_keys(config);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// ABC: old config creation here
|
|
407
|
+
|
|
408
|
+
uint8_t scid[LOCAL_CONN_ID_LEN];
|
|
409
|
+
int rng = open("/dev/urandom", O_RDONLY);
|
|
410
|
+
if (rng < 0) {
|
|
411
|
+
perror("failed to open /dev/urandom");
|
|
412
|
+
return -1;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
ssize_t rand_len = read(rng, &scid, sizeof(scid));
|
|
416
|
+
if (rand_len < 0) {
|
|
417
|
+
perror("failed to create connection ID");
|
|
418
|
+
return -1;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
struct conn_io *conn_io = malloc(sizeof(*conn_io));
|
|
422
|
+
if (conn_io == NULL) {
|
|
423
|
+
fprintf(stderr, "failed to allocate connection IO\n");
|
|
424
|
+
return -1;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
conn_io->local_addr_len = sizeof(conn_io->local_addr);
|
|
428
|
+
if (getsockname(sock, (struct sockaddr *)&conn_io->local_addr,
|
|
429
|
+
&conn_io->local_addr_len) != 0)
|
|
430
|
+
{
|
|
431
|
+
perror("failed to get local address of socket");
|
|
432
|
+
return -1;
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
quiche_conn *conn = quiche_connect(host, (const uint8_t *) scid, sizeof(scid),
|
|
436
|
+
(struct sockaddr *) &conn_io->local_addr,
|
|
437
|
+
conn_io->local_addr_len,
|
|
438
|
+
peer->ai_addr, peer->ai_addrlen, config);
|
|
439
|
+
|
|
440
|
+
if (conn == NULL) {
|
|
441
|
+
fprintf(stderr, "failed to create connection\n");
|
|
442
|
+
return -1;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
conn_io->sock = sock;
|
|
446
|
+
conn_io->conn = conn;
|
|
447
|
+
conn_io->host = host;
|
|
448
|
+
|
|
449
|
+
ev_io watcher;
|
|
450
|
+
|
|
451
|
+
struct ev_loop *loop = ev_default_loop(0);
|
|
452
|
+
|
|
453
|
+
ev_io_init(&watcher, recv_cb, conn_io->sock, EV_READ);
|
|
454
|
+
ev_io_start(loop, &watcher);
|
|
455
|
+
watcher.data = conn_io;
|
|
456
|
+
|
|
457
|
+
ev_init(&conn_io->timer, timeout_cb);
|
|
458
|
+
conn_io->timer.data = conn_io;
|
|
459
|
+
|
|
460
|
+
flush_egress(loop, conn_io);
|
|
461
|
+
|
|
462
|
+
ev_loop(loop, 0);
|
|
463
|
+
|
|
464
|
+
freeaddrinfo(peer);
|
|
465
|
+
|
|
466
|
+
quiche_h3_conn_free(conn_io->http3);
|
|
467
|
+
|
|
468
|
+
quiche_conn_free(conn);
|
|
469
|
+
|
|
470
|
+
quiche_config_free(config);
|
|
471
|
+
|
|
472
|
+
return 0;
|
|
473
|
+
}
|