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