misc.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /*
  2. * misc.cpp
  3. *
  4. * Created on: Sep 23, 2017
  5. * Author: root
  6. */
  7. #include "git_version.h"
  8. #include "common.h"
  9. #include "encrypt.h"
  10. #include "misc.h"
  11. #include "network.h"
  12. #include "connection.h"
  13. #include "fd_manager.h"
  14. int hb_mode=1;
  15. int hb_len=1200;
  16. int mtu_warn=1375;//if a packet larger than mtu warn is receviced,there will be a warning
  17. int max_rst_to_show=15;
  18. int max_rst_allowed=-1;
  19. int enable_dns_resolve=0;
  20. int ttl_value=64;
  21. fd_manager_t fd_manager;
  22. //char remote_address[max_address_len]="";
  23. //char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_ip for -r option,source for --source-ip
  24. //u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line.
  25. //int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled
  26. address_t local_addr,remote_addr,source_addr,bind_addr;
  27. int bind_addr_used=0;
  28. int force_source_ip=0; //if --source-ip is enabled
  29. int force_source_port=0;
  30. id_t const_id=0;//an id used for connection recovery,its generated randomly,it never change since its generated
  31. int udp_fd=-1; //for client only. client use this fd to listen and handle udp connection
  32. int bind_fd=-1; //bind only,never send or recv. its just a dummy fd for bind,so that other program wont occupy the same port
  33. int epollfd=-1; //fd for epoll
  34. int timer_fd=-1; //the general timer fd for client and server.for server this is not the only timer find,every connection has a timer fd.
  35. int fail_time_counter=0;//determine if the max_fail_time is reached
  36. int epoll_trigger_counter=0;//for debug only
  37. int debug_flag=0;//for debug only
  38. int simple_rule=0; //deprecated.
  39. int keep_rule=0; //whether to monitor the iptables rule periodly,re-add if losted
  40. int auto_add_iptables_rule=0;//if -a is set
  41. int generate_iptables_rule=0;//if -g is set
  42. int generate_iptables_rule_add=0;// if --gen-add is set
  43. int retry_on_error=0;
  44. int debug_resend=0; // debug only
  45. char key_string[1000]= "secret key";// -k option
  46. char fifo_file[1000]="";
  47. int clear_iptables=0;
  48. int wait_xtables_lock=0;
  49. string iptables_command0="iptables ";
  50. string iptables_command="";
  51. string iptables_pattern="";
  52. int iptables_rule_added=0;
  53. int iptables_rule_keeped=0;
  54. int iptables_rule_keep_index=0;
  55. program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
  56. raw_mode_t raw_mode=mode_faketcp;
  57. unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
  58. int about_to_exit=0;
  59. int socket_buf_size=1024*1024;
  60. //char lower_level_arg[1000];
  61. int process_lower_level_arg()//handle --lower-level option
  62. {
  63. lower_level=1;
  64. if(strcmp(optarg,"auto")==0)
  65. {
  66. return 0;
  67. }
  68. lower_level_manual=1;
  69. if (strchr(optarg, '#') == 0) {
  70. mylog(log_fatal,
  71. "lower-level parameter invaild,check help page for format\n");
  72. myexit(-1);
  73. }
  74. lower_level = 1;
  75. u32_t hw[6];
  76. memset(hw, 0, sizeof(hw));
  77. sscanf(optarg, "%[^#]#%x:%x:%x:%x:%x:%x", if_name, &hw[0], &hw[1], &hw[2],
  78. &hw[3], &hw[4], &hw[5]);
  79. mylog(log_warn,
  80. "make sure this is correct: if_name=<%s> dest_mac_adress=<%02x:%02x:%02x:%02x:%02x:%02x> \n",
  81. if_name, hw[0], hw[1], hw[2], hw[3], hw[4], hw[5]);
  82. for (int i = 0; i < 6; i++) {
  83. dest_hw_addr[i] = uint8_t(hw[i]);
  84. }
  85. return 0;
  86. }
  87. void print_help()
  88. {
  89. char git_version_buf[100]={0};
  90. strncpy(git_version_buf,gitversion,10);
  91. printf("udp2raw-tunnel\n");
  92. printf("git version:%s ",git_version_buf);
  93. printf("build date:%s %s\n",__DATE__,__TIME__);
  94. printf("repository: https://github.com/wangyu-/udp2raw-tunnel\n");
  95. printf("\n");
  96. printf("usage:\n");
  97. printf(" run as client : ./this_program -c -l local_listen_ip:local_port -r server_address:server_port [options]\n");
  98. printf(" run as server : ./this_program -s -l server_listen_ip:server_port -r remote_address:remote_port [options]\n");
  99. printf("\n");
  100. printf("common options,these options must be same on both side:\n");
  101. printf(" --raw-mode <string> avaliable values:faketcp(default),udp,icmp\n");
  102. printf(" -k,--key <string> password to gen symetric key,default:\"secret key\"\n");
  103. printf(" --cipher-mode <string> avaliable values:aes128cbc(default),xor,none\n");
  104. printf(" --auth-mode <string> avaliable values:hmac_sha1,md5(default),crc32,simple,none\n");
  105. printf(" -a,--auto-rule auto add (and delete) iptables rule\n");
  106. printf(" -g,--gen-rule generate iptables rule then exit,so that you can copy and\n");
  107. printf(" add it manually.overrides -a\n");
  108. printf(" --disable-anti-replay disable anti-replay,not suggested\n");
  109. //printf("\n");
  110. printf("client options:\n");
  111. printf(" --source-ip <ip> force source-ip for raw socket\n");
  112. printf(" --source-port <port> force source-port for raw socket,tcp/udp only\n");
  113. printf(" this option disables port changing while re-connecting\n");
  114. // printf(" \n");
  115. printf("other options:\n");
  116. printf(" --conf-file <string> read options from a configuration file instead of command line.\n");
  117. printf(" check example.conf in repo for format\n");
  118. printf(" --fifo <string> use a fifo(named pipe) for sending commands to the running program,\n");
  119. printf(" check readme.md in repository for supported commands.\n");
  120. printf(" --log-level <number> 0:never 1:fatal 2:error 3:warn \n");
  121. printf(" 4:info (default) 5:debug 6:trace\n");
  122. // printf("\n");
  123. printf(" --log-position enable file name,function name,line number in log\n");
  124. printf(" --disable-color disable log color\n");
  125. printf(" --disable-bpf disable the kernel space filter,most time its not necessary\n");
  126. printf(" unless you suspect there is a bug\n");
  127. // printf("\n");
  128. printf(" --sock-buf <number> buf size for socket,>=10 and <=10240,unit:kbyte,default:1024\n");
  129. printf(" --force-sock-buf bypass system limitation while setting sock-buf\n");
  130. printf(" --seq-mode <number> seq increase mode for faketcp:\n");
  131. printf(" 0:static header,do not increase seq and ack_seq\n");
  132. printf(" 1:increase seq for every packet,simply ack last seq\n");
  133. printf(" 2:increase seq randomly, about every 3 packets,simply ack last seq\n");
  134. printf(" 3:simulate an almost real seq/ack procedure(default)\n");
  135. printf(" 4:similiar to 3,but do not consider TCP Option Window_Scale,\n");
  136. printf(" maybe useful when firewall doesnt support TCP Option \n");
  137. // printf("\n");
  138. printf(" --lower-level <string> send packets at OSI level 2, format:'if_name#dest_mac_adress'\n");
  139. printf(" ie:'eth0#00:23:45:67:89:b9'.or try '--lower-level auto' to obtain\n");
  140. printf(" the parameter automatically,specify it manually if 'auto' failed\n");
  141. printf(" --wait-lock wait for xtables lock while invoking iptables, need iptables v1.4.20+\n");
  142. printf(" --gen-add generate iptables rule and add it permanently,then exit.overrides -g\n");
  143. printf(" --keep-rule monitor iptables and auto re-add if necessary.implys -a\n");
  144. printf(" --hb-len <number> length of heart-beat packet, >=0 and <=1500\n");
  145. printf(" --mtu-warn <number> mtu warning threshold, unit:byte, default:1375\n");
  146. printf(" --clear clear any iptables rules added by this program.overrides everything\n");
  147. printf(" --retry-on-error retry on error, allow to start udp2raw before network is initialized\n");
  148. printf(" -h,--help print this help message\n");
  149. //printf("common options,these options must be same on both side\n");
  150. }
  151. int load_config(char *file_name, int &argc, vector<string> &argv) //load conf file and append to argv
  152. {
  153. // Load configurations from config_file instead of the command line.
  154. // See config.example for example configurations
  155. std::ifstream conf_file(file_name);
  156. std::string line;
  157. if(conf_file.fail())
  158. {
  159. mylog(log_fatal,"conf_file %s open failed,reason :%s\n",file_name,strerror(errno));
  160. myexit(-1);
  161. }
  162. while(std::getline(conf_file,line))
  163. {
  164. auto res=parse_conf_line(line);
  165. argc+=res.size();
  166. for(int i=0;i<(int)res.size();i++)
  167. {
  168. argv.push_back(res[i]);
  169. }
  170. }
  171. conf_file.close();
  172. return 0;
  173. }
  174. int process_log_level(int argc,char *argv[])//process --log-level and --disable-cloer --log-postion options
  175. {
  176. int i,j,k;
  177. for (i = 0; i < argc; i++)
  178. {
  179. if(strcmp(argv[i],"--log-level")==0)
  180. {
  181. if(i<argc -1)
  182. {
  183. sscanf(argv[i+1],"%d",&log_level);
  184. if(0<=log_level&&log_level<log_end)
  185. {
  186. }
  187. else
  188. {
  189. log_bare(log_fatal,"invalid log_level\n");
  190. myexit(-1);
  191. }
  192. }
  193. }
  194. if(strcmp(argv[i],"--disable-color")==0)
  195. {
  196. enable_log_color=0;
  197. }
  198. if(strcmp(argv[i],"--log-position")==0)
  199. {
  200. enable_log_position=1;
  201. }
  202. }
  203. return 0;
  204. }
  205. void process_arg(int argc, char *argv[]) //process all options
  206. {
  207. int i,j,k,opt;
  208. int option_index = 0;
  209. char options[]="l:r:schk:ag";
  210. static struct option long_options[] =
  211. {
  212. /* These options set a flag. */
  213. {"source-ip", required_argument, 0, 1},
  214. {"source-port", required_argument, 0, 1},
  215. {"log-level", required_argument, 0, 1},
  216. {"key", required_argument, 0, 'k'},
  217. {"auth-mode", required_argument, 0, 1},
  218. {"cipher-mode", required_argument, 0, 1},
  219. {"raw-mode", required_argument, 0, 1},
  220. {"disable-color", no_argument, 0, 1},
  221. {"log-position", no_argument, 0, 1},
  222. {"disable-bpf", no_argument, 0, 1},
  223. {"disable-anti-replay", no_argument, 0, 1},
  224. {"auto-rule", no_argument, 0, 'a'},
  225. {"gen-rule", no_argument, 0, 'g'},
  226. {"gen-add", no_argument, 0, 1},
  227. {"debug", no_argument, 0, 1},
  228. {"dev", required_argument, 0, 1},
  229. {"retry-on-error", no_argument, 0, 1},
  230. {"clear", no_argument, 0, 1},
  231. {"simple-rule", no_argument, 0, 1},
  232. {"keep-rule", no_argument, 0, 1},
  233. {"lower-level", required_argument, 0, 1},
  234. {"sock-buf", required_argument, 0, 1},
  235. {"seq-mode", required_argument, 0, 1},
  236. {"conf-file", required_argument, 0, 1},
  237. {"force-sock-buf", no_argument, 0, 1},
  238. {"wait-lock", no_argument, 0, 1},
  239. {"random-drop", required_argument, 0, 1},
  240. {"fifo", required_argument, 0, 1},
  241. {"hb-mode", required_argument, 0, 1},
  242. {"hb-len", required_argument, 0, 1},
  243. {"mtu-warn", required_argument, 0, 1},
  244. {"max-rst-to-show", required_argument, 0, 1},
  245. {"max-rst-allowed", required_argument, 0, 1},
  246. {"set-ttl", required_argument, 0, 1},
  247. {"dns-resolve", no_argument, 0, 1},
  248. {NULL, 0, 0, 0}
  249. };
  250. process_log_level(argc,argv);
  251. set<string> all_options;
  252. map<string,string> shortcut_map;
  253. all_options.insert("--help");
  254. all_options.insert("-h");
  255. string dummy="";
  256. for(i=0;i<(int)strlen(options);i++)
  257. {
  258. char val=options[i];
  259. if( ( val>='0'&&val<='9') ||( val>='a'&&val<='z')||(val>='A'&&val<='Z'))
  260. {
  261. all_options.insert(dummy+'-'+val);
  262. }
  263. }
  264. for(i=0;i<int( sizeof(long_options)/sizeof(long_options[0]) );i++)
  265. {
  266. if(long_options[i].name==NULL) break;
  267. int val=long_options[i].val;
  268. if( ( val>='0'&&val<='9') ||( val>='a'&&val<='z')||(val>='A'&&val<='Z'))
  269. {
  270. shortcut_map[dummy+"--"+long_options[i].name]= dummy+"-"+ char(val);
  271. }
  272. all_options.insert(dummy+"--"+long_options[i].name);
  273. }
  274. for (i = 0; i < argc; i++)
  275. {
  276. int len=strlen(argv[i]);
  277. if(len==0)
  278. {
  279. mylog(log_fatal,"found an empty string in options\n");
  280. myexit(-1);
  281. }
  282. if(len==1&&argv[i][0]=='-' )
  283. {
  284. mylog(log_fatal,"invaild option '-' in argv\n");
  285. myexit(-1);
  286. }
  287. if(len==2&&argv[i][0]=='-'&&argv[i][1]=='-' )
  288. {
  289. mylog(log_fatal,"invaild option '--' in argv\n");
  290. myexit(-1);
  291. }
  292. }
  293. mylog(log_info,"argc=%d ", argc);
  294. for (i = 0; i < argc; i++) {
  295. log_bare(log_info, "%s ", argv[i]);
  296. }
  297. log_bare(log_info, "\n");
  298. //string dummy="";
  299. for(i=+1;i<argc;i++)
  300. {
  301. if(argv[i][0]!='-') continue;
  302. string a=argv[i];
  303. if(a[0]=='-'&&a[1]!='-')
  304. a=dummy+a[0]+a[1];
  305. if(all_options.find(a.c_str())==all_options.end())
  306. {
  307. mylog(log_fatal,"invaild option %s\n",a.c_str());
  308. myexit(-1);
  309. }
  310. for(j=i+1;j<argc;j++)
  311. {
  312. if(argv[j][0]!='-') continue;
  313. string b=argv[j];
  314. if(b[0]=='-'&&b[1]!='-')
  315. b=dummy+b[0]+b[1];
  316. if(shortcut_map.find(a)!=shortcut_map.end())
  317. a=shortcut_map[a];
  318. if(shortcut_map.find(b)!=shortcut_map.end())
  319. b=shortcut_map[b];
  320. if(a==b)
  321. {
  322. mylog(log_fatal,"%s duplicates with %s\n",argv[i],argv[j]);
  323. myexit(-1);
  324. }
  325. }
  326. }
  327. int no_l = 1, no_r = 1;
  328. while ((opt = getopt_long(argc, argv,options,long_options,&option_index)) != -1) {
  329. //string opt_key;
  330. //opt_key+=opt;
  331. switch (opt) {
  332. case 'l':
  333. no_l = 0;
  334. local_addr.from_str(optarg);
  335. if(local_addr.get_port()==22)
  336. {
  337. mylog(log_fatal,"port 22 not allowed\n");
  338. myexit(-1);
  339. }
  340. /*
  341. if (strchr(optarg, ':') != 0) {
  342. sscanf(optarg, "%[^:]:%d", local_ip, &local_port);
  343. if(local_port==22)
  344. {
  345. mylog(log_fatal,"port 22 not allowed\n");
  346. myexit(-1);
  347. }
  348. } else {
  349. mylog(log_fatal,"invalid parameter for -l ,%s,should be ip:port\n",optarg);
  350. myexit(-1);
  351. }*/
  352. break;
  353. case 'r':
  354. no_r = 0;
  355. remote_addr.from_str(optarg);
  356. if(remote_addr.get_port()==22)
  357. {
  358. mylog(log_fatal,"port 22 not allowed\n");
  359. myexit(-1);
  360. }
  361. /*
  362. if (strchr(optarg, ':') != 0) {
  363. sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
  364. if(remote_port==22)
  365. {
  366. mylog(log_fatal,"port 22 not allowed\n");
  367. myexit(-1);
  368. }
  369. } else {
  370. mylog(log_fatal,"invalid parameter for -r ,%s,should be ip:port\n",optarg);
  371. myexit(-1);
  372. }*/
  373. break;
  374. case 's':
  375. if(program_mode==0)
  376. {
  377. program_mode=server_mode;
  378. }
  379. else
  380. {
  381. mylog(log_fatal,"-s /-c has already been set,conflict\n");
  382. myexit(-1);
  383. }
  384. break;
  385. case 'c':
  386. if(program_mode==0)
  387. {
  388. program_mode=client_mode;
  389. }
  390. else
  391. {
  392. mylog(log_fatal,"-s /-c has already been set,conflict\n");
  393. myexit(-1);
  394. }
  395. break;
  396. case 'h':
  397. break;
  398. case 'a':
  399. auto_add_iptables_rule=1;
  400. break;
  401. case 'g':
  402. generate_iptables_rule=1;
  403. break;
  404. case 'k':
  405. mylog(log_debug,"parsing key option\n");
  406. sscanf(optarg,"%s",key_string);
  407. break;
  408. case 1:
  409. mylog(log_debug,"option_index: %d\n",option_index);
  410. if(strcmp(long_options[option_index].name,"clear")==0)
  411. {
  412. clear_iptables=1;
  413. }
  414. /////////////////////fix this later
  415. /*
  416. else if(strcmp(long_options[option_index].name,"source-ip")==0)
  417. {
  418. mylog(log_debug,"parsing long option :source-ip\n");
  419. sscanf(optarg, "%s", source_ip);
  420. mylog(log_debug,"source: %s\n",source_ip);
  421. force_source_ip=1;
  422. }
  423. else if(strcmp(long_options[option_index].name,"source-port")==0)
  424. {
  425. mylog(log_debug,"parsing long option :source-port\n");
  426. sscanf(optarg, "%d", &source_port);
  427. mylog(log_info,"source: %d\n",source_port);
  428. }*/
  429. else if(strcmp(long_options[option_index].name,"raw-mode")==0)
  430. {
  431. for(i=0;i<mode_end;i++)
  432. {
  433. if(strcmp(optarg,raw_mode_tostring[i])==0)
  434. {
  435. //printf("%d i\n",i);
  436. //printf("%s",raw_mode_tostring[i]);
  437. raw_mode=(raw_mode_t)i;
  438. break;
  439. }
  440. }
  441. if(i==mode_end)
  442. {
  443. mylog(log_fatal,"no such raw_mode %s\n",optarg);
  444. myexit(-1);
  445. }
  446. }
  447. else if(strcmp(long_options[option_index].name,"auth-mode")==0)
  448. {
  449. for(i=0;i<auth_end;i++)
  450. {
  451. if(strcmp(optarg,auth_mode_tostring[i])==0)
  452. {
  453. auth_mode=(auth_mode_t)i;
  454. if(auth_mode==auth_none)
  455. {
  456. disable_anti_replay=1;
  457. }
  458. break;
  459. }
  460. }
  461. if(i==auth_end)
  462. {
  463. mylog(log_fatal,"no such auth_mode %s\n",optarg);
  464. myexit(-1);
  465. }
  466. }
  467. else if(strcmp(long_options[option_index].name,"cipher-mode")==0)
  468. {
  469. for(i=0;i<cipher_end;i++)
  470. {
  471. if(strcmp(optarg,cipher_mode_tostring[i])==0)
  472. {
  473. cipher_mode=(cipher_mode_t)i;
  474. break;
  475. }
  476. }
  477. if(i==cipher_end)
  478. {
  479. mylog(log_fatal,"no such cipher_mode %s\n",optarg);
  480. myexit(-1);
  481. }
  482. }
  483. else if(strcmp(long_options[option_index].name,"log-level")==0)
  484. {
  485. }
  486. else if(strcmp(long_options[option_index].name,"lower-level")==0)
  487. {
  488. process_lower_level_arg();
  489. //lower_level=1;
  490. //strcpy(lower_level_arg,optarg);
  491. }
  492. else if(strcmp(long_options[option_index].name,"simple-rule")==0)
  493. {
  494. simple_rule=1;
  495. }
  496. else if(strcmp(long_options[option_index].name,"keep-rule")==0)
  497. {
  498. keep_rule=1;
  499. }
  500. else if(strcmp(long_options[option_index].name,"gen-add")==0)
  501. {
  502. generate_iptables_rule_add=1;
  503. }
  504. else if(strcmp(long_options[option_index].name,"disable-color")==0)
  505. {
  506. //enable_log_color=0;
  507. }
  508. else if(strcmp(long_options[option_index].name,"debug")==0)
  509. {
  510. debug_flag=1;
  511. //enable_log_color=0;
  512. }
  513. else if(strcmp(long_options[option_index].name,"dev")==0)
  514. {
  515. sscanf(optarg,"%s",dev);
  516. //enable_log_color=0;
  517. }
  518. else if(strcmp(long_options[option_index].name,"debug-resend")==0)
  519. {
  520. //debug_resend=1;
  521. //enable_log_color=0;
  522. }
  523. else if(strcmp(long_options[option_index].name,"log-position")==0)
  524. {
  525. //enable_log_position=1;
  526. }
  527. else if(strcmp(long_options[option_index].name,"force-sock-buf")==0)
  528. {
  529. force_socket_buf=1;
  530. }
  531. else if(strcmp(long_options[option_index].name,"retry-on-error")==0)
  532. {
  533. retry_on_error=1;
  534. }
  535. else if(strcmp(long_options[option_index].name,"wait-lock")==0)
  536. {
  537. wait_xtables_lock=1;
  538. }
  539. else if(strcmp(long_options[option_index].name,"disable-bpf")==0)
  540. {
  541. disable_bpf_filter=1;
  542. }
  543. else if(strcmp(long_options[option_index].name,"disable-anti-replay")==0)
  544. {
  545. disable_anti_replay=1;
  546. }
  547. else if(strcmp(long_options[option_index].name,"sock-buf")==0)
  548. {
  549. int tmp=-1;
  550. sscanf(optarg,"%d",&tmp);
  551. if(10<=tmp&&tmp<=10*1024)
  552. {
  553. socket_buf_size=tmp*1024;
  554. }
  555. else
  556. {
  557. mylog(log_fatal,"sock-buf value must be between 1 and 10240 (kbyte) \n");
  558. myexit(-1);
  559. }
  560. }
  561. else if(strcmp(long_options[option_index].name,"seq-mode")==0)
  562. {
  563. sscanf(optarg,"%d",&seq_mode);
  564. if(0<=seq_mode&&seq_mode<=max_seq_mode)
  565. {
  566. }
  567. else
  568. {
  569. mylog(log_fatal,"seq_mode value must be 0,1,or 2 \n");
  570. myexit(-1);
  571. }
  572. }
  573. else if(strcmp(long_options[option_index].name,"random-drop")==0)
  574. {
  575. sscanf(optarg,"%d",&random_drop);
  576. if(random_drop<0||random_drop>10000)
  577. {
  578. mylog(log_fatal,"random_drop must be between 0 10000 \n");
  579. myexit(-1);
  580. }
  581. mylog(log_info,"random_drop =%d \n",random_drop);
  582. }
  583. else if(strcmp(long_options[option_index].name,"fifo")==0)
  584. {
  585. sscanf(optarg,"%s",fifo_file);
  586. mylog(log_info,"fifo_file =%s \n",fifo_file);
  587. }
  588. else if(strcmp(long_options[option_index].name,"conf-file")==0)
  589. {
  590. mylog(log_info,"configuration loaded from %s\n",optarg);
  591. }
  592. else if(strcmp(long_options[option_index].name,"hb-mode")==0)
  593. {
  594. sscanf(optarg,"%d",&hb_mode);
  595. assert(hb_mode==0||hb_mode==1);
  596. mylog(log_info,"hb_mode =%d \n",hb_mode);
  597. }
  598. else if(strcmp(long_options[option_index].name,"hb-len")==0)
  599. {
  600. sscanf(optarg,"%d",&hb_len);
  601. assert(hb_len>=0&&hb_len<=1500);
  602. mylog(log_info,"hb_len =%d \n",hb_len);
  603. }
  604. else if(strcmp(long_options[option_index].name,"mtu-warn")==0)
  605. {
  606. sscanf(optarg,"%d",&mtu_warn);
  607. assert(mtu_warn>0);
  608. mylog(log_info,"mtu_warn=%d \n",mtu_warn);
  609. }
  610. else if(strcmp(long_options[option_index].name,"max-rst-to-show")==0)
  611. {
  612. sscanf(optarg,"%d",&max_rst_to_show);
  613. assert(max_rst_to_show>=-1);
  614. mylog(log_info,"max_rst_to_show=%d \n",max_rst_to_show);
  615. }
  616. else if(strcmp(long_options[option_index].name,"max-rst-allowed")==0)
  617. {
  618. sscanf(optarg,"%d",&max_rst_allowed);
  619. assert(max_rst_allowed>=-1);
  620. mylog(log_info,"max_rst_allowed=%d \n",max_rst_allowed);
  621. }
  622. else if(strcmp(long_options[option_index].name,"set-ttl")==0)
  623. {
  624. sscanf(optarg,"%d",&ttl_value);
  625. assert(ttl_value>=0&&ttl_value<=255);
  626. mylog(log_info,"ttl_value=%d\n",ttl_value);
  627. }
  628. else if(strcmp(long_options[option_index].name,"dns-resolve")==0) // currently not used
  629. {
  630. enable_dns_resolve=1;
  631. mylog(log_info,"dns-resolve enabled\n");
  632. }
  633. else
  634. {
  635. mylog(log_warn,"ignored unknown long option ,option_index:%d code:<%x>\n",option_index, optopt);
  636. }
  637. break;
  638. default:
  639. mylog(log_fatal,"unknown option ,code:<%c>,<%x>\n",optopt, optopt);
  640. myexit(-1);
  641. }
  642. }
  643. if (no_l)
  644. mylog(log_fatal,"error: -l not found\n");
  645. if (no_r)
  646. mylog(log_fatal,"error: -r not found\n");
  647. if(program_mode==0)
  648. mylog(log_fatal,"error: -c /-s hasnt been set\n");
  649. if (no_l || no_r||program_mode==0)
  650. {
  651. print_help();
  652. myexit(-1);
  653. }
  654. //if(lower_level)
  655. //process_lower_level_arg();
  656. mylog(log_info,"important variables: ");
  657. log_bare(log_info,"log_level=%d:%s ",log_level,log_text[log_level]);
  658. log_bare(log_info,"raw_mode=%s ",raw_mode_tostring[raw_mode]);
  659. log_bare(log_info,"cipher_mode=%s ",cipher_mode_tostring[cipher_mode]);
  660. log_bare(log_info,"auth_mode=%s ",auth_mode_tostring[auth_mode]);
  661. log_bare(log_info,"key=%s ",key_string);
  662. log_bare(log_info,"local_addr=%s ",local_addr.get_str());
  663. log_bare(log_info,"remote_addr=%s ",remote_addr.get_str());
  664. if(force_source_ip||force_source_port)
  665. log_bare(log_info,"source_addr=%s ",source_addr.get_str());
  666. /*log_bare(log_info,"local_ip=%s ",local_ip);
  667. log_bare(log_info,"local_port=%d ",local_port);
  668. log_bare(log_info,"remote_address=%s ",remote_address);
  669. log_bare(log_info,"remote_port=%d ",remote_port);
  670. log_bare(log_info,"source_ip=%s ",source_ip);
  671. log_bare(log_info,"source_port=%d ",source_port);*/
  672. log_bare(log_info,"socket_buf_size=%d ",socket_buf_size);
  673. log_bare(log_info,"\n");
  674. }
  675. void pre_process_arg(int argc, char *argv[])//mainly for load conf file
  676. {
  677. int i,j,k;
  678. for (i = 0; i < argc; i++)
  679. {
  680. if(strcmp(argv[i],"--unit-test")==0)
  681. {
  682. unit_test();
  683. myexit(0);
  684. }
  685. }
  686. for (i = 0; i < argc; i++)
  687. {
  688. if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0)
  689. {
  690. print_help();
  691. myexit(0);
  692. }
  693. }
  694. if (argc == 1)
  695. {
  696. print_help();
  697. myexit(-1);
  698. }
  699. process_log_level(argc,argv);
  700. int new_argc=0;
  701. vector<string> new_argv;
  702. int count=0;
  703. int pos=-1;
  704. for (i = 0; i < argc; i++)
  705. {
  706. if(strcmp(argv[i],"--conf-file")==0)
  707. {
  708. count++;
  709. pos=i;
  710. if(i==argc)
  711. {
  712. mylog(log_fatal,"--conf-file need a parameter\n");
  713. myexit(-1);
  714. }
  715. if(argv[i+1][0]=='-')
  716. {
  717. mylog(log_fatal,"--conf-file need a parameter\n");
  718. myexit(-1);
  719. }
  720. i++;
  721. }
  722. else
  723. {
  724. //printf("<%s>",argv[i]);
  725. new_argc++;
  726. new_argv.push_back(argv[i]);
  727. }
  728. }
  729. if(count>1)
  730. {
  731. mylog(log_fatal,"duplicated --conf-file option\n");
  732. myexit(-1);
  733. }
  734. if(count>0)
  735. {
  736. load_config(argv[pos+1],new_argc,new_argv);
  737. }
  738. char* new_argv_char[new_argv.size()];
  739. new_argc=0;
  740. for(i=0;i<(int)new_argv.size();i++)
  741. {
  742. if(strcmp(new_argv[i].c_str(),"--conf-file")==0)
  743. {
  744. mylog(log_fatal,"cant have --conf-file in a config file\n");
  745. myexit(-1);
  746. }
  747. new_argv_char[new_argc++]=(char *)new_argv[i].c_str();
  748. }
  749. process_arg(new_argc,new_argv_char);
  750. }
  751. void *run_keep(void *none) //called in a new thread for --keep-rule option
  752. {
  753. while(1)
  754. {
  755. sleep(iptables_rule_keep_interval);
  756. keep_iptables_rule();
  757. if(about_to_exit) //just incase it runs forever if there is some bug,not necessary
  758. {
  759. sleep(10);
  760. keep_thread_running=0; //not thread safe ,but wont cause problem
  761. break;
  762. }
  763. }
  764. return NULL;
  765. }
  766. void iptables_rule() // handles -a -g --gen-add --keep-rule --clear --wait-lock
  767. {
  768. if(!wait_xtables_lock)
  769. {
  770. iptables_command=iptables_command0;
  771. }
  772. else
  773. {
  774. iptables_command=iptables_command0+"-w ";
  775. }
  776. if(clear_iptables)
  777. {
  778. char *output;
  779. //int ret =system("iptables-save |grep udp2raw_dWRwMnJhdw|sed -n 's/^-A/iptables -D/p'|sh");
  780. int ret =run_command(iptables_command+"-S|sed -n '/udp2rawDwrW/p'|sed -n 's/^-A/"+iptables_command+"-D/p'|sh",output);
  781. int ret2 =run_command(iptables_command+"-S|sed -n '/udp2rawDwrW/p'|sed -n 's/^-N/"+iptables_command+"-X/p'|sh",output);
  782. //system("iptables-save |grep udp2raw_dWRwMnJhdw|sed 's/^-A/iptables -D/'|sh");
  783. //system("iptables-save|grep -v udp2raw_dWRwMnJhdw|iptables-restore");
  784. mylog(log_info,"tried to clear all iptables rule created previously,return value %d %d\n",ret,ret2);
  785. myexit(-1);
  786. }
  787. if(auto_add_iptables_rule&&generate_iptables_rule)
  788. {
  789. mylog(log_warn," -g overrides -a\n");
  790. auto_add_iptables_rule=0;
  791. //myexit(-1);
  792. }
  793. if(generate_iptables_rule_add&&generate_iptables_rule)
  794. {
  795. mylog(log_warn," --gen-add overrides -g\n");
  796. generate_iptables_rule=0;
  797. //myexit(-1);
  798. }
  799. if(keep_rule&&auto_add_iptables_rule==0)
  800. {
  801. auto_add_iptables_rule=1;
  802. mylog(log_warn," --keep_rule implys -a\n");
  803. generate_iptables_rule=0;
  804. //myexit(-1);
  805. }
  806. char tmp_pattern[200];
  807. string pattern="";
  808. if(program_mode==client_mode)
  809. {
  810. if(raw_mode==mode_faketcp)
  811. {
  812. sprintf(tmp_pattern,"-s %s/32 -p tcp -m tcp --sport %d",remote_addr.get_ip(),remote_addr.get_port());
  813. }
  814. if(raw_mode==mode_udp)
  815. {
  816. sprintf(tmp_pattern,"-s %s/32 -p udp -m udp --sport %d",remote_addr.get_ip(),remote_addr.get_port());
  817. }
  818. if(raw_mode==mode_icmp)
  819. {
  820. sprintf(tmp_pattern,"-s %s/32 -p icmp",remote_addr.get_ip());
  821. }
  822. pattern=tmp_pattern;
  823. }
  824. if(program_mode==server_mode)
  825. {
  826. if(raw_mode==mode_faketcp)
  827. {
  828. sprintf(tmp_pattern,"-p tcp -m tcp --dport %d",local_addr.get_port());
  829. }
  830. if(raw_mode==mode_udp)
  831. {
  832. sprintf(tmp_pattern,"-p udp -m udp --dport %d",local_addr.get_port());
  833. }
  834. if(raw_mode==mode_icmp)
  835. {
  836. if(local_addr.inner.ipv4.sin_addr.s_addr==0)
  837. {
  838. sprintf(tmp_pattern,"-p icmp");
  839. }
  840. else
  841. {
  842. sprintf(tmp_pattern,"-d %s/32 -p icmp",local_addr.get_ip());
  843. }
  844. }
  845. pattern=tmp_pattern;
  846. }
  847. /*
  848. if(!simple_rule)
  849. {
  850. pattern += " -m comment --comment udp2rawDwrW_";
  851. char const_id_str[100];
  852. sprintf(const_id_str, "%x_", const_id);
  853. pattern += const_id_str;
  854. time_t timer;
  855. char buffer[26];
  856. struct tm* tm_info;
  857. time(&timer);
  858. tm_info = localtime(&timer);
  859. strftime(buffer, 26, "%Y-%m-%d-%H:%M:%S", tm_info);
  860. pattern += buffer;
  861. }*/
  862. if(generate_iptables_rule)
  863. {
  864. string rule=iptables_command+"-I INPUT ";
  865. rule+=pattern;
  866. rule+=" -j DROP";
  867. printf("generated iptables rule:\n");
  868. printf("%s\n",rule.c_str());
  869. myexit(0);
  870. }
  871. if(generate_iptables_rule_add)
  872. {
  873. iptables_gen_add(pattern.c_str(),const_id);
  874. myexit(0);
  875. }
  876. if(auto_add_iptables_rule)
  877. {
  878. iptables_rule_init(pattern.c_str(),const_id,keep_rule);
  879. if(keep_rule)
  880. {
  881. if(pthread_create(&keep_thread, NULL, run_keep, 0)) {
  882. mylog(log_fatal, "Error creating thread\n");
  883. myexit(-1);
  884. }
  885. keep_thread_running=1;
  886. }
  887. }
  888. else
  889. {
  890. mylog(log_warn," -a has not been set, make sure you have added the needed iptables rules manually\n");
  891. }
  892. }
  893. int unit_test()
  894. {
  895. printf("running unit test\n");
  896. vector<string> conf_lines= {"---aaa","--aaa bbb","-a bbb"," \t \t \t-a\t \t \t bbbbb\t \t \t "};
  897. for(int i=0;i<int(conf_lines.size());i++)
  898. {
  899. printf("orign:%s\n",conf_lines[i].c_str());
  900. auto res=parse_conf_line(conf_lines[i]);
  901. printf("pasrse_result: size %d",int(res.size()));
  902. for(int j=0;j<int(res.size());j++)
  903. {
  904. printf("<%s>",res[j].c_str());
  905. }
  906. printf("\n");
  907. }
  908. char s1[]={1,2,3,4,5};
  909. char s2[]={1};
  910. short c1=csum((unsigned short*)s1,5);
  911. short c2=csum((unsigned short*)s2,1);
  912. //c2=0;
  913. printf("%x %x\n",(int)c1,(int)c2);
  914. const char buf[]={1,2,3,4,5,6,7,8,9,10,11,2,13,14,15,16};
  915. char key[100]={0};
  916. char buf2[100]={0};
  917. char buf3[100]={0};
  918. char buf4[100]={0};
  919. int len=16;
  920. for(int i=0;i<len;i++)
  921. {
  922. printf("<%d>",buf[i]);
  923. }
  924. printf("\n");
  925. cipher_encrypt(buf,buf2,len,key);
  926. for(int i=0;i<len;i++)
  927. {
  928. printf("<%d>",buf2[i]);
  929. }
  930. printf("\n");
  931. int temp_len=len;
  932. cipher_decrypt(buf2,buf3,len,key);
  933. for(int i=0;i<len;i++)
  934. {
  935. printf("<%d>",buf3[i]);
  936. }
  937. printf("\n");
  938. cipher_encrypt(buf2,buf4,temp_len,key);
  939. for(int i=0;i<temp_len;i++)
  940. {
  941. printf("<%d>",buf4[i]);
  942. }
  943. return 0;
  944. }
  945. int set_timer(int epollfd,int &timer_fd)//put a timer_fd into epoll,general function,used both in client and server
  946. {
  947. int ret;
  948. epoll_event ev;
  949. itimerspec its;
  950. memset(&its,0,sizeof(its));
  951. if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  952. {
  953. mylog(log_fatal,"timer_fd create error\n");
  954. myexit(1);
  955. }
  956. its.it_interval.tv_sec=(timer_interval/1000);
  957. its.it_interval.tv_nsec=(timer_interval%1000)*1000ll*1000ll;
  958. its.it_value.tv_nsec=1; //imidiately
  959. timerfd_settime(timer_fd,0,&its,0);
  960. ev.events = EPOLLIN;
  961. ev.data.u64 = timer_fd;
  962. ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
  963. if (ret < 0) {
  964. mylog(log_fatal,"epoll_ctl return %d\n", ret);
  965. myexit(-1);
  966. }
  967. return 0;
  968. }
  969. int set_timer_server(int epollfd,int &timer_fd,fd64_t &fd64)//only for server
  970. {
  971. int ret;
  972. epoll_event ev;
  973. itimerspec its;
  974. memset(&its,0,sizeof(its));
  975. if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  976. {
  977. mylog(log_fatal,"timer_fd create error\n");
  978. myexit(1);
  979. }
  980. its.it_interval.tv_sec=(timer_interval/1000);
  981. its.it_interval.tv_nsec=(timer_interval%1000)*1000ll*1000ll;
  982. its.it_value.tv_nsec=1; //imidiately
  983. timerfd_settime(timer_fd,0,&its,0);
  984. fd64=fd_manager.create(timer_fd);
  985. ev.events = EPOLLIN;
  986. ev.data.u64 = fd64;////difference
  987. ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
  988. if (ret < 0) {
  989. mylog(log_fatal,"epoll_ctl return %d\n", ret);
  990. myexit(-1);
  991. }
  992. return 0;
  993. }
  994. int handle_lower_level(raw_info_t &raw_info)//fill lower_level info,when --lower-level is enabled,only for server
  995. {
  996. packet_info_t &send_info=raw_info.send_info;
  997. packet_info_t &recv_info=raw_info.recv_info;
  998. if(lower_level_manual)
  999. {
  1000. memset(&send_info.addr_ll,0,sizeof(send_info.addr_ll));
  1001. send_info.addr_ll.sll_family=AF_PACKET;
  1002. send_info.addr_ll.sll_ifindex=ifindex;
  1003. send_info.addr_ll.sll_halen=ETHER_ADDR_LEN;
  1004. send_info.addr_ll.sll_protocol=htons(ETH_P_IP);
  1005. memcpy(&send_info.addr_ll.sll_addr,dest_hw_addr,ETHER_ADDR_LEN);
  1006. mylog(log_debug,"[manual]lower level info %x %x\n ",send_info.addr_ll.sll_halen,send_info.addr_ll.sll_protocol);
  1007. }
  1008. else
  1009. {
  1010. memset(&send_info.addr_ll,0,sizeof(send_info.addr_ll));
  1011. send_info.addr_ll.sll_family=recv_info.addr_ll.sll_family;
  1012. send_info.addr_ll.sll_ifindex=recv_info.addr_ll.sll_ifindex;
  1013. send_info.addr_ll.sll_protocol=recv_info.addr_ll.sll_protocol;
  1014. send_info.addr_ll.sll_halen=recv_info.addr_ll.sll_halen;
  1015. memcpy(send_info.addr_ll.sll_addr,recv_info.addr_ll.sll_addr,sizeof(send_info.addr_ll.sll_addr));
  1016. //other bytes should be kept zero.
  1017. mylog(log_debug,"[auto]lower level info %x %x\n ",send_info.addr_ll.sll_halen,send_info.addr_ll.sll_protocol);
  1018. }
  1019. return 0;
  1020. }
  1021. string chain[2];
  1022. string rule_keep[2];
  1023. string rule_keep_add[2];
  1024. string rule_keep_del[2];
  1025. u64_t keep_rule_last_time=0;
  1026. pthread_t keep_thread;
  1027. int keep_thread_running=0;
  1028. int iptables_gen_add(const char * s,u32_t const_id)
  1029. {
  1030. string dummy="";
  1031. iptables_pattern=s;
  1032. chain[0] =dummy+ "udp2rawDwrW_C";
  1033. rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
  1034. rule_keep_add[0]=iptables_command+"-I INPUT "+rule_keep[0];
  1035. char *output;
  1036. run_command(iptables_command+"-N "+chain[0],output,show_none);
  1037. run_command(iptables_command+"-F "+chain[0],output);
  1038. run_command(iptables_command+"-I "+chain[0] + " -j DROP",output);
  1039. rule_keep_del[0]=iptables_command+"-D INPUT "+rule_keep[0];
  1040. run_command(rule_keep_del[0],output,show_none);
  1041. run_command(rule_keep_del[0],output,show_none);
  1042. if(run_command(rule_keep_add[0],output)!=0)
  1043. {
  1044. mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[0].c_str());
  1045. myexit(-1);
  1046. }
  1047. return 0;
  1048. }
  1049. int iptables_rule_init(const char * s,u32_t const_id,int keep)
  1050. {
  1051. iptables_pattern=s;
  1052. iptables_rule_added=1;
  1053. iptables_rule_keeped=keep;
  1054. string dummy="";
  1055. char const_id_str[100];
  1056. sprintf(const_id_str, "%x", const_id);
  1057. chain[0] =dummy+ "udp2rawDwrW_"+const_id_str+"_C0";
  1058. chain[1] =dummy+ "udp2rawDwrW_"+const_id_str+"_C1";
  1059. rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
  1060. rule_keep[1]=dummy+ iptables_pattern+" -j " +chain[1];
  1061. rule_keep_add[0]=iptables_command+"-I INPUT "+rule_keep[0];
  1062. rule_keep_add[1]=iptables_command+"-I INPUT "+rule_keep[1];
  1063. rule_keep_del[0]=iptables_command+"-D INPUT "+rule_keep[0];
  1064. rule_keep_del[1]=iptables_command+"-D INPUT "+rule_keep[1];
  1065. keep_rule_last_time=get_current_time();
  1066. char *output;
  1067. for(int i=0;i<=iptables_rule_keeped;i++)
  1068. {
  1069. run_command(iptables_command+"-N "+chain[i],output);
  1070. run_command(iptables_command+"-F "+chain[i],output);
  1071. run_command(iptables_command+"-I "+chain[i] + " -j DROP",output);
  1072. if(run_command(rule_keep_add[i],output)!=0)
  1073. {
  1074. mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[i].c_str());
  1075. myexit(-1);
  1076. }
  1077. }
  1078. mylog(log_warn,"auto added iptables rules\n");
  1079. return 0;
  1080. }
  1081. int keep_iptables_rule() //magic to work on a machine without grep/iptables --check/-m commment
  1082. {
  1083. /*
  1084. if(iptables_rule_keeped==0) return 0;
  1085. uint64_t tmp_current_time=get_current_time();
  1086. if(tmp_current_time-keep_rule_last_time<=iptables_rule_keep_interval)
  1087. {
  1088. return 0;
  1089. }
  1090. else
  1091. {
  1092. keep_rule_last_time=tmp_current_time;
  1093. }*/
  1094. mylog(log_debug,"keep_iptables_rule begin %llu\n",get_current_time());
  1095. iptables_rule_keep_index+=1;
  1096. iptables_rule_keep_index%=2;
  1097. string dummy="";
  1098. char *output;
  1099. int i=iptables_rule_keep_index;
  1100. run_command(iptables_command + "-N " + chain[i], output,show_none);
  1101. if (run_command(iptables_command + "-F " + chain[i], output,show_none) != 0)
  1102. mylog(log_warn, "iptables -F failed %d\n",i);
  1103. if (run_command(iptables_command + "-I " + chain[i] + " -j DROP",output,show_none) != 0)
  1104. mylog(log_warn, "iptables -I failed %d\n",i);
  1105. if (run_command(rule_keep_del[i], output,show_none) != 0)
  1106. mylog(log_warn, "rule_keep_del failed %d\n",i);
  1107. run_command(rule_keep_del[i], output,show_none); //do it twice,incase it fails for unknown random reason
  1108. if(run_command(rule_keep_add[i], output,show_log)!=0)
  1109. mylog(log_warn, "rule_keep_del failed %d\n",i);
  1110. mylog(log_debug,"keep_iptables_rule end %llu\n",get_current_time());
  1111. return 0;
  1112. }
  1113. int clear_iptables_rule()
  1114. {
  1115. char *output;
  1116. string dummy="";
  1117. if(!iptables_rule_added) return 0;
  1118. for(int i=0;i<=iptables_rule_keeped;i++ )
  1119. {
  1120. run_command(rule_keep_del[i],output);
  1121. run_command(iptables_command+"-F "+chain[i],output);
  1122. run_command(iptables_command+"-X "+chain[i],output);
  1123. }
  1124. return 0;
  1125. }
  1126. void signal_handler(int sig)
  1127. {
  1128. about_to_exit=1;
  1129. // myexit(0);
  1130. }