From c07b837dbad1ac176a6c18062dab9184e7080309 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 21 Mar 2009 13:31:03 +0100 Subject: Net::Connection-Klasse zur besseren Strukturierung gekapselt --- src/Core/ConnectionManager.cpp | 149 +++++++++++++-------- src/Core/ConnectionManager.h | 56 ++++++-- .../DaemonCommandRequestHandler.cpp | 2 +- .../RequestHandlers/DaemonCommandRequestHandler.h | 2 +- .../RequestHandlers/DaemonFSInfoRequestHandler.cpp | 2 +- .../RequestHandlers/DaemonFSInfoRequestHandler.h | 2 +- .../RequestHandlers/DaemonListRequestHandler.h | 2 +- .../RequestHandlers/DaemonStatusRequestHandler.cpp | 2 +- .../RequestHandlers/DaemonStatusRequestHandler.h | 2 +- .../RequestHandlers/GSSAPIAuthRequestHandler.cpp | 6 +- .../RequestHandlers/GSSAPIAuthRequestHandler.h | 2 +- src/Core/RequestHandlers/IdentifyRequestHandler.h | 2 +- src/Core/RequestHandlers/LogRequestHandler.h | 2 +- src/Core/RequestHandlers/UserListRequestHandler.h | 2 +- src/Core/Requests/CommandRequest.h | 2 +- src/Core/Requests/DaemonStateUpdateRequest.h | 2 +- 16 files changed, 157 insertions(+), 80 deletions(-) (limited to 'src/Core') diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 3ce52fc..3d252c2 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -45,12 +45,49 @@ namespace Core { ConnectionManager ConnectionManager::connectionManager; +bool ConnectionManager::Connection::send(const Net::Packet &packet) { + return connection->send(packet); +} + +ConnectionManager::Connection::Connection(Net::ServerConnection *connection0, ConnectionType type0) +: connection(connection0), type(type0), hostInfo(0) { + connection->signalReceive().connect(sigc::mem_fun(this, &Connection::receive)); +} + +ConnectionManager::Connection::~Connection() { + delete connection; +} + +bool ConnectionManager::Connection::isConnected() const { + return connection->isConnected(); +} + +bool ConnectionManager::Connection::disconnect() { + connection->disconnect(); + + return true; +} + +void* ConnectionManager::Connection::getCertificate(size_t *size) const { + const gnutls_datum_t *cert = connection->getCertificate(); + + *size = cert->size; + return cert->data; +} + +void* ConnectionManager::Connection::getPeerCertificate(size_t *size) const { + const gnutls_datum_t *cert = connection->getPeerCertificate(); + + *size = cert->size; + return cert->data; +} -void ConnectionManager::updateState(const std::string &name, Common::HostInfo::State state) { - daemonInfo[name].setState(state); +void ConnectionManager::updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state) { + hostInfo->setState(state); - for(std::list::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) { - Common::RequestManager::get()->sendRequest(*con, Common::Request::slot_type(), name, state); + for(std::list::iterator con = connections.begin(); con != connections.end(); ++con) { + if((*con)->getConnectionType() == Connection::CLIENT) + Common::RequestManager::get()->sendRequest(*con, Common::Request::slot_type(), hostInfo->getName(), state); } } @@ -92,7 +129,6 @@ bool ConnectionManager::handleConfigEntry(const Common::ConfigEntry &entry, bool if(entry[0].getSize() == 1) { if(entry[1].empty()) { daemonInfo.insert(std::make_pair(entry[0][0], Common::HostInfo(entry[0][0]))); - identifiedDaemonConnections.insert(std::make_pair(entry[0][0], 0)); return true; } @@ -146,11 +182,9 @@ void ConnectionManager::doInit() { } void ConnectionManager::doDeinit() { - for(std::list::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) + for(std::list::iterator con = connections.begin(); con != connections.end(); ++con) delete *con; - for(std::list::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) - delete *con; Common::RequestManager::get()->unregisterPacketType("AuthGSSAPI"); Common::RequestManager::get()->unregisterPacketType("DaemonCommand"); @@ -166,19 +200,15 @@ void ConnectionManager::doDeinit() { Net::Connection::deinit(); } -void ConnectionManager::handleConnections(std::list& connections) { - for(std::list::iterator con = connections.begin(); con != connections.end();) { +void ConnectionManager::run() { + // TODO Logging + + Net::FdManager::get()->run(); + + for(std::list::iterator con = connections.begin(); con != connections.end();) { if(!(*con)->isConnected()) { - if((*con)->isIdentified()) { - for(std::map::iterator idCon = identifiedDaemonConnections.begin(); idCon != identifiedDaemonConnections.end(); ++idCon) { - if(idCon->second == *con) { - idCon->second = 0; - - updateState(idCon->first, Common::HostInfo::INACTIVE); - break; - } - } - } + if((*con)->isIdentified()) + updateState((*con)->getHostInfo(), Common::HostInfo::INACTIVE); Common::RequestManager::get()->unregisterConnection(*con); delete *con; @@ -187,69 +217,78 @@ void ConnectionManager::handleConnections(std::list& con else ++con; } -} - -void ConnectionManager::run() { - // TODO Logging - - Net::FdManager::get()->run(); - - handleConnections(daemonConnections); - handleConnections(clientConnections); for(std::list::iterator listener = listeners.begin(); listener != listeners.end(); ++listener) { Net::ServerConnection *con; while((con = (*listener)->getConnection()) != 0) { - (con->isDaemonConnection() ? daemonConnections : clientConnections).push_back(con); - Common::RequestManager::get()->registerConnection(con); + Connection *connection = new Connection(con, + con->isDaemonConnection() ? Connection::DAEMON : Connection::CLIENT); + connections.push_back(connection); + Common::RequestManager::get()->registerConnection(connection); } } } -Net::Connection* ConnectionManager::getDaemonConnection(const std::string &name) const throw (Common::Exception&) { - std::map::const_iterator daemon = identifiedDaemonConnections.find(name); - if(daemon == identifiedDaemonConnections.end()) +Common::Connection* ConnectionManager::getDaemonConnection(const std::string &name) const throw (Common::Exception&) { + const Common::HostInfo *hostInfo; + + try { + hostInfo = &daemonInfo.at(name); + } + catch(std::out_of_range&) { throw Common::Exception(Common::Exception::UNKNOWN_DAEMON); + } - if(!daemon->second) - throw Common::Exception(Common::Exception::NOT_AVAILABLE); + if(hostInfo->getState() != Common::HostInfo::INACTIVE) { + for(std::list::const_iterator it = connections.begin(); it != connections.end(); ++it) { + if((*it)->getHostInfo() == hostInfo) { + return *it; + } + } + } - return daemon->second; + throw(Common::Exception::NOT_AVAILABLE); } -std::string ConnectionManager::getDaemonName(const Net::Connection *con) const throw (Common::Exception&) { - for(std::map::const_iterator daemon = identifiedDaemonConnections.begin(); daemon != identifiedDaemonConnections.end(); ++daemon) { - if(daemon->second == con) - return daemon->first; +std::string ConnectionManager::getDaemonName(const Common::Connection *con) const throw (Common::Exception&) { + const Connection *connection = dynamic_cast(con); + + if(connection) { + if(connection->isIdentified()) { + return connection->getHostInfo()->getName(); + } } throw Common::Exception(Common::Exception::UNKNOWN_DAEMON); } -void ConnectionManager::identifyDaemonConnection(Net::Connection *connection, const std::string &name) throw (Common::Exception&) { +void ConnectionManager::identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Common::Exception&) { // TODO Logging - if(connection->isIdentified()) - throw Common::Exception(Common::Exception::ALREADY_IDENTIFIED); + Connection *connection = dynamic_cast(con); - std::list::iterator con = std::find(daemonConnections.begin(), daemonConnections.end(), connection); - if(con == daemonConnections.end()) + if(!connection || (connection->getConnectionType() != Connection::DAEMON)) throw Common::Exception(Common::Exception::INVALID_ACTION); - std::map::iterator idCon = identifiedDaemonConnections.find(name); - if(idCon == identifiedDaemonConnections.end()) { + if(connection->isIdentified()) + throw Common::Exception(Common::Exception::ALREADY_IDENTIFIED); + + if(daemonInfo.count(name) == 0) throw Common::Exception(Common::Exception::UNKNOWN_DAEMON); - } - if(idCon->second) { - idCon->second->disconnect(); - Common::Logger::log(Common::Logger::WARNING, "Disconnecting old connection."); + Common::HostInfo *hostInfo = &daemonInfo[name]; + + if(hostInfo->getState() != Common::HostInfo::INACTIVE) { + try { + getDaemonConnection(name)->disconnect(); + Common::Logger::log(Common::Logger::WARNING, "Disconnecting old connection."); + } + catch(Common::Exception&) {} } - idCon->second = *con; - updateState(idCon->first, Common::HostInfo::RUNNING); - connection->setIdentified(); + connection->identify(hostInfo); + updateState(hostInfo, Common::HostInfo::RUNNING); Common::Logger::logf("Identified as '%s'.", name.c_str()); } diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index 4623ab6..75395ad 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -36,7 +36,6 @@ namespace Mad { namespace Net { class Listener; -class Connection; class ServerConnection; class Packet; } @@ -45,6 +44,47 @@ namespace Core { class ConnectionManager : public Common::Configurable, public Common::Initializable { private: + class Connection : public Common::Connection { + public: + enum ConnectionType { + DAEMON, CLIENT + }; + + private: + Net::ServerConnection *connection; + ConnectionType type; + Common::HostInfo *hostInfo; + + protected: + virtual bool send(const Net::Packet &packet); + + public: + Connection(Net::ServerConnection *connection0, ConnectionType type0); + virtual ~Connection(); + + bool isConnected() const; + + virtual bool disconnect(); + virtual void* getCertificate(size_t *size) const; + virtual void* getPeerCertificate(size_t *size) const; + + ConnectionType getConnectionType() const { + return type; + } + + Common::HostInfo *getHostInfo() const { + return hostInfo; + } + + bool isIdentified() const { + return hostInfo; + } + + void identify(Common::HostInfo *info) { + hostInfo = info; + } + }; + static ConnectionManager connectionManager; std::string x509TrustFile, x509CrlFile, x509CertFile, x509KeyFile; @@ -52,19 +92,15 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa std::vector listenerAddresses; std::list listeners; - std::list daemonConnections; - std::list clientConnections; + std::list connections; std::map daemonInfo; - std::map identifiedDaemonConnections; // Prevent shallow copy ConnectionManager(const ConnectionManager &o); ConnectionManager& operator=(const ConnectionManager &o); - void handleConnections(std::list &connections); - - void updateState(const std::string &name, Common::HostInfo::State state); + void updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state); ConnectionManager() {} @@ -82,10 +118,10 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa void run(); - Net::Connection* getDaemonConnection(const std::string &name) const throw (Common::Exception&); - std::string getDaemonName(const Net::Connection *con) const throw (Common::Exception&); + Common::Connection* getDaemonConnection(const std::string &name) const throw (Common::Exception&); + std::string getDaemonName(const Common::Connection *con) const throw (Common::Exception&); - void identifyDaemonConnection(Net::Connection *connection, const std::string &name) throw (Common::Exception&); + void identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Common::Exception&); std::vector getDaemonList() const; }; diff --git a/src/Core/RequestHandlers/DaemonCommandRequestHandler.cpp b/src/Core/RequestHandlers/DaemonCommandRequestHandler.cpp index c9a4530..0e316bc 100644 --- a/src/Core/RequestHandlers/DaemonCommandRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonCommandRequestHandler.cpp @@ -45,7 +45,7 @@ void DaemonCommandRequestHandler::handlePacket(const Common::XmlPacket &packet) std::string command = packet["command"]; try { - Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); + Common::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); Common::RequestManager::get()->sendRequest(daemonCon, sigc::mem_fun(this, &DaemonCommandRequestHandler::requestFinished), command == "reboot"); } diff --git a/src/Core/RequestHandlers/DaemonCommandRequestHandler.h b/src/Core/RequestHandlers/DaemonCommandRequestHandler.h index 9e089e3..aab9539 100644 --- a/src/Core/RequestHandlers/DaemonCommandRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonCommandRequestHandler.h @@ -36,7 +36,7 @@ class DaemonCommandRequestHandler : public Common::RequestHandler { virtual void handlePacket(const Common::XmlPacket &packet); public: - DaemonCommandRequestHandler(Net::Connection *connection, uint16_t requestId) + DaemonCommandRequestHandler(Common::Connection *connection, uint16_t requestId) : RequestHandler(connection, requestId) {} }; diff --git a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp index ae12a8e..547611c 100644 --- a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.cpp @@ -44,7 +44,7 @@ void DaemonFSInfoRequestHandler::handlePacket(const Common::XmlPacket &packet) { // TODO Require authentication try { - Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); + Common::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); Common::RequestManager::get()->sendRequest(daemonCon, sigc::mem_fun(this, &DaemonFSInfoRequestHandler::requestFinished)); } diff --git a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h index 6a70c5e..66800d8 100644 --- a/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonFSInfoRequestHandler.h @@ -36,7 +36,7 @@ class DaemonFSInfoRequestHandler : public Common::RequestHandler { virtual void handlePacket(const Common::XmlPacket &packet); public: - DaemonFSInfoRequestHandler(Net::Connection *connection, uint16_t requestId) + DaemonFSInfoRequestHandler(Common::Connection *connection, uint16_t requestId) : RequestHandler(connection, requestId) {} }; diff --git a/src/Core/RequestHandlers/DaemonListRequestHandler.h b/src/Core/RequestHandlers/DaemonListRequestHandler.h index 07badae..a469603 100644 --- a/src/Core/RequestHandlers/DaemonListRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonListRequestHandler.h @@ -31,7 +31,7 @@ class DaemonListRequestHandler : public Common::RequestHandler { virtual void handlePacket(const Common::XmlPacket &packet); public: - DaemonListRequestHandler(Net::Connection *connection, uint16_t requestId) + DaemonListRequestHandler(Common::Connection *connection, uint16_t requestId) : RequestHandler(connection, requestId) {} }; diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp index 66c3aa1..a84307b 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp @@ -46,7 +46,7 @@ void DaemonStatusRequestHandler::handlePacket(const Common::XmlPacket &packet) { std::string daemonName = packet["daemonName"]; try { - Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); + Common::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); Common::RequestManager::get()->sendRequest(daemonCon, sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)); } diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h index f232649..5ef6089 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h @@ -36,7 +36,7 @@ class DaemonStatusRequestHandler : public Common::RequestHandler { virtual void handlePacket(const Common::XmlPacket &packet); public: - DaemonStatusRequestHandler(Net::Connection *connection, uint16_t requestId) + DaemonStatusRequestHandler(Common::Connection *connection, uint16_t requestId) : RequestHandler(connection, requestId) {} }; diff --git a/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.cpp b/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.cpp index ad0a293..7c2b482 100644 --- a/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.cpp +++ b/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.cpp @@ -84,10 +84,12 @@ void GSSAPIAuthRequestHandler::handlePacket(const Common::XmlPacket &packet) { if(!packet["binary"].isEmpty()) return; - const gnutls_datum_t *cert = getConnection()->getCertificate(); + /*const gnutls_datum_t *cert = getConnection()->getCertificate(); recvBuffer.length = cert->size; - recvBuffer.value = cert->data; + recvBuffer.value = cert->data;*/ + + recvBuffer.value = getConnection()->getCertificate(&recvBuffer.length); majStat = gss_get_mic(&minStat, gssContext, GSS_C_QOP_DEFAULT, &recvBuffer, &sendBuffer); diff --git a/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.h b/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.h index 06c1886..dd4d564 100644 --- a/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.h +++ b/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.h @@ -37,7 +37,7 @@ class GSSAPIAuthRequestHandler : public Common::RequestHandler { virtual void handlePacket(const Common::XmlPacket &packet); public: - GSSAPIAuthRequestHandler(Net::Connection *connection, uint16_t requestId) + GSSAPIAuthRequestHandler(Common::Connection *connection, uint16_t requestId) : RequestHandler(connection, requestId), gssContext(GSS_C_NO_CONTEXT), gssContinue(true), sentSignature(false) {} }; diff --git a/src/Core/RequestHandlers/IdentifyRequestHandler.h b/src/Core/RequestHandlers/IdentifyRequestHandler.h index 37aa74b..016f777 100644 --- a/src/Core/RequestHandlers/IdentifyRequestHandler.h +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.h @@ -31,7 +31,7 @@ class IdentifyRequestHandler : public Common::RequestHandler { virtual void handlePacket(const Common::XmlPacket &packet); public: - IdentifyRequestHandler(Net::Connection *connection, uint16_t requestId) + IdentifyRequestHandler(Common::Connection *connection, uint16_t requestId) : RequestHandler(connection, requestId) {} }; diff --git a/src/Core/RequestHandlers/LogRequestHandler.h b/src/Core/RequestHandlers/LogRequestHandler.h index 9cf63ec..c391346 100644 --- a/src/Core/RequestHandlers/LogRequestHandler.h +++ b/src/Core/RequestHandlers/LogRequestHandler.h @@ -31,7 +31,7 @@ class LogRequestHandler : public Common::RequestHandler { virtual void handlePacket(const Common::XmlPacket &packet); public: - LogRequestHandler(Net::Connection *connection, uint16_t requestId) + LogRequestHandler(Common::Connection *connection, uint16_t requestId) : RequestHandler(connection, requestId) {} }; diff --git a/src/Core/RequestHandlers/UserListRequestHandler.h b/src/Core/RequestHandlers/UserListRequestHandler.h index 8eae11e..a80c451 100644 --- a/src/Core/RequestHandlers/UserListRequestHandler.h +++ b/src/Core/RequestHandlers/UserListRequestHandler.h @@ -38,7 +38,7 @@ class UserListRequestHandler : public Common::RequestHandler { virtual void handlePacket(const Common::XmlPacket &packet); public: - UserListRequestHandler(Net::Connection *connection, uint16_t requestId) + UserListRequestHandler(Common::Connection *connection, uint16_t requestId) : RequestHandler(connection, requestId) {} }; diff --git a/src/Core/Requests/CommandRequest.h b/src/Core/Requests/CommandRequest.h index 2214a29..2e4d1de 100644 --- a/src/Core/Requests/CommandRequest.h +++ b/src/Core/Requests/CommandRequest.h @@ -34,7 +34,7 @@ class CommandRequest : public Common::Request { virtual void sendRequest(); public: - CommandRequest(Net::Connection *connection, uint16_t requestId, slot_type slot, bool reboot0) + CommandRequest(Common::Connection *connection, uint16_t requestId, slot_type slot, bool reboot0) : Common::Request(connection, requestId, slot), reboot(reboot0) {} }; diff --git a/src/Core/Requests/DaemonStateUpdateRequest.h b/src/Core/Requests/DaemonStateUpdateRequest.h index f310119..a1a8bb3 100644 --- a/src/Core/Requests/DaemonStateUpdateRequest.h +++ b/src/Core/Requests/DaemonStateUpdateRequest.h @@ -36,7 +36,7 @@ class DaemonStateUpdateRequest : public Common::Request { virtual void sendRequest(); public: - DaemonStateUpdateRequest(Net::Connection *connection, uint16_t requestId, slot_type slot, const std::string &name0, Common::HostInfo::State state0) + DaemonStateUpdateRequest(Common::Connection *connection, uint16_t requestId, slot_type slot, const std::string &name0, Common::HostInfo::State state0) : Common::Request(connection, requestId, slot), name(name0), state(state0) {} }; -- cgit v1.2.3