main.cpp 38 KB

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