ngtcp2_pkt.h 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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_PKT_H
  26. #define NGTCP2_PKT_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. /* QUIC header macros */
  32. #define NGTCP2_HEADER_FORM_BIT 0x80
  33. #define NGTCP2_FIXED_BIT_MASK 0x40
  34. #define NGTCP2_PKT_NUMLEN_MASK 0x03
  35. /* Long header specific macros */
  36. #define NGTCP2_LONG_TYPE_MASK 0x30
  37. #define NGTCP2_LONG_RESERVED_BIT_MASK 0x0c
  38. /* Short header specific macros */
  39. #define NGTCP2_SHORT_RESERVED_BIT_MASK 0x18
  40. #define NGTCP2_SHORT_KEY_PHASE_BIT 0x04
  41. /* NGTCP2_SR_TYPE is a Type field of Stateless Reset. */
  42. #define NGTCP2_SR_TYPE 0x1f
  43. /* NGTCP2_MIN_LONG_HEADERLEN is the minimum length of long header.
  44. That is (1|1|TT|RR|PP)<1> + VERSION<4> + DCIL<1> + SCIL<1> +
  45. LENGTH<1> + PKN<1> */
  46. #define NGTCP2_MIN_LONG_HEADERLEN (1 + 4 + 1 + 1 + 1 + 1)
  47. /* STREAM frame specific macros */
  48. #define NGTCP2_STREAM_FIN_BIT 0x01
  49. #define NGTCP2_STREAM_LEN_BIT 0x02
  50. #define NGTCP2_STREAM_OFF_BIT 0x04
  51. /* NGTCP2_STREAM_OVERHEAD is the maximum number of bytes required
  52. other than payload for STREAM frame. That is from type field to
  53. the beginning of the payload. */
  54. #define NGTCP2_STREAM_OVERHEAD (1 + 8 + 8 + 8)
  55. /* NGTCP2_CRYPTO_OVERHEAD is the maximum number of bytes required
  56. other than payload for CRYPTO frame. That is from type field to
  57. the beginning of the payload. */
  58. #define NGTCP2_CRYPTO_OVERHEAD (1 + 8 + 8)
  59. /* NGTCP2_DATAGRAM_OVERHEAD is the maximum number of bytes required
  60. other than payload for DATAGRAM frame. That is from type field to
  61. the beginning of the payload. */
  62. #define NGTCP2_DATAGRAM_OVERHEAD (1 + 8)
  63. /* NGTCP2_MAX_SERVER_STREAM_ID_BIDI is the maximum bidirectional
  64. server stream ID. */
  65. #define NGTCP2_MAX_SERVER_STREAM_ID_BIDI ((int64_t)0x3ffffffffffffffdll)
  66. /* NGTCP2_MAX_CLIENT_STREAM_ID_BIDI is the maximum bidirectional
  67. client stream ID. */
  68. #define NGTCP2_MAX_CLIENT_STREAM_ID_BIDI ((int64_t)0x3ffffffffffffffcll)
  69. /* NGTCP2_MAX_SERVER_STREAM_ID_UNI is the maximum unidirectional
  70. server stream ID. */
  71. #define NGTCP2_MAX_SERVER_STREAM_ID_UNI ((int64_t)0x3fffffffffffffffll)
  72. /* NGTCP2_MAX_CLIENT_STREAM_ID_UNI is the maximum unidirectional
  73. client stream ID. */
  74. #define NGTCP2_MAX_CLIENT_STREAM_ID_UNI ((int64_t)0x3ffffffffffffffell)
  75. /* NGTCP2_MAX_NUM_ACK_RANGES is the maximum number of Additional ACK
  76. ranges which this library can create, or decode. */
  77. #define NGTCP2_MAX_ACK_RANGES 32
  78. /* NGTCP2_MAX_PKT_NUM is the maximum packet number. */
  79. #define NGTCP2_MAX_PKT_NUM ((int64_t)((1ll << 62) - 1))
  80. /* NGTCP2_MIN_PKT_EXPANDLEN is the minimum packet size expansion to
  81. hide/trigger Stateless Reset. */
  82. #define NGTCP2_MIN_PKT_EXPANDLEN 22
  83. /* NGTCP2_RETRY_TAGLEN is the length of Retry packet integrity tag. */
  84. #define NGTCP2_RETRY_TAGLEN 16
  85. /* NGTCP2_HARD_MAX_UDP_PAYLOAD_SIZE is the maximum UDP datagram
  86. payload size that this library can write. */
  87. #define NGTCP2_HARD_MAX_UDP_PAYLOAD_SIZE ((1 << 24) - 1)
  88. /* NGTCP2_PKT_LENGTHLEN is the number of bytes that is occupied by
  89. Length field in Long packet header. */
  90. #define NGTCP2_PKT_LENGTHLEN 4
  91. /* NGTCP2_PKT_TYPE_INITIAL_V1 is Initial long header packet type for
  92. QUIC v1. */
  93. #define NGTCP2_PKT_TYPE_INITIAL_V1 0x0
  94. /* NGTCP2_PKT_TYPE_0RTT_V1 is 0RTT long header packet type for QUIC
  95. v1. */
  96. #define NGTCP2_PKT_TYPE_0RTT_V1 0x1
  97. /* NGTCP2_PKT_TYPE_HANDSHAKE_V1 is Handshake long header packet type
  98. for QUIC v1. */
  99. #define NGTCP2_PKT_TYPE_HANDSHAKE_V1 0x2
  100. /* NGTCP2_PKT_TYPE_RETRY_V1 is Retry long header packet type for QUIC
  101. v1. */
  102. #define NGTCP2_PKT_TYPE_RETRY_V1 0x3
  103. /* NGTCP2_PKT_TYPE_INITIAL_V2 is Initial long header packet type for
  104. QUIC v2. */
  105. #define NGTCP2_PKT_TYPE_INITIAL_V2 0x1
  106. /* NGTCP2_PKT_TYPE_0RTT_V2 is 0RTT long header packet type for QUIC
  107. v2. */
  108. #define NGTCP2_PKT_TYPE_0RTT_V2 0x2
  109. /* NGTCP2_PKT_TYPE_HANDSHAKE_V2 is Handshake long header packet type
  110. for QUIC v2. */
  111. #define NGTCP2_PKT_TYPE_HANDSHAKE_V2 0x3
  112. /* NGTCP2_PKT_TYPE_RETRY_V2 is Retry long header packet type for QUIC
  113. v2. */
  114. #define NGTCP2_PKT_TYPE_RETRY_V2 0x0
  115. typedef struct ngtcp2_pkt_retry {
  116. ngtcp2_cid odcid;
  117. uint8_t *token;
  118. size_t tokenlen;
  119. uint8_t tag[NGTCP2_RETRY_TAGLEN];
  120. } ngtcp2_pkt_retry;
  121. #define NGTCP2_FRAME_PADDING 0x00
  122. #define NGTCP2_FRAME_PING 0x01
  123. #define NGTCP2_FRAME_ACK 0x02
  124. #define NGTCP2_FRAME_ACK_ECN 0x03
  125. #define NGTCP2_FRAME_RESET_STREAM 0x04
  126. #define NGTCP2_FRAME_STOP_SENDING 0x05
  127. #define NGTCP2_FRAME_CRYPTO 0x06
  128. #define NGTCP2_FRAME_NEW_TOKEN 0x07
  129. #define NGTCP2_FRAME_STREAM 0x08
  130. #define NGTCP2_FRAME_MAX_DATA 0x10
  131. #define NGTCP2_FRAME_MAX_STREAM_DATA 0x11
  132. #define NGTCP2_FRAME_MAX_STREAMS_BIDI 0x12
  133. #define NGTCP2_FRAME_MAX_STREAMS_UNI 0x13
  134. #define NGTCP2_FRAME_DATA_BLOCKED 0x14
  135. #define NGTCP2_FRAME_STREAM_DATA_BLOCKED 0x15
  136. #define NGTCP2_FRAME_STREAMS_BLOCKED_BIDI 0x16
  137. #define NGTCP2_FRAME_STREAMS_BLOCKED_UNI 0x17
  138. #define NGTCP2_FRAME_NEW_CONNECTION_ID 0x18
  139. #define NGTCP2_FRAME_RETIRE_CONNECTION_ID 0x19
  140. #define NGTCP2_FRAME_PATH_CHALLENGE 0x1a
  141. #define NGTCP2_FRAME_PATH_RESPONSE 0x1b
  142. #define NGTCP2_FRAME_CONNECTION_CLOSE 0x1c
  143. #define NGTCP2_FRAME_CONNECTION_CLOSE_APP 0x1d
  144. #define NGTCP2_FRAME_HANDSHAKE_DONE 0x1e
  145. #define NGTCP2_FRAME_DATAGRAM 0x30
  146. #define NGTCP2_FRAME_DATAGRAM_LEN 0x31
  147. /* ngtcp2_stream represents STREAM and CRYPTO frames. */
  148. typedef struct ngtcp2_stream {
  149. uint64_t type;
  150. /**
  151. * flags of decoded STREAM frame. This gets ignored when encoding
  152. * STREAM frame. CRYPTO frame does not include this field, and must
  153. * set it to 0.
  154. */
  155. uint8_t flags;
  156. /* CRYPTO frame does not include this field, and must set it to
  157. 0. */
  158. uint8_t fin;
  159. /* CRYPTO frame does not include this field, and must set it to
  160. 0. */
  161. int64_t stream_id;
  162. uint64_t offset;
  163. /* datacnt is the number of elements that data contains. Although
  164. the length of data is 1 in this definition, the library may
  165. allocate extra bytes to hold more elements. */
  166. size_t datacnt;
  167. /* data is the array of ngtcp2_vec which references data. */
  168. ngtcp2_vec data[1];
  169. } ngtcp2_stream;
  170. typedef struct ngtcp2_ack_range {
  171. uint64_t gap;
  172. uint64_t len;
  173. } ngtcp2_ack_range;
  174. typedef struct ngtcp2_ack {
  175. uint64_t type;
  176. int64_t largest_ack;
  177. uint64_t ack_delay;
  178. /**
  179. * ack_delay_unscaled is an ack_delay multiplied by
  180. * 2**ack_delay_component * NGTCP2_MICROSECONDS.
  181. */
  182. ngtcp2_duration ack_delay_unscaled;
  183. struct {
  184. uint64_t ect0;
  185. uint64_t ect1;
  186. uint64_t ce;
  187. } ecn;
  188. uint64_t first_ack_range;
  189. size_t rangecnt;
  190. ngtcp2_ack_range ranges[1];
  191. } ngtcp2_ack;
  192. typedef struct ngtcp2_padding {
  193. uint64_t type;
  194. /**
  195. * The length of contiguous PADDING frames.
  196. */
  197. size_t len;
  198. } ngtcp2_padding;
  199. typedef struct ngtcp2_reset_stream {
  200. uint64_t type;
  201. int64_t stream_id;
  202. uint64_t app_error_code;
  203. uint64_t final_size;
  204. } ngtcp2_reset_stream;
  205. typedef struct ngtcp2_connection_close {
  206. uint64_t type;
  207. uint64_t error_code;
  208. uint64_t frame_type;
  209. size_t reasonlen;
  210. uint8_t *reason;
  211. } ngtcp2_connection_close;
  212. typedef struct ngtcp2_max_data {
  213. uint64_t type;
  214. /**
  215. * max_data is Maximum Data.
  216. */
  217. uint64_t max_data;
  218. } ngtcp2_max_data;
  219. typedef struct ngtcp2_max_stream_data {
  220. uint64_t type;
  221. int64_t stream_id;
  222. uint64_t max_stream_data;
  223. } ngtcp2_max_stream_data;
  224. typedef struct ngtcp2_max_streams {
  225. uint64_t type;
  226. uint64_t max_streams;
  227. } ngtcp2_max_streams;
  228. typedef struct ngtcp2_ping {
  229. uint64_t type;
  230. } ngtcp2_ping;
  231. typedef struct ngtcp2_data_blocked {
  232. uint64_t type;
  233. uint64_t offset;
  234. } ngtcp2_data_blocked;
  235. typedef struct ngtcp2_stream_data_blocked {
  236. uint64_t type;
  237. int64_t stream_id;
  238. uint64_t offset;
  239. } ngtcp2_stream_data_blocked;
  240. typedef struct ngtcp2_streams_blocked {
  241. uint64_t type;
  242. uint64_t max_streams;
  243. } ngtcp2_streams_blocked;
  244. typedef struct ngtcp2_new_connection_id {
  245. uint64_t type;
  246. uint64_t seq;
  247. uint64_t retire_prior_to;
  248. ngtcp2_cid cid;
  249. uint8_t stateless_reset_token[NGTCP2_STATELESS_RESET_TOKENLEN];
  250. } ngtcp2_new_connection_id;
  251. typedef struct ngtcp2_stop_sending {
  252. uint64_t type;
  253. int64_t stream_id;
  254. uint64_t app_error_code;
  255. } ngtcp2_stop_sending;
  256. typedef struct ngtcp2_path_challenge {
  257. uint64_t type;
  258. uint8_t data[NGTCP2_PATH_CHALLENGE_DATALEN];
  259. } ngtcp2_path_challenge;
  260. typedef struct ngtcp2_path_response {
  261. uint64_t type;
  262. uint8_t data[NGTCP2_PATH_CHALLENGE_DATALEN];
  263. } ngtcp2_path_response;
  264. typedef struct ngtcp2_new_token {
  265. uint64_t type;
  266. uint8_t *token;
  267. size_t tokenlen;
  268. } ngtcp2_new_token;
  269. typedef struct ngtcp2_retire_connection_id {
  270. uint64_t type;
  271. uint64_t seq;
  272. } ngtcp2_retire_connection_id;
  273. typedef struct ngtcp2_handshake_done {
  274. uint64_t type;
  275. } ngtcp2_handshake_done;
  276. typedef struct ngtcp2_datagram {
  277. uint64_t type;
  278. /* dgram_id is an opaque identifier chosen by an application. */
  279. uint64_t dgram_id;
  280. /* datacnt is the number of elements that data contains. */
  281. size_t datacnt;
  282. /* data is a pointer to ngtcp2_vec array that stores data. */
  283. ngtcp2_vec *data;
  284. /* rdata is conveniently embedded to ngtcp2_datagram, so that data
  285. field can just point to the address of this field to store a
  286. single vector which is the case when DATAGRAM is received from a
  287. remote endpoint. */
  288. ngtcp2_vec rdata[1];
  289. } ngtcp2_datagram;
  290. typedef union ngtcp2_frame {
  291. uint64_t type;
  292. ngtcp2_stream stream;
  293. ngtcp2_ack ack;
  294. ngtcp2_padding padding;
  295. ngtcp2_reset_stream reset_stream;
  296. ngtcp2_connection_close connection_close;
  297. ngtcp2_max_data max_data;
  298. ngtcp2_max_stream_data max_stream_data;
  299. ngtcp2_max_streams max_streams;
  300. ngtcp2_ping ping;
  301. ngtcp2_data_blocked data_blocked;
  302. ngtcp2_stream_data_blocked stream_data_blocked;
  303. ngtcp2_streams_blocked streams_blocked;
  304. ngtcp2_new_connection_id new_connection_id;
  305. ngtcp2_stop_sending stop_sending;
  306. ngtcp2_path_challenge path_challenge;
  307. ngtcp2_path_response path_response;
  308. ngtcp2_new_token new_token;
  309. ngtcp2_retire_connection_id retire_connection_id;
  310. ngtcp2_handshake_done handshake_done;
  311. ngtcp2_datagram datagram;
  312. /* Extend ngtcp2_frame so that ngtcp2_stream has at least additional
  313. 3 ngtcp2_vec, totaling 4 slots, which can store HEADERS header,
  314. HEADERS payload, DATA header, and DATA payload in the standard
  315. sized ngtcp2_frame_chain. */
  316. uint8_t pad[sizeof(ngtcp2_stream) + sizeof(ngtcp2_vec) * 3];
  317. } ngtcp2_frame;
  318. typedef struct ngtcp2_pkt_chain ngtcp2_pkt_chain;
  319. /*
  320. * ngtcp2_pkt_chain is the chain of incoming packets buffered.
  321. */
  322. struct ngtcp2_pkt_chain {
  323. ngtcp2_path_storage path;
  324. ngtcp2_pkt_info pi;
  325. ngtcp2_pkt_chain *next;
  326. uint8_t *pkt;
  327. /* pktlen is length of a QUIC packet. */
  328. size_t pktlen;
  329. /* dgramlen is length of UDP datagram payload that a QUIC packet is
  330. included. */
  331. size_t dgramlen;
  332. ngtcp2_tstamp ts;
  333. };
  334. /*
  335. * ngtcp2_pkt_chain_new allocates ngtcp2_pkt_chain objects, and
  336. * assigns its pointer to |*ppc|. The content of buffer pointed by
  337. * |pkt| of length |pktlen| is copied into |*ppc|. The packet is
  338. * obtained via the network |path|. The values of path->local and
  339. * path->remote are copied into |*ppc|.
  340. *
  341. * This function returns 0 if it succeeds, or one of the following
  342. * negative error codes:
  343. *
  344. * NGTCP2_ERR_NOMEM
  345. * Out of memory.
  346. */
  347. int ngtcp2_pkt_chain_new(ngtcp2_pkt_chain **ppc, const ngtcp2_path *path,
  348. const ngtcp2_pkt_info *pi, const uint8_t *pkt,
  349. size_t pktlen, size_t dgramlen, ngtcp2_tstamp ts,
  350. const ngtcp2_mem *mem);
  351. /*
  352. * ngtcp2_pkt_chain_del deallocates |pc|. It also frees the memory
  353. * pointed by |pc|.
  354. */
  355. void ngtcp2_pkt_chain_del(ngtcp2_pkt_chain *pc, const ngtcp2_mem *mem);
  356. /*
  357. * ngtcp2_pkt_hd_init initializes |hd| with the given values. If
  358. * |dcid| and/or |scid| is NULL, Destination Connection ID and/or
  359. * Source Connection ID of |hd| is empty respectively. |pkt_numlen|
  360. * is the number of bytes used to encode |pkt_num| and either 1, 2, or
  361. * 4. |version| is QUIC version for long header. |len| is the length
  362. * field of Initial, 0RTT, and Handshake packets.
  363. */
  364. void ngtcp2_pkt_hd_init(ngtcp2_pkt_hd *hd, uint8_t flags, uint8_t type,
  365. const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
  366. int64_t pkt_num, size_t pkt_numlen, uint32_t version,
  367. size_t len);
  368. /*
  369. * ngtcp2_pkt_encode_hd_long encodes |hd| as QUIC long header into
  370. * |out| which has length |outlen|. It returns the number of bytes
  371. * written into |out| if it succeeds, or one of the following negative
  372. * error codes:
  373. *
  374. * NGTCP2_ERR_NOBUF
  375. * Buffer is too short
  376. */
  377. ngtcp2_ssize ngtcp2_pkt_encode_hd_long(uint8_t *out, size_t outlen,
  378. const ngtcp2_pkt_hd *hd);
  379. /*
  380. * ngtcp2_pkt_encode_hd_short encodes |hd| as QUIC short header into
  381. * |out| which has length |outlen|. It returns the number of bytes
  382. * written into |out| if it succeeds, or one of the following negative
  383. * error codes:
  384. *
  385. * NGTCP2_ERR_NOBUF
  386. * Buffer is too short
  387. */
  388. ngtcp2_ssize ngtcp2_pkt_encode_hd_short(uint8_t *out, size_t outlen,
  389. const ngtcp2_pkt_hd *hd);
  390. /**
  391. * @function
  392. *
  393. * `ngtcp2_pkt_decode_frame` decodes a QUIC frame from the buffer
  394. * pointed by |payload| whose length is |payloadlen|.
  395. *
  396. * This function returns the number of bytes read to decode a single
  397. * frame if it succeeds, or one of the following negative error codes:
  398. *
  399. * :enum:`NGTCP2_ERR_FRAME_ENCODING`
  400. * Frame is badly formatted; or frame type is unknown; or
  401. * |payloadlen| is 0.
  402. */
  403. ngtcp2_ssize ngtcp2_pkt_decode_frame(ngtcp2_frame *dest, const uint8_t *payload,
  404. size_t payloadlen);
  405. /**
  406. * @function
  407. *
  408. * `ngtcp2_pkt_encode_frame` encodes a frame |fr| into the buffer
  409. * pointed by |out| of length |outlen|.
  410. *
  411. * This function returns the number of bytes written to the buffer, or
  412. * one of the following negative error codes:
  413. *
  414. * :enum:`NGTCP2_ERR_NOBUF`
  415. * Buffer does not have enough capacity to write a frame.
  416. */
  417. ngtcp2_ssize ngtcp2_pkt_encode_frame(uint8_t *out, size_t outlen,
  418. ngtcp2_frame *fr);
  419. /*
  420. * ngtcp2_pkt_decode_version_negotiation decodes Version Negotiation
  421. * packet payload |payload| of length |payloadlen|, and stores the
  422. * result in |dest|. |dest| must have enough capacity to store the
  423. * result. |payloadlen| also must be a multiple of sizeof(uint32_t).
  424. *
  425. * This function returns the number of versions written in |dest|.
  426. */
  427. size_t ngtcp2_pkt_decode_version_negotiation(uint32_t *dest,
  428. const uint8_t *payload,
  429. size_t payloadlen);
  430. /*
  431. * ngtcp2_pkt_decode_stateless_reset decodes Stateless Reset payload
  432. * |payload| of length |payloadlen|. The |payload| must start with
  433. * Stateless Reset Token.
  434. *
  435. * This function returns 0 if it succeeds, or one of the following
  436. * negative error codes:
  437. *
  438. * NGTCP2_ERR_INVALID_ARGUMENT
  439. * Payloadlen is too short.
  440. */
  441. int ngtcp2_pkt_decode_stateless_reset(ngtcp2_pkt_stateless_reset *sr,
  442. const uint8_t *payload,
  443. size_t payloadlen);
  444. /*
  445. * ngtcp2_pkt_decode_retry decodes Retry packet payload |payload| of
  446. * length |payloadlen|. The |payload| must start with Retry token
  447. * field.
  448. *
  449. * This function returns 0 if it succeeds, or one of the following
  450. * negative error codes:
  451. *
  452. * NGTCP2_ERR_INVALID_ARGUMENT
  453. * Payloadlen is too short.
  454. */
  455. int ngtcp2_pkt_decode_retry(ngtcp2_pkt_retry *dest, const uint8_t *payload,
  456. size_t payloadlen);
  457. /*
  458. * ngtcp2_pkt_decode_stream_frame decodes STREAM frame from |payload|
  459. * of length |payloadlen|. The result is stored in the object pointed
  460. * by |dest|. STREAM frame must start at payload[0]. This function
  461. * finishes when it decodes one STREAM frame, and returns the exact
  462. * number of bytes read to decode a frame if it succeeds, or one of
  463. * the following negative error codes:
  464. *
  465. * NGTCP2_ERR_FRAME_ENCODING
  466. * Payload is too short to include STREAM frame.
  467. */
  468. ngtcp2_ssize ngtcp2_pkt_decode_stream_frame(ngtcp2_stream *dest,
  469. const uint8_t *payload,
  470. size_t payloadlen);
  471. /*
  472. * ngtcp2_pkt_decode_ack_frame decodes ACK frame from |payload| of
  473. * length |payloadlen|. The result is stored in the object pointed by
  474. * |dest|. ACK frame must start at payload[0]. This function
  475. * finishes when it decodes one ACK frame, and returns the exact
  476. * number of bytes read to decode a frame if it succeeds, or one of
  477. * the following negative error codes:
  478. *
  479. * NGTCP2_ERR_FRAME_ENCODING
  480. * Payload is too short to include ACK frame.
  481. */
  482. ngtcp2_ssize ngtcp2_pkt_decode_ack_frame(ngtcp2_ack *dest,
  483. const uint8_t *payload,
  484. size_t payloadlen);
  485. /*
  486. * ngtcp2_pkt_decode_padding_frame decodes contiguous PADDING frames
  487. * from |payload| of length |payloadlen|. It continues to parse
  488. * frames as long as the frame type is PADDING. It finishes when it
  489. * encounters the frame type which is not PADDING, or all input data
  490. * is read. The first byte (payload[0]) must be NGTCP2_FRAME_PADDING.
  491. * This function returns the exact number of bytes read to decode
  492. * PADDING frames.
  493. */
  494. ngtcp2_ssize ngtcp2_pkt_decode_padding_frame(ngtcp2_padding *dest,
  495. const uint8_t *payload,
  496. size_t payloadlen);
  497. /*
  498. * ngtcp2_pkt_decode_reset_stream_frame decodes RESET_STREAM frame
  499. * from |payload| of length |payloadlen|. The result is stored in the
  500. * object pointed by |dest|. RESET_STREAM frame must start at
  501. * payload[0]. This function finishes when it decodes one
  502. * RESET_STREAM frame, and returns the exact number of bytes read to
  503. * decode a frame if it succeeds, or one of the following negative
  504. * error codes:
  505. *
  506. * NGTCP2_ERR_FRAME_ENCODING
  507. * Payload is too short to include RESET_STREAM frame.
  508. */
  509. ngtcp2_ssize ngtcp2_pkt_decode_reset_stream_frame(ngtcp2_reset_stream *dest,
  510. const uint8_t *payload,
  511. size_t payloadlen);
  512. /*
  513. * ngtcp2_pkt_decode_connection_close_frame decodes CONNECTION_CLOSE
  514. * frame from |payload| of length |payloadlen|. The result is stored
  515. * in the object pointed by |dest|. CONNECTION_CLOSE frame must start
  516. * at payload[0]. This function finishes when it decodes one
  517. * CONNECTION_CLOSE frame, and returns the exact number of bytes read
  518. * to decode a frame if it succeeds, or one of the following negative
  519. * error codes:
  520. *
  521. * NGTCP2_ERR_FRAME_ENCODING
  522. * Payload is too short to include CONNECTION_CLOSE frame.
  523. */
  524. ngtcp2_ssize ngtcp2_pkt_decode_connection_close_frame(
  525. ngtcp2_connection_close *dest, const uint8_t *payload, size_t payloadlen);
  526. /*
  527. * ngtcp2_pkt_decode_max_data_frame decodes MAX_DATA frame from
  528. * |payload| of length |payloadlen|. The result is stored in the
  529. * object pointed by |dest|. MAX_DATA frame must start at payload[0].
  530. * This function finishes when it decodes one MAX_DATA frame, and
  531. * returns the exact number of bytes read to decode a frame if it
  532. * succeeds, or one of the following negative error codes:
  533. *
  534. * NGTCP2_ERR_FRAME_ENCODING
  535. * Payload is too short to include MAX_DATA frame.
  536. */
  537. ngtcp2_ssize ngtcp2_pkt_decode_max_data_frame(ngtcp2_max_data *dest,
  538. const uint8_t *payload,
  539. size_t payloadlen);
  540. /*
  541. * ngtcp2_pkt_decode_max_stream_data_frame decodes MAX_STREAM_DATA
  542. * frame from |payload| of length |payloadlen|. The result is stored
  543. * in the object pointed by |dest|. MAX_STREAM_DATA frame must start
  544. * at payload[0]. This function finishes when it decodes one
  545. * MAX_STREAM_DATA frame, and returns the exact number of bytes read
  546. * to decode a frame if it succeeds, or one of the following negative
  547. * error codes:
  548. *
  549. * NGTCP2_ERR_FRAME_ENCODING
  550. * Payload is too short to include MAX_STREAM_DATA frame.
  551. */
  552. ngtcp2_ssize ngtcp2_pkt_decode_max_stream_data_frame(
  553. ngtcp2_max_stream_data *dest, const uint8_t *payload, size_t payloadlen);
  554. /*
  555. * ngtcp2_pkt_decode_max_streams_frame decodes MAX_STREAMS frame from
  556. * |payload| of length |payloadlen|. The result is stored in the
  557. * object pointed by |dest|. MAX_STREAMS frame must start at
  558. * payload[0]. This function finishes when it decodes one MAX_STREAMS
  559. * frame, and returns the exact number of bytes read to decode a frame
  560. * if it succeeds, or one of the following negative error codes:
  561. *
  562. * NGTCP2_ERR_FRAME_ENCODING
  563. * Payload is too short to include MAX_STREAMS frame.
  564. */
  565. ngtcp2_ssize ngtcp2_pkt_decode_max_streams_frame(ngtcp2_max_streams *dest,
  566. const uint8_t *payload,
  567. size_t payloadlen);
  568. /*
  569. * ngtcp2_pkt_decode_ping_frame decodes PING frame from |payload| of
  570. * length |payloadlen|. The result is stored in the object pointed by
  571. * |dest|. PING frame must start at payload[0]. This function
  572. * finishes when it decodes one PING frame, and returns the exact
  573. * number of bytes read to decode a frame.
  574. */
  575. ngtcp2_ssize ngtcp2_pkt_decode_ping_frame(ngtcp2_ping *dest,
  576. const uint8_t *payload,
  577. size_t payloadlen);
  578. /*
  579. * ngtcp2_pkt_decode_data_blocked_frame decodes DATA_BLOCKED frame
  580. * from |payload| of length |payloadlen|. The result is stored in the
  581. * object pointed by |dest|. DATA_BLOCKED frame must start at
  582. * payload[0]. This function finishes when it decodes one
  583. * DATA_BLOCKED frame, and returns the exact number of bytes read to
  584. * decode a frame if it succeeds, or one of the following negative
  585. * error codes:
  586. *
  587. * NGTCP2_ERR_FRAME_ENCODING
  588. * Payload is too short to include DATA_BLOCKED frame.
  589. */
  590. ngtcp2_ssize ngtcp2_pkt_decode_data_blocked_frame(ngtcp2_data_blocked *dest,
  591. const uint8_t *payload,
  592. size_t payloadlen);
  593. /*
  594. * ngtcp2_pkt_decode_stream_data_blocked_frame decodes
  595. * STREAM_DATA_BLOCKED frame from |payload| of length |payloadlen|.
  596. * The result is stored in the object pointed by |dest|.
  597. * STREAM_DATA_BLOCKED frame must start at payload[0]. This function
  598. * finishes when it decodes one STREAM_DATA_BLOCKED frame, and returns
  599. * the exact number of bytes read to decode a frame if it succeeds, or
  600. * one of the following negative error codes:
  601. *
  602. * NGTCP2_ERR_FRAME_ENCODING
  603. * Payload is too short to include STREAM_DATA_BLOCKED frame.
  604. */
  605. ngtcp2_ssize ngtcp2_pkt_decode_stream_data_blocked_frame(
  606. ngtcp2_stream_data_blocked *dest, const uint8_t *payload, size_t payloadlen);
  607. /*
  608. * ngtcp2_pkt_decode_streams_blocked_frame decodes STREAMS_BLOCKED
  609. * frame from |payload| of length |payloadlen|. The result is stored
  610. * in the object pointed by |dest|. STREAMS_BLOCKED frame must start
  611. * at payload[0]. This function finishes when it decodes one
  612. * STREAMS_BLOCKED frame, and returns the exact number of bytes read
  613. * to decode a frame if it succeeds, or one of the following negative
  614. * error codes:
  615. *
  616. * NGTCP2_ERR_FRAME_ENCODING
  617. * Payload is too short to include STREAMS_BLOCKED frame.
  618. */
  619. ngtcp2_ssize ngtcp2_pkt_decode_streams_blocked_frame(
  620. ngtcp2_streams_blocked *dest, const uint8_t *payload, size_t payloadlen);
  621. /*
  622. * ngtcp2_pkt_decode_new_connection_id_frame decodes NEW_CONNECTION_ID
  623. * frame from |payload| of length |payloadlen|. The result is stored
  624. * in the object pointed by |dest|. NEW_CONNECTION_ID frame must
  625. * start at payload[0]. This function finishes when it decodes one
  626. * NEW_CONNECTION_ID frame, and returns the exact number of bytes read
  627. * to decode a frame if it succeeds, or one of the following negative
  628. * error codes:
  629. *
  630. * NGTCP2_ERR_FRAME_ENCODING
  631. * Payload is too short to include NEW_CONNECTION_ID frame; or the
  632. * length of Connection ID is strictly less than NGTCP2_MIN_CIDLEN
  633. * or greater than NGTCP2_MAX_CIDLEN.
  634. */
  635. ngtcp2_ssize ngtcp2_pkt_decode_new_connection_id_frame(
  636. ngtcp2_new_connection_id *dest, const uint8_t *payload, size_t payloadlen);
  637. /*
  638. * ngtcp2_pkt_decode_stop_sending_frame decodes STOP_SENDING frame
  639. * from |payload| of length |payloadlen|. The result is stored in the
  640. * object pointed by |dest|. STOP_SENDING frame must start at
  641. * payload[0]. This function finishes when it decodes one
  642. * STOP_SENDING frame, and returns the exact number of bytes read to
  643. * decode a frame if it succeeds, or one of the following negative
  644. * error codes:
  645. *
  646. * NGTCP2_ERR_FRAME_ENCODING
  647. * Payload is too short to include STOP_SENDING frame.
  648. */
  649. ngtcp2_ssize ngtcp2_pkt_decode_stop_sending_frame(ngtcp2_stop_sending *dest,
  650. const uint8_t *payload,
  651. size_t payloadlen);
  652. /*
  653. * ngtcp2_pkt_decode_path_challenge_frame decodes PATH_CHALLENGE frame
  654. * from |payload| of length |payloadlen|. The result is stored in the
  655. * object pointed by |dest|. PATH_CHALLENGE frame must start at
  656. * payload[0]. This function finishes when it decodes one
  657. * PATH_CHALLENGE frame, and returns the exact number of bytes read to
  658. * decode a frame if it succeeds, or one of the following negative
  659. * error codes:
  660. *
  661. * NGTCP2_ERR_FRAME_ENCODING
  662. * Payload is too short to include PATH_CHALLENGE frame.
  663. */
  664. ngtcp2_ssize ngtcp2_pkt_decode_path_challenge_frame(ngtcp2_path_challenge *dest,
  665. const uint8_t *payload,
  666. size_t payloadlen);
  667. /*
  668. * ngtcp2_pkt_decode_path_response_frame decodes PATH_RESPONSE frame
  669. * from |payload| of length |payloadlen|. The result is stored in the
  670. * object pointed by |dest|. PATH_RESPONSE frame must start at
  671. * payload[0]. This function finishes when it decodes one
  672. * PATH_RESPONSE frame, and returns the exact number of bytes read to
  673. * decode a frame if it succeeds, or one of the following negative
  674. * error codes:
  675. *
  676. * NGTCP2_ERR_FRAME_ENCODING
  677. * Payload is too short to include PATH_RESPONSE frame.
  678. */
  679. ngtcp2_ssize ngtcp2_pkt_decode_path_response_frame(ngtcp2_path_response *dest,
  680. const uint8_t *payload,
  681. size_t payloadlen);
  682. /*
  683. * ngtcp2_pkt_decode_crypto_frame decodes CRYPTO frame from |payload|
  684. * of length |payloadlen|. The result is stored in the object pointed
  685. * by |dest|. CRYPTO frame must start at payload[0]. This function
  686. * finishes when it decodes one CRYPTO frame, and returns the exact
  687. * number of bytes read to decode a frame if it succeeds, or one of
  688. * the following negative error codes:
  689. *
  690. * NGTCP2_ERR_FRAME_ENCODING
  691. * Payload is too short to include CRYPTO frame.
  692. */
  693. ngtcp2_ssize ngtcp2_pkt_decode_crypto_frame(ngtcp2_stream *dest,
  694. const uint8_t *payload,
  695. size_t payloadlen);
  696. /*
  697. * ngtcp2_pkt_decode_new_token_frame decodes NEW_TOKEN frame from
  698. * |payload| of length |payloadlen|. The result is stored in the
  699. * object pointed by |dest|. NEW_TOKEN frame must start at
  700. * payload[0]. This function finishes when it decodes one NEW_TOKEN
  701. * frame, and returns the exact number of bytes read to decode a frame
  702. * if it succeeds, or one of the following negative error codes:
  703. *
  704. * NGTCP2_ERR_FRAME_ENCODING
  705. * Payload is too short to include NEW_TOKEN frame.
  706. */
  707. ngtcp2_ssize ngtcp2_pkt_decode_new_token_frame(ngtcp2_new_token *dest,
  708. const uint8_t *payload,
  709. size_t payloadlen);
  710. /*
  711. * ngtcp2_pkt_decode_retire_connection_id_frame decodes
  712. * RETIRE_CONNECTION_ID frame from |payload| of length |payloadlen|.
  713. * The result is stored in the object pointed by |dest|.
  714. * RETIRE_CONNECTION_ID frame must start at payload[0]. This function
  715. * finishes when it decodes one RETIRE_CONNECTION_ID frame, and
  716. * returns the exact number of bytes read to decode a frame if it
  717. * succeeds, or one of the following negative error codes:
  718. *
  719. * NGTCP2_ERR_FRAME_ENCODING
  720. * Payload is too short to include RETIRE_CONNECTION_ID frame.
  721. */
  722. ngtcp2_ssize ngtcp2_pkt_decode_retire_connection_id_frame(
  723. ngtcp2_retire_connection_id *dest, const uint8_t *payload, size_t payloadlen);
  724. /*
  725. * ngtcp2_pkt_decode_handshake_done_frame decodes HANDSHAKE_DONE frame
  726. * from |payload| of length |payloadlen|. The result is stored in the
  727. * object pointed by |dest|. HANDSHAKE_DONE frame must start at
  728. * payload[0]. This function finishes when it decodes one
  729. * HANDSHAKE_DONE frame, and returns the exact number of bytes read to
  730. * decode a frame.
  731. */
  732. ngtcp2_ssize ngtcp2_pkt_decode_handshake_done_frame(ngtcp2_handshake_done *dest,
  733. const uint8_t *payload,
  734. size_t payloadlen);
  735. /*
  736. * ngtcp2_pkt_decode_datagram_frame decodes DATAGRAM frame from
  737. * |payload| of length |payloadlen|. The result is stored in the
  738. * object pointed by |dest|. DATAGRAM frame must start at payload[0].
  739. * This function finishes when it decodes one DATAGRAM frame, and
  740. * returns the exact number of bytes read to decode a frame if it
  741. * succeeds, or one of the following negative error codes:
  742. *
  743. * NGTCP2_ERR_FRAME_ENCODING
  744. * Payload is too short to include DATAGRAM frame.
  745. */
  746. ngtcp2_ssize ngtcp2_pkt_decode_datagram_frame(ngtcp2_datagram *dest,
  747. const uint8_t *payload,
  748. size_t payloadlen);
  749. /*
  750. * ngtcp2_pkt_encode_stream_frame encodes STREAM frame |fr| into the
  751. * buffer pointed by |out| of length |outlen|.
  752. *
  753. * This function assigns <the serialized frame type> &
  754. * ~NGTCP2_FRAME_STREAM to fr->flags.
  755. *
  756. * This function returns the number of bytes written if it succeeds,
  757. * or one of the following negative error codes:
  758. *
  759. * NGTCP2_ERR_NOBUF
  760. * Buffer does not have enough capacity to write a frame.
  761. */
  762. ngtcp2_ssize ngtcp2_pkt_encode_stream_frame(uint8_t *out, size_t outlen,
  763. ngtcp2_stream *fr);
  764. /*
  765. * ngtcp2_pkt_encode_ack_frame encodes ACK frame |fr| into the buffer
  766. * pointed by |out| of length |outlen|.
  767. *
  768. * This function returns the number of bytes written if it succeeds,
  769. * or one of the following negative error codes:
  770. *
  771. * NGTCP2_ERR_NOBUF
  772. * Buffer does not have enough capacity to write a frame.
  773. */
  774. ngtcp2_ssize ngtcp2_pkt_encode_ack_frame(uint8_t *out, size_t outlen,
  775. const ngtcp2_ack *fr);
  776. /*
  777. * ngtcp2_pkt_encode_padding_frame encodes PADDING frame |fr| into the
  778. * buffer pointed by |out| of length |outlen|.
  779. *
  780. * This function encodes consecutive fr->len PADDING frames.
  781. *
  782. * This function returns the number of bytes written if it succeeds,
  783. * or one of the following negative error codes:
  784. *
  785. * NGTCP2_ERR_NOBUF
  786. * Buffer does not have enough capacity to write frame(s).
  787. */
  788. ngtcp2_ssize ngtcp2_pkt_encode_padding_frame(uint8_t *out, size_t outlen,
  789. const ngtcp2_padding *fr);
  790. /*
  791. * ngtcp2_pkt_encode_reset_stream_frame encodes RESET_STREAM frame
  792. * |fr| into the buffer pointed by |out| of length |buflen|.
  793. *
  794. * This function returns the number of bytes written if it succeeds,
  795. * or one of the following negative error codes:
  796. *
  797. * NGTCP2_ERR_NOBUF
  798. * Buffer does not have enough capacity to write a frame.
  799. */
  800. ngtcp2_ssize
  801. ngtcp2_pkt_encode_reset_stream_frame(uint8_t *out, size_t outlen,
  802. const ngtcp2_reset_stream *fr);
  803. /*
  804. * ngtcp2_pkt_encode_connection_close_frame encodes CONNECTION_CLOSE
  805. * frame |fr| into the buffer pointed by |out| of length |outlen|.
  806. *
  807. * This function returns the number of bytes written if it succeeds,
  808. * or one of the following negative error codes:
  809. *
  810. * NGTCP2_ERR_NOBUF
  811. * Buffer does not have enough capacity to write a frame.
  812. */
  813. ngtcp2_ssize
  814. ngtcp2_pkt_encode_connection_close_frame(uint8_t *out, size_t outlen,
  815. const ngtcp2_connection_close *fr);
  816. /*
  817. * ngtcp2_pkt_encode_max_data_frame encodes MAX_DATA frame |fr| into
  818. * the buffer pointed by |out| of length |outlen|.
  819. *
  820. * This function returns the number of bytes written if it succeeds,
  821. * or one of the following negative error codes:
  822. *
  823. * NGTCP2_ERR_NOBUF
  824. * Buffer does not have enough capacity to write a frame.
  825. */
  826. ngtcp2_ssize ngtcp2_pkt_encode_max_data_frame(uint8_t *out, size_t outlen,
  827. const ngtcp2_max_data *fr);
  828. /*
  829. * ngtcp2_pkt_encode_max_stream_data_frame encodes MAX_STREAM_DATA
  830. * frame |fr| into the buffer pointed by |out| of length |outlen|.
  831. *
  832. * This function returns the number of bytes written if it succeeds,
  833. * or one of the following negative error codes:
  834. *
  835. * NGTCP2_ERR_NOBUF
  836. * Buffer does not have enough capacity to write a frame.
  837. */
  838. ngtcp2_ssize
  839. ngtcp2_pkt_encode_max_stream_data_frame(uint8_t *out, size_t outlen,
  840. const ngtcp2_max_stream_data *fr);
  841. /*
  842. * ngtcp2_pkt_encode_max_streams_frame encodes MAX_STREAMS
  843. * frame |fr| into the buffer pointed by |out| of length |outlen|.
  844. *
  845. * This function returns the number of bytes written if it succeeds,
  846. * or one of the following negative error codes:
  847. *
  848. * NGTCP2_ERR_NOBUF
  849. * Buffer does not have enough capacity to write a frame.
  850. */
  851. ngtcp2_ssize ngtcp2_pkt_encode_max_streams_frame(uint8_t *out, size_t outlen,
  852. const ngtcp2_max_streams *fr);
  853. /*
  854. * ngtcp2_pkt_encode_ping_frame encodes PING frame |fr| into the
  855. * buffer pointed by |out| of length |outlen|.
  856. *
  857. * This function returns the number of bytes written if it succeeds,
  858. * or one of the following negative error codes:
  859. *
  860. * NGTCP2_ERR_NOBUF
  861. * Buffer does not have enough capacity to write a frame.
  862. */
  863. ngtcp2_ssize ngtcp2_pkt_encode_ping_frame(uint8_t *out, size_t outlen,
  864. const ngtcp2_ping *fr);
  865. /*
  866. * ngtcp2_pkt_encode_data_blocked_frame encodes DATA_BLOCKED frame
  867. * |fr| into the buffer pointed by |out| of length |outlen|.
  868. *
  869. * This function returns the number of bytes written if it succeeds,
  870. * or one of the following negative error codes:
  871. *
  872. * NGTCP2_ERR_NOBUF
  873. * Buffer does not have enough capacity to write a frame.
  874. */
  875. ngtcp2_ssize
  876. ngtcp2_pkt_encode_data_blocked_frame(uint8_t *out, size_t outlen,
  877. const ngtcp2_data_blocked *fr);
  878. /*
  879. * ngtcp2_pkt_encode_stream_data_blocked_frame encodes
  880. * STREAM_DATA_BLOCKED frame |fr| into the buffer pointed by |out| of
  881. * length |outlen|.
  882. *
  883. * This function returns the number of bytes written if it succeeds,
  884. * or one of the following negative error codes:
  885. *
  886. * NGTCP2_ERR_NOBUF
  887. * Buffer does not have enough capacity to write a frame.
  888. */
  889. ngtcp2_ssize ngtcp2_pkt_encode_stream_data_blocked_frame(
  890. uint8_t *out, size_t outlen, const ngtcp2_stream_data_blocked *fr);
  891. /*
  892. * ngtcp2_pkt_encode_streams_blocked_frame encodes STREAMS_BLOCKED
  893. * frame |fr| into the buffer pointed by |out| of length |outlen|.
  894. *
  895. * This function returns the number of bytes written if it succeeds,
  896. * or one of the following negative error codes:
  897. *
  898. * NGTCP2_ERR_NOBUF
  899. * Buffer does not have enough capacity to write a frame.
  900. */
  901. ngtcp2_ssize
  902. ngtcp2_pkt_encode_streams_blocked_frame(uint8_t *out, size_t outlen,
  903. const ngtcp2_streams_blocked *fr);
  904. /*
  905. * ngtcp2_pkt_encode_new_connection_id_frame encodes NEW_CONNECTION_ID
  906. * frame |fr| into the buffer pointed by |out| of length |outlen|.
  907. *
  908. * This function returns the number of bytes written if it succeeds,
  909. * or one of the following negative error codes:
  910. *
  911. * NGTCP2_ERR_NOBUF
  912. * Buffer does not have enough capacity to write a frame.
  913. */
  914. ngtcp2_ssize
  915. ngtcp2_pkt_encode_new_connection_id_frame(uint8_t *out, size_t outlen,
  916. const ngtcp2_new_connection_id *fr);
  917. /*
  918. * ngtcp2_pkt_encode_stop_sending_frame encodes STOP_SENDING frame
  919. * |fr| into the buffer pointed by |out| of length |outlen|.
  920. *
  921. * This function returns the number of bytes written if it succeeds,
  922. * or one of the following negative error codes:
  923. *
  924. * NGTCP2_ERR_NOBUF
  925. * Buffer does not have enough capacity to write a frame.
  926. */
  927. ngtcp2_ssize
  928. ngtcp2_pkt_encode_stop_sending_frame(uint8_t *out, size_t outlen,
  929. const ngtcp2_stop_sending *fr);
  930. /*
  931. * ngtcp2_pkt_encode_path_challenge_frame encodes PATH_CHALLENGE frame
  932. * |fr| into the buffer pointed by |out| of length |outlen|.
  933. *
  934. * This function returns the number of bytes written if it succeeds,
  935. * or one of the following negative error codes:
  936. *
  937. * NGTCP2_ERR_NOBUF
  938. * Buffer does not have enough capacity to write a frame.
  939. */
  940. ngtcp2_ssize
  941. ngtcp2_pkt_encode_path_challenge_frame(uint8_t *out, size_t outlen,
  942. const ngtcp2_path_challenge *fr);
  943. /*
  944. * ngtcp2_pkt_encode_path_response_frame encodes PATH_RESPONSE frame
  945. * |fr| into the buffer pointed by |out| of length |outlen|.
  946. *
  947. * This function returns the number of bytes written if it succeeds,
  948. * or one of the following negative error codes:
  949. *
  950. * NGTCP2_ERR_NOBUF
  951. * Buffer does not have enough capacity to write a frame.
  952. */
  953. ngtcp2_ssize
  954. ngtcp2_pkt_encode_path_response_frame(uint8_t *out, size_t outlen,
  955. const ngtcp2_path_response *fr);
  956. /*
  957. * ngtcp2_pkt_encode_crypto_frame encodes CRYPTO frame |fr| into the
  958. * buffer pointed by |out| of length |outlen|.
  959. *
  960. * This function returns the number of bytes written if it succeeds,
  961. * or one of the following negative error codes:
  962. *
  963. * NGTCP2_ERR_NOBUF
  964. * Buffer does not have enough capacity to write a frame.
  965. */
  966. ngtcp2_ssize ngtcp2_pkt_encode_crypto_frame(uint8_t *out, size_t outlen,
  967. const ngtcp2_stream *fr);
  968. /*
  969. * ngtcp2_pkt_encode_new_token_frame encodes NEW_TOKEN frame |fr| into
  970. * the buffer pointed by |out| of length |outlen|.
  971. *
  972. * This function returns the number of bytes written if it succeeds,
  973. * or one of the following negative error codes:
  974. *
  975. * NGTCP2_ERR_NOBUF
  976. * Buffer does not have enough capacity to write a frame.
  977. */
  978. ngtcp2_ssize ngtcp2_pkt_encode_new_token_frame(uint8_t *out, size_t outlen,
  979. const ngtcp2_new_token *fr);
  980. /*
  981. * ngtcp2_pkt_encode_retire_connection_id_frame encodes
  982. * RETIRE_CONNECTION_ID frame |fr| into the buffer pointed by |out| of
  983. * length |outlen|.
  984. *
  985. * This function returns the number of bytes written if it succeeds,
  986. * or one of the following negative error codes:
  987. *
  988. * NGTCP2_ERR_NOBUF
  989. * Buffer does not have enough capacity to write a frame.
  990. */
  991. ngtcp2_ssize ngtcp2_pkt_encode_retire_connection_id_frame(
  992. uint8_t *out, size_t outlen, const ngtcp2_retire_connection_id *fr);
  993. /*
  994. * ngtcp2_pkt_encode_handshake_done_frame encodes HANDSHAKE_DONE frame
  995. * |fr| into the buffer pointed by |out| of length |outlen|.
  996. *
  997. * This function returns the number of bytes written if it succeeds,
  998. * or one of the following negative error codes:
  999. *
  1000. * NGTCP2_ERR_NOBUF
  1001. * Buffer does not have enough capacity to write a frame.
  1002. */
  1003. ngtcp2_ssize
  1004. ngtcp2_pkt_encode_handshake_done_frame(uint8_t *out, size_t outlen,
  1005. const ngtcp2_handshake_done *fr);
  1006. /*
  1007. * ngtcp2_pkt_encode_datagram_frame encodes DATAGRAM frame |fr| into
  1008. * the buffer pointed by |out| of length |outlen|.
  1009. *
  1010. * This function returns the number of bytes written if it succeeds,
  1011. * or one of the following negative error codes:
  1012. *
  1013. * NGTCP2_ERR_NOBUF
  1014. * Buffer does not have enough capacity to write a frame.
  1015. */
  1016. ngtcp2_ssize ngtcp2_pkt_encode_datagram_frame(uint8_t *out, size_t outlen,
  1017. const ngtcp2_datagram *fr);
  1018. /*
  1019. * ngtcp2_pkt_adjust_pkt_num finds the full 62 bits packet number for
  1020. * |pkt_num|, which is encoded in |pkt_numlen| bytes. The
  1021. * |max_pkt_num| is the highest successfully authenticated packet
  1022. * number.
  1023. */
  1024. int64_t ngtcp2_pkt_adjust_pkt_num(int64_t max_pkt_num, int64_t pkt_num,
  1025. size_t pkt_numlen);
  1026. /*
  1027. * ngtcp2_pkt_validate_ack verifies whether |fr| is malformed or not.
  1028. * |min_pkt_num| is the minimum packet number that a local endpoint
  1029. * sends. It is an error to receive acknowledgements for a packet
  1030. * less than |min_pkt_num|.
  1031. *
  1032. * This function returns 0 if it succeeds, or one of the following
  1033. * negative error codes:
  1034. *
  1035. * NGTCP2_ERR_ACK_FRAME
  1036. * ACK frame is malformed
  1037. * NGTCP2_ERR_PROTO
  1038. * |fr| contains a packet number less than |min_pkt_num|.
  1039. */
  1040. int ngtcp2_pkt_validate_ack(const ngtcp2_ack *fr, int64_t min_pkt_num);
  1041. /*
  1042. * ngtcp2_pkt_stream_max_datalen returns the maximum number of bytes
  1043. * which can be sent for stream denoted by |stream_id|. |offset| is
  1044. * an offset within the stream. |len| is the estimated number of
  1045. * bytes to send. |left| is the size of buffer. If |left| is too
  1046. * small to write STREAM frame, this function returns (size_t)-1.
  1047. */
  1048. size_t ngtcp2_pkt_stream_max_datalen(int64_t stream_id, uint64_t offset,
  1049. uint64_t len, size_t left);
  1050. /*
  1051. * ngtcp2_pkt_crypto_max_datalen returns the maximum number of bytes
  1052. * which can be sent for crypto stream. |offset| is an offset within
  1053. * the crypto stream. |len| is the estimated number of bytes to send.
  1054. * |left| is the size of buffer. If |left| is too small to write
  1055. * CRYPTO frame, this function returns (size_t)-1.
  1056. */
  1057. size_t ngtcp2_pkt_crypto_max_datalen(uint64_t offset, size_t len, size_t left);
  1058. /*
  1059. * ngtcp2_pkt_datagram_framelen returns the length of DATAGRAM frame
  1060. * to encode |len| bytes of data.
  1061. */
  1062. size_t ngtcp2_pkt_datagram_framelen(size_t len);
  1063. /*
  1064. * ngtcp2_pkt_verify_reserved_bits verifies that the first byte |c| of
  1065. * the packet header has the correct reserved bits.
  1066. *
  1067. * This function returns 0 if it succeeds, or one of the following
  1068. * negative error codes:
  1069. *
  1070. * NGTCP2_ERR_PROTO
  1071. * Reserved bits has wrong value.
  1072. */
  1073. int ngtcp2_pkt_verify_reserved_bits(uint8_t c);
  1074. /*
  1075. * ngtcp2_pkt_encode_pseudo_retry encodes Retry pseudo-packet in the
  1076. * buffer pointed by |dest| of length |destlen|.
  1077. *
  1078. * This function returns 0 if it succeeds, or one of the following
  1079. * negative error codes:
  1080. *
  1081. * NGTCP2_ERR_BUF
  1082. * Buffer is too short.
  1083. */
  1084. ngtcp2_ssize ngtcp2_pkt_encode_pseudo_retry(
  1085. uint8_t *dest, size_t destlen, const ngtcp2_pkt_hd *hd, uint8_t unused,
  1086. const ngtcp2_cid *odcid, const uint8_t *token, size_t tokenlen);
  1087. /*
  1088. * ngtcp2_pkt_verify_retry_tag verifies Retry packet. The buffer
  1089. * pointed by |pkt| of length |pktlen| must contain Retry packet
  1090. * including packet header. The odcid and tag fields of |retry| must
  1091. * be specified. |aead| must be AEAD_AES_128_GCM.
  1092. *
  1093. * This function returns 0 if it succeeds, or one of the following
  1094. * negative error codes:
  1095. *
  1096. * NGTCP2_ERR_PROTO
  1097. * Verification failed.
  1098. */
  1099. int ngtcp2_pkt_verify_retry_tag(uint32_t version, const ngtcp2_pkt_retry *retry,
  1100. const uint8_t *pkt, size_t pktlen,
  1101. ngtcp2_encrypt encrypt,
  1102. const ngtcp2_crypto_aead *aead,
  1103. const ngtcp2_crypto_aead_ctx *aead_ctx);
  1104. /*
  1105. * ngtcp2_pkt_versioned_type returns versioned packet type for a
  1106. * version |version| that corresponds to the version-independent
  1107. * |pkt_type|.
  1108. */
  1109. uint8_t ngtcp2_pkt_versioned_type(uint32_t version, uint32_t pkt_type);
  1110. /**
  1111. * @function
  1112. *
  1113. * `ngtcp2_pkt_get_type_long` returns the version-independent long
  1114. * packet type. |version| is the QUIC version. |c| is the first byte
  1115. * of Long packet header. If |version| is not supported by the
  1116. * library, it returns 0.
  1117. */
  1118. uint8_t ngtcp2_pkt_get_type_long(uint32_t version, uint8_t c);
  1119. #endif /* !defined(NGTCP2_PKT_H) */