forked from wangyu-/tinyPortMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1097 lines (928 loc) · 27.3 KB
/
main.cpp
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
#include "common.h"
#include "log.h"
#include "git_version.h"
#include "fd_manager.h"
using namespace std;
typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
typedef long long i64_t;
typedef unsigned int u32_t;
typedef int i32_t;
int disable_conn_clear=0;
int max_pending_packet=0;
int enable_udp=0,enable_tcp=0;
const int listen_fd_buf_size=2*1024*1024;
address_t local_addr,remote_addr;
int VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV;
//template <class key_t>
struct lru_collector_t:not_copy_able_t
{
typedef void* key_t;
//#define key_t void*
struct lru_pair_t
{
key_t key;
my_time_t ts;
};
unordered_map<key_t,list<lru_pair_t>::iterator> mp;
list<lru_pair_t> q;
int update(key_t key)
{
assert(mp.find(key)!=mp.end());
auto it=mp[key];
q.erase(it);
my_time_t value=get_current_time();
if(!q.empty())
{
assert(value >=q.front().ts);
}
lru_pair_t tmp; tmp.key=key; tmp.ts=value;
q.push_front( tmp);
mp[key]=q.begin();
return 0;
}
int new_key(key_t key)
{
assert(mp.find(key)==mp.end());
my_time_t value=get_current_time();
if(!q.empty())
{
assert(value >=q.front().ts);
}
lru_pair_t tmp; tmp.key=key; tmp.ts=value;
q.push_front( tmp);
mp[key]=q.begin();
return 0;
}
int size()
{
return q.size();
}
int empty()
{
return q.empty();
}
void clear()
{
mp.clear(); q.clear();
}
my_time_t ts_of(key_t key)
{
assert(mp.find(key)!=mp.end());
return mp[key]->ts;
}
my_time_t peek_back(key_t &key)
{
assert(!q.empty());
auto it=q.end(); it--;
key=it->key;
return it->ts;
}
void erase(key_t key)
{
assert(mp.find(key)!=mp.end());
q.erase(mp[key]);
mp.erase(key);
}
/*
void erase_back()
{
assert(!q.empty());
auto it=q.end(); it--;
key_t key=it->key;
erase(key);
}*/
};
struct conn_manager_udp_t
{
unordered_map<address_t,udp_pair_t*,address_t::hash_function> adress_to_info;
list<udp_pair_t> udp_pair_list;
long long last_clear_time;
lru_collector_t lru;
//list<udp_pair_t>::iterator clear_it;
conn_manager_udp_t()
{
last_clear_time=0;
adress_to_info.reserve(10007);
//clear_it=udp_pair_list.begin();
}
int erase(list<udp_pair_t>::iterator &it)
{
struct ev_loop * loop= ev_default_loop (0);
ev_io_stop(loop, &it->ev);
mylog(log_info,"[udp]inactive connection {%s} cleared, udp connections=%d\n",it->addr_s,(int)(udp_pair_list.size()-1));
mylog(log_debug,"[udp] lru.size()=%d\n",(int)lru.size()-1);
auto tmp_it=adress_to_info.find(it->adress);
assert(tmp_it!=adress_to_info.end());
adress_to_info.erase(tmp_it);
fd_manager.fd64_close(it->fd64);
lru.erase(&*it);
udp_pair_list.erase(it);
return 0;
}
int clear_inactive()
{
if(get_current_time()-last_clear_time>conn_clear_interval)
{
last_clear_time=get_current_time();
return clear_inactive0();
}
return 0;
}
int clear_inactive0()
{
if(disable_conn_clear) return 0;
int cnt=0;
//list<tcp_pair_t>::iterator it=clear_it,old_it;
int size=udp_pair_list.size();
int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 2% each time,to avoid latency glitch
u64_t current_time=get_current_time();
num_to_clean=min(num_to_clean,size);
for(;;)
{
if(cnt>=num_to_clean) break;
//if(tcp_pair_list.begin()==tcp_pair_list.end()) break;
if(lru.empty()) break;
lru_collector_t::key_t key;
my_time_t ts=lru.peek_back(key);
if(current_time- ts < conn_timeout_tcp) break;
erase( ((udp_pair_t *) key)->it );
cnt++;
}
return 0;
}
}conn_manager_udp;
struct conn_manager_tcp_t
{
list<tcp_pair_t> tcp_pair_list;
long long last_clear_time;
lru_collector_t lru;
conn_manager_tcp_t()
{
last_clear_time=0;
}
int erase(list<tcp_pair_t>::iterator &it)
{
struct ev_loop * loop= ev_default_loop (0);
ev_io_stop(loop, &it->local.ev);
ev_io_stop(loop, &it->remote.ev);
fd_manager.fd64_close( it->local.fd64);
fd_manager.fd64_close( it->remote.fd64);
mylog(log_info,"[tcp]inactive connection {%s} cleared, tcp connections=%d\n",it->addr_s,(int)(tcp_pair_list.size()-1));
mylog(log_debug,"[tcp] lru.size()=%d\n",(int)lru.size()-1);
lru.erase(&*it);
tcp_pair_list.erase(it);
return 0;
}
int erase_closed(list<tcp_pair_t>::iterator &it)//just a copy of erase()
{
struct ev_loop * loop= ev_default_loop (0);
ev_io_stop(loop, &it->local.ev);
ev_io_stop(loop, &it->remote.ev);
fd_manager.fd64_close( it->local.fd64);
fd_manager.fd64_close( it->remote.fd64);
mylog(log_info,"[tcp]closed connection {%s} cleared, tcp connections=%d\n",it->addr_s,(int)(tcp_pair_list.size()-1));
mylog(log_debug,"[tcp] lru.size()=%d\n",(int)lru.size()-1);
lru.erase(&*it);
tcp_pair_list.erase(it);
return 0;
}
int clear_inactive()
{
if(get_current_time()-last_clear_time>conn_clear_interval)
{
last_clear_time=get_current_time();
return clear_inactive0();
}
return 0;
}
int clear_inactive0()
{
if(disable_conn_clear) return 0;
int cnt=0;
//list<tcp_pair_t>::iterator it=clear_it,old_it;
int size=tcp_pair_list.size();
int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 2% each time,to avoid latency glitch
u64_t current_time=get_current_time();
num_to_clean=min(num_to_clean,size);
for(;;)
{
if(cnt>=num_to_clean) break;
//if(tcp_pair_list.begin()==tcp_pair_list.end()) break;
if(lru.empty()) break;
lru_collector_t::key_t key;
my_time_t ts=lru.peek_back(key);
if(current_time- ts < conn_timeout_tcp) break;
erase( ((tcp_pair_t *) key)->it );
cnt++;
}
//clear_it=it;
return 0;
}
}conn_manager_tcp;
void tcp_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
{
if((revents&EV_ERROR) !=0)
{
assert(0==1);
}
//mylog(log_info,"[tcp]tcp_cb called \n");
fd64_t fd64=watcher->u64;
if(!fd_manager.exist(fd64))
{
mylog(log_warn,"[tcp]fd64 no longer exist\n");
return;
}
assert(fd_manager.exist_info(fd64));
fd_info_t & fd_info=fd_manager.get_info(fd64);
assert(fd_info.is_tcp==1);
tcp_pair_t &tcp_pair=*(fd_info.tcp_pair_p);
/*if((revents&EV_ERROR) !=0)
{
assert(0==1);
//mylog(log_info,"[tcp]connection closed, events[idx].events=%x \n",(u32_t) revents);
//ev_io_stop(loop, &tcp_pair.local.ev);
//ev_io_stop(loop, &tcp_pair.remote.ev);
//conn_manager_tcp.erase(tcp_pair.it);
return;
}*/
tcp_info_t *my_info_p,*other_info_p;
if(fd64==tcp_pair.local.fd64)
{
mylog(log_trace,"[tcp]fd64==tcp_pair.local.fd64\n");
my_info_p=&tcp_pair.local;
other_info_p=&tcp_pair.remote;
}
else if(fd64==tcp_pair.remote.fd64)
{
mylog(log_trace,"[tcp]fd64==tcp_pair.remote.fd64\n");
my_info_p=&tcp_pair.remote;
other_info_p=&tcp_pair.local;
}
else
{
assert(0==1);
}
tcp_info_t &my_info=*my_info_p;
tcp_info_t &other_info=*other_info_p;
int my_fd=fd_manager.to_fd(my_info.fd64);
int other_fd=fd_manager.to_fd(other_info.fd64);
assert(watcher->fd==my_fd);
//zdmylog(log_info,"[tcp]my_fd=%d,other_fd=%d \n",my_fd,other_fd);
if( (revents & EV_READ) !=0 )
{
mylog(log_trace,"[tcp]events[idx].events & EPOLLIN !=0 \n");
if((my_info.ev.events&EV_READ) ==0)
{
mylog(log_debug,"[tcp]out of date event, my_info.ev.events&EPOLLIN) ==0 \n");
return;
}
assert(my_info.data_len==0);
int recv_len=recv(my_fd,my_info.data,max_data_len_tcp,0);//use a larger buffer than udp
mylog(log_trace,"fd=%d,recv_len=%d\n",my_fd,recv_len);
if(recv_len==0)
{
mylog(log_info,"[tcp]recv_len=%d,connection {%s} closed bc of EOF\n",recv_len,tcp_pair.addr_s);
conn_manager_tcp.erase_closed(tcp_pair.it);
return;
}
if(recv_len<0)
{
mylog(log_info,"[tcp]recv_len=%d,connection {%s} closed bc of %s,fd=%d\n",recv_len,tcp_pair.addr_s,get_sock_error(),my_fd);
conn_manager_tcp.erase_closed(tcp_pair.it);
return;
}
conn_manager_tcp.lru.update(&(*tcp_pair.it));
//tcp_pair.last_active_time=get_current_time();
my_info.data_len=recv_len;
my_info.begin=my_info.data;
assert((other_info.ev.events & EV_WRITE)==0);
int send_len=send(other_fd,my_info.begin,my_info.data_len,0);
if(send_len<=0)
{
//NOP
}
else
{
my_info.data_len-=send_len;
my_info.begin+=send_len;
}
if(my_info.data_len!=0)
{
//epoll_event ev;
//ev=other_info.ev;
//ret = epoll_ctl(epollfd, EPOLL_CTL_MOD, other_fd, &ev);
//assert(ret==0);
ev_io_stop(loop, &other_info.ev);
other_info.ev.events|=EV_WRITE;
ev_io_init (&other_info.ev, tcp_cb, other_fd, other_info.ev.events);
ev_io_start(loop, &other_info.ev);
//ev=my_info.ev;
//ret = epoll_ctl(epollfd, EPOLL_CTL_MOD, my_fd, &ev);
//assert(ret==0);
ev_io_stop(loop, &my_info.ev);
my_info.ev.events&=~EV_READ;
ev_io_init (&my_info.ev, tcp_cb, my_fd, my_info.ev.events);
ev_io_start(loop, &my_info.ev);
}
}
else if( (revents & EV_WRITE) !=0)
{
mylog(log_trace,"[tcp]events[idx].events & EPOLLOUT !=0\n");
if( (my_info.ev.events&EV_WRITE) ==0)
{
mylog(log_debug,"[tcp]out of date event, my_info.ev.events&EPOLLOUT) ==0 \n");
return;
}
assert(other_info.data_len!=0);
int send_len=send(my_fd,other_info.begin,other_info.data_len,0);
if(send_len==0)
{
mylog(log_warn,"[tcp]send_len=%d,connection {%s} closed bc of send_len==0\n",send_len,tcp_pair.addr_s);
conn_manager_tcp.erase_closed(tcp_pair.it);
return;
}
if(send_len<0)
{
mylog(log_info,"[tcp]send_len=%d,connection {%s} closed bc of %s\n",send_len,tcp_pair.addr_s,get_sock_error());
conn_manager_tcp.erase_closed(tcp_pair.it);
return;
}
conn_manager_tcp.lru.update(&(*tcp_pair.it));
//tcp_pair.last_active_time=get_current_time();
mylog(log_trace,"[tcp]fd=%d send len=%d\n",my_fd,send_len);
other_info.data_len-=send_len;
other_info.begin+=send_len;
if(other_info.data_len==0)
{
//ev=my_info.ev;
//ret = epoll_ctl(epollfd, EPOLL_CTL_MOD, my_fd, &ev);
//assert(ret==0);
ev_io_stop(loop, &my_info.ev);
my_info.ev.events&=~EV_WRITE;
ev_io_init (&my_info.ev, tcp_cb, my_fd, my_info.ev.events);
ev_io_start(loop, &my_info.ev);
assert((other_info.ev.events & EV_READ)==0);
//ev=other_info.ev;
//ret = epoll_ctl(epollfd, EPOLL_CTL_MOD, other_fd, &ev);
//assert(ret==0);
ev_io_stop(loop, &other_info.ev);
other_info.ev.events|=EV_READ;
ev_io_init (&other_info.ev, tcp_cb, other_fd, other_info.ev.events);
ev_io_start(loop, &other_info.ev);
}
else
{
//keep waitting for EPOLLOUT;
}
}
else
{
mylog(log_fatal,"[tcp]got unexpected event,events[idx].events=%x\n",(u32_t)revents);
myexit(-1);
}
}
void tcp_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
{
int ret;
int local_listen_fd_tcp=watcher->fd;
if((revents&EV_ERROR) !=0)
{
assert(0==1);
return ;
}
/*if((events[idx].events & EPOLLERR) !=0 ||(events[idx].events & EPOLLHUP) !=0)
{
mylog(log_error,"[tcp]EPOLLERR or EPOLLHUP from listen_fd events[idx].events=%x \n",events[idx].events);
//if there is an error, we will eventually get it at accept()
}*/
socklen_t tmp_len = sizeof(address_t::storage_t);
address_t::storage_t tmp_sockaddr_in={0};
memset(&tmp_sockaddr_in,0,sizeof(tmp_sockaddr_in));
int new_fd=accept(local_listen_fd_tcp, (struct sockaddr*) &tmp_sockaddr_in,&tmp_len);
if(new_fd<0)
{
mylog(log_warn,"[tcp]accept failed %d %s\n", new_fd,get_sock_error());
//continue;
return ;
}
address_t tmp_addr;
tmp_addr.from_sockaddr((sockaddr*)&tmp_sockaddr_in,tmp_len);
set_buf_size(new_fd,socket_buf_size);
setnonblocking(new_fd);
char ip_addr[max_addr_len];
tmp_addr.to_str(ip_addr);
//sprintf(ip_port_s,"%s:%d",my_ntoa(addr_tmp.sin_addr.s_addr),addr_tmp.sin_port);
if(int(conn_manager_tcp.tcp_pair_list.size())>=max_conn_num)
{
mylog(log_warn,"[tcp]new connection from {%s},but ignored,bc of max_conn_num reached\n",ip_addr);
sock_close(new_fd);
return ;
//continue;
}
int new_remote_fd = socket(remote_addr.get_type(), SOCK_STREAM, 0);
if(new_remote_fd<0)
{
mylog(log_fatal,"[tcp]create new_remote_fd failed \n");
myexit(1);
}
set_buf_size(new_remote_fd,socket_buf_size);
setnonblocking(new_remote_fd);
ret=connect(new_remote_fd,(struct sockaddr*) &remote_addr.inner,remote_addr.get_len());
if(ret!=0)
{
mylog(log_debug,"[tcp]connect returned %d,errno=%s\n",ret,get_sock_error());
}
else
{
mylog(log_debug,"[tcp]connect returned 0\n");
}
conn_manager_tcp.tcp_pair_list.emplace_back();
auto it=conn_manager_tcp.tcp_pair_list.end();
it--;
conn_manager_tcp.lru.new_key(&(*it));
tcp_pair_t &tcp_pair=*it;
strcpy(tcp_pair.addr_s,ip_addr);
mylog(log_info,"[tcp]new_connection from {%s},fd1=%d,fd2=%d,tcp connections=%d\n",tcp_pair.addr_s,new_fd,new_remote_fd,(int)conn_manager_tcp.tcp_pair_list.size());
tcp_pair.local.fd64=fd_manager.create(new_fd);
fd_manager.get_info(tcp_pair.local.fd64).tcp_pair_p= &tcp_pair;
fd_manager.get_info(tcp_pair.local.fd64).is_tcp=1;
//tcp_pair.local.ev.events=EV_READ;
tcp_pair.local.ev.u64=tcp_pair.local.fd64;
tcp_pair.remote.fd64=fd_manager.create(new_remote_fd);
fd_manager.get_info(tcp_pair.remote.fd64).tcp_pair_p= &tcp_pair;
fd_manager.get_info(tcp_pair.remote.fd64).is_tcp=1;
//tcp_pair.remote.ev.events=EV_READ;
tcp_pair.remote.ev.u64=tcp_pair.remote.fd64;
conn_manager_tcp.lru.update(&(*it));
//tcp_pair.last_active_time=get_current_time();
tcp_pair.it=it;
//epoll_event ev;
//ev=tcp_pair.local.ev;
//ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_fd, &ev);
//assert(ret==0);
ev_io_init (&tcp_pair.local.ev, tcp_cb, new_fd, EV_READ);
ev_io_start(loop, &tcp_pair.local.ev);
//ev=tcp_pair.remote.ev;
//ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_remote_fd, &ev);
//assert(ret==0);
ev_io_init (&tcp_pair.remote.ev, tcp_cb, new_remote_fd, EV_READ);
ev_io_start(loop, &tcp_pair.remote.ev);
}
void clear_timer_cb(struct ev_loop *loop, struct ev_timer* timer, int revents)
{
conn_manager_tcp.clear_inactive();
conn_manager_udp.clear_inactive();
}
void udp_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
{
if((revents&EV_ERROR) !=0)
{
assert(0==1);
}
int ret;
fd64_t fd64=watcher->u64;
if(!fd_manager.exist(fd64))
{
mylog(log_warn,"[udp]fd64 no longer exist\n");
return;
}
assert(fd_manager.exist_info(fd64));
fd_info_t & fd_info=fd_manager.get_info(fd64);
assert(fd_info.is_tcp==0);
int udp_fd=fd_manager.to_fd(fd64);
udp_pair_t & udp_pair=*fd_manager.get_info(fd64).udp_pair_p;
//assert(conn_manager.exist_fd(udp_fd));
//if(!conn_manager.exist_fd(udp_fd)) continue;
/*
if((events[idx].events & EPOLLERR) !=0 ||(events[idx].events & EPOLLHUP) !=0)
{
mylog(log_warn,"[udp]EPOLLERR or EPOLLHUP from udp_remote_fd events[idx].events=%x \n",events[idx].events);
}*/
char data[max_data_len_udp+200];
int data_len =recv(udp_fd,data,max_data_len_udp+1,0);
mylog(log_trace, "[udp]received data from udp fd %d, len=%d\n", udp_fd,data_len);
if(data_len==max_data_len_udp+1)
{
mylog(log_warn,"huge packet from {%s}, data_len > %d,dropped\n",udp_pair.addr_s,max_data_len_udp);
return;
}
if(data_len<0)
{
mylog(log_warn,"[udp]recv failed %d ,udp_fd%d,errno:%s\n", data_len,udp_fd,get_sock_error());
return;
}
conn_manager_udp.lru.update(&udp_pair);
//udp_pair.last_active_time=get_current_time();
int local_listen_fd_udp=udp_pair.local_listen_fd;
ret = sendto(local_listen_fd_udp, data,data_len,0, (struct sockaddr *)&udp_pair.adress.inner,udp_pair.adress.get_len());
if (ret < 0) {
mylog(log_warn, "[udp]sento returned %d,%s\n", ret,get_sock_error());
//perror("ret<0");
}
}
void udp_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
{
if((revents&EV_ERROR) !=0)
{
assert(0==1);
return ;
}
/*if((events[idx].events & EPOLLERR) !=0 ||(events[idx].events & EPOLLHUP) !=0)
{
mylog(log_error,"[udp]EPOLLERR or EPOLLHUP from listen_fd events[idx].events=%x \n",events[idx].events);
//if there is an error, we will eventually get it at recvfrom();
}*/
char data[max_data_len_udp+200];
int data_len;
socklen_t tmp_len = sizeof(address_t::storage_t);
address_t::storage_t tmp_sockaddr_in={0};
memset(&tmp_sockaddr_in,0,sizeof(tmp_sockaddr_in));
int local_listen_fd_udp=watcher->fd;
if ((data_len = recvfrom(local_listen_fd_udp, data, max_data_len_udp+1, 0,
(struct sockaddr *) &tmp_sockaddr_in, &tmp_len)) == -1) //<--first packet from a new ip:port turple
{
mylog(log_debug,"[udp]recv_from error,errno %s,this shouldnt happen,but lets try to pretend it didnt happen",get_sock_error());
//myexit(1);
return;
}
address_t tmp_addr;
tmp_addr.from_sockaddr((sockaddr*)&tmp_sockaddr_in,tmp_len);
data[data_len] = 0; //for easier debug
char ip_addr[max_addr_len];
tmp_addr.to_str(ip_addr);
mylog(log_trace, "[udp]received data from udp_listen_fd from {%s}, len=%d\n",ip_addr,data_len);
if(data_len==max_data_len_udp+1)
{
mylog(log_warn,"huge packet from {%s}, data_len > %d,dropped\n",ip_addr,max_data_len_udp);
return;
}
auto it=conn_manager_udp.adress_to_info.find(tmp_addr);
if(it==conn_manager_udp.adress_to_info.end())
{
if(int(conn_manager_udp.udp_pair_list.size())>=max_conn_num)
{
mylog(log_info,"[udp]new connection from {%s},but ignored,bc of max_conv_num reached\n",ip_addr);
return;
}
int new_udp_fd=remote_addr.new_connected_udp_fd();
if(new_udp_fd==-1)
{
mylog(log_info,"[udp]new connection from {%s} ,but create udp fd failed\n",ip_addr);
return;
}
fd64_t fd64=fd_manager.create(new_udp_fd);
fd_manager.get_info(fd64);//just create the info
//struct epoll_event ev;
mylog(log_trace, "[udp]u64: %lld\n", fd64);
//ev.events = EPOLLIN;
//ev.data.u64 = fd64;
//ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev);
//assert(ret==0);
conn_manager_udp.udp_pair_list.emplace_back();
auto list_it=conn_manager_udp.udp_pair_list.end();
list_it--;
conn_manager_udp.lru.new_key(&(*list_it));
udp_pair_t &udp_pair=*list_it;
udp_pair.ev.u64=fd64;
ev_io_init (&udp_pair.ev, udp_cb, new_udp_fd, EV_READ);
ev_io_start(loop, &udp_pair.ev);
mylog(log_info,"[udp]new connection from {%s},udp fd=%d,udp connections=%d\n",ip_addr,new_udp_fd,(int)conn_manager_udp.udp_pair_list.size());
udp_pair.adress=tmp_addr;
udp_pair.fd64=fd64;
//udp_pair.last_active_time=get_current_time();
strcpy(udp_pair.addr_s,ip_addr);
udp_pair.it=list_it;
udp_pair.local_listen_fd=local_listen_fd_udp;
fd_manager.get_info(fd64).udp_pair_p=&udp_pair;
conn_manager_udp.adress_to_info[tmp_addr]=&udp_pair;
it=conn_manager_udp.adress_to_info.find(tmp_addr);
//it=adress_to_info.
}
//auto it=conn_manager_udp.adress_to_info.find(tmp_addr);
assert(it!=conn_manager_udp.adress_to_info.end() );
udp_pair_t &udp_pair=*(it->second);
int udp_fd= fd_manager.to_fd(udp_pair.fd64);
conn_manager_udp.lru.update(&udp_pair);
//udp_pair.last_active_time=get_current_time();
int ret;
ret = send(udp_fd, data,data_len, 0);
if (ret < 0) {
mylog(log_warn, "[udp]send returned %d,%s\n", ret,get_sock_error() );
}
}
void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents)
{
mylog(log_info, "got sigpipe, ignored");
}
void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents)
{
mylog(log_info, "got sigterm, exit");
myexit(0);
}
void sigint_cb(struct ev_loop *l, ev_signal *w, int revents)
{
mylog(log_info, "got sigint, exit");
myexit(0);
}
int event_loop()
{
int local_listen_fd_tcp=-1;
int local_listen_fd_udp=-1;
//struct sockaddr_in local_me,remote_dst;
int yes = 1;int ret;
local_listen_fd_tcp = socket(local_addr.get_type(), SOCK_STREAM, 0);
if(local_listen_fd_tcp<0)
{
mylog(log_fatal,"[tcp]create listen socket failed\n");
myexit(1);
}
setsockopt(local_listen_fd_tcp, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); //avoid annoying bind problem
set_buf_size(local_listen_fd_tcp,listen_fd_buf_size);
setnonblocking(local_listen_fd_tcp);
local_listen_fd_udp = socket(local_addr.get_type(), SOCK_DGRAM, IPPROTO_UDP);
if(local_listen_fd_udp<0)
{
mylog(log_fatal,"[udp]create listen socket failed\n");
myexit(1);
}
setsockopt(local_listen_fd_udp, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); //this is not necessary.
set_buf_size(local_listen_fd_udp,listen_fd_buf_size);
setnonblocking(local_listen_fd_udp);
//int epollfd = epoll_create1(0);
//const int max_events = 4096;
//struct epoll_event ev, events[max_events];
//if (epollfd < 0)
//{
// mylog(log_fatal,"epoll created return %d\n", epollfd);
// myexit(-1);
//}
struct ev_loop * loop= ev_default_loop(0);
assert(loop != NULL);
struct ev_io tcp_accept_watcher;
if(enable_tcp)
{
if (::bind(local_listen_fd_tcp, (struct sockaddr*) &local_addr.inner, local_addr.get_len()) !=0)
{
mylog(log_fatal,"[tcp]socket bind failed, %s",get_sock_error());
myexit(1);
}
if (listen (local_listen_fd_tcp, 512) !=0) //512 is max pending tcp connection,its large enough
{
mylog(log_fatal,"[tcp]socket listen failed error, %s",get_sock_error());
myexit(1);
}
//ev.events = EPOLLIN;
//ev.data.u64 = local_listen_fd_tcp;
//int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, local_listen_fd_tcp, &ev);
//if(ret!=0)
//{
// mylog(log_fatal,"[tcp]epoll EPOLL_CTL_ADD return %d\n", epollfd);
// myexit(-1);
//}
ev_io_init(&tcp_accept_watcher, tcp_accept_cb, local_listen_fd_tcp, EV_READ);
ev_io_start(loop, &tcp_accept_watcher);
}
struct ev_io udp_accept_watcher;
if(enable_udp)
{
if (::bind(local_listen_fd_udp, (struct sockaddr*) &local_addr.inner, local_addr.get_len()) == -1)
{
mylog(log_fatal,"[udp]socket bind error");
myexit(1);
}
//ev.events = EPOLLIN;
//ev.data.u64 = local_listen_fd_udp;
//int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, local_listen_fd_udp, &ev);
//if(ret!=0)
//{
// mylog(log_fatal,"[udp]epoll created return %d\n", epollfd);
// myexit(-1);
//}
ev_io_init(&udp_accept_watcher, udp_accept_cb, local_listen_fd_udp, EV_READ);
ev_io_start(loop, &udp_accept_watcher);
}
//int clear_timer_fd=-1;
struct ev_timer clear_timer;
ev_timer_init(&clear_timer, clear_timer_cb, 0, timer_interval/1000.0);
ev_timer_start(loop, &clear_timer);
ev_run(loop, 0);
/*
set_timer(epollfd,clear_timer_fd);
//u32_t roller=0;
*/
myexit(0);
return 0;
}
void print_help()
{
char git_version_buf[100]={0};
strncpy(git_version_buf,gitversion,10);
printf("\n");
printf("tinyPortMapper\n");
printf("git version:%s ",git_version_buf);
printf("build date:%s %s\n",__DATE__,__TIME__);
printf("repository: https://github.com/wangyu-/tinyPortMapper\n");
printf("\n");
printf("usage:\n");
printf(" ./this_program -l <listen_ip>:<listen_port> -r <remote_ip>:<remote_port> [options]\n");
printf("\n");
printf("main options:\n");
printf(" -t enable TCP forwarding/mapping\n");
printf(" -u enable UDP forwarding/mapping\n");
//printf("NOTE: If neither of -t or -u is provided,this program enables both TCP and UDP forward\n");
printf("\n");
printf("other options:\n");
printf(" --sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
printf(" --log-level <number> 0: never 1: fatal 2: error 3: warn \n");
printf(" 4: info (default) 5: debug 6: trace\n");
printf(" --log-position enable file name, function name, line number in log\n");
printf(" --disable-color disable log color\n");
printf(" --enable-color enable log color, log color is enabled by default on most platforms\n");
printf(" -h,--help print this help message\n");
printf("\n");
//printf("common options,these options must be same on both side\n");
}
void process_arg(int argc, char *argv[])
{
int i, j, k;
int opt;
static struct option long_options[] =
{
{"log-level", required_argument, 0, 1},
{"log-position", no_argument, 0, 1},
{"disable-color", no_argument, 0, 1},
{"enable-color", no_argument, 0, 1},
{"sock-buf", required_argument, 0, 1},
{NULL, 0, 0, 0}
};
int option_index = 0;
if (argc == 1)
{
print_help();
myexit( -1);
}
for (i = 0; i < argc; i++)
{
if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0)
{
print_help();
myexit(0);
}
}
for (i = 0; i < argc; i++)
{
if(strcmp(argv[i],"--log-level")==0)
{
if(i<argc -1)
{
sscanf(argv[i+1],"%d",&log_level);
if(0<=log_level&&log_level<log_end)
{
}
else
{
log_bare(log_fatal,"invalid log_level\n");
myexit(-1);
}
}
}
if(strcmp(argv[i],"--disable-color")==0)
{
enable_log_color=0;
}
if(strcmp(argv[i],"--enable-color")==0)
{
enable_log_color=1;
}
}
mylog(log_info,"argc=%d ", argc);
for (i = 0; i < argc; i++) {
log_bare(log_info, "%s ", argv[i]);
}
log_bare(log_info, "\n");
if (argc == 1)
{
print_help();
myexit(-1);
}
int no_l = 1, no_r = 1;
while ((opt = getopt_long(argc, argv, "l:r:tuh:",long_options,&option_index)) != -1)
{
//string opt_key;
//opt_key+=opt;
switch (opt)
{
case 'l':
no_l = 0;
local_addr.from_str(optarg);
break;
case 'r':
no_r = 0;
remote_addr.from_str(optarg);
break;
case 't':
enable_tcp=1;
break;
case 'u':
enable_udp=1;
break;
case 'h':
break;
case 1:
if(strcmp(long_options[option_index].name,"log-level")==0)
{
}
else if(strcmp(long_options[option_index].name,"disable-color")==0)
{
//enable_log_color=0;
}
else if(strcmp(long_options[option_index].name,"enable-color")==0)
{
//enable_log_color=0;
}
else if(strcmp(long_options[option_index].name,"log-position")==0)