main.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. #include "common.h"
  2. #include "log.h"
  3. #include "git_version.h"
  4. #include "lib/rs.h"
  5. #include "packet.h"
  6. //#include "conn_manager.h"
  7. #include "delay_manager.h"
  8. #include "classic.h"
  9. using namespace std;
  10. typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
  11. typedef long long i64_t;
  12. typedef unsigned int u32_t;
  13. typedef int i32_t;
  14. int dup_num=1;
  15. int dup_delay_min=20; //0.1ms
  16. int dup_delay_max=20;
  17. int jitter_min=0;
  18. int jitter_max=0;
  19. //int random_number_fd=-1;
  20. int mtu_warn=1350;
  21. u32_t remote_address_uint32=0;
  22. char local_address[100], remote_address[100];
  23. int local_port = -1, remote_port = -1;
  24. u64_t last_report_time=0;
  25. int report_interval=0;
  26. //conn_manager_t conn_manager;
  27. delay_manager_t delay_manager;
  28. const int disable_conv_clear=0;
  29. struct conv_manager_t // manage the udp connections
  30. {
  31. //typedef hash_map map;
  32. unordered_map<u64_t,u32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
  33. unordered_map<u32_t,u64_t> conv_to_u64;
  34. unordered_map<u32_t,u64_t> conv_last_active_time;
  35. unordered_map<u32_t,u64_t>::iterator clear_it;
  36. unordered_map<u32_t,u64_t>::iterator it;
  37. unordered_map<u32_t,u64_t>::iterator old_it;
  38. //void (*clear_function)(uint64_t u64) ;
  39. long long last_clear_time;
  40. conv_manager_t()
  41. {
  42. clear_it=conv_last_active_time.begin();
  43. long long last_clear_time=0;
  44. //clear_function=0;
  45. }
  46. ~conv_manager_t()
  47. {
  48. clear();
  49. }
  50. int get_size()
  51. {
  52. return conv_to_u64.size();
  53. }
  54. void reserve()
  55. {
  56. u64_to_conv.reserve(10007);
  57. conv_to_u64.reserve(10007);
  58. conv_last_active_time.reserve(10007);
  59. }
  60. void clear()
  61. {
  62. ///// if(disable_conv_clear) return ;
  63. if(program_mode==server_mode)
  64. {
  65. for(it=conv_to_u64.begin();it!=conv_to_u64.end();it++)
  66. {
  67. //int fd=int((it->second<<32u)>>32u);
  68. ////// server_clear_function( it->second);//////////////todo
  69. }
  70. }
  71. u64_to_conv.clear();
  72. conv_to_u64.clear();
  73. conv_last_active_time.clear();
  74. clear_it=conv_last_active_time.begin();
  75. }
  76. u32_t get_new_conv()
  77. {
  78. u32_t conv=get_true_random_number_nz();
  79. while(conv_to_u64.find(conv)!=conv_to_u64.end())
  80. {
  81. conv=get_true_random_number_nz();
  82. }
  83. return conv;
  84. }
  85. int is_conv_used(u32_t conv)
  86. {
  87. return conv_to_u64.find(conv)!=conv_to_u64.end();
  88. }
  89. int is_u64_used(u64_t u64)
  90. {
  91. return u64_to_conv.find(u64)!=u64_to_conv.end();
  92. }
  93. u32_t find_conv_by_u64(u64_t u64)
  94. {
  95. return u64_to_conv[u64];
  96. }
  97. u64_t find_u64_by_conv(u32_t conv)
  98. {
  99. return conv_to_u64[conv];
  100. }
  101. int update_active_time(u32_t conv)
  102. {
  103. return conv_last_active_time[conv]=get_current_time();
  104. }
  105. int insert_conv(u32_t conv,u64_t u64)
  106. {
  107. u64_to_conv[u64]=conv;
  108. conv_to_u64[conv]=u64;
  109. conv_last_active_time[conv]=get_current_time();
  110. return 0;
  111. }
  112. int erase_conv(u32_t conv)
  113. {
  114. if(disable_conv_clear) return 0;
  115. u64_t u64=conv_to_u64[conv];
  116. if(program_mode==server_mode)
  117. {
  118. //server_clear_function(u64);
  119. }
  120. conv_to_u64.erase(conv);
  121. u64_to_conv.erase(u64);
  122. conv_last_active_time.erase(conv);
  123. return 0;
  124. }
  125. int clear_inactive(char * ip_port=0)
  126. {
  127. if(get_current_time()-last_clear_time>conv_clear_interval)
  128. {
  129. last_clear_time=get_current_time();
  130. return clear_inactive0(ip_port);
  131. }
  132. return 0;
  133. }
  134. int clear_inactive0(char * ip_port)
  135. {
  136. if(disable_conv_clear) return 0;
  137. //map<uint32_t,uint64_t>::iterator it;
  138. int cnt=0;
  139. it=clear_it;
  140. int size=conv_last_active_time.size();
  141. int num_to_clean=size/conv_clear_ratio+conv_clear_min; //clear 1/10 each time,to avoid latency glitch
  142. num_to_clean=min(num_to_clean,size);
  143. u64_t current_time=get_current_time();
  144. for(;;)
  145. {
  146. if(cnt>=num_to_clean) break;
  147. if(conv_last_active_time.begin()==conv_last_active_time.end()) break;
  148. if(it==conv_last_active_time.end())
  149. {
  150. it=conv_last_active_time.begin();
  151. }
  152. if( current_time -it->second >conv_timeout )
  153. {
  154. //mylog(log_info,"inactive conv %u cleared \n",it->first);
  155. old_it=it;
  156. it++;
  157. u32_t conv= old_it->first;
  158. erase_conv(old_it->first);
  159. if(ip_port==0)
  160. {
  161. mylog(log_info,"conv %x cleared\n",conv);
  162. }
  163. else
  164. {
  165. mylog(log_info,"[%s]conv %x cleared\n",ip_port,conv);
  166. }
  167. }
  168. else
  169. {
  170. it++;
  171. }
  172. cnt++;
  173. }
  174. return 0;
  175. }
  176. };//g_conv_manager;
  177. struct conn_info_t //stores info for a raw connection.for client ,there is only one connection,for server there can be thousand of connection since server can
  178. //handle multiple clients
  179. {
  180. conv_manager_t conv_manager;
  181. };
  182. struct conn_manager_t //manager for connections. for client,we dont need conn_manager since there is only one connection.for server we use one conn_manager for all connections
  183. {
  184. u32_t ready_num;
  185. unordered_map<int,conn_info_t *> udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search
  186. unordered_map<int,conn_info_t *> timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy
  187. unordered_map<id_t,conn_info_t *> const_id_mp;
  188. unordered_map<u64_t,conn_info_t*> mp; //put it at end so that it de-consturcts first
  189. unordered_map<u64_t,conn_info_t*>::iterator clear_it;
  190. long long last_clear_time;
  191. conn_manager_t()
  192. {
  193. ready_num=0;
  194. mp.reserve(10007);
  195. clear_it=mp.begin();
  196. timer_fd_mp.reserve(10007);
  197. const_id_mp.reserve(10007);
  198. udp_fd_mp.reserve(100007);
  199. last_clear_time=0;
  200. //current_ready_ip=0;
  201. // current_ready_port=0;
  202. }
  203. int exist(u32_t ip,uint16_t port)
  204. {
  205. u64_t u64=0;
  206. u64=ip;
  207. u64<<=32u;
  208. u64|=port;
  209. if(mp.find(u64)!=mp.end())
  210. {
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. /*
  216. int insert(uint32_t ip,uint16_t port)
  217. {
  218. uint64_t u64=0;
  219. u64=ip;
  220. u64<<=32u;
  221. u64|=port;
  222. mp[u64];
  223. return 0;
  224. }*/
  225. conn_info_t *& find_insert_p(u32_t ip,uint16_t port) //be aware,the adress may change after rehash
  226. {
  227. u64_t u64=0;
  228. u64=ip;
  229. u64<<=32u;
  230. u64|=port;
  231. unordered_map<u64_t,conn_info_t*>::iterator it=mp.find(u64);
  232. if(it==mp.end())
  233. {
  234. mp[u64]=new conn_info_t;
  235. }
  236. return mp[u64];
  237. }
  238. conn_info_t & find_insert(u32_t ip,uint16_t port) //be aware,the adress may change after rehash
  239. {
  240. u64_t u64=0;
  241. u64=ip;
  242. u64<<=32u;
  243. u64|=port;
  244. unordered_map<u64_t,conn_info_t*>::iterator it=mp.find(u64);
  245. if(it==mp.end())
  246. {
  247. mp[u64]=new conn_info_t;
  248. }
  249. return *mp[u64];
  250. }
  251. int erase(unordered_map<u64_t,conn_info_t*>::iterator erase_it)
  252. {
  253. if(erase_it->second->state.server_current_state==server_ready)
  254. {
  255. ready_num--;
  256. assert(i32_t(ready_num)!=-1);
  257. assert(erase_it->second!=0);
  258. assert(erase_it->second->timer_fd !=0);
  259. assert(erase_it->second->oppsite_const_id!=0);
  260. assert(const_id_mp.find(erase_it->second->oppsite_const_id)!=const_id_mp.end());
  261. assert(timer_fd_mp.find(erase_it->second->timer_fd)!=timer_fd_mp.end());
  262. const_id_mp.erase(erase_it->second->oppsite_const_id);
  263. timer_fd_mp.erase(erase_it->second->timer_fd);
  264. close(erase_it->second->timer_fd);// close will auto delte it from epoll
  265. delete(erase_it->second);
  266. mp.erase(erase_it->first);
  267. }
  268. else
  269. {
  270. assert(erase_it->second->blob==0);
  271. assert(erase_it->second->timer_fd ==0);
  272. assert(erase_it->second->oppsite_const_id==0);
  273. delete(erase_it->second);
  274. mp.erase(erase_it->first);
  275. }
  276. return 0;
  277. }
  278. int clear_inactive()
  279. {
  280. if(get_current_time()-last_clear_time>conn_clear_interval)
  281. {
  282. last_clear_time=get_current_time();
  283. return clear_inactive0();
  284. }
  285. return 0;
  286. }
  287. int clear_inactive0()
  288. {
  289. unordered_map<u64_t,conn_info_t*>::iterator it;
  290. unordered_map<u64_t,conn_info_t*>::iterator old_it;
  291. if(disable_conn_clear) return 0;
  292. //map<uint32_t,uint64_t>::iterator it;
  293. int cnt=0;
  294. it=clear_it;
  295. int size=mp.size();
  296. int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 1/10 each time,to avoid latency glitch
  297. mylog(log_trace,"mp.size() %d\n", size);
  298. num_to_clean=min(num_to_clean,(int)mp.size());
  299. u64_t current_time=get_current_time();
  300. for(;;)
  301. {
  302. if(cnt>=num_to_clean) break;
  303. if(mp.begin()==mp.end()) break;
  304. if(it==mp.end())
  305. {
  306. it=mp.begin();
  307. }
  308. if(it->second->state.server_current_state==server_ready &&current_time - it->second->last_hb_recv_time <=server_conn_timeout)
  309. {
  310. it++;
  311. }
  312. else if(it->second->state.server_current_state!=server_ready&& current_time - it->second->last_state_time <=server_handshake_timeout )
  313. {
  314. it++;
  315. }
  316. else if(it->second->blob!=0&&it->second->blob->conv_manager.get_size() >0)
  317. {
  318. assert(it->second->state.server_current_state==server_ready);
  319. it++;
  320. }
  321. else
  322. {
  323. mylog(log_info,"[%s:%d]inactive conn cleared \n",my_ntoa(it->second->raw_info.recv_info.src_ip),it->second->raw_info.recv_info.src_port);
  324. old_it=it;
  325. it++;
  326. erase(old_it);
  327. }
  328. cnt++;
  329. }
  330. return 0;
  331. }
  332. }conn_manager;
  333. int VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV;
  334. int event_loop()
  335. {
  336. struct sockaddr_in local_me, local_other;
  337. local_listen_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  338. int yes = 1;
  339. //setsockopt(local_listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  340. set_buf_size(local_listen_fd,4*1024*1024);
  341. setnonblocking(local_listen_fd);
  342. //char data[buf_len];
  343. //char *data=data0;
  344. socklen_t slen = sizeof(sockaddr_in);
  345. memset(&local_me, 0, sizeof(local_me));
  346. local_me.sin_family = AF_INET;
  347. local_me.sin_port = htons(local_port);
  348. local_me.sin_addr.s_addr = inet_addr(local_address);
  349. if (bind(local_listen_fd, (struct sockaddr*) &local_me, slen) == -1)
  350. {
  351. mylog(log_fatal,"socket bind error");
  352. myexit(1);
  353. }
  354. int epollfd = epoll_create1(0);
  355. const int max_events = 4096;
  356. struct epoll_event ev, events[max_events];
  357. if (epollfd < 0)
  358. {
  359. mylog(log_fatal,"epoll created return %d\n", epollfd);
  360. myexit(-1);
  361. }
  362. ev.events = EPOLLIN;
  363. ev.data.fd = local_listen_fd;
  364. int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, local_listen_fd, &ev);
  365. if(ret!=0)
  366. {
  367. mylog(log_fatal,"epoll created return %d\n", epollfd);
  368. myexit(-1);
  369. }
  370. int clear_timer_fd=-1;
  371. set_timer_ms(epollfd,clear_timer_fd,timer_interval);
  372. ev.events = EPOLLIN;
  373. ev.data.fd = delay_manager.timer_fd();
  374. epoll_ctl(epollfd, EPOLL_CTL_ADD, delay_manager.timer_fd, &ev);
  375. if (ret < 0)
  376. {
  377. mylog(log_fatal,"epoll_ctl return %d\n", ret);
  378. myexit(-1);
  379. }
  380. for (;;)
  381. {
  382. int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000); //3mins
  383. if (nfds < 0)
  384. {
  385. mylog(log_fatal,"epoll_wait return %d\n", nfds);
  386. myexit(-1);
  387. }
  388. int n;
  389. int clear_triggered=0;
  390. for (n = 0; n < nfds; ++n)
  391. {
  392. if (events[n].data.fd == local_listen_fd) //data income from local end
  393. {
  394. char data[buf_len];
  395. int data_len;
  396. slen = sizeof(sockaddr_in);
  397. if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0,
  398. (struct sockaddr *) &local_other, &slen)) == -1) //<--first packet from a new ip:port turple
  399. {
  400. mylog(log_error,"recv_from error,errno %s,this shouldnt happen,but lets try to pretend it didnt happen",strerror(errno));
  401. //myexit(1);
  402. continue;
  403. }
  404. mylog(log_trace, "received data from listen fd,%s:%d, len=%d\n", my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port),data_len);
  405. if(data_len>mtu_warn)
  406. {
  407. mylog(log_warn,"huge packet,data len=%d (>%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn);
  408. }
  409. data[data_len] = 0; //for easier debug
  410. u64_t u64=pack_u64(local_other.sin_addr.s_addr,ntohs(local_other.sin_port));
  411. if(!conn_manager.exist_u64(u64))
  412. {
  413. if(int(conn_manager.fd_to_u64.size())>=max_conv_num)
  414. {
  415. mylog(log_info,"new connection from %s:%d ,but ignored,bc of max_conv_num reached\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port));
  416. continue;
  417. }
  418. int new_udp_fd;
  419. if(create_new_udp(new_udp_fd,remote_address_uint32,remote_port)!=0)
  420. {
  421. mylog(log_info,"new connection from %s:%d ,but create udp fd failed\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port));
  422. continue;
  423. }
  424. struct epoll_event ev;
  425. mylog(log_trace, "u64: %lld\n", u64);
  426. ev.events = EPOLLIN;
  427. ev.data.fd = new_udp_fd;
  428. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev);
  429. if (ret != 0) {
  430. mylog(log_info,"new connection from %s:%d ,but add to epoll failed\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port));
  431. close(new_udp_fd);
  432. continue;
  433. }
  434. mylog(log_info,"new connection from %s:%d ,created new udp fd %d\n",my_ntoa(local_other.sin_addr.s_addr),ntohs(local_other.sin_port),new_udp_fd);
  435. conn_manager.insert_fd(new_udp_fd,u64);
  436. }
  437. int new_udp_fd=conn_manager.find_fd_by_u64(u64);
  438. conn_manager.update_active_time(new_udp_fd);
  439. int ret;
  440. if(is_client)
  441. {
  442. add_seq(data,data_len);
  443. my_time_t sum=0;
  444. for(int i=0;i<dup_num;i++)
  445. {
  446. printf("<%d>\n",i);
  447. char new_data[buf_len];
  448. int new_len=0;
  449. do_obscure(data, data_len, new_data, new_len);
  450. delay_data_t tmp;
  451. tmp.type=enum_send_fd;
  452. tmp.data=new_data;
  453. tmp.len=new_len;
  454. tmp.dest.fd=new_udp_fd;
  455. if(i==0)
  456. sum+=random_between(jitter_min,jitter_max)*100;
  457. else
  458. sum+=random_between(dup_delay_min,dup_delay_max)*100;
  459. delay_manager.add(sum,tmp);
  460. }
  461. /*
  462. if(jitter_max==0)
  463. {
  464. char new_data[buf_len];
  465. int new_len=0;
  466. do_obscure(data, data_len, new_data, new_len);
  467. ret = send_fd(new_udp_fd, new_data,new_len, 0);
  468. if (ret < 0) {
  469. mylog(log_warn, "send returned %d ,errno:%s\n", ret,strerror(errno));
  470. }
  471. add_and_new(new_udp_fd, dup_num - 1,random_between(dup_delay_min,dup_delay_max), data, data_len,u64);
  472. }
  473. else
  474. {
  475. add_and_new(new_udp_fd, dup_num,random_between(jitter_min,jitter_max), data, data_len,u64);
  476. }*/
  477. packet_send_count++;
  478. }
  479. else
  480. {
  481. printf("i got a packet\n");
  482. char new_data[buf_len];
  483. int new_len;
  484. if (de_obscure(data, data_len, new_data, new_len) != 0) {
  485. printf("failed 1\n");
  486. mylog(log_trace,"de_obscure failed \n");
  487. continue;
  488. }
  489. //dup_packet_recv_count++;
  490. if (remove_seq(new_data, new_len) != 0) {
  491. printf("failed 2\n");
  492. mylog(log_trace,"remove_seq failed \n");
  493. continue;
  494. }
  495. //packet_recv_count++;
  496. ret = send_fd(new_udp_fd, new_data,new_len, 0);
  497. if (ret < 0) {
  498. mylog(log_warn, "send returned %d,%s\n", ret,strerror(errno));
  499. //perror("what happened????");
  500. }
  501. }
  502. }
  503. else if(events[n].data.fd == clear_timer_fd)
  504. {
  505. clear_triggered=1;
  506. if(report_interval!=0 &&get_current_time()-last_report_time>u64_t(report_interval)*1000)
  507. {
  508. last_report_time=get_current_time();
  509. if(is_client)
  510. mylog(log_info,"client-->server: %llu,%llu(include dup); server-->client %llu,%lld(include dup)\n",packet_send_count,
  511. dup_packet_send_count,packet_recv_count,dup_packet_recv_count);
  512. else
  513. mylog(log_info,"client-->server: %llu,%llu(include dup); server-->client %llu,%lld(include dup)\n",packet_recv_count,dup_packet_recv_count,packet_send_count,
  514. dup_packet_send_count);
  515. }
  516. }
  517. else if (events[n].data.fd == delay_manager.timer_fd)
  518. {
  519. uint64_t value;
  520. read(delay_manager.timer_fd, &value, 8);
  521. //printf("<timerfd_triggered, %d>",delay_mp.size());
  522. //fflush(stdout);
  523. }
  524. else
  525. {
  526. int udp_fd=events[n].data.fd;
  527. if(!conn_manager.exist_fd(udp_fd)) continue;
  528. char data[buf_len];
  529. int data_len =recv(udp_fd,data,max_data_len,0);
  530. mylog(log_trace, "received data from udp fd %d, len=%d\n", udp_fd,data_len);
  531. if(data_len<0)
  532. {
  533. if(errno==ECONNREFUSED)
  534. {
  535. //conn_manager.clear_list.push_back(udp_fd);
  536. mylog(log_debug, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,udp_fd,strerror(errno));
  537. }
  538. mylog(log_warn, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,udp_fd,strerror(errno));
  539. continue;
  540. }
  541. if(data_len>mtu_warn)
  542. {
  543. mylog(log_warn,"huge packet,data len=%d (>%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn);
  544. }
  545. assert(conn_manager.exist_fd(udp_fd));
  546. conn_manager.update_active_time(udp_fd);
  547. u64_t u64=conn_manager.find_u64_by_fd(udp_fd);
  548. if(is_client)
  549. {
  550. char new_data[buf_len];
  551. int new_len;
  552. if (de_obscure(data, data_len, new_data, new_len) != 0) {
  553. mylog(log_debug,"data_len=%d \n",data_len);
  554. continue;
  555. }
  556. //dup_packet_recv_count++;
  557. if (remove_seq(new_data, new_len) != 0) {
  558. mylog(log_debug,"remove_seq error \n");
  559. continue;
  560. }
  561. //packet_recv_count++;
  562. ret = sendto_u64(u64, new_data,
  563. new_len , 0);
  564. if (ret < 0) {
  565. mylog(log_warn, "sento returned %d,%s\n", ret,strerror(errno));
  566. //perror("ret<0");
  567. }
  568. }
  569. else
  570. {
  571. add_seq(data,data_len);
  572. my_time_t sum=0;
  573. for(int i=0;i<dup_num;i++)
  574. {
  575. printf("<%d>\n",i);
  576. char new_data[buf_len];
  577. int new_len=0;
  578. do_obscure(data, data_len, new_data, new_len);
  579. delay_data_t tmp;
  580. tmp.type=enum_sendto_u64;
  581. tmp.data=new_data;
  582. tmp.len=new_len;
  583. tmp.dest.u64=u64;
  584. if(i==0)
  585. sum+=random_between(jitter_min,jitter_max)*100;
  586. else
  587. sum+=random_between(dup_delay_min,dup_delay_max)*100;
  588. delay_manager.add(sum,tmp);
  589. }
  590. packet_send_count++;
  591. }
  592. //mylog(log_trace, "%s :%d\n", inet_ntoa(tmp_sockaddr.sin_addr),
  593. // ntohs(tmp_sockaddr.sin_port));
  594. //mylog(log_trace, "%d byte sent\n", ret);
  595. }
  596. }
  597. delay_manager.check();
  598. //conn_manager.check_clear_list();
  599. if(clear_triggered) // 删除操作在epoll event的最后进行,防止event cache中的fd失效。
  600. {
  601. u64_t value;
  602. read(clear_timer_fd, &value, 8);
  603. mylog(log_trace, "timer!\n");
  604. conn_manager.clear_inactive();
  605. }
  606. }
  607. myexit(0);
  608. return 0;
  609. }
  610. int unit_test()
  611. {
  612. int i,j,k;
  613. void *code=fec_new(3,6);
  614. char arr[6][100]=
  615. {
  616. "aaa","bbb","ccc"
  617. ,"ddd","eee","fff"
  618. };
  619. char *data[6];
  620. for(i=0;i<6;i++)
  621. {
  622. data[i]=arr[i];
  623. }
  624. rs_encode(code,data,3);
  625. //printf("%d %d",(int)(unsigned char)arr[5][0],(int)('a'^'b'^'c'^'d'^'e'));
  626. for(i=0;i<6;i++)
  627. {
  628. printf("<%s>",data[i]);
  629. }
  630. data[0]=0;
  631. //data[1]=0;
  632. //data[5]=0;
  633. int ret=rs_decode(code,data,3);
  634. printf("ret:%d\n",ret);
  635. for(i=0;i<6;i++)
  636. {
  637. printf("<%s>",data[i]);
  638. }
  639. fec_free(code);
  640. return 0;
  641. }
  642. void print_help()
  643. {
  644. char git_version_buf[100]={0};
  645. strncpy(git_version_buf,gitversion,10);
  646. printf("UDPspeeder\n");
  647. printf("git version:%s ",git_version_buf);
  648. printf("build date:%s %s\n",__DATE__,__TIME__);
  649. printf("repository: https://github.com/wangyu-/UDPspeeder\n");
  650. printf("\n");
  651. printf("usage:\n");
  652. printf(" run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]\n");
  653. printf(" run as server : ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options]\n");
  654. printf("\n");
  655. printf("common option,must be same on both sides:\n");
  656. printf(" -k,--key <string> key for simple xor encryption,default:\"secret key\"\n");
  657. printf("main options:\n");
  658. printf(" -d <number> duplicated packet number, -d 0 means no duplicate. default value:0\n");
  659. printf(" -t <number> duplicated packet delay time, unit: 0.1ms,default value:20(2ms)\n");
  660. printf(" -j <number> simulated jitter.randomly delay first packet for 0~jitter_value*0.1 ms,to\n");
  661. printf(" create simulated jitter.default value:0.do not use if you dont\n");
  662. printf(" know what it means\n");
  663. printf(" --report <number> turn on udp send/recv report,and set a time interval for reporting,unit:s\n");
  664. printf("advanced options:\n");
  665. printf(" -t tmin:tmax simliar to -t above,but delay randomly between tmin and tmax\n");
  666. printf(" -j jmin:jmax simliar to -j above,but create jitter randomly between jmin and jmax\n");
  667. printf(" --random-drop <number> simulate packet loss ,unit:0.01%%\n");
  668. printf(" --disable-filter disable duplicate packet filter.\n");
  669. printf(" -m <number> max pending packets,to prevent the program from eating up all your memory,\n");
  670. printf(" default value:0(disabled).\n");
  671. printf("other options:\n");
  672. printf(" --log-level <number> 0:never 1:fatal 2:error 3:warn \n");
  673. printf(" 4:info (default) 5:debug 6:trace\n");
  674. printf(" --log-position enable file name,function name,line number in log\n");
  675. printf(" --disable-color disable log color\n");
  676. printf(" --sock-buf <number> buf size for socket,>=10 and <=10240,unit:kbyte,default:1024\n");
  677. //printf(" -p use multi-process mode instead of epoll.very costly,only for test,do dont use\n");
  678. printf(" -h,--help print this help message\n");
  679. //printf("common options,these options must be same on both side\n");
  680. }
  681. void process_arg(int argc, char *argv[])
  682. {
  683. int i, j, k;
  684. int opt;
  685. static struct option long_options[] =
  686. {
  687. {"log-level", required_argument, 0, 1},
  688. {"log-position", no_argument, 0, 1},
  689. {"disable-color", no_argument, 0, 1},
  690. {"disable-filter", no_argument, 0, 1},
  691. {"sock-buf", required_argument, 0, 1},
  692. {"random-drop", required_argument, 0, 1},
  693. {"report", required_argument, 0, 1},
  694. {NULL, 0, 0, 0}
  695. };
  696. int option_index = 0;
  697. if (argc == 1)
  698. {
  699. print_help();
  700. myexit( -1);
  701. }
  702. for (i = 0; i < argc; i++)
  703. {
  704. if(strcmp(argv[i],"--unit-test")==0)
  705. {
  706. unit_test();
  707. myexit(0);
  708. }
  709. }
  710. for (i = 0; i < argc; i++)
  711. {
  712. if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0)
  713. {
  714. print_help();
  715. myexit(0);
  716. }
  717. }
  718. for (i = 0; i < argc; i++)
  719. {
  720. if(strcmp(argv[i],"--log-level")==0)
  721. {
  722. if(i<argc -1)
  723. {
  724. sscanf(argv[i+1],"%d",&log_level);
  725. if(0<=log_level&&log_level<log_end)
  726. {
  727. }
  728. else
  729. {
  730. log_bare(log_fatal,"invalid log_level\n");
  731. myexit(-1);
  732. }
  733. }
  734. }
  735. if(strcmp(argv[i],"--disable-color")==0)
  736. {
  737. enable_log_color=0;
  738. }
  739. }
  740. mylog(log_info,"argc=%d ", argc);
  741. for (i = 0; i < argc; i++) {
  742. log_bare(log_info, "%s ", argv[i]);
  743. }
  744. log_bare(log_info, "\n");
  745. if (argc == 1)
  746. {
  747. print_help();
  748. myexit(-1);
  749. }
  750. int no_l = 1, no_r = 1;
  751. while ((opt = getopt_long(argc, argv, "l:r:d:t:hcspk:j:m:",long_options,&option_index)) != -1)
  752. {
  753. //string opt_key;
  754. //opt_key+=opt;
  755. switch (opt)
  756. {
  757. case 'p':
  758. //multi_process_mode=1;
  759. break;
  760. case 'k':
  761. sscanf(optarg,"%s\n",key_string);
  762. mylog(log_debug,"key=%s\n",key_string);
  763. if(strlen(key_string)==0)
  764. {
  765. mylog(log_fatal,"key len=0??\n");
  766. myexit(-1);
  767. }
  768. break;
  769. case 'm':
  770. sscanf(optarg,"%d\n",&max_pending_packet);
  771. if(max_pending_packet<1000)
  772. {
  773. mylog(log_fatal,"max_pending_packet must be >1000\n");
  774. myexit(-1);
  775. }
  776. break;
  777. case 'j':
  778. if (strchr(optarg, ':') == 0)
  779. {
  780. int jitter;
  781. sscanf(optarg,"%d\n",&jitter);
  782. if(jitter<0 ||jitter>1000*100)
  783. {
  784. mylog(log_fatal,"jitter must be between 0 and 100,000(10 second)\n");
  785. myexit(-1);
  786. }
  787. jitter_min=0;
  788. jitter_max=jitter;
  789. }
  790. else
  791. {
  792. sscanf(optarg,"%d:%d\n",&jitter_min,&jitter_max);
  793. if(jitter_min<0 ||jitter_max<0||jitter_min>jitter_max)
  794. {
  795. mylog(log_fatal," must satisfy 0<=jmin<=jmax\n");
  796. myexit(-1);
  797. }
  798. }
  799. break;
  800. case 't':
  801. if (strchr(optarg, ':') == 0)
  802. {
  803. int dup_delay=-1;
  804. sscanf(optarg,"%d\n",&dup_delay);
  805. if(dup_delay<1||dup_delay>1000*100)
  806. {
  807. mylog(log_fatal,"dup_delay must be between 1 and 100,000(10 second)\n");
  808. myexit(-1);
  809. }
  810. dup_delay_min=dup_delay_max=dup_delay;
  811. }
  812. else
  813. {
  814. sscanf(optarg,"%d:%d\n",&dup_delay_min,&dup_delay_max);
  815. if(dup_delay_min<1 ||dup_delay_max<1||dup_delay_min>dup_delay_max)
  816. {
  817. mylog(log_fatal," must satisfy 1<=dmin<=dmax\n");
  818. myexit(-1);
  819. }
  820. }
  821. break;
  822. case 'd':
  823. dup_num=-1;
  824. sscanf(optarg,"%d\n",&dup_num);
  825. if(dup_num<0 ||dup_num>5)
  826. {
  827. mylog(log_fatal,"dup_num must be between 0 and 5\n");
  828. myexit(-1);
  829. }
  830. dup_num+=1;
  831. break;
  832. case 'c':
  833. is_client = 1;
  834. break;
  835. case 's':
  836. is_server = 1;
  837. break;
  838. case 'l':
  839. no_l = 0;
  840. if (strchr(optarg, ':') != 0)
  841. {
  842. sscanf(optarg, "%[^:]:%d", local_address, &local_port);
  843. }
  844. else
  845. {
  846. mylog(log_fatal," -r ip:port\n");
  847. myexit(1);
  848. strcpy(local_address, "127.0.0.1");
  849. sscanf(optarg, "%d", &local_port);
  850. }
  851. break;
  852. case 'r':
  853. no_r = 0;
  854. if (strchr(optarg, ':') != 0)
  855. {
  856. //printf("in :\n");
  857. //printf("%s\n",optarg);
  858. sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
  859. //printf("%d\n",remote_port);
  860. }
  861. else
  862. {
  863. mylog(log_fatal," -r ip:port\n");
  864. myexit(1);
  865. strcpy(remote_address, "127.0.0.1");
  866. sscanf(optarg, "%d", &remote_port);
  867. }
  868. break;
  869. case 'h':
  870. break;
  871. case 1:
  872. if(strcmp(long_options[option_index].name,"log-level")==0)
  873. {
  874. }
  875. else if(strcmp(long_options[option_index].name,"disable-filter")==0)
  876. {
  877. disable_replay_filter=1;
  878. //enable_log_color=0;
  879. }
  880. else if(strcmp(long_options[option_index].name,"disable-color")==0)
  881. {
  882. //enable_log_color=0;
  883. }
  884. else if(strcmp(long_options[option_index].name,"log-position")==0)
  885. {
  886. enable_log_position=1;
  887. }
  888. else if(strcmp(long_options[option_index].name,"random-drop")==0)
  889. {
  890. sscanf(optarg,"%d",&random_drop);
  891. if(random_drop<0||random_drop>10000)
  892. {
  893. mylog(log_fatal,"random_drop must be between 0 10000 \n");
  894. myexit(-1);
  895. }
  896. }
  897. else if(strcmp(long_options[option_index].name,"report")==0)
  898. {
  899. sscanf(optarg,"%d",&report_interval);
  900. if(report_interval<=0)
  901. {
  902. mylog(log_fatal,"report_interval must be >0 \n");
  903. myexit(-1);
  904. }
  905. }
  906. else if(strcmp(long_options[option_index].name,"sock-buf")==0)
  907. {
  908. int tmp=-1;
  909. sscanf(optarg,"%d",&tmp);
  910. if(10<=tmp&&tmp<=10*1024)
  911. {
  912. socket_buf_size=tmp*1024;
  913. }
  914. else
  915. {
  916. mylog(log_fatal,"sock-buf value must be between 1 and 10240 (kbyte) \n");
  917. myexit(-1);
  918. }
  919. }
  920. else
  921. {
  922. mylog(log_fatal,"unknown option\n");
  923. myexit(-1);
  924. }
  925. break;
  926. default:
  927. mylog(log_fatal,"unknown option <%x>", opt);
  928. myexit(-1);
  929. }
  930. }
  931. if (no_l)
  932. mylog(log_fatal,"error: -i not found\n");
  933. if (no_r)
  934. mylog(log_fatal,"error: -o not found\n");
  935. if (no_l || no_r)
  936. myexit(-1);
  937. if (is_client == 0 && is_server == 0)
  938. {
  939. mylog(log_fatal,"-s -c hasnt been set\n");
  940. myexit(-1);
  941. }
  942. if (is_client == 1 && is_server == 1)
  943. {
  944. mylog(log_fatal,"-s -c cant be both set\n");
  945. myexit(-1);
  946. }
  947. }
  948. int main(int argc, char *argv[])
  949. {
  950. if(argc==1||argc==0)
  951. {
  952. printf("this_program classic\n");
  953. printf("this_program fec\n");
  954. return 0;
  955. }
  956. /*
  957. if(argc>=2&&strcmp(argv[1],"fec")!=0)
  958. {
  959. printf("running into classic mode!\n");
  960. return classic::main(argc,argv);
  961. }*/
  962. assert(sizeof(u64_t)==8);
  963. assert(sizeof(i64_t)==8);
  964. assert(sizeof(u32_t)==4);
  965. assert(sizeof(i32_t)==4);
  966. dup2(1, 2); //redirect stderr to stdout
  967. int i, j, k;
  968. process_arg(argc,argv);
  969. delay_manager.capacity=max_pending_packet;
  970. init_random_number_fd();
  971. remote_address_uint32=inet_addr(remote_address);
  972. event_loop();
  973. return 0;
  974. }