diff options
-rw-r--r-- | src/options.c | 33 | ||||
-rw-r--r-- | src/options.h | 4 | ||||
-rw-r--r-- | src/string_list.c | 55 | ||||
-rw-r--r-- | src/string_list.h | 8 | ||||
-rw-r--r-- | src/uanytun.c | 32 |
5 files changed, 96 insertions, 36 deletions
diff --git a/src/options.c b/src/options.c index 6e02649..01c6528 100644 --- a/src/options.c +++ b/src/options.c @@ -123,6 +123,20 @@ i++; \ } +#define PARSE_STRING_LIST(SHORT, LONG, LIST) \ + else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ + { \ + if(argc < 1 || argv[i+1][0] == '-') \ + return i; \ + int ret = string_list_add(&LIST, argv[i+1]); \ + if(ret == -2) \ + return ret; \ + else if(ret) \ + return i+1; \ + argc--; \ + i++; \ + } + int options_parse_hex_string(const char* hex, buffer_t* buffer) { if(!hex || !buffer) @@ -185,7 +199,6 @@ int options_parse_ifconfig(const char* arg, ifconfig_param_t* ifcfg) } } - printf("no / found\n"); free(str); return 1; } @@ -222,6 +235,7 @@ int options_parse(options_t* opt, int argc, char* argv[]) PARSE_STRING_PARAM("-i","--interface", opt->local_addr_) PARSE_STRING_PARAM("-p","--port", opt->local_port_) PARSE_INT_PARAM("-s","--sender-id", opt->sender_id_) + PARSE_STRING_LIST("-L","--log", opt->log_targets_) PARSE_STRING_PARAM("-r","--remote-host", opt->remote_addr_) PARSE_STRING_PARAM("-o","--remote-port", opt->remote_port_) PARSE_STRING_PARAM("-d","--dev", opt->dev_name_) @@ -246,6 +260,11 @@ int options_parse(options_t* opt, int argc, char* argv[]) return i; } + return 0; +} + +void options_parse_post(options_t* opt) +{ #ifndef NO_CRYPT if(!strcmp(opt->cipher_, "null") && !strcmp(opt->auth_algo_, "null") && strcmp(opt->kd_prf_, "null")) { @@ -261,8 +280,6 @@ int options_parse(options_t* opt, int argc, char* argv[]) if(!(opt->dev_name_) && !(opt->dev_type_)) opt->dev_type_ = strdup("tun"); - - return 0; } void options_default(options_t* opt) @@ -276,6 +293,7 @@ void options_default(options_t* opt) opt->groupname_ = NULL; opt->chroot_dir_ = NULL; opt->pid_file_ = NULL; + string_list_init(&opt->log_targets_); opt->local_addr_ = NULL; opt->local_port_ = strdup("4444"); opt->sender_id_ = 0; @@ -319,6 +337,7 @@ void options_clear(options_t* opt) free(opt->chroot_dir_); if(opt->pid_file_) free(opt->pid_file_); + string_list_clear(&opt->log_targets_); if(opt->local_addr_) free(opt->local_addr_); if(opt->local_port_) @@ -363,6 +382,9 @@ void options_print_usage() printf(" [-i|--interface] <ip-address> local ip address to bind to\n"); printf(" [-p|--port] <port> local port to bind to\n"); printf(" [-s|--sender-id ] <sender id> the sender id to use\n"); + printf(" [-L|--log] <target>:<level>[,<param1>[,<param2>..]]\n"); + printf(" add a log target, can be invoked several times\n"); + printf(" [-r|--remote-host] <hostname|ip> remote host\n"); printf(" [-o|--remote-port] <port> remote port\n"); printf(" [-d|--dev] <name> device name\n"); @@ -388,12 +410,17 @@ void options_print_usage() void options_print(options_t* opt) { + if(!opt) + return; + printf("progname: '%s'\n", opt->progname_); printf("daemonize: %d\n", opt->daemonize_); printf("username: '%s'\n", opt->username_); printf("groupname: '%s'\n", opt->groupname_); printf("chroot_dir: '%s'\n", opt->chroot_dir_); printf("pid_file: '%s'\n", opt->pid_file_); + printf("log_targets: \n"); + string_list_print(&opt->log_targets_, " '", "'\n"); printf("local_addr: '%s'\n", opt->local_addr_); printf("local_port: '%s'\n", opt->local_port_); printf("sender_id: %d\n", opt->sender_id_); diff --git a/src/options.h b/src/options.h index f18c254..609521f 100644 --- a/src/options.h +++ b/src/options.h @@ -35,6 +35,8 @@ #ifndef _OPTIONS_H_ #define _OPTIONS_H_ +#include "string_list.h" + struct ifconfig_param_struct { char* net_addr_; u_int16_t prefix_length_; @@ -48,6 +50,7 @@ struct options_struct { char* groupname_; char* chroot_dir_; char* pid_file_; + string_list_t log_targets_; char* local_addr_; char* local_port_; sender_id_t sender_id_; @@ -76,6 +79,7 @@ int options_parse_hex_string(const char* hex, buffer_t* buffer); int options_parse_ifconfig(const char* arg, ifconfig_param_t* ifcfg); int options_parse(options_t* opt, int argc, char* argv[]); +void options_parse_post(options_t* opt); void options_default(options_t* opt); void options_clear(options_t* opt); void options_print_usage(); diff --git a/src/string_list.c b/src/string_list.c index bb59ff6..f23fcdc 100644 --- a/src/string_list.c +++ b/src/string_list.c @@ -43,7 +43,7 @@ void string_list_init(string_list_t* list) if(!list) return; - list->first = NULL; + list->first_ = NULL; } void string_list_clear(string_list_t* list) @@ -51,12 +51,12 @@ void string_list_clear(string_list_t* list) if(!list) return; - while(list->first) { + while(list->first_) { string_list_element_t* tmp; - tmp = list->first; - list->first = tmp->next; - if(tmp->string) - free(tmp->string); + tmp = list->first_; + list->first_ = tmp->next_; + if(tmp->string_) + free(tmp->string_); free(tmp); } } @@ -66,40 +66,47 @@ int string_list_add(string_list_t* list, const char* string) if(!list) return -1; - if(!list->first) { - list->first = malloc(sizeof(string_list_element_t)); - if(!list->first) + if(!list->first_) { + list->first_ = malloc(sizeof(string_list_element_t)); + if(!list->first_) return -2; - list->first->next = 0; - list->first->string = strdup(string); - if(!list->first) + list->first_->next_ = 0; + list->first_->string_ = strdup(string); + if(!list->first_->string_) { + free(list->first_); + list->first_ = NULL; return -2; + } } else { - string_list_element_t* tmp = list->first; - while(tmp->next) - tmp = tmp->next; + string_list_element_t* tmp = list->first_; + while(tmp->next_) + tmp = tmp->next_; - tmp->next = malloc(sizeof(string_list_element_t)); - if(!tmp->next) + tmp->next_ = malloc(sizeof(string_list_element_t)); + if(!tmp->next_) return -2; - tmp->next->next = 0; - tmp->next->string = strdup(string); - if(!tmp->next) + tmp->next_->next_ = 0; + tmp->next_->string_ = strdup(string); + if(!tmp->next_->string_) { + free(list->first_); + list->first_ = NULL; return -2; + } } + return 0; } -void string_list_print(string_list_t* list) +void string_list_print(string_list_t* list, const char* head, const char* tail) { if(!list) return; - string_list_element_t* tmp = list->first; + string_list_element_t* tmp = list->first_; while(tmp) { - printf("%s\n",tmp->string); - tmp = tmp->next; + printf("%s%s%s", head, tmp->string_, tail); + tmp = tmp->next_; } } diff --git a/src/string_list.h b/src/string_list.h index 22d3eef..fc1f4ac 100644 --- a/src/string_list.h +++ b/src/string_list.h @@ -36,13 +36,13 @@ #define _STRING_LIST_H_ struct string_list_element_struct { - char* string; - struct string_list_element_struct* next; + char* string_; + struct string_list_element_struct* next_; }; typedef struct string_list_element_struct string_list_element_t; struct string_list_struct { - string_list_element_t* first; + string_list_element_t* first_; }; typedef struct string_list_struct string_list_t; @@ -50,6 +50,6 @@ void string_list_init(string_list_t* list); void string_list_clear(string_list_t* list); int string_list_add(string_list_t* list, const char* string); -void string_list_print(string_list_t* list); +void string_list_print(string_list_t* list, const char* head, const char* tail); #endif diff --git a/src/uanytun.c b/src/uanytun.c index 9080dd3..57c52f1 100644 --- a/src/uanytun.c +++ b/src/uanytun.c @@ -328,27 +328,49 @@ void print_hex_dump(const u_int8_t* buf, u_int32_t len) int main(int argc, char* argv[]) { log_init(); - log_add_target("syslog:3,uanytun,daemon"); - log_printf(NOTICE, "just started..."); options_t opt; int ret = options_parse(&opt, argc, argv); if(ret) { - options_clear(&opt); if(ret > 0) { fprintf(stderr, "syntax error near: %s\n\n", argv[ret]); - log_printf(ERROR, "syntax error, exitting"); } if(ret == -2) { fprintf(stderr, "memory error on options_parse, exitting\n"); - log_printf(ERROR, "memory error on options_parse, exitting"); } if(ret == -1 || ret > 0) options_print_usage(); + options_clear(&opt); + log_close(); exit(ret); } + string_list_element_t* tmp = opt.log_targets_.first_; + if(!tmp) { + log_add_target("syslog:3,uanytun,daemon"); + } + else { + while(tmp) { + ret = log_add_target(tmp->string_); + if(ret) { + switch(ret) { + case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break; + case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", tmp->string_); break; + case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", tmp->string_); break; + default: fprintf(stderr, "syntax error near: '%s', exitting\n", tmp->string_); break; + } + + options_clear(&opt); + log_close(); + exit(ret); + } + tmp = tmp->next_; + } + } + + log_printf(NOTICE, "just started..."); + options_parse_post(&opt); priv_info_t priv; if(opt.username_) |