1
0

network.cpp 88 KB

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