quic 0.0.7 → 0.0.8
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +51 -2
- data/ext/quic/connection_client.c +271 -72
- data/ext/quic/connection_common.c +347 -28
- data/ext/quic/connection_common.h +115 -15
- data/ext/quic/connection_server.c +20 -7
- data/ext/quic/endpoint.c +28 -6
- data/ext/quic/ext_api.c +12 -0
- data/ext/quic/include/quic/ext.h +5 -0
- data/ext/quic/quic.c +8 -0
- data/ext/quic/quic.h +1 -0
- data/ext/quic/stream.c +2 -2
- data/lib/quic/connection.rb +29 -11
- data/lib/quic/settings.rb +50 -4
- data/lib/quic/version.rb +1 -1
- data/sig/quic.rbs +96 -2
- metadata +1 -1
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
#include "endpoint.h"
|
|
3
3
|
#include "stream.h"
|
|
4
4
|
|
|
5
|
+
#include <errno.h>
|
|
6
|
+
#include <fcntl.h>
|
|
5
7
|
#include <netinet/in.h>
|
|
6
8
|
#include <string.h>
|
|
7
9
|
#include <sys/socket.h>
|
|
8
10
|
#include <time.h>
|
|
11
|
+
#include <unistd.h>
|
|
9
12
|
|
|
10
13
|
#include <openssl/rand.h>
|
|
11
14
|
|
|
@@ -57,8 +60,8 @@ static void quic_conn_free(void *ptr) {
|
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
static size_t quic_conn_size(const void *ptr) {
|
|
60
|
-
(
|
|
61
|
-
return sizeof(quic_conn_t);
|
|
63
|
+
const quic_conn_t *c = (const quic_conn_t *)ptr;
|
|
64
|
+
return sizeof(quic_conn_t) + (c->qlog_buf != NULL ? QUIC_QLOG_BUFLEN : 0);
|
|
62
65
|
}
|
|
63
66
|
|
|
64
67
|
/* owner is the object wrapping this struct, so it needs no mark. */
|
|
@@ -69,6 +72,7 @@ static void quic_conn_mark(void *ptr) {
|
|
|
69
72
|
rb_gc_mark_movable(c->endpoint_owner);
|
|
70
73
|
rb_gc_mark_movable(c->error);
|
|
71
74
|
rb_gc_mark_movable(c->closebuf);
|
|
75
|
+
rb_gc_mark_movable(c->qlog_path);
|
|
72
76
|
}
|
|
73
77
|
|
|
74
78
|
/* GC.compact may relocate the owner connection object. Follow it so the
|
|
@@ -83,6 +87,7 @@ static void quic_conn_compact(void *ptr) {
|
|
|
83
87
|
c->endpoint_owner = rb_gc_location(c->endpoint_owner);
|
|
84
88
|
c->error = rb_gc_location(c->error);
|
|
85
89
|
c->closebuf = rb_gc_location(c->closebuf);
|
|
90
|
+
c->qlog_path = rb_gc_location(c->qlog_path);
|
|
86
91
|
}
|
|
87
92
|
|
|
88
93
|
const rb_data_type_t quic_conn_data_type = {
|
|
@@ -107,7 +112,13 @@ quic_conn_alloc(VALUE klass) {
|
|
|
107
112
|
c->endpoint_owner = Qnil;
|
|
108
113
|
c->error = Qnil;
|
|
109
114
|
c->closebuf = Qnil;
|
|
110
|
-
|
|
115
|
+
c->qlog_path = Qnil;
|
|
116
|
+
c->qlog_fd = -1;
|
|
117
|
+
VALUE self = TypedData_Wrap_Struct(klass, &quic_conn_data_type, c);
|
|
118
|
+
/* Set here so that it names the wrapping object even on one made with
|
|
119
|
+
.allocate, which never reaches a constructor. */
|
|
120
|
+
c->owner = self;
|
|
121
|
+
return self;
|
|
111
122
|
}
|
|
112
123
|
|
|
113
124
|
/* Re-raise the exception a callback left in c->pending_exc, if any. Called
|
|
@@ -138,7 +149,21 @@ quic_conn_t *quic_conn_get(VALUE self) {
|
|
|
138
149
|
return c;
|
|
139
150
|
}
|
|
140
151
|
|
|
141
|
-
|
|
152
|
+
quic_conn_t *quic_conn_get_open(VALUE self) {
|
|
153
|
+
quic_conn_t *c = quic_conn_get(self);
|
|
154
|
+
if (c->conn == NULL) {
|
|
155
|
+
rb_raise(rb_eQUICErrorInvalidState,
|
|
156
|
+
c->rebuild_failed
|
|
157
|
+
? "the connection could not be rebuilt after Version "
|
|
158
|
+
"Negotiation"
|
|
159
|
+
: "the connection was never opened");
|
|
160
|
+
}
|
|
161
|
+
return c;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
ngtcp2_conn *quic_conn_ptr(VALUE owner) {
|
|
165
|
+
return quic_conn_get_open(owner)->conn;
|
|
166
|
+
}
|
|
142
167
|
|
|
143
168
|
ngtcp2_tstamp quic_now(void) {
|
|
144
169
|
struct timespec ts;
|
|
@@ -253,8 +278,99 @@ void quic_fill_transport_params(ngtcp2_transport_params *params, VALUE tp) {
|
|
|
253
278
|
quic_get_uint64_field(tp, "active_connection_id_limit");
|
|
254
279
|
}
|
|
255
280
|
|
|
256
|
-
|
|
281
|
+
/* The version sym (:v1 or :v2) names, or 0 for any other value. */
|
|
282
|
+
static uint32_t quic_version_from_sym(VALUE sym) {
|
|
283
|
+
if (sym == ID2SYM(rb_intern("v1")))
|
|
284
|
+
return NGTCP2_PROTO_VER_V1;
|
|
285
|
+
if (sym == ID2SYM(rb_intern("v2")))
|
|
286
|
+
return NGTCP2_PROTO_VER_V2;
|
|
287
|
+
return 0;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/* Read the list settings_v.<name> into out. Without duplicates, no more
|
|
291
|
+
than two entries get through: a third valid one repeats one of them. */
|
|
292
|
+
static size_t quic_read_version_list(VALUE settings_v, const char *name,
|
|
293
|
+
uint32_t out[2]) {
|
|
294
|
+
VALUE ary = rb_funcall(settings_v, rb_intern(name), 0);
|
|
295
|
+
long len = RB_TYPE_P(ary, T_ARRAY) ? RARRAY_LEN(ary) : 0;
|
|
296
|
+
bool valid = len > 0;
|
|
297
|
+
for (long i = 0; valid && i < len; i++) {
|
|
298
|
+
valid = quic_version_from_sym(RARRAY_AREF(ary, i)) != 0;
|
|
299
|
+
}
|
|
300
|
+
if (!valid) {
|
|
301
|
+
rb_raise(rb_eArgError,
|
|
302
|
+
"%s must be a non-empty Array of :v1 and :v2 (got %+" PRIsVALUE
|
|
303
|
+
")",
|
|
304
|
+
name, ary);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
size_t n = 0;
|
|
308
|
+
for (long i = 0; i < len; i++) {
|
|
309
|
+
VALUE sym = RARRAY_AREF(ary, i);
|
|
310
|
+
uint32_t version = quic_version_from_sym(sym);
|
|
311
|
+
for (size_t j = 0; j < n; j++) {
|
|
312
|
+
if (out[j] == version) {
|
|
313
|
+
rb_raise(rb_eArgError, "%s lists %+" PRIsVALUE " more than once", name,
|
|
314
|
+
sym);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
out[n++] = version;
|
|
318
|
+
}
|
|
319
|
+
return n;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
void quic_read_versions(VALUE settings_v, quic_versions_buf *vbuf) {
|
|
323
|
+
VALUE version = rb_funcall(settings_v, rb_intern("version"), 0);
|
|
324
|
+
vbuf->chosen = quic_version_from_sym(version);
|
|
325
|
+
if (vbuf->chosen == 0) {
|
|
326
|
+
rb_raise(rb_eArgError, "version must be :v1 or :v2 (got %+" PRIsVALUE ")",
|
|
327
|
+
version);
|
|
328
|
+
}
|
|
329
|
+
vbuf->preferredlen =
|
|
330
|
+
quic_read_version_list(settings_v, "preferred_versions", vbuf->preferred);
|
|
331
|
+
vbuf->availablelen =
|
|
332
|
+
quic_read_version_list(settings_v, "available_versions", vbuf->available);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
bool quic_versions_accept(const quic_versions_buf *vbuf, uint32_t version) {
|
|
336
|
+
for (size_t i = 0; i < vbuf->preferredlen; i++) {
|
|
337
|
+
if (vbuf->preferred[i] == version)
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
for (size_t i = 0; i < vbuf->availablelen; i++) {
|
|
341
|
+
if (vbuf->available[i] == version)
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
uint64_t quic_read_qlog_max_bytes(VALUE settings_v) {
|
|
348
|
+
VALUE v = rb_funcall(settings_v, rb_intern("qlog_max_bytes"), 0);
|
|
349
|
+
if (NIL_P(v))
|
|
350
|
+
return 0;
|
|
351
|
+
/* Sorted out before any conversion: NUM2ULL would round 1.5 down, raise
|
|
352
|
+
TypeError or RangeError rather than ArgumentError, and take 2**63. */
|
|
353
|
+
if (RB_INTEGER_TYPE_P(v) && RTEST(rb_funcall(v, '>', 1, INT2FIX(0))) &&
|
|
354
|
+
RTEST(rb_funcall(v, rb_intern("<="), 1, LL2NUM(INT64_MAX)))) {
|
|
355
|
+
return (uint64_t)NUM2LL(v);
|
|
356
|
+
}
|
|
357
|
+
rb_raise(rb_eArgError,
|
|
358
|
+
"qlog_max_bytes must be a positive Integer or nil (got %+" PRIsVALUE
|
|
359
|
+
")",
|
|
360
|
+
v);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
void quic_fill_settings(ngtcp2_settings *settings, VALUE st,
|
|
364
|
+
quic_versions_buf *vbuf) {
|
|
365
|
+
quic_read_versions(st, vbuf);
|
|
366
|
+
/* Only checked here, so that a wrong value raises when a connection or
|
|
367
|
+
an Endpoint is made, with or without qlog_dir. */
|
|
368
|
+
quic_read_qlog_max_bytes(st);
|
|
257
369
|
ngtcp2_settings_default(settings);
|
|
370
|
+
settings->preferred_versions = vbuf->preferred;
|
|
371
|
+
settings->preferred_versionslen = vbuf->preferredlen;
|
|
372
|
+
settings->available_versions = vbuf->available;
|
|
373
|
+
settings->available_versionslen = vbuf->availablelen;
|
|
258
374
|
settings->initial_ts = quic_now();
|
|
259
375
|
settings->cc_algo =
|
|
260
376
|
quic_cc_algo_from_sym(rb_funcall(st, rb_intern("cc_algo"), 0));
|
|
@@ -268,41 +384,150 @@ void quic_fill_settings(ngtcp2_settings *settings, VALUE st) {
|
|
|
268
384
|
settings->no_pmtud = RTEST(no_pmtud) ? 1 : 0;
|
|
269
385
|
}
|
|
270
386
|
|
|
271
|
-
/*
|
|
387
|
+
/* Write len bytes of data to the end of c's qlog file, retrying short
|
|
388
|
+
writes and EINTR. Moves qlog_committed past the last '\n' written: ngtcp2
|
|
389
|
+
writes '\n' only at the end of a record, so that is where the last whole
|
|
390
|
+
record ends. On a failure, truncates the file back to qlog_committed and
|
|
391
|
+
returns false; the caller closes it. */
|
|
392
|
+
static bool quic_qlog_write_all(quic_conn_t *c, const uint8_t *data,
|
|
393
|
+
size_t len) {
|
|
394
|
+
off_t start = c->qlog_written;
|
|
395
|
+
size_t done = 0;
|
|
396
|
+
while (done < len) {
|
|
397
|
+
ssize_t n = write(c->qlog_fd, data + done, len - done);
|
|
398
|
+
if (n < 0 && errno == EINTR)
|
|
399
|
+
continue;
|
|
400
|
+
if (n <= 0) {
|
|
401
|
+
if (ftruncate(c->qlog_fd, c->qlog_committed) != 0) {
|
|
402
|
+
/* Nothing more to do: the caller closes the file all the same. */
|
|
403
|
+
}
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
for (size_t i = done + (size_t)n; i > done; i--) {
|
|
407
|
+
if (data[i - 1] == '\n') {
|
|
408
|
+
c->qlog_committed = start + (off_t)i;
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
done += (size_t)n;
|
|
413
|
+
c->qlog_written = start + (off_t)done;
|
|
414
|
+
}
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/* Write out the records in c's qlog buffer. Returns false when that failed
|
|
419
|
+
and closed the file. */
|
|
420
|
+
static bool quic_qlog_flush(quic_conn_t *c) {
|
|
421
|
+
if (c->qlog_fd < 0)
|
|
422
|
+
return false;
|
|
423
|
+
size_t len = c->qlog_buflen;
|
|
424
|
+
c->qlog_buflen = 0;
|
|
425
|
+
if (len > 0 && !quic_qlog_write_all(c, c->qlog_buf, len)) {
|
|
426
|
+
quic_conn_close_qlog(c);
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/* ngtcp2_settings.qlog_write: data is one whole record, or nothing with
|
|
433
|
+
NGTCP2_QLOG_WRITE_FLAG_FIN. Touches no Ruby object, so it needs no
|
|
272
434
|
rb_protect. A write error only costs qlog. */
|
|
273
435
|
static void quic_qlog_write_cb(void *user_data, uint32_t flags,
|
|
274
436
|
const void *data, size_t datalen) {
|
|
275
437
|
quic_conn_t *c = (quic_conn_t *)user_data;
|
|
276
|
-
|
|
438
|
+
/* Closed already: ngtcp2 goes on logging after the connection ends. */
|
|
439
|
+
if (c->qlog_fd < 0)
|
|
277
440
|
return;
|
|
278
|
-
|
|
441
|
+
if (c->qlog_max_bytes > 0) {
|
|
442
|
+
/* Before the record goes anywhere. Every record taken so far fits, so
|
|
443
|
+
writing out the buffer on closing stays within the limit too. In
|
|
444
|
+
uint64_t, so that nothing overflows near INT64_MAX. */
|
|
445
|
+
uint64_t used = (uint64_t)c->qlog_written + c->qlog_buflen;
|
|
446
|
+
if (used > c->qlog_max_bytes || c->qlog_max_bytes - used < datalen) {
|
|
447
|
+
/* This record and every one after it are dropped. Closing frees the
|
|
448
|
+
buffer, so nothing below may run. */
|
|
449
|
+
quic_conn_close_qlog(c);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (datalen > QUIC_QLOG_BUFLEN - c->qlog_buflen) {
|
|
454
|
+
/* A failure closed the file and freed the buffer. */
|
|
455
|
+
if (!quic_qlog_flush(c))
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (datalen > QUIC_QLOG_BUFLEN) {
|
|
459
|
+
/* Larger than the buffer (no ngtcp2 record is): written as it is, after
|
|
460
|
+
the records before it. */
|
|
461
|
+
if (!quic_qlog_write_all(c, (const uint8_t *)data, datalen)) {
|
|
462
|
+
quic_conn_close_qlog(c);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
} else if (datalen > 0) {
|
|
466
|
+
memcpy(c->qlog_buf + c->qlog_buflen, data, datalen);
|
|
467
|
+
c->qlog_buflen += datalen;
|
|
468
|
+
}
|
|
279
469
|
if (flags & NGTCP2_QLOG_WRITE_FLAG_FIN)
|
|
280
470
|
quic_conn_close_qlog(c);
|
|
281
471
|
}
|
|
282
472
|
|
|
283
473
|
void quic_conn_close_qlog(quic_conn_t *c) {
|
|
284
|
-
if (c->
|
|
474
|
+
if (c->qlog_fd < 0)
|
|
285
475
|
return;
|
|
286
|
-
|
|
287
|
-
c->
|
|
476
|
+
size_t len = c->qlog_buflen;
|
|
477
|
+
c->qlog_buflen = 0;
|
|
478
|
+
if (len > 0)
|
|
479
|
+
quic_qlog_write_all(c, c->qlog_buf, len);
|
|
480
|
+
close(c->qlog_fd);
|
|
481
|
+
/* Right away: a reconnect after Version Negotiation may open its new file
|
|
482
|
+
under the same number. */
|
|
483
|
+
c->qlog_fd = -1;
|
|
484
|
+
xfree(c->qlog_buf);
|
|
485
|
+
c->qlog_buf = NULL;
|
|
288
486
|
}
|
|
289
487
|
|
|
290
488
|
void quic_conn_open_qlog(quic_conn_t *c, VALUE settings_v,
|
|
291
489
|
ngtcp2_settings *settings) {
|
|
490
|
+
/* A reconnect after Version Negotiation drops the last file's path. */
|
|
491
|
+
c->qlog_path = Qnil;
|
|
292
492
|
VALUE dir = quic_path_value(rb_funcall(settings_v, rb_intern("qlog_dir"), 0),
|
|
293
493
|
"qlog_dir");
|
|
294
494
|
if (NIL_P(dir))
|
|
295
495
|
return;
|
|
496
|
+
/* quic_fill_settings has checked it already. Read before the file is
|
|
497
|
+
opened all the same, so that nothing is left open if it raised. */
|
|
498
|
+
uint64_t max_bytes = quic_read_qlog_max_bytes(settings_v);
|
|
296
499
|
|
|
297
500
|
char hex[NGTCP2_MAX_CIDLEN * 2 + 1];
|
|
298
501
|
for (size_t i = 0; i < c->scid.datalen; i++) {
|
|
299
502
|
snprintf(hex + i * 2, 3, "%02x", c->scid.data[i]);
|
|
300
503
|
}
|
|
301
504
|
hex[c->scid.datalen * 2] = '\0';
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
505
|
+
/* File.expand_path: against the current directory now, so that the path
|
|
506
|
+
keeps naming this file after a chdir. Symbolic links are left as they
|
|
507
|
+
are. */
|
|
508
|
+
VALUE path = rb_file_expand_path(
|
|
509
|
+
rb_sprintf("%" PRIsVALUE "/%s.sqlog", dir, hex), Qnil);
|
|
510
|
+
/* Allocated first, since xmalloc may raise and leave an open fd behind. */
|
|
511
|
+
uint8_t *buf = xmalloc(QUIC_QLOG_BUFLEN);
|
|
512
|
+
/* O_NOFOLLOW: qlog_dir may be shared, like /tmp, and a symbolic link
|
|
513
|
+
someone else left there must not be followed. 0666 and the umask give
|
|
514
|
+
the permissions fopen(path, "w") did. The String's bytes are not kept:
|
|
515
|
+
GC.compact may move them. */
|
|
516
|
+
int fd = open(StringValueCStr(path),
|
|
517
|
+
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0666);
|
|
518
|
+
if (fd < 0) {
|
|
519
|
+
xfree(buf);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
c->qlog_path = rb_obj_freeze(path);
|
|
523
|
+
c->qlog_fd = fd;
|
|
524
|
+
c->qlog_buf = buf;
|
|
525
|
+
c->qlog_buflen = 0;
|
|
526
|
+
c->qlog_written = 0;
|
|
527
|
+
c->qlog_committed = 0;
|
|
528
|
+
/* Per file: after Version Negotiation the new file counts from 0. */
|
|
529
|
+
c->qlog_max_bytes = max_bytes;
|
|
530
|
+
settings->qlog_write = quic_qlog_write_cb;
|
|
306
531
|
}
|
|
307
532
|
|
|
308
533
|
/* Validate a path setting (ca_file, ca_path, ...) and return it as a String,
|
|
@@ -667,7 +892,7 @@ static VALUE quic_conn_write_pkt(int argc, VALUE *argv, VALUE self) {
|
|
|
667
892
|
VALUE buffer = Qnil;
|
|
668
893
|
rb_scan_args(argc, argv, "01", &buffer);
|
|
669
894
|
|
|
670
|
-
quic_conn_t *c =
|
|
895
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
671
896
|
|
|
672
897
|
if (NIL_P(buffer)) {
|
|
673
898
|
/* rb_str_buf_new returns an ASCII-8BIT (binary) String per CRuby spec,
|
|
@@ -716,7 +941,7 @@ static VALUE quic_conn_write_pkt(int argc, VALUE *argv, VALUE self) {
|
|
|
716
941
|
peer. */
|
|
717
942
|
VALUE
|
|
718
943
|
quic_conn_error_new(quic_conn_t *c, int rv) {
|
|
719
|
-
if (rv != NGTCP2_ERR_CRYPTO)
|
|
944
|
+
if (rv != NGTCP2_ERR_CRYPTO || c->conn == NULL)
|
|
720
945
|
return quic_ngtcp2_error_new(rv);
|
|
721
946
|
|
|
722
947
|
/* 0 means "not set" to ngtcp2; close_notify (0) is never sent for a
|
|
@@ -761,6 +986,12 @@ static VALUE quic_read_pkt_body(VALUE arg) {
|
|
|
761
986
|
int rv = quic_conn_read_pkt_raw(a->c, &a->path,
|
|
762
987
|
(const uint8_t *)RSTRING_PTR(a->packet),
|
|
763
988
|
(size_t)RSTRING_LEN(a->packet));
|
|
989
|
+
/* Ahead of everything below, which reads c->conn: a failed reconnect
|
|
990
|
+
leaves none. Only a client gets this error. */
|
|
991
|
+
if (rv == NGTCP2_ERR_RECV_VERSION_NEGOTIATION && !a->c->is_server) {
|
|
992
|
+
quic_client_handle_version_negotiation(a->c);
|
|
993
|
+
return Qnil;
|
|
994
|
+
}
|
|
764
995
|
/* The peer closed or reset the connection; ngtcp2 logs nothing more. */
|
|
765
996
|
if (ngtcp2_conn_in_draining_period2(a->c->conn))
|
|
766
997
|
quic_conn_close_qlog(a->c);
|
|
@@ -799,7 +1030,7 @@ static VALUE quic_conn_read_pkt(int argc, VALUE *argv, VALUE self) {
|
|
|
799
1030
|
quic_require_sockaddr(local_sockaddr, "local_sockaddr");
|
|
800
1031
|
quic_require_sockaddr(remote_sockaddr, "remote_sockaddr");
|
|
801
1032
|
|
|
802
|
-
quic_conn_t *c =
|
|
1033
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
803
1034
|
|
|
804
1035
|
struct quic_read_pkt_args args;
|
|
805
1036
|
args.c = c;
|
|
@@ -816,22 +1047,85 @@ static VALUE quic_conn_read_pkt(int argc, VALUE *argv, VALUE self) {
|
|
|
816
1047
|
}
|
|
817
1048
|
|
|
818
1049
|
static VALUE quic_conn_handshake_completed_p(VALUE self) {
|
|
819
|
-
quic_conn_t *c =
|
|
1050
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
820
1051
|
return ngtcp2_conn_get_handshake_completed2(c->conn) ? Qtrue : Qfalse;
|
|
821
1052
|
}
|
|
822
1053
|
|
|
1054
|
+
/* :v1 or :v2 once the version is settled, nil before: a server settles it
|
|
1055
|
+
on the client's transport parameters, a client on the server's first
|
|
1056
|
+
Initial. */
|
|
1057
|
+
static VALUE quic_conn_negotiated_version(VALUE self) {
|
|
1058
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
1059
|
+
uint32_t version = ngtcp2_conn_get_negotiated_version2(c->conn);
|
|
1060
|
+
switch (version) {
|
|
1061
|
+
case 0:
|
|
1062
|
+
return Qnil;
|
|
1063
|
+
case NGTCP2_PROTO_VER_V1:
|
|
1064
|
+
return ID2SYM(rb_intern("v1"));
|
|
1065
|
+
case NGTCP2_PROTO_VER_V2:
|
|
1066
|
+
return ID2SYM(rb_intern("v2"));
|
|
1067
|
+
default:
|
|
1068
|
+
rb_raise(rb_eRuntimeError, "unexpected QUIC version 0x%08x",
|
|
1069
|
+
(unsigned int)version);
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
/* The peer of the current path, as a sockaddr String for Pump#remote_address
|
|
1074
|
+
to make an Addrinfo of. A copy: ngtcp2's path storage changes on the next
|
|
1075
|
+
read_pkt and goes away with the connection. On a server, ngtcp2 switches
|
|
1076
|
+
the current path to a new peer address before validating it, and back if
|
|
1077
|
+
the validation fails. */
|
|
1078
|
+
static VALUE quic_conn_remote_sockaddr(VALUE self) {
|
|
1079
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
1080
|
+
const ngtcp2_path *path = ngtcp2_conn_get_path2(c->conn);
|
|
1081
|
+
return rb_str_new((const char *)path->remote.addr,
|
|
1082
|
+
(long)path->remote.addrlen);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/* The ALPN protocol the handshake settled on, frozen ASCII-8BIT, or nil
|
|
1086
|
+
before it is settled: a server settles it on the first ClientHello, a
|
|
1087
|
+
client on the server's EncryptedExtensions. A client stays at nil when
|
|
1088
|
+
the server sent no ALPN. picotls keeps it NUL-terminated without a length,
|
|
1089
|
+
and sends it by strlen, so strlen is its length here too. */
|
|
1090
|
+
static VALUE quic_conn_alpn_protocol(VALUE self) {
|
|
1091
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
1092
|
+
/* The constructors make the ptls_t after the ngtcp2_conn, so one that
|
|
1093
|
+
failed in between leaves no TLS session to ask. */
|
|
1094
|
+
if (c->cptls.ptls == NULL)
|
|
1095
|
+
return Qnil;
|
|
1096
|
+
const char *proto = ptls_get_negotiated_protocol(c->cptls.ptls);
|
|
1097
|
+
if (proto == NULL)
|
|
1098
|
+
return Qnil;
|
|
1099
|
+
return rb_obj_freeze(rb_str_new(proto, (long)strlen(proto)));
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
/* The cipher suite's IANA name (e.g. "TLS_AES_128_GCM_SHA256"), an interned
|
|
1103
|
+
US-ASCII String, or nil before it is settled: a server settles it on the
|
|
1104
|
+
first ClientHello, a client on the first ServerHello (or
|
|
1105
|
+
HelloRetryRequest). */
|
|
1106
|
+
static VALUE quic_conn_cipher_suite(VALUE self) {
|
|
1107
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
1108
|
+
if (c->cptls.ptls == NULL) /* as in quic_conn_alpn_protocol */
|
|
1109
|
+
return Qnil;
|
|
1110
|
+
ptls_cipher_suite_t *cs = ptls_get_cipher(c->cptls.ptls);
|
|
1111
|
+
if (cs == NULL)
|
|
1112
|
+
return Qnil;
|
|
1113
|
+
return rb_enc_interned_str(cs->name, (long)strlen(cs->name),
|
|
1114
|
+
rb_usascii_encoding());
|
|
1115
|
+
}
|
|
1116
|
+
|
|
823
1117
|
static VALUE quic_conn_in_closing_period_p(VALUE self) {
|
|
824
|
-
quic_conn_t *c =
|
|
1118
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
825
1119
|
return ngtcp2_conn_in_closing_period2(c->conn) ? Qtrue : Qfalse;
|
|
826
1120
|
}
|
|
827
1121
|
|
|
828
1122
|
static VALUE quic_conn_in_draining_period_p(VALUE self) {
|
|
829
|
-
quic_conn_t *c =
|
|
1123
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
830
1124
|
return ngtcp2_conn_in_draining_period2(c->conn) ? Qtrue : Qfalse;
|
|
831
1125
|
}
|
|
832
1126
|
|
|
833
1127
|
static VALUE quic_conn_expiry(VALUE self) {
|
|
834
|
-
quic_conn_t *c =
|
|
1128
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
835
1129
|
ngtcp2_tstamp t = ngtcp2_conn_get_expiry2(c->conn);
|
|
836
1130
|
if (t == UINT64_MAX)
|
|
837
1131
|
return Qnil;
|
|
@@ -842,7 +1136,7 @@ static VALUE quic_conn_expiry(VALUE self) {
|
|
|
842
1136
|
read_pkt or handle_expiry to advance ngtcp2 state, then write_pkt to flush.
|
|
843
1137
|
*/
|
|
844
1138
|
static VALUE quic_conn_handle_expiry(VALUE self) {
|
|
845
|
-
quic_conn_t *c =
|
|
1139
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
846
1140
|
c->now = quic_now();
|
|
847
1141
|
int rv = ngtcp2_conn_handle_expiry(c->conn, c->now);
|
|
848
1142
|
/* Timed out idle: the connection ends without a CONNECTION_CLOSE. */
|
|
@@ -863,7 +1157,7 @@ static void quic_conn_require_stream_mode(quic_conn_t *c) {
|
|
|
863
1157
|
}
|
|
864
1158
|
|
|
865
1159
|
static VALUE quic_conn_open_bidi_stream(VALUE self) {
|
|
866
|
-
quic_conn_t *c =
|
|
1160
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
867
1161
|
quic_conn_require_stream_mode(c);
|
|
868
1162
|
|
|
869
1163
|
int64_t stream_id;
|
|
@@ -874,7 +1168,7 @@ static VALUE quic_conn_open_bidi_stream(VALUE self) {
|
|
|
874
1168
|
}
|
|
875
1169
|
|
|
876
1170
|
static VALUE quic_conn_open_uni_stream(VALUE self) {
|
|
877
|
-
quic_conn_t *c =
|
|
1171
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
878
1172
|
quic_conn_require_stream_mode(c);
|
|
879
1173
|
|
|
880
1174
|
int64_t stream_id;
|
|
@@ -903,7 +1197,7 @@ static VALUE quic_conn_close_m(VALUE self, VALUE error_code_v, VALUE reason_v) {
|
|
|
903
1197
|
reason_len = (size_t)RSTRING_LEN(reason_v);
|
|
904
1198
|
}
|
|
905
1199
|
|
|
906
|
-
quic_conn_t *c =
|
|
1200
|
+
quic_conn_t *c = quic_conn_get_open(self);
|
|
907
1201
|
|
|
908
1202
|
/* Idempotent: a subsequent #close after the conn already entered the
|
|
909
1203
|
closing/draining period does not emit another CONNECTION_CLOSE. */
|
|
@@ -948,8 +1242,9 @@ static VALUE quic_conn_close_m(VALUE self, VALUE error_code_v, VALUE reason_v) {
|
|
|
948
1242
|
ngtcp2_ssize n = ngtcp2_conn_write_connection_close(
|
|
949
1243
|
c->conn, &path_storage.path, &pi, dest, (size_t)QUIC_WRITE_PKT_BUFLEN,
|
|
950
1244
|
&ccerr, quic_now());
|
|
951
|
-
/* The connection ends here whatever ngtcp2 returned
|
|
952
|
-
CONNECTION_CLOSE, if one was built,
|
|
1245
|
+
/* The connection ends here whatever ngtcp2 returned. ngtcp2 has logged
|
|
1246
|
+
the CONNECTION_CLOSE, if one was built, and closing the qlog writes it
|
|
1247
|
+
out. */
|
|
953
1248
|
quic_conn_close_qlog(c);
|
|
954
1249
|
|
|
955
1250
|
/* Pre-handshake or otherwise not in a state where CONNECTION_CLOSE can be
|
|
@@ -979,6 +1274,20 @@ static VALUE quic_conn_error_m(VALUE self) {
|
|
|
979
1274
|
return quic_conn_get(self)->error;
|
|
980
1275
|
}
|
|
981
1276
|
|
|
1277
|
+
/* See quic_conn_t.qlog_path. */
|
|
1278
|
+
static VALUE quic_conn_qlog_path(VALUE self) {
|
|
1279
|
+
return quic_conn_get(self)->qlog_path;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/* Write out the qlog records buffered so far, without fsync. Keeps the GVL:
|
|
1283
|
+
it is at most QUIC_QLOG_BUFLEN bytes, as when ngtcp2's qlog_write fills
|
|
1284
|
+
the buffer, and the GVL is what keeps two threads off the buffer. A
|
|
1285
|
+
failure stops the qlog as it does anywhere else, and raises nothing. */
|
|
1286
|
+
static VALUE quic_conn_flush_qlog_m(VALUE self) {
|
|
1287
|
+
quic_qlog_flush(quic_conn_get(self));
|
|
1288
|
+
return Qnil;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
982
1291
|
void quic_conn_define_shared_methods(VALUE klass) {
|
|
983
1292
|
rb_define_method(klass, "write_pkt", quic_conn_write_pkt, -1);
|
|
984
1293
|
rb_define_method(klass, "read_pkt", quic_conn_read_pkt, -1);
|
|
@@ -986,6 +1295,10 @@ void quic_conn_define_shared_methods(VALUE klass) {
|
|
|
986
1295
|
rb_define_method(klass, "handle_expiry", quic_conn_handle_expiry, 0);
|
|
987
1296
|
rb_define_method(klass, "handshake_completed?",
|
|
988
1297
|
quic_conn_handshake_completed_p, 0);
|
|
1298
|
+
rb_define_method(klass, "negotiated_version", quic_conn_negotiated_version,
|
|
1299
|
+
0);
|
|
1300
|
+
rb_define_method(klass, "alpn_protocol", quic_conn_alpn_protocol, 0);
|
|
1301
|
+
rb_define_method(klass, "cipher_suite", quic_conn_cipher_suite, 0);
|
|
989
1302
|
rb_define_method(klass, "in_closing_period?", quic_conn_in_closing_period_p,
|
|
990
1303
|
0);
|
|
991
1304
|
rb_define_method(klass, "in_draining_period?", quic_conn_in_draining_period_p,
|
|
@@ -995,5 +1308,11 @@ void quic_conn_define_shared_methods(VALUE klass) {
|
|
|
995
1308
|
/* #close itself is Pump's (lib/quic/connection.rb), which sends what
|
|
996
1309
|
#_close returns; defining it here would hide Pump's. */
|
|
997
1310
|
rb_define_private_method(klass, "_close", quic_conn_close_m, 2);
|
|
1311
|
+
/* Likewise #remote_address is Pump's, which makes an Addrinfo of what
|
|
1312
|
+
#_remote_sockaddr returns. */
|
|
1313
|
+
rb_define_private_method(klass, "_remote_sockaddr", quic_conn_remote_sockaddr,
|
|
1314
|
+
0);
|
|
998
1315
|
rb_define_method(klass, "error", quic_conn_error_m, 0);
|
|
1316
|
+
rb_define_method(klass, "qlog_path", quic_conn_qlog_path, 0);
|
|
1317
|
+
rb_define_method(klass, "flush_qlog", quic_conn_flush_qlog_m, 0);
|
|
999
1318
|
}
|