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.
@@ -2,6 +2,7 @@
2
2
  #define QUIC_CONNECTION_COMMON_H 1
3
3
 
4
4
  #include <stdio.h>
5
+ #include <sys/types.h>
5
6
 
6
7
  #include "quic.h"
7
8
  #include "quic/ext.h"
@@ -14,6 +15,14 @@
14
15
  minimum destlen ngtcp2 accepts. Revisit when PMTUD is enabled (Phase 4+). */
15
16
  #define QUIC_WRITE_PKT_BUFLEN NGTCP2_MAX_UDP_PAYLOAD_SIZE
16
17
 
18
+ /* Size of a connection's qlog buffer (quic_conn_t.qlog_buf). Holds four of
19
+ ngtcp2's largest records (NGTCP2_QLOG_BUFLEN, 4096 bytes). */
20
+ #define QUIC_QLOG_BUFLEN (16 * 1024)
21
+
22
+ /* How many of the versions a Version Negotiation packet offers a client
23
+ keeps for QUIC::Error::VersionNegotiation's message. */
24
+ #define QUIC_VN_OFFERED_MAX 8
25
+
17
26
  /* Whether the stream handler h has member, judged by the size it was built
18
27
  with: a handler built against an older quic/ext.h is shorter, and its
19
28
  memory ends before the members added since. Compared with the end of the
@@ -106,11 +115,51 @@ typedef struct {
106
115
  connection's flow control window only what it has sent, so #write and
107
116
  #write_nonblock take this off as well. */
108
117
  uint64_t stream_unsent;
109
- /* <qlog_dir>/<SCID>.sqlog while ngtcp2 writes qlog to it, else NULL.
110
- quic_conn_close_qlog closes it as soon as the connection ends;
111
- NGTCP2_QLOG_WRITE_FLAG_FIN, sent by ngtcp2_conn_del, closes one still
112
- open when the connection is freed. */
113
- FILE *qlog;
118
+ /* The qlog file, <qlog_dir>/<SCID>.sqlog.
119
+ - qlog_fd: the file while ngtcp2 writes qlog to it, else -1.
120
+ - qlog_buf / qlog_buflen: the records not yet written out, in a buffer
121
+ of QUIC_QLOG_BUFLEN bytes that exists only while qlog_fd is open.
122
+ ngtcp2 hands over one whole record per qlog_write call, and the
123
+ buffer is written out only when the next one does not fit, on
124
+ #flush_qlog and on closing. A write-out may take several write(2)s,
125
+ but once it is done (or truncated after a failure) the file ends
126
+ with a whole record.
127
+ - qlog_written: the file's length, all of it written by this struct.
128
+ - qlog_committed: where the last record written out in full ends. A
129
+ failed write truncates the file back to it and stops the qlog.
130
+ - qlog_max_bytes: Settings#qlog_max_bytes, 0 for no limit. A record
131
+ that would take qlog_written + qlog_buflen past it is dropped and
132
+ stops the qlog, so the file never grows past it.
133
+ quic_conn_close_qlog is the only thing that closes the file: wherever
134
+ the connection ends, on NGTCP2_QLOG_WRITE_FLAG_FIN, sent by
135
+ ngtcp2_conn_del, and at qlog_max_bytes. ngtcp2 keeps calling qlog_write
136
+ after that, and those calls do nothing. */
137
+ int qlog_fd;
138
+ uint8_t *qlog_buf;
139
+ size_t qlog_buflen;
140
+ off_t qlog_written;
141
+ off_t qlog_committed;
142
+ uint64_t qlog_max_bytes;
143
+ /* The absolute path of the qlog file (#qlog_path), a frozen String set
144
+ when the file was opened, and kept after it is closed. Qnil when no
145
+ file was opened. A reconnect after Version Negotiation replaces it with
146
+ the new file's, or Qnil. */
147
+ VALUE qlog_path;
148
+ /* Client only, for Version Negotiation (connection_client.c), which
149
+ makes a new ngtcp2_conn and ptls_t in this struct.
150
+ - vn_candidates: the preferred_versions also in available_versions, in
151
+ order of preference; what the client may switch to.
152
+ - vn_offered / vn_offeredlen: the first QUIC_VN_OFFERED_MAX versions
153
+ the Version Negotiation packet offered, and how many it offered.
154
+ - vn_version: the one recv_version_negotiation chose, 0 for none.
155
+ - rebuild_failed: making the new connection failed, so there is no
156
+ ngtcp2_conn any more. */
157
+ uint32_t vn_candidates[2];
158
+ size_t vn_candidateslen;
159
+ uint32_t vn_offered[QUIC_VN_OFFERED_MAX];
160
+ size_t vn_offeredlen;
161
+ uint32_t vn_version;
162
+ bool rebuild_failed;
114
163
  /* The rest is for a connection a QUIC::Endpoint made, and stays zero (nil)
115
164
  otherwise. endpoint is the Endpoint whose table holds this connection,
116
165
  NULL once it has left the table; endpoint_owner is that Endpoint's
@@ -140,7 +189,7 @@ extern ptls_key_exchange_algorithm_t *quic_key_exchanges[];
140
189
  extern ptls_cipher_suite_t *quic_cipher_suites[];
141
190
 
142
191
  /* Allocator for both connection classes. The struct starts zeroed, except
143
- its VALUE fields, which start as Qnil. */
192
+ its VALUE fields, which start as Qnil, and qlog_fd, which starts as -1. */
144
193
  VALUE quic_conn_alloc(VALUE klass);
