network.cpp 50 KB

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