lsquic_version.c revision 03e6b668
1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <string.h>
350aadb33SDmitri Tikhonov
450aadb33SDmitri Tikhonov#include "lsquic.h"
550aadb33SDmitri Tikhonov#include "lsquic_int_types.h"
650aadb33SDmitri Tikhonov#include "lsquic_version.h"
750aadb33SDmitri Tikhonov
850aadb33SDmitri Tikhonov
950aadb33SDmitri Tikhonovstatic const unsigned char version_tags[N_LSQVER][4] =
1050aadb33SDmitri Tikhonov{
1150aadb33SDmitri Tikhonov    [LSQVER_039] = { 'Q', '0', '3', '9', },
12c95974e9SDmitri Tikhonov    [LSQVER_043] = { 'Q', '0', '4', '3', },
13c7d81ce1SDmitri Tikhonov    [LSQVER_046] = { 'Q', '0', '4', '6', },
149626cfc2SDmitri Tikhonov#if LSQUIC_USE_Q098
159626cfc2SDmitri Tikhonov    [LSQVER_098] = { 'Q', '0', '9', '8', },
169626cfc2SDmitri Tikhonov#endif
1792f6e17bSDmitri Tikhonov    [LSQVER_ID23] = { 0xFF, 0, 0, 23, },
1803e6b668SDmitri Tikhonov    [LSQVER_ID24] = { 0xFF, 0, 0, 24, },
195392f7a3SLiteSpeed Tech    [LSQVER_VERNEG] = { 0xFA, 0xFA, 0xFA, 0xFA, },
2050aadb33SDmitri Tikhonov};
2150aadb33SDmitri Tikhonov
2250aadb33SDmitri Tikhonov
2350aadb33SDmitri Tikhonovuint32_t
2450aadb33SDmitri Tikhonovlsquic_ver2tag (unsigned version)
2550aadb33SDmitri Tikhonov{
2650aadb33SDmitri Tikhonov    lsquic_ver_tag_t tag;
2750aadb33SDmitri Tikhonov    if (version < N_LSQVER)
2850aadb33SDmitri Tikhonov    {
2950aadb33SDmitri Tikhonov        memcpy(&tag, version_tags[version], 4);
3050aadb33SDmitri Tikhonov        return tag;
3150aadb33SDmitri Tikhonov    }
3250aadb33SDmitri Tikhonov    else
3350aadb33SDmitri Tikhonov        return 0;
3450aadb33SDmitri Tikhonov}
3550aadb33SDmitri Tikhonov
3650aadb33SDmitri Tikhonov
3750aadb33SDmitri Tikhonovenum lsquic_version
3850aadb33SDmitri Tikhonovlsquic_tag2ver (uint32_t ver_tag)
3950aadb33SDmitri Tikhonov{
4050aadb33SDmitri Tikhonov    unsigned n;
4150aadb33SDmitri Tikhonov    for (n = 0; n < sizeof(version_tags) / sizeof(version_tags[0]); ++n)
4250aadb33SDmitri Tikhonov        if (0 == memcmp(version_tags[n], &ver_tag, sizeof(ver_tag)))
4350aadb33SDmitri Tikhonov            return n;
4450aadb33SDmitri Tikhonov    return -1;
4550aadb33SDmitri Tikhonov}
4650aadb33SDmitri Tikhonov
4750aadb33SDmitri Tikhonov
485392f7a3SLiteSpeed Techconst char *const lsquic_ver2str[N_LSQVER] = {
495392f7a3SLiteSpeed Tech    [LSQVER_039] = "Q039",
505392f7a3SLiteSpeed Tech    [LSQVER_043] = "Q043",
515392f7a3SLiteSpeed Tech    [LSQVER_046] = "Q046",
525392f7a3SLiteSpeed Tech#if LSQUIC_USE_Q098
535392f7a3SLiteSpeed Tech    [LSQVER_098] = "Q098",
545392f7a3SLiteSpeed Tech#endif
5592f6e17bSDmitri Tikhonov    [LSQVER_ID23] = "FF000017",
5603e6b668SDmitri Tikhonov    [LSQVER_ID24] = "FF000018",
575392f7a3SLiteSpeed Tech    [LSQVER_VERNEG] = "FAFAFAFA",
585392f7a3SLiteSpeed Tech};
595392f7a3SLiteSpeed Tech
605392f7a3SLiteSpeed Tech
6150aadb33SDmitri Tikhonovenum lsquic_version
6250aadb33SDmitri Tikhonovlsquic_str2ver (const char *str, size_t len)
6350aadb33SDmitri Tikhonov{
645392f7a3SLiteSpeed Tech    enum lsquic_version ver;
6550aadb33SDmitri Tikhonov    uint32_t tag;
6650aadb33SDmitri Tikhonov
6750aadb33SDmitri Tikhonov    if (len == sizeof(tag) && 'Q' == str[0])
6850aadb33SDmitri Tikhonov    {
6950aadb33SDmitri Tikhonov        memcpy(&tag, str, sizeof(tag));
7050aadb33SDmitri Tikhonov        return lsquic_tag2ver(tag);
7150aadb33SDmitri Tikhonov    }
7250aadb33SDmitri Tikhonov
735392f7a3SLiteSpeed Tech    for (ver = 0; ver < N_LSQVER; ++ver)
745392f7a3SLiteSpeed Tech        if (strlen(lsquic_ver2str[ver]) == len
755392f7a3SLiteSpeed Tech            && strncasecmp(lsquic_ver2str[ver], str, len) == 0)
765392f7a3SLiteSpeed Tech        {
775392f7a3SLiteSpeed Tech            return ver;
785392f7a3SLiteSpeed Tech        }
7950aadb33SDmitri Tikhonov
805392f7a3SLiteSpeed Tech    return -1;
815392f7a3SLiteSpeed Tech}
8250aadb33SDmitri Tikhonov
8350aadb33SDmitri Tikhonov
845392f7a3SLiteSpeed Techint
855392f7a3SLiteSpeed Techlsquic_gen_ver_tags (unsigned char *buf, size_t bufsz, unsigned version_bitmask)
865392f7a3SLiteSpeed Tech{
875392f7a3SLiteSpeed Tech    unsigned n;
885392f7a3SLiteSpeed Tech    lsquic_ver_tag_t tag;
895392f7a3SLiteSpeed Tech    unsigned char *p = buf;
905392f7a3SLiteSpeed Tech    unsigned char *const pend = p + bufsz;
915392f7a3SLiteSpeed Tech    for (n = 0; version_bitmask; ++n)
925392f7a3SLiteSpeed Tech    {
935392f7a3SLiteSpeed Tech        if (version_bitmask & (1 << n))
945392f7a3SLiteSpeed Tech        {
955392f7a3SLiteSpeed Tech            if (p + 4 > pend)
965392f7a3SLiteSpeed Tech                return -1;
975392f7a3SLiteSpeed Tech            version_bitmask &= ~(1 << n);
985392f7a3SLiteSpeed Tech            tag = lsquic_ver2tag(n);
995392f7a3SLiteSpeed Tech            if (0 == tag)
1005392f7a3SLiteSpeed Tech                return -1;
1015392f7a3SLiteSpeed Tech            memcpy(p, &tag, 4);
1025392f7a3SLiteSpeed Tech            p += 4;
1035392f7a3SLiteSpeed Tech        }
1045392f7a3SLiteSpeed Tech    }
1055392f7a3SLiteSpeed Tech    return p - buf;
1065392f7a3SLiteSpeed Tech}
107