From 94c00010a0280b8adc220377916a65b8dba08b62 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 6 Aug 2009 23:41:04 +0200 Subject: Added IPv6 support --- config.h.in | 2 +- src/Server/ConnectionManager.cpp | 41 +++++++++++++++++++++++++--------------- src/Server/ConnectionManager.h | 2 +- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/config.h.in b/config.h.in index 80e4694..5e7ba50 100644 --- a/config.h.in +++ b/config.h.in @@ -1,3 +1,3 @@ #define MODULE_SUFFIX "${CMAKE_SHARED_MODULE_SUFFIX}" -#define DEFAULT_PORT 6666 \ No newline at end of file +#define DEFAULT_PORT 6666 diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp index 96f383a..8fe6408 100644 --- a/src/Server/ConnectionManager.cpp +++ b/src/Server/ConnectionManager.cpp @@ -71,31 +71,42 @@ void* ConnectionManager::ServerConnection::getPeerCertificate(size_t *size) cons return cert->data; }*/ -boost::asio::ip::tcp::endpoint ConnectionManager::parseAddress(const std::string &str) { - // TODO IPv6 +boost::asio::ip::tcp::endpoint ConnectionManager::parseAddress(const std::string &str) throw(Core::Exception) { + try { + if(str == "*") + return boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), DEFAULT_PORT); - if(str == "*") - return boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), DEFAULT_PORT); + boost::smatch match; - boost::smatch match; + static const boost::regex r1("\\*:(\\d+)", boost::regex_constants::perl); + if(boost::regex_match(str, match, r1)) + return boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), std::atoi(match[1].str().c_str())); - static const boost::regex r1("\\*:(\\d+)", boost::regex_constants::perl); - if(boost::regex_match(str, match, r1)) { - return boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), std::atoi(match[1].str().c_str())); - } + static const boost::regex r2("\\[(.+)\\]:(\\d+)", boost::regex_constants::perl); + if(boost::regex_match(str, match, r2)) + return boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v6::from_string(match[1].str()), + std::atoi(match[2].str().c_str())); + + static const boost::regex r3("\\[(.+)\\]", boost::regex_constants::perl); + if(boost::regex_match(str, match, r3)) + return boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v6::from_string(match[1].str()), DEFAULT_PORT); - static const boost::regex r2("(.+):(\\d+)", boost::regex_constants::perl); - if(boost::regex_match(str, match, r2)) { - return boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(match[1].str()), std::atoi(match[2].str().c_str())); + static const boost::regex r4("((?:\\d{1,3}\\.){3}\\d{1,3}):(\\d+)", boost::regex_constants::perl); + if(boost::regex_match(str, match, r4)) + return boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string(match[1].str()), std::atoi(match[2].str().c_str())); + + static const boost::regex r5("((?:\\d{1,3}\\.){3}\\d{1,3})", boost::regex_constants::perl); + if(boost::regex_match(str, match, r5)) + return boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string(match[1].str()), DEFAULT_PORT); } + catch(...) {} - return boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(str), DEFAULT_PORT); + throw Core::Exception(Core::Exception::INVALID_INPUT); } void ConnectionManager::updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state) { hostInfo->setState(state); - for(std::set >::iterator con = connections.begin(); con != connections.end(); ++con) { if((*con)->getConnectionType() == ServerConnection::CLIENT) { boost::shared_ptr request(new Requests::DaemonStateUpdateRequest(application, hostInfo->getName(), state)); @@ -113,7 +124,7 @@ bool ConnectionManager::handleConfigEntry(const Core::ConfigEntry &entry, bool h listenerAddresses.push_back(parseAddress(entry[0][0])); } catch(Core::Exception &e) { - // TODO Log error + application->logf(Core::LoggerBase::WARNING, "ConnectionManager: Invalid listen address '%s'", entry[0][0].c_str()); } return true; diff --git a/src/Server/ConnectionManager.h b/src/Server/ConnectionManager.h index 5f41afa..6fc6013 100644 --- a/src/Server/ConnectionManager.h +++ b/src/Server/ConnectionManager.h @@ -109,7 +109,7 @@ class ConnectionManager : public Core::Configurable, private boost::noncopyable boost::shared_ptr daemonRequestHandlerGroup; boost::shared_ptr userRequestHandlerGroup; - static boost::asio::ip::tcp::endpoint parseAddress(const std::string &str); + static boost::asio::ip::tcp::endpoint parseAddress(const std::string &str) throw(Core::Exception); void updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state); -- cgit v1.2.3