145
194
 
146
195
  /* CLOCK_MONOTONIC in ngtcp2's units (nanoseconds). */
@@ -181,14 +230,29 @@ VALUE quic_server_conn_new(VALUE klass, const ngtcp2_pkt_hd *hd,
181
230
  VALUE transport_params, VALUE settings_v,
182
231
  quic_endpoint_t *ep, VALUE ep_owner);
183
232
 
233
+ /* The struct of a Client or Server, whether or not it has an ngtcp2_conn.
234
+ The constructors call it before they make one; GC paths, ngtcp2 callbacks
235
+ and an Endpoint's own connections use it too. */
184
236
  quic_conn_t *quic_conn_get(VALUE self);
185
237
 
238
+ /* Like quic_conn_get, for a Ruby method that touches c->conn: raises
239
+ QUIC::Error::InvalidState when there is no ngtcp2_conn (an object made
240
+ with .allocate, or a client whose reconnect after Version Negotiation
241
+ failed), instead of letting ngtcp2 dereference NULL. */
242
+ quic_conn_t *quic_conn_get_open(VALUE self);
243
+
186
244
  /* The raw ngtcp2_conn of a Client or Server, for stream.c to issue ngtcp2
187
245
  calls (e.g. ngtcp2_conn_shutdown_stream_read) on streams whose @client is
188
246
  that connection. The caller must keep the connection object alive for the
189
- duration of the call. */
247
+ duration of the call. Raises like quic_conn_get_open. */
190
248
  ngtcp2_conn *quic_conn_ptr(VALUE owner);
191
249
 
250
+ /* #read_pkt of client c got NGTCP2_ERR_RECV_VERSION_NEGOTIATION: connect
251
+ again in the version recv_version_negotiation chose, or raise
252
+ QUIC::Error::VersionNegotiation when it found none. A failed reconnect
253
+ leaves c without an ngtcp2_conn and raises. */
254
+ void quic_client_handle_version_negotiation(quic_conn_t *c);
255
+
192
256
  /* ngtcp2_crypto_conn_ref.get_conn for quic_conn_t. */
193
257
  ngtcp2_conn *quic_conn_get_conn(ngtcp2_crypto_conn_ref *conn_ref);
194
258
 
@@ -222,20 +286,56 @@ void quic_conn_copy_alpn(quic_conn_t *c, VALUE settings_v);
222
286
  initial_max_streams_bidi streams over the whole connection. */
223
287
  void quic_conn_give_back_peer_stream(ngtcp2_conn *conn, int64_t stream_id);
224
288
 
289
+ /* QUIC::Settings#version, #preferred_versions and #available_versions as
290
+ ngtcp2 takes them. ngtcp2_conn_*_new copies the arrays, so a struct on the
291
+ caller's stack only has to outlive that call. Only v1 and v2 exist, so a
292
+ list without duplicates holds at most two. */
293
+ typedef struct {
294
+ uint32_t chosen;
295
+ uint32_t preferred[2];
296
+ size_t preferredlen;
297
+ uint32_t available[2];
298
+ size_t availablelen;
299
+ } quic_versions_buf;
300
+
301
+ /* Read and check the three version settings of settings_v into *vbuf.
302
+ Raises ArgumentError unless version is :v1 or :v2 and both lists are
303
+ non-empty Arrays of :v1 and :v2 without duplicates. */
304
+ void quic_read_versions(VALUE settings_v, quic_versions_buf *vbuf);
305
+
306
+ /* Whether a server with these settings takes a client that starts with
307
+ version: one in preferred_versions or available_versions. */
308
+ bool quic_versions_accept(const quic_versions_buf *vbuf, uint32_t version);
309
+
225
310
  void quic_fill_transport_params(ngtcp2_transport_params *params, VALUE tp);
