network.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  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. #include "misc.h"
  11. int raw_recv_fd=-1;
  12. int raw_send_fd=-1;
  13. 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));
  14. int seq_mode=3;
  15. int max_seq_mode=4;
  16. int random_drop=0;
  17. int filter_port=-1;
  18. int disable_bpf_filter=0; //for test only,most time no need to disable this
  19. u32_t bind_address_uint32=0;
  20. int lower_level=0;
  21. int lower_level_manual=0;
  22. int ifindex=-1;
  23. char if_name[100]="";
  24. unsigned short g_ip_id_counter=0;
  25. unsigned char dest_hw_addr[sizeof(sockaddr_ll::sll_addr)]=
  26. {0xff,0xff,0xff,0xff,0xff,0xff,0,0};
  27. //{0x00,0x23,0x45,0x67,0x89,0xb9};
  28. const u32_t receive_window_lower_bound=40960;
  29. const u32_t receive_window_random_range=512;
  30. const unsigned char wscale=0x05;
  31. struct sock_filter code_tcp_old[] = {
  32. { 0x28, 0, 0, 0x0000000c },//0
  33. { 0x15, 0, 10, 0x00000800 },//1
  34. { 0x30, 0, 0, 0x00000017 },//2
  35. { 0x15, 0, 8, 0x00000006 },//3
  36. { 0x28, 0, 0, 0x00000014 },//4
  37. { 0x45, 6, 0, 0x00001fff },//5
  38. { 0xb1, 0, 0, 0x0000000e },//6
  39. { 0x48, 0, 0, 0x0000000e },//7
  40. { 0x15, 2, 0, 0x0000ef32 },//8
  41. { 0x48, 0, 0, 0x00000010 },//9
  42. { 0x15, 0, 1, 0x0000ef32 },//10
  43. { 0x6, 0, 0, 0x0000ffff },//11
  44. { 0x6, 0, 0, 0x00000000 },//12
  45. };
  46. struct sock_filter code_tcp[] = {
  47. //{ 0x5, 0, 0, 0x00000001 },//0 //jump to 2,dirty hack from tcpdump -d's output
  48. //{ 0x5, 0, 0, 0x00000000 },//1
  49. { 0x30, 0, 0, 0x00000009 },//2
  50. { 0x15, 0, 6, 0x00000006 },//3
  51. { 0x28, 0, 0, 0x00000006 },//4
  52. { 0x45, 4, 0, 0x00001fff },//5
  53. { 0xb1, 0, 0, 0x00000000 },//6
  54. { 0x48, 0, 0, 0x00000002 },//7
  55. { 0x15, 0, 1, 0x0000fffe },//8 //modify this fffe to the port you listen on
  56. { 0x6, 0, 0, 0x0000ffff },//9
  57. { 0x6, 0, 0, 0x00000000 },//10
  58. };
  59. int code_tcp_port_index=6;
  60. struct sock_filter code_udp[] = {
  61. //{ 0x5, 0, 0, 0x00000001 },
  62. //{ 0x5, 0, 0, 0x00000000 },
  63. { 0x30, 0, 0, 0x00000009 },
  64. { 0x15, 0, 6, 0x00000011 },
  65. { 0x28, 0, 0, 0x00000006 },
  66. { 0x45, 4, 0, 0x00001fff },
  67. { 0xb1, 0, 0, 0x00000000 },
  68. { 0x48, 0, 0, 0x00000002 },
  69. { 0x15, 0, 1, 0x0000fffe }, //modify this fffe to the port you listen on
  70. { 0x6, 0, 0, 0x0000ffff },
  71. { 0x6, 0, 0, 0x00000000 },
  72. };
  73. int code_udp_port_index=6;
  74. struct sock_filter code_icmp[] = {
  75. //{ 0x5, 0, 0, 0x00000001 },
  76. //{ 0x5, 0, 0, 0x00000000 },
  77. { 0x30, 0, 0, 0x00000009 },
  78. { 0x15, 0, 1, 0x00000001 },
  79. { 0x6, 0, 0, 0x0000ffff },
  80. { 0x6, 0, 0, 0x00000000 },
  81. };
  82. /*
  83. tcpdump -i eth1 ip and icmp -d
  84. (000) ldh [12]
  85. (001) jeq #0x800 jt 2 jf 5
  86. (002) ldb [23]
  87. (003) jeq #0x1 jt 4 jf 5
  88. (004) ret #65535
  89. (005) ret #0
  90. tcpdump -i eth1 ip and icmp -dd
  91. { 0x28, 0, 0, 0x0000000c },
  92. { 0x15, 0, 3, 0x00000800 },
  93. { 0x30, 0, 0, 0x00000017 },
  94. { 0x15, 0, 1, 0x00000001 },
  95. { 0x6, 0, 0, 0x0000ffff },
  96. { 0x6, 0, 0, 0x00000000 },
  97. */
  98. /*
  99. tcpdump -i eth1 ip and tcp and dst port 65534 -dd
  100. { 0x28, 0, 0, 0x0000000c },
  101. { 0x15, 0, 8, 0x00000800 },
  102. { 0x30, 0, 0, 0x00000017 },
  103. { 0x15, 0, 6, 0x00000006 },
  104. { 0x28, 0, 0, 0x00000014 },
  105. { 0x45, 4, 0, 0x00001fff },
  106. { 0xb1, 0, 0, 0x0000000e },
  107. { 0x48, 0, 0, 0x00000010 },
  108. { 0x15, 0, 1, 0x0000fffe },
  109. { 0x6, 0, 0, 0x0000ffff },
  110. { 0x6, 0, 0, 0x00000000 },
  111. (000) ldh [12]
  112. (001) jeq #0x800 jt 2 jf 10
  113. (002) ldb [23]
  114. (003) jeq #0x6 jt 4 jf 10
  115. (004) ldh [20]
  116. (005) jset #0x1fff jt 10 jf 6
  117. (006) ldxb 4*([14]&0xf)
  118. (007) ldh [x + 16]
  119. (008) jeq #0xfffe jt 9 jf 10
  120. (009) ret #65535
  121. (010) ret #0
  122. */
  123. packet_info_t::packet_info_t()
  124. {
  125. src_port=0;
  126. dst_port=0;
  127. if (raw_mode == mode_faketcp)
  128. {
  129. protocol = IPPROTO_TCP;
  130. ack_seq = get_true_random_number();
  131. seq = get_true_random_number();
  132. has_ts=0;
  133. ts_ack=0;
  134. syn=0;
  135. ack=1;
  136. ack_seq_counter=0;
  137. //mylog(log_info,"<cons ,ts_ack= %u>\n",ts_ack);
  138. }
  139. else if (raw_mode == mode_udp)
  140. {
  141. protocol = IPPROTO_UDP;
  142. }
  143. else if (raw_mode == mode_icmp)
  144. {
  145. protocol = IPPROTO_ICMP;
  146. icmp_seq=0;
  147. }
  148. }
  149. int init_raw_socket()
  150. {
  151. g_ip_id_counter=get_true_random_number()%65535;
  152. if(lower_level==0)
  153. {
  154. raw_send_fd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
  155. if(raw_send_fd == -1) {
  156. mylog(log_fatal,"Failed to create raw_send_fd\n");
  157. //perror("Failed to create raw_send_fd");
  158. myexit(1);
  159. }
  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. }
  168. else
  169. {
  170. raw_send_fd = socket(PF_PACKET , SOCK_DGRAM , htons(ETH_P_IP));
  171. if(raw_send_fd == -1) {
  172. mylog(log_fatal,"Failed to create raw_send_fd\n");
  173. //perror("Failed to create raw_send_fd");
  174. myexit(1);
  175. }
  176. //init_ifindex(if_name);
  177. }
  178. if(force_socket_buf)
  179. {
  180. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  181. {
  182. mylog(log_fatal,"SO_SNDBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  183. myexit(1);
  184. }
  185. }
  186. else
  187. {
  188. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
  189. {
  190. mylog(log_fatal,"SO_SNDBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  191. myexit(1);
  192. }
  193. }
  194. //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  195. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  196. if(raw_recv_fd == -1) {
  197. mylog(log_fatal,"Failed to create raw_recv_fd\n");
  198. //perror("");
  199. myexit(1);
  200. }
  201. if(force_socket_buf)
  202. {
  203. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  204. {
  205. mylog(log_fatal,"SO_RCVBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  206. myexit(1);
  207. }
  208. }
  209. else
  210. {
  211. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
  212. {
  213. mylog(log_fatal,"SO_RCVBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  214. myexit(1);
  215. }
  216. }
  217. //IP_HDRINCL to tell the kernel that headers are included in the packet
  218. setnonblocking(raw_send_fd); //not really necessary
  219. setnonblocking(raw_recv_fd);
  220. return 0;
  221. }
  222. void init_filter(int port)
  223. {
  224. sock_fprog bpf;
  225. if(raw_mode==mode_faketcp||raw_mode==mode_udp)
  226. {
  227. filter_port=port;
  228. }
  229. if(disable_bpf_filter) return;
  230. //if(raw_mode==mode_icmp) return ;
  231. //code_tcp[8].k=code_tcp[10].k=port;
  232. if(raw_mode==mode_faketcp)
  233. {
  234. bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
  235. code_tcp[code_tcp_port_index].k=port;
  236. bpf.filter = code_tcp;
  237. }
  238. else if(raw_mode==mode_udp)
  239. {
  240. bpf.len = sizeof(code_udp)/sizeof(code_udp[0]);
  241. code_udp[code_udp_port_index].k=port;
  242. bpf.filter = code_udp;
  243. }
  244. else if(raw_mode==mode_icmp)
  245. {
  246. bpf.len = sizeof(code_icmp)/sizeof(code_icmp[0]);
  247. bpf.filter = code_icmp;
  248. }
  249. int dummy;
  250. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy)); //in case i forgot to remove
  251. if (ret != 0)
  252. {
  253. mylog(log_debug,"error remove fiter\n");
  254. //perror("filter");
  255. //exit(-1);
  256. }
  257. ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
  258. if (ret != 0)
  259. {
  260. mylog(log_fatal,"error set fiter\n");
  261. //perror("filter");
  262. myexit(-1);
  263. }
  264. }
  265. void remove_filter()
  266. {
  267. filter_port=0;
  268. int dummy;
  269. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
  270. if (ret != 0)
  271. {
  272. mylog(log_debug,"error remove fiter\n");
  273. //perror("filter");
  274. //exit(-1);
  275. }
  276. }
  277. int init_ifindex(const char * if_name,int &index)
  278. {
  279. struct ifreq ifr;
  280. size_t if_name_len=strlen(if_name);
  281. if (if_name_len<sizeof(ifr.ifr_name)) {
  282. memcpy(ifr.ifr_name,if_name,if_name_len);
  283. ifr.ifr_name[if_name_len]=0;
  284. } else {
  285. mylog(log_fatal,"interface name is too long\n");
  286. myexit(-1);
  287. }
  288. if (ioctl(raw_send_fd,SIOCGIFINDEX,&ifr)==-1) {
  289. mylog(log_fatal,"SIOCGIFINDEX fail ,%s\n",strerror(errno));
  290. myexit(-1);
  291. }
  292. index=ifr.ifr_ifindex;
  293. mylog(log_info,"ifname:%s ifindex:%d\n",if_name,index);
  294. return 0;
  295. }
  296. bool interface_has_arp(const char * interface) {
  297. struct ifreq ifr;
  298. // int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
  299. int sock=raw_send_fd;
  300. memset(&ifr, 0, sizeof(ifr));
  301. strcpy(ifr.ifr_name, interface);
  302. if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
  303. //perror("SIOCGIFFLAGS");
  304. mylog(log_fatal,"ioctl(sock, SIOCGIFFLAGS, &ifr) failed for interface %s,errno %s\n",interface,strerror(errno));
  305. myexit(-1);
  306. }
  307. //close(sock);
  308. return !(ifr.ifr_flags & IFF_NOARP);
  309. }
  310. struct route_info_t
  311. {
  312. string if_name;
  313. u32_t dest;
  314. u32_t mask;
  315. u32_t gw;
  316. u32_t flag;
  317. };
  318. int dest_idx=1;
  319. int gw_idx=2;
  320. int if_idx=0;
  321. int mask_idx=7;
  322. int flag_idx=3;
  323. vector<int> find_route_entry(const vector<route_info_t> &route_info_vec,u32_t ip)
  324. {
  325. vector<int> res;
  326. for(u32_t i=0;i<=32;i++)
  327. {
  328. u32_t mask=0xffffffff;
  329. //mask >>=i;
  330. //if(i==32) mask=0; //why 0xffffffff>>32 equals 0xffffffff??
  331. mask <<=i;
  332. if(i==32) mask=0;
  333. log_bare(log_debug,"(mask:%x)",mask);
  334. for(u32_t j=0;j<route_info_vec.size();j++)
  335. {
  336. const route_info_t & info=route_info_vec[j];
  337. if(info.mask!=mask)
  338. continue;
  339. log_bare(log_debug,"<<%d,%d>>",i,j);
  340. if((info.dest&mask)==(ip&mask))
  341. {
  342. log_bare(log_debug,"found!");
  343. res.push_back(j);
  344. }
  345. }
  346. if(res.size()!=0)
  347. {
  348. return res;
  349. }
  350. }
  351. return res;
  352. }
  353. int find_direct_dest(const vector<route_info_t> &route_info_vec,u32_t ip,u32_t &dest_ip,string &if_name)
  354. {
  355. vector<int> res;
  356. for(int i=0;i<1000;i++)
  357. {
  358. res=find_route_entry(route_info_vec,ip);
  359. log_bare(log_debug,"<entry:%u>",(u32_t)res.size());
  360. if(res.size()==0)
  361. {
  362. mylog(log_error,"cant find route entry\n");
  363. return -1;
  364. }
  365. if(res.size()>1)
  366. {
  367. mylog(log_error,"found duplicated entries\n");
  368. return -1;
  369. }
  370. if((route_info_vec[res[0]].flag&2)==0)
  371. {
  372. dest_ip=ip;
  373. if_name=route_info_vec[res[0]].if_name;
  374. return 0;
  375. }
  376. else
  377. {
  378. ip=route_info_vec[res[0]].gw;
  379. }
  380. }
  381. mylog(log_error,"dead loop in find_direct_dest\n");
  382. return -1;
  383. }
  384. struct arp_info_t
  385. {
  386. u32_t ip;
  387. string hw;
  388. string if_name;
  389. };
  390. int arp_ip_idx=0;
  391. int arp_hw_idx=3;
  392. int arp_if_idx=5;
  393. int find_arp(const vector<arp_info_t> &arp_info_vec,u32_t ip,string if_name,string &hw)
  394. {
  395. int pos=-1;
  396. int count=0;
  397. for(u32_t i=0;i<arp_info_vec.size();i++)
  398. {
  399. const arp_info_t & info=arp_info_vec[i];
  400. if(info.if_name!=if_name) continue;
  401. if(info.ip==ip)
  402. {
  403. count++;
  404. pos=i;
  405. }
  406. }
  407. if(count==0)
  408. {
  409. //mylog(log_warn,"cant find arp entry for %s %s,using 00:00:00:00:00:00\n",my_ntoa(ip),if_name.c_str());
  410. //hw="00:00:00:00:00:00";
  411. mylog(log_error,"cant find arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
  412. return -1;
  413. }
  414. if(count>1)
  415. {
  416. mylog(log_error,"find multiple arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
  417. return -1;
  418. }
  419. hw=arp_info_vec[pos].hw;
  420. return 0;
  421. }
  422. int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw)
  423. {
  424. ip=htonl(ip);
  425. if(ip==htonl(inet_addr("127.0.0.1")))
  426. {
  427. dest_ip=ntohl(ip);
  428. if_name="lo";
  429. hw="00:00:00:00:00:00";
  430. return 0;
  431. }
  432. string route_file;
  433. if(read_file("/proc/net/route",route_file)!=0) return -1;
  434. string arp_file;
  435. if(read_file("/proc/net/arp",arp_file)!=0) return -1;
  436. log_bare(log_debug,"/proc/net/route:<<%s>>\n",route_file.c_str());
  437. log_bare(log_debug,"/proc/net/arp:<<%s>>\n",route_file.c_str());
  438. auto route_vec2=string_to_vec2(route_file.c_str());
  439. vector<route_info_t> route_info_vec;
  440. for(u32_t i=1;i<route_vec2.size();i++)
  441. {
  442. log_bare(log_debug,"<size:%u>",(u32_t)route_vec2[i].size());
  443. if(route_vec2[i].size()!=11)
  444. {
  445. mylog(log_error,"route coloum %d !=11 \n",int(route_vec2[i].size()));
  446. return -1;
  447. }
  448. route_info_t tmp;
  449. tmp.if_name=route_vec2[i][if_idx];
  450. if(hex_to_u32_with_endian(route_vec2[i][dest_idx],tmp.dest)!=0) return -1;
  451. if(hex_to_u32_with_endian(route_vec2[i][gw_idx],tmp.gw)!=0) return -1;
  452. if(hex_to_u32_with_endian(route_vec2[i][mask_idx],tmp.mask)!=0) return -1;
  453. if(hex_to_u32(route_vec2[i][flag_idx],tmp.flag)!=0)return -1;
  454. route_info_vec.push_back(tmp);
  455. for(u32_t j=0;j<route_vec2[i].size();j++)
  456. {
  457. log_bare(log_debug,"<%s>",route_vec2[i][j].c_str());
  458. }
  459. log_bare(log_debug,"%s dest:%x mask:%x gw:%x flag:%x",tmp.if_name.c_str(),tmp.dest,tmp.mask,tmp.gw,tmp.flag);
  460. log_bare(log_debug,"\n");
  461. }
  462. if(find_direct_dest(route_info_vec,ip,dest_ip,if_name)!=0)
  463. {
  464. mylog(log_error,"find_direct_dest failed for ip %s\n",my_ntoa(ntohl(ip)));
  465. return -1;
  466. }
  467. log_bare(log_debug,"========\n");
  468. auto arp_vec2=string_to_vec2(arp_file.c_str());
  469. vector<arp_info_t> arp_info_vec;
  470. for(u32_t i=1;i<arp_vec2.size();i++)
  471. {
  472. log_bare(log_debug,"<<arp_vec2[i].size(): %d>>",(int)arp_vec2[i].size());
  473. for(u32_t j=0;j<arp_vec2[i].size();j++)
  474. {
  475. log_bare(log_debug,"<%s>",arp_vec2[i][j].c_str());
  476. }
  477. if(arp_vec2[i].size()!=6)
  478. {
  479. mylog(log_error,"arp coloum %d !=11 \n",int(arp_vec2[i].size()));
  480. return -1;
  481. }
  482. arp_info_t tmp;
  483. tmp.if_name=arp_vec2[i][arp_if_idx];
  484. tmp.hw=arp_vec2[i][arp_hw_idx];
  485. tmp.ip=htonl(inet_addr(arp_vec2[i][arp_ip_idx].c_str()));
  486. arp_info_vec.push_back(tmp);
  487. log_bare(log_debug,"\n");
  488. }
  489. if(!interface_has_arp(if_name.c_str()))
  490. {
  491. mylog(log_info,"%s is a noarp interface,using 00:00:00:00:00:00\n",if_name.c_str());
  492. hw="00:00:00:00:00:00";
  493. }
  494. else if(find_arp(arp_info_vec,dest_ip,if_name,hw)!=0)
  495. {
  496. mylog(log_error,"find_arp failed for dest_ip %s ,if_name %s\n",my_ntoa(ntohl(ip)),if_name.c_str());
  497. return -1;
  498. }
  499. //printf("%s\n",hw.c_str());
  500. dest_ip=ntohl(dest_ip);
  501. return 0;
  502. }
  503. int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
  504. {
  505. const packet_info_t &send_info=raw_info.send_info;
  506. const packet_info_t &recv_info=raw_info.recv_info;
  507. char send_raw_ip_buf[buf_len];
  508. if(raw_info.disabled)
  509. {
  510. mylog(log_debug,"[%s,%d]connection disabled, no packet will be sent\n",my_ntoa(recv_info.src_ip),recv_info.src_port);
  511. assert(max_rst_allowed>=0);
  512. return 0;
  513. }
  514. struct iphdr *iph = (struct iphdr *) send_raw_ip_buf;
  515. memset(iph,0,sizeof(iphdr));
  516. iph->ihl = sizeof(iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
  517. iph->version = 4;
  518. iph->tos = 0;
  519. if(lower_level)
  520. {
  521. //iph->id=0;
  522. iph->id = htons (g_ip_id_counter++); //Id of this packet
  523. }
  524. else
  525. iph->id = htons (g_ip_id_counter++); //Id of this packet
  526. //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
  527. iph->frag_off = htons(0x4000); //DF set,others are zero
  528. // iph->frag_off = htons(0x0000); //DF set,others are zero
  529. iph->ttl = 64;
  530. iph->protocol = send_info.protocol;
  531. iph->check = 0; //Set to 0 before calculating checksum
  532. iph->saddr = send_info.src_ip; //Spoof the source ip address
  533. iph->daddr = send_info.dst_ip;
  534. uint16_t ip_tot_len=sizeof (struct iphdr)+payloadlen;
  535. 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
  536. else
  537. iph->tot_len = 0;
  538. memcpy(send_raw_ip_buf+sizeof(iphdr) , payload, payloadlen);
  539. if(lower_level) iph->check =
  540. csum ((unsigned short *) send_raw_ip_buf, iph->ihl*4); //this is not necessary ,kernel will always auto fill this
  541. else
  542. iph->check=0;
  543. int ret;
  544. if(lower_level==0)
  545. {
  546. struct sockaddr_in sin={0};
  547. sin.sin_family = AF_INET;
  548. //sin.sin_port = htons(info.dst_port); //dont need this
  549. sin.sin_addr.s_addr = send_info.dst_ip;
  550. ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  551. }
  552. else
  553. {
  554. struct sockaddr_ll addr={0}; //={0} not necessary
  555. memcpy(&addr,&send_info.addr_ll,sizeof(addr));
  556. ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &addr, sizeof (addr));
  557. }
  558. if(ret==-1)
  559. {
  560. mylog(log_trace,"sendto failed\n");
  561. //perror("why?");
  562. return -1;
  563. }
  564. else
  565. {
  566. //mylog(log_info,"sendto succ\n");
  567. }
  568. return 0;
  569. }
  570. int peek_raw(packet_info_t &peek_info)
  571. { static char peek_raw_buf[buf_len];
  572. char *ip_begin=peek_raw_buf+link_level_header_len;
  573. struct sockaddr saddr={0};
  574. socklen_t saddr_size=sizeof(saddr);
  575. 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
  576. iphdr * iph = (struct iphdr *) (ip_begin);
  577. //mylog(log_info,"recv_len %d\n",recv_len);
  578. if(recv_len<int(sizeof(iphdr)))
  579. {
  580. mylog(log_trace,"failed here %d %d\n",recv_len,int(sizeof(iphdr)));
  581. mylog(log_trace,"%s\n ",strerror(errno));
  582. return -1;
  583. }
  584. peek_info.src_ip=iph->saddr;
  585. unsigned short iphdrlen =iph->ihl*4;
  586. char *payload=ip_begin+iphdrlen;
  587. //mylog(log_info,"protocol %d\n",iph->protocol);
  588. switch(raw_mode)
  589. {
  590. case mode_faketcp:
  591. {
  592. if(iph->protocol!=IPPROTO_TCP)
  593. {
  594. mylog(log_trace,"failed here");
  595. return -1;
  596. }
  597. struct tcphdr *tcph=(tcphdr *)payload;
  598. if(recv_len<int( iphdrlen+sizeof(tcphdr) ))
  599. {
  600. mylog(log_trace,"failed here");
  601. return -1;
  602. }
  603. peek_info.src_port=ntohs(tcph->source);
  604. peek_info.syn=tcph->syn;
  605. break;
  606. }
  607. case mode_udp:
  608. {
  609. if(iph->protocol!=IPPROTO_UDP) return -1;
  610. struct udphdr *udph=(udphdr *)payload;
  611. if(recv_len<int(iphdrlen+sizeof(udphdr)))
  612. return -1;
  613. peek_info.src_port=ntohs(udph->source);
  614. break;
  615. }
  616. case mode_icmp:
  617. {
  618. if(iph->protocol!=IPPROTO_ICMP) return -1;
  619. struct icmphdr *icmph=(icmphdr *)payload;
  620. if(recv_len<int( iphdrlen+sizeof(icmphdr) ))
  621. return -1;
  622. peek_info.src_port=ntohs(icmph->id);
  623. break;
  624. }
  625. default:return -1;
  626. }
  627. return 0;
  628. }
  629. int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
  630. {
  631. const packet_info_t &send_info=raw_info.send_info;
  632. packet_info_t &recv_info=raw_info.recv_info;
  633. static char recv_raw_ip_buf[buf_len];
  634. iphdr * iph;
  635. struct sockaddr_ll saddr={0};
  636. socklen_t saddr_size = sizeof(saddr);
  637. int flag=0;
  638. int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len+1, flag ,(sockaddr*)&saddr , &saddr_size);
  639. if(recv_len==max_data_len+1)
  640. {
  641. mylog(log_warn,"huge packet, data_len > %d,dropped\n",max_data_len);
  642. return -1;
  643. }
  644. if(recv_len<0)
  645. {
  646. mylog(log_trace,"recv_len %d\n",recv_len);
  647. return -1;
  648. }
  649. if(recv_len<int(link_level_header_len))
  650. {
  651. mylog(log_trace,"length error\n");
  652. }
  653. if(link_level_header_len ==14&&(recv_raw_ip_buf[12]!=8||recv_raw_ip_buf[13]!=0))
  654. {
  655. mylog(log_trace,"not an ipv4 packet!\n");
  656. return -1;
  657. }
  658. char *ip_begin=recv_raw_ip_buf+link_level_header_len; //14 is eth net header
  659. iph = (struct iphdr *) (ip_begin);
  660. recv_info.src_ip=iph->saddr;
  661. recv_info.dst_ip=iph->daddr;
  662. recv_info.protocol=iph->protocol;
  663. if(lower_level)
  664. {
  665. memcpy(&recv_info.addr_ll,&saddr,sizeof(recv_info.addr_ll));
  666. }
  667. if(bind_address_uint32!=0 &&recv_info.dst_ip!=bind_address_uint32)
  668. {
  669. mylog(log_trace,"bind adress doenst match, dropped\n");
  670. //printf(" bind adress doenst match, dropped\n");
  671. return -1;
  672. }
  673. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  674. mylog(log_trace,"iph ihl error\n");
  675. return -1;
  676. }
  677. int ip_len=ntohs(iph->tot_len);
  678. if(recv_len-int(link_level_header_len) <ip_len)
  679. {
  680. mylog(log_debug,"incomplete packet\n");
  681. return -1;
  682. }
  683. unsigned short iphdrlen =iph->ihl*4;
  684. u32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  685. if(ip_chk!=0)
  686. {
  687. mylog(log_debug,"ip header error %x\n",ip_chk);
  688. return -1;
  689. }
  690. payload=ip_begin+iphdrlen;
  691. payloadlen=ip_len-iphdrlen;
  692. if(payloadlen<0)
  693. {
  694. mylog(log_warn,"error payload len\n");
  695. return -1;
  696. }
  697. return 0;
  698. }
  699. int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
  700. {
  701. const packet_info_t &send_info=raw_info.send_info;
  702. const packet_info_t &recv_info=raw_info.recv_info;
  703. char send_raw_icmp_buf[buf_len];
  704. icmphdr *icmph=(struct icmphdr *) (send_raw_icmp_buf);
  705. memset(icmph,0,sizeof(icmphdr));
  706. if(program_mode==client_mode)
  707. {
  708. icmph->type=8;
  709. }
  710. else
  711. {
  712. icmph->type=0;
  713. }
  714. icmph->code=0;
  715. icmph->id=htons(send_info.src_port);
  716. icmph->seq=htons(send_info.icmp_seq); /////////////modify
  717. memcpy(send_raw_icmp_buf+sizeof(icmphdr),payload,payloadlen);
  718. icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(icmphdr)+payloadlen);
  719. if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(icmphdr)+payloadlen)!=0)
  720. {
  721. return -1;
  722. }
  723. /*if(program_mode==client_mode)
  724. {
  725. send_info.icmp_seq++;
  726. }*/
  727. return 0;
  728. }
  729. int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
  730. {
  731. const packet_info_t &send_info=raw_info.send_info;
  732. const packet_info_t &recv_info=raw_info.recv_info;
  733. char send_raw_udp_buf[buf_len];
  734. udphdr *udph=(struct udphdr *) (send_raw_udp_buf
  735. + sizeof(struct pseudo_header));
  736. memset(udph,0,sizeof(udphdr));
  737. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_udp_buf);
  738. udph->source = htons(send_info.src_port);
  739. udph->dest = htons(send_info.dst_port);
  740. int udp_tot_len=payloadlen+sizeof(udphdr);
  741. if(udp_tot_len>65535)
  742. {
  743. mylog(log_debug,"invalid len\n");
  744. return -1;
  745. }
  746. mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len);
  747. udph->len=htons(uint16_t(udp_tot_len));
  748. memcpy(send_raw_udp_buf+sizeof(struct pseudo_header)+sizeof(udphdr),payload,payloadlen);
  749. psh->source_address = send_info.src_ip;
  750. psh->dest_address = send_info.dst_ip;
  751. psh->placeholder = 0;
  752. psh->protocol = IPPROTO_UDP;
  753. psh->tcp_length = htons(uint16_t(udp_tot_len));
  754. int csum_size = sizeof(struct pseudo_header) +udp_tot_len ;
  755. udph->check = csum( (unsigned short*) send_raw_udp_buf, csum_size);
  756. if(send_raw_ip(raw_info,send_raw_udp_buf+ sizeof(struct pseudo_header),udp_tot_len)!=0)
  757. {
  758. return -1;
  759. }
  760. return 0;
  761. }
  762. int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { //TODO seq increase
  763. const packet_info_t &send_info=raw_info.send_info;
  764. const packet_info_t &recv_info=raw_info.recv_info;
  765. //mylog(log_debug,"syn %d\n",send_info.syn);
  766. char send_raw_tcp_buf[buf_len];
  767. //char *send_raw_tcp_buf=send_raw_tcp_buf0;
  768. struct tcphdr *tcph = (struct tcphdr *) (send_raw_tcp_buf
  769. + sizeof(struct pseudo_header));
  770. memset(tcph,0,sizeof(tcphdr));
  771. struct pseudo_header *psh = (struct pseudo_header *) (send_raw_tcp_buf);
  772. //TCP Header
  773. tcph->source = htons(send_info.src_port);
  774. tcph->dest = htons(send_info.dst_port);
  775. tcph->seq = htonl(send_info.seq);
  776. tcph->ack_seq = htonl(send_info.ack_seq);
  777. tcph->fin = 0;
  778. tcph->syn = send_info.syn;
  779. tcph->rst = 0;
  780. tcph->psh = send_info.psh;
  781. tcph->ack = send_info.ack;
  782. if (tcph->syn == 1) {
  783. tcph->doff = 10; //tcp header size
  784. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  785. send_raw_tcp_buf[i++] = 0x02; //mss
  786. send_raw_tcp_buf[i++] = 0x04;
  787. send_raw_tcp_buf[i++] = 0x05;
  788. send_raw_tcp_buf[i++] = (char)0xb4;
  789. //raw_send_buf[i++]=0x01;
  790. //raw_send_buf[i++]=0x01;
  791. send_raw_tcp_buf[i++] = 0x04; //sack ok
  792. send_raw_tcp_buf[i++] = 0x02; //sack ok
  793. send_raw_tcp_buf[i++] = 0x08; //ts i=6
  794. send_raw_tcp_buf[i++] = 0x0a; //i=7
  795. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
  796. // (u32_t) get_current_time());
  797. u32_t ts=htonl((u32_t) get_current_time());
  798. memcpy(&send_raw_tcp_buf[i],&ts,sizeof(ts));
  799. i += 4;
  800. //mylog(log_info,"[syn]<send_info.ts_ack= %u>\n",send_info.ts_ack);
  801. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  802. u32_t ts_ack=htonl(send_info.ts_ack);
  803. memcpy(&send_raw_tcp_buf[i],&ts_ack,sizeof(ts_ack));
  804. i += 4;
  805. send_raw_tcp_buf[i++] = 0x01;
  806. send_raw_tcp_buf[i++] = 0x03;
  807. send_raw_tcp_buf[i++] = 0x03;
  808. send_raw_tcp_buf[i++] = wscale;
  809. } else {
  810. tcph->doff = 8;
  811. int i = sizeof(pseudo_header)+sizeof(tcphdr);
  812. send_raw_tcp_buf[i++] = 0x01;
  813. send_raw_tcp_buf[i++] = 0x01;
  814. send_raw_tcp_buf[i++] = 0x08; //ts //i=2
  815. send_raw_tcp_buf[i++] = 0x0a; //i=3;
  816. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
  817. // (u32_t) get_current_time());
  818. u32_t ts=htonl((u32_t) get_current_time());
  819. memcpy(&send_raw_tcp_buf[i],&ts,sizeof(ts));
  820. i += 4;
  821. //mylog(log_info,"<send_info.ts_ack= %u>\n",send_info.ts_ack);
  822. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  823. u32_t ts_ack=htonl(send_info.ts_ack);
  824. memcpy(&send_raw_tcp_buf[i],&ts_ack,sizeof(ts_ack));
  825. i += 4;
  826. }
  827. tcph->urg = 0;
  828. //tcph->window = htons((uint16_t)(1024));
  829. tcph->window = htons((uint16_t) (receive_window_lower_bound + random() % receive_window_random_range));
  830. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  831. tcph->urg_ptr = 0;
  832. char *tcp_data = send_raw_tcp_buf+sizeof(struct pseudo_header) + tcph->doff * 4;
  833. memcpy(tcp_data, payload, payloadlen);
  834. psh->source_address = send_info.src_ip;
  835. psh->dest_address = send_info.dst_ip;
  836. psh->placeholder = 0;
  837. psh->protocol = IPPROTO_TCP;
  838. psh->tcp_length = htons(tcph->doff * 4 + payloadlen);
  839. int csum_size = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  840. tcph->check = csum( (unsigned short*) send_raw_tcp_buf, csum_size);
  841. int tcp_totlen=tcph->doff*4 + payloadlen;
  842. if(send_raw_ip(raw_info,send_raw_tcp_buf+ sizeof(struct pseudo_header),tcp_totlen)!=0)
  843. {
  844. return -1;
  845. }
  846. raw_info.send_info.data_len=payloadlen;
  847. return 0;
  848. }
  849. /*
  850. int send_raw_tcp_deprecated(const packet_info_t &info,const char * payload,int payloadlen)
  851. {
  852. static uint16_t ip_id=1;
  853. char raw_send_buf[buf_len];
  854. char raw_send_buf2[buf_len];
  855. //if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
  856. mylog(log_trace,"send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  857. char *data;
  858. memset(raw_send_buf,0,payloadlen+100);
  859. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  860. //TCP header
  861. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  862. struct sockaddr_in sin;
  863. struct pseudo_header psh;
  864. //some address resolution
  865. sin.sin_family = AF_INET;
  866. sin.sin_port = htons(info.dst_port);
  867. sin.sin_addr.s_addr = info.dst_ip;
  868. //Fill in the IP Header
  869. iph->ihl = 5;
  870. iph->version = 4;
  871. iph->tos = 0;
  872. iph->id = htonl (ip_id++); //Id of this packet
  873. iph->frag_off = htons(0x4000); //DF set,others are zero
  874. iph->ttl = 64;
  875. iph->protocol = IPPROTO_TCP;
  876. iph->check = 0; //Set to 0 before calculating checksum
  877. iph->saddr = info.src_ip; //Spoof the source ip address
  878. iph->daddr = info.dst_ip;
  879. //TCP Header
  880. tcph->source = htons(info.src_port);
  881. tcph->dest = htons(info.dst_port);
  882. tcph->seq =htonl(info.seq);
  883. tcph->ack_seq = htonl(info.ack_seq);
  884. tcph->fin=0;
  885. tcph->syn=info.syn;
  886. tcph->rst=0;
  887. tcph->psh=info.psh;
  888. tcph->ack=info.ack;
  889. if(tcph->syn==1)
  890. {
  891. tcph->doff = 10; //tcp header size
  892. int i=sizeof (struct iphdr)+20;
  893. raw_send_buf[i++]=0x02;//mss
  894. raw_send_buf[i++]=0x04;
  895. raw_send_buf[i++]=0x05;
  896. raw_send_buf[i++]=0xb4;
  897. //raw_send_buf[i++]=0x01;
  898. //raw_send_buf[i++]=0x01;
  899. raw_send_buf[i++]=0x04; //sack ok
  900. raw_send_buf[i++]=0x02; //sack ok
  901. raw_send_buf[i++]=0x08; //i=6;
  902. raw_send_buf[i++]=0x0a;
  903. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  904. i+=4;
  905. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  906. i+=4;
  907. raw_send_buf[i++]=0x01;
  908. raw_send_buf[i++]=0x03;
  909. raw_send_buf[i++]=0x03;
  910. raw_send_buf[i++]=0x05;
  911. }
  912. else
  913. {
  914. tcph->doff=8;
  915. int i=sizeof (struct iphdr)+20;
  916. raw_send_buf[i++]=0x01;
  917. raw_send_buf[i++]=0x01;
  918. raw_send_buf[i++]=0x08; //i=0;
  919. raw_send_buf[i++]=0x0a;
  920. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  921. i+=4;
  922. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  923. i+=4;
  924. }
  925. tcph->urg=0;
  926. //tcph->window = htons((uint16_t)(1024));
  927. tcph->window = htons((uint16_t)(10240+random()%100));
  928. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  929. tcph->urg_ptr = 0;
  930. //Data part
  931. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  932. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  933. memcpy(data , payload, payloadlen);
  934. psh.source_address = info.src_ip;
  935. psh.dest_address = sin.sin_addr.s_addr;
  936. psh.placeholder = 0;
  937. psh.protocol = IPPROTO_TCP;
  938. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  939. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  940. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  941. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  942. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  943. //Ip checksum
  944. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  945. mylog(log_trace,"sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
  946. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  947. if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
  948. {
  949. if(seq_mode==0)
  950. {
  951. }
  952. else if(seq_mode==1)
  953. {
  954. g_packet_info_send.seq+=payloadlen;
  955. }
  956. else if(seq_mode==2)
  957. {
  958. if(random()% 5==3 )
  959. g_packet_info_send.seq+=payloadlen;
  960. }
  961. }
  962. mylog(log_trace,"<ret:%d>\n",ret);
  963. if(ret<0)
  964. {
  965. mylog(log_fatal,"");
  966. perror("raw send error\n");
  967. //printf("send error\n");
  968. }
  969. return 0;
  970. }
  971. */
  972. int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  973. {
  974. const packet_info_t &send_info=raw_info.send_info;
  975. packet_info_t &recv_info=raw_info.recv_info;
  976. static char recv_raw_icmp_buf[buf_len];
  977. char * ip_payload;
  978. int ip_payloadlen;
  979. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  980. {
  981. mylog(log_debug,"recv_raw_ip error\n");
  982. return -1;
  983. }
  984. if(recv_info.protocol!=IPPROTO_ICMP)
  985. {
  986. //printf("not udp protocol\n");
  987. return -1;
  988. }
  989. icmphdr *icmph=(struct icmphdr *) (ip_payload);
  990. if(ntohs(icmph->id)!=send_info.src_port)
  991. {
  992. mylog(log_debug,"icmp id mis-match,ignored\n");
  993. return -1;
  994. }
  995. recv_info.src_port=recv_info.dst_port=ntohs(icmph->id);
  996. recv_info.icmp_seq=ntohs(icmph->seq);
  997. if(program_mode==client_mode)
  998. {
  999. if(icmph->type!=0)
  1000. return -1;
  1001. }
  1002. else
  1003. {
  1004. if(icmph->type!=8)
  1005. return -1;
  1006. }
  1007. if(icmph->code!=0)
  1008. return -1;
  1009. unsigned short check = csum( (unsigned short*) ip_payload, ip_payloadlen);
  1010. if(check!=0)
  1011. {
  1012. mylog(log_debug,"icmp checksum fail %x\n",check);
  1013. return -1;
  1014. }
  1015. //mylog(log_info,"program_mode=%d\n",program_mode);
  1016. /*
  1017. if(program_mode==server_mode)
  1018. {
  1019. send_info.icmp_seq=ntohs(icmph->seq);
  1020. //mylog(log_info,"send_info.seq=%d\n",send_info.seq);
  1021. }*/
  1022. payload=ip_payload+sizeof(icmphdr);
  1023. payloadlen=ip_payloadlen-sizeof(icmphdr);
  1024. mylog(log_trace,"get a packet len=%d\n",payloadlen);
  1025. return 0;
  1026. }
  1027. int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  1028. {
  1029. const packet_info_t &send_info=raw_info.send_info;
  1030. packet_info_t &recv_info=raw_info.recv_info;
  1031. static char recv_raw_udp_buf[buf_len];
  1032. char * ip_payload;
  1033. int ip_payloadlen;
  1034. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1035. {
  1036. mylog(log_debug,"recv_raw_ip error\n");
  1037. return -1;
  1038. }
  1039. if(recv_info.protocol!=IPPROTO_UDP)
  1040. {
  1041. //printf("not udp protocol\n");
  1042. return -1;
  1043. }
  1044. if(ip_payloadlen<int( sizeof(udphdr) ))
  1045. {
  1046. mylog(log_debug,"too short to hold udpheader\n");
  1047. return -1;
  1048. }
  1049. udphdr *udph=(struct udphdr*)ip_payload;
  1050. if(int(ntohs(udph->len))!=ip_payloadlen)
  1051. {
  1052. mylog(log_debug,"udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
  1053. return -1;
  1054. }
  1055. if(udph->dest!=ntohs(uint16_t(filter_port)))
  1056. {
  1057. //printf("%x %x",tcph->dest,);
  1058. return -1;
  1059. }
  1060. memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1061. struct pseudo_header *psh=(pseudo_header *)recv_raw_udp_buf ;
  1062. psh->source_address = recv_info.src_ip;
  1063. psh->dest_address = recv_info.dst_ip;
  1064. psh->placeholder = 0;
  1065. psh->protocol = IPPROTO_UDP;
  1066. psh->tcp_length = htons(ip_payloadlen);
  1067. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  1068. uint16_t udp_chk = csum( (unsigned short*) recv_raw_udp_buf, csum_len);
  1069. if(udp_chk!=0)
  1070. {
  1071. mylog(log_debug,"udp_chk:%x\n",udp_chk);
  1072. mylog(log_debug,"udp header error\n");
  1073. return -1;
  1074. }
  1075. char *udp_begin=recv_raw_udp_buf+sizeof(struct pseudo_header);
  1076. recv_info.src_port=ntohs(udph->source);
  1077. recv_info.dst_port=ntohs(udph->dest);
  1078. payloadlen = ip_payloadlen-sizeof(udphdr);
  1079. payload=udp_begin+sizeof(udphdr);
  1080. return 0;
  1081. }
  1082. int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
  1083. {
  1084. const packet_info_t &send_info=raw_info.send_info;
  1085. packet_info_t &recv_info=raw_info.recv_info;
  1086. static char recv_raw_tcp_buf[buf_len];
  1087. char * ip_payload;
  1088. int ip_payloadlen;
  1089. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1090. {
  1091. mylog(log_debug,"recv_raw_ip error\n");
  1092. return -1;
  1093. }
  1094. if(recv_info.protocol!=IPPROTO_TCP)
  1095. {
  1096. //printf("not tcp protocol\n");
  1097. return -1;
  1098. }
  1099. tcphdr * tcph=(struct tcphdr*)ip_payload;
  1100. unsigned short tcphdrlen = tcph->doff*4;
  1101. if (!(tcphdrlen > 0 && tcphdrlen <=60)) {
  1102. mylog(log_debug,"tcph error\n");
  1103. return 0;
  1104. }
  1105. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1106. {
  1107. //printf("%x %x",tcph->dest,);
  1108. return -1;
  1109. }
  1110. memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1111. struct pseudo_header *psh=(pseudo_header *)recv_raw_tcp_buf ;
  1112. psh->source_address = recv_info.src_ip;
  1113. psh->dest_address = recv_info.dst_ip;
  1114. psh->placeholder = 0;
  1115. psh->protocol = IPPROTO_TCP;
  1116. psh->tcp_length = htons(ip_payloadlen);
  1117. int csum_len=sizeof(struct pseudo_header)+ip_payloadlen;
  1118. uint16_t tcp_chk = csum( (unsigned short*) recv_raw_tcp_buf, csum_len);
  1119. if(tcp_chk!=0)
  1120. {
  1121. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  1122. mylog(log_debug,"tcp header error\n");
  1123. return -1;
  1124. }
  1125. char *tcp_begin=recv_raw_tcp_buf+sizeof(struct pseudo_header); //ip packet's data part
  1126. char *tcp_option=recv_raw_tcp_buf+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1127. recv_info.has_ts=0;
  1128. recv_info.ts=0;
  1129. if(tcph->doff==10)
  1130. {
  1131. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1132. {
  1133. recv_info.has_ts=1;
  1134. //recv_info.ts=ntohl(*(u32_t*)(&tcp_option[8]));
  1135. memcpy(&recv_info.ts,&tcp_option[8],sizeof(recv_info.ts));
  1136. recv_info.ts=ntohl(recv_info.ts);
  1137. //recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[12]));
  1138. memcpy(&recv_info.ts_ack,&tcp_option[12],sizeof(recv_info.ts_ack));
  1139. recv_info.ts_ack=ntohl(recv_info.ts_ack);
  1140. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1141. }
  1142. else
  1143. {
  1144. // mylog(log_info,"\n");
  1145. }
  1146. }
  1147. else if(tcph->doff==8)
  1148. {
  1149. if(tcp_option[2]==0x08 &&tcp_option[3]==0x0a)
  1150. {
  1151. recv_info.has_ts=1;
  1152. //recv_info.ts=ntohl(*(u32_t*)(&tcp_option[4]));
  1153. memcpy(&recv_info.ts,&tcp_option[4],sizeof(recv_info.ts));
  1154. recv_info.ts=ntohl(recv_info.ts);
  1155. //recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[8]));
  1156. memcpy(&recv_info.ts_ack,&tcp_option[8],sizeof(recv_info.ts_ack));
  1157. recv_info.ts_ack=ntohl(recv_info.ts_ack);
  1158. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1159. }
  1160. else
  1161. {
  1162. //mylog(log_info,"!!!\n");
  1163. }
  1164. }
  1165. else
  1166. {
  1167. //mylog(log_info,"tcph->doff= %u\n",tcph->doff);
  1168. }
  1169. recv_info.ack=tcph->ack;
  1170. recv_info.syn=tcph->syn;
  1171. recv_info.rst=tcph->rst;
  1172. recv_info.src_port=ntohs(tcph->source);
  1173. recv_info.dst_port=ntohs(tcph->dest);
  1174. recv_info.seq=ntohl(tcph->seq);
  1175. // recv_info.last_last_ack_seq=recv_info.last_ack_seq;
  1176. //recv_info.last_ack_seq=recv_info.ack_seq;
  1177. u32_t last_ack_seq=recv_info.ack_seq;
  1178. recv_info.ack_seq=ntohl(tcph->ack_seq);
  1179. if(recv_info.ack_seq==last_ack_seq)
  1180. {
  1181. recv_info.ack_seq_counter++;
  1182. }
  1183. else
  1184. {
  1185. recv_info.ack_seq_counter=0;
  1186. }
  1187. recv_info.psh=tcph->psh;
  1188. if(tcph->rst==1)
  1189. {
  1190. raw_info.rst_received++;
  1191. if(max_rst_to_show>0)
  1192. {
  1193. if(raw_info.rst_received < max_rst_to_show)
  1194. {
  1195. mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
  1196. }
  1197. else if(raw_info.rst_received == max_rst_to_show)
  1198. {
  1199. mylog(log_warn,"[%s,%d]rst==1,cnt=%d >=max_rst_to_show, this log will be muted for current connection\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
  1200. }
  1201. else
  1202. {
  1203. mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
  1204. }
  1205. }
  1206. else if(max_rst_to_show==0)
  1207. {
  1208. mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
  1209. }
  1210. else
  1211. {
  1212. mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
  1213. }
  1214. if(max_rst_allowed>=0 && raw_info.rst_received==max_rst_allowed+1 )
  1215. {
  1216. mylog(log_warn,"[%s,%d]connection disabled because of rst_received=%d > max_rst_allow=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received,(int)max_rst_allowed );
  1217. raw_info.disabled=1;
  1218. }
  1219. }
  1220. /* if(recv_info.has_ts)
  1221. {
  1222. send_info.ts_ack=recv_info.ts; //////////////////////////////////////////////modify
  1223. }*/
  1224. payloadlen = ip_payloadlen-tcphdrlen;
  1225. payload=tcp_begin+tcphdrlen;
  1226. /*if (recv_info.syn == 0 && recv_info.ack == 1&& payloadlen != 0) //only modify send_info when the packet is not part of handshake
  1227. {
  1228. send_info.ack_seq=recv_info.seq;
  1229. }*/
  1230. raw_info.recv_info.data_len=payloadlen;
  1231. return 0;
  1232. }
  1233. /*
  1234. int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
  1235. {
  1236. static char buf[buf_len];
  1237. char raw_recv_buf[buf_len];
  1238. char raw_recv_buf2[buf_len];
  1239. char raw_recv_buf3[buf_len];
  1240. iphdr * iph;
  1241. tcphdr * tcph;
  1242. int size;
  1243. struct sockaddr saddr;
  1244. socklen_t saddr_size;
  1245. saddr_size = sizeof(saddr);
  1246. mylog(log_trace,"raw!\n");
  1247. size = recvfrom(raw_recv_fd, buf, max_data_len, 0 ,&saddr , &saddr_size);
  1248. if(buf[12]!=8||buf[13]!=0)
  1249. {
  1250. mylog(log_debug,"not an ipv4 packet!\n");
  1251. return -1;
  1252. }
  1253. char *ip_begin=buf+14;
  1254. iph = (struct iphdr *) (ip_begin);
  1255. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  1256. mylog(log_debug,"iph ihl error");
  1257. return -1;
  1258. }
  1259. if (iph->protocol != IPPROTO_TCP) {
  1260. mylog(log_debug,"iph protocal != tcp\n");
  1261. return -1;
  1262. }
  1263. int ip_len=ntohs(iph->tot_len);
  1264. unsigned short iphdrlen =iph->ihl*4;
  1265. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  1266. unsigned short tcphdrlen = tcph->doff*4;
  1267. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  1268. mylog(log_debug,"tcph error");
  1269. return 0;
  1270. }
  1271. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1272. {
  1273. //printf("%x %x",tcph->dest,);
  1274. return -1;
  1275. }
  1276. /////ip
  1277. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  1278. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  1279. /////ip end
  1280. ///tcp
  1281. struct pseudo_header psh;
  1282. psh.source_address = iph->saddr;
  1283. psh.dest_address = iph->daddr;
  1284. psh.placeholder = 0;
  1285. psh.protocol = IPPROTO_TCP;
  1286. psh.tcp_length = htons(ip_len-iphdrlen);
  1287. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  1288. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  1289. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  1290. if(ip_chk!=0)
  1291. {
  1292. mylog(log_debug,"ip header error %d\n",ip_chk);
  1293. return -1;
  1294. }
  1295. if(tcp_chk!=0)
  1296. {
  1297. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  1298. mylog(log_debug,"tcp header error\n");
  1299. return -1;
  1300. }
  1301. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
  1302. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1303. info.has_ts=0;
  1304. if(tcph->doff==10)
  1305. {
  1306. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1307. {
  1308. info.has_ts=1;
  1309. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  1310. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  1311. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1312. }
  1313. }
  1314. else if(tcph->doff==8)
  1315. {
  1316. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  1317. {
  1318. info.has_ts=1;
  1319. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  1320. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  1321. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1322. }
  1323. }
  1324. if(tcph->rst==1)
  1325. {
  1326. mylog(log_warn,"%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  1327. }
  1328. info.ack=tcph->ack;
  1329. info.syn=tcph->syn;
  1330. info.rst=tcph->rst;
  1331. info.src_port=ntohs(tcph->source);
  1332. info.src_ip=iph->saddr;
  1333. info.seq=ntohl(tcph->seq);
  1334. info.ack_seq=ntohl(tcph->ack_seq);
  1335. info.psh=tcph->psh;
  1336. if(info.has_ts)
  1337. {
  1338. g_packet_info_send.ts_ack=info.ts;
  1339. }
  1340. ////tcp end
  1341. payloadlen = ip_len-tcphdrlen-iphdrlen;
  1342. payload=ip_begin+tcphdrlen+iphdrlen;
  1343. if(payloadlen>0&&payload[0]=='h')
  1344. {
  1345. mylog(log_debug,"recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
  1346. }
  1347. if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
  1348. {
  1349. //if(seq_increse)
  1350. g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
  1351. }
  1352. //printf("%d\n",ip_len);
  1353. mylog(log_trace,"<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
  1354. return 0;
  1355. }*/
  1356. int send_raw0(raw_info_t &raw_info,const char * payload,int payloadlen)
  1357. {
  1358. if (random_drop != 0) {
  1359. if (get_true_random_number() % 10000 < (u32_t) random_drop) {
  1360. return 0;
  1361. }
  1362. }
  1363. packet_info_t &send_info=raw_info.send_info;
  1364. packet_info_t &recv_info=raw_info.recv_info;
  1365. 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);
  1366. switch(raw_mode)
  1367. {
  1368. case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
  1369. case mode_udp: return send_raw_udp(raw_info,payload,payloadlen);
  1370. case mode_icmp: return send_raw_icmp(raw_info,payload,payloadlen);
  1371. default:return -1;
  1372. }
  1373. }
  1374. int recv_raw0(raw_info_t &raw_info,char * &payload,int &payloadlen)
  1375. {
  1376. packet_info_t &send_info=raw_info.send_info;
  1377. packet_info_t &recv_info=raw_info.recv_info;
  1378. switch(raw_mode)
  1379. {
  1380. case mode_faketcp:return recv_raw_tcp(raw_info,payload,payloadlen);
  1381. case mode_udp: return recv_raw_udp(raw_info,payload,payloadlen);
  1382. case mode_icmp: return recv_raw_icmp(raw_info,payload,payloadlen);
  1383. default: return -1;
  1384. }
  1385. }
  1386. int after_send_raw0(raw_info_t &raw_info)
  1387. {
  1388. packet_info_t &send_info=raw_info.send_info;
  1389. packet_info_t &recv_info=raw_info.recv_info;
  1390. if(raw_mode==mode_faketcp)
  1391. {
  1392. if (send_info.syn == 0 && send_info.ack == 1&& raw_info.send_info.data_len != 0) //only modify send_info when the packet is not part of handshake
  1393. {
  1394. if (seq_mode == 0)
  1395. {
  1396. } else if (seq_mode == 1)
  1397. {
  1398. send_info.seq += raw_info.send_info.data_len; //////////////////modify
  1399. } else if (seq_mode == 2)
  1400. {
  1401. if (random() % 5 == 3)
  1402. send_info.seq += raw_info.send_info.data_len; //////////////////modify
  1403. }
  1404. else if(seq_mode==3||seq_mode==4)
  1405. {
  1406. send_info.seq += raw_info.send_info.data_len;
  1407. u32_t window_size;
  1408. if(seq_mode==3)
  1409. {
  1410. window_size=(u32_t)((u32_t)receive_window_lower_bound<<(u32_t)wscale);
  1411. }
  1412. else//seq_mode==4
  1413. {
  1414. window_size=(u32_t)((u32_t)receive_window_lower_bound);
  1415. }
  1416. if(larger_than_u32(send_info.seq+max_data_len,recv_info.ack_seq+window_size))
  1417. {
  1418. send_info.seq=raw_info.recv_info.ack_seq;
  1419. }
  1420. if(recv_info.ack_seq_counter>=3) //simulate tcp fast re-transmit
  1421. {
  1422. recv_info.ack_seq_counter=0;
  1423. send_info.seq=raw_info.recv_info.ack_seq;
  1424. }
  1425. if(larger_than_u32(raw_info.recv_info.ack_seq,send_info.seq)) //for further use,currently no effect.
  1426. {
  1427. send_info.seq=raw_info.recv_info.ack_seq;
  1428. }
  1429. }
  1430. }
  1431. }
  1432. if(raw_mode==mode_icmp)
  1433. {
  1434. if(program_mode==client_mode)
  1435. {
  1436. send_info.icmp_seq++;
  1437. }
  1438. }
  1439. return 0;
  1440. }
  1441. int after_recv_raw0(raw_info_t &raw_info)
  1442. {
  1443. packet_info_t &send_info=raw_info.send_info;
  1444. packet_info_t &recv_info=raw_info.recv_info;
  1445. if(raw_mode==mode_faketcp)
  1446. {
  1447. if(recv_info.has_ts)
  1448. send_info.ts_ack=recv_info.ts;
  1449. if (recv_info.syn == 0 && recv_info.ack == 1 && raw_info.recv_info.data_len != 0) //only modify send_info when the packet is not part of handshake
  1450. {
  1451. if(seq_mode==0||seq_mode==1||seq_mode==2)
  1452. {
  1453. if(larger_than_u32(recv_info.seq+raw_info.recv_info.data_len,send_info.ack_seq))
  1454. send_info.ack_seq = recv_info.seq+raw_info.recv_info.data_len;//TODO only update if its larger
  1455. }
  1456. else if(seq_mode==3||seq_mode==4)
  1457. {
  1458. if(recv_info.seq==send_info.ack_seq)
  1459. {
  1460. send_info.ack_seq=recv_info.seq+raw_info.recv_info.data_len;//currently we dont remembr tcp segments,this is the simplest way
  1461. //TODO implement tcp segment remembering and SACK.
  1462. }
  1463. }
  1464. }
  1465. }
  1466. if(raw_mode==mode_icmp)
  1467. {
  1468. if(program_mode==server_mode)
  1469. {
  1470. if(larger_than_u16(recv_info.icmp_seq,send_info.icmp_seq))
  1471. send_info.icmp_seq = recv_info.icmp_seq; //TODO only update if its larger
  1472. }
  1473. }
  1474. return 0;
  1475. }
  1476. /*
  1477. int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen)
  1478. {
  1479. packet_info_t &send_info=raw_info.send_info;
  1480. packet_info_t &recv_info=raw_info.recv_info;
  1481. int ret=send_raw0(raw_info,payload,payloadlen);
  1482. if(ret<0) return ret;
  1483. else
  1484. {
  1485. after_send_raw0(raw_info);
  1486. return ret;
  1487. }
  1488. }
  1489. int recv_raw(raw_info_t &raw_info,char *& payload,int & payloadlen)
  1490. {
  1491. packet_info_t &send_info=raw_info.send_info;
  1492. packet_info_t &recv_info=raw_info.recv_info;
  1493. int ret=recv_raw0(raw_info,payload,payloadlen);
  1494. if(ret<0) return ret;
  1495. else
  1496. {
  1497. after_recv_raw0(raw_info);
  1498. return ret;
  1499. }
  1500. }*/
  1501. int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port) //a trick to get src adress for a dest adress,so that we can use the src address in raw socket as source ip
  1502. {
  1503. struct sockaddr_in remote_addr_in={0};
  1504. socklen_t slen = sizeof(sockaddr_in);
  1505. //memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  1506. remote_addr_in.sin_family = AF_INET;
  1507. remote_addr_in.sin_port = htons(remote_port);
  1508. remote_addr_in.sin_addr.s_addr = remote_ip_uint32;
  1509. int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  1510. if(new_udp_fd<0)
  1511. {
  1512. mylog(log_warn,"create udp_fd error\n");
  1513. return -1;
  1514. }
  1515. //set_buf_size(new_udp_fd);
  1516. mylog(log_debug,"created new udp_fd %d\n",new_udp_fd);
  1517. int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
  1518. if(ret!=0)
  1519. {
  1520. mylog(log_warn,"udp fd connect fail\n");
  1521. close(new_udp_fd);
  1522. return -1;
  1523. }
  1524. struct sockaddr_in my_addr={0};
  1525. socklen_t len=sizeof(my_addr);
  1526. if(getsockname(new_udp_fd, (struct sockaddr *) &my_addr, &len)!=0) return -1;
  1527. ip=my_addr.sin_addr.s_addr;
  1528. close(new_udp_fd);
  1529. return 0;
  1530. }
  1531. int try_to_list_and_bind(int &fd,u32_t local_ip_uint32,int port) //try to bind to a port,may fail.
  1532. {
  1533. int old_bind_fd=fd;
  1534. if(raw_mode==mode_faketcp)
  1535. {
  1536. fd=socket(AF_INET,SOCK_STREAM,0);
  1537. }
  1538. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  1539. {
  1540. fd=socket(AF_INET,SOCK_DGRAM,0);
  1541. }
  1542. if(old_bind_fd!=-1)
  1543. {
  1544. close(old_bind_fd);
  1545. }
  1546. struct sockaddr_in temp_bind_addr={0};
  1547. //bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  1548. temp_bind_addr.sin_family = AF_INET;
  1549. temp_bind_addr.sin_port = htons(port);
  1550. temp_bind_addr.sin_addr.s_addr = local_ip_uint32;
  1551. if (bind(fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
  1552. {
  1553. mylog(log_debug,"bind fail\n");
  1554. return -1;
  1555. }
  1556. if(raw_mode==mode_faketcp)
  1557. {
  1558. if (listen(fd, SOMAXCONN) != 0) {
  1559. mylog(log_warn,"listen fail\n");
  1560. return -1;
  1561. }
  1562. }
  1563. return 0;
  1564. }
  1565. int client_bind_to_a_new_port(int &fd,u32_t local_ip_uint32)//find a free port and bind to it.
  1566. {
  1567. int raw_send_port=10000+get_true_random_number()%(65535-10000);
  1568. for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
  1569. {
  1570. if (try_to_list_and_bind(fd,local_ip_uint32,raw_send_port)==0)
  1571. {
  1572. return raw_send_port;
  1573. }
  1574. }
  1575. mylog(log_fatal,"bind port fail\n");
  1576. myexit(-1);
  1577. return -1;////for compiler check
  1578. }