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