diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-15 02:19:06 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-15 02:19:06 +0200 |
commit | fbe26b0e48e6f3714900833174fcf42196e86fc8 (patch) | |
tree | 0f9528f2ad25c50e55a13e6fd60bf545f48ebf88 | |
parent | 082dac7a8cb39ec1b005680680c4f3e1e8ddc256 (diff) | |
download | mad-fbe26b0e48e6f3714900833174fcf42196e86fc8.tar mad-fbe26b0e48e6f3714900833174fcf42196e86fc8.zip |
Identifikationsinformationen im ConnectionManager speichern
-rw-r--r-- | src/Common/RequestHandlers/Makefile.am | 4 | ||||
-rw-r--r-- | src/Common/RequestHandlers/Makefile.in | 8 | ||||
-rw-r--r-- | src/Common/RequestManager.cpp | 2 | ||||
-rw-r--r-- | src/Core/ConnectionManager.cpp | 46 | ||||
-rw-r--r-- | src/Core/ConnectionManager.h | 7 | ||||
-rw-r--r-- | src/Core/RequestHandlers/IdentifyRequestHandler.cpp (renamed from src/Common/RequestHandlers/IdentifyRequestHandler.cpp) | 5 | ||||
-rw-r--r-- | src/Core/RequestHandlers/IdentifyRequestHandler.h (renamed from src/Common/RequestHandlers/IdentifyRequestHandler.h) | 6 | ||||
-rw-r--r-- | src/Core/RequestHandlers/Makefile.am | 4 | ||||
-rw-r--r-- | src/Core/RequestHandlers/Makefile.in | 8 | ||||
-rw-r--r-- | src/Net/ClientConnection.cpp | 7 | ||||
-rw-r--r-- | src/Net/Connection.cpp | 10 | ||||
-rw-r--r-- | src/Net/Connection.h | 12 | ||||
-rw-r--r-- | src/mad-core.cpp | 1 |
13 files changed, 83 insertions, 37 deletions
diff --git a/src/Common/RequestHandlers/Makefile.am b/src/Common/RequestHandlers/Makefile.am index a8aac28..9133148 100644 --- a/src/Common/RequestHandlers/Makefile.am +++ b/src/Common/RequestHandlers/Makefile.am @@ -1,5 +1,5 @@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = DisconnectRequestHandler.cpp IdentifyRequestHandler.cpp +librequesthandlers_la_SOURCES = DisconnectRequestHandler.cpp -noinst_HEADERS = DisconnectRequestHandler.h IdentifyRequestHandler.h +noinst_HEADERS = DisconnectRequestHandler.h diff --git a/src/Common/RequestHandlers/Makefile.in b/src/Common/RequestHandlers/Makefile.in index f149d99..c55394a 100644 --- a/src/Common/RequestHandlers/Makefile.in +++ b/src/Common/RequestHandlers/Makefile.in @@ -45,8 +45,7 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequesthandlers_la_LIBADD = -am_librequesthandlers_la_OBJECTS = DisconnectRequestHandler.lo \ - IdentifyRequestHandler.lo +am_librequesthandlers_la_OBJECTS = DisconnectRequestHandler.lo librequesthandlers_la_OBJECTS = $(am_librequesthandlers_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -185,8 +184,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = DisconnectRequestHandler.cpp IdentifyRequestHandler.cpp -noinst_HEADERS = DisconnectRequestHandler.h IdentifyRequestHandler.h +librequesthandlers_la_SOURCES = DisconnectRequestHandler.cpp +noinst_HEADERS = DisconnectRequestHandler.h all: all-am .SUFFIXES: @@ -239,7 +238,6 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DisconnectRequestHandler.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IdentifyRequestHandler.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index 93bbea5..8ebfce0 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -20,7 +20,6 @@ #include "RequestManager.h" #include "Request.h" #include "RequestHandlers/DisconnectRequestHandler.h" -#include "RequestHandlers/IdentifyRequestHandler.h" #include <iostream> @@ -140,7 +139,6 @@ void RequestManager::unregisterPacketType(Net::Packet::Type type) { RequestManager::RequestManager(bool core) : requestId(core ? -2 : -1) { registerPacketType<RequestHandlers::DisconnectRequestHandler>(Net::Packet::DISCONNECT); - registerPacketType<RequestHandlers::IdentifyRequestHandler>(Net::Packet::IDENTIFY); } RequestManager::~RequestManager() { diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 78497e7..72bf07f 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -22,10 +22,15 @@ #include "RequestHandlers/CoreStatusRequestHandler.h" #include "RequestHandlers/DaemonStatusRequestHandler.h" #include "RequestHandlers/GSSAPIAuthRequestHandler.h" +#include "RequestHandlers/IdentifyRequestHandler.h" #include <Net/ServerConnection.h> #include <Net/Packet.h> #include <Net/Listener.h> + #include <unistd.h> +#include <algorithm> + +#include <iostream> namespace Mad { namespace Core { @@ -63,6 +68,7 @@ ConnectionManager::ConnectionManager() { Common::RequestManager::getRequestManager()->registerPacketType<RequestHandlers::CoreStatusRequestHandler>(Net::Packet::CORE_STATUS); Common::RequestManager::getRequestManager()->registerPacketType<RequestHandlers::DaemonStatusRequestHandler>(Net::Packet::DAEMON_STATUS); Common::RequestManager::getRequestManager()->registerPacketType<RequestHandlers::GSSAPIAuthRequestHandler>(Net::Packet::GSSAPI_AUTH); + Common::RequestManager::getRequestManager()->registerPacketType<RequestHandlers::IdentifyRequestHandler>(Net::Packet::IDENTIFY); ConfigManager *configManager = ConfigManager::getConfigManager(); @@ -120,6 +126,15 @@ void ConnectionManager::handleConnections(std::list<Net::ServerConnection*>& con ++con; } else { + if((*con)->isIdentified()) { + for(std::map<std::string,Net::ServerConnection*>::iterator idCon = identifiedDaemonConnections.begin(); idCon != identifiedDaemonConnections.end(); ++idCon) { + if(idCon->second == *con) { + idCon->second = 0; + break; + } + } + } + Common::RequestManager::getRequestManager()->unregisterConnection(*con); delete *con; connections.erase(con++); @@ -153,5 +168,36 @@ Net::Connection* ConnectionManager::getDaemonConnection(const std::string &name) return daemon->second; } +void ConnectionManager::identifyDaemonConnection(Net::Connection *connection, const std::string &name) { + // TODO Error handling + + if(connection->isIdentified()) { + std::cerr << "Already identified." << std::endl; + return; + } + + std::list<Net::ServerConnection*>::iterator con = std::find(daemonConnections.begin(), daemonConnections.end(), connection); + if(con == daemonConnections.end()) { + std::cerr << "Connection not found." << std::endl; + return; + } + + std::map<std::string,Net::ServerConnection*>::iterator idCon = identifiedDaemonConnections.find(name); + if(idCon == identifiedDaemonConnections.end()) { + std::cerr << "Name not found." << std::endl; + return; + } + + if(idCon->second) { + idCon->second->disconnect(); + std::cerr << "Disconnecting old connection" << std::endl; + } + + idCon->second = *con; + connection->setIdentified(); + + std::cerr << "Identified as '" << name << "'." << std::endl; +} + } } diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index 4ca3d59..6e098b6 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -70,12 +70,12 @@ class ConnectionManager { return connectionManager.get(); } - virtual ~ConnectionManager(); - - void init() { + static void init() { connectionManager = std::auto_ptr<ConnectionManager>(new ConnectionManager()); } + virtual ~ConnectionManager(); + bool wait(int timeout) { return (poll(pollfds.data(), pollfds.size(), timeout) > 0); } @@ -83,6 +83,7 @@ class ConnectionManager { void run(); Net::Connection* getDaemonConnection(const std::string &name) const; + void identifyDaemonConnection(Net::Connection *connection, const std::string &name); }; } diff --git a/src/Common/RequestHandlers/IdentifyRequestHandler.cpp b/src/Core/RequestHandlers/IdentifyRequestHandler.cpp index d17c3b2..9a80959 100644 --- a/src/Common/RequestHandlers/IdentifyRequestHandler.cpp +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.cpp @@ -18,10 +18,11 @@ */ #include "IdentifyRequestHandler.h" +#include "../ConnectionManager.h" #include <Net/Connection.h> namespace Mad { -namespace Common { +namespace Core { namespace RequestHandlers { bool IdentifyRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { @@ -33,7 +34,7 @@ bool IdentifyRequestHandler::handlePacket(Net::Connection *connection, const Net // TODO Require authentication - connection->setName(std::string((const char*)packet.getData(), packet.getLength())); + ConnectionManager::getConnectionManager()->identifyDaemonConnection(connection, std::string((const char*)packet.getData(), packet.getLength())); if(!connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId()))) return false; diff --git a/src/Common/RequestHandlers/IdentifyRequestHandler.h b/src/Core/RequestHandlers/IdentifyRequestHandler.h index 6bd4b31..df03434 100644 --- a/src/Common/RequestHandlers/IdentifyRequestHandler.h +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.h @@ -20,13 +20,13 @@ #ifndef MAD_COMMON_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ #define MAD_COMMON_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ -#include "../RequestHandler.h" +#include <Common/RequestHandler.h> namespace Mad { -namespace Common { +namespace Core { namespace RequestHandlers { -class IdentifyRequestHandler : public RequestHandler { +class IdentifyRequestHandler : public Common::RequestHandler { public: IdentifyRequestHandler() {} diff --git a/src/Core/RequestHandlers/Makefile.am b/src/Core/RequestHandlers/Makefile.am index 3be5350..c13d0d7 100644 --- a/src/Core/RequestHandlers/Makefile.am +++ b/src/Core/RequestHandlers/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = CoreStatusRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp +librequesthandlers_la_SOURCES = CoreStatusRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp IdentifyRequestHandler.cpp -noinst_HEADERS = CoreStatusRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h +noinst_HEADERS = CoreStatusRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h IdentifyRequestHandler.h diff --git a/src/Core/RequestHandlers/Makefile.in b/src/Core/RequestHandlers/Makefile.in index 6661e32..191fd28 100644 --- a/src/Core/RequestHandlers/Makefile.in +++ b/src/Core/RequestHandlers/Makefile.in @@ -46,7 +46,8 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) librequesthandlers_la_LIBADD = am_librequesthandlers_la_OBJECTS = CoreStatusRequestHandler.lo \ - DaemonStatusRequestHandler.lo GSSAPIAuthRequestHandler.lo + DaemonStatusRequestHandler.lo GSSAPIAuthRequestHandler.lo \ + IdentifyRequestHandler.lo librequesthandlers_la_OBJECTS = $(am_librequesthandlers_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -185,8 +186,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = librequesthandlers.la -librequesthandlers_la_SOURCES = CoreStatusRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp -noinst_HEADERS = CoreStatusRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h +librequesthandlers_la_SOURCES = CoreStatusRequestHandler.cpp DaemonStatusRequestHandler.cpp GSSAPIAuthRequestHandler.cpp IdentifyRequestHandler.cpp +noinst_HEADERS = CoreStatusRequestHandler.h DaemonStatusRequestHandler.h GSSAPIAuthRequestHandler.h IdentifyRequestHandler.h all: all-am .SUFFIXES: @@ -241,6 +242,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoreStatusRequestHandler.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DaemonStatusRequestHandler.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GSSAPIAuthRequestHandler.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IdentifyRequestHandler.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp index d818f7e..695cba5 100644 --- a/src/Net/ClientConnection.cpp +++ b/src/Net/ClientConnection.cpp @@ -56,13 +56,18 @@ void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Con daemon = daemon0; if(isConnected()) - disconnect(); + return; + // TODO Error sock = socket(PF_INET, SOCK_STREAM, 0); if(sock < 0) throw ConnectionException("socket()", std::strerror(errno)); + if(peer) + delete peer; peer = new IPAddress(address); + authenticated = false; + identified = false; if(::connect(sock, peer->getSockAddr(), peer->getSockAddrLength()) < 0) { close(sock); diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 22f4570..8eee0cc 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -38,6 +38,9 @@ Connection::~Connection() { } gnutls_certificate_free_credentials(x509_cred); + + if(peer) + delete peer; } void Connection::handshake() { @@ -276,13 +279,6 @@ void Connection::doDisconnect() { gnutls_deinit(session); - if(peer) - delete peer; - peer = 0; - - authenticated = false; - name.clear(); - state = DISCONNECTED; } diff --git a/src/Net/Connection.h b/src/Net/Connection.h index 953ccc8..77b8cc4 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -56,9 +56,6 @@ class Connection { sigc::signal<void,Connection*,const Packet&> signal; - bool authenticated; - std::string name; - void doHandshake(); void packetHeaderReceiveHandler(const void *data, unsigned long length); @@ -99,6 +96,7 @@ class Connection { gnutls_certificate_credentials_t x509_cred; IPAddress *peer; + bool authenticated, identified; void handshake(); @@ -110,7 +108,7 @@ class Connection { bool enterReceiveLoop(); public: - Connection() : state(DISCONNECTED), authenticated(false), peer(0) { + Connection() : state(DISCONNECTED), peer(0), authenticated(false), identified(false) { transR.length = transR.transmitted = 0; transR.data = 0; @@ -131,6 +129,9 @@ class Connection { bool isAuthenticated() const {return authenticated;} void setAuthenticated() {authenticated = true;} + bool isIdentified() const {return identified;} + void setIdentified() {identified = true;} + const gnutls_datum_t* getCertificate() { return gnutls_certificate_get_ours(session); } @@ -155,9 +156,6 @@ class Connection { sigc::signal<void,Connection*,const Packet&> signalReceive() const {return signal;} - std::string getName() const {return name;} - void setName(const std::string& name0) {name = name0;} - static void init() { gnutls_global_init(); } diff --git a/src/mad-core.cpp b/src/mad-core.cpp index 4bfbcfc..ddc1414 100644 --- a/src/mad-core.cpp +++ b/src/mad-core.cpp @@ -30,6 +30,7 @@ int main() { sigprocmask(SIG_BLOCK, &signals, 0); Mad::Core::ConfigManager::useConfigManager(); + Mad::Core::ConnectionManager::init(); while(true) { if(Mad::Core::ConnectionManager::getConnectionManager()->wait(10000)) |