226
- void quic_fill_settings(ngtcp2_settings *settings, VALUE st);
311
+
312
+ /* Read settings_v.qlog_max_bytes: 0 for nil (no limit), else the limit.
313
+ Raises ArgumentError unless it is nil or an Integer from 1 to
314
+ INT64_MAX. */
315
+ uint64_t quic_read_qlog_max_bytes(VALUE settings_v);
316
+
317
+ /* Fill *settings from st, a QUIC::Settings. The version lists are read into
318
+ *vbuf, which *settings points into, so vbuf has to live as long as
319
+ settings is used. */
320
+ void quic_fill_settings(ngtcp2_settings *settings, VALUE st,
321
+ quic_versions_buf *vbuf);
227
322
 
228
323
  /* When settings_v.qlog_dir is set, open <qlog_dir>/<c->scid in hex>.sqlog
229
- (as ngtcp2's examples name it) and have ngtcp2 write the connection's
230
- qlog there. c->scid must be set. A file that cannot be opened only goes
231
- without qlog: QUIC::Settings#check_qlog_dir already caught a wrong
232
- directory when the connection or Endpoint was set up. */
324
+ (as ngtcp2's examples name it), allocate its buffer and have ngtcp2 write
325
+ the connection's qlog there. A relative qlog_dir is expanded against the
326
+ current directory, and c->qlog_path is set to the file's absolute path.
327
+ c->scid must be set, and c must have no qlog file open. A file that
328
+ cannot be opened (a symbolic link included) only goes without qlog, and
329
+ leaves c->qlog_path nil: QUIC::Settings#check_qlog_dir already caught a
330
+ wrong directory when the connection or Endpoint was set up. */
233
331
  void quic_conn_open_qlog(quic_conn_t *c, VALUE settings_v,
234
332
  ngtcp2_settings *settings);
235
333
 
236
- /* Close c's qlog file, if open. Called wherever the connection ends, so
237
- that the file does not wait for GC; what ngtcp2 logs afterwards goes
238
- nowhere. */
334
+ /* Write out c's buffered qlog records and close the file, if open. A failed
335
+ write truncates the file back to the last whole record. Called wherever the
336
+ connection ends, so that the file does not wait for GC, and where the qlog
337
+ reaches Settings#qlog_max_bytes; what ngtcp2 logs afterwards goes nowhere.
338
+ Calls no Ruby code, since quic_conn_free calls it too. */
239
339
  void quic_conn_close_qlog(quic_conn_t *c);
240
340
 
241
341
  /* Zero *callbacks and fill in everything both endpoints share. The caller
@@ -4,6 +4,7 @@
4
4
  #include <openssl/rand.h>
5
5
 
6
6
  NORETURN(static void quic_server_raise_unacceptable(void));
7
+ NORETURN(static void quic_server_raise_unsupported(uint32_t version));
7
8
 
8
9
  /* The first datagram cannot start a connection. code stays nil: this is
9
10
  _accept's own verdict, not an ngtcp2 error passed through. */
@@ -11,6 +12,16 @@ static void quic_server_raise_unacceptable(void) {
11
12
  rb_raise(rb_eQUICErrorProto, "not an acceptable Initial packet");
12
13
  }
13
14
 
15
+ /* The client started with a version ngtcp2 does not know, or one the
16
+ Settings do not take; to the caller both are a version this server does
17
+ not take. */
18
+ static void quic_server_raise_unsupported(uint32_t version) {
19
+ rb_raise(rb_eQUICErrorProto,
20
+ "unsupported QUIC version 0x%08x (Version Negotiation is not "
21
+ "implemented)",
22
+ (unsigned int)version);
23
+ }
24
+
14
25
  VALUE
15
26
  quic_server_conn_new(VALUE klass, const ngtcp2_pkt_hd *hd,
16
27
  const ngtcp2_path *path, VALUE tls_context,
@@ -65,7 +76,8 @@ quic_server_conn_new(VALUE klass, const ngtcp2_pkt_hd *hd,
65
76
  callbacks.recv_client_initial = ngtcp2_crypto_recv_client_initial_cb;
66
77
 
67
78
  ngtcp2_settings settings;
68
- quic_fill_settings(&settings, settings_v);
79
+ quic_versions_buf vbuf;
80
+ quic_fill_settings(&settings, settings_v, &vbuf);
69
81
  /* Every raise below closes it first. */
70
82
  quic_conn_open_qlog(c, settings_v, &settings);
71
83
 
@@ -160,12 +172,8 @@ static VALUE quic_server_accept(int argc, VALUE *argv, VALUE klass) {
160
172
 
161
173
  ngtcp2_version_cid vc;
162
174
  int rv = ngtcp2_pkt_decode_version_cid(&vc, data, datalen, QUIC_SCID_LEN);
163
- if (rv == NGTCP2_ERR_VERSION_NEGOTIATION) {
164
- rb_raise(rb_eQUICErrorProto,
165
- "unsupported QUIC version 0x%08x (Version Negotiation is not "
166
- "implemented)",
167
- (unsigned int)vc.version);
168
- }
175
+ if (rv == NGTCP2_ERR_VERSION_NEGOTIATION)
176
+ quic_server_raise_unsupported(vc.version);
169
177
  if (rv != 0)
170
178
  quic_server_raise_unacceptable();
171
179
 
@@ -176,6 +184,11 @@ static VALUE quic_server_accept(int argc, VALUE *argv, VALUE klass) {
176
184
  if (ngtcp2_accept(&hd, data, datalen) != 0)
177
185
  quic_server_raise_unacceptable();
178
186
 
187
+ quic_versions_buf vbuf;
188
+ quic_read_versions(settings_v, &vbuf);
189
+ if (!quic_versions_accept(&vbuf, hd.version))
190
+ quic_server_raise_unsupported(hd.version);
191
+
179
192
  /* A context of this connection's own, as one made by
180
193
  QUIC::TLSContext.server. verify_mode, ca_file and ca_path are not read,
181
194
  since no client certificate is requested. */
data/ext/quic/endpoint.c CHANGED
@@ -46,6 +46,9 @@ struct quic_endpoint {
46
46
  VALUE tls_context;
47
47
  VALUE transport_params;
48
48
  VALUE settings;
49
+ /* settings' versions, read once by Endpoint.new: which versions a new
50
+ connection may start with, and what Version Negotiation offers. */
51
+ quic_versions_buf versions;
49
52
  long max_connections;
50
53
  bool accepting;
51
54
  uint8_t reset_secret[QUIC_STATELESS_RESET_SECRETLEN];
@@ -245,17 +248,20 @@ static void quic_endpoint_send_version_negotiation(quic_endpoint_t *ep,
245
248
  RAND_bytes((uint8_t *)&grease, sizeof(grease)) != 1) {
246
249
  return;
247
250
  }
248
- /* A reserved version (0x?a?a?a?a) ahead of the one supported, so that
251
+ /* A reserved version (0x?a?a?a?a) ahead of preferred_versions, so that
249
252
  clients keep handling versions they do not know (RFC 9000 section
250
- 6.3). */
251
- uint32_t versions[] = {(grease & 0xf0f0f0f0U) | 0x0a0a0a0aU,
252
- NGTCP2_PROTO_VER_V1};
253
+ 6.3), as examples/server.cc does. */
254
+ uint32_t versions[1 + sizeof(ep->versions.preferred) /
255
+ sizeof(ep->versions.preferred[0])];
256
+ versions[0] = (grease & 0xf0f0f0f0U) | 0x0a0a0a0aU;
257
+ memcpy(versions + 1, ep->versions.preferred,
258
+ ep->versions.preferredlen * sizeof(versions[0]));
253
259
 
254
260
  VALUE pkt = rb_str_buf_new(NGTCP2_MAX_UDP_PAYLOAD_SIZE);
255
261
  ngtcp2_ssize n = ngtcp2_pkt_write_version_negotiation(
256
262
  (uint8_t *)RSTRING_PTR(pkt), NGTCP2_MAX_UDP_PAYLOAD_SIZE, unused_bits,
257
263
  vc->scid, vc->scidlen, vc->dcid, vc->dcidlen, versions,
258
- sizeof(versions) / sizeof(versions[0]));
264
+ 1 + ep->versions.preferredlen);
259
265
  if (n <= 0)
260
266
  return;
261
267
  rb_str_set_len(pkt, n);
@@ -491,7 +497,8 @@ static VALUE quic_endpoint_s_new(int argc, VALUE *argv, VALUE klass) {
491
497
  ngtcp2_transport_params params;
492
498
  quic_fill_transport_params(&params, values[2]);
493
499
  ngtcp2_settings settings;
494
- quic_fill_settings(&settings, values[3]);
500
+ quic_versions_buf versions;
501
+ quic_fill_settings(&settings, values[3], &versions);
495
502
  long max_connections = NUM2LONG(values[4]);
496
503
  if (max_connections <= 0) {
497
504
  rb_raise(rb_eArgError, "max_connections must be positive (got %ld)",
@@ -508,6 +515,7 @@ static VALUE quic_endpoint_s_new(int argc, VALUE *argv, VALUE klass) {
508
515
  ep->tls_context = values[1];
509
516
  ep->transport_params = values[2];
510
517
  ep->settings = values[3];
518
+ ep->versions = versions;
511
519
  ep->max_connections = max_connections;
512
520
  ep->accepting = true;
513
521
  ep->reset_bucket = QUIC_STATELESS_RESET_BURST;
@@ -571,6 +579,20 @@ static VALUE quic_endpoint_accept(struct quic_endpoint_read_args *a,
571
579
  return Qnil;
572
580
  }
573
581
 
582
+ /* Ahead of the refusal below, which would answer in a version the
583
+ Settings do not take. */
584
+ if (!quic_versions_accept(&ep->versions, hd.version)) {
585
+ ngtcp2_version_cid vc = {
586
+ .version = hd.version,
587
+ .dcid = hd.dcid.data,
588
+ .dcidlen = hd.dcid.datalen,
589
+ .scid = hd.scid.data,
590
+ .scidlen = hd.scid.datalen,
591
+ };
592
+ quic_endpoint_send_version_negotiation(ep, &vc, a->remote);
593
+ return Qnil;
594
+ }
595
+
574
596
  if (!ep->accepting || RARRAY_LEN(ep->conns) >= ep->max_connections) {
575
597
  quic_endpoint_refuse(ep, &hd, a->remote);
576
598
  return Qnil;
data/ext/quic/ext_api.c CHANGED
@@ -26,6 +26,8 @@ static int quic_ext_set_stream_handler(quic_ext_conn *ec,
26
26
  quic_conn_t *c = quic_ext_conn_struct(ec);
27
27
  if (h == NULL || !QUIC_EXT_HANDLER_HAS(h, handshake_completed))
28
28
  return QUIC_EXT_ERR_OTHER;
29
+ if (c->conn == NULL)
30
+ return QUIC_EXT_ERR_INVALID_STATE;
29
31
  if (c->handler != NULL)
30
32
  return QUIC_EXT_ERR_INVALID_STATE;
31
33
  VALUE streams = rb_ivar_get(c->owner, rb_intern("@streams"));
@@ -41,6 +43,8 @@ static int quic_ext_set_stream_handler(quic_ext_conn *ec,
41
43
 
42
44
  static int quic_ext_open_uni_stream(quic_ext_conn *ec, int64_t *id) {
43
45
  quic_conn_t *c = quic_ext_conn_struct(ec);
46
+ if (c->conn == NULL)
47
+ return QUIC_EXT_ERR_INVALID_STATE;
44
48
  int rv = ngtcp2_conn_open_uni_stream(c->conn, id, NULL);
45
49
  if (rv == NGTCP2_ERR_STREAM_ID_BLOCKED)
46
50
  return QUIC_EXT_ERR_STREAM_ID_BLOCKED;
@@ -52,6 +56,8 @@ static int quic_ext_open_uni_stream(quic_ext_conn *ec, int64_t *id) {
52
56
  to the application). */
53
57
  static int quic_ext_consume(quic_ext_conn *ec, int64_t id, uint64_t n) {
54
58
  quic_conn_t *c = quic_ext_conn_struct(ec);
59
+ if (c->conn == NULL)
60
+ return QUIC_EXT_ERR_INVALID_STATE;
55
61
  if (ngtcp2_conn_extend_max_stream_offset(c->conn, id, n) != 0)
56
62
  return QUIC_EXT_ERR_OTHER;
57
63
  ngtcp2_conn_extend_max_offset(c->conn, n);
@@ -62,6 +68,8 @@ static int quic_ext_consume(quic_ext_conn *ec, int64_t id, uint64_t n) {
62
68
  static int quic_ext_shutdown_stream_read(quic_ext_conn *ec, int64_t id,
63
69
  uint64_t app_error_code) {
64
70
  quic_conn_t *c = quic_ext_conn_struct(ec);
71
+ if (c->conn == NULL)
72
+ return QUIC_EXT_ERR_INVALID_STATE;
65
73
  return ngtcp2_conn_shutdown_stream_read(c->conn, 0, id, app_error_code) == 0
66
74
  ? 0
67
75
  : QUIC_EXT_ERR_OTHER;
@@ -70,6 +78,8 @@ static int quic_ext_shutdown_stream_read(quic_ext_conn *ec, int64_t id,
70
78
  static int quic_ext_shutdown_stream_write(quic_ext_conn *ec, int64_t id,
71
79
  uint64_t app_error_code) {
72
80
  quic_conn_t *c = quic_ext_conn_struct(ec);
81
+ if (c->conn == NULL)
82
+ return QUIC_EXT_ERR_INVALID_STATE;
73
83
  return ngtcp2_conn_shutdown_stream_write(c->conn, 0, id, app_error_code) == 0
74
84
  ? 0
75
85
  : QUIC_EXT_ERR_OTHER;
@@ -79,6 +89,8 @@ static int quic_ext_shutdown_stream_write(quic_ext_conn *ec, int64_t id,
79
89
  later raises arrive through the extend_max_remote_streams_bidi handler. */
80
90
  static uint64_t quic_ext_local_max_streams_bidi(quic_ext_conn *ec) {
81
91
  quic_conn_t *c = quic_ext_conn_struct(ec);
92
+ if (c->conn == NULL)
93
+ return 0;
82
94
  return ngtcp2_conn_get_local_transport_params2(c->conn)
83
95
  ->initial_max_streams_bidi;
84
96
  }
@@ -27,6 +27,11 @@
27
27
  * meant for initialization called from a Ruby method. A handler function
28
28
  * returning a negative value makes the ngtcp2 callback fail with
29
29
  * NGTCP2_ERR_CALLBACK_FAILURE.
30
+ * - Unopened connections: on a connection with no ngtcp2 connection behind
31
+ * it (a Client or Server made with .allocate), set_stream_handler,
32
+ * open_uni_stream, consume, shutdown_stream_read and
33
+ * shutdown_stream_write return QUIC_EXT_ERR_INVALID_STATE, and
34
+ * local_max_streams_bidi returns 0.
30
35
  * - Lifetime: `owner` passed to set_stream_handler is marked by the
31
36
  * connection while it lives. `ud` must not move (allocate it with
32
37
  * xmalloc, not as a Ruby object), and `h` must outlive the connection
data/ext/quic/quic.c CHANGED
@@ -23,6 +23,7 @@ VALUE rb_eQUICErrorStreamClosed;
23
23
  VALUE rb_eQUICErrorStreamReset;
24
24
  VALUE rb_eQUICErrorNotBound;
25
25
  VALUE rb_eQUICErrorInvalidState;
26
+ VALUE rb_eQUICErrorVersionNegotiation;
26
27
 
27
28
  /* The one symbol besides Init_quic that quic.so exports: other extensions
28
29
  look it up with rb_ext_resolve_symbol("quic/quic", "quic_ext_get_api")
@@ -78,6 +79,9 @@ quic_ngtcp2_error_new(int rv) {
78
79
  case NGTCP2_ERR_STREAM_SHUT_WR:
79
80
  cls = rb_eQUICErrorStreamClosed;
80
81
  break;
82
+ case NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE:
83
+ cls = rb_eQUICErrorVersionNegotiation;
84
+ break;
81
85
  default:
82
86
  cls = rb_eQUICErrorUnknown;
83
87
  break;
@@ -150,6 +154,10 @@ RUBY_FUNC_EXPORTED void Init_quic(void) {
150
154
  stream handler (quic/ext.h) owns. */
151
155
  rb_eQUICErrorInvalidState =
152
156
  rb_define_class_under(rb_eQUICError, "InvalidState", rb_eQUICError);
157
+ /* No QUIC version both ends can use, or a sign that someone forced a
158
+ worse one on them. */
159
+ rb_eQUICErrorVersionNegotiation =
160
+ rb_define_class_under(rb_eQUICError, "VersionNegotiation", rb_eQUICError);
153
161
 
154
162
  Init_quic_tls_context(rb_mQUIC);
155
163
  Init_quic_connection_client(rb_mQUICConnection);
data/ext/quic/quic.h CHANGED
@@ -41,6 +41,7 @@ extern VALUE rb_eQUICErrorStreamClosed;
41
41
  extern VALUE rb_eQUICErrorStreamReset;
42
42
  extern VALUE rb_eQUICErrorNotBound;
43
43
  extern VALUE rb_eQUICErrorInvalidState;
44
+ extern VALUE rb_eQUICErrorVersionNegotiation;
44
45
 
45
46
  void Init_quic_connection_client(VALUE rb_mQUICConnection);
46
47
  void Init_quic_connection_server(VALUE rb_mQUICConnection);
data/ext/quic/stream.c CHANGED
@@ -157,7 +157,7 @@ static bool quic_stream_fin_kwarg(int argc, VALUE *argv, VALUE *data) {
157
157
  queue far more than the peer allows. Callers check the stream is
158
158
  writable first. */
159
159
  static uint64_t quic_stream_window_left(VALUE client_v, quic_stream_t *s) {
160
- quic_conn_t *c = quic_conn_get(client_v);
160
+ quic_conn_t *c = quic_conn_get_open(client_v);
161
161
  uint64_t stream_left =
162
162
  ngtcp2_conn_get_max_stream_data_left2(c->conn, s->stream_id);
163
163
  uint64_t conn_left = ngtcp2_conn_get_max_data_left2(c->conn);
@@ -720,7 +720,7 @@ static VALUE quic_stream_reset_m(int argc, VALUE *argv, VALUE self) {
720
720
  return Qnil;
721
721
  }
722
722
 
723
- quic_conn_t *c = quic_conn_get(client_v);
723
+ quic_conn_t *c = quic_conn_get_open(client_v);
724
724
  int rv =
725
725
  ngtcp2_conn_shutdown_stream_write(c->conn, 0, s->stream_id, error_code);
726
726
  if (rv != 0)
@@ -12,7 +12,8 @@ module QUIC
12
12
  # Attach an IO.select / #send / #recvfrom-compatible UDP socket so that
13
13
  # #run and Stream blocking I/O can drive the wire transparently. Caller
14
14
  # is responsible for socket lifetime; we do not close it. The socket
15
- # must already be #connect'd to the peer (we read #remote_address).
15
+ # must already be #connect'd to the peer (we read the socket's
16
+ # #remote_address).
16
17
  def bind(sock)
17
18
  @sock = sock
18
19
  self
@@ -119,6 +120,28 @@ module QUIC
119
120
  nil
120
121
  end
121
122
 
123
+ # The peer of the connection's current path, as a UDP Addrinfo. It
124
+ # follows the peer to a new address when an Endpoint's connection
125
+ # migrates; a Client or Server.accept connection keeps one path.
126
+ # Raises QUIC::Error::InvalidState when there is no ngtcp2 connection
127
+ # behind the object.
128
+ #
129
+ # Telling whether the address is the same as before takes care:
130
+ # - Each call makes a new Addrinfo, and Addrinfo defines neither ==
131
+ # nor eql?/hash (it inherits the identity checks). So two
132
+ # calls never compare equal even for the same address, and the
133
+ # Addrinfos do not dedupe as Hash keys or with #uniq. Compare
134
+ # #to_sockaddr (or use it as the key) instead.
135
+ # - #to_sockaddr holds the bytes the connection was given (the address
136
+ # Client.new resolved, or the remote sockaddr passed to read_pkt),
137
+ # not normalized. An IPv4 peer read through a dual-stack IPv6
138
+ # socket is an IPv4-mapped AF_INET6 address (::ffff:a.b.c.d), whose
139
+ # bytes differ from the AF_INET sockaddr of the same peer. Compare
140
+ # sockaddrs that come from the same socket.
141
+ def remote_address
142
+ Addrinfo.new(_remote_sockaddr, nil, :DGRAM, Socket::IPPROTO_UDP)
143
+ end
144
+
122
145
  private
123
146
 
124
147
  # A C stream handler (quic/ext.h) registered on the connection owns
@@ -160,14 +183,13 @@ module QUIC
160
183
  class Client
161
184
  include Pump
162
185
 
163
- attr_reader :remote_address
164
-
165
186
  # Build a Client and pin a single peer address. address_family takes a
166
187
  # Symbol (:inet for IPv4, :inet6 for IPv6) or nil to defer family
167
- # selection to Addrinfo.udp's implicit resolution. Pin the resolved
168
- # Addrinfo into @remote_address so callers can match their UDPSocket's
169
- # connected sockaddr (otherwise DNS round-robin between Client.new and
170
- # sock.connect can produce a path that ngtcp2 silently drops).
188
+ # selection to Addrinfo.udp's implicit resolution. The resolved address
189
+ # becomes the connection's path, which #remote_address returns, so
190
+ # callers can connect their UDPSocket to it (otherwise DNS round-robin
191
+ # between Client.new and sock.connect can produce a path that ngtcp2
192
+ # silently drops).
171
193
  def self.new(host:, port:, address_family: nil, transport_params: nil, settings: nil)
172
194
  family = case address_family
173
195
  when nil then nil
@@ -197,7 +219,6 @@ module QUIC
197
219
  )
198
220
  client.instance_variable_set(:@host, host)
199
221
  client.instance_variable_set(:@port, port)
200
- client.instance_variable_set(:@remote_address, remote_address)
201
222
  client
202
223
  end
203
224
  end
@@ -209,8 +230,6 @@ module QUIC
209
230
  class Server
210
231
  include Pump
211
232
 
212
- attr_reader :remote_address
213
-
214
233
  private_class_method :new
215
234
 
216
235
  # Wait for the first datagram on sock and build a connection for its
@@ -250,7 +269,6 @@ module QUIC
250
269
  )
251
270
  server.instance_variable_set(:@local_sockaddr, local_sockaddr)
252
271
  server.instance_variable_set(:@remote_sockaddr, remote_sockaddr)
253
- server.instance_variable_set(:@remote_address, sock.remote_address)
254
272
  server.bind(sock)
255
273
  server.read_pkt(data, local_sockaddr: local_sockaddr, remote_sockaddr: remote_sockaddr)
256
274
  server
data/lib/quic/settings.rb CHANGED
@@ -12,10 +12,18 @@ module QUIC
12
12
  :verify_mode,
13
13
  :ca_file,
14
14
  :ca_path,
15
- :qlog_dir
15
+ :qlog_dir,
16
+ :version,
17
+ :preferred_versions,
18
+ :available_versions,
19
+ :qlog_max_bytes
16
20
  )
17
21
 
18
22
  class Settings
23
+ DEFAULT_VERSIONS = [:v2, :v1].freeze
24
+ DEFAULT_QLOG_MAX_BYTES = 64 * 1024 * 1024
25
+ private_constant :DEFAULT_VERSIONS, :DEFAULT_QLOG_MAX_BYTES
26
+
19
27
  # verify_mode (:peer or :none), ca_file and ca_path control server
20
28
  # certificate verification. They default here so that callers building a
21
29
  # Settings without them keep working.
@@ -33,8 +41,42 @@ module QUIC
33
41
  # qlog_dir (a String or Pathname) makes every connection write its qlog
34
42
  # to <qlog_dir>/<its first source connection ID in hex>.sqlog. A file
35
43
  # that cannot be opened when a connection is made only costs that
36
- # connection's qlog.
37
- def initialize(verify_mode: :peer, ca_file: nil, ca_path: nil, qlog_dir: nil, **rest)
44
+ # connection's qlog. Records are buffered, and written out whole when
45
+ # the buffer (16 KiB) fills up, on Connection#flush_qlog and when the
46
+ # connection ends or its qlog stops.
47
+ #
48
+ # qlog_max_bytes caps each qlog file: an Integer from 1 to 2**63 - 1
49
+ # (64 MiB unless given), or nil for no limit. The record that would
50
+ # take a file past it is dropped with every one after it, and the file,
51
+ # which ends with the last whole record, is closed; the connection goes
52
+ # on without qlog. A client that connects again after Version
53
+ # Negotiation counts its new file from 0. Client.new,
54
+ # Connection::Server.accept and Endpoint.new raise ArgumentError for
55
+ # anything else, with or without qlog_dir.
56
+ #
57
+ # version, preferred_versions and available_versions choose the QUIC
58
+ # versions (:v1 for RFC 9000, :v2 for RFC 9369):
59
+ #
60
+ # - version is the one a client starts with. A server does not read it.
61
+ # - preferred_versions lists the versions in order of preference. A server
62
+ # upgrades a client to the first of them the client can use (compatible
63
+ # version negotiation) and lists them in its Version Negotiation
64
+ # packets.
65
+ # - available_versions is what goes into the version_information
66
+ # transport parameter.
67
+ #
68
+ # A server accepts a client that starts with any version in either list.
69
+ # With the defaults on both ends, a client starts with v1 and the server
70
+ # moves the connection to v2; a peer that knows only v1 stays on v1.
71
+ #
72
+ # Both lists are copied and frozen here, so changing the Array that was
73
+ # passed in later changes no Settings.
74
+ def initialize(verify_mode: :peer, ca_file: nil, ca_path: nil, qlog_dir: nil,
75
+ version: :v1, preferred_versions: DEFAULT_VERSIONS, available_versions: DEFAULT_VERSIONS,
76
+ qlog_max_bytes: DEFAULT_QLOG_MAX_BYTES, **rest)
77
+ # Anything but an Array is left for the C extension to reject.
78
+ preferred_versions = preferred_versions.dup.freeze if preferred_versions.is_a?(Array)
79
+ available_versions = available_versions.dup.freeze if available_versions.is_a?(Array)
38
80
  super
39
81
  end
40
82
 
@@ -50,7 +92,11 @@ module QUIC
50
92
  verify_mode: :peer,
51
93
  ca_file: nil,
52
94
  ca_path: nil,
53
- qlog_dir: nil
95
+ qlog_dir: nil,
96
+ version: :v1,
97
+ preferred_versions: DEFAULT_VERSIONS,
98
+ available_versions: DEFAULT_VERSIONS,
99
+ qlog_max_bytes: DEFAULT_QLOG_MAX_BYTES
54
100
  )
55
101
  end
56
102
 
data/lib/quic/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QUIC
4
- VERSION = "0.0.7"
4
+ VERSION = "0.0.8"
5
5
  end