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