network.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  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 g_fix_gro=1;
  12. int raw_recv_fd=-1;
  13. int raw_send_fd=-1;
  14. 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));
  15. int use_tcp_dummy_socket=0;
  16. int seq_mode=3;
  17. int max_seq_mode=4;
  18. int random_drop=0;
  19. int filter_port=-1;
  20. int disable_bpf_filter=0; //for test only,most time no need to disable this
  21. //u32_t bind_address_uint32=0;
  22. int lower_level=0;
  23. int lower_level_manual=0;
  24. int ifindex=-1;
  25. char if_name[100]="";
  26. char dev[100]="";
  27. unsigned short g_ip_id_counter=0;
  28. unsigned char dest_hw_addr[sizeof(sockaddr_ll::sll_addr)]=
  29. {0xff,0xff,0xff,0xff,0xff,0xff,0,0};
  30. //{0x00,0x23,0x45,0x67,0x89,0xb9};
  31. const u32_t receive_window_lower_bound=40960;
  32. const u32_t receive_window_random_range=512;
  33. const unsigned char wscale=0x05;
  34. char g_packet_buf[buf_len]; //looks dirty but works well
  35. int g_packet_buf_len=-1;
  36. int g_packet_buf_cnt=0;
  37. union
  38. {
  39. sockaddr_ll ll;
  40. sockaddr_in ipv4;
  41. sockaddr_in6 ipv6;
  42. }g_sockaddr;
  43. socklen_t g_sockaddr_len = -1;
  44. struct sock_filter code_tcp_old[] = {
  45. { 0x28, 0, 0, 0x0000000c },//0
  46. { 0x15, 0, 10, 0x00000800 },//1
  47. { 0x30, 0, 0, 0x00000017 },//2
  48. { 0x15, 0, 8, 0x00000006 },//3
  49. { 0x28, 0, 0, 0x00000014 },//4
  50. { 0x45, 6, 0, 0x00001fff },//5
  51. { 0xb1, 0, 0, 0x0000000e },//6
  52. { 0x48, 0, 0, 0x0000000e },//7
  53. { 0x15, 2, 0, 0x0000ef32 },//8
  54. { 0x48, 0, 0, 0x00000010 },//9
  55. { 0x15, 0, 1, 0x0000ef32 },//10
  56. { 0x6, 0, 0, 0x0000ffff },//11
  57. { 0x6, 0, 0, 0x00000000 },//12
  58. };
  59. struct sock_filter code_tcp[] = {
  60. //{ 0x5, 0, 0, 0x00000001 },//0 //jump to 2,dirty hack from tcpdump -d's output
  61. //{ 0x5, 0, 0, 0x00000000 },//1
  62. { 0x30, 0, 0, 0x00000009 },//2
  63. { 0x15, 0, 6, 0x00000006 },//3
  64. { 0x28, 0, 0, 0x00000006 },//4
  65. { 0x45, 4, 0, 0x00001fff },//5
  66. { 0xb1, 0, 0, 0x00000000 },//6
  67. { 0x48, 0, 0, 0x00000002 },//7
  68. { 0x15, 0, 1, 0x0000fffe },//8 //modify this fffe to the port you listen on
  69. { 0x6, 0, 0, 0x0000ffff },//9
  70. { 0x6, 0, 0, 0x00000000 },//10
  71. };
  72. /*
  73. { 0x28, 0, 0, 0x0000000c },
  74. { 0x15, 0, 8, 0x00000800 },
  75. { 0x30, 0, 0, 0x00000017 },
  76. { 0x15, 0, 6, 0x00000006 },
  77. { 0x28, 0, 0, 0x00000014 },
  78. { 0x45, 4, 0, 0x00001fff },
  79. { 0xb1, 0, 0, 0x0000000e },
  80. { 0x48, 0, 0, 0x00000010 },
  81. { 0x15, 0, 1, 0x0000fffe },
  82. { 0x6, 0, 0, 0x0000ffff },
  83. { 0x6, 0, 0, 0x00000000 },
  84. */
  85. int code_tcp_port_index=6;
  86. //tcpdump -i ens33 ip6 and tcp and dst port 65534 -dd
  87. struct sock_filter code_tcp6[] = {
  88. //{ 0x28, 0, 0, 0x0000000c },//0
  89. //{ 0x15, 0, 5, 0x000086dd },//1
  90. { 0x30, 0, 0, 0x00000006 },//2
  91. { 0x15, 0, 3, 0x00000006 },//3
  92. { 0x28, 0, 0, 0x0000002a },//4
  93. { 0x15, 0, 1, 0x0000fffe },//5
  94. { 0x6, 0, 0, 0x00040000 },//6
  95. { 0x6, 0, 0, 0x00000000 },//7
  96. };//note: this filter doesnt support extension headers
  97. /*
  98. { 0x30, 0, 0, 0x00000014 },//2
  99. { 0x15, 0, 3, 0x00000006 },//3
  100. { 0x28, 0, 0, 0x00000038 },//4
  101. { 0x15, 0, 1, 0x0000fffe },//5
  102. { 0x6, 0, 0, 0x00040000 },//6
  103. { 0x6, 0, 0, 0x00000000 },//7
  104. */
  105. int code_tcp6_port_index=3;
  106. struct sock_filter code_udp[] = {
  107. //{ 0x5, 0, 0, 0x00000001 },
  108. //{ 0x5, 0, 0, 0x00000000 },
  109. { 0x30, 0, 0, 0x00000009 },
  110. { 0x15, 0, 6, 0x00000011 },
  111. { 0x28, 0, 0, 0x00000006 },
  112. { 0x45, 4, 0, 0x00001fff },
  113. { 0xb1, 0, 0, 0x00000000 },
  114. { 0x48, 0, 0, 0x00000002 },
  115. { 0x15, 0, 1, 0x0000fffe }, //modify this fffe to the port you listen on
  116. { 0x6, 0, 0, 0x0000ffff },
  117. { 0x6, 0, 0, 0x00000000 },
  118. };
  119. int code_udp_port_index=6;
  120. struct sock_filter code_udp6[] = {
  121. // { 0x28, 0, 0, 0x0000000c },
  122. // { 0x15, 0, 5, 0x000086dd },
  123. { 0x30, 0, 0, 0x00000006 },
  124. { 0x15, 0, 3, 0x00000011 },
  125. { 0x28, 0, 0, 0x0000002a },
  126. { 0x15, 0, 1, 0x0000fffe },
  127. { 0x6, 0, 0, 0x00040000 },
  128. { 0x6, 0, 0, 0x00000000 },
  129. };
  130. int code_udp6_port_index=3;
  131. struct sock_filter code_icmp[] = {
  132. //{ 0x5, 0, 0, 0x00000001 },
  133. //{ 0x5, 0, 0, 0x00000000 },
  134. { 0x30, 0, 0, 0x00000009 },
  135. { 0x15, 0, 1, 0x00000001 },
  136. { 0x6, 0, 0, 0x0000ffff },
  137. { 0x6, 0, 0, 0x00000000 },
  138. };
  139. struct sock_filter code_icmp6[] = {
  140. // { 0x28, 0, 0, 0x0000000c },
  141. // { 0x15, 0, 6, 0x000086dd },
  142. { 0x30, 0, 0, 0x00000006 },
  143. { 0x15, 3, 0, 0x0000003a },
  144. { 0x15, 0, 3, 0x0000002c },
  145. { 0x30, 0, 0, 0x00000028 },
  146. { 0x15, 0, 1, 0x0000003a },
  147. { 0x6, 0, 0, 0x00040000 },
  148. { 0x6, 0, 0, 0x00000000 },
  149. };
  150. /*
  151. tcpdump -i eth1 ip and icmp -d
  152. (000) ldh [12]
  153. (001) jeq #0x800 jt 2 jf 5
  154. (002) ldb [23]
  155. (003) jeq #0x1 jt 4 jf 5
  156. (004) ret #65535
  157. (005) ret #0
  158. tcpdump -i eth1 ip and icmp -dd
  159. { 0x28, 0, 0, 0x0000000c },
  160. { 0x15, 0, 3, 0x00000800 },
  161. { 0x30, 0, 0, 0x00000017 },
  162. { 0x15, 0, 1, 0x00000001 },
  163. { 0x6, 0, 0, 0x0000ffff },
  164. { 0x6, 0, 0, 0x00000000 },
  165. */
  166. /*
  167. tcpdump -i eth1 ip and tcp and dst port 65534 -dd
  168. { 0x28, 0, 0, 0x0000000c },
  169. { 0x15, 0, 8, 0x00000800 },
  170. { 0x30, 0, 0, 0x00000017 },
  171. { 0x15, 0, 6, 0x00000006 },
  172. { 0x28, 0, 0, 0x00000014 },
  173. { 0x45, 4, 0, 0x00001fff },
  174. { 0xb1, 0, 0, 0x0000000e },
  175. { 0x48, 0, 0, 0x00000010 },
  176. { 0x15, 0, 1, 0x0000fffe },
  177. { 0x6, 0, 0, 0x0000ffff },
  178. { 0x6, 0, 0, 0x00000000 },
  179. (000) ldh [12]
  180. (001) jeq #0x800 jt 2 jf 10
  181. (002) ldb [23]
  182. (003) jeq #0x6 jt 4 jf 10
  183. (004) ldh [20]
  184. (005) jset #0x1fff jt 10 jf 6
  185. (006) ldxb 4*([14]&0xf)
  186. (007) ldh [x + 16]
  187. (008) jeq #0xfffe jt 9 jf 10
  188. (009) ret #65535
  189. (010) ret #0
  190. */
  191. packet_info_t::packet_info_t()
  192. {
  193. src_port=0;
  194. dst_port=0;
  195. if (raw_mode == mode_faketcp)
  196. {
  197. protocol = IPPROTO_TCP;
  198. ack_seq = get_true_random_number();
  199. seq = get_true_random_number();
  200. has_ts=0;
  201. ts_ack=0;
  202. syn=0;
  203. ack=1;
  204. ack_seq_counter=0;
  205. //mylog(log_info,"<cons ,ts_ack= %u>\n",ts_ack);
  206. }
  207. else if (raw_mode == mode_udp)
  208. {
  209. protocol = IPPROTO_UDP;
  210. }
  211. else if (raw_mode == mode_icmp)
  212. {
  213. if(raw_ip_version==AF_INET)
  214. {
  215. protocol = IPPROTO_ICMP;
  216. }
  217. else
  218. {
  219. assert(raw_ip_version==AF_INET6);
  220. protocol = IPPROTO_ICMPV6;
  221. }
  222. my_icmp_seq=0;
  223. }
  224. }
  225. int init_raw_socket()
  226. {
  227. assert(raw_ip_version==AF_INET||raw_ip_version==AF_INET6);
  228. g_ip_id_counter=get_true_random_number()%65535;
  229. if(lower_level==0)
  230. {
  231. raw_send_fd = socket(raw_ip_version , SOCK_RAW , IPPROTO_RAW);// IPPROTO_TCP??
  232. if(raw_send_fd == -1) {
  233. mylog(log_fatal,"Failed to create raw_send_fd\n");
  234. //perror("Failed to create raw_send_fd");
  235. myexit(1);
  236. }
  237. /*ETH_P_IP
  238. int one = 1;
  239. const int *val = &one;
  240. if (setsockopt (raw_send_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0) {
  241. mylog(log_fatal,"Error setting IP_HDRINCL %d\n",errno);
  242. //perror("Error setting IP_HDRINCL");
  243. myexit(2);
  244. }*/
  245. }
  246. else
  247. {
  248. raw_send_fd = socket(PF_PACKET , SOCK_DGRAM , htons(ETH_P_IP));// todo how to create a recv only raw socket?
  249. if(raw_send_fd == -1) {
  250. mylog(log_fatal,"Failed to create raw_send_fd\n");
  251. //perror("Failed to create raw_send_fd");
  252. myexit(1);
  253. }
  254. //init_ifindex(if_name);
  255. }
  256. int opt = 0;
  257. assert(setsockopt(raw_send_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt))==0);// raw_send_fd is for send only, set its recv buffer to zero
  258. if(force_socket_buf)
  259. {
  260. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  261. {
  262. mylog(log_fatal,"SO_SNDBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  263. myexit(1);
  264. }
  265. }
  266. else
  267. {
  268. if(setsockopt(raw_send_fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
  269. {
  270. mylog(log_fatal,"SO_SNDBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  271. myexit(1);
  272. }
  273. }
  274. //raw_fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
  275. if(raw_ip_version==AF_INET)
  276. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  277. else
  278. raw_recv_fd= socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
  279. //ETH_P_IP doesnt read outgoing packets
  280. // https://stackoverflow.com/questions/20264895/eth-p-ip-is-not-working-as-expected-i-can-only-receive-incoming-packets
  281. // to capture both incoming and outgoing packets use ETH_P_ALL
  282. if(raw_recv_fd == -1) {
  283. mylog(log_fatal,"Failed to create raw_recv_fd\n");
  284. //perror("");
  285. myexit(1);
  286. }
  287. if(strlen(dev)!=0)
  288. {
  289. struct sockaddr_ll bind_address;
  290. memset(&bind_address, 0, sizeof(bind_address));
  291. int index=-1;
  292. assert(init_ifindex(dev,raw_recv_fd,index)==0);
  293. bind_address.sll_family = AF_PACKET;
  294. if(raw_ip_version==AF_INET)
  295. bind_address.sll_protocol = htons(ETH_P_IP);
  296. else
  297. bind_address.sll_protocol = htons(ETH_P_IPV6);
  298. bind_address.sll_ifindex = index;
  299. if(bind(raw_recv_fd, (struct sockaddr *)&bind_address, sizeof(bind_address))==-1)
  300. {
  301. mylog(log_fatal,"bind to dev [%s] failed\n",dev);
  302. myexit(1);
  303. }
  304. }
  305. if(force_socket_buf)
  306. {
  307. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
  308. {
  309. mylog(log_fatal,"SO_RCVBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  310. myexit(1);
  311. }
  312. }
  313. else
  314. {
  315. if(setsockopt(raw_recv_fd, SOL_SOCKET, SO_RCVBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
  316. {
  317. mylog(log_fatal,"SO_RCVBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
  318. myexit(1);
  319. }
  320. }
  321. //IP_HDRINCL to tell the kernel that headers are included in the packet
  322. setnonblocking(raw_send_fd); //not really necessary
  323. setnonblocking(raw_recv_fd);
  324. return 0;
  325. }
  326. void init_filter(int port)
  327. {
  328. sock_fprog bpf;
  329. assert(raw_ip_version==AF_INET||raw_ip_version==AF_INET6);
  330. if(raw_mode==mode_faketcp||raw_mode==mode_udp)
  331. {
  332. filter_port=port;
  333. }
  334. if(disable_bpf_filter) return;
  335. //if(raw_mode==mode_icmp) return ;
  336. //code_tcp[8].k=code_tcp[10].k=port;
  337. if(raw_mode==mode_faketcp)
  338. {
  339. if(raw_ip_version==AF_INET)
  340. {
  341. bpf.len = sizeof(code_tcp)/sizeof(code_tcp[0]);
  342. code_tcp[code_tcp_port_index].k=port;
  343. bpf.filter = code_tcp;
  344. }else
  345. {
  346. bpf.len = sizeof(code_tcp6)/sizeof(code_tcp6[0]);
  347. code_tcp6[code_tcp6_port_index].k=port;
  348. bpf.filter = code_tcp6;
  349. }
  350. }
  351. else if(raw_mode==mode_udp)
  352. {
  353. if(raw_ip_version==AF_INET)
  354. {
  355. bpf.len = sizeof(code_udp)/sizeof(code_udp[0]);
  356. code_udp[code_udp_port_index].k=port;
  357. bpf.filter = code_udp;
  358. }else
  359. {
  360. bpf.len = sizeof(code_udp6)/sizeof(code_udp6[0]);
  361. code_udp6[code_udp6_port_index].k=port;
  362. bpf.filter = code_udp6;
  363. }
  364. }
  365. else if(raw_mode==mode_icmp)
  366. {
  367. if(raw_ip_version==AF_INET)
  368. {
  369. bpf.len = sizeof(code_icmp)/sizeof(code_icmp[0]);
  370. bpf.filter = code_icmp;
  371. }else
  372. {
  373. bpf.len = sizeof(code_icmp6)/sizeof(code_icmp6[0]);
  374. bpf.filter = code_icmp6;
  375. }
  376. }
  377. int dummy;
  378. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy)); //in case i forgot to remove
  379. if (ret != 0)
  380. {
  381. mylog(log_debug,"error remove fiter\n");
  382. //perror("filter");
  383. //exit(-1);
  384. }
  385. ret = setsockopt(raw_recv_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
  386. if (ret != 0)
  387. {
  388. mylog(log_fatal,"error set fiter\n");
  389. //perror("filter");
  390. myexit(-1);
  391. }
  392. }
  393. void remove_filter()
  394. {
  395. filter_port=0;
  396. int dummy;
  397. int ret=setsockopt(raw_recv_fd, SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy));
  398. if (ret != 0)
  399. {
  400. mylog(log_debug,"error remove fiter\n");
  401. //perror("filter");
  402. //exit(-1);
  403. }
  404. }
  405. int init_ifindex(const char * if_name,int fd,int &index)
  406. {
  407. struct ifreq ifr;
  408. size_t if_name_len=strlen(if_name);
  409. if (if_name_len<sizeof(ifr.ifr_name)) {
  410. memcpy(ifr.ifr_name,if_name,if_name_len);
  411. ifr.ifr_name[if_name_len]=0;
  412. } else {
  413. mylog(log_fatal,"interface name is too long\n");
  414. myexit(-1);
  415. }
  416. if (ioctl(fd,SIOCGIFINDEX,&ifr)==-1) {
  417. mylog(log_fatal,"SIOCGIFINDEX fail ,%s\n",strerror(errno));
  418. myexit(-1);
  419. }
  420. index=ifr.ifr_ifindex;
  421. mylog(log_info,"ifname:%s ifindex:%d\n",if_name,index);
  422. return 0;
  423. }
  424. bool interface_has_arp(const char * interface) {
  425. struct ifreq ifr;
  426. // int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
  427. int sock=raw_send_fd;
  428. memset(&ifr, 0, sizeof(ifr));
  429. strcpy(ifr.ifr_name, interface);
  430. if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
  431. //perror("SIOCGIFFLAGS");
  432. mylog(log_fatal,"ioctl(sock, SIOCGIFFLAGS, &ifr) failed for interface %s,errno %s\n",interface,strerror(errno));
  433. myexit(-1);
  434. }
  435. //close(sock);
  436. return !(ifr.ifr_flags & IFF_NOARP);
  437. }
  438. struct route_info_t
  439. {
  440. string if_name;
  441. u32_t dest;
  442. u32_t mask;
  443. u32_t gw;
  444. u32_t flag;
  445. };
  446. int dest_idx=1;
  447. int gw_idx=2;
  448. int if_idx=0;
  449. int mask_idx=7;
  450. int flag_idx=3;
  451. vector<int> find_route_entry(const vector<route_info_t> &route_info_vec,u32_t ip)
  452. {
  453. vector<int> res;
  454. for(u32_t i=0;i<=32;i++)
  455. {
  456. u32_t mask=0xffffffff;
  457. //mask >>=i;
  458. //if(i==32) mask=0; //why 0xffffffff>>32 equals 0xffffffff??
  459. mask <<=i;
  460. if(i==32) mask=0;
  461. log_bare(log_debug,"(mask:%x)",mask);
  462. for(u32_t j=0;j<route_info_vec.size();j++)
  463. {
  464. const route_info_t & info=route_info_vec[j];
  465. if(info.mask!=mask)
  466. continue;
  467. log_bare(log_debug,"<<%d,%d>>",i,j);
  468. if((info.dest&mask)==(ip&mask))
  469. {
  470. log_bare(log_debug,"found!");
  471. res.push_back(j);
  472. }
  473. }
  474. if(res.size()!=0)
  475. {
  476. return res;
  477. }
  478. }
  479. return res;
  480. }
  481. int find_direct_dest(const vector<route_info_t> &route_info_vec,u32_t ip,u32_t &dest_ip,string &if_name)
  482. {
  483. vector<int> res;
  484. for(int i=0;i<1000;i++)
  485. {
  486. res=find_route_entry(route_info_vec,ip);
  487. log_bare(log_debug,"<entry:%u>",(u32_t)res.size());
  488. if(res.size()==0)
  489. {
  490. mylog(log_error,"cant find route entry\n");
  491. return -1;
  492. }
  493. if(res.size()>1)
  494. {
  495. mylog(log_error,"found duplicated entries\n");
  496. return -1;
  497. }
  498. if((route_info_vec[res[0]].flag&2)==0)
  499. {
  500. dest_ip=ip;
  501. if_name=route_info_vec[res[0]].if_name;
  502. return 0;
  503. }
  504. else
  505. {
  506. ip=route_info_vec[res[0]].gw;
  507. }
  508. }
  509. mylog(log_error,"dead loop in find_direct_dest\n");
  510. return -1;
  511. }
  512. struct arp_info_t
  513. {
  514. u32_t ip;
  515. string hw;
  516. string if_name;
  517. };
  518. int arp_ip_idx=0;
  519. int arp_hw_idx=3;
  520. int arp_if_idx=5;
  521. int find_arp(const vector<arp_info_t> &arp_info_vec,u32_t ip,string if_name,string &hw)
  522. {
  523. int pos=-1;
  524. int count=0;
  525. for(u32_t i=0;i<arp_info_vec.size();i++)
  526. {
  527. const arp_info_t & info=arp_info_vec[i];
  528. if(info.if_name!=if_name) continue;
  529. if(info.ip==ip)
  530. {
  531. count++;
  532. pos=i;
  533. }
  534. }
  535. if(count==0)
  536. {
  537. //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());
  538. //hw="00:00:00:00:00:00";
  539. mylog(log_error,"cant find arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
  540. return -1;
  541. }
  542. if(count>1)
  543. {
  544. mylog(log_error,"find multiple arp entry for %s %s\n",my_ntoa(ip),if_name.c_str());
  545. return -1;
  546. }
  547. hw=arp_info_vec[pos].hw;
  548. return 0;
  549. }
  550. int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw)
  551. {
  552. ip=htonl(ip);
  553. if(ip==htonl(inet_addr("127.0.0.1")))
  554. {
  555. dest_ip=ntohl(ip);
  556. if_name="lo";
  557. hw="00:00:00:00:00:00";
  558. return 0;
  559. }
  560. string route_file;
  561. if(read_file("/proc/net/route",route_file)!=0) return -1;
  562. string arp_file;
  563. if(read_file("/proc/net/arp",arp_file)!=0) return -1;
  564. log_bare(log_debug,"/proc/net/route:<<%s>>\n",route_file.c_str());
  565. log_bare(log_debug,"/proc/net/arp:<<%s>>\n",route_file.c_str());
  566. auto route_vec2=string_to_vec2(route_file.c_str());
  567. vector<route_info_t> route_info_vec;
  568. for(u32_t i=1;i<route_vec2.size();i++)
  569. {
  570. log_bare(log_debug,"<size:%u>",(u32_t)route_vec2[i].size());
  571. if(route_vec2[i].size()!=11)
  572. {
  573. mylog(log_error,"route coloum %d !=11 \n",int(route_vec2[i].size()));
  574. return -1;
  575. }
  576. route_info_t tmp;
  577. tmp.if_name=route_vec2[i][if_idx];
  578. if(hex_to_u32_with_endian(route_vec2[i][dest_idx],tmp.dest)!=0) return -1;
  579. if(hex_to_u32_with_endian(route_vec2[i][gw_idx],tmp.gw)!=0) return -1;
  580. if(hex_to_u32_with_endian(route_vec2[i][mask_idx],tmp.mask)!=0) return -1;
  581. if(hex_to_u32(route_vec2[i][flag_idx],tmp.flag)!=0)return -1;
  582. route_info_vec.push_back(tmp);
  583. for(u32_t j=0;j<route_vec2[i].size();j++)
  584. {
  585. log_bare(log_debug,"<%s>",route_vec2[i][j].c_str());
  586. }
  587. 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);
  588. log_bare(log_debug,"\n");
  589. }
  590. if(find_direct_dest(route_info_vec,ip,dest_ip,if_name)!=0)
  591. {
  592. mylog(log_error,"find_direct_dest failed for ip %s\n",my_ntoa(ntohl(ip)));
  593. return -1;
  594. }
  595. log_bare(log_debug,"========\n");
  596. auto arp_vec2=string_to_vec2(arp_file.c_str());
  597. vector<arp_info_t> arp_info_vec;
  598. for(u32_t i=1;i<arp_vec2.size();i++)
  599. {
  600. log_bare(log_debug,"<<arp_vec2[i].size(): %d>>",(int)arp_vec2[i].size());
  601. for(u32_t j=0;j<arp_vec2[i].size();j++)
  602. {
  603. log_bare(log_debug,"<%s>",arp_vec2[i][j].c_str());
  604. }
  605. if(arp_vec2[i].size()!=6)
  606. {
  607. mylog(log_error,"arp coloum %d !=11 \n",int(arp_vec2[i].size()));
  608. return -1;
  609. }
  610. arp_info_t tmp;
  611. tmp.if_name=arp_vec2[i][arp_if_idx];
  612. tmp.hw=arp_vec2[i][arp_hw_idx];
  613. tmp.ip=htonl(inet_addr(arp_vec2[i][arp_ip_idx].c_str()));
  614. arp_info_vec.push_back(tmp);
  615. log_bare(log_debug,"\n");
  616. }
  617. if(!interface_has_arp(if_name.c_str()))
  618. {
  619. mylog(log_info,"%s is a noarp interface,using 00:00:00:00:00:00\n",if_name.c_str());
  620. hw="00:00:00:00:00:00";
  621. }
  622. else if(find_arp(arp_info_vec,dest_ip,if_name,hw)!=0)
  623. {
  624. mylog(log_error,"find_arp failed for dest_ip %s ,if_name %s\n",my_ntoa(ntohl(ip)),if_name.c_str());
  625. return -1;
  626. }
  627. //printf("%s\n",hw.c_str());
  628. dest_ip=ntohl(dest_ip);
  629. return 0;
  630. }
  631. int send_raw_packet(raw_info_t &raw_info,const char * packet,int len)
  632. {
  633. const packet_info_t &send_info=raw_info.send_info;
  634. const packet_info_t &recv_info=raw_info.recv_info;
  635. int ret;
  636. if(lower_level==0)
  637. {
  638. if(raw_ip_version==AF_INET)
  639. {
  640. struct sockaddr_in sin={0};
  641. sin.sin_family = raw_ip_version;
  642. //sin.sin_port = htons(info.dst_port); //dont need this
  643. sin.sin_addr.s_addr = send_info.new_dst_ip.v4;
  644. ret = sendto(raw_send_fd, packet, len , 0, (struct sockaddr *) &sin, sizeof (sin));
  645. }
  646. else if(raw_ip_version==AF_INET6)
  647. {
  648. struct sockaddr_in6 sin={0};
  649. sin.sin6_family = raw_ip_version;
  650. //sin.sin_port = htons(info.dst_port); //dont need this
  651. sin.sin6_addr = send_info.new_dst_ip.v6;
  652. ret = sendto(raw_send_fd, packet, len , 0, (struct sockaddr *) &sin, sizeof (sin));
  653. }else
  654. {
  655. assert(0==1);
  656. }
  657. }
  658. else
  659. {
  660. struct sockaddr_ll addr={0}; //={0} not necessary
  661. memcpy(&addr,&send_info.addr_ll,sizeof(addr));
  662. ret = sendto(raw_send_fd, packet, len , 0, (struct sockaddr *) &addr, sizeof (addr));
  663. }
  664. if(ret==-1)
  665. {
  666. mylog(log_trace,"sendto failed\n");
  667. //perror("why?");
  668. return -1;
  669. }
  670. else
  671. {
  672. //mylog(log_info,"sendto succ\n");
  673. }
  674. return 0;
  675. }
  676. int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
  677. {
  678. const packet_info_t &send_info=raw_info.send_info;
  679. const packet_info_t &recv_info=raw_info.recv_info;
  680. char send_raw_ip_buf[buf_len];
  681. if(raw_info.disabled)
  682. {
  683. mylog(log_debug,"[%s,%d]connection disabled, no packet will be sent\n",recv_info.new_src_ip.get_str1(),recv_info.src_port);
  684. assert(max_rst_allowed>=0);
  685. return 0;
  686. }
  687. uint16_t ip_tot_len;
  688. if(raw_ip_version==AF_INET)
  689. {
  690. struct my_iphdr *iph = (struct my_iphdr *) send_raw_ip_buf;
  691. memset(iph,0,sizeof(my_iphdr));
  692. iph->ihl = sizeof(my_iphdr)/4; //we dont use ip options,so the length is just sizeof(iphdr)
  693. iph->version = 4;
  694. iph->tos = 0;
  695. if(lower_level)
  696. {
  697. //iph->id=0;
  698. iph->id = htons (g_ip_id_counter++); //Id of this packet
  699. }
  700. else//no need to else?
  701. {
  702. iph->id = htons (g_ip_id_counter++); //Id of this packet
  703. //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
  704. }
  705. iph->frag_off = htons(0x4000); //DF set,others are zero
  706. // iph->frag_off = htons(0x0000); //DF set,others are zero
  707. iph->ttl = (unsigned char)ttl_value;
  708. iph->protocol = send_info.protocol;
  709. iph->check = 0; //Set to 0 before calculating checksum
  710. iph->saddr = send_info.new_src_ip.v4; //Spoof the source ip address
  711. iph->daddr = send_info.new_dst_ip.v4;
  712. ip_tot_len=sizeof (struct my_iphdr)+payloadlen;
  713. 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
  714. else
  715. iph->tot_len = 0;
  716. memcpy(send_raw_ip_buf+sizeof(my_iphdr) , payload, payloadlen);
  717. if(lower_level) iph->check =
  718. csum ((unsigned short *) send_raw_ip_buf, iph->ihl*4); //this is not necessary ,kernel will always auto fill this
  719. else
  720. iph->check=0;
  721. }
  722. else
  723. {
  724. assert(raw_ip_version==AF_INET6);
  725. struct my_ip6hdr *ip6h = (struct my_ip6hdr *) send_raw_ip_buf;
  726. memset(ip6h,0,sizeof(my_ip6hdr));
  727. ip6h->version=6;
  728. ip6h->payload_len=htons(payloadlen);
  729. ip6h->next_header=send_info.protocol;
  730. ip6h->hop_limit=(unsigned char)ttl_value;
  731. ip6h->src=send_info.new_src_ip.v6;
  732. ip6h->dst=send_info.new_dst_ip.v6;
  733. ip_tot_len=sizeof (struct my_ip6hdr)+payloadlen;
  734. memcpy(send_raw_ip_buf+sizeof(my_ip6hdr) , payload, payloadlen);
  735. }
  736. return send_raw_packet(raw_info,send_raw_ip_buf,ip_tot_len);
  737. }
  738. int pre_recv_raw_packet()
  739. {
  740. assert(g_packet_buf_cnt==0);
  741. g_sockaddr_len=sizeof(g_sockaddr.ll);
  742. g_packet_buf_len = recvfrom(raw_recv_fd, g_packet_buf, max_data_len+1, 0 ,(sockaddr*)&g_sockaddr , &g_sockaddr_len);
  743. //assert(g_sockaddr_len==sizeof(g_sockaddr.ll)); //g_sockaddr_len=18, sizeof(g_sockaddr.ll)=20, why its not equal? maybe its bc sll_halen is 6?
  744. //assert(g_addr_ll_size==sizeof(g_addr_ll));
  745. if(g_packet_buf_len==max_data_len+1)
  746. {
  747. mylog(log_warn,"huge packet, data_len %d > %d(max_data_len),dropped\n",g_packet_buf_len,max_data_len);
  748. return -1;
  749. }
  750. if(g_packet_buf_len> single_max_data_len+1)
  751. {
  752. if(g_fix_gro==0)
  753. {
  754. mylog(log_warn, "huge packet, data_len %d > %d(single_max_data_len) dropped\n", g_packet_buf_len,
  755. single_max_data_len);
  756. return -1;
  757. }
  758. else
  759. {
  760. mylog(log_debug, "huge packet, data_len %d > %d(single_max_data_len) dropped\n", g_packet_buf_len,
  761. single_max_data_len);
  762. return -1;
  763. }
  764. }
  765. if(g_packet_buf_len<0)
  766. {
  767. mylog(log_trace,"recv_len %d\n",g_packet_buf_len);
  768. return -1;
  769. }
  770. g_packet_buf_cnt++;
  771. return 0;
  772. }
  773. int discard_raw_packet()
  774. {
  775. assert(g_packet_buf_cnt==1);
  776. g_packet_buf_cnt--;
  777. return 0;
  778. }
  779. int recv_raw_packet(char * &packet,int &len,int peek)
  780. {
  781. assert(g_packet_buf_cnt==1);
  782. if(!peek)
  783. g_packet_buf_cnt--;
  784. if(g_packet_buf_len<int(link_level_header_len))
  785. {
  786. mylog(log_trace,"packet len %d shorter than link_level_header_len %d\n",g_packet_buf_len,int(link_level_header_len));
  787. return -1;
  788. }
  789. if(link_level_header_len ==14)
  790. {
  791. unsigned char a=g_packet_buf[12];
  792. unsigned char b=g_packet_buf[13];
  793. if(! ((a==0x08&&b==0x00) ||(a==0x86&&b==0xdd)) )
  794. {
  795. mylog(log_trace,"not an ipv4 or ipv6 packet!\n");
  796. return -1;
  797. }
  798. }
  799. packet=g_packet_buf+int(link_level_header_len);
  800. len=g_packet_buf_len-int(link_level_header_len);
  801. return 0;
  802. }
  803. int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
  804. {
  805. char *raw_packet_buf;
  806. //static char recv_raw_ip_buf[buf_len];
  807. int raw_packet_len;
  808. if(recv_raw_packet(raw_packet_buf,raw_packet_len,raw_info.peek)!=0) return -1;
  809. //const packet_info_t &send_info=raw_info.send_info;
  810. packet_info_t &recv_info=raw_info.recv_info;
  811. my_iphdr * iph;
  812. my_ip6hdr * ip6h;
  813. int flag=0;
  814. //int recv_len = recvfrom(raw_recv_fd, recv_raw_ip_buf, max_data_len+1, flag ,(sockaddr*)&saddr , &saddr_size);
  815. char *ip_begin=raw_packet_buf; //14 is eth net header
  816. if(raw_packet_len<1)
  817. {
  818. mylog(log_trace,"raw_packet_len <1, dropped\n");
  819. return -1;
  820. }
  821. iph = (struct my_iphdr *) (ip_begin);
  822. ip6h= (struct my_ip6hdr *) (ip_begin);
  823. if(raw_ip_version==AF_INET)
  824. {
  825. if(iph->version!=4)
  826. {
  827. mylog(log_trace,"expect ipv4 packet, but got something else: %02x\n",iph->version);
  828. return -1;
  829. }
  830. if(raw_packet_len<(int)sizeof(my_iphdr))
  831. {
  832. mylog(log_trace,"raw_packet_len<sizeof(iphdr)\n");
  833. return -1;
  834. }
  835. }
  836. else
  837. {
  838. assert(raw_ip_version==AF_INET6);
  839. if(ip6h->version!=6)
  840. {
  841. mylog(log_trace,"expect ipv6 packet, but got something else: %02x\n",ip6h->version);
  842. return -1;
  843. }
  844. if(raw_packet_len<(int)sizeof(my_ip6hdr))
  845. {
  846. mylog(log_trace,"raw_packet_len<sizeof(ip6_hdr)\n");
  847. return -1;
  848. }
  849. }
  850. if(lower_level)
  851. {
  852. memcpy(&recv_info.addr_ll,&g_sockaddr.ll,sizeof(recv_info.addr_ll));
  853. }
  854. unsigned short iphdrlen;
  855. int ip_len;
  856. if(raw_ip_version==AF_INET)
  857. {
  858. recv_info.new_src_ip.v4=iph->saddr;
  859. recv_info.new_dst_ip.v4=iph->daddr;
  860. recv_info.protocol=iph->protocol;
  861. iphdrlen =iph->ihl*4;
  862. ip_len=ntohs(iph->tot_len);
  863. }
  864. else
  865. {
  866. //todo flow id
  867. assert(raw_ip_version==AF_INET6);
  868. recv_info.new_src_ip.v6=ip6h->src;
  869. recv_info.new_dst_ip.v6=ip6h->dst;
  870. iphdrlen=40;
  871. recv_info.protocol=ip6h->next_header; //todo handle extension headers;
  872. ip_len=ntohs(ip6h->payload_len)+iphdrlen;
  873. }
  874. if(bind_addr_used && !recv_info.new_dst_ip.equal(bind_addr))
  875. {
  876. mylog(log_trace,"bind adress doenst match %s %s, dropped\n",recv_info.new_dst_ip.get_str1(), bind_addr.get_str2());
  877. //printf(" bind adress doenst match, dropped\n");
  878. return -1;
  879. }
  880. //if (!(iph->ihl > 0 && iph->ihl <=60)) {
  881. // mylog(log_trace,"iph ihl error\n");
  882. // return -1;
  883. // }
  884. if(raw_packet_len <ip_len)
  885. {
  886. mylog(log_debug,"incomplete packet\n");
  887. return -1;
  888. }
  889. if(raw_ip_version==AF_INET)
  890. {
  891. if(raw_info.peek==0)// avoid cal it twice
  892. {
  893. u32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  894. if(ip_chk!=0)
  895. {
  896. mylog(log_debug,"ip header error %x\n",ip_chk);
  897. return -1;
  898. }
  899. }
  900. }
  901. else
  902. {
  903. //do nothing
  904. }
  905. payload=ip_begin+iphdrlen;
  906. payloadlen=ip_len-iphdrlen;
  907. if(payloadlen<0)
  908. {
  909. mylog(log_warn,"error payload len\n");
  910. return -1;
  911. }
  912. return 0;
  913. }
  914. int peek_raw(raw_info_t &raw_info)
  915. {
  916. //static char peek_raw_buf[buf_len];
  917. //assert(g_packet_buf_cnt==1);
  918. //g_packet_buf_cnt--;
  919. //char * peek_raw_buf=g_packet_buf;
  920. //int recv_len=g_packet_buf_len;
  921. //char *ip_begin=peek_raw_buf+link_level_header_len;
  922. //struct sockaddr saddr={0};
  923. //socklen_t saddr_size=sizeof(saddr);
  924. //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
  925. //iphdr * iph = (struct iphdr *) (ip_begin);
  926. //mylog(log_info,"recv_len %d\n",recv_len);
  927. //if(recv_len<int(sizeof(iphdr)))
  928. //{
  929. // mylog(log_trace,"failed here %d %d\n",recv_len,int(sizeof(iphdr)));
  930. // mylog(log_trace,"%s\n ",strerror(errno));
  931. // return -1;
  932. //}
  933. //peek_info.new_src_ip.v4=iph->saddr;
  934. //unsigned short iphdrlen =iph->ihl*4;
  935. //char *payload=ip_begin+iphdrlen;
  936. packet_info_t &recv_info=raw_info.recv_info;
  937. char *payload;int payload_len;
  938. if(recv_raw_ip(raw_info,payload,payload_len)!=0)
  939. return -1;
  940. //mylog(log_info,"protocol %d\n",iph->protocol);
  941. switch(raw_mode)
  942. {
  943. case mode_faketcp:
  944. {
  945. if(recv_info.protocol!=IPPROTO_TCP)
  946. {
  947. mylog(log_trace,"failed here");
  948. return -1;
  949. }
  950. struct my_tcphdr *tcph=(my_tcphdr *)payload;
  951. if(payload_len<int(sizeof(my_tcphdr) ))
  952. {
  953. mylog(log_trace,"failed here");
  954. return -1;
  955. }
  956. recv_info.src_port=ntohs(tcph->source);
  957. recv_info.syn=tcph->syn;
  958. break;
  959. }
  960. case mode_udp:
  961. {
  962. if(recv_info.protocol!=IPPROTO_UDP) return -1;
  963. struct my_udphdr *udph=(my_udphdr *)payload;
  964. if(payload_len<int(sizeof(my_udphdr)))
  965. return -1;
  966. recv_info.src_port=ntohs(udph->source);
  967. break;
  968. }
  969. case mode_icmp:
  970. {
  971. if(raw_ip_version==AF_INET)
  972. {
  973. if(recv_info.protocol!=IPPROTO_ICMP) return -1;
  974. }else
  975. {
  976. assert(raw_ip_version==AF_INET6);
  977. if(recv_info.protocol!=IPPROTO_ICMPV6) return -1;
  978. }
  979. struct my_icmphdr *icmph=(my_icmphdr *)payload;
  980. if(payload_len<int(sizeof(my_udphdr)))
  981. return -1;
  982. recv_info.src_port=ntohs(icmph->id);
  983. break;
  984. }
  985. default:return -1;
  986. }
  987. return 0;
  988. }
  989. int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen)
  990. {
  991. const packet_info_t &send_info=raw_info.send_info;
  992. const packet_info_t &recv_info=raw_info.recv_info;
  993. char send_raw_icmp_buf[buf_len];
  994. my_icmphdr *icmph=(struct my_icmphdr *) (send_raw_icmp_buf);
  995. memset(icmph,0,sizeof(my_icmphdr));
  996. if(raw_ip_version==AF_INET)
  997. {
  998. if(program_mode==client_mode)
  999. {
  1000. icmph->type=8;
  1001. }
  1002. else
  1003. {
  1004. icmph->type=0;
  1005. }
  1006. }
  1007. else
  1008. {
  1009. assert(raw_ip_version==AF_INET6);
  1010. if(program_mode==client_mode)
  1011. {
  1012. icmph->type=128;
  1013. }
  1014. else
  1015. {
  1016. icmph->type=129;
  1017. }
  1018. }
  1019. icmph->code=0;
  1020. icmph->id=htons(send_info.src_port);
  1021. icmph->seq=htons(send_info.my_icmp_seq); /////////////modify
  1022. memcpy(send_raw_icmp_buf+sizeof(my_icmphdr),payload,payloadlen);
  1023. if(raw_ip_version==AF_INET)
  1024. {
  1025. icmph->check_sum = csum( (unsigned short*) send_raw_icmp_buf, sizeof(my_icmphdr)+payloadlen);
  1026. }
  1027. else
  1028. {
  1029. assert(raw_ip_version==AF_INET6);
  1030. pseudo_header6 v6;
  1031. struct pseudo_header6 *psh = &v6;
  1032. psh->src=send_info.new_src_ip.v6;
  1033. psh->dst=send_info.new_dst_ip.v6;
  1034. psh->next_header=IPPROTO_ICMPV6;
  1035. psh->tcp_length=htons(sizeof(my_icmphdr)+payloadlen);
  1036. psh->placeholder1 = 0;
  1037. psh->placeholder2 = 0;
  1038. icmph->check_sum = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) send_raw_icmp_buf, sizeof(my_icmphdr)+payloadlen);
  1039. }
  1040. if(send_raw_ip(raw_info,send_raw_icmp_buf,sizeof(my_icmphdr)+payloadlen)!=0)
  1041. {
  1042. return -1;
  1043. }
  1044. /*if(program_mode==client_mode)
  1045. {
  1046. send_info.icmp_seq++;
  1047. }*/
  1048. return 0;
  1049. }
  1050. int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
  1051. {
  1052. const packet_info_t &send_info=raw_info.send_info;
  1053. const packet_info_t &recv_info=raw_info.recv_info;
  1054. char send_raw_udp_buf[buf_len];
  1055. my_udphdr *udph=(struct my_udphdr *) (send_raw_udp_buf);
  1056. memset(udph,0,sizeof(my_udphdr));
  1057. udph->source = htons(send_info.src_port);
  1058. udph->dest = htons(send_info.dst_port);
  1059. int udp_tot_len=payloadlen+sizeof(my_udphdr);
  1060. if(udp_tot_len>65535)
  1061. {
  1062. mylog(log_debug,"invalid len\n");
  1063. return -1;
  1064. }
  1065. mylog(log_trace,"udp_len:%d %d\n",udp_tot_len,udph->len);
  1066. udph->len=htons(uint16_t(udp_tot_len));
  1067. memcpy(send_raw_udp_buf+sizeof(my_udphdr),payload,payloadlen);
  1068. if(raw_ip_version==AF_INET)
  1069. {
  1070. pseudo_header v4;
  1071. struct pseudo_header *psh = &v4;
  1072. psh->source_address = send_info.new_src_ip.v4;
  1073. psh->dest_address = send_info.new_dst_ip.v4;
  1074. psh->placeholder = 0;
  1075. psh->protocol = IPPROTO_UDP;
  1076. psh->tcp_length = htons(udp_tot_len);
  1077. udph->check = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) send_raw_udp_buf, udp_tot_len);
  1078. }
  1079. else
  1080. {
  1081. assert(raw_ip_version==AF_INET6);
  1082. pseudo_header6 v6;
  1083. struct pseudo_header6 *psh = &v6;
  1084. psh->src=send_info.new_src_ip.v6;
  1085. psh->dst=send_info.new_dst_ip.v6;
  1086. psh->next_header=IPPROTO_UDP;
  1087. psh->tcp_length=htons(udp_tot_len);
  1088. psh->placeholder1 = 0;
  1089. psh->placeholder2 = 0;
  1090. udph->check = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) send_raw_udp_buf, udp_tot_len);
  1091. }
  1092. if(send_raw_ip(raw_info,send_raw_udp_buf,udp_tot_len)!=0)
  1093. {
  1094. return -1;
  1095. }
  1096. return 0;
  1097. }
  1098. int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) { //TODO seq increase
  1099. const packet_info_t &send_info=raw_info.send_info;
  1100. const packet_info_t &recv_info=raw_info.recv_info;
  1101. //mylog(log_debug,"syn %d\n",send_info.syn);
  1102. char send_raw_tcp_buf[buf_len];
  1103. //char *send_raw_tcp_buf=send_raw_tcp_buf0;
  1104. struct my_tcphdr *tcph = (struct my_tcphdr *) (send_raw_tcp_buf);
  1105. memset(tcph,0,sizeof(my_tcphdr));
  1106. //TCP Header
  1107. tcph->source = htons(send_info.src_port);
  1108. tcph->dest = htons(send_info.dst_port);
  1109. tcph->seq = htonl(send_info.seq);
  1110. tcph->ack_seq = htonl(send_info.ack_seq);
  1111. tcph->fin = 0;
  1112. tcph->syn = send_info.syn;
  1113. tcph->rst = 0;
  1114. tcph->psh = send_info.psh;
  1115. tcph->ack = send_info.ack;
  1116. if (tcph->syn == 1) {
  1117. tcph->doff = 10; //tcp header size
  1118. int i = sizeof(my_tcphdr);
  1119. send_raw_tcp_buf[i++] = 0x02; //mss
  1120. send_raw_tcp_buf[i++] = 0x04;
  1121. send_raw_tcp_buf[i++] = 0x05;
  1122. send_raw_tcp_buf[i++] = (char)0xb4;
  1123. //raw_send_buf[i++]=0x01;
  1124. //raw_send_buf[i++]=0x01;
  1125. send_raw_tcp_buf[i++] = 0x04; //sack ok
  1126. send_raw_tcp_buf[i++] = 0x02; //sack ok
  1127. send_raw_tcp_buf[i++] = 0x08; //ts i=6
  1128. send_raw_tcp_buf[i++] = 0x0a; //i=7
  1129. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
  1130. // (u32_t) get_current_time());
  1131. u32_t ts=htonl((u32_t) get_current_time());
  1132. memcpy(&send_raw_tcp_buf[i],&ts,sizeof(ts));
  1133. i += 4;
  1134. //mylog(log_info,"[syn]<send_info.ts_ack= %u>\n",send_info.ts_ack);
  1135. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  1136. u32_t ts_ack=htonl(send_info.ts_ack);
  1137. memcpy(&send_raw_tcp_buf[i],&ts_ack,sizeof(ts_ack));
  1138. i += 4;
  1139. send_raw_tcp_buf[i++] = 0x01;
  1140. send_raw_tcp_buf[i++] = 0x03;
  1141. send_raw_tcp_buf[i++] = 0x03;
  1142. send_raw_tcp_buf[i++] = wscale;
  1143. } else {
  1144. tcph->doff = 8;
  1145. int i = sizeof(my_tcphdr);
  1146. send_raw_tcp_buf[i++] = 0x01;
  1147. send_raw_tcp_buf[i++] = 0x01;
  1148. send_raw_tcp_buf[i++] = 0x08; //ts //i=2
  1149. send_raw_tcp_buf[i++] = 0x0a; //i=3;
  1150. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(
  1151. // (u32_t) get_current_time());
  1152. u32_t ts=htonl((u32_t) get_current_time());
  1153. memcpy(&send_raw_tcp_buf[i],&ts,sizeof(ts));
  1154. i += 4;
  1155. //mylog(log_info,"<send_info.ts_ack= %u>\n",send_info.ts_ack);
  1156. //*(u32_t*) (&send_raw_tcp_buf[i]) = htonl(send_info.ts_ack);
  1157. u32_t ts_ack=htonl(send_info.ts_ack);
  1158. memcpy(&send_raw_tcp_buf[i],&ts_ack,sizeof(ts_ack));
  1159. i += 4;
  1160. }
  1161. tcph->urg = 0;
  1162. //tcph->window = htons((uint16_t)(1024));
  1163. tcph->window = htons((uint16_t) (receive_window_lower_bound + get_true_random_number() % receive_window_random_range));
  1164. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  1165. tcph->urg_ptr = 0;
  1166. char *tcp_data = send_raw_tcp_buf+ + tcph->doff * 4;
  1167. memcpy(tcp_data, payload, payloadlen);
  1168. int tcp_totlen=tcph->doff*4 + payloadlen;
  1169. if(raw_ip_version==AF_INET)
  1170. {
  1171. pseudo_header v4;
  1172. struct pseudo_header *psh = &v4;
  1173. psh->source_address = send_info.new_src_ip.v4;
  1174. psh->dest_address = send_info.new_dst_ip.v4;
  1175. psh->placeholder = 0;
  1176. psh->protocol = IPPROTO_TCP;
  1177. psh->tcp_length = htons(tcp_totlen);
  1178. tcph->check = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) send_raw_tcp_buf, tcp_totlen);
  1179. }
  1180. else
  1181. {
  1182. assert(raw_ip_version==AF_INET6);
  1183. pseudo_header6 v6;
  1184. struct pseudo_header6 *psh = &v6;
  1185. psh->src=send_info.new_src_ip.v6;
  1186. psh->dst=send_info.new_dst_ip.v6;
  1187. psh->next_header=IPPROTO_TCP;
  1188. psh->tcp_length=htons(tcp_totlen);
  1189. psh->placeholder1 = 0;
  1190. psh->placeholder2 = 0;
  1191. tcph->check = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) send_raw_tcp_buf, tcp_totlen);
  1192. }
  1193. if(send_raw_ip(raw_info,send_raw_tcp_buf,tcp_totlen)!=0)
  1194. {
  1195. return -1;
  1196. }
  1197. raw_info.send_info.data_len=payloadlen;
  1198. return 0;
  1199. }
  1200. /*
  1201. int send_raw_tcp_deprecated(const packet_info_t &info,const char * payload,int payloadlen)
  1202. {
  1203. static uint16_t ip_id=1;
  1204. char raw_send_buf[buf_len];
  1205. char raw_send_buf2[buf_len];
  1206. //if((prog_mode==client_mode&& payloadlen!=9) ||(prog_mode==server_mode&& payloadlen!=5 ) )
  1207. mylog(log_trace,"send raw from to %d %d %d %d\n",info.src_ip,info.src_port,info.dst_ip,info.dst_port);
  1208. char *data;
  1209. memset(raw_send_buf,0,payloadlen+100);
  1210. struct iphdr *iph = (struct iphdr *) raw_send_buf;
  1211. //TCP header
  1212. struct tcphdr *tcph = (struct tcphdr *) (raw_send_buf + sizeof (struct ip));
  1213. struct sockaddr_in sin;
  1214. struct pseudo_header psh;
  1215. //some address resolution
  1216. sin.sin_family = AF_INET;
  1217. sin.sin_port = htons(info.dst_port);
  1218. sin.sin_addr.s_addr = info.dst_ip;
  1219. //Fill in the IP Header
  1220. iph->ihl = 5;
  1221. iph->version = 4;
  1222. iph->tos = 0;
  1223. iph->id = htonl (ip_id++); //Id of this packet
  1224. iph->frag_off = htons(0x4000); //DF set,others are zero
  1225. iph->ttl = (unsigned char)ttl_value;
  1226. iph->protocol = IPPROTO_TCP;
  1227. iph->check = 0; //Set to 0 before calculating checksum
  1228. iph->saddr = info.src_ip; //Spoof the source ip address
  1229. iph->daddr = info.dst_ip;
  1230. //TCP Header
  1231. tcph->source = htons(info.src_port);
  1232. tcph->dest = htons(info.dst_port);
  1233. tcph->seq =htonl(info.seq);
  1234. tcph->ack_seq = htonl(info.ack_seq);
  1235. tcph->fin=0;
  1236. tcph->syn=info.syn;
  1237. tcph->rst=0;
  1238. tcph->psh=info.psh;
  1239. tcph->ack=info.ack;
  1240. if(tcph->syn==1)
  1241. {
  1242. tcph->doff = 10; //tcp header size
  1243. int i=sizeof (struct iphdr)+20;
  1244. raw_send_buf[i++]=0x02;//mss
  1245. raw_send_buf[i++]=0x04;
  1246. raw_send_buf[i++]=0x05;
  1247. raw_send_buf[i++]=0xb4;
  1248. //raw_send_buf[i++]=0x01;
  1249. //raw_send_buf[i++]=0x01;
  1250. raw_send_buf[i++]=0x04; //sack ok
  1251. raw_send_buf[i++]=0x02; //sack ok
  1252. raw_send_buf[i++]=0x08; //i=6;
  1253. raw_send_buf[i++]=0x0a;
  1254. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  1255. i+=4;
  1256. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  1257. i+=4;
  1258. raw_send_buf[i++]=0x01;
  1259. raw_send_buf[i++]=0x03;
  1260. raw_send_buf[i++]=0x03;
  1261. raw_send_buf[i++]=0x05;
  1262. }
  1263. else
  1264. {
  1265. tcph->doff=8;
  1266. int i=sizeof (struct iphdr)+20;
  1267. raw_send_buf[i++]=0x01;
  1268. raw_send_buf[i++]=0x01;
  1269. raw_send_buf[i++]=0x08; //i=0;
  1270. raw_send_buf[i++]=0x0a;
  1271. *(uint32_t*)(& raw_send_buf[i])=htonl((uint32_t)get_current_time());
  1272. i+=4;
  1273. *(uint32_t*)(& raw_send_buf[i])=htonl(info.ts_ack);
  1274. i+=4;
  1275. }
  1276. tcph->urg=0;
  1277. //tcph->window = htons((uint16_t)(1024));
  1278. tcph->window = htons((uint16_t)(10240+random()%100));
  1279. tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
  1280. tcph->urg_ptr = 0;
  1281. //Data part
  1282. data = raw_send_buf + sizeof(struct iphdr) + tcph->doff*4;
  1283. iph->tot_len = sizeof (struct iphdr) + tcph->doff*4 + payloadlen;
  1284. memcpy(data , payload, payloadlen);
  1285. psh.source_address = info.src_ip;
  1286. psh.dest_address = sin.sin_addr.s_addr;
  1287. psh.placeholder = 0;
  1288. psh.protocol = IPPROTO_TCP;
  1289. psh.tcp_length = htons(tcph->doff*4 + payloadlen );
  1290. int psize = sizeof(struct pseudo_header) + tcph->doff*4 + payloadlen;
  1291. memcpy(raw_send_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  1292. memcpy(raw_send_buf2 + sizeof(struct pseudo_header) , tcph , tcph->doff*4 + payloadlen);
  1293. tcph->check = csum( (unsigned short*) raw_send_buf2, psize);
  1294. //Ip checksum
  1295. iph->check = csum ((unsigned short *) raw_send_buf, iph->tot_len);
  1296. mylog(log_trace,"sent seq ack_seq len<%u %u %d>\n",g_packet_info_send.seq,g_packet_info_send.ack_seq,payloadlen);
  1297. int ret = sendto(raw_send_fd, raw_send_buf, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
  1298. if(g_packet_info_send.syn==0&&g_packet_info_send.ack==1&&payloadlen!=0)
  1299. {
  1300. if(seq_mode==0)
  1301. {
  1302. }
  1303. else if(seq_mode==1)
  1304. {
  1305. g_packet_info_send.seq+=payloadlen;
  1306. }
  1307. else if(seq_mode==2)
  1308. {
  1309. if(random()% 5==3 )
  1310. g_packet_info_send.seq+=payloadlen;
  1311. }
  1312. }
  1313. mylog(log_trace,"<ret:%d>\n",ret);
  1314. if(ret<0)
  1315. {
  1316. mylog(log_fatal,"");
  1317. perror("raw send error\n");
  1318. //printf("send error\n");
  1319. }
  1320. return 0;
  1321. }
  1322. */
  1323. int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  1324. {
  1325. const packet_info_t &send_info=raw_info.send_info;
  1326. packet_info_t &recv_info=raw_info.recv_info;
  1327. static char recv_raw_icmp_buf[buf_len];
  1328. char * ip_payload;
  1329. int ip_payloadlen;
  1330. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1331. {
  1332. mylog(log_debug,"recv_raw_ip error\n");
  1333. return -1;
  1334. }
  1335. mylog(log_trace,"ip_payloadlen=%d\n",ip_payloadlen);
  1336. if(raw_ip_version==AF_INET)
  1337. {
  1338. if(recv_info.protocol!=IPPROTO_ICMP)
  1339. {
  1340. //printf("not udp protocol\n");
  1341. return -1;
  1342. }
  1343. }
  1344. else
  1345. {
  1346. assert(raw_ip_version==AF_INET6);
  1347. if(recv_info.protocol!=IPPROTO_ICMPV6)
  1348. {
  1349. //printf("not udp protocol\n");
  1350. return -1;
  1351. }
  1352. }
  1353. if(ip_payloadlen<int( sizeof(my_icmphdr) ))
  1354. {
  1355. mylog(log_debug,"too short to hold icmp header\n");
  1356. return -1;
  1357. }
  1358. my_icmphdr *icmph=(struct my_icmphdr *) (ip_payload);
  1359. if(ntohs(icmph->id)!=send_info.src_port)
  1360. {
  1361. mylog(log_debug,"icmp id mis-match,ignored\n");
  1362. return -1;
  1363. }
  1364. recv_info.src_port=recv_info.dst_port=ntohs(icmph->id);
  1365. recv_info.my_icmp_seq=ntohs(icmph->seq);
  1366. if(icmph->code!=0)
  1367. return -1;
  1368. unsigned short check ;
  1369. if(raw_ip_version==AF_INET)
  1370. {
  1371. if(program_mode==client_mode)
  1372. {
  1373. if(icmph->type!=0)
  1374. return -1;
  1375. }
  1376. else
  1377. {
  1378. if(icmph->type!=8)
  1379. return -1;
  1380. }
  1381. check= csum( (unsigned short*) ip_payload, ip_payloadlen);
  1382. }
  1383. else
  1384. {
  1385. assert(raw_ip_version==AF_INET6);
  1386. if(program_mode==client_mode)
  1387. {
  1388. if(icmph->type!=129)
  1389. return -1;
  1390. }
  1391. else
  1392. {
  1393. if(icmph->type!=128)
  1394. return -1;
  1395. }
  1396. pseudo_header6 tmp_header;
  1397. struct pseudo_header6 *psh=&tmp_header ;
  1398. psh->src = recv_info.new_src_ip.v6;
  1399. psh->dst = recv_info.new_dst_ip.v6;
  1400. psh->placeholder1 = 0;
  1401. psh->placeholder2 = 0;
  1402. psh->next_header = IPPROTO_ICMPV6;
  1403. psh->tcp_length = htons(ip_payloadlen);
  1404. check = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) ip_payload, ip_payloadlen);
  1405. }
  1406. if(check!=0)
  1407. {
  1408. mylog(log_debug,"icmp checksum fail %x\n",check);
  1409. return -1;
  1410. }
  1411. //mylog(log_info,"program_mode=%d\n",program_mode);
  1412. /*
  1413. if(program_mode==server_mode)
  1414. {
  1415. send_info.icmp_seq=ntohs(icmph->seq);
  1416. //mylog(log_info,"send_info.seq=%d\n",send_info.seq);
  1417. }*/
  1418. payload=ip_payload+sizeof(my_icmphdr);
  1419. payloadlen=ip_payloadlen-sizeof(my_icmphdr);
  1420. mylog(log_trace,"get a packet len=%d\n",payloadlen);
  1421. return 0;
  1422. }
  1423. int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen)
  1424. {
  1425. const packet_info_t &send_info=raw_info.send_info;
  1426. packet_info_t &recv_info=raw_info.recv_info;
  1427. //static char recv_raw_udp_buf[buf_len];
  1428. char * ip_payload;
  1429. int ip_payloadlen;
  1430. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1431. {
  1432. mylog(log_debug,"recv_raw_ip error\n");
  1433. return -1;
  1434. }
  1435. if(recv_info.protocol!=IPPROTO_UDP)
  1436. {
  1437. //printf("not udp protocol\n");
  1438. return -1;
  1439. }
  1440. if(ip_payloadlen<int( sizeof(my_udphdr) ))
  1441. {
  1442. mylog(log_debug,"too short to hold udpheader\n");
  1443. return -1;
  1444. }
  1445. my_udphdr *udph=(struct my_udphdr*)ip_payload;
  1446. if(int(ntohs(udph->len))!=ip_payloadlen)
  1447. {
  1448. mylog(log_debug,"udp length error %d %d \n",ntohs(udph->len),ip_payloadlen);
  1449. return -1;
  1450. }
  1451. if(udph->dest!=ntohs(uint16_t(filter_port)))
  1452. {
  1453. //printf("%x %x",tcph->dest,);
  1454. return -1;
  1455. }
  1456. //memcpy(recv_raw_udp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1457. /*
  1458. pseudo_header tmp_header={0};
  1459. struct pseudo_header *psh=&tmp_header ;
  1460. psh->source_address = recv_info.new_src_ip.v4;
  1461. psh->dest_address = recv_info.new_dst_ip.v4;
  1462. psh->placeholder = 0;
  1463. psh->protocol = IPPROTO_UDP;
  1464. psh->tcp_length = htons(ip_payloadlen);
  1465. int csum_len=ip_payloadlen;
  1466. uint16_t udp_chk = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) ip_payload, csum_len);
  1467. */
  1468. uint16_t udp_chk;
  1469. int csum_len=ip_payloadlen;
  1470. if(raw_ip_version==AF_INET)
  1471. {
  1472. pseudo_header tmp_header;
  1473. struct pseudo_header *psh=&tmp_header ;
  1474. psh->source_address = recv_info.new_src_ip.v4;
  1475. psh->dest_address = recv_info.new_dst_ip.v4;
  1476. psh->placeholder = 0;
  1477. psh->protocol = IPPROTO_UDP;
  1478. psh->tcp_length = htons(ip_payloadlen);
  1479. udp_chk = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) ip_payload, csum_len);
  1480. }
  1481. else
  1482. {
  1483. assert(raw_ip_version==AF_INET6);
  1484. pseudo_header6 tmp_header;
  1485. struct pseudo_header6 *psh=&tmp_header ;
  1486. psh->src = recv_info.new_src_ip.v6;
  1487. psh->dst = recv_info.new_dst_ip.v6;
  1488. psh->placeholder1 = 0;
  1489. psh->placeholder2 = 0;
  1490. psh->next_header = IPPROTO_UDP;
  1491. psh->tcp_length = htons(ip_payloadlen);
  1492. udp_chk = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) ip_payload, csum_len);
  1493. }
  1494. if(udp_chk!=0)
  1495. {
  1496. mylog(log_debug,"udp_chk:%x\n",udp_chk);
  1497. mylog(log_debug,"udp header error\n");
  1498. return -1;
  1499. }
  1500. char *udp_begin=ip_payload;
  1501. recv_info.src_port=ntohs(udph->source);
  1502. recv_info.dst_port=ntohs(udph->dest);
  1503. payloadlen = ip_payloadlen-sizeof(my_udphdr);
  1504. payload=udp_begin+sizeof(my_udphdr);
  1505. return 0;
  1506. }
  1507. int parse_tcp_option(char * option_begin,char * option_end,packet_info_t &recv_info)
  1508. {
  1509. recv_info.has_ts=0;
  1510. recv_info.ts=0;
  1511. char *ptr=option_begin;
  1512. //char *option_end=tcp_begin+tcp_hdr_len;
  1513. while(ptr<option_end)
  1514. {
  1515. if(*ptr==0)
  1516. {
  1517. return 0;
  1518. }
  1519. else if(*ptr==1)
  1520. {
  1521. ptr++;
  1522. }
  1523. else if(*ptr==8)
  1524. {
  1525. if(ptr+1>=option_end)
  1526. {
  1527. mylog(log_trace,"invaild option ptr+1==option_end,for ts\n");
  1528. return -1;
  1529. }
  1530. if(*(ptr+1)!=10)
  1531. {
  1532. mylog(log_trace,"invaild ts len\n");
  1533. return -1;
  1534. }
  1535. if(ptr+10>option_end)
  1536. {
  1537. mylog(log_trace,"ptr+10>option_end for ts\n");
  1538. return -1;
  1539. }
  1540. recv_info.has_ts=1;
  1541. recv_info.ts= read_u32(ptr+2);
  1542. recv_info.ts_ack=read_u32(ptr+6);
  1543. //printf("<%d %d>!\n",recv_info.ts,recv_info.ts_ack);
  1544. //return 0;//we currently only parse ts, so just return after its found
  1545. ptr+=8;
  1546. }
  1547. else
  1548. {
  1549. if(ptr+1>=option_end)
  1550. {
  1551. mylog(log_trace,"invaild option ptr+1==option_end\n");
  1552. return -1;
  1553. }
  1554. else
  1555. {
  1556. int len=(unsigned char)*(ptr+1);
  1557. if(len<=1)
  1558. {
  1559. mylog(log_trace,"invaild option len %d\n",len);
  1560. return -1;
  1561. }
  1562. //omit check
  1563. ptr+=len;
  1564. }
  1565. }
  1566. //printf("!");
  1567. }
  1568. //printf("\n");
  1569. return 0;
  1570. }
  1571. int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
  1572. {
  1573. const packet_info_t &send_info=raw_info.send_info;
  1574. packet_info_t &recv_info=raw_info.recv_info;
  1575. //static char recv_raw_tcp_buf[buf_len];
  1576. char * ip_payload;
  1577. int ip_payloadlen;
  1578. if(recv_raw_ip(raw_info,ip_payload,ip_payloadlen)!=0)
  1579. {
  1580. mylog(log_debug,"recv_raw_ip error\n");
  1581. return -1;
  1582. }
  1583. if(recv_info.protocol!=IPPROTO_TCP)
  1584. {
  1585. //printf("not tcp protocol\n");
  1586. return -1;
  1587. }
  1588. my_tcphdr * tcph=(struct my_tcphdr*)ip_payload;
  1589. unsigned short tcphdrlen = tcph->doff*4;
  1590. if (!(tcphdrlen > 0 && tcphdrlen <=60)) {
  1591. mylog(log_debug,"tcph error\n");
  1592. return 0;
  1593. }
  1594. if (tcphdrlen >ip_payloadlen) {
  1595. mylog(log_debug,"error,tcphdrlen >ip_payloadlen\n");
  1596. return 0;
  1597. }
  1598. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1599. {
  1600. //printf("%x %x",tcph->dest,);
  1601. return -1;
  1602. }
  1603. // memcpy(recv_raw_tcp_buf+ sizeof(struct pseudo_header) , ip_payload , ip_payloadlen);
  1604. uint16_t tcp_chk;
  1605. int csum_len=ip_payloadlen;
  1606. if(raw_ip_version==AF_INET)
  1607. {
  1608. pseudo_header tmp_header;
  1609. struct pseudo_header *psh=&tmp_header ;
  1610. psh->source_address = recv_info.new_src_ip.v4;
  1611. psh->dest_address = recv_info.new_dst_ip.v4;
  1612. psh->placeholder = 0;
  1613. psh->protocol = IPPROTO_TCP;
  1614. psh->tcp_length = htons(ip_payloadlen);
  1615. tcp_chk = csum_with_header((char *)psh,sizeof(pseudo_header), (unsigned short*) ip_payload, csum_len);
  1616. }
  1617. else
  1618. {
  1619. assert(raw_ip_version==AF_INET6);
  1620. pseudo_header6 tmp_header;
  1621. struct pseudo_header6 *psh=&tmp_header ;
  1622. psh->src = recv_info.new_src_ip.v6;
  1623. psh->dst = recv_info.new_dst_ip.v6;
  1624. psh->placeholder1 = 0;
  1625. psh->placeholder2 = 0;
  1626. psh->next_header = IPPROTO_TCP;
  1627. psh->tcp_length = htons(ip_payloadlen);
  1628. tcp_chk = csum_with_header((char *)psh,sizeof(pseudo_header6), (unsigned short*) ip_payload, csum_len);
  1629. }
  1630. /*for(int i=0;i<csum_len;i++)
  1631. {
  1632. printf("<%d>",int(ip_payload[i]));
  1633. }
  1634. printf("\n");*/
  1635. if(tcp_chk!=0)
  1636. {
  1637. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  1638. mylog(log_debug,"tcp header error\n");
  1639. return -1;
  1640. }
  1641. char *tcp_begin=ip_payload; //ip packet's data part
  1642. char *tcp_option=ip_payload+sizeof(my_tcphdr);
  1643. char *option_end=ip_payload+tcphdrlen;
  1644. /*
  1645. //old ts parse code
  1646. recv_info.has_ts=0;
  1647. recv_info.ts=0;
  1648. if(tcph->doff==10)
  1649. {
  1650. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1651. {
  1652. recv_info.has_ts=1;
  1653. //recv_info.ts=ntohl(*(u32_t*)(&tcp_option[8]));
  1654. memcpy(&recv_info.ts,&tcp_option[8],sizeof(recv_info.ts));
  1655. recv_info.ts=ntohl(recv_info.ts);
  1656. //recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[12]));
  1657. memcpy(&recv_info.ts_ack,&tcp_option[12],sizeof(recv_info.ts_ack));
  1658. recv_info.ts_ack=ntohl(recv_info.ts_ack);
  1659. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1660. }
  1661. else
  1662. {
  1663. // mylog(log_info,"\n");
  1664. }
  1665. }
  1666. else if(tcph->doff==8)
  1667. {
  1668. if(tcp_option[2]==0x08 &&tcp_option[3]==0x0a)
  1669. {
  1670. recv_info.has_ts=1;
  1671. //recv_info.ts=ntohl(*(u32_t*)(&tcp_option[4]));
  1672. memcpy(&recv_info.ts,&tcp_option[4],sizeof(recv_info.ts));
  1673. recv_info.ts=ntohl(recv_info.ts);
  1674. //recv_info.ts_ack=ntohl(*(u32_t*)(&tcp_option[8]));
  1675. memcpy(&recv_info.ts_ack,&tcp_option[8],sizeof(recv_info.ts_ack));
  1676. recv_info.ts_ack=ntohl(recv_info.ts_ack);
  1677. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1678. }
  1679. else
  1680. {
  1681. //mylog(log_info,"!!!\n");
  1682. }
  1683. }
  1684. else
  1685. {
  1686. //mylog(log_info,"tcph->doff= %u\n",tcph->doff);
  1687. }
  1688. printf("<%d %d>\n",recv_info.ts,recv_info.ts_ack);
  1689. */
  1690. parse_tcp_option(tcp_option,option_end,recv_info);
  1691. recv_info.ack=tcph->ack;
  1692. recv_info.syn=tcph->syn;
  1693. recv_info.rst=tcph->rst;
  1694. recv_info.src_port=ntohs(tcph->source);
  1695. recv_info.dst_port=ntohs(tcph->dest);
  1696. recv_info.seq=ntohl(tcph->seq);
  1697. // recv_info.last_last_ack_seq=recv_info.last_ack_seq;
  1698. //recv_info.last_ack_seq=recv_info.ack_seq;
  1699. u32_t last_ack_seq=recv_info.ack_seq;
  1700. recv_info.ack_seq=ntohl(tcph->ack_seq);
  1701. if(recv_info.ack_seq==last_ack_seq)
  1702. {
  1703. recv_info.ack_seq_counter++;
  1704. }
  1705. else
  1706. {
  1707. recv_info.ack_seq_counter=0;
  1708. }
  1709. recv_info.psh=tcph->psh;
  1710. if(tcph->rst==1)
  1711. {
  1712. raw_info.rst_received++;
  1713. if(max_rst_to_show>0)
  1714. {
  1715. if(raw_info.rst_received < max_rst_to_show)
  1716. {
  1717. mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1718. }
  1719. else if(raw_info.rst_received == max_rst_to_show)
  1720. {
  1721. mylog(log_warn,"[%s,%d]rst==1,cnt=%d >=max_rst_to_show, this log will be muted for current connection\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1722. }
  1723. else
  1724. {
  1725. mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1726. }
  1727. }
  1728. else if(max_rst_to_show==0)
  1729. {
  1730. mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1731. }
  1732. else
  1733. {
  1734. mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
  1735. }
  1736. if(max_rst_allowed>=0 && raw_info.rst_received==max_rst_allowed+1 )
  1737. {
  1738. mylog(log_warn,"[%s,%d]connection disabled because of rst_received=%d > max_rst_allow=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received,(int)max_rst_allowed );
  1739. raw_info.disabled=1;
  1740. }
  1741. }
  1742. /* if(recv_info.has_ts)
  1743. {
  1744. send_info.ts_ack=recv_info.ts; //////////////////////////////////////////////modify
  1745. }*/
  1746. payloadlen = ip_payloadlen-tcphdrlen;
  1747. payload=tcp_begin+tcphdrlen;
  1748. /*if (recv_info.syn == 0 && recv_info.ack == 1&& payloadlen != 0) //only modify send_info when the packet is not part of handshake
  1749. {
  1750. send_info.ack_seq=recv_info.seq;
  1751. }*/
  1752. raw_info.recv_info.data_len=payloadlen;
  1753. return 0;
  1754. }
  1755. /*
  1756. int recv_raw_tcp_deprecated(packet_info_t &info,char * &payload,int &payloadlen)
  1757. {
  1758. static char buf[buf_len];
  1759. char raw_recv_buf[buf_len];
  1760. char raw_recv_buf2[buf_len];
  1761. char raw_recv_buf3[buf_len];
  1762. iphdr * iph;
  1763. tcphdr * tcph;
  1764. int size;
  1765. struct sockaddr saddr;
  1766. socklen_t saddr_size;
  1767. saddr_size = sizeof(saddr);
  1768. mylog(log_trace,"raw!\n");
  1769. size = recvfrom(raw_recv_fd, buf, max_data_len, 0 ,&saddr , &saddr_size);
  1770. if(buf[12]!=8||buf[13]!=0)
  1771. {
  1772. mylog(log_debug,"not an ipv4 packet!\n");
  1773. return -1;
  1774. }
  1775. char *ip_begin=buf+14;
  1776. iph = (struct iphdr *) (ip_begin);
  1777. if (!(iph->ihl > 0 && iph->ihl <=60)) {
  1778. mylog(log_debug,"iph ihl error");
  1779. return -1;
  1780. }
  1781. if (iph->protocol != IPPROTO_TCP) {
  1782. mylog(log_debug,"iph protocal != tcp\n");
  1783. return -1;
  1784. }
  1785. int ip_len=ntohs(iph->tot_len);
  1786. unsigned short iphdrlen =iph->ihl*4;
  1787. tcph=(struct tcphdr*)(ip_begin+ iphdrlen);
  1788. unsigned short tcphdrlen = tcph->doff*4;
  1789. if (!(tcph->doff > 0 && tcph->doff <=60)) {
  1790. mylog(log_debug,"tcph error");
  1791. return 0;
  1792. }
  1793. if(tcph->dest!=ntohs(uint16_t(filter_port)))
  1794. {
  1795. //printf("%x %x",tcph->dest,);
  1796. return -1;
  1797. }
  1798. /////ip
  1799. uint32_t ip_chk=csum ((unsigned short *) ip_begin, iphdrlen);
  1800. int psize = sizeof(struct pseudo_header) + ip_len-iphdrlen;
  1801. /////ip end
  1802. ///tcp
  1803. struct pseudo_header psh;
  1804. psh.source_address = iph->saddr;
  1805. psh.dest_address = iph->daddr;
  1806. psh.placeholder = 0;
  1807. psh.protocol = IPPROTO_TCP;
  1808. psh.tcp_length = htons(ip_len-iphdrlen);
  1809. memcpy(raw_recv_buf2 , (char*) &psh , sizeof (struct pseudo_header));
  1810. memcpy(raw_recv_buf2 + sizeof(struct pseudo_header) , ip_begin+ iphdrlen , ip_len-iphdrlen);
  1811. uint16_t tcp_chk = csum( (unsigned short*) raw_recv_buf2, psize);
  1812. if(ip_chk!=0)
  1813. {
  1814. mylog(log_debug,"ip header error %d\n",ip_chk);
  1815. return -1;
  1816. }
  1817. if(tcp_chk!=0)
  1818. {
  1819. mylog(log_debug,"tcp_chk:%x\n",tcp_chk);
  1820. mylog(log_debug,"tcp header error\n");
  1821. return -1;
  1822. }
  1823. char *tcp_begin=raw_recv_buf2+sizeof(struct pseudo_header); //ip packet's data part
  1824. char *tcp_option=raw_recv_buf2+sizeof(struct pseudo_header)+sizeof(tcphdr);
  1825. info.has_ts=0;
  1826. if(tcph->doff==10)
  1827. {
  1828. if(tcp_option[6]==0x08 &&tcp_option[7]==0x0a)
  1829. {
  1830. info.has_ts=1;
  1831. info.ts=ntohl(*(uint32_t*)(&tcp_option[8]));
  1832. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[12]));
  1833. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[8]));
  1834. }
  1835. }
  1836. else if(tcph->doff==8)
  1837. {
  1838. if(tcp_option[3]==0x08 &&tcp_option[4]==0x0a)
  1839. {
  1840. info.has_ts=1;
  1841. info.ts=ntohl(*(uint32_t*)(&tcp_option[0]));
  1842. info.ts_ack=ntohl(*(uint32_t*)(&tcp_option[4]));
  1843. //g_packet_info_send.ts_ack= ntohl(*(uint32_t*)(&tcp_option[0]));
  1844. }
  1845. }
  1846. if(tcph->rst==1)
  1847. {
  1848. mylog(log_warn,"%%%%%%%%%%rst==1%%%%%%%%%%%%%\n");
  1849. }
  1850. info.ack=tcph->ack;
  1851. info.syn=tcph->syn;
  1852. info.rst=tcph->rst;
  1853. info.src_port=ntohs(tcph->source);
  1854. info.src_ip=iph->saddr;
  1855. info.seq=ntohl(tcph->seq);
  1856. info.ack_seq=ntohl(tcph->ack_seq);
  1857. info.psh=tcph->psh;
  1858. if(info.has_ts)
  1859. {
  1860. g_packet_info_send.ts_ack=info.ts;
  1861. }
  1862. ////tcp end
  1863. payloadlen = ip_len-tcphdrlen-iphdrlen;
  1864. payload=ip_begin+tcphdrlen+iphdrlen;
  1865. if(payloadlen>0&&payload[0]=='h')
  1866. {
  1867. mylog(log_debug,"recvd <%u %u %d>\n",ntohl(tcph->seq ),ntohl(tcph->ack_seq), payloadlen);
  1868. }
  1869. if(payloadlen>0&&tcph->syn==0&&tcph->ack==1)
  1870. {
  1871. //if(seq_increse)
  1872. g_packet_info_send.ack_seq=ntohl(tcph->seq)+(uint32_t)payloadlen;
  1873. }
  1874. //printf("%d\n",ip_len);
  1875. mylog(log_trace,"<%u,%u,%u,%u,%d>\n",(unsigned int)iphdrlen,(unsigned int)tcphdrlen,(unsigned int)tcph->syn,(unsigned int)tcph->ack,payloadlen);
  1876. return 0;
  1877. }*/
  1878. int send_raw0(raw_info_t &raw_info,const char * payload,int payloadlen)
  1879. {
  1880. if (random_drop != 0) {
  1881. if (get_true_random_number() % 10000 < (u32_t) random_drop) {
  1882. return 0;
  1883. }
  1884. }
  1885. packet_info_t &send_info=raw_info.send_info;
  1886. packet_info_t &recv_info=raw_info.recv_info;
  1887. mylog(log_trace,"send_raw : from %s %d to %s %d\n",send_info.new_src_ip.get_str1(),send_info.src_port,send_info.new_dst_ip.get_str2(),send_info.dst_port);
  1888. switch(raw_mode)
  1889. {
  1890. case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
  1891. case mode_udp: return send_raw_udp(raw_info,payload,payloadlen);
  1892. case mode_icmp: return send_raw_icmp(raw_info,payload,payloadlen);
  1893. default:return -1;
  1894. }
  1895. }
  1896. int recv_raw0(raw_info_t &raw_info,char * &payload,int &payloadlen)
  1897. {
  1898. packet_info_t &send_info=raw_info.send_info;
  1899. packet_info_t &recv_info=raw_info.recv_info;
  1900. switch(raw_mode)
  1901. {
  1902. case mode_faketcp:return recv_raw_tcp(raw_info,payload,payloadlen);
  1903. case mode_udp: return recv_raw_udp(raw_info,payload,payloadlen);
  1904. case mode_icmp: return recv_raw_icmp(raw_info,payload,payloadlen);
  1905. default: return -1;
  1906. }
  1907. }
  1908. int after_send_raw0(raw_info_t &raw_info)
  1909. {
  1910. packet_info_t &send_info=raw_info.send_info;
  1911. packet_info_t &recv_info=raw_info.recv_info;
  1912. if(raw_mode==mode_faketcp)
  1913. {
  1914. 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
  1915. {
  1916. if (seq_mode == 0)
  1917. {
  1918. } else if (seq_mode == 1)
  1919. {
  1920. send_info.seq += raw_info.send_info.data_len; //////////////////modify
  1921. } else if (seq_mode == 2)
  1922. {
  1923. if (get_true_random_number() % 5 == 3)
  1924. send_info.seq += raw_info.send_info.data_len; //////////////////modify
  1925. }
  1926. else if(seq_mode==3||seq_mode==4)
  1927. {
  1928. send_info.seq += raw_info.send_info.data_len;
  1929. u32_t window_size;
  1930. if(seq_mode==3)
  1931. {
  1932. window_size=(u32_t)((u32_t)receive_window_lower_bound<<(u32_t)wscale);
  1933. }
  1934. else//seq_mode==4
  1935. {
  1936. window_size=(u32_t)((u32_t)receive_window_lower_bound);
  1937. }
  1938. if(larger_than_u32(send_info.seq+max_data_len,recv_info.ack_seq+window_size))
  1939. {
  1940. send_info.seq=raw_info.recv_info.ack_seq;
  1941. }
  1942. if(recv_info.ack_seq_counter>=3) //simulate tcp fast re-transmit
  1943. {
  1944. recv_info.ack_seq_counter=0;
  1945. send_info.seq=raw_info.recv_info.ack_seq;
  1946. }
  1947. if(larger_than_u32(raw_info.recv_info.ack_seq,send_info.seq)) //for further use,currently no effect.
  1948. {
  1949. send_info.seq=raw_info.recv_info.ack_seq;
  1950. }
  1951. }
  1952. }
  1953. }
  1954. if(raw_mode==mode_icmp)
  1955. {
  1956. if(program_mode==client_mode)
  1957. {
  1958. send_info.my_icmp_seq++;
  1959. }
  1960. }
  1961. return 0;
  1962. }
  1963. int after_recv_raw0(raw_info_t &raw_info)
  1964. {
  1965. packet_info_t &send_info=raw_info.send_info;
  1966. packet_info_t &recv_info=raw_info.recv_info;
  1967. if(raw_mode==mode_faketcp)
  1968. {
  1969. if(recv_info.has_ts)
  1970. send_info.ts_ack=recv_info.ts;
  1971. 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
  1972. {
  1973. if(seq_mode==0||seq_mode==1||seq_mode==2)
  1974. {
  1975. if(larger_than_u32(recv_info.seq+raw_info.recv_info.data_len,send_info.ack_seq))
  1976. send_info.ack_seq = recv_info.seq+raw_info.recv_info.data_len;//TODO only update if its larger
  1977. }
  1978. else if(seq_mode==3||seq_mode==4)
  1979. {
  1980. if(recv_info.seq==send_info.ack_seq)
  1981. {
  1982. send_info.ack_seq=recv_info.seq+raw_info.recv_info.data_len;//currently we dont remembr tcp segments,this is the simplest way
  1983. //TODO implement tcp segment remembering and SACK.
  1984. }
  1985. }
  1986. }
  1987. }
  1988. if(raw_mode==mode_icmp)
  1989. {
  1990. if(program_mode==server_mode)
  1991. {
  1992. if(larger_than_u16(recv_info.my_icmp_seq,send_info.my_icmp_seq))
  1993. send_info.my_icmp_seq = recv_info.my_icmp_seq; //TODO only update if its larger
  1994. }
  1995. }
  1996. return 0;
  1997. }
  1998. /*
  1999. int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen)
  2000. {
  2001. packet_info_t &send_info=raw_info.send_info;
  2002. packet_info_t &recv_info=raw_info.recv_info;
  2003. int ret=send_raw0(raw_info,payload,payloadlen);
  2004. if(ret<0) return ret;
  2005. else
  2006. {
  2007. after_send_raw0(raw_info);
  2008. return ret;
  2009. }
  2010. }
  2011. int recv_raw(raw_info_t &raw_info,char *& payload,int & payloadlen)
  2012. {
  2013. packet_info_t &send_info=raw_info.send_info;
  2014. packet_info_t &recv_info=raw_info.recv_info;
  2015. int ret=recv_raw0(raw_info,payload,payloadlen);
  2016. if(ret<0) return ret;
  2017. else
  2018. {
  2019. after_recv_raw0(raw_info);
  2020. return ret;
  2021. }
  2022. }*/
  2023. /*
  2024. 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
  2025. {
  2026. struct sockaddr_in remote_addr_in={0};
  2027. socklen_t slen = sizeof(sockaddr_in);
  2028. //memset(&remote_addr_in, 0, sizeof(remote_addr_in));
  2029. remote_addr_in.sin_family = AF_INET;
  2030. remote_addr_in.sin_port = htons(remote_port);
  2031. remote_addr_in.sin_addr.s_addr = remote_ip_uint32;
  2032. int new_udp_fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  2033. if(new_udp_fd<0)
  2034. {
  2035. mylog(log_warn,"create udp_fd error\n");
  2036. return -1;
  2037. }
  2038. //set_buf_size(new_udp_fd);
  2039. mylog(log_debug,"created new udp_fd %d\n",new_udp_fd);
  2040. int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
  2041. if(ret!=0)
  2042. {
  2043. mylog(log_warn,"udp fd connect fail\n");
  2044. close(new_udp_fd);
  2045. return -1;
  2046. }
  2047. struct sockaddr_in my_addr={0};
  2048. socklen_t len=sizeof(my_addr);
  2049. if(getsockname(new_udp_fd, (struct sockaddr *) &my_addr, &len)!=0){close(new_udp_fd); return -1;}
  2050. ip=my_addr.sin_addr.s_addr;
  2051. close(new_udp_fd);
  2052. return 0;
  2053. }*/
  2054. int get_src_adress2(address_t &output_addr,address_t remote_addr)
  2055. {
  2056. int new_udp_fd=remote_addr.new_connected_udp_fd();
  2057. if(new_udp_fd<0)
  2058. {
  2059. mylog(log_warn,"create udp_fd error\n");
  2060. return -1;
  2061. }
  2062. socklen_t len=sizeof(output_addr.inner);
  2063. if(getsockname(new_udp_fd, (struct sockaddr *) &output_addr.inner, &len)!=0) {close(new_udp_fd); return -1;}
  2064. assert(output_addr.get_type()==remote_addr.get_type());
  2065. close(new_udp_fd);
  2066. return 0;
  2067. }
  2068. /*
  2069. int try_to_list_and_bind(int &fd,u32_t local_ip_uint32,int port) //try to bind to a port,may fail.
  2070. {
  2071. int old_bind_fd=fd;
  2072. if(raw_mode==mode_faketcp)
  2073. {
  2074. fd=socket(AF_INET,SOCK_STREAM,0);
  2075. }
  2076. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  2077. {
  2078. fd=socket(AF_INET,SOCK_DGRAM,0);
  2079. }
  2080. if(old_bind_fd!=-1)
  2081. {
  2082. close(old_bind_fd);
  2083. }
  2084. struct sockaddr_in temp_bind_addr={0};
  2085. //bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  2086. temp_bind_addr.sin_family = AF_INET;
  2087. temp_bind_addr.sin_port = htons(port);
  2088. temp_bind_addr.sin_addr.s_addr = local_ip_uint32;
  2089. if (bind(fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
  2090. {
  2091. mylog(log_debug,"bind fail\n");
  2092. return -1;
  2093. }
  2094. if(raw_mode==mode_faketcp)
  2095. {
  2096. if (listen(fd, SOMAXCONN) != 0) {
  2097. mylog(log_warn,"listen fail\n");
  2098. return -1;
  2099. }
  2100. }
  2101. return 0;
  2102. }*/
  2103. int try_to_list_and_bind2(int &fd,address_t address) //try to bind to a port,may fail.
  2104. {
  2105. if(fd!=-1)
  2106. {
  2107. close(fd);
  2108. }
  2109. if(raw_mode==mode_faketcp)
  2110. {
  2111. fd=socket(address.get_type(),SOCK_STREAM,0);
  2112. }
  2113. else if(raw_mode==mode_udp||raw_mode==mode_icmp)
  2114. {
  2115. fd=socket(address.get_type(),SOCK_DGRAM,0);
  2116. }
  2117. if(fd==-1)
  2118. {
  2119. mylog(log_debug,"create fd fail\n");
  2120. return -1;
  2121. }
  2122. /*struct sockaddr_in temp_bind_addr={0};
  2123. //bzero(&temp_bind_addr, sizeof(temp_bind_addr));
  2124. temp_bind_addr.sin_family = AF_INET;
  2125. temp_bind_addr.sin_port = htons(port);
  2126. temp_bind_addr.sin_addr.s_addr = local_ip_uint32;*/
  2127. if (bind(fd, (struct sockaddr*)&address.inner, address.get_len()) !=0)
  2128. {
  2129. mylog(log_debug,"bind fail\n");
  2130. return -1;
  2131. }
  2132. if(raw_mode==mode_faketcp&&!use_tcp_dummy_socket)
  2133. {
  2134. if (listen(fd, SOMAXCONN) != 0) {
  2135. mylog(log_warn,"listen fail\n");
  2136. return -1;
  2137. }
  2138. }
  2139. return 0;
  2140. }
  2141. /*
  2142. int client_bind_to_a_new_port(int &fd,u32_t local_ip_uint32)//find a free port and bind to it.
  2143. {
  2144. int raw_send_port=10000+get_true_random_number()%(65535-10000);
  2145. for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
  2146. {
  2147. if (try_to_list_and_bind(fd,local_ip_uint32,raw_send_port)==0)
  2148. {
  2149. return raw_send_port;
  2150. }
  2151. }
  2152. mylog(log_fatal,"bind port fail\n");
  2153. myexit(-1);
  2154. return -1;////for compiler check
  2155. }*/
  2156. int client_bind_to_a_new_port2(int &fd,const address_t& address)//find a free port and bind to it.
  2157. {
  2158. address_t tmp=address;
  2159. for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
  2160. {
  2161. int raw_send_port=10000+get_true_random_number()%(65535-10000);
  2162. tmp.set_port(raw_send_port);
  2163. if (try_to_list_and_bind2(fd,tmp)==0)
  2164. {
  2165. return raw_send_port;
  2166. }
  2167. }
  2168. mylog(log_fatal,"bind port fail\n");
  2169. myexit(-1);
  2170. return -1;////for compiler check
  2171. }