main.cpp 46 KB

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