-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathudpserver.c
517 lines (440 loc) · 15.4 KB
/
udpserver.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
/*
* Project: udptunnel
* File: udpserver.c
*
* Copyright (C) 2009 Daniel Meekins
* Contact: dmeekins - gmail
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>
#else
#include "helpers/winhelpers.h"
#endif
#include "common.h"
#include "list.h"
#include "client.h"
#include "message.h"
#include "socket.h"
#include "acl.h"
extern int debug_level;
extern int ipver;
static int running = 1;
static int next_client_id = 1;
/* internal functions */
static int handle_message(uint16_t id, uint8_t msg_type, char *data,
int data_len, socket_t *from, list_t *clients,
fd_set *client_fds,
list_t *acls);
static void disconnect_and_remove_client(uint16_t id, list_t *clients,
fd_set *fds, int full_disconnect);
static void signal_handler(int sig);
/*
* UDP Tunnel server main(). Handles program arguments, initializes everything,
* and runs the main loop.
*/
int udpserver(int argc, char *argv[])
{
char host_str[ADDRSTRLEN];
char port_str[ADDRSTRLEN];
char addrstr[ADDRSTRLEN];
list_t *clients = NULL;
list_t *acls = NULL;
socket_t *udp_sock = NULL;
socket_t *udp_from = NULL;
char data[MSG_MAX_LEN];
client_t *client;
uint16_t tmp_id;
uint8_t tmp_type;
uint16_t tmp_len;
acl_t *tmp_acl;
struct timeval curr_time;
struct timeval timeout;
struct timeval check_time;
struct timeval check_interval;
fd_set client_fds;
fd_set read_fds;
int num_fds;
int i;
int ret;
signal(SIGINT, &signal_handler);
if(argc == 1) /* only port specified */
{
ERROR_GOTO(!isnum(argv[0]), "invalid port format", done);
strncpy(port_str, argv[0], sizeof(port_str));
port_str[sizeof(port_str)-1] = 0;
host_str[0] = 0;
argv++;
argc--;
}
else
{
/* first arg will be host IP if it's not the port */
if(!isnum(argv[0]))
{
ERROR_GOTO(!isipaddr(argv[0], ipver),
"invalid IP address format", done);
strncpy(host_str, argv[0], sizeof(host_str));
host_str[sizeof(host_str)-1] = 0;
argv++;
argc--;
}
else
{
host_str[0] = 0;
}
/* next arg will be the port */
ERROR_GOTO(!isnum(argv[0]), "invalid port format", done);
strncpy(port_str, argv[0], sizeof(port_str));
port_str[sizeof(port_str)-1] = 0;
argv++;
argc--;
}
/* create empty unsorted acl list */
acls = list_create(sizeof(acl_t), NULL, NULL, p_acl_free, 0);
ERROR_GOTO(acls == NULL, "creating acl list", done);
for(i = 0; i < argc; i++)
{
tmp_acl = acl_create(argv[i], ipver);
ERROR_GOTO(tmp_acl == NULL, "creating acl", done);
list_add(acls, tmp_acl, 0);
if(debug_level >= DEBUG_LEVEL2)
{
printf("adding acl entry: ");
acl_print(tmp_acl);
}
}
/* add ALLOW ALL entry at end of list */
tmp_acl = acl_create(ACL_DEFAULT, ipver);
ERROR_GOTO(tmp_acl == NULL, "creating acl", done);
list_add(acls, tmp_acl, 0);
if(debug_level >= DEBUG_LEVEL2)
{
printf("adding acl entry: ");
acl_print(tmp_acl);
}
/* Create an empty list for the clients */
clients = list_create(sizeof(client_t), p_client_cmp, p_client_copy,
p_client_free, 1);
if(!clients)
goto done;
/* Create the socket to receive UDP messages on the specified port */
udp_sock = sock_create((host_str[0] == 0 ? NULL : host_str), port_str,
ipver, SOCK_TYPE_UDP, 1, 1);
if(!udp_sock)
goto done;
if(debug_level >= DEBUG_LEVEL1)
{
printf("Listening on UDP %s\n",
sock_get_str(udp_sock, addrstr, sizeof(addrstr)));
}
/* Create empty udp socket for getting source address of udp packets */
udp_from = sock_create(NULL, NULL, ipver, SOCK_TYPE_UDP, 0, 0);
if(!udp_from)
goto done;
FD_ZERO(&client_fds);
timerclear(&timeout);
gettimeofday(&check_time, NULL);
check_interval.tv_sec = 0;
check_interval.tv_usec = 500000;
while(running)
{
if(!timerisset(&timeout))
timeout.tv_usec = 50000;
/* Reset the file desc. set */
read_fds = client_fds;
FD_SET(SOCK_FD(udp_sock), &read_fds);
ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout);
PERROR_GOTO(ret < 0, "select", done);
num_fds = ret;
gettimeofday(&curr_time, NULL);
/* Go through all the clients and check if didn't get an ACK for sent
data during the timeout period */
if(timercmp(&curr_time, &check_time, >))
{
for(i = 0; i < LIST_LEN(clients); i++)
{
client = list_get_at(clients, i);
if(client_timed_out(client, curr_time))
{
disconnect_and_remove_client(CLIENT_ID(client), clients,
&client_fds, 1);
i--;
continue;
}
ret = client_check_and_resend(client, curr_time);
if(ret == -2)
{
disconnect_and_remove_client(CLIENT_ID(client), clients,
&client_fds, 1);
i--;
continue;
}
}
/* Set time to chech this stuff next */
timeradd(&curr_time, &check_interval, &check_time);
}
if(num_fds == 0)
continue;
/* Get any data received on the UDP socket */
if(FD_ISSET(SOCK_FD(udp_sock), &read_fds))
{
ret = msg_recv_msg(udp_sock, udp_from, data, sizeof(data),
&tmp_id, &tmp_type, &tmp_len);
if(ret == 0)
ret = handle_message(tmp_id, tmp_type, data, tmp_len,
udp_from, clients, &client_fds, acls);
if(ret < 0)
disconnect_and_remove_client(tmp_id, clients, &client_fds, 1);
num_fds--;
}
/* Go through all the clients and get any TCP data that is ready */
for(i = 0; i < LIST_LEN(clients); i++)
{
client = list_get_at(clients, i);
if(num_fds > 0 && client_tcp_fd_isset(client, &read_fds))
{
ret = client_recv_tcp_data(client);
if(ret == -1)
{
disconnect_and_remove_client(CLIENT_ID(client),
clients, &client_fds, 1);
i--; /* Since there will be one less element in list */
continue;
}
else if(ret == -2)
{
client_mark_to_disconnect(client);
disconnect_and_remove_client(CLIENT_ID(client),
clients, &client_fds, 0);
}
num_fds--;
}
/* send any TCP data that was ready */
ret = client_send_udp_data(client);
if(ret < 0)
{
disconnect_and_remove_client(CLIENT_ID(client),
clients, &client_fds, 1);
i--; /* Since there will be one less element in list */
continue;
}
}
/* Finally, send any udp data that's still in the queue */
for(i = 0; i < LIST_LEN(clients); i++)
{
client = list_get_at(clients, i);
ret = client_send_udp_data(client);
if(ret < 0 || client_ready_to_disconnect(client))
{
disconnect_and_remove_client(CLIENT_ID(client), clients,
&client_fds, 1);
i--;
continue;
}
}
}
done:
if(debug_level >= DEBUG_LEVEL1)
printf("Cleaning up...\n");
if(acls)
list_free(acls);
if(clients)
list_free(clients);
if(udp_sock)
{
sock_close(udp_sock);
sock_free(udp_sock);
}
if(udp_from)
sock_free(udp_from);
if(debug_level >= DEBUG_LEVEL1)
printf("Goodbye.\n");
return 0;
}
/*
* Closes the client's TCP socket (not UDP, since it is shared) and remove from
* the fd set. If full_disconnect is set, remove the list.
*/
void disconnect_and_remove_client(uint16_t id, list_t *clients,
fd_set *fds, int full_disconnect)
{
client_t *c;
if(id == 0)
return;
c = list_get(clients, &id);
if(!c)
return;
/* ok to call multiple times since fd will be -1 after first disconnect */
client_remove_tcp_fd_from_set(c, fds);
client_disconnect_tcp(c);
if(full_disconnect)
{
client_send_goodbye(c);
if(debug_level >= DEBUG_LEVEL1)
printf("Client %d disconnected.\n", CLIENT_ID(c));
list_delete(clients, &id);
}
}
/*
* Handles the message received from the UDP tunnel. Returns 0 for success, -1
* for some error that it handled, and -2 if the connection should be
* disconnected.
*/
int handle_message(uint16_t id, uint8_t msg_type, char *data, int data_len,
socket_t *from, list_t *clients, fd_set *client_fds,
list_t *acls)
{
client_t *c = NULL;
client_t *c2 = NULL;
socket_t *tcp_sock = NULL;
int ret = 0;
if(id != 0)
{
c = list_get(clients, &id);
if(!c)
return -1;
}
if(id == 0 && msg_type != MSG_TYPE_HELLO)
return -2;
switch(msg_type)
{
case MSG_TYPE_GOODBYE:
ret = -2;
break;
/* Data in the hello message will be like "hostname port", possibly
without the null terminator. This will look for the space and
parse out the hostname or ip address and port number */
case MSG_TYPE_HELLO:
{
int i;
char port[6]; /* need this so port str can have null term. */
char src_addrstr[ADDRSTRLEN];
char dst_addrstr[ADDRSTRLEN];
uint16_t sport, dport;
uint16_t req_id;
if(id != 0)
break;
req_id = ntohs(*((uint16_t*)data));
data += sizeof(uint16_t);
data_len -= sizeof(uint16_t);
/* look for the space separating the host and port */
for(i = 0; i < data_len; i++)
if(data[i] == ' ')
break;
if(i == data_len)
break;
/* null terminate the host and get the port number to the string */
data[i++] = 0;
strncpy(port, data+i, data_len-i);
port[data_len-i] = 0;
/* Create an unconnected TCP socket for the remote host, the
client itself, add it to the list of clients */
tcp_sock = sock_create(data, port, ipver, SOCK_TYPE_TCP, 0, 0);
ERROR_GOTO(tcp_sock == NULL, "Error creating tcp socket", error);
c = client_create(next_client_id++, tcp_sock, from, 0);
sock_free(tcp_sock);
ERROR_GOTO(c == NULL, "Error creating client", error);
c2 = list_add(clients, c, 1);
ERROR_GOTO(c2 == NULL, "Error adding client to list", error);
sock_get_addrstr(CLIENT_UDP_SOCK(c2), src_addrstr,
sizeof(src_addrstr));
sock_get_addrstr(CLIENT_TCP_SOCK(c2), dst_addrstr,
sizeof(dst_addrstr));
sport = sock_get_port(CLIENT_UDP_SOCK(c2));
dport = sock_get_port(CLIENT_TCP_SOCK(c2));
for(i = 0; i < LIST_LEN(acls); i++)
{
ret = acl_action(list_get_at(acls, i), src_addrstr, sport,
dst_addrstr, dport);
if(ret == ACL_ACTION_ALLOW)
{
if(debug_level >= DEBUG_LEVEL2)
printf("Connection %s:%hu -> %s:%hu allowed\n",
src_addrstr, sport, dst_addrstr, dport);
break;
}
else if(ret == ACL_ACTION_DENY)
{
if(debug_level >= DEBUG_LEVEL2)
printf("Connection to %s:%hu -> %s:%hu denied\n",
src_addrstr, sport, dst_addrstr, dport);
msg_send_msg(from, next_client_id, MSG_TYPE_GOODBYE,
NULL, 0);
client_free(c);
return -2;
}
}
if(debug_level >= DEBUG_LEVEL1)
{
sock_get_str(CLIENT_UDP_SOCK(c2), src_addrstr,
sizeof(src_addrstr));
sock_get_str(CLIENT_TCP_SOCK(c2), dst_addrstr,
sizeof(dst_addrstr));
printf("New connection(%d): udp://%s -> tcp://%s\n",
CLIENT_ID(c2), src_addrstr, dst_addrstr);
}
/* Send the Hello ACK message if created client successfully */
client_send_helloack(c2, req_id);
client_reset_keepalive(c2);
client_free(c);
break;
}
/* Can connect to TCP connection once received the Hello ACK */
case MSG_TYPE_HELLOACK:
if(client_connect_tcp(c) != 0)
return -2;
client_got_helloack(c);
client_add_tcp_fd_to_set(c, client_fds);
break;
/* Resets the timeout of the client's keep alive time */
case MSG_TYPE_KEEPALIVE:
client_reset_keepalive(c);
break;
/* Receives the data it got from the UDP tunnel and sends it to the
TCP connection. */
case MSG_TYPE_DATA0:
case MSG_TYPE_DATA1:
ret = client_got_udp_data(c, data, data_len, msg_type);
if(ret == 0)
ret = client_send_tcp_data(c);
break;
/* Receives the ACK from the UDP tunnel to set the internal client
state. */
case MSG_TYPE_ACK0:
case MSG_TYPE_ACK1:
client_got_ack(c, msg_type);
break;
default:
ret = -1;
}
return ret;
error:
return -1;
}
void signal_handler(int sig)
{
switch(sig)
{
case SIGINT:
running = 0;
}
}