quic 0.0.6 → 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
@@ -65,11 +74,18 @@ typedef struct {
65
74
  ptls_get_data_ptr. The ref must outlive the ptls_t. */
66
75
  ngtcp2_crypto_conn_ref conn_ref;
67
76
  /* Back-reference to the Client or Server Ruby object that owns this
68
- struct. ngtcp2 stream callbacks receive a void* user_data equal to this
69
- struct, and they look up @streams via owner. We are stored INSIDE owner
70
- via TypedData_Wrap_Struct, so owner is guaranteed alive while we exist
71
- (no dmark needed). GC.compact may relocate owner, so quic_conn_compact
72
- updates this field via rb_gc_location. */
77
+ struct. ngtcp2 callbacks receive a void* user_data equal to this
78
+ struct, and reach @streams and @accept_queue via owner. We are stored
79
+ INSIDE owner via TypedData_Wrap_Struct, so owner is guaranteed alive
80
+ while we exist (no dmark needed). GC.compact may relocate owner, so
81
+ quic_conn_compact updates this field via rb_gc_location.
82
+
83
+ A stream's callbacks get its QUIC::Stream's quic_stream_t from
84
+ ngtcp2's stream_user_data, which is only safe while @streams keeps
85
+ that Stream alive. So a Stream goes into @streams before ngtcp2 is
86
+ given its struct, and leaves only at the end of the stream_close
87
+ callback, which must not fail after that: a failing stream_close
88
+ leaves the stream in ngtcp2, pointing at a struct GC may free. */
73
89
  VALUE owner;
74
90
  /* An exception raised by Ruby code inside an ngtcp2 callback, caught with
75
91
  rb_protect so that it does not unwind through ngtcp2's frames. #read_pkt,
@@ -94,11 +110,56 @@ typedef struct {
94
110
  /* Set by the constructor. Decides which peer-initiated streams
95
111
  recv_stream_data queues for #accept_stream. */
96
112
  bool is_server;
97
- /* <qlog_dir>/<SCID>.sqlog while ngtcp2 writes qlog to it, else NULL.
98
- quic_conn_close_qlog closes it as soon as the connection ends;
99
- NGTCP2_QLOG_WRITE_FLAG_FIN, sent by ngtcp2_conn_del, closes one still
100
- open when the connection is freed. */
101
- FILE *qlog;
113
+ /* The bytes the QUIC::Streams of this connection have queued and not yet
114
+ handed to ngtcp2, stopped streams excepted. ngtcp2 takes off the
115
+ connection's flow control window only what it has sent, so #write and
116
+ #write_nonblock take this off as well. */
117
+ uint64_t stream_unsent;
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;
102
163
  /* The rest is for a connection a QUIC::Endpoint made, and stays zero (nil)
103
164
  otherwise. endpoint is the Endpoint whose table holds this connection,
104
165
  NULL once it has left the table; endpoint_owner is that Endpoint's
@@ -128,12 +189,18 @@ extern ptls_key_exchange_algorithm_t *quic_key_exchanges[];
128
189
  extern ptls_cipher_suite_t *quic_cipher_suites[];
129
190
 
130
191
  /* Allocator for both connection classes. The struct starts zeroed, except
131
- its VALUE fields, which start as Qnil. */
192
+ its VALUE fields, which start as Qnil, and qlog_fd, which starts as -1. */
132
193
  VALUE quic_conn_alloc(VALUE klass);
133
194
 
134
195
  /* CLOCK_MONOTONIC in ngtcp2's units (nanoseconds). */
135
196
  ngtcp2_tstamp quic_now(void);
136
197
 
198
+ /* Run body(arg), Ruby code an ngtcp2 callback of c calls, under rb_protect.
199
+ An exception is kept in c->pending_exc (the first one wins; ngtcp2 stops
200
+ at the first failing callback anyway) and turned into
201
+ NGTCP2_ERR_CALLBACK_FAILURE, so that it never unwinds through ngtcp2. */
202
+ int quic_conn_protect(quic_conn_t *c, VALUE (*body)(VALUE), void *arg);
203
+
137
204
  /* Build the next packet of the connection self (wrapping c) into dest of
138
205
  QUIC_WRITE_PKT_BUFLEN bytes, as #write_pkt does, and store its path in
139
206
  *path. Returns the packet's length, 0 when there is nothing to send, or a
@@ -163,14 +230,29 @@ VALUE quic_server_conn_new(VALUE klass, const ngtcp2_pkt_hd *hd,
163
230
  VALUE transport_params, VALUE settings_v,
164
231
  quic_endpoint_t *ep, VALUE ep_owner);
165
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. */
166
236
  quic_conn_t *quic_conn_get(VALUE self);
167
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
+
168
244
  /* The raw ngtcp2_conn of a Client or Server, for stream.c to issue ngtcp2
169
245
  calls (e.g. ngtcp2_conn_shutdown_stream_read) on streams whose @client is
170
246
  that connection. The caller must keep the connection object alive for the
171
- duration of the call. */
247
+ duration of the call. Raises like quic_conn_get_open. */
172
248
  ngtcp2_conn *quic_conn_ptr(VALUE owner);
173
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
+
174
256
  /* ngtcp2_crypto_conn_ref.get_conn for quic_conn_t. */
175
257
  ngtcp2_conn *quic_conn_get_conn(ngtcp2_crypto_conn_ref *conn_ref);
176
258
 
@@ -204,20 +286,56 @@ void quic_conn_copy_alpn(quic_conn_t *c, VALUE settings_v);
204
286
  initial_max_streams_bidi streams over the whole connection. */
205
287
  void quic_conn_give_back_peer_stream(ngtcp2_conn *conn, int64_t stream_id);
206
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
+
207
310
  void quic_fill_transport_params(ngtcp2_transport_params *params, VALUE tp);
208
- 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);
209
322
 
210
323
  /* When settings_v.qlog_dir is set, open <qlog_dir>/<c->scid in hex>.sqlog
211
- (as ngtcp2's examples name it) and have ngtcp2 write the connection's
212
- qlog there. c->scid must be set. A file that cannot be opened only goes
213
- without qlog: QUIC::Settings#check_qlog_dir already caught a wrong
214
- 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. */
215
331
  void quic_conn_open_qlog(quic_conn_t *c, VALUE settings_v,
216
332
  ngtcp2_settings *settings);
217
333
 
218
- /* Close c's qlog file, if open. Called wherever the connection ends, so
219
- that the file does not wait for GC; what ngtcp2 logs afterwards goes
220
- 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. */
221
339
  void quic_conn_close_qlog(quic_conn_t *c);
222
340
 
223
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);