misc.cpp 41 KB

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