lsquic.h revision 8ca33e0e
1/* Copyright (c) 2017 - 2019 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 1 27#define LSQUIC_MINOR_VERSION 19 28#define LSQUIC_PATCH_VERSION 0 29 30/** 31 * Engine flags: 32 */ 33 34/** Server mode */ 35#define LSENG_SERVER (1 << 0) 36 37/** Treat stream 3 as headers stream and, in general, behave like the 38 * regular QUIC. 39 */ 40#define LSENG_HTTP (1 << 1) 41 42#define LSENG_HTTP_SERVER (LSENG_SERVER|LSENG_HTTP) 43 44/** 45 * This is a list of QUIC versions that we know of. List of supported 46 * versions is in LSQUIC_SUPPORTED_VERSIONS. 47 */ 48enum lsquic_version 49{ 50 51 /** Q035. This is the first version to be supported by LSQUIC. */ 52 LSQVER_035, 53 54 /* 55 * Q037. This version is like Q035, except the way packet hashes are 56 * generated is different for clients and servers. In addition, new 57 * option NSTP (no STOP_WAITING frames) is rumored to be supported at 58 * some point in the future. 59 */ 60 /* Support for this version has been removed. The comment remains to 61 * document the changes. 62 */ 63 64 /* 65 * Q038. Based on Q037, supports PADDING frames in the middle of packet 66 * and NSTP (no STOP_WAITING frames) option. 67 */ 68 /* Support for this version has been removed. The comment remains to 69 * document the changes. 70 */ 71 72 /** 73 * Q039. Switch to big endian. Do not ack acks. Send connection level 74 * WINDOW_UPDATE frame every 20 sent packets which do not contain 75 * retransmittable frames. 76 */ 77 LSQVER_039, 78 79 /* 80 * Q041. RST_STREAM, ACK and STREAM frames match IETF format. 81 */ 82 /* Support for this version has been removed. The comment remains to 83 * document the changes. 84 */ 85 86 /* 87 * Q042. Receiving overlapping stream data is allowed. 88 */ 89 /* Support for this version has been removed. The comment remains to 90 * document the changes. 91 */ 92 93 /** 94 * Q043. Support for processing PRIORITY frames. Since this library 95 * has supported PRIORITY frames from the beginning, this version is 96 * exactly the same as LSQVER_042. 97 */ 98 LSQVER_043, 99 100 /** 101 * Q044. IETF-like packet headers are used. Frames are the same as 102 * in Q043. Server never includes CIDs in short packets. 103 */ 104 LSQVER_044, 105 106#if LSQUIC_USE_Q098 107 /** 108 * Q098. This is a made-up, experimental version used to test version 109 * negotiation. The choice of 98 is similar to Google's choice of 99 110 * as the "IETF" version. 111 */ 112 LSQVER_098, 113#define LSQUIC_EXPERIMENTAL_Q098 (1 << LSQVER_098) 114#else 115#define LSQUIC_EXPERIMENTAL_Q098 0 116#endif 117 118 N_LSQVER 119}; 120 121/** 122 * We currently support versions 35, 39, 43, and 44. 123 * @see lsquic_version 124 */ 125#define LSQUIC_SUPPORTED_VERSIONS ((1 << N_LSQVER) - 1) 126 127#define LSQUIC_EXPERIMENTAL_VERSIONS (0 \ 128 | LSQUIC_EXPERIMENTAL_Q098) 129 130#define LSQUIC_DEPRECATED_VERSIONS 0 131 132#define LSQUIC_GQUIC_HEADER_VERSIONS ( \ 133 (1 << LSQVER_035) | (1 << LSQVER_039) | (1 << LSQVER_043)) 134 135/** 136 * List of versions in which the server never includes CID in short packets. 137 */ 138#define LSQUIC_FORCED_TCID0_VERSIONS (1 << LSQVER_044) 139 140enum lsquic_hsk_status 141{ 142 /** 143 * The handshake failed. 144 */ 145 LSQ_HSK_FAIL, 146 /** 147 * The handshake succeeded without 0-RTT. 148 */ 149 LSQ_HSK_OK, 150 /** 151 * The handshake succeeded with 0-RTT. 152 */ 153 LSQ_HSK_0RTT_OK, 154}; 155 156/** 157 * @struct lsquic_stream_if 158 * @brief The definition of callback functions call by lsquic_stream to 159 * process events. 160 * 161 */ 162struct lsquic_stream_if { 163 164 /** 165 * Use @ref lsquic_conn_get_ctx to get back the context. It is 166 * OK for this function to return NULL. 167 */ 168 lsquic_conn_ctx_t *(*on_new_conn)(void *stream_if_ctx, 169 lsquic_conn_t *c); 170 171 /** This is called when our side received GOAWAY frame. After this, 172 * new streams should not be created. The callback is optional. 173 */ 174 void (*on_goaway_received)(lsquic_conn_t *c); 175 void (*on_conn_closed)(lsquic_conn_t *c); 176 177 /** If you need to initiate a connection, call lsquic_conn_make_stream(). 178 * This will cause `on_new_stream' callback to be called when appropriate 179 * (this operation is delayed when maximum number of outgoing streams is 180 * reached). 181 * 182 * After `on_close' is called, the stream is no longer accessible. 183 */ 184 lsquic_stream_ctx_t * 185 (*on_new_stream)(void *stream_if_ctx, lsquic_stream_t *s); 186 187 void (*on_read) (lsquic_stream_t *s, lsquic_stream_ctx_t *h); 188 void (*on_write) (lsquic_stream_t *s, lsquic_stream_ctx_t *h); 189 void (*on_close) (lsquic_stream_t *s, lsquic_stream_ctx_t *h); 190 /** 191 * When handshake is completed, this callback is called. `ok' is set 192 * to true if handshake was successful; otherwise, `ok' is set to 193 * false. 194 * 195 * This callback is optional. 196 */ 197 void (*on_hsk_done)(lsquic_conn_t *c, enum lsquic_hsk_status s); 198}; 199 200/** 201 * Minimum flow control window is set to 16 KB for both client and server. 202 * This means we can send up to this amount of data before handshake gets 203 * completed. 204 */ 205#define LSQUIC_MIN_FCW (16 * 1024) 206 207/* Each LSQUIC_DF_* value corresponds to es_* entry in 208 * lsquic_engine_settings below. 209 */ 210 211/** 212 * By default, deprecated and experimental versions are not included. 213 */ 214#define LSQUIC_DF_VERSIONS (LSQUIC_SUPPORTED_VERSIONS & \ 215 ~LSQUIC_DEPRECATED_VERSIONS & \ 216 ~LSQUIC_EXPERIMENTAL_VERSIONS) 217 218#define LSQUIC_DF_CFCW_SERVER (3 * 1024 * 1024 / 2) 219#define LSQUIC_DF_CFCW_CLIENT (15 * 1024 * 1024) 220#define LSQUIC_DF_SFCW_SERVER (1 * 1024 * 1024) 221#define LSQUIC_DF_SFCW_CLIENT (6 * 1024 * 1024) 222#define LSQUIC_DF_MAX_STREAMS_IN 100 223 224/** 225 * Default handshake timeout in microseconds. 226 */ 227#define LSQUIC_DF_HANDSHAKE_TO (10 * 1000 * 1000) 228 229#define LSQUIC_DF_IDLE_CONN_TO (30 * 1000 * 1000) 230#define LSQUIC_DF_SILENT_CLOSE 1 231 232/** Default value of maximum header list size. If set to non-zero value, 233 * SETTINGS_MAX_HEADER_LIST_SIZE will be sent to peer after handshake is 234 * completed (assuming the peer supports this setting frame type). 235 */ 236#define LSQUIC_DF_MAX_HEADER_LIST_SIZE 0 237 238/** Default value of UAID (user-agent ID). */ 239#define LSQUIC_DF_UA "LSQUIC" 240 241#define LSQUIC_DF_STTL 86400 242#define LSQUIC_DF_MAX_INCHOATE (1 * 1000 * 1000) 243#define LSQUIC_DF_SUPPORT_SREJ_SERVER 1 244#define LSQUIC_DF_SUPPORT_SREJ_CLIENT 0 /* TODO: client support */ 245/** Do not use NSTP by default */ 246#define LSQUIC_DF_SUPPORT_NSTP 0 247#define LSQUIC_DF_SUPPORT_PUSH 1 248#define LSQUIC_DF_SUPPORT_TCID0 0 249/** By default, LSQUIC ignores Public Reset packets. */ 250#define LSQUIC_DF_HONOR_PRST 0 251 252/** By default, infinite loop checks are turned on */ 253#define LSQUIC_DF_PROGRESS_CHECK 1000 254 255/** By default, read/write events are dispatched in a loop */ 256#define LSQUIC_DF_RW_ONCE 0 257 258/** By default, the threshold is not enabled */ 259#define LSQUIC_DF_PROC_TIME_THRESH 0 260 261/** By default, packets are paced */ 262#define LSQUIC_DF_PACE_PACKETS 1 263 264/** Default clock granularity is 1000 microseconds */ 265#define LSQUIC_DF_CLOCK_GRANULARITY 1000 266 267struct lsquic_engine_settings { 268 /** 269 * This is a bit mask wherein each bit corresponds to a value in 270 * enum lsquic_version. Client starts negotiating with the highest 271 * version and goes down. Server supports either of the versions 272 * specified here. 273 * 274 * @see lsquic_version 275 */ 276 unsigned es_versions; 277 278 /** 279 * Initial default CFCW. 280 * 281 * In server mode, per-connection values may be set lower than 282 * this if resources are scarce. 283 * 284 * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW. 285 * 286 * @see es_max_cfcw 287 */ 288 unsigned es_cfcw; 289 290 /** 291 * Initial default SFCW. 292 * 293 * In server mode, per-connection values may be set lower than 294 * this if resources are scarce. 295 * 296 * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW. 297 * 298 * @see es_max_sfcw 299 */ 300 unsigned es_sfcw; 301 302 /** 303 * This value is used to specify maximum allowed value CFCW is allowed 304 * to reach due to window auto-tuning. By default, this value is zero, 305 * which means that CFCW is not allowed to increase from its initial 306 * value. 307 * 308 * @see es_cfcw 309 */ 310 unsigned es_max_cfcw; 311 312 unsigned es_max_sfcw; 313 314 /** MIDS */ 315 unsigned es_max_streams_in; 316 317 /** 318 * Handshake timeout in microseconds. 319 * 320 * For client, this can be set to an arbitrary value (zero turns the 321 * timeout off). 322 * 323 */ 324 unsigned long es_handshake_to; 325 326 /** ICSL in microseconds */ 327 unsigned long es_idle_conn_to; 328 329 /** SCLS (silent close) */ 330 int es_silent_close; 331 332 /** 333 * This corresponds to SETTINGS_MAX_HEADER_LIST_SIZE 334 * (RFC 7540, Section 6.5.2). 0 means no limit. Defaults 335 * to @ref LSQUIC_DF_MAX_HEADER_LIST_SIZE. 336 */ 337 unsigned es_max_header_list_size; 338 339 /** UAID -- User-Agent ID. Defaults to @ref LSQUIC_DF_UA. */ 340 const char *es_ua; 341 342 uint32_t es_pdmd; /* One fixed value X509 */ 343 uint32_t es_aead; /* One fixed value AESG */ 344 uint32_t es_kexs; /* One fixed value C255 */ 345 346 /** 347 * Support SREJ: for client side, this means supporting server's SREJ 348 * responses (this does not work yet) and for server side, this means 349 * generating SREJ instead of REJ when appropriate. 350 */ 351 int es_support_srej; 352 353 /** 354 * Setting this value to 0 means that 355 * 356 * For client: 357 * a) we send a SETTINGS frame to indicate that we do not support server 358 * push; and 359 * b) All incoming pushed streams get reset immediately. 360 * (For maximum effect, set es_max_streams_in to 0.) 361 * 362 */ 363 int es_support_push; 364 365 /** 366 * If set to true value, the server will not include connection ID in 367 * outgoing packets if client's CHLO specifies TCID=0. 368 * 369 * For client, this means including TCID=0 into CHLO message. Note that 370 * in this case, the engine tracks connections by the 371 * (source-addr, dest-addr) tuple, thereby making it necessary to create 372 * a socket for each connection. 373 * 374 * This option has no effect in Q044, as the server never includes CIDs 375 * in the short packets. 376 * 377 * The default is @ref LSQUIC_DF_SUPPORT_TCID0. 378 */ 379 int es_support_tcid0; 380 381 /** 382 * Q037 and higher support "No STOP_WAITING frame" mode. When set, the 383 * client will send NSTP option in its Client Hello message and will not 384 * sent STOP_WAITING frames, while ignoring incoming STOP_WAITING frames, 385 * if any. Note that if the version negotiation happens to downgrade the 386 * client below Q037, this mode will *not* be used. 387 * 388 * This option does not affect the server, as it must support NSTP mode 389 * if it was specified by the client. 390 */ 391 int es_support_nstp; 392 393 /** 394 * If set to true value, the library will drop connections when it 395 * receives corresponding Public Reset packet. The default is to 396 * ignore these packets. 397 */ 398 int es_honor_prst; 399 400 /** 401 * A non-zero value enables internal checks that identify suspected 402 * infinite loops in user @ref on_read and @ref on_write callbacks 403 * and break them. An infinite loop may occur if user code keeps 404 * on performing the same operation without checking status, e.g. 405 * reading from a closed stream etc. 406 * 407 * The value of this parameter is as follows: should a callback return 408 * this number of times in a row without making progress (that is, 409 * reading, writing, or changing stream state), loop break will occur. 410 * 411 * The defaut value is @ref LSQUIC_DF_PROGRESS_CHECK. 412 */ 413 unsigned es_progress_check; 414 415 /** 416 * A non-zero value make stream dispatch its read-write events once 417 * per call. 418 * 419 * When zero, read and write events are dispatched until the stream 420 * is no longer readable or writeable, respectively, or until the 421 * user signals unwillingness to read or write using 422 * @ref lsquic_stream_wantread() or @ref lsquic_stream_wantwrite() 423 * or shuts down the stream. 424 * 425 * The default value is @ref LSQUIC_DF_RW_ONCE. 426 */ 427 int es_rw_once; 428 429 /** 430 * If set, this value specifies that number of microseconds that 431 * @ref lsquic_engine_process_conns() and 432 * @ref lsquic_engine_send_unsent_packets() are allowed to spend 433 * before returning. 434 * 435 * This is not an exact science and the connections must make 436 * progress, so the deadline is checked after all connections get 437 * a chance to tick (in the case of @ref lsquic_engine_process_conns()) 438 * and at least one batch of packets is sent out. 439 * 440 * When processing function runs out of its time slice, immediate 441 * calls to @ref lsquic_engine_has_unsent_packets() return false. 442 * 443 * The default value is @ref LSQUIC_DF_PROC_TIME_THRESH. 444 */ 445 unsigned es_proc_time_thresh; 446 447 /** 448 * If set to true, packet pacing is implemented per connection. 449 * 450 * The default value is @ref LSQUIC_DF_PACE_PACKETS. 451 */ 452 int es_pace_packets; 453 454 /** 455 * Clock granularity information is used by the pacer. The value 456 * is in microseconds; default is @ref LSQUIC_DF_CLOCK_GRANULARITY. 457 */ 458 unsigned es_clock_granularity; 459}; 460 461/* Initialize `settings' to default values */ 462void 463lsquic_engine_init_settings (struct lsquic_engine_settings *, 464 unsigned lsquic_engine_flags); 465 466/** 467 * Check settings for errors. 468 * 469 * @param settings Settings struct. 470 * 471 * @param flags Engine flags. 472 * 473 * @param err_buf Optional pointer to buffer into which error string 474 * is written. 475 476 * @param err_buf_sz Size of err_buf. No more than this number of bytes 477 * will be written to err_buf, including the NUL byte. 478 * 479 * @retval 0 Settings have no errors. 480 * @retval -1 There are errors in settings. 481 */ 482int 483lsquic_engine_check_settings (const struct lsquic_engine_settings *settings, 484 unsigned lsquic_engine_flags, 485 char *err_buf, size_t err_buf_sz); 486 487struct lsquic_out_spec 488{ 489 const unsigned char *buf; 490 size_t sz; 491 const struct sockaddr *local_sa; 492 const struct sockaddr *dest_sa; 493 void *peer_ctx; 494}; 495 496/** 497 * Returns number of packets successfully sent out or -1 on error. -1 should 498 * only be returned if no packets were sent out. If -1 is returned or if the 499 * return value is smaller than `n_packets_out', this indicates that sending 500 * of packets is not possible No packets will be attempted to be sent out 501 * until @ref lsquic_engine_send_unsent_packets() is called. 502 */ 503typedef int (*lsquic_packets_out_f)( 504 void *packets_out_ctx, 505 const struct lsquic_out_spec *out_spec, 506 unsigned n_packets_out 507); 508 509/** 510 * The packet out memory interface is used by LSQUIC to get buffers to 511 * which outgoing packets will be written before they are passed to 512 * ea_packets_out callback. 513 * 514 * If not specified, malloc() and free() are used. 515 */ 516struct lsquic_packout_mem_if 517{ 518 /** 519 * Allocate buffer for sending. 520 */ 521 void * (*pmi_allocate) (void *pmi_ctx, void *conn_ctx, unsigned short sz, 522 char is_ipv6); 523 /** 524 * This function is used to release the allocated buffer after it is 525 * sent via @ref ea_packets_out. 526 */ 527 void (*pmi_release) (void *pmi_ctx, void *conn_ctx, void *buf, 528 char is_ipv6); 529 /** 530 * If allocated buffer is not going to be sent, return it to the caller 531 * using this function. 532 */ 533 void (*pmi_return) (void *pmi_ctx, void *conn_ctx, void *buf, 534 char is_ipv6); 535}; 536 537struct stack_st_X509; 538 539/** 540 * When headers are processed, various errors may occur. They are listed 541 * in this enum. 542 */ 543enum lsquic_header_status 544{ 545 LSQUIC_HDR_OK, 546 /** Duplicate pseudo-header */ 547 LSQUIC_HDR_ERR_DUPLICATE_PSDO_HDR, 548 /** Not all request pseudo-headers are present */ 549 LSQUIC_HDR_ERR_INCOMPL_REQ_PSDO_HDR, 550 /** Unnecessary request pseudo-header present in the response */ 551 LSQUIC_HDR_ERR_UNNEC_REQ_PSDO_HDR, 552 LSQUIC_HDR_ERR_BAD_REQ_HEADER = LSQUIC_HDR_ERR_UNNEC_REQ_PSDO_HDR, 553 /** Not all response pseudo-headers are present */ 554 LSQUIC_HDR_ERR_INCOMPL_RESP_PSDO_HDR, 555 /** Unnecessary response pseudo-header present in the response. */ 556 LSQUIC_HDR_ERR_UNNEC_RESP_PSDO_HDR, 557 /** Unknown pseudo-header */ 558 LSQUIC_HDR_ERR_UNKNOWN_PSDO_HDR, 559 /** Uppercase letter in header */ 560 LSQUIC_HDR_ERR_UPPERCASE_HEADER, 561 /** Misplaced pseudo-header */ 562 LSQUIC_HDR_ERR_MISPLACED_PSDO_HDR, 563 /** Missing pseudo-header */ 564 LSQUIC_HDR_ERR_MISSING_PSDO_HDR, 565 /** Header or headers are too large */ 566 LSQUIC_HDR_ERR_HEADERS_TOO_LARGE, 567 /** Cannot allocate any more memory. */ 568 LSQUIC_HDR_ERR_NOMEM, 569}; 570 571struct lsquic_hset_if 572{ 573 /** 574 * Create a new header set. This object is (and must be) fetched from a 575 * stream by calling @ref lsquic_stream_get_hset() before the stream can 576 * be read. 577 */ 578 void * (*hsi_create_header_set)(void *hsi_ctx, 579 int is_push_promise); 580 /** 581 * Process new header. Return 0 on success, -1 if there is a problem with 582 * the header. -1 is treated as a stream error: the associated stream is 583 * reset. 584 * 585 * `hdr_set' is the header set object returned by 586 * @ref hsi_create_header_set(). 587 * 588 * `name_idx' is set to the index in the HPACK static table whose entry's 589 * name element matches `name'. If there is no such match, `name_idx' is 590 * set to zero. 591 * 592 * If `name' is NULL, this means that no more header are going to be 593 * added to the set. 594 */ 595 enum lsquic_header_status (*hsi_process_header)(void *hdr_set, 596 unsigned name_idx, 597 const char *name, unsigned name_len, 598 const char *value, unsigned value_len); 599 /** 600 * Discard header set. This is called for unclaimed header sets and 601 * header sets that had an error. 602 */ 603 void (*hsi_discard_header_set)(void *hdr_set); 604}; 605 606/* TODO: describe this important data structure */ 607typedef struct lsquic_engine_api 608{ 609 const struct lsquic_engine_settings *ea_settings; /* Optional */ 610 const struct lsquic_stream_if *ea_stream_if; 611 void *ea_stream_if_ctx; 612 lsquic_packets_out_f ea_packets_out; 613 void *ea_packets_out_ctx; 614 /** 615 * Memory interface is optional. 616 */ 617 const struct lsquic_packout_mem_if *ea_pmi; 618 void *ea_pmi_ctx; 619 /** 620 * Function to verify server certificate. The chain contains at least 621 * one element. The first element in the chain is the server 622 * certificate. The chain belongs to the library. If you want to 623 * retain it, call sk_X509_up_ref(). 624 * 625 * 0 is returned on success, -1 on error. 626 * 627 * If the function pointer is not set, no verification is performed 628 * (the connection is allowed to proceed). 629 */ 630 int (*ea_verify_cert)(void *verify_ctx, 631 struct stack_st_X509 *chain); 632 void *ea_verify_ctx; 633 634 /** 635 * Optional header set interface. If not specified, the incoming headers 636 * are converted to HTTP/1.x format and are read from stream and have to 637 * be parsed again. 638 */ 639 const struct lsquic_hset_if *ea_hsi_if; 640 void *ea_hsi_ctx; 641#if LSQUIC_CONN_STATS 642 /** 643 * If set, engine will print cumulative connection statistics to this 644 * file just before it is destroyed. 645 */ 646 void /* FILE, really */ *ea_stats_fh; 647#endif 648} lsquic_engine_api_t; 649 650/** 651 * Create new engine. 652 * 653 * @param lsquic_engine_flags A bitmask of @ref LSENG_SERVER and 654 * @ref LSENG_HTTP 655 */ 656lsquic_engine_t * 657lsquic_engine_new (unsigned lsquic_engine_flags, 658 const struct lsquic_engine_api *); 659 660/** 661 * Create a client connection to peer identified by `peer_ctx'. 662 * If `max_packet_size' is set to zero, it is inferred based on `peer_sa': 663 * 1350 for IPv6 and 1370 for IPv4. 664 */ 665lsquic_conn_t * 666lsquic_engine_connect (lsquic_engine_t *, const struct sockaddr *local_sa, 667 const struct sockaddr *peer_sa, 668 void *peer_ctx, lsquic_conn_ctx_t *conn_ctx, 669 const char *hostname, unsigned short max_packet_size, 670 const unsigned char *zero_rtt, size_t zero_rtt_len); 671 672/** 673 * Pass incoming packet to the QUIC engine. This function can be called 674 * more than once in a row. After you add one or more packets, call 675 * lsquic_engine_process_conns() to schedule output, if any. 676 * 677 * @retval 0 Packet was processed by a real connection. 678 * 679 * @retval -1 Some error occurred. Possible reasons are invalid packet 680 * size or failure to allocate memory. 681 */ 682int 683lsquic_engine_packet_in (lsquic_engine_t *, 684 const unsigned char *packet_in_data, size_t packet_in_size, 685 const struct sockaddr *sa_local, const struct sockaddr *sa_peer, 686 void *peer_ctx); 687 688/** 689 * Process tickable connections. This function must be called often enough so 690 * that packets and connections do not expire. 691 */ 692void 693lsquic_engine_process_conns (lsquic_engine_t *engine); 694 695/** 696 * Returns true if engine has some unsent packets. This happens if 697 * @ref ea_packets_out() could not send everything out. 698 */ 699int 700lsquic_engine_has_unsent_packets (lsquic_engine_t *engine); 701 702/** 703 * Send out as many unsent packets as possibe: until we are out of unsent 704 * packets or until @ref ea_packets_out() fails. 705 * 706 * If @ref ea_packets_out() does fail (that is, it returns an error), this 707 * function must be called to signify that sending of packets is possible 708 * again. 709 */ 710void 711lsquic_engine_send_unsent_packets (lsquic_engine_t *engine); 712 713void 714lsquic_engine_destroy (lsquic_engine_t *); 715 716/** Return max allowed outbound streams less current outbound streams. */ 717unsigned 718lsquic_conn_n_avail_streams (const lsquic_conn_t *); 719 720void lsquic_conn_make_stream(lsquic_conn_t *); 721 722/** Return number of delayed streams currently pending */ 723unsigned 724lsquic_conn_n_pending_streams (const lsquic_conn_t *); 725 726/** Cancel `n' pending streams. Returns new number of pending streams. */ 727unsigned 728lsquic_conn_cancel_pending_streams (lsquic_conn_t *, unsigned n); 729 730/** 731 * Mark connection as going away: send GOAWAY frame and do not accept 732 * any more incoming streams, nor generate streams of our own. 733 */ 734void 735lsquic_conn_going_away(lsquic_conn_t *conn); 736 737/** 738 * This forces connection close. on_conn_closed and on_close callbacks 739 * will be called. 740 */ 741void lsquic_conn_close(lsquic_conn_t *conn); 742 743int lsquic_stream_wantread(lsquic_stream_t *s, int is_want); 744ssize_t lsquic_stream_read(lsquic_stream_t *s, void *buf, size_t len); 745ssize_t lsquic_stream_readv(lsquic_stream_t *s, const struct iovec *, 746 int iovcnt); 747 748int lsquic_stream_wantwrite(lsquic_stream_t *s, int is_want); 749 750/** 751 * Write `len' bytes to the stream. Returns number of bytes written, which 752 * may be smaller that `len'. 753 */ 754ssize_t lsquic_stream_write(lsquic_stream_t *s, const void *buf, size_t len); 755 756ssize_t lsquic_stream_writev(lsquic_stream_t *s, const struct iovec *vec, int count); 757 758/** 759 * Used as argument to @ref lsquic_stream_writef() 760 */ 761struct lsquic_reader 762{ 763 /** 764 * Not a ssize_t because the read function is not supposed to return 765 * an error. If an error occurs in the read function (for example, when 766 * reading from a file fails), it is supposed to deal with the error 767 * itself. 768 */ 769 size_t (*lsqr_read) (void *lsqr_ctx, void *buf, size_t count); 770 /** 771 * Return number of bytes remaining in the reader. 772 */ 773 size_t (*lsqr_size) (void *lsqr_ctx); 774 void *lsqr_ctx; 775}; 776 777/** 778 * Write to stream using @ref lsquic_reader. This is the most generic of 779 * the write functions -- @ref lsquic_stream_write() and 780 * @ref lsquic_stream_writev() utilize the same mechanism. 781 * 782 * @retval Number of bytes written or -1 on error. 783 */ 784ssize_t 785lsquic_stream_writef (lsquic_stream_t *, struct lsquic_reader *); 786 787/** 788 * Flush any buffered data. This triggers packetizing even a single byte 789 * into a separate frame. Flushing a closed stream is an error. 790 * 791 * @retval 0 Success 792 * @retval -1 Failure 793 */ 794int 795lsquic_stream_flush (lsquic_stream_t *s); 796 797/** 798 * @typedef lsquic_http_header_t 799 * @brief HTTP header structure. Contains header name and value. 800 * 801 */ 802typedef struct lsquic_http_header 803{ 804 struct iovec name; 805 struct iovec value; 806} lsquic_http_header_t; 807 808/** 809 * @typedef lsquic_http_headers_t 810 * @brief HTTP header list structure. Contains a list of HTTP headers in key/value pairs. 811 * used in API functions to pass headers. 812 */ 813struct lsquic_http_headers 814{ 815 int count; 816 lsquic_http_header_t *headers; 817}; 818 819int lsquic_stream_send_headers(lsquic_stream_t *s, 820 const lsquic_http_headers_t *h, int eos); 821 822/** 823 * Get header set associated with the stream. The header set is created by 824 * @ref hsi_create_header_set() callback. After this call, the ownership of 825 * the header set is trasnferred to the caller. 826 * 827 * This call must precede calls to @ref lsquic_stream_read() and 828 * @ref lsquic_stream_readv(). 829 * 830 * If the optional header set interface (@ref ea_hsi_if) is not specified, 831 * this function returns NULL. 832 */ 833void * 834lsquic_stream_get_hset (lsquic_stream_t *); 835 836int lsquic_conn_is_push_enabled(lsquic_conn_t *c); 837 838/** Possible values for how are 0, 1, and 2. See shutdown(2). */ 839int lsquic_stream_shutdown(lsquic_stream_t *s, int how); 840 841int lsquic_stream_close(lsquic_stream_t *s); 842 843/** 844 * Get certificate chain returned by the server. This can be used for 845 * server certificate verifiction. 846 * 847 * If server certificate cannot be verified, the connection can be closed 848 * using lsquic_conn_cert_verification_failed(). 849 * 850 * The caller releases the stack using sk_X509_free(). 851 */ 852struct stack_st_X509 * 853lsquic_conn_get_server_cert_chain (lsquic_conn_t *); 854 855/** 856 * Get server config zero_rtt from the encryption session. 857 * Returns the number of bytes written to the zero_rtt. 858 */ 859ssize_t 860lsquic_conn_get_zero_rtt(const lsquic_conn_t *, 861 unsigned char *zero_rtt, size_t zero_rtt_len); 862 863/** Returns ID of the stream */ 864uint32_t 865lsquic_stream_id (const lsquic_stream_t *s); 866 867/** 868 * Returns stream ctx associated with the stream. (The context is what 869 * is returned by @ref on_new_stream callback). 870 */ 871lsquic_stream_ctx_t * 872lsquic_stream_get_ctx (const lsquic_stream_t *s); 873 874/** Returns true if this is a pushed stream */ 875int 876lsquic_stream_is_pushed (const lsquic_stream_t *s); 877 878/** 879 * Refuse pushed stream. Call it from @ref on_new_stream. 880 * 881 * No need to call lsquic_stream_close() after this. on_close will be called. 882 * 883 * @see lsquic_stream_is_pushed 884 */ 885int 886lsquic_stream_refuse_push (lsquic_stream_t *s); 887 888/** 889 * Get information associated with pushed stream: 890 * 891 * @param ref_stream_id Stream ID in response to which push promise was 892 * sent. 893 * @param hdr_set Header set. This object was passed to or generated 894 * by @ref lsquic_conn_push_stream(). 895 * 896 * @retval 0 Success. 897 * @retval -1 This is not a pushed stream. 898 */ 899int 900lsquic_stream_push_info (const lsquic_stream_t *, uint32_t *ref_stream_id, 901 void **hdr_set); 902 903/** Return current priority of the stream */ 904unsigned lsquic_stream_priority (const lsquic_stream_t *s); 905 906/** 907 * Set stream priority. Valid priority values are 1 through 256, inclusive. 908 * 909 * @retval 0 Success. 910 * @retval -1 Priority value is invalid. 911 */ 912int lsquic_stream_set_priority (lsquic_stream_t *s, unsigned priority); 913 914/** 915 * Get a pointer to the connection object. Use it with lsquic_conn_* 916 * functions. 917 */ 918lsquic_conn_t * lsquic_stream_conn(const lsquic_stream_t *s); 919 920lsquic_stream_t * 921lsquic_conn_get_stream_by_id (lsquic_conn_t *c, uint32_t stream_id); 922 923/** Get connection ID */ 924lsquic_cid_t 925lsquic_conn_id (const lsquic_conn_t *c); 926 927/** Get pointer to the engine */ 928lsquic_engine_t * 929lsquic_conn_get_engine (lsquic_conn_t *c); 930 931int lsquic_conn_get_sockaddr(const lsquic_conn_t *c, 932 const struct sockaddr **local, const struct sockaddr **peer); 933 934struct lsquic_logger_if { 935 int (*vprintf)(void *logger_ctx, const char *fmt, va_list args); 936}; 937 938/** 939 * Enumerate timestamp styles supported by LSQUIC logger mechanism. 940 */ 941enum lsquic_logger_timestamp_style { 942 /** 943 * No timestamp is generated. 944 */ 945 LLTS_NONE, 946 947 /** 948 * The timestamp consists of 24 hours, minutes, seconds, and 949 * milliseconds. Example: 13:43:46.671 950 */ 951 LLTS_HHMMSSMS, 952 953 /** 954 * Like above, plus date, e.g: 2017-03-21 13:43:46.671 955 */ 956 LLTS_YYYYMMDD_HHMMSSMS, 957 958 /** 959 * This is Chrome-like timestamp used by proto-quic. The timestamp 960 * includes month, date, hours, minutes, seconds, and microseconds. 961 * 962 * Example: 1223/104613.946956 (instead of 12/23 10:46:13.946956). 963 * 964 * This is to facilitate reading two logs side-by-side. 965 */ 966 LLTS_CHROMELIKE, 967 968 /** 969 * The timestamp consists of 24 hours, minutes, seconds, and 970 * microseconds. Example: 13:43:46.671123 971 */ 972 LLTS_HHMMSSUS, 973 974 /** 975 * Date and time using microsecond resolution, 976 * e.g: 2017-03-21 13:43:46.671123 977 */ 978 LLTS_YYYYMMDD_HHMMSSUS, 979 980 N_LLTS 981}; 982 983/** 984 * Call this if you want to do something with LSQUIC log messages, as they 985 * are thrown out by default. 986 */ 987void lsquic_logger_init(const struct lsquic_logger_if *, void *logger_ctx, 988 enum lsquic_logger_timestamp_style); 989 990/** 991 * Set log level for all LSQUIC modules. Acceptable values are debug, info, 992 * notice, warning, error, alert, emerg, crit (case-insensitive). 993 * 994 * @retval 0 Success. 995 * @retval -1 Failure: log_level is not valid. 996 */ 997int 998lsquic_set_log_level (const char *log_level); 999 1000/** 1001 * E.g. "event=debug" 1002 */ 1003int 1004lsquic_logger_lopt (const char *optarg); 1005 1006/** 1007 * Return the list of QUIC versions (as bitmask) this engine instance 1008 * supports. 1009 */ 1010unsigned lsquic_engine_quic_versions (const lsquic_engine_t *); 1011 1012/** 1013 * This is one of the flags that can be passed to @ref lsquic_global_init. 1014 * Use it to initialize LSQUIC for use in client mode. 1015 */ 1016#define LSQUIC_GLOBAL_CLIENT (1 << 0) 1017 1018/** 1019 * This is one of the flags that can be passed to @ref lsquic_global_init. 1020 * Use it to initialize LSQUIC for use in server mode. 1021 */ 1022#define LSQUIC_GLOBAL_SERVER (1 << 1) 1023 1024/** 1025 * Initialize LSQUIC. This must be called before any other LSQUIC function 1026 * is called. Returns 0 on success and -1 on failure. 1027 * 1028 * @param flags This a bitmask of @ref LSQUIC_GLOBAL_CLIENT and 1029 * @ref LSQUIC_GLOBAL_SERVER. At least one of these 1030 * flags should be specified. 1031 * 1032 * @retval 0 Success. 1033 * @retval -1 Initialization failed. 1034 * 1035 * @see LSQUIC_GLOBAL_CLIENT 1036 * @see LSQUIC_GLOBAL_SERVER 1037 */ 1038int 1039lsquic_global_init (int flags); 1040 1041/** 1042 * Clean up global state created by @ref lsquic_global_init. Should be 1043 * called after all LSQUIC engine instances are gone. 1044 */ 1045void 1046lsquic_global_cleanup (void); 1047 1048/** 1049 * Get QUIC version used by the connection. 1050 * 1051 * @see lsquic_version 1052 */ 1053enum lsquic_version 1054lsquic_conn_quic_version (const lsquic_conn_t *c); 1055 1056/** Translate string QUIC version to LSQUIC QUIC version representation */ 1057enum lsquic_version 1058lsquic_str2ver (const char *str, size_t len); 1059 1060/** 1061 * Get user-supplied context associated with the connection. 1062 */ 1063lsquic_conn_ctx_t * 1064lsquic_conn_get_ctx (const lsquic_conn_t *c); 1065 1066/** 1067 * Set user-supplied context associated with the connection. 1068 */ 1069void lsquic_conn_set_ctx (lsquic_conn_t *c, lsquic_conn_ctx_t *h); 1070 1071/** 1072 * Get peer context associated with the connection. 1073 */ 1074void *lsquic_conn_get_peer_ctx( const lsquic_conn_t *lconn); 1075 1076/** 1077 * Abort connection. 1078 */ 1079void 1080lsquic_conn_abort (lsquic_conn_t *c); 1081 1082/** 1083 * Returns true if there are connections to be processed, false otherwise. 1084 * If true, `diff' is set to the difference between the earliest advisory 1085 * tick time and now. If the former is in the past, the value of `diff' 1086 * is negative. 1087 */ 1088int 1089lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff); 1090 1091/** 1092 * Return number of connections whose advisory tick time is before current 1093 * time plus `from_now' microseconds from now. `from_now' can be negative. 1094 */ 1095unsigned 1096lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now); 1097 1098enum LSQUIC_CONN_STATUS 1099{ 1100 LSCONN_ST_HSK_IN_PROGRESS, 1101 LSCONN_ST_CONNECTED, 1102 LSCONN_ST_HSK_FAILURE, 1103 LSCONN_ST_GOING_AWAY, 1104 LSCONN_ST_TIMED_OUT, 1105 /* If es_honor_prst is not set, the connection will never get public 1106 * reset packets and this flag will not be set. 1107 */ 1108 LSCONN_ST_RESET, 1109 LSCONN_ST_USER_ABORTED, 1110 LSCONN_ST_ERROR, 1111 LSCONN_ST_CLOSED, 1112 LSCONN_ST_PEER_GOING_AWAY, 1113}; 1114 1115enum LSQUIC_CONN_STATUS 1116lsquic_conn_status (lsquic_conn_t *, char *errbuf, size_t bufsz); 1117 1118extern const char *const 1119lsquic_ver2str[N_LSQVER]; 1120 1121#ifdef __cplusplus 1122} 1123#endif 1124 1125#endif //__LSQUIC_H__ 1126 1127