ngtcp2_conn.h 42 KB

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