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