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