forked from ambrop72/badvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdpGwClient.c
568 lines (448 loc) · 18.9 KB
/
UdpGwClient.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright (C) Ambroz Bizjak <[email protected]>
* Contributions:
* Transparent DNS: Copyright (C) Kerem Hadimli <[email protected]>
*
* 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/offset.h>
#include <misc/byteorder.h>
#include <misc/compare.h>
#include <base/BLog.h>
#include <udpgw_client/UdpGwClient.h>
#include <generated/blog_channel_UdpGwClient.h>
static int uint16_comparator (void *unused, uint16_t *v1, uint16_t *v2);
static int compare_addresses (BAddr v1, BAddr v2);
static int conaddr_comparator (void *unused, struct UdpGwClient_conaddr *v1, struct UdpGwClient_conaddr *v2);
static void free_server (UdpGwClient *o);
static void decoder_handler_error (UdpGwClient *o);
static void recv_interface_handler_send (UdpGwClient *o, uint8_t *data, int data_len);
static void send_monitor_handler (UdpGwClient *o);
static void keepalive_if_handler_done (UdpGwClient *o);
static struct UdpGwClient_connection * find_connection_by_conaddr (UdpGwClient *o, struct UdpGwClient_conaddr conaddr);
static struct UdpGwClient_connection * find_connection_by_conid (UdpGwClient *o, uint16_t conid);
static uint16_t find_unused_conid (UdpGwClient *o);
static void connection_init (UdpGwClient *o, struct UdpGwClient_conaddr conaddr, uint8_t flags, const uint8_t *data, int data_len);
static void connection_free (struct UdpGwClient_connection *con);
static void connection_first_job_handler (struct UdpGwClient_connection *con);
static void connection_send (struct UdpGwClient_connection *con, uint8_t flags, const uint8_t *data, int data_len);
static struct UdpGwClient_connection * reuse_connection (UdpGwClient *o, struct UdpGwClient_conaddr conaddr);
static int uint16_comparator (void *unused, uint16_t *v1, uint16_t *v2)
{
return B_COMPARE(*v1, *v2);
}
static int compare_addresses (BAddr v1, BAddr v2)
{
ASSERT(v1.type == BADDR_TYPE_IPV4)
ASSERT(v2.type == BADDR_TYPE_IPV4)
int cmp = B_COMPARE(v1.ipv4.port, v2.ipv4.port);
if (cmp) {
return cmp;
}
return B_COMPARE(v1.ipv4.ip, v2.ipv4.ip);
}
static int conaddr_comparator (void *unused, struct UdpGwClient_conaddr *v1, struct UdpGwClient_conaddr *v2)
{
ASSERT(v1->local_addr.type == BADDR_TYPE_IPV4)
ASSERT(v1->remote_addr.type == BADDR_TYPE_IPV4)
ASSERT(v2->local_addr.type == BADDR_TYPE_IPV4)
ASSERT(v2->remote_addr.type == BADDR_TYPE_IPV4)
int r = compare_addresses(v1->remote_addr, v2->remote_addr);
if (r) {
return r;
}
return compare_addresses(v1->local_addr, v2->local_addr);
}
static void free_server (UdpGwClient *o)
{
// disconnect send connector
PacketPassConnector_DisconnectOutput(&o->send_connector);
// free send sender
PacketStreamSender_Free(&o->send_sender);
// free receive decoder
PacketProtoDecoder_Free(&o->recv_decoder);
// free receive interface
PacketPassInterface_Free(&o->recv_if);
}
static void decoder_handler_error (UdpGwClient *o)
{
DebugObject_Access(&o->d_obj);
ASSERT(o->have_server)
BLog(BLOG_ERROR, "decoder error");
// report error
o->handler_servererror(o->user);
return;
}
static void recv_interface_handler_send (UdpGwClient *o, uint8_t *data, int data_len)
{
DebugObject_Access(&o->d_obj);
ASSERT(o->have_server)
ASSERT(data_len >= 0)
ASSERT(data_len <= o->udpgw_mtu)
// accept packet
PacketPassInterface_Done(&o->recv_if);
// check header
if (data_len < sizeof(struct udpgw_header)) {
BLog(BLOG_ERROR, "missing header");
return;
}
struct udpgw_header header;
memcpy(&header, data, sizeof(header));
data += sizeof(header);
data_len -= sizeof(header);
uint16_t conid = ltoh16(header.conid);
// check remaining data
if (data_len > o->udp_mtu) {
BLog(BLOG_ERROR, "too much data");
return;
}
// find connection
struct UdpGwClient_connection *con = find_connection_by_conid(o, conid);
if (!con) {
BLog(BLOG_ERROR, "unknown conid");
return;
}
// check remote address
if (con->conaddr.remote_addr.ipv4.port != header.addr_port || con->conaddr.remote_addr.ipv4.ip != header.addr_ip) {
BLog(BLOG_ERROR, "wrong remote address");
return;
}
// move connection to front of the list
LinkedList1_Remove(&o->connections_list, &con->connections_list_node);
LinkedList1_Append(&o->connections_list, &con->connections_list_node);
// pass packet to user
o->handler_received(o->user, con->conaddr.local_addr, con->conaddr.remote_addr, data, data_len);
return;
}
static void send_monitor_handler (UdpGwClient *o)
{
DebugObject_Access(&o->d_obj);
if (o->keepalive_sending) {
return;
}
BLog(BLOG_INFO, "keepalive");
// send keepalive
PacketPassInterface_Sender_Send(o->keepalive_if, (uint8_t *)&o->keepalive_packet, sizeof(o->keepalive_packet));
// set sending keep-alive
o->keepalive_sending = 1;
}
static void keepalive_if_handler_done (UdpGwClient *o)
{
DebugObject_Access(&o->d_obj);
ASSERT(o->keepalive_sending)
// set not sending keepalive
o->keepalive_sending = 0;
}
static struct UdpGwClient_connection * find_connection_by_conaddr (UdpGwClient *o, struct UdpGwClient_conaddr conaddr)
{
BAVLNode *tree_node = BAVL_LookupExact(&o->connections_tree_by_conaddr, &conaddr);
if (!tree_node) {
return NULL;
}
return UPPER_OBJECT(tree_node, struct UdpGwClient_connection, connections_tree_by_conaddr_node);
}
static struct UdpGwClient_connection * find_connection_by_conid (UdpGwClient *o, uint16_t conid)
{
BAVLNode *tree_node = BAVL_LookupExact(&o->connections_tree_by_conid, &conid);
if (!tree_node) {
return NULL;
}
return UPPER_OBJECT(tree_node, struct UdpGwClient_connection, connections_tree_by_conid_node);
}
static uint16_t find_unused_conid (UdpGwClient *o)
{
ASSERT(o->num_connections < o->max_connections)
while (1) {
if (!find_connection_by_conid(o, o->next_conid)) {
return o->next_conid;
}
if (o->next_conid == o->max_connections - 1) {
o->next_conid = 0;
} else {
o->next_conid++;
}
}
}
static void connection_init (UdpGwClient *o, struct UdpGwClient_conaddr conaddr, uint8_t flags, const uint8_t *data, int data_len)
{
ASSERT(o->num_connections < o->max_connections)
ASSERT(!find_connection_by_conaddr(o, conaddr))
ASSERT(data_len >= 0)
ASSERT(data_len <= o->udp_mtu)
// allocate structure
struct UdpGwClient_connection *con = (struct UdpGwClient_connection *)malloc(sizeof(*con));
if (!con) {
BLog(BLOG_ERROR, "malloc failed");
goto fail0;
}
// init arguments
con->client = o;
con->conaddr = conaddr;
con->first_flags = flags;
con->first_data = data;
con->first_data_len = data_len;
// allocate conid
con->conid = find_unused_conid(o);
// init first job
BPending_Init(&con->first_job, BReactor_PendingGroup(o->reactor), (BPending_handler)connection_first_job_handler, con);
BPending_Set(&con->first_job);
// init queue flow
PacketPassFairQueueFlow_Init(&con->send_qflow, &o->send_queue);
// init PacketProtoFlow
if (!PacketProtoFlow_Init(&con->send_ppflow, o->udpgw_mtu, o->send_buffer_size, PacketPassFairQueueFlow_GetInput(&con->send_qflow), BReactor_PendingGroup(o->reactor))) {
BLog(BLOG_ERROR, "PacketProtoFlow_Init failed");
goto fail1;
}
con->send_if = PacketProtoFlow_GetInput(&con->send_ppflow);
// insert to connections tree by conaddr
ASSERT_EXECUTE(BAVL_Insert(&o->connections_tree_by_conaddr, &con->connections_tree_by_conaddr_node, NULL))
// insert to connections tree by conid
ASSERT_EXECUTE(BAVL_Insert(&o->connections_tree_by_conid, &con->connections_tree_by_conid_node, NULL))
// insert to connections list
LinkedList1_Append(&o->connections_list, &con->connections_list_node);
// increment number of connections
o->num_connections++;
return;
fail1:
PacketPassFairQueueFlow_Free(&con->send_qflow);
BPending_Free(&con->first_job);
free(con);
fail0:
return;
}
static void connection_free (struct UdpGwClient_connection *con)
{
UdpGwClient *o = con->client;
PacketPassFairQueueFlow_AssertFree(&con->send_qflow);
// decrement number of connections
o->num_connections--;
// remove from connections list
LinkedList1_Remove(&o->connections_list, &con->connections_list_node);
// remove from connections tree by conid
BAVL_Remove(&o->connections_tree_by_conid, &con->connections_tree_by_conid_node);
// remove from connections tree by conaddr
BAVL_Remove(&o->connections_tree_by_conaddr, &con->connections_tree_by_conaddr_node);
// free PacketProtoFlow
PacketProtoFlow_Free(&con->send_ppflow);
// free queue flow
PacketPassFairQueueFlow_Free(&con->send_qflow);
// free first job
BPending_Free(&con->first_job);
// free structure
free(con);
}
static void connection_first_job_handler (struct UdpGwClient_connection *con)
{
connection_send(con, UDPGW_CLIENT_FLAG_REBIND|con->first_flags, con->first_data, con->first_data_len);
}
static void connection_send (struct UdpGwClient_connection *con, uint8_t flags, const uint8_t *data, int data_len)
{
UdpGwClient *o = con->client;
B_USE(o)
ASSERT(data_len >= 0)
ASSERT(data_len <= o->udp_mtu)
// get buffer location
uint8_t *out;
if (!BufferWriter_StartPacket(con->send_if, &out)) {
BLog(BLOG_ERROR, "out of buffer");
return;
}
// write header
struct udpgw_header header;
header.flags = ltoh8(flags);
header.conid = ltoh16(con->conid);
header.addr_ip = con->conaddr.remote_addr.ipv4.ip;
header.addr_port = con->conaddr.remote_addr.ipv4.port;
memcpy(out, &header, sizeof(header));
// write packet to buffer
memcpy(out + sizeof(header), data, data_len);
// submit packet to buffer
BufferWriter_EndPacket(con->send_if, sizeof(header) + data_len);
}
static struct UdpGwClient_connection * reuse_connection (UdpGwClient *o, struct UdpGwClient_conaddr conaddr)
{
ASSERT(!find_connection_by_conaddr(o, conaddr))
ASSERT(o->num_connections > 0)
// get least recently used connection
struct UdpGwClient_connection *con = UPPER_OBJECT(LinkedList1_GetFirst(&o->connections_list), struct UdpGwClient_connection, connections_list_node);
// remove from connections tree by conaddr
BAVL_Remove(&o->connections_tree_by_conaddr, &con->connections_tree_by_conaddr_node);
// set new conaddr
con->conaddr = conaddr;
// insert to connections tree by conaddr
ASSERT_EXECUTE(BAVL_Insert(&o->connections_tree_by_conaddr, &con->connections_tree_by_conaddr_node, NULL))
return con;
}
int UdpGwClient_Init (UdpGwClient *o, int udp_mtu, int max_connections, int send_buffer_size, btime_t keepalive_time, BReactor *reactor, void *user,
UdpGwClient_handler_servererror handler_servererror,
UdpGwClient_handler_received handler_received)
{
ASSERT(udp_mtu >= 0)
ASSERT(udpgw_compute_mtu(udp_mtu) >= 0)
ASSERT(udpgw_compute_mtu(udp_mtu) <= PACKETPROTO_MAXPAYLOAD)
ASSERT(max_connections > 0)
ASSERT(send_buffer_size > 0)
// init arguments
o->udp_mtu = udp_mtu;
o->max_connections = max_connections;
o->send_buffer_size = send_buffer_size;
o->keepalive_time = keepalive_time;
o->reactor = reactor;
o->user = user;
o->handler_servererror = handler_servererror;
o->handler_received = handler_received;
// limit max connections to number of conid's
if (o->max_connections > UINT16_MAX + 1) {
o->max_connections = UINT16_MAX + 1;
}
// compute MTUs
o->udpgw_mtu = udpgw_compute_mtu(o->udp_mtu);
o->pp_mtu = o->udpgw_mtu + sizeof(struct packetproto_header);
// init connections tree by conaddr
BAVL_Init(&o->connections_tree_by_conaddr, OFFSET_DIFF(struct UdpGwClient_connection, conaddr, connections_tree_by_conaddr_node), (BAVL_comparator)conaddr_comparator, NULL);
// init connections tree by conid
BAVL_Init(&o->connections_tree_by_conid, OFFSET_DIFF(struct UdpGwClient_connection, conid, connections_tree_by_conid_node), (BAVL_comparator)uint16_comparator, NULL);
// init connections list
LinkedList1_Init(&o->connections_list);
// set zero connections
o->num_connections = 0;
// set next conid
o->next_conid = 0;
// init send connector
PacketPassConnector_Init(&o->send_connector, o->pp_mtu, BReactor_PendingGroup(o->reactor));
// init send monitor
PacketPassInactivityMonitor_Init(&o->send_monitor, PacketPassConnector_GetInput(&o->send_connector), o->reactor, o->keepalive_time, (PacketPassInactivityMonitor_handler)send_monitor_handler, o);
// init send queue
if (!PacketPassFairQueue_Init(&o->send_queue, PacketPassInactivityMonitor_GetInput(&o->send_monitor), BReactor_PendingGroup(o->reactor), 0, 1)) {
goto fail0;
}
// construct keepalive packet
o->keepalive_packet.pp.len = sizeof(o->keepalive_packet.udpgw);
memset(&o->keepalive_packet.udpgw, 0, sizeof(o->keepalive_packet.udpgw));
o->keepalive_packet.udpgw.flags = UDPGW_CLIENT_FLAG_KEEPALIVE;
// init keepalive queue flow
PacketPassFairQueueFlow_Init(&o->keepalive_qflow, &o->send_queue);
o->keepalive_if = PacketPassFairQueueFlow_GetInput(&o->keepalive_qflow);
// init keepalive output
PacketPassInterface_Sender_Init(o->keepalive_if, (PacketPassInterface_handler_done)keepalive_if_handler_done, o);
// set not sending keepalive
o->keepalive_sending = 0;
// set have no server
o->have_server = 0;
DebugObject_Init(&o->d_obj);
return 1;
fail0:
PacketPassInactivityMonitor_Free(&o->send_monitor);
PacketPassConnector_Free(&o->send_connector);
return 0;
}
void UdpGwClient_Free (UdpGwClient *o)
{
DebugObject_Free(&o->d_obj);
// allow freeing send queue flows
PacketPassFairQueue_PrepareFree(&o->send_queue);
// free connections
while (!LinkedList1_IsEmpty(&o->connections_list)) {
struct UdpGwClient_connection *con = UPPER_OBJECT(LinkedList1_GetFirst(&o->connections_list), struct UdpGwClient_connection, connections_list_node);
connection_free(con);
}
// free server
if (o->have_server) {
free_server(o);
}
// free keepalive queue flow
PacketPassFairQueueFlow_Free(&o->keepalive_qflow);
// free send queue
PacketPassFairQueue_Free(&o->send_queue);
// free send
PacketPassInactivityMonitor_Free(&o->send_monitor);
// free send connector
PacketPassConnector_Free(&o->send_connector);
}
void UdpGwClient_SubmitPacket (UdpGwClient *o, BAddr local_addr, BAddr remote_addr, int is_dns, const uint8_t *data, int data_len)
{
DebugObject_Access(&o->d_obj);
ASSERT(local_addr.type == BADDR_TYPE_IPV4)
ASSERT(remote_addr.type == BADDR_TYPE_IPV4)
ASSERT(data_len >= 0)
ASSERT(data_len <= o->udp_mtu)
// build conaddr
struct UdpGwClient_conaddr conaddr;
conaddr.local_addr = local_addr;
conaddr.remote_addr = remote_addr;
// lookup connection
struct UdpGwClient_connection *con = find_connection_by_conaddr(o, conaddr);
uint8_t flags = 0;
if (is_dns) {
// route to remote DNS server instead of provided address
flags |= UDPGW_CLIENT_FLAG_DNS;
}
// if no connection and can't create a new one, reuse the least recently used une
if (!con && o->num_connections == o->max_connections) {
con = reuse_connection(o, conaddr);
flags |= UDPGW_CLIENT_FLAG_REBIND;
}
if (!con) {
// create new connection
connection_init(o, conaddr, flags, data, data_len);
} else {
// move connection to front of the list
LinkedList1_Remove(&o->connections_list, &con->connections_list_node);
LinkedList1_Append(&o->connections_list, &con->connections_list_node);
// send packet to existing connection
connection_send(con, flags, data, data_len);
}
}
int UdpGwClient_ConnectServer (UdpGwClient *o, StreamPassInterface *send_if, StreamRecvInterface *recv_if)
{
DebugObject_Access(&o->d_obj);
ASSERT(!o->have_server)
// init receive interface
PacketPassInterface_Init(&o->recv_if, o->udpgw_mtu, (PacketPassInterface_handler_send)recv_interface_handler_send, o, BReactor_PendingGroup(o->reactor));
// init receive decoder
if (!PacketProtoDecoder_Init(&o->recv_decoder, recv_if, &o->recv_if, BReactor_PendingGroup(o->reactor), o, (PacketProtoDecoder_handler_error)decoder_handler_error)) {
BLog(BLOG_ERROR, "PacketProtoDecoder_Init failed");
goto fail1;
}
// init send sender
PacketStreamSender_Init(&o->send_sender, send_if, o->pp_mtu, BReactor_PendingGroup(o->reactor));
// connect send connector
PacketPassConnector_ConnectOutput(&o->send_connector, PacketStreamSender_GetInput(&o->send_sender));
// set have server
o->have_server = 1;
return 1;
fail1:
PacketPassInterface_Free(&o->recv_if);
return 0;
}
void UdpGwClient_DisconnectServer (UdpGwClient *o)
{
DebugObject_Access(&o->d_obj);
ASSERT(o->have_server)
// free server
free_server(o);
// set have no server
o->have_server = 0;
}