From cc35771abf4fc7e0dd472bd818ff2b4962c7e204 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 16 Sep 2008 01:20:43 +0200 Subject: Bessere Fehlerbehandlung --- src/Client/Requests/CoreStatusRequest.cpp | 9 +-- src/Client/Requests/CoreStatusRequest.h | 2 +- src/Client/Requests/DaemonListRequest.cpp | 9 +-- src/Client/Requests/DaemonListRequest.h | 2 +- src/Client/Requests/DaemonStatusRequest.cpp | 9 +-- src/Client/Requests/DaemonStatusRequest.h | 2 +- src/Common/Exception.h | 50 ++++++++++++++++ src/Common/Makefile.am | 2 +- src/Common/Makefile.in | 2 +- src/Common/RequestHandler.h | 2 +- .../RequestHandlers/DisconnectRequestHandler.cpp | 18 +++--- .../RequestHandlers/DisconnectRequestHandler.h | 2 +- .../RequestHandlers/StatusRequestHandler.cpp | 21 ++++--- src/Common/RequestHandlers/StatusRequestHandler.h | 2 +- src/Common/RequestManager.cpp | 3 + src/Common/Requests/DisconnectRequest.cpp | 15 ++--- src/Common/Requests/DisconnectRequest.h | 2 +- src/Common/Requests/GSSAPIAuthRequest.cpp | 22 +++---- src/Common/Requests/GSSAPIAuthRequest.h | 2 +- src/Common/Requests/IdentifyRequest.cpp | 5 +- src/Common/Requests/IdentifyRequest.h | 2 +- src/Core/ConnectionManager.cpp | 26 ++++----- src/Core/ConnectionManager.h | 5 +- .../RequestHandlers/DaemonListRequestHandler.cpp | 19 +++--- .../RequestHandlers/DaemonListRequestHandler.h | 2 +- .../RequestHandlers/DaemonStatusRequestHandler.cpp | 29 ++++++---- .../RequestHandlers/DaemonStatusRequestHandler.h | 2 +- .../RequestHandlers/GSSAPIAuthRequestHandler.cpp | 28 +++++---- .../RequestHandlers/GSSAPIAuthRequestHandler.h | 2 +- .../RequestHandlers/IdentifyRequestHandler.cpp | 29 +++++++--- src/Core/RequestHandlers/IdentifyRequestHandler.h | 2 +- src/Core/Requests/DaemonStatusRequest.cpp | 9 +-- src/Core/Requests/DaemonStatusRequest.h | 2 +- src/Net/Packets/ErrorPacket.cpp | 48 ++++++++++++++++ src/Net/Packets/ErrorPacket.h | 67 ++++++++++++++++++++++ src/Net/Packets/Makefile.am | 4 +- src/Net/Packets/Makefile.in | 8 ++- 37 files changed, 340 insertions(+), 125 deletions(-) create mode 100644 src/Common/Exception.h create mode 100644 src/Net/Packets/ErrorPacket.cpp create mode 100644 src/Net/Packets/ErrorPacket.h diff --git a/src/Client/Requests/CoreStatusRequest.cpp b/src/Client/Requests/CoreStatusRequest.cpp index 1c477b8..93552f6 100644 --- a/src/Client/Requests/CoreStatusRequest.cpp +++ b/src/Client/Requests/CoreStatusRequest.cpp @@ -44,14 +44,15 @@ bool CoreStatusRequest::sendRequest(Net::Connection *connection, uint16_t reques return true; } -bool CoreStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) - return false; // TODO Logging +void CoreStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::OK) { + signalFinished().emit(); + return; // TODO Logging + } finished(Net::Packets::HostStatusPacket(packet)); signalFinished().emit(); - return true; } } diff --git a/src/Client/Requests/CoreStatusRequest.h b/src/Client/Requests/CoreStatusRequest.h index 55d6a22..ff9bdc1 100644 --- a/src/Client/Requests/CoreStatusRequest.h +++ b/src/Client/Requests/CoreStatusRequest.h @@ -45,7 +45,7 @@ class CoreStatusRequest : public Common::Request { static bool send(Net::Connection *connection, const sigc::slot &callback); virtual bool sendRequest(Net::Connection *connection, uint16_t requestId); - virtual bool handlePacket(Net::Connection*, const Net::Packet &packet); + virtual void handlePacket(Net::Connection*, const Net::Packet &packet); }; } diff --git a/src/Client/Requests/DaemonListRequest.cpp b/src/Client/Requests/DaemonListRequest.cpp index 97ee335..cc6d353 100644 --- a/src/Client/Requests/DaemonListRequest.cpp +++ b/src/Client/Requests/DaemonListRequest.cpp @@ -44,14 +44,15 @@ bool DaemonListRequest::sendRequest(Net::Connection *connection, uint16_t reques return true; } -bool DaemonListRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) - return false; // TODO Logging +void DaemonListRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::OK) { + signalFinished().emit(); + return; // TODO Logging + } finished(Net::Packets::NameListPacket(packet)); signalFinished().emit(); - return true; } } diff --git a/src/Client/Requests/DaemonListRequest.h b/src/Client/Requests/DaemonListRequest.h index 1a820d7..9a11d19 100644 --- a/src/Client/Requests/DaemonListRequest.h +++ b/src/Client/Requests/DaemonListRequest.h @@ -45,7 +45,7 @@ class DaemonListRequest : public Common::Request { static bool send(Net::Connection *connection, const sigc::slot &callback); virtual bool sendRequest(Net::Connection *connection, uint16_t requestId); - virtual bool handlePacket(Net::Connection*, const Net::Packet &packet); + virtual void handlePacket(Net::Connection*, const Net::Packet &packet); }; } diff --git a/src/Client/Requests/DaemonStatusRequest.cpp b/src/Client/Requests/DaemonStatusRequest.cpp index 8308e96..7a7b47e 100644 --- a/src/Client/Requests/DaemonStatusRequest.cpp +++ b/src/Client/Requests/DaemonStatusRequest.cpp @@ -44,14 +44,15 @@ bool DaemonStatusRequest::sendRequest(Net::Connection *connection, uint16_t requ return true; } -bool DaemonStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) - return false; // TODO Logging +void DaemonStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::OK) { + signalFinished().emit(); + return; // TODO Logging + } finished(Net::Packets::HostStatusPacket(packet)); signalFinished().emit(); - return true; } } diff --git a/src/Client/Requests/DaemonStatusRequest.h b/src/Client/Requests/DaemonStatusRequest.h index 0aff41b..34d3a90 100644 --- a/src/Client/Requests/DaemonStatusRequest.h +++ b/src/Client/Requests/DaemonStatusRequest.h @@ -48,7 +48,7 @@ class DaemonStatusRequest : public Common::Request { static bool send(Net::Connection *connection, const sigc::slot &callback, const std::string &daemon0); virtual bool sendRequest(Net::Connection *connection, uint16_t requestId); - virtual bool handlePacket(Net::Connection*, const Net::Packet &packet); + virtual void handlePacket(Net::Connection*, const Net::Packet &packet); }; } diff --git a/src/Common/Exception.h b/src/Common/Exception.h new file mode 100644 index 0000000..6b4cf9f --- /dev/null +++ b/src/Common/Exception.h @@ -0,0 +1,50 @@ +/* + * Exception.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_EXCEPTION_H_ +#define MAD_COMMON_EXCEPTION_H_ + +namespace Mad { +namespace Common { + +class Exception { + public: + enum ErrorCode { + SUCCESS = 0x0000, UNEXPECTED_PACKET = 0x0001, INVALID_ACTION = 0x0002, + ALREADY_IDENTIFIED = 0x0010, UNKNOWN_DAEMON = 0x0011, DAEMON_INACTIVE = 0x0012 + }; + + private: + ErrorCode errorCode; + unsigned long subCode; + unsigned long subSubCode; + + public: + Exception(ErrorCode errorCode0, unsigned long subCode0 = 0, unsigned long subSubCode0 = 0) : errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} + virtual ~Exception() {} + + ErrorCode getErrorCode() const {return errorCode;} + unsigned long getSubCode() const {return subCode;} + unsigned long getSubSubCode() const {return subSubCode;} +}; + +} +} + +#endif /* MAD_COMMON_EXCEPTION_H_ */ diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index 8ea7c14..f422b46 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -4,4 +4,4 @@ noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = ConfigManager.cpp RequestManager.cpp SystemBackend.cpp Util.cpp libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = ConfigManager.h Request.h RequestHandler.h RequestManager.h SystemBackend.h Util.h +noinst_HEADERS = ConfigManager.h Exception.h Request.h RequestHandler.h RequestManager.h SystemBackend.h Util.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 66992ea..9361d39 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -199,7 +199,7 @@ SUBDIRS = Backends Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la libcommon_la_SOURCES = ConfigManager.cpp RequestManager.cpp SystemBackend.cpp Util.cpp libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = ConfigManager.h Request.h RequestHandler.h RequestManager.h SystemBackend.h Util.h +noinst_HEADERS = ConfigManager.h Exception.h Request.h RequestHandler.h RequestManager.h SystemBackend.h Util.h all: all-recursive .SUFFIXES: diff --git a/src/Common/RequestHandler.h b/src/Common/RequestHandler.h index 305836c..2bc2361 100644 --- a/src/Common/RequestHandler.h +++ b/src/Common/RequestHandler.h @@ -45,7 +45,7 @@ class RequestHandler { sigc::signal signalFinished() {return finished;} - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet) = 0; + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet) = 0; }; } diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp index aa98c04..010f1e7 100644 --- a/src/Common/RequestHandlers/DisconnectRequestHandler.cpp +++ b/src/Common/RequestHandlers/DisconnectRequestHandler.cpp @@ -19,23 +19,27 @@ #include "DisconnectRequestHandler.h" #include +#include + +#include namespace Mad { namespace Common { namespace RequestHandlers { -bool DisconnectRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::DISCONNECT) - return false; // TODO Logging +void DisconnectRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::DISCONNECT) { + std::cerr << "Received an unexpected packet." << std::endl; + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); - if(!connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId()))) - return false; + signalFinished().emit(); + return; + } + connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId())); connection->disconnect(); signalFinished().emit(); - - return true; } } diff --git a/src/Common/RequestHandlers/DisconnectRequestHandler.h b/src/Common/RequestHandlers/DisconnectRequestHandler.h index 784700d..b4da634 100644 --- a/src/Common/RequestHandlers/DisconnectRequestHandler.h +++ b/src/Common/RequestHandlers/DisconnectRequestHandler.h @@ -30,7 +30,7 @@ class DisconnectRequestHandler : public RequestHandler { public: DisconnectRequestHandler() {} - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); }; } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 0a437fa..bcff6b6 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -20,28 +20,33 @@ #include "StatusRequestHandler.h" #include "../SystemBackend.h" #include +#include #include +#include + namespace Mad { namespace Common { namespace RequestHandlers { -bool StatusRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::STATUS) - return false; // TODO Logging +void StatusRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::STATUS) { + std::cerr << "Received an unexpected packet." << std::endl; + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + signalFinished().emit(); + return; + } // TODO Require authentication SystemBackend::UptimeInfo uptimeInfo = SystemBackend::getBackend()->getUptimeInfo(); SystemBackend::MemoryInfo memInfo = SystemBackend::getBackend()->getMemoryInfo(); - if(!connection->send(Net::Packets::HostStatusPacket(Net::Packet::OK, packet.getRequestId(), uptimeInfo.uptime, uptimeInfo.idleTime, - memInfo.totalMem, memInfo.freeMem, memInfo.totalSwap, memInfo.freeSwap))) - return false; + connection->send(Net::Packets::HostStatusPacket(Net::Packet::OK, packet.getRequestId(), uptimeInfo.uptime, uptimeInfo.idleTime, + memInfo.totalMem, memInfo.freeMem, memInfo.totalSwap, memInfo.freeSwap)); signalFinished().emit(); - - return true; } } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.h b/src/Common/RequestHandlers/StatusRequestHandler.h index db2176a..f98a9a4 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.h +++ b/src/Common/RequestHandlers/StatusRequestHandler.h @@ -30,7 +30,7 @@ class StatusRequestHandler : public RequestHandler { public: StatusRequestHandler() {} - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); }; } diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index e2864b1..5d3fdcd 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -21,6 +21,8 @@ #include "Request.h" #include "RequestHandlers/DisconnectRequestHandler.h" +#include + #include #include #include @@ -90,6 +92,7 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack std::cerr << "Received an unexpected packet." << std::endl; + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Exception(Exception::UNEXPECTED_PACKET))); } bool RequestManager::sendRequest(Net::Connection *connection, Request *request) { diff --git a/src/Common/Requests/DisconnectRequest.cpp b/src/Common/Requests/DisconnectRequest.cpp index 090b840..81208f2 100644 --- a/src/Common/Requests/DisconnectRequest.cpp +++ b/src/Common/Requests/DisconnectRequest.cpp @@ -43,16 +43,17 @@ bool DisconnectRequest::sendRequest(Net::Connection *connection, uint16_t reques return true; } -bool DisconnectRequest::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) - return false; // TODO Logging +void DisconnectRequest::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::OK) { + signalFinished().emit(); + return; // TODO Logging + } - connection->disconnect(); + connection->disconnect(); - finished(); + finished(); - signalFinished().emit(); - return true; + signalFinished().emit(); } } diff --git a/src/Common/Requests/DisconnectRequest.h b/src/Common/Requests/DisconnectRequest.h index 6c87f85..dcc2b9c 100644 --- a/src/Common/Requests/DisconnectRequest.h +++ b/src/Common/Requests/DisconnectRequest.h @@ -38,7 +38,7 @@ class DisconnectRequest : public Request { static bool send(Net::Connection *connection, const sigc::slot &callback); virtual bool sendRequest(Net::Connection *connection, uint16_t requestId); - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); }; } diff --git a/src/Common/Requests/GSSAPIAuthRequest.cpp b/src/Common/Requests/GSSAPIAuthRequest.cpp index a4a1b17..91488d0 100644 --- a/src/Common/Requests/GSSAPIAuthRequest.cpp +++ b/src/Common/Requests/GSSAPIAuthRequest.cpp @@ -86,13 +86,17 @@ bool GSSAPIAuthRequest::sendRequest(Net::Connection *connection, uint16_t reques return true; } -bool GSSAPIAuthRequest::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::GSSAPI_AUTH) - return false; // TODO Logging +void GSSAPIAuthRequest::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::GSSAPI_AUTH) { + signalFinished().emit(); + return; // TODO Logging + } OM_uint32 majStat, minStat; gss_buffer_desc recvBuffer, sendBuffer; + // Needs error handling! + if(gssContinue) { recvBuffer.length = packet.getLength(); recvBuffer.value = std::malloc(recvBuffer.length); @@ -109,12 +113,12 @@ bool GSSAPIAuthRequest::handlePacket(Net::Connection *connection, const Net::Pac } else if(majStat != GSS_S_CONTINUE_NEEDED) { gss_release_buffer(&minStat, &sendBuffer); - return false; + return; } if(!connection->send(Net::Packet(Net::Packet::GSSAPI_AUTH, packet.getRequestId(), sendBuffer.value, sendBuffer.length))) { gss_release_buffer(&minStat, &sendBuffer); - return false; + return; } gss_release_buffer(&minStat, &sendBuffer); @@ -134,7 +138,7 @@ bool GSSAPIAuthRequest::handlePacket(Net::Connection *connection, const Net::Pac std::free(recvBuffer.value); if(majStat != GSS_S_COMPLETE) - return false; + return; connection->setAuthenticated(); std::cout << "Authentication complete." << std::endl; @@ -143,20 +147,18 @@ bool GSSAPIAuthRequest::handlePacket(Net::Connection *connection, const Net::Pac if(majStat != GSS_S_COMPLETE) { gss_release_buffer(&minStat, &sendBuffer); - return false; + return; } if(!connection->send(Net::Packet(Net::Packet::GSSAPI_AUTH, packet.getRequestId(), sendBuffer.value, sendBuffer.length))) { gss_release_buffer(&minStat, &sendBuffer); - return false; + return; } gss_release_buffer(&minStat, &sendBuffer); signalFinished().emit(); } - - return true; } } diff --git a/src/Common/Requests/GSSAPIAuthRequest.h b/src/Common/Requests/GSSAPIAuthRequest.h index e9a200e..336e4f0 100644 --- a/src/Common/Requests/GSSAPIAuthRequest.h +++ b/src/Common/Requests/GSSAPIAuthRequest.h @@ -49,7 +49,7 @@ class GSSAPIAuthRequest : public Request { static bool send(Net::Connection *connection, const std::string &serviceName0); virtual bool sendRequest(Net::Connection *connection, uint16_t requestId); - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); }; } diff --git a/src/Common/Requests/IdentifyRequest.cpp b/src/Common/Requests/IdentifyRequest.cpp index 9214baf..23249cd 100644 --- a/src/Common/Requests/IdentifyRequest.cpp +++ b/src/Common/Requests/IdentifyRequest.cpp @@ -42,12 +42,11 @@ bool IdentifyRequest::sendRequest(Net::Connection *connection, uint16_t requestI return true; } -bool IdentifyRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { +void IdentifyRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { if(packet.getType() != Net::Packet::OK) - return false; // TODO Logging + return; // TODO Logging signalFinished().emit(); - return true; } } diff --git a/src/Common/Requests/IdentifyRequest.h b/src/Common/Requests/IdentifyRequest.h index 0b783e4..5a02949 100644 --- a/src/Common/Requests/IdentifyRequest.h +++ b/src/Common/Requests/IdentifyRequest.h @@ -37,7 +37,7 @@ class IdentifyRequest : public Request { static bool send(Net::Connection *connection, const std::string &hostname0); virtual bool sendRequest(Net::Connection *connection, uint16_t requestId); - virtual bool handlePacket(Net::Connection*, const Net::Packet &packet); + virtual void handlePacket(Net::Connection*, const Net::Packet &packet); }; } diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 60f3ac4..2926fbf 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -162,32 +162,30 @@ void ConnectionManager::run() { refreshPollfds(); } -Net::Connection* ConnectionManager::getDaemonConnection(const std::string &name) const { +Net::Connection* ConnectionManager::getDaemonConnection(const std::string &name) const throw (Common::Exception&) { std::map::const_iterator daemon = identifiedDaemonConnections.find(name); if(daemon == identifiedDaemonConnections.end()) - return 0; + throw Common::Exception(Common::Exception::UNKNOWN_DAEMON); + + if(!daemon->second) + throw Common::Exception(Common::Exception::DAEMON_INACTIVE); return daemon->second; } -void ConnectionManager::identifyDaemonConnection(Net::Connection *connection, const std::string &name) { - // TODO Error handling +void ConnectionManager::identifyDaemonConnection(Net::Connection *connection, const std::string &name) throw (Common::Exception&) { + // TODO Logging - if(connection->isIdentified()) { - std::cerr << "Already identified." << std::endl; - return; - } + if(connection->isIdentified()) + throw Common::Exception(Common::Exception::ALREADY_IDENTIFIED); std::list::iterator con = std::find(daemonConnections.begin(), daemonConnections.end(), connection); - if(con == daemonConnections.end()) { - std::cerr << "Connection not found." << std::endl; - return; - } + if(con == daemonConnections.end()) + throw Common::Exception(Common::Exception::INVALID_ACTION); std::map::iterator idCon = identifiedDaemonConnections.find(name); if(idCon == identifiedDaemonConnections.end()) { - std::cerr << "Name not found." << std::endl; - return; + throw Common::Exception(Common::Exception::UNKNOWN_DAEMON); } if(idCon->second) { diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index ba029ec..be9beb9 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -27,6 +27,7 @@ #include #include "DaemonInfo.h" +#include #include namespace Mad { @@ -82,8 +83,8 @@ class ConnectionManager { void run(); - Net::Connection* getDaemonConnection(const std::string &name) const; - void identifyDaemonConnection(Net::Connection *connection, const std::string &name); + Net::Connection* getDaemonConnection(const std::string &name) const throw (Common::Exception&); + void identifyDaemonConnection(Net::Connection *connection, const std::string &name) throw (Common::Exception&); std::map getDaemonList() const; }; diff --git a/src/Core/RequestHandlers/DaemonListRequestHandler.cpp b/src/Core/RequestHandlers/DaemonListRequestHandler.cpp index 1f137ad..2bee423 100644 --- a/src/Core/RequestHandlers/DaemonListRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonListRequestHandler.cpp @@ -20,15 +20,23 @@ #include "DaemonListRequestHandler.h" #include "../ConnectionManager.h" #include +#include #include +#include + namespace Mad { namespace Core { namespace RequestHandlers { -bool DaemonListRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::LIST_DAEMONS) - return false; // TODO Logging +void DaemonListRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::LIST_DAEMONS) { + std::cerr << "Received an unexpected packet." << std::endl; + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + signalFinished().emit(); + return; + } // TODO Require authentication @@ -38,12 +46,9 @@ bool DaemonListRequestHandler::handlePacket(Net::Connection *connection, const N for(std::map::iterator daemon = daemons.begin(); daemon != daemons.end(); ++daemon) names.push_back(daemon->first); - if(!connection->send(Net::Packets::NameListPacket(Net::Packet::OK, packet.getRequestId(), names))) - return false; + connection->send(Net::Packets::NameListPacket(Net::Packet::OK, packet.getRequestId(), names)); signalFinished().emit(); - - return true; } } diff --git a/src/Core/RequestHandlers/DaemonListRequestHandler.h b/src/Core/RequestHandlers/DaemonListRequestHandler.h index 406d212..1aabe5b 100644 --- a/src/Core/RequestHandlers/DaemonListRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonListRequestHandler.h @@ -30,7 +30,7 @@ class DaemonListRequestHandler : public Common::RequestHandler { public: DaemonListRequestHandler() {} - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); }; } diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp index 0179ecc..bb5d45f 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp @@ -20,15 +20,24 @@ #include "DaemonStatusRequestHandler.h" #include "../ConnectionManager.h" #include +#include #include +#include + + namespace Mad { namespace Core { namespace RequestHandlers { -bool DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::DAEMON_STATUS) - return false; // TODO Logging +void DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::DAEMON_STATUS) { + std::cerr << "Received an unexpected packet." << std::endl; + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + signalFinished().emit(); + return; + } // TODO Require authentication @@ -37,13 +46,13 @@ bool DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, const std::string daemonName((char*)packet.getData(), packet.getLength()); - Net::Connection *daemonCon = ConnectionManager::getConnectionManager()->getDaemonConnection(daemonName); - if(!daemonCon) - return false; - - Requests::DaemonStatusRequest::send(daemonCon, sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)); - - return true; + try { + Net::Connection *daemonCon = ConnectionManager::getConnectionManager()->getDaemonConnection(daemonName); + Requests::DaemonStatusRequest::send(daemonCon, sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)); + } + catch(Common::Exception &e) { + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), e)); + } } void DaemonStatusRequestHandler::requestFinished(const Net::Packets::HostStatusPacket &packet) { diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h index 5d755bd..916a3ec 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h @@ -47,7 +47,7 @@ class DaemonStatusRequestHandler : public Common::RequestHandler { public: DaemonStatusRequestHandler() {} - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); }; } diff --git a/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.cpp b/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.cpp index 94098b9..d65da7e 100644 --- a/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.cpp +++ b/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.cpp @@ -19,6 +19,7 @@ #include "GSSAPIAuthRequestHandler.h" #include +#include #include @@ -30,13 +31,20 @@ namespace RequestHandlers { // TODO Error handling -bool GSSAPIAuthRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::GSSAPI_AUTH) - return false; // TODO Logging +void GSSAPIAuthRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::GSSAPI_AUTH) { + std::cerr << "Received an unexpected packet." << std::endl; + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + signalFinished().emit(); + return; + } OM_uint32 majStat, minStat; gss_buffer_desc recvBuffer, sendBuffer; + // Needs error handling! + if(gssContinue) { recvBuffer.length = packet.getLength(); recvBuffer.value = std::malloc(recvBuffer.length); @@ -52,19 +60,19 @@ bool GSSAPIAuthRequestHandler::handlePacket(Net::Connection *connection, const N } else if(majStat != GSS_S_CONTINUE_NEEDED) { gss_release_buffer(&minStat, &sendBuffer); - return false; + return; } if(!connection->send(Net::Packet(Net::Packet::GSSAPI_AUTH, packet.getRequestId(), sendBuffer.value, sendBuffer.length))) { gss_release_buffer(&minStat, &sendBuffer); - return false; + return; } gss_release_buffer(&minStat, &sendBuffer); } else if(!sentSignature) { if(packet.getLength() != 0) - return false; + return; const gnutls_datum_t *cert = connection->getCertificate(); @@ -75,12 +83,12 @@ bool GSSAPIAuthRequestHandler::handlePacket(Net::Connection *connection, const N if(majStat != GSS_S_COMPLETE) { gss_release_buffer(&minStat, &sendBuffer); - return false; + return; } if(!connection->send(Net::Packet(Net::Packet::GSSAPI_AUTH, packet.getRequestId(), sendBuffer.value, sendBuffer.length))) { gss_release_buffer(&minStat, &sendBuffer); - return false; + return; } gss_release_buffer(&minStat, &sendBuffer); @@ -97,12 +105,10 @@ bool GSSAPIAuthRequestHandler::handlePacket(Net::Connection *connection, const N std::free(recvBuffer.value); if(majStat != GSS_S_COMPLETE) - return false; + return; signalFinished().emit(); } - - return true; } } diff --git a/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.h b/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.h index 57ea14f..2a7884b 100644 --- a/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.h +++ b/src/Core/RequestHandlers/GSSAPIAuthRequestHandler.h @@ -36,7 +36,7 @@ class GSSAPIAuthRequestHandler : public Common::RequestHandler { public: GSSAPIAuthRequestHandler() : gssContext(GSS_C_NO_CONTEXT), gssContinue(true), sentSignature(false) {} - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); }; } diff --git a/src/Core/RequestHandlers/IdentifyRequestHandler.cpp b/src/Core/RequestHandlers/IdentifyRequestHandler.cpp index 48fccf2..4e65aca 100644 --- a/src/Core/RequestHandlers/IdentifyRequestHandler.cpp +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.cpp @@ -19,26 +19,37 @@ #include "IdentifyRequestHandler.h" #include "../ConnectionManager.h" +#include #include +#include + +#include + namespace Mad { namespace Core { namespace RequestHandlers { -bool IdentifyRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::IDENTIFY) - return false; // TODO Logging +void IdentifyRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::IDENTIFY) { + std::cerr << "Received an unexpected packet." << std::endl; + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); - // TODO Require authentication + signalFinished().emit(); + return; + } - ConnectionManager::getConnectionManager()->identifyDaemonConnection(connection, std::string((const char*)packet.getData(), packet.getLength())); + // TODO Require authentication + try { + ConnectionManager::getConnectionManager()->identifyDaemonConnection(connection, std::string((const char*)packet.getData(), packet.getLength())); - if(!connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId()))) - return false; + connection->send(Net::Packet(Net::Packet::OK, packet.getRequestId())); + } + catch(Common::Exception &e) { + connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), e)); + } signalFinished().emit(); - - return true; } } diff --git a/src/Core/RequestHandlers/IdentifyRequestHandler.h b/src/Core/RequestHandlers/IdentifyRequestHandler.h index df03434..74ffd54 100644 --- a/src/Core/RequestHandlers/IdentifyRequestHandler.h +++ b/src/Core/RequestHandlers/IdentifyRequestHandler.h @@ -30,7 +30,7 @@ class IdentifyRequestHandler : public Common::RequestHandler { public: IdentifyRequestHandler() {} - virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); }; } diff --git a/src/Core/Requests/DaemonStatusRequest.cpp b/src/Core/Requests/DaemonStatusRequest.cpp index 118203f..86ef3c6 100644 --- a/src/Core/Requests/DaemonStatusRequest.cpp +++ b/src/Core/Requests/DaemonStatusRequest.cpp @@ -44,14 +44,15 @@ bool DaemonStatusRequest::sendRequest(Net::Connection *connection, uint16_t requ return true; } -bool DaemonStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) - return false; // TODO Logging +void DaemonStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { + if(packet.getType() != Net::Packet::OK) { + signalFinished().emit(); + return; // TODO Logging + } finished(Net::Packets::HostStatusPacket(packet)); signalFinished().emit(); - return true; } } diff --git a/src/Core/Requests/DaemonStatusRequest.h b/src/Core/Requests/DaemonStatusRequest.h index b9ec401..ce6e4f7 100644 --- a/src/Core/Requests/DaemonStatusRequest.h +++ b/src/Core/Requests/DaemonStatusRequest.h @@ -46,7 +46,7 @@ class DaemonStatusRequest : public Common::Request { static bool send(Net::Connection *connection, const sigc::slot &callback); virtual bool sendRequest(Net::Connection *connection, uint16_t requestId); - virtual bool handlePacket(Net::Connection*, const Net::Packet &packet); + virtual void handlePacket(Net::Connection*, const Net::Packet &packet); }; } diff --git a/src/Net/Packets/ErrorPacket.cpp b/src/Net/Packets/ErrorPacket.cpp new file mode 100644 index 0000000..2a72415 --- /dev/null +++ b/src/Net/Packets/ErrorPacket.cpp @@ -0,0 +1,48 @@ +/* + * ErrorPacket.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 "ErrorPacket.h" + +namespace Mad { +namespace Net { +namespace Packets { + +ErrorPacket::ErrorPacket(Type type, uint16_t requestId, const Common::Exception &exception) +: Packet(type, requestId) +{ + setLength(sizeof(ErrorData)); + errorData = (ErrorData*)&rawData->data; + + errorData->errorCode = htonl(exception.getErrorCode()); + errorData->subCode = htonl(exception.getSubCode()); + errorData->subSubCode = htonl(exception.getSubSubCode()); +} + +ErrorPacket& ErrorPacket::operator=(const Packet &p) { + Packet::operator=(p); + + setLength(sizeof(ErrorData)); + errorData = (ErrorData*)&rawData->data; + + return *this; +} + +} +} +} diff --git a/src/Net/Packets/ErrorPacket.h b/src/Net/Packets/ErrorPacket.h new file mode 100644 index 0000000..4c6a026 --- /dev/null +++ b/src/Net/Packets/ErrorPacket.h @@ -0,0 +1,67 @@ +/* + * ErrorPacket.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_NET_PACKETS_ERRORPACKET_H_ +#define MAD_NET_PACKETS_ERRORPACKET_H_ + +#include "../Packet.h" +#include + +namespace Mad { +namespace Net { +namespace Packets { + +class ErrorPacket : public Packet { + protected: + struct ErrorData { + uint32_t errorCode; + uint32_t subCode; + uint32_t subSubCode; + }; + + ErrorData *errorData; + + public: + ErrorPacket(Type type, uint16_t requestId, const Common::Exception &exception); + + ErrorPacket(const Packet &p) : Packet(p) { + setLength(sizeof(ErrorData)); + errorData = (ErrorData*)&rawData->data; + } + + ErrorPacket(const ErrorPacket &p) : Packet(p) { + errorData = (ErrorData*)&rawData->data; + } + + ErrorPacket& operator=(const Packet &p); + + ErrorPacket& operator=(const ErrorPacket &p) { + return (*this = (Packet)p); + } + + Common::Exception getException() const { + return Common::Exception((Common::Exception::ErrorCode)ntohl(errorData->errorCode), ntohl(errorData->subCode), ntohl(errorData->subSubCode)); + } +}; + +} +} +} + +#endif /* MAD_NET_PACKETS_ERRORPACKET_H_ */ diff --git a/src/Net/Packets/Makefile.am b/src/Net/Packets/Makefile.am index 9e1f589..4600c96 100644 --- a/src/Net/Packets/Makefile.am +++ b/src/Net/Packets/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = HostStatusPacket.cpp NameListPacket.cpp +libpackets_la_SOURCES = ErrorPacket.cpp HostStatusPacket.cpp NameListPacket.cpp -noinst_HEADERS = HostStatusPacket.h NameListPacket.h +noinst_HEADERS = ErrorPacket.cpp HostStatusPacket.h NameListPacket.h diff --git a/src/Net/Packets/Makefile.in b/src/Net/Packets/Makefile.in index 6a7e39f..abd8137 100644 --- a/src/Net/Packets/Makefile.in +++ b/src/Net/Packets/Makefile.in @@ -45,7 +45,8 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libpackets_la_LIBADD = -am_libpackets_la_OBJECTS = HostStatusPacket.lo NameListPacket.lo +am_libpackets_la_OBJECTS = ErrorPacket.lo HostStatusPacket.lo \ + NameListPacket.lo libpackets_la_OBJECTS = $(am_libpackets_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -184,8 +185,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = HostStatusPacket.cpp NameListPacket.cpp -noinst_HEADERS = HostStatusPacket.h NameListPacket.h +libpackets_la_SOURCES = ErrorPacket.cpp HostStatusPacket.cpp NameListPacket.cpp +noinst_HEADERS = ErrorPacket.cpp HostStatusPacket.h NameListPacket.h all: all-am .SUFFIXES: @@ -237,6 +238,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ErrorPacket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HostStatusPacket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/NameListPacket.Plo@am__quote@ -- cgit v1.2.3