• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef QUICHE_H
28 #define QUICHE_H
29 
30 #if defined(__cplusplus)
31 extern "C" {
32 #endif
33 
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <stddef.h>
37 #ifdef __unix__
38 #include <sys/types.h>
39 #endif
40 #ifdef _MSC_VER
41 #include <BaseTsd.h>
42 #define ssize_t SSIZE_T
43 #endif
44 
45 // QUIC transport API.
46 //
47 
48 // The current QUIC wire version.
49 #define QUICHE_PROTOCOL_VERSION 0xff00001d
50 
51 // The maximum length of a connection ID.
52 #define QUICHE_MAX_CONN_ID_LEN 20
53 
54 // The minimum length of Initial packets sent by a client.
55 #define QUICHE_MIN_CLIENT_INITIAL_LEN 1200
56 
57 enum quiche_error {
58     // There is no more work to do.
59     QUICHE_ERR_DONE = -1,
60 
61     // The provided buffer is too short.
62     QUICHE_ERR_BUFFER_TOO_SHORT = -2,
63 
64     // The provided packet cannot be parsed because its version is unknown.
65     QUICHE_ERR_UNKNOWN_VERSION = -3,
66 
67     // The provided packet cannot be parsed because it contains an invalid
68     // frame.
69     QUICHE_ERR_INVALID_FRAME = -4,
70 
71     // The provided packet cannot be parsed.
72     QUICHE_ERR_INVALID_PACKET = -5,
73 
74     // The operation cannot be completed because the connection is in an
75     // invalid state.
76     QUICHE_ERR_INVALID_STATE = -6,
77 
78     // The operation cannot be completed because the stream is in an
79     // invalid state.
80     QUICHE_ERR_INVALID_STREAM_STATE = -7,
81 
82     // The peer's transport params cannot be parsed.
83     QUICHE_ERR_INVALID_TRANSPORT_PARAM = -8,
84 
85     // A cryptographic operation failed.
86     QUICHE_ERR_CRYPTO_FAIL = -9,
87 
88     // The TLS handshake failed.
89     QUICHE_ERR_TLS_FAIL = -10,
90 
91     // The peer violated the local flow control limits.
92     QUICHE_ERR_FLOW_CONTROL = -11,
93 
94     // The peer violated the local stream limits.
95     QUICHE_ERR_STREAM_LIMIT = -12,
96 
97     // The received data exceeds the stream's final size.
98     QUICHE_ERR_FINAL_SIZE = -13,
99 
100     // Error in congestion control.
101     QUICHE_ERR_CONGESTION_CONTROL = -14,
102 };
103 
104 // Returns a human readable string with the quiche version number.
105 const char *quiche_version(void);
106 
107 // Enables logging. |cb| will be called with log messages
108 int quiche_enable_debug_logging(void (*cb)(const char *line, void *argp),
109                                 void *argp);
110 
111 // Stores configuration shared between multiple connections.
112 typedef struct Config quiche_config;
113 
114 // Creates a config object with the given version.
115 quiche_config *quiche_config_new(uint32_t version);
116 
117 // Configures the given certificate chain.
118 int quiche_config_load_cert_chain_from_pem_file(quiche_config *config,
119                                                 const char *path);
120 
121 // Configures the given private key.
122 int quiche_config_load_priv_key_from_pem_file(quiche_config *config,
123                                               const char *path);
124 
125 // Configures whether to verify the peer's certificate.
126 void quiche_config_verify_peer(quiche_config *config, bool v);
127 
128 // Configures whether to send GREASE.
129 void quiche_config_grease(quiche_config *config, bool v);
130 
131 // Enables logging of secrets.
132 void quiche_config_log_keys(quiche_config *config);
133 
134 // Enables sending or receiving early data.
135 void quiche_config_enable_early_data(quiche_config *config);
136 
137 // Configures the list of supported application protocols.
138 int quiche_config_set_application_protos(quiche_config *config,
139                                          const uint8_t *protos,
140                                          size_t protos_len);
141 
142 // Sets the `max_idle_timeout` transport parameter.
143 void quiche_config_set_max_idle_timeout(quiche_config *config, uint64_t v);
144 
145 // Sets the `max_udp_payload_size transport` parameter.
146 void quiche_config_set_max_udp_payload_size(quiche_config *config, uint64_t v);
147 
148 // Sets the `initial_max_data` transport parameter.
149 void quiche_config_set_initial_max_data(quiche_config *config, uint64_t v);
150 
151 // Sets the `initial_max_stream_data_bidi_local` transport parameter.
152 void quiche_config_set_initial_max_stream_data_bidi_local(quiche_config *config, uint64_t v);
153 
154 // Sets the `initial_max_stream_data_bidi_remote` transport parameter.
155 void quiche_config_set_initial_max_stream_data_bidi_remote(quiche_config *config, uint64_t v);
156 
157 // Sets the `initial_max_stream_data_uni` transport parameter.
158 void quiche_config_set_initial_max_stream_data_uni(quiche_config *config, uint64_t v);
159 
160 // Sets the `initial_max_streams_bidi` transport parameter.
161 void quiche_config_set_initial_max_streams_bidi(quiche_config *config, uint64_t v);
162 
163 // Sets the `initial_max_streams_uni` transport parameter.
164 void quiche_config_set_initial_max_streams_uni(quiche_config *config, uint64_t v);
165 
166 // Sets the `ack_delay_exponent` transport parameter.
167 void quiche_config_set_ack_delay_exponent(quiche_config *config, uint64_t v);
168 
169 // Sets the `max_ack_delay` transport parameter.
170 void quiche_config_set_max_ack_delay(quiche_config *config, uint64_t v);
171 
172 // Sets the `disable_active_migration` transport parameter.
173 void quiche_config_set_disable_active_migration(quiche_config *config, bool v);
174 
175 enum quiche_cc_algorithm {
176     QUICHE_CC_RENO = 0,
177     QUICHE_CC_CUBIC = 1,
178 };
179 
180 // Sets the congestion control algorithm used.
181 void quiche_config_set_cc_algorithm(quiche_config *config, enum quiche_cc_algorithm algo);
182 
183 // Configures whether to use HyStart++.
184 void quiche_config_enable_hystart(quiche_config *config, bool v);
185 
186 // Configures whether to enable receiving DATAGRAM frames.
187 void quiche_config_enable_dgram(quiche_config *config, bool enabled,
188                                 size_t recv_queue_len,
189                                 size_t send_queue_len);
190 
191 // Frees the config object.
192 void quiche_config_free(quiche_config *config);
193 
194 // Extracts version, type, source / destination connection ID and address
195 // verification token from the packet in |buf|.
196 int quiche_header_info(const uint8_t *buf, size_t buf_len, size_t dcil,
197                        uint32_t *version, uint8_t *type,
198                        uint8_t *scid, size_t *scid_len,
199                        uint8_t *dcid, size_t *dcid_len,
200                        uint8_t *token, size_t *token_len);
201 
202 // A QUIC connection.
203 typedef struct Connection quiche_conn;
204 
205 // Creates a new server-side connection.
206 quiche_conn *quiche_accept(const uint8_t *scid, size_t scid_len,
207                            const uint8_t *odcid, size_t odcid_len,
208                            quiche_config *config);
209 
210 // Creates a new client-side connection.
211 quiche_conn *quiche_connect(const char *server_name, const uint8_t *scid,
212                             size_t scid_len, quiche_config *config);
213 
214 // Writes a version negotiation packet.
215 ssize_t quiche_negotiate_version(const uint8_t *scid, size_t scid_len,
216                                  const uint8_t *dcid, size_t dcid_len,
217                                  uint8_t *out, size_t out_len);
218 
219 // Writes a retry packet.
220 ssize_t quiche_retry(const uint8_t *scid, size_t scid_len,
221                      const uint8_t *dcid, size_t dcid_len,
222                      const uint8_t *new_scid, size_t new_scid_len,
223                      const uint8_t *token, size_t token_len,
224                      uint32_t version, uint8_t *out, size_t out_len);
225 
226 // Returns true if the given protocol version is supported.
227 bool quiche_version_is_supported(uint32_t version);
228 
229 quiche_conn *quiche_conn_new_with_tls(const uint8_t *scid, size_t scid_len,
230                                       const uint8_t *odcid, size_t odcid_len,
231                                       quiche_config *config, void *ssl,
232                                       bool is_server);
233 
234 // Enables keylog to the specified file path. Returns true on success.
235 bool quiche_conn_set_keylog_path(quiche_conn *conn, const char *path);
236 
237 // Enables keylog to the specified file descriptor. Unix only.
238 void quiche_conn_set_keylog_fd(quiche_conn *conn, int fd);
239 
240 // Enables qlog to the specified file path. Returns true on success.
241 bool quiche_conn_set_qlog_path(quiche_conn *conn, const char *path,
242                           const char *log_title, const char *log_desc);
243 
244 // Enables qlog to the specified file descriptor. Unix only.
245 void quiche_conn_set_qlog_fd(quiche_conn *conn, int fd, const char *log_title,
246                              const char *log_desc);
247 
248 // Processes QUIC packets received from the peer.
249 ssize_t quiche_conn_recv(quiche_conn *conn, uint8_t *buf, size_t buf_len);
250 
251 // Writes a single QUIC packet to be sent to the peer.
252 ssize_t quiche_conn_send(quiche_conn *conn, uint8_t *out, size_t out_len);
253 
254 // Buffer holding data at a specific offset.
255 typedef struct RangeBuf quiche_rangebuf;
256 
257 // Reads contiguous data from a stream.
258 ssize_t quiche_conn_stream_recv(quiche_conn *conn, uint64_t stream_id,
259                                 uint8_t *out, size_t buf_len, bool *fin);
260 
261 // Writes data to a stream.
262 ssize_t quiche_conn_stream_send(quiche_conn *conn, uint64_t stream_id,
263                                 const uint8_t *buf, size_t buf_len, bool fin);
264 
265 enum quiche_shutdown {
266     QUICHE_SHUTDOWN_READ = 0,
267     QUICHE_SHUTDOWN_WRITE = 1,
268 };
269 
270 // Shuts down reading or writing from/to the specified stream.
271 int quiche_conn_stream_shutdown(quiche_conn *conn, uint64_t stream_id,
272                                 enum quiche_shutdown direction, uint64_t err);
273 
274 ssize_t quiche_conn_stream_capacity(quiche_conn *conn, uint64_t stream_id);
275 
276 // Returns true if all the data has been read from the specified stream.
277 bool quiche_conn_stream_finished(quiche_conn *conn, uint64_t stream_id);
278 
279 typedef struct StreamIter quiche_stream_iter;
280 
281 // Returns an iterator over streams that have outstanding data to read.
282 quiche_stream_iter *quiche_conn_readable(quiche_conn *conn);
283 
284 // Returns an iterator over streams that can be written to.
285 quiche_stream_iter *quiche_conn_writable(quiche_conn *conn);
286 
287 // Returns the amount of time until the next timeout event, in nanoseconds.
288 uint64_t quiche_conn_timeout_as_nanos(quiche_conn *conn);
289 
290 // Returns the amount of time until the next timeout event, in milliseconds.
291 uint64_t quiche_conn_timeout_as_millis(quiche_conn *conn);
292 
293 // Processes a timeout event.
294 void quiche_conn_on_timeout(quiche_conn *conn);
295 
296 // Closes the connection with the given error and reason.
297 int quiche_conn_close(quiche_conn *conn, bool app, uint64_t err,
298                       const uint8_t *reason, size_t reason_len);
299 
300 // Returns the negotiated ALPN protocol.
301 void quiche_conn_application_proto(quiche_conn *conn, const uint8_t **out,
302                                    size_t *out_len);
303 
304 // Returns true if the connection handshake is complete.
305 bool quiche_conn_is_established(quiche_conn *conn);
306 
307 // Returns true if the connection has a pending handshake that has progressed
308 // enough to send or receive early data.
309 bool quiche_conn_is_in_early_data(quiche_conn *conn);
310 
311 // Returns true if the connection is closed.
312 bool quiche_conn_is_closed(quiche_conn *conn);
313 
314 // Initializes the stream's application data.
315 //
316 // Stream data can only be initialized once. Additional calls to this method
317 // will fail.
318 //
319 // Note that the application is responsible for freeing the data.
320 int quiche_conn_stream_init_application_data(quiche_conn *conn,
321                                              uint64_t stream_id,
322                                              void *data);
323 
324 // Returns the stream's application data, if any was initialized.
325 void *quiche_conn_stream_application_data(quiche_conn *conn, uint64_t stream_id);
326 
327 // Fetches the next stream from the given iterator. Returns false if there are
328 // no more elements in the iterator.
329 bool quiche_stream_iter_next(quiche_stream_iter *iter, uint64_t *stream_id);
330 
331 // Frees the given stream iterator object.
332 void quiche_stream_iter_free(quiche_stream_iter *iter);
333 
334 typedef struct {
335     // The number of QUIC packets received on this connection.
336     size_t recv;
337 
338     // The number of QUIC packets sent on this connection.
339     size_t sent;
340 
341     // The number of QUIC packets that were lost.
342     size_t lost;
343 
344     // The estimated round-trip time of the connection (in nanoseconds).
345     uint64_t rtt;
346 
347     // The size of the connection's congestion window in bytes.
348     size_t cwnd;
349 
350     // The estimated data delivery rate in bytes/s.
351     uint64_t delivery_rate;
352 } quiche_stats;
353 
354 // Collects and returns statistics about the connection.
355 void quiche_conn_stats(quiche_conn *conn, quiche_stats *out);
356 
357 // Returns the maximum DATAGRAM payload that can be sent.
358 ssize_t quiche_conn_dgram_max_writable_len(quiche_conn *conn);
359 
360 // Reads the first received DATAGRAM.
361 ssize_t quiche_conn_dgram_recv(quiche_conn *conn, uint8_t *buf,
362                                size_t buf_len);
363 
364 // Sends data in a DATAGRAM frame.
365 ssize_t quiche_conn_dgram_send(quiche_conn *conn, const uint8_t *buf,
366                                size_t buf_len);
367 
368 // Purges queued outgoing DATAGRAMs matching the predicate.
369 void quiche_conn_dgram_purge_outgoing(quiche_conn *conn,
370                                       bool (*f)(uint8_t *, size_t));
371 
372 // Frees the connection object.
373 void quiche_conn_free(quiche_conn *conn);
374 
375 
376 // HTTP/3 API
377 //
378 
379 // List of ALPN tokens of supported HTTP/3 versions.
380 #define QUICHE_H3_APPLICATION_PROTOCOL "\x05h3-29\x05h3-28\x05h3-27"
381 
382 enum quiche_h3_error {
383     /// There is no error or no work to do
384     QUICHE_H3_ERR_DONE = -1,
385 
386     /// The provided buffer is too short.
387     QUICHE_H3_ERR_BUFFER_TOO_SHORT = -2,
388 
389     /// Internal error in the HTTP/3 stack.
390     QUICHE_H3_ERR_INTERNAL_ERROR = -3,
391 
392     /// Endpoint detected that the peer is exhibiting behavior that causes.
393     /// excessive load.
394     QUICHE_H3_ERR_EXCESSIVE_LOAD = -4,
395 
396     /// Stream ID or Push ID greater that current maximum was
397     /// used incorrectly, such as exceeding a limit, reducing a limit,
398     /// or being reused.
399     QUICHE_H3_ERR_ID_ERROR= -5,
400 
401     /// The endpoint detected that its peer created a stream that it will not
402     /// accept.
403     QUICHE_H3_ERR_STREAM_CREATION_ERROR = -6,
404 
405     /// A required critical stream was closed.
406     QUICHE_H3_ERR_CLOSED_CRITICAL_STREAM = -7,
407 
408     /// No SETTINGS frame at beginning of control stream.
409     QUICHE_H3_ERR_MISSING_SETTINGS = -8,
410 
411     /// A frame was received which is not permitted in the current state.
412     QUICHE_H3_ERR_FRAME_UNEXPECTED = -9,
413 
414     /// Frame violated layout or size rules.
415     QUICHE_H3_ERR_FRAME_ERROR = -10,
416 
417     /// QPACK Header block decompression failure.
418     QUICHE_H3_ERR_QPACK_DECOMPRESSION_FAILED = -11,
419 
420     /// Error originated from the transport layer.
421     QUICHE_H3_ERR_TRANSPORT_ERROR = -12,
422 
423     /// The underlying QUIC stream (or connection) doesn't have enough capacity
424     /// for the operation to complete. The application should retry later on.
425     QUICHE_H3_ERR_STREAM_BLOCKED = -13,
426 };
427 
428 // Stores configuration shared between multiple connections.
429 typedef struct Http3Config quiche_h3_config;
430 
431 // Creates an HTTP/3 config object with default settings values.
432 quiche_h3_config *quiche_h3_config_new(void);
433 
434 // Sets the `SETTINGS_MAX_HEADER_LIST_SIZE` setting.
435 void quiche_h3_config_set_max_header_list_size(quiche_h3_config *config, uint64_t v);
436 
437 // Sets the `SETTINGS_QPACK_MAX_TABLE_CAPACITY` setting.
438 void quiche_h3_config_set_qpack_max_table_capacity(quiche_h3_config *config, uint64_t v);
439 
440 // Sets the `SETTINGS_QPACK_BLOCKED_STREAMS` setting.
441 void quiche_h3_config_set_qpack_blocked_streams(quiche_h3_config *config, uint64_t v);
442 
443 // Frees the HTTP/3 config object.
444 void quiche_h3_config_free(quiche_h3_config *config);
445 
446 // A QUIC connection.
447 typedef struct Http3Connection quiche_h3_conn;
448 
449 // Creates a new server-side connection.
450 quiche_h3_conn *quiche_h3_accept(quiche_conn *quiche_conn,
451                                  quiche_h3_config *config);
452 
453 // Creates a new HTTP/3 connection using the provided QUIC connection.
454 quiche_h3_conn *quiche_h3_conn_new_with_transport(quiche_conn *quiche_conn,
455                                                   quiche_h3_config *config);
456 
457 enum quiche_h3_event_type {
458     QUICHE_H3_EVENT_HEADERS,
459     QUICHE_H3_EVENT_DATA,
460     QUICHE_H3_EVENT_FINISHED,
461     QUICHE_H3_EVENT_DATAGRAM,
462     QUICHE_H3_EVENT_GOAWAY,
463 };
464 
465 typedef struct Http3Event quiche_h3_event;
466 
467 // Processes HTTP/3 data received from the peer.
468 int quiche_h3_conn_poll(quiche_h3_conn *conn, quiche_conn *quic_conn,
469                         quiche_h3_event **ev);
470 
471 // Returns the type of the event.
472 enum quiche_h3_event_type quiche_h3_event_type(quiche_h3_event *ev);
473 
474 // Iterates over the headers in the event.
475 //
476 // The `cb` callback will be called for each header in `ev`. `cb` should check
477 // the validity of pseudo-headers and headers. If `cb` returns any value other
478 // than `0`, processing will be interrupted and the value is returned to the
479 // caller.
480 int quiche_h3_event_for_each_header(quiche_h3_event *ev,
481                                     int (*cb)(uint8_t *name, size_t name_len,
482                                               uint8_t *value, size_t value_len,
483                                               void *argp),
484                                     void *argp);
485 
486 // Check whether data will follow the headers on the stream.
487 bool quiche_h3_event_headers_has_body(quiche_h3_event *ev);
488 
489 // Frees the HTTP/3 event object.
490 void quiche_h3_event_free(quiche_h3_event *ev);
491 
492 typedef struct {
493     const uint8_t *name;
494     size_t name_len;
495 
496     const uint8_t *value;
497     size_t value_len;
498 } quiche_h3_header;
499 
500 // Sends an HTTP/3 request.
501 int64_t quiche_h3_send_request(quiche_h3_conn *conn, quiche_conn *quic_conn,
502                                quiche_h3_header *headers, size_t headers_len,
503                                bool fin);
504 
505 // Sends an HTTP/3 response on the specified stream with default priority.
506 int quiche_h3_send_response(quiche_h3_conn *conn, quiche_conn *quic_conn,
507                             uint64_t stream_id, quiche_h3_header *headers,
508                             size_t headers_len, bool fin);
509 
510 // Sends an HTTP/3 response on the specified stream with specified priority.
511 int quiche_h3_send_response_with_priority(quiche_h3_conn *conn,
512                             quiche_conn *quic_conn, uint64_t stream_id,
513                             quiche_h3_header *headers, size_t headers_len,
514                             const char *priority, bool fin);
515 
516 // Sends an HTTP/3 body chunk on the given stream.
517 ssize_t quiche_h3_send_body(quiche_h3_conn *conn, quiche_conn *quic_conn,
518                             uint64_t stream_id, uint8_t *body, size_t body_len,
519                             bool fin);
520 
521 // Reads request or response body data into the provided buffer.
522 ssize_t quiche_h3_recv_body(quiche_h3_conn *conn, quiche_conn *quic_conn,
523                             uint64_t stream_id, uint8_t *out, size_t out_len);
524 
525 // Writes data to the DATAGRAM send queue.
526 ssize_t quiche_h3_send_dgram(quiche_h3_conn *conn, quiche_conn *quic_conn,
527                             uint64_t flow_id, uint8_t *data, size_t data_len);
528 
529 // Reads data from the DATAGRAM receive queue.
530 ssize_t quiche_h3_recv_dgram(quiche_h3_conn *conn, quiche_conn *quic_conn,
531                             uint64_t *flow_id, uint8_t *out, size_t out_len);
532 
533 // Frees the HTTP/3 connection object.
534 void quiche_h3_conn_free(quiche_h3_conn *conn);
535 
536 #if defined(__cplusplus)
537 }  // extern C
538 #endif
539 
540 #endif // QUICHE_H
541