From ea607fcc37694c55e10d0180bdaf8a676bbdf7c8 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 25 Nov 2008 04:13:55 +0000 Subject: options parser support ipv6 now fixed anytun-controld - fixed log issues - added better error output - some cleanups --- src/anyCtrOptions.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 9 deletions(-) (limited to 'src/anyCtrOptions.cpp') diff --git a/src/anyCtrOptions.cpp b/src/anyCtrOptions.cpp index 7543206..2256677 100644 --- a/src/anyCtrOptions.cpp +++ b/src/anyCtrOptions.cpp @@ -61,7 +61,7 @@ Options::Options() chroot_dir_ = "/var/run/anytun-controld"; pid_file_ = ""; bind_to_addr_ = "127.0.0.1"; - bind_to_port_ = 4445; + bind_to_port_ = "4445"; } Options::~Options() @@ -156,12 +156,61 @@ bool Options::parse(int argc, char* argv[]) return false; } - if(control_host != "") { - std::stringstream tmp_stream(control_host); - getline(tmp_stream,bind_to_addr_,':'); - if(!tmp_stream.good()) - return false; - tmp_stream >> bind_to_port_; + if(control_host != "") + return splitAndSetHostPort(control_host); + + return true; +} + +bool Options::splitAndSetHostPort(std::string hostPort) +{ + if(hostPort.length() >= 2 && hostPort[0] == ':' && hostPort[1] != ':') { + bind_to_addr_ = ""; + hostPort.erase(0,1); + std::stringstream tmp_stream(hostPort); + tmp_stream >> bind_to_port_; + return true; + } + + 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); + } + + if(hasPort) { + std::stringstream tmp_stream(hostPort); + + getline(tmp_stream, bind_to_addr_, '/'); + if(!tmp_stream.good()) + return false; + + tmp_stream >> bind_to_port_; + } + else { + bind_to_addr_ = hostPort; + bind_to_port_ = "2323"; // default sync port } return true; @@ -190,6 +239,8 @@ void Options::printOptions() std::cout << "username='" << username_ << "'" << std::endl; std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl; std::cout << "pid_file='" << pid_file_ << "'" << std::endl; + std::cout << "bind_to_addr_='" << bind_to_addr_ << "'" << std::endl; + std::cout << "bind_to_port_='" << bind_to_port_ << "'" << std::endl; } std::string Options::getProgname() @@ -293,12 +344,12 @@ Options& Options::setBindToAddr(std::string b) return *this; } -uint16_t Options::getBindToPort() +std::string Options::getBindToPort() { return bind_to_port_; } -Options& Options::setBindToPort(uint16_t b) +Options& Options::setBindToPort(std::string b) { bind_to_port_ = b; return *this; -- cgit v1.2.3