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