diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/linux/tun.c | 46 | ||||
-rw-r--r-- | src/options.c | 80 | ||||
-rw-r--r-- | src/options.h | 10 | ||||
-rw-r--r-- | src/tun.h | 8 | ||||
-rw-r--r-- | src/tun_helper.h | 14 | ||||
-rw-r--r-- | src/uanytun.c | 2 |
6 files changed, 97 insertions, 63 deletions
diff --git a/src/linux/tun.c b/src/linux/tun.c index 88d6d6e..d8044b5 100644 --- a/src/linux/tun.c +++ b/src/linux/tun.c @@ -52,12 +52,11 @@ #include "log.h" -int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp) -{ +int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix){ if(!dev) return; - tun_conf(dev, dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400); + tun_conf(dev, dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400); dev->actual_name_ = NULL; dev->fd_ = open(DEFAULT_DEVICE, O_RDWR); @@ -103,7 +102,7 @@ int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, cons return -2; } - if(ifcfg_lp && ifcfg_rnmp) + if(ifcfg_addr) tun_do_ifconfig(dev); return 0; @@ -125,11 +124,8 @@ void tun_close(tun_device_t* dev) if(dev->actual_name_) free(dev->actual_name_); - if(dev->local_) - free(dev->local_); - - if(dev->remote_netmask_) - free(dev->remote_netmask_); + if(dev->net_addr_) + free(dev->net_addr_); } int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len) @@ -184,25 +180,25 @@ int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len) void tun_do_ifconfig(tun_device_t* dev) { - if(!dev || !dev->actual_name_ || !dev->local_ || !dev->remote_netmask_) + if(!dev || !dev->actual_name_ || !dev->net_addr_) return; - char* command = NULL; - if(dev->type_ == TYPE_TUN) - asprintf(&command, "/sbin/ifconfig %s %s pointopoint %s mtu %d", dev->actual_name_, dev->local_, dev->remote_netmask_, dev->mtu_); - else - asprintf(&command, "/sbin/ifconfig %s %s netmask %s mtu %d", dev->actual_name_, dev->local_, dev->remote_netmask_, dev->mtu_); +/* char* command = NULL; */ +/* if(dev->type_ == TYPE_TUN) */ +/* asprintf(&command, "/sbin/ifconfig %s %s pointopoint %s mtu %d", dev->actual_name_, dev->local_, dev->remote_netmask_, dev->mtu_); */ +/* else */ +/* asprintf(&command, "/sbin/ifconfig %s %s netmask %s mtu %d", dev->actual_name_, dev->local_, dev->remote_netmask_, dev->mtu_); */ - if(!command) { - log_printf(ERR, "Execution of ifconfig failed"); - return; - } +/* if(!command) { */ +/* log_printf(ERR, "Execution of ifconfig failed"); */ +/* return; */ +/* } */ - int result = system(command); - if(result == -1) - log_printf(ERR, "Execution of ifconfig failed"); - else - log_printf(NOTICE, "ifconfig returned %d", WEXITSTATUS(result)); +/* int result = system(command); */ +/* if(result == -1) */ +/* log_printf(ERR, "Execution of ifconfig failed"); */ +/* else */ +/* log_printf(NOTICE, "ifconfig returned %d", WEXITSTATUS(result)); */ - free(command); +/* free(command); */ } diff --git a/src/options.c b/src/options.c index 843cd4a..887cc4b 100644 --- a/src/options.c +++ b/src/options.c @@ -89,22 +89,19 @@ i++; \ } -#define PARSE_STRING_PARAM2(SHORT, LONG, VALUE1, VALUE2) \ +#define PARSE_IFCONFIG_PARAM(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ { \ - if(argc < 2 || \ - argv[i+1][0] == '-' || argv[i+2][0] == '-') \ + if(argc < 1 || argv[i+1][0] == '-') \ return i; \ - if(VALUE1) free(VALUE1); \ - VALUE1 = strdup(argv[i+1]); \ - if(!VALUE1) \ - return -2; \ - if(VALUE2) free(VALUE2); \ - VALUE2 = strdup(argv[i+2]); \ - if(!VALUE2) \ - return -2; \ - argc-=2; \ - i+=2; \ + int ret; \ + ret = options_parse_ifconfig(argv[i+1], &VALUE); \ + if(ret > 0) \ + return i+1; \ + if(ret < 0) \ + return ret; \ + argc--; \ + i++; \ } #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \ @@ -156,6 +153,43 @@ int options_parse_hex_string(const char* hex, buffer_t* buffer) return 0; } +int options_parse_ifconfig(const char* arg, ifconfig_param_t* ifcfg) +{ + char* str = strdup(arg); + if(!str) + return -2; + + char* ptr = str; + for(;*ptr;++ptr) { + if(*ptr == '/') { + *ptr = 0; + ptr++; + if(!(*ptr)) { + free(str); + return 1; + } + + ifcfg->prefix_length_ = atoi(ptr); + ifcfg->net_addr_ = strdup(str); + free(str); + + if(!ifcfg->net_addr_) + return -2; + + return 0; + } + if(!isdigit(*ptr) && *ptr != '.') { + free(str); + return 1; + } + } + + printf("no / found\n"); + free(str); + return 1; +} + + int options_parse(options_t* opt, int argc, char* argv[]) { if(!opt) @@ -191,7 +225,7 @@ int options_parse(options_t* opt, int argc, char* argv[]) PARSE_STRING_PARAM("-o","--remote-port", opt->remote_port_) PARSE_STRING_PARAM("-d","--dev", opt->dev_name_) PARSE_STRING_PARAM("-t","--type", opt->dev_type_) - PARSE_STRING_PARAM2("-n","--ifconfig", opt->ifconfig_param_local_, opt->ifconfig_param_remote_netmask_) + PARSE_IFCONFIG_PARAM("-n","--ifconfig", opt->ifconfig_param_) PARSE_STRING_PARAM("-x","--post-up-script", opt->post_up_script_) PARSE_INT_PARAM("-m","--mux", opt->mux_) PARSE_INT_PARAM("-w","--window-size", opt->seq_window_size_) @@ -247,8 +281,8 @@ void options_default(options_t* opt) opt->remote_port_ = strdup("4444"); opt->dev_name_ = NULL; opt->dev_type_ = NULL; - opt->ifconfig_param_local_ = NULL; - opt->ifconfig_param_remote_netmask_ = NULL; + opt->ifconfig_param_.net_addr_ = NULL; + opt->ifconfig_param_.prefix_length_ = 0; opt->post_up_script_ = NULL; opt->mux_ = 0; opt->seq_window_size_ = 0; @@ -294,10 +328,8 @@ void options_clear(options_t* opt) free(opt->dev_name_); if(opt->dev_type_) free(opt->dev_type_); - if(opt->ifconfig_param_local_) - free(opt->ifconfig_param_local_); - if(opt->ifconfig_param_remote_netmask_) - free(opt->ifconfig_param_remote_netmask_); + if(opt->ifconfig_param_.net_addr_) + free(opt->ifconfig_param_.net_addr_); if(opt->post_up_script_) free(opt->post_up_script_); if(opt->cipher_) @@ -332,8 +364,8 @@ void options_print_usage() printf(" [-o|--remote-port] <port> remote port\n"); printf(" [-d|--dev] <name> device name\n"); printf(" [-t|--type] <tun|tap> device type\n"); - printf(" [-n|--ifconfig] <local> the local address for the tun/tap device\n"); - printf(" <remote|netmask> the remote address(tun) or netmask(tap)\n"); + + printf(" [-n|--ifconfig] <local>/<prefix> the local address for the tun/tap device and the used prefix length\n"); printf(" [-x|--post-up-script] <script> script gets called after interface is created\n"); printf(" [-m|--mux] <mux-id> the multiplex id to use\n"); printf(" [-w|--window-size] <window size> seqence number window size\n"); @@ -365,8 +397,8 @@ void options_print(options_t* opt) printf("remote_port: '%s'\n", opt->remote_port_); printf("dev_name: '%s'\n", opt->dev_name_); printf("dev_type: '%s'\n", opt->dev_type_); - printf("ifconfig_local: '%s'\n", opt->ifconfig_param_local_); - printf("ifconfig_remote_netmask: '%s'\n", opt->ifconfig_param_remote_netmask_); + printf("ifconfig_net_addr: '%s'\n", opt->ifconfig_param_.net_addr_); + printf("ifconfig_prefix_length: %d\n", opt->ifconfig_param_.prefix_length_); printf("post_up_script: '%s'\n", opt->post_up_script_); printf("mux: %d\n", opt->mux_); printf("seq_window_size: %d\n", opt->seq_window_size_); diff --git a/src/options.h b/src/options.h index 4186055..6108d41 100644 --- a/src/options.h +++ b/src/options.h @@ -35,6 +35,12 @@ #ifndef _OPTIONS_H_ #define _OPTIONS_H_ +struct ifconfig_param_struct { + char* net_addr_; + u_int16_t prefix_length_; +}; +typedef struct ifconfig_param_struct ifconfig_param_t; + struct options_struct { char* progname_; int daemonize_; @@ -49,8 +55,7 @@ struct options_struct { char* remote_port_; char* dev_name_; char* dev_type_; - char* ifconfig_param_local_; - char* ifconfig_param_remote_netmask_; + ifconfig_param_t ifconfig_param_; char* post_up_script_; mux_t mux_; window_size_t seq_window_size_; @@ -67,6 +72,7 @@ struct options_struct { typedef struct options_struct options_t; 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_default(options_t* opt); @@ -46,12 +46,12 @@ struct tun_device_struct { char* actual_name_; device_type_t type_; u_int16_t mtu_; - char* local_; - char* remote_netmask_; + char* net_addr_; + u_int16_t prefix_length_; }; typedef struct tun_device_struct tun_device_t; -int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp); +int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix); int tun_init_post(tun_device_t* dev); void tun_do_ifconfig(tun_device_t* dev); void tun_close(tun_device_t* dev); @@ -60,7 +60,7 @@ int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len); int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len); // in tun_helper.h -void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp, u_int16_t mtu); +void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix, u_int16_t mtu); int tun_fix_return(int ret, size_t pi_length); const char* tun_get_type_string(tun_device_t* dev); diff --git a/src/tun_helper.h b/src/tun_helper.h index b403891..47c8033 100644 --- a/src/tun_helper.h +++ b/src/tun_helper.h @@ -37,7 +37,7 @@ #include <string.h> -void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp, u_int16_t mtu) +void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix, u_int16_t mtu) { if(!dev) return; @@ -56,12 +56,12 @@ void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, con dev->type_ = TYPE_TAP; } - dev->local_ = NULL; - dev->remote_netmask_ = NULL; - if(ifcfg_lp) - dev->local_ = strdup(ifcfg_lp); - if(ifcfg_rnmp) - dev->remote_netmask_ = strdup(ifcfg_rnmp); + dev->net_addr_ = NULL; + dev->prefix_length_ = 0; + if(ifcfg_addr) { + dev->net_addr_ = strdup(ifcfg_addr); + dev->prefix_length_ = ifcfg_prefix; + } } diff --git a/src/uanytun.c b/src/uanytun.c index 48cc020..c80ca8b 100644 --- a/src/uanytun.c +++ b/src/uanytun.c @@ -358,7 +358,7 @@ int main(int argc, char* argv[]) #endif tun_device_t dev; - ret = tun_init(&dev, opt.dev_name_, opt.dev_type_, opt.ifconfig_param_local_, opt.ifconfig_param_remote_netmask_); + ret = tun_init(&dev, opt.dev_name_, opt.dev_type_, opt.ifconfig_param_.net_addr_, opt.ifconfig_param_.prefix_length_); if(ret) { log_printf(ERR, "error on tun_init, exitting"); options_clear(&opt); |