diff --git a/.cproject b/.cproject index db8289e..6f343d0 100644 --- a/.cproject +++ b/.cproject @@ -1,8 +1,8 @@ - - + + @@ -14,28 +14,17 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + @@ -43,16 +32,6 @@ - - - - - - - - - - @@ -62,5 +41,8 @@ - + + + + diff --git a/common.cpp b/common.cpp index 2088e34..3fb6f2b 100644 --- a/common.cpp +++ b/common.cpp @@ -618,3 +618,73 @@ string trim(const string& str, char c) { size_t last = str.find_last_not_of(c); return str.substr(first,(last-first+1)); } + +vector parse_conf_line(const string& s) +{ + char buf[s.length()+200]; + char *p=buf; + int i=int(s.length())-1; + int j; + vectorres; + strcpy(buf,(char *)s.c_str()); + while(i>=0) + { + if(buf[i]==' ' || buf[i]== '\t') + buf[i]=0; + else break; + i--; + } + while(*p!=0) + { + if(*p==' ' || *p== '\t') + { + p++; + } + else break; + } + int new_len=strlen(p); + if(new_len==0)return res; + if(p[0]=='#') return res; + if(p[0]!='-') + { + mylog(log_fatal,"line :<%s> not begin with '-' ",s.c_str()); + myexit(-1); + } + + for(i=0;i > string_to_vec2(const char * s); string trim(const string& str, char c); +string trim_conf_line(const string& str); + +vector parse_conf_line(const string& s); + //extern string iptables_pattern; #endif /* COMMON_H_ */ diff --git a/main.cpp b/main.cpp index 45e5c42..947148b 100755 --- a/main.cpp +++ b/main.cpp @@ -575,6 +575,7 @@ int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,char *data,int data_len); int server_on_raw_recv_handshake1(conn_info_t &conn_info,char * ip_port,char * data, int data_len); +void process_arg(int argc, char *argv[]); int DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD; ////////////////=======================declear divider============================= @@ -2522,7 +2523,7 @@ void print_help() printf(" this option disables port changing while re-connecting\n"); // printf(" \n"); printf("other options:\n"); - printf(" --config-file read options from a configuration file instead of command line\n"); + printf(" --conf-file read options from a configuration file instead of command line\n"); printf(" --log-level 0:never 1:fatal 2:error 3:warn \n"); printf(" 4:info (default) 5:debug 6:trace\n"); // printf("\n"); @@ -2546,152 +2547,53 @@ void print_help() //printf("common options,these options must be same on both side\n"); } -void process_arg(int argc, char *argv[], bool read_config = true); -std::string trim_config_line(std::string line) -{ - auto str = trim(line, ' '); // Space - str = trim(str, ' '); // Tab - return str; -} -std::size_t find_config_divider(std::string line) -{ - std::size_t pos = line.find(" ",0); // Space - if(pos==std::string::npos) - { - pos = line.find(" ",0); // Tab - } - return pos; -} -void load_config(char *config_file, int argc_orig, char *argv_orig[]) + +int load_config(char *file_name, int &argc, vector &argv) { // Load configurations from config_file instead of the command line. // See config.example for example configurations - std::ifstream conf_file(config_file); + std::ifstream conf_file(file_name); std::string line; - std::vector arguments; + if(conf_file.fail()) + { + mylog(log_fatal,"conf_file %s open failed,reason :%s\n",file_name,strerror(errno)); + myexit(-1); + } while(std::getline(conf_file,line)) { - line = trim_config_line(line); - if(line==""||line.at(0)=='#') + auto res=parse_conf_line(line); + + argc+=res.size(); + for(int i=0;i<(int)res.size();i++) { - continue; - } - auto pos = find_config_divider(line); - if(pos==std::string::npos) - { - arguments.push_back(line); - } - else - { - auto p1 = line.substr(0,pos); - auto p2 = line.substr(pos+1,line.length() - pos - 1); - arguments.push_back(trim_config_line(p1)); - arguments.push_back(trim_config_line(p2)); + argv.push_back(res[i]); } } conf_file.close(); - // Append the new arguments to the original argv - int argc = arguments.size() + argc_orig; - char *argv[argc]; - for(int i=0; i short_opts_map = { - {"-k","--key"}, - {"-a","--auto-rule"}, - {"-g","--gen-rule"}, - }; // Keep this in sync with the shortcuts - - int option_index = 0; - std::set checked_opts = {}; - for (i = 0; i < argc; i++) + printf("running unit test\n"); + vector conf_lines= {"---aaa","--aaa bbb","-a bbb"," \t \t \t-a\t \t \t bbbbb\t \t \t "}; + for(int i=0;isecond; - } - if(checked_opts.find(opt)!=checked_opts.end()) - { - char *err_msg = new char(255); - sprintf(err_msg,"Duplicate argument %s",opt.c_str()); - log_bare(log_fatal,err_msg); - myexit(-1); - } - checked_opts.insert(opt); + printf("<%s>",res[j].c_str()); } + printf("\n"); } - if (argc == 1) - { - print_help(); - myexit(-1); - } + return 0; +} +int process_log_level(int argc,char *argv[]) +{ + int i,j,k; for (i = 0; i < argc; i++) { if(strcmp(argv[i],"--log-level")==0) @@ -2714,18 +2616,119 @@ void process_arg(int argc, char *argv[], bool read_config) enable_log_color=0; } } + return 0; +} +void process_arg(int argc, char *argv[]) +{ + int i,j,k,opt; - mylog(log_info,"argc=%d ", argc); + int option_index = 0; + + char options[]="l:r:schk:ag"; + static struct option long_options[] = + { + /* These options set a flag. */ + {"source-ip", required_argument, 0, 1}, + {"source-port", required_argument, 0, 1}, + {"log-level", required_argument, 0, 1}, + {"key", required_argument, 0, 'k'}, + {"auth-mode", required_argument, 0, 1}, + {"cipher-mode", required_argument, 0, 1}, + {"raw-mode", required_argument, 0, 1}, + {"disable-color", no_argument, 0, 1}, + {"log-position", no_argument, 0, 1}, + {"disable-bpf", no_argument, 0, 1}, + {"disable-anti-replay", no_argument, 0, 1}, + {"auto-rule", no_argument, 0, 'a'}, + {"gen-rule", no_argument, 0, 'g'}, + {"gen-add", no_argument, 0, 1}, + {"debug", no_argument, 0, 1}, + {"clear", no_argument, 0, 1}, + {"simple-rule", no_argument, 0, 1}, + {"keep-rule", no_argument, 0, 1}, + {"lower-level", required_argument, 0, 1}, + {"sock-buf", required_argument, 0, 1}, + {"seq-mode", required_argument, 0, 1}, + {"conf-file", required_argument, 0, 1}, + {NULL, 0, 0, 0} + }; + + process_log_level(argc,argv); + + set all_options; + map shortcut_map; + + all_options.insert("--help"); + all_options.insert("-h"); + all_options.insert("--conf-file"); + string dummy=""; + for(i=0;i<(int)strlen(options);i++) + { + + char val=options[i]; + if( ( val>='0'&&val<='9') ||( val>='a'&&val<='z')||(val>='A'&&val<='Z')) + { + all_options.insert(dummy+'-'+val); + } + } + for(i=0;i='0'&&val<='9') ||( val>='a'&&val<='z')||(val>='A'&&val<='Z')) + { + shortcut_map[dummy+"--"+long_options[i].name]= dummy+"-"+ char(val); + } + all_options.insert(dummy+"--"+long_options[i].name); + } + + mylog(log_info,"argc=%d ", argc); for (i = 0; i < argc; i++) { log_bare(log_info, "%s ", argv[i]); } log_bare(log_info, "\n"); + //string dummy=""; + for(i=+1;i new_argv; + + int count=0; + int pos=-1; + + for (i = 0; i < argc; i++) + { + if(strcmp(argv[i],"--conf-file")==0) + { + count++; + pos=i; + if(i==argc) + { + mylog(log_fatal,"--conf-file need a parameter\n"); + myexit(-1); + } + if(argv[i+1][1]=='-') + { + mylog(log_fatal,"--conf-file need a parameter\n"); + myexit(-1); + } + i++; + } + else + { + //printf("<%s>",argv[i]); + new_argc++; + new_argv.push_back(argv[i]); + } + } + if(count>1) + { + mylog(log_fatal,"duplicated --conf-file option\n"); + myexit(-1); + } + + if(count>0) + { + load_config(argv[pos+1],new_argc,new_argv); + } + char* new_argv_char[new_argv.size()]; + + new_argc=0; + for(i=0;i<(int)new_argv.size();i++) + { + if(strcmp(new_argv[i].c_str(),"--conf-file")==0) + { + mylog(log_fatal,"cant have --conf-file in a config file\n"); + myexit(-1); + } + new_argv_char[new_argc++]=(char *)new_argv[i].c_str(); + } + process_arg(new_argc,new_argv_char); + +} void *run_keep(void *none) { @@ -3168,7 +3258,7 @@ int main(int argc, char *argv[]) signal(SIGTERM, signal_handler); signal(SIGQUIT, signal_handler); - process_arg(argc,argv); + pre_process_arg(argc,argv); if(geteuid() != 0) { diff --git a/makefile b/makefile index b4c01ce..c9efe39 100755 --- a/makefile +++ b/makefile @@ -13,10 +13,10 @@ TAR=${NAME}_binaries.tar.gz ${NAME}_amd64 ${NAME}_x86 ${NAME}_x86_asm_aes ${NA all: rm -f ${NAME} - ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -static -O3 + ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -ggdb -static -O3 fast: rm -f ${NAME} - ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt + ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -ggdb debug: rm -f ${NAME} ${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -Wformat-nonliteral -D MY_DEBUG