network.cpp 46 KB

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