network.cpp 33 KB

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