misc.cpp 34 KB

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