forked from ambrop72/badvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketProtoDecoder.c
182 lines (148 loc) · 5.68 KB
/
PacketProtoDecoder.c
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
/**
* @file PacketProtoDecoder.c
* @author Ambroz Bizjak <[email protected]>
*
* @section LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include <misc/debug.h>
#include <misc/byteorder.h>
#include <misc/minmax.h>
#include <base/BLog.h>
#include <flow/PacketProtoDecoder.h>
#include <generated/blog_channel_PacketProtoDecoder.h>
static void process_data (PacketProtoDecoder *enc);
static void input_handler_done (PacketProtoDecoder *enc, int data_len);
static void output_handler_done (PacketProtoDecoder *enc);
void process_data (PacketProtoDecoder *enc)
{
int was_error = 0;
do {
uint8_t *data = enc->buf + enc->buf_start;
int left = enc->buf_used;
// check if header was received
if (left < sizeof(struct packetproto_header)) {
break;
}
struct packetproto_header header;
memcpy(&header, data, sizeof(header));
data += sizeof(struct packetproto_header);
left -= sizeof(struct packetproto_header);
int data_len = ltoh16(header.len);
// check data length
if (data_len > enc->output_mtu) {
BLog(BLOG_NOTICE, "error: packet too large");
was_error = 1;
break;
}
// check if whole packet was received
if (left < data_len) {
break;
}
// update buffer
enc->buf_start += sizeof(struct packetproto_header) + data_len;
enc->buf_used -= sizeof(struct packetproto_header) + data_len;
// submit packet
PacketPassInterface_Sender_Send(enc->output, data, data_len);
return;
} while (0);
if (was_error) {
// reset buffer
enc->buf_start = 0;
enc->buf_used = 0;
} else {
// if we reached the end of the buffer, wrap around to allow more data to be received
if (enc->buf_start + enc->buf_used == enc->buf_size) {
memmove(enc->buf, enc->buf + enc->buf_start, enc->buf_used);
enc->buf_start = 0;
}
}
// receive data
StreamRecvInterface_Receiver_Recv(enc->input, enc->buf + (enc->buf_start + enc->buf_used), enc->buf_size - (enc->buf_start + enc->buf_used));
// if we had error, report it
if (was_error) {
enc->handler_error(enc->user);
return;
}
}
static void input_handler_done (PacketProtoDecoder *enc, int data_len)
{
ASSERT(data_len > 0)
ASSERT(data_len <= enc->buf_size - (enc->buf_start + enc->buf_used))
DebugObject_Access(&enc->d_obj);
// update buffer
enc->buf_used += data_len;
// process data
process_data(enc);
return;
}
void output_handler_done (PacketProtoDecoder *enc)
{
DebugObject_Access(&enc->d_obj);
// process data
process_data(enc);
return;
}
int PacketProtoDecoder_Init (PacketProtoDecoder *enc, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg, void *user, PacketProtoDecoder_handler_error handler_error)
{
// init arguments
enc->input = input;
enc->output = output;
enc->user = user;
enc->handler_error = handler_error;
// init input
StreamRecvInterface_Receiver_Init(enc->input, (StreamRecvInterface_handler_done)input_handler_done, enc);
// init output
PacketPassInterface_Sender_Init(enc->output, (PacketPassInterface_handler_done)output_handler_done, enc);
// set output MTU, limit by maximum payload size
enc->output_mtu = bmin_int(PacketPassInterface_GetMTU(enc->output), PACKETPROTO_MAXPAYLOAD);
// init buffer state
enc->buf_size = PACKETPROTO_ENCLEN(enc->output_mtu);
enc->buf_start = 0;
enc->buf_used = 0;
// allocate buffer
if (!(enc->buf = (uint8_t *)malloc(enc->buf_size))) {
goto fail0;
}
// start receiving
StreamRecvInterface_Receiver_Recv(enc->input, enc->buf, enc->buf_size);
DebugObject_Init(&enc->d_obj);
return 1;
fail0:
return 0;
}
void PacketProtoDecoder_Free (PacketProtoDecoder *enc)
{
DebugObject_Free(&enc->d_obj);
// free buffer
free(enc->buf);
}
void PacketProtoDecoder_Reset (PacketProtoDecoder *enc)
{
DebugObject_Access(&enc->d_obj);
enc->buf_start += enc->buf_used;
enc->buf_used = 0;
}