summaryrefslogtreecommitdiffstats
path: root/src/quic_frame.c
blob: f98f5944ed06025db01a9a7eb664469003515f25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
/*
 * Copyright 2019 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <string.h>

#include <import/eb64tree.h>
#include <haproxy/quic_conn-t.h>
#include <haproxy/quic_enc.h>
#include <haproxy/quic_frame.h>
#include <haproxy/quic_tp-t.h>
#include <haproxy/trace.h>

#define TRACE_SOURCE    &trace_quic

const char *quic_frame_type_string(enum quic_frame_type ft)
{
	switch (ft) {
	case QUIC_FT_PADDING:
		return "PADDING";
	case QUIC_FT_PING:
		return "PING";
	case QUIC_FT_ACK:
		return "ACK";
	case QUIC_FT_ACK_ECN:
		return "ACK_ENC";
	case QUIC_FT_RESET_STREAM:
		return "RESET_STREAM";
	case QUIC_FT_STOP_SENDING:
		return "STOP_SENDING";
	case QUIC_FT_CRYPTO:
		return "CRYPTO";
	case QUIC_FT_NEW_TOKEN:
		return "NEW_TOKEN";

	case QUIC_FT_STREAM_8:
		return "STREAM_8";
	case QUIC_FT_STREAM_9:
		return "STREAM_9";
	case QUIC_FT_STREAM_A:
		return "STREAM_A";
	case QUIC_FT_STREAM_B:
		return "STREAM_B";
	case QUIC_FT_STREAM_C:
		return "STREAM_C";
	case QUIC_FT_STREAM_D:
		return "STREAM_D";
	case QUIC_FT_STREAM_E:
		return "STREAM_E";
	case QUIC_FT_STREAM_F:
		return "STREAM_F";

	case QUIC_FT_MAX_DATA:
		return "MAX_DATA";
	case QUIC_FT_MAX_STREAM_DATA:
		return "MAX_STREAM_DATA";
	case QUIC_FT_MAX_STREAMS_BIDI:
		return "MAX_STREAMS_BIDI";
	case QUIC_FT_MAX_STREAMS_UNI:
		return "MAX_STREAMS_UNI";
	case QUIC_FT_DATA_BLOCKED:
		return "DATA_BLOCKED";
	case QUIC_FT_STREAM_DATA_BLOCKED:
		return "STREAM_DATA_BLOCKED";
	case QUIC_FT_STREAMS_BLOCKED_BIDI:
		return "STREAMS_BLOCKED_BIDI";
	case QUIC_FT_STREAMS_BLOCKED_UNI:
		return "STREAMS_BLOCKED_UNI";
	case QUIC_FT_NEW_CONNECTION_ID:
		return "NEW_CONNECTION_ID";
	case QUIC_FT_RETIRE_CONNECTION_ID:
		return "RETIRE_CONNECTION_ID";
	case QUIC_FT_PATH_CHALLENGE:
		return "PATH_CHALLENGE";
	case QUIC_FT_PATH_RESPONSE:
		return "PATH_RESPONSE";
	case QUIC_FT_CONNECTION_CLOSE:
		return "CONNECTION_CLOSE";
	case QUIC_FT_CONNECTION_CLOSE_APP:
		return "CONNECTION_CLOSE_APP";
	case QUIC_FT_HANDSHAKE_DONE:
		return "HANDSHAKE_DONE";
	default:
		return "UNKNOWN";
	}
}

static void chunk_cc_phrase_appendf(struct buffer *buf,
                                    const unsigned char *phr, size_t phrlen)
{
	chunk_appendf(buf, " reason_phrase: '");
	while (phrlen--)
		chunk_appendf(buf, "%c", *phr++);
	chunk_appendf(buf, "'");
}

/* Add traces to <buf> depending on <frm> frame type. */
void chunk_frm_appendf(struct buffer *buf, const struct quic_frame *frm)
{
	chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
	switch (frm->type) {
	case QUIC_FT_CRYPTO:
	{
		const struct quic_crypto *cf = &frm->crypto;
		chunk_appendf(buf, " cfoff=%llu cflen=%llu",
		              (ull)cf->offset, (ull)cf->len);
		break;
	}
	case QUIC_FT_RESET_STREAM:
	{
		const struct quic_reset_stream *rs = &frm->reset_stream;
		chunk_appendf(buf, " id=%llu app_error_code=%llu final_size=%llu",
		              (ull)rs->id, (ull)rs->app_error_code, (ull)rs->final_size);
		break;
	}
	case QUIC_FT_STOP_SENDING:
	{
		const struct quic_stop_sending *s = &frm->stop_sending;
		chunk_appendf(&trace_buf, " id=%llu app_error_code=%llu",
		              (ull)s->id, (ull)s->app_error_code);
		break;
	}
	case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
	{
		const struct quic_stream *s = &frm->stream;
		chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
		              !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
		              !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
		              (ull)s->id, (ull)s->offset.key, (ull)s->len);
		break;
	}
	case QUIC_FT_MAX_DATA:
	{
		const struct quic_max_data *s = &frm->max_data;
		chunk_appendf(&trace_buf, " max_data=%llu", (ull)s->max_data);
		break;
	}
	case QUIC_FT_MAX_STREAM_DATA:
	{
		const struct quic_max_stream_data *s = &frm->max_stream_data;
		chunk_appendf(&trace_buf, " id=%llu max_stream_data=%llu",
		              (ull)s->id, (ull)s->max_stream_data);
		break;
	}
	case QUIC_FT_MAX_STREAMS_BIDI:
	{
		const struct quic_max_streams *s = &frm->max_streams_bidi;
		chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
		break;
	}
	case QUIC_FT_MAX_STREAMS_UNI:
	{
		const struct quic_max_streams *s = &frm->max_streams_uni;
		chunk_appendf(&trace_buf, " max_streams=%llu", (ull)s->max_streams);
		break;
	}
	case QUIC_FT_DATA_BLOCKED:
	{
		const struct quic_data_blocked *s = &frm->data_blocked;
		chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
		break;
	}
	case QUIC_FT_STREAM_DATA_BLOCKED:
	{
		const struct quic_stream_data_blocked *s = &frm->stream_data_blocked;
		chunk_appendf(&trace_buf, " id=%llu limit=%llu",
		              (ull)s->id, (ull)s->limit);
		break;
	}
	case QUIC_FT_STREAMS_BLOCKED_BIDI:
	{
		const struct quic_streams_blocked *s = &frm->streams_blocked_bidi;
		chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
		break;
	}
	case QUIC_FT_STREAMS_BLOCKED_UNI:
	{
		const struct quic_streams_blocked *s = &frm->streams_blocked_uni;
		chunk_appendf(&trace_buf, " limit=%llu", (ull)s->limit);
		break;
	}
	case QUIC_FT_RETIRE_CONNECTION_ID:
	{
		const struct quic_retire_connection_id *rci = &frm->retire_connection_id;
		chunk_appendf(&trace_buf, " seq_num=%llu", (ull)rci->seq_num);
		break;
	}
	case QUIC_FT_CONNECTION_CLOSE:
	{
		const struct quic_connection_close *cc = &frm->connection_close;
		size_t plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
		chunk_appendf(&trace_buf,
		              " error_code=%llu frame_type=%llu reason_phrase_len=%llu",
		              (ull)cc->error_code, (ull)cc->frame_type,
		              (ull)cc->reason_phrase_len);
		if (plen)
			chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
		break;
	}
	case QUIC_FT_CONNECTION_CLOSE_APP:
	{
		const struct quic_connection_close_app *cc = &frm->connection_close_app;
		size_t plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
		chunk_appendf(&trace_buf,
		              " error_code=%llu reason_phrase_len=%llu",
		              (ull)cc->error_code, (ull)cc->reason_phrase_len);
		if (plen)
			chunk_cc_phrase_appendf(&trace_buf, cc->reason_phrase, plen);
		break;
	}
	}
}

