misc.cpp 34 KB

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