network.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * network.cpp
  3. *
  4. * Created on: Jul 29, 2017
  5. * Author: wangyu
  6. */
  7. #include "common.h"
  8. #include "network.h"
  9. #include "log.h"
  10. int raw_recv_fd=-1;
  11. int raw_send_fd=-1;
  12. uint32_t link_level_header_len=0;//set it to 14 if SOCK_RAW is used in socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  13. int seq_mode=2;
  14. int filter_port=-1;
  15. int disable_bpf_filter=0; //for test only,most time no need to disable this
  16. uint32_t bind_address_uint32=0;
  17. struct sock_filter code_tcp_old[] = {
  18. { 0x28, 0, 0, 0x0000000c },//0
  19. { 0x15, 0, 10, 0x00000800 },//1
  20. { 0x30, 0, 0, 0x00000017 },//2
  21. { 0x15, 0, 8, 0x00000006 },//3
  22. { 0x28, 0, 0, 0x00000014 },//4
  23. { 0x45, 6, 0, 0x00001fff },//5
  24. { 0xb1, 0, 0, 0x0000000e },//6
  25. { 0x48, 0, 0, 0x0000000e },//7
  26. { 0x15, 2, 0, 0x0000ef32 },//8
  27. { 0x48, 0, 0, 0x00000010 },//9
  28. { 0x15, 0, 1, 0x0000ef32 },//10
  29. { 0x6, 0, 0, 0x0000ffff },//11
  30. { 0x6, 0, 0, 0x00000000 },//12
  31. };
  32. struct sock_filter code_tcp[] = {
  33. { 0x5, 0, 0, 0x00000001 },//0 //jump to 2,dirty hack from tcpdump -d's output
  34. { 0x5, 0, 0, 0x00000000 },//1
  35. { 0x30, 0, 0, 0x00000009 },//2
  36. { 0x15, 0, 6, 0x00000006 },//3
  37. { 0x28, 0, 0, 0x00000006 },//4
  38. { 0x45, 4, 0, 0x00001fff },//5
  39. { 0xb1, 0, 0, 0x00000000 },//6
  40. { 0x48, 0, 0, 0x00000002 },//7
  41. { 0x15, 0, 1, 0x0000fffe },//8
  42. { 0x6, 0, 0, 0x0000ffff },//9
  43. { 0x6, 0, 0, 0x00000000 },//10
  44. };
  45. int code_tcp_port_index=8;
  46. struct sock_filter code_udp[] = {
  47. { 0x5, 0, 0, 0x00000001 },
  48. { 0x5, 0, 0, 0x00000000 },
  49. { 0x30, 0, 0, 0x00000009 },
  50. { 0x15, 0, 6, 0x00000011 },
  51. { 0x28, 0, 0, 0x00000006 },
  52. { 0x45, 4, 0, 0x00001fff },
  53. { 0xb1, 0, 0, 0x00000000 },
  54. { 0x48, 0, 0, 0x00000002 },
  55. { 0x15, 0, 1, 0x0000fffe },
  56. { 0x6, 0, 0, 0x0000ffff },
  57. { 0x6, 0, 0, 0x00000000 },
  58. };
  59. int code_udp_port_index=8;
  60. struct sock_filter code_icmp[] = {
  61. { 0x5, 0, 0, 0x00000001 },
  62. { 0x5, 0, 0, 0x00000000 },
  63. { 0x30, 0, 0, 0x00000009 },
  64. { 0x15, 0, 1, 0x00000001 },
  65. { 0x6, 0, 0, 0x0000ffff },
  66. { 0x6, 0, 0, 0x00000000 },
  67. };
  68. /*
  69. tcpdump -i eth1 ip and icmp -d
  70. (000) ldh [12]
  71. (001) jeq #0x800 jt 2 jf 5
  72. (002) ldb [23]
  73. (003) jeq #0x1 jt 4 jf 5
  74. (004) ret #65535
  75. (005) ret #0
  76. tcpdump -i eth1 ip and icmp -dd
  77. { 0x28, 0, 0, 0x0000000c },
  78. { 0x15, 0, 3, 0x00000800 },
  79. { 0x30, 0, 0, 0x00000017 },
  80. { 0x15, 0, 1, 0x00000001 },
  81. { 0x6, 0, 0, 0x0000ffff },
  82. { 0x6, 0, 0, 0x00000000 },
  83. */
  84. /*
  85. tcpdump -i eth1 ip and tcp and dst port 65534 -dd
  86. { 0x28, 0, 0, 0x0000000c },
  87. { 0x15, 0, 8, 0x00000800 },
  88. { 0x30, 0, 0, 0x00000017 },
  89. { 0x15, 0, 6, 0x00000006 },
  90. { 0x28, 0, 0, 0x00000014 },
  91. { 0x45, 4, 0, 0x00001fff },
  92. { 0xb1, 0, 0, 0x0000000e },
  93. { 0x48, 0, 0, 0x00000010 },
  94. { 0x15, 0, 1, 0x0000fffe },
  95. { 0x6, 0, 0, 0x0000ffff },
  96. { 0x6, 0, 0, 0x00000000 },
  97. (000) ldh [12]
  98. (001) jeq #0x800 jt 2 jf 10
  99. (002) ldb [23]
  100. (003) jeq #0x6 jt 4 jf 10
  101. (004) ldh [20]
  102. (005) jset #0x1fff jt 10 jf 6
  103. (006) ldxb 4*([14]&0xf)
  104. (007) ldh [x + 16]
  105. (008) jeq #0xfffe jt 9 jf 10
  106. (009) ret #65535
  107. (010) ret #0
  108. */
  109. packet_info_t::packet_info_t()
  110. {
  111. src_port=0;
  112. dst_port=0;
  113. if (raw_mode == mode_faketcp)
  114. {
  115. protocol = IPPROTO_TCP;
  116. ack_seq = get_true_random_number();
  117. seq = get_true_random_number();
  118. has_ts=0;
  119. ts_ack=0;
  120. syn=0;
  121. ack=1;
  122. }
  123. else if (raw_mode == mode_udp)
  124. {
  125. protocol = IPPROTO_UDP;
  126. }
  127. else if (raw_mode == mode_icmp)
  128. {
  129. protocol = IPPROTO_ICMP;
  130. icmp_seq=0;
  131. }
  132. }
  133. int init_raw_socket()
  134. {
  135. raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
  136. if(raw_send_fd == -1) {
  137. mylog(log_fatal,"Failed to create raw_send_fd\n");
  138. //perror("Failed to create raw_send_fd");
  139. exit(1);
  140. }
  141. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  142. {
  143. mylog(log_fatal,"SO_SNDBUFFORCE fail\n");
  144. exit(1);
  145. }
  146. //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  147. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  148. if(raw_recv_fd == -1) {
  149. mylog(log_fatal,"Failed to create raw_recv_fd\n");
  150. //perror("");
  151. exit(1);
  152. }
  153. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  154. {
  155. mylog(log_fatal,"SO_RCVBUFFORCE fail\n");
  156. exit(1);
  157. }
  158. //IP_HDRINCL to tell the kernel that headers are included in the packet
  159. int one = 1;
  160. const int *val = &one;
  161. if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
  162. mylog(log_fatal,"Error setting IP_HDRINCL %d\n",errno);
  163. //perror("Error setting IP_HDRINCL");
  164. exit(2);
  165. }
  166. setnonblocking(raw_send_fd); //not really necessary
  167. setnonblocking(raw_recv_fd);
  168. return 0;
  169. }
  170. void init_filter(int port)
  171. {
  172. sock_fprog bpf;
  173. if(raw_mode==mode_faketcp||raw_mode==mode_udp)
  174. {
  175. filter_port=port;
  176. }
  177. if(disable_bpf_filter) return;
  178. //if(raw_mode==mode_icmp) return ;
  179. //code_tcp[8].k=code_tcp[10].k=port;
  180. if(raw_mode==mode_faketcp)
  181. {
  182. bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
  183. code_tcp[code_tcp_port_index].k=port;
  184. bpf.filter = code_tcp;
  185. }
  186. else if(raw_mode==mode_udp)
  187. {
  188. bpf.len = sizeof(code_udp)/sizeof(code_udp[0]);
  189. code_udp[code_udp_port_index].k=port;
  190. bpf.filter = code_udp;
  191. }
  192. else if(raw_mode==mode_icmp)
  193. {
  194. bpf.len = sizeof(code_icmp)/sizeof(code_icmp[0]);
  195. bpf.filter = code_icmp;
  196. }
  197. int dummy;
  198. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy)); //in case i forgot to remove
  199. if (ret != 0)
  200. {
  201. mylog(log_debug,"error remove fiter\n");
  202. //perror("filter");
  203. //exit(-1);
  204. }
  205. ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
  206. if (ret != 0)
  207. {
  208. mylog(log_fatal,"error set fiter\n");
  209. //perror("filter");
  210. exit(-1);
  211. }
  212. }
  213. void remove_filter()
  214. {
  215. filter_port=0;
  216. int dummy;
  217. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
  218. if (ret != 0)
  219. {
  220. mylog(log_debug,"error remove fiter\n");
  221. //perror("filter");
  222. //exit(-1);
  223. }
  224. }
  225. int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
  226. {
  227. const packet_info_t &send_info=raw_info.send_info;
  228. const packet_info_t &recv_info=raw_info.recv_info;
  229. char send_raw_ip_buf[buf_len];
  230. struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
  231. memset(iph,0,sizeof(iphdr));
  232. struct sockaddr_in sin;
  233. sin.sin_family = AF_INET;
  234. //sin.sin_port = htons(info.dst_port); //dont need this
  235. sin.sin_addr.s_addr = send_info.dst_ip;
  236. iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
  237. iph->version = 4;
  238. iph->tos = 0;
  239. // iph->id = htonl (ip_id++); //Id of this packet
  240. // iph->id = 0; //Id of this packet ,kernel will auto fill this if id is zero
  241. iph->frag_off = htons(0x4000); //DF set,others are zero
  242. // iph->frag_off = htons(0x0000); //DF set,others are zero
  243. iph->ttl = 64;
  244. iph->protocol = send_info.protocol;
  245. iph->check = 0; //Set to 0 before calculating checksum
  246. iph->saddr = send_info.src_ip; //Spoof the source ip address
  247. iph->daddr = send_info.dst_ip;
  248. uint16_t ip_tot_len=sizeof (struct iphdr)+payloadlen;
  249. // iph->tot_len = htons(ip_tot_len); //this is not necessary ,kernel will always auto fill this //http://man7.org/linux/man-pages/man7/raw.7.html
  250. //iph->tot_len = ip_tot_len;
  251. memcpy(send_raw_ip_buf+sizeof(iphdr) , payload, payloadlen);
  252. //iph->check = csum ((unsigned short *) send_raw_ip_buf, ip_tot_len); //this is not necessary ,kernel will always auto fill this
  253. int ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  254. if(ret==-1)
  255. {
  256. mylog(log_debug,"sendto failed\n");
  257. //perror("why?");
  258. return -1;
  259. }
  260. return 0;
  261. }
  262. int peek_raw(packet_info_t &peek_info)
  263. { static char peek_raw_buf[buf_len];
  264. char *ip_begin=peek_raw_buf+link_level_header_len;
  265. struct sockaddr saddr;
  266. socklen_t saddr_size;
  267. int recv_len = recvfrom(raw_recv_fd, peek_raw_buf,buf_len, MSG_PEEK ,&saddr , &saddr_size);//change buf_len to something smaller,we only need header here
  268. iphdr * iph = (struct iphdr *) (ip_begin);
  269. //mylog(log_info,"recv_len %d\n",recv_len);
  270. if(recv_len<int(sizeof(iphdr)))
  271. {
  272. return -1;
  273. }
  274. peek_info.src_ip=iph->saddr;
  275. unsigned short iphdrlen =iph->ihl*4;
  276. char *payload=ip_begin+iphdrlen;
  277. //mylog(log_info,"protocol %d\n",iph->protocol);
  278. switch(raw_mode)
  279. {
  280. case mode_faketcp:
  281. {
  282. if(iph->protocol!=IPPROTO_TCP) return -1;
  283. struct tcphdr *tcph=(tcphdr *)payload;
  284. if(recv_len<int( iphdrlen+sizeof(tcphdr) ))
  285. return -1;
  286. peek_info.src_port=ntohs(tcph->source);
  287. peek_info.syn=tcph->syn;
  288. break;
  289. }
  290. case mode_udp:
  291. {
  292. if(iph->protocol!=IPPROTO_UDP) return -1;
  293. struct udphdr *udph=(udphdr *)payload;
  294. if(recv_len<int(iphdrlen+sizeof(udphdr)))
  295. return -1;
  296. peek_info.src_port=ntohs(udph->source);
  297. break;
  298. }
  299. case mode_icmp:
  300. {
  301. if(iph->protocol!=IPPROTO_ICMP) return -1;
  302. struct icmphdr *icmph=(icmphdr *)payload;
  303. if(recv_len<int( iphdrlen+sizeof(icmphdr) ))
  304. return -1;
  305. peek_info.src_port=ntohs(icmph->id);
  306. break;
  307. }
  308. default:return -1;
  309. }
  310. return 0;
  311. }
  312. int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
  313. {
  314. const packet_info_t &send_info=raw_info.send_info;
  315. packet_info_t &recv_info=raw_info.recv_info;
  316. static char recv_raw_ip_buf[buf_len];
  317. iphdr * iph;
  318. struct sockaddr saddr;
  319. socklen_t saddr_size;
  320. saddr_size = sizeof(saddr);
  321. int flag=0;
  322. int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, buf_len, flag ,&saddr , &saddr_size);
  323. if(recv_len<0)
  324. {
  325. mylog(log_trace,"recv_len %d\n",recv_len);
  326. return -1;
  327. }
  328. if(recv_len<int(link_level_header_len))
  329. {
  330. mylog(log_trace,"length error\n");
  331. }
  332. if(link_level_header_len ==14&&(recv_raw_ip_buf[12]!=8||recv_raw_ip_buf[13]!=0))
  333. {
  334. mylog(log_trace,"not an ipv4 packet!\n");
  335. return -1;
  336. }
  337. char *ip_begin=recv_raw_ip_buf+link_level_header_len; //14 is eth net header
  338. iph = (struct iphdr *) (ip_begin);
  339. recv_info.src_ip=iph->saddr;
  340. recv_info.dst_ip=iph->daddr;
  341. recv_info.protocol=iph->protocol;
  342. if(bind_address_uint32!=0 &&recv_info.dst_ip!=bind_address_uint32)
  343. {
  344. //printf(" bind adress doenst match, dropped\n");
  345. return -1;
  346. }
  347. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  348. mylog(log_trace,"iph ihl error\n");
  349. return -1;
  350. }
  351. int ip_len=ntohs(iph->tot_len);
  352. if(recv_len-int(link_level_header_len) <ip_len)
  353. {
  354. mylog(log_debug,"incomplete packet\n");
  355. return -1;
  356. }
  357. unsigned short iphdrlen =iph->ihl*4;
  358. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  359. if(ip_chk!=0)
  360. {
  361. mylog(log_debug,"ip header error %d\n",ip_chk);
  362. return -1;
  363. }
  364. payload=ip_begin+iphdrlen;
  365. payloadlen=ip_len-iphdrlen;
  366. if(payloadlen<0)
  367. {
  368. mylog(log_warn,"error payload len\n");
  369. return -1;
  370. }
  371. return 0;
  372. }
  373. int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
  374. {
  375. const packet_info_t &send_info=raw_info.send_info;
  376. const packet_info_t &recv_info=raw_info.recv_info;
  377. char send_raw_icmp_buf[buf_len];
  378. icmphdr *icmph=(struct icmphdr *) (send_raw_icmp_buf);
  379. memset(icmph,0,sizeof(icmphdr));
  380. if(program_mode==client_mode)
  381. {
  382. icmph->type=8;
  383. }
  384. else
  385. {
  386. icmph->type=0;
  387. }
  388. icmph->code=0;
  389. icmph->id=htons(send_info.src_port);
  390. icmph->seq=htons(send_info.icmp_seq); /////////////modify
  391. memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
  392. icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
  393. if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(icmphdr)+payloadlen)!=0)
  394. {
  395. return -1;
  396. }
  397. /*if(program_mode==client_mode)
  398. {
  399. send_info.icmp_seq++;
  400. }*/
  401. return 0;
  402. }
  403. int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
  404. {
  405. const packet_info_t &send_info=raw_info.send_info;
  406. const packet_info_t &recv_info=raw_info.recv_info;
  407. char send_raw_udp_buf[buf_len];
  408. udphdr *udph=(struct udphdr *) (send_raw_udp_buf
  409. + sizeof(struct pseudo_header));
  410. memset(udph,0,sizeof(udphdr));
  411. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_udp_buf);
  412. udph->source = htons(send_info.src_port);
  413. udph->dest = htons(send_info.dst_port);
  414. int udp_tot_len=payloadlen+sizeof(udphdr);
  415. if(udp_tot_len>65535)
  416. {
  417. mylog(log_debug,"invalid len\n");
  418. return -1;
  419. }
  420. mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len);
  421. udph->len=htons(uint16_t(udp_tot_len));
  422. memcpy(send_raw_udp_buf+sizeof(struct pseudo_header)+sizeof(udphdr),payload,payloadlen);
  423. psh->source_address = send_info.src_ip;
  424. psh->dest_address = send_info.dst_ip;
  425. psh->placeholder = 0;
  426. psh->protocol = IPPROTO_UDP;
  427. psh->tcp_length = htons(uint16_t(udp_tot_len));
  428. int csum_size = sizeof(struct pseudo_header) +udp_tot_len ;
  429. udph->check = csum( (unsigned short*) send_raw_udp_buf, csum_size);
  430. if(send_raw_ip(raw_info,send_raw_udp_buf+ sizeof(struct pseudo_header),udp_tot_len)!=0)
  431. {
  432. return -1;
  433. }
  434. return 0;
  435. }
  436. int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { //TODO seq increase
  437. const packet_info_t &send_info=raw_info.send_info;
  438. const packet_info_t &recv_info=raw_info.recv_info;
  439. //mylog(log_debug,"syn %d\n",send_info.syn);
  440. char send_raw_tcp_buf0[buf_len];
  441. char *send_raw_tcp_buf=send_raw_tcp_buf0;
  442. struct tcphdr *tcph = (struct tcphdr *) (send_raw_tcp_buf
  443. + sizeof(struct pseudo_header));
  444. memset(tcph,0,sizeof(tcphdr));
  445. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_tcp_buf);
  446. //TCP Header
  447. tcph->source = htons(send_info.src_port);
  448. tcph->dest = htons(send_info.dst_port);
  449. tcph->seq = htonl(send_info.seq);
  450. tcph->ack_seq = htonl(send_info.ack_seq);
  451. tcph->fin = 0;
  452. tcph->syn = send_info.syn;
  453. tcph->rst = 0;
  454. tcph->psh = send_info.psh;
  455. tcph->ack = send_info.ack;
  456. if (tcph->syn == 1) {
  457. tcph->doff = 10; //tcp header size
  458. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  459. send_raw_tcp_buf[i++] = 0x02; //mss
  460. send_raw_tcp_buf[i++] = 0x04;
  461. send_raw_tcp_buf[i++] = 0x05;
  462. send_raw_tcp_buf[i++] = (char)0xb4;
  463. //raw_send_buf[i++]=0x01;
  464. //raw_send_buf[i++]=0x01;
  465. send_raw_tcp_buf[i++] = 0x04; //sack ok
  466. send_raw_tcp_buf[i++] = 0x02; //sack ok
  467. send_raw_tcp_buf[i++] = 0x08; //ts
  468. send_raw_tcp_buf[i++] = 0x0a;
  469. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
  470. (uint32_t) get_current_time());
  471. i += 4;
  472. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  473. i += 4;
  474. send_raw_tcp_buf[i++] = 0x01;
  475. send_raw_tcp_buf[i++] = 0x03;
  476. send_raw_tcp_buf[i++] = 0x03;
  477. send_raw_tcp_buf[i++] = 0x05;
  478. } else {
  479. tcph->doff = 8;
  480. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  481. send_raw_tcp_buf[i++] = 0x01;
  482. send_raw_tcp_buf[i++] = 0x01;
  483. send_raw_tcp_buf[i++] = 0x08; //ts
  484. send_raw_tcp_buf[i++] = 0x0a;
  485. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(
  486. (uint32_t) get_current_time());
  487. i += 4;
  488. *(uint32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  489. i += 4;
  490. }
  491. tcph->urg = 0;
  492. //tcph->window = htons((uint16_t)(1024));
  493. tcph->window = htons((uint16_t) (10240 + random() % 100));
  494. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  495. tcph->urg_ptr = 0;
  496. char *tcp_data = send_raw_tcp_buf+sizeof(struct pseudo_header) + tcph->doff * 4;
  497. memcpy(tcp_data, payload, payloadlen);
  498. psh->source_address = send_info.src_ip;
  499. psh->dest_address = send_info.dst_ip;
  500. psh->placeholder = 0;
  501. psh->protocol = IPPROTO_TCP;
  502. psh->tcp_length = htons(tcph->doff * 4 + payloadlen);
  503. int csum_size = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  504. tcph->check = csum( (unsigned short*) send_raw_tcp_buf, csum_size);
  505. int tcp_totlen=tcph->doff*4 + payloadlen;
  506. if(send_raw_ip(raw_info,send_raw_tcp_buf+ sizeof(struct pseudo_header),tcp_totlen)!=0)
  507. {
  508. return -1;
  509. }
  510. raw_info.last_send_len=payloadlen;
  511. return 0;
  512. }
  513. /*
  514. int send_raw_tcp_deprecated(const packet_info_t &info,const char * payload,int payloadlen)
  515. {
  516. static uint16_t ip_id=1;
  517. char raw_send_buf[buf_len];
  518. char raw_send_buf2[buf_len];
  519. //if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
  520. mylog(log_trace,"send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  521. char *data;
  522. memset(raw_send_buf,0,payloadlen+100);
  523. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  524. //TCP header
  525. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  526. struct sockaddr_in sin;
  527. struct pseudo_header psh;
  528. //some address resolution
  529. sin.sin_family = AF_INET;
  530. sin.sin_port = htons(info.dst_port);
  531. sin.sin_addr.s_addr = info.dst_ip;
  532. //Fill in the IP Header
  533. iph->ihl = 5;
  534. iph->version = 4;
  535. iph->tos = 0;
  536. iph->id = htonl (ip_id++); //Id of this packet
  537. iph->frag_off = htons(0x4000); //DF set,others are zero
  538. iph->ttl = 64;
  539. iph->protocol = IPPROTO_TCP;
  540. iph->check = 0; //Set to 0 before calculating checksum
  541. iph->saddr = info.src_ip; //Spoof the source ip address
  542. iph->daddr = info.dst_ip;
  543. //TCP Header
  544. tcph->source = htons(info.src_port);
  545. tcph->dest = htons(info.dst_port);
  546. tcph->seq =htonl(info.seq);
  547. tcph->ack_seq = htonl(info.ack_seq);
  548. tcph->fin=0;
  549. tcph->syn=info.syn;
  550. tcph->rst=0;
  551. tcph->psh=info.psh;
  552. tcph->ack=info.ack;
  553. if(tcph->syn==1)
  554. {
  555. tcph->doff = 10; //tcp header size
  556. int i=sizeof (struct iphdr)+20;
  557. raw_send_buf[i++]=0x02;//mss
  558. raw_send_buf[i++]=0x04;
  559. raw_send_buf[i++]=0x05;
  560. raw_send_buf[i++]=0xb4;
  561. //raw_send_buf[i++]=0x01;
  562. //raw_send_buf[i++]=0x01;
  563. raw_send_buf[i++]=0x04; //sack ok
  564. raw_send_buf[i++]=0x02; //sack ok
  565. raw_send_buf[i++]=0x08; //i=6;
  566. raw_send_buf[i++]=0x0a;
  567. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  568. i+=4;
  569. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  570. i+=4;
  571. raw_send_buf[i++]=0x01;
  572. raw_send_buf[i++]=0x03;
  573. raw_send_buf[i++]=0x03;
  574. raw_send_buf[i++]=0x05;
  575. }
  576. else
  577. {
  578. tcph->doff=8;
  579. int i=sizeof (struct iphdr)+20;
  580. raw_send_buf[i++]=0x01;
  581. raw_send_buf[i++]=0x01;
  582. raw_send_buf[i++]=0x08; //i=0;
  583. raw_send_buf[i++]=0x0a;
  584. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  585. i+=4;
  586. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  587. i+=4;
  588. }
  589. tcph->urg=0;
  590. //tcph->window = htons((uint16_t)(1024));
  591. tcph->window = htons((uint16_t)(10240+random()%100));
  592. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  593. tcph->urg_ptr = 0;
  594. //Data part
  595. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  596. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  597. memcpy(data , payload, payloadlen);
  598. psh.source_address = info.src_ip;
  599. psh.dest_address = sin.sin_addr.s_addr;
  600. psh.placeholder = 0;
  601. psh.protocol = IPPROTO_TCP;
  602. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  603. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  604. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  605. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  606. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  607. //Ip checksum
  608. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  609. mylog(log_trace,"sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
  610. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  611. if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
  612. {
  613. if(seq_mode==0)
  614. {
  615. }
  616. else if(seq_mode==1)
  617. {
  618. g_packet_info_send.seq+=payloadlen;
  619. }
  620. else if(seq_mode==2)
  621. {
  622. if(random()% 5==3 )
  623. g_packet_info_send.seq+=payloadlen;
  624. }
  625. }
  626. mylog(log_trace,"<ret:%d>\n",ret);
  627. if(ret<0)
  628. {
  629. mylog(log_fatal,"");
  630. perror("raw send error\n");
  631. //printf("send error\n");
  632. }
  633. return 0;
  634. }
  635. */
  636. int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  637. {
  638. const packet_info_t &send_info=raw_info.send_info;
  639. packet_info_t &recv_info=raw_info.recv_info;
  640. static char recv_raw_icmp_buf[buf_len];
  641. char * ip_payload;
  642. int ip_payloadlen;
  643. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  644. {
  645. mylog(log_debug,"recv_raw_ip error\n");
  646. return -1;
  647. }
  648. if(recv_info.protocol!=IPPROTO_ICMP)
  649. {
  650. //printf("not udp protocol\n");
  651. return -1;
  652. }
  653. icmphdr *icmph=(struct icmphdr *) (ip_payload);
  654. if(ntohs(icmph->id)!=send_info.src_port)
  655. {
  656. mylog(log_debug,"icmp id mis-match,ignored\n");
  657. return -1;
  658. }
  659. recv_info.src_port=recv_info.dst_port=ntohs(icmph->id);
  660. if(program_mode==client_mode)
  661. {
  662. if(icmph->type!=0)
  663. return -1;
  664. }
  665. else
  666. {
  667. if(icmph->type!=8)
  668. return -1;
  669. }
  670. if(icmph->code!=0)
  671. return -1;
  672. unsigned short check = csum( (unsigned short*) ip_payload, ip_payloadlen);
  673. if(check!=0)
  674. {
  675. mylog(log_debug,"icmp checksum fail %x\n",check);
  676. return -1;
  677. }
  678. //mylog(log_info,"program_mode=%d\n",program_mode);
  679. /*
  680. if(program_mode==server_mode)
  681. {
  682. send_info.icmp_seq=ntohs(icmph->seq);
  683. //mylog(log_info,"send_info.seq=%d\n",send_info.seq);
  684. }*/
  685. payload=ip_payload+sizeof(icmphdr);
  686. payloadlen=ip_payloadlen-sizeof(icmphdr);
  687. mylog(log_trace,"get a packet len=%d\n",payloadlen);
  688. return 0;
  689. }
  690. int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  691. {
  692. const packet_info_t &send_info=raw_info.send_info;
  693. packet_info_t &recv_info=raw_info.recv_info;
  694. static char recv_raw_udp_buf[buf_len];
  695. char * ip_payload;
  696. int ip_payloadlen;
  697. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  698. {
  699. mylog(log_debug,"recv_raw_ip error\n");
  700. return -1;
  701. }
  702. if(recv_info.protocol!=IPPROTO_UDP)
  703. {
  704. //printf("not udp protocol\n");
  705. return -1;
  706. }
  707. if(ip_payloadlen<int( sizeof(udphdr) ))
  708. {
  709. mylog(log_debug,"too short to hold udpheader\n");
  710. return -1;
  711. }
  712. udphdr *udph=(struct udphdr*)ip_payload;
  713. if(ntohs(udph->len)!=ip_payloadlen)
  714. {
  715. mylog(log_debug,"udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
  716. return -1;
  717. }
  718. if(udph->dest!=ntohs(uint16_t(filter_port)))
  719. {
  720. //printf("%x %x",tcph->dest,);
  721. return -1;
  722. }
  723. memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  724. struct pseudo_header *psh=(pseudo_header *)recv_raw_udp_buf ;
  725. psh->source_address = recv_info.src_ip;
  726. psh->dest_address = recv_info.dst_ip;
  727. psh->placeholder = 0;
  728. psh->protocol = IPPROTO_UDP;
  729. psh->tcp_length = htons(ip_payloadlen);
  730. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  731. uint16_t udp_chk = csum( (unsigned short*) recv_raw_udp_buf, csum_len);
  732. if(udp_chk!=0)
  733. {
  734. mylog(log_debug,"udp_chk:%x\n",udp_chk);
  735. mylog(log_debug,"udp header error\n");
  736. return -1;
  737. }
  738. char *udp_begin=recv_raw_udp_buf+sizeof(struct pseudo_header);
  739. recv_info.src_port=ntohs(udph->source);
  740. recv_info.dst_port=ntohs(udph->dest);
  741. payloadlen = ip_payloadlen-sizeof(udphdr);
  742. payload=udp_begin+sizeof(udphdr);
  743. return 0;
  744. }
  745. int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
  746. {
  747. const packet_info_t &send_info=raw_info.send_info;
  748. packet_info_t &recv_info=raw_info.recv_info;
  749. static char recv_raw_tcp_buf[buf_len];
  750. char * ip_payload;
  751. int ip_payloadlen;
  752. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  753. {
  754. mylog(log_debug,"recv_raw_ip error\n");
  755. return -1;
  756. }
  757. if(recv_info.protocol!=IPPROTO_TCP)
  758. {
  759. //printf("not tcp protocol\n");
  760. return -1;
  761. }
  762. tcphdr * tcph=(struct tcphdr*)ip_payload;
  763. unsigned short tcphdrlen = tcph->doff*4;
  764. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  765. mylog(log_debug,"tcph error\n");
  766. return 0;
  767. }
  768. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  769. {
  770. //printf("%x %x",tcph->dest,);
  771. return -1;
  772. }
  773. memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  774. struct pseudo_header *psh=(pseudo_header *)recv_raw_tcp_buf ;
  775. psh->source_address = recv_info.src_ip;
  776. psh->dest_address = recv_info.dst_ip;
  777. psh->placeholder = 0;
  778. psh->protocol = IPPROTO_TCP;
  779. psh->tcp_length = htons(ip_payloadlen);
  780. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  781. uint16_t tcp_chk = csum( (unsigned short*) recv_raw_tcp_buf, csum_len);
  782. if(tcp_chk!=0)
  783. {
  784. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  785. mylog(log_debug,"tcp header error\n");
  786. return -1;
  787. }
  788. char *tcp_begin=recv_raw_tcp_buf+sizeof(struct pseudo_header); //ip packet's data part
  789. char *tcp_option=recv_raw_tcp_buf+sizeof(struct pseudo_header)+sizeof(tcphdr);
  790. recv_info.has_ts=0;
  791. if(tcph->doff==10)
  792. {
  793. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  794. {
  795. recv_info.has_ts=1;
  796. recv_info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  797. recv_info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  798. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  799. }
  800. }
  801. else if(tcph->doff==8)
  802. {
  803. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  804. {
  805. recv_info.has_ts=1;
  806. recv_info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  807. recv_info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  808. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  809. }
  810. }
  811. if(tcph->rst==1)
  812. {
  813. mylog(log_warn,"%%%%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  814. }
  815. recv_info.ack=tcph->ack;
  816. recv_info.syn=tcph->syn;
  817. recv_info.rst=tcph->rst;
  818. recv_info.src_port=ntohs(tcph->source);
  819. recv_info.dst_port=ntohs(tcph->dest);
  820. recv_info.seq=ntohl(tcph->seq);
  821. recv_info.ack_seq=ntohl(tcph->ack_seq);
  822. recv_info.psh=tcph->psh;
  823. /* if(recv_info.has_ts)
  824. {
  825. send_info.ts_ack=recv_info.ts; //////////////////////////////////////////////modify
  826. }*/
  827. payloadlen = ip_payloadlen-tcphdrlen;
  828. payload=tcp_begin+tcphdrlen;
  829. /*if (recv_info.syn == 0 && recv_info.ack == 1&& payloadlen != 0) //only modify send_info when the packet is not part of handshake
  830. {
  831. send_info.ack_seq=recv_info.seq;
  832. }*/
  833. raw_info.last_recv_len=payloadlen;
  834. return 0;
  835. }
  836. /*
  837. int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
  838. {
  839. static char buf[buf_len];
  840. char raw_recv_buf[buf_len];
  841. char raw_recv_buf2[buf_len];
  842. char raw_recv_buf3[buf_len];
  843. iphdr * iph;
  844. tcphdr * tcph;
  845. int size;
  846. struct sockaddr saddr;
  847. socklen_t saddr_size;
  848. saddr_size = sizeof(saddr);
  849. mylog(log_trace,"raw!\n");
  850. size = recvfrom(raw_recv_fd, buf, buf_len, 0 ,&saddr , &saddr_size);
  851. if(buf[12]!=8||buf[13]!=0)
  852. {
  853. mylog(log_debug,"not an ipv4 packet!\n");
  854. return -1;
  855. }
  856. char *ip_begin=buf+14;
  857. iph = (struct iphdr *) (ip_begin);
  858. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  859. mylog(log_debug,"iph ihl error");
  860. return -1;
  861. }
  862. if (iph->protocol != IPPROTO_TCP) {
  863. mylog(log_debug,"iph protocal != tcp\n");
  864. return -1;
  865. }
  866. int ip_len=ntohs(iph->tot_len);
  867. unsigned short iphdrlen =iph->ihl*4;
  868. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  869. unsigned short tcphdrlen = tcph->doff*4;
  870. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  871. mylog(log_debug,"tcph error");
  872. return 0;
  873. }
  874. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  875. {
  876. //printf("%x %x",tcph->dest,);
  877. return -1;
  878. }
  879. /////ip
  880. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  881. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  882. /////ip end
  883. ///tcp
  884. struct pseudo_header psh;
  885. psh.source_address = iph->saddr;
  886. psh.dest_address = iph->daddr;
  887. psh.placeholder = 0;
  888. psh.protocol = IPPROTO_TCP;
  889. psh.tcp_length = htons(ip_len-iphdrlen);
  890. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  891. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  892. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  893. if(ip_chk!=0)
  894. {
  895. mylog(log_debug,"ip header error %d\n",ip_chk);
  896. return -1;
  897. }
  898. if(tcp_chk!=0)
  899. {
  900. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  901. mylog(log_debug,"tcp header error\n");
  902. return -1;
  903. }
  904. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
  905. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  906. info.has_ts=0;
  907. if(tcph->doff==10)
  908. {
  909. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  910. {
  911. info.has_ts=1;
  912. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  913. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  914. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  915. }
  916. }
  917. else if(tcph->doff==8)
  918. {
  919. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  920. {
  921. info.has_ts=1;
  922. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  923. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  924. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  925. }
  926. }
  927. if(tcph->rst==1)
  928. {
  929. mylog(log_warn,"%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  930. }
  931. info.ack=tcph->ack;
  932. info.syn=tcph->syn;
  933. info.rst=tcph->rst;
  934. info.src_port=ntohs(tcph->source);
  935. info.src_ip=iph->saddr;
  936. info.seq=ntohl(tcph->seq);
  937. info.ack_seq=ntohl(tcph->ack_seq);
  938. info.psh=tcph->psh;
  939. if(info.has_ts)
  940. {
  941. g_packet_info_send.ts_ack=info.ts;
  942. }
  943. ////tcp end
  944. payloadlen = ip_len-tcphdrlen-iphdrlen;
  945. payload=ip_begin+tcphdrlen+iphdrlen;
  946. if(payloadlen>0&&payload[0]=='h')
  947. {
  948. mylog(log_debug,"recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
  949. }
  950. if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
  951. {
  952. //if(seq_increse)
  953. g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
  954. }
  955. //printf("%d\n",ip_len);
  956. mylog(log_trace,"<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
  957. return 0;
  958. }*/
  959. int send_raw0(raw_info_t &raw_info,const char * payload,int payloadlen)
  960. {
  961. packet_info_t &send_info=raw_info.send_info;
  962. packet_info_t &recv_info=raw_info.recv_info;
  963. mylog(log_trace,"send_raw : from %x %d to %x %d\n",send_info.src_ip,send_info.src_port,send_info.dst_ip,send_info.dst_port);
  964. switch(raw_mode)
  965. {
  966. case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
  967. case mode_udp: return send_raw_udp(raw_info,payload,payloadlen);
  968. case mode_icmp: return send_raw_icmp(raw_info,payload,payloadlen);
  969. default:return -1;
  970. }
  971. }
  972. int recv_raw0(raw_info_t &raw_info,char * &payload,int &payloadlen)
  973. {
  974. packet_info_t &send_info=raw_info.send_info;
  975. packet_info_t &recv_info=raw_info.recv_info;
  976. switch(raw_mode)
  977. {
  978. case mode_faketcp:return recv_raw_tcp(raw_info,payload,payloadlen);
  979. case mode_udp: return recv_raw_udp(raw_info,payload,payloadlen);
  980. case mode_icmp: return recv_raw_icmp(raw_info,payload,payloadlen);
  981. default: return -1;
  982. }
  983. }
  984. int after_send_raw0(raw_info_t &raw_info)
  985. {
  986. packet_info_t &send_info=raw_info.send_info;
  987. packet_info_t &recv_info=raw_info.recv_info;
  988. if(raw_mode==mode_faketcp)
  989. {
  990. if (send_info.syn == 0 && send_info.ack == 1&& raw_info.last_send_len != 0) //only modify send_info when the packet is not part of handshake
  991. {
  992. if (seq_mode == 0)
  993. {
  994. } else if (seq_mode == 1)
  995. {
  996. send_info.seq += raw_info.last_send_len; //////////////////modify
  997. } else if (seq_mode == 2)
  998. {
  999. if (random() % 5 == 3)
  1000. send_info.seq += raw_info.last_send_len; //////////////////modify
  1001. }
  1002. }
  1003. }
  1004. if(raw_mode==mode_icmp)
  1005. {
  1006. if(program_mode==client_mode)
  1007. {
  1008. send_info.icmp_seq++;
  1009. }
  1010. }
  1011. return 0;
  1012. }
  1013. int after_recv_raw0(raw_info_t &raw_info)
  1014. {
  1015. packet_info_t &send_info=raw_info.send_info;
  1016. packet_info_t &recv_info=raw_info.recv_info;
  1017. if(raw_mode==mode_faketcp)
  1018. {
  1019. if(recv_info.has_ts)
  1020. send_info.ts_ack=recv_info.ts;
  1021. if (recv_info.syn == 0 && recv_info.ack == 1 && raw_info.last_recv_len != 0) //only modify send_info when the packet is not part of handshake
  1022. {
  1023. send_info.ack_seq = recv_info.seq+raw_info.last_recv_len;//TODO only update if its larger
  1024. }
  1025. }
  1026. if(raw_mode==mode_icmp)
  1027. {
  1028. if(program_mode==server_mode)
  1029. {
  1030. send_info.icmp_seq = recv_info.icmp_seq; //TODO only update if its larger
  1031. }
  1032. }
  1033. return 0;
  1034. }
  1035. int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen)
  1036. {
  1037. packet_info_t &send_info=raw_info.send_info;
  1038. packet_info_t &recv_info=raw_info.recv_info;
  1039. int ret=send_raw(raw_info,payload,payloadlen);
  1040. if(ret<0) return ret;
  1041. else
  1042. {
  1043. after_send_raw0(raw_info);
  1044. return ret;
  1045. }
  1046. }
  1047. int recv_raw(raw_info_t &raw_info,char *& payload,int & payloadlen)
  1048. {
  1049. packet_info_t &send_info=raw_info.send_info;
  1050. packet_info_t &recv_info=raw_info.recv_info;
  1051. int ret=recv_raw(raw_info,payload,payloadlen);
  1052. if(ret<0) return ret;
  1053. else
  1054. {
  1055. after_recv_raw0(raw_info);
  1056. return ret;
  1057. }
  1058. }