/* Encode <frm> PADDING frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_padding_frame(unsigned char **buf, const unsigned char *end,
                                    struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_padding *padding = &frm->padding;

	if (end - *buf < padding->len - 1)
		return 0;

	memset(*buf, 0, padding->len - 1);
	*buf += padding->len - 1;

	return 1;
}

/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_padding_frame(struct quic_frame *frm, struct quic_conn *qc,
                                    const unsigned char **buf, const unsigned char *end)
{
	const unsigned char *beg;
	struct quic_padding *padding = &frm->padding;

	beg = *buf;
	padding->len = 1;
	while (*buf < end && !**buf)
		(*buf)++;
	padding->len += *buf - beg;

	return 1;
}

/* Encode a ACK frame into <buf> buffer.
 * Always succeeds.
 */
static int quic_build_ping_frame(unsigned char **buf, const unsigned char *end,
                                 struct quic_frame *frm, struct quic_conn *conn)
{
	/* No field */
	return 1;
}

/* Parse a PADDING frame from <buf> buffer with <end> as end into <frm> frame.
 * Always succeeds.
 */
static int quic_parse_ping_frame(struct quic_frame *frm, struct quic_conn *qc,
                                 const unsigned char **buf, const unsigned char *end)
{
	/* No field */
	return 1;
}

