-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp-redirect.c
1050 lines (844 loc) · 41.4 KB
/
udp-redirect.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file udp-redirect.c
* @author Dan Podeanu <[email protected]>
* @version 1.0.0
*
* @section LICENSE
*
* 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 2 of
* the License, or (at your option) any later version.
*
* @section DESCRIPTION
*
* A simple and high performance UDP redirector.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <poll.h>
#include <string.h>
#include <math.h>
#include <netdb.h>
#include <time.h>
/**
* The udp-redirect version
*/
#define UDP_REDIRECT_VERSION "1.0.0"
/**
* The delay in seconds between displaying statistics
*/
#define STATISTICS_DELAY_SECONDS 60
/**
* The size of the network buffer used for receiving / sending packets
*/
#define NETWORK_BUFFER_SIZE 65535
/**
* @brief Readability: errno value for OK.
*/
#define EOK 0
/**
* Maximum known errno, used to ignore harmless sendto / recvfrom errors
*/
#define MAX_ERRNO 256
/**
* Initialize errno ignore set
*/
#define ERRNO_IGNORE_INIT(X) memset((X), 0, MAX_ERRNO * sizeof(unsigned char))
/**
* Set errno ignore boolean for a specific errno
*/
#define ERRNO_IGNORE_SET(X, Y) if ((Y) >= 0 && (Y) < MAX_ERRNO) (X)[(Y)] = 1
/**
* Check if errno is in declared set
*/
#define ERRNO_IGNORE_CHECK(X, Y) ((Y) >= 0 && (Y) < MAX_ERRNO && (X)[(Y)] == 1)
/**
* @brief The available debug levels.
*/
enum DEBUG_LEVEL {
DEBUG_LEVEL_ERROR = 0, ///< Error messages
DEBUG_LEVEL_INFO = 1, ///< Informational messages
DEBUG_LEVEL_VERBOSE = 2, ///< Verbose messages
DEBUG_LEVEL_DEBUG = 3 ///< Debug messages
};
/**
* Standard debug macro requiring a locally defined debug level.
* Adapted from the excellent https://github.com/jleffler/soq/blob/master/src/libsoq/debug.h
*
* Note that __VA_ARGS__ is a GCC extension; used for convenience to support DEBUG without format arguments.
*/
#define DEBUG(debug_level_local, debug_level_message, fmt, ...) \
do { \
if ((debug_level_local) >= (debug_level_message)) { \
fprintf(stderr, "%s:%d:%d:%s(): " fmt "\n", __FILE__, \
__LINE__, (int)(time(NULL)), __func__, ##__VA_ARGS__); \
} \
} while (0)
/**
* Command line options.
*/
static struct option longopts[] = {
{ "verbose", no_argument, NULL, 'v' }, ///< Verbose mode, can be specified multiple times (optional)
{ "debug", no_argument, NULL, 'd' }, ///< Debug mode (optional)
{ "listen-address", required_argument, NULL, 'a' }, ///< Listen address (optional)
{ "listen-port", required_argument, NULL, 'b' }, ///< Listen port (required)
{ "listen-interface", required_argument, NULL, 'c' }, ///< Listen interface (optional)
{ "connect-address", required_argument, NULL, 'g' }, ///< Connect address (required)
{ "connect-host", required_argument, NULL, 'h' }, ///< Connect host (required)
{ "connect-port", required_argument, NULL, 'i' }, ///< Connect port (required)
{ "send-address", required_argument, NULL, 'm' }, ///< Send packets address (optional)
{ "send-port", required_argument, NULL, 'n' }, ///< Send packets port (optional)
{ "send-interface", required_argument, NULL, 'o' }, ///< Send packets interface (optional)
{ "listen-address-strict", no_argument, NULL, 'x' }, ///< Listener only receives packets from the same endpoint
{ "connect-address-strict",no_argument, NULL, 'y' }, ///< Sender only receives packets from the connect address
{ "listen-sender-address", required_argument, NULL, 'k' }, ///< Connect expects packets from this source address
{ "listen-sender-port", required_argument, NULL, 'l' }, ///< Connect expects packets from this source port
{ "ignore-errors", no_argument, NULL, 'r' }, ///< Ignore harmless recvfrom / sendto errors (default)
{ "stop-errors", no_argument, NULL, 's' }, ///< Do NOT ignore harmless recvfrom / sendto errors
{ "stats", no_argument, NULL, 'q' }, ///< Display stats every 60 seconds
{ "version", no_argument, NULL, 'z' }, ///< Display the version
{ NULL, 0, NULL, 0 }
};
/**
* Store command line option values in one place.
*/
struct settings {
char *laddr; ///< Listen address
int lport; ///< Listen port
char *lif; ///< Listen interface
char *caddr; ///< Connect address
char *chost; ///< Connect host
int cport; ///< Connect port
char *saddr; ///< Send packets from address
int sport; ///< Send packets from port
char *sif; ///< Send packets from interface
int lstrict; ///< Strict mode for listener (set endpoint on first packet arrival)
int cstrict; ///< Strict mode for sender (only accept from caddr / cport)
char *lsaddr; ///< Listen port expects packets from this address
int lsport; ///< Listen port only expects packets from this port
int eignore; ///< Ignore most recvfrom / sendto errors
int stats; ///< Display stats every 60 seconds
};
/**
* Store and display statistics
*/
struct statistics {
time_t time_display_last;
time_t time_display_first;
unsigned long count_listen_packet_receive;
unsigned long count_listen_byte_receive;
unsigned long count_listen_packet_send;
unsigned long count_listen_byte_send;
unsigned long count_connect_packet_receive;
unsigned long count_connect_byte_receive;
unsigned long count_connect_packet_send;
unsigned long count_connect_byte_send;
unsigned long count_listen_packet_receive_total;
unsigned long count_listen_byte_receive_total;
unsigned long count_listen_packet_send_total;
unsigned long count_listen_byte_send_total;
unsigned long count_connect_packet_receive_total;
unsigned long count_connect_byte_receive_total;
unsigned long count_connect_packet_send_total;
unsigned long count_connect_byte_send_total;
};
/* Function prototypes */
int socket_setup(const int debug_level, const char *desc, const char *xaddr, const int xport, const char *xif, struct sockaddr_in *xsock_name);
char *resolve_host(int debug_level, const char *host);
void settings_initialize(struct settings *s);
void usage(const char *argv0, const char *message);
void statistics_initialize(struct statistics *st);
double int_to_human_value(double value);
char int_to_human_char(double value);
void statistics_display(int debug_level, struct statistics *st, time_t now);
/**
* Main program function.
*
* @param[in] argc The count of arguments, including the program name.
* @param[in] argv The program arguments
* @return The program return code.
*
*/
int main(int argc, char *argv[]) {
/* Store debug level and program name */
int debug_level = DEBUG_LEVEL_ERROR;
char *argv0 = argv[0];
int ch; /* Used by getopt_long */
/* Command line arguments */
struct settings s;
struct statistics st;
time_t now;
int lsock; /* Listen socket */
int ssock; /* Send socket */
struct sockaddr_in lsock_name; /* Listen socket name */
struct sockaddr_in ssock_name; /* Send socket name */
struct sockaddr_in caddr; /* Connect address */
/* Simplify inet_ntop usage in DEBUG() by reserving buffers to write output */
char print_buffer1[INET_ADDRSTRLEN];
char print_buffer2[INET_ADDRSTRLEN];
char network_buffer[NETWORK_BUFFER_SIZE]; /* The network buffer. All reads and writes happen here */
struct pollfd ufds[2]; /* Poll file descriptors */
struct sockaddr_in endpoint; /* Address where the current packet was received from */
struct sockaddr_in previous_endpoint; /* Address where the previous packet was received from */
unsigned char errno_ignore[MAX_ERRNO];
settings_initialize(&s);
statistics_initialize(&st);
while ((ch = getopt_long(argc, argv, "", longopts, NULL)) != -1) {
switch (ch) {
case 'v': /* --verbose */
if (debug_level < DEBUG_LEVEL_VERBOSE) {
debug_level = DEBUG_LEVEL_VERBOSE;
} else {
debug_level = debug_level + 1;
}
break;
case 'd': /* --debug */
debug_level = DEBUG_LEVEL_DEBUG;
break;
case 'a': /* --listen-address */
s.laddr = optarg;
break;
case 'b': /* --listen-port */
s.lport = atoi(optarg);
if (errno != EOK) {
perror("atoi");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Invalid listen port: %s (%d)", optarg, errno);
exit(EXIT_FAILURE);
}
break;
case 'c': /* --listeninterface */
s.lif = optarg;
break;
case 'g': /* --connect-address */
s.caddr = optarg;
break;
case 'h': /* --connect-host */
s.chost = optarg;
break;
case 'i': /* --connect-port */
s.cport = atoi(optarg);
if (errno != EOK) {
perror("atoi");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Invalid connect port: %s (%d)", optarg, errno);
exit(EXIT_FAILURE);
}
break;
case 'm': /* --send-address */
s.saddr = optarg;
break;
case 'n': /* --send-port */
s.sport = atoi(optarg);
if (errno != EOK) {
perror("atoi");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Invalid send port: %s (%d)", optarg, errno);
exit(EXIT_FAILURE);
}
break;
case 'o': /* --send-interface */
s.sif = optarg;
break;
case 'x': /* --listen-address-strict */
s.lstrict = 1;
break;
case 'y': /* --connect-address-strict */
s.cstrict = 1;
break;
case 'k': /* --listen-sender-address */
s.lsaddr = optarg;
break;
case 'l': /* --listen-sender-port */
s.lsport = atoi(optarg);
if (errno != EOK) {
perror("atoi");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Invalid send port: %s (%d)", optarg, errno);
exit(EXIT_FAILURE);
}
break;
case 'r': /* --ignore-errors */
s.eignore = 1;
break;
case 's': /* --stop-errors */
s.eignore = 0;
break;
case 'q': /* --stats */
s.stats = 1;
break;
case 'z': /* --version */
fprintf(stderr, "udp-redirect v%s\n", UDP_REDIRECT_VERSION);
exit(EXIT_SUCCESS);
break;
default:
usage(argv0, NULL);
break;
}
}
argc -= optind;
argv += optind;
if (argc != 0) {
usage(argv0, "Unknown argument");
}
if (s.lport == 0) {
usage(argv0, "Listen port not specified");
}
if (s.caddr == NULL && s.chost == NULL) {
usage(argv0, "Connect host or address not specified");
}
if (s.cport == 0) {
usage(argv0, "Connect port not specified");
}
if ((s.lsaddr != NULL && s.lsport == 0) ||
(s.lsaddr == NULL && s.lsport != 0)) {
usage(argv0, "Options --listen-sender-port and --list-sender-address must either both be specified or none");
}
/* Set strict mode if using lsport and csport */
if (s.lsaddr != NULL && s.lsport != 0) {
s.lstrict = 1;
}
/* Resolve connect host if available */
if (s.chost != NULL) {
s.caddr = resolve_host(debug_level, s.chost);
}
DEBUG(debug_level, DEBUG_LEVEL_INFO, "---- INFO ----");
// DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Debug level: %d", debug_level);
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Listen address: %s", (s.laddr != NULL)?s.laddr:"ANY");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Listen port: %d", s.lport);
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Listen interface: %s", (s.lif != NULL)?s.lif:"ANY");
if (s.chost != NULL) {
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Connect host: %s", s.chost);
}
if (s.caddr != NULL) {
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Connect address: %s", s.caddr);
}
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Connect port: %d", s.cport);
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Send address: %s", (s.saddr != NULL)?s.saddr:"ANY");
if (s.sport != 0) {
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Send port: %d", s.sport);
} else {
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Send port: %s", "ANY");
}
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Send interface: %s", (s.sif != NULL)?s.sif:"ANY");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Listen strict: %s", s.cstrict?"ENABLED":"DISABLED");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Connect strict: %s", s.lstrict?"ENABLED":"DISABLED");
if (s.lsaddr != NULL) {
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Listen only accepts packets from address: %s", s.lsaddr);
}
if (s.lsport != 0) {
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Listen only accepts packets from port: %d", s.lsport);
}
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Ignore errors: %s", s.eignore?"ENABLED":"DISABLED");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Display stats: %s", s.stats?"ENABLED":"DISABLED");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "---- START ----");
lsock = socket_setup(debug_level, "Listen", s.laddr, s.lport, s.lif, &lsock_name); /* Set up listening socket */
ssock = socket_setup(debug_level, "Send", s.saddr, s.sport, s.sif, &ssock_name); /* Set up send socket */
/* Set up connect address */
caddr.sin_family = AF_INET;
if ((caddr.sin_addr.s_addr = inet_addr(s.caddr)) == INADDR_NONE) {
perror("inet_addr");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Invalid connect address %s (%d)", s.caddr, errno);
exit(EXIT_FAILURE);
}
caddr.sin_port = htons(s.cport);
endpoint.sin_addr.s_addr = 0; /* No packet received, no endpoint */
previous_endpoint.sin_family = AF_INET;
if (s.lsaddr == NULL && s.lsport == 0) {
previous_endpoint.sin_addr.s_addr = 0; /* No packet received, no previous endpoint */
} else {
if ((previous_endpoint.sin_addr.s_addr = inet_addr(s.lsaddr)) == INADDR_NONE) {
perror("inet_addr");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Invalid listen packet address %s (%d)", s.lsaddr, errno);
exit(EXIT_FAILURE);
}
previous_endpoint.sin_port = htons(s.lsport);
}
int endpoint_len = sizeof(endpoint);
ERRNO_IGNORE_INIT(errno_ignore);
ERRNO_IGNORE_SET(errno_ignore, EINTR); /* Always ignore EINTR */
if (s.eignore == 1) { /* List of harmless recvfrom / sendto errors. Possibly incorrect. */
ERRNO_IGNORE_SET(errno_ignore, EAGAIN);
ERRNO_IGNORE_SET(errno_ignore, EHOSTUNREACH);
ERRNO_IGNORE_SET(errno_ignore, ENETDOWN);
ERRNO_IGNORE_SET(errno_ignore, ENETUNREACH);
ERRNO_IGNORE_SET(errno_ignore, ENOBUFS);
ERRNO_IGNORE_SET(errno_ignore, EPIPE);
ERRNO_IGNORE_SET(errno_ignore, EADDRNOTAVAIL);
}
DEBUG(debug_level, DEBUG_LEVEL_VERBOSE, "entering infinite loop");
st.time_display_first = time(NULL);
/* Main loop */
while (1) {
int poll_retval;
int recvfrom_retval;
int sendto_retval;
now = time(NULL);
ufds[0].fd = lsock; ufds[0].events = POLLIN | POLLPRI; ufds[0].revents = 0;
ufds[1].fd = ssock; ufds[1].events = POLLIN | POLLPRI; ufds[1].revents = 0;
DEBUG(debug_level, DEBUG_LEVEL_DEBUG, "waiting for readable sockets");
if (s.stats && (now - st.time_display_last) > STATISTICS_DELAY_SECONDS) {
statistics_display(debug_level, &st, now);
st.time_display_last = now;
}
if ((poll_retval = poll(ufds, 2, 1000)) == -1) {
if (errno == EINTR) {
continue;
}
perror("poll");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Could not check readable sockets (%d)", errno);
exit(EXIT_FAILURE);
}
if (poll_retval == 0) {
DEBUG(debug_level, DEBUG_LEVEL_DEBUG, "poll timeout");
continue;
}
/* New data on the LISTEN socket */
if (ufds[0].revents & POLLIN || ufds[0].revents & POLLPRI) {
if ((recvfrom_retval = recvfrom(lsock, network_buffer, sizeof(network_buffer), 0,
(struct sockaddr *)&endpoint, (socklen_t *)&endpoint_len)) == -1) {
if (!ERRNO_IGNORE_CHECK(errno_ignore, errno)) {
perror("recvfrom");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Listen cannot receive (%d)", errno);
exit(EXIT_FAILURE);
}
}
if (recvfrom_retval > 0) {
st.count_listen_packet_receive++;
st.count_listen_byte_receive += recvfrom_retval;
DEBUG(debug_level, DEBUG_LEVEL_DEBUG, "RECEIVE (%s, %d) -> (%s, %d) (LISTEN PORT): %d bytes",
inet_ntop(AF_INET, &(endpoint.sin_addr), print_buffer1, INET_ADDRSTRLEN), ntohs(endpoint.sin_port),
inet_ntop(AF_INET, &(lsock_name.sin_addr), print_buffer2, INET_ADDRSTRLEN), ntohs(lsock_name.sin_port),
recvfrom_retval);
/** Accept the packet IF:
* - There's no previous endpoint, OR
* - There is a previous endpoint, but we are not in strict mode, OR
* - The previous endpoint matches the current endpoint
*/
if ((previous_endpoint.sin_addr.s_addr == 0 || !s.lstrict) ||
(previous_endpoint.sin_addr.s_addr == endpoint.sin_addr.s_addr &&
previous_endpoint.sin_port == endpoint.sin_port)) {
if (previous_endpoint.sin_addr.s_addr == 0 || !s.lstrict) {
if (previous_endpoint.sin_addr.s_addr != endpoint.sin_addr.s_addr ||
previous_endpoint.sin_port != endpoint.sin_port) {
DEBUG(debug_level, DEBUG_LEVEL_DEBUG, "LISTEN remote endpoint set to (%s, %d)", inet_ntoa(endpoint.sin_addr), ntohs(endpoint.sin_port));
}
previous_endpoint.sin_addr.s_addr = endpoint.sin_addr.s_addr;
previous_endpoint.sin_port = endpoint.sin_port;
}
if ((sendto_retval = sendto(ssock, network_buffer, recvfrom_retval, 0,
(struct sockaddr *)&caddr, sizeof(caddr))) == -1) {
if (!ERRNO_IGNORE_CHECK(errno_ignore, errno)) {
perror("sendto");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot send packet to send port (%d)", errno);
exit(EXIT_FAILURE);
}
} else { // At least one byte was sent, record it
st.count_connect_packet_send++;
st.count_connect_byte_send += sendto_retval;
}
DEBUG(debug_level, (sendto_retval == recvfrom_retval || s.eignore == 1)?DEBUG_LEVEL_DEBUG:DEBUG_LEVEL_ERROR,
"SEND (%s, %d) -> (%s, %d) (SEND PORT): %d bytes (%s WRITE %d bytes)",
inet_ntop(AF_INET, &(ssock_name.sin_addr), print_buffer1, INET_ADDRSTRLEN), ntohs(ssock_name.sin_port),
inet_ntop(AF_INET, &(caddr.sin_addr), print_buffer2, INET_ADDRSTRLEN), ntohs(caddr.sin_port),
sendto_retval,
(sendto_retval == recvfrom_retval)?"FULL":"PARTIAL", recvfrom_retval);
} else {
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "LISTEN PORT invalid source (%s, %d), was expecting (%s, %d)",
inet_ntop(AF_INET, &(endpoint.sin_addr), print_buffer1, INET_ADDRSTRLEN), ntohs(endpoint.sin_port),
inet_ntop(AF_INET, &(previous_endpoint.sin_addr), print_buffer2, INET_ADDRSTRLEN), ntohs(previous_endpoint.sin_port));
}
}
}
/* New data on the SEND socket */
if (ufds[1].revents & POLLIN || ufds[1].revents & POLLPRI) {
if ((recvfrom_retval = recvfrom(ssock, network_buffer, sizeof(network_buffer), 0,
(struct sockaddr *)&endpoint, (socklen_t *)&endpoint_len)) == -1) {
if (!ERRNO_IGNORE_CHECK(errno_ignore, errno)) {
perror("recvfrom");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Send cannot receive packet (%d)", errno);
exit(EXIT_FAILURE);
}
}
if (recvfrom_retval > 0) {
st.count_connect_packet_receive++;
st.count_connect_byte_receive += recvfrom_retval;
DEBUG(debug_level, DEBUG_LEVEL_DEBUG, "RECEIVE (%s, %d) -> (%s, %d) (SEND PORT): %d bytes",
inet_ntop(AF_INET, &(endpoint.sin_addr), print_buffer1, INET_ADDRSTRLEN), ntohs(endpoint.sin_port),
inet_ntop(AF_INET, &(ssock_name.sin_addr), print_buffer2, INET_ADDRSTRLEN), ntohs(ssock_name.sin_port),
recvfrom_retval);
/** Accept the packet IF:
* - The listen socket has received a packet, so we know the endpoint, AND
* - The packet was received from the connect endpoint, OR
* - We are not in strict mode
*/
if (previous_endpoint.sin_addr.s_addr != 0 &&
(!s.cstrict || (caddr.sin_addr.s_addr == endpoint.sin_addr.s_addr && caddr.sin_port == endpoint.sin_port))) {
if ((sendto_retval = sendto(lsock, network_buffer, recvfrom_retval, 0,
(struct sockaddr *)&previous_endpoint, sizeof(previous_endpoint))) == -1) {
if (!ERRNO_IGNORE_CHECK(errno_ignore, errno)) {
perror("sendto");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Cannot send packet to listen port (%d)", errno);
exit(EXIT_FAILURE);
}
} else { // At least one byte was sent, record it
st.count_listen_packet_send++;
st.count_listen_byte_send += sendto_retval;
}
DEBUG(debug_level, (sendto_retval == recvfrom_retval || s.eignore == 1)?DEBUG_LEVEL_DEBUG:DEBUG_LEVEL_ERROR,
"SEND (%s, %d) -> (%s, %d) (LISTEN PORT): %d bytes (%s WRITE %d bytes)",
inet_ntop(AF_INET, &(lsock_name.sin_addr), print_buffer1, INET_ADDRSTRLEN), ntohs(lsock_name.sin_port),
inet_ntop(AF_INET, &(previous_endpoint.sin_addr), print_buffer2, INET_ADDRSTRLEN), ntohs(previous_endpoint.sin_port),
sendto_retval,
(sendto_retval == recvfrom_retval)?"FULL":"PARTIAL", recvfrom_retval);
} else {
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "SEND PORT invalid source (%s, %d), was expecting (%s, %d)",
inet_ntop(AF_INET, &(endpoint.sin_addr), print_buffer1, INET_ADDRSTRLEN), ntohs(endpoint.sin_port),
inet_ntop(AF_INET, &(caddr.sin_addr), print_buffer2, INET_ADDRSTRLEN), ntohs(caddr.sin_port));
}
}
}
}
/* Never reached. */
return 0;
}
/* Network helper functions below */
/**
* Creates a UDP socket on the specified address, port and interface, returning the socket and
* the socket name (if either arguments were NULL or 0).
*
* @param[in] debug_level The debug level to be used for the DEBUG() macro
* @param[in] desc The caller description, added to debug messages
* @param[in] xaddr The IPV4 address for the socket to be created, or NULL for INADDR_ANY
* @param[in] xport The IPV4 port for the socket to be created, or 0 for random (decided by bind())
* @param[in] xif The OS interface name to bind to, or NULL for all interfaces.
* @param[out] xsock_name The name of the socket created.
* @return The socket file descriptor as integer.
*
*/
int socket_setup(const int debug_level, const char *desc, const char *xaddr, const int xport, const char *xif, struct sockaddr_in *xsock_name) {
int xsock;
const int enable = 1;
struct sockaddr_in addr;
/* Set up listening socket */
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: create", desc);
if ((xsock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
perror("socket");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot create DGRAM socket (%d)", errno);
exit(EXIT_FAILURE);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
/* Address specified or any */
if (xaddr != NULL) {
if ((addr.sin_addr.s_addr = inet_addr(xaddr)) == INADDR_NONE) {
perror("inet_addr");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "%s address invalid %s (%d)", desc, xaddr, errno);
exit(EXIT_FAILURE);
}
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: bind to address %s", desc, xaddr);
} else {
addr.sin_addr.s_addr = INADDR_ANY;
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: bind to address %s", desc, "ANY");
}
/* Port specified or any */
if (xport != 0) {
addr.sin_port = htons(xport);
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: bind to port %d", desc, xport);
} else {
addr.sin_port = 0;
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: bind to port %s", desc, "ANY");
}
if (xif != NULL) {
#ifdef __MACH__
unsigned int xif_idx;
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: bind to interface %s", desc, xif);
if ((xif_idx = if_nametoindex(xif)) == 0) {
perror("if_nametoindex");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot get the interface ID (%d)", errno);
exit(EXIT_FAILURE);
}
if (setsockopt(xsock, IPPROTO_IP, IP_BOUND_IF, &xif_idx, sizeof(xif_idx)) == -1) {
perror("setsockopt");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot set socket interface (%d)", errno);
exit(EXIT_FAILURE);
}
#elif __unix__
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: bind to interface %s", desc, xif);
if (setsockopt(xsock, SOL_SOCKET, SO_BINDTODEVICE, xif, strlen(xif)) == -1) {
perror("setsockopt");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot set socket interface (%d)", errno);
exit(EXIT_FAILURE);
}
#endif
} else {
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: bind to interface %s", desc, "ANY");
}
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: reuse local address", desc);
if (setsockopt(xsock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
perror("setsockopt");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot set socket SO_REUSEADDR (%d)", errno);
exit(EXIT_FAILURE);
}
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: set nonblocking", desc);
if (fcntl(xsock, F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot set socket O_NONBLOCK (%d)", errno);
exit(EXIT_FAILURE);
}
DEBUG(debug_level, DEBUG_LEVEL_INFO, "%s socket: bind", desc);
if (bind(xsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("bind");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot bind socket (%d)", errno);
exit(EXIT_FAILURE);
}
socklen_t xsock_name_len = sizeof(*xsock_name);
if (getsockname(xsock, (struct sockaddr *)xsock_name, &xsock_name_len) == -1) {
perror("getsockname");
DEBUG(debug_level, DEBUG_LEVEL_ERROR, "Cannot get socket name (%d)", errno);
exit(EXIT_FAILURE);
}
return xsock;
}
/**
* Resolve a host to an IP address.
* @param[in] debug_level The debug level to be used for the DEBUG() macro
* @param[in] host The host to resolve
* @return A newly allocated buffer containing the IP.
*/
char *resolve_host(int debug_level, const char *host) {
struct hostent *host_info;
struct in_addr *address;
char *retval;
if ((host_info = gethostbyname(host)) == NULL) {
perror("gethostbyname");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Could not resolve host %s (%d)", host, errno);
exit(EXIT_FAILURE);
}
address = (struct in_addr *)(host_info->h_addr);
if ((retval = strdup(inet_ntoa(*address))) == NULL) {
perror("strdup");
DEBUG(debug_level, DEBUG_LEVEL_INFO, "Could not duplicate string (%d)", errno);
exit(EXIT_FAILURE);
}
DEBUG(debug_level, DEBUG_LEVEL_DEBUG, "Resolved %s to %s", host, strdup(inet_ntoa(*address)));
return retval;
}
/* Settings helper functions below */
/**
* Initialize settings.
* @param[out] s The settings structure to initialize.
*/
void settings_initialize(struct settings *s) {
s->laddr = NULL;
s->lport = 0;
s->lif = NULL;
s->caddr = NULL;
s->chost = NULL;
s->cport = 0;
s->saddr = NULL;
s->sport = 0;
s->sif = NULL;
s->lstrict = 0;
s->cstrict = 0;
s->lsaddr = NULL;
s->lsport = 0;
s->eignore = 1;
s->stats = 0;
}
/**
* Displays the program usage and exit with error.
*
* @param[in] argv0 The program name as started, e.g., /usr/local/bin/udp-redirect
* @param[in] message The error message, or NULL
*
*/
void usage(const char *argv0, const char *message) {
if (message != NULL)
fprintf(stderr, "%s\n", message);
fprintf(stderr, "Usage: %s\n", argv0);
fprintf(stderr, " [--listen-address <address>] --listen-port <port> [--listen-interface <interface>]\n");
fprintf(stderr, " [--connect-address <address> | --connect-host <hostname> --connect-port <port>\n");
fprintf(stderr, " [--send-address <address>] [--send-port <port>] [--send-interface <interface>]\n");
fprintf(stderr, " [--list-address-strict] [--connect-address-strict]\n");
fprintf(stderr, " [--lsten-sender-addr <address>] [--listen-sender-port <port>]\n");
fprintf(stderr, " [--ignore-errors] [--stop-errors]\n");
fprintf(stderr, " [--stats] [--verbose] [--debug] [--version]\n");
fprintf(stderr, "\n");
fprintf(stderr, "--stats Display sent/received bytes statistics every 60 seconds (optional)\n");
fprintf(stderr, "--verbose Verbose mode, can be specified multiple times (optional)\n");
fprintf(stderr, "--debug Debug mode (optional)\n");
fprintf(stderr, "--version Display the version and exit\n");
fprintf(stderr, "\n");
fprintf(stderr, "--listen-address <ipv4 address> Listen address (optional)\n");
fprintf(stderr, "--listen-port <port> Listen port (required)\n");
fprintf(stderr, "--listen-interface <interface> Listen interface name (optional)\n");
fprintf(stderr, "--listen-address-strict Only receive packets from the same source as the first packet (optional)\n");
fprintf(stderr, "\n");
fprintf(stderr, "--connect-address <ipv4 address> Connect address (required)\n");
fprintf(stderr, "--connect-host <hostname> Connect host, overwrites --connect-address if both are specified (required)\n");
fprintf(stderr, "--connect-port <port> Connect port (required)\n");
fprintf(stderr, "--connect-address-strict Only receive packets from --connect-address / --connect-port (optional)\n");
fprintf(stderr, "\n");
fprintf(stderr, "--send-address <ipv4 address> Send packets from address (optional)\n");
fprintf(stderr, "--send-port <port> Send packets from port (optional)\n");
fprintf(stderr, "--send-interface <interface> Send packets from interface (optional)\n");
fprintf(stderr, "\n");
fprintf(stderr, "--listen-sender-address <ipv4 address> Listen endpoint only accepts packets from this source address (optional)\n");
fprintf(stderr, "--listen-sender-port <port> Listen endpoint only accepts packets from this source port (optional)\n");
fprintf(stderr, " (must be set together, --listen-address-strict is implied)\n");
fprintf(stderr, "\n");
fprintf(stderr, "--ignore-errors Ignore most receive or send errors (unreachable, etc.) instead of exiting (optional) (default)\n");
fprintf(stderr, "--stop-errors Exit on most receive or send errors (unreachable, etc.) (optional)\n");
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
/* Statistics helper functions below */
/**
* Hardcoded printf format for a human readable value made of a float and a char
*/
#define HUMAN_READABLE_FORMAT "%.1lf%c"
/**
* Shortcut for the above, for shorter printf
*/
#define HRF HUMAN_READABLE_FORMAT
/**
* Hardcoded invocations of int to human readable, compatible with HUMAN_READABLE_FORMAT in printf
*/
#define HUMAN_READABLE(X) int_to_human_value((X)), int_to_human_char((X))
/**
* The human readable size suffixes
*/
#define HUMAN_READABLE_SIZES { ' ', 'K', 'M', 'G', 'T', 'P', 'E' }
/**
* The number of human readable size suffixes
*/
#define HUMAN_READABLE_SIZES_COUNT 7
/**
* Initialize statistics.
* @param[out] st The statistics structure to initialize.
*/
void statistics_initialize(struct statistics *st) {
st->time_display_last = 0;
st->time_display_first = 0;
st->count_listen_packet_receive = 0;
st->count_listen_byte_receive = 0;
st->count_listen_packet_send = 0;
st->count_listen_byte_send = 0;
st->count_connect_packet_receive = 0;
st->count_connect_byte_receive = 0;
st->count_connect_packet_send = 0;
st->count_connect_byte_send = 0;
st->count_listen_packet_receive_total = 0;
st->count_listen_byte_receive_total = 0;
st->count_listen_packet_send_total = 0;
st->count_listen_byte_send_total = 0;
st->count_connect_packet_receive_total = 0;
st->count_connect_byte_receive_total = 0;
st->count_connect_packet_send_total = 0;
st->count_connect_byte_send_total = 0;
}
/**
* Convert a value to human readable (i.e., 1500 = 1.5K). Divide by 1000, not 1024.
* @param[in] value The value to be converted
* @param[in] host The host to resolve
* @return The numeric portion of the human readable value.
*/
double int_to_human_value(double value) {
double dvalue = value;
int count = 0;
while (dvalue > 1000 && count < (HUMAN_READABLE_SIZES_COUNT - 1)) {
dvalue = dvalue / 1000;
count = count + 1;
}
return dvalue;
}
/**
* Convert a value to human readable (i.e., 1500 = 1.5K). Divide by 1000, not 1024.
* @param[in] value The value to be converted
* @param[in] host The host to resolve
* @return The character (K, M, G, etc.) portion of the human readable value.
*/
char int_to_human_char(double value) {
double dvalue = value;
int count = 0;
static const char human_readable_sizes[] = HUMAN_READABLE_SIZES;
while (dvalue > 1000 && count < (HUMAN_READABLE_SIZES_COUNT - 1)) {
dvalue = dvalue / 1000;
count = count + 1;
}
return human_readable_sizes[count];
}
/**
* Display the stored statistics
* @param[in] debug_level The debug level to be used for the DEBUG() macro
* @param[in] st The statistics structure
* @param[in] now The current time
*/
void statistics_display(int debug_level, struct statistics *st, time_t now) {
int time_delta = now - st->time_display_last;
int time_delta_total = now - st->time_display_first;
if (time_delta < 1)
time_delta = 1;
if (time_delta_total < 1)
time_delta_total = 1;
st->count_listen_packet_receive_total += st->count_listen_packet_receive;
st->count_listen_byte_receive_total += st->count_listen_byte_receive;
st->count_listen_packet_send_total += st->count_listen_packet_send;
st->count_listen_byte_send_total += st->count_listen_byte_send;
st->count_connect_packet_receive_total += st->count_connect_packet_receive;
st->count_connect_byte_receive_total += st->count_connect_byte_receive;
st->count_connect_packet_send_total += st->count_connect_packet_send;
st->count_connect_byte_send_total += st->count_connect_byte_send;
DEBUG(debug_level, DEBUG_LEVEL_INFO, "---- STATS %ds ----", STATISTICS_DELAY_SECONDS);