1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov/*
350aadb33SDmitri Tikhonov * lsquic_packet_common.c -- some common packet-related routines
450aadb33SDmitri Tikhonov */
550aadb33SDmitri Tikhonov
65392f7a3SLiteSpeed Tech#include <stdint.h>
750aadb33SDmitri Tikhonov#include <stdio.h>
85392f7a3SLiteSpeed Tech#include <time.h>
950aadb33SDmitri Tikhonov
105392f7a3SLiteSpeed Tech#include "lsquic.h"
115392f7a3SLiteSpeed Tech#include "lsquic_types.h"
1250aadb33SDmitri Tikhonov#include "lsquic_logger.h"
135392f7a3SLiteSpeed Tech#include "lsquic_enc_sess.h"
1450aadb33SDmitri Tikhonov#include "lsquic_packet_common.h"
1550aadb33SDmitri Tikhonov
1650aadb33SDmitri Tikhonov
1750aadb33SDmitri Tikhonovconst char *
18bfc7bfd8SDmitri Tikhonovlsquic_frame_types_to_str (char *buf, size_t bufsz,
19bfc7bfd8SDmitri Tikhonov                                           enum quic_ft_bit frame_types)
2050aadb33SDmitri Tikhonov{
2150aadb33SDmitri Tikhonov    char *p;
2250aadb33SDmitri Tikhonov    int i, w;
2350aadb33SDmitri Tikhonov    size_t sz;
2450aadb33SDmitri Tikhonov
2550aadb33SDmitri Tikhonov    if (bufsz > 0)
2650aadb33SDmitri Tikhonov        buf[0] = '\0';
2750aadb33SDmitri Tikhonov
2850aadb33SDmitri Tikhonov    p = buf;
2950aadb33SDmitri Tikhonov    for (i = 0; i < N_QUIC_FRAMES; ++i)
3050aadb33SDmitri Tikhonov    {
3150aadb33SDmitri Tikhonov        if (frame_types & (1 << i))
3250aadb33SDmitri Tikhonov        {
3350aadb33SDmitri Tikhonov            sz = bufsz - (p - buf);
3450aadb33SDmitri Tikhonov            w = snprintf(p, sz, "%.*s%s", p > buf, " ",
3550aadb33SDmitri Tikhonov                            frame_type_2_str[i] + sizeof("QUIC_FRAME_") - 1);
3650aadb33SDmitri Tikhonov            if (w > (int) sz)
3750aadb33SDmitri Tikhonov            {
3850aadb33SDmitri Tikhonov                LSQ_WARN("not enough room for all frame types");
3950aadb33SDmitri Tikhonov                break;
4050aadb33SDmitri Tikhonov            }
4150aadb33SDmitri Tikhonov            p += w;
4250aadb33SDmitri Tikhonov        }
4350aadb33SDmitri Tikhonov        frame_types &= ~(1 << i);
4450aadb33SDmitri Tikhonov    }
4550aadb33SDmitri Tikhonov
4650aadb33SDmitri Tikhonov    return buf;
4750aadb33SDmitri Tikhonov}
4850aadb33SDmitri Tikhonov
4950aadb33SDmitri Tikhonov
505392f7a3SLiteSpeed Techconst char *const lsquic_hety2str[] =
5150aadb33SDmitri Tikhonov{
525392f7a3SLiteSpeed Tech    [HETY_NOT_SET]      = "Short",
535392f7a3SLiteSpeed Tech    [HETY_VERNEG]       = "Version Negotiation",
545392f7a3SLiteSpeed Tech    [HETY_INITIAL]      = "Initial",
555392f7a3SLiteSpeed Tech    [HETY_RETRY]        = "Retry",
565392f7a3SLiteSpeed Tech    [HETY_HANDSHAKE]    = "Handshake",
575392f7a3SLiteSpeed Tech    [HETY_0RTT]         = "0-RTT",
585392f7a3SLiteSpeed Tech};
5950aadb33SDmitri Tikhonov
6050aadb33SDmitri Tikhonov
615392f7a3SLiteSpeed Tech/* [draft-ietf-quic-tls-14], Section 4 */
625392f7a3SLiteSpeed Techconst enum packnum_space lsquic_hety2pns[] =
6350aadb33SDmitri Tikhonov{
645392f7a3SLiteSpeed Tech    [HETY_NOT_SET]      = PNS_APP,
655392f7a3SLiteSpeed Tech    [HETY_VERNEG]       = 0,
665392f7a3SLiteSpeed Tech    [HETY_INITIAL]      = PNS_INIT,
675392f7a3SLiteSpeed Tech    [HETY_RETRY]        = 0,
685392f7a3SLiteSpeed Tech    [HETY_HANDSHAKE]    = PNS_HSK,
695392f7a3SLiteSpeed Tech    [HETY_0RTT]         = PNS_APP,
705392f7a3SLiteSpeed Tech};
7150aadb33SDmitri Tikhonov
7250aadb33SDmitri Tikhonov
735392f7a3SLiteSpeed Tech/* [draft-ietf-quic-tls-14], Section 4 */
745392f7a3SLiteSpeed Techconst enum packnum_space lsquic_enclev2pns[] =
755392f7a3SLiteSpeed Tech{
765392f7a3SLiteSpeed Tech    [ENC_LEV_CLEAR]      = PNS_INIT,
775392f7a3SLiteSpeed Tech    [ENC_LEV_INIT]       = PNS_HSK,
785392f7a3SLiteSpeed Tech    [ENC_LEV_EARLY]      = PNS_APP,
795392f7a3SLiteSpeed Tech    [ENC_LEV_FORW]       = PNS_APP,
805392f7a3SLiteSpeed Tech};
819626cfc2SDmitri Tikhonov
829626cfc2SDmitri Tikhonov
835392f7a3SLiteSpeed Techconst char *const lsquic_pns2str[] =
849626cfc2SDmitri Tikhonov{
855392f7a3SLiteSpeed Tech    [PNS_INIT]  = "Init PNS",
865392f7a3SLiteSpeed Tech    [PNS_HSK]   = "Handshake PNS",
875392f7a3SLiteSpeed Tech    [PNS_APP]   = "App PNS",
889626cfc2SDmitri Tikhonov};
89