From fbe26b0e48e6f3714900833174fcf42196e86fc8 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 15 Sep 2008 02:19:06 +0200 Subject: Identifikationsinformationen im ConnectionManager speichern --- .../RequestHandlers/IdentifyRequestHandler.cpp | 48 --------------------- .../RequestHandlers/IdentifyRequestHandler.h | 40 ------------------ src/Common/RequestHandlers/Makefile.am | 4 +- src/Common/RequestHandlers/Makefile.in | 8 ++-- src/Common/RequestManager.cpp | 2 - src/Core/ConnectionManager.cpp | 46 ++++++++++++++++++++ src/Core/ConnectionManager.h | 7 ++-- .../RequestHandlers/IdentifyRequestHandler.cpp | 49 ++++++++++++++++++++++ src/Core/RequestHandlers/IdentifyRequestHandler.h | 40 ++++++++++++++++++ src/Core/RequestHandlers/Makefile.am | 4 +- src/Core/RequestHandlers/Makefile.in | 8 ++-- src/Net/ClientConnection.cpp | 7 +++- src/Net/Connection.cpp | 10 ++--- src/Net/Connection.h | 12 +++--- src/mad-core.cpp | 1 + 15 files changed, 166 insertions(+), 120 deletions(-) delete mode 100644 src/Common/RequestHandlers/IdentifyRequestHandler.cpp delete mode 100644 src/Common/RequestHandlers/IdentifyRequestHandler.h create mode 100644 src/Core/RequestHandlers/IdentifyRequestHandler.cpp create mode 100644 src/Core/RequestHandlers/IdentifyRequestHandler.h diff --git a/src/Common/RequestHandlers/IdentifyRequestHandler.cpp b/src/Common/RequestHandlers/IdentifyRequestHandler.cpp deleted file mode 100644 index d17c3b2..0000000 --- a/src/Common/RequestHandlers/IdentifyRequestHandler.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * IdentifyRequestHandler.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "IdentifyRequestHandler.h" -#include - -namespace Mad { -namespace Common { -namespace RequestHandlers { - -bool IdentifyRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(isFinished()) - return false; - - if(packet.getType() != Net::Packet::IDENTIFY) - return false; // TODO Logging - - // TODO Require authentication - - connection->setName(std::string((const char*)packet.getData(), packet.getLength())); - - if(!connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId()))) - return false; - - setFinished(); - - return true; -} - -} -} -} diff --git a/src/Common/RequestHandlers/IdentifyRequestHandler.h b/src/Common/RequestHandlers/IdentifyRequestHandler.h deleted file mode 100644 index 6bd4b31..0000000 --- a/src/Common/RequestHandlers/IdentifyRequestHandler.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * IdentifyRequestHandler.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_COMMON_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ -#define MAD_COMMON_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ - -#include "../RequestHandler.h" - -namespace Mad { -namespace Common { -namespace RequestHandlers { - -class IdentifyRequestHandler : public RequestHandler { - public: - IdentifyRequestHandler() {} - - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); -}; - -} -} -} - -#endif /* MAD_COMMON_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ */ 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 @@ -140,7 +139,6 @@ void RequestManager::unregisterPacketType(Net::Packet::Type type) { RequestManager::RequestManager(bool core) : requestId(core ? -2 : -1) { registerPacketType(Net::Packet::DISCONNECT); - registerPacketType(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 #include #include + #include +#include + +#include namespace Mad { namespace Core { @@ -63,6 +68,7 @@ ConnectionManager::ConnectionManager() { Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::CORE_STATUS); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::DAEMON_STATUS); Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::GSSAPI_AUTH); + Common::RequestManager::getRequestManager()->registerPacketType(Net::Packet::IDENTIFY); ConfigManager *configManager = ConfigManager::getConfigManager(); @@ -120,6 +126,15 @@ void ConnectionManager::handleConnections(std::list& con ++con; } else { + if((*con)->isIdentified()) { + for(std::map::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::iterator con = std::find(daemonConnections.begin(), daemonConnections.end(), connection); + if(con == daemonConnections.end()) { + std::cerr << "Connection not found." << std::endl; + return; + } + + std::map::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(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/Core/RequestHandlers/IdentifyRequestHandler.cpp b/src/Core/RequestHandlers/IdentifyRequestHandler.cpp new file mode 100644 index 0000000..9a80959 --- /dev/null +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.cpp @@ -0,0 +1,49 @@ +/* + * IdentifyRequestHandler.cpp + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "IdentifyRequestHandler.h" +#include "../ConnectionManager.h" +#include + +namespace Mad { +namespace Core { +namespace RequestHandlers { + +bool IdentifyRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(isFinished()) + return false; + + if(packet.getType() != Net::Packet::IDENTIFY) + return false; // TODO Logging + + // TODO Require authentication + + ConnectionManager::getConnectionManager()->identifyDaemonConnection(connection, std::string((const char*)packet.getData(), packet.getLength())); + + if(!connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId()))) + return false; + + setFinished(); + + return true; +} + +} +} +} diff --git a/src/Core/RequestHandlers/IdentifyRequestHandler.h b/src/Core/RequestHandlers/IdentifyRequestHandler.h new file mode 100644 index 0000000..df03434 --- /dev/null +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.h @@ -0,0 +1,40 @@ +/* + * IdentifyRequestHandler.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_COMMON_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ +#define MAD_COMMON_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ + +#include + +namespace Mad { +namespace Core { +namespace RequestHandlers { + +class IdentifyRequestHandler : public Common::RequestHandler { + public: + IdentifyRequestHandler() {} + + virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); +}; + +} +} +} + +#endif /* MAD_COMMON_REQUESTHANDLERS_IDENTIFYREQUESTHANDLER_H_ */ 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 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 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)) -- cgit v1.2.3