misc.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 and easy-faketcp in client mode\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. {"no-pcap-mutex", no_argument, 0, 1},
  260. {NULL, 0, 0, 0}
  261. };
  262. process_log_level(argc,argv);
  263. set<string> all_options;
  264. map<string,string> shortcut_map;
  265. all_options.insert("--help");
  266. all_options.insert("-h");
  267. string dummy="";
  268. for(i=0;i<(int)strlen(options);i++)
  269. {
  270. char val=options[i];
  271. if( ( val>='0'&&val<='9') ||( val>='a'&&val<='z')||(val>='A'&&val<='Z'))
  272. {
  273. all_options.insert(dummy+'-'+val);
  274. }
  275. }
  276. for(i=0;i<int( sizeof(long_options)/sizeof(long_options[0]) );i++)
  277. {
  278. if(long_options[i].name==NULL) break;
  279. int val=long_options[i].val;
  280. if( ( val>='0'&&val<='9') ||( val>='a'&&val<='z')||(val>='A'&&val<='Z'))
  281. {
  282. shortcut_map[dummy+"--"+long_options[i].name]= dummy+"-"+ char(val);
  283. }
  284. all_options.insert(dummy+"--"+long_options[i].name);
  285. }
  286. for (i = 0; i < argc; i++)
  287. {
  288. int len=strlen(argv[i]);
  289. if(len==0)
  290. {
  291. mylog(log_fatal,"found an empty string in options\n");
  292. myexit(-1);
  293. }
  294. if(len==1&&argv[i][0]=='-' )
  295. {
  296. mylog(log_fatal,"invaild option '-' in argv\n");
  297. myexit(-1);
  298. }
  299. if(len==2&&argv[i][0]=='-'&&argv[i][1]=='-' )
  300. {
  301. mylog(log_fatal,"invaild option '--' in argv\n");
  302. myexit(-1);
  303. }
  304. }
  305. mylog(log_info,"argc=%d ", argc);
  306. for (i = 0; i < argc; i++) {
  307. log_bare(log_info, "%s ", argv[i]);
  308. }
  309. log_bare(log_info, "\n");
  310. //string dummy="";
  311. for(i=+1;i<argc;i++)
  312. {
  313. if(argv[i][0]!='-') continue;
  314. string a=argv[i];
  315. if(a[0]=='-'&&a[1]!='-')
  316. a=dummy+a[0]+a[1];
  317. if(all_options.find(a.c_str())==all_options.end())
  318. {
  319. mylog(log_fatal,"invaild option %s\n",a.c_str());
  320. myexit(-1);
  321. }
  322. for(j=i+1;j<argc;j++)
  323. {
  324. if(argv[j][0]!='-') continue;
  325. string b=argv[j];
  326. if(b[0]=='-'&&b[1]!='-')
  327. b=dummy+b[0]+b[1];
  328. if(shortcut_map.find(a)!=shortcut_map.end())
  329. a=shortcut_map[a];
  330. if(shortcut_map.find(b)!=shortcut_map.end())
  331. b=shortcut_map[b];
  332. if(a==b)
  333. {
  334. mylog(log_fatal,"%s duplicates with %s\n",argv[i],argv[j]);
  335. myexit(-1);
  336. }
  337. }
  338. }
  339. int no_l = 1, no_r = 1;
  340. while ((opt = getopt_long(argc, argv,options,long_options,&option_index)) != -1) {
  341. //string opt_key;
  342. //opt_key+=opt;
  343. switch (opt) {
  344. case 'l':
  345. no_l = 0;
  346. local_addr.from_str(optarg);
  347. if(local_addr.get_port()==22)
  348. {
  349. mylog(log_fatal,"port 22 not allowed\n");
  350. myexit(-1);
  351. }
  352. /*
  353. if (strchr(optarg, ':') != 0) {
  354. sscanf(optarg, "%[^:]:%d", local_ip, &local_port);
  355. if(local_port==22)
  356. {
  357. mylog(log_fatal,"port 22 not allowed\n");
  358. myexit(-1);
  359. }
  360. } else {
  361. mylog(log_fatal,"invalid parameter for -l ,%s,should be ip:port\n",optarg);
  362. myexit(-1);
  363. }*/
  364. break;
  365. case 'r':
  366. no_r = 0;
  367. remote_addr.from_str(optarg);
  368. if(remote_addr.get_port()==22)
  369. {
  370. mylog(log_fatal,"port 22 not allowed\n");
  371. myexit(-1);
  372. }
  373. /*
  374. if (strchr(optarg, ':') != 0) {
  375. sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
  376. if(remote_port==22)
  377. {
  378. mylog(log_fatal,"port 22 not allowed\n");
  379. myexit(-1);
  380. }
  381. } else {
  382. mylog(log_fatal,"invalid parameter for -r ,%s,should be ip:port\n",optarg);
  383. myexit(-1);
  384. }*/
  385. break;
  386. case 's':
  387. if(program_mode==0)
  388. {
  389. program_mode=server_mode;
  390. }
  391. else
  392. {
  393. mylog(log_fatal,"-s /-c has already been set,conflict\n");
  394. myexit(-1);
  395. }
  396. break;
  397. case 'c':
  398. if(program_mode==0)
  399. {
  400. program_mode=client_mode;
  401. }
  402. else
  403. {
  404. mylog(log_fatal,"-s /-c has already been set,conflict\n");
  405. myexit(-1);
  406. }
  407. break;
  408. case 'h':
  409. break;
  410. case 'a':
  411. mylog(log_fatal,"-a not supported in this version, check -g or --raw-mode easyfaketcp\n");
  412. myexit(-1);
  413. auto_add_iptables_rule=1;
  414. break;
  415. case 'g':
  416. generate_iptables_rule=1;
  417. break;
  418. case 'k':
  419. mylog(log_debug,"parsing key option\n");
  420. sscanf(optarg,"%s",key_string);
  421. break;
  422. case 1:
  423. mylog(log_debug,"option_index: %d\n",option_index);
  424. if(strcmp(long_options[option_index].name,"clear")==0)
  425. {
  426. mylog(log_fatal,"--clear not supported in this version\n");
  427. myexit(-1);
  428. clear_iptables=1;
  429. }
  430. else if(strcmp(long_options[option_index].name,"source-ip")==0)
  431. {
  432. mylog(log_debug,"parsing long option :source-ip\n");
  433. //sscanf(optarg, "%s", source_ip);
  434. source_addr.from_str_ip_only(optarg);
  435. mylog(log_debug,"source: %s\n",source_addr.get_ip());
  436. force_source_ip=1;
  437. }
  438. else if(strcmp(long_options[option_index].name,"source-port")==0)
  439. {
  440. mylog(log_debug,"parsing long option :source-port\n");
  441. sscanf(optarg, "%d", &source_port);
  442. mylog(log_info,"source: %d\n",source_port);
  443. force_source_port=1;
  444. }
  445. else if(strcmp(long_options[option_index].name,"raw-mode")==0)
  446. {
  447. /*
  448. for(i=0;i<mode_end;i++)
  449. {
  450. if(strcmp(optarg,raw_mode_tostring[i])==0)
  451. {
  452. //printf("%d i\n",i);
  453. //printf("%s",raw_mode_tostring[i]);
  454. raw_mode=(raw_mode_t)i;
  455. break;
  456. }
  457. }
  458. if(i==mode_end)
  459. {
  460. mylog(log_fatal,"no such raw_mode %s\n",optarg);
  461. myexit(-1);
  462. }
  463. */
  464. if(strcmp(optarg,"easyfaketcp")==0||strcmp(optarg,"easy_faketcp")==0||strcmp(optarg,"easy-faketcp")==0)
  465. {
  466. raw_mode=mode_faketcp;
  467. use_tcp_dummy_socket=1;
  468. }
  469. else
  470. {
  471. for(i=0;i<mode_end;i++)
  472. {
  473. if(strcmp(optarg,raw_mode_tostring[i])==0)
  474. {
  475. //printf("%d i\n",i);
  476. //printf("%s",raw_mode_tostring[i]);
  477. raw_mode=(raw_mode_t)i;
  478. break;
  479. }
  480. }
  481. if(i==mode_end)
  482. {
  483. mylog(log_fatal,"no such raw_mode %s\n",optarg);
  484. myexit(-1);
  485. }
  486. }
  487. }
  488. else if(strcmp(long_options[option_index].name,"auth-mode")==0)
  489. {
  490. for(i=0;i<auth_end;i++)
  491. {
  492. if(strcmp(optarg,auth_mode_tostring[i])==0)
  493. {
  494. auth_mode=(auth_mode_t)i;
  495. if(auth_mode==auth_none)
  496. {
  497. disable_anti_replay=1;
  498. }
  499. break;
  500. }
  501. }
  502. if(i==auth_end)
  503. {
  504. mylog(log_fatal,"no such auth_mode %s\n",optarg);
  505. myexit(-1);
  506. }
  507. }
  508. else if(strcmp(long_options[option_index].name,"cipher-mode")==0)
  509. {
  510. for(i=0;i<cipher_end;i++)
  511. {
  512. if(strcmp(optarg,cipher_mode_tostring[i])==0)
  513. {
  514. cipher_mode=(cipher_mode_t)i;
  515. break;
  516. }
  517. }
  518. if(i==cipher_end)
  519. {
  520. mylog(log_fatal,"no such cipher_mode %s\n",optarg);
  521. myexit(-1);
  522. }
  523. }
  524. else if(strcmp(long_options[option_index].name,"log-level")==0)
  525. {
  526. }
  527. else if(strcmp(long_options[option_index].name,"lower-level")==0)
  528. {
  529. //process_lower_level_arg();
  530. mylog(log_fatal,"--lower-level not supported in this version\n");
  531. myexit(-1);
  532. //lower_level=1;
  533. //strcpy(lower_level_arg,optarg);
  534. }
  535. else if(strcmp(long_options[option_index].name,"simple-rule")==0)
  536. {
  537. mylog(log_fatal,"--simple-rule not supported in this version\n");
  538. myexit(-1);
  539. simple_rule=1;
  540. }
  541. else if(strcmp(long_options[option_index].name,"keep-rule")==0)
  542. {
  543. mylog(log_fatal,"--keep-rule not supported in this version\n");
  544. myexit(-1);
  545. keep_rule=1;
  546. }
  547. else if(strcmp(long_options[option_index].name,"gen-add")==0)
  548. {
  549. mylog(log_fatal,"--gen-add not supported in this version\n");
  550. myexit(-1);
  551. generate_iptables_rule_add=1;
  552. }
  553. else if(strcmp(long_options[option_index].name,"disable-color")==0)
  554. {
  555. //enable_log_color=0;
  556. }
  557. else if(strcmp(long_options[option_index].name,"enable-color")==0)
  558. {
  559. //enable_log_color=0;
  560. }
  561. else if(strcmp(long_options[option_index].name,"debug")==0)
  562. {
  563. debug_flag=1;
  564. //enable_log_color=0;
  565. }
  566. else if(strcmp(long_options[option_index].name,"dev")==0)
  567. {
  568. sscanf(optarg,"%s",dev);
  569. //enable_log_color=0;
  570. mylog(log_info,"dev=[%s]\n",dev);
  571. }
  572. else if(strcmp(long_options[option_index].name,"debug-resend")==0)
  573. {
  574. //debug_resend=1;
  575. //enable_log_color=0;
  576. }
  577. else if(strcmp(long_options[option_index].name,"log-position")==0)
  578. {
  579. //enable_log_position=1;
  580. }
  581. else if(strcmp(long_options[option_index].name,"force-sock-buf")==0)
  582. {
  583. mylog(log_fatal,"--force-sock-buf not supported in this version\n");
  584. myexit(-1);
  585. force_socket_buf=1;
  586. }
  587. else if(strcmp(long_options[option_index].name,"retry-on-error")==0)
  588. {
  589. retry_on_error=1;
  590. }
  591. else if(strcmp(long_options[option_index].name,"wait-lock")==0)
  592. {
  593. wait_xtables_lock=1;
  594. }
  595. else if(strcmp(long_options[option_index].name,"disable-bpf")==0)
  596. {
  597. disable_bpf_filter=1;
  598. }
  599. else if(strcmp(long_options[option_index].name,"disable-anti-replay")==0)
  600. {
  601. disable_anti_replay=1;
  602. }
  603. else if(strcmp(long_options[option_index].name,"sock-buf")==0)
  604. {
  605. int tmp=-1;
  606. sscanf(optarg,"%d",&tmp);
  607. if(10<=tmp&&tmp<=10*1024)
  608. {
  609. socket_buf_size=tmp*1024;
  610. }
  611. else
  612. {
  613. mylog(log_fatal,"sock-buf value must be between 1 and 10240 (kbyte) \n");
  614. myexit(-1);
  615. }
  616. }
  617. else if(strcmp(long_options[option_index].name,"seq-mode")==0)
  618. {
  619. sscanf(optarg,"%d",&seq_mode);
  620. if(0<=seq_mode&&seq_mode<=max_seq_mode)
  621. {
  622. }
  623. else
  624. {
  625. mylog(log_fatal,"seq_mode value must be 0,1,or 2 \n");
  626. myexit(-1);
  627. }
  628. }
  629. else if(strcmp(long_options[option_index].name,"random-drop")==0)
  630. {
  631. sscanf(optarg,"%d",&random_drop);
  632. if(random_drop<0||random_drop>10000)
  633. {
  634. mylog(log_fatal,"random_drop must be between 0 10000 \n");
  635. myexit(-1);
  636. }
  637. mylog(log_info,"random_drop =%d \n",random_drop);
  638. }
  639. else if(strcmp(long_options[option_index].name,"fifo")==0)
  640. {
  641. mylog(log_fatal,"--fifo not supported in this version\n");
  642. myexit(-1);
  643. sscanf(optarg,"%s",fifo_file);
  644. mylog(log_info,"fifo_file =%s \n",fifo_file);
  645. }
  646. else if(strcmp(long_options[option_index].name,"conf-file")==0)
  647. {
  648. mylog(log_info,"configuration loaded from %s\n",optarg);
  649. }
  650. else if(strcmp(long_options[option_index].name,"hb-mode")==0)
  651. {
  652. sscanf(optarg,"%d",&hb_mode);
  653. assert(hb_mode==0||hb_mode==1);
  654. mylog(log_info,"hb_mode =%d \n",hb_mode);
  655. }
  656. else if(strcmp(long_options[option_index].name,"hb-len")==0)
  657. {
  658. sscanf(optarg,"%d",&hb_len);
  659. assert(hb_len>=0&&hb_len<=1500);
  660. mylog(log_info,"hb_len =%d \n",hb_len);
  661. }
  662. else if(strcmp(long_options[option_index].name,"mtu-warn")==0)
  663. {
  664. sscanf(optarg,"%d",&mtu_warn);
  665. assert(mtu_warn>0);
  666. mylog(log_info,"mtu_warn=%d \n",mtu_warn);
  667. }
  668. else if(strcmp(long_options[option_index].name,"max-rst-to-show")==0)
  669. {
  670. sscanf(optarg,"%d",&max_rst_to_show);
  671. assert(max_rst_to_show>=-1);
  672. mylog(log_info,"max_rst_to_show=%d \n",max_rst_to_show);
  673. }
  674. else if(strcmp(long_options[option_index].name,"max-rst-allowed")==0)
  675. {
  676. sscanf(optarg,"%d",&max_rst_allowed);
  677. assert(max_rst_allowed>=-1);
  678. mylog(log_info,"max_rst_allowed=%d \n",max_rst_allowed);
  679. }
  680. else if(strcmp(long_options[option_index].name,"set-ttl")==0)
  681. {
  682. sscanf(optarg,"%d",&ttl_value);
  683. assert(ttl_value>=0&&ttl_value<=255);
  684. mylog(log_info,"ttl_value=%d\n",ttl_value);
  685. }
  686. else if(strcmp(long_options[option_index].name,"dns-resolve")==0) // currently not used
  687. {
  688. enable_dns_resolve=1;
  689. mylog(log_info,"dns-resolve enabled\n");
  690. }
  691. else if(strcmp(long_options[option_index].name,"pcap-send")==0)
  692. {
  693. send_with_pcap=1;
  694. mylog(log_info,"--pcap-send enabled, now pcap will be used for sending packet instead of libnet\n");
  695. }
  696. else if(strcmp(long_options[option_index].name,"no-pcap-mutex")==0)
  697. {
  698. use_pcap_mutex=9;
  699. mylog(log_warn,"--no-pcap-mutex enabled, we will assume the underlying pcap calls are threadsafe\n");
  700. }
  701. else if(strcmp(long_options[option_index].name,"easy-tcp")==0)
  702. {
  703. use_tcp_dummy_socket=1;
  704. mylog(log_info,"--easy-tcp enabled, now a dummy tcp socket will be created for handshake and block rst\n");
  705. }
  706. else
  707. {
  708. mylog(log_warn,"ignored unknown long option ,option_index:%d code:<%x>\n",option_index, optopt);
  709. }
  710. break;
  711. default:
  712. mylog(log_fatal,"unknown option ,code:<%c>,<%x>\n",optopt, optopt);
  713. myexit(-1);
  714. }
  715. }
  716. if (no_l)
  717. mylog(log_fatal,"error: -l not found\n");
  718. if (no_r)
  719. mylog(log_fatal,"error: -r not found\n");
  720. if(program_mode==0)
  721. mylog(log_fatal,"error: -c /-s hasnt been set\n");
  722. if (no_l || no_r||program_mode==0)
  723. {
  724. print_help();
  725. myexit(-1);
  726. }
  727. if(program_mode==client_mode)
  728. {
  729. raw_ip_version=remote_addr.get_type();
  730. }
  731. else
  732. {
  733. raw_ip_version=local_addr.get_type();
  734. }
  735. if(auto_add_iptables_rule&& use_tcp_dummy_socket)
  736. {
  737. mylog(log_error,"-a,--auto-rule is not supposed to be used with easyfaketcp mode, you are likely making a mistake, but we can try to continue\n");
  738. }
  739. if(keep_rule&& use_tcp_dummy_socket)
  740. {
  741. mylog(log_error,"--keep-rule is not supposed to be used with easyfaketcp mode, you are likely making a mistake, but we can try to continue\n");
  742. }
  743. mylog(log_info,"important variables: ");
  744. log_bare(log_info,"log_level=%d:%s ",log_level,log_text[log_level]);
  745. log_bare(log_info,"raw_mode=%s ",raw_mode_tostring[raw_mode]);
  746. log_bare(log_info,"cipher_mode=%s ",cipher_mode_tostring[cipher_mode]);
  747. log_bare(log_info,"auth_mode=%s ",auth_mode_tostring[auth_mode]);
  748. log_bare(log_info,"key=%s ",key_string);
  749. log_bare(log_info,"local_addr=%s ",local_addr.get_str());
  750. log_bare(log_info,"remote_addr=%s ",remote_addr.get_str());
  751. if(force_source_ip)
  752. log_bare(log_info,"source_addr=%s ",source_addr.get_ip());
  753. if(force_source_port)
  754. log_bare(log_info,"source_port=%d ",source_port);
  755. log_bare(log_info,"socket_buf_size=%d ",socket_buf_size);
  756. log_bare(log_info,"\n");
  757. }
  758. void pre_process_arg(int argc, char *argv[])//mainly for load conf file
  759. {
  760. int i,j,k;
  761. for (i = 0; i < argc; i++)
  762. {
  763. if(strcmp(argv[i],"--unit-test")==0)
  764. {
  765. unit_test();
  766. myexit(0);
  767. }
  768. }
  769. for (i = 0; i < argc; i++)
  770. {
  771. if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0)
  772. {
  773. print_help();
  774. myexit(0);
  775. }
  776. }
  777. if (argc == 1)
  778. {
  779. print_help();
  780. myexit(-1);
  781. }
  782. process_log_level(argc,argv);
  783. int new_argc=0;
  784. vector<string> new_argv;
  785. int count=0;
  786. int pos=-1;
  787. for (i = 0; i < argc; i++)
  788. {
  789. if(strcmp(argv[i],"--conf-file")==0)
  790. {
  791. count++;
  792. pos=i;
  793. if(i==argc)
  794. {
  795. mylog(log_fatal,"--conf-file need a parameter\n");
  796. myexit(-1);
  797. }
  798. if(argv[i+1][0]=='-')
  799. {
  800. mylog(log_fatal,"--conf-file need a parameter\n");
  801. myexit(-1);
  802. }
  803. i++;
  804. }
  805. else
  806. {
  807. //printf("<%s>",argv[i]);
  808. new_argc++;
  809. new_argv.push_back(argv[i]);
  810. }
  811. }
  812. if(count>1)
  813. {
  814. mylog(log_fatal,"duplicated --conf-file option\n");
  815. myexit(-1);
  816. }
  817. if(count>0)
  818. {
  819. load_config(argv[pos+1],new_argc,new_argv);
  820. }
  821. char* new_argv_char[new_argv.size()];
  822. new_argc=0;
  823. for(i=0;i<(int)new_argv.size();i++)
  824. {
  825. if(strcmp(new_argv[i].c_str(),"--conf-file")==0)
  826. {
  827. mylog(log_fatal,"cant have --conf-file in a config file\n");
  828. myexit(-1);
  829. }
  830. new_argv_char[new_argc++]=(char *)new_argv[i].c_str();
  831. }
  832. process_arg(new_argc,new_argv_char);
  833. }
  834. /*
  835. void *run_keep(void *none) //called in a new thread for --keep-rule option
  836. {
  837. while(1)
  838. {
  839. sleep(iptables_rule_keep_interval);
  840. keep_iptables_rule();
  841. if(about_to_exit) //just incase it runs forever if there is some bug,not necessary
  842. {
  843. sleep(10);
  844. keep_thread_running=0; //not thread safe ,but wont cause problem
  845. break;
  846. }
  847. }
  848. return NULL;
  849. }
  850. void iptables_rule() // handles -a -g --gen-add --keep-rule --clear --wait-lock
  851. {
  852. assert(raw_ip_version==AF_INET||raw_ip_version==AF_INET6);
  853. if(raw_ip_version==AF_INET)
  854. {
  855. iptables_command0="iptables ";
  856. }
  857. else
  858. iptables_command0="ip6tables ";
  859. if(!wait_xtables_lock)
  860. {
  861. iptables_command=iptables_command0;
  862. }
  863. else
  864. {
  865. iptables_command=iptables_command0+"-w ";
  866. }
  867. if(clear_iptables)
  868. {
  869. char *output;
  870. //int ret =system("iptables-save |grep udp2raw_dWRwMnJhdw|sed -n 's/^-A/iptables -D/p'|sh");
  871. int ret =run_command(iptables_command+"-S|sed -n '/udp2rawDwrW/p'|sed -n 's/^-A/"+iptables_command+"-D/p'|sh",output);
  872. int ret2 =run_command(iptables_command+"-S|sed -n '/udp2rawDwrW/p'|sed -n 's/^-N/"+iptables_command+"-X/p'|sh",output);
  873. //system("iptables-save |grep udp2raw_dWRwMnJhdw|sed 's/^-A/iptables -D/'|sh");
  874. //system("iptables-save|grep -v udp2raw_dWRwMnJhdw|iptables-restore");
  875. mylog(log_info,"tried to clear all iptables rule created previously,return value %d %d\n",ret,ret2);
  876. myexit(-1);
  877. }
  878. if(auto_add_iptables_rule&&generate_iptables_rule)
  879. {
  880. mylog(log_warn," -g overrides -a\n");
  881. auto_add_iptables_rule=0;
  882. //myexit(-1);
  883. }
  884. if(generate_iptables_rule_add&&generate_iptables_rule)
  885. {
  886. mylog(log_warn," --gen-add overrides -g\n");
  887. generate_iptables_rule=0;
  888. //myexit(-1);
  889. }
  890. if(keep_rule&&auto_add_iptables_rule==0)
  891. {
  892. auto_add_iptables_rule=1;
  893. mylog(log_warn," --keep_rule implys -a\n");
  894. generate_iptables_rule=0;
  895. //myexit(-1);
  896. }
  897. char tmp_pattern[200];
  898. string pattern="";
  899. if(program_mode==client_mode)
  900. {
  901. tmp_pattern[0]=0;
  902. if(raw_mode==mode_faketcp)
  903. {
  904. sprintf(tmp_pattern,"-s %s -p tcp -m tcp --sport %d",remote_addr.get_ip(),remote_addr.get_port());
  905. }
  906. if(raw_mode==mode_udp)
  907. {
  908. sprintf(tmp_pattern,"-s %s -p udp -m udp --sport %d",remote_addr.get_ip(),remote_addr.get_port());
  909. }
  910. if(raw_mode==mode_icmp)
  911. {
  912. if(raw_ip_version==AF_INET)
  913. sprintf(tmp_pattern,"-s %s -p icmp --icmp-type 0",remote_addr.get_ip());
  914. else
  915. sprintf(tmp_pattern,"-s %s -p icmpv6 --icmpv6-type 129",remote_addr.get_ip());
  916. }
  917. pattern+=tmp_pattern;
  918. }
  919. if(program_mode==server_mode)
  920. {
  921. tmp_pattern[0]=0;
  922. if(raw_ip_version==AF_INET)
  923. {
  924. if(local_addr.inner.ipv4.sin_addr.s_addr!=0)
  925. {
  926. sprintf(tmp_pattern,"-d %s ",local_addr.get_ip());
  927. }
  928. }
  929. else
  930. {
  931. char zero_arr[16]={0};
  932. if(memcmp(&local_addr.inner.ipv6.sin6_addr,zero_arr,16)!=0)
  933. {
  934. sprintf(tmp_pattern,"-d %s ",local_addr.get_ip());
  935. }
  936. }
  937. pattern+=tmp_pattern;
  938. tmp_pattern[0]=0;
  939. if(raw_mode==mode_faketcp)
  940. {
  941. sprintf(tmp_pattern,"-p tcp -m tcp --dport %d",local_addr.get_port());
  942. }
  943. if(raw_mode==mode_udp)
  944. {
  945. sprintf(tmp_pattern,"-p udp -m udp --dport %d",local_addr.get_port());
  946. }
  947. if(raw_mode==mode_icmp)
  948. {
  949. if(raw_ip_version==AF_INET)
  950. sprintf(tmp_pattern,"-p icmp --icmp-type 8");
  951. else
  952. sprintf(tmp_pattern,"-p icmpv6 --icmpv6-type 128");
  953. }
  954. pattern+=tmp_pattern;
  955. }
  956. if(generate_iptables_rule)
  957. {
  958. string rule=iptables_command+"-I INPUT ";
  959. rule+=pattern;
  960. rule+=" -j DROP";
  961. printf("generated iptables rule:\n");
  962. printf("%s\n",rule.c_str());
  963. myexit(0);
  964. }
  965. if(generate_iptables_rule_add)
  966. {
  967. iptables_gen_add(pattern.c_str(),const_id);
  968. myexit(0);
  969. }
  970. if(auto_add_iptables_rule)
  971. {
  972. iptables_rule_init(pattern.c_str(),const_id,keep_rule);
  973. if(keep_rule)
  974. {
  975. if(pthread_create(&keep_thread, NULL, run_keep, 0)) {
  976. mylog(log_fatal, "Error creating thread\n");
  977. myexit(-1);
  978. }
  979. keep_thread_running=1;
  980. }
  981. }
  982. else
  983. {
  984. mylog(log_warn," -a has not been set, make sure you have added the needed iptables rules manually\n");
  985. }
  986. }
  987. */
  988. int unit_test()
  989. {
  990. printf("running unit test\n");
  991. vector<string> conf_lines= {"---aaa","--aaa bbb","-a bbb"," \t \t \t-a\t \t \t bbbbb\t \t \t "};
  992. for(int i=0;i<int(conf_lines.size());i++)
  993. {
  994. printf("orign:%s\n",conf_lines[i].c_str());
  995. auto res=parse_conf_line(conf_lines[i]);
  996. printf("pasrse_result: size %d",int(res.size()));
  997. for(int j=0;j<int(res.size());j++)
  998. {
  999. printf("<%s>",res[j].c_str());
  1000. }
  1001. printf("\n");
  1002. }
  1003. char s1[]={1,2,3,4,5};
  1004. char s2[]={1};
  1005. short c1=csum((unsigned short*)s1,5);
  1006. short c2=csum((unsigned short*)s2,1);
  1007. //c2=0;
  1008. printf("%x %x\n",(int)c1,(int)c2);
  1009. const char buf[]={1,2,3,4,5,6,7,8,9,10,11,2,13,14,15,16};
  1010. char key[100]={0};
  1011. char buf2[100]={0};
  1012. char buf3[100]={0};
  1013. char buf4[100]={0};
  1014. int len=16;
  1015. for(int i=0;i<len;i++)
  1016. {
  1017. printf("<%d>",buf[i]);
  1018. }
  1019. printf("\n");
  1020. cipher_encrypt(buf,buf2,len,key);
  1021. for(int i=0;i<len;i++)
  1022. {
  1023. printf("<%d>",buf2[i]);
  1024. }
  1025. printf("\n");
  1026. int temp_len=len;
  1027. cipher_decrypt(buf2,buf3,len,key);
  1028. for(int i=0;i<len;i++)
  1029. {
  1030. printf("<%d>",buf3[i]);
  1031. }
  1032. printf("\n");
  1033. cipher_encrypt(buf2,buf4,temp_len,key);
  1034. for(int i=0;i<temp_len;i++)
  1035. {
  1036. printf("<%d>",buf4[i]);
  1037. }
  1038. return 0;
  1039. }
  1040. /*
  1041. int set_timer(int epollfd,int &timer_fd)//put a timer_fd into epoll,general function,used both in client and server
  1042. {
  1043. int ret;
  1044. epoll_event ev;
  1045. itimerspec its;
  1046. memset(&its,0,sizeof(its));
  1047. if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  1048. {
  1049. mylog(log_fatal,"timer_fd create error\n");
  1050. myexit(1);
  1051. }
  1052. its.it_interval.tv_sec=(timer_interval/1000);
  1053. its.it_interval.tv_nsec=(timer_interval%1000)*1000ll*1000ll;
  1054. its.it_value.tv_nsec=1; //imidiately
  1055. timerfd_settime(timer_fd,0,&its,0);
  1056. ev.events = EPOLLIN;
  1057. ev.data.u64 = timer_fd;
  1058. ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
  1059. if (ret < 0) {
  1060. mylog(log_fatal,"epoll_ctl return %d\n", ret);
  1061. myexit(-1);
  1062. }
  1063. return 0;
  1064. }
  1065. int set_timer_server(int epollfd,int &timer_fd,fd64_t &fd64)//only for server
  1066. {
  1067. int ret;
  1068. epoll_event ev;
  1069. itimerspec its;
  1070. memset(&its,0,sizeof(its));
  1071. if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  1072. {
  1073. mylog(log_fatal,"timer_fd create error\n");
  1074. myexit(1);
  1075. }
  1076. its.it_interval.tv_sec=(timer_interval/1000);
  1077. its.it_interval.tv_nsec=(timer_interval%1000)*1000ll*1000ll;
  1078. its.it_value.tv_nsec=1; //imidiately
  1079. timerfd_settime(timer_fd,0,&its,0);
  1080. fd64=fd_manager.create(timer_fd);
  1081. ev.events = EPOLLIN;
  1082. ev.data.u64 = fd64;////difference
  1083. ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
  1084. if (ret < 0) {
  1085. mylog(log_fatal,"epoll_ctl return %d\n", ret);
  1086. myexit(-1);
  1087. }
  1088. return 0;
  1089. }
  1090. int handle_lower_level(raw_info_t &raw_info)//fill lower_level info,when --lower-level is enabled,only for server
  1091. {
  1092. packet_info_t &send_info=raw_info.send_info;
  1093. packet_info_t &recv_info=raw_info.recv_info;
  1094. if(lower_level_manual)
  1095. {
  1096. memset(&send_info.addr_ll,0,sizeof(send_info.addr_ll));
  1097. send_info.addr_ll.sll_family=AF_PACKET;
  1098. send_info.addr_ll.sll_ifindex=ifindex;
  1099. send_info.addr_ll.sll_halen=ETHER_ADDR_LEN;
  1100. send_info.addr_ll.sll_protocol=htons(ETH_P_IP);
  1101. memcpy(&send_info.addr_ll.sll_addr,dest_hw_addr,ETHER_ADDR_LEN);
  1102. mylog(log_debug,"[manual]lower level info %x %x\n ",send_info.addr_ll.sll_halen,send_info.addr_ll.sll_protocol);
  1103. }
  1104. else
  1105. {
  1106. memset(&send_info.addr_ll,0,sizeof(send_info.addr_ll));
  1107. send_info.addr_ll.sll_family=recv_info.addr_ll.sll_family;
  1108. send_info.addr_ll.sll_ifindex=recv_info.addr_ll.sll_ifindex;
  1109. send_info.addr_ll.sll_protocol=recv_info.addr_ll.sll_protocol;
  1110. send_info.addr_ll.sll_halen=recv_info.addr_ll.sll_halen;
  1111. memcpy(send_info.addr_ll.sll_addr,recv_info.addr_ll.sll_addr,sizeof(send_info.addr_ll.sll_addr));
  1112. //other bytes should be kept zero.
  1113. mylog(log_debug,"[auto]lower level info %x %x\n ",send_info.addr_ll.sll_halen,send_info.addr_ll.sll_protocol);
  1114. }
  1115. return 0;
  1116. }
  1117. string chain[2];
  1118. string rule_keep[2];
  1119. string rule_keep_add[2];
  1120. string rule_keep_del[2];
  1121. u64_t keep_rule_last_time=0;
  1122. pthread_t keep_thread;
  1123. int keep_thread_running=0;
  1124. int iptables_gen_add(const char * s,u32_t const_id)
  1125. {
  1126. string dummy="";
  1127. iptables_pattern=s;
  1128. chain[0] =dummy+ "udp2rawDwrW_C";
  1129. rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
  1130. rule_keep_add[0]=iptables_command+"-I INPUT "+rule_keep[0];
  1131. char *output;
  1132. run_command(iptables_command+"-N "+chain[0],output,show_none);
  1133. run_command(iptables_command+"-F "+chain[0],output);
  1134. run_command(iptables_command+"-I "+chain[0] + " -j DROP",output);
  1135. rule_keep_del[0]=iptables_command+"-D INPUT "+rule_keep[0];
  1136. run_command(rule_keep_del[0],output,show_none);
  1137. run_command(rule_keep_del[0],output,show_none);
  1138. if(run_command(rule_keep_add[0],output)!=0)
  1139. {
  1140. mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[0].c_str());
  1141. myexit(-1);
  1142. }
  1143. return 0;
  1144. }
  1145. int iptables_rule_init(const char * s,u32_t const_id,int keep)
  1146. {
  1147. iptables_pattern=s;
  1148. iptables_rule_added=1;
  1149. iptables_rule_keeped=keep;
  1150. string dummy="";
  1151. char const_id_str[100];
  1152. sprintf(const_id_str, "%x", const_id);
  1153. chain[0] =dummy+ "udp2rawDwrW_"+const_id_str+"_C0";
  1154. chain[1] =dummy+ "udp2rawDwrW_"+const_id_str+"_C1";
  1155. rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
  1156. rule_keep[1]=dummy+ iptables_pattern+" -j " +chain[1];
  1157. rule_keep_add[0]=iptables_command+"-I INPUT "+rule_keep[0];
  1158. rule_keep_add[1]=iptables_command+"-I INPUT "+rule_keep[1];
  1159. rule_keep_del[0]=iptables_command+"-D INPUT "+rule_keep[0];
  1160. rule_keep_del[1]=iptables_command+"-D INPUT "+rule_keep[1];
  1161. keep_rule_last_time=get_current_time();
  1162. char *output;
  1163. for(int i=0;i<=iptables_rule_keeped;i++)
  1164. {
  1165. run_command(iptables_command+"-N "+chain[i],output);
  1166. run_command(iptables_command+"-F "+chain[i],output);
  1167. run_command(iptables_command+"-I "+chain[i] + " -j DROP",output);
  1168. if(run_command(rule_keep_add[i],output)!=0)
  1169. {
  1170. mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[i].c_str());
  1171. myexit(-1);
  1172. }
  1173. }
  1174. mylog(log_warn,"auto added iptables rules\n");
  1175. return 0;
  1176. }
  1177. int keep_iptables_rule() //magic to work on a machine without grep/iptables --check/-m commment
  1178. {
  1179. mylog(log_debug,"keep_iptables_rule begin %llu\n",get_current_time());
  1180. iptables_rule_keep_index+=1;
  1181. iptables_rule_keep_index%=2;
  1182. string dummy="";
  1183. char *output;
  1184. int i=iptables_rule_keep_index;
  1185. run_command(iptables_command + "-N " + chain[i], output,show_none);
  1186. if (run_command(iptables_command + "-F " + chain[i], output,show_none) != 0)
  1187. mylog(log_warn, "iptables -F failed %d\n",i);
  1188. if (run_command(iptables_command + "-I " + chain[i] + " -j DROP",output,show_none) != 0)
  1189. mylog(log_warn, "iptables -I failed %d\n",i);
  1190. if (run_command(rule_keep_del[i], output,show_none) != 0)
  1191. mylog(log_warn, "rule_keep_del failed %d\n",i);
  1192. run_command(rule_keep_del[i], output,show_none); //do it twice,incase it fails for unknown random reason
  1193. if(run_command(rule_keep_add[i], output,show_log)!=0)
  1194. mylog(log_warn, "rule_keep_del failed %d\n",i);
  1195. mylog(log_debug,"keep_iptables_rule end %llu\n",get_current_time());
  1196. return 0;
  1197. }
  1198. int clear_iptables_rule()
  1199. {
  1200. char *output;
  1201. string dummy="";
  1202. if(!iptables_rule_added) return 0;
  1203. for(int i=0;i<=iptables_rule_keeped;i++ )
  1204. {
  1205. run_command(rule_keep_del[i],output);
  1206. run_command(iptables_command+"-F "+chain[i],output);
  1207. run_command(iptables_command+"-X "+chain[i],output);
  1208. }
  1209. return 0;
  1210. }
  1211. */
  1212. void iptables_rule() // handles -a -g --gen-add --keep-rule --clear --wait-lock
  1213. {
  1214. if(generate_iptables_rule)
  1215. {
  1216. if(raw_mode==mode_faketcp && use_tcp_dummy_socket==1)
  1217. {
  1218. mylog(log_fatal, "failed,-g doesnt work with easy-faketcp mode\n");
  1219. myexit(-1);
  1220. }
  1221. if(raw_mode==mode_udp)
  1222. {
  1223. mylog(log_warn, "It not necessary to use iptables/firewall rule in udp mode\n");
  1224. }
  1225. log_bare(log_warn,"for linux, use:\n");
  1226. if(raw_ip_version==AF_INET)
  1227. {
  1228. if(raw_mode==mode_faketcp)
  1229. printf("iptables -I INPUT -s %s -p tcp -m tcp --sport %d -j DROP\n",remote_addr.get_ip(),remote_addr.get_port());
  1230. if(raw_mode==mode_udp)
  1231. printf("iptables -I INPUT -s %s -p udp -m udp --sport %d -j DROP\n",remote_addr.get_ip(),remote_addr.get_port());
  1232. if(raw_mode==mode_icmp)
  1233. printf("iptables -I INPUT -s %s -p icmp --icmp-type 0 -j DROP\n",remote_addr.get_ip());
  1234. printf("\n");
  1235. }
  1236. else
  1237. {
  1238. assert(raw_ip_version==AF_INET6);
  1239. if(raw_mode==mode_faketcp)
  1240. printf("ip6tables -I INPUT -s %s -p tcp -m tcp --sport %d -j DROP\n",remote_addr.get_ip(),remote_addr.get_port());
  1241. if(raw_mode==mode_udp)
  1242. printf("ip6tables -I INPUT -s %s -p udp -m udp --sport %d -j DROP\n",remote_addr.get_ip(),remote_addr.get_port());
  1243. if(raw_mode==mode_icmp)
  1244. printf("ip6tables -I INPUT -s %s -p -p icmpv6 --icmpv6-type 129 -j DROP\n",remote_addr.get_ip());
  1245. printf("\n");
  1246. }
  1247. log_bare(log_warn,"for mac/bsd use:\n");
  1248. if(raw_ip_version==AF_INET)
  1249. {
  1250. if(raw_mode==mode_faketcp)
  1251. printf("echo 'block drop inet proto tcp from %s port %d to any' > ./1.conf\n",remote_addr.get_ip(),remote_addr.get_port());
  1252. if(raw_mode==mode_udp)
  1253. printf("echo 'block drop inet proto udp from %s port %d to any' > ./1.conf\n",remote_addr.get_ip(),remote_addr.get_port());
  1254. if(raw_mode==mode_icmp)
  1255. printf("echo 'block drop inet proto icmp from %s to any' > ./1.conf\n",remote_addr.get_ip());
  1256. }
  1257. else
  1258. {
  1259. assert(raw_ip_version==AF_INET6);
  1260. if(raw_mode==mode_faketcp)
  1261. printf("echo 'block drop inet6 proto tcp from %s port %d to any' > ./1.conf\n",remote_addr.get_ip(),remote_addr.get_port());
  1262. if(raw_mode==mode_udp)
  1263. printf("echo 'block drop inet6 proto udp from %s port %d to any' > ./1.conf\n",remote_addr.get_ip(),remote_addr.get_port());
  1264. if(raw_mode==mode_icmp)
  1265. printf("echo 'block drop inet6 proto icmp6 from %s to any' > ./1.conf\n",remote_addr.get_ip());
  1266. }
  1267. printf("pfctl -f ./1.conf\n");
  1268. printf("pfctl -e\n");
  1269. printf("\n");
  1270. log_bare(log_warn,"for windows vista and above use:\n");
  1271. if(raw_ip_version==AF_INET)
  1272. {
  1273. if(raw_mode==mode_faketcp)
  1274. {
  1275. 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());
  1276. 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());
  1277. }
  1278. if(raw_mode==mode_udp)
  1279. {
  1280. 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());
  1281. 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());
  1282. }
  1283. if(raw_mode==mode_icmp)
  1284. {
  1285. printf("netsh advfirewall firewall add rule name=udp2raw protocol=ICMPV4 dir=in remoteip=%s action=block\n",remote_addr.get_ip());
  1286. printf("netsh advfirewall firewall add rule name=udp2raw protocol=ICMPV4 dir=out remoteip=%s action=block\n",remote_addr.get_ip());
  1287. }
  1288. }
  1289. else
  1290. {
  1291. assert(raw_ip_version==AF_INET6);
  1292. if(raw_mode==mode_faketcp)
  1293. {
  1294. 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());
  1295. 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());
  1296. }
  1297. if(raw_mode==mode_udp)
  1298. {
  1299. 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());
  1300. 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());
  1301. }
  1302. if(raw_mode==mode_icmp)
  1303. {
  1304. printf("netsh advfirewall firewall add rule name=udp2raw protocol=ICMPV6 dir=in remoteip=%s action=block\n",remote_addr.get_ip());
  1305. printf("netsh advfirewall firewall add rule name=udp2raw protocol=ICMPV6 dir=out remoteip=%s action=block\n",remote_addr.get_ip());
  1306. }
  1307. }
  1308. myexit(0);
  1309. }
  1310. }
  1311. void signal_handler(int sig)
  1312. {
  1313. about_to_exit=1;
  1314. // myexit(0);
  1315. }