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