main.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<sys/socket.h>
  4. #include<arpa/inet.h>
  5. #include<stdlib.h>
  6. #include<getopt.h>
  7. #include <unistd.h>
  8. #include<errno.h>
  9. #include <fcntl.h>
  10. //#include"aes.h"
  11. #include <sys/epoll.h>
  12. #include <sys/wait.h>
  13. #include<map>
  14. #include<string>
  15. #include<vector>
  16. #include <sys/socket.h> //for socket ofcourse
  17. #include <sys/types.h>
  18. #include <stdlib.h> //for exit(0);
  19. #include <errno.h> //For errno - the error number
  20. #include <netinet/tcp.h> //Provides declarations for tcp header
  21. #include <netinet/ip.h> //Provides declarations for ip header
  22. #include <netinet/if_ether.h>
  23. #include <arpa/inet.h>
  24. #include <fcntl.h>
  25. #include <byteswap.h>
  26. #include <sys/socket.h>
  27. #include <sys/types.h>
  28. #include <arpa/inet.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/filter.h>
  31. #include <sys/time.h>
  32. #include <time.h>
  33. #include <sys/timerfd.h>
  34. #include <set>
  35. #include <encrypt.h>
  36. #include <inttypes.h>
  37. using namespace std;
  38. char local_address[100], remote_address[100],source_address[100];
  39. int local_port = -1, remote_port = -1;
  40. int epollfd ;
  41. uint32_t const_id=0;
  42. uint32_t oppsite_const_id=0;
  43. uint32_t my_id=0;
  44. uint32_t oppsite_id=0;
  45. uint32_t conv_id=0;
  46. const int handshake_timeout=2000;
  47. const int heartbeat_timeout=10000;
  48. const int udp_timeout=3000;
  49. const int heartbeat_interval=1000;
  50. const int timer_interval=500;
  51. const int RETRY_TIME=3;
  52. //const uint16_t tcp_window=50000;
  53. const int buf_len = 65535+100;
  54. const int server_mode=2;
  55. const int client_mode=1;
  56. int prog_mode=0; //0 unset; 1client 2server
  57. const int debug_mode=0;
  58. int bind_fd;
  59. int first_data_packet=0;
  60. const int seq_mode=2; //0 dont increase /1 increase //increase randomly,about every 5 packet
  61. const uint64_t epoll_timer_fd_sn=1;
  62. const uint64_t epoll_raw_recv_fd_sn=2;
  63. const uint64_t epoll_udp_fd_sn_begin=256;
  64. uint64_t epoll_udp_fd_sn=epoll_udp_fd_sn_begin; //udp_fd_sn =256,512,768......the lower 8 bit is not used,to avoid confliction
  65. const int server_nothing=0;
  66. const int server_syn_ack_sent=1;
  67. const int server_heartbeat_sent=2;
  68. const int server_ready=3;
  69. int server_current_state=server_nothing;
  70. long long last_hb_recv_time;
  71. long long last_udp_recv_time=0;
  72. int socket_buf_size=1024*1024*4;
  73. int udp_fd=-1;
  74. int raw_recv_fd;
  75. int raw_send_fd;
  76. const int client_nothing=0;
  77. const int client_syn_sent=1;
  78. const int client_ack_sent=2;
  79. const int client_heartbeat_sent=3;
  80. const int client_ready=4;
  81. int client_current_state=client_nothing;
  82. int retry_counter;
  83. long long last_state_time=0;
  84. long long last_hb_sent_time=0;
  85. char buf[buf_len];
  86. char buf2[buf_len];
  87. char raw_send_buf[buf_len];
  88. char raw_send_buf2[buf_len];
  89. char raw_recv_buf[buf_len];
  90. char raw_recv_buf2[buf_len];
  91. char raw_recv_buf3[buf_len];
  92. char replay_buf[buf_len];
  93. char send_data_buf[buf_len]; //buf for send data and send hb
  94. struct sock_filter code[] = {
  95. { 0x28, 0, 0, 0x0000000c },//0
  96. { 0x15, 0, 10, 0x00000800 },//1
  97. { 0x30, 0, 0, 0x00000017 },//2
  98. { 0x15, 0, 8, 0x00000006 },//3
  99. { 0x28, 0, 0, 0x00000014 },//4
  100. { 0x45, 6, 0, 0x00001fff },//5
  101. { 0xb1, 0, 0, 0x0000000e },//6
  102. { 0x48, 0, 0, 0x0000000e },//7
  103. { 0x15, 2, 0, 0x0000ef32 },//8
  104. { 0x48, 0, 0, 0x00000010 },//9
  105. { 0x15, 0, 1, 0x0000ef32 },//10
  106. { 0x6, 0, 0, 0x0000ffff },//11
  107. { 0x6, 0, 0, 0x00000000 },//12
  108. };
  109. sock_fprog bpf;
  110. uint16_t ip_id=1;
  111. //const int MTU=1440;
  112. struct sockaddr_in udp_old_addr_in;
  113. uint64_t seq=0;
  114. uint8_t key[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 0,0,0,0};
  115. const int window_size=2000;
  116. int random_number_fd=-1;
  117. void init_random_number_fd()
  118. {
  119. random_number_fd=open("/dev/urandom",O_RDONLY);
  120. if(random_number_fd==-1)
  121. {
  122. printf("error open /dev/urandom");
  123. exit(-1);
  124. }
  125. }
  126. uint32_t get_true_random_number()
  127. {
  128. uint32_t ret;
  129. read(random_number_fd,&ret,sizeof(ret));
  130. return htonl(ret);
  131. }
  132. struct anti_replay_t
  133. {
  134. uint64_t max_packet_received;
  135. char window[window_size];
  136. char disabled;
  137. anti_replay_t()
  138. {
  139. disabled=0;
  140. max_packet_received=0;
  141. //memset(window,0,sizeof(window)); //not necessary
  142. }
  143. void re_init()
  144. {
  145. disabled=0;
  146. max_packet_received=0;
  147. //memset(window,0,sizeof(window));
  148. }
  149. void disable()
  150. {
  151. disabled=1;
  152. }
  153. void enable()
  154. {
  155. disabled=0;
  156. }
  157. int is_vaild(uint64_t seq)
  158. {
  159. //if(disabled) return 0;
  160. if(seq==max_packet_received) return 0||disabled;
  161. else if(seq>max_packet_received)
  162. {
  163. if(seq-max_packet_received>=window_size)
  164. {
  165. memset(window,0,sizeof(window));
  166. window[seq%window_size]=1;
  167. }
  168. else
  169. {
  170. for (int i=max_packet_received+1;i<seq;i++)
  171. window[i%window_size]=0;
  172. window[seq%window_size]=1;
  173. }
  174. max_packet_received=seq;
  175. return 1;
  176. }
  177. else if(seq<max_packet_received)
  178. {
  179. if(max_packet_received-seq>=window_size) return 0||disabled;
  180. else
  181. {
  182. if (window[seq%window_size]==1) return 0||disabled;
  183. else
  184. {
  185. window[seq%window_size]=1;
  186. return 1;
  187. }
  188. }
  189. }
  190. }
  191. }anti_replay;
  192. int pre_send(char * data, int &data_len)
  193. {
  194. //return 0;
  195. if(data_len<0) return -3;
  196. seq++;
  197. uint32_t seq_high= htonl(seq>>32u);
  198. uint32_t seq_low= htonl((seq<<32u)>>32u);
  199. memcpy(replay_buf,&seq_high,sizeof(uint32_t));
  200. memcpy(replay_buf+sizeof(uint32_t),&seq_low,sizeof(uint32_t));
  201. memcpy(replay_buf+sizeof(uint32_t)*2,data,data_len);
  202. data_len+=sizeof(uint32_t)*2;
  203. //memcpy(data,replay_buf,data_len);
  204. if(my_encrypt((unsigned char*)replay_buf,(unsigned char*)data,data_len,key) <0)
  205. {
  206. printf("encrypt fail\n");
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. int pre_recv(char * data, int &data_len)
  212. {
  213. //return 0;
  214. if(data_len<0) return -1;
  215. //if(data_len<8+16) return -3;
  216. if(my_decrypt((uint8_t*)data,(uint8_t*)replay_buf,data_len,key) <0)
  217. {
  218. printf("decrypt fail\n");
  219. return -1;
  220. }
  221. data_len-=sizeof(uint32_t)*2;
  222. if(data_len<0)
  223. {
  224. printf("data_len<=0\n");
  225. return -2;
  226. }
  227. uint64_t seq_high= ntohl(*((uint32_t*)(replay_buf) ) );
  228. uint32_t seq_low= ntohl(*((uint32_t*)(replay_buf+sizeof(uint32_t)) ) );
  229. uint64_t recv_seq =(seq_high<<32u )+seq_low;
  230. if(anti_replay.is_vaild(recv_seq)!=1)
  231. {
  232. printf("dropped replay packet\n");
  233. return -1;
  234. }
  235. printf("<<<<<%ld,%d,%ld>>>>\n",seq_high,seq_low,recv_seq);
  236. memcpy(data,replay_buf+sizeof(uint32_t)*2,data_len);
  237. return 0;
  238. }
  239. void handler(int num) {
  240. int status;
  241. int pid;
  242. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  243. if (WIFEXITED(status)) {
  244. //printf("The child exit with code %d",WEXITSTATUS(status));
  245. }
  246. }
  247. }
  248. void setnonblocking(int sock) {
  249. int opts;
  250. opts = fcntl(sock, F_GETFL);
  251. if (opts < 0) {
  252. perror("fcntl(sock,GETFL)");
  253. exit(1);
  254. }
  255. opts = opts | O_NONBLOCK;
  256. if (fcntl(sock, F_SETFL, opts) < 0) {
  257. perror("fcntl(sock,SETFL,opts)");
  258. exit(1);
  259. }
  260. }
  261. int set_udp_buf_size(int fd)
  262. {
  263. if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  264. {
  265. printf("SO_SNDBUFFORCE fail\n");
  266. exit(1);
  267. }
  268. if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  269. {
  270. printf("SO_RCVBUFFORCE fail\n");
  271. exit(1);
  272. }
  273. return 0;
  274. }
  275. int init_raw_socket()
  276. {
  277. raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
  278. if(raw_send_fd == -1) {
  279. perror("Failed to create raw_send_fd");
  280. exit(1);
  281. }
  282. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  283. {
  284. printf("SO_SNDBUFFORCE fail\n");
  285. exit(1);
  286. }
  287. //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  288. raw_recv_fd= socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  289. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  290. {
  291. printf("SO_RCVBUFFORCE fail\n");
  292. exit(1);
  293. }
  294. //raw_fd=socket(AF_PACKET , SOCK_RAW , htons(ETH_P_IP));
  295. // packet_recv_sd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
  296. if(raw_recv_fd == -1) {
  297. //socket creation failed, may be because of non-root privileges
  298. perror("Failed to create raw_recv_fd");
  299. exit(1);
  300. }
  301. //IP_HDRINCL to tell the kernel that headers are included in the packet
  302. int one = 1;
  303. const int *val = &one;
  304. if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
  305. perror("Error setting IP_HDRINCL");
  306. exit(2);
  307. }
  308. return 0;
  309. }
  310. long long get_current_time()
  311. {
  312. timespec tmp_time;
  313. clock_gettime(CLOCK_MONOTONIC, &tmp_time);
  314. return tmp_time.tv_sec*1000+tmp_time.tv_nsec/(1000*1000ll);
  315. }
  316. const int conv_timeout=60000; //60 second
  317. const int conv_clear_ratio=10;
  318. void server_clear(uint64_t u64)
  319. {
  320. int fd=int((u64<<32u)>>32u);
  321. epoll_event ev;
  322. ev.events = EPOLLIN;
  323. ev.data.u64 = u64;
  324. int ret = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev);
  325. if (ret!=0)
  326. {
  327. printf("fd:%d epoll delete failed!!!!\n",fd);
  328. }
  329. ret= close(fd);
  330. if (ret!=0)
  331. {
  332. printf("close fd %d failed !!!!\n",fd);
  333. }
  334. }
  335. struct conv_manager_t
  336. {
  337. map<uint64_t,uint32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
  338. map<uint32_t,uint64_t> conv_to_u64;
  339. map<uint32_t,uint64_t> conv_last_active_time;
  340. map<uint32_t,uint64_t>::iterator clear_it;
  341. void (*clear_function)(uint64_t u64) ;
  342. conv_manager_t()
  343. {
  344. clear_it=conv_last_active_time.begin();
  345. clear_function=0;
  346. }
  347. void set_clear_function(void (*a)(uint64_t u64))
  348. {
  349. clear_function=a;
  350. }
  351. void clear()
  352. {
  353. if(clear_function!=0)
  354. {
  355. map<uint32_t,uint64_t>::iterator it;
  356. for(it=conv_last_active_time.begin();it!=conv_last_active_time.end();it++)
  357. {
  358. clear_function(it->second);
  359. }
  360. }
  361. u64_to_conv.clear();
  362. conv_to_u64.clear();
  363. conv_last_active_time.clear();
  364. clear_it=conv_last_active_time.begin();
  365. }
  366. uint32_t get_new_conv()
  367. {
  368. uint32_t conv=get_true_random_number();
  369. while(conv!=0&&conv_to_u64.find(conv)!=conv_to_u64.end())
  370. {
  371. conv=get_true_random_number();
  372. }
  373. return conv;
  374. }
  375. int is_conv_used(uint32_t conv)
  376. {
  377. return conv_to_u64.find(conv)!=conv_to_u64.end();
  378. }
  379. int is_u64_used(uint64_t u64)
  380. {
  381. return u64_to_conv.find(u64)!=u64_to_conv.end();
  382. }
  383. uint32_t find_conv_by_u64(uint64_t u64)
  384. {
  385. return u64_to_conv[u64];
  386. }
  387. uint64_t find_u64_by_conv(uint32_t conv)
  388. {
  389. return conv_to_u64[conv];
  390. }
  391. int update_active_time(uint32_t conv)
  392. {
  393. return conv_last_active_time[conv]=get_current_time();
  394. }
  395. int insert_conv(uint32_t conv,uint64_t u64)
  396. {
  397. u64_to_conv[u64]=conv;
  398. conv_to_u64[conv]=u64;
  399. conv_last_active_time[conv]=get_current_time();
  400. return 0;
  401. }
  402. int erase_conv(uint32_t conv)
  403. {
  404. uint64_t u64=conv_to_u64[conv];
  405. if(clear_function!=0)
  406. {
  407. clear_function(u64);
  408. }
  409. conv_to_u64.erase(conv);
  410. u64_to_conv.erase(u64);
  411. conv_last_active_time.erase(conv);
  412. return 0;
  413. }
  414. int clean_inactive( )
  415. {
  416. map<uint32_t,uint64_t>::iterator old_it;
  417. map<uint32_t,uint64_t>::iterator it;
  418. int cnt=0;
  419. it=clear_it;
  420. int size=conv_last_active_time.size();
  421. int num_to_clean=size/conv_clear_ratio; //clear 1/10 each time,to avoid latency glitch
  422. uint64_t current_time=get_current_time();
  423. for(;;)
  424. {
  425. if(cnt>=num_to_clean) break;
  426. if(conv_last_active_time.begin()==conv_last_active_time.end()) break;
  427. if(it==conv_last_active_time.end())
  428. {
  429. it=conv_last_active_time.begin();
  430. }
  431. if( current_time -it->second >conv_timeout )
  432. {
  433. printf("inactive conv %u cleared !!!!!!!!!!!!!!!!!!!!!!!!!\n",it->first);
  434. old_it=it;
  435. it++;
  436. erase_conv(old_it->first);
  437. }
  438. else
  439. {
  440. it++;
  441. }
  442. cnt++;
  443. }
  444. return 0;
  445. }
  446. }conv_manager;
  447. void init_filter(int port)
  448. {
  449. code[8].k=code[10].k=port;
  450. bpf.len = sizeof(code)/sizeof(code[0]);
  451. bpf.filter = code;
  452. //printf("<%d>\n",bpf.len);
  453. int dummy;
  454. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
  455. if (ret != 0)
  456. {
  457. printf("error remove fiter\n");
  458. perror("filter");
  459. //exit(-1);
  460. }
  461. ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
  462. //memset(code,0,sizeof(code));
  463. if (ret != 0)
  464. {
  465. printf("error set fiter\n");
  466. perror("filter");
  467. exit(-1);
  468. }
  469. }
  470. void process_arg(int argc, char *argv[])
  471. {
  472. int i,j,k,opt;
  473. static struct option long_options[] =
  474. {
  475. /* These options set a flag. */
  476. {"source-ip", required_argument, 0, 1},
  477. };
  478. int option_index = 0;
  479. printf("argc=%d ", argc);
  480. for (i = 0; i < argc; i++)
  481. printf("%s ", argv[i]);
  482. printf("\n");
  483. if (argc == 1)
  484. {
  485. printf(
  486. "proc -l [adress:]port -r [adress:]port [-a passwd] [-b passwd]\n");
  487. exit(-1);
  488. }
  489. int no_l = 1, no_r = 1;
  490. while ((opt = getopt_long(argc, argv, "l:r:sch",long_options,&option_index)) != -1) {
  491. //string opt_key;
  492. //opt_key+=opt;
  493. switch (opt) {
  494. case 'l':
  495. no_l = 0;
  496. if (strchr(optarg, ':') != 0) {
  497. sscanf(optarg, "%[^:]:%d", local_address, &local_port);
  498. } else {
  499. strcpy(local_address, "127.0.0.1");
  500. sscanf(optarg, "%d", &local_port);
  501. }
  502. break;
  503. case 'r':
  504. no_r = 0;
  505. if (strchr(optarg, ':') != 0) {
  506. sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
  507. } else {
  508. strcpy(remote_address, "127.0.0.1");
  509. sscanf(optarg, "%d", &remote_port);
  510. }
  511. break;
  512. case 's':
  513. if(prog_mode==0)
  514. {
  515. prog_mode=server_mode;
  516. }
  517. else
  518. {
  519. printf("-s /-c has already been set,-s option conflict");
  520. exit(-1);
  521. }
  522. break;
  523. case 'c':
  524. if(prog_mode==0)
  525. {
  526. prog_mode=client_mode;
  527. }
  528. else
  529. {
  530. printf("-s /-c has already been set,-c option conflict");
  531. exit(-1);
  532. }
  533. break;
  534. case 'h':
  535. break;
  536. case 1:
  537. //if (strchr(optarg, ':') != 0) {
  538. sscanf(optarg, "%s", source_address);
  539. printf("source: %s",source_address);
  540. //} else {
  541. //printf("format --source-ip :adress");
  542. //exit(-1);
  543. //}
  544. break;
  545. default:
  546. printf("ignore unknown <%s>", optopt);
  547. }
  548. }
  549. if (no_l)
  550. printf("error: -i not found\n");
  551. if (no_r)
  552. printf("error: -o not found\n");
  553. if(prog_mode==0)
  554. printf("error: -s /-r hasnt been set\n");
  555. if (no_l || no_r||prog_mode==0)
  556. {
  557. exit(-1);
  558. }
  559. }
  560. struct packet_info_t
  561. {
  562. //ip_part:
  563. uint32_t src_ip;
  564. uint16_t src_port;
  565. uint32_t dst_ip;
  566. uint16_t dst_port;
  567. //tcp_part:
  568. bool syn,ack,psh;
  569. uint32_t seq,ack_seq;
  570. uint32_t ts,ts_ack;
  571. }g_packet_info;
  572. struct pseudo_header {
  573. u_int32_t source_address;
  574. u_int32_t dest_address;
  575. u_int8_t placeholder;
  576. u_int8_t protocol;
  577. u_int16_t tcp_length;
  578. };
  579. /*
  580. Generic checksum calculation function
  581. */
  582. unsigned short csum(unsigned short *ptr,int nbytes) {
  583. register long sum;
  584. unsigned short oddbyte;
  585. register short answer;
  586. sum=0;
  587. while(nbytes>1) {
  588. sum+=*ptr++;
  589. nbytes-=2;
  590. }
  591. if(nbytes==1) {
  592. oddbyte=0;
  593. *((u_char*)&oddbyte)=*(u_char*)ptr;
  594. sum+=oddbyte;
  595. }
  596. sum = (sum>>16)+(sum & 0xffff);
  597. sum = sum + (sum>>16);
  598. answer=(short)~sum;
  599. return(answer);
  600. }
  601. int send_raw(packet_info_t &info,char * payload,int payloadlen)
  602. {
  603. if(prog_mode==client_mode&& payloadlen!=9 ||prog_mode==server_mode&& payloadlen!=5)
  604. printf("send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  605. char *data;
  606. memset(raw_send_buf,0,payloadlen+100);
  607. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  608. //TCP header
  609. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  610. struct sockaddr_in sin;
  611. struct pseudo_header psh;
  612. //some address resolution
  613. sin.sin_family = AF_INET;
  614. sin.sin_port = htons(info.dst_port);
  615. sin.sin_addr.s_addr = info.dst_ip;
  616. //Fill in the IP Header
  617. iph->ihl = 5;
  618. iph->version = 4;
  619. iph->tos = 0;
  620. iph->id = htonl (ip_id++); //Id of this packet
  621. iph->frag_off = htons(0x4000); //DF set,others are zero
  622. iph->ttl = 64;
  623. iph->protocol = IPPROTO_TCP;
  624. iph->check = 0; //Set to 0 before calculating checksum
  625. iph->saddr = info.src_ip; //Spoof the source ip address
  626. iph->daddr = info.dst_ip;
  627. //TCP Header
  628. tcph->source = htons(info.src_port);
  629. tcph->dest = htons(info.dst_port);
  630. tcph->seq =htonl(info.seq);
  631. tcph->ack_seq = htonl(info.ack_seq);
  632. tcph->fin=0;
  633. tcph->syn=info.syn;
  634. tcph->rst=0;
  635. tcph->psh=info.psh;
  636. tcph->ack=info.ack;
  637. if(tcph->syn==1)
  638. {
  639. tcph->doff = 10; //tcp header size
  640. int i=sizeof (struct iphdr)+20;
  641. raw_send_buf[i++]=0x02;//mss
  642. raw_send_buf[i++]=0x04;
  643. raw_send_buf[i++]=0x05;
  644. raw_send_buf[i++]=0xb4;
  645. //raw_send_buf[i++]=0x01;
  646. //raw_send_buf[i++]=0x01;
  647. raw_send_buf[i++]=0x04; //sack ok
  648. raw_send_buf[i++]=0x02; //sack ok
  649. raw_send_buf[i++]=0x08; //i=6;
  650. raw_send_buf[i++]=0x0a;
  651. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  652. i+=4;
  653. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  654. i+=4;
  655. raw_send_buf[i++]=0x01;
  656. raw_send_buf[i++]=0x03;
  657. raw_send_buf[i++]=0x03;
  658. raw_send_buf[i++]=0x05;
  659. }
  660. else
  661. {
  662. tcph->doff=8;
  663. int i=sizeof (struct iphdr)+20;
  664. raw_send_buf[i++]=0x01;
  665. raw_send_buf[i++]=0x01;
  666. raw_send_buf[i++]=0x08; //i=0;
  667. raw_send_buf[i++]=0x0a;
  668. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  669. i+=4;
  670. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  671. i+=4;
  672. }
  673. tcph->urg=0;
  674. //tcph->window = htons((uint16_t)(1024));
  675. tcph->window = htons((uint16_t)(10240+random()%100));
  676. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  677. tcph->urg_ptr = 0;
  678. //Data part
  679. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  680. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  681. memcpy(data , payload, payloadlen);
  682. psh.source_address = info.src_ip;
  683. psh.dest_address = sin.sin_addr.s_addr;
  684. psh.placeholder = 0;
  685. psh.protocol = IPPROTO_TCP;
  686. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  687. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  688. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  689. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  690. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  691. //Ip checksum
  692. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  693. if(prog_mode==client_mode&& payloadlen!=9 ||prog_mode==server_mode&& payloadlen!=5)
  694. printf("sent seq ack_seq len<%u %u %d>\n",g_packet_info.seq,g_packet_info.ack_seq,payloadlen);
  695. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  696. if(g_packet_info.syn==0&&g_packet_info.ack==1&&payloadlen!=0)
  697. {
  698. if(seq_mode==0)
  699. {
  700. }
  701. else if(seq_mode==1)
  702. {
  703. g_packet_info.seq+=payloadlen;
  704. }
  705. else if(seq_mode==2)
  706. {
  707. if(random()% 5==3 )
  708. g_packet_info.seq+=payloadlen;
  709. }
  710. }
  711. if(debug_mode) printf("<ret:%d>\n",ret);
  712. if(ret<0)
  713. {
  714. perror("raw send error");
  715. //printf("send error\n");
  716. }
  717. return 0;
  718. }
  719. int send_data(packet_info_t &info,char* data,int len,uint32_t id1,uint32_t id2,uint32_t conv_id)
  720. {
  721. int new_len=1+sizeof(my_id)*3+len;
  722. send_data_buf[0]='d';
  723. uint32_t tmp;
  724. tmp=htonl(id1);
  725. memcpy(send_data_buf+1,&tmp,sizeof(my_id));
  726. tmp=htonl(id2);
  727. memcpy(send_data_buf+1+sizeof(my_id),&tmp,sizeof(my_id));
  728. tmp=htonl(conv_id);
  729. memcpy(send_data_buf+1+sizeof(my_id)*2,&tmp,sizeof(my_id));
  730. memcpy(send_data_buf+1+sizeof(my_id)*3,data,len);
  731. if(pre_send(send_data_buf,new_len)<0)
  732. {
  733. return -1;
  734. }
  735. send_raw(info,send_data_buf,new_len);
  736. return 0;
  737. }
  738. const int hb_length=1+3*sizeof(uint32_t);
  739. int send_hb(packet_info_t &info,uint32_t id1,uint32_t id2 ,uint32_t id3)
  740. {
  741. int new_len=1+sizeof(my_id)*3;
  742. send_data_buf[0]='h';
  743. uint32_t tmp;
  744. tmp=htonl(id1);
  745. memcpy(send_data_buf+1,&tmp,sizeof(my_id));
  746. tmp=htonl(id2);
  747. memcpy(send_data_buf+1+sizeof(my_id),&tmp,sizeof(my_id));
  748. tmp=htonl(id3);
  749. memcpy(send_data_buf+1+sizeof(my_id)*2,&tmp,sizeof(my_id));
  750. if(pre_send(send_data_buf,new_len)<0)
  751. {
  752. return -1;
  753. }
  754. send_raw(info,send_data_buf,new_len);
  755. return 0;
  756. }
  757. /*int send_sync()
  758. {
  759. //g_packet_info.seq=3;
  760. g_packet_info.ack=0;
  761. g_packet_info.syn=1;
  762. //g_packet_info.ack_seq=5;
  763. g_packet_info.psh=0;
  764. send_raw(g_packet_info,0,0);
  765. return 0;
  766. }*/
  767. int try_to_list_and_bind(int port)
  768. {
  769. int old_bind_fd=bind_fd;
  770. bind_fd=socket(AF_INET,SOCK_STREAM,0);
  771. if(old_bind_fd!=-1)
  772. {
  773. close(old_bind_fd);
  774. }
  775. struct sockaddr_in temp_bind_addr;
  776. bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  777. temp_bind_addr.sin_family = AF_INET;
  778. temp_bind_addr.sin_port = htons(port);
  779. temp_bind_addr.sin_addr.s_addr = inet_addr(local_address);
  780. if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
  781. {
  782. printf("bind fail\n");
  783. return -1;
  784. }
  785. if(listen(bind_fd, SOMAXCONN) != 0 )
  786. {
  787. printf("listen fail\n");
  788. return -1;
  789. }
  790. return 0;
  791. }
  792. int client_bind_to_a_new_port()
  793. {
  794. int raw_send_port=10000+get_true_random_number()%(65535-10000);
  795. for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
  796. {
  797. if (try_to_list_and_bind(raw_send_port)==0)
  798. {
  799. return raw_send_port;
  800. }
  801. }
  802. printf("bind port fail\n");
  803. fflush(stdout);
  804. exit(-1);
  805. return -1;////for compiler check
  806. }
  807. int fake_tcp_keep_connection_client() //for client
  808. {
  809. conv_manager.clean_inactive();
  810. if(debug_mode)printf("timer!\n");
  811. //fflush(stdout);
  812. begin:
  813. if(client_current_state==client_nothing)
  814. {
  815. anti_replay.re_init(); // this is not safe
  816. client_current_state=client_syn_sent;
  817. last_state_time=get_current_time();
  818. printf("state changed from nothing to syn_sent\n");
  819. retry_counter=RETRY_TIME;
  820. g_packet_info.src_port=client_bind_to_a_new_port();
  821. printf("using port %d\n",g_packet_info.src_port);
  822. g_packet_info.src_ip=inet_addr(source_address);
  823. init_filter(g_packet_info.src_port);
  824. g_packet_info.seq=get_true_random_number();
  825. g_packet_info.ack_seq=0;//get_true_random_number();
  826. g_packet_info.ts_ack=0;
  827. g_packet_info.ack=0;
  828. g_packet_info.syn=1;
  829. g_packet_info.psh=0;
  830. send_raw(g_packet_info,0,0);
  831. }
  832. if(client_current_state==client_syn_sent &&get_current_time()-last_state_time>handshake_timeout)
  833. {
  834. if(retry_counter==0)
  835. {
  836. client_current_state=client_nothing;
  837. printf("state back to nothing\n");
  838. return 0;
  839. //goto begin;
  840. }
  841. else
  842. {
  843. retry_counter--;
  844. printf("retry send sync\n");
  845. send_raw(g_packet_info,0,0);
  846. last_state_time=get_current_time();
  847. }
  848. }
  849. if(client_current_state==client_ack_sent &&get_current_time()-last_state_time>handshake_timeout)
  850. {
  851. if(retry_counter==0)
  852. {
  853. client_current_state=client_nothing;
  854. printf("state back to nothing\n");
  855. return 0;
  856. //goto begin;
  857. }
  858. else
  859. {
  860. retry_counter--;
  861. send_raw(g_packet_info,0,0);
  862. last_state_time=get_current_time();
  863. printf("retry send ack counter left:%d\n",retry_counter);
  864. }
  865. }
  866. if(client_current_state==client_heartbeat_sent&&get_current_time()-last_state_time>handshake_timeout)
  867. {
  868. if(retry_counter==0)
  869. {
  870. client_current_state=client_nothing;
  871. printf("state back to nothing\n");
  872. return 0;
  873. //goto begin;
  874. }
  875. else
  876. {
  877. retry_counter--;
  878. send_hb(g_packet_info,my_id,oppsite_id,const_id);
  879. last_state_time=get_current_time();
  880. printf("retry send heart_beat counter left:%d\n",retry_counter);
  881. printf("heartbeat sent <%x,%x>\n",oppsite_id,my_id);
  882. }
  883. }
  884. if(client_current_state==client_ready)
  885. {
  886. if(debug_mode)printf("time %lld %lld\n",get_current_time(),last_state_time);
  887. if(get_current_time()-last_hb_recv_time>heartbeat_timeout)
  888. {
  889. client_current_state=client_nothing;
  890. my_id=get_true_random_number();
  891. printf("state back to nothing\n");
  892. return 0;
  893. }
  894. if(get_current_time()-last_hb_sent_time<heartbeat_interval)
  895. {
  896. return 0;
  897. }
  898. if(debug_mode)printf("heartbeat sent <%x,%x>\n",oppsite_id,my_id);
  899. send_hb(g_packet_info,my_id,oppsite_id,const_id);
  900. last_hb_sent_time=get_current_time();
  901. }
  902. }
  903. int fake_tcp_keep_connection_server()
  904. {
  905. conv_manager.clean_inactive();
  906. //begin:
  907. if(debug_mode) printf("timer!\n");
  908. if(server_current_state==server_nothing)
  909. {
  910. return 0;
  911. }
  912. if(server_current_state==server_syn_ack_sent &&get_current_time()-last_state_time>handshake_timeout )
  913. {
  914. if(retry_counter==0)
  915. {
  916. server_current_state=server_nothing;
  917. printf("state back to nothing\n");
  918. }
  919. else
  920. {
  921. retry_counter--;
  922. send_raw(g_packet_info,0,0);
  923. last_state_time=get_current_time();
  924. printf("resend syn ack\n");
  925. }
  926. }
  927. if(server_current_state==server_heartbeat_sent &&get_current_time()-last_state_time>handshake_timeout)
  928. {
  929. if(retry_counter==0)
  930. {
  931. server_current_state=server_nothing;
  932. printf("state back to nothing\n");
  933. }
  934. else
  935. {
  936. retry_counter--;
  937. send_hb(g_packet_info,my_id,0,const_id);
  938. last_state_time=get_current_time();
  939. printf("half heart beat sent<%x>\n",my_id);
  940. }
  941. }
  942. if(server_current_state==server_ready)
  943. {
  944. if( get_current_time()-last_hb_recv_time>heartbeat_timeout )
  945. {
  946. printf("%lld %lld",get_current_time(),last_state_time);
  947. server_current_state=server_nothing;
  948. printf("changed session id\n");
  949. my_id=get_true_random_number();
  950. printf("state back to nothing\n");
  951. printf("changed state to server_nothing111\n");
  952. return 0;
  953. }
  954. if(get_current_time()-last_hb_sent_time<heartbeat_interval)
  955. {
  956. return 0;
  957. }
  958. send_hb(g_packet_info,my_id,oppsite_id,const_id);
  959. last_hb_sent_time=get_current_time();
  960. if(debug_mode) printf("heart beat sent<%x>\n",my_id);
  961. }
  962. }
  963. int set_timer(int epollfd,int &timer_fd)
  964. {
  965. int ret;
  966. epoll_event ev;
  967. itimerspec its;
  968. memset(&its,0,sizeof(its));
  969. if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  970. {
  971. printf("timer_fd create error");
  972. exit(1);
  973. }
  974. its.it_interval.tv_nsec=timer_interval*1000ll*1000ll;
  975. its.it_value.tv_nsec=1; //imidiately
  976. timerfd_settime(timer_fd,0,&its,0);
  977. ev.events = EPOLLIN;
  978. ev.data.u64 = epoll_timer_fd_sn;
  979. epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
  980. if (ret < 0) {
  981. printf("epoll_ctl return %d\n", ret);
  982. exit(-1);
  983. }
  984. }
  985. int client_raw_recv(iphdr *iph,tcphdr *tcph,char * data,int data_len)
  986. {
  987. if(client_current_state==client_syn_sent )
  988. {
  989. if (!(tcph->syn==1&&tcph->ack==1&&data_len==0)) return 0;
  990. if(iph->saddr!=g_packet_info.dst_ip||ntohs(tcph->source)!=g_packet_info.dst_port)
  991. {
  992. printf("unexpected adress %d %d %d %d\n",iph->saddr,g_packet_info.dst_ip,ntohl(tcph->source),g_packet_info.dst_port);
  993. return 0;
  994. }
  995. g_packet_info.ack_seq=ntohl(tcph->seq)+1;
  996. g_packet_info.psh=0;
  997. g_packet_info.syn=0;
  998. g_packet_info.ack=1;
  999. g_packet_info.seq+=1;
  1000. printf("sent ack back\n");
  1001. send_raw(g_packet_info,0,0);
  1002. client_current_state=client_ack_sent;
  1003. last_state_time=get_current_time();
  1004. retry_counter=RETRY_TIME;
  1005. printf("changed state to client_ack_sent\n");
  1006. }
  1007. if(client_current_state==client_ack_sent )
  1008. {
  1009. if( tcph->syn==1||tcph->ack!=1 ||data_len==0)
  1010. {
  1011. printf("unexpected syn ack or other zero lenght packet\n");
  1012. return 0;
  1013. }
  1014. if(data_len<hb_length||data[0]!='h')
  1015. {
  1016. printf("not a heartbeat\n");
  1017. return 0;
  1018. }
  1019. if(iph->saddr!=g_packet_info.dst_ip||ntohs(tcph->source)!=g_packet_info.dst_port)
  1020. {
  1021. printf("unexpected adress\n");
  1022. return 0;
  1023. }
  1024. oppsite_id= ntohl(* ((uint32_t *)&data[1]));
  1025. printf("====first hb received %x\n==",oppsite_id);
  1026. printf("changed state to client_heartbeat_sent\n");
  1027. send_hb(g_packet_info,my_id,oppsite_id,const_id);
  1028. client_current_state=client_heartbeat_sent;
  1029. last_state_time=get_current_time();
  1030. retry_counter=RETRY_TIME;
  1031. }
  1032. if(client_current_state==client_heartbeat_sent)
  1033. {
  1034. if( tcph->syn==1||tcph->ack!=1 ||data_len==0)
  1035. {
  1036. printf("unexpected syn ack or other zero lenght packet\n");
  1037. return 0;
  1038. }
  1039. if(data_len<hb_length||data[0]!='h')
  1040. {
  1041. printf("not a heartbeat\n");
  1042. return 0;
  1043. }
  1044. if(iph->saddr!=g_packet_info.dst_ip||ntohs(tcph->source)!=g_packet_info.dst_port)
  1045. {
  1046. printf("unexpected adress\n");
  1047. return 0;
  1048. }
  1049. uint32_t tmp_my_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
  1050. if(tmp_my_id!=my_id)
  1051. {
  1052. printf("auth fail\n");
  1053. return 0;
  1054. }
  1055. uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
  1056. if(tmp_oppsite_session_id!=oppsite_id)
  1057. {
  1058. printf("oppsite id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
  1059. return 0;
  1060. }
  1061. printf("changed state to client_ready\n");
  1062. client_current_state=client_ready;
  1063. last_state_time=get_current_time();
  1064. last_hb_recv_time=get_current_time();
  1065. }
  1066. if(client_current_state==client_ready )
  1067. {
  1068. if( tcph->syn==1||tcph->ack!=1 ||data_len==0)
  1069. {
  1070. printf("unexpected syn ack");
  1071. return 0;
  1072. }
  1073. if(iph->saddr!=g_packet_info.dst_ip||ntohs(tcph->source)!=g_packet_info.dst_port)
  1074. {
  1075. printf("unexpected adress\n");
  1076. return 0;
  1077. }
  1078. if(data_len>=hb_length&&data[0]=='h')
  1079. {
  1080. if(debug_mode)printf("heart beat received\n");
  1081. last_hb_recv_time=get_current_time();
  1082. return 0;
  1083. }
  1084. else if(data_len>=sizeof(my_id)*3+1&&data[0]=='d')
  1085. {
  1086. printf("received a data from fake tcp,len:%d\n",data_len);
  1087. uint32_t tmp_session_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
  1088. if(tmp_session_id!=my_id)
  1089. {
  1090. printf("client session id mismatch%x %x,ignore\n",tmp_session_id,my_id);
  1091. return 0;
  1092. }
  1093. uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
  1094. if(tmp_oppsite_session_id!=oppsite_id)
  1095. {
  1096. printf("server session id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
  1097. return 0;
  1098. }
  1099. last_hb_recv_time=get_current_time();
  1100. uint32_t tmp_conv_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
  1101. /*
  1102. if(tmp_conv_id!=conv_id)
  1103. {
  1104. printf("conv id mismatch%x %x,ignore\n",tmp_oppsite_session_id,my_id);
  1105. return 0;
  1106. }*/
  1107. if(!conv_manager.is_conv_used(tmp_conv_id))
  1108. {
  1109. printf("unknow conv %d,ignore\n",tmp_conv_id);
  1110. return 0;
  1111. }
  1112. conv_manager.update_active_time(tmp_conv_id);
  1113. uint64_t u64=conv_manager.find_u64_by_conv(tmp_conv_id);
  1114. sockaddr_in tmp_sockaddr;
  1115. memset(&tmp_sockaddr,0,sizeof(tmp_sockaddr));
  1116. tmp_sockaddr.sin_family = AF_INET;
  1117. tmp_sockaddr.sin_addr.s_addr=(u64>>32u);
  1118. tmp_sockaddr.sin_port= htons(uint16_t((u64<<32u)>>32u));
  1119. int ret=sendto(udp_fd,data+1+sizeof(my_id)*3,data_len -(1+sizeof(my_id)*3),0,(struct sockaddr *)&tmp_sockaddr,sizeof(tmp_sockaddr));
  1120. if(ret<0)perror("ret<0");
  1121. printf("%s :%d\n",inet_ntoa(tmp_sockaddr.sin_addr),ntohs(tmp_sockaddr.sin_port));
  1122. printf("%d byte sent!!!!!!!!!!!!!!!!!!\n",ret);
  1123. }
  1124. return 0;
  1125. }
  1126. }
  1127. int server_raw_recv(iphdr * iph,tcphdr *tcph,char * data,int data_len)
  1128. {
  1129. if(server_current_state==server_nothing)
  1130. {
  1131. anti_replay.re_init();
  1132. if(!( tcph->syn==1&&tcph->ack==0 &&data_len==0)) return 0;
  1133. g_packet_info.dst_port=ntohs(tcph->source);
  1134. g_packet_info.dst_ip=iph->saddr;
  1135. g_packet_info.ack_seq=ntohl(tcph->seq)+1;
  1136. g_packet_info.psh=0;
  1137. g_packet_info.syn=1;
  1138. g_packet_info.ack=1;
  1139. g_packet_info.seq=get_true_random_number();//not necessary to set
  1140. printf("sent syn ack\n");
  1141. send_raw(g_packet_info,0,0);
  1142. printf("changed state to server_syn_ack_sent\n");
  1143. server_current_state=server_syn_ack_sent;
  1144. retry_counter=RETRY_TIME;
  1145. last_state_time=get_current_time();
  1146. }
  1147. else if(server_current_state==server_syn_ack_sent)
  1148. {
  1149. if(!( tcph->syn==0&&tcph->ack==1 &&data_len==0)) return 0;
  1150. if(iph->saddr!=g_packet_info.dst_ip||ntohs(tcph->source)!=g_packet_info.dst_port)
  1151. {
  1152. printf("unexpected adress\n");
  1153. return 0;
  1154. }
  1155. g_packet_info.syn=0;
  1156. g_packet_info.ack=1;
  1157. g_packet_info.seq+=1;////////is this right?
  1158. send_hb(g_packet_info,my_id,0,const_id); // send a hb immidately
  1159. printf("changed state to server_heartbeat_sent\n");
  1160. server_current_state=server_heartbeat_sent;
  1161. last_state_time=get_current_time();
  1162. retry_counter=RETRY_TIME;
  1163. }
  1164. else if(server_current_state==server_heartbeat_sent)//heart beat received
  1165. {
  1166. if( tcph->syn==1||tcph->ack!=1 ||data_len==0) return 0;
  1167. if(iph->saddr!=g_packet_info.dst_ip||ntohs(tcph->source)!=g_packet_info.dst_port)
  1168. {
  1169. printf("unexpected adress\n");
  1170. return 0;
  1171. }
  1172. if(data_len<hb_length||data[0]!='h')
  1173. {
  1174. return 0;
  1175. }
  1176. uint32_t tmp_session_id= ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
  1177. uint32_t tmp_oppsite_const_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
  1178. if(oppsite_const_id!=0&&tmp_oppsite_const_id!=oppsite_const_id)
  1179. {
  1180. conv_manager.clear();
  1181. }
  1182. oppsite_const_id=tmp_oppsite_const_id;
  1183. printf("received hb %x %x\n",oppsite_id,tmp_session_id);
  1184. if(tmp_session_id!=my_id)
  1185. {
  1186. printf("auth fail!!\n");
  1187. return 0;
  1188. }
  1189. int tmp_oppsite_session_id= ntohl(* ((uint32_t *)&data[1]));
  1190. oppsite_id=tmp_oppsite_session_id;
  1191. send_hb(g_packet_info,my_id,oppsite_id,const_id);
  1192. server_current_state=server_ready;
  1193. last_state_time=get_current_time();
  1194. last_hb_recv_time=get_current_time();
  1195. //first_data_packet=1;
  1196. printf("changed state to server_ready\n");
  1197. }
  1198. else if(server_current_state==server_ready)
  1199. {
  1200. if( tcph->syn==1||tcph->ack!=1 ||data_len==0) return 0;
  1201. if(iph->saddr!=g_packet_info.dst_ip||ntohs(tcph->source)!=g_packet_info.dst_port)
  1202. {
  1203. printf("unexpected adress\n");
  1204. return 0;
  1205. }
  1206. if(data[0]=='h'&&data_len>=hb_length)
  1207. {
  1208. uint32_t tmp= ntohl(* ((uint32_t *)&data[1+sizeof(uint32_t)]));
  1209. if(debug_mode)printf("received hb <%x,%x>\n",oppsite_id,tmp);
  1210. last_hb_recv_time=get_current_time();
  1211. return 0;
  1212. }
  1213. else if(data[0]=='d'&&data_len>=sizeof(my_id)*3+1)
  1214. {
  1215. uint32_t tmp_oppsite_session_id=ntohl(* ((uint32_t *)&data[1]));
  1216. uint32_t tmp_session_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)]));
  1217. uint32_t tmp_conv_id=ntohl(* ((uint32_t *)&data[1+sizeof(my_id)*2]));
  1218. if(tmp_session_id!=my_id)
  1219. {
  1220. printf("my id mismatch,ignore\n");
  1221. return 0;
  1222. }
  1223. if(tmp_oppsite_session_id!=oppsite_id)
  1224. {
  1225. printf("oppsite id mismatch,ignore\n");
  1226. return 0;
  1227. }
  1228. last_hb_recv_time=get_current_time();
  1229. printf("<<<<conv:%u>>>>\n",tmp_conv_id);
  1230. if(!conv_manager.is_conv_used(tmp_conv_id))
  1231. {
  1232. struct sockaddr_in remote_addr_in;
  1233. socklen_t slen = sizeof(sockaddr_in);
  1234. memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  1235. remote_addr_in.sin_family = AF_INET;
  1236. remote_addr_in.sin_port = htons(remote_port);
  1237. remote_addr_in.sin_addr.s_addr = inet_addr(remote_address);
  1238. int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  1239. if(new_udp_fd<0)
  1240. {
  1241. printf("create udp_fd error");
  1242. return -1;
  1243. }
  1244. set_udp_buf_size(new_udp_fd);
  1245. printf("created new udp_fd %d\n",new_udp_fd);
  1246. int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
  1247. if(ret!=0)
  1248. {
  1249. printf("udp fd connect fail\n");
  1250. close(new_udp_fd);
  1251. return -1;
  1252. }
  1253. struct epoll_event ev;
  1254. uint64_t u64=((u_int64_t(tmp_conv_id))<<32u)+(uint32_t)new_udp_fd;
  1255. printf("u64: %ld\n",u64);
  1256. ev.events = EPOLLIN;
  1257. ev.data.u64 = u64;
  1258. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev);
  1259. if (ret!= 0) {
  1260. printf("add udp_fd error\n");
  1261. close(new_udp_fd);
  1262. return -1;
  1263. }
  1264. conv_manager.insert_conv(tmp_conv_id,u64);
  1265. }
  1266. uint64_t u64=conv_manager.find_u64_by_conv(tmp_conv_id);
  1267. conv_manager.update_active_time(tmp_conv_id);
  1268. int fd=int((u64<<32u)>>32u);
  1269. printf("received a data from fake tcp,len:%d\n",data_len);
  1270. int ret=send(fd,data+1+sizeof(my_id)*3,data_len -(1+sizeof(my_id)*3),0);
  1271. printf("%d byte sent ,fd :%d\n ",ret,fd);
  1272. if(ret<0)
  1273. {
  1274. perror("what happened????");
  1275. }
  1276. /*
  1277. if(first_data_packet==0&& tmp_conv_id!=conv_id) //magic to find out which one is actually larger
  1278. //consider 0xffffffff+1= 0x0 ,in this case 0x0 is "actually" larger
  1279. {
  1280. uint32_t smaller,bigger;
  1281. smaller=min(conv_id,tmp_conv_id);//smaller in normal sense
  1282. bigger=max(conv_id,tmp_conv_id);
  1283. uint32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
  1284. if(distance==bigger-smaller)
  1285. {
  1286. if(bigger==conv_id) //received_session_id is acutally bigger
  1287. {
  1288. printf("old_session_id ,ingored1\n");
  1289. return 0;
  1290. }
  1291. }
  1292. else
  1293. {
  1294. if(smaller==conv_id) //received_session_id is acutally bigger
  1295. {
  1296. printf("old_session_id ,ingored2\n");
  1297. return 0;
  1298. }
  1299. }
  1300. }
  1301. first_data_packet=0;
  1302. if(udp_fd==-1||tmp_conv_id!=conv_id)// this is first send or client changed session
  1303. {
  1304. int old_fd=udp_fd;
  1305. struct sockaddr_in remote_addr_in;
  1306. socklen_t slen = sizeof(sockaddr_in);
  1307. memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  1308. remote_addr_in.sin_family = AF_INET;
  1309. remote_addr_in.sin_port = htons(remote_port);
  1310. remote_addr_in.sin_addr.s_addr = inet_addr(remote_address);
  1311. udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  1312. set_udp_buf_size(udp_fd);
  1313. printf("created new udp_fd");
  1314. int ret = connect(udp_fd, (struct sockaddr *) &remote_addr_in, slen);
  1315. if(ret!=0)
  1316. {
  1317. printf("udp fd connect fail\n");
  1318. }
  1319. struct epoll_event ev;
  1320. ev.events = EPOLLIN;
  1321. epoll_udp_fd_sn+=256;
  1322. ev.data.u64 = epoll_udp_fd_sn;
  1323. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_fd, &ev);
  1324. if (ret!= 0) {
  1325. printf("add udp_fd error\n");
  1326. exit(-1);
  1327. }
  1328. if(old_fd!=-1)
  1329. {
  1330. epoll_ctl(epollfd, EPOLL_CTL_DEL, old_fd, 0);
  1331. close(old_fd);
  1332. }
  1333. }
  1334. if(tmp_conv_id!=conv_id)
  1335. {
  1336. conv_id=tmp_conv_id;
  1337. }
  1338. */
  1339. }
  1340. }
  1341. }
  1342. int on_raw_recv(iphdr * & iph,tcphdr * &tcph,char * &data,int &data_len)
  1343. {
  1344. int size;
  1345. struct sockaddr saddr;
  1346. socklen_t saddr_size;
  1347. saddr_size = sizeof(saddr);
  1348. if(debug_mode)printf("raw!\n");
  1349. size = recvfrom(raw_recv_fd, buf, buf_len, 0 ,&saddr , &saddr_size);
  1350. if(buf[12]!=8||buf[13]!=0)
  1351. {
  1352. printf("not an ipv4 packet!\n");
  1353. fflush(stdout);
  1354. return 0;
  1355. }
  1356. char *ip_begin=buf+14;
  1357. iph = (struct iphdr *) (ip_begin);
  1358. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  1359. if(debug_mode) printf("iph ihl error");
  1360. return 0;
  1361. }
  1362. if (iph->protocol != IPPROTO_TCP) {
  1363. if(debug_mode)printf("iph protocal != tcp\n");
  1364. return 0;
  1365. }
  1366. int ip_len=ntohs(iph->tot_len);
  1367. unsigned short iphdrlen =iph->ihl*4;
  1368. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  1369. unsigned short tcphdrlen = tcph->doff*4;
  1370. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  1371. if(debug_mode) printf("tcph error");
  1372. return 0;
  1373. }
  1374. /////ip
  1375. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  1376. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  1377. /////ip end
  1378. ///tcp
  1379. struct pseudo_header psh;
  1380. psh.source_address = iph->saddr;
  1381. psh.dest_address = iph->daddr;
  1382. psh.placeholder = 0;
  1383. psh.protocol = IPPROTO_TCP;
  1384. psh.tcp_length = htons(ip_len-iphdrlen);
  1385. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  1386. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  1387. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  1388. if(ip_chk!=0)
  1389. {
  1390. printf("ip header error %d\n",ip_chk);
  1391. return 0;
  1392. }
  1393. if(tcp_chk!=0)
  1394. {
  1395. printf("tcp_chk:%x\n",tcp_chk);
  1396. printf("tcp header error\n");
  1397. return 0;
  1398. }
  1399. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //data
  1400. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1401. if(tcph->doff==10)
  1402. {
  1403. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1404. {
  1405. g_packet_info.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1406. }
  1407. }
  1408. if(tcph->doff==8)
  1409. {
  1410. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  1411. {
  1412. g_packet_info.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1413. }
  1414. }
  1415. if(tcph->rst==1)
  1416. {
  1417. printf("%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  1418. }
  1419. ////tcp end
  1420. // char pseudo_tcp_buffer[MTU];
  1421. data_len = ip_len-tcphdrlen-iphdrlen;
  1422. data=ip_begin+tcphdrlen+iphdrlen;
  1423. if(data_len>0&&data[0]=='h')
  1424. {
  1425. printf("recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), data_len);
  1426. }
  1427. if(data_len>0&&tcph->syn==0&&tcph->ack==1)
  1428. {
  1429. //if(seq_increse)
  1430. g_packet_info.ack_seq=ntohl(tcph->seq)+(uint32_t)data_len;
  1431. }
  1432. //printf("%d\n",ip_len);
  1433. /*
  1434. for(int i=0;i<size;i++)
  1435. {
  1436. printf("<%x>",(unsigned char)buf[i]);
  1437. }
  1438. printf("\n");
  1439. for(int i=0;i<data_len;i++)
  1440. {
  1441. printf("<%x>",(unsigned char)data[i]);
  1442. }*/
  1443. if(debug_mode)
  1444. {
  1445. printf("\n");
  1446. printf("<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,data_len);
  1447. //fflush(stdout);
  1448. }
  1449. return 0;
  1450. }
  1451. int client()
  1452. {
  1453. int i, j, k;int ret;
  1454. init_raw_socket();
  1455. my_id=get_true_random_number();
  1456. conv_id=get_true_random_number();
  1457. //init_filter(source_port);
  1458. g_packet_info.dst_ip=inet_addr(remote_address);
  1459. g_packet_info.dst_port=remote_port;
  1460. //g_packet_info.src_ip=inet_addr(source_address);
  1461. //g_packet_info.src_port=source_port;
  1462. udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  1463. set_udp_buf_size(udp_fd);
  1464. int yes = 1;
  1465. //setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  1466. struct sockaddr_in local_me;
  1467. socklen_t slen = sizeof(sockaddr_in);
  1468. memset(&local_me, 0, sizeof(local_me));
  1469. local_me.sin_family = AF_INET;
  1470. local_me.sin_port = htons(local_port);
  1471. local_me.sin_addr.s_addr = inet_addr(local_address);
  1472. if (bind(udp_fd, (struct sockaddr*) &local_me, slen) == -1) {
  1473. perror("socket bind error");
  1474. exit(1);
  1475. }
  1476. setnonblocking(udp_fd);
  1477. int epollfd = epoll_create1(0);
  1478. const int max_events = 4096;
  1479. struct epoll_event ev, events[max_events];
  1480. if (epollfd < 0) {
  1481. printf("epoll return %d\n", epollfd);
  1482. exit(-1);
  1483. }
  1484. ev.events = EPOLLIN;
  1485. ev.data.u64 = epoll_udp_fd_sn;
  1486. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, udp_fd, &ev);
  1487. if (ret!=0) {
  1488. printf("add udp_listen_fd error\n");
  1489. exit(-1);
  1490. }
  1491. ev.events = EPOLLIN;
  1492. ev.data.u64 = epoll_raw_recv_fd_sn;
  1493. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
  1494. if (ret!= 0) {
  1495. printf("add raw_fd error\n");
  1496. exit(-1);
  1497. }
  1498. ////add_timer for fake_tcp_keep_connection_client
  1499. //sleep(10);
  1500. memset(&udp_old_addr_in,0,sizeof(sockaddr_in));
  1501. int unbind=1;
  1502. int timer_fd;
  1503. set_timer(epollfd,timer_fd);
  1504. while(1)////////////////////////
  1505. {
  1506. int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000);
  1507. if (nfds < 0) { //allow zero
  1508. printf("epoll_wait return %d\n", nfds);
  1509. exit(-1);
  1510. }
  1511. int n;
  1512. for (n = 0; n < nfds; ++n) {
  1513. if (events[n].data.u64 == epoll_raw_recv_fd_sn)
  1514. {
  1515. iphdr *iph;tcphdr *tcph;char* data;int data_len;
  1516. on_raw_recv(iph,tcph,data,data_len);
  1517. int new_len=data_len;
  1518. memcpy(raw_recv_buf3,data,new_len);
  1519. if(data_len!=0)
  1520. {
  1521. if(pre_recv(raw_recv_buf3,new_len)<0)
  1522. continue;
  1523. }
  1524. client_raw_recv(iph,tcph,raw_recv_buf3,new_len);
  1525. }
  1526. if(events[n].data.u64 ==epoll_timer_fd_sn)
  1527. {
  1528. //printf("timer!\n");
  1529. //fflush(stdout);
  1530. uint64_t value;
  1531. read(timer_fd, &value, 8);
  1532. fake_tcp_keep_connection_client();
  1533. }
  1534. if (events[n].data.u64 == epoll_udp_fd_sn)
  1535. {
  1536. socklen_t recv_len;
  1537. struct sockaddr_in udp_new_addr_in;
  1538. if ((recv_len = recvfrom(udp_fd, buf, buf_len, 0,
  1539. (struct sockaddr *) &udp_new_addr_in, &slen)) == -1) {
  1540. printf("recv_from error");
  1541. exit(1);
  1542. };
  1543. printf("Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr),
  1544. ntohs(udp_new_addr_in.sin_port),recv_len);
  1545. /*
  1546. if(udp_old_addr_in.sin_addr.s_addr==0&&udp_old_addr_in.sin_port==0)
  1547. {
  1548. memcpy(&udp_old_addr_in,&udp_new_addr_in,sizeof(udp_new_addr_in));
  1549. }
  1550. else if(udp_new_addr_in.sin_addr.s_addr!=udp_old_addr_in.sin_addr.s_addr
  1551. ||udp_new_addr_in.sin_port!=udp_old_addr_in.sin_port)
  1552. {
  1553. if(get_current_time()- last_udp_recv_time <udp_timeout)
  1554. {
  1555. printf("new <ip,port> connected in,ignored,bc last connection is still active\n");
  1556. continue;
  1557. }
  1558. else
  1559. {
  1560. printf("new <ip,port> connected in,accpeted\n");
  1561. memcpy(&udp_old_addr_in,&udp_new_addr_in,sizeof(udp_new_addr_in));
  1562. conv_id++;
  1563. }
  1564. }*/
  1565. //last_udp_recv_time=get_current_time();
  1566. uint64_t u64=((uint64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
  1567. uint32_t conv;
  1568. if(!conv_manager.is_u64_used(u64))
  1569. {
  1570. printf("new connection!!!!!!!!!!!\n");
  1571. conv=conv_manager.get_new_conv();
  1572. conv_manager.insert_conv(conv,u64);
  1573. }
  1574. else
  1575. {
  1576. conv=conv_manager.find_conv_by_u64(u64);
  1577. }
  1578. conv_manager.update_active_time(conv);
  1579. if(client_current_state==client_ready)
  1580. {
  1581. send_data(g_packet_info,buf,recv_len,my_id,oppsite_id,conv);
  1582. }
  1583. }
  1584. }
  1585. }
  1586. return 0;
  1587. }
  1588. int server()
  1589. {
  1590. conv_manager.set_clear_function(server_clear);
  1591. int i, j, k;int ret;
  1592. g_packet_info.src_ip=inet_addr(local_address);
  1593. g_packet_info.src_port=local_port;
  1594. bind_fd=socket(AF_INET,SOCK_STREAM,0);
  1595. struct sockaddr_in temp_bind_addr;
  1596. bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  1597. temp_bind_addr.sin_family = AF_INET;
  1598. temp_bind_addr.sin_port = htons(local_port);
  1599. temp_bind_addr.sin_addr.s_addr = inet_addr(local_address);
  1600. if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
  1601. {
  1602. printf("bind fail\n");
  1603. exit(-1);
  1604. }
  1605. if(listen(bind_fd, SOMAXCONN) != 0 )
  1606. {
  1607. printf("listen fail\n");
  1608. exit(-1);
  1609. }
  1610. init_raw_socket();
  1611. init_filter(local_port);
  1612. epollfd = epoll_create1(0);
  1613. const int max_events = 4096;
  1614. struct epoll_event ev, events[max_events];
  1615. if (epollfd < 0) {
  1616. printf("epoll return %d\n", epollfd);
  1617. exit(-1);
  1618. }
  1619. ev.events = EPOLLIN;
  1620. ev.data.u64 = epoll_raw_recv_fd_sn;
  1621. ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
  1622. if (ret!= 0) {
  1623. printf("add raw_fd error\n");
  1624. exit(-1);
  1625. }
  1626. int timer_fd;
  1627. set_timer(epollfd,timer_fd);
  1628. while(1)////////////////////////
  1629. {
  1630. int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000);
  1631. if (nfds < 0) { //allow zero
  1632. printf("epoll_wait return %d\n", nfds);
  1633. exit(-1);
  1634. }
  1635. int n;
  1636. const int MTU=1440;
  1637. for (n = 0; n < nfds; ++n)
  1638. {
  1639. if ((events[n].data.u64 >>32u) > 0u)
  1640. {
  1641. uint32_t conv_id=events[n].data.u64>>32u;
  1642. if(!conv_manager.is_u64_used(events[n].data.u64))
  1643. {
  1644. printf("conv no longer exists");
  1645. continue;
  1646. }
  1647. int fd=int((events[n].data.u64<<32u)>>32u);
  1648. int recv_len=recv(fd,buf,buf_len,0);
  1649. printf("received a packet from udp_fd,len:%d\n",recv_len);
  1650. if(recv_len<0)
  1651. {
  1652. printf("continue\n");
  1653. perror("wtf?");
  1654. continue;
  1655. //return 0;
  1656. }
  1657. conv_manager.update_active_time(conv_id);
  1658. if(server_current_state==server_ready)
  1659. {
  1660. send_data(g_packet_info,buf,recv_len,my_id,oppsite_id,conv_id);
  1661. printf("send !!!!!!!!!!!!!!!!!!");
  1662. }
  1663. }
  1664. //printf("%d %d %d %d\n",timer_fd,raw_recv_fd,raw_send_fd,n);
  1665. if (events[n].data.u64 == epoll_timer_fd_sn)
  1666. {
  1667. uint64_t value;
  1668. read(timer_fd, &value, 8);
  1669. fake_tcp_keep_connection_server();
  1670. }
  1671. if (events[n].data.u64 == epoll_raw_recv_fd_sn)
  1672. {
  1673. iphdr *iph;tcphdr *tcph;char* data;int data_len;
  1674. on_raw_recv(iph,tcph,data,data_len);
  1675. int new_len=data_len;
  1676. memcpy(raw_recv_buf3,data,new_len);
  1677. if(data_len!=0)
  1678. {
  1679. if(pre_recv(raw_recv_buf3,new_len)<0)
  1680. continue;
  1681. }
  1682. server_raw_recv(iph,tcph,raw_recv_buf3,new_len);
  1683. }
  1684. }
  1685. }
  1686. return 0;
  1687. }
  1688. int main(int argc, char *argv[])
  1689. {
  1690. init_random_number_fd();
  1691. const_id=get_true_random_number();
  1692. g_packet_info.ack_seq=get_true_random_number();
  1693. g_packet_info.seq=get_true_random_number();
  1694. int i, j, k;
  1695. signal(SIGCHLD, handler);
  1696. process_arg(argc,argv);
  1697. if(prog_mode==client_mode)
  1698. {
  1699. client();
  1700. }
  1701. else
  1702. {
  1703. server();
  1704. }
  1705. return 0;
  1706. }