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