| 1 | /* |
| 2 | * ngtcp2 |
| 3 | * |
| 4 | * Copyright (c) 2017 ngtcp2 contributors |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | * a copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be |
| 15 | * included in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | #ifndef NGTCP2_CONN_H |
| 26 | #define NGTCP2_CONN_H |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | # include <config.h> |
| 30 | #endif /* defined(HAVE_CONFIG_H) */ |
| 31 | |
| 32 | #include <ngtcp2/ngtcp2.h> |
| 33 | |
| 34 | #include "ngtcp2_mem.h" |
| 35 | #include "ngtcp2_crypto.h" |
| 36 | #include "ngtcp2_acktr.h" |
| 37 | #include "ngtcp2_rtb.h" |
| 38 | #include "ngtcp2_strm.h" |
| 39 | #include "ngtcp2_idtr.h" |
| 40 | #include "ngtcp2_str.h" |
| 41 | #include "ngtcp2_pkt.h" |
| 42 | #include "ngtcp2_log.h" |
| 43 | #include "ngtcp2_pq.h" |
| 44 | #include "ngtcp2_cc.h" |
| 45 | #include "ngtcp2_bbr.h" |
| 46 | #include "ngtcp2_pv.h" |
| 47 | #include "ngtcp2_pmtud.h" |
| 48 | #include "ngtcp2_cid.h" |
| 49 | #include "ngtcp2_buf.h" |
| 50 | #include "ngtcp2_ppe.h" |
| 51 | #include "ngtcp2_qlog.h" |
| 52 | #include "ngtcp2_rst.h" |
| 53 | #include "ngtcp2_conn_stat.h" |
| 54 | #include "ngtcp2_dcidtr.h" |
| 55 | #include "ngtcp2_pcg.h" |
| 56 | #include "ngtcp2_ratelim.h" |
| 57 | |
| 58 | typedef enum { |
| 59 | /* Client specific handshake states */ |
| 60 | NGTCP2_CS_CLIENT_INITIAL, |
| 61 | NGTCP2_CS_CLIENT_WAIT_HANDSHAKE, |
| 62 | /* Server specific handshake states */ |
| 63 | NGTCP2_CS_SERVER_INITIAL, |
| 64 | NGTCP2_CS_SERVER_WAIT_HANDSHAKE, |
| 65 | /* Shared by both client and server */ |
| 66 | NGTCP2_CS_POST_HANDSHAKE, |
| 67 | NGTCP2_CS_CLOSING, |
| 68 | NGTCP2_CS_DRAINING, |
| 69 | } ngtcp2_conn_state; |
| 70 | |
| 71 | /* NGTCP2_MAX_NUM_BUFFED_RX_PKTS is the maximum number of buffered |
| 72 | reordered packets. */ |
| 73 | #define NGTCP2_MAX_NUM_BUFFED_RX_PKTS 4 |
| 74 | |
| 75 | /* NGTCP2_MAX_REORDERED_CRYPTO_DATA is the maximum offset of crypto |
| 76 | data which is not continuous. In other words, there is a gap of |
| 77 | unreceived data. */ |
| 78 | #define NGTCP2_MAX_REORDERED_CRYPTO_DATA 65536 |
| 79 | |
| 80 | /* NGTCP2_MAX_SCID_POOL_SIZE is the maximum number of source |
| 81 | connection ID the local endpoint provides to the remote endpoint. |
| 82 | The chosen value was described in old draft. Now a remote endpoint |
| 83 | tells the maximum value. The value can be quite large, and we have |
| 84 | to put the sane limit.*/ |
| 85 | #define NGTCP2_MAX_SCID_POOL_SIZE 8 |
| 86 | |
| 87 | /* NGTCP2_ECN_MAX_NUM_VALIDATION_PKTS is the maximum number of ECN marked |
| 88 | packets sent in NGTCP2_ECN_STATE_TESTING period. */ |
| 89 | #define NGTCP2_ECN_MAX_NUM_VALIDATION_PKTS 10 |
| 90 | |
| 91 | /* NGTCP2_CCERR_MAX_REASONLEN is the maximum length of reason phrase |
| 92 | to remember. If the received reason phrase is longer than this |
| 93 | value, it is truncated. */ |
| 94 | #define NGTCP2_CCERR_MAX_REASONLEN 1024 |
| 95 | |
| 96 | /* NGTCP2_WRITE_PKT_FLAG_NONE indicates that no flag is set. */ |
| 97 | #define NGTCP2_WRITE_PKT_FLAG_NONE 0x00u |
| 98 | /* NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING indicates that packet other |
| 99 | than Initial packet should be padded so that UDP datagram payload |
| 100 | is at least NGTCP2_MAX_UDP_PAYLOAD_SIZE bytes. Initial packet |
| 101 | might be padded based on QUIC requirement regardless of this |
| 102 | flag. */ |
| 103 | #define NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING 0x01u |
| 104 | /* NGTCP2_WRITE_PKT_FLAG_MORE indicates that more frames might come |
| 105 | and it should be encoded into the current packet. */ |
| 106 | #define NGTCP2_WRITE_PKT_FLAG_MORE 0x02u |
| 107 | /* NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING_FULL is just like |
| 108 | NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING, but it requests to add |
| 109 | padding to the full UDP datagram payload size. */ |
| 110 | #define NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING_FULL 0x04u |
| 111 | /* NGTCP2_WRITE_PKT_FLAG_PADDING_IF_NOT_EMPTY adds padding to the QUIC |
| 112 | packet as much as possible if the packet is not empty. */ |
| 113 | #define NGTCP2_WRITE_PKT_FLAG_PADDING_IF_NOT_EMPTY 0x08u |
| 114 | |
| 115 | typedef struct ngtcp2_path_challenge_entry { |
| 116 | ngtcp2_path_storage ps; |
| 117 | uint8_t data[NGTCP2_PATH_CHALLENGE_DATALEN]; |
| 118 | } ngtcp2_path_challenge_entry; |
| 119 | |
| 120 | void ngtcp2_path_challenge_entry_init(ngtcp2_path_challenge_entry *pcent, |
| 121 | const ngtcp2_path *path, |
| 122 | const uint8_t *data); |
| 123 | |
| 124 | /* NGTCP2_CONN_FLAG_NONE indicates that no flag is set. */ |
| 125 | #define NGTCP2_CONN_FLAG_NONE 0x00u |
| 126 | /* NGTCP2_CONN_FLAG_TLS_HANDSHAKE_COMPLETED is set when TLS stack |
| 127 | declares that TLS handshake has completed. The condition of this |
| 128 | declaration varies between TLS implementations and this flag does |
| 129 | not indicate the completion of QUIC handshake. Some |
| 130 | implementations declare TLS handshake completion as server when |
| 131 | they write off Server Finished and before deriving application rx |
| 132 | secret. */ |
| 133 | #define NGTCP2_CONN_FLAG_TLS_HANDSHAKE_COMPLETED 0x01u |
| 134 | /* NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED is set when the first |
| 135 | Initial packet has successfully been processed. */ |
| 136 | #define NGTCP2_CONN_FLAG_INITIAL_PKT_PROCESSED 0x02u |
| 137 | /* NGTCP2_CONN_FLAG_TRANSPORT_PARAM_RECVED is set if transport |
| 138 | parameters are received. */ |
| 139 | #define NGTCP2_CONN_FLAG_TRANSPORT_PARAM_RECVED 0x04u |
| 140 | /* NGTCP2_CONN_FLAG_LOCAL_TRANSPORT_PARAMS_COMMITTED is set when a |
| 141 | local transport parameters are applied. */ |
| 142 | #define NGTCP2_CONN_FLAG_LOCAL_TRANSPORT_PARAMS_COMMITTED 0x08u |
| 143 | /* NGTCP2_CONN_FLAG_RECV_RETRY is set when a client receives Retry |
| 144 | packet. */ |
| 145 | #define NGTCP2_CONN_FLAG_RECV_RETRY 0x10u |
| 146 | /* NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED is set when 0-RTT packet is |
| 147 | rejected by a peer. */ |
| 148 | #define NGTCP2_CONN_FLAG_EARLY_DATA_REJECTED 0x20u |
| 149 | /* NGTCP2_CONN_FLAG_KEEP_ALIVE_CANCELLED is set when the expired |
| 150 | keep-alive timer has been cancelled. */ |
| 151 | #define NGTCP2_CONN_FLAG_KEEP_ALIVE_CANCELLED 0x40u |
| 152 | /* NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED is set when an endpoint |
| 153 | confirmed completion of handshake. */ |
| 154 | #define NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED 0x80u |
| 155 | /* NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED is set when the library |
| 156 | transitions its state to "post handshake". */ |
| 157 | #define NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED 0x0100u |
| 158 | /* NGTCP2_CONN_FLAG_HANDSHAKE_EARLY_RETRANSMIT is set when the early |
| 159 | handshake retransmission has done when server receives overlapping |
| 160 | Initial crypto data. */ |
| 161 | #define NGTCP2_CONN_FLAG_HANDSHAKE_EARLY_RETRANSMIT 0x0200u |
| 162 | /* NGTCP2_CONN_FLAG_CLEAR_FIXED_BIT indicates that the local endpoint |
| 163 | sends a QUIC packet without Fixed Bit set if a remote endpoint |
| 164 | supports Greasing QUIC Bit extension. */ |
| 165 | #define NGTCP2_CONN_FLAG_CLEAR_FIXED_BIT 0x0400u |
| 166 | /* NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED is set when key update is |
| 167 | not confirmed by the local endpoint. That is, it has not received |
| 168 | ACK frame which acknowledges packet which is encrypted with new |
| 169 | key. */ |
| 170 | #define NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED 0x0800u |
| 171 | /* NGTCP2_CONN_FLAG_PPE_PENDING is set when |
| 172 | NGTCP2_WRITE_STREAM_FLAG_MORE is used and the intermediate state of |
| 173 | ngtcp2_ppe is stored in pkt struct of ngtcp2_conn. */ |
| 174 | #define NGTCP2_CONN_FLAG_PPE_PENDING 0x1000u |
| 175 | /* NGTCP2_CONN_FLAG_RESTART_IDLE_TIMER_ON_WRITE is set when idle timer |
| 176 | should be restarted on next write. */ |
| 177 | #define NGTCP2_CONN_FLAG_RESTART_IDLE_TIMER_ON_WRITE 0x2000u |
| 178 | /* NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED indicates that server as peer |
| 179 | verified client address. This flag is only used by client. */ |
| 180 | #define NGTCP2_CONN_FLAG_SERVER_ADDR_VERIFIED 0x4000u |
| 181 | /* NGTCP2_CONN_FLAG_EARLY_KEY_INSTALLED indicates that an early key is |
| 182 | installed. conn->early.ckm cannot be used for this purpose because |
| 183 | it might be discarded when a certain condition is met. */ |
| 184 | #define NGTCP2_CONN_FLAG_EARLY_KEY_INSTALLED 0x8000u |
| 185 | /* NGTCP2_CONN_FLAG_KEY_UPDATE_INITIATOR is set when the local |
| 186 | endpoint has initiated key update. */ |
| 187 | #define NGTCP2_CONN_FLAG_KEY_UPDATE_INITIATOR 0x10000u |
| 188 | /* NGTCP2_CONN_FLAG_AGGREGATE_PKTS is set when |
| 189 | ngtcp2_conn_writev_stream is called inside the callback invoked by |
| 190 | ngtcp2_conn_write_aggregate_pkt. */ |
| 191 | #define NGTCP2_CONN_FLAG_AGGREGATE_PKTS 0x20000u |
| 192 | /* NGTCP2_CONN_FLAG_CRUMBLE_INITIAL_CRYPTO, if set, crumbles an |
| 193 | Initial CRYPTO frame into pieces as a countermeasure against Deep |
| 194 | Packet Inspection. */ |
| 195 | #define NGTCP2_CONN_FLAG_CRUMBLE_INITIAL_CRYPTO 0x40000u |
| 196 | |
| 197 | typedef struct ngtcp2_pktns { |
| 198 | struct { |
| 199 | /* last_pkt_num is the packet number which the local endpoint sent |
| 200 | last time.*/ |
| 201 | int64_t last_pkt_num; |
| 202 | struct { |
| 203 | /* next_pkt_num is the next packet number to skip. */ |
| 204 | int64_t next_pkt_num; |
| 205 | /* exponent makes gap of skipping packets spread |
| 206 | exponentially. */ |
| 207 | int64_t exponent; |
| 208 | } skip_pkt; |
| 209 | ngtcp2_frame_chain *frq; |
| 210 | /* non_ack_pkt_start_ts is the timestamp since the local endpoint |
| 211 | starts sending continuous non ACK-eliciting packets. */ |
| 212 | ngtcp2_tstamp non_ack_pkt_start_ts; |
| 213 | |
| 214 | struct { |
| 215 | /* ect0 is the number of QUIC packets, not UDP datagram, which |
| 216 | are sent in UDP datagram with ECT0 marking. */ |
| 217 | size_t ect0; |
| 218 | /* start_pkt_num is the lowest packet number that are sent |
| 219 | during ECN validation period. */ |
| 220 | int64_t start_pkt_num; |
| 221 | /* validation_pkt_sent is the number of QUIC packets sent during |
| 222 | validation period. */ |
| 223 | size_t validation_pkt_sent; |
| 224 | /* validation_pkt_lost is the number of QUIC packets lost during |
| 225 | validation period. */ |
| 226 | size_t validation_pkt_lost; |
| 227 | } ecn; |
| 228 | } tx; |
| 229 | |
| 230 | struct { |
| 231 | /* pngap tracks received packet number in order to suppress |
| 232 | duplicated packet number. */ |
| 233 | ngtcp2_gaptr pngap; |
| 234 | /* max_ack_eliciting_pkt_num is the largest ack-eliciting packet |
| 235 | number received so far. */ |
| 236 | int64_t max_ack_eliciting_pkt_num; |
| 237 | /* |
| 238 | * buffed_pkts is buffered packets which cannot be decrypted with |
| 239 | * the current encryption level. |
| 240 | * |
| 241 | * In server Initial encryption level, 0-RTT packet may be buffered. |
| 242 | * In server Handshake encryption level, Short packet may be buffered. |
| 243 | * |
| 244 | * In client Initial encryption level, Handshake or Short packet may |
| 245 | * be buffered. In client Handshake encryption level, Short packet |
| 246 | * may be buffered. |
| 247 | * |
| 248 | * - 0-RTT packet is only buffered in server Initial encryption |
| 249 | * level ngtcp2_pktns. |
| 250 | * |
| 251 | * - Handshake packet is only buffered in client Handshake |
| 252 | * encryption level ngtcp2_pktns. |
| 253 | * |
| 254 | * - Short packet is only buffered in Short encryption level |
| 255 | * ngtcp2_pktns. |
| 256 | */ |
| 257 | ngtcp2_pkt_chain *buffed_pkts; |
| 258 | } rx; |
| 259 | |
| 260 | struct { |
| 261 | struct { |
| 262 | /* offset is the offset of crypto stream in this packet number |
| 263 | space. */ |
| 264 | uint64_t offset; |
| 265 | /* ckm is a cryptographic key, and iv to encrypt outgoing |
| 266 | packets. */ |
| 267 | ngtcp2_crypto_km *ckm; |
| 268 | /* hp_ctx is cipher context for packet header protection. */ |
| 269 | ngtcp2_crypto_cipher_ctx hp_ctx; |
| 270 | /* data is the submitted crypto data. */ |
| 271 | ngtcp2_buf_chain *data; |
| 272 | } tx; |
| 273 | |
| 274 | struct { |
| 275 | /* ckm is a cryptographic key, and iv to decrypt incoming |
| 276 | packets. */ |
| 277 | ngtcp2_crypto_km *ckm; |
| 278 | /* hp_ctx is cipher context for packet header protection. */ |
| 279 | ngtcp2_crypto_cipher_ctx hp_ctx; |
| 280 | } rx; |
| 281 | |
| 282 | ngtcp2_strm strm; |
| 283 | ngtcp2_crypto_ctx ctx; |
| 284 | } crypto; |
| 285 | |
| 286 | ngtcp2_acktr acktr; |
| 287 | ngtcp2_rtb rtb; |
| 288 | ngtcp2_pktns_id id; |
| 289 | } ngtcp2_pktns; |
| 290 | |
| 291 | typedef enum ngtcp2_ecn_state { |
| 292 | NGTCP2_ECN_STATE_TESTING, |
| 293 | NGTCP2_ECN_STATE_UNKNOWN, |
| 294 | NGTCP2_ECN_STATE_FAILED, |
| 295 | NGTCP2_ECN_STATE_CAPABLE, |
| 296 | } ngtcp2_ecn_state; |
| 297 | |
| 298 | /* ngtcp2_early_transport_params is the values remembered by client |
| 299 | from the previous session. */ |
| 300 | typedef struct ngtcp2_early_transport_params { |
| 301 | uint64_t initial_max_streams_bidi; |
| 302 | uint64_t initial_max_streams_uni; |
| 303 | uint64_t initial_max_stream_data_bidi_local; |
| 304 | uint64_t initial_max_stream_data_bidi_remote; |
| 305 | uint64_t initial_max_stream_data_uni; |
| 306 | uint64_t initial_max_data; |
| 307 | uint64_t active_connection_id_limit; |
| 308 | uint64_t max_datagram_frame_size; |
| 309 | } ngtcp2_early_transport_params; |
| 310 | |
| 311 | ngtcp2_static_ringbuf_def(path_challenge, 4, |
| 312 | sizeof(ngtcp2_path_challenge_entry)) |
| 313 | |
| 314 | ngtcp2_static_ringbuf_def(path_history, 4, sizeof(ngtcp2_path_history_entry)) |
| 315 | |
| 316 | ngtcp2_objalloc_decl(strm, ngtcp2_strm, oplent) |
| 317 | |
| 318 | struct ngtcp2_conn { |
| 319 | ngtcp2_objalloc frc_objalloc; |
| 320 | ngtcp2_objalloc rtb_entry_objalloc; |
| 321 | ngtcp2_objalloc strm_objalloc; |
| 322 | ngtcp2_conn_state state; |
| 323 | ngtcp2_callbacks callbacks; |
| 324 | /* rcid is a connection ID present in Initial or 0-RTT packet from |
| 325 | client as destination connection ID. Server uses this field to |
| 326 | check that duplicated Initial or 0-RTT packet are indeed sent to |
| 327 | this connection. Client uses this field to validate |
| 328 | original_destination_connection_id transport parameter. */ |
| 329 | ngtcp2_cid rcid; |
| 330 | /* oscid is the source connection ID initially used by the local |
| 331 | endpoint. */ |
| 332 | ngtcp2_cid oscid; |
| 333 | /* retry_scid is the source connection ID from Retry packet. Client |
| 334 | records it in order to verify retry_source_connection_id |
| 335 | transport parameter. Server does not use this field. */ |
| 336 | ngtcp2_cid retry_scid; |
| 337 | /* hs_local_addr is a local address used during handshake. */ |
| 338 | ngtcp2_sockaddr_union hs_local_addr; |
| 339 | ngtcp2_pktns *in_pktns; |
| 340 | ngtcp2_pktns *hs_pktns; |
| 341 | ngtcp2_pktns pktns; |
| 342 | |
| 343 | struct { |
| 344 | /* current is the current destination connection ID. */ |
| 345 | ngtcp2_dcid current; |
| 346 | ngtcp2_dcidtr dtr; |
| 347 | /* seqgap tracks received sequence numbers in order to ignore |
| 348 | retransmitted duplicated NEW_CONNECTION_ID frame. */ |
| 349 | ngtcp2_gaptr seqgap; |
| 350 | /* retire_prior_to is the largest retire_prior_to received so |
| 351 | far. */ |
| 352 | uint64_t retire_prior_to; |
| 353 | } dcid; |
| 354 | |
| 355 | struct { |
| 356 | /* set is a set of CID sent to peer. The peer can use any CIDs in |
| 357 | this set. This includes used CID as well as unused ones. */ |
| 358 | ngtcp2_ksl set; |
| 359 | /* used is a set of CID used by peer. The sort function of this |
| 360 | priority queue takes timestamp when CID is retired and sorts |
| 361 | them in ascending order. */ |
| 362 | ngtcp2_pq used; |
| 363 | /* last_seq is the last sequence number of connection ID. */ |
| 364 | uint64_t last_seq; |
| 365 | /* num_retired is the number of retired Connection ID still |
| 366 | included in set. */ |
| 367 | size_t num_retired; |
| 368 | /* num_in_flight is the number of NEW_CONNECTION_ID frames that |
| 369 | are in-flight and not acknowledged yet. */ |
| 370 | size_t num_in_flight; |
| 371 | } scid; |
| 372 | |
| 373 | struct { |
| 374 | /* strmq contains ngtcp2_strm which has frames to send. */ |
| 375 | ngtcp2_pq strmq; |
| 376 | /* offset is the offset the local endpoint has sent to the remote |
| 377 | endpoint. */ |
| 378 | uint64_t offset; |
| 379 | /* max_offset is the maximum offset that local endpoint can |
| 380 | send. */ |
| 381 | uint64_t max_offset; |
| 382 | /* last_blocked_offset is the largest offset where the |
| 383 | transmission of stream data is blocked. */ |
| 384 | uint64_t last_blocked_offset; |
| 385 | /* last_max_data_ts is the timestamp when last MAX_DATA frame is |
| 386 | sent. */ |
| 387 | ngtcp2_tstamp last_max_data_ts; |
| 388 | |
| 389 | struct { |
| 390 | /* validation_start_ts is the timestamp when ECN validation is |
| 391 | started. It is UINT64_MAX if it has not started yet. */ |
| 392 | ngtcp2_tstamp validation_start_ts; |
| 393 | /* dgram_sent is the number of UDP datagram sent during ECN |
| 394 | validation period. */ |
| 395 | size_t dgram_sent; |
| 396 | /* state is the state of ECN validation */ |
| 397 | ngtcp2_ecn_state state; |
| 398 | } ecn; |
| 399 | |
| 400 | struct { |
| 401 | /* pktlen is the number of bytes written before calling |
| 402 | ngtcp2_conn_update_pkt_tx_time which resets this field to |
| 403 | 0. */ |
| 404 | size_t pktlen; |
| 405 | /* next_ts is the time to send next packet. It is UINT64_MAX if |
| 406 | packet pacing is disabled or expired.*/ |
| 407 | ngtcp2_tstamp next_ts; |
| 408 | /* compensation is the amount of time that a local endpoint |
| 409 | waits too long for pacing. This happens because there is an |
| 410 | overhead before start writing packets after pacing timer |
| 411 | expires. If multiple QUIC connections are handled by a |
| 412 | single thread, which is typical use case for event loop based |
| 413 | servers, each processing of QUIC connection adds overhead, |
| 414 | for example, TLS handshake, and packet encryption/decryption, |
| 415 | etc. */ |
| 416 | ngtcp2_duration compensation; |
| 417 | } pacing; |
| 418 | } tx; |
| 419 | |
| 420 | struct { |
| 421 | /* unsent_max_offset is the maximum offset that remote endpoint |
| 422 | can send without extending MAX_DATA. This limit is not yet |
| 423 | notified to the remote endpoint. */ |
| 424 | uint64_t unsent_max_offset; |
| 425 | /* offset is the cumulative sum of stream data received for this |
| 426 | connection. */ |
| 427 | uint64_t offset; |
| 428 | /* max_offset is the maximum offset that remote endpoint can |
| 429 | send. */ |
| 430 | uint64_t max_offset; |
| 431 | /* window is the connection-level flow control window size. */ |
| 432 | uint64_t window; |
| 433 | /* path_challenge stores received PATH_CHALLENGE data. */ |
| 434 | ngtcp2_static_ringbuf_path_challenge path_challenge; |
| 435 | /* ccerr is the received connection close error. */ |
| 436 | ngtcp2_ccerr ccerr; |
| 437 | |
| 438 | struct { |
| 439 | /* pkt_num is the lowest incoming packet number of the packet |
| 440 | that server verified preferred address usage of client. */ |
| 441 | int64_t pkt_num; |
| 442 | } preferred_addr; |
| 443 | } rx; |
| 444 | |
| 445 | struct { |
| 446 | ngtcp2_crypto_km *ckm; |
| 447 | ngtcp2_crypto_cipher_ctx hp_ctx; |
| 448 | ngtcp2_crypto_ctx ctx; |
| 449 | /* discard_started_ts is the timestamp when the timer to discard |
| 450 | early key has started. Used by server only. */ |
| 451 | ngtcp2_tstamp discard_started_ts; |
| 452 | /* transport_params is the values remembered by client from the |
| 453 | previous session. These are set by |
| 454 | ngtcp2_conn_set_early_remote_transport_params(). Server does |
| 455 | not use this field. Server must not set values for these |
| 456 | parameters that are smaller than the remembered values. */ |
| 457 | ngtcp2_early_transport_params transport_params; |
| 458 | } early; |
| 459 | |
| 460 | struct { |
| 461 | ngtcp2_settings settings; |
| 462 | /* transport_params is the local transport parameters. It is used |
| 463 | for Short packet only. */ |
| 464 | ngtcp2_transport_params transport_params; |
| 465 | struct { |
| 466 | /* max_streams is the maximum number of bidirectional streams which |
| 467 | the local endpoint can open. */ |
| 468 | uint64_t max_streams; |
| 469 | /* next_stream_id is the bidirectional stream ID which the local |
| 470 | endpoint opens next. */ |
| 471 | int64_t next_stream_id; |
| 472 | } bidi; |
| 473 | |
| 474 | struct { |
| 475 | /* max_streams is the maximum number of unidirectional streams |
| 476 | which the local endpoint can open. */ |
| 477 | uint64_t max_streams; |
| 478 | /* next_stream_id is the unidirectional stream ID which the |
| 479 | local endpoint opens next. */ |
| 480 | int64_t next_stream_id; |
| 481 | } uni; |
| 482 | } local; |
| 483 | |
| 484 | struct { |
| 485 | /* transport_params is the received transport parameters during |
| 486 | handshake. It is used for Short packet only. */ |
| 487 | ngtcp2_transport_params *transport_params; |
| 488 | /* pending_transport_params is received transport parameters |
| 489 | during handshake. It is copied to transport_params when 1RTT |
| 490 | key is available. */ |
| 491 | ngtcp2_transport_params *pending_transport_params; |
| 492 | struct { |
| 493 | ngtcp2_idtr idtr; |
| 494 | /* unsent_max_streams is the maximum number of streams of peer |
| 495 | initiated bidirectional stream which the local endpoint can |
| 496 | accept. This limit is not yet notified to the remote |
| 497 | endpoint. */ |
| 498 | uint64_t unsent_max_streams; |
| 499 | /* max_streams is the maximum number of streams of peer |
| 500 | initiated bidirectional stream which the local endpoint can |
| 501 | accept. */ |
| 502 | uint64_t max_streams; |
| 503 | } bidi; |
| 504 | |
| 505 | struct { |
| 506 | ngtcp2_idtr idtr; |
| 507 | /* unsent_max_streams is the maximum number of streams of peer |
| 508 | initiated unidirectional stream which the local endpoint can |
| 509 | accept. This limit is not yet notified to the remote |
| 510 | endpoint. */ |
| 511 | uint64_t unsent_max_streams; |
| 512 | /* max_streams is the maximum number of streams of peer |
| 513 | initiated unidirectional stream which the local endpoint can |
| 514 | accept. */ |
| 515 | uint64_t max_streams; |
| 516 | } uni; |
| 517 | } remote; |
| 518 | |
| 519 | struct { |
| 520 | struct { |
| 521 | /* new_tx_ckm is a new sender 1RTT key which has not been |
| 522 | used. */ |
| 523 | ngtcp2_crypto_km *new_tx_ckm; |
| 524 | /* new_rx_ckm is a new receiver 1RTT key which has not |
| 525 | successfully decrypted incoming packet yet. */ |
| 526 | ngtcp2_crypto_km *new_rx_ckm; |
| 527 | /* old_rx_ckm is an old receiver 1RTT key. */ |
| 528 | ngtcp2_crypto_km *old_rx_ckm; |
| 529 | /* confirmed_ts is the time instant when the key update is |
| 530 | confirmed by the local endpoint last time. UINT64_MAX means |
| 531 | undefined value. */ |
| 532 | ngtcp2_tstamp confirmed_ts; |
| 533 | } key_update; |
| 534 | |
| 535 | /* tls_native_handle is a native handle to TLS session object. */ |
| 536 | void *tls_native_handle; |
| 537 | /* decrypt_hp_buf is a buffer which is used to write unprotected |
| 538 | packet header. */ |
| 539 | ngtcp2_vec decrypt_hp_buf; |
| 540 | /* decrypt_buf is a buffer which is used to write decrypted data. */ |
| 541 | ngtcp2_vec decrypt_buf; |
| 542 | /* retry_aead is AEAD to verify Retry packet integrity. It is |
| 543 | used by client only. */ |
| 544 | ngtcp2_crypto_aead retry_aead; |
| 545 | /* retry_aead_ctx is AEAD cipher context to verify Retry packet |
| 546 | integrity. It is used by client only. */ |
| 547 | ngtcp2_crypto_aead_ctx retry_aead_ctx; |
| 548 | /* decryption_failure_count is the number of received packets that |
| 549 | fail authentication. */ |
| 550 | uint64_t decryption_failure_count; |
| 551 | /* tls_error is TLS related error. */ |
| 552 | int tls_error; |
| 553 | /* tls_alert is TLS alert generated by the local endpoint. */ |
| 554 | uint8_t tls_alert; |
| 555 | } crypto; |
| 556 | |
| 557 | /* pkt contains the packet intermediate construction data to support |
| 558 | NGTCP2_WRITE_STREAM_FLAG_MORE */ |
| 559 | struct { |
| 560 | ngtcp2_crypto_cc cc; |
| 561 | ngtcp2_pkt_hd hd; |
| 562 | ngtcp2_ppe ppe; |
| 563 | ngtcp2_frame_chain **pfrc; |
| 564 | ngtcp2_ssize hs_spktlen; |
| 565 | int pkt_empty; |
| 566 | int hd_logged; |
| 567 | int require_padding; |
| 568 | /* flags is bitwise OR of zero or more of |
| 569 | NGTCP2_RTB_ENTRY_FLAG_*. */ |
| 570 | uint16_t rtb_entry_flags; |
| 571 | } pkt; |
| 572 | |
| 573 | struct { |
| 574 | /* last_ts is a timestamp when a last packet is sent or received |
| 575 | on a current path. */ |
| 576 | ngtcp2_tstamp last_ts; |
| 577 | /* timeout is keep-alive timeout. When it expires, a packet |
| 578 | should be sent to a current path to keep connection alive. It |
| 579 | might be used to keep NAT binding intact. If 0 is set, |
| 580 | keep-alive timer is disabled. */ |
| 581 | ngtcp2_duration timeout; |
| 582 | } keep_alive; |
| 583 | |
| 584 | struct { |
| 585 | /* Initial keys for negotiated version. If original version == |
| 586 | negotiated version, these fields are not used. */ |
| 587 | struct { |
| 588 | ngtcp2_crypto_km *ckm; |
| 589 | ngtcp2_crypto_cipher_ctx hp_ctx; |
| 590 | } rx; |
| 591 | struct { |
| 592 | ngtcp2_crypto_km *ckm; |
| 593 | ngtcp2_crypto_cipher_ctx hp_ctx; |
| 594 | } tx; |
| 595 | /* version is QUIC version that the above Initial keys are created |
| 596 | for. */ |
| 597 | uint32_t version; |
| 598 | /* preferred_versions is the array of versions that are preferred |
| 599 | by the local endpoint. Server negotiates one of those versions |
| 600 | in this array if a client initially selects a less preferred |
| 601 | version. Client uses this field and original_version field to |
| 602 | prevent version downgrade attack if it reacted upon Version |
| 603 | Negotiation packet. */ |
| 604 | uint32_t *preferred_versions; |
| 605 | /* preferred_versionslen is the number of versions stored in the |
| 606 | array pointed by preferred_versions. This field is only used |
| 607 | by server. */ |
| 608 | size_t preferred_versionslen; |
| 609 | /* available_versions is the versions that the local endpoint |
| 610 | sends in version_information transport parameter. This is the |
| 611 | wire image of available_versions field of version_information |
| 612 | transport parameter. */ |
| 613 | uint8_t *available_versions; |
| 614 | /* available_versionslen is the length of data pointed by |
| 615 | available_versions field. */ |
| 616 | size_t available_versionslen; |
| 617 | } vneg; |
| 618 | |
| 619 | ngtcp2_map strms; |
| 620 | ngtcp2_conn_stat cstat; |
| 621 | ngtcp2_pv *pv; |
| 622 | ngtcp2_pmtud *pmtud; |
| 623 | ngtcp2_log log; |
| 624 | ngtcp2_qlog qlog; |
| 625 | ngtcp2_rst rst; |
| 626 | union { |
| 627 | ngtcp2_cc cc; |
| 628 | ngtcp2_cc_reno reno; |
| 629 | ngtcp2_cc_cubic cubic; |
| 630 | ngtcp2_cc_bbr bbr; |
| 631 | }; |
| 632 | /* path_history remembers the paths that have been validated |
| 633 | successfully. The path is added to this history when a local |
| 634 | endpoint migrates to the another path. */ |
| 635 | ngtcp2_static_ringbuf_path_history path_history; |
| 636 | /* glitch_rlim is the rate limit of glitches that can be tolerated. |
| 637 | If more than those glitches are detected, a connection is |
| 638 | closed. */ |
| 639 | ngtcp2_ratelim glitch_rlim; |
| 640 | const ngtcp2_mem *mem; |
| 641 | /* idle_ts is the time instant when idle timer started. */ |
| 642 | ngtcp2_tstamp idle_ts; |
| 643 | /* handshake_confirmed_ts is the time instant when handshake is |
| 644 | confirmed. For server, it is confirmed when completed. */ |
| 645 | ngtcp2_tstamp handshake_confirmed_ts; |
| 646 | ngtcp2_pcg32 pcg; |
| 647 | void *user_data; |
| 648 | uint32_t client_chosen_version; |
| 649 | uint32_t negotiated_version; |
| 650 | /* flags is bitwise OR of zero or more of NGTCP2_CONN_FLAG_*. */ |
| 651 | uint32_t flags; |
| 652 | ngtcp2_cc_algo cc_algo; |
| 653 | int server; |
| 654 | }; |
| 655 | |
| 656 | typedef enum ngtcp2_vmsg_type { |
| 657 | NGTCP2_VMSG_TYPE_STREAM, |
| 658 | NGTCP2_VMSG_TYPE_DATAGRAM, |
| 659 | } ngtcp2_vmsg_type; |
| 660 | |
| 661 | typedef struct ngtcp2_vmsg_stream { |
| 662 | /* strm is a stream that data is sent to. */ |
| 663 | ngtcp2_strm *strm; |
| 664 | /* data is the pointer to ngtcp2_vec array which contains the stream |
| 665 | data to send. */ |
| 666 | const ngtcp2_vec *data; |
| 667 | /* datacnt is the number of ngtcp2_vec pointed by data. */ |
| 668 | size_t datacnt; |
| 669 | /* pdatalen is the pointer to the variable which the number of bytes |
| 670 | written is assigned to if pdatalen is not NULL. */ |
| 671 | ngtcp2_ssize *pdatalen; |
| 672 | /* flags is bitwise OR of zero or more of |
| 673 | NGTCP2_WRITE_STREAM_FLAG_*. */ |
| 674 | uint32_t flags; |
| 675 | } ngtcp2_vmsg_stream; |
| 676 | |
| 677 | typedef struct ngtcp2_vmsg_datagram { |
| 678 | /* data is the pointer to ngtcp2_vec array which contains the data |
| 679 | to send. */ |
| 680 | const ngtcp2_vec *data; |
| 681 | /* datacnt is the number of ngtcp2_vec pointed by data. */ |
| 682 | size_t datacnt; |
| 683 | /* dgram_id is an opaque identifier chosen by an application. */ |
| 684 | uint64_t dgram_id; |
| 685 | /* paccepted is the pointer to the variable which, if it is not |
| 686 | NULL, is assigned nonzero if data is written to a packet. */ |
| 687 | int *paccepted; |
| 688 | /* flags is bitwise OR of zero or more of |
| 689 | NGTCP2_WRITE_DATAGRAM_FLAG_*. */ |
| 690 | uint32_t flags; |
| 691 | } ngtcp2_vmsg_datagram; |
| 692 | |
| 693 | typedef struct ngtcp2_vmsg { |
| 694 | ngtcp2_vmsg_type type; |
| 695 | union { |
| 696 | ngtcp2_vmsg_stream stream; |
| 697 | ngtcp2_vmsg_datagram datagram; |
| 698 | }; |
| 699 | } ngtcp2_vmsg; |
| 700 | |
| 701 | /* |
| 702 | * ngtcp2_conn_find_stream returns a stream whose stream ID is |
| 703 | * |stream_id|. If no such stream is found, it returns NULL. |
| 704 | */ |
| 705 | ngtcp2_strm *ngtcp2_conn_find_stream(ngtcp2_conn *conn, int64_t stream_id); |
| 706 | |
| 707 | /* |
| 708 | * conn_init_stream initializes |strm|. Its stream ID is |stream_id|. |
| 709 | * This function adds |strm| to conn->strms. |strm| must be allocated |
| 710 | * by the caller. |
| 711 | * |
| 712 | * This function returns 0 if it succeeds, or one of the following |
| 713 | * negative error codes: |
| 714 | * |
| 715 | * NGTCP2_ERR_NOMEM |
| 716 | * Out of memory |
| 717 | * NGTCP2_ERR_CALLBACK_FAILURE |
| 718 | * User-callback function failed. |
| 719 | */ |
| 720 | int ngtcp2_conn_init_stream(ngtcp2_conn *conn, ngtcp2_strm *strm, |
| 721 | int64_t stream_id, void *stream_user_data); |
| 722 | |
| 723 | /* |
| 724 | * ngtcp2_conn_close_stream closes stream |strm|. |
| 725 | * |
| 726 | * This function returns 0 if it succeeds, or one of the following |
| 727 | * negative error codes: |
| 728 | * |
| 729 | * NGTCP2_ERR_INVALID_ARGUMENT |
| 730 | * Stream is not found. |
| 731 | * NGTCP2_ERR_CALLBACK_FAILURE |
| 732 | * User-defined callback function failed. |
| 733 | */ |
| 734 | int ngtcp2_conn_close_stream(ngtcp2_conn *conn, ngtcp2_strm *strm); |
| 735 | |
| 736 | /* |
| 737 | * ngtcp2_conn_close_stream closes stream |strm| if no further |
| 738 | * transmission and reception are allowed, and all reordered incoming |
| 739 | * data are emitted to the application, and the transmitted data are |
| 740 | * acked. |
| 741 | * |
| 742 | * This function returns 0 if it succeeds, or one of the following |
| 743 | * negative error codes: |
| 744 | * |
| 745 | * NGTCP2_ERR_INVALID_ARGUMENT |
| 746 | * Stream is not found. |
| 747 | * NGTCP2_ERR_CALLBACK_FAILURE |
| 748 | * User-defined callback function failed. |
| 749 | */ |
| 750 | int ngtcp2_conn_close_stream_if_shut_rdwr(ngtcp2_conn *conn, ngtcp2_strm *strm); |
| 751 | |
| 752 | /* |
| 753 | * ngtcp2_conn_update_rtt updates RTT measurements. |rtt| is a latest |
| 754 | * RTT which is not adjusted by ack delay. |ack_delay| is unscaled |
| 755 | * ack_delay included in ACK frame. |ack_delay| is actually tainted |
| 756 | * (sent by peer), so don't assume that |ack_delay| is always smaller |
| 757 | * than, or equals to |rtt|. |
| 758 | */ |
| 759 | void ngtcp2_conn_update_rtt(ngtcp2_conn *conn, ngtcp2_duration rtt, |
| 760 | ngtcp2_duration ack_delay, ngtcp2_tstamp ts); |
| 761 | |
| 762 | void ngtcp2_conn_set_loss_detection_timer(ngtcp2_conn *conn, ngtcp2_tstamp ts); |
| 763 | |
| 764 | void ngtcp2_conn_cancel_loss_detection_timer(ngtcp2_conn *conn); |
| 765 | |
| 766 | int ngtcp2_conn_on_loss_detection_timer(ngtcp2_conn *conn, ngtcp2_tstamp ts); |
| 767 | |
| 768 | /* |
| 769 | * ngtcp2_conn_detect_lost_pkt detects lost packets. |
| 770 | * |
| 771 | * This function returns 0 if it succeeds, or one of the following |
| 772 | * negative error codes: |
| 773 | * |
| 774 | * NGTCP2_ERR_NOMEM |
| 775 | * Out of memory. |
| 776 | */ |
| 777 | int ngtcp2_conn_detect_lost_pkt(ngtcp2_conn *conn, ngtcp2_pktns *pktns, |
| 778 | ngtcp2_conn_stat *cstat, ngtcp2_tstamp ts); |
| 779 | |
| 780 | /* |
| 781 | * ngtcp2_conn_tx_strmq_top returns the ngtcp2_strm which sits on the |
| 782 | * top of queue. tx_strmq must not be empty. |
| 783 | */ |
| 784 | ngtcp2_strm *ngtcp2_conn_tx_strmq_top(ngtcp2_conn *conn); |
| 785 | |
| 786 | /* |
| 787 | * ngtcp2_conn_tx_strmq_pop pops the ngtcp2_strm from the queue. |
| 788 | * tx_strmq must not be empty. |
| 789 | */ |
| 790 | void ngtcp2_conn_tx_strmq_pop(ngtcp2_conn *conn); |
| 791 | |
| 792 | /* |
| 793 | * ngtcp2_conn_tx_strmq_push pushes |strm| into tx_strmq. |
| 794 | * |
| 795 | * This function returns 0 if it succeeds, or one of the following |
| 796 | * negative error codes: |
| 797 | * |
| 798 | * NGTCP2_ERR_NOMEM |
| 799 | * Out of memory. |
| 800 | */ |
| 801 | int ngtcp2_conn_tx_strmq_push(ngtcp2_conn *conn, ngtcp2_strm *strm); |
| 802 | |
| 803 | /* |
| 804 | * ngtcp2_conn_internal_expiry returns the minimum expiry time among |
| 805 | * all timers in |conn|. |
| 806 | */ |
| 807 | ngtcp2_tstamp ngtcp2_conn_internal_expiry(ngtcp2_conn *conn); |
| 808 | |
| 809 | ngtcp2_ssize ngtcp2_conn_write_vmsg(ngtcp2_conn *conn, ngtcp2_path *path, |
| 810 | int pkt_info_version, ngtcp2_pkt_info *pi, |
| 811 | uint8_t *dest, size_t destlen, |
| 812 | uint8_t wflags, ngtcp2_vmsg *vmsg, |
| 813 | ngtcp2_tstamp ts); |
| 814 | |
| 815 | /* |
| 816 | * ngtcp2_conn_write_single_frame_pkt writes a packet which contains |
| 817 | * |fr| frame only in the buffer pointed by |dest| whose length if |
| 818 | * |destlen|. |type| is a long packet type to send. If |type| is 0, |
| 819 | * Short packet is used. |dcid| is used as a destination connection |
| 820 | * ID. |flags| is zero or more of NGTCP2_WRITE_PKT_FLAG_*. Only |
| 821 | * NGTCP2_WRITE_PKT_FLAG_REQUIRE_PADDING is recognized. |
| 822 | * |
| 823 | * The packet written by this function will not be retransmitted. |
| 824 | * |
| 825 | * This function returns the number of bytes written in |dest| if it |
| 826 | * succeeds, or one of the following negative error codes: |
| 827 | * |
| 828 | * NGTCP2_ERR_CALLBACK_FAILURE |
| 829 | * User-defined callback function failed. |
| 830 | */ |
| 831 | ngtcp2_ssize ngtcp2_conn_write_single_frame_pkt( |
| 832 | ngtcp2_conn *conn, ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, |
| 833 | uint8_t type, uint8_t flags, const ngtcp2_cid *dcid, ngtcp2_frame *fr, |
| 834 | uint16_t rtb_entry_flags, const ngtcp2_path *path, ngtcp2_tstamp ts); |
| 835 | |
| 836 | /* |
| 837 | * ngtcp2_conn_commit_local_transport_params commits the local |
| 838 | * transport parameters, which is currently set to |
| 839 | * conn->local.settings.transport_params. This function will do some |
| 840 | * amends on transport parameters for adjusting default values. |
| 841 | * |
| 842 | * This function returns 0 if it succeeds, or one of the following |
| 843 | * negative error codes: |
| 844 | * |
| 845 | * NGTCP2_ERR_NOMEM |
| 846 | * Out of memory. |
| 847 | * NGTCP2_ERR_INVALID_ARGUMENT |
| 848 | * CID in preferred address equals to the original SCID. |
| 849 | */ |
| 850 | int ngtcp2_conn_commit_local_transport_params(ngtcp2_conn *conn); |
| 851 | |
| 852 | /* |
| 853 | * ngtcp2_conn_lost_pkt_expiry returns the earliest expiry time of |
| 854 | * lost packet. |
| 855 | */ |
| 856 | ngtcp2_tstamp ngtcp2_conn_lost_pkt_expiry(ngtcp2_conn *conn); |
| 857 | |
| 858 | /* |
| 859 | * ngtcp2_conn_remove_lost_pkt removes the expired lost packet. |
| 860 | */ |
| 861 | void ngtcp2_conn_remove_lost_pkt(ngtcp2_conn *conn, ngtcp2_tstamp ts); |
| 862 | |
| 863 | uint64_t ngtcp2_conn_tx_strmq_first_cycle(ngtcp2_conn *conn); |
| 864 | |
| 865 | /** |
| 866 | * @function |
| 867 | * |
| 868 | * `ngtcp2_conn_ack_delay_expiry` returns the expiry time point of |
| 869 | * delayed protected ACK. One should call |
| 870 | * `ngtcp2_conn_cancel_expired_ack_delay_timer` and |
| 871 | * `ngtcp2_conn_write_pkt` (or `ngtcp2_conn_writev_stream`) when it |
| 872 | * expires. It returns UINT64_MAX if there is no expiry. |
| 873 | */ |
| 874 | ngtcp2_tstamp ngtcp2_conn_ack_delay_expiry(ngtcp2_conn *conn); |
| 875 | |
| 876 | /** |
| 877 | * @function |
| 878 | * |
| 879 | * `ngtcp2_conn_cancel_expired_ack_delay_timer` stops expired ACK |
| 880 | * delay timer. |ts| is the current time. This function must be |
| 881 | * called when `ngtcp2_conn_ack_delay_expiry` <= ts. |
| 882 | */ |
| 883 | void ngtcp2_conn_cancel_expired_ack_delay_timer(ngtcp2_conn *conn, |
| 884 | ngtcp2_tstamp ts); |
| 885 | |
| 886 | /** |
| 887 | * @function |
| 888 | * |
| 889 | * `ngtcp2_conn_loss_detection_expiry` returns the expiry time point |
| 890 | * of loss detection timer. One should call |
| 891 | * `ngtcp2_conn_on_loss_detection_timer` and `ngtcp2_conn_write_pkt` |
| 892 | * (or `ngtcp2_conn_writev_stream`) when it expires. It returns |
| 893 | * UINT64_MAX if loss detection timer is not armed. |
| 894 | */ |
| 895 | ngtcp2_tstamp ngtcp2_conn_loss_detection_expiry(ngtcp2_conn *conn); |
| 896 | |
| 897 | /** |
| 898 | * @function |
| 899 | * |
| 900 | * `ngtcp2_conn_get_idle_expiry` returns the time when a connection |
| 901 | * should be closed if it continues to be idle. If idle timeout is |
| 902 | * disabled, this function returns ``UINT64_MAX``. |
| 903 | */ |
| 904 | ngtcp2_tstamp ngtcp2_conn_get_idle_expiry(ngtcp2_conn *conn); |
| 905 | |
| 906 | ngtcp2_duration ngtcp2_conn_compute_pto(ngtcp2_conn *conn, ngtcp2_pktns *pktns); |
| 907 | |
| 908 | /* |
| 909 | * ngtcp2_conn_track_retired_dcid_seq tracks the sequence number |seq| |
| 910 | * of unacknowledged retiring Destination Connection ID. |
| 911 | * |
| 912 | * This function returns 0 if it succeeds, or one of the following |
| 913 | * negative error codes: |
| 914 | * |
| 915 | * NGTCP2_ERR_CONNECTION_ID_LIMIT |
| 916 | * The number of unacknowledged retirement exceeds the limit. |
| 917 | */ |
| 918 | int ngtcp2_conn_track_retired_dcid_seq(ngtcp2_conn *conn, uint64_t seq); |
| 919 | |
| 920 | /* |
| 921 | * ngtcp2_conn_untrack_retired_dcid_seq deletes the sequence number |
| 922 | * |seq| of unacknowledged retiring Destination Connection ID. It is |
| 923 | * fine if such sequence number is not found. |
| 924 | */ |
| 925 | void ngtcp2_conn_untrack_retired_dcid_seq(ngtcp2_conn *conn, uint64_t seq); |
| 926 | |
| 927 | /* |
| 928 | * ngtcp2_conn_check_retired_dcid_tracked returns nonzero if |seq| has |
| 929 | * already been tracked. |
| 930 | */ |
| 931 | int ngtcp2_conn_check_retired_dcid_tracked(ngtcp2_conn *conn, uint64_t seq); |
| 932 | |
| 933 | /* |
| 934 | * ngtcp2_conn_server_negotiate_version negotiates QUIC version. It |
| 935 | * is compatible version negotiation. It returns the negotiated QUIC |
| 936 | * version. This function must not be called by client. |
| 937 | */ |
| 938 | uint32_t |
| 939 | ngtcp2_conn_server_negotiate_version(ngtcp2_conn *conn, |
| 940 | const ngtcp2_version_info *version_info); |
| 941 | |
| 942 | /** |
| 943 | * @function |
| 944 | * |
| 945 | * `ngtcp2_conn_write_connection_close_pkt` writes a packet which |
| 946 | * contains a CONNECTION_CLOSE frame (type 0x1c) in the buffer pointed |
| 947 | * by |dest| whose capacity is |datalen|. |
| 948 | * |
| 949 | * If |path| is not ``NULL``, this function stores the network path |
| 950 | * with which the packet should be sent. Each addr field must point |
| 951 | * to the buffer which should be at least ``sizeof(struct |
| 952 | * sockaddr_storage)`` bytes long. The assignment might not be done |
| 953 | * if nothing is written to |dest|. |
| 954 | * |
| 955 | * If |pi| is not ``NULL``, this function stores packet metadata in it |
| 956 | * if it succeeds. The metadata includes ECN markings. |
| 957 | * |
| 958 | * This function must not be called from inside the callback |
| 959 | * functions. |
| 960 | * |
| 961 | * At the moment, successful call to this function makes connection |
| 962 | * close. We may change this behaviour in the future to allow |
| 963 | * graceful shutdown. |
| 964 | * |
| 965 | * This function returns the number of bytes written in |dest| if it |
| 966 | * succeeds, or one of the following negative error codes: |
| 967 | * |
| 968 | * :macro:`NGTCP2_ERR_NOMEM` |
| 969 | * Out of memory |
| 970 | * :macro:`NGTCP2_ERR_NOBUF` |
| 971 | * Buffer is too small |
| 972 | * :macro:`NGTCP2_ERR_INVALID_STATE` |
| 973 | * The current state does not allow sending CONNECTION_CLOSE. |
| 974 | * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED` |
| 975 | * Packet number is exhausted, and cannot send any more packet. |
| 976 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` |
| 977 | * User callback failed |
| 978 | */ |
| 979 | ngtcp2_ssize ngtcp2_conn_write_connection_close_pkt( |
| 980 | ngtcp2_conn *conn, ngtcp2_path *path, ngtcp2_pkt_info *pi, uint8_t *dest, |
| 981 | size_t destlen, uint64_t error_code, const uint8_t *reason, size_t reasonlen, |
| 982 | ngtcp2_tstamp ts); |
| 983 | |
| 984 | /** |
| 985 | * @function |
| 986 | * |
| 987 | * `ngtcp2_conn_write_application_close_pkt` writes a packet which |
| 988 | * contains a CONNECTION_CLOSE frame (type 0x1d) in the buffer pointed |
| 989 | * by |dest| whose capacity is |datalen|. |
| 990 | * |
| 991 | * If |path| is not ``NULL``, this function stores the network path |
| 992 | * with which the packet should be sent. Each addr field must point |
| 993 | * to the buffer which should be at least ``sizeof(struct |
| 994 | * sockaddr_storage)`` bytes long. The assignment might not be done |
| 995 | * if nothing is written to |dest|. |
| 996 | * |
| 997 | * If |pi| is not ``NULL``, this function stores packet metadata in it |
| 998 | * if it succeeds. The metadata includes ECN markings. |
| 999 | * |
| 1000 | * If handshake has not been confirmed yet, CONNECTION_CLOSE (type |
| 1001 | * 0x1c) with error code :macro:`NGTCP2_APPLICATION_ERROR` is written |
| 1002 | * instead. |
| 1003 | * |
| 1004 | * This function must not be called from inside the callback |
| 1005 | * functions. |
| 1006 | * |
| 1007 | * At the moment, successful call to this function makes connection |
| 1008 | * close. We may change this behaviour in the future to allow |
| 1009 | * graceful shutdown. |
| 1010 | * |
| 1011 | * This function returns the number of bytes written in |dest| if it |
| 1012 | * succeeds, or one of the following negative error codes: |
| 1013 | * |
| 1014 | * :macro:`NGTCP2_ERR_NOMEM` |
| 1015 | * Out of memory |
| 1016 | * :macro:`NGTCP2_ERR_NOBUF` |
| 1017 | * Buffer is too small |
| 1018 | * :macro:`NGTCP2_ERR_INVALID_STATE` |
| 1019 | * The current state does not allow sending CONNECTION_CLOSE. |
| 1020 | * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED` |
| 1021 | * Packet number is exhausted, and cannot send any more packet. |
| 1022 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` |
| 1023 | * User callback failed |
| 1024 | */ |
| 1025 | ngtcp2_ssize ngtcp2_conn_write_application_close_pkt( |
| 1026 | ngtcp2_conn *conn, ngtcp2_path *path, ngtcp2_pkt_info *pi, uint8_t *dest, |
| 1027 | size_t destlen, uint64_t app_error_code, const uint8_t *reason, |
| 1028 | size_t reasonlen, ngtcp2_tstamp ts); |
| 1029 | |
| 1030 | int ngtcp2_conn_start_pmtud(ngtcp2_conn *conn); |
| 1031 | |
| 1032 | void ngtcp2_conn_stop_pmtud(ngtcp2_conn *conn); |
| 1033 | |
| 1034 | /** |
| 1035 | * @function |
| 1036 | * |
| 1037 | * `ngtcp2_conn_set_remote_transport_params` sets transport parameter |
| 1038 | * |params| from a remote endpoint to |conn|. |
| 1039 | * |
| 1040 | * This function returns 0 if it succeeds, or one of the following |
| 1041 | * negative error codes: |
| 1042 | * |
| 1043 | * :macro:`NGTCP2_ERR_TRANSPORT_PARAM` |
| 1044 | * Failed to validate a remote transport parameters. |
| 1045 | * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE` |
| 1046 | * Version negotiation failure. |
| 1047 | * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` |
| 1048 | * User callback failed |
| 1049 | * :macro:`NGTCP2_ERR_NOMEM` |
| 1050 | * Out of memory. |
| 1051 | */ |
| 1052 | int ngtcp2_conn_set_remote_transport_params( |
| 1053 | ngtcp2_conn *conn, const ngtcp2_transport_params *params); |
| 1054 | |
| 1055 | /** |
| 1056 | * @function |
| 1057 | * |
| 1058 | * `ngtcp2_conn_set_0rtt_remote_transport_params` sets |params| as |
| 1059 | * transport parameters previously received from a server. The |
| 1060 | * parameters are used to send early data. QUIC requires that client |
| 1061 | * application should remember transport parameters along with a |
| 1062 | * session ticket. |
| 1063 | * |
| 1064 | * At least following fields should be set: |
| 1065 | * |
| 1066 | * - initial_max_stream_id_bidi |
| 1067 | * - initial_max_stream_id_uni |
| 1068 | * - initial_max_stream_data_bidi_local |
| 1069 | * - initial_max_stream_data_bidi_remote |
| 1070 | * - initial_max_stream_data_uni |
| 1071 | * - initial_max_data |
| 1072 | * - active_connection_id_limit |
| 1073 | * - max_datagram_frame_size (if DATAGRAM extension was negotiated) |
| 1074 | * |
| 1075 | * The following fields are ignored: |
| 1076 | * |
| 1077 | * - ack_delay_exponent |
| 1078 | * - max_ack_delay |
| 1079 | * - initial_scid |
| 1080 | * - original_dcid |
| 1081 | * - preferred_address and preferred_address_present |
| 1082 | * - retry_scid and retry_scid_present |
| 1083 | * - stateless_reset_token and stateless_reset_token_present |
| 1084 | * |
| 1085 | * This function returns 0 if it succeeds, or one of the following |
| 1086 | * negative error codes: |
| 1087 | * |
| 1088 | * :macro:`NGTCP2_ERR_NOMEM` |
| 1089 | * Out of memory. |
| 1090 | */ |
| 1091 | int ngtcp2_conn_set_0rtt_remote_transport_params( |
| 1092 | ngtcp2_conn *conn, const ngtcp2_transport_params *params); |
| 1093 | |
| 1094 | /* |
| 1095 | * ngtcp2_conn_discard_initial_state discards state for Initial packet |
| 1096 | * number space. |
| 1097 | */ |
| 1098 | void ngtcp2_conn_discard_initial_state(ngtcp2_conn *conn, ngtcp2_tstamp ts); |
| 1099 | |
| 1100 | /* |
| 1101 | * ngtcp2_conn_discard_handshake_state discards state for Handshake |
| 1102 | * packet number space. |
| 1103 | */ |
| 1104 | void ngtcp2_conn_discard_handshake_state(ngtcp2_conn *conn, ngtcp2_tstamp ts); |
| 1105 | |
| 1106 | void ngtcp2_conn_add_path_history(ngtcp2_conn *conn, const ngtcp2_dcid *dcid, |
| 1107 | ngtcp2_tstamp ts); |
| 1108 | |
| 1109 | const ngtcp2_path_history_entry * |
| 1110 | ngtcp2_conn_find_path_history(ngtcp2_conn *conn, const ngtcp2_path *path, |
| 1111 | ngtcp2_tstamp ts); |
| 1112 | |
| 1113 | #endif /* !defined(NGTCP2_CONN_H) */ |