/* Encode a ACK frame.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_ack_frame(unsigned char **buf, const unsigned char *end,
                                struct quic_frame *frm, struct quic_conn *qc)
{
	struct quic_tx_ack *tx_ack = &frm->tx_ack;
	struct eb64_node *ar, *prev_ar;
	struct quic_arng_node *ar_node, *prev_ar_node;

	ar = eb64_last(&tx_ack->arngs->root);
	ar_node = eb64_entry(ar, struct quic_arng_node, first);
	TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM,
	            qc,, &ar_node->last, &ar_node->first.key);
	if (!quic_enc_int(buf, end, ar_node->last) ||
	    !quic_enc_int(buf, end, tx_ack->ack_delay) ||
	    !quic_enc_int(buf, end, tx_ack->arngs->sz - 1) ||
	    !quic_enc_int(buf, end, ar_node->last - ar_node->first.key))
		return 0;

	while ((prev_ar = eb64_prev(ar))) {
		prev_ar_node = eb64_entry(prev_ar, struct quic_arng_node, first);
		TRACE_DEVEL("ack range", QUIC_EV_CONN_PRSAFRM, qc,,
		            &prev_ar_node->last, &prev_ar_node->first.key);
		if (!quic_enc_int(buf, end, ar_node->first.key - prev_ar_node->last - 2) ||
		    !quic_enc_int(buf, end, prev_ar_node->last - prev_ar_node->first.key))
			return 0;

		ar = prev_ar;
		ar_node = eb64_entry(ar, struct quic_arng_node, first);
	}

	return 1;
}

/* Parse an ACK frame header from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_ack_frame_header(struct quic_frame *frm, struct quic_conn *qc,
                                       const unsigned char **buf, const unsigned char *end)
{
	int ret;
	struct quic_ack *ack = &frm->ack;

	ret = quic_dec_int(&ack->largest_ack, buf, end);
	if (!ret)
		return 0;

	ret = quic_dec_int(&ack->ack_delay, buf, end);
	if (!ret)
		return 0;

	ret = quic_dec_int(&ack->ack_range_num, buf, end);
	if (!ret)
		return 0;

	ret = quic_dec_int(&ack->first_ack_range, buf, end);
	if (!ret)
		return 0;

	return 1;
}

/* Encode a ACK_ECN frame.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_ack_ecn_frame(unsigned char **buf, const unsigned char *end,
                                    struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_ack *ack = &frm->ack;

	return quic_enc_int(buf, end, ack->largest_ack) &&
		quic_enc_int(buf, end, ack->ack_delay) &&
		quic_enc_int(buf, end, ack->first_ack_range) &&
		quic_enc_int(buf, end, ack->ack_range_num);
}

/* Parse an ACK_ECN frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_ack_ecn_frame(struct quic_frame *frm, struct quic_conn *qc,
                                    const unsigned char **buf, const unsigned char *end)
{
	struct quic_ack *ack = &frm->ack;

	return quic_dec_int(&ack->largest_ack, buf, end) &&
		quic_dec_int(&ack->ack_delay, buf, end) &&
		quic_dec_int(&ack->first_ack_range, buf, end) &&
		quic_dec_int(&ack->ack_range_num, buf, end);
}

/* Encode a RESET_STREAM frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_reset_stream_frame(unsigned char **buf, const unsigned char *end,
                                         struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_reset_stream *reset_stream = &frm->reset_stream;

	return quic_enc_int(buf, end, reset_stream->id) &&
		quic_enc_int(buf, end, reset_stream->app_error_code) &&
		quic_enc_int(buf, end, reset_stream->final_size);
}

/* Parse a RESET_STREAM frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_reset_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
                                         const unsigned char **buf, const unsigned char *end)
{
	struct quic_reset_stream *reset_stream = &frm->reset_stream;

	return quic_dec_int(&reset_stream->id, buf, end) &&
		quic_dec_int(&reset_stream->app_error_code, buf, end) &&
		quic_dec_int(&reset_stream->final_size, buf, end);
}

/* Encode a STOP_SENDING frame.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_stop_sending_frame(unsigned char **buf, const unsigned char *end,
                                         struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_stop_sending *stop_sending = &frm->stop_sending;

	return quic_enc_int(buf, end, stop_sending->id) &&
		quic_enc_int(buf, end, stop_sending->app_error_code);
}

/* Parse a STOP_SENDING frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_stop_sending_frame(struct quic_frame *frm, struct quic_conn *qc,
                                         const unsigned char **buf, const unsigned char *end)
{
	struct quic_stop_sending *stop_sending = &frm->stop_sending;

	return quic_dec_int(&stop_sending->id, buf, end) &&
		quic_dec_int(&stop_sending->app_error_code, buf, end);
}

/* Encode a CRYPTO frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_crypto_frame(unsigned char **buf, const unsigned char *end,
                                   struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_crypto *crypto = &frm->crypto;
	const struct quic_enc_level *qel = crypto->qel;
	size_t offset, len;

	if (!quic_enc_int(buf, end, crypto->offset) ||
	    !quic_enc_int(buf, end, crypto->len) || end - *buf < crypto->len)
		return 0;

	len = crypto->len;
	offset = crypto->offset;
	while (len) {
		int idx;
		size_t to_copy;
		const unsigned char *data;

		idx = offset >> QUIC_CRYPTO_BUF_SHIFT;
		to_copy = qel->tx.crypto.bufs[idx]->sz - (offset & QUIC_CRYPTO_BUF_MASK);
		if (to_copy > len)
			to_copy = len;
		data = qel->tx.crypto.bufs[idx]->data + (offset & QUIC_CRYPTO_BUF_MASK);
		memcpy(*buf, data, to_copy);
		*buf += to_copy;
		offset += to_copy;
		len -= to_copy;
	}

	return 1;
}

/* Parse a CRYPTO frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_crypto_frame(struct quic_frame *frm, struct quic_conn *qc,
                                   const unsigned char **buf, const unsigned char *end)
{
	struct quic_crypto *crypto = &frm->crypto;

	if (!quic_dec_int(&crypto->offset, buf, end) ||
	    !quic_dec_int(&crypto->len, buf, end) || end - *buf < crypto->len)
		return 0;

	crypto->data = *buf;
	*buf += crypto->len;

	return 1;
}

/* Encode a NEW_TOKEN frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_new_token_frame(unsigned char **buf, const unsigned char *end,
                                      struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_new_token *new_token = &frm->new_token;

	if (!quic_enc_int(buf, end, new_token->len) || end - *buf < new_token->len)
		return 0;

	memcpy(*buf, new_token->data, new_token->len);

	return 1;
}

/* Parse a NEW_TOKEN frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_new_token_frame(struct quic_frame *frm, struct quic_conn *qc,
                                      const unsigned char **buf, const unsigned char *end)
{
	struct quic_new_token *new_token = &frm->new_token;

	if (!quic_dec_int(&new_token->len, buf, end) || end - *buf < new_token->len)
		return 0;

	new_token->data = *buf;
	*buf += new_token->len;

	return 1;
}

/* Encode a STREAM frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_stream_frame(unsigned char **buf, const unsigned char *end,
                                   struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_stream *stream = &frm->stream;
	const unsigned char *wrap;

	if (!quic_enc_int(buf, end, stream->id) ||
	    ((frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) && !quic_enc_int(buf, end, stream->offset.key)) ||
	    ((frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) &&
	     (!quic_enc_int(buf, end, stream->len) || end - *buf < stream->len)))
		return 0;

	wrap = (const unsigned char *)b_wrap(stream->buf);
	if (stream->data + stream->len > wrap) {
		size_t to_copy = wrap - stream->data;
		memcpy(*buf, stream->data, to_copy);
		*buf += to_copy;

		to_copy = stream->len - to_copy;
		memcpy(*buf, b_orig(stream->buf), to_copy);
		*buf += to_copy;
	}
	else {
		memcpy(*buf, stream->data, stream->len);
		*buf += stream->len;
	}

	return 1;
}

/* Parse a STREAM frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_stream_frame(struct quic_frame *frm, struct quic_conn *qc,
                                   const unsigned char **buf, const unsigned char *end)
{
	struct quic_stream *stream = &frm->stream;

	if (!quic_dec_int(&stream->id, buf, end))
		return 0;

	/* Offset parsing */
	if (!(frm->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)) {
		stream->offset.key = 0;
	}
	else if (!quic_dec_int((uint64_t *)&stream->offset.key, buf, end))
		return 0;

	/* Length parsing */
	if (!(frm->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT)) {
		stream->len = end - *buf;
	}
	else if (!quic_dec_int(&stream->len, buf, end) || end - *buf < stream->len)
		return 0;

	stream->fin = (frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT);

	stream->data = *buf;
	*buf += stream->len;

	return 1;
}

