network.cpp 31 KB

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