main.cpp 40 KB

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