/* Encode a MAX_DATA frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_max_data_frame(unsigned char **buf, const unsigned char *end,
                                     struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_max_data *max_data = &frm->max_data;

	return quic_enc_int(buf, end, max_data->max_data);
}

/* Parse a MAX_DATA frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_max_data_frame(struct quic_frame *frm, struct quic_conn *qc,
                                     const unsigned char **buf, const unsigned char *end)
{
	struct quic_max_data *max_data = &frm->max_data;

	return quic_dec_int(&max_data->max_data, buf, end);
}

/* Encode a MAX_STREAM_DATA frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_max_stream_data_frame(unsigned char **buf, const unsigned char *end,
                                            struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;

	return quic_enc_int(buf, end, max_stream_data->id) &&
		quic_enc_int(buf, end, max_stream_data->max_stream_data);
}

/* Parse a MAX_STREAM_DATA frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_max_stream_data_frame(struct quic_frame *frm, struct quic_conn *qc,
                                            const unsigned char **buf, const unsigned char *end)
{
	struct quic_max_stream_data *max_stream_data = &frm->max_stream_data;

	return quic_dec_int(&max_stream_data->id, buf, end) &&
		quic_dec_int(&max_stream_data->max_stream_data, buf, end);
}

/* Encode a MAX_STREAMS frame for bidirectional streams into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_max_streams_bidi_frame(unsigned char **buf, const unsigned char *end,
                                             struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;

	return quic_enc_int(buf, end, max_streams_bidi->max_streams);
}

/* Parse a MAX_STREAMS frame for bidirectional streams from <buf> buffer with <end>
 * as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_max_streams_bidi_frame(struct quic_frame *frm, struct quic_conn *qc,
                                             const unsigned char **buf, const unsigned char *end)
{
	struct quic_max_streams *max_streams_bidi = &frm->max_streams_bidi;

	return quic_dec_int(&max_streams_bidi->max_streams, buf, end);
}

/* Encode a MAX_STREAMS frame for unidirectional streams into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_max_streams_uni_frame(unsigned char **buf, const unsigned char *end,
                                            struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;

	return quic_enc_int(buf, end, max_streams_uni->max_streams);
}

/* Parse a MAX_STREAMS frame for undirectional streams from <buf> buffer with <end>
 * as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_max_streams_uni_frame(struct quic_frame *frm, struct quic_conn *qc,
                                            const unsigned char **buf, const unsigned char *end)
{
	struct quic_max_streams *max_streams_uni = &frm->max_streams_uni;

	return quic_dec_int(&max_streams_uni->max_streams, buf, end);
}

/* Encode a DATA_BLOCKED frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_data_blocked_frame(unsigned char **buf, const unsigned char *end,
                                         struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_data_blocked *data_blocked = &frm->data_blocked;

	return quic_enc_int(buf, end, data_blocked->limit);
}

/* Parse a DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
                                         const unsigned char **buf, const unsigned char *end)
{
	struct quic_data_blocked *data_blocked = &frm->data_blocked;

	return quic_dec_int(&data_blocked->limit, buf, end);
}

/* Encode a STREAM_DATA_BLOCKED into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_stream_data_blocked_frame(unsigned char **buf, const unsigned char *end,
                                                struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;

	return quic_enc_int(buf, end, stream_data_blocked->id) &&
		quic_enc_int(buf, end, stream_data_blocked->limit);
}

/* Parse a STREAM_DATA_BLOCKED frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_stream_data_blocked_frame(struct quic_frame *frm, struct quic_conn *qc,
                                                const unsigned char **buf, const unsigned char *end)
{
	struct quic_stream_data_blocked *stream_data_blocked = &frm->stream_data_blocked;

	return quic_dec_int(&stream_data_blocked->id, buf, end) &&
		quic_dec_int(&stream_data_blocked->limit, buf, end);
}

/* Encode a STREAMS_BLOCKED frame for bidirectional streams into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_streams_blocked_bidi_frame(unsigned char **buf, const unsigned char *end,
                                                 struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;

	return quic_enc_int(buf, end, streams_blocked_bidi->limit);
}

/* Parse a STREAMS_BLOCKED frame for bidirectional streams from <buf> buffer with <end>
 * as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_streams_blocked_bidi_frame(struct quic_frame *frm, struct quic_conn *qc,
                                                 const unsigned char **buf, const unsigned char *end)
{
	struct quic_streams_blocked *streams_blocked_bidi = &frm->streams_blocked_bidi;

	return quic_dec_int(&streams_blocked_bidi->limit, buf, end);
}

/* Encode a STREAMS_BLOCKED frame for unidirectional streams into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_streams_blocked_uni_frame(unsigned char **buf, const unsigned char *end,
                                                struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;

	return quic_enc_int(buf, end, streams_blocked_uni->limit);
}

/* Parse a STREAMS_BLOCKED frame for unidirectional streams from <buf> buffer with <end>
 * as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_streams_blocked_uni_frame(struct quic_frame *frm, struct quic_conn *qc,
                                                const unsigned char **buf, const unsigned char *end)
{
	struct quic_streams_blocked *streams_blocked_uni = &frm->streams_blocked_uni;

	return quic_dec_int(&streams_blocked_uni->limit, buf, end);
}

/* Encode a NEW_CONNECTION_ID frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_new_connection_id_frame(unsigned char **buf, const unsigned char *end,
                                              struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_new_connection_id *new_cid = &frm->new_connection_id;

	if (!quic_enc_int(buf, end, new_cid->seq_num) ||
	    !quic_enc_int(buf, end, new_cid->retire_prior_to) ||
	    end - *buf < sizeof new_cid->cid.len + new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
		return 0;

	*(*buf)++ = new_cid->cid.len;

	if (new_cid->cid.len) {
		memcpy(*buf, new_cid->cid.data, new_cid->cid.len);
		*buf += new_cid->cid.len;
	}
	memcpy(*buf, new_cid->stateless_reset_token, QUIC_STATELESS_RESET_TOKEN_LEN);
	*buf += QUIC_STATELESS_RESET_TOKEN_LEN;

	return 1;
}

/* Parse a NEW_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_new_connection_id_frame(struct quic_frame *frm, struct quic_conn *qc,
                                              const unsigned char **buf, const unsigned char *end)
{
	struct quic_new_connection_id *new_cid = &frm->new_connection_id;

	if (!quic_dec_int(&new_cid->seq_num, buf, end) ||
	    !quic_dec_int(&new_cid->retire_prior_to, buf, end) || end <= *buf)
		return 0;

	new_cid->cid.len = *(*buf)++;
	if (end - *buf < new_cid->cid.len + QUIC_STATELESS_RESET_TOKEN_LEN)
		return 0;

	if (new_cid->cid.len) {
		new_cid->cid.data = *buf;
		*buf += new_cid->cid.len;
	}
	new_cid->stateless_reset_token = *buf;
	*buf += QUIC_STATELESS_RESET_TOKEN_LEN;

	return 1;
}

/* Encode a RETIRE_CONNECTION_ID frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_retire_connection_id_frame(unsigned char **buf, const unsigned char *end,
                                                 struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;

	return quic_enc_int(buf, end, retire_connection_id->seq_num);
}

/* Parse a RETIRE_CONNECTION_ID frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_retire_connection_id_frame(struct quic_frame *frm, struct quic_conn *qc,
                                                 const unsigned char **buf, const unsigned char *end)
{
	struct quic_retire_connection_id *retire_connection_id = &frm->retire_connection_id;

	return quic_dec_int(&retire_connection_id->seq_num, buf, end);
}

/* Encode a PATH_CHALLENGE frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_path_challenge_frame(unsigned char **buf, const unsigned char *end,
                                           struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_path_challenge *path_challenge = &frm->path_challenge;

	if (end - *buf < sizeof path_challenge->data)
		return 0;

	memcpy(*buf, path_challenge->data, sizeof path_challenge->data);
	*buf += sizeof path_challenge->data;

	return 1;
}

/* Parse a PATH_CHALLENGE frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_path_challenge_frame(struct quic_frame *frm, struct quic_conn *qc,
                                           const unsigned char **buf, const unsigned char *end)
{
	struct quic_path_challenge *path_challenge = &frm->path_challenge;

	if (end - *buf < sizeof path_challenge->data)
		return 0;

	memcpy(path_challenge->data, *buf, sizeof path_challenge->data);
	*buf += sizeof path_challenge->data;

	return 1;
}


/* Encode a PATH_RESPONSE frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_path_response_frame(unsigned char **buf, const unsigned char *end,
                                          struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;

	if (end - *buf < sizeof path_challenge_response->data)
		return 0;

	memcpy(*buf, path_challenge_response->data, sizeof path_challenge_response->data);
	*buf += sizeof path_challenge_response->data;

	return 1;
}

/* Parse a PATH_RESPONSE frame from <buf> buffer with <end> as end into <frm> frame.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_path_response_frame(struct quic_frame *frm, struct quic_conn *qc,
                                          const unsigned char **buf, const unsigned char *end)
{
	struct quic_path_challenge_response *path_challenge_response = &frm->path_challenge_response;

	if (end - *buf < sizeof path_challenge_response->data)
		return 0;

	memcpy(path_challenge_response->data, *buf, sizeof path_challenge_response->data);
	*buf += sizeof path_challenge_response->data;

	return 1;
}

/* Encode a CONNECTION_CLOSE frame at QUIC layer into <buf> buffer.
 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
 * and another at QUIC layer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_connection_close_frame(unsigned char **buf, const unsigned char *end,
                                             struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_connection_close *cc = &frm->connection_close;

	if (!quic_enc_int(buf, end, cc->error_code) ||
	    !quic_enc_int(buf, end, cc->frame_type) ||
	    !quic_enc_int(buf, end, cc->reason_phrase_len) ||
	    end - *buf < cc->reason_phrase_len)
		return 0;

	memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
	*buf += cc->reason_phrase_len;

	return 1;
}

/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
 * and another at QUIC layer.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_connection_close_frame(struct quic_frame *frm, struct quic_conn *qc,
                                             const unsigned char **buf, const unsigned char *end)
{
	size_t plen;
	struct quic_connection_close *cc = &frm->connection_close;

	if (!quic_dec_int(&cc->error_code, buf, end) ||
	    !quic_dec_int(&cc->frame_type, buf, end) ||
	    !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
	    end - *buf < cc->reason_phrase_len)
		return 0;

	plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
	memcpy(cc->reason_phrase, *buf, plen);
	*buf += cc->reason_phrase_len;

	return 1;
}

/* Encode a CONNECTION_CLOSE frame at application layer into <buf> buffer.
 * Note there exist two types of CONNECTION_CLOSE frame, one for application layer
 * and another at QUIC layer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 */
