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,529 +1,529 @@
|
|
|
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
|
-
|
|
73
|
-
struct sockaddr_storage peer_addr;
|
|
74
|
-
socklen_t peer_addr_len;
|
|
75
|
-
|
|
76
|
-
UT_hash_handle hh;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
static quiche_config *config = NULL;
|
|
80
|
-
|
|
81
|
-
static struct connections *conns = NULL;
|
|
82
|
-
|
|
83
|
-
static void timeout_cb(EV_P_ ev_timer *w, int revents);
|
|
84
|
-
|
|
85
|
-
static void debug_log(const char *line, void *argp) {
|
|
86
|
-
fprintf(stderr, "%s\n", line);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
static void flush_egress(struct ev_loop *loop, struct conn_io *conn_io) {
|
|
90
|
-
static uint8_t out[MAX_DATAGRAM_SIZE];
|
|
91
|
-
|
|
92
|
-
quiche_send_info send_info;
|
|
93
|
-
|
|
94
|
-
while (1) {
|
|
95
|
-
ssize_t written = quiche_conn_send(conn_io->conn, out, sizeof(out),
|
|
96
|
-
&send_info);
|
|
97
|
-
|
|
98
|
-
if (written == QUICHE_ERR_DONE) {
|
|
99
|
-
fprintf(stderr, "done writing\n");
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (written < 0) {
|
|
104
|
-
fprintf(stderr, "failed to create packet: %zd\n", written);
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
ssize_t sent = sendto(conn_io->sock, out, written, 0,
|
|
109
|
-
(struct sockaddr *) &send_info.to,
|
|
110
|
-
send_info.to_len);
|
|
111
|
-
|
|
112
|
-
if (sent != written) {
|
|
113
|
-
perror("failed to send");
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
fprintf(stderr, "sent %zd bytes\n", sent);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
double t = quiche_conn_timeout_as_nanos(conn_io->conn) / 1e9f;
|
|
121
|
-
conn_io->timer.repeat = t;
|
|
122
|
-
ev_timer_again(loop, &conn_io->timer);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
static void mint_token(const uint8_t *dcid, size_t dcid_len,
|
|
126
|
-
struct sockaddr_storage *addr, socklen_t addr_len,
|
|
127
|
-
uint8_t *token, size_t *token_len) {
|
|
128
|
-
memcpy(token, "quiche", sizeof("quiche") - 1);
|
|
129
|
-
memcpy(token + sizeof("quiche") - 1, addr, addr_len);
|
|
130
|
-
memcpy(token + sizeof("quiche") - 1 + addr_len, dcid, dcid_len);
|
|
131
|
-
|
|
132
|
-
*token_len = sizeof("quiche") - 1 + addr_len + dcid_len;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
static bool validate_token(const uint8_t *token, size_t token_len,
|
|
136
|
-
struct sockaddr_storage *addr, socklen_t addr_len,
|
|
137
|
-
uint8_t *odcid, size_t *odcid_len) {
|
|
138
|
-
if ((token_len < sizeof("quiche") - 1) ||
|
|
139
|
-
memcmp(token, "quiche", sizeof("quiche") - 1)) {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
token += sizeof("quiche") - 1;
|
|
144
|
-
token_len -= sizeof("quiche") - 1;
|
|
145
|
-
|
|
146
|
-
if ((token_len < addr_len) || memcmp(token, addr, addr_len)) {
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
token += addr_len;
|
|
151
|
-
token_len -= addr_len;
|
|
152
|
-
|
|
153
|
-
if (*odcid_len < token_len) {
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
memcpy(odcid, token, token_len);
|
|
158
|
-
*odcid_len = token_len;
|
|
159
|
-
|
|
160
|
-
return true;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
static uint8_t *gen_cid(uint8_t *cid, size_t cid_len) {
|
|
164
|
-
int rng = open("/dev/urandom", O_RDONLY);
|
|
165
|
-
if (rng < 0) {
|
|
166
|
-
perror("failed to open /dev/urandom");
|
|
167
|
-
return NULL;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
ssize_t rand_len = read(rng, cid, cid_len);
|
|
171
|
-
if (rand_len < 0) {
|
|
172
|
-
perror("failed to create connection ID");
|
|
173
|
-
return NULL;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return cid;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
static struct conn_io *create_conn(uint8_t *scid, size_t scid_len,
|
|
180
|
-
uint8_t *odcid, size_t odcid_len,
|
|
181
|
-
struct sockaddr *local_addr,
|
|
182
|
-
socklen_t local_addr_len,
|
|
183
|
-
struct sockaddr_storage *peer_addr,
|
|
184
|
-
socklen_t peer_addr_len)
|
|
185
|
-
{
|
|
186
|
-
struct conn_io *conn_io = calloc(1, sizeof(*conn_io));
|
|
187
|
-
if (conn_io == NULL) {
|
|
188
|
-
fprintf(stderr, "failed to allocate connection IO\n");
|
|
189
|
-
return NULL;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (scid_len != LOCAL_CONN_ID_LEN) {
|
|
193
|
-
fprintf(stderr, "failed, scid length too short\n");
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
memcpy(conn_io->cid, scid, LOCAL_CONN_ID_LEN);
|
|
197
|
-
|
|
198
|
-
quiche_conn *conn = quiche_accept(conn_io->cid, LOCAL_CONN_ID_LEN,
|
|
199
|
-
odcid, odcid_len,
|
|
200
|
-
local_addr,
|
|
201
|
-
local_addr_len,
|
|
202
|
-
(struct sockaddr *) peer_addr,
|
|
203
|
-
peer_addr_len,
|
|
204
|
-
config);
|
|
205
|
-
|
|
206
|
-
if (conn == NULL) {
|
|
207
|
-
fprintf(stderr, "failed to create connection\n");
|
|
208
|
-
return NULL;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
conn_io->sock = conns->sock;
|
|
212
|
-
conn_io->conn = conn;
|
|
213
|
-
|
|
214
|
-
memcpy(&conn_io->peer_addr, peer_addr, peer_addr_len);
|
|
215
|
-
conn_io->peer_addr_len = peer_addr_len;
|
|
216
|
-
|
|
217
|
-
ev_init(&conn_io->timer, timeout_cb);
|
|
218
|
-
conn_io->timer.data = conn_io;
|
|
219
|
-
|
|
220
|
-
HASH_ADD(hh, conns->h, cid, LOCAL_CONN_ID_LEN, conn_io);
|
|
221
|
-
|
|
222
|
-
fprintf(stderr, "new connection\n");
|
|
223
|
-
|
|
224
|
-
return conn_io;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
static void recv_cb(EV_P_ ev_io *w, int revents) {
|
|
228
|
-
struct conn_io *tmp, *conn_io = NULL;
|
|
229
|
-
|
|
230
|
-
static uint8_t buf[65535];
|
|
231
|
-
static uint8_t out[MAX_DATAGRAM_SIZE];
|
|
232
|
-
|
|
233
|
-
while (1) {
|
|
234
|
-
struct sockaddr_storage peer_addr;
|
|
235
|
-
socklen_t peer_addr_len = sizeof(peer_addr);
|
|
236
|
-
memset(&peer_addr, 0, peer_addr_len);
|
|
237
|
-
|
|
238
|
-
ssize_t read = recvfrom(conns->sock, buf, sizeof(buf), 0,
|
|
239
|
-
(struct sockaddr *) &peer_addr,
|
|
240
|
-
&peer_addr_len);
|
|
241
|
-
|
|
242
|
-
if (read < 0) {
|
|
243
|
-
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
|
|
244
|
-
fprintf(stderr, "recv would block\n");
|
|
245
|
-
break;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
perror("failed to read");
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
uint8_t type;
|
|
253
|
-
uint32_t version;
|
|
254
|
-
|
|
255
|
-
uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
|
|
256
|
-
size_t scid_len = sizeof(scid);
|
|
257
|
-
|
|
258
|
-
uint8_t dcid[QUICHE_MAX_CONN_ID_LEN];
|
|
259
|
-
size_t dcid_len = sizeof(dcid);
|
|
260
|
-
|
|
261
|
-
uint8_t odcid[QUICHE_MAX_CONN_ID_LEN];
|
|
262
|
-
size_t odcid_len = sizeof(odcid);
|
|
263
|
-
|
|
264
|
-
uint8_t token[MAX_TOKEN_LEN];
|
|
265
|
-
size_t token_len = sizeof(token);
|
|
266
|
-
|
|
267
|
-
int rc = quiche_header_info(buf, read, LOCAL_CONN_ID_LEN, &version,
|
|
268
|
-
&type, scid, &scid_len, dcid, &dcid_len,
|
|
269
|
-
token, &token_len);
|
|
270
|
-
if (rc < 0) {
|
|
271
|
-
fprintf(stderr, "failed to parse header: %d\n", rc);
|
|
272
|
-
continue;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
HASH_FIND(hh, conns->h, dcid, dcid_len, conn_io);
|
|
276
|
-
|
|
277
|
-
if (conn_io == NULL) {
|
|
278
|
-
if (!quiche_version_is_supported(version)) {
|
|
279
|
-
fprintf(stderr, "version negotiation\n");
|
|
280
|
-
|
|
281
|
-
ssize_t written = quiche_negotiate_version(scid, scid_len,
|
|
282
|
-
dcid, dcid_len,
|
|
283
|
-
out, sizeof(out));
|
|
284
|
-
|
|
285
|
-
if (written < 0) {
|
|
286
|
-
fprintf(stderr, "failed to create vneg packet: %zd\n",
|
|
287
|
-
written);
|
|
288
|
-
continue;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
ssize_t sent = sendto(conns->sock, out, written, 0,
|
|
292
|
-
(struct sockaddr *) &peer_addr,
|
|
293
|
-
peer_addr_len);
|
|
294
|
-
if (sent != written) {
|
|
295
|
-
perror("failed to send");
|
|
296
|
-
continue;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
fprintf(stderr, "sent %zd bytes\n", sent);
|
|
300
|
-
continue;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
if (token_len == 0) {
|
|
304
|
-
fprintf(stderr, "stateless retry\n");
|
|
305
|
-
|
|
306
|
-
mint_token(dcid, dcid_len, &peer_addr, peer_addr_len,
|
|
307
|
-
token, &token_len);
|
|
308
|
-
|
|
309
|
-
uint8_t new_cid[LOCAL_CONN_ID_LEN];
|
|
310
|
-
|
|
311
|
-
if (gen_cid(new_cid, LOCAL_CONN_ID_LEN) == NULL) {
|
|
312
|
-
continue;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
ssize_t written = quiche_retry(scid, scid_len,
|
|
316
|
-
dcid, dcid_len,
|
|
317
|
-
new_cid, LOCAL_CONN_ID_LEN,
|
|
318
|
-
token, token_len,
|
|
319
|
-
version, out, sizeof(out));
|
|
320
|
-
|
|
321
|
-
if (written < 0) {
|
|
322
|
-
fprintf(stderr, "failed to create retry packet: %zd\n",
|
|
323
|
-
written);
|
|
324
|
-
continue;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
ssize_t sent = sendto(conns->sock, out, written, 0,
|
|
328
|
-
(struct sockaddr *) &peer_addr,
|
|
329
|
-
peer_addr_len);
|
|
330
|
-
if (sent != written) {
|
|
331
|
-
perror("failed to send");
|
|
332
|
-
continue;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
fprintf(stderr, "sent %zd bytes\n", sent);
|
|
336
|
-
continue;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
if (!validate_token(token, token_len, &peer_addr, peer_addr_len,
|
|
341
|
-
odcid, &odcid_len)) {
|
|
342
|
-
fprintf(stderr, "invalid address validation token\n");
|
|
343
|
-
continue;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
conn_io = create_conn(dcid, dcid_len, odcid, odcid_len,
|
|
347
|
-
conns->local_addr, conns->local_addr_len,
|
|
348
|
-
&peer_addr, peer_addr_len);
|
|
349
|
-
|
|
350
|
-
if (conn_io == NULL) {
|
|
351
|
-
continue;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
quiche_recv_info recv_info = {
|
|
356
|
-
(struct sockaddr *)&peer_addr,
|
|
357
|
-
peer_addr_len,
|
|
358
|
-
|
|
359
|
-
conns->local_addr,
|
|
360
|
-
conns->local_addr_len,
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
ssize_t done = quiche_conn_recv(conn_io->conn, buf, read, &recv_info);
|
|
364
|
-
|
|
365
|
-
if (done < 0) {
|
|
366
|
-
fprintf(stderr, "failed to process packet: %zd\n", done);
|
|
367
|
-
continue;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
fprintf(stderr, "recv %zd bytes\n", done);
|
|
371
|
-
|
|
372
|
-
if (quiche_conn_is_established(conn_io->conn)) {
|
|
373
|
-
uint64_t s = 0;
|
|
374
|
-
|
|
375
|
-
quiche_stream_iter *readable = quiche_conn_readable(conn_io->conn);
|
|
376
|
-
|
|
377
|
-
while (quiche_stream_iter_next(readable, &s)) {
|
|
378
|
-
fprintf(stderr, "stream %" PRIu64 " is readable\n", s);
|
|
379
|
-
|
|
380
|
-
bool fin = false;
|
|
381
|
-
uint64_t error_code;
|
|
382
|
-
ssize_t recv_len = quiche_conn_stream_recv(conn_io->conn, s,
|
|
383
|
-
buf, sizeof(buf),
|
|
384
|
-
&fin, &error_code);
|
|
385
|
-
if (recv_len < 0) {
|
|
386
|
-
break;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
if (fin) {
|
|
390
|
-
static const char *resp = "byez\n";
|
|
391
|
-
uint64_t error_code;
|
|
392
|
-
quiche_conn_stream_send(conn_io->conn, s, (uint8_t *) resp,
|
|
393
|
-
5, true, &error_code);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
quiche_stream_iter_free(readable);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
HASH_ITER(hh, conns->h, conn_io, tmp) {
|
|
402
|
-
flush_egress(loop, conn_io);
|
|
403
|
-
|
|
404
|
-
if (quiche_conn_is_closed(conn_io->conn)) {
|
|
405
|
-
quiche_stats stats;
|
|
406
|
-
quiche_path_stats path_stats;
|
|
407
|
-
|
|
408
|
-
quiche_conn_stats(conn_io->conn, &stats);
|
|
409
|
-
quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
|
|
410
|
-
|
|
411
|
-
fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns cwnd=%zu\n",
|
|
412
|
-
stats.recv, stats.sent, stats.lost, path_stats.rtt, path_stats.cwnd);
|
|
413
|
-
|
|
414
|
-
HASH_DELETE(hh, conns->h, conn_io);
|
|
415
|
-
|
|
416
|
-
ev_timer_stop(loop, &conn_io->timer);
|
|
417
|
-
quiche_conn_free(conn_io->conn);
|
|
418
|
-
free(conn_io);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
static void timeout_cb(EV_P_ ev_timer *w, int revents) {
|
|
424
|
-
struct conn_io *conn_io = w->data;
|
|
425
|
-
quiche_conn_on_timeout(conn_io->conn);
|
|
426
|
-
|
|
427
|
-
fprintf(stderr, "timeout\n");
|
|
428
|
-
|
|
429
|
-
flush_egress(loop, conn_io);
|
|
430
|
-
|
|
431
|
-
if (quiche_conn_is_closed(conn_io->conn)) {
|
|
432
|
-
quiche_stats stats;
|
|
433
|
-
quiche_path_stats path_stats;
|
|
434
|
-
|
|
435
|
-
quiche_conn_stats(conn_io->conn, &stats);
|
|
436
|
-
quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
|
|
437
|
-
|
|
438
|
-
fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns cwnd=%zu\n",
|
|
439
|
-
stats.recv, stats.sent, stats.lost, path_stats.rtt, path_stats.cwnd);
|
|
440
|
-
|
|
441
|
-
HASH_DELETE(hh, conns->h, conn_io);
|
|
442
|
-
|
|
443
|
-
ev_timer_stop(loop, &conn_io->timer);
|
|
444
|
-
quiche_conn_free(conn_io->conn);
|
|
445
|
-
free(conn_io);
|
|
446
|
-
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
int main(int argc, char *argv[]) {
|
|
452
|
-
const char *host = argv[1];
|
|
453
|
-
const char *port = argv[2];
|
|
454
|
-
|
|
455
|
-
const struct addrinfo hints = {
|
|
456
|
-
.ai_family = PF_UNSPEC,
|
|
457
|
-
.ai_socktype = SOCK_DGRAM,
|
|
458
|
-
.ai_protocol = IPPROTO_UDP
|
|
459
|
-
};
|
|
460
|
-
|
|
461
|
-
quiche_enable_debug_logging(debug_log, NULL);
|
|
462
|
-
|
|
463
|
-
struct addrinfo *local;
|
|
464
|
-
if (getaddrinfo(host, port, &hints, &local) != 0) {
|
|
465
|
-
perror("failed to resolve host");
|
|
466
|
-
return -1;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
int sock = socket(local->ai_family, SOCK_DGRAM, 0);
|
|
470
|
-
if (sock < 0) {
|
|
471
|
-
perror("failed to create socket");
|
|
472
|
-
return -1;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
|
|
476
|
-
perror("failed to make socket non-blocking");
|
|
477
|
-
return -1;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
|
|
481
|
-
perror("failed to connect socket");
|
|
482
|
-
return -1;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
config = quiche_config_new(QUICHE_PROTOCOL_VERSION);
|
|
486
|
-
if (config == NULL) {
|
|
487
|
-
fprintf(stderr, "failed to create config\n");
|
|
488
|
-
return -1;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
quiche_config_load_cert_chain_from_pem_file(config, "./cert.crt");
|
|
492
|
-
quiche_config_load_priv_key_from_pem_file(config, "./cert.key");
|
|
493
|
-
|
|
494
|
-
quiche_config_set_application_protos(config,
|
|
495
|
-
(uint8_t *) "\x0ahq-interop\x05hq-29\x05hq-28\x05hq-27\x08http/0.9", 38);
|
|
496
|
-
|
|
497
|
-
quiche_config_set_max_idle_timeout(config, 5000);
|
|
498
|
-
quiche_config_set_max_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
|
499
|
-
quiche_config_set_max_send_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
|
500
|
-
quiche_config_set_initial_max_data(config, 10000000);
|
|
501
|
-
quiche_config_set_initial_max_stream_data_bidi_local(config, 1000000);
|
|
502
|
-
quiche_config_set_initial_max_stream_data_bidi_remote(config, 1000000);
|
|
503
|
-
quiche_config_set_initial_max_streams_bidi(config, 100);
|
|
504
|
-
quiche_config_set_cc_algorithm(config, QUICHE_CC_RENO);
|
|
505
|
-
|
|
506
|
-
struct connections c;
|
|
507
|
-
c.sock = sock;
|
|
508
|
-
c.h = NULL;
|
|
509
|
-
c.local_addr = local->ai_addr;
|
|
510
|
-
c.local_addr_len = local->ai_addrlen;
|
|
511
|
-
|
|
512
|
-
conns = &c;
|
|
513
|
-
|
|
514
|
-
ev_io watcher;
|
|
515
|
-
|
|
516
|
-
struct ev_loop *loop = ev_default_loop(0);
|
|
517
|
-
|
|
518
|
-
ev_io_init(&watcher, recv_cb, sock, EV_READ);
|
|
519
|
-
ev_io_start(loop, &watcher);
|
|
520
|
-
watcher.data = &c;
|
|
521
|
-
|
|
522
|
-
ev_loop(loop, 0);
|
|
523
|
-
|
|
524
|
-
freeaddrinfo(local);
|
|
525
|
-
|
|
526
|
-
quiche_config_free(config);
|
|
527
|
-
|
|
528
|
-
return 0;
|
|
529
|
-
}
|
|
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
|
+
|
|
73
|
+
struct sockaddr_storage peer_addr;
|
|
74
|
+
socklen_t peer_addr_len;
|
|
75
|
+
|
|
76
|
+
UT_hash_handle hh;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
static quiche_config *config = NULL;
|
|
80
|
+
|
|
81
|
+
static struct connections *conns = NULL;
|
|
82
|
+
|
|
83
|
+
static void timeout_cb(EV_P_ ev_timer *w, int revents);
|
|
84
|
+
|
|
85
|
+
static void debug_log(const char *line, void *argp) {
|
|
86
|
+
fprintf(stderr, "%s\n", line);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static void flush_egress(struct ev_loop *loop, struct conn_io *conn_io) {
|
|
90
|
+
static uint8_t out[MAX_DATAGRAM_SIZE];
|
|
91
|
+
|
|
92
|
+
quiche_send_info send_info;
|
|
93
|
+
|
|
94
|
+
while (1) {
|
|
95
|
+
ssize_t written = quiche_conn_send(conn_io->conn, out, sizeof(out),
|
|
96
|
+
&send_info);
|
|
97
|
+
|
|
98
|
+
if (written == QUICHE_ERR_DONE) {
|
|
99
|
+
fprintf(stderr, "done writing\n");
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (written < 0) {
|
|
104
|
+
fprintf(stderr, "failed to create packet: %zd\n", written);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
ssize_t sent = sendto(conn_io->sock, out, written, 0,
|
|
109
|
+
(struct sockaddr *) &send_info.to,
|
|
110
|
+
send_info.to_len);
|
|
111
|
+
|
|
112
|
+
if (sent != written) {
|
|
113
|
+
perror("failed to send");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
fprintf(stderr, "sent %zd bytes\n", sent);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
double t = quiche_conn_timeout_as_nanos(conn_io->conn) / 1e9f;
|
|
121
|
+
conn_io->timer.repeat = t;
|
|
122
|
+
ev_timer_again(loop, &conn_io->timer);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static void mint_token(const uint8_t *dcid, size_t dcid_len,
|
|
126
|
+
struct sockaddr_storage *addr, socklen_t addr_len,
|
|
127
|
+
uint8_t *token, size_t *token_len) {
|
|
128
|
+
memcpy(token, "quiche", sizeof("quiche") - 1);
|
|
129
|
+
memcpy(token + sizeof("quiche") - 1, addr, addr_len);
|
|
130
|
+
memcpy(token + sizeof("quiche") - 1 + addr_len, dcid, dcid_len);
|
|
131
|
+
|
|
132
|
+
*token_len = sizeof("quiche") - 1 + addr_len + dcid_len;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static bool validate_token(const uint8_t *token, size_t token_len,
|
|
136
|
+
struct sockaddr_storage *addr, socklen_t addr_len,
|
|
137
|
+
uint8_t *odcid, size_t *odcid_len) {
|
|
138
|
+
if ((token_len < sizeof("quiche") - 1) ||
|
|
139
|
+
memcmp(token, "quiche", sizeof("quiche") - 1)) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
token += sizeof("quiche") - 1;
|
|
144
|
+
token_len -= sizeof("quiche") - 1;
|
|
145
|
+
|
|
146
|
+
if ((token_len < addr_len) || memcmp(token, addr, addr_len)) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
token += addr_len;
|
|
151
|
+
token_len -= addr_len;
|
|
152
|
+
|
|
153
|
+
if (*odcid_len < token_len) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
memcpy(odcid, token, token_len);
|
|
158
|
+
*odcid_len = token_len;
|
|
159
|
+
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static uint8_t *gen_cid(uint8_t *cid, size_t cid_len) {
|
|
164
|
+
int rng = open("/dev/urandom", O_RDONLY);
|
|
165
|
+
if (rng < 0) {
|
|
166
|
+
perror("failed to open /dev/urandom");
|
|
167
|
+
return NULL;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
ssize_t rand_len = read(rng, cid, cid_len);
|
|
171
|
+
if (rand_len < 0) {
|
|
172
|
+
perror("failed to create connection ID");
|
|
173
|
+
return NULL;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return cid;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static struct conn_io *create_conn(uint8_t *scid, size_t scid_len,
|
|
180
|
+
uint8_t *odcid, size_t odcid_len,
|
|
181
|
+
struct sockaddr *local_addr,
|
|
182
|
+
socklen_t local_addr_len,
|
|
183
|
+
struct sockaddr_storage *peer_addr,
|
|
184
|
+
socklen_t peer_addr_len)
|
|
185
|
+
{
|
|
186
|
+
struct conn_io *conn_io = calloc(1, sizeof(*conn_io));
|
|
187
|
+
if (conn_io == NULL) {
|
|
188
|
+
fprintf(stderr, "failed to allocate connection IO\n");
|
|
189
|
+
return NULL;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (scid_len != LOCAL_CONN_ID_LEN) {
|
|
193
|
+
fprintf(stderr, "failed, scid length too short\n");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
memcpy(conn_io->cid, scid, LOCAL_CONN_ID_LEN);
|
|
197
|
+
|
|
198
|
+
quiche_conn *conn = quiche_accept(conn_io->cid, LOCAL_CONN_ID_LEN,
|
|
199
|
+
odcid, odcid_len,
|
|
200
|
+
local_addr,
|
|
201
|
+
local_addr_len,
|
|
202
|
+
(struct sockaddr *) peer_addr,
|
|
203
|
+
peer_addr_len,
|
|
204
|
+
config);
|
|
205
|
+
|
|
206
|
+
if (conn == NULL) {
|
|
207
|
+
fprintf(stderr, "failed to create connection\n");
|
|
208
|
+
return NULL;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
conn_io->sock = conns->sock;
|
|
212
|
+
conn_io->conn = conn;
|
|
213
|
+
|
|
214
|
+
memcpy(&conn_io->peer_addr, peer_addr, peer_addr_len);
|
|
215
|
+
conn_io->peer_addr_len = peer_addr_len;
|
|
216
|
+
|
|
217
|
+
ev_init(&conn_io->timer, timeout_cb);
|
|
218
|
+
conn_io->timer.data = conn_io;
|
|
219
|
+
|
|
220
|
+
HASH_ADD(hh, conns->h, cid, LOCAL_CONN_ID_LEN, conn_io);
|
|
221
|
+
|
|
222
|
+
fprintf(stderr, "new connection\n");
|
|
223
|
+
|
|
224
|
+
return conn_io;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static void recv_cb(EV_P_ ev_io *w, int revents) {
|
|
228
|
+
struct conn_io *tmp, *conn_io = NULL;
|
|
229
|
+
|
|
230
|
+
static uint8_t buf[65535];
|
|
231
|
+
static uint8_t out[MAX_DATAGRAM_SIZE];
|
|
232
|
+
|
|
233
|
+
while (1) {
|
|
234
|
+
struct sockaddr_storage peer_addr;
|
|
235
|
+
socklen_t peer_addr_len = sizeof(peer_addr);
|
|
236
|
+
memset(&peer_addr, 0, peer_addr_len);
|
|
237
|
+
|
|
238
|
+
ssize_t read = recvfrom(conns->sock, buf, sizeof(buf), 0,
|
|
239
|
+
(struct sockaddr *) &peer_addr,
|
|
240
|
+
&peer_addr_len);
|
|
241
|
+
|
|
242
|
+
if (read < 0) {
|
|
243
|
+
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
|
|
244
|
+
fprintf(stderr, "recv would block\n");
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
perror("failed to read");
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
uint8_t type;
|
|
253
|
+
uint32_t version;
|
|
254
|
+
|
|
255
|
+
uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
|
|
256
|
+
size_t scid_len = sizeof(scid);
|
|
257
|
+
|
|
258
|
+
uint8_t dcid[QUICHE_MAX_CONN_ID_LEN];
|
|
259
|
+
size_t dcid_len = sizeof(dcid);
|
|
260
|
+
|
|
261
|
+
uint8_t odcid[QUICHE_MAX_CONN_ID_LEN];
|
|
262
|
+
size_t odcid_len = sizeof(odcid);
|
|
263
|
+
|
|
264
|
+
uint8_t token[MAX_TOKEN_LEN];
|
|
265
|
+
size_t token_len = sizeof(token);
|
|
266
|
+
|
|
267
|
+
int rc = quiche_header_info(buf, read, LOCAL_CONN_ID_LEN, &version,
|
|
268
|
+
&type, scid, &scid_len, dcid, &dcid_len,
|
|
269
|
+
token, &token_len);
|
|
270
|
+
if (rc < 0) {
|
|
271
|
+
fprintf(stderr, "failed to parse header: %d\n", rc);
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
HASH_FIND(hh, conns->h, dcid, dcid_len, conn_io);
|
|
276
|
+
|
|
277
|
+
if (conn_io == NULL) {
|
|
278
|
+
if (!quiche_version_is_supported(version)) {
|
|
279
|
+
fprintf(stderr, "version negotiation\n");
|
|
280
|
+
|
|
281
|
+
ssize_t written = quiche_negotiate_version(scid, scid_len,
|
|
282
|
+
dcid, dcid_len,
|
|
283
|
+
out, sizeof(out));
|
|
284
|
+
|
|
285
|
+
if (written < 0) {
|
|
286
|
+
fprintf(stderr, "failed to create vneg packet: %zd\n",
|
|
287
|
+
written);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
ssize_t sent = sendto(conns->sock, out, written, 0,
|
|
292
|
+
(struct sockaddr *) &peer_addr,
|
|
293
|
+
peer_addr_len);
|
|
294
|
+
if (sent != written) {
|
|
295
|
+
perror("failed to send");
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
fprintf(stderr, "sent %zd bytes\n", sent);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (token_len == 0) {
|
|
304
|
+
fprintf(stderr, "stateless retry\n");
|
|
305
|
+
|
|
306
|
+
mint_token(dcid, dcid_len, &peer_addr, peer_addr_len,
|
|
307
|
+
token, &token_len);
|
|
308
|
+
|
|
309
|
+
uint8_t new_cid[LOCAL_CONN_ID_LEN];
|
|
310
|
+
|
|
311
|
+
if (gen_cid(new_cid, LOCAL_CONN_ID_LEN) == NULL) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
ssize_t written = quiche_retry(scid, scid_len,
|
|
316
|
+
dcid, dcid_len,
|
|
317
|
+
new_cid, LOCAL_CONN_ID_LEN,
|
|
318
|
+
token, token_len,
|
|
319
|
+
version, out, sizeof(out));
|
|
320
|
+
|
|
321
|
+
if (written < 0) {
|
|
322
|
+
fprintf(stderr, "failed to create retry packet: %zd\n",
|
|
323
|
+
written);
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
ssize_t sent = sendto(conns->sock, out, written, 0,
|
|
328
|
+
(struct sockaddr *) &peer_addr,
|
|
329
|
+
peer_addr_len);
|
|
330
|
+
if (sent != written) {
|
|
331
|
+
perror("failed to send");
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
fprintf(stderr, "sent %zd bytes\n", sent);
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
if (!validate_token(token, token_len, &peer_addr, peer_addr_len,
|
|
341
|
+
odcid, &odcid_len)) {
|
|
342
|
+
fprintf(stderr, "invalid address validation token\n");
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
conn_io = create_conn(dcid, dcid_len, odcid, odcid_len,
|
|
347
|
+
conns->local_addr, conns->local_addr_len,
|
|
348
|
+
&peer_addr, peer_addr_len);
|
|
349
|
+
|
|
350
|
+
if (conn_io == NULL) {
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
quiche_recv_info recv_info = {
|
|
356
|
+
(struct sockaddr *)&peer_addr,
|
|
357
|
+
peer_addr_len,
|
|
358
|
+
|
|
359
|
+
conns->local_addr,
|
|
360
|
+
conns->local_addr_len,
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
ssize_t done = quiche_conn_recv(conn_io->conn, buf, read, &recv_info);
|
|
364
|
+
|
|
365
|
+
if (done < 0) {
|
|
366
|
+
fprintf(stderr, "failed to process packet: %zd\n", done);
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
fprintf(stderr, "recv %zd bytes\n", done);
|
|
371
|
+
|
|
372
|
+
if (quiche_conn_is_established(conn_io->conn)) {
|
|
373
|
+
uint64_t s = 0;
|
|
374
|
+
|
|
375
|
+
quiche_stream_iter *readable = quiche_conn_readable(conn_io->conn);
|
|
376
|
+
|
|
377
|
+
while (quiche_stream_iter_next(readable, &s)) {
|
|
378
|
+
fprintf(stderr, "stream %" PRIu64 " is readable\n", s);
|
|
379
|
+
|
|
380
|
+
bool fin = false;
|
|
381
|
+
uint64_t error_code;
|
|
382
|
+
ssize_t recv_len = quiche_conn_stream_recv(conn_io->conn, s,
|
|
383
|
+
buf, sizeof(buf),
|
|
384
|
+
&fin, &error_code);
|
|
385
|
+
if (recv_len < 0) {
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (fin) {
|
|
390
|
+
static const char *resp = "byez\n";
|
|
391
|
+
uint64_t error_code;
|
|
392
|
+
quiche_conn_stream_send(conn_io->conn, s, (uint8_t *) resp,
|
|
393
|
+
5, true, &error_code);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
quiche_stream_iter_free(readable);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
HASH_ITER(hh, conns->h, conn_io, tmp) {
|
|
402
|
+
flush_egress(loop, conn_io);
|
|
403
|
+
|
|
404
|
+
if (quiche_conn_is_closed(conn_io->conn)) {
|
|
405
|
+
quiche_stats stats;
|
|
406
|
+
quiche_path_stats path_stats;
|
|
407
|
+
|
|
408
|
+
quiche_conn_stats(conn_io->conn, &stats);
|
|
409
|
+
quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
|
|
410
|
+
|
|
411
|
+
fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns cwnd=%zu\n",
|
|
412
|
+
stats.recv, stats.sent, stats.lost, path_stats.rtt, path_stats.cwnd);
|
|
413
|
+
|
|
414
|
+
HASH_DELETE(hh, conns->h, conn_io);
|
|
415
|
+
|
|
416
|
+
ev_timer_stop(loop, &conn_io->timer);
|
|
417
|
+
quiche_conn_free(conn_io->conn);
|
|
418
|
+
free(conn_io);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
static void timeout_cb(EV_P_ ev_timer *w, int revents) {
|
|
424
|
+
struct conn_io *conn_io = w->data;
|
|
425
|
+
quiche_conn_on_timeout(conn_io->conn);
|
|
426
|
+
|
|
427
|
+
fprintf(stderr, "timeout\n");
|
|
428
|
+
|
|
429
|
+
flush_egress(loop, conn_io);
|
|
430
|
+
|
|
431
|
+
if (quiche_conn_is_closed(conn_io->conn)) {
|
|
432
|
+
quiche_stats stats;
|
|
433
|
+
quiche_path_stats path_stats;
|
|
434
|
+
|
|
435
|
+
quiche_conn_stats(conn_io->conn, &stats);
|
|
436
|
+
quiche_conn_path_stats(conn_io->conn, 0, &path_stats);
|
|
437
|
+
|
|
438
|
+
fprintf(stderr, "connection closed, recv=%zu sent=%zu lost=%zu rtt=%" PRIu64 "ns cwnd=%zu\n",
|
|
439
|
+
stats.recv, stats.sent, stats.lost, path_stats.rtt, path_stats.cwnd);
|
|
440
|
+
|
|
441
|
+
HASH_DELETE(hh, conns->h, conn_io);
|
|
442
|
+
|
|
443
|
+
ev_timer_stop(loop, &conn_io->timer);
|
|
444
|
+
quiche_conn_free(conn_io->conn);
|
|
445
|
+
free(conn_io);
|
|
446
|
+
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
int main(int argc, char *argv[]) {
|
|
452
|
+
const char *host = argv[1];
|
|
453
|
+
const char *port = argv[2];
|
|
454
|
+
|
|
455
|
+
const struct addrinfo hints = {
|
|
456
|
+
.ai_family = PF_UNSPEC,
|
|
457
|
+
.ai_socktype = SOCK_DGRAM,
|
|
458
|
+
.ai_protocol = IPPROTO_UDP
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
quiche_enable_debug_logging(debug_log, NULL);
|
|
462
|
+
|
|
463
|
+
struct addrinfo *local;
|
|
464
|
+
if (getaddrinfo(host, port, &hints, &local) != 0) {
|
|
465
|
+
perror("failed to resolve host");
|
|
466
|
+
return -1;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
int sock = socket(local->ai_family, SOCK_DGRAM, 0);
|
|
470
|
+
if (sock < 0) {
|
|
471
|
+
perror("failed to create socket");
|
|
472
|
+
return -1;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
|
|
476
|
+
perror("failed to make socket non-blocking");
|
|
477
|
+
return -1;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
|
|
481
|
+
perror("failed to connect socket");
|
|
482
|
+
return -1;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
config = quiche_config_new(QUICHE_PROTOCOL_VERSION);
|
|
486
|
+
if (config == NULL) {
|
|
487
|
+
fprintf(stderr, "failed to create config\n");
|
|
488
|
+
return -1;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
quiche_config_load_cert_chain_from_pem_file(config, "./cert.crt");
|
|
492
|
+
quiche_config_load_priv_key_from_pem_file(config, "./cert.key");
|
|
493
|
+
|
|
494
|
+
quiche_config_set_application_protos(config,
|
|
495
|
+
(uint8_t *) "\x0ahq-interop\x05hq-29\x05hq-28\x05hq-27\x08http/0.9", 38);
|
|
496
|
+
|
|
497
|
+
quiche_config_set_max_idle_timeout(config, 5000);
|
|
498
|
+
quiche_config_set_max_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
|
499
|
+
quiche_config_set_max_send_udp_payload_size(config, MAX_DATAGRAM_SIZE);
|
|
500
|
+
quiche_config_set_initial_max_data(config, 10000000);
|
|
501
|
+
quiche_config_set_initial_max_stream_data_bidi_local(config, 1000000);
|
|
502
|
+
quiche_config_set_initial_max_stream_data_bidi_remote(config, 1000000);
|
|
503
|
+
quiche_config_set_initial_max_streams_bidi(config, 100);
|
|
504
|
+
quiche_config_set_cc_algorithm(config, QUICHE_CC_RENO);
|
|
505
|
+
|
|
506
|
+
struct connections c;
|
|
507
|
+
c.sock = sock;
|
|
508
|
+
c.h = NULL;
|
|
509
|
+
c.local_addr = local->ai_addr;
|
|
510
|
+
c.local_addr_len = local->ai_addrlen;
|
|
511
|
+
|
|
512
|
+
conns = &c;
|
|
513
|
+
|
|
514
|
+
ev_io watcher;
|
|
515
|
+
|
|
516
|
+
struct ev_loop *loop = ev_default_loop(0);
|
|
517
|
+
|
|
518
|
+
ev_io_init(&watcher, recv_cb, sock, EV_READ);
|
|
519
|
+
ev_io_start(loop, &watcher);
|
|
520
|
+
watcher.data = &c;
|
|
521
|
+
|
|
522
|
+
ev_loop(loop, 0);
|
|
523
|
+
|
|
524
|
+
freeaddrinfo(local);
|
|
525
|
+
|
|
526
|
+
quiche_config_free(config);
|
|
527
|
+
|
|
528
|
+
return 0;
|
|
529
|
+
}
|