From d399c8468aedc975d52632624786681a3a7ceacc Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 3 Feb 2009 23:56:14 +0000 Subject: new ifconfig syntax (only command line parser yet) --- src/linux/tun.c | 46 +++++++++++++++----------------- src/options.c | 80 +++++++++++++++++++++++++++++++++++++++----------------- src/options.h | 10 +++++-- src/tun.h | 8 +++--- src/tun_helper.h | 14 +++++----- 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] remote port\n"); printf(" [-d|--dev] device name\n"); printf(" [-t|--type] device type\n"); - printf(" [-n|--ifconfig] the local address for the tun/tap device\n"); - printf(" the remote address(tun) or netmask(tap)\n"); + + printf(" [-n|--ifconfig] / the local address for the tun/tap device and the used prefix length\n"); printf(" [-x|--post-up-script]