• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ngtcp2
3  *
4  * Copyright (c) 2017 ngtcp2 contributors
5  * Copyright (c) 2017 nghttp2 contributors
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 #ifndef NGTCP2_H
27 #define NGTCP2_H
28 
29 /* Define WIN32 when build target is Win32 API (borrowed from
30    libcurl) */
31 #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
32 #  define WIN32
33 #endif
34 
35 #ifdef _MSC_VER
36 #  pragma warning(push)
37 #  pragma warning(disable : 4324)
38 #endif
39 
40 #include <stdlib.h>
41 #if defined(_MSC_VER) && (_MSC_VER < 1800)
42 /* MSVC < 2013 does not have inttypes.h because it is not C99
43    compliant.  See compiler macros and version number in
44    https://sourceforge.net/p/predef/wiki/Compilers/ */
45 #  include <stdint.h>
46 #else /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
47 #  include <inttypes.h>
48 #endif /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
49 #include <sys/types.h>
50 #include <stdarg.h>
51 #include <stddef.h>
52 
53 #ifndef NGTCP2_USE_GENERIC_SOCKADDR
54 #  ifdef WIN32
55 #    ifndef WIN32_LEAN_AND_MEAN
56 #      define WIN32_LEAN_AND_MEAN
57 #    endif
58 #    include <ws2tcpip.h>
59 #  else
60 #    include <sys/socket.h>
61 #    include <netinet/in.h>
62 #  endif
63 #endif
64 
65 #ifdef AF_INET
66 #  define NGTCP2_AF_INET AF_INET
67 #else
68 #  define NGTCP2_AF_INET 2
69 #endif
70 
71 #ifdef AF_INET6
72 #  define NGTCP2_AF_INET6 AF_INET6
73 #else
74 #  define NGTCP2_AF_INET6 23
75 #  define NGTCP2_USE_GENERIC_IPV6_SOCKADDR
76 #endif
77 
78 #include <ngtcp2/version.h>
79 
80 #ifdef NGTCP2_STATICLIB
81 #  define NGTCP2_EXTERN
82 #elif defined(WIN32)
83 #  ifdef BUILDING_NGTCP2
84 #    define NGTCP2_EXTERN __declspec(dllexport)
85 #  else /* !BUILDING_NGTCP2 */
86 #    define NGTCP2_EXTERN __declspec(dllimport)
87 #  endif /* !BUILDING_NGTCP2 */
88 #else    /* !defined(WIN32) */
89 #  ifdef BUILDING_NGTCP2
90 #    define NGTCP2_EXTERN __attribute__((visibility("default")))
91 #  else /* !BUILDING_NGTCP2 */
92 #    define NGTCP2_EXTERN
93 #  endif /* !BUILDING_NGTCP2 */
94 #endif   /* !defined(WIN32) */
95 
96 #ifdef _MSC_VER
97 #  define NGTCP2_ALIGN(N) __declspec(align(N))
98 #else /* !_MSC_VER */
99 #  define NGTCP2_ALIGN(N) __attribute__((aligned(N)))
100 #endif /* !_MSC_VER */
101 
102 #ifdef __cplusplus
103 extern "C" {
104 #endif
105 
106 /**
107  * @typedef
108  *
109  * :type:`ngtcp2_ssize` is signed counterpart of size_t.
110  */
111 typedef ptrdiff_t ngtcp2_ssize;
112 
113 /**
114  * @functypedef
115  *
116  * :type:`ngtcp2_malloc` is a custom memory allocator to replace
117  * :manpage:`malloc(3)`.  The |user_data| is
118  * :member:`ngtcp2_mem.user_data`.
119  */
120 typedef void *(*ngtcp2_malloc)(size_t size, void *user_data);
121 
122 /**
123  * @functypedef
124  *
125  * :type:`ngtcp2_free` is a custom memory allocator to replace
126  * :manpage:`free(3)`.  The |user_data| is
127  * :member:`ngtcp2_mem.user_data`.
128  */
129 typedef void (*ngtcp2_free)(void *ptr, void *user_data);
130 
131 /**
132  * @functypedef
133  *
134  * :type:`ngtcp2_calloc` is a custom memory allocator to replace
135  * :manpage:`calloc(3)`.  The |user_data| is the
136  * :member:`ngtcp2_mem.user_data`.
137  */
138 typedef void *(*ngtcp2_calloc)(size_t nmemb, size_t size, void *user_data);
139 
140 /**
141  * @functypedef
142  *
143  * :type:`ngtcp2_realloc` is a custom memory allocator to replace
144  * :manpage:`realloc(3)`.  The |user_data| is the
145  * :member:`ngtcp2_mem.user_data`.
146  */
147 typedef void *(*ngtcp2_realloc)(void *ptr, size_t size, void *user_data);
148 
149 /**
150  * @struct
151  *
152  * :type:`ngtcp2_mem` is a custom memory allocator.  The
153  * :member:`user_data` field is passed to each allocator function.
154  * This can be used, for example, to achieve per-connection memory
155  * pool.
156  *
157  * In the following example code, ``my_malloc``, ``my_free``,
158  * ``my_calloc`` and ``my_realloc`` are the replacement of the
159  * standard allocators :manpage:`malloc(3)`, :manpage:`free(3)`,
160  * :manpage:`calloc(3)` and :manpage:`realloc(3)` respectively::
161  *
162  *     void *my_malloc_cb(size_t size, void *user_data) {
163  *       (void)user_data;
164  *       return my_malloc(size);
165  *     }
166  *
167  *     void my_free_cb(void *ptr, void *user_data) {
168  *       (void)user_data;
169  *       my_free(ptr);
170  *     }
171  *
172  *     void *my_calloc_cb(size_t nmemb, size_t size, void *user_data) {
173  *       (void)user_data;
174  *       return my_calloc(nmemb, size);
175  *     }
176  *
177  *     void *my_realloc_cb(void *ptr, size_t size, void *user_data) {
178  *       (void)user_data;
179  *       return my_realloc(ptr, size);
180  *     }
181  *
182  *     void conn_new() {
183  *       ngtcp2_mem mem = {NULL, my_malloc_cb, my_free_cb, my_calloc_cb,
184  *                          my_realloc_cb};
185  *
186  *       ...
187  *     }
188  */
189 typedef struct ngtcp2_mem {
190   /**
191    * :member:`user_data` is an arbitrary user supplied data.  This
192    * is passed to each allocator function.
193    */
194   void *user_data;
195   /**
196    * :member:`malloc` is a custom allocator function to replace
197    * :manpage:`malloc(3)`.
198    */
199   ngtcp2_malloc malloc;
200   /**
201    * :member:`free` is a custom allocator function to replace
202    * :manpage:`free(3)`.
203    */
204   ngtcp2_free free;
205   /**
206    * :member:`calloc` is a custom allocator function to replace
207    * :manpage:`calloc(3)`.
208    */
209   ngtcp2_calloc calloc;
210   /**
211    * :member:`realloc` is a custom allocator function to replace
212    * :manpage:`realloc(3)`.
213    */
214   ngtcp2_realloc realloc;
215 } ngtcp2_mem;
216 
217 /**
218  * @macrosection
219  *
220  * Time related macros
221  */
222 
223 /**
224  * @macro
225  *
226  * :macro:`NGTCP2_SECONDS` is a count of tick which corresponds to 1 second.
227  */
228 #define NGTCP2_SECONDS ((ngtcp2_duration)1000000000ULL)
229 
230 /**
231  * @macro
232  *
233  * :macro:`NGTCP2_MILLISECONDS` is a count of tick which corresponds
234  * to 1 millisecond.
235  */
236 #define NGTCP2_MILLISECONDS ((ngtcp2_duration)1000000ULL)
237 
238 /**
239  * @macro
240  *
241  * :macro:`NGTCP2_MICROSECONDS` is a count of tick which corresponds
242  * to 1 microsecond.
243  */
244 #define NGTCP2_MICROSECONDS ((ngtcp2_duration)1000ULL)
245 
246 /**
247  * @macro
248  *
249  * :macro:`NGTCP2_NANOSECONDS` is a count of tick which corresponds to
250  * 1 nanosecond.
251  */
252 #define NGTCP2_NANOSECONDS ((ngtcp2_duration)1ULL)
253 
254 /**
255  * @macrosection
256  *
257  * QUIC protocol version macros
258  */
259 
260 /**
261  * @macro
262  *
263  * :macro:`NGTCP2_PROTO_VER_V1` is the QUIC version 1.
264  */
265 #define NGTCP2_PROTO_VER_V1 ((uint32_t)0x00000001u)
266 
267 /**
268  * @macro
269  *
270  * :macro:`NGTCP2_PROTO_VER_V2_DRAFT` is the provisional version
271  * number for QUIC version 2 draft.
272  *
273  * https://quicwg.org/quic-v2/draft-ietf-quic-v2.html
274  */
275 #define NGTCP2_PROTO_VER_V2_DRAFT ((uint32_t)0x709a50c4u)
276 
277 /**
278  * @macro
279  *
280  * :macro:`NGTCP2_PROTO_VER_DRAFT_MAX` is the maximum QUIC draft
281  * version that this library supports.
282  */
283 #define NGTCP2_PROTO_VER_DRAFT_MAX 0xff000020u
284 
285 /**
286  * @macro
287  *
288  * :macro:`NGTCP2_PROTO_VER_DRAFT_MIN` is the minimum QUIC draft
289  * version that this library supports.
290  */
291 #define NGTCP2_PROTO_VER_DRAFT_MIN 0xff00001du
292 
293 /**
294  * @macro
295  *
296  * :macro:`NGTCP2_PROTO_VER_MAX` is the highest QUIC version that this
297  * library supports.
298  */
299 #define NGTCP2_PROTO_VER_MAX NGTCP2_PROTO_VER_V1
300 
301 /**
302  * @macro
303  *
304  * :macro:`NGTCP2_PROTO_VER_MIN` is the lowest QUIC version that this
305  * library supports.
306  */
307 #define NGTCP2_PROTO_VER_MIN NGTCP2_PROTO_VER_DRAFT_MIN
308 
309 /**
310  * @macro
311  *
312  * :macro:`NGTCP2_RESERVED_VERSION_MASK` is the bit mask of reserved
313  * version.
314  */
315 #define NGTCP2_RESERVED_VERSION_MASK 0x0a0a0a0au
316 
317 /**
318  * @macrosection
319  *
320  * UDP datagram related macros
321  */
322 
323 /**
324  * @macro
325  *
326  * :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE` is the default maximum UDP
327  * datagram payload size that this endpoint transmits.
328  */
329 #define NGTCP2_MAX_UDP_PAYLOAD_SIZE 1200
330 
331 /**
332  * @macro
333  *
334  * :macro:`NGTCP2_MAX_PMTUD_UDP_PAYLOAD_SIZE` is the maximum UDP
335  * datagram payload size that Path MTU Discovery can discover.
336  */
337 #define NGTCP2_MAX_PMTUD_UDP_PAYLOAD_SIZE 1452
338 
339 /**
340  * @macrosection
341  *
342  * QUIC specific macros
343  */
344 
345 /**
346  * @macro
347  *
348  * :macro:`NGTCP2_MAX_VARINT` is the maximum value which can be
349  * encoded in variable-length integer encoding.
350  */
351 #define NGTCP2_MAX_VARINT ((1ULL << 62) - 1)
352 
353 /**
354  * @macro
355  *
356  * :macro:`NGTCP2_STATELESS_RESET_TOKENLEN` is the length of Stateless
357  * Reset Token.
358  */
359 #define NGTCP2_STATELESS_RESET_TOKENLEN 16
360 
361 /**
362  * @macro
363  *
364  * :macro:`NGTCP2_MIN_STATELESS_RESET_RANDLEN` is the minimum length
365  * of random bytes (Unpredictable Bits) in Stateless Reset packet
366  */
367 #define NGTCP2_MIN_STATELESS_RESET_RANDLEN 5
368 
369 /**
370  * @macro
371  *
372  * :macro:`NGTCP2_PATH_CHALLENGE_DATALEN` is the length of
373  * PATH_CHALLENGE data.
374  */
375 #define NGTCP2_PATH_CHALLENGE_DATALEN 8
376 
377 /**
378  * @macro
379  *
380  * :macro:`NGTCP2_RETRY_KEY_DRAFT` is an encryption key to create
381  * integrity tag of Retry packet.  It is used for QUIC draft versions.
382  */
383 #define NGTCP2_RETRY_KEY_DRAFT                                                 \
384   "\xcc\xce\x18\x7e\xd0\x9a\x09\xd0\x57\x28\x15\x5a\x6c\xb9\x6b\xe1"
385 
386 /**
387  * @macro
388  *
389  * :macro:`NGTCP2_RETRY_NONCE_DRAFT` is nonce used when generating
390  * integrity tag of Retry packet.  It is used for QUIC draft versions.
391  */
392 #define NGTCP2_RETRY_NONCE_DRAFT                                               \
393   "\xe5\x49\x30\xf9\x7f\x21\x36\xf0\x53\x0a\x8c\x1c"
394 
395 /**
396  * @macro
397  *
398  * :macro:`NGTCP2_RETRY_KEY_V1` is an encryption key to create
399  * integrity tag of Retry packet.  It is used for QUIC v1.
400  */
401 #define NGTCP2_RETRY_KEY_V1                                                    \
402   "\xbe\x0c\x69\x0b\x9f\x66\x57\x5a\x1d\x76\x6b\x54\xe3\x68\xc8\x4e"
403 
404 /**
405  * @macro
406  *
407  * :macro:`NGTCP2_RETRY_NONCE_V1` is nonce used when generating integrity
408  * tag of Retry packet.  It is used for QUIC v1.
409  */
410 #define NGTCP2_RETRY_NONCE_V1 "\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb"
411 
412 /**
413  * @macro
414  *
415  * :macro:`NGTCP2_RETRY_KEY_V2_DRAFT` is an encryption key to create
416  * integrity tag of Retry packet.  It is used for QUIC v2 draft.
417  *
418  * https://quicwg.org/quic-v2/draft-ietf-quic-v2.html
419  */
420 #define NGTCP2_RETRY_KEY_V2_DRAFT                                              \
421   "\xba\x85\x8d\xc7\xb4\x3d\xe5\xdb\xf8\x76\x17\xff\x4a\xb2\x53\xdb"
422 
423 /**
424  * @macro
425  *
426  * :macro:`NGTCP2_RETRY_NONCE_V2_DRAFT` is nonce used when generating
427  * integrity tag of Retry packet.  It is used for QUIC v2 draft.
428  *
429  * https://quicwg.org/quic-v2/draft-ietf-quic-v2.html
430  */
431 #define NGTCP2_RETRY_NONCE_V2_DRAFT                                            \
432   "\x14\x1b\x99\xc2\x39\xb0\x3e\x78\x5d\x6a\x2e\x9f"
433 
434 /**
435  * @macro
436  *
437  * :macro:`NGTCP2_HP_MASKLEN` is the length of header protection mask.
438  */
439 #define NGTCP2_HP_MASKLEN 5
440 
441 /**
442  * @macro
443  *
444  * :macro:`NGTCP2_HP_SAMPLELEN` is the number bytes sampled when
445  * encrypting a packet header.
446  */
447 #define NGTCP2_HP_SAMPLELEN 16
448 
449 /**
450  * @macro
451  *
452  * :macro:`NGTCP2_DEFAULT_INITIAL_RTT` is a default initial RTT.
453  */
454 #define NGTCP2_DEFAULT_INITIAL_RTT (333 * NGTCP2_MILLISECONDS)
455 
456 /**
457  * @macro
458  *
459  * :macro:`NGTCP2_MAX_CIDLEN` is the maximum length of Connection ID.
460  */
461 #define NGTCP2_MAX_CIDLEN 20
462 
463 /**
464  * @macro
465  *
466  * :macro:`NGTCP2_MIN_CIDLEN` is the minimum length of Connection ID.
467  */
468 #define NGTCP2_MIN_CIDLEN 1
469 
470 /**
471  * @macro
472  *
473  * :macro:`NGTCP2_MIN_INITIAL_DCIDLEN` is the minimum length of
474  * Destination Connection ID in Client Initial packet if it does not
475  * bear token from Retry packet.
476  */
477 #define NGTCP2_MIN_INITIAL_DCIDLEN 8
478 
479 /**
480  * @macro
481  *
482  * :macro:`NGTCP2_DEFAULT_HANDSHAKE_TIMEOUT` is the default handshake
483  * timeout.
484  */
485 #define NGTCP2_DEFAULT_HANDSHAKE_TIMEOUT (10 * NGTCP2_SECONDS)
486 
487 /**
488  * @macrosection
489  *
490  * ECN related macros
491  */
492 
493 /**
494  * @macro
495  *
496  * :macro:`NGTCP2_ECN_NOT_ECT` indicates no ECN marking.
497  */
498 #define NGTCP2_ECN_NOT_ECT 0x0
499 
500 /**
501  * @macro
502  *
503  * :macro:`NGTCP2_ECN_ECT_1` is ECT(1) codepoint.
504  */
505 #define NGTCP2_ECN_ECT_1 0x1
506 
507 /**
508  * @macro
509  *
510  * :macro:`NGTCP2_ECN_ECT_0` is ECT(0) codepoint.
511  */
512 #define NGTCP2_ECN_ECT_0 0x2
513 
514 /**
515  * @macro
516  *
517  * :macro:`NGTCP2_ECN_CE` is CE codepoint.
518  */
519 #define NGTCP2_ECN_CE 0x3
520 
521 /**
522  * @macro
523  *
524  * :macro:`NGTCP2_ECN_MASK` is a bit mask to get ECN marking.
525  */
526 #define NGTCP2_ECN_MASK 0x3
527 
528 #define NGTCP2_PKT_INFO_VERSION_V1 1
529 #define NGTCP2_PKT_INFO_VERSION NGTCP2_PKT_INFO_VERSION_V1
530 
531 /**
532  * @struct
533  *
534  * :type:`ngtcp2_pkt_info` is a packet metadata.
535  */
536 typedef struct NGTCP2_ALIGN(8) ngtcp2_pkt_info {
537   /**
538    * :member:`ecn` is ECN marking and when passing
539    * `ngtcp2_conn_read_pkt()`, and it should be either
540    * :macro:`NGTCP2_ECN_NOT_ECT`, :macro:`NGTCP2_ECN_ECT_1`,
541    * :macro:`NGTCP2_ECN_ECT_0`, or :macro:`NGTCP2_ECN_CE`.
542    */
543   uint32_t ecn;
544 } ngtcp2_pkt_info;
545 
546 /**
547  * @macrosection
548  *
549  * ngtcp2 library error codes
550  */
551 
552 /**
553  * @macro
554  *
555  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT` indicates that a passed
556  * argument is invalid.
557  */
558 #define NGTCP2_ERR_INVALID_ARGUMENT -201
559 /**
560  * @macro
561  *
562  * :macro:`NGTCP2_ERR_NOBUF` indicates that a provided buffer does not
563  * have enough space to store data.
564  */
565 #define NGTCP2_ERR_NOBUF -203
566 /**
567  * @macro
568  *
569  * :macro:`NGTCP2_ERR_PROTO` indicates a general protocol error.
570  */
571 #define NGTCP2_ERR_PROTO -205
572 /**
573  * @macro
574  *
575  * :macro:`NGTCP2_ERR_INVALID_STATE` indicates that a requested
576  * operation is not allowed at the current connection state.
577  */
578 #define NGTCP2_ERR_INVALID_STATE -206
579 /**
580  * @macro
581  *
582  * :macro:`NGTCP2_ERR_ACK_FRAME` indicates that an invalid ACK frame
583  * is received.
584  */
585 #define NGTCP2_ERR_ACK_FRAME -207
586 /**
587  * @macro
588  *
589  * :macro:`NGTCP2_ERR_STREAM_ID_BLOCKED` indicates that there is no
590  * spare stream ID available.
591  */
592 #define NGTCP2_ERR_STREAM_ID_BLOCKED -208
593 /**
594  * @macro
595  *
596  * :macro:`NGTCP2_ERR_STREAM_IN_USE` indicates that a stream ID is
597  * already in use.
598  */
599 #define NGTCP2_ERR_STREAM_IN_USE -209
600 /**
601  * @macro
602  *
603  * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED` indicates that stream data
604  * cannot be sent because of flow control.
605  */
606 #define NGTCP2_ERR_STREAM_DATA_BLOCKED -210
607 /**
608  * @macro
609  *
610  * :macro:`NGTCP2_ERR_FLOW_CONTROL` indicates flow control error.
611  */
612 #define NGTCP2_ERR_FLOW_CONTROL -211
613 /**
614  * @macro
615  *
616  * :macro:`NGTCP2_ERR_CONNECTION_ID_LIMIT` indicates that the number
617  * of received Connection ID exceeds acceptable limit.
618  */
619 #define NGTCP2_ERR_CONNECTION_ID_LIMIT -212
620 /**
621  * @macro
622  *
623  * :macro:`NGTCP2_ERR_STREAM_LIMIT` indicates that a remote endpoint
624  * opens more streams that is permitted.
625  */
626 #define NGTCP2_ERR_STREAM_LIMIT -213
627 /**
628  * @macro
629  *
630  * :macro:`NGTCP2_ERR_FINAL_SIZE` indicates that inconsistent final
631  * size of a stream.
632  */
633 #define NGTCP2_ERR_FINAL_SIZE -214
634 /**
635  * @macro
636  *
637  * :macro:`NGTCP2_ERR_CRYPTO` indicates crypto (TLS) related error.
638  */
639 #define NGTCP2_ERR_CRYPTO -215
640 /**
641  * @macro
642  *
643  * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED` indicates that packet number
644  * is exhausted.
645  */
646 #define NGTCP2_ERR_PKT_NUM_EXHAUSTED -216
647 /**
648  * @macro
649  *
650  * :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM` indicates that a
651  * required transport parameter is missing.
652  */
653 #define NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM -217
654 /**
655  * @macro
656  *
657  * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM` indicates that a
658  * transport parameter is malformed.
659  */
660 #define NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM -218
661 /**
662  * @macro
663  *
664  * :macro:`NGTCP2_ERR_FRAME_ENCODING` indicates there is an error in
665  * frame encoding.
666  */
667 #define NGTCP2_ERR_FRAME_ENCODING -219
668 /**
669  * @macro
670  *
671  * :macro:`NGTCP2_ERR_DECRYPT` indicates a decryption failure.
672  */
673 #define NGTCP2_ERR_DECRYPT -220
674 /**
675  * @macro
676  *
677  * :macro:`NGTCP2_ERR_STREAM_SHUT_WR` indicates no more data can be
678  * sent to a stream.
679  */
680 #define NGTCP2_ERR_STREAM_SHUT_WR -221
681 /**
682  * @macro
683  *
684  * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND` indicates that a stream was not
685  * found.
686  */
687 #define NGTCP2_ERR_STREAM_NOT_FOUND -222
688 /**
689  * @macro
690  *
691  * :macro:`NGTCP2_ERR_STREAM_STATE` indicates that a requested
692  * operation is not allowed at the current stream state.
693  */
694 #define NGTCP2_ERR_STREAM_STATE -226
695 /**
696  * @macro
697  *
698  * :macro:`NGTCP2_ERR_RECV_VERSION_NEGOTIATION` indicates that Version
699  * Negotiation packet was received.
700  */
701 #define NGTCP2_ERR_RECV_VERSION_NEGOTIATION -229
702 /**
703  * @macro
704  *
705  * :macro:`NGTCP2_ERR_CLOSING` indicates that connection is in closing
706  * state.
707  */
708 #define NGTCP2_ERR_CLOSING -230
709 /**
710  * @macro
711  *
712  * :macro:`NGTCP2_ERR_DRAINING` indicates that connection is in
713  * draining state.
714  */
715 #define NGTCP2_ERR_DRAINING -231
716 /**
717  * @macro
718  *
719  * :macro:`NGTCP2_ERR_TRANSPORT_PARAM` indicates a general transport
720  * parameter error.
721  */
722 #define NGTCP2_ERR_TRANSPORT_PARAM -234
723 /**
724  * @macro
725  *
726  * :macro:`NGTCP2_ERR_DISCARD_PKT` indicates a packet was discarded.
727  */
728 #define NGTCP2_ERR_DISCARD_PKT -235
729 /**
730  * @macro
731  *
732  * :macro:`NGTCP2_ERR_CONN_ID_BLOCKED` indicates that there is no
733  * spare Connection ID available.
734  */
735 #define NGTCP2_ERR_CONN_ID_BLOCKED -237
736 /**
737  * @macro
738  *
739  * :macro:`NGTCP2_ERR_INTERNAL` indicates an internal error.
740  */
741 #define NGTCP2_ERR_INTERNAL -238
742 /**
743  * @macro
744  *
745  * :macro:`NGTCP2_ERR_CRYPTO_BUFFER_EXCEEDED` indicates that a crypto
746  * buffer exceeded.
747  */
748 #define NGTCP2_ERR_CRYPTO_BUFFER_EXCEEDED -239
749 /**
750  * @macro
751  *
752  * :macro:`NGTCP2_ERR_WRITE_MORE` indicates
753  * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` is used and a function call
754  * succeeded.
755  */
756 #define NGTCP2_ERR_WRITE_MORE -240
757 /**
758  * @macro
759  *
760  * :macro:`NGTCP2_ERR_RETRY` indicates that server should send Retry
761  * packet.
762  */
763 #define NGTCP2_ERR_RETRY -241
764 /**
765  * @macro
766  *
767  * :macro:`NGTCP2_ERR_DROP_CONN` indicates that an endpoint should
768  * drop connection immediately.
769  */
770 #define NGTCP2_ERR_DROP_CONN -242
771 /**
772  * @macro
773  *
774  * :macro:`NGTCP2_ERR_AEAD_LIMIT_REACHED` indicates AEAD encryption
775  * limit is reached and key update is not available.  An endpoint
776  * should drop connection immediately.
777  */
778 #define NGTCP2_ERR_AEAD_LIMIT_REACHED -243
779 /**
780  * @macro
781  *
782  * :macro:`NGTCP2_ERR_NO_VIABLE_PATH` indicates that path validation
783  * could not probe that a path is capable of sending UDP datagram
784  * payload of size at least 1200 bytes.
785  */
786 #define NGTCP2_ERR_NO_VIABLE_PATH -244
787 /**
788  * @macro
789  *
790  * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION` indicates that server
791  * should send Version Negotiation packet.
792  */
793 #define NGTCP2_ERR_VERSION_NEGOTIATION -245
794 /**
795  * @macro
796  *
797  * :macro:`NGTCP2_ERR_HANDSHAKE_TIMEOUT` indicates that QUIC
798  * connection is not established before the specified deadline.
799  */
800 #define NGTCP2_ERR_HANDSHAKE_TIMEOUT -246
801 /**
802  * @macro
803  *
804  * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE` indicates the
805  * version negotiation failed.
806  */
807 #define NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE -247
808 /**
809  * @macro
810  *
811  * :macro:`NGTCP2_ERR_IDLE_CLOSE` indicates the connection should be
812  * closed silently because of idle timeout.
813  */
814 #define NGTCP2_ERR_IDLE_CLOSE -248
815 /**
816  * @macro
817  *
818  * :macro:`NGTCP2_ERR_FATAL` indicates that error codes less than this
819  * value is fatal error.  When this error is returned, an endpoint
820  * should drop connection immediately.
821  */
822 #define NGTCP2_ERR_FATAL -500
823 /**
824  * @macro
825  *
826  * :macro:`NGTCP2_ERR_NOMEM` indicates out of memory.
827  */
828 #define NGTCP2_ERR_NOMEM -501
829 /**
830  * @macro
831  *
832  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` indicates that user defined
833  * callback function failed.
834  */
835 #define NGTCP2_ERR_CALLBACK_FAILURE -502
836 
837 /**
838  * @macrosection
839  *
840  * QUIC packet header flags
841  */
842 
843 /**
844  * @macro
845  *
846  * :macro:`NGTCP2_PKT_FLAG_NONE` indicates no flag set.
847  */
848 #define NGTCP2_PKT_FLAG_NONE 0x00u
849 
850 /**
851  * @macro
852  *
853  * :macro:`NGTCP2_PKT_FLAG_LONG_FORM` indicates the Long header packet
854  * header.
855  */
856 #define NGTCP2_PKT_FLAG_LONG_FORM 0x01u
857 
858 /**
859  * @macro
860  *
861  * :macro:`NGTCP2_PKT_FLAG_FIXED_BIT_CLEAR` indicates that Fixed Bit
862  * (aka QUIC bit) is not set.
863  */
864 #define NGTCP2_PKT_FLAG_FIXED_BIT_CLEAR 0x02u
865 
866 /**
867  * @macro
868  *
869  * :macro:`NGTCP2_PKT_FLAG_KEY_PHASE` indicates Key Phase bit set.
870  */
871 #define NGTCP2_PKT_FLAG_KEY_PHASE 0x04u
872 
873 /**
874  * @enum
875  *
876  * :type:`ngtcp2_pkt_type` defines QUIC version-independent QUIC
877  * packet types.
878  */
879 typedef enum ngtcp2_pkt_type {
880   /**
881    * :enum:`NGTCP2_PKT_VERSION_NEGOTIATION` is defined by libngtcp2
882    * for convenience.
883    */
884   NGTCP2_PKT_VERSION_NEGOTIATION = 0x80,
885   /**
886    * :enum:`NGTCP2_PKT_STATELESS_RESET` is defined by libngtcp2 for
887    * convenience.
888    */
889   NGTCP2_PKT_STATELESS_RESET = 0x81,
890   /**
891    * :enum:`NGTCP2_PKT_INITIAL` indicates Initial packet.
892    */
893   NGTCP2_PKT_INITIAL = 0x10,
894   /**
895    * :enum:`NGTCP2_PKT_0RTT` indicates 0RTT packet.
896    */
897   NGTCP2_PKT_0RTT = 0x11,
898   /**
899    * :enum:`NGTCP2_PKT_HANDSHAKE` indicates Handshake packet.
900    */
901   NGTCP2_PKT_HANDSHAKE = 0x12,
902   /**
903    * :enum:`NGTCP2_PKT_RETRY` indicates Retry packet.
904    */
905   NGTCP2_PKT_RETRY = 0x13,
906   /**
907    * :enum:`NGTCP2_PKT_1RTT` is defined by libngtcp2 for convenience.
908    */
909   NGTCP2_PKT_1RTT = 0x40
910 } ngtcp2_pkt_type;
911 
912 /**
913  * @macrosection
914  *
915  * QUIC transport error code
916  */
917 
918 /**
919  * @macro
920  *
921  * :macro:`NGTCP2_NO_ERROR` is QUIC transport error code ``NO_ERROR``.
922  */
923 #define NGTCP2_NO_ERROR 0x0u
924 
925 /**
926  * @macro
927  *
928  * :macro:`NGTCP2_INTERNAL_ERROR` is QUIC transport error code
929  * ``INTERNAL_ERROR``.
930  */
931 #define NGTCP2_INTERNAL_ERROR 0x1u
932 
933 /**
934  * @macro
935  *
936  * :macro:`NGTCP2_CONNECTION_REFUSED` is QUIC transport error code
937  * ``CONNECTION_REFUSED``.
938  */
939 #define NGTCP2_CONNECTION_REFUSED 0x2u
940 
941 /**
942  * @macro
943  *
944  * :macro:`NGTCP2_FLOW_CONTROL_ERROR` is QUIC transport error code
945  * ``FLOW_CONTROL_ERROR``.
946  */
947 #define NGTCP2_FLOW_CONTROL_ERROR 0x3u
948 
949 /**
950  * @macro
951  *
952  * :macro:`NGTCP2_STREAM_LIMIT_ERROR` is QUIC transport error code
953  * ``STREAM_LIMIT_ERROR``.
954  */
955 #define NGTCP2_STREAM_LIMIT_ERROR 0x4u
956 
957 /**
958  * @macro
959  *
960  * :macro:`NGTCP2_STREAM_STATE_ERROR` is QUIC transport error code
961  * ``STREAM_STATE_ERROR``.
962  */
963 #define NGTCP2_STREAM_STATE_ERROR 0x5u
964 
965 /**
966  * @macro
967  *
968  * :macro:`NGTCP2_FINAL_SIZE_ERROR` is QUIC transport error code
969  * ``FINAL_SIZE_ERROR``.
970  */
971 #define NGTCP2_FINAL_SIZE_ERROR 0x6u
972 
973 /**
974  * @macro
975  *
976  * :macro:`NGTCP2_FRAME_ENCODING_ERROR` is QUIC transport error code
977  * ``FRAME_ENCODING_ERROR``.
978  */
979 #define NGTCP2_FRAME_ENCODING_ERROR 0x7u
980 
981 /**
982  * @macro
983  *
984  * :macro:`NGTCP2_TRANSPORT_PARAMETER_ERROR` is QUIC transport error
985  * code ``TRANSPORT_PARAMETER_ERROR``.
986  */
987 #define NGTCP2_TRANSPORT_PARAMETER_ERROR 0x8u
988 
989 /**
990  * @macro
991  *
992  * :macro:`NGTCP2_CONNECTION_ID_LIMIT_ERROR` is QUIC transport error
993  * code ``CONNECTION_ID_LIMIT_ERROR``.
994  */
995 #define NGTCP2_CONNECTION_ID_LIMIT_ERROR 0x9u
996 
997 /**
998  * @macro
999  *
1000  * :macro:`NGTCP2_PROTOCOL_VIOLATION` is QUIC transport error code
1001  * ``PROTOCOL_VIOLATION``.
1002  */
1003 #define NGTCP2_PROTOCOL_VIOLATION 0xau
1004 
1005 /**
1006  * @macro
1007  *
1008  * :macro:`NGTCP2_INVALID_TOKEN` is QUIC transport error code
1009  * ``INVALID_TOKEN``.
1010  */
1011 #define NGTCP2_INVALID_TOKEN 0xbu
1012 
1013 /**
1014  * @macro
1015  *
1016  * :macro:`NGTCP2_APPLICATION_ERROR` is QUIC transport error code
1017  * ``APPLICATION_ERROR``.
1018  */
1019 #define NGTCP2_APPLICATION_ERROR 0xcu
1020 
1021 /**
1022  * @macro
1023  *
1024  * :macro:`NGTCP2_CRYPTO_BUFFER_EXCEEDED` is QUIC transport error code
1025  * ``CRYPTO_BUFFER_EXCEEDED``.
1026  */
1027 #define NGTCP2_CRYPTO_BUFFER_EXCEEDED 0xdu
1028 
1029 /**
1030  * @macro
1031  *
1032  * :macro:`NGTCP2_KEY_UPDATE_ERROR` is QUIC transport error code
1033  * ``KEY_UPDATE_ERROR``.
1034  */
1035 #define NGTCP2_KEY_UPDATE_ERROR 0xeu
1036 
1037 /**
1038  * @macro
1039  *
1040  * :macro:`NGTCP2_AEAD_LIMIT_REACHED` is QUIC transport error code
1041  * ``AEAD_LIMIT_REACHED``.
1042  */
1043 #define NGTCP2_AEAD_LIMIT_REACHED 0xfu
1044 
1045 /**
1046  * @macro
1047  *
1048  * :macro:`NGTCP2_NO_VIABLE_PATH` is QUIC transport error code
1049  * ``NO_VIABLE_PATH``.
1050  */
1051 #define NGTCP2_NO_VIABLE_PATH 0x10u
1052 
1053 /**
1054  * @macro
1055  *
1056  * :macro:`NGTCP2_CRYPTO_ERROR` is QUIC transport error code
1057  * ``CRYPTO_ERROR``.
1058  */
1059 #define NGTCP2_CRYPTO_ERROR 0x100u
1060 
1061 /**
1062  * @macro
1063  *
1064  * :macro:`NGTCP2_VERSION_NEGOTIATION_ERROR_DRAFT` is QUIC transport
1065  * error code ``VERSION_NEGOTIATION_ERROR``.
1066  *
1067  * https://quicwg.org/quic-v2/draft-ietf-quic-v2.html
1068  */
1069 #define NGTCP2_VERSION_NEGOTIATION_ERROR_DRAFT 0x53f8u
1070 
1071 /**
1072  * @enum
1073  *
1074  * :type:`ngtcp2_path_validation_result` defines path validation
1075  * result code.
1076  */
1077 typedef enum ngtcp2_path_validation_result {
1078   /**
1079    * :enum:`NGTCP2_PATH_VALIDATION_RESULT_SUCCESS` indicates
1080    * successful validation.
1081    */
1082   NGTCP2_PATH_VALIDATION_RESULT_SUCCESS,
1083   /**
1084    * :enum:`NGTCP2_PATH_VALIDATION_RESULT_FAILURE` indicates
1085    * validation failure.
1086    */
1087   NGTCP2_PATH_VALIDATION_RESULT_FAILURE,
1088   /**
1089    * :enum:`NGTCP2_PATH_VALIDATION_RESULT_ABORTED` indicates that path
1090    * validation was aborted.
1091    */
1092   NGTCP2_PATH_VALIDATION_RESULT_ABORTED
1093 } ngtcp2_path_validation_result;
1094 
1095 /**
1096  * @typedef
1097  *
1098  * :type:`ngtcp2_tstamp` is a timestamp with nanosecond resolution.
1099  */
1100 typedef uint64_t ngtcp2_tstamp;
1101 
1102 /**
1103  * @typedef
1104  *
1105  * :type:`ngtcp2_duration` is a period of time in nanosecond
1106  * resolution.
1107  */
1108 typedef uint64_t ngtcp2_duration;
1109 
1110 /**
1111  * @struct
1112  *
1113  * :type:`ngtcp2_cid` holds a Connection ID.
1114  */
1115 typedef struct ngtcp2_cid {
1116   /**
1117    * :member:`datalen` is the length of Connection ID.
1118    */
1119   size_t datalen;
1120   /**
1121    * :member:`data` is the buffer to store Connection ID.
1122    */
1123   uint8_t data[NGTCP2_MAX_CIDLEN];
1124 } ngtcp2_cid;
1125 
1126 /**
1127  * @struct
1128  *
1129  * :type:`ngtcp2_vec` is struct iovec compatible structure to
1130  * reference arbitrary array of bytes.
1131  */
1132 typedef struct ngtcp2_vec {
1133   /**
1134    * :member:`base` points to the data.
1135    */
1136   uint8_t *base;
1137   /**
1138    * :member:`len` is the number of bytes which the buffer pointed by
1139    * base contains.
1140    */
1141   size_t len;
1142 } ngtcp2_vec;
1143 
1144 /**
1145  * @function
1146  *
1147  * `ngtcp2_cid_init` initializes Connection ID |cid| with the byte
1148  * string pointed by |data| and its length is |datalen|.  |datalen|
1149  * must be at most :macro:`NGTCP2_MAX_CIDLEN`.
1150  */
1151 NGTCP2_EXTERN void ngtcp2_cid_init(ngtcp2_cid *cid, const uint8_t *data,
1152                                    size_t datalen);
1153 
1154 /**
1155  * @function
1156  *
1157  * `ngtcp2_cid_eq` returns nonzero if |a| and |b| share the same
1158  * Connection ID.
1159  */
1160 NGTCP2_EXTERN int ngtcp2_cid_eq(const ngtcp2_cid *a, const ngtcp2_cid *b);
1161 
1162 /**
1163  * @struct
1164  *
1165  * :type:`ngtcp2_pkt_hd` represents QUIC packet header.
1166  */
1167 typedef struct ngtcp2_pkt_hd {
1168   /**
1169    * :member:`dcid` is Destination Connection ID.
1170    */
1171   ngtcp2_cid dcid;
1172   /**
1173    * :member:`scid` is Source Connection ID.
1174    */
1175   ngtcp2_cid scid;
1176   /**
1177    * :member:`pkt_num` is a packet number.
1178    */
1179   int64_t pkt_num;
1180   /**
1181    * :member:`token` contains token for Initial
1182    * packet.
1183    */
1184   ngtcp2_vec token;
1185   /**
1186    * :member:`pkt_numlen` is the number of bytes spent to encode
1187    * :member:`pkt_num`.
1188    */
1189   size_t pkt_numlen;
1190   /**
1191    * :member:`len` is the sum of :member:`pkt_numlen` and the length
1192    * of QUIC packet payload.
1193    */
1194   size_t len;
1195   /**
1196    * :member:`version` is QUIC version.
1197    */
1198   uint32_t version;
1199   /**
1200    * :member:`type` is a type of QUIC packet.  See
1201    * :type:`ngtcp2_pkt_type`.
1202    */
1203   uint8_t type;
1204   /**
1205    * :member:`flags` is zero or more of :macro:`NGTCP2_PKT_FLAG_*
1206    * <NGTCP2_PKT_FLAG_NONE>`.
1207    */
1208   uint8_t flags;
1209 } ngtcp2_pkt_hd;
1210 
1211 /**
1212  * @struct
1213  *
1214  * :type:`ngtcp2_pkt_stateless_reset` represents Stateless Reset.
1215  */
1216 typedef struct ngtcp2_pkt_stateless_reset {
1217   /**
1218    * :member:`stateless_reset_token` contains stateless reset token.
1219    */
1220   uint8_t stateless_reset_token[NGTCP2_STATELESS_RESET_TOKENLEN];
1221   /**
1222    * :member:`rand` points a buffer which contains random bytes
1223    * section.
1224    */
1225   const uint8_t *rand;
1226   /**
1227    * :member:`randlen` is the number of random bytes.
1228    */
1229   size_t randlen;
1230 } ngtcp2_pkt_stateless_reset;
1231 
1232 /**
1233  * @enum
1234  *
1235  * :type:`ngtcp2_transport_params_type` defines TLS message type which
1236  * carries transport parameters.
1237  */
1238 typedef enum ngtcp2_transport_params_type {
1239   /**
1240    * :enum:`NGTCP2_TRANSPORT_PARAMS_TYPE_CLIENT_HELLO` is Client Hello
1241    * TLS message.
1242    */
1243   NGTCP2_TRANSPORT_PARAMS_TYPE_CLIENT_HELLO,
1244   /**
1245    * :enum:`NGTCP2_TRANSPORT_PARAMS_TYPE_ENCRYPTED_EXTENSIONS` is
1246    * Encrypted Extensions TLS message.
1247    */
1248   NGTCP2_TRANSPORT_PARAMS_TYPE_ENCRYPTED_EXTENSIONS
1249 } ngtcp2_transport_params_type;
1250 
1251 /**
1252  * @macrosection
1253  *
1254  * QUIC transport parameters related macros
1255  */
1256 
1257 /**
1258  * @macro
1259  *
1260  * :macro:`NGTCP2_DEFAULT_MAX_RECV_UDP_PAYLOAD_SIZE` is the default
1261  * value of max_udp_payload_size transport parameter.
1262  */
1263 #define NGTCP2_DEFAULT_MAX_RECV_UDP_PAYLOAD_SIZE 65527
1264 
1265 /**
1266  * @macro
1267  *
1268  * :macro:`NGTCP2_DEFAULT_ACK_DELAY_EXPONENT` is a default value of
1269  * scaling factor of ACK Delay field in ACK frame.
1270  */
1271 #define NGTCP2_DEFAULT_ACK_DELAY_EXPONENT 3
1272 
1273 /**
1274  * @macro
1275  *
1276  * :macro:`NGTCP2_DEFAULT_MAX_ACK_DELAY` is a default value of the
1277  * maximum amount of time in nanoseconds by which endpoint delays
1278  * sending acknowledgement.
1279  */
1280 #define NGTCP2_DEFAULT_MAX_ACK_DELAY (25 * NGTCP2_MILLISECONDS)
1281 
1282 /**
1283  * @macro
1284  *
1285  * :macro:`NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT` is the default
1286  * value of active_connection_id_limit transport parameter value if
1287  * omitted.
1288  */
1289 #define NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT 2
1290 
1291 /**
1292  * @macro
1293  *
1294  * :macro:`NGTCP2_TLSEXT_QUIC_TRANSPORT_PARAMETERS_V1` is TLS
1295  * extension type of quic_transport_parameters.
1296  */
1297 #define NGTCP2_TLSEXT_QUIC_TRANSPORT_PARAMETERS_V1 0x39u
1298 
1299 /**
1300  * @macro
1301  *
1302  * :macro:`NGTCP2_TLSEXT_QUIC_TRANSPORT_PARAMETERS_DRAFT` is TLS
1303  * extension type of quic_transport_parameters used during draft
1304  * development.
1305  */
1306 #define NGTCP2_TLSEXT_QUIC_TRANSPORT_PARAMETERS_DRAFT 0xffa5u
1307 
1308 /**
1309  * @struct
1310  *
1311  * :type:`ngtcp2_preferred_addr` represents preferred address
1312  * structure.
1313  */
1314 typedef struct ngtcp2_preferred_addr {
1315   /**
1316    * :member:`cid` is a Connection ID.
1317    */
1318   ngtcp2_cid cid;
1319   /**
1320    * :member:`ipv4_port` is a port of IPv4 address.
1321    */
1322   uint16_t ipv4_port;
1323   /**
1324    * :member:`ipv6_port` is a port of IPv6 address.
1325    */
1326   uint16_t ipv6_port;
1327   /**
1328    * :member:`ipv4_addr` contains IPv4 address in network byte order.
1329    */
1330   uint8_t ipv4_addr[4];
1331   /**
1332    * :member:`ipv6_addr` contains IPv6 address in network byte order.
1333    */
1334   uint8_t ipv6_addr[16];
1335   /**
1336    * :member:`ipv4_present` indicates that :member:`ipv4_addr` and
1337    * :member:`ipv4_port` contain IPv4 address and port respectively.
1338    */
1339   uint8_t ipv4_present;
1340   /**
1341    * :member:`ipv6_present` indicates that :member:`ipv6_addr` and
1342    * :member:`ipv6_port` contain IPv6 address and port respectively.
1343    */
1344   uint8_t ipv6_present;
1345   /**
1346    * :member:`stateless_reset_token` contains stateless reset token.
1347    */
1348   uint8_t stateless_reset_token[NGTCP2_STATELESS_RESET_TOKENLEN];
1349 } ngtcp2_preferred_addr;
1350 
1351 /**
1352  * @struct
1353  *
1354  * :type:`ngtcp2_version_info` represents version_information
1355  * structure.
1356  */
1357 typedef struct ngtcp2_version_info {
1358   /**
1359    * :member:`chosen_version` is the version chosen by the sender.
1360    */
1361   uint32_t chosen_version;
1362   /**
1363    * :member:`other_versions` points the wire image of other_versions
1364    * field.  The each version is therefore in network byte order.
1365    */
1366   uint8_t *other_versions;
1367   /**
1368    * :member:`other_versionslen` is the number of bytes pointed by
1369    * :member:`other_versions`, not the number of versions included.
1370    */
1371   size_t other_versionslen;
1372 } ngtcp2_version_info;
1373 
1374 #define NGTCP2_TRANSPORT_PARAMS_VERSION_V1 1
1375 #define NGTCP2_TRANSPORT_PARAMS_VERSION NGTCP2_TRANSPORT_PARAMS_VERSION_V1
1376 
1377 /**
1378  * @struct
1379  *
1380  * :type:`ngtcp2_transport_params` represents QUIC transport
1381  * parameters.
1382  */
1383 typedef struct ngtcp2_transport_params {
1384   /**
1385    * :member:`preferred_address` contains preferred address if
1386    * :member:`preferred_address_present` is nonzero.
1387    */
1388   ngtcp2_preferred_addr preferred_address;
1389   /**
1390    * :member:`original_dcid` is the Destination Connection ID field
1391    * from the first Initial packet from client.  Server must specify
1392    * this field.  It is expected that application knows the original
1393    * Destination Connection ID even if it sends Retry packet, for
1394    * example, by including it in retry token.  Otherwise, application
1395    * should not specify this field.
1396    */
1397   ngtcp2_cid original_dcid;
1398   /**
1399    * :member:`initial_scid` is the Source Connection ID field from the
1400    * first Initial packet the endpoint sends.  Application should not
1401    * specify this field.
1402    */
1403   ngtcp2_cid initial_scid;
1404   /**
1405    * :member:`retry_scid` is the Source Connection ID field from Retry
1406    * packet.  Only server uses this field.  If server application
1407    * received Initial packet with retry token from client and server
1408    * verified its token, server application must set Destination
1409    * Connection ID field from the Initial packet to this field and set
1410    * :member:`retry_scid_present` to nonzero.  Server application must
1411    * verify that the Destination Connection ID from Initial packet was
1412    * sent in Retry packet by, for example, including the Connection ID
1413    * in a token, or including it in AAD when encrypting a token.
1414    */
1415   ngtcp2_cid retry_scid;
1416   /**
1417    * :member:`initial_max_stream_data_bidi_local` is the size of flow
1418    * control window of locally initiated stream.  This is the number
1419    * of bytes that the remote endpoint can send and the local endpoint
1420    * must ensure that it has enough buffer to receive them.
1421    */
1422   uint64_t initial_max_stream_data_bidi_local;
1423   /**
1424    * :member:`initial_max_stream_data_bidi_remote` is the size of flow
1425    * control window of remotely initiated stream.  This is the number
1426    * of bytes that the remote endpoint can send and the local endpoint
1427    * must ensure that it has enough buffer to receive them.
1428    */
1429   uint64_t initial_max_stream_data_bidi_remote;
1430   /**
1431    * :member:`initial_max_stream_data_uni` is the size of flow control
1432    * window of remotely initiated unidirectional stream.  This is the
1433    * number of bytes that the remote endpoint can send and the local
1434    * endpoint must ensure that it has enough buffer to receive them.
1435    */
1436   uint64_t initial_max_stream_data_uni;
1437   /**
1438    * :member:`initial_max_data` is the connection level flow control
1439    * window.
1440    */
1441   uint64_t initial_max_data;
1442   /**
1443    * :member:`initial_max_streams_bidi` is the number of concurrent
1444    * streams that the remote endpoint can create.
1445    */
1446   uint64_t initial_max_streams_bidi;
1447   /**
1448    * :member:`initial_max_streams_uni` is the number of concurrent
1449    * unidirectional streams that the remote endpoint can create.
1450    */
1451   uint64_t initial_max_streams_uni;
1452   /**
1453    * :member:`max_idle_timeout` is a duration during which sender
1454    * allows quiescent.
1455    */
1456   ngtcp2_duration max_idle_timeout;
1457   /**
1458    * :member:`max_udp_payload_size` is the maximum datagram size that
1459    * the endpoint can receive.
1460    */
1461   uint64_t max_udp_payload_size;
1462   /**
1463    * :member:`active_connection_id_limit` is the maximum number of
1464    * Connection ID that sender can store.
1465    */
1466   uint64_t active_connection_id_limit;
1467   /**
1468    * :member:`ack_delay_exponent` is the exponent used in ACK Delay
1469    * field in ACK frame.
1470    */
1471   uint64_t ack_delay_exponent;
1472   /**
1473    * :member:`max_ack_delay` is the maximum acknowledgement delay by
1474    * which the endpoint will delay sending acknowledgements.
1475    */
1476   ngtcp2_duration max_ack_delay;
1477   /**
1478    * :member:`max_datagram_frame_size` is the maximum size of DATAGRAM
1479    * frame that this endpoint willingly receives.  Specifying 0
1480    * disables DATAGRAM support.  See
1481    * https://datatracker.ietf.org/doc/html/rfc9221
1482    */
1483   uint64_t max_datagram_frame_size;
1484   /**
1485    * :member:`stateless_reset_token_present` is nonzero if
1486    * :member:`stateless_reset_token` field is set.
1487    */
1488   uint8_t stateless_reset_token_present;
1489   /**
1490    * :member:`disable_active_migration` is nonzero if the endpoint
1491    * does not support active connection migration.
1492    */
1493   uint8_t disable_active_migration;
1494   /**
1495    * :member:`retry_scid_present` is nonzero if :member:`retry_scid`
1496    * field is set.
1497    */
1498   uint8_t retry_scid_present;
1499   /**
1500    * :member:`preferred_address_present` is nonzero if
1501    * :member:`preferred_address` is set.
1502    */
1503   uint8_t preferred_address_present;
1504   /**
1505    * :member:`stateless_reset_token` contains stateless reset token.
1506    */
1507   uint8_t stateless_reset_token[NGTCP2_STATELESS_RESET_TOKENLEN];
1508   /**
1509    * :member:`grease_quic_bit` is nonzero if sender supports "Greasing
1510    * the QUIC Bit" extension.  See
1511    * https://datatracker.ietf.org/doc/html/draft-ietf-quic-bit-grease.
1512    * Note that the local endpoint always enables greasing QUIC bit
1513    * regardless of this field value.
1514    */
1515   uint8_t grease_quic_bit;
1516   /**
1517    * :member:`version_info` contains version_information field if
1518    * :member:`version_info_present` is nonzero.  Application should
1519    * not specify this field.
1520    */
1521   ngtcp2_version_info version_info;
1522   /**
1523    * :member:`version_info_present` is nonzero if
1524    * :member:`version_info` is set.  Application should not specify
1525    * this field.
1526    */
1527   uint8_t version_info_present;
1528 } ngtcp2_transport_params;
1529 
1530 /**
1531  * @enum
1532  *
1533  * :type:`ngtcp2_pktns_id` defines packet number space identifier.
1534  */
1535 typedef enum ngtcp2_pktns_id {
1536   /**
1537    * :enum:`NGTCP2_PKTNS_ID_INITIAL` is the Initial packet number
1538    * space.
1539    */
1540   NGTCP2_PKTNS_ID_INITIAL,
1541   /**
1542    * :enum:`NGTCP2_PKTNS_ID_HANDSHAKE` is the Handshake packet number
1543    * space.
1544    */
1545   NGTCP2_PKTNS_ID_HANDSHAKE,
1546   /**
1547    * :enum:`NGTCP2_PKTNS_ID_APPLICATION` is the Application data
1548    * packet number space.
1549    */
1550   NGTCP2_PKTNS_ID_APPLICATION,
1551   /**
1552    * :enum:`NGTCP2_PKTNS_ID_MAX` is defined to get the number of
1553    * packet number spaces.
1554    */
1555   NGTCP2_PKTNS_ID_MAX
1556 } ngtcp2_pktns_id;
1557 
1558 #define NGTCP2_CONN_STAT_VERSION_V1 1
1559 #define NGTCP2_CONN_STAT_VERSION NGTCP2_CONN_STAT_VERSION_V1
1560 
1561 /**
1562  * @struct
1563  *
1564  * :type:`ngtcp2_conn_stat` holds various connection statistics, and
1565  * computed data for recovery and congestion controller.
1566  */
1567 typedef struct ngtcp2_conn_stat {
1568   /**
1569    * :member:`latest_rtt` is the latest RTT sample which is not
1570    * adjusted by acknowledgement delay.
1571    */
1572   ngtcp2_duration latest_rtt;
1573   /**
1574    * :member:`min_rtt` is the minimum RTT seen so far.  It is not
1575    * adjusted by acknowledgement delay.
1576    */
1577   ngtcp2_duration min_rtt;
1578   /**
1579    * :member:`smoothed_rtt` is the smoothed RTT.
1580    */
1581   ngtcp2_duration smoothed_rtt;
1582   /**
1583    * :member:`rttvar` is a mean deviation of observed RTT.
1584    */
1585   ngtcp2_duration rttvar;
1586   /**
1587    * :member:`initial_rtt` is the initial RTT which is used when no
1588    * RTT sample is available.
1589    */
1590   ngtcp2_duration initial_rtt;
1591   /**
1592    * :member:`first_rtt_sample_ts` is the timestamp when the first RTT
1593    * sample is obtained.
1594    */
1595   ngtcp2_tstamp first_rtt_sample_ts;
1596   /**
1597    * :member:`pto_count` is the count of successive PTO timer
1598    * expiration.
1599    */
1600   size_t pto_count;
1601   /**
1602    * :member:`loss_detection_timer` is the deadline of the current
1603    * loss detection timer.
1604    */
1605   ngtcp2_tstamp loss_detection_timer;
1606   /**
1607    * :member:`last_tx_pkt_ts` corresponds to
1608    * time_of_last_ack_eliciting_packet in :rfc:`9002`.
1609    */
1610   ngtcp2_tstamp last_tx_pkt_ts[NGTCP2_PKTNS_ID_MAX];
1611   /**
1612    * :member:`loss_time` corresponds to loss_time in :rfc:`9002`.
1613    */
1614   ngtcp2_tstamp loss_time[NGTCP2_PKTNS_ID_MAX];
1615   /**
1616    * :member:`cwnd` is the size of congestion window.
1617    */
1618   uint64_t cwnd;
1619   /**
1620    * :member:`ssthresh` is slow start threshold.
1621    */
1622   uint64_t ssthresh;
1623   /**
1624    * :member:`congestion_recovery_start_ts` is the timestamp when
1625    * congestion recovery started.
1626    */
1627   ngtcp2_tstamp congestion_recovery_start_ts;
1628   /**
1629    * :member:`bytes_in_flight` is the number in bytes of all sent
1630    * packets which have not been acknowledged.
1631    */
1632   uint64_t bytes_in_flight;
1633   /**
1634    * :member:`max_udp_payload_size` is the maximum size of UDP
1635    * datagram payload that this endpoint transmits.  It is used by
1636    * congestion controller to compute congestion window.
1637    */
1638   size_t max_udp_payload_size;
1639   /**
1640    * :member:`delivery_rate_sec` is the current sending rate measured
1641    * in byte per second.
1642    */
1643   uint64_t delivery_rate_sec;
1644   /**
1645    * :member:`pacing_rate` is the current packet sending rate.  If
1646    * pacing is disabled, 0 is set.
1647    */
1648   double pacing_rate;
1649   /**
1650    * :member:`send_quantum` is the maximum size of a data aggregate
1651    * scheduled and transmitted together.
1652    */
1653   size_t send_quantum;
1654 } ngtcp2_conn_stat;
1655 
1656 /**
1657  * @enum
1658  *
1659  * :type:`ngtcp2_cc_algo` defines congestion control algorithms.
1660  */
1661 typedef enum ngtcp2_cc_algo {
1662   /**
1663    * :enum:`NGTCP2_CC_ALGO_RENO` represents Reno.
1664    */
1665   NGTCP2_CC_ALGO_RENO = 0x00,
1666   /**
1667    * :enum:`NGTCP2_CC_ALGO_CUBIC` represents Cubic.
1668    */
1669   NGTCP2_CC_ALGO_CUBIC = 0x01,
1670   /**
1671    * :enum:`NGTCP2_CC_ALGO_BBR` represents BBR.  If BBR is chosen,
1672    * packet pacing is enabled.
1673    */
1674   NGTCP2_CC_ALGO_BBR = 0x02,
1675   /**
1676    * :enum:`NGTCP2_CC_ALGO_BBR2` represents BBR v2.  If BBR v2 is
1677    * chosen, packet pacing is enabled.
1678    */
1679   NGTCP2_CC_ALGO_BBR2 = 0x03
1680 } ngtcp2_cc_algo;
1681 
1682 /**
1683  * @functypedef
1684  *
1685  * :type:`ngtcp2_printf` is a callback function for logging.
1686  * |user_data| is the same object passed to `ngtcp2_conn_client_new`
1687  * or `ngtcp2_conn_server_new`.
1688  */
1689 typedef void (*ngtcp2_printf)(void *user_data, const char *format, ...);
1690 
1691 /**
1692  * @macrosection
1693  *
1694  * QLog related macros
1695  */
1696 
1697 /**
1698  * @macro
1699  *
1700  * :macro:`NGTCP2_QLOG_WRITE_FLAG_NONE` indicates no flag set.
1701  */
1702 #define NGTCP2_QLOG_WRITE_FLAG_NONE 0x00u
1703 /**
1704  * @macro
1705  *
1706  * :macro:`NGTCP2_QLOG_WRITE_FLAG_FIN` indicates that this is the
1707  * final call to :type:`ngtcp2_qlog_write` in the current connection.
1708  */
1709 #define NGTCP2_QLOG_WRITE_FLAG_FIN 0x01u
1710 
1711 /**
1712  * @struct
1713  *
1714  * :type:`ngtcp2_rand_ctx` is a wrapper around native random number
1715  * generator.  It is opaque to the ngtcp2 library.  This might be
1716  * useful if application needs to specify random number generator per
1717  * thread or per connection.
1718  */
1719 typedef struct ngtcp2_rand_ctx {
1720   /**
1721    * :member:`native_handle` is a pointer to an underlying random
1722    * number generator.
1723    */
1724   void *native_handle;
1725 } ngtcp2_rand_ctx;
1726 
1727 /**
1728  * @functypedef
1729  *
1730  * :type:`ngtcp2_qlog_write` is a callback function which is called to
1731  * write qlog |data| of length |datalen| bytes.  |flags| is bitwise OR
1732  * of zero or more of :macro:`NGTCP2_QLOG_WRITE_FLAG_*
1733  * <NGTCP2_QLOG_WRITE_FLAG_NONE>`.  If
1734  * :macro:`NGTCP2_QLOG_WRITE_FLAG_FIN` is set, |datalen| may be 0.
1735  */
1736 typedef void (*ngtcp2_qlog_write)(void *user_data, uint32_t flags,
1737                                   const void *data, size_t datalen);
1738 
1739 /**
1740  * @struct
1741  *
1742  * :type:`ngtcp2_qlog_settings` is a set of settings for qlog.
1743  */
1744 typedef struct ngtcp2_qlog_settings {
1745   /**
1746    * :member:`odcid` is Original Destination Connection ID sent by
1747    * client.  It is used as group_id and ODCID fields.  Client ignores
1748    * this field and uses dcid parameter passed to
1749    * `ngtcp2_conn_client_new()`.
1750    */
1751   ngtcp2_cid odcid;
1752   /**
1753    * :member:`write` is a callback function to write qlog.  Setting
1754    * ``NULL`` disables qlog.
1755    */
1756   ngtcp2_qlog_write write;
1757 } ngtcp2_qlog_settings;
1758 
1759 #define NGTCP2_SETTINGS_VERSION_V1 1
1760 #define NGTCP2_SETTINGS_VERSION NGTCP2_SETTINGS_VERSION_V1
1761 
1762 /**
1763  * @struct
1764  *
1765  * :type:`ngtcp2_settings` defines QUIC connection settings.
1766  */
1767 typedef struct ngtcp2_settings {
1768   /**
1769    * :member:`qlog` is qlog settings.
1770    */
1771   ngtcp2_qlog_settings qlog;
1772   /**
1773    * :member:`cc_algo` specifies congestion control algorithm.
1774    */
1775   ngtcp2_cc_algo cc_algo;
1776   /**
1777    * :member:`initial_ts` is an initial timestamp given to the
1778    * library.
1779    */
1780   ngtcp2_tstamp initial_ts;
1781   /**
1782    * :member:`initial_rtt` is an initial RTT.
1783    */
1784   ngtcp2_duration initial_rtt;
1785   /**
1786    * :member:`log_printf` is a function that the library uses to write
1787    * logs.  ``NULL`` means no logging output.  It is nothing to do
1788    * with qlog.
1789    */
1790   ngtcp2_printf log_printf;
1791   /**
1792    * :member:`max_udp_payload_size` is the maximum size of UDP
1793    * datagram payload that this endpoint transmits.  It is used by
1794    * congestion controller to compute congestion window.
1795    */
1796   size_t max_udp_payload_size;
1797   /**
1798    * :member:`token` is a token from Retry packet or NEW_TOKEN frame.
1799    *
1800    * Server sets this field if it received the token in Client Initial
1801    * packet and successfully validated.
1802    *
1803    * Client sets this field if it intends to send token in its Initial
1804    * packet.
1805    *
1806    * `ngtcp2_conn_server_new` and `ngtcp2_conn_client_new` make a copy
1807    * of token.
1808    */
1809   ngtcp2_vec token;
1810   /**
1811    * :member:`rand_ctx` is an optional random number generator to be
1812    * passed to :type:`ngtcp2_rand` callback.
1813    */
1814   ngtcp2_rand_ctx rand_ctx;
1815   /**
1816    * :member:`max_window` is the maximum connection-level flow control
1817    * window if connection-level window auto-tuning is enabled.  The
1818    * connection-level window auto tuning is enabled if nonzero value
1819    * is specified in this field.  The initial value of window size is
1820    * :member:`ngtcp2_transport_params.initial_max_data`.  The window
1821    * size is scaled up to the value specified in this field.
1822    */
1823   uint64_t max_window;
1824   /**
1825    * :member:`max_stream_window` is the maximum stream-level flow
1826    * control window if stream-level window auto-tuning is enabled.
1827    * The stream-level window auto-tuning is enabled if nonzero value
1828    * is specified in this field.  The initial value of window size is
1829    * :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_remote`,
1830    * :member:`ngtcp2_transport_params.initial_max_stream_data_bidi_local`,
1831    * or :member:`ngtcp2_transport_params.initial_max_stream_data_uni`,
1832    * depending on the type of stream.  The window size is scaled up to
1833    * the value specified in this field.
1834    */
1835   uint64_t max_stream_window;
1836   /**
1837    * :member:`ack_thresh` is the maximum number of unacknowledged
1838    * packets before sending acknowledgement.  It triggers the
1839    * immediate acknowledgement.
1840    */
1841   size_t ack_thresh;
1842   /**
1843    * :member:`no_udp_payload_size_shaping`, if set to nonzero,
1844    * instructs the library not to limit the UDP payload size to
1845    * :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE` (which can be extended by
1846    * Path MTU Discovery) and instead use the mininum size among the
1847    * given buffer size, :member:`max_udp_payload_size`, and the
1848    * received max_udp_payload QUIC transport parameter.
1849    */
1850   int no_udp_payload_size_shaping;
1851   /**
1852    * :member:`handshake_timeout` is the period of time before giving
1853    * up QUIC connection establishment.  If QUIC handshake is not
1854    * complete within this period, `ngtcp2_conn_handle_expiry` returns
1855    * :macro:`NGTCP2_ERR_HANDSHAKE_TIMEOUT` error.  The deadline is
1856    * :member:`initial_ts` + :member:`handshake_timeout`.  If this
1857    * field is set to ``UINT64_MAX``, no handshake timeout is set.
1858    */
1859   ngtcp2_duration handshake_timeout;
1860   /**
1861    * :member:`preferred_versions` is the array of versions that are
1862    * preferred by the local endpoint.  All versions set in this array
1863    * must be supported by the library, and compatible to QUIC v1.  The
1864    * reserved versions are not allowed.  They are sorted in the order
1865    * of preference.
1866    *
1867    * On compatible version negotiation, server will negotiate one of
1868    * those versions contained in this array if a client initially
1869    * chooses a less preferred version.  This version set corresponds
1870    * to Offered Versions in QUIC Version Negotiation draft, and it should
1871    * be sent in Version Negotiation packet.
1872    *
1873    * Client uses this field and :member:`original_version` to prevent
1874    * version downgrade attack if it reacted upon Version Negotiation
1875    * packet.  If this field is specified, client must include
1876    * |client_chosen_version| passed to `ngtcp2_conn_client_new` unless
1877    * |client_chosen_version| is a reserved version.
1878    */
1879   uint32_t *preferred_versions;
1880   /**
1881    * :member:`preferred_versionslen` is the number of versions that
1882    * are contained in the array pointed by
1883    * :member:`preferred_versions`.
1884    */
1885   size_t preferred_versionslen;
1886   /**
1887    * :member:`other_versions` is the array of versions that are set in
1888    * :member:`other_versions <ngtcp2_version_info.other_versions>`
1889    * field of outgoing version_information QUIC transport parameter.
1890    *
1891    * For server, this corresponds to Fully-Deployed Versions in QUIC
1892    * Version Negotiation draft.  If this field is set not, it is set
1893    * to :member:`preferred_versions` internally if
1894    * :member:`preferred_versionslen` is not zero.  If this field is
1895    * not set, and :member:`preferred_versionslen` is zero, this field
1896    * is set to :macro:`NGTCP2_PROTO_VER_V1` internally.
1897    *
1898    * Client must include |client_chosen_version| passed to
1899    * `ngtcp2_conn_client_new` in this array if this field is set and
1900    * |client_chosen_version| is not a reserved version.  If this field
1901    * is not set, |client_chosen_version| passed to
1902    * `ngtcp2_conn_client_new` will be set in this field internally
1903    * unless |client_chosen_version| is a reserved version.
1904    */
1905   uint32_t *other_versions;
1906   /**
1907    * :member:`other_versionslen` is the number of versions that are
1908    * contained in the array pointed by :member:`other_versions`.
1909    */
1910   size_t other_versionslen;
1911   /**
1912    * :member:`original_version` is the original version that client
1913    * initially used to make a connection attempt.  If it is set, and
1914    * it differs from |client_chosen_version| passed to
1915    * `ngtcp2_conn_client_new`, the library assumes that client reacted
1916    * upon Version Negotiation packet.  Server does not use this field.
1917    */
1918   uint32_t original_version;
1919   /**
1920    * :member:`no_pmtud`, if set to nonzero, disables Path MTU
1921    * Discovery.
1922    */
1923   int no_pmtud;
1924 } ngtcp2_settings;
1925 
1926 #ifdef NGTCP2_USE_GENERIC_SOCKADDR
1927 typedef struct ngtcp2_sockaddr {
1928   uint16_t sa_family;
1929   uint8_t sa_data[14];
1930 } ngtcp2_sockaddr;
1931 
1932 typedef struct ngtcp2_in_addr {
1933   uint32_t s_addr;
1934 } ngtcp2_in_addr;
1935 
1936 typedef struct ngtcp2_sockaddr_in {
1937   uint16_t sin_family;
1938   uint16_t sin_port;
1939   ngtcp2_in_addr sin_addr;
1940   uint8_t sin_zero[8];
1941 } ngtcp2_sockaddr_in;
1942 
1943 #  define NGTCP2_SS_MAXSIZE 128
1944 #  define NGTCP2_SS_ALIGNSIZE (sizeof(uint64_t))
1945 #  define NGTCP2_SS_PAD1SIZE (NGTCP2_SS_ALIGNSIZE - sizeof(uint16_t))
1946 #  define NGTCP2_SS_PAD2SIZE                                                   \
1947     (NGTCP2_SS_MAXSIZE -                                                       \
1948      (sizeof(uint16_t) + NGTCP2_SS_PAD1SIZE + NGTCP2_SS_ALIGNSIZE))
1949 
1950 typedef struct ngtcp2_sockaddr_storage {
1951   uint16_t ss_family;
1952   uint8_t _ss_pad1[NGTCP2_SS_PAD1SIZE];
1953   uint64_t _ss_align;
1954   uint8_t _ss_pad2[NGTCP2_SS_PAD2SIZE];
1955 } ngtcp2_sockaddr_storage;
1956 
1957 #  undef NGTCP2_SS_PAD2SIZE
1958 #  undef NGTCP2_SS_PAD1SIZE
1959 #  undef NGTCP2_SS_ALIGNSIZE
1960 #  undef NGTCP2_SS_MAXSIZE
1961 
1962 typedef uint32_t ngtcp2_socklen;
1963 #else
1964 /**
1965  * @typedef
1966  *
1967  * :type:`ngtcp2_sockaddr` is typedefed to struct sockaddr.  If
1968  * :macro:`NGTCP2_USE_GENERIC_SOCKADDR` is defined, it is typedefed to
1969  * the generic struct sockaddr defined in ngtcp2.h.
1970  */
1971 typedef struct sockaddr ngtcp2_sockaddr;
1972 /**
1973  * @typedef
1974  *
1975  * :type:`ngtcp2_sockaddr_storage` is typedefed to struct
1976  * sockaddr_storage.  If :macro:`NGTCP2_USE_GENERIC_SOCKADDR` is
1977  * defined, it is typedefed to the generic struct sockaddr_storage
1978  * defined in ngtcp2.h.
1979  */
1980 typedef struct sockaddr_storage ngtcp2_sockaddr_storage;
1981 typedef struct sockaddr_in ngtcp2_sockaddr_in;
1982 /**
1983  * @typedef
1984  *
1985  * :type:`ngtcp2_socklen` is typedefed to socklen_t.  If
1986  * :macro:`NGTCP2_USE_GENERIC_SOCKADDR` is defined, it is typedefed to
1987  * uint32_t.
1988  */
1989 typedef socklen_t ngtcp2_socklen;
1990 #endif
1991 
1992 #if defined(NGTCP2_USE_GENERIC_SOCKADDR) ||                                    \
1993     defined(NGTCP2_USE_GENERIC_IPV6_SOCKADDR)
1994 typedef struct ngtcp2_in6_addr {
1995   uint8_t in6_addr[16];
1996 } ngtcp2_in6_addr;
1997 
1998 typedef struct ngtcp2_sockaddr_in6 {
1999   uint16_t sin6_family;
2000   uint16_t sin6_port;
2001   uint32_t sin6_flowinfo;
2002   ngtcp2_in6_addr sin6_addr;
2003   uint32_t sin6_scope_id;
2004 } ngtcp2_sockaddr_in6;
2005 #else
2006 typedef struct sockaddr_in6 ngtcp2_sockaddr_in6;
2007 #endif
2008 
2009 /**
2010  * @struct
2011  *
2012  * :type:`ngtcp2_addr` is the endpoint address.
2013  */
2014 typedef struct ngtcp2_addr {
2015   /**
2016    * :member:`addr` points to the buffer which contains endpoint
2017    * address.  It must not be ``NULL``.
2018    */
2019   ngtcp2_sockaddr *addr;
2020   /**
2021    * :member:`addrlen` is the length of addr.
2022    */
2023   ngtcp2_socklen addrlen;
2024 } ngtcp2_addr;
2025 
2026 /**
2027  * @struct
2028  *
2029  * :type:`ngtcp2_path` is the network endpoints where a packet is sent
2030  * and received.
2031  */
2032 typedef struct ngtcp2_path {
2033   /**
2034    * :member:`local` is the address of local endpoint.
2035    */
2036   ngtcp2_addr local;
2037   /**
2038    * :member:`remote` is the address of remote endpoint.
2039    */
2040   ngtcp2_addr remote;
2041   /**
2042    * :member:`user_data` is an arbitrary data and opaque to the
2043    * library.
2044    *
2045    * Note that :type:`ngtcp2_path` is generally passed to
2046    * :type:`ngtcp2_conn` by an application, and :type:`ngtcp2_conn`
2047    * stores their copies.  Unfortunately, there is no way for the
2048    * application to know when :type:`ngtcp2_conn` finishes using a
2049    * specific :type:`ngtcp2_path` object in mid connection, which
2050    * means that the application cannot free the data pointed by this
2051    * field.  Therefore, it is advised to use this field only when the
2052    * data pointed by this field persists in an entire lifetime of the
2053    * connection.
2054    */
2055   void *user_data;
2056 } ngtcp2_path;
2057 
2058 /**
2059  * @struct
2060  *
2061  * :type:`ngtcp2_path_storage` is a convenient struct to have buffers
2062  * to store the longest addresses.
2063  */
2064 typedef struct ngtcp2_path_storage {
2065   /**
2066    * :member:`path` stores network path.
2067    */
2068   ngtcp2_path path;
2069   /**
2070    * :member:`local_addrbuf` is a buffer to store local address.
2071    */
2072   ngtcp2_sockaddr_storage local_addrbuf;
2073   /**
2074    * :member:`remote_addrbuf` is a buffer to store remote address.
2075    */
2076   ngtcp2_sockaddr_storage remote_addrbuf;
2077 } ngtcp2_path_storage;
2078 
2079 /**
2080  * @struct
2081  *
2082  * :type:`ngtcp2_crypto_md` is a wrapper around native message digest
2083  * object.
2084  */
2085 typedef struct ngtcp2_crypto_md {
2086   /**
2087    * :member:`native_handle` is a pointer to an underlying message
2088    * digest object.
2089    */
2090   void *native_handle;
2091 } ngtcp2_crypto_md;
2092 
2093 /**
2094  * @struct
2095  *
2096  * :type:`ngtcp2_crypto_aead` is a wrapper around native AEAD object.
2097  */
2098 typedef struct ngtcp2_crypto_aead {
2099   /**
2100    * :member:`native_handle` is a pointer to an underlying AEAD
2101    * object.
2102    */
2103   void *native_handle;
2104   /**
2105    * :member:`max_overhead` is the number of additional bytes which
2106    * AEAD encryption needs on encryption.
2107    */
2108   size_t max_overhead;
2109 } ngtcp2_crypto_aead;
2110 
2111 /**
2112  * @struct
2113  *
2114  * :type:`ngtcp2_crypto_cipher` is a wrapper around native cipher
2115  * object.
2116  */
2117 typedef struct ngtcp2_crypto_cipher {
2118   /**
2119    * :member:`native_handle` is a pointer to an underlying cipher
2120    * object.
2121    */
2122   void *native_handle;
2123 } ngtcp2_crypto_cipher;
2124 
2125 /**
2126  * @struct
2127  *
2128  * :type:`ngtcp2_crypto_aead_ctx` is a wrapper around native AEAD
2129  * cipher context object.  It should be initialized with a specific
2130  * key.  ngtcp2 library reuses this context object to encrypt or
2131  * decrypt multiple packets.
2132  */
2133 typedef struct ngtcp2_crypto_aead_ctx {
2134   /**
2135    * :member:`native_handle` is a pointer to an underlying AEAD
2136    * context object.
2137    */
2138   void *native_handle;
2139 } ngtcp2_crypto_aead_ctx;
2140 
2141 /**
2142  * @struct
2143  *
2144  * :type:`ngtcp2_crypto_cipher_ctx` is a wrapper around native cipher
2145  * context object.  It should be initialized with a specific key.
2146  * ngtcp2 library reuses this context object to encrypt or decrypt
2147  * multiple packet headers.
2148  */
2149 typedef struct ngtcp2_crypto_cipher_ctx {
2150   /**
2151    * :member:`native_handle` is a pointer to an underlying cipher
2152    * context object.
2153    */
2154   void *native_handle;
2155 } ngtcp2_crypto_cipher_ctx;
2156 
2157 /**
2158  * @struct
2159  *
2160  * :type:`ngtcp2_crypto_ctx` is a convenient structure to bind all
2161  * crypto related objects in one place.  Use
2162  * `ngtcp2_crypto_ctx_initial` to initialize this struct for Initial
2163  * packet encryption.  For Handshake and 1RTT packets, use
2164  * `ngtcp2_crypto_ctx_tls`.
2165  */
2166 typedef struct ngtcp2_crypto_ctx {
2167   /**
2168    * :member:`aead` is AEAD object.
2169    */
2170   ngtcp2_crypto_aead aead;
2171   /**
2172    * :member:`md` is message digest object.
2173    */
2174   ngtcp2_crypto_md md;
2175   /**
2176    * :member:`hp` is header protection cipher.
2177    */
2178   ngtcp2_crypto_cipher hp;
2179   /**
2180    * :member:`max_encryption` is the number of encryption which this
2181    * key can be used with.
2182    */
2183   uint64_t max_encryption;
2184   /**
2185    * :member:`max_decryption_failure` is the number of decryption
2186    * failure with this key.
2187    */
2188   uint64_t max_decryption_failure;
2189 } ngtcp2_crypto_ctx;
2190 
2191 /**
2192  * @function
2193  *
2194  * `ngtcp2_encode_transport_params` encodes |params| in |dest| of
2195  * length |destlen|.
2196  *
2197  * If |dest| is NULL, and |destlen| is zero, this function just
2198  * returns the number of bytes required to store the encoded transport
2199  * parameters.
2200  *
2201  * This function returns the number of written, or one of the
2202  * following negative error codes:
2203  *
2204  * :macro:`NGTCP2_ERR_NOBUF`
2205  *     Buffer is too small.
2206  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
2207  *     |exttype| is invalid.
2208  */
2209 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_encode_transport_params_versioned(
2210     uint8_t *dest, size_t destlen, ngtcp2_transport_params_type exttype,
2211     int transport_params_version, const ngtcp2_transport_params *params);
2212 
2213 /**
2214  * @function
2215  *
2216  * `ngtcp2_decode_transport_params` decodes transport parameters in
2217  * |data| of length |datalen|, and stores the result in the object
2218  * pointed by |params|.
2219  *
2220  * If the optional parameters are missing, the default value is
2221  * assigned.
2222  *
2223  * The following fields may point to somewhere inside the buffer
2224  * pointed by |data| of length |datalen|:
2225  *
2226  * - :member:`ngtcp2_transport_params.version_info.other_versions
2227  *   <ngtcp2_version_info.other_versions>`
2228  *
2229  * This function returns 0 if it succeeds, or one of the following
2230  * negative error codes:
2231  *
2232  * :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM`
2233  *     The required parameter is missing.
2234  * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
2235  *     The input is malformed.
2236  */
2237 NGTCP2_EXTERN int ngtcp2_decode_transport_params_versioned(
2238     int transport_params_version, ngtcp2_transport_params *params,
2239     ngtcp2_transport_params_type exttype, const uint8_t *data, size_t datalen);
2240 
2241 /**
2242  * @function
2243  *
2244  * `ngtcp2_decode_transport_params_new` decodes transport parameters
2245  * in |data| of length |datalen|, and stores the result in the object
2246  * allocated dynamically.  The pointer to the allocated object is
2247  * assigned to |*pparams|.  Unlike `ngtcp2_decode_transport_params`,
2248  * all direct and indirect fields are also allocated dynamically if
2249  * needed.
2250  *
2251  * |mem| is a memory allocator to allocate memory.  If |mem| is
2252  * ``NULL``, the memory allocator returned by `ngtcp2_mem_default()`
2253  * is used.
2254  *
2255  * If the optional parameters are missing, the default value is
2256  * assigned.
2257  *
2258  * `ngtcp2_transport_params_del` frees the memory allocated by this
2259  * function.
2260  *
2261  * This function returns 0 if it succeeds, or one of the following
2262  * negative error codes:
2263  *
2264  * :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM`
2265  *     The required parameter is missing.
2266  * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
2267  *     The input is malformed.
2268  * :macro:`NGTCP2_ERR_NOMEM`
2269  *     Out of memory.
2270  */
2271 NGTCP2_EXTERN int ngtcp2_decode_transport_params_new(
2272     ngtcp2_transport_params **pparams, ngtcp2_transport_params_type exttype,
2273     const uint8_t *data, size_t datalen, const ngtcp2_mem *mem);
2274 
2275 /**
2276  * @function
2277  *
2278  * `ngtcp2_transport_params_del` frees the |params| which must be
2279  * dynamically allocated by `ngtcp2_decode_transport_params_new`.
2280  *
2281  * |mem| is a memory allocator that allocated |params|.  If |mem| is
2282  * ``NULL``, the memory allocator returned by `ngtcp2_mem_default()`
2283  * is used.
2284  *
2285  * If |params| is ``NULL``, this function does nothing.
2286  */
2287 NGTCP2_EXTERN void ngtcp2_transport_params_del(ngtcp2_transport_params *params,
2288                                                const ngtcp2_mem *mem);
2289 
2290 /**
2291  * @struct
2292  *
2293  * :type:`ngtcp2_version_cid` is a convenient struct to store the
2294  * result of `ngtcp2_pkt_decode_version_cid`.
2295  */
2296 typedef struct ngtcp2_version_cid {
2297   /**
2298    * :member:`version` stores QUIC version.
2299    */
2300   uint32_t version;
2301   /**
2302    * :member:`dcid` points to the Destination Connection ID.
2303    */
2304   const uint8_t *dcid;
2305   /**
2306    * :member:`dcidlen` is the length of the Destination Connection ID
2307    * pointed by :member:`dcid`.
2308    */
2309   size_t dcidlen;
2310   /**
2311    * :member:`scid` points to the Source Connection ID.
2312    */
2313   const uint8_t *scid;
2314   /**
2315    * :member:`scidlen` is the length of the Source Connection ID
2316    * pointed by :member:`scid`.
2317    */
2318   size_t scidlen;
2319 } ngtcp2_version_cid;
2320 
2321 /**
2322  * @function
2323  *
2324  * `ngtcp2_pkt_decode_version_cid` extracts QUIC version, Destination
2325  * Connection ID and Source Connection ID from the packet pointed by
2326  * |data| of length |datalen|.  This function can handle Connection ID
2327  * up to 255 bytes unlike `ngtcp2_pkt_decode_hd_long` or
2328  * `ngtcp2_pkt_decode_hd_short` which are only capable of handling
2329  * Connection ID less than or equal to :macro:`NGTCP2_MAX_CIDLEN`.
2330  * Longer Connection ID is only valid if the version is unsupported
2331  * QUIC version.
2332  *
2333  * If the given packet is Long header packet, this function extracts
2334  * the version from the packet and assigns it to
2335  * :member:`dest->version <ngtcp2_version_cid.version>`.  It also
2336  * extracts the pointer to the Destination Connection ID and its
2337  * length and assigns them to :member:`dest->dcid
2338  * <ngtcp2_version_cid.dcid>` and :member:`dest->dcidlen
2339  * <ngtcp2_version_cid.dcidlen>` respectively.  Similarly, it extracts
2340  * the pointer to the Source Connection ID and its length and assigns
2341  * them to :member:`dest->scid <ngtcp2_version_cid.scid>` and
2342  * :member:`dest->scidlen <ngtcp2_version_cid.scidlen>` respectively.
2343  *
2344  * If the given packet is Short header packet, :member:`dest->version
2345  * <ngtcp2_version_cid.version>` will be 0, :member:`dest->scid
2346  * <ngtcp2_version_cid.scid>` will be ``NULL``, and
2347  * :member:`dest->scidlen <ngtcp2_version_cid.scidlen>` will be 0.
2348  * Because the Short header packet does not have the length of
2349  * Destination Connection ID, the caller has to pass the length in
2350  * |short_dcidlen|.  This function extracts the pointer to the
2351  * Destination Connection ID and assigns it to :member:`dest->dcid
2352  * <ngtcp2_version_cid.dcid>`.  |short_dcidlen| is assigned to
2353  * :member:`dest->dcidlen <ngtcp2_version_cid.dcidlen>`.
2354  *
2355  * If Version Negotiation is required, this function returns
2356  * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION`.  Unlike the other error
2357  * cases, all fields of |dest| are assigned as described above.
2358  *
2359  * This function returns 0 if it succeeds.  Otherwise, one of the
2360  * following negative error code:
2361  *
2362  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
2363  *     The function could not decode the packet header.
2364  * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION`
2365  *     Version Negotiation packet should be sent.
2366  */
2367 NGTCP2_EXTERN int ngtcp2_pkt_decode_version_cid(ngtcp2_version_cid *dest,
2368                                                 const uint8_t *data,
2369                                                 size_t datalen,
2370                                                 size_t short_dcidlen);
2371 
2372 /**
2373  * @function
2374  *
2375  * `ngtcp2_pkt_decode_hd_long` decodes QUIC long packet header in
2376  * |pkt| of length |pktlen|.  This function only parses the input just
2377  * before packet number field.
2378  *
2379  * This function does not verify that length field is correct.  In
2380  * other words, this function succeeds even if length > |pktlen|.
2381  *
2382  * This function can handle Connection ID up to
2383  * :macro:`NGTCP2_MAX_CIDLEN`.  Consider to use
2384  * `ngtcp2_pkt_decode_version_cid` to get longer Connection ID.
2385  *
2386  * This function handles Version Negotiation specially.  If version
2387  * field is 0, |pkt| must contain Version Negotiation packet.  Version
2388  * Negotiation packet has random type in wire format.  For
2389  * convenience, this function sets
2390  * :enum:`ngtcp2_pkt_type.NGTCP2_PKT_VERSION_NEGOTIATION` to
2391  * :member:`dest->type <ngtcp2_pkt_hd.type>`, clears
2392  * :macro:`NGTCP2_PKT_FLAG_LONG_FORM` flag from :member:`dest->flags
2393  * <ngtcp2_pkt_hd.flags>`, and sets 0 to :member:`dest->len
2394  * <ngtcp2_pkt_hd.len>`.  Version Negotiation packet occupies a single
2395  * packet.
2396  *
2397  * It stores the result in the object pointed by |dest|, and returns
2398  * the number of bytes decoded to read the packet header if it
2399  * succeeds, or one of the following error codes:
2400  *
2401  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
2402  *     Packet is too short; or it is not a long header
2403  */
2404 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_decode_hd_long(ngtcp2_pkt_hd *dest,
2405                                                      const uint8_t *pkt,
2406                                                      size_t pktlen);
2407 
2408 /**
2409  * @function
2410  *
2411  * `ngtcp2_pkt_decode_hd_short` decodes QUIC short header packet
2412  * header in |pkt| of length |pktlen|.  |dcidlen| is the length of
2413  * DCID in packet header.  Short header packet does not encode the
2414  * length of connection ID, thus we need the input from the outside.
2415  * This function only parses the input just before packet number
2416  * field.  This function can handle Connection ID up to
2417  * :macro:`NGTCP2_MAX_CIDLEN`.  Consider to use
2418  * `ngtcp2_pkt_decode_version_cid` to get longer Connection ID.  It
2419  * stores the result in the object pointed by |dest|, and returns the
2420  * number of bytes decoded to read the packet header if it succeeds,
2421  * or one of the following error codes:
2422  *
2423  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
2424  *     Packet is too short; or it is not a short header
2425  */
2426 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_decode_hd_short(ngtcp2_pkt_hd *dest,
2427                                                       const uint8_t *pkt,
2428                                                       size_t pktlen,
2429                                                       size_t dcidlen);
2430 
2431 /**
2432  * @function
2433  *
2434  * `ngtcp2_pkt_write_stateless_reset` writes Stateless Reset packet in
2435  * the buffer pointed by |dest| whose length is |destlen|.
2436  * |stateless_reset_token| is a pointer to the Stateless Reset Token,
2437  * and its length must be :macro:`NGTCP2_STATELESS_RESET_TOKENLEN`
2438  * bytes long.  |rand| specifies the random octets preceding Stateless
2439  * Reset Token.  The length of |rand| is specified by |randlen| which
2440  * must be at least :macro:`NGTCP2_MIN_STATELESS_RESET_RANDLEN` bytes
2441  * long.
2442  *
2443  * If |randlen| is too long to write them all in the buffer, |rand| is
2444  * written to the buffer as much as possible, and is truncated.
2445  *
2446  * This function returns the number of bytes written to the buffer, or
2447  * one of the following negative error codes:
2448  *
2449  * :macro:`NGTCP2_ERR_NOBUF`
2450  *     Buffer is too small.
2451  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
2452  *     |randlen| is strictly less than
2453  *     :macro:`NGTCP2_MIN_STATELESS_RESET_RANDLEN`.
2454  */
2455 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_stateless_reset(
2456     uint8_t *dest, size_t destlen, const uint8_t *stateless_reset_token,
2457     const uint8_t *rand, size_t randlen);
2458 
2459 /**
2460  * @function
2461  *
2462  * `ngtcp2_pkt_write_version_negotiation` writes Version Negotiation
2463  * packet in the buffer pointed by |dest| whose length is |destlen|.
2464  * |unused_random| should be generated randomly.  |dcid| is the
2465  * destination connection ID which appears in a packet as a source
2466  * connection ID sent by client which caused version negotiation.
2467  * Similarly, |scid| is the source connection ID which appears in a
2468  * packet as a destination connection ID sent by client.  |sv| is a
2469  * list of supported versions, and |nsv| specifies the number of
2470  * supported versions included in |sv|.
2471  *
2472  * This function returns the number of bytes written to the buffer, or
2473  * one of the following negative error codes:
2474  *
2475  * :macro:`NGTCP2_ERR_NOBUF`
2476  *     Buffer is too small.
2477  */
2478 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_version_negotiation(
2479     uint8_t *dest, size_t destlen, uint8_t unused_random, const uint8_t *dcid,
2480     size_t dcidlen, const uint8_t *scid, size_t scidlen, const uint32_t *sv,
2481     size_t nsv);
2482 
2483 /**
2484  * @struct
2485  *
2486  * :type:`ngtcp2_conn` represents a single QUIC connection.
2487  */
2488 typedef struct ngtcp2_conn ngtcp2_conn;
2489 
2490 /**
2491  * @functypedef
2492  *
2493  * :type:`ngtcp2_client_initial` is invoked when client application
2494  * asks TLS stack to produce first TLS cryptographic handshake data.
2495  *
2496  * This implementation of this callback must get the first handshake
2497  * data from TLS stack and pass it to ngtcp2 library using
2498  * `ngtcp2_conn_submit_crypto_data` function.  Make sure that before
2499  * calling `ngtcp2_conn_submit_crypto_data` function, client
2500  * application must create initial packet protection keys and IVs, and
2501  * provide them to ngtcp2 library using
2502  * `ngtcp2_conn_install_initial_key`.
2503  *
2504  * This callback function must return 0 if it succeeds, or
2505  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
2506  * return immediately.
2507  */
2508 typedef int (*ngtcp2_client_initial)(ngtcp2_conn *conn, void *user_data);
2509 
2510 /**
2511  * @functypedef
2512  *
2513  * :type:`ngtcp2_recv_client_initial` is invoked when server receives
2514  * Initial packet from client.  An server application must implement
2515  * this callback, and generate initial keys and IVs for both
2516  * transmission and reception.  Install them using
2517  * `ngtcp2_conn_install_initial_key`.  |dcid| is the destination
2518  * connection ID which client generated randomly.  It is used to
2519  * derive initial packet protection keys.
2520  *
2521  * The callback function must return 0 if it succeeds.  If an error
2522  * occurs, return :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the
2523  * library call return immediately.
2524  */
2525 typedef int (*ngtcp2_recv_client_initial)(ngtcp2_conn *conn,
2526                                           const ngtcp2_cid *dcid,
2527                                           void *user_data);
2528 
2529 /**
2530  * @enum
2531  *
2532  * :type:`ngtcp2_crypto_level` is encryption level.
2533  */
2534 typedef enum ngtcp2_crypto_level {
2535   /**
2536    * :enum:`NGTCP2_CRYPTO_LEVEL_INITIAL` is Initial Keys encryption
2537    * level.
2538    */
2539   NGTCP2_CRYPTO_LEVEL_INITIAL,
2540   /**
2541    * :enum:`NGTCP2_CRYPTO_LEVEL_HANDSHAKE` is Handshake Keys
2542    * encryption level.
2543    */
2544   NGTCP2_CRYPTO_LEVEL_HANDSHAKE,
2545   /**
2546    * :enum:`NGTCP2_CRYPTO_LEVEL_APPLICATION` is Application Data
2547    * (1-RTT) Keys encryption level.
2548    */
2549   NGTCP2_CRYPTO_LEVEL_APPLICATION,
2550   /**
2551    * :enum:`NGTCP2_CRYPTO_LEVEL_EARLY` is Early Data (0-RTT) Keys
2552    * encryption level.
2553    */
2554   NGTCP2_CRYPTO_LEVEL_EARLY
2555 } ngtcp2_crypto_level;
2556 
2557 /**
2558  * @functypedef
2559  *
2560  * :type`ngtcp2_recv_crypto_data` is invoked when crypto data is
2561  * received.  The received data is pointed to by |data|, and its
2562  * length is |datalen|.  The |offset| specifies the offset where
2563  * |data| is positioned.  |user_data| is the arbitrary pointer passed
2564  * to `ngtcp2_conn_client_new` or `ngtcp2_conn_server_new`.  The
2565  * ngtcp2 library ensures that the crypto data is passed to the
2566  * application in the increasing order of |offset|.  |datalen| is
2567  * always strictly greater than 0.  |crypto_level| indicates the
2568  * encryption level where this data is received.  Crypto data can
2569  * never be received in
2570  * :enum:`ngtcp2_crypto_level.NGTCP2_CRYPTO_LEVEL_EARLY`.
2571  *
2572  * The application should provide the given data to TLS stack.
2573  *
2574  * The callback function must return 0 if it succeeds, or one of the
2575  * following negative error codes:
2576  *
2577  * - :macro:`NGTCP2_ERR_CRYPTO`
2578  * - :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM`
2579  * - :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
2580  * - :macro:`NGTCP2_ERR_TRANSPORT_PARAM`
2581  * - :macro:`NGTCP2_ERR_PROTO`
2582  * - :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE`
2583  * - :macro:`NGTCP2_ERR_NOMEM`
2584  * - :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
2585  *
2586  * If the other value is returned, it is treated as
2587  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`.
2588  *
2589  * If application encounters fatal error, return
2590  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
2591  * return immediately.
2592  */
2593 typedef int (*ngtcp2_recv_crypto_data)(ngtcp2_conn *conn,
2594                                        ngtcp2_crypto_level crypto_level,
2595                                        uint64_t offset, const uint8_t *data,
2596                                        size_t datalen, void *user_data);
2597 
2598 /**
2599  * @functypedef
2600  *
2601  * :type:`ngtcp2_handshake_completed` is invoked when QUIC
2602  * cryptographic handshake has completed.
2603  *
2604  * The callback function must return 0 if it succeeds.  Returning
2605  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
2606  * immediately.
2607  */
2608 typedef int (*ngtcp2_handshake_completed)(ngtcp2_conn *conn, void *user_data);
2609 
2610 /**
2611  * @functypedef
2612  *
2613  * :type:`ngtcp2_handshake_confirmed` is invoked when QUIC
2614  * cryptographic handshake is confirmed.  The handshake confirmation
2615  * means that both endpoints agree that handshake has finished.
2616  *
2617  * The callback function must return 0 if it succeeds.  Returning
2618  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
2619  * immediately.
2620  */
2621 typedef int (*ngtcp2_handshake_confirmed)(ngtcp2_conn *conn, void *user_data);
2622 
2623 /**
2624  * @functypedef
2625  *
2626  * :type:`ngtcp2_recv_version_negotiation` is invoked when Version
2627  * Negotiation packet is received.  |hd| is the pointer to the QUIC
2628  * packet header object.  The vector |sv| of |nsv| elements contains
2629  * the QUIC version the server supports.  Since Version Negotiation is
2630  * only sent by server, this callback function is used by client only.
2631  *
2632  * The callback function must return 0 if it succeeds, or
2633  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
2634  * return immediately.
2635  */
2636 typedef int (*ngtcp2_recv_version_negotiation)(ngtcp2_conn *conn,
2637                                                const ngtcp2_pkt_hd *hd,
2638                                                const uint32_t *sv, size_t nsv,
2639                                                void *user_data);
2640 
2641 /**
2642  * @functypedef
2643  *
2644  * :type:`ngtcp2_recv_retry` is invoked when Retry packet is received.
2645  * This callback is client use only.
2646  *
2647  * Application must regenerate packet protection key, IV, and header
2648  * protection key for Initial packets using the destination connection
2649  * ID obtained by :member:`hd->scid <ngtcp2_pkt_hd.scid>` and install
2650  * them by calling `ngtcp2_conn_install_initial_key()`.
2651  *
2652  * 0-RTT data accepted by the ngtcp2 library will be automatically
2653  * retransmitted as 0-RTT data by the library.
2654  *
2655  * The callback function must return 0 if it succeeds.  Returning
2656  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
2657  * immediately.
2658  */
2659 typedef int (*ngtcp2_recv_retry)(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd,
2660                                  void *user_data);
2661 
2662 /**
2663  * @functypedef
2664  *
2665  * :type:`ngtcp2_encrypt` is invoked when the ngtcp2 library asks the
2666  * application to encrypt packet payload.  The packet payload to
2667  * encrypt is passed as |plaintext| of length |plaintextlen|.  The
2668  * AEAD cipher is |aead|.  |aead_ctx| is the AEAD cipher context
2669  * object which is initialized with encryption key.  The nonce is
2670  * passed as |nonce| of length |noncelen|.  The Additional
2671  * Authenticated Data is passed as |aad| of length |aadlen|.
2672  *
2673  * The implementation of this callback must encrypt |plaintext| using
2674  * the negotiated cipher suite and write the ciphertext into the
2675  * buffer pointed by |dest|.  |dest| has enough capacity to store the
2676  * ciphertext and any additional AEAD tag data.
2677  *
2678  * |dest| and |plaintext| may point to the same buffer.
2679  *
2680  * The callback function must return 0 if it succeeds, or
2681  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
2682  * return immediately.
2683  */
2684 typedef int (*ngtcp2_encrypt)(uint8_t *dest, const ngtcp2_crypto_aead *aead,
2685                               const ngtcp2_crypto_aead_ctx *aead_ctx,
2686                               const uint8_t *plaintext, size_t plaintextlen,
2687                               const uint8_t *nonce, size_t noncelen,
2688                               const uint8_t *aad, size_t aadlen);
2689 
2690 /**
2691  * @functypedef
2692  *
2693  * :type:`ngtcp2_decrypt` is invoked when the ngtcp2 library asks the
2694  * application to decrypt packet payload.  The packet payload to
2695  * decrypt is passed as |ciphertext| of length |ciphertextlen|.  The
2696  * AEAD cipher is |aead|.  |aead_ctx| is the AEAD cipher context
2697  * object which is initialized with decryption key.  The nonce is
2698  * passed as |nonce| of length |noncelen|.  The Additional
2699  * Authenticated Data is passed as |aad| of length |aadlen|.
2700  *
2701  * The implementation of this callback must decrypt |ciphertext| using
2702  * the negotiated cipher suite and write the ciphertext into the
2703  * buffer pointed by |dest|.  |dest| has enough capacity to store the
2704  * cleartext.
2705  *
2706  * |dest| and |ciphertext| may point to the same buffer.
2707  *
2708  * The callback function must return 0 if it succeeds.  If TLS stack
2709  * fails to decrypt data, return :macro:`NGTCP2_ERR_DECRYPT`.  For any
2710  * other errors, return :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which
2711  * makes the library call return immediately.
2712  */
2713 typedef int (*ngtcp2_decrypt)(uint8_t *dest, const ngtcp2_crypto_aead *aead,
2714                               const ngtcp2_crypto_aead_ctx *aead_ctx,
2715                               const uint8_t *ciphertext, size_t ciphertextlen,
2716                               const uint8_t *nonce, size_t noncelen,
2717                               const uint8_t *aad, size_t aadlen);
2718 
2719 /**
2720  * @functypedef
2721  *
2722  * :type:`ngtcp2_hp_mask` is invoked when the ngtcp2 library asks the
2723  * application to produce a mask to encrypt or decrypt packet header.
2724  * The encryption cipher is |hp|.  |hp_ctx| is the cipher context
2725  * object which is initialized with header protection key.  The sample
2726  * is passed as |sample| which is :macro:`NGTCP2_HP_SAMPLELEN` bytes
2727  * long.
2728  *
2729  * The implementation of this callback must produce a mask using the
2730  * header protection cipher suite specified by QUIC specification and
2731  * write the result into the buffer pointed by |dest|.  The length of
2732  * the mask must be at least :macro:`NGTCP2_HP_MASKLEN`.  The library
2733  * only uses the first :macro:`NGTCP2_HP_MASKLEN` bytes of the
2734  * produced mask.  The buffer pointed by |dest| is guaranteed to have
2735  * at least :macro:`NGTCP2_HP_SAMPLELEN` bytes available for
2736  * convenience.
2737  *
2738  * The callback function must return 0 if it succeeds, or
2739  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library call
2740  * return immediately.
2741  */
2742 typedef int (*ngtcp2_hp_mask)(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
2743                               const ngtcp2_crypto_cipher_ctx *hp_ctx,
2744                               const uint8_t *sample);
2745 
2746 /**
2747  * @macrosection
2748  *
2749  * Stream data flags
2750  */
2751 
2752 /**
2753  * @macro
2754  *
2755  * :macro:`NGTCP2_STREAM_DATA_FLAG_NONE` indicates no flag set.
2756  */
2757 #define NGTCP2_STREAM_DATA_FLAG_NONE 0x00u
2758 
2759 /**
2760  * @macro
2761  *
2762  * :macro:`NGTCP2_STREAM_DATA_FLAG_FIN` indicates that this chunk of
2763  * data is final piece of an incoming stream.
2764  */
2765 #define NGTCP2_STREAM_DATA_FLAG_FIN 0x01u
2766 
2767 /**
2768  * @macro
2769  *
2770  * :macro:`NGTCP2_STREAM_DATA_FLAG_EARLY` indicates that this chunk of
2771  * data contains data received in 0RTT packet and the handshake has
2772  * not completed yet, which means that the data might be replayed.
2773  */
2774 #define NGTCP2_STREAM_DATA_FLAG_EARLY 0x02u
2775 
2776 /**
2777  * @functypedef
2778  *
2779  * :type:`ngtcp2_recv_stream_data` is invoked when stream data is
2780  * received.  The stream is specified by |stream_id|.  |flags| is the
2781  * bitwise-OR of zero or more of :macro:`NGTCP2_STREAM_DATA_FLAG_*
2782  * <NGTCP2_STREAM_DATA_FLAG_NONE>`.  If |flags| &
2783  * :macro:`NGTCP2_STREAM_DATA_FLAG_FIN` is nonzero, this portion of
2784  * the data is the last data in this stream.  |offset| is the offset
2785  * where this data begins.  The library ensures that data is passed to
2786  * the application in the non-decreasing order of |offset| without any
2787  * overlap.  The data is passed as |data| of length |datalen|.
2788  * |datalen| may be 0 if and only if |fin| is nonzero.
2789  *
2790  * If :macro:`NGTCP2_STREAM_DATA_FLAG_EARLY` is set in |flags|, it
2791  * indicates that a part of or whole data was received in 0RTT packet
2792  * and a handshake has not completed yet.
2793  *
2794  * The callback function must return 0 if it succeeds, or
2795  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
2796  * immediately.
2797  */
2798 typedef int (*ngtcp2_recv_stream_data)(ngtcp2_conn *conn, uint32_t flags,
2799                                        int64_t stream_id, uint64_t offset,
2800                                        const uint8_t *data, size_t datalen,
2801                                        void *user_data, void *stream_user_data);
2802 
2803 /**
2804  * @functypedef
2805  *
2806  * :type:`ngtcp2_stream_open` is a callback function which is called
2807  * when remote stream is opened by peer.  This function is not called
2808  * if stream is opened by implicitly (we might reconsider this
2809  * behaviour).
2810  *
2811  * The implementation of this callback should return 0 if it succeeds.
2812  * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
2813  * call return immediately.
2814  */
2815 typedef int (*ngtcp2_stream_open)(ngtcp2_conn *conn, int64_t stream_id,
2816                                   void *user_data);
2817 
2818 /**
2819  * @macrosection
2820  *
2821  * Stream close flags
2822  */
2823 
2824 /**
2825  * @macro
2826  *
2827  * :macro:`NGTCP2_STREAM_CLOSE_FLAG_NONE` indicates no flag set.
2828  */
2829 #define NGTCP2_STREAM_CLOSE_FLAG_NONE 0x00u
2830 
2831 /**
2832  * @macro
2833  *
2834  * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` indicates that
2835  * app_error_code parameter is set.
2836  */
2837 #define NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET 0x01u
2838 
2839 /**
2840  * @functypedef
2841  *
2842  * :type:`ngtcp2_stream_close` is invoked when a stream is closed.
2843  * This callback is not called when QUIC connection is closed before
2844  * existing streams are closed.  |flags| is the bitwise-OR of zero or
2845  * more of :macro:`NGTCP2_STREAM_CLOSE_FLAG_*
2846  * <NGTCP2_STREAM_CLOSE_FLAG_NONE>`.  |app_error_code| indicates the
2847  * error code of this closure if
2848  * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` is set in
2849  * |flags|.  If it is not set, the stream was closed without any error
2850  * code, which generally means success.
2851  *
2852  * |app_error_code| is the first application error code sent by a
2853  * local endpoint, or received from a remote endpoint.  If a stream is
2854  * closed cleanly, no application error code is exchanged.  Since QUIC
2855  * stack does not know the application error code which indicates "no
2856  * errors", |app_error_code| is set to 0 and
2857  * :macro:`NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET` is not set in
2858  * |flags| in this case.
2859  *
2860  * The implementation of this callback should return 0 if it succeeds.
2861  * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
2862  * call return immediately.
2863  */
2864 typedef int (*ngtcp2_stream_close)(ngtcp2_conn *conn, uint32_t flags,
2865                                    int64_t stream_id, uint64_t app_error_code,
2866                                    void *user_data, void *stream_user_data);
2867 
2868 /**
2869  * @functypedef
2870  *
2871  * :type:`ngtcp2_stream_reset` is invoked when a stream identified by
2872  * |stream_id| is reset by a remote endpoint.
2873  *
2874  * The implementation of this callback should return 0 if it succeeds.
2875  * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
2876  * call return immediately.
2877  */
2878 typedef int (*ngtcp2_stream_reset)(ngtcp2_conn *conn, int64_t stream_id,
2879                                    uint64_t final_size, uint64_t app_error_code,
2880                                    void *user_data, void *stream_user_data);
2881 
2882 /**
2883  * @functypedef
2884  *
2885  * :type:`ngtcp2_acked_stream_data_offset` is a callback function
2886  * which is called when stream data is acked, and application can free
2887  * the data.  The acked range of data is [offset, offset + datalen).
2888  * For a given stream_id, this callback is called sequentially in
2889  * increasing order of |offset| without any overlap.  |datalen| is
2890  * normally strictly greater than 0.  One exception is that when a
2891  * packet which includes STREAM frame which has fin flag set, and 0
2892  * length data, this callback is invoked with 0 passed as |datalen|.
2893  *
2894  * If a stream is closed prematurely and stream data is still
2895  * in-flight, this callback function is not called for those data.
2896  *
2897  * The implementation of this callback should return 0 if it succeeds.
2898  * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
2899  * call return immediately.
2900  */
2901 typedef int (*ngtcp2_acked_stream_data_offset)(
2902     ngtcp2_conn *conn, int64_t stream_id, uint64_t offset, uint64_t datalen,
2903     void *user_data, void *stream_user_data);
2904 
2905 /**
2906  * @functypedef
2907  *
2908  * :type:`ngtcp2_recv_stateless_reset` is a callback function which is
2909  * called when Stateless Reset packet is received.  The stateless
2910  * reset details are given in |sr|.
2911  *
2912  * The implementation of this callback should return 0 if it succeeds.
2913  * Returning :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library
2914  * call return immediately.
2915  */
2916 typedef int (*ngtcp2_recv_stateless_reset)(ngtcp2_conn *conn,
2917                                            const ngtcp2_pkt_stateless_reset *sr,
2918                                            void *user_data);
2919 
2920 /**
2921  * @functypedef
2922  *
2923  * :type:`ngtcp2_extend_max_streams` is a callback function which is
2924  * called every time max stream ID is strictly extended.
2925  * |max_streams| is the cumulative number of streams which an endpoint
2926  * can open.
2927  *
2928  * The callback function must return 0 if it succeeds.  Returning
2929  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
2930  * immediately.
2931  */
2932 typedef int (*ngtcp2_extend_max_streams)(ngtcp2_conn *conn,
2933                                          uint64_t max_streams, void *user_data);
2934 
2935 /**
2936  * @functypedef
2937  *
2938  * :type:`ngtcp2_extend_max_stream_data` is a callback function which
2939  * is invoked when max stream data is extended.  |stream_id|
2940  * identifies the stream.  |max_data| is a cumulative number of bytes
2941  * the endpoint can send on this stream.
2942  *
2943  * The callback function must return 0 if it succeeds.  Returning
2944  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
2945  * immediately.
2946  */
2947 typedef int (*ngtcp2_extend_max_stream_data)(ngtcp2_conn *conn,
2948                                              int64_t stream_id,
2949                                              uint64_t max_data, void *user_data,
2950                                              void *stream_user_data);
2951 
2952 /**
2953  * @functypedef
2954  *
2955  * :type:`ngtcp2_rand` is a callback function to get randomized byte
2956  * string from application.  Application must fill random |destlen|
2957  * bytes to the buffer pointed by |dest|.  The generated bytes are
2958  * used only in non-cryptographic context.
2959  */
2960 typedef void (*ngtcp2_rand)(uint8_t *dest, size_t destlen,
2961                             const ngtcp2_rand_ctx *rand_ctx);
2962 
2963 /**
2964  * @functypedef
2965  *
2966  * :type:`ngtcp2_get_new_connection_id` is a callback function to ask
2967  * an application for new connection ID.  Application must generate
2968  * new unused connection ID with the exact |cidlen| bytes and store it
2969  * in |cid|.  It also has to generate stateless reset token into
2970  * |token|.  The length of stateless reset token is
2971  * :macro:`NGTCP2_STATELESS_RESET_TOKENLEN` and it is guaranteed that
2972  * the buffer pointed by |cid| has the sufficient space to store the
2973  * token.
2974  *
2975  * The callback function must return 0 if it succeeds.  Returning
2976  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
2977  * immediately.
2978  */
2979 typedef int (*ngtcp2_get_new_connection_id)(ngtcp2_conn *conn, ngtcp2_cid *cid,
2980                                             uint8_t *token, size_t cidlen,
2981                                             void *user_data);
2982 
2983 /**
2984  * @functypedef
2985  *
2986  * :type:`ngtcp2_remove_connection_id` is a callback function which
2987  * notifies the application that connection ID |cid| is no longer used
2988  * by remote endpoint.
2989  *
2990  * The callback function must return 0 if it succeeds.  Returning
2991  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
2992  * immediately.
2993  */
2994 typedef int (*ngtcp2_remove_connection_id)(ngtcp2_conn *conn,
2995                                            const ngtcp2_cid *cid,
2996                                            void *user_data);
2997 
2998 /**
2999  * @functypedef
3000  *
3001  * :type:`ngtcp2_update_key` is a callback function which tells the
3002  * application that it must generate new packet protection keying
3003  * materials and AEAD cipher context objects with new keys.  The
3004  * current set of secrets are given as |current_rx_secret| and
3005  * |current_tx_secret| of length |secretlen|.  They are decryption and
3006  * encryption secrets respectively.
3007  *
3008  * The application has to generate new secrets and keys for both
3009  * encryption and decryption, and write decryption secret and IV to
3010  * the buffer pointed by |rx_secret| and |rx_iv| respectively.  It
3011  * also has to create new AEAD cipher context object with new
3012  * decryption key and initialize |rx_aead_ctx| with it.  Similarly,
3013  * write encryption secret and IV to the buffer pointed by |tx_secret|
3014  * and |tx_iv|.  Create new AEAD cipher context object with new
3015  * encryption key and initialize |tx_aead_ctx| with it.  All given
3016  * buffers have the enough capacity to store secret, key and IV.
3017  *
3018  * The callback function must return 0 if it succeeds.  Returning
3019  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3020  * immediately.
3021  */
3022 typedef int (*ngtcp2_update_key)(
3023     ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
3024     ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv,
3025     ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv,
3026     const uint8_t *current_rx_secret, const uint8_t *current_tx_secret,
3027     size_t secretlen, void *user_data);
3028 
3029 /**
3030  * @macrosection
3031  *
3032  * Path validation related macros
3033  */
3034 
3035 /**
3036  * @macro
3037  *
3038  * :macro:`NGTCP2_PATH_VALIDATION_FLAG_NONE` indicates no flag set.
3039  */
3040 #define NGTCP2_PATH_VALIDATION_FLAG_NONE 0x00u
3041 
3042 /**
3043  * @macro
3044  *
3045  * :macro:`NGTCP2_PATH_VALIDATION_FLAG_PREFERRED_ADDR` indicates the
3046  * validation involving server preferred address.  This flag is only
3047  * set for client.
3048  */
3049 #define NGTCP2_PATH_VALIDATION_FLAG_PREFERRED_ADDR 0x01u
3050 
3051 /**
3052  * @functypedef
3053  *
3054  * :type:`ngtcp2_path_validation` is a callback function which tells
3055  * the application the outcome of path validation.  |flags| is zero or
3056  * more of :macro:`NGTCP2_PATH_VALIDATION_FLAG_*
3057  * <NGTCP2_PATH_VALIDATION_FLAG_NONE>`.  |path| is the path that was
3058  * validated.  If |res| is
3059  * :enum:`ngtcp2_path_validation_result.NGTCP2_PATH_VALIDATION_RESULT_SUCCESS`,
3060  * the path validation succeeded.  If |res| is
3061  * :enum:`ngtcp2_path_validation_result.NGTCP2_PATH_VALIDATION_RESULT_FAILURE`,
3062  * the path validation failed.
3063  *
3064  * The callback function must return 0 if it succeeds.  Returning
3065  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3066  * immediately.
3067  */
3068 typedef int (*ngtcp2_path_validation)(ngtcp2_conn *conn, uint32_t flags,
3069                                       const ngtcp2_path *path,
3070                                       ngtcp2_path_validation_result res,
3071                                       void *user_data);
3072 
3073 /**
3074  * @functypedef
3075  *
3076  * :type:`ngtcp2_select_preferred_addr` is a callback function which
3077  * asks a client application to choose server address from preferred
3078  * addresses |paddr| received from server.  An application should
3079  * write a network path for a selected preferred address in |dest|.
3080  * More specifically, the selected preferred address must be set to
3081  * :member:`dest->remote <ngtcp2_path.remote>`, a client source
3082  * address must be set to :member:`dest->local <ngtcp2_path.local>`.
3083  * If a client source address does not change for the new server
3084  * address, leave :member:`dest->local <ngtcp2_path.local>`
3085  * unmodified, or copy the value of :member:`local
3086  * <ngtcp2_path.local>` field of the current network path obtained
3087  * from `ngtcp2_conn_get_path()`.  Both :member:`dest->local.addr
3088  * <ngtcp2_addr.addr>` and :member:`dest->remote.addr
3089  * <ngtcp2_addr.addr>` point to buffers which are at least
3090  * ``sizeof(struct sockaddr_storage)`` bytes long, respectively.  If
3091  * an application denies the preferred addresses, just leave |dest|
3092  * unmodified (or set :member:`dest->remote.addrlen
3093  * <ngtcp2_addr.addrlen>` to 0) and return 0.
3094  *
3095  * The callback function must return 0 if it succeeds.  Returning
3096  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3097  * immediately.
3098  */
3099 typedef int (*ngtcp2_select_preferred_addr)(ngtcp2_conn *conn,
3100                                             ngtcp2_path *dest,
3101                                             const ngtcp2_preferred_addr *paddr,
3102                                             void *user_data);
3103 
3104 /**
3105  * @enum
3106  *
3107  * :type:`ngtcp2_connection_id_status_type` defines a set of status
3108  * for Destination Connection ID.
3109  */
3110 typedef enum ngtcp2_connection_id_status_type {
3111   /**
3112    * :enum:`NGTCP2_CONNECTION_ID_STATUS_TYPE_ACTIVATE` indicates that
3113    * a local endpoint starts using new destination Connection ID.
3114    */
3115   NGTCP2_CONNECTION_ID_STATUS_TYPE_ACTIVATE,
3116   /**
3117    * :enum:`NGTCP2_CONNECTION_ID_STATUS_TYPE_DEACTIVATE` indicates
3118    * that a local endpoint stops using a given destination Connection
3119    * ID.
3120    */
3121   NGTCP2_CONNECTION_ID_STATUS_TYPE_DEACTIVATE
3122 } ngtcp2_connection_id_status_type;
3123 
3124 /**
3125  * @functypedef
3126  *
3127  * :type:`ngtcp2_connection_id_status` is a callback function which is
3128  * called when the status of Connection ID changes.
3129  *
3130  * |token| is the associated stateless reset token and it is ``NULL``
3131  * if no token is present.
3132  *
3133  * |type| is the one of the value defined in
3134  * :type:`ngtcp2_connection_id_status_type`.  The new value might be
3135  * added in the future release.
3136  *
3137  * The callback function must return 0 if it succeeds.  Returning
3138  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3139  * immediately.
3140  */
3141 typedef int (*ngtcp2_connection_id_status)(ngtcp2_conn *conn, int type,
3142                                            uint64_t seq, const ngtcp2_cid *cid,
3143                                            const uint8_t *token,
3144                                            void *user_data);
3145 
3146 /**
3147  * @functypedef
3148  *
3149  * :type:`ngtcp2_recv_new_token` is a callback function which is
3150  * called when new token is received from server.
3151  *
3152  * |token| is the received token.
3153  *
3154  * The callback function must return 0 if it succeeds.  Returning
3155  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3156  * immediately.
3157  */
3158 typedef int (*ngtcp2_recv_new_token)(ngtcp2_conn *conn, const ngtcp2_vec *token,
3159                                      void *user_data);
3160 
3161 /**
3162  * @functypedef
3163  *
3164  * :type:`ngtcp2_delete_crypto_aead_ctx` is a callback function which
3165  * must delete the native object pointed by
3166  * :member:`aead_ctx->native_handle
3167  * <ngtcp2_crypto_aead_ctx.native_handle>`.
3168  */
3169 typedef void (*ngtcp2_delete_crypto_aead_ctx)(ngtcp2_conn *conn,
3170                                               ngtcp2_crypto_aead_ctx *aead_ctx,
3171                                               void *user_data);
3172 
3173 /**
3174  * @functypedef
3175  *
3176  * :type:`ngtcp2_delete_crypto_cipher_ctx` is a callback function
3177  * which must delete the native object pointed by
3178  * :member:`cipher_ctx->native_handle
3179  * <ngtcp2_crypto_cipher_ctx.native_handle>`.
3180  */
3181 typedef void (*ngtcp2_delete_crypto_cipher_ctx)(
3182     ngtcp2_conn *conn, ngtcp2_crypto_cipher_ctx *cipher_ctx, void *user_data);
3183 
3184 /**
3185  * @macrosection
3186  *
3187  * Datagram flags
3188  */
3189 
3190 /**
3191  * @macro
3192  *
3193  * :macro:`NGTCP2_DATAGRAM_FLAG_NONE` indicates no flag set.
3194  */
3195 #define NGTCP2_DATAGRAM_FLAG_NONE 0x00u
3196 
3197 /**
3198  * @macro
3199  *
3200  * :macro:`NGTCP2_DATAGRAM_FLAG_EARLY` indicates that DATAGRAM frame
3201  * is received in 0RTT packet and the handshake has not completed yet,
3202  * which means that the data might be replayed.
3203  */
3204 #define NGTCP2_DATAGRAM_FLAG_EARLY 0x01u
3205 
3206 /**
3207  * @functypedef
3208  *
3209  * :type:`ngtcp2_recv_datagram` is invoked when DATAGRAM frame is
3210  * received.  |flags| is bitwise-OR of zero or more of
3211  * :macro:`NGTCP2_DATAGRAM_FLAG_* <NGTCP2_DATAGRAM_FLAG_NONE>`.
3212  *
3213  * If :macro:`NGTCP2_DATAGRAM_FLAG_EARLY` is set in |flags|, it
3214  * indicates that DATAGRAM frame was received in 0RTT packet and a
3215  * handshake has not completed yet.
3216  *
3217  * The callback function must return 0 if it succeeds, or
3218  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
3219  * immediately.
3220  */
3221 typedef int (*ngtcp2_recv_datagram)(ngtcp2_conn *conn, uint32_t flags,
3222                                     const uint8_t *data, size_t datalen,
3223                                     void *user_data);
3224 
3225 /**
3226  * @functypedef
3227  *
3228  * :type:`ngtcp2_ack_datagram` is invoked when a packet which contains
3229  * DATAGRAM frame which is identified by |dgram_id| is acknowledged.
3230  * |dgram_id| is the valued passed to `ngtcp2_conn_writev_datagram`.
3231  *
3232  * The callback function must return 0 if it succeeds, or
3233  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
3234  * immediately.
3235  */
3236 typedef int (*ngtcp2_ack_datagram)(ngtcp2_conn *conn, uint64_t dgram_id,
3237                                    void *user_data);
3238 
3239 /**
3240  * @functypedef
3241  *
3242  * :type:`ngtcp2_lost_datagram` is invoked when a packet which
3243  * contains DATAGRAM frame which is identified by |dgram_id| is
3244  * declared lost.  |dgram_id| is the valued passed to
3245  * `ngtcp2_conn_writev_datagram`.  Note that the loss might be
3246  * spurious, and DATAGRAM frame might be acknowledged later.
3247  *
3248  * The callback function must return 0 if it succeeds, or
3249  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` which makes the library return
3250  * immediately.
3251  */
3252 typedef int (*ngtcp2_lost_datagram)(ngtcp2_conn *conn, uint64_t dgram_id,
3253                                     void *user_data);
3254 
3255 /**
3256  * @functypedef
3257  *
3258  * :type:`ngtcp2_get_path_challenge_data` is a callback function to
3259  * ask an application for new data that is sent in PATH_CHALLENGE
3260  * frame.  Application must generate new unpredictable exactly
3261  * :macro:`NGTCP2_PATH_CHALLENGE_DATALEN` bytes of random data and
3262  * store them into the buffer pointed by |data|.
3263  *
3264  * The callback function must return 0 if it succeeds.  Returning
3265  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3266  * immediately.
3267  */
3268 typedef int (*ngtcp2_get_path_challenge_data)(ngtcp2_conn *conn, uint8_t *data,
3269                                               void *user_data);
3270 
3271 /**
3272  * @functypedef
3273  *
3274  * :type:`ngtcp2_stream_stop_sending` is invoked when a stream is no
3275  * longer read by a local endpoint before it receives all stream data.
3276  * This function is called at most once per stream.  |app_error_code|
3277  * is the error code passed to `ngtcp2_conn_shutdown_stream_read` or
3278  * `ngtcp2_conn_shutdown_stream`.
3279  *
3280  * The callback function must return 0 if it succeeds.  Returning
3281  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3282  * immediately.
3283  */
3284 typedef int (*ngtcp2_stream_stop_sending)(ngtcp2_conn *conn, int64_t stream_id,
3285                                           uint64_t app_error_code,
3286                                           void *user_data,
3287                                           void *stream_user_data);
3288 
3289 /**
3290  * @functypedef
3291  *
3292  * :type:`ngtcp2_version_negotiation` is invoked when the compatible
3293  * version negotiation takes place.  For client, it is called when it
3294  * sees a change in version field of a long header packet.  This
3295  * callback function might be called multiple times for client.  For
3296  * server, it is called once when the version is negotiated.
3297  *
3298  * The implementation of this callback must install new Initial keys
3299  * for |version|.  Use `ngtcp2_conn_install_vneg_initial_key` to
3300  * install keys.
3301  *
3302  * The callback function must return 0 if it succeeds.  Returning
3303  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3304  * immediately.
3305  */
3306 typedef int (*ngtcp2_version_negotiation)(ngtcp2_conn *conn, uint32_t version,
3307                                           const ngtcp2_cid *client_dcid,
3308                                           void *user_data);
3309 
3310 /**
3311  * @functypedef
3312  *
3313  * :type:`ngtcp2_recv_key` is invoked when new key is installed to
3314  * |conn| during QUIC cryptographic handshake.
3315  *
3316  * The callback function must return 0 if it succeeds.  Returning
3317  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE` makes the library call return
3318  * immediately.
3319  */
3320 typedef int (*ngtcp2_recv_key)(ngtcp2_conn *conn, ngtcp2_crypto_level level,
3321                                void *user_data);
3322 
3323 #define NGTCP2_CALLBACKS_VERSION_V1 1
3324 #define NGTCP2_CALLBACKS_VERSION NGTCP2_CALLBACKS_VERSION_V1
3325 
3326 /**
3327  * @struct
3328  *
3329  * :type:`ngtcp2_callbacks` holds a set of callback functions.
3330  */
3331 typedef struct ngtcp2_callbacks {
3332   /**
3333    * :member:`client_initial` is a callback function which is invoked
3334    * when client asks TLS stack to produce first TLS cryptographic
3335    * handshake message.  This callback function must be specified for
3336    * a client application.
3337    */
3338   ngtcp2_client_initial client_initial;
3339   /**
3340    * :member:`recv_client_initial` is a callback function which is
3341    * invoked when a server receives the first packet from client.
3342    * This callback function must be specified for a server application.
3343    */
3344   ngtcp2_recv_client_initial recv_client_initial;
3345   /**
3346    * :member:`recv_crypto_data` is a callback function which is
3347    * invoked when cryptographic data (CRYPTO frame, in other words,
3348    * TLS message) is received.  This callback function must be
3349    * specified.
3350    */
3351   ngtcp2_recv_crypto_data recv_crypto_data;
3352   /**
3353    * :member:`handshake_completed` is a callback function which is
3354    * invoked when QUIC cryptographic handshake has completed.  This
3355    * callback function is optional.
3356    */
3357   ngtcp2_handshake_completed handshake_completed;
3358   /**
3359    * :member:`recv_version_negotiation` is a callback function which
3360    * is invoked when Version Negotiation packet is received by a
3361    * client.  This callback function is optional.
3362    */
3363   ngtcp2_recv_version_negotiation recv_version_negotiation;
3364   /**
3365    * :member:`encrypt` is a callback function which is invoked to
3366    * encrypt a QUIC packet.  This callback function must be specified.
3367    */
3368   ngtcp2_encrypt encrypt;
3369   /**
3370    * :member:`decrypt` is a callback function which is invoked to
3371    * decrypt a QUIC packet.  This callback function must be specified.
3372    */
3373   ngtcp2_decrypt decrypt;
3374   /**
3375    * :member:`hp_mask` is a callback function which is invoked to get
3376    * a mask to encrypt or decrypt packet header.  This callback
3377    * function must be specified.
3378    */
3379   ngtcp2_hp_mask hp_mask;
3380   /**
3381    * :member:`recv_stream_data` is a callback function which is
3382    * invoked when STREAM data, which includes application data, is
3383    * received.  This callback function is optional.
3384    */
3385   ngtcp2_recv_stream_data recv_stream_data;
3386   /**
3387    * :member:`acked_stream_data_offset` is a callback function which
3388    * is invoked when STREAM data, which includes application data, is
3389    * acknowledged by a remote endpoint.  It tells an application the
3390    * largest offset of acknowledged STREAM data without a gap so that
3391    * application can free memory for the data.  This callback function
3392    * is optional.
3393    */
3394   ngtcp2_acked_stream_data_offset acked_stream_data_offset;
3395   /**
3396    * :member:`stream_open` is a callback function which is invoked
3397    * when new remote stream is opened by a remote endpoint.  This
3398    * callback function is optional.
3399    */
3400   ngtcp2_stream_open stream_open;
3401   /**
3402    * :member:`stream_close` is a callback function which is invoked
3403    * when a stream is closed.  This callback function is optional.
3404    */
3405   ngtcp2_stream_close stream_close;
3406   /**
3407    * :member:`recv_stateless_reset` is a callback function which is
3408    * invoked when Stateless Reset packet is received.  This callback
3409    * function is optional.
3410    */
3411   ngtcp2_recv_stateless_reset recv_stateless_reset;
3412   /**
3413    * :member:`recv_retry` is a callback function which is invoked when
3414    * a client receives Retry packet.  For client, this callback
3415    * function must be specified.  Server never receive Retry packet.
3416    */
3417   ngtcp2_recv_retry recv_retry;
3418   /**
3419    * :member:`extend_max_local_streams_bidi` is a callback function
3420    * which is invoked when the number of bidirectional stream which a
3421    * local endpoint can open is increased.  This callback function is
3422    * optional.
3423    */
3424   ngtcp2_extend_max_streams extend_max_local_streams_bidi;
3425   /**
3426    * :member:`extend_max_local_streams_uni` is a callback function
3427    * which is invoked when the number of unidirectional stream which a
3428    * local endpoint can open is increased.  This callback function is
3429    * optional.
3430    */
3431   ngtcp2_extend_max_streams extend_max_local_streams_uni;
3432   /**
3433    * :member:`rand` is a callback function which is invoked when the
3434    * library needs sequence of random data.  This callback function
3435    * must be specified.
3436    */
3437   ngtcp2_rand rand;
3438   /**
3439    * :member:`get_new_connection_id` is a callback function which is
3440    * invoked when the library needs new connection ID.  This callback
3441    * function must be specified.
3442    */
3443   ngtcp2_get_new_connection_id get_new_connection_id;
3444   /**
3445    * :member:`remove_connection_id` is a callback function which
3446    * notifies an application that connection ID is no longer used by a
3447    * remote endpoint.  This callback function is optional.
3448    */
3449   ngtcp2_remove_connection_id remove_connection_id;
3450   /**
3451    * :member:`update_key` is a callback function which is invoked when
3452    * the library tells an application that it must update keying
3453    * materials and install new keys.  This callback function must be
3454    * specified.
3455    */
3456   ngtcp2_update_key update_key;
3457   /**
3458    * :member:`path_validation` is a callback function which is invoked
3459    * when path validation completed.  This callback function is
3460    * optional.
3461    */
3462   ngtcp2_path_validation path_validation;
3463   /**
3464    * :member:`select_preferred_addr` is a callback function which is
3465    * invoked when the library asks a client to select preferred
3466    * address presented by a server.  This callback function is
3467    * optional.
3468    */
3469   ngtcp2_select_preferred_addr select_preferred_addr;
3470   /**
3471    * :member:`stream_reset` is a callback function which is invoked
3472    * when a stream is reset by a remote endpoint.  This callback
3473    * function is optional.
3474    */
3475   ngtcp2_stream_reset stream_reset;
3476   /**
3477    * :member:`extend_max_remote_streams_bidi` is a callback function
3478    * which is invoked when the number of bidirectional streams which a
3479    * remote endpoint can open is increased.  This callback function is
3480    * optional.
3481    */
3482   ngtcp2_extend_max_streams extend_max_remote_streams_bidi;
3483   /**
3484    * :member:`extend_max_remote_streams_uni` is a callback function
3485    * which is invoked when the number of unidirectional streams which
3486    * a remote endpoint can open is increased.  This callback function
3487    * is optional.
3488    */
3489   ngtcp2_extend_max_streams extend_max_remote_streams_uni;
3490   /**
3491    * :member:`extend_max_stream_data` is callback function which is
3492    * invoked when the maximum offset of STREAM data that a local
3493    * endpoint can send is increased.  This callback function is
3494    * optional.
3495    */
3496   ngtcp2_extend_max_stream_data extend_max_stream_data;
3497   /**
3498    * :member:`dcid_status` is a callback function which is invoked
3499    * when the new destination Connection ID is activated or the
3500    * activated destination Connection ID is now deactivated.  This
3501    * callback function is optional.
3502    */
3503   ngtcp2_connection_id_status dcid_status;
3504   /**
3505    * :member:`handshake_confirmed` is a callback function which is
3506    * invoked when both endpoints agree that handshake has finished.
3507    * This field is ignored by server because handshake_completed
3508    * indicates the handshake confirmation for server.  This callback
3509    * function is optional.
3510    */
3511   ngtcp2_handshake_confirmed handshake_confirmed;
3512   /**
3513    * :member:`recv_new_token` is a callback function which is invoked
3514    * when new token is received from server.  This field is ignored by
3515    * server.  This callback function is optional.
3516    */
3517   ngtcp2_recv_new_token recv_new_token;
3518   /**
3519    * :member:`delete_crypto_aead_ctx` is a callback function which
3520    * deletes a given AEAD cipher context object.  This callback
3521    * function must be specified.
3522    */
3523   ngtcp2_delete_crypto_aead_ctx delete_crypto_aead_ctx;
3524   /**
3525    * :member:`delete_crypto_cipher_ctx` is a callback function which
3526    * deletes a given cipher context object.  This callback function
3527    * must be specified.
3528    */
3529   ngtcp2_delete_crypto_cipher_ctx delete_crypto_cipher_ctx;
3530   /**
3531    * :member:`recv_datagram` is a callback function which is invoked
3532    * when DATAGRAM frame is received.  This callback function is
3533    * optional.
3534    */
3535   ngtcp2_recv_datagram recv_datagram;
3536   /**
3537    * :member:`ack_datagram` is a callback function which is invoked
3538    * when a packet containing DATAGRAM frame is acknowledged.  This
3539    * callback function is optional.
3540    */
3541   ngtcp2_ack_datagram ack_datagram;
3542   /**
3543    * :member:`lost_datagram` is a callback function which is invoked
3544    * when a packet containing DATAGRAM frame is declared lost.  This
3545    * callback function is optional.
3546    */
3547   ngtcp2_lost_datagram lost_datagram;
3548   /**
3549    * :member:`get_path_challenge_data` is a callback function which is
3550    * invoked when the library needs new PATH_CHALLENGE data.  This
3551    * callback must be specified.
3552    */
3553   ngtcp2_get_path_challenge_data get_path_challenge_data;
3554   /**
3555    * :member:`stream_stop_sending` is a callback function which is
3556    * invoked when a local endpoint no longer reads from a stream
3557    * before it receives all stream data.  This callback function is
3558    * optional.
3559    */
3560   ngtcp2_stream_stop_sending stream_stop_sending;
3561   /**
3562    * :member:`version_negotiation` is a callback function which is
3563    * invoked when the compatible version negotiation takes place.
3564    * This callback function must be specified.
3565    */
3566   ngtcp2_version_negotiation version_negotiation;
3567   /**
3568    * :member:`recv_rx_key` is a callback function which is invoked
3569    * when a new key for decrypting packets is installed during QUIC
3570    * cryptographic handshake.  It is not called for
3571    * :enum:`ngtcp2_crypto_level.NGTCP2_CRYPTO_LEVEL_INITIAL`.
3572    */
3573   ngtcp2_recv_key recv_rx_key;
3574   /**
3575    * :member:`recv_tx_key` is a callback function which is invoked
3576    * when a new key for encrypting packets is installed during QUIC
3577    * cryptographic handshake.  It is not called for
3578    * :enum:`ngtcp2_crypto_level.NGTCP2_CRYPTO_LEVEL_INITIAL`.
3579    */
3580   ngtcp2_recv_key recv_tx_key;
3581 } ngtcp2_callbacks;
3582 
3583 /**
3584  * @function
3585  *
3586  * `ngtcp2_pkt_write_connection_close` writes Initial packet
3587  * containing CONNECTION_CLOSE frame with the given |error_code| and
3588  * the optional |reason| of length |reasonlen| to the buffer pointed
3589  * by |dest| of length |destlen|.  All encryption parameters are for
3590  * Initial packet encryption.  The packet number is always 0.
3591  *
3592  * The primary use case of this function is for server to send
3593  * CONNECTION_CLOSE frame in Initial packet to close connection
3594  * without committing the state when validating Retry token fails.
3595  *
3596  * This function returns the number of bytes written if it succeeds,
3597  * or one of the following negative error codes:
3598  *
3599  * :macro:`NGTCP2_ERR_NOBUF`
3600  *     Buffer is too small.
3601  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
3602  *     Callback function failed.
3603  */
3604 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_connection_close(
3605     uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid,
3606     const ngtcp2_cid *scid, uint64_t error_code, const uint8_t *reason,
3607     size_t reasonlen, ngtcp2_encrypt encrypt, const ngtcp2_crypto_aead *aead,
3608     const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv,
3609     ngtcp2_hp_mask hp_mask, const ngtcp2_crypto_cipher *hp,
3610     const ngtcp2_crypto_cipher_ctx *hp_ctx);
3611 
3612 /**
3613  * @function
3614  *
3615  * `ngtcp2_pkt_write_retry` writes Retry packet in the buffer pointed
3616  * by |dest| whose length is |destlen|.  |dcid| is the destination
3617  * connection ID which appeared in a packet as a source connection ID
3618  * sent by client.  |scid| is a server chosen source connection ID.
3619  * |odcid| specifies Original Destination Connection ID which appeared
3620  * in a packet as a destination connection ID sent by client.  |token|
3621  * specifies Retry Token, and |tokenlen| specifies its length.  |aead|
3622  * must be AEAD_AES_128_GCM.  |aead_ctx| must be initialized with
3623  * :macro:`NGTCP2_RETRY_KEY` as an encryption key.
3624  *
3625  * This function returns the number of bytes written to the buffer, or
3626  * one of the following negative error codes:
3627  *
3628  * :macro:`NGTCP2_ERR_NOBUF`
3629  *     Buffer is too small.
3630  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
3631  *     Callback function failed.
3632  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
3633  *     :member:`odcid->datalen <ngtcp2_cid.datalen>` is less than
3634  *     :macro:`NGTCP2_MIN_INITIAL_DCIDLEN`.
3635  */
3636 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_pkt_write_retry(
3637     uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid,
3638     const ngtcp2_cid *scid, const ngtcp2_cid *odcid, const uint8_t *token,
3639     size_t tokenlen, ngtcp2_encrypt encrypt, const ngtcp2_crypto_aead *aead,
3640     const ngtcp2_crypto_aead_ctx *aead_ctx);
3641 
3642 /**
3643  * @function
3644  *
3645  * `ngtcp2_accept` is used by server implementation, and decides
3646  * whether packet |pkt| of length |pktlen| from client is acceptable
3647  * for the very initial packet to a connection.
3648  *
3649  * If |dest| is not ``NULL`` and the function returns 0, or
3650  * :macro:`NGTCP2_ERR_RETRY`, the decoded packet header is stored to
3651  * the object pointed by |dest|.
3652  *
3653  * This function returns 0 if it succeeds, or one of the following
3654  * negative error codes:
3655  *
3656  * :macro:`NGTCP2_ERR_RETRY`
3657  *     Retry packet should be sent.
3658  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
3659  *     The packet is not acceptable for the very first packet to a new
3660  *     connection; or the function failed to parse the packet header.
3661  */
3662 NGTCP2_EXTERN int ngtcp2_accept(ngtcp2_pkt_hd *dest, const uint8_t *pkt,
3663                                 size_t pktlen);
3664 
3665 /**
3666  * @function
3667  *
3668  * `ngtcp2_conn_client_new` creates new :type:`ngtcp2_conn`, and
3669  * initializes it as client.  |dcid| is randomized destination
3670  * connection ID.  |scid| is source connection ID.
3671  * |client_chosen_version| is a QUIC version that a client chooses.
3672  * |path| is the network path where this QUIC connection is being
3673  * established and must not be ``NULL``.  |callbacks|, |settings|, and
3674  * |params| must not be ``NULL``, and the function make a copy of each
3675  * of them.  |params| is local QUIC transport parameters and sent to a
3676  * remote endpoint during handshake.  |user_data| is the arbitrary
3677  * pointer which is passed to the user-defined callback functions.  If
3678  * |mem| is ``NULL``, the memory allocator returned by
3679  * `ngtcp2_mem_default()` is used.
3680  *
3681  * This function returns 0 if it succeeds, or one of the following
3682  * negative error codes:
3683  *
3684  * :macro:`NGTCP2_ERR_NOMEM`
3685  *     Out of memory.
3686  */
3687 NGTCP2_EXTERN int ngtcp2_conn_client_new_versioned(
3688     ngtcp2_conn **pconn, const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
3689     const ngtcp2_path *path, uint32_t client_chosen_version,
3690     int callbacks_version, const ngtcp2_callbacks *callbacks,
3691     int settings_version, const ngtcp2_settings *settings,
3692     int transport_params_version, const ngtcp2_transport_params *params,
3693     const ngtcp2_mem *mem, void *user_data);
3694 
3695 /**
3696  * @function
3697  *
3698  * `ngtcp2_conn_server_new` creates new :type:`ngtcp2_conn`, and
3699  * initializes it as server.  |dcid| is a destination connection ID.
3700  * |scid| is a source connection ID.  |path| is the network path where
3701  * this QUIC connection is being established and must not be ``NULL``.
3702  * |client_chosen_version| is a QUIC version that a client chooses.
3703  * |callbacks|, |settings|, and |params| must not be ``NULL``, and the
3704  * function make a copy of each of them.  |params| is local QUIC
3705  * transport parameters and sent to a remote endpoint during
3706  * handshake.  |user_data| is the arbitrary pointer which is passed to
3707  * the user-defined callback functions.  If |mem| is ``NULL``, the
3708  * memory allocator returned by `ngtcp2_mem_default()` is used.
3709  *
3710  * This function returns 0 if it succeeds, or one of the following
3711  * negative error codes:
3712  *
3713  * :macro:`NGTCP2_ERR_NOMEM`
3714  *     Out of memory.
3715  */
3716 NGTCP2_EXTERN int ngtcp2_conn_server_new_versioned(
3717     ngtcp2_conn **pconn, const ngtcp2_cid *dcid, const ngtcp2_cid *scid,
3718     const ngtcp2_path *path, uint32_t client_chosen_version,
3719     int callbacks_version, const ngtcp2_callbacks *callbacks,
3720     int settings_version, const ngtcp2_settings *settings,
3721     int transport_params_version, const ngtcp2_transport_params *params,
3722     const ngtcp2_mem *mem, void *user_data);
3723 
3724 /**
3725  * @function
3726  *
3727  * `ngtcp2_conn_del` frees resources allocated for |conn|.  It also
3728  * frees memory pointed by |conn|.
3729  */
3730 NGTCP2_EXTERN void ngtcp2_conn_del(ngtcp2_conn *conn);
3731 
3732 /**
3733  * @function
3734  *
3735  * `ngtcp2_conn_read_pkt` decrypts QUIC packet given in |pkt| of
3736  * length |pktlen| and processes it.  |path| is the network path the
3737  * packet is delivered and must not be ``NULL``.  |pi| is packet
3738  * metadata and may be ``NULL``. This function performs QUIC handshake
3739  * as well.
3740  *
3741  * This function must not be called from inside the callback
3742  * functions.
3743  *
3744  * This function returns 0 if it succeeds, or negative error codes.
3745  * If :macro:`NGTCP2_ERR_RETRY` is returned, application must be a
3746  * server and it must perform address validation by sending Retry
3747  * packet and discard the connection state.  If
3748  * :macro:`NGTCP2_ERR_DROP_CONN` is returned, server application must
3749  * drop the connection silently (without sending any CONNECTION_CLOSE
3750  * frame) and discard connection state.  If
3751  * :macro:`NGTCP2_ERR_DRAINING` is returned, a connection has entered
3752  * the draining state, and no further packet transmission is allowed.
3753  * If :macro:`NGTCP2_ERR_CRYPTO` is returned, the error happened in
3754  * TLS stack and `ngtcp2_conn_get_tls_alert` returns TLS alert if set.
3755  *
3756  * If any other negative errors are returned, call
3757  * `ngtcp2_conn_write_connection_close` to get terminal packet, and
3758  * sending it makes QUIC connection enter the closing state.
3759  */
3760 NGTCP2_EXTERN int
3761 ngtcp2_conn_read_pkt_versioned(ngtcp2_conn *conn, const ngtcp2_path *path,
3762                                int pkt_info_version, const ngtcp2_pkt_info *pi,
3763                                const uint8_t *pkt, size_t pktlen,
3764                                ngtcp2_tstamp ts);
3765 
3766 /**
3767  * @function
3768  *
3769  * `ngtcp2_conn_write_pkt` is equivalent to calling
3770  * `ngtcp2_conn_writev_stream` with -1 as stream_id, no stream data, and
3771  * :macro:`NGTCP2_WRITE_STREAM_FLAG_NONE` as flags.
3772  */
3773 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_pkt_versioned(
3774     ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
3775     ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_tstamp ts);
3776 
3777 /**
3778  * @function
3779  *
3780  * `ngtcp2_conn_handshake_completed` tells |conn| that the TLS stack
3781  * declares TLS handshake completion.  This does not mean QUIC
3782  * handshake has completed.  The library needs extra conditions to be
3783  * met.
3784  */
3785 NGTCP2_EXTERN void ngtcp2_conn_handshake_completed(ngtcp2_conn *conn);
3786 
3787 /**
3788  * @function
3789  *
3790  * `ngtcp2_conn_get_handshake_completed` returns nonzero if QUIC handshake
3791  * has completed.
3792  */
3793 NGTCP2_EXTERN int ngtcp2_conn_get_handshake_completed(ngtcp2_conn *conn);
3794 
3795 /**
3796  * @function
3797  *
3798  * `ngtcp2_conn_install_initial_key` installs packet protection keying
3799  * materials for Initial packets.  |rx_aead_ctx| is AEAD cipher
3800  * context object and must be initialized with a decryption key.
3801  * |rx_iv| is IV of length |rx_ivlen| for decryption.  |rx_hp_ctx| is
3802  * a packet header protection cipher context object for decryption.
3803  * Similarly, |tx_aead_ctx|, |tx_iv| and |tx_hp_ctx| are for
3804  * encrypting outgoing packets and are the same length with the
3805  * decryption counterpart .  If they have already been set, they are
3806  * overwritten.
3807  *
3808  * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
3809  * that is larger.
3810  *
3811  * If this function succeeds, |conn| takes ownership of |rx_aead_ctx|,
3812  * |rx_hp_ctx|, |tx_aead_ctx|, and |tx_hp_ctx|.
3813  * :type:`ngtcp2_delete_crypto_aead_ctx` and
3814  * :type:`ngtcp2_delete_crypto_cipher_ctx` will be called to delete
3815  * these objects when they are no longer used.  If this function
3816  * fails, the caller is responsible to delete them.
3817  *
3818  * After receiving Retry packet, the DCID most likely changes.  In
3819  * that case, client application must generate these keying materials
3820  * again based on new DCID and install them again.
3821  *
3822  * This function returns 0 if it succeeds, or one of the following
3823  * negative error codes:
3824  *
3825  * :macro:`NGTCP2_ERR_NOMEM`
3826  *     Out of memory.
3827  */
3828 NGTCP2_EXTERN int ngtcp2_conn_install_initial_key(
3829     ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *rx_aead_ctx,
3830     const uint8_t *rx_iv, const ngtcp2_crypto_cipher_ctx *rx_hp_ctx,
3831     const ngtcp2_crypto_aead_ctx *tx_aead_ctx, const uint8_t *tx_iv,
3832     const ngtcp2_crypto_cipher_ctx *tx_hp_ctx, size_t ivlen);
3833 
3834 /**
3835  * @function
3836  *
3837  * `ngtcp2_conn_install_vneg_initial_key` installs packet protection
3838  * keying materials for Initial packets on compatible version
3839  * negotiation for |version|.  |rx_aead_ctx| is AEAD cipher context
3840  * object and must be initialized with a decryption key.  |rx_iv| is
3841  * IV of length |rx_ivlen| for decryption.  |rx_hp_ctx| is a packet
3842  * header protection cipher context object for decryption.  Similarly,
3843  * |tx_aead_ctx|, |tx_iv| and |tx_hp_ctx| are for encrypting outgoing
3844  * packets and are the same length with the decryption counterpart .
3845  * If they have already been set, they are overwritten.
3846  *
3847  * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
3848  * that is larger.
3849  *
3850  * If this function succeeds, |conn| takes ownership of |rx_aead_ctx|,
3851  * |rx_hp_ctx|, |tx_aead_ctx|, and |tx_hp_ctx|.
3852  * :type:`ngtcp2_delete_crypto_aead_ctx` and
3853  * :type:`ngtcp2_delete_crypto_cipher_ctx` will be called to delete
3854  * these objects when they are no longer used.  If this function
3855  * fails, the caller is responsible to delete them.
3856  *
3857  * This function returns 0 if it succeeds, or one of the following
3858  * negative error codes:
3859  *
3860  * :macro:`NGTCP2_ERR_NOMEM`
3861  *     Out of memory.
3862  */
3863 NGTCP2_EXTERN int ngtcp2_conn_install_vneg_initial_key(
3864     ngtcp2_conn *conn, uint32_t version,
3865     const ngtcp2_crypto_aead_ctx *rx_aead_ctx, const uint8_t *rx_iv,
3866     const ngtcp2_crypto_cipher_ctx *rx_hp_ctx,
3867     const ngtcp2_crypto_aead_ctx *tx_aead_ctx, const uint8_t *tx_iv,
3868     const ngtcp2_crypto_cipher_ctx *tx_hp_ctx, size_t ivlen);
3869 
3870 /**
3871  * @function
3872  *
3873  * `ngtcp2_conn_install_rx_handshake_key` installs packet protection
3874  * keying materials for decrypting incoming Handshake packets.
3875  * |aead_ctx| is AEAD cipher context object which must be initialized
3876  * with a decryption key.  |iv| is IV of length |ivlen|.  |hp_ctx| is
3877  * a packet header protection cipher context object.
3878  *
3879  * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
3880  * that is larger.
3881  *
3882  * If this function succeeds, |conn| takes ownership of |aead_ctx|,
3883  * and |hp_ctx|.  :type:`ngtcp2_delete_crypto_aead_ctx` and
3884  * :type:`ngtcp2_delete_crypto_cipher_ctx` will be called to delete
3885  * these objects when they are no longer used.  If this function
3886  * fails, the caller is responsible to delete them.
3887  *
3888  * This function returns 0 if it succeeds, or one of the following
3889  * negative error codes:
3890  *
3891  * :macro:`NGTCP2_ERR_NOMEM`
3892  *     Out of memory.
3893  */
3894 NGTCP2_EXTERN int ngtcp2_conn_install_rx_handshake_key(
3895     ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
3896     const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
3897 
3898 /**
3899  * @function
3900  *
3901  * `ngtcp2_conn_install_tx_handshake_key` installs packet protection
3902  * keying materials for encrypting outgoing Handshake packets.
3903  * |aead_ctx| is AEAD cipher context object which must be initialized
3904  * with an encryption key.  |iv| is IV of length |ivlen|.  |hp_ctx| is
3905  * a packet header protection cipher context object.
3906  *
3907  * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
3908  * that is larger.
3909  *
3910  * If this function succeeds, |conn| takes ownership of |aead_ctx| and
3911  * |hp_ctx|.  :type:`ngtcp2_delete_crypto_aead_ctx` and
3912  * :type:`ngtcp2_delete_crypto_cipher_ctx` will be called to delete
3913  * these objects when they are no longer used.  If this function
3914  * fails, the caller is responsible to delete them.
3915  *
3916  * This function returns 0 if it succeeds, or one of the following
3917  * negative error codes:
3918  *
3919  * :macro:`NGTCP2_ERR_NOMEM`
3920  *     Out of memory.
3921  */
3922 NGTCP2_EXTERN int ngtcp2_conn_install_tx_handshake_key(
3923     ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
3924     const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
3925 
3926 /**
3927  * @function
3928  *
3929  * `ngtcp2_conn_install_early_key` installs packet protection AEAD
3930  * cipher context object |aead_ctx|, IV |iv| of length |ivlen|, and
3931  * packet header protection cipher context object |hp_ctx| to encrypt
3932  * (for client) or decrypt (for server) 0RTT packets.
3933  *
3934  * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
3935  * that is larger.
3936  *
3937  * If this function succeeds, |conn| takes ownership of |aead_ctx| and
3938  * |hp_ctx|.  :type:`ngtcp2_delete_crypto_aead_ctx` and
3939  * :type:`ngtcp2_delete_crypto_cipher_ctx` will be called to delete
3940  * these objects when they are no longer used.  If this function
3941  * fails, the caller is responsible to delete them.
3942  *
3943  * This function returns 0 if it succeeds, or one of the following
3944  * negative error codes:
3945  *
3946  * :macro:`NGTCP2_ERR_NOMEM`
3947  *     Out of memory.
3948  */
3949 NGTCP2_EXTERN int ngtcp2_conn_install_early_key(
3950     ngtcp2_conn *conn, const ngtcp2_crypto_aead_ctx *aead_ctx,
3951     const uint8_t *iv, size_t ivlen, const ngtcp2_crypto_cipher_ctx *hp_ctx);
3952 
3953 /**
3954  * @function
3955  *
3956  * `ngtcp2_conn_install_rx_key` installs packet protection keying
3957  * materials for decrypting Short header packets.  |secret| of length
3958  * |secretlen| is the decryption secret which is used to derive keying
3959  * materials passed to this function.  |aead_ctx| is AEAD cipher
3960  * context object which must be initialized with a decryption key.
3961  * |iv| is IV of length |ivlen|.  |hp_ctx| is a packet header
3962  * protection cipher context object.
3963  *
3964  * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
3965  * that is larger.
3966  *
3967  * If this function succeeds, |conn| takes ownership of |aead_ctx| and
3968  * |hp_ctx|.  :type:`ngtcp2_delete_crypto_aead_ctx` and
3969  * :type:`ngtcp2_delete_crypto_cipher_ctx` will be called to delete
3970  * these objects when they are no longer used.  If this function
3971  * fails, the caller is responsible to delete them.
3972  *
3973  * This function returns 0 if it succeeds, or one of the following
3974  * negative error codes:
3975  *
3976  * :macro:`NGTCP2_ERR_NOMEM`
3977  *     Out of memory.
3978  */
3979 NGTCP2_EXTERN int ngtcp2_conn_install_rx_key(
3980     ngtcp2_conn *conn, const uint8_t *secret, size_t secretlen,
3981     const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv, size_t ivlen,
3982     const ngtcp2_crypto_cipher_ctx *hp_ctx);
3983 
3984 /**
3985  * @function
3986  *
3987  * `ngtcp2_conn_install_tx_key` installs packet protection keying
3988  * materials for encrypting Short header packets.  |secret| of length
3989  * |secretlen| is the encryption secret which is used to derive keying
3990  * materials passed to this function.  |aead_ctx| is AEAD cipher
3991  * context object which must be initialized with an encryption key.
3992  * |iv| is IV of length |ivlen|.  |hp_ctx| is a packet header
3993  * protection cipher context object.
3994  *
3995  * |ivlen| must be the minimum length of AEAD nonce, or 8 bytes if
3996  * that is larger.
3997  *
3998  * If this function succeeds, |conn| takes ownership of |aead_ctx| and
3999  * |hp_ctx|.  :type:`ngtcp2_delete_crypto_aead_ctx` and
4000  * :type:`ngtcp2_delete_crypto_cipher_ctx` will be called to delete
4001  * these objects when they are no longer used.  If this function
4002  * fails, the caller is responsible to delete them.
4003  *
4004  * This function returns 0 if it succeeds, or one of the following
4005  * negative error codes:
4006  *
4007  * :macro:`NGTCP2_ERR_NOMEM`
4008  *     Out of memory.
4009  */
4010 NGTCP2_EXTERN int ngtcp2_conn_install_tx_key(
4011     ngtcp2_conn *conn, const uint8_t *secret, size_t secretlen,
4012     const ngtcp2_crypto_aead_ctx *aead_ctx, const uint8_t *iv, size_t ivlen,
4013     const ngtcp2_crypto_cipher_ctx *hp_ctx);
4014 
4015 /**
4016  * @function
4017  *
4018  * `ngtcp2_conn_initiate_key_update` initiates the key update.
4019  *
4020  * This function returns 0 if it succeeds, or one of the following
4021  * negative error codes:
4022  *
4023  * :macro:`NGTCP2_ERR_INVALID_STATE`
4024  *     The previous key update has not been confirmed yet; or key
4025  *     update is too frequent; or new keys are not available yet.
4026  */
4027 NGTCP2_EXTERN int ngtcp2_conn_initiate_key_update(ngtcp2_conn *conn,
4028                                                   ngtcp2_tstamp ts);
4029 
4030 /**
4031  * @function
4032  *
4033  * `ngtcp2_conn_set_tls_error` sets the TLS related error |liberr| in
4034  * |conn|.  |liberr| must be one of ngtcp2 library error codes (which
4035  * is defined as NGTCP2_ERR_* macro, such as
4036  * :macro:`NGTCP2_ERR_DECRYPT`).  In general, error code should be
4037  * propagated via return value, but sometimes ngtcp2 API is called
4038  * inside callback function of TLS stack and it does not allow to
4039  * return ngtcp2 error code directly.  In this case, implementation
4040  * can set the error code (e.g.,
4041  * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`) using this function.
4042  */
4043 NGTCP2_EXTERN void ngtcp2_conn_set_tls_error(ngtcp2_conn *conn, int liberr);
4044 
4045 /**
4046  * @function
4047  *
4048  * `ngtcp2_conn_get_tls_error` returns the value set by
4049  * `ngtcp2_conn_set_tls_error`.  If no value is set, this function
4050  * returns 0.
4051  */
4052 NGTCP2_EXTERN int ngtcp2_conn_get_tls_error(ngtcp2_conn *conn);
4053 
4054 /**
4055  * @function
4056  *
4057  * `ngtcp2_conn_set_tls_alert` sets a TLS alert |alert| generated by a
4058  * local endpoint to |conn|.
4059  */
4060 NGTCP2_EXTERN void ngtcp2_conn_set_tls_alert(ngtcp2_conn *conn, uint8_t alert);
4061 
4062 /**
4063  * @function
4064  *
4065  * `ngtcp2_conn_get_tls_alert` returns the value set by
4066  * `ngtcp2_conn_set_tls_alert`.  If no value is set, this function
4067  * returns 0.
4068  */
4069 NGTCP2_EXTERN uint8_t ngtcp2_conn_get_tls_alert(ngtcp2_conn *conn);
4070 
4071 /**
4072  * @function
4073  *
4074  * `ngtcp2_conn_set_keep_alive_timeout` sets keep-alive timeout.  If
4075  * nonzero value is given, after a connection is idle at least in a
4076  * given amount of time, a keep-alive packet is sent.  If 0 is set,
4077  * keep-alive functionality is disabled and this is the default.
4078  */
4079 NGTCP2_EXTERN void ngtcp2_conn_set_keep_alive_timeout(ngtcp2_conn *conn,
4080                                                       ngtcp2_duration timeout);
4081 
4082 /**
4083  * @function
4084  *
4085  * `ngtcp2_conn_get_expiry` returns the next expiry time.
4086  *
4087  * Call `ngtcp2_conn_handle_expiry()` and `ngtcp2_conn_write_pkt` (or
4088  * `ngtcp2_conn_writev_stream`) if expiry time is passed.
4089  */
4090 NGTCP2_EXTERN ngtcp2_tstamp ngtcp2_conn_get_expiry(ngtcp2_conn *conn);
4091 
4092 /**
4093  * @function
4094  *
4095  * `ngtcp2_conn_handle_expiry` handles expired timer.  It does nothing
4096  * if timer is not expired.
4097  */
4098 NGTCP2_EXTERN int ngtcp2_conn_handle_expiry(ngtcp2_conn *conn,
4099                                             ngtcp2_tstamp ts);
4100 
4101 /**
4102  * @function
4103  *
4104  * `ngtcp2_conn_get_pto` returns Probe Timeout (PTO).
4105  */
4106 NGTCP2_EXTERN ngtcp2_duration ngtcp2_conn_get_pto(ngtcp2_conn *conn);
4107 
4108 /**
4109  * @function
4110  *
4111  * `ngtcp2_conn_decode_remote_transport_params` decodes QUIC transport
4112  * parameters from the buffer pointed by |data| of length |datalen|,
4113  * and sets the result to |conn|.
4114  *
4115  * This function returns 0 if it succeeds, or one of the following
4116  * negative error codes:
4117  *
4118  * :macro:`NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM`
4119  *     The required parameter is missing.
4120  * :macro:`NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM`
4121  *     The input is malformed.
4122  * :macro:`NGTCP2_ERR_TRANSPORT_PARAM`
4123  *     Failed to validate the remote QUIC transport parameters.
4124  * :macro:`NGTCP2_ERR_VERSION_NEGOTIATION_FAILURE`
4125  *     Version negotiation failure.
4126  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
4127  *     User callback failed
4128  */
4129 NGTCP2_EXTERN int
4130 ngtcp2_conn_decode_remote_transport_params(ngtcp2_conn *conn,
4131                                            const uint8_t *data, size_t datalen);
4132 
4133 /**
4134  * @function
4135  *
4136  * `ngtcp2_conn_get_remote_transport_params` returns a pointer to the
4137  * remote QUIC transport parameters.  If no remote transport
4138  * parameters are set, it returns NULL.
4139  */
4140 NGTCP2_EXTERN const ngtcp2_transport_params *
4141 ngtcp2_conn_get_remote_transport_params(ngtcp2_conn *conn);
4142 
4143 /**
4144  * @function
4145  *
4146  * `ngtcp2_conn_set_early_remote_transport_params` sets |params| as
4147  * transport parameters previously received from a server.  The
4148  * parameters are used to send early data.  QUIC requires that client
4149  * application should remember transport parameters along with a
4150  * session ticket.
4151  *
4152  * At least following fields should be set:
4153  *
4154  * - initial_max_stream_id_bidi
4155  * - initial_max_stream_id_uni
4156  * - initial_max_stream_data_bidi_local
4157  * - initial_max_stream_data_bidi_remote
4158  * - initial_max_stream_data_uni
4159  * - initial_max_data
4160  * - active_connection_id_limit
4161  * - max_datagram_frame_size (if DATAGRAM extension was negotiated)
4162  *
4163  * The following fields are ignored:
4164  *
4165  * - ack_delay_exponent
4166  * - max_ack_delay
4167  * - initial_scid
4168  * - original_dcid
4169  * - preferred_address and preferred_address_present
4170  * - retry_scid and retry_scid_present
4171  * - stateless_reset_token and stateless_reset_token_present
4172  */
4173 NGTCP2_EXTERN void ngtcp2_conn_set_early_remote_transport_params_versioned(
4174     ngtcp2_conn *conn, int transport_params_version,
4175     const ngtcp2_transport_params *params);
4176 
4177 /**
4178  * @function
4179  *
4180  * `ngtcp2_conn_set_local_transport_params` sets the local transport
4181  * parameters |params|.  This function can only be called by server.
4182  * Although the local transport parameters are passed to
4183  * `ngtcp2_conn_server_new`, server might want to update them after
4184  * ALPN is chosen.  In that case, server can update the transport
4185  * parameter with this function.  Server must call this function
4186  * before calling `ngtcp2_conn_install_tx_handshake_key`.
4187  *
4188  * This function returns 0 if it succeeds, or one of the following
4189  * negative error codes:
4190  *
4191  * :macro:`NGTCP2_ERR_INVALID_STATE`
4192  *     `ngtcp2_conn_install_tx_handshake_key` has been called.
4193  */
4194 NGTCP2_EXTERN int ngtcp2_conn_set_local_transport_params_versioned(
4195     ngtcp2_conn *conn, int transport_params_version,
4196     const ngtcp2_transport_params *params);
4197 
4198 /**
4199  * @function
4200  *
4201  * `ngtcp2_conn_get_local_transport_params` returns a pointer to the
4202  * local QUIC transport parameters.
4203  */
4204 NGTCP2_EXTERN const ngtcp2_transport_params *
4205 ngtcp2_conn_get_local_transport_params(ngtcp2_conn *conn);
4206 
4207 /**
4208  * @function
4209  *
4210  * `ngtcp2_conn_encode_local_transport_params` encodes the local QUIC
4211  * transport parameters in |dest| of length |destlen|.  This is
4212  * equivalent to calling `ngtcp2_conn_get_local_transport_params` and
4213  * then `ngtcp2_encode_transport_params`.
4214  *
4215  * This function returns the number of written, or one of the
4216  * following negative error codes:
4217  *
4218  * :macro:`NGTCP2_ERR_NOBUF`
4219  *     Buffer is too small.
4220  */
4221 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_encode_local_transport_params(
4222     ngtcp2_conn *conn, uint8_t *dest, size_t destlen);
4223 
4224 /**
4225  * @function
4226  *
4227  * `ngtcp2_conn_open_bidi_stream` opens new bidirectional stream.  The
4228  * |stream_user_data| is the user data specific to the stream.  The
4229  * open stream ID is stored in |*pstream_id|.
4230  *
4231  * Application can call this function before handshake completes.  For
4232  * 0RTT packet, application can call this function after calling
4233  * `ngtcp2_conn_set_early_remote_transport_params`.  For 1RTT packet,
4234  * application can call this function after calling
4235  * `ngtcp2_conn_decode_remote_transport_params` and
4236  * `ngtcp2_conn_install_tx_key`.  If ngtcp2 crypto support library is
4237  * used, application can call this function after calling
4238  * `ngtcp2_crypto_derive_and_install_tx_key` for 1RTT packet.
4239  *
4240  * This function returns 0 if it succeeds, or one of the following
4241  * negative error codes:
4242  *
4243  * :macro:`NGTCP2_ERR_NOMEM`
4244  *     Out of memory
4245  * :macro:`NGTCP2_ERR_STREAM_ID_BLOCKED`
4246  *     The remote peer does not allow |stream_id| yet.
4247  */
4248 NGTCP2_EXTERN int ngtcp2_conn_open_bidi_stream(ngtcp2_conn *conn,
4249                                                int64_t *pstream_id,
4250                                                void *stream_user_data);
4251 
4252 /**
4253  * @function
4254  *
4255  * `ngtcp2_conn_open_uni_stream` opens new unidirectional stream.  The
4256  * |stream_user_data| is the user data specific to the stream.  The
4257  * open stream ID is stored in |*pstream_id|.
4258  *
4259  * Application can call this function before handshake completes.  For
4260  * 0RTT packet, application can call this function after calling
4261  * `ngtcp2_conn_set_early_remote_transport_params`.  For 1RTT packet,
4262  * application can call this function after calling
4263  * `ngtcp2_conn_decode_remote_transport_params` and
4264  * `ngtcp2_conn_install_tx_key`.  If ngtcp2 crypto support library is
4265  * used, application can call this function after calling
4266  * `ngtcp2_crypto_derive_and_install_tx_key` for 1RTT packet.
4267  *
4268  * This function returns 0 if it succeeds, or one of the following
4269  * negative error codes:
4270  *
4271  * :macro:`NGTCP2_ERR_NOMEM`
4272  *     Out of memory
4273  * :macro:`NGTCP2_ERR_STREAM_ID_BLOCKED`
4274  *     The remote peer does not allow |stream_id| yet.
4275  */
4276 NGTCP2_EXTERN int ngtcp2_conn_open_uni_stream(ngtcp2_conn *conn,
4277                                               int64_t *pstream_id,
4278                                               void *stream_user_data);
4279 
4280 /**
4281  * @function
4282  *
4283  * `ngtcp2_conn_shutdown_stream` closes stream denoted by |stream_id|
4284  * abruptly.  |app_error_code| is one of application error codes, and
4285  * indicates the reason of shutdown.  Successful call of this function
4286  * does not immediately erase the state of the stream.  The actual
4287  * deletion is done when the remote endpoint sends acknowledgement.
4288  * Calling this function is equivalent to call
4289  * `ngtcp2_conn_shutdown_stream_read`, and
4290  * `ngtcp2_conn_shutdown_stream_write` sequentially.
4291  *
4292  * This function returns 0 if it succeeds, or one of the following
4293  * negative error codes:
4294  *
4295  * :macro:`NGTCP2_ERR_NOMEM`
4296  *     Out of memory
4297  */
4298 NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream(ngtcp2_conn *conn,
4299                                               int64_t stream_id,
4300                                               uint64_t app_error_code);
4301 
4302 /**
4303  * @function
4304  *
4305  * `ngtcp2_conn_shutdown_stream_write` closes write-side of stream
4306  * denoted by |stream_id| abruptly.  |app_error_code| is one of
4307  * application error codes, and indicates the reason of shutdown.  If
4308  * this function succeeds, no application data is sent to the remote
4309  * endpoint.  It discards all data which has not been acknowledged
4310  * yet.
4311  *
4312  * This function returns 0 if it succeeds, or one of the following
4313  * negative error codes:
4314  *
4315  * :macro:`NGTCP2_ERR_NOMEM`
4316  *     Out of memory
4317  */
4318 NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream_write(ngtcp2_conn *conn,
4319                                                     int64_t stream_id,
4320                                                     uint64_t app_error_code);
4321 
4322 /**
4323  * @function
4324  *
4325  * `ngtcp2_conn_shutdown_stream_read` closes read-side of stream
4326  * denoted by |stream_id| abruptly.  |app_error_code| is one of
4327  * application error codes, and indicates the reason of shutdown.  If
4328  * this function succeeds, no application data is forwarded to an
4329  * application layer.
4330  *
4331  * This function returns 0 if it succeeds, or one of the following
4332  * negative error codes:
4333  *
4334  * :macro:`NGTCP2_ERR_NOMEM`
4335  *     Out of memory
4336  */
4337 NGTCP2_EXTERN int ngtcp2_conn_shutdown_stream_read(ngtcp2_conn *conn,
4338                                                    int64_t stream_id,
4339                                                    uint64_t app_error_code);
4340 
4341 /**
4342  * @macrosection
4343  *
4344  * Write stream data flags
4345  */
4346 
4347 /**
4348  * @macro
4349  *
4350  * :macro:`NGTCP2_WRITE_STREAM_FLAG_NONE` indicates no flag set.
4351  */
4352 #define NGTCP2_WRITE_STREAM_FLAG_NONE 0x00u
4353 
4354 /**
4355  * @macro
4356  *
4357  * :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` indicates that more data may
4358  * come and should be coalesced into the same packet if possible.
4359  */
4360 #define NGTCP2_WRITE_STREAM_FLAG_MORE 0x01u
4361 
4362 /**
4363  * @macro
4364  *
4365  * :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` indicates that the passed
4366  * data is the final part of a stream.
4367  */
4368 #define NGTCP2_WRITE_STREAM_FLAG_FIN 0x02u
4369 
4370 /**
4371  * @function
4372  *
4373  * `ngtcp2_conn_write_stream` is just like
4374  * `ngtcp2_conn_writev_stream`.  The only difference is that it
4375  * conveniently accepts a single buffer.
4376  */
4377 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_stream_versioned(
4378     ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
4379     ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen,
4380     uint32_t flags, int64_t stream_id, const uint8_t *data, size_t datalen,
4381     ngtcp2_tstamp ts);
4382 
4383 /**
4384  * @function
4385  *
4386  * `ngtcp2_conn_writev_stream` writes a packet containing stream data
4387  * of stream denoted by |stream_id|.  The buffer of the packet is
4388  * pointed by |dest| of length |destlen|.  This function performs QUIC
4389  * handshake as well.
4390  *
4391  * |destlen| should be at least
4392  * :member:`ngtcp2_settings.max_udp_payload_size`.
4393  *
4394  * Specifying -1 to |stream_id| means no new stream data to send.
4395  *
4396  * If |path| is not ``NULL``, this function stores the network path
4397  * with which the packet should be sent.  Each addr field must point
4398  * to the buffer which should be at least ``sizeof(struct
4399  * sockaddr_storage)`` bytes long.  The assignment might not be done
4400  * if nothing is written to |dest|.
4401  *
4402  * If |pi| is not ``NULL``, this function stores packet metadata in it
4403  * if it succeeds.  The metadata includes ECN markings.  When calling
4404  * this function again after it returns
4405  * :macro:`NGTCP2_ERR_WRITE_MORE`, caller must pass the same |pi| to
4406  * this function.
4407  *
4408  * If the all given data is encoded as STREAM frame in |dest|, and if
4409  * |flags| & :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` is nonzero, fin
4410  * flag is set to outgoing STREAM frame.  Otherwise, fin flag in
4411  * STREAM frame is not set.
4412  *
4413  * This packet may contain frames other than STREAM frame.  The packet
4414  * might not contain STREAM frame if other frames occupy the packet.
4415  * In that case, |*pdatalen| would be -1 if |pdatalen| is not
4416  * ``NULL``.
4417  *
4418  * If |flags| & :macro:`NGTCP2_WRITE_STREAM_FLAG_FIN` is nonzero, and
4419  * 0 length STREAM frame is successfully serialized, |*pdatalen| would
4420  * be 0.
4421  *
4422  * The number of data encoded in STREAM frame is stored in |*pdatalen|
4423  * if it is not ``NULL``.  The caller must keep the portion of data
4424  * covered by |*pdatalen| bytes in tact until
4425  * :type:`ngtcp2_acked_stream_data_offset` indicates that they are
4426  * acknowledged by a remote endpoint or the stream is closed.
4427  *
4428  * If |flags| equals to :macro:`NGTCP2_WRITE_STREAM_FLAG_NONE`, this
4429  * function produces a single payload of UDP packet.  If the given
4430  * stream data is small (e.g., few bytes), the packet might be
4431  * severely under filled.  Too many small packet might increase
4432  * overall packet processing costs.  Unless there are retransmissions,
4433  * by default, application can only send 1 STREAM frame in one QUIC
4434  * packet.  In order to include more than 1 STREAM frame in one QUIC
4435  * packet, specify :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` in |flags|.
4436  * This is analogous to ``MSG_MORE`` flag in :manpage:`send(2)`.  If
4437  * the :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` is used, there are 4
4438  * outcomes:
4439  *
4440  * - The function returns the written length of packet just like
4441  *   without :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE`.  This is because
4442  *   packet is nearly full and the library decided to make a complete
4443  *   packet.  |*pdatalen| might be -1 or >= 0.
4444  *
4445  * - The function returns :macro:`NGTCP2_ERR_WRITE_MORE`.  In this
4446  *   case, |*pdatalen| >= 0 is asserted.  This indicates that
4447  *   application can call this function with different stream data (or
4448  *   `ngtcp2_conn_writev_datagram` if it has data to send in
4449  *   unreliable datagram) to pack them into the same packet.
4450  *   Application has to specify the same |conn|, |path|, |pi|, |dest|,
4451  *   |destlen|, and |ts| parameters, otherwise the behaviour is
4452  *   undefined.  The application can change |flags|.
4453  *
4454  * - The function returns :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED` which
4455  *   indicates that stream is blocked because of flow control.
4456  *
4457  * - The other error might be returned just like without
4458  *   :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE`.
4459  *
4460  * When application sees :macro:`NGTCP2_ERR_WRITE_MORE`, it must not
4461  * call other ngtcp2 API functions (application can still call
4462  * `ngtcp2_conn_write_connection_close` to handle error from this
4463  * function).  Just keep calling `ngtcp2_conn_writev_stream`,
4464  * `ngtcp2_conn_write_pkt`, or `ngtcp2_conn_writev_datagram` until it
4465  * returns a positive number (which indicates a complete packet is
4466  * ready).  If there is no stream data to include, call this function
4467  * with |stream_id| as -1 to stop coalescing and write a packet.
4468  *
4469  * This function returns 0 if it cannot write any frame because buffer
4470  * is too small, or packet is congestion limited.  Application should
4471  * keep reading and wait for congestion window to grow.
4472  *
4473  * This function must not be called from inside the callback
4474  * functions.
4475  *
4476  * If pacing is enabled, `ngtcp2_conn_update_pkt_tx_time` must be
4477  * called after this function.  Application may call this function
4478  * multiple times before calling `ngtcp2_conn_update_pkt_tx_time`.
4479  * Packet pacing is enabled if BBR congestion controller algorithm is
4480  * used.
4481  *
4482  * This function returns the number of bytes written in |dest| if it
4483  * succeeds, or one of the following negative error codes:
4484  *
4485  * :macro:`NGTCP2_ERR_NOMEM`
4486  *     Out of memory
4487  * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`
4488  *     Stream does not exist
4489  * :macro:`NGTCP2_ERR_STREAM_SHUT_WR`
4490  *     Stream is half closed (local); or stream is being reset.
4491  * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
4492  *     Packet number is exhausted, and cannot send any more packet.
4493  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
4494  *     User callback failed
4495  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
4496  *     The total length of stream data is too large.
4497  * :macro:`NGTCP2_ERR_STREAM_DATA_BLOCKED`
4498  *     Stream is blocked because of flow control.
4499  * :macro:`NGTCP2_ERR_WRITE_MORE`
4500  *     (Only when :macro:`NGTCP2_WRITE_STREAM_FLAG_MORE` is specified)
4501  *     Application can call this function to pack more stream data
4502  *     into the same packet.  See above to know how it works.
4503  *
4504  * In general, if the error code which satisfies
4505  * `ngtcp2_err_is_fatal(err) <ngtcp2_err_is_fatal>` != 0 is returned,
4506  * the application should just close the connection by calling
4507  * `ngtcp2_conn_write_connection_close` or just delete the QUIC
4508  * connection using `ngtcp2_conn_del`.  It is undefined to call the
4509  * other library functions.
4510  */
4511 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_writev_stream_versioned(
4512     ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
4513     ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, ngtcp2_ssize *pdatalen,
4514     uint32_t flags, int64_t stream_id, const ngtcp2_vec *datav, size_t datavcnt,
4515     ngtcp2_tstamp ts);
4516 
4517 /**
4518  * @macrosection
4519  *
4520  * Write datagram flags
4521  */
4522 
4523 /**
4524  * @macro
4525  *
4526  * :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_NONE` indicates no flag set.
4527  */
4528 #define NGTCP2_WRITE_DATAGRAM_FLAG_NONE 0x00u
4529 
4530 /**
4531  * @macro
4532  *
4533  * :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` indicates that more data
4534  * may come and should be coalesced into the same packet if possible.
4535  */
4536 #define NGTCP2_WRITE_DATAGRAM_FLAG_MORE 0x01u
4537 
4538 /**
4539  * @function
4540  *
4541  * `ngtcp2_conn_writev_datagram` writes a packet containing unreliable
4542  * data in DATAGRAM frame.  The buffer of the packet is pointed by
4543  * |dest| of length |destlen|.  This function performs QUIC handshake
4544  * as well.
4545  *
4546  * |destlen| should be at least
4547  * :member:`ngtcp2_settings.max_udp_payload_size`.
4548  *
4549  * For |path| and |pi| parameters, refer to
4550  * `ngtcp2_conn_writev_stream`.
4551  *
4552  * If the given data is written to the buffer, nonzero value is
4553  * assigned to |*paccepted| if it is not NULL.  The data in DATAGRAM
4554  * frame cannot be fragmented; writing partial data is not possible.
4555  *
4556  * |dgram_id| is an opaque identifier which should uniquely identify
4557  * the given DATAGRAM.  It is passed to :type:`ngtcp2_ack_datagram`
4558  * callback when a packet that contains DATAGRAM frame is
4559  * acknowledged.  It is passed to :type:`ngtcp2_lost_datagram`
4560  * callback when a packet that contains DATAGRAM frame is declared
4561  * lost.  If an application uses neither of those callbacks, it can
4562  * sets 0 to this parameter.
4563  *
4564  * This function might write other frames other than DATAGRAM, just
4565  * like `ngtcp2_conn_writev_stream`.
4566  *
4567  * If the function returns 0, it means that no more data cannot be
4568  * sent because of congestion control limit; or, data does not fit
4569  * into the provided buffer; or, a local endpoint, as a server, is
4570  * unable to send data because of its amplification limit.  In this
4571  * case, |*paccepted| is assigned zero if it is not NULL.
4572  *
4573  * If :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` is set in |flags|,
4574  * there are 3 outcomes:
4575  *
4576  * - The function returns the written length of packet just like
4577  *   without :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE`.  This is
4578  *   because packet is nearly full and the library decided to make a
4579  *   complete packet.  |*paccepted| might be zero or nonzero.
4580  *
4581  * - The function returns :macro:`NGTCP2_ERR_WRITE_MORE`.  In this
4582  *   case, |*paccepted| != 0 is asserted.  This indicates that
4583  *   application can call this function with another unreliable data
4584  *   (or `ngtcp2_conn_writev_stream` if it has stream data to send) to
4585  *   pack them into the same packet.  Application has to specify the
4586  *   same |conn|, |path|, |pi|, |dest|, |destlen|, and |ts|
4587  *   parameters, otherwise the behaviour is undefined.  The
4588  *   application can change |flags|.
4589  *
4590  * - The other error might be returned just like without
4591  *   :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE`.
4592  *
4593  * When application sees :macro:`NGTCP2_ERR_WRITE_MORE`, it must not
4594  * call other ngtcp2 API functions (application can still call
4595  * `ngtcp2_conn_write_connection_close` to handle error from this
4596  * function).  Just keep calling `ngtcp2_conn_writev_datagram`,
4597  * `ngtcp2_conn_writev_stream` or `ngtcp2_conn_write_pkt` until it
4598  * returns a positive number (which indicates a complete packet is
4599  * ready).
4600  *
4601  * This function returns the number of bytes written in |dest| if it
4602  * succeeds, or one of the following negative error codes:
4603  *
4604  * :macro:`NGTCP2_ERR_NOMEM`
4605  *     Out of memory
4606  * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
4607  *     Packet number is exhausted, and cannot send any more packet.
4608  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
4609  *     User callback failed
4610  * :macro:`NGTCP2_ERR_WRITE_MORE`
4611  *     (Only when :macro:`NGTCP2_WRITE_DATAGRAM_FLAG_MORE` is
4612  *     specified) Application can call this function to pack more data
4613  *     into the same packet.  See above to know how it works.
4614  * :macro:`NGTCP2_ERR_INVALID_STATE`
4615  *     A remote endpoint did not express the DATAGRAM frame support.
4616  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
4617  *     The provisional DATAGRAM frame size exceeds the maximum
4618  *     DATAGRAM frame size that a remote endpoint can receive.
4619  *
4620  * In general, if the error code which satisfies
4621  * `ngtcp2_err_is_fatal(err) <ngtcp2_err_is_fatal>` != 0 is returned,
4622  * the application should just close the connection by calling
4623  * `ngtcp2_conn_write_connection_close` or just delete the QUIC
4624  * connection using `ngtcp2_conn_del`.  It is undefined to call the
4625  * other library functions.
4626  */
4627 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_writev_datagram_versioned(
4628     ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
4629     ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, int *paccepted,
4630     uint32_t flags, uint64_t dgram_id, const ngtcp2_vec *datav, size_t datavcnt,
4631     ngtcp2_tstamp ts);
4632 
4633 /**
4634  * @function
4635  *
4636  * `ngtcp2_conn_is_in_closing_period` returns nonzero if |conn| is in
4637  * the closing period.
4638  */
4639 NGTCP2_EXTERN int ngtcp2_conn_is_in_closing_period(ngtcp2_conn *conn);
4640 
4641 /**
4642  * @function
4643  *
4644  * `ngtcp2_conn_is_in_draining_period` returns nonzero if |conn| is in
4645  * the draining period.
4646  */
4647 NGTCP2_EXTERN int ngtcp2_conn_is_in_draining_period(ngtcp2_conn *conn);
4648 
4649 /**
4650  * @function
4651  *
4652  * `ngtcp2_conn_extend_max_stream_offset` extends stream's max stream
4653  * data value by |datalen|.
4654  *
4655  * This function returns 0 if it succeeds, or one of the following
4656  * negative error codes:
4657  *
4658  * :macro:`NGTCP2_ERR_NOMEM`
4659  *     Out of memory.
4660  */
4661 NGTCP2_EXTERN int ngtcp2_conn_extend_max_stream_offset(ngtcp2_conn *conn,
4662                                                        int64_t stream_id,
4663                                                        uint64_t datalen);
4664 
4665 /**
4666  * @function
4667  *
4668  * `ngtcp2_conn_extend_max_offset` extends max data offset by
4669  * |datalen|.
4670  */
4671 NGTCP2_EXTERN void ngtcp2_conn_extend_max_offset(ngtcp2_conn *conn,
4672                                                  uint64_t datalen);
4673 
4674 /**
4675  * @function
4676  *
4677  * `ngtcp2_conn_extend_max_streams_bidi` extends the number of maximum
4678  * local bidirectional streams that a remote endpoint can open by |n|.
4679  *
4680  * The library does not increase maximum stream limit automatically.
4681  * The exception is when a stream is closed without
4682  * :type:`ngtcp2_stream_open` callback being called.  In this case,
4683  * stream limit is increased automatically.
4684  */
4685 NGTCP2_EXTERN void ngtcp2_conn_extend_max_streams_bidi(ngtcp2_conn *conn,
4686                                                        size_t n);
4687 
4688 /**
4689  * @function
4690  *
4691  * `ngtcp2_conn_extend_max_streams_uni` extends the number of maximum
4692  * local unidirectional streams that a remote endpoint can open by
4693  * |n|.
4694  *
4695  * The library does not increase maximum stream limit automatically.
4696  * The exception is when a stream is closed without
4697  * :type:`ngtcp2_stream_open` callback being called.  In this case,
4698  * stream limit is increased automatically.
4699  */
4700 NGTCP2_EXTERN void ngtcp2_conn_extend_max_streams_uni(ngtcp2_conn *conn,
4701                                                       size_t n);
4702 
4703 /**
4704  * @function
4705  *
4706  * `ngtcp2_conn_get_dcid` returns the non-NULL pointer to destination
4707  * connection ID.  If no destination connection ID is present, the
4708  * return value is not ``NULL``, and its datalen field is 0.
4709  */
4710 NGTCP2_EXTERN const ngtcp2_cid *ngtcp2_conn_get_dcid(ngtcp2_conn *conn);
4711 
4712 /**
4713  * @function
4714  *
4715  * `ngtcp2_conn_get_client_initial_dcid` returns the non-NULL pointer
4716  * to the Destination Connection ID that client sent in its Initial
4717  * packet.
4718  */
4719 NGTCP2_EXTERN const ngtcp2_cid *
4720 ngtcp2_conn_get_client_initial_dcid(ngtcp2_conn *conn);
4721 
4722 /**
4723  * @function
4724  *
4725  * `ngtcp2_conn_get_num_scid` returns the number of source connection
4726  * IDs which the local endpoint has provided to the peer and have not
4727  * retired.
4728  */
4729 NGTCP2_EXTERN size_t ngtcp2_conn_get_num_scid(ngtcp2_conn *conn);
4730 
4731 /**
4732  * @function
4733  *
4734  * `ngtcp2_conn_get_scid` writes the all source connection IDs which
4735  * the local endpoint has provided to the peer and have not retired in
4736  * |dest|.  The buffer pointed by |dest| must have
4737  * ``sizeof(ngtcp2_cid) * n`` bytes available, where n is the return
4738  * value of `ngtcp2_conn_get_num_scid()`.
4739  */
4740 NGTCP2_EXTERN size_t ngtcp2_conn_get_scid(ngtcp2_conn *conn, ngtcp2_cid *dest);
4741 
4742 /**
4743  * @function
4744  *
4745  * `ngtcp2_conn_get_num_active_dcid` returns the number of the active
4746  * destination connection ID.
4747  */
4748 NGTCP2_EXTERN size_t ngtcp2_conn_get_num_active_dcid(ngtcp2_conn *conn);
4749 
4750 /**
4751  * @struct
4752  *
4753  * :type:`ngtcp2_cid_token` is the convenient struct to store
4754  * Connection ID, its associated path, and stateless reset token.
4755  */
4756 typedef struct ngtcp2_cid_token {
4757   /**
4758    * :member:`seq` is the sequence number of this Connection ID.
4759    */
4760   uint64_t seq;
4761   /**
4762    * :member:`cid` is Connection ID.
4763    */
4764   ngtcp2_cid cid;
4765   /**
4766    * :member:`ps` is the path which is associated to this Connection
4767    * ID.
4768    */
4769   ngtcp2_path_storage ps;
4770   /**
4771    * :member:`token` is the stateless reset token for this Connection
4772    * ID.
4773    */
4774   uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN];
4775   /**
4776    * :member:`token_present` is nonzero if token contains stateless
4777    * reset token.
4778    */
4779   uint8_t token_present;
4780 } ngtcp2_cid_token;
4781 
4782 /**
4783  * @function
4784  *
4785  * `ngtcp2_conn_get_active_dcid` writes the all active destination
4786  * connection IDs and tokens to |dest|.  The buffer pointed by |dest|
4787  * must have ``sizeof(ngtcp2_cid_token) * n`` bytes available, where n
4788  * is the return value of `ngtcp2_conn_get_num_active_dcid()`.
4789  */
4790 NGTCP2_EXTERN size_t ngtcp2_conn_get_active_dcid(ngtcp2_conn *conn,
4791                                                  ngtcp2_cid_token *dest);
4792 
4793 /**
4794  * @function
4795  *
4796  * `ngtcp2_conn_get_client_chosen_version` returns the client chosen
4797  * version.
4798  */
4799 NGTCP2_EXTERN uint32_t ngtcp2_conn_get_client_chosen_version(ngtcp2_conn *conn);
4800 
4801 /**
4802  * @function
4803  *
4804  * `ngtcp2_conn_get_negotiated_version` returns the negotiated version.
4805  *
4806  * Until the version is negotiated, this function returns 0.
4807  */
4808 NGTCP2_EXTERN uint32_t ngtcp2_conn_get_negotiated_version(ngtcp2_conn *conn);
4809 
4810 /**
4811  * @function
4812  *
4813  * `ngtcp2_conn_early_data_rejected` tells |conn| that early data was
4814  * rejected by a server.  |conn| discards the following connection
4815  * states:
4816  *
4817  * - Any opended streams.
4818  * - Stream identifier allocations.
4819  * - Max data extended by `ngtcp2_conn_extend_max_offset`.
4820  * - Max bidi streams extended by `ngtcp2_conn_extend_max_streams_bidi`.
4821  * - Max uni streams extended by `ngtcp2_conn_extend_max_streams_uni`.
4822  *
4823  * Application which wishes to retransmit early data, it has to open
4824  * streams and send stream data again.
4825  */
4826 NGTCP2_EXTERN void ngtcp2_conn_early_data_rejected(ngtcp2_conn *conn);
4827 
4828 /**
4829  * @function
4830  *
4831  * `ngtcp2_conn_get_conn_stat` assigns connection statistics data to
4832  * |*cstat|.
4833  */
4834 NGTCP2_EXTERN void ngtcp2_conn_get_conn_stat_versioned(ngtcp2_conn *conn,
4835                                                        int conn_stat_version,
4836                                                        ngtcp2_conn_stat *cstat);
4837 
4838 /**
4839  * @function
4840  *
4841  * `ngtcp2_conn_submit_crypto_data` submits crypto stream data |data|
4842  * of length |datalen| to the library for transmission.  The
4843  * encryption level is given in |crypto_level|.
4844  *
4845  * The library makes a copy of the buffer pointed by |data| of length
4846  * |datalen|.  Application can discard |data|.
4847  */
4848 NGTCP2_EXTERN int
4849 ngtcp2_conn_submit_crypto_data(ngtcp2_conn *conn,
4850                                ngtcp2_crypto_level crypto_level,
4851                                const uint8_t *data, const size_t datalen);
4852 
4853 /**
4854  * @function
4855  *
4856  * `ngtcp2_conn_submit_new_token` submits address validation token.
4857  * It is sent in NEW_TOKEN frame.  Only server can call this function.
4858  * |tokenlen| must not be 0.
4859  *
4860  * This function makes a copy of the buffer pointed by |token| of
4861  * length |tokenlen|.
4862  *
4863  * This function returns 0 if it succeeds, or one of the following
4864  * negative error codes:
4865  *
4866  * :macro:`NGTCP2_ERR_NOMEM`
4867  *     Out of memory.
4868  */
4869 NGTCP2_EXTERN int ngtcp2_conn_submit_new_token(ngtcp2_conn *conn,
4870                                                const uint8_t *token,
4871                                                size_t tokenlen);
4872 
4873 /**
4874  * @function
4875  *
4876  * `ngtcp2_conn_set_local_addr` sets local endpoint address |addr| to
4877  * the current path of |conn|.
4878  */
4879 NGTCP2_EXTERN void ngtcp2_conn_set_local_addr(ngtcp2_conn *conn,
4880                                               const ngtcp2_addr *addr);
4881 
4882 /**
4883  * @function
4884  *
4885  * `ngtcp2_conn_set_path_user_data` sets the |path_user_data| to the
4886  * current path (see :member:`ngtcp2_path.user_data`).
4887  */
4888 NGTCP2_EXTERN void ngtcp2_conn_set_path_user_data(ngtcp2_conn *conn,
4889                                                   void *path_user_data);
4890 
4891 /**
4892  * @function
4893  *
4894  * `ngtcp2_conn_get_path` returns the current path.
4895  */
4896 NGTCP2_EXTERN const ngtcp2_path *ngtcp2_conn_get_path(ngtcp2_conn *conn);
4897 
4898 /**
4899  * @function
4900  *
4901  * `ngtcp2_conn_get_max_udp_payload_size` returns the maximum UDP
4902  * payload size that this local endpoint would send.  This is the
4903  * value of :member:`ngtcp2_settings.max_udp_payload_size` that is
4904  * passed to `ngtcp2_conn_client_new` or `ngtcp2_conn_server_new`.
4905  */
4906 NGTCP2_EXTERN size_t ngtcp2_conn_get_max_udp_payload_size(ngtcp2_conn *conn);
4907 
4908 /**
4909  * @function
4910  *
4911  * `ngtcp2_conn_get_path_max_udp_payload_size` returns the maximum UDP
4912  * payload size for the current path.  If
4913  * :member:`ngtcp2_settings.no_udp_payload_size_shaping` is set to
4914  * nonzero, this function is equivalent to
4915  * `ngtcp2_conn_get_max_udp_payload_size`.  Otherwise, it returns the
4916  * maximum UDP payload size that is probed for the current path.
4917  */
4918 NGTCP2_EXTERN size_t
4919 ngtcp2_conn_get_path_max_udp_payload_size(ngtcp2_conn *conn);
4920 
4921 /**
4922  * @function
4923  *
4924  * `ngtcp2_conn_initiate_immediate_migration` starts connection
4925  * migration to the given |path|.
4926  * Only client can initiate migration.  This function does
4927  * immediate migration; it does not probe peer reachability from a new
4928  * local address.
4929  *
4930  * This function returns 0 if it succeeds, or one of the following
4931  * negative error codes:
4932  *
4933  * :macro:`NGTCP2_ERR_INVALID_STATE`
4934  *     Migration is disabled; or handshake is not yet confirmed; or
4935  *     client is migrating to server's preferred address.
4936  * :macro:`NGTCP2_ERR_CONN_ID_BLOCKED`
4937  *     No unused connection ID is available.
4938  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
4939  *     |local_addr| equals the current local address.
4940  * :macro:`NGTCP2_ERR_NOMEM`
4941  *     Out of memory
4942  */
4943 NGTCP2_EXTERN int ngtcp2_conn_initiate_immediate_migration(
4944     ngtcp2_conn *conn, const ngtcp2_path *path, ngtcp2_tstamp ts);
4945 
4946 /**
4947  * @function
4948  *
4949  * `ngtcp2_conn_initiate_migration` starts connection migration to the
4950  * given |path|.  Only client can initiate migration.  Unlike
4951  * `ngtcp2_conn_initiate_immediate_migration`, this function starts a
4952  * path validation with a new path and migrate to the new path after
4953  * successful path validation.
4954  *
4955  * This function returns 0 if it succeeds, or one of the following
4956  * negative error codes:
4957  *
4958  * :macro:`NGTCP2_ERR_INVALID_STATE`
4959  *     Migration is disabled; or handshake is not yet confirmed; or
4960  *     client is migrating to server's preferred address.
4961  * :macro:`NGTCP2_ERR_CONN_ID_BLOCKED`
4962  *     No unused connection ID is available.
4963  * :macro:`NGTCP2_ERR_INVALID_ARGUMENT`
4964  *     |local_addr| equals the current local address.
4965  * :macro:`NGTCP2_ERR_NOMEM`
4966  *     Out of memory
4967  */
4968 NGTCP2_EXTERN int ngtcp2_conn_initiate_migration(ngtcp2_conn *conn,
4969                                                  const ngtcp2_path *path,
4970                                                  ngtcp2_tstamp ts);
4971 
4972 /**
4973  * @function
4974  *
4975  * `ngtcp2_conn_get_max_local_streams_uni` returns the cumulative
4976  * number of streams which local endpoint can open.
4977  */
4978 NGTCP2_EXTERN uint64_t ngtcp2_conn_get_max_local_streams_uni(ngtcp2_conn *conn);
4979 
4980 /**
4981  * @function
4982  *
4983  * `ngtcp2_conn_get_max_data_left` returns the number of bytes that
4984  * this local endpoint can send in this connection.
4985  */
4986 NGTCP2_EXTERN uint64_t ngtcp2_conn_get_max_data_left(ngtcp2_conn *conn);
4987 
4988 /**
4989  * @function
4990  *
4991  * `ngtcp2_conn_get_max_stream_data_left` returns the number of bytes
4992  * that this local endpoint can send to a stream identified by
4993  * |stream_id|.  If no such stream is found, this function returns 0.
4994  */
4995 NGTCP2_EXTERN uint64_t ngtcp2_conn_get_max_stream_data_left(ngtcp2_conn *conn,
4996                                                             int64_t stream_id);
4997 
4998 /**
4999  * @function
5000  *
5001  * `ngtcp2_conn_get_streams_bidi_left` returns the number of
5002  * bidirectional streams which the local endpoint can open without
5003  * violating stream concurrency limit.
5004  */
5005 NGTCP2_EXTERN uint64_t ngtcp2_conn_get_streams_bidi_left(ngtcp2_conn *conn);
5006 
5007 /**
5008  * @function
5009  *
5010  * `ngtcp2_conn_get_streams_uni_left` returns the number of
5011  * unidirectional streams which the local endpoint can open without
5012  * violating stream concurrency limit.
5013  */
5014 NGTCP2_EXTERN uint64_t ngtcp2_conn_get_streams_uni_left(ngtcp2_conn *conn);
5015 
5016 /**
5017  * @function
5018  *
5019  * `ngtcp2_conn_get_cwnd_left` returns the cwnd minus the number of
5020  * bytes in flight on the current path.  If the former is smaller than
5021  * the latter, this function returns 0.
5022  */
5023 NGTCP2_EXTERN uint64_t ngtcp2_conn_get_cwnd_left(ngtcp2_conn *conn);
5024 
5025 /**
5026  * @function
5027  *
5028  * `ngtcp2_conn_set_initial_crypto_ctx` sets |ctx| for Initial packet
5029  * encryption.  The passed data will be passed to
5030  * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
5031  * :type:`ngtcp2_hp_mask` callbacks.
5032  */
5033 NGTCP2_EXTERN void
5034 ngtcp2_conn_set_initial_crypto_ctx(ngtcp2_conn *conn,
5035                                    const ngtcp2_crypto_ctx *ctx);
5036 
5037 /**
5038  * @function
5039  *
5040  * `ngtcp2_conn_get_initial_crypto_ctx` returns
5041  * :type:`ngtcp2_crypto_ctx` object for Initial packet encryption.
5042  */
5043 NGTCP2_EXTERN const ngtcp2_crypto_ctx *
5044 ngtcp2_conn_get_initial_crypto_ctx(ngtcp2_conn *conn);
5045 
5046 /**
5047  * @function
5048  *
5049  * `ngtcp2_conn_set_crypto_ctx` sets |ctx| for Handshake/1RTT packet
5050  * encryption.  The passed data will be passed to
5051  * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
5052  * :type:`ngtcp2_hp_mask` callbacks.
5053  */
5054 NGTCP2_EXTERN void ngtcp2_conn_set_crypto_ctx(ngtcp2_conn *conn,
5055                                               const ngtcp2_crypto_ctx *ctx);
5056 
5057 /**
5058  * @function
5059  *
5060  * `ngtcp2_conn_get_tls_native_handle` returns TLS native handle set by
5061  * `ngtcp2_conn_set_tls_native_handle()`.
5062  */
5063 NGTCP2_EXTERN void *ngtcp2_conn_get_tls_native_handle(ngtcp2_conn *conn);
5064 
5065 /**
5066  * @function
5067  *
5068  * `ngtcp2_conn_set_tls_native_handle` sets TLS native handle
5069  * |tls_native_handle| to |conn|.  Internally, it is used as an opaque
5070  * pointer.
5071  */
5072 NGTCP2_EXTERN void ngtcp2_conn_set_tls_native_handle(ngtcp2_conn *conn,
5073                                                      void *tls_native_handle);
5074 
5075 /**
5076  * @function
5077  *
5078  * `ngtcp2_conn_set_retry_aead` sets |aead| and |aead_ctx| for Retry
5079  * integrity tag verification.  |aead| must be AEAD_AES_128_GCM.
5080  * |aead_ctx| must be initialized with :macro:`NGTCP2_RETRY_KEY` as
5081  * encryption key.  This function must be called if |conn| is
5082  * initialized as client.  Server does not verify the tag and has no
5083  * need to call this function.
5084  *
5085  * If this function succeeds, |conn| takes ownership of |aead_ctx|.
5086  * :type:`ngtcp2_delete_crypto_aead_ctx` will be called to delete this
5087  * object when it is no longer used.  If this function fails, the
5088  * caller is responsible to delete it.
5089  */
5090 NGTCP2_EXTERN void
5091 ngtcp2_conn_set_retry_aead(ngtcp2_conn *conn, const ngtcp2_crypto_aead *aead,
5092                            const ngtcp2_crypto_aead_ctx *aead_ctx);
5093 
5094 /**
5095  * @function
5096  *
5097  * `ngtcp2_conn_get_crypto_ctx` returns :type:`ngtcp2_crypto_ctx`
5098  * object for Handshake/1RTT packet encryption.
5099  */
5100 NGTCP2_EXTERN const ngtcp2_crypto_ctx *
5101 ngtcp2_conn_get_crypto_ctx(ngtcp2_conn *conn);
5102 
5103 /**
5104  * @function
5105  *
5106  * `ngtcp2_conn_set_early_crypto_ctx` sets |ctx| for 0RTT packet
5107  * encryption.  The passed data will be passed to
5108  * :type:`ngtcp2_encrypt`, :type:`ngtcp2_decrypt` and
5109  * :type:`ngtcp2_hp_mask` callbacks.
5110  */
5111 NGTCP2_EXTERN void
5112 ngtcp2_conn_set_early_crypto_ctx(ngtcp2_conn *conn,
5113                                  const ngtcp2_crypto_ctx *ctx);
5114 
5115 /**
5116  * @function
5117  *
5118  * `ngtcp2_conn_get_early_crypto_ctx` returns
5119  * :type:`ngtcp2_crypto_ctx` object for 0RTT packet encryption.
5120  */
5121 NGTCP2_EXTERN const ngtcp2_crypto_ctx *
5122 ngtcp2_conn_get_early_crypto_ctx(ngtcp2_conn *conn);
5123 
5124 /**
5125  * @enum
5126  *
5127  * :type:`ngtcp2_connection_close_error_code_type` defines connection
5128  * error code type.
5129  */
5130 typedef enum ngtcp2_connection_close_error_code_type {
5131   /**
5132    * :enum:`NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT`
5133    * indicates the error code is QUIC transport error code.
5134    */
5135   NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT,
5136   /**
5137    * :enum:`NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_APPLICATION`
5138    * indicates the error code is application error code.
5139    */
5140   NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_APPLICATION,
5141   /**
5142    * :enum:`NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_VERSION_NEGOTIATION`
5143    * is a special case of QUIC transport error, and it indicates that
5144    * client receives Version Negotiation packet.
5145    */
5146   NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_VERSION_NEGOTIATION,
5147   /**
5148    * :enum:`NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_IDLE_CLOSE`
5149    * is a special case of QUIC transport error, and it indicates that
5150    * connection is closed because of idle timeout.
5151    */
5152   NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_IDLE_CLOSE
5153 } ngtcp2_connection_close_error_code_type;
5154 
5155 /**
5156  * @struct
5157  *
5158  * :type:`ngtcp2_connection_close_error` contains connection
5159  * error code, its type, and the optional reason phrase.
5160  */
5161 typedef struct ngtcp2_connection_close_error {
5162   /**
5163    * :member:`type` is the type of :member:`error_code`.
5164    */
5165   ngtcp2_connection_close_error_code_type type;
5166   /**
5167    * :member:`error_code` is the error code for connection closure.
5168    */
5169   uint64_t error_code;
5170   /**
5171    * :member:`frame_type` is the type of QUIC frame which triggers
5172    * this connection error.  This field is set to 0 if the frame type
5173    * is unknown.
5174    */
5175   uint64_t frame_type;
5176   /**
5177    * :member:`reason` points to the buffer which contains a reason
5178    * phrase.  It may be NULL if there is no reason phrase.  If it is
5179    * received from a remote endpoint, it is truncated to at most 1024
5180    * bytes.
5181    */
5182   uint8_t *reason;
5183   /**
5184    * :member:`reasonlen` is the length of data pointed by
5185    * :member:`reason`.
5186    */
5187   size_t reasonlen;
5188 } ngtcp2_connection_close_error;
5189 
5190 /**
5191  * @function
5192  *
5193  * `ngtcp2_connection_close_error_default` initializes |ccerr| with
5194  * the default values.  It sets the following fields:
5195  *
5196  * - :member:`type <ngtcp2_connection_close_error.type>` =
5197  *   :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT`
5198  * - :member:`error_code <ngtcp2_connection_close_error.error_code>` =
5199  *   :macro:`NGTCP2_NO_ERROR`.
5200  * - :member:`frame_type <ngtcp2_connection_close_error.frame_type>` =
5201  *   0
5202  * - :member:`reason <ngtcp2_connection_close_error.reason>` = NULL
5203  * - :member:`reasonlen <ngtcp2_connection_close_error.reasonlen>` = 0
5204  */
5205 NGTCP2_EXTERN void
5206 ngtcp2_connection_close_error_default(ngtcp2_connection_close_error *ccerr);
5207 
5208 /**
5209  * @function
5210  *
5211  * `ngtcp2_connection_close_error_set_transport_error` sets
5212  * :member:`ccerr->type <ngtcp2_connection_close_error.type>` to
5213  * :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT`,
5214  * and :member:`ccerr->error_code
5215  * <ngtcp2_connection_close_error.error_code>` to |error_code|.
5216  * |reason| is the reason phrase of length |reasonlen|.  This function
5217  * does not make a copy of the reason phrase.
5218  */
5219 NGTCP2_EXTERN void ngtcp2_connection_close_error_set_transport_error(
5220     ngtcp2_connection_close_error *ccerr, uint64_t error_code,
5221     const uint8_t *reason, size_t reasonlen);
5222 
5223 /**
5224  * @function
5225  *
5226  * `ngtcp2_connection_close_error_set_transport_error_liberr` sets
5227  * type and error_code based on |liberr|.
5228  *
5229  * If |liberr| is :macro:`NGTCP2_ERR_RECV_VERSION_NEGOTIATION`,
5230  * :member:`ccerr->type <ngtcp2_connection_close_error.type>` is set
5231  * to
5232  * :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_VERSION_NEGOTIATION`,
5233  * and :member:`ccerr->error_code
5234  * <ngtcp2_connection_close_error.error_code>` to
5235  * :macro:`NGTCP2_NO_ERROR`.  If |liberr| is
5236  * :macro:`NGTCP2_ERR_IDLE_CLOSE`, :member:`ccerr->type
5237  * <ngtcp2_connection_close_error.type>` is set to
5238  * :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_IDLE_CLOSE`,
5239  * and :member:`ccerr->error_code
5240  * <ngtcp2_connection_close_error.error_code>` to
5241  * :macro:`NGTCP2_NO_ERROR`.  Otherwise, :member:`ccerr->type
5242  * <ngtcp2_connection_close_error.type>` is set to
5243  * :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT`,
5244  * and :member:`ccerr->error_code
5245  * <ngtcp2_connection_close_error.error_code>` is set to an error code
5246  * inferred by |liberr| (see
5247  * `ngtcp2_err_infer_quic_transport_error_code`).  |reason| is the
5248  * reason phrase of length |reasonlen|.  This function does not make a
5249  * copy of the reason phrase.
5250  */
5251 NGTCP2_EXTERN void ngtcp2_connection_close_error_set_transport_error_liberr(
5252     ngtcp2_connection_close_error *ccerr, int liberr, const uint8_t *reason,
5253     size_t reasonlen);
5254 
5255 /**
5256  * @function
5257  *
5258  * `ngtcp2_connection_close_error_set_transport_error_tls_alert` sets
5259  * :member:`ccerr->type <ngtcp2_connection_close_error.type>` to
5260  * :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT`,
5261  * and :member:`ccerr->error_code
5262  * <ngtcp2_connection_close_error.error_code>` to bitwise-OR of
5263  * :macro:`NGTCP2_CRYPTO_ERROR` and |tls_alert|.  |reason| is the
5264  * reason phrase of length |reasonlen|.  This function does not make a
5265  * copy of the reason phrase.
5266  */
5267 NGTCP2_EXTERN void ngtcp2_connection_close_error_set_transport_error_tls_alert(
5268     ngtcp2_connection_close_error *ccerr, uint8_t tls_alert,
5269     const uint8_t *reason, size_t reasonlen);
5270 
5271 /**
5272  * @function
5273  *
5274  * `ngtcp2_connection_close_error_set_application_error` sets
5275  * :member:`ccerr->type <ngtcp2_connection_close_error.type>` to
5276  * :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_APPLICATION`,
5277  * and :member:`ccerr->error_code
5278  * <ngtcp2_connection_close_error.error_code>` to |error_code|.
5279  * |reason| is the reason phrase of length |reasonlen|.  This function
5280  * does not make a copy of the reason phrase.
5281  */
5282 NGTCP2_EXTERN void ngtcp2_connection_close_error_set_application_error(
5283     ngtcp2_connection_close_error *ccerr, uint64_t error_code,
5284     const uint8_t *reason, size_t reasonlen);
5285 
5286 /**
5287  * @function
5288  *
5289  * `ngtcp2_conn_write_connection_close` writes a packet which contains
5290  * CONNECTION_CLOSE frame(s) (type 0x1c or 0x1d) in the buffer pointed
5291  * by |dest| whose capacity is |destlen|.
5292  *
5293  * For client, |destlen| should be at least
5294  * :macro:`NGTCP2_MAX_UDP_PAYLOAD_SIZE`.
5295  *
5296  * If |path| is not ``NULL``, this function stores the network path
5297  * with which the packet should be sent.  Each addr field must point
5298  * to the buffer which should be at least ``sizeof(struct
5299  * sockaddr_storage)`` bytes long.  The assignment might not be done
5300  * if nothing is written to |dest|.
5301  *
5302  * If |pi| is not ``NULL``, this function stores packet metadata in it
5303  * if it succeeds.  The metadata includes ECN markings.
5304  *
5305  * If :member:`ccerr->type <ngtcp2_connection_close_error.type>` ==
5306  * :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT`,
5307  * this function sends CONNECTION_CLOSE (type 0x1c) frame.  If
5308  * :member:`ccerr->type <ngtcp2_connection_close_error.type>` ==
5309  * :enum:`ngtcp2_connection_close_error_code_type.NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_APPLICATION`,
5310  * it sends CONNECTION_CLOSE (type 0x1d) frame.  Otherwise, it does
5311  * not produce any data, and returns 0.
5312  *
5313  * This function must not be called from inside the callback
5314  * functions.
5315  *
5316  * At the moment, successful call to this function makes connection
5317  * close.  We may change this behaviour in the future to allow
5318  * graceful shutdown.
5319  *
5320  * This function returns the number of bytes written in |dest| if it
5321  * succeeds, or one of the following negative error codes:
5322  *
5323  * :macro:`NGTCP2_ERR_NOMEM`
5324  *     Out of memory
5325  * :macro:`NGTCP2_ERR_NOBUF`
5326  *     Buffer is too small
5327  * :macro:`NGTCP2_ERR_INVALID_STATE`
5328  *     The current state does not allow sending CONNECTION_CLOSE.
5329  * :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
5330  *     Packet number is exhausted, and cannot send any more packet.
5331  * :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
5332  *     User callback failed
5333  */
5334 NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_connection_close_versioned(
5335     ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
5336     ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen,
5337     const ngtcp2_connection_close_error *ccerr, ngtcp2_tstamp ts);
5338 
5339 /**
5340  * @function
5341  *
5342  * `ngtcp2_conn_get_connection_close_error` stores the received
5343  * connection close error in |ccerr|.
5344  */
5345 NGTCP2_EXTERN void
5346 ngtcp2_conn_get_connection_close_error(ngtcp2_conn *conn,
5347                                        ngtcp2_connection_close_error *ccerr);
5348 
5349 /**
5350  * @function
5351  *
5352  * `ngtcp2_conn_is_local_stream` returns nonzero if |stream_id| denotes the
5353  * stream which a local endpoint issues.
5354  */
5355 NGTCP2_EXTERN int ngtcp2_conn_is_local_stream(ngtcp2_conn *conn,
5356                                               int64_t stream_id);
5357 
5358 /**
5359  * @function
5360  *
5361  * `ngtcp2_conn_is_server` returns nonzero if |conn| is initialized as
5362  * server.
5363  */
5364 NGTCP2_EXTERN int ngtcp2_conn_is_server(ngtcp2_conn *conn);
5365 
5366 /**
5367  * @function
5368  *
5369  * `ngtcp2_conn_after_retry` returns nonzero if |conn| as a client has
5370  * received Retry packet from server and successfully validated it.
5371  */
5372 NGTCP2_EXTERN int ngtcp2_conn_after_retry(ngtcp2_conn *conn);
5373 
5374 /**
5375  * @function
5376  *
5377  * `ngtcp2_conn_set_stream_user_data` sets |stream_user_data| to the
5378  * stream identified by |stream_id|.
5379  *
5380  * This function returns 0 if it succeeds, or one of the following
5381  * negative error codes:
5382  *
5383  * :macro:`NGTCP2_ERR_STREAM_NOT_FOUND`
5384  *     Stream does not exist
5385  */
5386 NGTCP2_EXTERN int ngtcp2_conn_set_stream_user_data(ngtcp2_conn *conn,
5387                                                    int64_t stream_id,
5388                                                    void *stream_user_data);
5389 
5390 /**
5391  * @function
5392  *
5393  * `ngtcp2_conn_update_pkt_tx_time` sets the time instant of the next
5394  * packet transmission.  This function is noop if packet pacing is
5395  * disabled.  If packet pacing is enabled, this function must be
5396  * called after (multiple invocation of) `ngtcp2_conn_writev_stream`.
5397  * If packet aggregation (e.g., packet batching, GSO) is used, call
5398  * this function after all aggregated datagrams are sent, which
5399  * indicates multiple invocation of `ngtcp2_conn_writev_stream`.
5400  */
5401 NGTCP2_EXTERN void ngtcp2_conn_update_pkt_tx_time(ngtcp2_conn *conn,
5402                                                   ngtcp2_tstamp ts);
5403 
5404 /**
5405  * @function
5406  *
5407  * `ngtcp2_conn_get_send_quantum` returns the maximum number of bytes
5408  * that can be sent in one go without packet spacing.  If packet
5409  * pacing is disabled, this function returns SIZE_MAX.
5410  */
5411 NGTCP2_EXTERN size_t ngtcp2_conn_get_send_quantum(ngtcp2_conn *conn);
5412 
5413 /**
5414  * @function
5415  *
5416  * `ngtcp2_conn_get_stream_loss_count` returns the number of packets
5417  * that contain STREAM frame for a stream identified by |stream_id|
5418  * and are declared to be lost.  The number may include the spurious
5419  * losses.  If no stream identified by |stream_id| is found, this
5420  * function returns 0.
5421  */
5422 NGTCP2_EXTERN size_t ngtcp2_conn_get_stream_loss_count(ngtcp2_conn *conn,
5423                                                        int64_t stream_id);
5424 
5425 /**
5426  * @function
5427  *
5428  * `ngtcp2_strerror` returns the text representation of |liberr|.
5429  * |liberr| must be one of ngtcp2 library error codes (which is
5430  * defined as NGTCP2_ERR_* macro, such as
5431  * :macro:`NGTCP2_ERR_DECRYPT`).
5432  */
5433 NGTCP2_EXTERN const char *ngtcp2_strerror(int liberr);
5434 
5435 /**
5436  * @function
5437  *
5438  * `ngtcp2_err_is_fatal` returns nonzero if |liberr| is a fatal error.
5439  * |liberr| must be one of ngtcp2 library error codes (which is
5440  * defined as NGTCP2_ERR_* macro, such as
5441  * :macro:`NGTCP2_ERR_DECRYPT`).
5442  */
5443 NGTCP2_EXTERN int ngtcp2_err_is_fatal(int liberr);
5444 
5445 /**
5446  * @function
5447  *
5448  * `ngtcp2_err_infer_quic_transport_error_code` returns a QUIC
5449  * transport error code which corresponds to |liberr|.  |liberr| must
5450  * be one of ngtcp2 library error codes (which is defined as
5451  * NGTCP2_ERR_* macro, such as :macro:`NGTCP2_ERR_DECRYPT`).
5452  */
5453 NGTCP2_EXTERN uint64_t ngtcp2_err_infer_quic_transport_error_code(int liberr);
5454 
5455 /**
5456  * @function
5457  *
5458  * `ngtcp2_addr_init` initializes |dest| with the given arguments and
5459  * returns |dest|.
5460  */
5461 NGTCP2_EXTERN ngtcp2_addr *ngtcp2_addr_init(ngtcp2_addr *dest,
5462                                             const ngtcp2_sockaddr *addr,
5463                                             ngtcp2_socklen addrlen);
5464 
5465 /**
5466  * @function
5467  *
5468  * `ngtcp2_addr_copy_byte` copies |addr| of length |addrlen| into the
5469  * buffer pointed by :member:`dest->addr <ngtcp2_addr.addr>`.
5470  * :member:`dest->addrlen <ngtcp2_addr.addrlen>` is updated to have
5471  * |addrlen|.  This function assumes that :member:`dest->addr
5472  * <ngtcp2_addr.addr>` points to a buffer which has a sufficient
5473  * capacity to store the copy.
5474  */
5475 NGTCP2_EXTERN void ngtcp2_addr_copy_byte(ngtcp2_addr *dest,
5476                                          const ngtcp2_sockaddr *addr,
5477                                          ngtcp2_socklen addrlen);
5478 
5479 /**
5480  * @function
5481  *
5482  * `ngtcp2_path_storage_init` initializes |ps| with the given
5483  * arguments.  This function copies |local_addr| and |remote_addr|.
5484  */
5485 NGTCP2_EXTERN void ngtcp2_path_storage_init(ngtcp2_path_storage *ps,
5486                                             const ngtcp2_sockaddr *local_addr,
5487                                             ngtcp2_socklen local_addrlen,
5488                                             const ngtcp2_sockaddr *remote_addr,
5489                                             ngtcp2_socklen remote_addrlen,
5490                                             void *user_data);
5491 
5492 /**
5493  * @function
5494  *
5495  * `ngtcp2_path_storage_zero` initializes |ps| with the zero length
5496  * addresses.
5497  */
5498 NGTCP2_EXTERN void ngtcp2_path_storage_zero(ngtcp2_path_storage *ps);
5499 
5500 /**
5501  * @function
5502  *
5503  * `ngtcp2_settings_default` initializes |settings| with the default
5504  * values.  First this function fills |settings| with 0 and set the
5505  * default value to the following fields:
5506  *
5507  * * :type:`cc_algo <ngtcp2_settings.cc_algo>` =
5508  *   :enum:`ngtcp2_cc_algo.NGTCP2_CC_ALGO_CUBIC`
5509  * * :type:`initial_rtt <ngtcp2_settings.initial_rtt>` =
5510  *   :macro:`NGTCP2_DEFAULT_INITIAL_RTT`
5511  * * :type:`ack_thresh <ngtcp2_settings.ack_thresh>` = 2
5512  * * :type:`max_udp_payload_size
5513  *   <ngtcp2_settings.max_udp_payload_size>` = 1452
5514  * * :type:`handshake_timeout <ngtcp2_settings.handshake_timeout>` =
5515  *   :macro:`NGTCP2_DEFAULT_HANDSHAKE_TIMEOUT`.
5516  */
5517 NGTCP2_EXTERN void ngtcp2_settings_default_versioned(int settings_version,
5518                                                      ngtcp2_settings *settings);
5519 
5520 /**
5521  * @function
5522  *
5523  * `ngtcp2_transport_params_default` initializes |params| with the
5524  * default values.  First this function fills |params| with 0 and set
5525  * the default value to the following fields:
5526  *
5527  * * :type:`max_udp_payload_size
5528  *   <ngtcp2_transport_params.max_udp_payload_size>` =
5529  *   :macro:`NGTCP2_DEFAULT_MAX_RECV_UDP_PAYLOAD_SIZE`
5530  * * :type:`ack_delay_exponent
5531  *   <ngtcp2_transport_params.ack_delay_exponent>` =
5532  *   :macro:`NGTCP2_DEFAULT_ACK_DELAY_EXPONENT`
5533  * * :type:`max_ack_delay <ngtcp2_transport_params.max_ack_delay>` =
5534  *   :macro:`NGTCP2_DEFAULT_MAX_ACK_DELAY`
5535  * * :type:`active_connection_id_limit
5536  *   <ngtcp2_transport_params.active_connection_id_limit>` =
5537  *   :macro:`NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT`
5538  */
5539 NGTCP2_EXTERN void
5540 ngtcp2_transport_params_default_versioned(int transport_params_version,
5541                                           ngtcp2_transport_params *params);
5542 
5543 /**
5544  * @function
5545  *
5546  * `ngtcp2_mem_default` returns the default, system standard memory
5547  * allocator.
5548  */
5549 NGTCP2_EXTERN const ngtcp2_mem *ngtcp2_mem_default(void);
5550 
5551 /**
5552  * @macrosection
5553  *
5554  * ngtcp2_info macros
5555  */
5556 
5557 /**
5558  * @macro
5559  *
5560  * :macro:`NGTCP2_VERSION_AGE` is the age of :type:`ngtcp2_info`
5561  */
5562 #define NGTCP2_VERSION_AGE 1
5563 
5564 /**
5565  * @struct
5566  *
5567  * :type:`ngtcp2_info` is what `ngtcp2_version()` returns.  It holds
5568  * information about the particular ngtcp2 version.
5569  */
5570 typedef struct ngtcp2_info {
5571   /**
5572    * :member:`age` is the age of this struct.  This instance of ngtcp2
5573    * sets it to :macro:`NGTCP2_VERSION_AGE` but a future version may
5574    * bump it and add more struct fields at the bottom
5575    */
5576   int age;
5577   /**
5578    * :member:`version_num` is the :macro:`NGTCP2_VERSION_NUM` number
5579    * (since age ==1)
5580    */
5581   int version_num;
5582   /**
5583    * :member:`version_str` points to the :macro:`NGTCP2_VERSION`
5584    * string (since age ==1)
5585    */
5586   const char *version_str;
5587   /* -------- the above fields all exist when age == 1 */
5588 } ngtcp2_info;
5589 
5590 /**
5591  * @function
5592  *
5593  * `ngtcp2_version` returns a pointer to a ngtcp2_info struct with
5594  * version information about the run-time library in use.  The
5595  * |least_version| argument can be set to a 24 bit numerical value for
5596  * the least accepted version number and if the condition is not met,
5597  * this function will return a ``NULL``.  Pass in 0 to skip the
5598  * version checking.
5599  */
5600 NGTCP2_EXTERN const ngtcp2_info *ngtcp2_version(int least_version);
5601 
5602 /**
5603  * @function
5604  *
5605  * `ngtcp2_is_bidi_stream` returns nonzero if |stream_id| denotes
5606  * bidirectional stream.
5607  */
5608 NGTCP2_EXTERN int ngtcp2_is_bidi_stream(int64_t stream_id);
5609 
5610 /**
5611  * @function
5612  *
5613  * `ngtcp2_path_copy` copies |src| into |dest|.  This function assumes
5614  * that |dest| has enough buffer to store the deep copy of
5615  * :member:`src->local <ngtcp2_path.local>` and :member:`src->remote
5616  * <ngtcp2_path.remote>`.
5617  */
5618 NGTCP2_EXTERN void ngtcp2_path_copy(ngtcp2_path *dest, const ngtcp2_path *src);
5619 
5620 /**
5621  * @function
5622  *
5623  * `ngtcp2_path_eq` returns nonzero if |a| and |b| shares the same
5624  * local and remote addresses.
5625  */
5626 NGTCP2_EXTERN int ngtcp2_path_eq(const ngtcp2_path *a, const ngtcp2_path *b);
5627 
5628 /**
5629  * @function
5630  *
5631  * `ngtcp2_is_supported_version` returns nonzero if the library supports
5632  * QUIC version |version|.
5633  */
5634 NGTCP2_EXTERN int ngtcp2_is_supported_version(uint32_t version);
5635 
5636 /*
5637  * @function
5638  *
5639  * `ngtcp2_is_reserved_version` returns nonzero if |version| is a
5640  * reserved version.
5641  */
5642 NGTCP2_EXTERN int ngtcp2_is_reserved_version(uint32_t version);
5643 
5644 /**
5645  * @function
5646  *
5647  * `ngtcp2_select_version` selects and returns a version from the
5648  * version set |offered_versions| of |offered_versionslen| elements.
5649  * |preferred_versions| of |preferred_versionslen| elements specifies
5650  * the preference of versions, which is sorted in the order of
5651  * preference.  All versions included in |preferred_versions| must be
5652  * supported by the library, that is, passing a version to
5653  * `ngtcp2_is_supported_version` must return nonzero.  This function
5654  * is intended to be used by client when it receives Version
5655  * Negotiation packet.  If no version is selected, this function
5656  * returns 0.
5657  */
5658 NGTCP2_EXTERN uint32_t ngtcp2_select_version(const uint32_t *preferred_versions,
5659                                              size_t preferred_versionslen,
5660                                              const uint32_t *offered_versions,
5661                                              size_t offered_versionslen);
5662 
5663 /*
5664  * Versioned function wrappers
5665  */
5666 
5667 /*
5668  * `ngtcp2_conn_read_pkt` is a wrapper around
5669  * `ngtcp2_conn_read_pkt_versioned` to set the correct struct version.
5670  */
5671 #define ngtcp2_conn_read_pkt(CONN, PATH, PI, PKT, PKTLEN, TS)                  \
5672   ngtcp2_conn_read_pkt_versioned((CONN), (PATH), NGTCP2_PKT_INFO_VERSION,      \
5673                                  (PI), (PKT), (PKTLEN), (TS))
5674 
5675 /*
5676  * `ngtcp2_conn_write_pkt` is a wrapper around
5677  * `ngtcp2_conn_write_pkt_versioned` to set the correct struct
5678  * version.
5679  */
5680 #define ngtcp2_conn_write_pkt(CONN, PATH, PI, DEST, DESTLEN, TS)               \
5681   ngtcp2_conn_write_pkt_versioned((CONN), (PATH), NGTCP2_PKT_INFO_VERSION,     \
5682                                   (PI), (DEST), (DESTLEN), (TS))
5683 
5684 /*
5685  * `ngtcp2_conn_write_stream` is a wrapper around
5686  * `ngtcp2_conn_write_stream_versioned` to set the correct struct
5687  * version.
5688  */
5689 #define ngtcp2_conn_write_stream(CONN, PATH, PI, DEST, DESTLEN, PDATALEN,      \
5690                                  FLAGS, STREAM_ID, DATA, DATALEN, TS)          \
5691   ngtcp2_conn_write_stream_versioned(                                          \
5692       (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN),        \
5693       (PDATALEN), (FLAGS), (STREAM_ID), (DATA), (DATALEN), (TS))
5694 
5695 /*
5696  * `ngtcp2_conn_writev_stream` is a wrapper around
5697  * `ngtcp2_conn_writev_stream_versioned` to set the correct struct
5698  * version.
5699  */
5700 #define ngtcp2_conn_writev_stream(CONN, PATH, PI, DEST, DESTLEN, PDATALEN,     \
5701                                   FLAGS, STREAM_ID, DATAV, DATAVCNT, TS)       \
5702   ngtcp2_conn_writev_stream_versioned(                                         \
5703       (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN),        \
5704       (PDATALEN), (FLAGS), (STREAM_ID), (DATAV), (DATAVCNT), (TS))
5705 
5706 /*
5707  * `ngtcp2_conn_writev_datagram` is a wrapper around
5708  * `ngtcp2_conn_writev_datagram_versioned` to set the correct struct
5709  * version.
5710  */
5711 #define ngtcp2_conn_writev_datagram(CONN, PATH, PI, DEST, DESTLEN, PACCEPTED,  \
5712                                     FLAGS, DGRAM_ID, DATAV, DATAVCNT, TS)      \
5713   ngtcp2_conn_writev_datagram_versioned(                                       \
5714       (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN),        \
5715       (PACCEPTED), (FLAGS), (DGRAM_ID), (DATAV), (DATAVCNT), (TS))
5716 
5717 /*
5718  * `ngtcp2_conn_write_connection_close` is a wrapper around
5719  * `ngtcp2_conn_write_connection_close_versioned` to set the correct
5720  * struct version.
5721  */
5722 #define ngtcp2_conn_write_connection_close(CONN, PATH, PI, DEST, DESTLEN,      \
5723                                            CCERR, TS)                          \
5724   ngtcp2_conn_write_connection_close_versioned(                                \
5725       (CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN),        \
5726       (CCERR), (TS))
5727 
5728 /*
5729  * `ngtcp2_encode_transport_params` is a wrapper around
5730  * `ngtcp2_encode_transport_params_versioned` to set the correct
5731  * struct version.
5732  */
5733 #define ngtcp2_encode_transport_params(DEST, DESTLEN, EXTTYPE, PARAMS)         \
5734   ngtcp2_encode_transport_params_versioned(                                    \
5735       (DEST), (DESTLEN), (EXTTYPE), NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS))
5736 
5737 /*
5738  * `ngtcp2_decode_transport_params` is a wrapper around
5739  * `ngtcp2_decode_transport_params_versioned` to set the correct
5740  * struct version.
5741  */
5742 #define ngtcp2_decode_transport_params(PARAMS, EXTTYPE, DATA, DATALEN)         \
5743   ngtcp2_decode_transport_params_versioned(                                    \
5744       NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS), (EXTTYPE), (DATA), (DATALEN))
5745 
5746 /*
5747  * `ngtcp2_conn_client_new` is a wrapper around
5748  * `ngtcp2_conn_client_new_versioned` to set the correct struct
5749  * version.
5750  */
5751 #define ngtcp2_conn_client_new(PCONN, DCID, SCID, PATH, VERSION, CALLBACKS,    \
5752                                SETTINGS, PARAMS, MEM, USER_DATA)               \
5753   ngtcp2_conn_client_new_versioned(                                            \
5754       (PCONN), (DCID), (SCID), (PATH), (VERSION), NGTCP2_CALLBACKS_VERSION,    \
5755       (CALLBACKS), NGTCP2_SETTINGS_VERSION, (SETTINGS),                        \
5756       NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS), (MEM), (USER_DATA))
5757 
5758 /*
5759  * `ngtcp2_conn_server_new` is a wrapper around
5760  * `ngtcp2_conn_server_new_versioned` to set the correct struct
5761  * version.
5762  */
5763 #define ngtcp2_conn_server_new(PCONN, DCID, SCID, PATH, VERSION, CALLBACKS,    \
5764                                SETTINGS, PARAMS, MEM, USER_DATA)               \
5765   ngtcp2_conn_server_new_versioned(                                            \
5766       (PCONN), (DCID), (SCID), (PATH), (VERSION), NGTCP2_CALLBACKS_VERSION,    \
5767       (CALLBACKS), NGTCP2_SETTINGS_VERSION, (SETTINGS),                        \
5768       NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS), (MEM), (USER_DATA))
5769 
5770 /*
5771  * `ngtcp2_conn_set_early_remote_transport_params` is a wrapper around
5772  * `ngtcp2_conn_set_early_remote_transport_params_versioned` to set
5773  * the correct struct version.
5774  */
5775 #define ngtcp2_conn_set_early_remote_transport_params(CONN, PARAMS)            \
5776   ngtcp2_conn_set_early_remote_transport_params_versioned(                     \
5777       (CONN), NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS))
5778 
5779 /*
5780  * `ngtcp2_conn_set_local_transport_params` is a wrapper around
5781  * `ngtcp2_conn_set_local_transport_params_versioned` to set the
5782  * correct struct version.
5783  */
5784 #define ngtcp2_conn_set_local_transport_params(CONN, PARAMS)                   \
5785   ngtcp2_conn_set_local_transport_params_versioned(                            \
5786       (CONN), NGTCP2_TRANSPORT_PARAMS_VERSION, (PARAMS))
5787 
5788 /*
5789  * `ngtcp2_transport_params_default` is a wrapper around
5790  * `ngtcp2_transport_params_default_versioned` to set the correct
5791  * struct version.
5792  */
5793 #define ngtcp2_transport_params_default(PARAMS)                                \
5794   ngtcp2_transport_params_default_versioned(NGTCP2_TRANSPORT_PARAMS_VERSION,   \
5795                                             (PARAMS))
5796 
5797 /*
5798  * `ngtcp2_conn_get_conn_stat` is a wrapper around
5799  * `ngtcp2_conn_get_conn_stat_versioned` to set the correct struct
5800  * version.
5801  */
5802 #define ngtcp2_conn_get_conn_stat(CONN, CSTAT)                                 \
5803   ngtcp2_conn_get_conn_stat_versioned((CONN), NGTCP2_CONN_STAT_VERSION, (CSTAT))
5804 
5805 /*
5806  * `ngtcp2_settings_default` is a wrapper around
5807  * `ngtcp2_settings_default_versioned` to set the correct struct
5808  * version.
5809  */
5810 #define ngtcp2_settings_default(SETTINGS)                                      \
5811   ngtcp2_settings_default_versioned(NGTCP2_SETTINGS_VERSION, (SETTINGS))
5812 
5813 #ifdef _MSC_VER
5814 #  pragma warning(pop)
5815 #endif
5816 
5817 #ifdef __cplusplus
5818 }
5819 #endif
5820 
5821 #endif /* NGTCP2_H */
5822