network.cpp 35 KB

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