static int quic_build_connection_close_app_frame(unsigned char **buf, const unsigned char *end,
                                                 struct quic_frame *frm, struct quic_conn *conn)
{
	struct quic_connection_close_app *cc = &frm->connection_close_app;

	if (!quic_enc_int(buf, end, cc->error_code) ||
	    !quic_enc_int(buf, end, cc->reason_phrase_len) ||
	    end - *buf < cc->reason_phrase_len)
		return 0;

	memcpy(*buf, cc->reason_phrase, cc->reason_phrase_len);
	*buf += cc->reason_phrase_len;

	return 1;
}

/* Parse a CONNECTION_CLOSE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
 * Note there exist two types of CONNECTION_CLOSE frame, one for the application layer
 * and another at QUIC layer.
 * Return 1 if succeeded (enough room to parse this frame), 0 if not.
 */
static int quic_parse_connection_close_app_frame(struct quic_frame *frm, struct quic_conn *qc,
                                                 const unsigned char **buf, const unsigned char *end)
{
	size_t plen;
	struct quic_connection_close_app *cc = &frm->connection_close_app;

	if (!quic_dec_int(&cc->error_code, buf, end) ||
	    !quic_dec_int(&cc->reason_phrase_len, buf, end) ||
	    end - *buf < cc->reason_phrase_len)
		return 0;

	plen = QUIC_MIN((size_t)cc->reason_phrase_len, sizeof cc->reason_phrase);
	memcpy(cc->reason_phrase, *buf, plen);
	*buf += cc->reason_phrase_len;

	return 1;
}

