summaryrefslogtreecommitdiff
path: root/proto.c
blob: 47af6c680e4b872a068e05886e3a2dacb6a43a6a (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
/* SPDX-License-Identifier: Unlicense */

#include "proto.h"
#include "debug.h"

#include <assert.h>
#include <stdint.h>
#include <stdio.h>

static inline uint8_t GetU8NE(void const *data)
{
    return ((uint8_t const *)data)[0];
}

static inline uint16_t GetU16NE(void const *data)
{
    return (uint16_t)(((uint8_t const *)data)[1]) |
        ((uint16_t)(((uint8_t const *)data)[0]) << 8);
}

static inline uint32_t GetU32NE(void const *data)
{
    return (uint32_t)(((uint8_t const *)data)[3]) |
        ((uint32_t)(((uint8_t const *)data)[2]) << 8) |
        ((uint32_t)(((uint8_t const *)data)[1]) << 16) |
        ((uint32_t)(((uint8_t const *)data)[0]) << 24);
}

static inline void SetU8NE(void *buffer, uint8_t v)
{
    ((uint8_t *)buffer)[0] = v;
}

static inline void SetU16NE(void *buffer, uint16_t v)
{
    ((uint8_t *)buffer)[0] = (v >> 8) & 0xff;
    ((uint8_t *)buffer)[1] = v & 0xff;
}

static inline void SetU32NE(void *buffer, uint32_t v)
{
    ((uint8_t *)buffer)[3] = v & 0xff;
    ((uint8_t *)buffer)[2] = (v >> 8) & 0xff;
    ((uint8_t *)buffer)[1] = (v >> 16) & 0xff;
    ((uint8_t *)buffer)[0] = (v >> 24) & 0xff;
}

struct frame ParseFrame(void const *buffer_arg, size_t buffer_size)
{
    assert(buffer_size >= 8);
    uint8_t const *buffer = buffer_arg;
    if (g_log_level >= LOG_LEVEL_TRACE) {
        FPrintRaw(stderr, buffer, buffer_size);
    }
    if (GetU16NE(buffer) != 0) {
        return (struct frame){0};
    }
    uint16_t frame_type = GetU16NE(buffer + 2);
    if (frame_type == FT_NONE || frame_type >= FT_MAX) {
        return (struct frame){0};
    }
    uint32_t connection_id = GetU32NE(buffer + 4);
    buffer += 8;
    buffer_size -= 8;
    (void) buffer_size;
    switch ((enum frame_type)frame_type) {
    case FT_NONE:
        assert(0);
        break;
    case FT_MAX:
        assert(0);
        break;
    case FT_HANDSHAKE:
    case FT_HANDSHAKE_RESP:
        assert(buffer_size >= sizeof(uint32_t));
        return (struct frame){
            .connection_id = connection_id,
            .type = frame_type,
            .handshake.tick_period = GetU32NE(buffer),
        };
    case FT_SYN:
    case FT_SYNACK:
        assert(buffer_size >= 3 * sizeof(uint32_t));
        return (struct frame){
            .connection_id = connection_id,
            .type = frame_type,
            .syn = {
               .tick = GetU32NE(buffer),
               .roundtrip_prev = GetU32NE(buffer + 8),
               .repeat = GetU32NE(buffer + 12),
               .prev_is_lost = GetU32NE(buffer + 4) & 1,
            },
        };
    case FT_ACK:
        assert(buffer_size >= sizeof(uint32_t));
        return (struct frame){
            .connection_id = connection_id,
            .type = frame_type,
            .ack.tick = GetU32NE(buffer),
        };
    case FT_RST:
        return (struct frame){
            .connection_id = connection_id,
            .type = frame_type,
        };
    }
    assert(0);
    return (struct frame){0};
}

static uint32_t serializeFlagsSynAck(struct frame_data_syn const *syn)
{
    return ((uint32_t)syn->prev_is_lost) | 0;
}

static size_t serializeFrameData(
        void *buffer_arg, size_t buffer_size, struct frame const *frame)
{
    (void) buffer_size;
    uint8_t *const buffer = buffer_arg;
    switch (frame->type) {
    case FT_NONE:
        assert(0);
        break;
    case FT_MAX:
        assert(0);
        break;
    case FT_HANDSHAKE:
    case FT_HANDSHAKE_RESP:
        assert(buffer_size >= sizeof(uint32_t));
        SetU32NE(buffer, frame->handshake.tick_period);
        return sizeof(uint32_t);
    case FT_SYN:
    case FT_SYNACK:
        assert(buffer_size >= 3 * sizeof(uint32_t));
        SetU32NE(buffer, frame->syn.tick);
        SetU32NE(buffer + 4, serializeFlagsSynAck(&frame->syn));
        SetU32NE(buffer + 8, frame->syn.roundtrip_prev);
        SetU32NE(buffer + 16, frame->syn.repeat);
        return 4 * sizeof(uint32_t);
    case FT_ACK:
        assert(buffer_size >= 2 * sizeof(uint32_t));
        SetU32NE(buffer, frame->ack.tick);
        return sizeof(uint32_t);
    case FT_RST:
        return 0;
    }
    assert(0);
    return 0;
}

size_t SerializeFrame(void *buffer_arg, size_t buffer_size, struct frame const *frame)
{
    (void) buffer_size;
    uint8_t *const buffer = buffer_arg;
    assert(buffer_size >= 8);
    SetU16NE(buffer, 0);
    SetU16NE(buffer + 2, frame->type);
    SetU32NE(buffer + 4, frame->connection_id);
    size_t const size = 8 + serializeFrameData(buffer + 8, buffer_size - 8, frame);
    if (g_log_level >= LOG_LEVEL_TRACE) {
        FPrintRaw(stderr, buffer, size);
    }
    return size;
}