From d634f32f4b2dc1be2842b98d796d8d7b410b79e1 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 17 Jan 2009 20:47:22 +0000 Subject: options parser refactoring --- src/options.cpp | 639 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 365 insertions(+), 274 deletions(-) (limited to 'src/options.cpp') diff --git a/src/options.cpp b/src/options.cpp index 4700047..8b76906 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -29,16 +29,101 @@ * along with anytun. If not, see . */ +#include #include #include #include #include -#include #include "datatypes.h" #include "options.h" #include "log.h" +std::ostream& operator<<(std::ostream& stream, syntax_error const& error) +{ + stream << "syntax error: " << error.what() << std::endl; + stream << " "; + for(u_int32_t i = 0; i < error.pos; ++i) stream << " "; + return stream << "^"; +} + +void OptionHost::init(std::string addrPort) +{ + std::string origAddrPort(addrPort); + size_t pos = addrPort.find_first_of("["); + + if(pos != std::string::npos && pos != 0) + throw syntax_error(origAddrPort, pos); // an [ was found but not at the beginning; + + bool hasPort = false; + if(pos != std::string::npos) { + addrPort.erase(pos, 1); + pos = addrPort.find_first_of("]"); + + if(pos == std::string::npos) + throw syntax_error(origAddrPort, origAddrPort.length()); //no trailing ] although an leading [ was found + + if(pos < addrPort.length()-2) { + if(addrPort[pos+1] != ':') + throw syntax_error(origAddrPort, pos+2); // wrong port delimieter + + addrPort[pos+1] = '/'; + hasPort = true; + } + else if(pos != addrPort.length()-1) + throw syntax_error(origAddrPort, pos+2); // too few characters left + + addrPort.erase(pos, 1); + } + else { + pos = addrPort.find_first_of(":"); + if(pos != std::string::npos && pos == addrPort.find_last_of(":")) { + // an ':' has been found and it is the only one -> assuming port present + hasPort = true; + addrPort[pos] = '/'; + } + } + + if(hasPort) { + std::stringstream tmp_stream(addrPort); + + getline(tmp_stream, addr, '/'); + if(!tmp_stream.good()) + throw syntax_error(origAddrPort, addr.length()); + + tmp_stream >> port; + } + else { + addr = addrPort; + port = "2323"; // default sync port + } +} + +std::istream& operator>>(std::istream& stream, OptionHost& host) +{ + std::string tmp; + stream >> tmp; + host.init(tmp); + return stream; +} + +void OptionRoute::init(std::string route) +{ + std::stringstream tmp_stream(route); + getline(tmp_stream, net_addr, '/'); + if(!tmp_stream.good()) + throw syntax_error(route, net_addr.length()); + tmp_stream >> prefix_length; +} + +std::istream& operator>>(std::istream& stream, OptionRoute& route) +{ + std::string tmp; + stream >> tmp; + route.init(tmp); + return stream; +} + Options* Options::inst = NULL; Mutex Options::instMutex; Options& gOpt = Options::instance(); @@ -56,157 +141,191 @@ Options& Options::instance() Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0)) { progname_ = "anytun"; + daemonize_ = true; chroot_ = false; username_ = "nobody"; chroot_dir_ = "/var/run/anytun"; pid_file_ = ""; - sender_id_ = 0; + + file_name_ = ""; + bind_to_.addr = "127.0.0.1"; + bind_to_.port = "2323"; + local_addr_ = ""; local_port_ = "4444"; - local_sync_port_ = ""; - remote_sync_port_ = ""; - remote_sync_addr_ = ""; remote_addr_ = ""; remote_port_ = "4444"; + local_sync_addr_ = ""; + local_sync_port_ = ""; + dev_name_ = ""; dev_type_ = ""; ifconfig_param_local_ = ""; ifconfig_param_remote_netmask_ = ""; post_up_script_ = ""; - seq_window_size_ = 100; + + sender_id_ = 0; + mux_ = 0; + seq_window_size_ = 0; + cipher_ = "aes-ctr"; + auth_algo_ = "sha1"; kd_prf_ = "aes-ctr"; ld_kdr_ = 0; - auth_algo_ = "sha1"; - mux_ = 0; } Options::~Options() { } -#define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \ - else if(str == SHORT || str == LONG) \ +#define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \ + else if(str == SHORT || str == LONG) \ VALUE = true; -#define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \ - else if(str == SHORT || str == LONG) \ +#define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \ + else if(str == SHORT || str == LONG) \ VALUE = false; -#define PARSE_SIGNED_INT_PARAM(SHORT, LONG, VALUE) \ - else if(str == SHORT || str == LONG) \ - { \ - if(argc < 1) \ - return i; \ - std::stringstream tmp; \ - tmp << argv[i+1]; \ - tmp >> VALUE; \ - argc--; \ - i++; \ +#define PARSE_SIGNED_INT_PARAM(SHORT, LONG, VALUE) \ + else if(str == SHORT || str == LONG) \ + { \ + if(argc < 1) \ + throw syntax_error(str, str.length()); \ + std::stringstream tmp; \ + tmp << argv[i+1]; \ + tmp >> VALUE; \ + argc--; \ + i++; \ } -#define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \ - else if(str == SHORT || str == LONG) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - std::stringstream tmp; \ - tmp << argv[i+1]; \ - tmp >> VALUE; \ - argc--; \ - i++; \ +#define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \ + else if(str == SHORT || str == LONG) \ + { \ + if(argc < 1) \ + throw syntax_error(str, str.length()); \ + if(argv[i+1][0] == '-') { \ + u_int32_t pos = str.length() + 1; \ + throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ + } \ + std::stringstream tmp; \ + tmp << argv[i+1]; \ + tmp >> VALUE; \ + argc--; \ + i++; \ } -#define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \ - else if(str == SHORT || str == LONG) \ - { \ - if(argc < 2 || \ - argv[i+1][0] == '-' || argv[i+2][0] == '-') \ - return i; \ - std::stringstream tmp; \ - tmp << argv[i+1] << " " << argv[i+2]; \ - tmp >> VALUE1; \ - tmp >> VALUE2; \ - argc-=2; \ - i+=2; \ +#define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \ + else if(str == SHORT || str == LONG) \ + { \ + if(argc < 1) \ + throw syntax_error(str, str.length()); \ + if(argc < 2) \ + throw syntax_error(str.append(" ").append(argv[i+1]), str.length()); \ + if(argv[i+1][0] == '-') { \ + u_int32_t pos = str.length() + 1; \ + throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ + } \ + if(argv[i+2][0] == '-') { \ + u_int32_t pos = str.length() + 1 + strlen(argv[i+1]) + 1; \ + throw syntax_error(str.append(" ").append(argv[i+1]).append(" ").append(argv[i+2]), pos); \ + } \ + std::stringstream tmp; \ + tmp << argv[i+1] << " " << argv[i+2]; \ + tmp >> VALUE1; \ + tmp >> VALUE2; \ + argc-=2; \ + i+=2; \ } -#define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \ - else if(str == SHORT || str == LONG) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - VALUE = Buffer(std::string(argv[i+1])); \ - for(size_t j=0; j < strlen(argv[i+1]); ++j) \ - argv[i+1][j] = '#'; \ - argc--; \ - i++; \ +#define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \ + else if(str == SHORT || str == LONG) \ + { \ + if(argc < 1) \ + throw syntax_error(str, str.length()); \ + if(argv[i+1][0] == '-') { \ + u_int32_t pos = str.length() + 1; \ + throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ + } \ + VALUE = Buffer(std::string(argv[i+1])); \ + for(size_t j=0; j < strlen(argv[i+1]); ++j) \ + argv[i+1][j] = '#'; \ + argc--; \ + i++; \ } -#define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \ - else if(str == SHORT || str == LONG) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - std::stringstream tmp(argv[i+1]); \ - while (tmp.good()) \ - { \ - std::string tmp_line; \ - getline(tmp,tmp_line,','); \ - LIST.push(tmp_line); \ - } \ - argc--; \ - i++; \ +#define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \ + else if(str == SHORT || str == LONG) \ + { \ + if(argc < 1) \ + throw syntax_error(str, str.length()); \ + if(argv[i+1][0] == '-') { \ + u_int32_t pos = str.length() + 1; \ + throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ + } \ + std::stringstream tmp(argv[i+1]); \ + while (tmp.good()) \ + { \ + std::string tmp_line; \ + getline(tmp,tmp_line,','); \ + LIST.push(tmp_line); \ + } \ + argc--; \ + i++; \ } -int32_t Options::parse(int argc, char* argv[]) +bool Options::parse(int argc, char* argv[]) { Lock lock(mutex); progname_ = argv[0]; argc--; - std::queue route_queue; std::queue host_port_queue; int32_t ld_kdr_tmp = ld_kdr_; + std::queue route_queue; for(int i=1; argc > 0; ++i) { std::string str(argv[i]); argc--; if(str == "-h" || str == "--help") - return -1; + return false; PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_) PARSE_BOOL_PARAM("-C","--chroot", chroot_) PARSE_SCALAR_PARAM("-u","--username", username_) PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_) PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_) + +// PARSE_SCALAR_PARAM("-f","--file", file_name_) +// PARSE_SCALAR_PARAM("-X","--control-host", bind_to_) + PARSE_SCALAR_PARAM("-i","--interface", local_addr_) PARSE_SCALAR_PARAM("-p","--port", local_port_) - PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_) - PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_) - PARSE_SCALAR_PARAM("-R","--remote-sync-host", remote_sync_addr_) - PARSE_SCALAR_PARAM("-O","--remote-sync-port", remote_sync_port_) PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_) PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_) + PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_) + PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_) + PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue) + PARSE_CSLIST_PARAM("-X","--control-host", host_port_queue) + PARSE_SCALAR_PARAM("-d","--dev", dev_name_) PARSE_SCALAR_PARAM("-t","--type", dev_type_) PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_, ifconfig_param_remote_netmask_) PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_) + PARSE_CSLIST_PARAM("-R","--route", route_queue) + PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_) PARSE_SCALAR_PARAM("-m","--mux", mux_) PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_) - PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue) - PARSE_CSLIST_PARAM("-X","--control-host", host_port_queue) - PARSE_CSLIST_PARAM("-T","--route", route_queue) + PARSE_SCALAR_PARAM("-c","--cipher", cipher_) + PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_) PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_) PARSE_SIGNED_INT_PARAM("-l","--ld-kdr", ld_kdr_tmp) - PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_) PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_) PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_) else - return i; + throw syntax_error(str, 0); } ld_kdr_ = static_cast(ld_kdr_tmp); @@ -219,166 +338,124 @@ int32_t Options::parse(int argc, char* argv[]) if(dev_name_ == "" && dev_type_ == "") dev_type_ = "tun"; - while(!host_port_queue.empty()) - { - bool ret = splitAndAddHostPort(host_port_queue.front(), connect_to_); - if(!ret) return -2; + while(!host_port_queue.empty()) { + OptionHost sh(host_port_queue.front()); + remote_sync_hosts_.push_back(sh); host_port_queue.pop(); } - while(!route_queue.empty()) - { - std::stringstream tmp_stream(route_queue.front()); - OptionRoute rt; - getline(tmp_stream,rt.net_addr,'/'); - if(!tmp_stream.good()) - return false; - tmp_stream >> rt.prefix_length; - route_queue.pop(); - routes_.push_back(rt); - } - return 0; -} - -bool Options::splitAndAddHostPort(std::string hostPort, ConnectToList& list) -{ - OptionConnectTo oct; - size_t pos = hostPort.find_first_of("["); - - if(pos != std::string::npos && pos != 0) - return false; // an [ was found but not at the beginning - bool hasPort = false; - if(pos != std::string::npos) { - hostPort.erase(pos, 1); - pos = hostPort.find_first_of("]"); - - if(pos == std::string::npos) - return false; // no trailing ] although an leading [ was found - - if(pos < hostPort.length()-2) { - - if(hostPort[pos+1] != ':') - return false; // wrong port delimieter - - hostPort[pos+1] = '/'; - hasPort = true; - } - else if(pos != hostPort.length()-1) - return false; // to few characters left - - hostPort.erase(pos, 1); - } - else { - pos = hostPort.find_first_of(":"); - if(pos != std::string::npos && pos == hostPort.find_last_of(":")) { - // an ':' has been found and it is the only one -> assuming port present - hasPort = true; - hostPort[pos] = '/'; - } - } - - if(hasPort) { - std::stringstream tmp_stream(hostPort); - getline(tmp_stream,oct.host,'/'); - if(!tmp_stream.good()) - return false; - - tmp_stream >> oct.port; - } - else { - oct.host = hostPort; - oct.port = "2323"; // default sync port + while(!route_queue.empty()) { + OptionRoute rt(route_queue.front()); + routes_.push_back(rt); + route_queue.pop(); } - list.push_back(oct); return true; } void Options::printUsage() { std::cout << "USAGE:" << std::endl; - std::cout << "anytun [-h|--help] prints this..." << std::endl; -// std::cout << " [-f|--config] the config file" << std::endl; - std::cout << " [-D|--nodaemonize] don't run in background" << std::endl; - std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl; - std::cout << " [-u|--username] if chroot change to this user" << std::endl; - std::cout << " [-H|--chroot-dir] chroot to this directory" << std::endl; - std::cout << " [-P|--write-pid] write pid to this file" << std::endl; - std::cout << " [-i|--interface] local anycast ip address to bind to" << std::endl; - std::cout << " [-p|--port] local anycast(data) port to bind to" << std::endl; - std::cout << " [-I|--sync-interface] local unicast(sync) ip address to bind to" << std::endl; - std::cout << " [-S|--sync-port] local unicast(sync) port to bind to" << std::endl; - std::cout << " [-M|--sync-hosts] [:][,[:][...]]"<< std::endl; - std::cout << " remote hosts to sync with" << std::endl; - std::cout << " [-X|--control-host] [:]"<< std::endl; - std::cout << " fetch the config from this host" << std::endl; - std::cout << " [-r|--remote-host] remote host" << std::endl; - std::cout << " [-o|--remote-port] remote port" << std::endl; - std::cout << " [-d|--dev] device name" << std::endl; - std::cout << " [-t|--type] device type" << std::endl; - std::cout << " [-n|--ifconfig] the local address for the tun/tap device" << std::endl - << " the remote address(tun) or netmask(tap)" << std::endl; - std::cout << " [-x|--post-up-script]