/* Encode a HANDSHAKE_DONE frame into <buf> buffer.
 * Always succeeds.
 */
static int quic_build_handshake_done_frame(unsigned char **buf, const unsigned char *end,
                                           struct quic_frame *frm, struct quic_conn *conn)
{
	/* No field */
	return 1;
}

/* Parse a HANDSHAKE_DONE frame at QUIC layer from <buf> buffer with <end> as end into <frm> frame.
 * Always succeed.
 */
static int quic_parse_handshake_done_frame(struct quic_frame *frm, struct quic_conn *qc,
                                           const unsigned char **buf, const unsigned char *end)
{
	/* No field */
	return 1;
}

struct quic_frame_builder {
	int (*func)(unsigned char **buf, const unsigned char *end,
                 struct quic_frame *frm, struct quic_conn *conn);
	uint32_t mask;
	unsigned char flags;
};

const struct quic_frame_builder quic_frame_builders[] = {
	[QUIC_FT_PADDING]              = { .func = quic_build_padding_frame,              .flags = QUIC_FL_TX_PACKET_PADDING,       .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
	[QUIC_FT_PING]                 = { .func = quic_build_ping_frame,                 .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
	[QUIC_FT_ACK]                  = { .func = quic_build_ack_frame,                  .flags = 0,                               .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
	[QUIC_FT_ACK_ECN]              = { .func = quic_build_ack_ecn_frame,              .flags = 0,                               .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
	[QUIC_FT_RESET_STREAM]         = { .func = quic_build_reset_stream_frame,         .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STOP_SENDING]         = { .func = quic_build_stop_sending_frame,         .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_CRYPTO]               = { .func = quic_build_crypto_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
	[QUIC_FT_NEW_TOKEN]            = { .func = quic_build_new_token_frame,            .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
	[QUIC_FT_STREAM_8]             = { .func = quic_build_stream_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_9]             = { .func = quic_build_stream_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_A]             = { .func = quic_build_stream_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_B]             = { .func = quic_build_stream_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_C]             = { .func = quic_build_stream_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_D]             = { .func = quic_build_stream_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_E]             = { .func = quic_build_stream_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_F]             = { .func = quic_build_stream_frame,               .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_MAX_DATA]             = { .func = quic_build_max_data_frame,             .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_MAX_STREAM_DATA]      = { .func = quic_build_max_stream_data_frame,      .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_MAX_STREAMS_BIDI]     = { .func = quic_build_max_streams_bidi_frame,     .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_MAX_STREAMS_UNI]      = { .func = quic_build_max_streams_uni_frame,      .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_DATA_BLOCKED]         = { .func = quic_build_data_blocked_frame,         .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_DATA_BLOCKED]  = { .func = quic_build_stream_data_blocked_frame,  .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAMS_BLOCKED_BIDI] = { .func = quic_build_streams_blocked_bidi_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAMS_BLOCKED_UNI]  = { .func = quic_build_streams_blocked_uni_frame,  .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_NEW_CONNECTION_ID]    = { .func = quic_build_new_connection_id_frame,    .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_RETIRE_CONNECTION_ID] = { .func = quic_build_retire_connection_id_frame, .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_PATH_CHALLENGE]       = { .func = quic_build_path_challenge_frame,       .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_PATH_RESPONSE]        = { .func = quic_build_path_response_frame,        .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_CONNECTION_CLOSE]     = { .func = quic_build_connection_close_frame,     .flags = 0,                               .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
	[QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_build_connection_close_app_frame, .flags = 0,                               .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_HANDSHAKE_DONE]       = { .func = quic_build_handshake_done_frame,       .flags = QUIC_FL_TX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
};

struct quic_frame_parser {
	int (*func)(struct quic_frame *frm, struct quic_conn *qc,
                const unsigned char **buf, const unsigned char *end);
	uint32_t mask;
	unsigned char flags;
};

const struct quic_frame_parser quic_frame_parsers[] = {
	[QUIC_FT_PADDING]              = { .func = quic_parse_padding_frame,              .flags = 0,                               .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
	[QUIC_FT_PING]                 = { .func = quic_parse_ping_frame,                 .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
	[QUIC_FT_ACK]                  = { .func = quic_parse_ack_frame_header,           .flags = 0,                               .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
	[QUIC_FT_ACK_ECN]              = { .func = quic_parse_ack_ecn_frame,              .flags = 0,                               .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
	[QUIC_FT_RESET_STREAM]         = { .func = quic_parse_reset_stream_frame,         .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STOP_SENDING]         = { .func = quic_parse_stop_sending_frame,         .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_CRYPTO]               = { .func = quic_parse_crypto_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE_IH_1_BITMASK, },
	[QUIC_FT_NEW_TOKEN]            = { .func = quic_parse_new_token_frame,            .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
	[QUIC_FT_STREAM_8]             = { .func = quic_parse_stream_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_9]             = { .func = quic_parse_stream_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_A]             = { .func = quic_parse_stream_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_B]             = { .func = quic_parse_stream_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_C]             = { .func = quic_parse_stream_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_D]             = { .func = quic_parse_stream_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_E]             = { .func = quic_parse_stream_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_F]             = { .func = quic_parse_stream_frame,               .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_MAX_DATA]             = { .func = quic_parse_max_data_frame,             .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_MAX_STREAM_DATA]      = { .func = quic_parse_max_stream_data_frame,      .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_MAX_STREAMS_BIDI]     = { .func = quic_parse_max_streams_bidi_frame,     .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_MAX_STREAMS_UNI]      = { .func = quic_parse_max_streams_uni_frame,      .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_DATA_BLOCKED]         = { .func = quic_parse_data_blocked_frame,         .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAM_DATA_BLOCKED]  = { .func = quic_parse_stream_data_blocked_frame,  .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAMS_BLOCKED_BIDI] = { .func = quic_parse_streams_blocked_bidi_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_STREAMS_BLOCKED_UNI]  = { .func = quic_parse_streams_blocked_uni_frame,  .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_NEW_CONNECTION_ID]    = { .func = quic_parse_new_connection_id_frame,    .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_RETIRE_CONNECTION_ID] = { .func = quic_parse_retire_connection_id_frame, .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_PATH_CHALLENGE]       = { .func = quic_parse_path_challenge_frame,       .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_PATH_RESPONSE]        = { .func = quic_parse_path_response_frame,        .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_CONNECTION_CLOSE]     = { .func = quic_parse_connection_close_frame,     .flags = 0,                               .mask = QUIC_FT_PKT_TYPE_IH01_BITMASK, },
	[QUIC_FT_CONNECTION_CLOSE_APP] = { .func = quic_parse_connection_close_app_frame, .flags = 0,                               .mask = QUIC_FT_PKT_TYPE___01_BITMASK, },
	[QUIC_FT_HANDSHAKE_DONE]       = { .func = quic_parse_handshake_done_frame,       .flags = QUIC_FL_RX_PACKET_ACK_ELICITING, .mask = QUIC_FT_PKT_TYPE____1_BITMASK, },
};

/* Decode a QUIC frame from <buf> buffer into <frm> frame.
 * Returns 1 if succeeded (enough data to parse the frame), 0 if not.
 */
int qc_parse_frm(struct quic_frame *frm, struct quic_rx_packet *pkt,
                 const unsigned char **buf, const unsigned char *end,
                 struct quic_conn *qc)
{
	int ret = 0;
	const struct quic_frame_parser *parser;

	TRACE_ENTER(QUIC_EV_CONN_PRSFRM, qc);
	if (end <= *buf) {
		TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSFRM, qc);
		goto leave;
	}

	frm->type = *(*buf)++;
	if (frm->type >= QUIC_FT_MAX) {
		TRACE_DEVEL("wrong frame type", QUIC_EV_CONN_PRSFRM, qc, frm);
		goto leave;
	}

	parser = &quic_frame_parsers[frm->type];
	if (!(parser->mask & (1U << pkt->type))) {
		TRACE_DEVEL("unauthorized frame", QUIC_EV_CONN_PRSFRM, qc, frm);
		goto leave;
	}

	TRACE_PROTO("frame", QUIC_EV_CONN_PRSFRM, qc, frm);
	if (!parser->func(frm, qc, buf, end)) {
		TRACE_DEVEL("parsing error", QUIC_EV_CONN_PRSFRM, qc, frm);
		goto leave;
	}

	pkt->flags |= parser->flags;

	ret = 1;
 leave:
	TRACE_LEAVE(QUIC_EV_CONN_PRSFRM, qc);
	return ret;
}

/* Encode <frm> QUIC frame into <buf> buffer.
 * Returns 1 if succeeded (enough room in <buf> to encode the frame), 0 if not.
 * The buffer is updated to point to one byte past the end of the built frame
 * only if succeeded.
 */
int qc_build_frm(unsigned char **buf, const unsigned char *end,
                 struct quic_frame *frm, struct quic_tx_packet *pkt,
                 struct quic_conn *qc)
{
	int ret = 0;
	const struct quic_frame_builder *builder;
	unsigned char *pos = *buf;

	TRACE_ENTER(QUIC_EV_CONN_BFRM, qc);
	builder = &quic_frame_builders[frm->type];
	if (!(builder->mask & (1U << pkt->type))) {
		/* XXX This it a bug to send an unauthorized frame with such a packet type XXX */
		TRACE_ERROR("unauthorized frame", QUIC_EV_CONN_BFRM, qc, frm);
		BUG_ON(!(builder->mask & (1U << pkt->type)));
	}

	if (end <= pos) {
		TRACE_DEVEL("not enough room", QUIC_EV_CONN_BFRM, qc, frm);
		goto leave;
	}

	TRACE_PROTO("frame", QUIC_EV_CONN_BFRM, qc, frm);
	*pos++ = frm->type;
	if (!quic_frame_builders[frm->type].func(&pos, end, frm, qc)) {
		TRACE_DEVEL("frame building error", QUIC_EV_CONN_BFRM, qc, frm);
		goto leave;
	}

	pkt->flags |= builder->flags;
	*buf = pos;

	ret = 1;
 leave:
	TRACE_LEAVE(QUIC_EV_CONN_BFRM, qc);
	return ret;
}