lsquic_parse_gquic_common.c revision 50aadb33
1/* Copyright (c) 2017 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_parse_gquic_common.c -- Parsing functions common to GQUIC
4 */
5
6#include <assert.h>
7#include <errno.h>
8#include <inttypes.h>
9#include <string.h>
10#include <stdlib.h>
11#include <sys/types.h>
12
13#include "lsquic_types.h"
14#include "lsquic_packet_common.h"
15#include "lsquic_packet_in.h"
16#include "lsquic_parse.h"
17#include "lsquic.h"
18
19#define LSQUIC_LOGGER_MODULE LSQLM_PARSE
20#include "lsquic_logger.h"
21
22#define CHECK_SPACE(need, pstart, pend)  \
23    do { if ((intptr_t) (need) > ((pend) - (pstart))) { return -1; } } while (0)
24
25/* This partially parses `packet_in' and returns 0 if in case it succeeded and
26 * -1 on failure.
27 *
28 * After this function returns 0, connection ID, nonce, and version fields can
29 * be examined.  To finsh parsing the packet, call version-specific
30 * pf_parse_packet_in_finish() routine.
31 */
32int
33parse_packet_in_begin (lsquic_packet_in_t *packet_in, size_t length,
34                            int is_server, struct packin_parse_state *state)
35{
36    int nbytes;
37    enum PACKET_PUBLIC_FLAGS public_flags;
38    const unsigned char *p = packet_in->pi_data;
39    const unsigned char *const pend = packet_in->pi_data + length;
40
41    CHECK_SPACE(1, p, pend);
42
43    public_flags = *p++;
44
45    if (public_flags & PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID)
46    {
47        CHECK_SPACE(8, p, pend);
48        memcpy(&packet_in->pi_conn_id, p, 8);
49        packet_in->pi_flags |= PI_CONN_ID;
50        p += 8;
51    }
52
53    if (public_flags & PACKET_PUBLIC_FLAGS_VERSION)
54    {
55        /* It seems that version negotiation packets sent by Google may have
56         * NONCE bit set.  Ignore it:
57         */
58        public_flags &= ~PACKET_PUBLIC_FLAGS_NONCE;
59
60        if (is_server)
61        {
62            CHECK_SPACE(4, p, pend);
63            packet_in->pi_quic_ver = p - packet_in->pi_data;
64            p += 4;
65        }
66        else
67        {   /* OK, we have a version negotiation packet.  We need to verify
68             * that it has correct structure.  See Section 4.3 of
69             * [draft-ietf-quic-transport-00].
70             */
71            if ((public_flags & ~(PACKET_PUBLIC_FLAGS_VERSION|
72                                  PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID))
73                || ((pend - p) & 3))
74                return -1;
75            CHECK_SPACE(4, p, pend);
76            packet_in->pi_quic_ver = p - packet_in->pi_data;
77            p = pend;
78        }
79    }
80    else
81    {
82        /* From [draft-hamilton-quic-transport-protocol-01]:
83         *    0x40 = MULTIPATH. This bit is reserved for multipath use.
84         *    0x80 is currently unused, and must be set to 0.
85         *
86         * The reference implementation checks that two high bits are not set
87         * if version flag is not set or if the version is the same.  For our
88         * purposes, all GQUIC version we support so far have these bits set
89         * to zero.
90         */
91        if (public_flags & (0x80|0x40))
92            return -1;
93        packet_in->pi_quic_ver = 0;
94    }
95
96    if (!is_server && (public_flags & PACKET_PUBLIC_FLAGS_NONCE) ==
97                                            PACKET_PUBLIC_FLAGS_NONCE)
98    {
99        CHECK_SPACE(32, p, pend);
100        packet_in->pi_nonce = p - packet_in->pi_data;
101        p += 32;
102    }
103    else
104        packet_in->pi_nonce = 0;
105
106    state->pps_p = p;
107
108    packet_in->pi_packno = 0;
109    if (0 == (public_flags & (PACKET_PUBLIC_FLAGS_VERSION|PACKET_PUBLIC_FLAGS_RST))
110        || ((public_flags & PACKET_PUBLIC_FLAGS_VERSION) && is_server))
111    {
112        nbytes = twobit_to_1246((public_flags >> 4) & 3);
113        CHECK_SPACE(nbytes, p, pend);
114        p += nbytes;
115        state->pps_nbytes = nbytes;
116    }
117    else
118        state->pps_nbytes = 0;
119
120    packet_in->pi_header_sz    = p - packet_in->pi_data;
121    packet_in->pi_frame_types  = 0;
122    memset(&packet_in->pi_next, 0, sizeof(packet_in->pi_next));
123    packet_in->pi_data_sz      = length;
124    packet_in->pi_refcnt       = 0;
125    packet_in->pi_received     = 0;
126
127    return 0;
128}
129
130
131static const enum QUIC_FRAME_TYPE byte2frame_type_Q035_thru_Q039[0x100] =
132{
133    [0x00] = QUIC_FRAME_PADDING,
134    [0x01] = QUIC_FRAME_RST_STREAM,
135    [0x02] = QUIC_FRAME_CONNECTION_CLOSE,
136    [0x03] = QUIC_FRAME_GOAWAY,
137    [0x04] = QUIC_FRAME_WINDOW_UPDATE,
138    [0x05] = QUIC_FRAME_BLOCKED,
139    [0x06] = QUIC_FRAME_STOP_WAITING,
140    [0x07] = QUIC_FRAME_PING,
141    [0x08] = QUIC_FRAME_INVALID,
142    [0x09] = QUIC_FRAME_INVALID,
143    [0x0A] = QUIC_FRAME_INVALID,
144    [0x0B] = QUIC_FRAME_INVALID,
145    [0x0C] = QUIC_FRAME_INVALID,
146    [0x0D] = QUIC_FRAME_INVALID,
147    [0x0E] = QUIC_FRAME_INVALID,
148    [0x0F] = QUIC_FRAME_INVALID,
149    [0x10] = QUIC_FRAME_INVALID,
150    [0x11] = QUIC_FRAME_INVALID,
151    [0x12] = QUIC_FRAME_INVALID,
152    [0x13] = QUIC_FRAME_INVALID,
153    [0x14] = QUIC_FRAME_INVALID,
154    [0x15] = QUIC_FRAME_INVALID,
155    [0x16] = QUIC_FRAME_INVALID,
156    [0x17] = QUIC_FRAME_INVALID,
157    [0x18] = QUIC_FRAME_INVALID,
158    [0x19] = QUIC_FRAME_INVALID,
159    [0x1A] = QUIC_FRAME_INVALID,
160    [0x1B] = QUIC_FRAME_INVALID,
161    [0x1C] = QUIC_FRAME_INVALID,
162    [0x1D] = QUIC_FRAME_INVALID,
163    [0x1E] = QUIC_FRAME_INVALID,
164    [0x1F] = QUIC_FRAME_INVALID,
165    [0x20] = QUIC_FRAME_INVALID,
166    [0x21] = QUIC_FRAME_INVALID,
167    [0x22] = QUIC_FRAME_INVALID,
168    [0x23] = QUIC_FRAME_INVALID,
169    [0x24] = QUIC_FRAME_INVALID,
170    [0x25] = QUIC_FRAME_INVALID,
171    [0x26] = QUIC_FRAME_INVALID,
172    [0x27] = QUIC_FRAME_INVALID,
173    [0x28] = QUIC_FRAME_INVALID,
174    [0x29] = QUIC_FRAME_INVALID,
175    [0x2A] = QUIC_FRAME_INVALID,
176    [0x2B] = QUIC_FRAME_INVALID,
177    [0x2C] = QUIC_FRAME_INVALID,
178    [0x2D] = QUIC_FRAME_INVALID,
179    [0x2E] = QUIC_FRAME_INVALID,
180    [0x2F] = QUIC_FRAME_INVALID,
181    [0x30] = QUIC_FRAME_INVALID,
182    [0x31] = QUIC_FRAME_INVALID,
183    [0x32] = QUIC_FRAME_INVALID,
184    [0x33] = QUIC_FRAME_INVALID,
185    [0x34] = QUIC_FRAME_INVALID,
186    [0x35] = QUIC_FRAME_INVALID,
187    [0x36] = QUIC_FRAME_INVALID,
188    [0x37] = QUIC_FRAME_INVALID,
189    [0x38] = QUIC_FRAME_INVALID,
190    [0x39] = QUIC_FRAME_INVALID,
191    [0x3A] = QUIC_FRAME_INVALID,
192    [0x3B] = QUIC_FRAME_INVALID,
193    [0x3C] = QUIC_FRAME_INVALID,
194    [0x3D] = QUIC_FRAME_INVALID,
195    [0x3E] = QUIC_FRAME_INVALID,
196    [0x3F] = QUIC_FRAME_INVALID,
197    [0x40] = QUIC_FRAME_ACK,
198    [0x41] = QUIC_FRAME_ACK,
199    [0x42] = QUIC_FRAME_ACK,
200    [0x43] = QUIC_FRAME_ACK,
201    [0x44] = QUIC_FRAME_ACK,
202    [0x45] = QUIC_FRAME_ACK,
203    [0x46] = QUIC_FRAME_ACK,
204    [0x47] = QUIC_FRAME_ACK,
205    [0x48] = QUIC_FRAME_ACK,
206    [0x49] = QUIC_FRAME_ACK,
207    [0x4A] = QUIC_FRAME_ACK,
208    [0x4B] = QUIC_FRAME_ACK,
209    [0x4C] = QUIC_FRAME_ACK,
210    [0x4D] = QUIC_FRAME_ACK,
211    [0x4E] = QUIC_FRAME_ACK,
212    [0x4F] = QUIC_FRAME_ACK,
213    [0x50] = QUIC_FRAME_ACK,
214    [0x51] = QUIC_FRAME_ACK,
215    [0x52] = QUIC_FRAME_ACK,
216    [0x53] = QUIC_FRAME_ACK,
217    [0x54] = QUIC_FRAME_ACK,
218    [0x55] = QUIC_FRAME_ACK,
219    [0x56] = QUIC_FRAME_ACK,
220    [0x57] = QUIC_FRAME_ACK,
221    [0x58] = QUIC_FRAME_ACK,
222    [0x59] = QUIC_FRAME_ACK,
223    [0x5A] = QUIC_FRAME_ACK,
224    [0x5B] = QUIC_FRAME_ACK,
225    [0x5C] = QUIC_FRAME_ACK,
226    [0x5D] = QUIC_FRAME_ACK,
227    [0x5E] = QUIC_FRAME_ACK,
228    [0x5F] = QUIC_FRAME_ACK,
229    [0x60] = QUIC_FRAME_ACK,
230    [0x61] = QUIC_FRAME_ACK,
231    [0x62] = QUIC_FRAME_ACK,
232    [0x63] = QUIC_FRAME_ACK,
233    [0x64] = QUIC_FRAME_ACK,
234    [0x65] = QUIC_FRAME_ACK,
235    [0x66] = QUIC_FRAME_ACK,
236    [0x67] = QUIC_FRAME_ACK,
237    [0x68] = QUIC_FRAME_ACK,
238    [0x69] = QUIC_FRAME_ACK,
239    [0x6A] = QUIC_FRAME_ACK,
240    [0x6B] = QUIC_FRAME_ACK,
241    [0x6C] = QUIC_FRAME_ACK,
242    [0x6D] = QUIC_FRAME_ACK,
243    [0x6E] = QUIC_FRAME_ACK,
244    [0x6F] = QUIC_FRAME_ACK,
245    [0x70] = QUIC_FRAME_ACK,
246    [0x71] = QUIC_FRAME_ACK,
247    [0x72] = QUIC_FRAME_ACK,
248    [0x73] = QUIC_FRAME_ACK,
249    [0x74] = QUIC_FRAME_ACK,
250    [0x75] = QUIC_FRAME_ACK,
251    [0x76] = QUIC_FRAME_ACK,
252    [0x77] = QUIC_FRAME_ACK,
253    [0x78] = QUIC_FRAME_ACK,
254    [0x79] = QUIC_FRAME_ACK,
255    [0x7A] = QUIC_FRAME_ACK,
256    [0x7B] = QUIC_FRAME_ACK,
257    [0x7C] = QUIC_FRAME_ACK,
258    [0x7D] = QUIC_FRAME_ACK,
259    [0x7E] = QUIC_FRAME_ACK,
260    [0x7F] = QUIC_FRAME_ACK,
261    [0x80] = QUIC_FRAME_STREAM,
262    [0x81] = QUIC_FRAME_STREAM,
263    [0x82] = QUIC_FRAME_STREAM,
264    [0x83] = QUIC_FRAME_STREAM,
265    [0x84] = QUIC_FRAME_STREAM,
266    [0x85] = QUIC_FRAME_STREAM,
267    [0x86] = QUIC_FRAME_STREAM,
268    [0x87] = QUIC_FRAME_STREAM,
269    [0x88] = QUIC_FRAME_STREAM,
270    [0x89] = QUIC_FRAME_STREAM,
271    [0x8A] = QUIC_FRAME_STREAM,
272    [0x8B] = QUIC_FRAME_STREAM,
273    [0x8C] = QUIC_FRAME_STREAM,
274    [0x8D] = QUIC_FRAME_STREAM,
275    [0x8E] = QUIC_FRAME_STREAM,
276    [0x8F] = QUIC_FRAME_STREAM,
277    [0x90] = QUIC_FRAME_STREAM,
278    [0x91] = QUIC_FRAME_STREAM,
279    [0x92] = QUIC_FRAME_STREAM,
280    [0x93] = QUIC_FRAME_STREAM,
281    [0x94] = QUIC_FRAME_STREAM,
282    [0x95] = QUIC_FRAME_STREAM,
283    [0x96] = QUIC_FRAME_STREAM,
284    [0x97] = QUIC_FRAME_STREAM,
285    [0x98] = QUIC_FRAME_STREAM,
286    [0x99] = QUIC_FRAME_STREAM,
287    [0x9A] = QUIC_FRAME_STREAM,
288    [0x9B] = QUIC_FRAME_STREAM,
289    [0x9C] = QUIC_FRAME_STREAM,
290    [0x9D] = QUIC_FRAME_STREAM,
291    [0x9E] = QUIC_FRAME_STREAM,
292    [0x9F] = QUIC_FRAME_STREAM,
293    [0xA0] = QUIC_FRAME_STREAM,
294    [0xA1] = QUIC_FRAME_STREAM,
295    [0xA2] = QUIC_FRAME_STREAM,
296    [0xA3] = QUIC_FRAME_STREAM,
297    [0xA4] = QUIC_FRAME_STREAM,
298    [0xA5] = QUIC_FRAME_STREAM,
299    [0xA6] = QUIC_FRAME_STREAM,
300    [0xA7] = QUIC_FRAME_STREAM,
301    [0xA8] = QUIC_FRAME_STREAM,
302    [0xA9] = QUIC_FRAME_STREAM,
303    [0xAA] = QUIC_FRAME_STREAM,
304    [0xAB] = QUIC_FRAME_STREAM,
305    [0xAC] = QUIC_FRAME_STREAM,
306    [0xAD] = QUIC_FRAME_STREAM,
307    [0xAE] = QUIC_FRAME_STREAM,
308    [0xAF] = QUIC_FRAME_STREAM,
309    [0xB0] = QUIC_FRAME_STREAM,
310    [0xB1] = QUIC_FRAME_STREAM,
311    [0xB2] = QUIC_FRAME_STREAM,
312    [0xB3] = QUIC_FRAME_STREAM,
313    [0xB4] = QUIC_FRAME_STREAM,
314    [0xB5] = QUIC_FRAME_STREAM,
315    [0xB6] = QUIC_FRAME_STREAM,
316    [0xB7] = QUIC_FRAME_STREAM,
317    [0xB8] = QUIC_FRAME_STREAM,
318    [0xB9] = QUIC_FRAME_STREAM,
319    [0xBA] = QUIC_FRAME_STREAM,
320    [0xBB] = QUIC_FRAME_STREAM,
321    [0xBC] = QUIC_FRAME_STREAM,
322    [0xBD] = QUIC_FRAME_STREAM,
323    [0xBE] = QUIC_FRAME_STREAM,
324    [0xBF] = QUIC_FRAME_STREAM,
325    [0xC0] = QUIC_FRAME_STREAM,
326    [0xC1] = QUIC_FRAME_STREAM,
327    [0xC2] = QUIC_FRAME_STREAM,
328    [0xC3] = QUIC_FRAME_STREAM,
329    [0xC4] = QUIC_FRAME_STREAM,
330    [0xC5] = QUIC_FRAME_STREAM,
331    [0xC6] = QUIC_FRAME_STREAM,
332    [0xC7] = QUIC_FRAME_STREAM,
333    [0xC8] = QUIC_FRAME_STREAM,
334    [0xC9] = QUIC_FRAME_STREAM,
335    [0xCA] = QUIC_FRAME_STREAM,
336    [0xCB] = QUIC_FRAME_STREAM,
337    [0xCC] = QUIC_FRAME_STREAM,
338    [0xCD] = QUIC_FRAME_STREAM,
339    [0xCE] = QUIC_FRAME_STREAM,
340    [0xCF] = QUIC_FRAME_STREAM,
341    [0xD0] = QUIC_FRAME_STREAM,
342    [0xD1] = QUIC_FRAME_STREAM,
343    [0xD2] = QUIC_FRAME_STREAM,
344    [0xD3] = QUIC_FRAME_STREAM,
345    [0xD4] = QUIC_FRAME_STREAM,
346    [0xD5] = QUIC_FRAME_STREAM,
347    [0xD6] = QUIC_FRAME_STREAM,
348    [0xD7] = QUIC_FRAME_STREAM,
349    [0xD8] = QUIC_FRAME_STREAM,
350    [0xD9] = QUIC_FRAME_STREAM,
351    [0xDA] = QUIC_FRAME_STREAM,
352    [0xDB] = QUIC_FRAME_STREAM,
353    [0xDC] = QUIC_FRAME_STREAM,
354    [0xDD] = QUIC_FRAME_STREAM,
355    [0xDE] = QUIC_FRAME_STREAM,
356    [0xDF] = QUIC_FRAME_STREAM,
357    [0xE0] = QUIC_FRAME_STREAM,
358    [0xE1] = QUIC_FRAME_STREAM,
359    [0xE2] = QUIC_FRAME_STREAM,
360    [0xE3] = QUIC_FRAME_STREAM,
361    [0xE4] = QUIC_FRAME_STREAM,
362    [0xE5] = QUIC_FRAME_STREAM,
363    [0xE6] = QUIC_FRAME_STREAM,
364    [0xE7] = QUIC_FRAME_STREAM,
365    [0xE8] = QUIC_FRAME_STREAM,
366    [0xE9] = QUIC_FRAME_STREAM,
367    [0xEA] = QUIC_FRAME_STREAM,
368    [0xEB] = QUIC_FRAME_STREAM,
369    [0xEC] = QUIC_FRAME_STREAM,
370    [0xED] = QUIC_FRAME_STREAM,
371    [0xEE] = QUIC_FRAME_STREAM,
372    [0xEF] = QUIC_FRAME_STREAM,
373    [0xF0] = QUIC_FRAME_STREAM,
374    [0xF1] = QUIC_FRAME_STREAM,
375    [0xF2] = QUIC_FRAME_STREAM,
376    [0xF3] = QUIC_FRAME_STREAM,
377    [0xF4] = QUIC_FRAME_STREAM,
378    [0xF5] = QUIC_FRAME_STREAM,
379    [0xF6] = QUIC_FRAME_STREAM,
380    [0xF7] = QUIC_FRAME_STREAM,
381    [0xF8] = QUIC_FRAME_STREAM,
382    [0xF9] = QUIC_FRAME_STREAM,
383    [0xFA] = QUIC_FRAME_STREAM,
384    [0xFB] = QUIC_FRAME_STREAM,
385    [0xFC] = QUIC_FRAME_STREAM,
386    [0xFD] = QUIC_FRAME_STREAM,
387    [0xFE] = QUIC_FRAME_STREAM,
388    [0xFF] = QUIC_FRAME_STREAM,
389};
390
391
392static const enum QUIC_FRAME_TYPE byte2frame_type_Q040[0x100] =
393{
394    [0x00] = QUIC_FRAME_PADDING,
395    [0x01] = QUIC_FRAME_RST_STREAM,
396    [0x02] = QUIC_FRAME_CONNECTION_CLOSE,
397    [0x03] = QUIC_FRAME_GOAWAY,
398    [0x04] = QUIC_FRAME_WINDOW_UPDATE,
399    [0x05] = QUIC_FRAME_BLOCKED,
400    [0x06] = QUIC_FRAME_STOP_WAITING,
401    [0x07] = QUIC_FRAME_PING,
402    [0x08] = QUIC_FRAME_INVALID,
403    [0x09] = QUIC_FRAME_INVALID,
404    [0x0A] = QUIC_FRAME_INVALID,
405    [0x0B] = QUIC_FRAME_INVALID,
406    [0x0C] = QUIC_FRAME_INVALID,
407    [0x0D] = QUIC_FRAME_INVALID,
408    [0x0E] = QUIC_FRAME_INVALID,
409    [0x0F] = QUIC_FRAME_INVALID,
410    [0x10] = QUIC_FRAME_INVALID,
411    [0x11] = QUIC_FRAME_INVALID,
412    [0x12] = QUIC_FRAME_INVALID,
413    [0x13] = QUIC_FRAME_INVALID,
414    [0x14] = QUIC_FRAME_INVALID,
415    [0x15] = QUIC_FRAME_INVALID,
416    [0x16] = QUIC_FRAME_INVALID,
417    [0x17] = QUIC_FRAME_INVALID,
418    [0x18] = QUIC_FRAME_INVALID,
419    [0x19] = QUIC_FRAME_INVALID,
420    [0x1A] = QUIC_FRAME_INVALID,
421    [0x1B] = QUIC_FRAME_INVALID,
422    [0x1C] = QUIC_FRAME_INVALID,
423    [0x1D] = QUIC_FRAME_INVALID,
424    [0x1E] = QUIC_FRAME_INVALID,
425    [0x1F] = QUIC_FRAME_INVALID,
426    [0x20] = QUIC_FRAME_INVALID,
427    [0x21] = QUIC_FRAME_INVALID,
428    [0x22] = QUIC_FRAME_INVALID,
429    [0x23] = QUIC_FRAME_INVALID,
430    [0x24] = QUIC_FRAME_INVALID,
431    [0x25] = QUIC_FRAME_INVALID,
432    [0x26] = QUIC_FRAME_INVALID,
433    [0x27] = QUIC_FRAME_INVALID,
434    [0x28] = QUIC_FRAME_INVALID,
435    [0x29] = QUIC_FRAME_INVALID,
436    [0x2A] = QUIC_FRAME_INVALID,
437    [0x2B] = QUIC_FRAME_INVALID,
438    [0x2C] = QUIC_FRAME_INVALID,
439    [0x2D] = QUIC_FRAME_INVALID,
440    [0x2E] = QUIC_FRAME_INVALID,
441    [0x2F] = QUIC_FRAME_INVALID,
442    [0x30] = QUIC_FRAME_INVALID,
443    [0x31] = QUIC_FRAME_INVALID,
444    [0x32] = QUIC_FRAME_INVALID,
445    [0x33] = QUIC_FRAME_INVALID,
446    [0x34] = QUIC_FRAME_INVALID,
447    [0x35] = QUIC_FRAME_INVALID,
448    [0x36] = QUIC_FRAME_INVALID,
449    [0x37] = QUIC_FRAME_INVALID,
450    [0x38] = QUIC_FRAME_INVALID,
451    [0x39] = QUIC_FRAME_INVALID,
452    [0x3A] = QUIC_FRAME_INVALID,
453    [0x3B] = QUIC_FRAME_INVALID,
454    [0x3C] = QUIC_FRAME_INVALID,
455    [0x3D] = QUIC_FRAME_INVALID,
456    [0x3E] = QUIC_FRAME_INVALID,
457    [0x3F] = QUIC_FRAME_INVALID,
458    [0x40] = QUIC_FRAME_INVALID,
459    [0x41] = QUIC_FRAME_INVALID,
460    [0x42] = QUIC_FRAME_INVALID,
461    [0x43] = QUIC_FRAME_INVALID,
462    [0x44] = QUIC_FRAME_INVALID,
463    [0x45] = QUIC_FRAME_INVALID,
464    [0x46] = QUIC_FRAME_INVALID,
465    [0x47] = QUIC_FRAME_INVALID,
466    [0x48] = QUIC_FRAME_INVALID,
467    [0x49] = QUIC_FRAME_INVALID,
468    [0x4A] = QUIC_FRAME_INVALID,
469    [0x4B] = QUIC_FRAME_INVALID,
470    [0x4C] = QUIC_FRAME_INVALID,
471    [0x4D] = QUIC_FRAME_INVALID,
472    [0x4E] = QUIC_FRAME_INVALID,
473    [0x4F] = QUIC_FRAME_INVALID,
474    [0x50] = QUIC_FRAME_INVALID,
475    [0x51] = QUIC_FRAME_INVALID,
476    [0x52] = QUIC_FRAME_INVALID,
477    [0x53] = QUIC_FRAME_INVALID,
478    [0x54] = QUIC_FRAME_INVALID,
479    [0x55] = QUIC_FRAME_INVALID,
480    [0x56] = QUIC_FRAME_INVALID,
481    [0x57] = QUIC_FRAME_INVALID,
482    [0x58] = QUIC_FRAME_INVALID,
483    [0x59] = QUIC_FRAME_INVALID,
484    [0x5A] = QUIC_FRAME_INVALID,
485    [0x5B] = QUIC_FRAME_INVALID,
486    [0x5C] = QUIC_FRAME_INVALID,
487    [0x5D] = QUIC_FRAME_INVALID,
488    [0x5E] = QUIC_FRAME_INVALID,
489    [0x5F] = QUIC_FRAME_INVALID,
490    [0x60] = QUIC_FRAME_INVALID,
491    [0x61] = QUIC_FRAME_INVALID,
492    [0x62] = QUIC_FRAME_INVALID,
493    [0x63] = QUIC_FRAME_INVALID,
494    [0x64] = QUIC_FRAME_INVALID,
495    [0x65] = QUIC_FRAME_INVALID,
496    [0x66] = QUIC_FRAME_INVALID,
497    [0x67] = QUIC_FRAME_INVALID,
498    [0x68] = QUIC_FRAME_INVALID,
499    [0x69] = QUIC_FRAME_INVALID,
500    [0x6A] = QUIC_FRAME_INVALID,
501    [0x6B] = QUIC_FRAME_INVALID,
502    [0x6C] = QUIC_FRAME_INVALID,
503    [0x6D] = QUIC_FRAME_INVALID,
504    [0x6E] = QUIC_FRAME_INVALID,
505    [0x6F] = QUIC_FRAME_INVALID,
506    [0x70] = QUIC_FRAME_INVALID,
507    [0x71] = QUIC_FRAME_INVALID,
508    [0x72] = QUIC_FRAME_INVALID,
509    [0x73] = QUIC_FRAME_INVALID,
510    [0x74] = QUIC_FRAME_INVALID,
511    [0x75] = QUIC_FRAME_INVALID,
512    [0x76] = QUIC_FRAME_INVALID,
513    [0x77] = QUIC_FRAME_INVALID,
514    [0x78] = QUIC_FRAME_INVALID,
515    [0x79] = QUIC_FRAME_INVALID,
516    [0x7A] = QUIC_FRAME_INVALID,
517    [0x7B] = QUIC_FRAME_INVALID,
518    [0x7C] = QUIC_FRAME_INVALID,
519    [0x7D] = QUIC_FRAME_INVALID,
520    [0x7E] = QUIC_FRAME_INVALID,
521    [0x7F] = QUIC_FRAME_INVALID,
522    [0x80] = QUIC_FRAME_INVALID,
523    [0x81] = QUIC_FRAME_INVALID,
524    [0x82] = QUIC_FRAME_INVALID,
525    [0x83] = QUIC_FRAME_INVALID,
526    [0x84] = QUIC_FRAME_INVALID,
527    [0x85] = QUIC_FRAME_INVALID,
528    [0x86] = QUIC_FRAME_INVALID,
529    [0x87] = QUIC_FRAME_INVALID,
530    [0x88] = QUIC_FRAME_INVALID,
531    [0x89] = QUIC_FRAME_INVALID,
532    [0x8A] = QUIC_FRAME_INVALID,
533    [0x8B] = QUIC_FRAME_INVALID,
534    [0x8C] = QUIC_FRAME_INVALID,
535    [0x8D] = QUIC_FRAME_INVALID,
536    [0x8E] = QUIC_FRAME_INVALID,
537    [0x8F] = QUIC_FRAME_INVALID,
538    [0x90] = QUIC_FRAME_INVALID,
539    [0x91] = QUIC_FRAME_INVALID,
540    [0x92] = QUIC_FRAME_INVALID,
541    [0x93] = QUIC_FRAME_INVALID,
542    [0x94] = QUIC_FRAME_INVALID,
543    [0x95] = QUIC_FRAME_INVALID,
544    [0x96] = QUIC_FRAME_INVALID,
545    [0x97] = QUIC_FRAME_INVALID,
546    [0x98] = QUIC_FRAME_INVALID,
547    [0x99] = QUIC_FRAME_INVALID,
548    [0x9A] = QUIC_FRAME_INVALID,
549    [0x9B] = QUIC_FRAME_INVALID,
550    [0x9C] = QUIC_FRAME_INVALID,
551    [0x9D] = QUIC_FRAME_INVALID,
552    [0x9E] = QUIC_FRAME_INVALID,
553    [0x9F] = QUIC_FRAME_INVALID,
554    [0xA0] = QUIC_FRAME_ACK,
555    [0xA1] = QUIC_FRAME_ACK,
556    [0xA2] = QUIC_FRAME_ACK,
557    [0xA3] = QUIC_FRAME_ACK,
558    [0xA4] = QUIC_FRAME_ACK,
559    [0xA5] = QUIC_FRAME_ACK,
560    [0xA6] = QUIC_FRAME_ACK,
561    [0xA7] = QUIC_FRAME_ACK,
562    [0xA8] = QUIC_FRAME_ACK,
563    [0xA9] = QUIC_FRAME_ACK,
564    [0xAA] = QUIC_FRAME_ACK,
565    [0xAB] = QUIC_FRAME_ACK,
566    [0xAC] = QUIC_FRAME_ACK,
567    [0xAD] = QUIC_FRAME_ACK,
568    [0xAE] = QUIC_FRAME_ACK,
569    [0xAF] = QUIC_FRAME_ACK,
570    [0xB0] = QUIC_FRAME_ACK,
571    [0xB1] = QUIC_FRAME_ACK,
572    [0xB2] = QUIC_FRAME_ACK,
573    [0xB3] = QUIC_FRAME_ACK,
574    [0xB4] = QUIC_FRAME_ACK,
575    [0xB5] = QUIC_FRAME_ACK,
576    [0xB6] = QUIC_FRAME_ACK,
577    [0xB7] = QUIC_FRAME_ACK,
578    [0xB8] = QUIC_FRAME_ACK,
579    [0xB9] = QUIC_FRAME_ACK,
580    [0xBA] = QUIC_FRAME_ACK,
581    [0xBB] = QUIC_FRAME_ACK,
582    [0xBC] = QUIC_FRAME_ACK,
583    [0xBD] = QUIC_FRAME_ACK,
584    [0xBE] = QUIC_FRAME_ACK,
585    [0xBF] = QUIC_FRAME_ACK,
586    [0xC0] = QUIC_FRAME_STREAM,
587    [0xC1] = QUIC_FRAME_STREAM,
588    [0xC2] = QUIC_FRAME_STREAM,
589    [0xC3] = QUIC_FRAME_STREAM,
590    [0xC4] = QUIC_FRAME_STREAM,
591    [0xC5] = QUIC_FRAME_STREAM,
592    [0xC6] = QUIC_FRAME_STREAM,
593    [0xC7] = QUIC_FRAME_STREAM,
594    [0xC8] = QUIC_FRAME_STREAM,
595    [0xC9] = QUIC_FRAME_STREAM,
596    [0xCA] = QUIC_FRAME_STREAM,
597    [0xCB] = QUIC_FRAME_STREAM,
598    [0xCC] = QUIC_FRAME_STREAM,
599    [0xCD] = QUIC_FRAME_STREAM,
600    [0xCE] = QUIC_FRAME_STREAM,
601    [0xCF] = QUIC_FRAME_STREAM,
602    [0xD0] = QUIC_FRAME_STREAM,
603    [0xD1] = QUIC_FRAME_STREAM,
604    [0xD2] = QUIC_FRAME_STREAM,
605    [0xD3] = QUIC_FRAME_STREAM,
606    [0xD4] = QUIC_FRAME_STREAM,
607    [0xD5] = QUIC_FRAME_STREAM,
608    [0xD6] = QUIC_FRAME_STREAM,
609    [0xD7] = QUIC_FRAME_STREAM,
610    [0xD8] = QUIC_FRAME_STREAM,
611    [0xD9] = QUIC_FRAME_STREAM,
612    [0xDA] = QUIC_FRAME_STREAM,
613    [0xDB] = QUIC_FRAME_STREAM,
614    [0xDC] = QUIC_FRAME_STREAM,
615    [0xDD] = QUIC_FRAME_STREAM,
616    [0xDE] = QUIC_FRAME_STREAM,
617    [0xDF] = QUIC_FRAME_STREAM,
618    [0xE0] = QUIC_FRAME_STREAM,
619    [0xE1] = QUIC_FRAME_STREAM,
620    [0xE2] = QUIC_FRAME_STREAM,
621    [0xE3] = QUIC_FRAME_STREAM,
622    [0xE4] = QUIC_FRAME_STREAM,
623    [0xE5] = QUIC_FRAME_STREAM,
624    [0xE6] = QUIC_FRAME_STREAM,
625    [0xE7] = QUIC_FRAME_STREAM,
626    [0xE8] = QUIC_FRAME_STREAM,
627    [0xE9] = QUIC_FRAME_STREAM,
628    [0xEA] = QUIC_FRAME_STREAM,
629    [0xEB] = QUIC_FRAME_STREAM,
630    [0xEC] = QUIC_FRAME_STREAM,
631    [0xED] = QUIC_FRAME_STREAM,
632    [0xEE] = QUIC_FRAME_STREAM,
633    [0xEF] = QUIC_FRAME_STREAM,
634    [0xF0] = QUIC_FRAME_STREAM,
635    [0xF1] = QUIC_FRAME_STREAM,
636    [0xF2] = QUIC_FRAME_STREAM,
637    [0xF3] = QUIC_FRAME_STREAM,
638    [0xF4] = QUIC_FRAME_STREAM,
639    [0xF5] = QUIC_FRAME_STREAM,
640    [0xF6] = QUIC_FRAME_STREAM,
641    [0xF7] = QUIC_FRAME_STREAM,
642    [0xF8] = QUIC_FRAME_STREAM,
643    [0xF9] = QUIC_FRAME_STREAM,
644    [0xFA] = QUIC_FRAME_STREAM,
645    [0xFB] = QUIC_FRAME_STREAM,
646    [0xFC] = QUIC_FRAME_STREAM,
647    [0xFD] = QUIC_FRAME_STREAM,
648    [0xFE] = QUIC_FRAME_STREAM,
649    [0xFF] = QUIC_FRAME_STREAM,
650};
651
652
653enum QUIC_FRAME_TYPE
654parse_frame_type_gquic_Q035_thru_Q039 (unsigned char b)
655{
656    return byte2frame_type_Q035_thru_Q039[b];
657}
658
659
660enum QUIC_FRAME_TYPE
661parse_frame_type_gquic_Q040 (unsigned char b)
662{
663    return byte2frame_type_Q040[b];
664}
665
666
667unsigned
668parse_stream_frame_header_sz_gquic (unsigned char type)
669{
670    const unsigned data_len      = (type >> 4) & 2;
671    const unsigned offset_len    = ((type >> 2) & 7) + 1 - !((type >> 2) & 7);
672    const unsigned stream_id_len = 1 + (type & 3);
673    return 1 + data_len + offset_len + stream_id_len;
674}
675
676
677size_t
678calc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset)
679{
680    return
681        /* Type */
682        1
683        /* Stream ID length */
684      + ((stream_id) > 0x0000FF)
685      + ((stream_id) > 0x00FFFF)
686      + ((stream_id) > 0xFFFFFF)
687      + 1
688        /* Offset length */
689      + ((offset) >= (1ULL << 56))
690      + ((offset) >= (1ULL << 48))
691      + ((offset) >= (1ULL << 40))
692      + ((offset) >= (1ULL << 32))
693      + ((offset) >= (1ULL << 24))
694      + ((offset) >= (1ULL << 16))
695      + (((offset) > 0) << 1)
696        /* Add data length (2) yourself, if necessary */
697    ;
698}
699
700
701char *
702acki2str (const struct ack_info *acki, size_t *sz)
703{
704    size_t off, bufsz, nw;
705    unsigned n;
706    char *buf;
707
708    bufsz = acki->n_ranges * (3 /* [-] */ + 20 /* ~0ULL */ * 2);
709    buf = malloc(bufsz);
710    if (!buf)
711    {
712        LSQ_WARN("%s: malloc(%zd) failure: %s", __func__, bufsz,
713                                                        strerror(errno));
714        return NULL;
715    }
716
717    off = 0;
718    for (n = 0; n < acki->n_ranges; ++n)
719    {
720        nw = snprintf(buf + off, bufsz - off, "[%"PRIu64"-%"PRIu64"]",
721                acki->ranges[n].high, acki->ranges[n].low);
722        if (nw > bufsz - off)
723            break;
724        off += nw;
725    }
726
727    *sz = off;
728    return buf;
729}
730
731
732