diff options
-rw-r--r-- | src/anytun.cpp | 36 | ||||
-rw-r--r-- | src/connectionList.cpp | 1 | ||||
-rw-r--r-- | src/options.cpp | 639 | ||||
-rw-r--r-- | src/options.h | 110 | ||||
-rw-r--r-- | src/threadParam.h | 4 |
5 files changed, 458 insertions, 332 deletions
diff --git a/src/anytun.cpp b/src/anytun.cpp index 8c7c239..2c06a46 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -111,7 +111,7 @@ void syncConnector(void* p ) { ThreadParam* param = reinterpret_cast<ThreadParam*>(p); - SyncClient sc ( param->connto.host, param->connto.port); + SyncClient sc ( param->connto.addr, param->connto.port); sc.run(); } @@ -330,22 +330,26 @@ int main(int argc, char* argv[]) { cLog.msg(Log::PRIO_NOTICE) << "anytun started..."; /// std::cout << "anytun - secure anycast tunneling protocol" << std::endl; - int32_t result = gOpt.parse(argc, argv); - if(result) { - if(result > 0) { - std::cerr << "syntax error near: " << argv[result] << std::endl << std::endl; - cLog.msg(Log::PRIO_ERR) << "syntax error, exitting"; - } - if(result == -2) { - std::cerr << "can't parse host-port definition" << std::endl << std::endl; - cLog.msg(Log::PRIO_ERR) << "can't parse host-port definition, exitting"; + + try + { + bool result = gOpt.parse(argc, argv); + if(!result) { + gOpt.printUsage(); + exit(0); } - + } + catch(syntax_error& e) + { + std::cerr << e << std::endl; gOpt.printUsage(); - - exit(result); + exit(-1); } + gOpt.printOptions(); + exit(0); + + std::ofstream pidFile; if(gOpt.getPidFile() != "") { pidFile.open(gOpt.getPidFile().c_str()); @@ -379,7 +383,7 @@ int main(int argc, char* argv[]) else src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort()); - ConnectToList connect_to = gOpt.getConnectTo(); + HostList connect_to = gOpt.getRemoteSyncHosts(); SyncQueue queue; if(gOpt.getRemoteAddr() != "") @@ -430,7 +434,7 @@ int main(int argc, char* argv[]) sig.init(); #endif - OptionConnectTo* connTo = new OptionConnectTo(); + OptionHost* connTo = new OptionHost(); ThreadParam p(dev, *src, *connTo); boost::thread senderThread(boost::bind(sender,&p)); @@ -443,7 +447,7 @@ int main(int argc, char* argv[]) syncListenerThread = new boost::thread(boost::bind(syncListener,&queue)); std::list<boost::thread *> connectThreads; - for(ConnectToList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) { + for(HostList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) { ThreadParam * point = new ThreadParam(dev, *src, *it); connectThreads.push_back(new boost::thread(boost::bind(syncConnector,point))); } diff --git a/src/connectionList.cpp b/src/connectionList.cpp index 38244b1..d6b373f 100644 --- a/src/connectionList.cpp +++ b/src/connectionList.cpp @@ -130,7 +130,6 @@ ConnectionParam & ConnectionList::getOrNewConnectionUnlocked(u_int16_t mux) seq_nr_t seq_nr_=0; KeyDerivation * kd = KeyDerivationFactory::create(gOpt.getKdPrf()); kd->init(Buffer(key, sizeof(key)), Buffer(salt, sizeof(salt))); -// kd->setLogKDRate(gOpt.getLdKdr()); ConnectionParam conn ( (*kd), (*seq), seq_nr_, PacketSourceEndpoint()); connections_.insert(ConnectionMap::value_type(mux, conn)); it = connections_.find(mux); 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 <http://www.gnu.org/licenses/>. */ +#include <cstring> #include <iostream> #include <queue> #include <string> #include <sstream> -#include <cstring> #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<std::string> route_queue; std::queue<std::string> host_port_queue; int32_t ld_kdr_tmp = ld_kdr_; + std::queue<std::string> 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<int8_t>(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] <file> 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] <username> if chroot change to this user" << std::endl; - std::cout << " [-H|--chroot-dir] <path> chroot to this directory" << std::endl; - std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl; - std::cout << " [-i|--interface] <ip-address> local anycast ip address to bind to" << std::endl; - std::cout << " [-p|--port] <port> local anycast(data) port to bind to" << std::endl; - std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl; - std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl; - std::cout << " [-M|--sync-hosts] <hostname|ip>[:<port>][,<hostname|ip>[:<port>][...]]"<< std::endl; - std::cout << " remote hosts to sync with" << std::endl; - std::cout << " [-X|--control-host] <hostname|ip>[:<port>]"<< std::endl; - std::cout << " fetch the config from this host" << std::endl; - std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl; - std::cout << " [-o|--remote-port] <port> remote port" << std::endl; - std::cout << " [-d|--dev] <name> device name" << std::endl; - std::cout << " [-t|--type] <tun|tap> device type" << std::endl; - std::cout << " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl - << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl; - std::cout << " [-x|--post-up-script] <script> script gets called after interface is created" << std::endl; - std::cout << " [-s|--sender-id ] <sender id> the sender id to use" << std::endl; - std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl; - std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl; - std::cout << " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl; - std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl; - std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl; - std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl; - std::cout << " [-l|--ld-kdr] <ld-kdr> log2 of key derivation rate" << std::endl; - std::cout << " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl; - std::cout << " [-T|--route] <net>/<prefix length> add a route to connection, can be invoked several times" << std::endl; + std::cout << "anytun " << std::endl; + std::cout << " [-h|--help] prints this..." << 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] <username> if chroot change to this user" << std::endl; + std::cout << " [-H|--chroot-dir] <path> chroot to this directory" << std::endl; + std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl; + +// std::cout << " [-f|--file] <path> path to input file" << std::endl; +// std::cout << " [-X|--control-host] < <hostname|ip>[:<port>] | :<port> >" << std::endl; +// std::cout << " local tcp port and or ip address to bind to" << std::endl; + + std::cout << " [-i|--interface] <hostname|ip> local anycast ip address to bind to" << std::endl; + std::cout << " [-p|--port] <port> local anycast(data) port to bind to" << std::endl; + std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl; + std::cout << " [-o|--remote-port] <port> remote port" << std::endl; + std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl; + std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl; + std::cout << " [-M|--sync-hosts] <hostname|ip>[:<port>][,<hostname|ip>[:<port>][...]]"<< std::endl; + std::cout << " remote hosts to sync with" << std::endl; + std::cout << " [-X|--control-host] <hostname|ip>[:<port>]"<< std::endl; + std::cout << " fetch the config from this host" << std::endl; + + std::cout << " [-d|--dev] <name> device name" << std::endl; + std::cout << " [-t|--type] <tun|tap> device type" << std::endl; + std::cout << " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl + << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl; + std::cout << " [-x|--post-up-script] <script> script gets called after interface is created" << std::endl; + std::cout << " [-R|--route] <net>/<prefix length> add a route to connection, can be invoked several times" << std::endl; + + std::cout << " [-s|--sender-id ] <sender id> the sender id to use" << std::endl; + std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl; + std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl; + + std::cout << " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl; + std::cout << " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl; + std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl; + std::cout << " [-l|--ld-kdr] <ld-kdr> log2 of key derivation rate" << std::endl; + std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl; + std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl; } void Options::printOptions() { Lock lock(mutex); std::cout << "Options:" << std::endl; + std::cout << std::endl; std::cout << "daemonize=" << daemonize_ << std::endl; std::cout << "chroot=" << chroot_ << std::endl; std::cout << "username='" << username_ << "'" << std::endl; std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl; std::cout << "pid_file='" << pid_file_ << "'" << std::endl; - std::cout << "sender_id='" << sender_id_ << "'" << std::endl; + std::cout << std::endl; +// std::cout << "file_name='" << file_name_ << "'" << std::endl; +// std::cout << "bind_to.addr='" << bind_to_.addr << "'" << std::endl; +// std::cout << "bind_to.port='" << bind_to_.port << "'" << std::endl; +// std::cout << std::endl; std::cout << "local_addr='" << local_addr_ << "'" << std::endl; std::cout << "local_port='" << local_port_ << "'" << std::endl; - std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl; - std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl; std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl; std::cout << "remote_port='" << remote_port_ << "'" << std::endl; + std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl; + std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl; + std::cout << "remote_sync_hosts="; + HostList::const_iterator it = remote_sync_hosts_.begin(); + for(; it != remote_sync_hosts_.end(); ++it) + std::cout << "'" << it->addr << "','" << it->port << "'; "; + std::cout << std::endl; + std::cout << std::endl; std::cout << "dev_name='" << dev_name_ << "'" << std::endl; std::cout << "dev_type='" << dev_type_ << "'" << std::endl; std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl; std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl; std::cout << "post_up_script='" << post_up_script_ << "'" << std::endl; - std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl; - std::cout << "mux_id=" << mux_ << std::endl; - std::cout << "cipher='" << cipher_ << "'" << std::endl; - std::cout << "key=" << key_.getHexDumpOneLine() << std::endl; - std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl; - std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl; - std::cout << "ld_kdr=" << static_cast<int32_t>(ld_kdr_) << std::endl; - std::cout << "auth_algo='" << auth_algo_ << "'" << std::endl; - - std::cout << "connect_to="; - ConnectToList::const_iterator it = connect_to_.begin(); - for(; it != connect_to_.end(); ++it) - std::cout << "'" << it->host << "','" << it->port << "';"; - std::cout << std::endl; std::cout << "routes:" << std::endl; RouteList::const_iterator rit; for(rit = routes_.begin(); rit != routes_.end(); ++rit) std::cout << " " << rit->net_addr << "/" << rit->prefix_length << std::endl; + std::cout << std::endl; + std::cout << "sender_id='" << sender_id_ << "'" << std::endl; + std::cout << "mux_id=" << mux_ << std::endl; + std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl; + std::cout << std::endl; + std::cout << "cipher='" << cipher_ << "'" << std::endl; + std::cout << "auth_algo='" << auth_algo_ << "'" << std::endl; + std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl; + std::cout << "ld_kdr=" << static_cast<int32_t>(ld_kdr_) << std::endl; + std::cout << "key=" << key_.getHexDumpOneLine() << std::endl; + std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl; } + + std::string Options::getProgname() { Lock lock(mutex); return progname_; } - Options& Options::setProgname(std::string p) { Lock lock(mutex); @@ -447,46 +524,57 @@ Options& Options::setPidFile(std::string p) return *this; } -ConnectToList Options::getConnectTo() + + +std::string Options::getFileName() { Lock lock(mutex); - return connect_to_; + return file_name_; } -sender_id_t Options::getSenderId() +Options& Options::setFileName(std::string f) { - return sender_id_; + Lock lock(mutex); + file_name_ = f; + return *this; } -Options& Options::setSenderId(sender_id_t s) +std::string Options::getBindToAddr() { - sender_id_ = s; - return *this; + Lock lock(mutex); + return bind_to_.addr; } -std::string Options::getLocalAddr() +Options& Options::setBindToAddr(std::string b) { Lock lock(mutex); - return local_addr_; + bind_to_.addr = b; + return *this; } -Options& Options::setLocalAddr(std::string l) +std::string Options::getBindToPort() { - Lock lock(mutex); - local_addr_ = l; + return bind_to_.port; +} + +Options& Options::setBindToPort(std::string b) +{ + bind_to_.port = b; return *this; } -std::string Options::getLocalSyncAddr() + + +std::string Options::getLocalAddr() { Lock lock(mutex); - return local_sync_addr_; + return local_addr_; } -Options& Options::setLocalSyncAddr(std::string l) +Options& Options::setLocalAddr(std::string l) { Lock lock(mutex); - local_sync_addr_ = l; + local_addr_ = l; return *this; } @@ -501,72 +589,61 @@ Options& Options::setLocalPort(std::string l) return *this; } -std::string Options::getLocalSyncPort() +std::string Options::getRemoteAddr() { - return local_sync_port_; + Lock lock(mutex); + return remote_addr_; } -Options& Options::setLocalSyncPort(std::string l) +Options& Options::setRemoteAddr(std::string r) { - local_sync_port_ = l; + Lock lock(mutex); + remote_addr_ = r; return *this; } -std::string Options::getRemoteSyncPort() +std::string Options::getRemotePort() { - return remote_sync_port_; + return remote_port_; } -Options& Options::setRemoteSyncPort(std::string l) +Options& Options::setRemotePort(std::string r) { - remote_sync_port_ = l; + remote_port_ = r; return *this; } -std::string Options::getRemoteSyncAddr() +std::string Options::getLocalSyncAddr() { Lock lock(mutex); - return remote_sync_addr_; + return local_sync_addr_; } -Options& Options::setRemoteSyncAddr(std::string r) +Options& Options::setLocalSyncAddr(std::string l) { Lock lock(mutex); - remote_sync_addr_ = r; + local_sync_addr_ = l; return *this; } -std::string Options::getRemoteAddr() +std::string Options::getLocalSyncPort() { - Lock lock(mutex); - return remote_addr_; + return local_sync_port_; } -Options& Options::setRemoteAddr(std::string r) +Options& Options::setLocalSyncPort(std::string l) { - Lock lock(mutex); - remote_addr_ = r; + local_sync_port_ = l; return *this; } -std::string Options::getRemotePort() +HostList Options::getRemoteSyncHosts() { - return remote_port_; + Lock lock(mutex); + return remote_sync_hosts_; } -Options& Options::setRemotePort(std::string r) -{ - remote_port_ = r; - return *this; -} -Options& Options::setRemoteAddrPort(std::string addr, std::string port) -{ - Lock lock(mutex); - remote_addr_ = addr; - remote_port_ = port; - return *this; -} std::string Options::getDevName() { @@ -633,53 +710,61 @@ Options& Options::setPostUpScript(std::string p) return *this; } -window_size_t Options::getSeqWindowSize() +RouteList Options::getRoutes() { - return seq_window_size_; + Lock lock(mutex); + return routes_; } -Options& Options::setSeqWindowSize(window_size_t s) + + +sender_id_t Options::getSenderId() { - seq_window_size_ = s; + return sender_id_; +} + +Options& Options::setSenderId(sender_id_t s) +{ + sender_id_ = s; return *this; } -std::string Options::getCipher() +mux_t Options::getMux() { Lock lock(mutex); - return cipher_; + return mux_; } -Options& Options::setCipher(std::string c) +Options& Options::setMux(mux_t m) { Lock lock(mutex); - cipher_ = c; + mux_ = m; return *this; } -std::string Options::getKdPrf() +window_size_t Options::getSeqWindowSize() { - Lock lock(mutex); - return kd_prf_; + return seq_window_size_; } -Options& Options::setKdPrf(std::string k) +Options& Options::setSeqWindowSize(window_size_t s) { - Lock lock(mutex); - kd_prf_ = k; + seq_window_size_ = s; return *this; } -int8_t Options::getLdKdr() + + +std::string Options::getCipher() { Lock lock(mutex); - return ld_kdr_; + return cipher_; } -Options& Options::setLdKdr(int8_t l) +Options& Options::setCipher(std::string c) { Lock lock(mutex); - ld_kdr_ = l; + cipher_ = c; return *this; } @@ -696,16 +781,29 @@ Options& Options::setAuthAlgo(std::string a) return *this; } -mux_t Options::getMux() +std::string Options::getKdPrf() { Lock lock(mutex); - return mux_; + return kd_prf_; } -Options& Options::setMux(mux_t m) +Options& Options::setKdPrf(std::string k) { Lock lock(mutex); - mux_ = m; + kd_prf_ = k; + return *this; +} + +int8_t Options::getLdKdr() +{ + Lock lock(mutex); + return ld_kdr_; +} + +Options& Options::setLdKdr(int8_t l) +{ + Lock lock(mutex); + ld_kdr_ = l; return *this; } @@ -734,10 +832,3 @@ Options& Options::setSalt(std::string s) salt_ = s; return *this; } - -RouteList Options::getRoutes() -{ - Lock lock(mutex); - return routes_; -} - diff --git a/src/options.h b/src/options.h index 87c6827..6cd8b8a 100644 --- a/src/options.h +++ b/src/options.h @@ -37,29 +37,50 @@ #include "threadUtils.hpp" #include <list> -typedef struct +class syntax_error : public std::runtime_error { - std::string host; - std::string port; -} OptionConnectTo; +public: + syntax_error(std::string t, u_int32_t p) : runtime_error(t), pos(p) {}; + u_int32_t pos; +}; +std::ostream& operator<<(std::ostream& stream, syntax_error const& error); -typedef struct +class OptionHost { - std::string net_addr; - u_int16_t prefix_length; -} OptionRoute; +public: + OptionHost() : addr(""), port("") {}; + OptionHost(std::string addrPort) { init(addrPort); }; + OptionHost(std::string a, std::string p) : addr(a), port(p) {}; + + void init(std::string addrPort); + + std::string addr; + std::string port; +}; +typedef std::list<OptionHost> HostList; +std::istream& operator>>(std::istream& stream, OptionHost& host); -typedef std::list<OptionRoute> RouteList; +class OptionRoute +{ +public: + OptionRoute() : net_addr(""), prefix_length(0) {}; + OptionRoute(std::string route) { init(route); }; + OptionRoute(std::string n, u_int16_t p) : net_addr(n), prefix_length(p) {}; + void init(std::string route); -typedef std::list<OptionConnectTo> ConnectToList; + std::string net_addr; + u_int16_t prefix_length; +}; +typedef std::list<OptionRoute> RouteList; +std::istream& operator>>(std::istream& stream, OptionRoute& route); class Options { public: static Options& instance(); - int32_t parse(int argc, char* argv[]); + bool parse(int argc, char* argv[]); void printUsage(); void printOptions(); @@ -75,25 +96,29 @@ public: Options& setChrootDir(std::string c); std::string getPidFile(); Options& setPidFile(std::string p); - sender_id_t getSenderId(); - Options& setSenderId(sender_id_t s); + + std::string getFileName(); + Options& setFileName(std::string f); + std::string getBindToAddr(); + Options& setBindToAddr(std::string b); + std::string getBindToPort(); + Options& setBindToPort(std::string b); + std::string getLocalAddr(); Options& setLocalAddr(std::string l); - std::string getLocalSyncAddr(); - Options& setLocalSyncAddr(std::string l); - std::string getRemoteSyncAddr(); - Options& setRemoteSyncAddr(std::string l); - std::string getRemoteSyncPort(); - Options& setRemoteSyncPort(std::string l); std::string getLocalPort(); Options& setLocalPort(std::string l); std::string getRemoteAddr(); Options& setRemoteAddr(std::string r); - std::string getLocalSyncPort(); - Options& setLocalSyncPort(std::string l); std::string getRemotePort(); Options& setRemotePort(std::string r); - Options& setRemoteAddrPort(std::string addr, std::string port); + + std::string getLocalSyncAddr(); + Options& setLocalSyncAddr(std::string l); + std::string getLocalSyncPort(); + Options& setLocalSyncPort(std::string l); + HostList getRemoteSyncHosts(); + std::string getDevName(); Options& setDevName(std::string d); std::string getDevType(); @@ -104,24 +129,27 @@ public: Options& setIfconfigParamRemoteNetmask(std::string i); std::string getPostUpScript(); Options& setPostUpScript(std::string p); + RouteList getRoutes(); + + sender_id_t getSenderId(); + Options& setSenderId(sender_id_t s); + mux_t getMux(); + Options& setMux(mux_t m); window_size_t getSeqWindowSize(); Options& setSeqWindowSize(window_size_t s); + std::string getCipher(); Options& setCipher(std::string c); + std::string getAuthAlgo(); + Options& setAuthAlgo(std::string a); std::string getKdPrf(); Options& setKdPrf(std::string k); int8_t getLdKdr(); Options& setLdKdr(int8_t l); - std::string getAuthAlgo(); - Options& setAuthAlgo(std::string a); - ConnectToList getConnectTo(); - Options& setMux(mux_t m); - mux_t getMux(); Options& setKey(std::string k); Buffer getKey(); Options& setSalt(std::string s); Buffer getSalt(); - RouteList getRoutes(); private: @@ -140,40 +168,44 @@ private: }; friend class instanceCleaner; - static bool splitAndAddHostPort(std::string hostPort, ConnectToList& list); - ::Mutex mutex; - ConnectToList connect_to_; std::string progname_; bool daemonize_; bool chroot_; std::string username_; std::string chroot_dir_; std::string pid_file_; - sender_id_t sender_id_; + + std::string file_name_; + OptionHost bind_to_; + std::string local_addr_; - std::string local_sync_addr_; std::string local_port_; - std::string local_sync_port_; - std::string remote_sync_addr_; - std::string remote_sync_port_; std::string remote_addr_; std::string remote_port_; + + std::string local_sync_addr_; + std::string local_sync_port_; + HostList remote_sync_hosts_; + std::string dev_name_; std::string dev_type_; std::string ifconfig_param_local_; std::string ifconfig_param_remote_netmask_; std::string post_up_script_; + RouteList routes_; + + sender_id_t sender_id_; + mux_t mux_; window_size_t seq_window_size_; + std::string cipher_; + std::string auth_algo_; std::string kd_prf_; int8_t ld_kdr_; - std::string auth_algo_; - mux_t mux_; Buffer key_; Buffer salt_; - RouteList routes_; }; extern Options& gOpt; diff --git a/src/threadParam.h b/src/threadParam.h index 16a18fe..60741d4 100644 --- a/src/threadParam.h +++ b/src/threadParam.h @@ -41,12 +41,12 @@ class ThreadParam { public: - ThreadParam(TunDevice& dev_,PacketSource& src_,OptionConnectTo & connto_) + ThreadParam(TunDevice& dev_,PacketSource& src_,OptionHost & connto_) : dev(dev_),src(src_),connto(connto_) {}; TunDevice& dev; PacketSource& src; - OptionConnectTo & connto; + OptionHost & connto; }; #endif |