-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.c
2498 lines (2269 loc) · 93 KB
/
main.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
/*
******************************************************************
udpbroadcastrelay (c)2020 Martin Wasley <github.com/marjohn56> & Berto Furth <github.com/bertofurth>
udp-broadcast-relay-redux
Relays UDP broadcasts to other networks, forging
the sender address.
Copyright (c) 2017 Michael Morrison <github.com/sonicsnes>
Copyright (c) 2003 Joachim Breitner <[email protected]>
Copyright (C) 2002 Nathan O'Sullivan
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.
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.
******************************************************************
*/
#define MAXIFS 256
#define MAXCIDRACL 256
#define MAXMULTICAST 256
#define MAXBLOCKIDS 256
#define MAX_MSEARCH_FILTERS 64
#define MAX_MSEARCH_PROXY 256
#define MAX_LOCATOR_LISTENER 256
#define MAX_LOCATOR_PROXIES 256
#define MAX_REST_LISTENER 256
#define MAX_REST_PROXIES 256
#define MAX_PROXY_SOCKETS (MAX_MSEARCH_PROXY + MAX_LOCATOR_LISTENER + MAX_LOCATOR_PROXIES + MAX_REST_LISTENER + MAX_REST_PROXIES)
/*
* MAXID used to be 99 when TTL was marked with the ID but
* now that DSCP is used it needs to be limited to 6 bits.
*/
#define MAXID 63
#define DPRINT if (debug) printf
#define DPRINT2 if (debug>=2) printf
#define DPRINTTIME if (debug>=2) printtime()
#define IPHEADER_LEN 20
#define UDPHEADER_LEN 8
#define HEADER_LEN (IPHEADER_LEN + UDPHEADER_LEN)
#define TTL_ID_OFFSET 64
#define SIGF_TERM 0x1
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#ifdef __FreeBSD__
#include <net/if.h>
#include <net/if_dl.h>
#else
#include <linux/if.h>
#endif
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <poll.h>
#include <strings.h>
#include <netdb.h>
#include <fcntl.h>
static int debug = 0;
static char g_pidfile[128];
/* list of addresses and interface numbers on local machine */
struct Iface {
struct in_addr dstaddr;
struct in_addr ifaddr;
char* ifname;
int ifindex;
int raw_socket;
};
static struct Iface ifs[MAXIFS];
static int maxifs = 0;
#define ACTION_BLOCK 0
#define ACTION_ALLOW 1
/* list of CIDR ACL entries */
struct CIDRACL {
struct in_addr network;
struct in_addr mask;
u_short numbits;
u_short action;
};
static struct CIDRACL CIDRs[MAXCIDRACL];
static int numCIDRs = 0;
static int defaultCIDRaction = ACTION_ALLOW;
/* Where we forge our packets */
static u_char gram[4096+HEADER_LEN]=
{
0x45, 0x00, 0x00, 0x26,
0x12, 0x34, 0x00, 0x00,
0xFF, 0x11, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0x00, 0x12, 0x00, 0x00,
'1','2','3','4','5','6','7','8','9','0'
};
static u_short fragmentID = 0;
/* types of sockets we receive data on */
#define MAIN_SOCKET 0
#define MSEARCH_SOCKET 1
#define LOCSVC_LISTENER 2
#define LOCSVC_CIENTSOCK 3
#define LOCSVC_SERVERSOCK 4
#define RESTSVC_LISTENER 5
#define RESTSVC_CIENTSOCK 6
#define RESTSVC_SERVERSOCK 7
#define MSEARCH_PROXY_EXPIRY 60
#define LOCATOR_LISTENER_EXPIRY 86400
#define REST_LISTENER_EXPIRY 86400
#define BLOCK_RETRY_TIME 10
#define MSEARCH_MARKER "M-SEARCH * HTTP/1.1\r\n"
#define NOTIFY_MARKER "NOTIFY * HTTP/1.1\r\n"
#define LOCATION_STRING_PREFIX "LOCATION: http://"
#define APPLICATION_STRING_PREFIX "Application-URL: http://"
#define MSEARCH_ACTION_FORWARD 1 // Forward M-SEARCH just like other UDP packets
#define MSEARCH_ACTION_BLOCK 2 // Drop M-SEARCH requests with this search string
#define MSEARCH_ACTION_PROXY 3 // Proxy M-SEARCH request an response via udpbroadcastrelay without modifying packet data
#define MSEARCH_ACTION_DIAL 10 // Full DIAL protocol processing proxy
/* Define an individual M-SEARCH filter */
struct MSEARCHFilter {
char* searchstring;
int action;
};
static struct MSEARCHFilter msearch_filters[MAX_MSEARCH_FILTERS];
static int num_msearch_filters= 0;
static int default_msearch_action= MSEARCH_ACTION_FORWARD;
/* list of SSDP M-SEARCH reply listener proxies */
struct MSEARCHProxy {
time_t expirytime;
unsigned int proxyid;
struct in_addr clienthost;
u_short clientport;
struct Iface* clientiface;
u_short localport;
int action;
int sock;
};
static struct MSEARCHProxy msearch_proxies[MAX_MSEARCH_PROXY];
static int num_msearch_proxies= 0;
static unsigned int next_msearch_proxyid= 0;
/* list of DIAL Locator service listners */
struct LocatorSvcListener {
time_t expirytime;
unsigned int listenerid;
struct in_addr serveraddr;
u_short serverport;
struct in_addr localaddr;
u_short localport;
int sock;
};
static struct LocatorSvcListener locatorsvc_listeners[MAX_LOCATOR_LISTENER];
static int num_locatorsvc_listeners= 0;
static unsigned int next_locatorsvc_listenerid= 0;
/* list of DIAL Locator service proxies */
struct LocatorSvcProxy {
unsigned int proxyid;
int clientsock;
int serversock;
struct in_addr serveraddr;
u_short serverport;
struct in_addr clientaddr;
u_short clientport;
struct in_addr slocaladdr;
u_short slocalport;
struct in_addr clocaladdr;
u_short clocalport;
};
static struct LocatorSvcProxy locatorsvc_proxies[MAX_LOCATOR_PROXIES];
static int num_locatorsvc_proxies= 0;
static unsigned int next_locatorsvc_proxyid= 0;
/* list of REST service listners */
struct RESTSvcListener {
time_t expirytime;
unsigned int listenerid;
struct in_addr serveraddr;
u_short serverport;
struct in_addr localaddr;
u_short localport;
int sock;
};
static struct RESTSvcListener restsvc_listeners[MAX_REST_LISTENER];
static int num_restsvc_listeners= 0;
static unsigned int next_restsvc_listenerid= 0;
/* list of REST service proxies */
struct RESTSvcProxy {
unsigned int proxyid;
int clientsock;
int serversock;
struct in_addr serveraddr;
u_short serverport;
struct in_addr clientaddr;
u_short clientport;
struct in_addr slocaladdr;
u_short slocalport;
struct in_addr clocaladdr;
u_short clocalport;
};
static struct RESTSvcProxy restsvc_proxies[MAX_REST_PROXIES];
static int num_restsvc_proxies= 0;
static unsigned int next_restsvc_proxyid= 0;
char* get_msearch_action_name (int action)
{
switch (action) {
case MSEARCH_ACTION_FORWARD:
return "FORWARD";
case MSEARCH_ACTION_BLOCK:
return "BLOCK";
case MSEARCH_ACTION_PROXY:
return "PROXY";
case MSEARCH_ACTION_DIAL:
return "DIAL";
}
return "<unknown>";
}
char ifname_buf[64];
char* ifname_from_idx (int ifindex)
{
for (int i=0; i<maxifs; i++)
if (ifs[i].ifindex == ifindex)
return ifs[i].ifname;
// shouldn't happen...
snprintf(ifname_buf, sizeof(ifname_buf), "Idx_%i", ifindex);
return ifname_buf;
}
/* CIDR ACL compare function to qsort with descending mask size */
int CIDRcompare (const void*a, const void* b)
{
return ((const struct CIDRACL*) b)->numbits - ((const struct CIDRACL*) a)->numbits;
}
/* Parse CIDR string into a network number and mask. Return 0 if CIDR string is invalid */
int parse_cidr(char *str, struct CIDRACL* CIDR)
{
char buf[256];
char *s;
int numbits;
strncpy(buf, str, sizeof(buf));
s = strchr(buf, '/');
if (!s) {
return 0;
}
*(s++) = 0;
if (!inet_aton(buf, &(CIDR->network))) {
return 0;
}
numbits = atoi(s);
if ((numbits < 0) || (numbits > 32)) {
return 0;
}
CIDR->numbits = numbits;
CIDR->mask.s_addr = htonl((0xFFFFFFFFUL << (32 - numbits)) & 0xFFFFFFFFUL);
CIDR->network.s_addr &= CIDR->mask.s_addr;
return 1;
}
void inet_ntoa2(struct in_addr in, char* chr, int len) {
char* from = inet_ntoa(in);
strncpy(chr, from, len);
}
/* Check if packet should be blocked based on source IP. Return 0 to block packet */
int check_cidr_acl(struct in_addr fromAddress)
{
int i;
int action = defaultCIDRaction;
for (i = 0; i < numCIDRs; i++) {
if ((fromAddress.s_addr & CIDRs[i].mask.s_addr) == CIDRs[i].network.s_addr) {
action = CIDRs[i].action;
break;
}
}
if (action == ACTION_BLOCK) {
if (i < numCIDRs) {
char network[255];
inet_ntoa2(CIDRs[i].network, network, sizeof(network));
DPRINT("Source IP matches blocked CIDR %s/%i. Packet Ignored.\n\n", network, CIDRs[i].numbits);
} else {
DPRINT("Source IP does not match any allowed CIDR range. Packet Ignored.\n\n");
}
return 0;
}
return 1;
}
// Print formatted current time for log
void printtime (void)
{
struct timeval tv;
time_t now;
long millisec;
struct tm* tm;
if (gettimeofday(&tv,NULL) == -1)
return;
now = tv.tv_sec;
tm = localtime(&now);
millisec = tv.tv_usec / 1000;
printf("%04i/%02i/%02i %02i:%02i:%02i.%03i ", tm->tm_year + 1900, tm->tm_mon + 1,
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int) millisec);
}
// Set socket options to receive TTL, TOS, receiving IP and interface for a socket.
// Return 0 on errror.
int enable_recvmsg_headers (int s, char *callername)
{
int yes = 1;
#ifdef __FreeBSD__
if(setsockopt(s, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes))<0) {
fprintf(stderr,"IP_RECVTTL (%s): %s\n",callername,strerror(errno));
return 0;
};
if(setsockopt(s, IPPROTO_IP, IP_RECVTOS, &yes, sizeof(yes))<0) {
fprintf(stderr,"IP_RECVTOS (%s): %s\n",callername,strerror(errno));
return 0;
};
if(setsockopt(s, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes))<0) {
fprintf(stderr,"IP_RECVIF (%s): %s\n",callername,strerror(errno));
return 0;
};
if(setsockopt(s, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes))<0) {
fprintf(stderr,"IP_RECVDSTADDR (%s): %s\n",callername,strerror(errno));
return 0;
};
#else
if(setsockopt(s, SOL_IP, IP_RECVTTL, &yes, sizeof(yes))<0) {
fprintf(stderr,"IP_RECVTTL (%s): %s\n",callername,strerror(errno));
return 0;
};
if(setsockopt(s, SOL_IP, IP_RECVTOS, &yes, sizeof(yes))<0) {
fprintf(stderr,"IP_RECVTOS (%s): %s\n",callername,strerror(errno));
return 0;
};
if(setsockopt(s, SOL_IP, IP_PKTINFO, &yes, sizeof(yes))<0) {
fprintf(stderr,"IP_PKTINFO (%s): %s\n",callername,strerror(errno));
return 0;
};
#endif
return 1;
}
// Receive message on socket and return address info. Also check that
// packet was received on a managed interface. to_port is passed in
// for debug print info. Returns number of bytes received or <0 for error
int recv_with_addrinfo (int s, void *buf, size_t buflen, struct Iface **iface_out,
struct in_addr *from_inaddr_out, u_short *from_port_out,
struct in_addr *to_inaddr_out, u_short to_port)
{
struct in_addr from_inaddr;
struct in_addr to_inaddr;
u_short from_port;
struct sockaddr_in rcv_addr;
struct msghdr rcv_msg;
struct iovec iov;
iov.iov_base = buf;
iov.iov_len = buflen;
u_char pkt_infos[16384];
int len;
rcv_msg.msg_name = &rcv_addr;
rcv_msg.msg_namelen = sizeof(rcv_addr);
rcv_msg.msg_iov = &iov;
rcv_msg.msg_iovlen = 1;
rcv_msg.msg_control = pkt_infos;
rcv_msg.msg_controllen = sizeof(pkt_infos);
len = recvmsg(s,&rcv_msg,0);
if (len <= 0) return len; /* ignore broken packets */
from_inaddr = rcv_addr.sin_addr;
from_port = ntohs(rcv_addr.sin_port);
/* Find the receiving interface and IP address */
struct cmsghdr *cmsg;
int rcv_ifindex = 0;
int foundRcvIf = 0;
int foundRcvIp = 0;
if (rcv_msg.msg_controllen > 0) {
for (cmsg=CMSG_FIRSTHDR(&rcv_msg);cmsg;cmsg=CMSG_NXTHDR(&rcv_msg,cmsg)) {
#ifdef __FreeBSD__
if (cmsg->cmsg_type==IP_RECVDSTADDR) {
to_inaddr=*((struct in_addr *)CMSG_DATA(cmsg));
foundRcvIp = 1;
}
if (cmsg->cmsg_type==IP_RECVIF) {
rcv_ifindex=((struct sockaddr_dl *)CMSG_DATA(cmsg))->sdl_index;
foundRcvIf = 1;
}
#else
if (cmsg->cmsg_type==IP_PKTINFO) {
rcv_ifindex=((struct in_pktinfo *)CMSG_DATA(cmsg))->ipi_ifindex;
foundRcvIf = 1;
to_inaddr=((struct in_pktinfo *)CMSG_DATA(cmsg))->ipi_addr;
foundRcvIp = 1;
}
#endif
}
}
if (!foundRcvIp) {
perror("Source IP not found on incoming packet\n");
return -2;
}
if (!foundRcvIf) {
perror("Interface not found on incoming packet\n");
return -2;
}
char from_addrstr[255];
inet_ntoa2(from_inaddr, from_addrstr, sizeof(from_addrstr));
char to_addrstr[255];
inet_ntoa2(to_inaddr, to_addrstr, sizeof(to_addrstr));
DPRINTTIME;
DPRINT("<- [ %s:%d -> %s:%d (iface=%s len=%i)\n",
from_addrstr, from_port, to_addrstr, to_port,
ifname_from_idx(rcv_ifindex), len
);
foundRcvIf = 0;
for (int iIf = 0; iIf < maxifs; iIf++) {
if (ifs[iIf].ifindex == rcv_ifindex) {
if (iface_out) *iface_out = &ifs[iIf];
foundRcvIf = 1;
}
}
if (!foundRcvIf) {
DPRINT("Not from managed iface\n\n");
return -3;
}
if (numCIDRs) {
if (!check_cidr_acl(from_inaddr))
return -4;
}
if (from_inaddr_out) *from_inaddr_out = from_inaddr;
if (from_port_out) *from_port_out = from_port;
if (to_inaddr_out) *to_inaddr_out = to_inaddr;
return len;
}
// Bind socket to 0.0.0.0:0 and return the local port number associated with
// the socket or return 0 on error.
u_short get_sock_local_port(int s, in_addr_t localip, char *callername)
{
struct sockaddr_in bind_addr;
struct sockaddr_in local_addr;
socklen_t local_addr_size = sizeof(local_addr);
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = 0;
bind_addr.sin_addr.s_addr = localip;
if(bind(s, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) {
fprintf(stderr,"bind (%s): %s\n",callername,strerror(errno));
return 0;
}
if(getsockname(s, (struct sockaddr *)&local_addr, &local_addr_size)<0) {
fprintf(stderr,"getsockname (%s): %s\n",callername,strerror(errno));
return 0;
}
return ntohs(local_addr.sin_port);
}
int extract_address (char *str, char *prefix, char **addr_start_ptr, char **addr_end_ptr,
struct in_addr *ipaddr, u_short *port)
{
char *startptr = str;
char *termptr = NULL;
int prefixlen = strlen(prefix);
char addrstr[64];
int addrlen;
while (*startptr) {
if (!strncasecmp(startptr,prefix,prefixlen)) {
break;
}
startptr++;
}
if (!*startptr) {
return 0;
}
startptr += prefixlen;
termptr = strchr(startptr,'/');
if (!termptr) {
return 0;
}
addrlen = termptr-startptr;
if (addrlen>=sizeof(addrstr)) {
return 0;
}
*addr_start_ptr = startptr;
*addr_end_ptr = termptr;
memcpy(addrstr,startptr,addrlen);
addrstr[addrlen] = 0;
termptr = strchr(addrstr,':');
*port = 80; // default if no port was found
if (termptr) {
*termptr = 0;
*port = atoi(termptr+1);
}
struct addrinfo hints;
struct addrinfo *results;
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(addrstr, NULL, &hints, &results) != 0) {
perror("getaddrinfo");
return 0;
}
*ipaddr = ((struct sockaddr_in*)(results->ai_addr))->sin_addr;
freeaddrinfo(results);
return 1;
}
u_short find_or_create_restsvc_listener(struct in_addr servertoaddr, u_short servertoport, struct in_addr listenaddr)
{
char serveraddrStr[255];
char localaddrStr[255];
time_t now = time(NULL);
int i= 0;
// Look for an existing proxy for this destination host ip:port
while (i < num_restsvc_listeners) {
if (restsvc_listeners[i].expirytime < now) {
inet_ntoa2(restsvc_listeners[i].serveraddr, serveraddrStr, sizeof(serveraddrStr));
inet_ntoa2(restsvc_listeners[i].localaddr, localaddrStr, sizeof(localaddrStr));
DPRINT2(" _Expire REST_Svc listener [id=%u] for proxy to %s:%d on local address %s:%d. %d proxies left\n",
restsvc_listeners[i].listenerid,
serveraddrStr, restsvc_listeners[i].serverport,
localaddrStr, restsvc_listeners[i].localport,
num_restsvc_listeners-1
);
close(restsvc_listeners[i].sock);
memcpy(restsvc_listeners+i, restsvc_listeners+(--num_restsvc_listeners), sizeof(*restsvc_listeners));
continue;
}
if( (restsvc_listeners[i].serveraddr.s_addr == servertoaddr.s_addr) &&
(restsvc_listeners[i].serverport == servertoport) &&
(restsvc_listeners[i].localaddr.s_addr == listenaddr.s_addr)) {
inet_ntoa2(restsvc_listeners[i].serveraddr, serveraddrStr, sizeof(serveraddrStr));
inet_ntoa2(restsvc_listeners[i].localaddr, localaddrStr, sizeof(localaddrStr));
DPRINT(" Found existing REST_Svc listener [id=%u] for proxy to %s:%d on local address %s:%d.\n",
restsvc_listeners[i].listenerid,
serveraddrStr, restsvc_listeners[i].serverport,
localaddrStr, restsvc_listeners[i].localport
);
restsvc_listeners[i].expirytime = now+REST_LISTENER_EXPIRY; // Update expiry time
return restsvc_listeners[i].localport;
}
i++;
}
// Add new proxy because there is no existing match
if(num_restsvc_listeners == MAX_REST_LISTENER) {
DPRINT("Can't add new REST_Svc listener - maximum number of listeners reached\n\n");
return 0;
}
int newsock;
u_short localport;
if((newsock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket (REST_Svc listener)");
return 0;
};
if ((localport = get_sock_local_port(newsock, listenaddr.s_addr,"LocatorSvc listener")) == 0) {
close(newsock);
return 0;
}
if(listen(newsock, 3) < 0) {
perror("listen (REST_Svc listener)");
close(newsock);
return 0;
}
restsvc_listeners[num_restsvc_listeners].expirytime = now+REST_LISTENER_EXPIRY;
restsvc_listeners[num_restsvc_listeners].listenerid = next_restsvc_listenerid++;
restsvc_listeners[num_restsvc_listeners].serveraddr = servertoaddr;
restsvc_listeners[num_restsvc_listeners].serverport = servertoport;
restsvc_listeners[num_restsvc_listeners].sock = newsock;
restsvc_listeners[num_restsvc_listeners].localaddr = listenaddr;
restsvc_listeners[num_restsvc_listeners].localport = localport;
num_restsvc_listeners++;
inet_ntoa2(servertoaddr, serveraddrStr, sizeof(serveraddrStr));
inet_ntoa2(listenaddr, localaddrStr, sizeof(localaddrStr));
DPRINT(" Created REST_Svc listener [id=%u] for proxy to %s:%d on local address %s:%d. Total proxies: %d\n",
restsvc_listeners[num_restsvc_listeners-1].listenerid,
serveraddrStr, servertoport,
localaddrStr, localport, num_restsvc_listeners
);
return localport;
}
void handle_rest_services_accept (int listerneridx)
{
int serversock;
int clientsock;
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
socklen_t clientaddrlen = sizeof(clientaddr);
char clientaddrStr[255];
char serveraddrStr[255];
if ((clientsock = accept(restsvc_listeners[listerneridx].sock, (struct sockaddr *)&clientaddr,
&clientaddrlen)) < 0)
{
perror("accept (REST_Svc proxy)");
return;
}
int flags;
if ((flags = fcntl(clientsock, F_GETFL, 0)) < 0) {
flags = 0;
}
if (fcntl(clientsock, F_SETFL, flags | O_NONBLOCK) < 0)
{
perror("accept (REST_Svc proxy)");
}
inet_ntoa2(clientaddr.sin_addr, clientaddrStr, sizeof(clientaddrStr));
DPRINT("REST_Svc proxy - accepted connection from client %s:%d\n",
clientaddrStr, clientaddr.sin_port
);
if (num_restsvc_proxies==MAX_REST_PROXIES) {
DPRINT("... closing connection. No free proxy slots\n");
close(clientsock);
return;
}
if ((serversock = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP)) < 0) {
perror("socket (REST_Svc proxy)");
close(clientsock);
return;
}
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(restsvc_listeners[listerneridx].serverport);
serveraddr.sin_addr = restsvc_listeners[listerneridx].serveraddr;
if (connect(serversock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {
if (errno==EINPROGRESS) {
// Give the connection 500ms time to complete
struct pollfd fds[1];
fds[0].fd = serversock;
fds[0].events = POLLOUT;
if (poll(fds,1,500)<1) {
perror("poll (REST_Svc proxy)");
close(serversock);
close(clientsock);
DPRINT("... closing connection. Connection timeout to peer\n");
return;
}
if (!(fds[0].revents & POLLOUT)) {
close(serversock);
close(clientsock);
DPRINT("... closing connection. Connection to peer not ready for writing\n");
return;
}
} else {
perror("connect (REST_Svc proxy)");
close(serversock);
close(clientsock);
DPRINT("... closing connection. Connection error to peer\n");
return;
}
}
// Get local addresses
struct sockaddr_in lserveraddr;
struct sockaddr_in lclientaddr;
socklen_t addrsize = sizeof(lserveraddr);
memset (&lserveraddr, 0, sizeof(lserveraddr));
if (getsockname(serversock, (struct sockaddr *)&lserveraddr, &addrsize) < 0) {
perror("getsockname (REST_Svc proxy lserveraddr)");
}
memset (&lclientaddr, 0, sizeof(lclientaddr));
if (getsockname(clientsock, (struct sockaddr *)&lclientaddr, &addrsize) < 0) {
perror("getsockname (REST_Svc proxy lclientaddr)");
}
// scavange old, closed proxy list entries
int i= 0;
while (i < num_restsvc_proxies) {
if (restsvc_proxies[i].clientsock < 0) {
inet_ntoa2(restsvc_proxies[i].clientaddr, clientaddrStr, sizeof(clientaddrStr));
inet_ntoa2(restsvc_proxies[i].serveraddr, serveraddrStr, sizeof(serveraddrStr));
DPRINT2(" _Scavange REST_Svc proxy [id=%u] for client %s:%d to server %s:%d. %d proxies left\n",
restsvc_proxies[i].proxyid,
clientaddrStr, restsvc_proxies[i].clientport,
serveraddrStr, restsvc_proxies[i].serverport,
num_restsvc_proxies-1
);
memcpy(restsvc_proxies+i, restsvc_proxies+(--num_restsvc_proxies), sizeof(*restsvc_proxies));
continue;
}
i++;
}
// add to proxy list
restsvc_proxies[num_restsvc_proxies].proxyid = next_restsvc_proxyid++;
restsvc_proxies[num_restsvc_proxies].clientsock = clientsock;
restsvc_proxies[num_restsvc_proxies].serversock = serversock;
restsvc_proxies[num_restsvc_proxies].serveraddr = restsvc_listeners[listerneridx].serveraddr;
restsvc_proxies[num_restsvc_proxies].serverport = restsvc_listeners[listerneridx].serverport;
restsvc_proxies[num_restsvc_proxies].clientaddr = clientaddr.sin_addr;
restsvc_proxies[num_restsvc_proxies].clientport = ntohs(clientaddr.sin_port);
restsvc_proxies[num_restsvc_proxies].slocaladdr = lserveraddr.sin_addr;
restsvc_proxies[num_restsvc_proxies].slocalport = ntohs(lserveraddr.sin_port);
restsvc_proxies[num_restsvc_proxies].clocaladdr = lclientaddr.sin_addr;
restsvc_proxies[num_restsvc_proxies].clocalport = ntohs(lclientaddr.sin_port);
num_restsvc_proxies++;
inet_ntoa2(clientaddr.sin_addr, clientaddrStr, sizeof(clientaddrStr));
inet_ntoa2(restsvc_listeners[listerneridx].serveraddr, serveraddrStr, sizeof(serveraddrStr));
DPRINT(" Added LocatorSvc proxy [id=%u] for client %s:%d to server %s:%d. Total proxies: %d\n",
restsvc_proxies[num_restsvc_proxies-1].proxyid,
clientaddrStr, restsvc_proxies[num_restsvc_proxies-1].clientport,
serveraddrStr, restsvc_proxies[num_restsvc_proxies-1].serverport,
num_restsvc_proxies
);
}
void handle_restsvc_proxy_recv (int proxyidx, int socktype)
{
int fromsock, tosock;
struct in_addr fromaddr;
struct in_addr fromlocaladdr;
struct in_addr toaddr;
struct in_addr tolocaladdr;
u_short fromport;
u_short fromlocalport;
u_short toport;
u_short tolocalport;
char toaddrStr[255];
char fromaddrStr[255];
char localaddrStr[255];
if (socktype==RESTSVC_CIENTSOCK) {
fromsock = restsvc_proxies[proxyidx].clientsock;
tosock = restsvc_proxies[proxyidx].serversock;
fromaddr = restsvc_proxies[proxyidx].clientaddr;
tolocaladdr = restsvc_proxies[proxyidx].clocaladdr;
toaddr = restsvc_proxies[proxyidx].serveraddr;
fromlocaladdr = restsvc_proxies[proxyidx].slocaladdr;
fromport = restsvc_proxies[proxyidx].clientport;
tolocalport = restsvc_proxies[proxyidx].clocalport;
toport = restsvc_proxies[proxyidx].serverport;
fromlocalport = restsvc_proxies[proxyidx].slocalport;
} else {
fromsock = restsvc_proxies[proxyidx].serversock;
tosock = restsvc_proxies[proxyidx].clientsock;
fromaddr = restsvc_proxies[proxyidx].serveraddr;
tolocaladdr = restsvc_proxies[proxyidx].slocaladdr;
toaddr = restsvc_proxies[proxyidx].clientaddr;
fromlocaladdr = restsvc_proxies[proxyidx].clocaladdr;
fromport = restsvc_proxies[proxyidx].serverport;
tolocalport = restsvc_proxies[proxyidx].slocalport;
toport = restsvc_proxies[proxyidx].clientport;
fromlocalport = restsvc_proxies[proxyidx].clocalport;
}
char buffer[32768];
int numread;
int numwritten;
numread = recv (fromsock, buffer, sizeof(buffer), 0);
inet_ntoa2(fromaddr, fromaddrStr, sizeof(fromaddrStr));
inet_ntoa2(tolocaladdr, localaddrStr, sizeof(localaddrStr));
DPRINTTIME;
DPRINT("<- TCP [ %s:%d -> %s:%d (len=%i)] to REST_Svc proxy [id=%u]\n",
fromaddrStr, fromport, localaddrStr, tolocalport,
numread, restsvc_proxies[proxyidx].proxyid
);
if (numread<=0) {
DPRINT2((numread==0)?
" %s connection gracefully closed. Shutting down REST_Svc proxy [id=%u].\n":
" Error reading %s socket. Shutting down REST_Svc proxy [id=%u].\n\n",
socktype==RESTSVC_CIENTSOCK?"Client":"Server", restsvc_proxies[proxyidx].proxyid
);
close(restsvc_proxies[proxyidx].clientsock);
close(restsvc_proxies[proxyidx].serversock);
restsvc_proxies[proxyidx].clientsock = -1;
restsvc_proxies[proxyidx].serversock = -1;
return;
}
numwritten = send(tosock, buffer, numread, 0);
if (numwritten < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
inet_ntoa2(toaddr, toaddrStr, sizeof(toaddrStr));
DPRINT(" REST_Svc proxy [id=%u] would block sending to %s:%d. Retrying in %dms\n",
restsvc_proxies[proxyidx].proxyid, toaddrStr, toport, BLOCK_RETRY_TIME);
struct pollfd fds[1];
fds[0].fd = tosock;
fds[0].events = POLLOUT;
poll(fds,1,BLOCK_RETRY_TIME);
numwritten = send(tosock, buffer, numread, 0);
if (numwritten < 0) {
perror("send (REST_Svc proxy retry)");
DPRINT(" REST_Svc proxy [id=%u] would block still block. Shutting down proxy\n",
restsvc_proxies[proxyidx].proxyid);
close(restsvc_proxies[proxyidx].clientsock);
close(restsvc_proxies[proxyidx].serversock);
restsvc_proxies[proxyidx].clientsock = -1;
restsvc_proxies[proxyidx].serversock = -1;
return;
}
} else {
perror("send (REST_Svc proxy)");
inet_ntoa2(toaddr, toaddrStr, sizeof(toaddrStr));
DPRINT(" REST_Svc proxy [id=%u] error sending to %s:%d. Shutting down proxy\n",
restsvc_proxies[proxyidx].proxyid, toaddrStr, toport);
close(restsvc_proxies[proxyidx].clientsock);
close(restsvc_proxies[proxyidx].serversock);
restsvc_proxies[proxyidx].clientsock = -1;
restsvc_proxies[proxyidx].serversock = -1;
return;
}
}
inet_ntoa2(fromlocaladdr, localaddrStr, sizeof(localaddrStr));
inet_ntoa2(toaddr, toaddrStr, sizeof(toaddrStr));
inet_ntoa2(fromaddr, fromaddrStr, sizeof(fromaddrStr));
DPRINTTIME;
DPRINT("-> TCP [ %s:%d -> %s:%d (len=%i)] for %s %s:%d via REST_Svc proxy [id=%u]\n\n",
localaddrStr, fromlocalport, toaddrStr, toport, numwritten,
socktype==RESTSVC_CIENTSOCK?"client":"server",
fromaddrStr, fromport, restsvc_proxies[proxyidx].proxyid
);
}
u_short find_or_create_locsvc_listener(struct in_addr servertoaddr, u_short servertoport, struct in_addr listenaddr)
{
char serveraddrStr[255];
char localaddrStr[255];
time_t now = time(NULL);
int i= 0;
// Look for an existing proxy for this destination host ip:port
while (i < num_locatorsvc_listeners) {
if (locatorsvc_listeners[i].expirytime < now) {
inet_ntoa2(locatorsvc_listeners[i].serveraddr, serveraddrStr, sizeof(serveraddrStr));
inet_ntoa2(locatorsvc_listeners[i].localaddr, localaddrStr, sizeof(localaddrStr));
DPRINT2(" _Expire LocatorSvc listener [id=%u] for proxy to %s:%d on local address %s:%d. %d proxies left\n",
locatorsvc_listeners[i].listenerid,
serveraddrStr, locatorsvc_listeners[i].serverport,
localaddrStr, locatorsvc_listeners[i].localport,
num_locatorsvc_listeners-1
);
close(locatorsvc_listeners[i].sock);
memcpy(locatorsvc_listeners+i, locatorsvc_listeners+(--num_locatorsvc_listeners), sizeof(*locatorsvc_listeners));
continue;
}
if( (locatorsvc_listeners[i].serveraddr.s_addr == servertoaddr.s_addr) &&
(locatorsvc_listeners[i].serverport == servertoport) &&
(locatorsvc_listeners[i].localaddr.s_addr == listenaddr.s_addr)) {
inet_ntoa2(locatorsvc_listeners[i].serveraddr, serveraddrStr, sizeof(serveraddrStr));
inet_ntoa2(locatorsvc_listeners[i].localaddr, localaddrStr, sizeof(localaddrStr));
DPRINT(" Found existing LocatorSvc listener [id=%u] for proxy to %s:%d on local address %s:%d.\n",
locatorsvc_listeners[i].listenerid,
serveraddrStr, locatorsvc_listeners[i].serverport,
localaddrStr, locatorsvc_listeners[i].localport
);
locatorsvc_listeners[i].expirytime = now+LOCATOR_LISTENER_EXPIRY; // Update expiry time
return locatorsvc_listeners[i].localport;
}
i++;
}
// Add new proxy because there is no existing match
if(num_locatorsvc_listeners == MAX_LOCATOR_LISTENER) {
DPRINT("Can't add new LocatorSvc listener - maximum number of listeners reached\n\n");
return 0;
}
int newsock;
u_short localport;
if((newsock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket (LocatorSvc listener)");
return 0;
};
if ((localport = get_sock_local_port(newsock, listenaddr.s_addr,"LocatorSvc listener")) == 0) {
close(newsock);
return 0;
}
if(listen(newsock, 3) < 0) {
perror("listen (LocatorSvc listener)");
close(newsock);
return 0;
}
locatorsvc_listeners[num_locatorsvc_listeners].expirytime = now+LOCATOR_LISTENER_EXPIRY;
locatorsvc_listeners[num_locatorsvc_listeners].listenerid = next_locatorsvc_listenerid++;
locatorsvc_listeners[num_locatorsvc_listeners].serveraddr = servertoaddr;
locatorsvc_listeners[num_locatorsvc_listeners].serverport = servertoport;
locatorsvc_listeners[num_locatorsvc_listeners].sock = newsock;
locatorsvc_listeners[num_locatorsvc_listeners].localaddr = listenaddr;
locatorsvc_listeners[num_locatorsvc_listeners].localport = localport;
num_locatorsvc_listeners++;
inet_ntoa2(servertoaddr, serveraddrStr, sizeof(serveraddrStr));
inet_ntoa2(listenaddr, localaddrStr, sizeof(localaddrStr));
DPRINT(" Created LocatorSvc listener [id=%u] for proxy to %s:%d on local address %s:%d. Total proxies: %d\n",
locatorsvc_listeners[num_locatorsvc_listeners-1].listenerid,
serveraddrStr, servertoport,
localaddrStr, localport, num_locatorsvc_listeners
);
return localport;
}
void handle_loc_services_accept (int listerneridx)
{
int serversock;
int clientsock;
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
socklen_t clientaddrlen = sizeof(clientaddr);
char clientaddrStr[255];
char serveraddrStr[255];
if ((clientsock = accept(locatorsvc_listeners[listerneridx].sock, (struct sockaddr *)&clientaddr,
&clientaddrlen)) < 0)
{
perror("accept (LocatorSvc proxy)");
return;
}
int flags;
if ((flags = fcntl(clientsock, F_GETFL, 0)) < 0) {
flags = 0;
}
if (fcntl(clientsock, F_SETFL, flags | O_NONBLOCK) < 0)
{
perror("accept (LocatorSvc proxy)");
}
inet_ntoa2(clientaddr.sin_addr, clientaddrStr, sizeof(clientaddrStr));
DPRINT("LocatorSvc proxy - accepted connection from client %s:%d\n",
clientaddrStr, clientaddr.sin_port
);