forked from ambrop72/badvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketProtoDecoder.c
174 lines (140 loc) · 4.89 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
/**
* @file PacketProtoDecoder.c
* @author Ambroz Bizjak <[email protected]>
*
* @section LICENSE
*
* This file is part of BadVPN.
*
* BadVPN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* BadVPN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#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 = (struct packetproto_header *)data;
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 = 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;
}