network.cpp 35 KB

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