lsquic.h revision 758aff32
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 2#ifndef __LSQUIC_H__ 3#define __LSQUIC_H__ 4 5/** 6 * @file 7 * public API for using liblsquic is defined in this file. 8 * 9 */ 10 11#include <stdarg.h> 12#include <lsquic_types.h> 13#ifndef WIN32 14#include <sys/uio.h> 15#include <time.h> 16#else 17#include <vc_compat.h> 18#endif 19 20struct sockaddr; 21 22#ifdef __cplusplus 23extern "C" { 24#endif 25 26#define LSQUIC_MAJOR_VERSION 2 27#define LSQUIC_MINOR_VERSION 23 28#define LSQUIC_PATCH_VERSION 2 29 30/** 31 * Engine flags: 32 */ 33 34/** Server mode */ 35#define LSENG_SERVER (1 << 0) 36 37/** Use HTTP behavior */ 38#define LSENG_HTTP (1 << 1) 39 40#define LSENG_HTTP_SERVER (LSENG_SERVER|LSENG_HTTP) 41 42/** 43 * This is a list of QUIC versions that we know of. List of supported 44 * versions is in LSQUIC_SUPPORTED_VERSIONS. 45 */ 46enum lsquic_version 47{ 48 /** 49 * Q043. Support for processing PRIORITY frames. Since this library 50 * has supported PRIORITY frames from the beginning, this version is 51 * exactly the same as LSQVER_042. 52 */ 53 LSQVER_043, 54 55 /** 56 * Q046. Use IETF Draft-17 compatible packet headers. 57 */ 58 LSQVER_046, 59 60 /** 61 * Q050. Variable-length QUIC server connection IDs. Use CRYPTO frames 62 * for handshake. IETF header format matching invariants-06. Packet 63 * number encryption. Initial packets are obfuscated. 64 */ 65 LSQVER_050, 66 67#if LSQUIC_USE_Q098 68 /** 69 * Q098. This is a made-up, experimental version used to test version 70 * negotiation. The choice of 98 is similar to Google's choice of 99 71 * as the "IETF" version. 72 */ 73 LSQVER_098, 74#define LSQUIC_EXPERIMENTAL_Q098 (1 << LSQVER_098) 75#else 76#define LSQUIC_EXPERIMENTAL_Q098 0 77#endif 78 79 /** 80 * IETF QUIC Draft-27 81 */ 82 LSQVER_ID27, 83 84 /** 85 * IETF QUIC Draft-28; this version is deprecated. 86 */ 87 LSQVER_ID28, 88 89 /** 90 * IETF QUIC Draft-29 91 */ 92 LSQVER_ID29, 93 94 /** 95 * IETF QUIC Draft-30 96 */ 97 LSQVER_ID30, 98 99 /** 100 * IETF QUIC Draft-31 101 */ 102 LSQVER_ID31, 103 104 /** 105 * Special version to trigger version negotiation. 106 * [draft-ietf-quic-transport-11], Section 3. 107 */ 108 LSQVER_VERNEG, 109 110 N_LSQVER 111}; 112 113/** 114 * We currently support versions 43, 46, 50, Draft-27, Draft-28, Draft-29, 115 * Draft-30, and Draft-31. 116 * @see lsquic_version 117 */ 118#define LSQUIC_SUPPORTED_VERSIONS ((1 << N_LSQVER) - 1) 119 120/** 121 * List of versions in which the server never includes CID in short packets. 122 */ 123#define LSQUIC_FORCED_TCID0_VERSIONS ((1 << LSQVER_046)|(1 << LSQVER_050)) 124 125#define LSQUIC_EXPERIMENTAL_VERSIONS ( \ 126 (1 << LSQVER_VERNEG) | LSQUIC_EXPERIMENTAL_Q098) 127 128#define LSQUIC_DEPRECATED_VERSIONS ((1 << LSQVER_ID27) | (1 << LSQVER_ID28) \ 129 | (1 << LSQVER_ID30)) 130 131#define LSQUIC_GQUIC_HEADER_VERSIONS (1 << LSQVER_043) 132 133#define LSQUIC_IETF_VERSIONS ((1 << LSQVER_ID27) | (1 << LSQVER_ID28) \ 134 | (1 << LSQVER_ID29) | (1 << LSQVER_ID30) \ 135 | (1 << LSQVER_ID31) | (1 << LSQVER_VERNEG)) 136 137#define LSQUIC_IETF_DRAFT_VERSIONS ((1 << LSQVER_ID27) | (1 << LSQVER_ID28) \ 138 | (1 << LSQVER_ID29) | (1 << LSQVER_ID30) \ 139 | (1 << LSQVER_ID31) | (1 << LSQVER_VERNEG)) 140 141enum lsquic_hsk_status 142{ 143 /** 144 * The handshake failed. 145 */ 146 LSQ_HSK_FAIL, 147 /** 148 * The handshake succeeded without session resumption. 149 */ 150 LSQ_HSK_OK, 151 /** 152 * The handshake succeeded with session resumption. 153 */ 154 LSQ_HSK_RESUMED_OK, 155 /** 156 * Session resumption failed. Retry the connection without session 157 * resumption. 158 */ 159 LSQ_HSK_RESUMED_FAIL, 160}; 161 162/** 163 * @struct lsquic_stream_if 164 * @brief The definitions of callback functions called by lsquic_stream to 165 * process events. 166 * 167 */ 168struct lsquic_stream_if { 169 170 /** 171 * Use @ref lsquic_conn_get_ctx to get back the context. It is 172 * OK for this function to return NULL. 173 */ 174 lsquic_conn_ctx_t *(*on_new_conn)(void *stream_if_ctx, 175 lsquic_conn_t *c); 176 177 /** This is called when our side received GOAWAY frame. After this, 178 * new streams should not be created. The callback is optional. 179 */ 180 void (*on_goaway_received)(lsquic_conn_t *c); 181 void (*on_conn_closed)(lsquic_conn_t *c); 182 183 /** If you need to initiate a connection, call lsquic_conn_make_stream(). 184 * This will cause `on_new_stream' callback to be called when appropriate 185 * (this operation is delayed when maximum number of outgoing streams is 186 * reached). 187 * 188 * After `on_close' is called, the stream is no longer accessible. 189 */ 190 lsquic_stream_ctx_t * 191 (*on_new_stream)(void *stream_if_ctx, lsquic_stream_t *s); 192 193 void (*on_read) (lsquic_stream_t *s, lsquic_stream_ctx_t *h); 194 void (*on_write) (lsquic_stream_t *s, lsquic_stream_ctx_t *h); 195 void (*on_close) (lsquic_stream_t *s, lsquic_stream_ctx_t *h); 196 /* Called when datagram is ready to be written */ 197 ssize_t (*on_dg_write)(lsquic_conn_t *c, void *, size_t); 198 /* Called when datagram is read from a packet. This callback is required 199 * when es_datagrams is true. Take care to process it quickly, as this 200 * is called during lsquic_engine_packet_in(). 201 */ 202 void (*on_datagram)(lsquic_conn_t *, const void *buf, size_t); 203 /* This callback in only called in client mode */ 204 /** 205 * When handshake is completed, this optional callback is called. 206 */ 207 void (*on_hsk_done)(lsquic_conn_t *c, enum lsquic_hsk_status s); 208 /** 209 * When client receives a token in NEW_TOKEN frame, this callback is called. 210 * The callback is optional. 211 */ 212 void (*on_new_token)(lsquic_conn_t *c, const unsigned char *token, 213 size_t token_size); 214 /** 215 * This optional callback lets client record information needed to 216 * perform a session resumption next time around. 217 */ 218 void (*on_sess_resume_info)(lsquic_conn_t *c, const unsigned char *, size_t); 219}; 220 221struct ssl_ctx_st; 222struct ssl_st; 223struct lsxpack_header; 224 225/** 226 * QUIC engine in server mode needs access to certificates. This is 227 * accomplished by providing a callback and a context to the engine 228 * constructor. 229 */ 230 231/* `sni' may be NULL if engine is not HTTP mode and client TLS transport 232 * parameters did not include the SNI. 233 */ 234typedef struct ssl_ctx_st * (*lsquic_lookup_cert_f)( 235 void *lsquic_cert_lookup_ctx, const struct sockaddr *local, const char *sni); 236 237/** 238 * Minimum flow control window is set to 16 KB for both client and server. 239 * This means we can send up to this amount of data before handshake gets 240 * completed. 241 */ 242#define LSQUIC_MIN_FCW (16 * 1024) 243 244/* Each LSQUIC_DF_* value corresponds to es_* entry in 245 * lsquic_engine_settings below. 246 */ 247 248/** 249 * By default, deprecated and experimental versions are not included. 250 */ 251#define LSQUIC_DF_VERSIONS (LSQUIC_SUPPORTED_VERSIONS & \ 252 ~LSQUIC_DEPRECATED_VERSIONS & \ 253 ~LSQUIC_EXPERIMENTAL_VERSIONS) 254 255#define LSQUIC_DF_CFCW_SERVER (3 * 1024 * 1024 / 2) 256#define LSQUIC_DF_CFCW_CLIENT (15 * 1024 * 1024) 257#define LSQUIC_DF_SFCW_SERVER (1 * 1024 * 1024) 258#define LSQUIC_DF_SFCW_CLIENT (6 * 1024 * 1024) 259#define LSQUIC_DF_MAX_STREAMS_IN 100 260 261/* IQUIC uses different names for these: */ 262#define LSQUIC_DF_INIT_MAX_DATA_SERVER LSQUIC_DF_CFCW_SERVER 263#define LSQUIC_DF_INIT_MAX_DATA_CLIENT LSQUIC_DF_CFCW_CLIENT 264#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_SERVER LSQUIC_DF_SFCW_SERVER 265#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_SERVER 0 266#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_CLIENT 0 267#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_CLIENT LSQUIC_DF_SFCW_CLIENT 268#define LSQUIC_DF_INIT_MAX_STREAMS_BIDI LSQUIC_DF_MAX_STREAMS_IN 269#define LSQUIC_DF_INIT_MAX_STREAMS_UNI_CLIENT 100 270#define LSQUIC_DF_INIT_MAX_STREAMS_UNI_SERVER 3 271/* XXX What's a good value here? */ 272#define LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_CLIENT (32 * 1024) 273#define LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER (12 * 1024) 274 275/** 276 * Default idle connection time in seconds. 277 */ 278#define LSQUIC_DF_IDLE_TIMEOUT 30 279 280/** 281 * Default ping period in seconds. 282 */ 283#define LSQUIC_DF_PING_PERIOD 15 284 285/** 286 * Default handshake timeout in microseconds. 287 */ 288#define LSQUIC_DF_HANDSHAKE_TO (10 * 1000 * 1000) 289 290#define LSQUIC_DF_IDLE_CONN_TO (LSQUIC_DF_IDLE_TIMEOUT * 1000 * 1000) 291#define LSQUIC_DF_SILENT_CLOSE 1 292 293/** Default value of maximum header list size. If set to non-zero value, 294 * SETTINGS_MAX_HEADER_LIST_SIZE will be sent to peer after handshake is 295 * completed (assuming the peer supports this setting frame type). 296 */ 297#define LSQUIC_DF_MAX_HEADER_LIST_SIZE 0 298 299/** Default value of UAID (user-agent ID). */ 300#define LSQUIC_DF_UA "LSQUIC" 301 302#define LSQUIC_DF_STTL 86400 303#define LSQUIC_DF_MAX_INCHOATE (1 * 1000 * 1000) 304/** Do not use NSTP by default */ 305#define LSQUIC_DF_SUPPORT_NSTP 0 306/** TODO: IETF QUIC clients do not support push */ 307#define LSQUIC_DF_SUPPORT_PUSH 1 308#define LSQUIC_DF_SUPPORT_TCID0 1 309/** By default, LSQUIC ignores Public Reset packets. */ 310#define LSQUIC_DF_HONOR_PRST 0 311 312/** 313 * By default, LSQUIC will not send Public Reset packets in response to 314 * packets that specify unknown connections. 315 */ 316#define LSQUIC_DF_SEND_PRST 0 317 318/** By default, infinite loop checks are turned on */ 319#define LSQUIC_DF_PROGRESS_CHECK 1000 320 321/** By default, read/write events are dispatched in a loop */ 322#define LSQUIC_DF_RW_ONCE 0 323 324/** By default, the threshold is not enabled */ 325#define LSQUIC_DF_PROC_TIME_THRESH 0 326 327/** By default, packets are paced */ 328#define LSQUIC_DF_PACE_PACKETS 1 329 330/** Default clock granularity is 1000 microseconds */ 331#define LSQUIC_DF_CLOCK_GRANULARITY 1000 332 333/** The default value is 8 for simplicity */ 334#define LSQUIC_DF_SCID_LEN 8 335 336/** The default value is 60 CIDs per minute */ 337#define LSQUIC_DF_SCID_ISS_RATE 60 338 339#define LSQUIC_DF_QPACK_DEC_MAX_BLOCKED 100 340#define LSQUIC_DF_QPACK_DEC_MAX_SIZE 4096 341#define LSQUIC_DF_QPACK_ENC_MAX_BLOCKED 100 342#define LSQUIC_DF_QPACK_ENC_MAX_SIZE 4096 343 344/* By default, QPACK experiments are turned off */ 345#define LSQUIC_DF_QPACK_EXPERIMENT 0 346 347/** ECN is disabled by default */ 348#define LSQUIC_DF_ECN 0 349 350/** Allow migration by default */ 351#define LSQUIC_DF_ALLOW_MIGRATION 1 352 353/** Use QL loss bits by default */ 354#define LSQUIC_DF_QL_BITS 2 355 356/** Turn spin bit on by default */ 357#define LSQUIC_DF_SPIN 1 358 359/** Turn off delayed ACKs extension by default */ 360#define LSQUIC_DF_DELAYED_ACKS 0 361 362/** Turn on timestamp extension by default */ 363#define LSQUIC_DF_TIMESTAMPS 1 364 365/* Use Adaptive CC by default */ 366#define LSQUIC_DF_CC_ALGO 3 367 368/* Default value of the CC RTT threshold is 1.5 ms */ 369#define LSQUIC_DF_CC_RTT_THRESH 1500 370 371/** Turn off datagram extension by default */ 372#define LSQUIC_DF_DATAGRAMS 0 373 374/** Assume optimistic NAT by default. */ 375#define LSQUIC_DF_OPTIMISTIC_NAT 1 376 377/** Turn on Extensible HTTP Priorities by default. */ 378#define LSQUIC_DF_EXT_HTTP_PRIO 1 379 380/** By default, incoming packet size is not limited. */ 381#define LSQUIC_DF_MAX_UDP_PAYLOAD_SIZE_RX 0 382 383/** 384 * By default, greasing the QUIC bit is enabled (if peer sent 385 * the "grease_quic_bit" transport parameter). 386 */ 387#define LSQUIC_DF_GREASE_QUIC_BIT 1 388 389/** By default, DPLPMTUD is enabled */ 390#define LSQUIC_DF_DPLPMTUD 1 391 392/** By default, this value is left up to the engine. */ 393#define LSQUIC_DF_BASE_PLPMTU 0 394 395/** By default, this value is left up to the engine. */ 396#define LSQUIC_DF_MAX_PLPMTU 0 397 398/** By default, drop no-progress connections after 60 seconds on the server */ 399#define LSQUIC_DF_NOPROGRESS_TIMEOUT_SERVER 60 400 401/** By default, do not use no-progress timeout on the client */ 402#define LSQUIC_DF_NOPROGRESS_TIMEOUT_CLIENT 0 403 404/** By default, we use the minimum timer of 1000 milliseconds */ 405#define LSQUIC_DF_MTU_PROBE_TIMER 1000 406 407struct lsquic_engine_settings { 408 /** 409 * This is a bit mask wherein each bit corresponds to a value in 410 * enum lsquic_version. Client starts negotiating with the highest 411 * version and goes down. Server supports either of the versions 412 * specified here. 413 * 414 * This setting applies to both Google and IETF QUIC. 415 * 416 * @see lsquic_version 417 */ 418 unsigned es_versions; 419 420 /** 421 * Initial default CFCW. 422 * 423 * In server mode, per-connection values may be set lower than 424 * this if resources are scarce. 425 * 426 * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW. 427 * 428 * @see es_max_cfcw 429 */ 430 unsigned es_cfcw; 431 432 /** 433 * Initial default SFCW. 434 * 435 * In server mode, per-connection values may be set lower than 436 * this if resources are scarce. 437 * 438 * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW. 439 * 440 * @see es_max_sfcw 441 */ 442 unsigned es_sfcw; 443 444 /** 445 * This value is used to specify maximum allowed value CFCW is allowed 446 * to reach due to window auto-tuning. By default, this value is zero, 447 * which means that CFCW is not allowed to increase from its initial 448 * value. 449 * 450 * This setting is applicable to both gQUIC and IETF QUIC. 451 * 452 * @see es_cfcw, @see es_init_max_data. 453 */ 454 unsigned es_max_cfcw; 455 456 /** 457 * This value is used to specify the maximum value stream flow control 458 * window is allowed to reach due to auto-tuning. By default, this 459 * value is zero, meaning that auto-tuning is turned off. 460 * 461 * This setting is applicable to both gQUIC and IETF QUIC. 462 * 463 * @see es_sfcw, @see es_init_max_stream_data_bidi_remote, 464 * @see es_init_max_stream_data_bidi_local. 465 */ 466 unsigned es_max_sfcw; 467 468 /** MIDS */ 469 unsigned es_max_streams_in; 470 471 /** 472 * Handshake timeout in microseconds. 473 * 474 * For client, this can be set to an arbitrary value (zero turns the 475 * timeout off). 476 * 477 * For server, this value is limited to about 16 seconds. Do not set 478 * it to zero. 479 */ 480 unsigned long es_handshake_to; 481 482 /** ICSL in microseconds; GQUIC only */ 483 unsigned long es_idle_conn_to; 484 485 /** 486 * When true, CONNECTION_CLOSE is not sent when connection times out. 487 * The server will also not send a reply to client's CONNECTION_CLOSE. 488 * 489 * Corresponds to SCLS (silent close) gQUIC option. 490 */ 491 int es_silent_close; 492 493 /** 494 * This corresponds to SETTINGS_MAX_HEADER_LIST_SIZE 495 * (RFC 7540, Section 6.5.2). 0 means no limit. Defaults 496 * to @ref LSQUIC_DF_MAX_HEADER_LIST_SIZE. 497 */ 498 unsigned es_max_header_list_size; 499 500 /** UAID -- User-Agent ID. Defaults to @ref LSQUIC_DF_UA. */ 501 const char *es_ua; 502 503 /** 504 * More parameters for server 505 */ 506 uint64_t es_sttl; /* SCFG TTL in seconds */ 507 508 uint32_t es_pdmd; /* One fixed value X509 */ 509 uint32_t es_aead; /* One fixed value AESG */ 510 uint32_t es_kexs; /* One fixed value C255 */ 511 512 /* Maximum number of incoming connections in inchoate state. This is 513 * only applicable in server mode. 514 */ 515 unsigned es_max_inchoate; 516 517 /** 518 * Setting this value to 0 means that 519 * 520 * For client: 521 * a) we send a SETTINGS frame to indicate that we do not support server 522 * push; and 523 * b) All incoming pushed streams get reset immediately. 524 * (For maximum effect, set es_max_streams_in to 0.) 525 * 526 * For server: 527 * lsquic_conn_push_stream() will return -1. 528 */ 529 int es_support_push; 530 531 /** 532 * If set to true value, the server will not include connection ID in 533 * outgoing packets if client's CHLO specifies TCID=0. 534 * 535 * For client, this means including TCID=0 into CHLO message. Note that 536 * in this case, the engine tracks connections by the 537 * (source-addr, dest-addr) tuple, thereby making it necessary to create 538 * a socket for each connection. 539 * 540 * This option has no effect in Q046 and Q050, as the server never includes 541 * CIDs in the short packets. 542 * 543 * This setting is applicable to gQUIC only. 544 * 545 * The default is @ref LSQUIC_DF_SUPPORT_TCID0. 546 */ 547 int es_support_tcid0; 548 549 /** 550 * Q037 and higher support "No STOP_WAITING frame" mode. When set, the 551 * client will send NSTP option in its Client Hello message and will not 552 * sent STOP_WAITING frames, while ignoring incoming STOP_WAITING frames, 553 * if any. Note that if the version negotiation happens to downgrade the 554 * client below Q037, this mode will *not* be used. 555 * 556 * This option does not affect the server, as it must support NSTP mode 557 * if it was specified by the client. 558 * 559 * This setting is applicable to gQUIC only. 560 */ 561 int es_support_nstp; 562 563 /** 564 * If set to true value, the library will drop connections when it 565 * receives corresponding Public Reset packet. The default is to 566 * ignore these packets. 567 * 568 * The default is @ref LSQUIC_DF_HONOR_PRST. 569 */ 570 int es_honor_prst; 571 572 /** 573 * If set to true value, the library will send Public Reset packets 574 * in response to incoming packets with unknown Connection IDs. 575 * The default is @ref LSQUIC_DF_SEND_PRST. 576 */ 577 int es_send_prst; 578 579 /** 580 * A non-zero value enables internal checks that identify suspected 581 * infinite loops in user @ref on_read and @ref on_write callbacks 582 * and break them. An infinite loop may occur if user code keeps 583 * on performing the same operation without checking status, e.g. 584 * reading from a closed stream etc. 585 * 586 * The value of this parameter is as follows: should a callback return 587 * this number of times in a row without making progress (that is, 588 * reading, writing, or changing stream state), loop break will occur. 589 * 590 * The defaut value is @ref LSQUIC_DF_PROGRESS_CHECK. 591 */ 592 unsigned es_progress_check; 593 594 /** 595 * A non-zero value make stream dispatch its read-write events once 596 * per call. 597 * 598 * When zero, read and write events are dispatched until the stream 599 * is no longer readable or writeable, respectively, or until the 600 * user signals unwillingness to read or write using 601 * @ref lsquic_stream_wantread() or @ref lsquic_stream_wantwrite() 602 * or shuts down the stream. 603 * 604 * This also applies to the on_dg_write() callback. 605 * 606 * The default value is @ref LSQUIC_DF_RW_ONCE. 607 */ 608 int es_rw_once; 609 610 /** 611 * If set, this value specifies the number of microseconds that 612 * @ref lsquic_engine_process_conns() and 613 * @ref lsquic_engine_send_unsent_packets() are allowed to spend 614 * before returning. 615 * 616 * This is not an exact science and the connections must make 617 * progress, so the deadline is checked after all connections get 618 * a chance to tick (in the case of @ref lsquic_engine_process_conns()) 619 * and at least one batch of packets is sent out. 620 * 621 * When processing function runs out of its time slice, immediate 622 * calls to @ref lsquic_engine_has_unsent_packets() return false. 623 * 624 * The default value is @ref LSQUIC_DF_PROC_TIME_THRESH. 625 */ 626 unsigned es_proc_time_thresh; 627 628 /** 629 * If set to true, packet pacing is implemented per connection. 630 * 631 * The default value is @ref LSQUIC_DF_PACE_PACKETS. 632 */ 633 int es_pace_packets; 634 635 /** 636 * Clock granularity information is used by the pacer. The value 637 * is in microseconds; default is @ref LSQUIC_DF_CLOCK_GRANULARITY. 638 */ 639 unsigned es_clock_granularity; 640 641 /** 642 * Congestion control algorithm to use. 643 * 644 * 0: Use default (@ref LSQUIC_DF_CC_ALGO) 645 * 1: Cubic 646 * 2: BBRv1 647 * 3: Adaptive (Cubic or BBRv1) 648 */ 649 unsigned es_cc_algo; 650 651 /** 652 * Congestion controller RTT threshold in microseconds. 653 * 654 * Adaptive congestion control uses BBRv1 until RTT is determined. At 655 * that point a permanent choice of congestion controller is made. If 656 * RTT is smaller than or equal to es_cc_rtt_thresh, congestion 657 * controller is switched to Cubic; otherwise, BBRv1 is picked. 658 * 659 * The default value is @ref LSQUIC_DF_CC_RTT_THRESH. 660 */ 661 unsigned es_cc_rtt_thresh; 662 663 /** 664 * No progress timeout. 665 * 666 * If connection does not make progress for this number of seconds, the 667 * connection is dropped. Here, progress is defined as user streams 668 * being written to or read from. 669 * 670 * If this value is zero, this timeout is disabled. 671 * 672 * Default value is @ref LSQUIC_DF_NOPROGRESS_TIMEOUT_SERVER in server 673 * mode and @ref LSQUIC_DF_NOPROGRESS_TIMEOUT_CLIENT in client mode. 674 */ 675 unsigned es_noprogress_timeout; 676 677 /* The following settings are specific to IETF QUIC. */ 678 /* vvvvvvvvvvv */ 679 680 /** 681 * Initial max data. 682 * 683 * This is a transport parameter. 684 * 685 * Depending on the engine mode, the default value is either 686 * @ref LSQUIC_DF_INIT_MAX_DATA_CLIENT or 687 * @ref LSQUIC_DF_INIT_MAX_DATA_SERVER. 688 */ 689 unsigned es_init_max_data; 690 691 /** 692 * Initial maximum amount of stream data allowed to be sent on streams 693 * created by remote end (peer). 694 * 695 * This is a transport parameter. 696 * 697 * Depending on the engine mode, the default value is either 698 * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_CLIENT or 699 * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_SERVER. 700 */ 701 unsigned es_init_max_stream_data_bidi_remote; 702 703 /** 704 * Initial maximum amount of stream data allowed to be sent on streams 705 * created by remote end (peer). 706 * 707 * This is a transport parameter. 708 * 709 * Depending on the engine mode, the default value is either 710 * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_CLIENT or 711 * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_SERVER. 712 */ 713 unsigned es_init_max_stream_data_bidi_local; 714 715 /** 716 * Initial max stream data for unidirectional streams initiated 717 * by remote endpoint. 718 * 719 * This is a transport parameter. 720 * 721 * Depending on the engine mode, the default value is either 722 * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_CLIENT or 723 * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER. 724 */ 725 unsigned es_init_max_stream_data_uni; 726 727 /** 728 * Maximum initial number of bidirectional stream. 729 * 730 * This is a transport parameter. 731 * 732 * Default value is @ref LSQUIC_DF_INIT_MAX_STREAMS_BIDI. 733 */ 734 unsigned es_init_max_streams_bidi; 735 736 /** 737 * Maximum initial number of unidirectional stream. 738 * 739 * This is a transport parameter. 740 * 741 * Default value is @ref LSQUIC_DF_INIT_MAX_STREAMS_UNI_CLIENT or 742 * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER. 743 */ 744 unsigned es_init_max_streams_uni; 745 746 /** 747 * Idle connection timeout. 748 * 749 * This is a transport parameter. 750 * 751 * (Note: es_idle_conn_to is not reused because it is in microseconds, 752 * which, I now realize, was not a good choice. Since it will be 753 * obsoleted some time after the switchover to IETF QUIC, we do not 754 * have to keep on using strange units.) 755 * 756 * Default value is @ref LSQUIC_DF_IDLE_TIMEOUT. 757 * 758 * Maximum value is 600 seconds. 759 */ 760 unsigned es_idle_timeout; 761 762 /** 763 * Ping period. If set to non-zero value, the connection will generate and 764 * send PING frames in the absence of other activity. 765 * 766 * By default, the server does not send PINGs and the period is set to zero. 767 * The client's defaut value is @ref LSQUIC_DF_PING_PERIOD. 768 */ 769 unsigned es_ping_period; 770 771 /** 772 * Source Connection ID length. Only applicable to the IETF QUIC 773 * versions. Valid values are 0 through 20, inclusive. 774 * 775 * Default value is @ref LSQUIC_DF_SCID_LEN. 776 */ 777 unsigned es_scid_len; 778 779 /** 780 * Source Connection ID issuance rate. Only applicable to the IETF QUIC 781 * versions. This field is measured in CIDs per minute. Using value 0 782 * indicates that there is no rate limit for CID issuance. 783 * 784 * Default value is @ref LSQUIC_DF_SCID_ISS_RATE. 785 */ 786 unsigned es_scid_iss_rate; 787 788 /** 789 * Maximum size of the QPACK dynamic table that the QPACK decoder will 790 * use. 791 * 792 * The default is @ref LSQUIC_DF_QPACK_DEC_MAX_SIZE. 793 */ 794 unsigned es_qpack_dec_max_size; 795 796 /** 797 * Maximum number of blocked streams that the QPACK decoder is willing 798 * to tolerate. 799 * 800 * The default is @ref LSQUIC_DF_QPACK_DEC_MAX_BLOCKED. 801 */ 802 unsigned es_qpack_dec_max_blocked; 803 804 /** 805 * Maximum size of the dynamic table that the encoder is willing to use. 806 * The actual size of the dynamic table will not exceed the minimum of 807 * this value and the value advertized by peer. 808 * 809 * The default is @ref LSQUIC_DF_QPACK_ENC_MAX_SIZE. 810 */ 811 unsigned es_qpack_enc_max_size; 812 813 /** 814 * Maximum number of blocked streams that the QPACK encoder is willing 815 * to risk. The actual number of blocked streams will not exceed the 816 * minimum of this value and the value advertized by peer. 817 * 818 * The default is @ref LSQUIC_DF_QPACK_ENC_MAX_BLOCKED. 819 */ 820 unsigned es_qpack_enc_max_blocked; 821 822 /** 823 * Enable ECN support. 824 * 825 * The default is @ref LSQUIC_DF_ECN 826 */ 827 int es_ecn; 828 829 /** 830 * Allow peer to migrate connection. 831 * 832 * The default is @ref LSQUIC_DF_ALLOW_MIGRATION 833 */ 834 int es_allow_migration; 835 836 /** 837 * Use QL loss bits. Allowed values are: 838 * 0: Do not use loss bits 839 * 1: Allow loss bits 840 * 2: Allow and send loss bits 841 * 842 * Default value is @ref LSQUIC_DF_QL_BITS 843 */ 844 int es_ql_bits; 845 846 /** 847 * Enable spin bit. Allowed values are 0 and 1. 848 * 849 * Default value is @ref LSQUIC_DF_SPIN 850 */ 851 int es_spin; 852 853 /** 854 * Enable delayed ACKs extension. Allowed values are 0 and 1. 855 * 856 * Warning: this is an experimental feature. Using it will most likely 857 * lead to degraded performance. 858 * 859 * Default value is @ref LSQUIC_DF_DELAYED_ACKS 860 */ 861 int es_delayed_acks; 862 863 /** 864 * Enable timestamps extension. Allowed values are 0 and 1. 865 * 866 * Default value is @ref LSQUIC_DF_TIMESTAMPS 867 */ 868 int es_timestamps; 869 870 /** 871 * Maximum packet size we are willing to receive. This is sent to 872 * peer in transport parameters: the library does not enforce this 873 * limit for incoming packets. 874 * 875 * If set to zero, limit is not set. 876 * 877 * Default value is @ref LSQUIC_DF_MAX_UDP_PAYLOAD_SIZE_RX 878 */ 879 unsigned short es_max_udp_payload_size_rx; 880 881 /** 882 * Enable the "QUIC bit grease" extension. When set to a true value, 883 * lsquic will grease the QUIC bit on the outgoing QUIC packets if 884 * the peer sent the "grease_quic_bit" transport parameter. 885 * 886 * Default value is @ref LSQUIC_DF_GREASE_QUIC_BIT 887 */ 888 int es_grease_quic_bit; 889 890 /** 891 * If set to true value, enable DPLPMTUD -- Datagram Packetization 892 * Layer Path MTU Discovery. 893 * 894 * Default value is @ref LSQUIC_DF_DPLPMTUD 895 */ 896 int es_dplpmtud; 897 898 /** 899 * PLPMTU size expected to work for most paths. 900 * 901 * If set to zero, this value is calculated based on QUIC and IP versions. 902 * 903 * Default value is @ref LSQUIC_DF_BASE_PLPMTU. 904 */ 905 unsigned short es_base_plpmtu; 906 907 /** 908 * Largest PLPMTU size the engine will try. 909 * 910 * If set to zero, picking this value is left to the engine. 911 * 912 * Default value is @ref LSQUIC_DF_MAX_PLPMTU. 913 */ 914 unsigned short es_max_plpmtu; 915 916 /** 917 * This value specifies how long the DPLPMTUD probe timer is, in 918 * milliseconds. [draft-ietf-tsvwg-datagram-plpmtud-17] says: 919 * 920 " PROBE_TIMER: The PROBE_TIMER is configured to expire after a period 921 " longer than the maximum time to receive an acknowledgment to a 922 " probe packet. This value MUST NOT be smaller than 1 second, and 923 " SHOULD be larger than 15 seconds. Guidance on selection of the 924 " timer value are provided in section 3.1.1 of the UDP Usage 925 " Guidelines [RFC8085]. 926 * 927 * If set to zero, the default is used. 928 * 929 * Default value is @ref LSQUIC_DF_MTU_PROBE_TIMER. 930 */ 931 unsigned es_mtu_probe_timer; 932 933 /** 934 * Enable datagram extension. Allowed values are 0 and 1. 935 * 936 * Default value is @ref LSQUIC_DF_DATAGRAMS 937 */ 938 int es_datagrams; 939 940 /** 941 * If set to true, changes in peer port are assumed to be due to a 942 * benign NAT rebinding and path characteristics -- MTU, RTT, and 943 * CC state -- are not reset. 944 * 945 * Default value is @ref LSQUIC_DF_OPTIMISTIC_NAT. 946 */ 947 int es_optimistic_nat; 948 949 /** 950 * If set to true, Extensible HTTP Priorities are enabled. This 951 * is HTTP/3-only setting. 952 * 953 * Default value is @ref LSQUIC_DF_EXT_HTTP_PRIO 954 */ 955 int es_ext_http_prio; 956 957 /** 958 * If set to 1, QPACK statistics are logged per connection. 959 * 960 * If set to 2, QPACK experiments are run. In this mode, encoder 961 * and decoder setting values are randomly selected (from the range 962 * [0, whatever is specified in es_qpack_(enc|dec)_*]) and these 963 * values along with compression ratio and user agent are logged at 964 * NOTICE level when connection is destroyed. The purpose of these 965 * experiments is to use compression performance statistics to figure 966 * out a good set of default values. 967 * 968 * Default value is @ref LSQUIC_DF_QPACK_EXPERIMENT. 969 */ 970 int es_qpack_experiment; 971}; 972 973/* Initialize `settings' to default values */ 974void 975lsquic_engine_init_settings (struct lsquic_engine_settings *, 976 unsigned lsquic_engine_flags); 977 978/** 979 * Check settings for errors. 980 * 981 * @param settings Settings struct. 982 * 983 * @param flags Engine flags. 984 * 985 * @param err_buf Optional pointer to buffer into which error string 986 * is written. 987 988 * @param err_buf_sz Size of err_buf. No more than this number of bytes 989 * will be written to err_buf, including the NUL byte. 990 * 991 * @retval 0 Settings have no errors. 992 * @retval -1 There are errors in settings. 993 */ 994int 995lsquic_engine_check_settings (const struct lsquic_engine_settings *settings, 996 unsigned lsquic_engine_flags, 997 char *err_buf, size_t err_buf_sz); 998 999struct lsquic_out_spec 1000{ 1001 struct iovec *iov; 1002 size_t iovlen; 1003 const struct sockaddr *local_sa; 1004 const struct sockaddr *dest_sa; 1005 void *peer_ctx; 1006 lsquic_conn_ctx_t *conn_ctx; /* will be NULL when sending out the first batch of handshake packets */ 1007 int ecn; /* Valid values are 0 - 3. See RFC 3168 */ 1008}; 1009 1010/** 1011 * Returns number of packets successfully sent out or -1 on error. -1 should 1012 * only be returned if no packets were sent out. If -1 is returned or if the 1013 * return value is smaller than `n_packets_out', this indicates that sending 1014 * of packets is not possible. 1015 * 1016 * If not all packets could be sent out, errno is examined. If it is not 1017 * EAGAIN or EWOULDBLOCK, the connection whose packet cause the error is 1018 * closed forthwith. 1019 * 1020 * No packets will be attempted to be sent out until 1021 * @ref lsquic_engine_send_unsent_packets() is called. 1022 */ 1023typedef int (*lsquic_packets_out_f)( 1024 void *packets_out_ctx, 1025 const struct lsquic_out_spec *out_spec, 1026 unsigned n_packets_out 1027); 1028 1029/** 1030 * The shared hash interface is used to share data between multiple LSQUIC 1031 * instances. 1032 */ 1033struct lsquic_shared_hash_if 1034{ 1035 /** 1036 * If you want your item to never expire, set `expiry' to zero. 1037 * Returns 0 on success, -1 on failure. 1038 * 1039 * If inserted successfully, `free()' will be called on `data' and 'key' 1040 * pointer when the element is deleted, whether due to expiration 1041 * or explicit deletion. 1042 */ 1043 int (*shi_insert)(void *shi_ctx, void *key, unsigned key_sz, 1044 void *data, unsigned data_sz, time_t expiry); 1045 /** 1046 * Returns 0 on success, -1 on failure. 1047 */ 1048 int (*shi_delete)(void *shi_ctx, const void *key, unsigned key_sz); 1049 1050 /** 1051 * `data' is pointed to the result and `data_sz' is set to the 1052 * object size. The implementation may choose to copy the object 1053 * into buffer pointed to by `data', so you should have it ready. 1054 * 1055 * @retval 1 found. 1056 * @retval 0 not found. 1057 * @retval -1 error (perhaps not enough room in `data' if copy was 1058 * attempted). 1059 */ 1060 int (*shi_lookup)(void *shi_ctx, const void *key, unsigned key_sz, 1061 void **data, unsigned *data_sz); 1062}; 1063 1064/** 1065 * The packet out memory interface is used by LSQUIC to get buffers to 1066 * which outgoing packets will be written before they are passed to 1067 * ea_packets_out callback. 1068 * 1069 * If not specified, malloc() and free() are used. 1070 */ 1071struct lsquic_packout_mem_if 1072{ 1073 /** 1074 * Allocate buffer for sending. 1075 */ 1076 void * (*pmi_allocate) (void *pmi_ctx, void *peer_ctx, lsquic_conn_ctx_t *, unsigned short sz, 1077 char is_ipv6); 1078 /** 1079 * This function is used to release the allocated buffer after it is 1080 * sent via @ref ea_packets_out. 1081 */ 1082 void (*pmi_release) (void *pmi_ctx, void *peer_ctx, void *buf, 1083 char is_ipv6); 1084 /** 1085 * If allocated buffer is not going to be sent, return it to the caller 1086 * using this function. 1087 */ 1088 void (*pmi_return) (void *pmi_ctx, void *peer_ctx, void *buf, 1089 char is_ipv6); 1090}; 1091 1092typedef void (*lsquic_cids_update_f)(void *ctx, void **peer_ctx, 1093 const lsquic_cid_t *cids, unsigned n_cids); 1094 1095struct stack_st_X509; 1096 1097enum lsquic_hsi_flag { 1098 /** 1099 * Turn HTTP/1.x mode on or off. In this mode, decoded name and value 1100 * pair are separated by ": " and "\r\n" is appended to the end of the 1101 * string. By default, this mode is off. 1102 */ 1103 LSQUIC_HSI_HTTP1X = 1 << 1, 1104 /** Include name hash into lsxpack_header */ 1105 LSQUIC_HSI_HASH_NAME = 1 << 2, 1106 /** Include nameval hash into lsxpack_header */ 1107 LSQUIC_HSI_HASH_NAMEVAL = 1 << 3, 1108}; 1109 1110struct lsquic_hset_if 1111{ 1112 /** 1113 * Create a new header set. This object is (and must be) fetched from a 1114 * stream by calling @ref lsquic_stream_get_hset() before the stream can 1115 * be read. 1116 * 1117 * `stream' may be set to NULL in server mode. 1118 */ 1119 void * (*hsi_create_header_set)(void *hsi_ctx, lsquic_stream_t *stream, 1120 int is_push_promise); 1121 /** 1122 * Return a header set prepared for decoding. If `hdr' is NULL, this 1123 * means return a new structure with at least `space' bytes available 1124 * in the decoder buffer. On success, a newly prepared header is 1125 * returned. 1126 * 1127 * If `hdr' is not NULL, it means there was not enough decoder buffer 1128 * and it must be increased to at least `space' bytes. `buf', `val_len', 1129 * and `name_offset' member of the `hdr' structure may change. On 1130 * success, the return value is the same as `hdr'. 1131 * 1132 * If NULL is returned, the space cannot be allocated. 1133 */ 1134 struct lsxpack_header * 1135 (*hsi_prepare_decode)(void *hdr_set, 1136 struct lsxpack_header *hdr, 1137 size_t space); 1138 /** 1139 * Process new header. Return 0 on success, a positive value if a header 1140 * error occured, or a negative value on any other error. 1141 * 1142 * A positive return value will result in cancellation of associated 1143 * stream. 1144 * 1145 * A negative return value will result in connection being aborted. 1146 * 1147 * `hdr_set' is the header set object returned by 1148 * @ref hsi_create_header_set(). 1149 * 1150 * `hdr' is the header returned by @ref `hsi_prepare_decode'. 1151 * 1152 * If `hdr' is NULL, this means that no more header are going to be 1153 * added to the set. 1154 */ 1155 int (*hsi_process_header)(void *hdr_set, struct lsxpack_header *hdr); 1156 /** 1157 * Discard header set. This is called for unclaimed header sets and 1158 * header sets that had an error. 1159 */ 1160 void (*hsi_discard_header_set)(void *hdr_set); 1161 /** 1162 * These flags specify properties of decoded headers passed to 1163 * hsi_process_header(). This is only applicable to QPACK headers; 1164 * HPACK library header properties are based on compilation, not 1165 * run-time, options. 1166 */ 1167 enum lsquic_hsi_flag hsi_flags; 1168}; 1169 1170/** 1171 * SSL keylog interface. 1172 */ 1173struct lsquic_keylog_if 1174{ 1175 /** Return keylog handle or NULL if no key logging is desired */ 1176 void * (*kli_open) (void *keylog_ctx, lsquic_conn_t *); 1177 1178 /** 1179 * Log line. The first argument is the pointer returned by 1180 * @ref kli_open. 1181 */ 1182 void (*kli_log_line) (void *handle, const char *line); 1183 1184 /** 1185 * Close handle. 1186 */ 1187 void (*kli_close) (void *handle); 1188}; 1189 1190/** 1191 * This struct contains a list of all callbacks that are used by the engine 1192 * to communicate with the user code. Most of these are optional, while 1193 * the following are mandatory: 1194 * 1195 * @ref ea_stream_if The stream interface. 1196 * @ref ea_packets_out Function to send packets. 1197 * @ref ea_lookup_cert Function to look up certificates by SNI (used 1198 * in server mode). 1199 * 1200 * A pointer to this structure is passed to engine constructor 1201 * @ref lsquic_engine_new(). 1202 */ 1203struct lsquic_engine_api 1204{ 1205 const struct lsquic_engine_settings *ea_settings; /* Optional */ 1206 /** Stream interface is required to manage connections and streams. */ 1207 const struct lsquic_stream_if *ea_stream_if; 1208 void *ea_stream_if_ctx; 1209 /** Function to send packets out is required. */ 1210 lsquic_packets_out_f ea_packets_out; 1211 void *ea_packets_out_ctx; 1212 /** Function to look up certificates by SNI is used in server mode. */ 1213 lsquic_lookup_cert_f ea_lookup_cert; 1214 void *ea_cert_lu_ctx; 1215 struct ssl_ctx_st * (*ea_get_ssl_ctx)(void *peer_ctx); 1216 /** 1217 * Shared hash interface is optional. If set to zero, performance of 1218 * multiple LSQUIC instances will be degraded. 1219 */ 1220 const struct lsquic_shared_hash_if *ea_shi; 1221 void *ea_shi_ctx; 1222 /** 1223 * Memory interface is optional. 1224 */ 1225 const struct lsquic_packout_mem_if *ea_pmi; 1226 void *ea_pmi_ctx; 1227 /** 1228 * Optional interface to report new and old source connection IDs. 1229 */ 1230 lsquic_cids_update_f ea_new_scids; 1231 lsquic_cids_update_f ea_live_scids; 1232 lsquic_cids_update_f ea_old_scids; 1233 void *ea_cids_update_ctx; 1234 /** 1235 * Function to verify server certificate. The chain contains at least 1236 * one element. The first element in the chain is the server 1237 * certificate. The chain belongs to the library. If you want to 1238 * retain it, call sk_X509_up_ref(). 1239 * 1240 * 0 is returned on success, -1 on error. 1241 * 1242 * If the function pointer is not set, no verification is performed 1243 * (the connection is allowed to proceed). 1244 */ 1245 int (*ea_verify_cert)(void *verify_ctx, 1246 struct stack_st_X509 *chain); 1247 void *ea_verify_ctx; 1248 1249 /** 1250 * Optional header set interface. If not specified, the incoming headers 1251 * are converted to HTTP/1.x format and are read from stream and have to 1252 * be parsed again. 1253 */ 1254 const struct lsquic_hset_if *ea_hsi_if; 1255 void *ea_hsi_ctx; 1256#if LSQUIC_CONN_STATS 1257 /** 1258 * If set, engine will print cumulative connection statistics to this 1259 * file just before it is destroyed. 1260 */ 1261 void /* FILE, really */ *ea_stats_fh; 1262#endif 1263 1264 /** 1265 * Optional SSL key logging interface. 1266 */ 1267 const struct lsquic_keylog_if *ea_keylog_if; 1268 void *ea_keylog_ctx; 1269 1270 /** 1271 * The optional ALPN string is used by the client if @ref LSENG_HTTP 1272 * is not set. 1273 */ 1274 const char *ea_alpn; 1275 1276 /** 1277 * Optional interface to control the creation of connection IDs 1278 */ 1279 void (*ea_generate_scid)(lsquic_conn_t *, 1280 lsquic_cid_t *, unsigned); 1281}; 1282 1283/** 1284 * Create new engine. 1285 * 1286 * @param lsquic_engine_flags A bitmask of @ref LSENG_SERVER and 1287 * @ref LSENG_HTTP 1288 * 1289 * @param api Required parameter that specifies 1290 * various callbacks. 1291 * 1292 * The engine can be instantiated either in server mode (when LSENG_SERVER 1293 * is set) or client mode. If you need both server and client in your 1294 * program, create two engines (or as many as you'd like). 1295 */ 1296lsquic_engine_t * 1297lsquic_engine_new (unsigned lsquic_engine_flags, 1298 const struct lsquic_engine_api *api); 1299 1300/** 1301 * Create a client connection to peer identified by `peer_ctx'. 1302 * 1303 * To let the engine specify QUIC version, use N_LSQVER. If session resumption 1304 * information is supplied, version is picked from there instead. 1305 * 1306 * If `base_plpmtu' is set to zero, it is selected based on the 1307 * engine settings, QUIC version, and IP version. 1308 */ 1309lsquic_conn_t * 1310lsquic_engine_connect (lsquic_engine_t *, enum lsquic_version, 1311 const struct sockaddr *local_sa, 1312 const struct sockaddr *peer_sa, 1313 void *peer_ctx, lsquic_conn_ctx_t *conn_ctx, 1314 const char *hostname, unsigned short base_plpmtu, 1315 const unsigned char *sess_resume, size_t sess_resume_len, 1316 /** Resumption token: optional */ 1317 const unsigned char *token, size_t token_sz); 1318 1319/** 1320 * Pass incoming packet to the QUIC engine. This function can be called 1321 * more than once in a row. After you add one or more packets, call 1322 * lsquic_engine_process_conns() to schedule output, if any. 1323 * 1324 * @retval 0 Packet was processed by a real connection. 1325 * 1326 * @retval 1 Packet was handled successfully, but not by a connection. 1327 * This may happen with version negotiation and public reset 1328 * packets as well as some packets that may be ignored. 1329 * 1330 * @retval -1 An error occurred. Possible reasons are failure to allocate 1331 * memory and invalid @param sa_local in client mode. 1332 */ 1333int 1334lsquic_engine_packet_in (lsquic_engine_t *, 1335 const unsigned char *packet_in_data, size_t packet_in_size, 1336 const struct sockaddr *sa_local, const struct sockaddr *sa_peer, 1337 void *peer_ctx, int ecn); 1338 1339/** 1340 * Process tickable connections. This function must be called often enough so 1341 * that packets and connections do not expire. 1342 */ 1343void 1344lsquic_engine_process_conns (lsquic_engine_t *engine); 1345 1346/** 1347 * Returns true if engine has some unsent packets. This happens if 1348 * @ref ea_packets_out() could not send everything out or if processing 1349 * deadline was exceeded (see @ref es_proc_time_thresh). 1350 */ 1351int 1352lsquic_engine_has_unsent_packets (lsquic_engine_t *engine); 1353 1354/** 1355 * Send out as many unsent packets as possibe: until we are out of unsent 1356 * packets or until @ref ea_packets_out() fails. 1357 * 1358 * If @ref ea_packets_out() does fail cannot send all packets, this 1359 * function must be called to signify that sending of packets is possible 1360 * again. 1361 */ 1362void 1363lsquic_engine_send_unsent_packets (lsquic_engine_t *engine); 1364 1365/** 1366 * Destroy engine and all connections and streams in it and free all 1367 * memory associated with this engine. 1368 */ 1369void 1370lsquic_engine_destroy (lsquic_engine_t *); 1371 1372/** Return max allowed outbound streams less current outbound streams. */ 1373unsigned 1374lsquic_conn_n_avail_streams (const lsquic_conn_t *); 1375 1376/** 1377 * Create a new request stream. This causes @ref on_new_stream() callback 1378 * to be called. If creating more requests is not permitted at the moment 1379 * (due to number of concurrent streams limit), stream creation is registered 1380 * as "pending" and the stream is created later when number of streams dips 1381 * under the limit again. Any number of pending streams can be created. 1382 * Use @ref lsquic_conn_n_pending_streams() and 1383 * @ref lsquic_conn_cancel_pending_streams() to manage pending streams. 1384 * 1385 * If connection is going away, @ref on_new_stream() is called with the 1386 * stream parameter set to NULL. 1387 */ 1388void 1389lsquic_conn_make_stream (lsquic_conn_t *); 1390 1391/** Return number of delayed streams currently pending */ 1392unsigned 1393lsquic_conn_n_pending_streams (const lsquic_conn_t *); 1394 1395/** Cancel `n' pending streams. Returns new number of pending streams. */ 1396unsigned 1397lsquic_conn_cancel_pending_streams (lsquic_conn_t *, unsigned n); 1398 1399/** 1400 * Mark connection as going away: send GOAWAY frame and do not accept 1401 * any more incoming streams, nor generate streams of our own. 1402 * 1403 * Only applicable to HTTP/3 and GQUIC connections. Otherwise a no-op. 1404 */ 1405void 1406lsquic_conn_going_away (lsquic_conn_t *); 1407 1408/** 1409 * This forces connection close. on_conn_closed and on_close callbacks 1410 * will be called. 1411 */ 1412void 1413lsquic_conn_close (lsquic_conn_t *); 1414 1415/** 1416 * Set whether you want to read from stream. If @param is_want is true, 1417 * @ref on_read() will be called when there is readable data in the 1418 * stream. If @param is false, @ref on_read() will not be called. 1419 * 1420 * Returns previous value of this flag. 1421 */ 1422int 1423lsquic_stream_wantread (lsquic_stream_t *s, int is_want); 1424 1425/** 1426 * Read up to @param len bytes from stream into @param buf. Returns number 1427 * of bytes read or -1 on error, in which case errno is set. Possible 1428 * errno values: 1429 * 1430 * EBADF The stream is closed. 1431 * ECONNRESET The stream has been reset. 1432 * EWOULDBLOCK There is no data to be read. 1433 * 1434 * Return value of zero indicates EOF. 1435 */ 1436ssize_t 1437lsquic_stream_read (lsquic_stream_t *s, void *buf, size_t len); 1438 1439/** 1440 * Similar to @ref lsquic_stream_read(), but reads data into @param vec. 1441 */ 1442ssize_t 1443lsquic_stream_readv (lsquic_stream_t *s, const struct iovec *vec, int iovcnt); 1444 1445/** 1446 * This function allows user-supplied callback to read the stream contents. 1447 * It is meant to be used for zero-copy stream processing. 1448 * 1449 * Return value and errors are same as in @ref lsquic_stream_read(). 1450 */ 1451ssize_t 1452lsquic_stream_readf (lsquic_stream_t *s, 1453 /** 1454 * The callback takes four parameters: 1455 * - Pointer to user-supplied context; 1456 * - Pointer to the data; 1457 * - Data size (can be zero); and 1458 * - Indicator whether the FIN follows the data. 1459 * 1460 * The callback returns number of bytes processed. If this number is zero 1461 * or is smaller than `len', reading from stream stops. 1462 */ 1463 size_t (*readf)(void *ctx, const unsigned char *buf, size_t len, int fin), 1464 void *ctx); 1465 1466/** 1467 * Set whether you want to write to stream. If @param is_want is true, 1468 * @ref on_write() will be called when it is possible to write data to 1469 * the stream. If @param is false, @ref on_write() will not be called. 1470 * 1471 * Returns previous value of this flag. 1472 */ 1473int 1474lsquic_stream_wantwrite (lsquic_stream_t *s, int is_want); 1475 1476/** 1477 * Write `len' bytes to the stream. Returns number of bytes written, which 1478 * may be smaller that `len'. 1479 * 1480 * A negative return value indicates a serious error (the library is likely 1481 * to have aborted the connection because of it). 1482 */ 1483ssize_t 1484lsquic_stream_write (lsquic_stream_t *s, const void *buf, size_t len); 1485 1486/** 1487 * Like @ref lsquic_stream_write(), but read data from @param vec. 1488 */ 1489ssize_t 1490lsquic_stream_writev (lsquic_stream_t *s, const struct iovec *vec, int count); 1491 1492/** 1493 * Write to streams using a single call to a preadv-like function. 1494 */ 1495ssize_t 1496lsquic_stream_pwritev (lsquic_stream_t *s, 1497 ssize_t (*preadv)(void *user_data, const struct iovec *iov, int iovcnt), 1498 void *user_data, size_t n_to_write); 1499 1500/** 1501 * Used as argument to @ref lsquic_stream_writef() 1502 */ 1503struct lsquic_reader 1504{ 1505 /** 1506 * Not a ssize_t because the read function is not supposed to return 1507 * an error. If an error occurs in the read function (for example, when 1508 * reading from a file fails), it is supposed to deal with the error 1509 * itself. 1510 */ 1511 size_t (*lsqr_read) (void *lsqr_ctx, void *buf, size_t count); 1512 /** 1513 * Return number of bytes remaining in the reader. 1514 */ 1515 size_t (*lsqr_size) (void *lsqr_ctx); 1516 void *lsqr_ctx; 1517}; 1518 1519/** 1520 * Write to stream using @ref lsquic_reader. This is the most generic of 1521 * the write functions -- @ref lsquic_stream_write() and 1522 * @ref lsquic_stream_writev() utilize the same mechanism. 1523 * 1524 * @retval Number of bytes written or -1 on error. 1525 */ 1526ssize_t 1527lsquic_stream_writef (lsquic_stream_t *, struct lsquic_reader *); 1528 1529/** 1530 * Flush any buffered data. This triggers packetizing even a single byte 1531 * into a separate frame. Flushing a closed stream is an error. 1532 * 1533 * @retval 0 Success 1534 * @retval -1 Failure 1535 */ 1536int 1537lsquic_stream_flush (lsquic_stream_t *s); 1538 1539/** 1540 * @typedef lsquic_http_headers_t 1541 * @brief HTTP header list structure. Contains a list of HTTP headers in key/value pairs. 1542 * used in API functions to pass headers. 1543 */ 1544struct lsquic_http_headers 1545{ 1546 int count; 1547 struct lsxpack_header *headers; 1548}; 1549 1550/** 1551 * Send headers in @param headers. This function must be called before 1552 * writing to the stream. The value of @param eos is ignored in IETF QUIC. 1553 */ 1554int 1555lsquic_stream_send_headers (lsquic_stream_t *s, 1556 const lsquic_http_headers_t *headers, int eos); 1557 1558/** 1559 * Get header set associated with the stream. The header set is created by 1560 * @ref hsi_create_header_set() callback. After this call, the ownership of 1561 * the header set is transferred to the caller. 1562 * 1563 * This call must precede calls to @ref lsquic_stream_read() and 1564 * @ref lsquic_stream_readv(). 1565 * 1566 * If the optional header set interface (@ref ea_hsi_if) is not specified, 1567 * this function returns NULL. 1568 */ 1569void * 1570lsquic_stream_get_hset (lsquic_stream_t *); 1571 1572/** 1573 * A server may push a stream. This call creates a new stream in reference 1574 * to stream `s'. It will behave as if the client made a request: it will 1575 * trigger on_new_stream() event and it can be used as a regular client- 1576 * initiated stream. 1577 * 1578 * `hdr_set' must be set. It is passed as-is to @lsquic_stream_get_hset. 1579 * 1580 * @retval 0 Stream pushed successfully. 1581 * @retval 1 Stream push failed because it is disabled or because we hit 1582 * stream limit or connection is going away. 1583 * @retval -1 Stream push failed because of an internal error. 1584 */ 1585int 1586lsquic_conn_push_stream (lsquic_conn_t *c, void *hdr_set, lsquic_stream_t *s, 1587 const lsquic_http_headers_t *headers); 1588 1589/** 1590 * Only makes sense in server mode: the client cannot push a stream and this 1591 * function always returns false in client mode. 1592 */ 1593int 1594lsquic_conn_is_push_enabled (lsquic_conn_t *); 1595 1596/** Possible values for how are 0, 1, and 2. See shutdown(2). */ 1597int lsquic_stream_shutdown(lsquic_stream_t *s, int how); 1598 1599int lsquic_stream_close(lsquic_stream_t *s); 1600 1601/** 1602 * Get certificate chain returned by the server. This can be used for 1603 * server certificate verification. 1604 * 1605 * The caller releases the stack using sk_X509_free(). 1606 */ 1607struct stack_st_X509 * 1608lsquic_conn_get_server_cert_chain (lsquic_conn_t *); 1609 1610/** Returns ID of the stream */ 1611lsquic_stream_id_t 1612lsquic_stream_id (const lsquic_stream_t *s); 1613 1614/** 1615 * Returns stream ctx associated with the stream. (The context is what 1616 * is returned by @ref on_new_stream callback). 1617 */ 1618lsquic_stream_ctx_t * 1619lsquic_stream_get_ctx (const lsquic_stream_t *s); 1620 1621/** Returns true if this is a pushed stream */ 1622int 1623lsquic_stream_is_pushed (const lsquic_stream_t *s); 1624 1625/** 1626 * Returns true if this stream was rejected, false otherwise. Use this as 1627 * an aid to distinguish between errors. 1628 */ 1629int 1630lsquic_stream_is_rejected (const lsquic_stream_t *s); 1631 1632/** 1633 * Refuse pushed stream. Call it from @ref on_new_stream. 1634 * 1635 * No need to call lsquic_stream_close() after this. on_close will be called. 1636 * 1637 * @see lsquic_stream_is_pushed 1638 */ 1639int 1640lsquic_stream_refuse_push (lsquic_stream_t *s); 1641 1642/** 1643 * Get information associated with pushed stream: 1644 * 1645 * @param ref_stream_id Stream ID in response to which push promise was 1646 * sent. 1647 * @param hdr_set Header set. This object was passed to or generated 1648 * by @ref lsquic_conn_push_stream(). 1649 * 1650 * @retval 0 Success. 1651 * @retval -1 This is not a pushed stream. 1652 */ 1653int 1654lsquic_stream_push_info (const lsquic_stream_t *, 1655 lsquic_stream_id_t *ref_stream_id, void **hdr_set); 1656 1657/** Return current priority of the stream */ 1658unsigned lsquic_stream_priority (const lsquic_stream_t *s); 1659 1660/** 1661 * Set stream priority. Valid priority values are 1 through 256, inclusive. 1662 * Lower value means higher priority. 1663 * 1664 * @retval 0 Success. 1665 * @retval -1 Priority value is invalid. 1666 */ 1667int lsquic_stream_set_priority (lsquic_stream_t *s, unsigned priority); 1668 1669/* 1670 * Definitions for Extensible HTTP Priorities: 1671 * https://tools.ietf.org/html/draft-ietf-httpbis-priority-01 1672 */ 1673/* This is maximum *value* -- but it's the lowest *priority* */ 1674#define LSQUIC_MAX_HTTP_URGENCY 7 1675#define LSQUIC_DEF_HTTP_URGENCY 3 1676#define LSQUIC_DEF_HTTP_INCREMENTAL 0 1677 1678struct lsquic_ext_http_prio 1679{ 1680 unsigned char urgency; 1681 signed char incremental; 1682}; 1683 1684/** 1685 * Get Extensible HTTP Priorities associated with the stream. 1686 * 1687 * Returns zero on success of a negative value on failure. A failure occurs 1688 * if this is not an HTTP/3 stream or if Extensible HTTP Priorities haven't 1689 * been enabled. See @ref es_ext_http_prio. 1690 */ 1691int 1692lsquic_stream_get_http_prio (lsquic_stream_t *, struct lsquic_ext_http_prio *); 1693 1694/** 1695 * Set Extensible HTTP Priorities of the stream. 1696 * 1697 * Returns zero on success of a negative value on failure. A failure occurs 1698 * if some internal error occured or if this is not an HTTP/3 stream or if 1699 * Extensible HTTP Priorities haven't been enabled. See @ref es_ext_http_prio. 1700 */ 1701int 1702lsquic_stream_set_http_prio (lsquic_stream_t *, 1703 const struct lsquic_ext_http_prio *); 1704 1705/** 1706 * Get a pointer to the connection object. Use it with lsquic_conn_* 1707 * functions. 1708 */ 1709lsquic_conn_t * lsquic_stream_conn(const lsquic_stream_t *s); 1710 1711/** Get connection ID */ 1712const lsquic_cid_t * 1713lsquic_conn_id (const lsquic_conn_t *c); 1714 1715/** Get pointer to the engine */ 1716lsquic_engine_t * 1717lsquic_conn_get_engine (lsquic_conn_t *c); 1718 1719int 1720lsquic_conn_get_sockaddr(lsquic_conn_t *c, 1721 const struct sockaddr **local, const struct sockaddr **peer); 1722 1723/* Returns previous value */ 1724int 1725lsquic_conn_want_datagram_write (lsquic_conn_t *, int is_want); 1726 1727/* Get minimum datagram size. By default, this value is zero. */ 1728size_t 1729lsquic_conn_get_min_datagram_size (lsquic_conn_t *); 1730 1731/* Set minimum datagram size. This is the minumum value of the buffer passed 1732 * to the on_dg_write() callback. 1733 */ 1734int 1735lsquic_conn_set_min_datagram_size (lsquic_conn_t *, size_t sz); 1736 1737struct lsquic_logger_if { 1738 int (*log_buf)(void *logger_ctx, const char *buf, size_t len); 1739}; 1740 1741/** 1742 * Enumerate timestamp styles supported by LSQUIC logger mechanism. 1743 */ 1744enum lsquic_logger_timestamp_style { 1745 /** 1746 * No timestamp is generated. 1747 */ 1748 LLTS_NONE, 1749 1750 /** 1751 * The timestamp consists of 24 hours, minutes, seconds, and 1752 * milliseconds. Example: 13:43:46.671 1753 */ 1754 LLTS_HHMMSSMS, 1755 1756 /** 1757 * Like above, plus date, e.g: 2017-03-21 13:43:46.671 1758 */ 1759 LLTS_YYYYMMDD_HHMMSSMS, 1760 1761 /** 1762 * This is Chrome-like timestamp used by proto-quic. The timestamp 1763 * includes month, date, hours, minutes, seconds, and microseconds. 1764 * 1765 * Example: 1223/104613.946956 (instead of 12/23 10:46:13.946956). 1766 * 1767 * This is to facilitate reading two logs side-by-side. 1768 */ 1769 LLTS_CHROMELIKE, 1770 1771 /** 1772 * The timestamp consists of 24 hours, minutes, seconds, and 1773 * microseconds. Example: 13:43:46.671123 1774 */ 1775 LLTS_HHMMSSUS, 1776 1777 /** 1778 * Date and time using microsecond resolution, 1779 * e.g: 2017-03-21 13:43:46.671123 1780 */ 1781 LLTS_YYYYMMDD_HHMMSSUS, 1782 1783 N_LLTS 1784}; 1785 1786/** 1787 * Call this if you want to do something with LSQUIC log messages, as they 1788 * are thrown out by default. 1789 */ 1790void lsquic_logger_init(const struct lsquic_logger_if *, void *logger_ctx, 1791 enum lsquic_logger_timestamp_style); 1792 1793/** 1794 * Set log level for all LSQUIC modules. Acceptable values are debug, info, 1795 * notice, warning, error, alert, emerg, crit (case-insensitive). 1796 * 1797 * @retval 0 Success. 1798 * @retval -1 Failure: log_level is not valid. 1799 */ 1800int 1801lsquic_set_log_level (const char *log_level); 1802 1803/** 1804 * E.g. "event=debug" 1805 */ 1806int 1807lsquic_logger_lopt (const char *optarg); 1808 1809/** 1810 * Return the list of QUIC versions (as bitmask) this engine instance 1811 * supports. 1812 */ 1813unsigned lsquic_engine_quic_versions (const lsquic_engine_t *); 1814 1815/** 1816 * This is one of the flags that can be passed to @ref lsquic_global_init. 1817 * Use it to initialize LSQUIC for use in client mode. 1818 */ 1819#define LSQUIC_GLOBAL_CLIENT (1 << 0) 1820 1821/** 1822 * This is one of the flags that can be passed to @ref lsquic_global_init. 1823 * Use it to initialize LSQUIC for use in server mode. 1824 */ 1825#define LSQUIC_GLOBAL_SERVER (1 << 1) 1826 1827/** 1828 * Initialize LSQUIC. This must be called before any other LSQUIC function 1829 * is called. Returns 0 on success and -1 on failure. 1830 * 1831 * @param flags This a bitmask of @ref LSQUIC_GLOBAL_CLIENT and 1832 * @ref LSQUIC_GLOBAL_SERVER. At least one of these 1833 * flags should be specified. 1834 * 1835 * @retval 0 Success. 1836 * @retval -1 Initialization failed. 1837 * 1838 * @see LSQUIC_GLOBAL_CLIENT 1839 * @see LSQUIC_GLOBAL_SERVER 1840 */ 1841int 1842lsquic_global_init (int flags); 1843 1844/** 1845 * Clean up global state created by @ref lsquic_global_init. Should be 1846 * called after all LSQUIC engine instances are gone. 1847 */ 1848void 1849lsquic_global_cleanup (void); 1850 1851/** 1852 * Get QUIC version used by the connection. 1853 * 1854 * @see lsquic_version 1855 */ 1856enum lsquic_version 1857lsquic_conn_quic_version (const lsquic_conn_t *c); 1858 1859/* Return keysize or -1 on error */ 1860int 1861lsquic_conn_crypto_keysize (const lsquic_conn_t *c); 1862 1863/* Return algorithm keysize or -1 on error */ 1864int 1865lsquic_conn_crypto_alg_keysize (const lsquic_conn_t *c); 1866 1867enum lsquic_crypto_ver 1868{ 1869 LSQ_CRY_QUIC, 1870 LSQ_CRY_TLSv13, 1871}; 1872 1873enum lsquic_crypto_ver 1874lsquic_conn_crypto_ver (const lsquic_conn_t *c); 1875 1876/* Return cipher or NULL on error */ 1877const char * 1878lsquic_conn_crypto_cipher (const lsquic_conn_t *c); 1879 1880/** Translate string QUIC version to LSQUIC QUIC version representation */ 1881enum lsquic_version 1882lsquic_str2ver (const char *str, size_t len); 1883 1884/** Translate ALPN (e.g. "h3", "h3-23", "h3-Q046") to LSQUIC enum */ 1885enum lsquic_version 1886lsquic_alpn2ver (const char *alpn, size_t len); 1887 1888/** 1889 * This function closes all mini connections and marks all full connections 1890 * as going away. In server mode, this also causes the engine to stop 1891 * creating new connections. 1892 */ 1893void 1894lsquic_engine_cooldown (lsquic_engine_t *); 1895 1896/** 1897 * Get user-supplied context associated with the connection. 1898 */ 1899lsquic_conn_ctx_t * 1900lsquic_conn_get_ctx (const lsquic_conn_t *); 1901 1902/** 1903 * Set user-supplied context associated with the connection. 1904 */ 1905void 1906lsquic_conn_set_ctx (lsquic_conn_t *, lsquic_conn_ctx_t *); 1907 1908/** 1909 * Get peer context associated with the connection. 1910 */ 1911void * 1912lsquic_conn_get_peer_ctx (lsquic_conn_t *, const struct sockaddr *local_sa); 1913 1914/** 1915 * Abort connection. 1916 */ 1917void 1918lsquic_conn_abort (lsquic_conn_t *); 1919 1920/** 1921 * Helper function: convert list of versions as specified in the argument 1922 * bitmask to string that can be included as argument to "v=" part of the 1923 * Alt-Svc header. 1924 * 1925 * For example (1<<LSQVER_037)|(1<<LSQVER_038) => "37,38" 1926 * 1927 * This is only applicable to Google QUIC versions. 1928 */ 1929const char * 1930lsquic_get_alt_svc_versions (unsigned versions); 1931 1932/** 1933 * Return a NULL-terminated list of HTTP/3 ALPNs, e.g "h3-17", "h3-18", "h3". 1934 */ 1935const char *const * 1936lsquic_get_h3_alpns (unsigned versions); 1937 1938/** 1939 * Returns true if provided buffer could be a valid handshake-stage packet, 1940 * false otherwise. Do not call this function if a connection has already 1941 * been established: it will return incorrect result. 1942 */ 1943int 1944lsquic_is_valid_hs_packet (lsquic_engine_t *, const unsigned char *, size_t); 1945 1946/** 1947 * Parse cid from packet stored in `buf' and store it to `cid'. Returns 0 1948 * on success and -1 on failure. 1949 */ 1950int 1951lsquic_cid_from_packet (const unsigned char *, size_t bufsz, lsquic_cid_t *cid); 1952 1953/** 1954 * Returns true if there are connections to be processed, false otherwise. 1955 * If true, `diff' is set to the difference between the earliest advisory 1956 * tick time and now. If the former is in the past, the value of `diff' 1957 * is negative. 1958 */ 1959int 1960lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff); 1961 1962/** 1963 * Return number of connections whose advisory tick time is before current 1964 * time plus `from_now' microseconds from now. `from_now' can be negative. 1965 */ 1966unsigned 1967lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now); 1968 1969enum LSQUIC_CONN_STATUS 1970{ 1971 LSCONN_ST_HSK_IN_PROGRESS, 1972 LSCONN_ST_CONNECTED, 1973 LSCONN_ST_HSK_FAILURE, 1974 LSCONN_ST_GOING_AWAY, 1975 LSCONN_ST_TIMED_OUT, 1976 /* If es_honor_prst is not set, the connection will never get public 1977 * reset packets and this flag will not be set. 1978 */ 1979 LSCONN_ST_RESET, 1980 LSCONN_ST_USER_ABORTED, 1981 LSCONN_ST_ERROR, 1982 LSCONN_ST_CLOSED, 1983 LSCONN_ST_PEER_GOING_AWAY, 1984}; 1985 1986enum LSQUIC_CONN_STATUS 1987lsquic_conn_status (lsquic_conn_t *, char *errbuf, size_t bufsz); 1988 1989extern const char *const 1990lsquic_ver2str[N_LSQVER]; 1991 1992#ifdef __cplusplus 1993} 1994#endif 1995 1996#endif //__LSQUIC_H__ 1997 1998