diff options
Diffstat (limited to 'Limber')
-rw-r--r-- | Limber/SConscript | 46 | ||||
-rw-r--r-- | Limber/Server/ServerFromClientSession.cpp | 184 | ||||
-rw-r--r-- | Limber/Server/ServerFromClientSession.h | 96 | ||||
-rw-r--r-- | Limber/Server/ServerSession.cpp | 4 | ||||
-rw-r--r-- | Limber/Server/ServerSession.h | 18 | ||||
-rw-r--r-- | Limber/Server/ServerStanzaRouter.cpp | 90 | ||||
-rw-r--r-- | Limber/Server/ServerStanzaRouter.h | 26 | ||||
-rw-r--r-- | Limber/Server/SimpleUserRegistry.cpp | 10 | ||||
-rw-r--r-- | Limber/Server/SimpleUserRegistry.h | 25 | ||||
-rw-r--r-- | Limber/Server/UnitTest/ServerStanzaRouterTest.cpp | 271 | ||||
-rw-r--r-- | Limber/Server/UserRegistry.cpp | 4 | ||||
-rw-r--r-- | Limber/Server/UserRegistry.h | 15 | ||||
-rw-r--r-- | Limber/main.cpp | 170 |
13 files changed, 484 insertions, 475 deletions
diff --git a/Limber/SConscript b/Limber/SConscript index 13bc0df..7aca509 100644 --- a/Limber/SConscript +++ b/Limber/SConscript @@ -1,30 +1,30 @@ Import("env") if env["SCONS_STAGE"] == "flags" : - env["LIMBER_FLAGS"] = { - "LIBPATH": [Dir(".")], - "LIBS": ["Limber"], - } + env["LIMBER_FLAGS"] = { + "LIBPATH": [Dir(".")], + "LIBS": ["Limber"], + } elif env["SCONS_STAGE"] == "build" : - libenv = env.Clone() - libenv.UseFlags(env["SWIFTEN_FLAGS"]) - libenv.UseFlags(env["SWIFTEN_DEP_FLAGS"]) - libenv.StaticLibrary("Limber", [ - "Server/ServerFromClientSession.cpp", - "Server/ServerSession.cpp", - "Server/ServerStanzaRouter.cpp", - "Server/SimpleUserRegistry.cpp", - "Server/UserRegistry.cpp", - ]) + libenv = env.Clone() + libenv.UseFlags(env["SWIFTEN_FLAGS"]) + libenv.UseFlags(env["SWIFTEN_DEP_FLAGS"]) + libenv.StaticLibrary("Limber", [ + "Server/ServerFromClientSession.cpp", + "Server/ServerSession.cpp", + "Server/ServerStanzaRouter.cpp", + "Server/SimpleUserRegistry.cpp", + "Server/UserRegistry.cpp", + ]) - myenv = env.Clone() - myenv.BuildVersion("BuildVersion.h", project = "limber") - myenv.UseFlags(env["LIMBER_FLAGS"]) - myenv.UseFlags(env["SWIFTEN_FLAGS"]) - myenv.UseFlags(env["SWIFTEN_DEP_FLAGS"]) - myenv.Program("limber", ["main.cpp"]) + myenv = env.Clone() + myenv.BuildVersion("BuildVersion.h", project = "limber") + myenv.UseFlags(env["LIMBER_FLAGS"]) + myenv.UseFlags(env["SWIFTEN_FLAGS"]) + myenv.UseFlags(env["SWIFTEN_DEP_FLAGS"]) + myenv.Program("limber", ["main.cpp"]) - env.Append(UNITTEST_SOURCES = [ - File("Server/UnitTest/ServerStanzaRouterTest.cpp"), - ]) + env.Append(UNITTEST_SOURCES = [ + File("Server/UnitTest/ServerStanzaRouterTest.cpp"), + ]) diff --git a/Limber/Server/ServerFromClientSession.cpp b/Limber/Server/ServerFromClientSession.cpp index 052ea9b..f2a3659 100644 --- a/Limber/Server/ServerFromClientSession.cpp +++ b/Limber/Server/ServerFromClientSession.cpp @@ -1,119 +1,121 @@ /* - * Copyright (c) 2010-2014 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include "Limber/Server/ServerFromClientSession.h" +#include <Limber/Server/ServerFromClientSession.h> + +#include <memory> -#include <boost/smart_ptr/make_shared.hpp> #include <boost/bind.hpp> -#include "Swiften/Elements/ProtocolHeader.h" -#include "Limber/Server/UserRegistry.h" -#include "Swiften/Network/Connection.h" -#include "Swiften/StreamStack/XMPPLayer.h" -#include "Swiften/Elements/StreamFeatures.h" -#include "Swiften/Elements/ResourceBind.h" -#include "Swiften/Elements/StartSession.h" -#include "Swiften/Elements/IQ.h" -#include "Swiften/Elements/AuthSuccess.h" -#include "Swiften/Elements/AuthFailure.h" -#include "Swiften/Elements/AuthRequest.h" -#include "Swiften/SASL/PLAINMessage.h" +#include <Swiften/Elements/AuthFailure.h> +#include <Swiften/Elements/AuthRequest.h> +#include <Swiften/Elements/AuthSuccess.h> +#include <Swiften/Elements/IQ.h> +#include <Swiften/Elements/ProtocolHeader.h> +#include <Swiften/Elements/ResourceBind.h> +#include <Swiften/Elements/StartSession.h> +#include <Swiften/Elements/StreamFeatures.h> +#include <Swiften/Network/Connection.h> +#include <Swiften/SASL/PLAINMessage.h> +#include <Swiften/StreamStack/XMPPLayer.h> + +#include <Limber/Server/UserRegistry.h> namespace Swift { ServerFromClientSession::ServerFromClientSession( - const std::string& id, - boost::shared_ptr<Connection> connection, - PayloadParserFactoryCollection* payloadParserFactories, - PayloadSerializerCollection* payloadSerializers, - XMLParserFactory* xmlParserFactory, - UserRegistry* userRegistry) : - Session(connection, payloadParserFactories, payloadSerializers, xmlParserFactory), - id_(id), - userRegistry_(userRegistry), - authenticated_(false), - initialized(false), - allowSASLEXTERNAL(false) { + const std::string& id, + std::shared_ptr<Connection> connection, + PayloadParserFactoryCollection* payloadParserFactories, + PayloadSerializerCollection* payloadSerializers, + XMLParserFactory* xmlParserFactory, + UserRegistry* userRegistry) : + Session(connection, payloadParserFactories, payloadSerializers, xmlParserFactory), + id_(id), + userRegistry_(userRegistry), + authenticated_(false), + initialized(false), + allowSASLEXTERNAL(false) { } -void ServerFromClientSession::handleElement(boost::shared_ptr<ToplevelElement> element) { - if (isInitialized()) { - onElementReceived(element); - } - else { - if (AuthRequest* authRequest = dynamic_cast<AuthRequest*>(element.get())) { - if (authRequest->getMechanism() == "PLAIN" || (allowSASLEXTERNAL && authRequest->getMechanism() == "EXTERNAL")) { - if (authRequest->getMechanism() == "EXTERNAL") { - getXMPPLayer()->writeElement(boost::make_shared<AuthSuccess>()); - authenticated_ = true; - getXMPPLayer()->resetParser(); - } - else { - PLAINMessage plainMessage(authRequest->getMessage() ? *authRequest->getMessage() : createSafeByteArray("")); - if (userRegistry_->isValidUserPassword(JID(plainMessage.getAuthenticationID(), getLocalJID().getDomain()), plainMessage.getPassword())) { - getXMPPLayer()->writeElement(boost::make_shared<AuthSuccess>()); - user_ = plainMessage.getAuthenticationID(); - authenticated_ = true; - getXMPPLayer()->resetParser(); - } - else { - getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure)); - finishSession(AuthenticationFailedError); - } - } - } - else { - getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure)); - finishSession(NoSupportedAuthMechanismsError); - } - } - else if (IQ* iq = dynamic_cast<IQ*>(element.get())) { - if (boost::shared_ptr<ResourceBind> resourceBind = iq->getPayload<ResourceBind>()) { - setRemoteJID(JID(user_, getLocalJID().getDomain(), resourceBind->getResource())); - boost::shared_ptr<ResourceBind> resultResourceBind(new ResourceBind()); - resultResourceBind->setJID(getRemoteJID()); - getXMPPLayer()->writeElement(IQ::createResult(JID(), iq->getID(), resultResourceBind)); - } - else if (iq->getPayload<StartSession>()) { - getXMPPLayer()->writeElement(IQ::createResult(getRemoteJID(), iq->getID())); - setInitialized(); - } - } - } +void ServerFromClientSession::handleElement(std::shared_ptr<ToplevelElement> element) { + if (isInitialized()) { + onElementReceived(element); + } + else { + if (AuthRequest* authRequest = dynamic_cast<AuthRequest*>(element.get())) { + if (authRequest->getMechanism() == "PLAIN" || (allowSASLEXTERNAL && authRequest->getMechanism() == "EXTERNAL")) { + if (authRequest->getMechanism() == "EXTERNAL") { + getXMPPLayer()->writeElement(std::make_shared<AuthSuccess>()); + authenticated_ = true; + getXMPPLayer()->resetParser(); + } + else { + PLAINMessage plainMessage(authRequest->getMessage() ? *authRequest->getMessage() : createSafeByteArray("")); + if (userRegistry_->isValidUserPassword(JID(plainMessage.getAuthenticationID(), getLocalJID().getDomain()), plainMessage.getPassword())) { + getXMPPLayer()->writeElement(std::make_shared<AuthSuccess>()); + user_ = plainMessage.getAuthenticationID(); + authenticated_ = true; + getXMPPLayer()->resetParser(); + } + else { + getXMPPLayer()->writeElement(std::shared_ptr<AuthFailure>(new AuthFailure)); + finishSession(AuthenticationFailedError); + } + } + } + else { + getXMPPLayer()->writeElement(std::shared_ptr<AuthFailure>(new AuthFailure)); + finishSession(NoSupportedAuthMechanismsError); + } + } + else if (IQ* iq = dynamic_cast<IQ*>(element.get())) { + if (std::shared_ptr<ResourceBind> resourceBind = iq->getPayload<ResourceBind>()) { + setRemoteJID(JID(user_, getLocalJID().getDomain(), resourceBind->getResource())); + std::shared_ptr<ResourceBind> resultResourceBind(new ResourceBind()); + resultResourceBind->setJID(getRemoteJID()); + getXMPPLayer()->writeElement(IQ::createResult(JID(), iq->getID(), resultResourceBind)); + } + else if (iq->getPayload<StartSession>()) { + getXMPPLayer()->writeElement(IQ::createResult(getRemoteJID(), iq->getID())); + setInitialized(); + } + } + } } void ServerFromClientSession::handleStreamStart(const ProtocolHeader& incomingHeader) { - setLocalJID(JID("", incomingHeader.getTo())); - ProtocolHeader header; - header.setFrom(incomingHeader.getTo()); - header.setID(id_); - getXMPPLayer()->writeHeader(header); + setLocalJID(JID("", incomingHeader.getTo())); + ProtocolHeader header; + header.setFrom(incomingHeader.getTo()); + header.setID(id_); + getXMPPLayer()->writeHeader(header); - boost::shared_ptr<StreamFeatures> features(new StreamFeatures()); - if (!authenticated_) { - features->addAuthenticationMechanism("PLAIN"); - if (allowSASLEXTERNAL) { - features->addAuthenticationMechanism("EXTERNAL"); - } - } - else { - features->setHasResourceBind(); - features->setHasSession(); - } - getXMPPLayer()->writeElement(features); + std::shared_ptr<StreamFeatures> features(new StreamFeatures()); + if (!authenticated_) { + features->addAuthenticationMechanism("PLAIN"); + if (allowSASLEXTERNAL) { + features->addAuthenticationMechanism("EXTERNAL"); + } + } + else { + features->setHasResourceBind(); + features->setHasSession(); + } + getXMPPLayer()->writeElement(features); } void ServerFromClientSession::setInitialized() { - initialized = true; - onSessionStarted(); + initialized = true; + onSessionStarted(); } void ServerFromClientSession::setAllowSASLEXTERNAL() { - allowSASLEXTERNAL = true; + allowSASLEXTERNAL = true; } } diff --git a/Limber/Server/ServerFromClientSession.h b/Limber/Server/ServerFromClientSession.h index 8b03569..6aea194 100644 --- a/Limber/Server/ServerFromClientSession.h +++ b/Limber/Server/ServerFromClientSession.h @@ -1,62 +1,62 @@ /* - * Copyright (c) 2010-2014 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> -#include <boost/enable_shared_from_this.hpp> +#include <memory> #include <string> -#include <Swiften/Base/boost_bsignals.h> -#include <Swiften/Session/Session.h> +#include <boost/signals2.hpp> + +#include <Swiften/Base/ByteArray.h> #include <Swiften/JID/JID.h> #include <Swiften/Network/Connection.h> -#include <Swiften/Base/ByteArray.h> +#include <Swiften/Session/Session.h> namespace Swift { - class ProtocolHeader; - class ToplevelElement; - class Stanza; - class PayloadParserFactoryCollection; - class PayloadSerializerCollection; - class StreamStack; - class UserRegistry; - class XMPPLayer; - class ConnectionLayer; - class Connection; - class XMLParserFactory; - - class ServerFromClientSession : public Session { - public: - ServerFromClientSession( - const std::string& id, - boost::shared_ptr<Connection> connection, - PayloadParserFactoryCollection* payloadParserFactories, - PayloadSerializerCollection* payloadSerializers, - XMLParserFactory* xmlParserFactory, - UserRegistry* userRegistry); - - boost::signal<void ()> onSessionStarted; - void setAllowSASLEXTERNAL(); - - private: - void handleElement(boost::shared_ptr<ToplevelElement>); - void handleStreamStart(const ProtocolHeader& header); - - void setInitialized(); - bool isInitialized() const { - return initialized; - } - - private: - std::string id_; - UserRegistry* userRegistry_; - bool authenticated_; - bool initialized; - bool allowSASLEXTERNAL; - std::string user_; - }; + class ProtocolHeader; + class ToplevelElement; + class Stanza; + class PayloadParserFactoryCollection; + class PayloadSerializerCollection; + class StreamStack; + class UserRegistry; + class XMPPLayer; + class ConnectionLayer; + class Connection; + class XMLParserFactory; + + class ServerFromClientSession : public Session { + public: + ServerFromClientSession( + const std::string& id, + std::shared_ptr<Connection> connection, + PayloadParserFactoryCollection* payloadParserFactories, + PayloadSerializerCollection* payloadSerializers, + XMLParserFactory* xmlParserFactory, + UserRegistry* userRegistry); + + boost::signals2::signal<void ()> onSessionStarted; + void setAllowSASLEXTERNAL(); + + private: + void handleElement(std::shared_ptr<ToplevelElement>); + void handleStreamStart(const ProtocolHeader& header); + + void setInitialized(); + bool isInitialized() const { + return initialized; + } + + private: + std::string id_; + UserRegistry* userRegistry_; + bool authenticated_; + bool initialized; + bool allowSASLEXTERNAL; + std::string user_; + }; } diff --git a/Limber/Server/ServerSession.cpp b/Limber/Server/ServerSession.cpp index 5f07619..a18a60f 100644 --- a/Limber/Server/ServerSession.cpp +++ b/Limber/Server/ServerSession.cpp @@ -1,10 +1,10 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include "Limber/Server/ServerSession.h" +#include <Limber/Server/ServerSession.h> namespace Swift { diff --git a/Limber/Server/ServerSession.h b/Limber/Server/ServerSession.h index 4296e16..0b39d7c 100644 --- a/Limber/Server/ServerSession.h +++ b/Limber/Server/ServerSession.h @@ -1,23 +1,23 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> +#include <memory> #include <Swiften/Elements/Stanza.h> namespace Swift { - class ServerSession { - public: - virtual ~ServerSession(); + class ServerSession { + public: + virtual ~ServerSession(); - virtual const JID& getJID() const = 0; - virtual int getPriority() const = 0; + virtual const JID& getJID() const = 0; + virtual int getPriority() const = 0; - virtual void sendStanza(boost::shared_ptr<Stanza>) = 0; - }; + virtual void sendStanza(std::shared_ptr<Stanza>) = 0; + }; } diff --git a/Limber/Server/ServerStanzaRouter.cpp b/Limber/Server/ServerStanzaRouter.cpp index 02ddb4d..b7ccca8 100644 --- a/Limber/Server/ServerStanzaRouter.cpp +++ b/Limber/Server/ServerStanzaRouter.cpp @@ -1,74 +1,76 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include "Limber/Server/ServerStanzaRouter.h" -#include "Limber/Server/ServerSession.h" +#include <Limber/Server/ServerStanzaRouter.h> -#include <cassert> #include <algorithm> +#include <cassert> + #include <Swiften/Base/Algorithm.h> +#include <Limber/Server/ServerSession.h> + namespace Swift { namespace { - struct PriorityLessThan { - bool operator()(const ServerSession* s1, const ServerSession* s2) const { - return s1->getPriority() < s2->getPriority(); - } - }; + struct PriorityLessThan { + bool operator()(const ServerSession* s1, const ServerSession* s2) const { + return s1->getPriority() < s2->getPriority(); + } + }; - struct HasJID { - HasJID(const JID& jid) : jid(jid) {} - bool operator()(const ServerSession* session) const { - return session->getJID().equals(jid, JID::WithResource); - } - JID jid; - }; + struct HasJID { + HasJID(const JID& jid) : jid(jid) {} + bool operator()(const ServerSession* session) const { + return session->getJID().equals(jid, JID::WithResource); + } + JID jid; + }; } ServerStanzaRouter::ServerStanzaRouter() { } -bool ServerStanzaRouter::routeStanza(boost::shared_ptr<Stanza> stanza) { - JID to = stanza->getTo(); - assert(to.isValid()); +bool ServerStanzaRouter::routeStanza(std::shared_ptr<Stanza> stanza) { + JID to = stanza->getTo(); + assert(to.isValid()); - // For a full JID, first try to route to a session with the full JID - if (!to.isBare()) { - std::vector<ServerSession*>::const_iterator i = std::find_if(clientSessions_.begin(), clientSessions_.end(), HasJID(to)); - if (i != clientSessions_.end()) { - (*i)->sendStanza(stanza); - return true; - } - } + // For a full JID, first try to route to a session with the full JID + if (!to.isBare()) { + std::vector<ServerSession*>::const_iterator i = std::find_if(clientSessions_.begin(), clientSessions_.end(), HasJID(to)); + if (i != clientSessions_.end()) { + (*i)->sendStanza(stanza); + return true; + } + } - // Look for candidate sessions - to = to.toBare(); - std::vector<ServerSession*> candidateSessions; - for (std::vector<ServerSession*>::const_iterator i = clientSessions_.begin(); i != clientSessions_.end(); ++i) { - if ((*i)->getJID().equals(to, JID::WithoutResource) && (*i)->getPriority() >= 0) { - candidateSessions.push_back(*i); - } - } - if (candidateSessions.empty()) { - return false; - } + // Look for candidate sessions + to = to.toBare(); + std::vector<ServerSession*> candidateSessions; + for (std::vector<ServerSession*>::const_iterator i = clientSessions_.begin(); i != clientSessions_.end(); ++i) { + if ((*i)->getJID().equals(to, JID::WithoutResource) && (*i)->getPriority() >= 0) { + candidateSessions.push_back(*i); + } + } + if (candidateSessions.empty()) { + return false; + } - // Find the session with the highest priority - std::vector<ServerSession*>::const_iterator i = std::max_element(clientSessions_.begin(), clientSessions_.end(), PriorityLessThan()); - (*i)->sendStanza(stanza); - return true; + // Find the session with the highest priority + std::vector<ServerSession*>::const_iterator i = std::max_element(clientSessions_.begin(), clientSessions_.end(), PriorityLessThan()); + (*i)->sendStanza(stanza); + return true; } void ServerStanzaRouter::addClientSession(ServerSession* clientSession) { - clientSessions_.push_back(clientSession); + clientSessions_.push_back(clientSession); } void ServerStanzaRouter::removeClientSession(ServerSession* clientSession) { - erase(clientSessions_, clientSession); + erase(clientSessions_, clientSession); } } diff --git a/Limber/Server/ServerStanzaRouter.h b/Limber/Server/ServerStanzaRouter.h index 5f0ad31..04192ba 100644 --- a/Limber/Server/ServerStanzaRouter.h +++ b/Limber/Server/ServerStanzaRouter.h @@ -1,30 +1,30 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once -#include <boost/shared_ptr.hpp> #include <map> +#include <memory> -#include <Swiften/JID/JID.h> #include <Swiften/Elements/Stanza.h> +#include <Swiften/JID/JID.h> namespace Swift { - class ServerSession; + class ServerSession; - class ServerStanzaRouter { - public: - ServerStanzaRouter(); + class ServerStanzaRouter { + public: + ServerStanzaRouter(); - bool routeStanza(boost::shared_ptr<Stanza>); + bool routeStanza(std::shared_ptr<Stanza>); - void addClientSession(ServerSession*); - void removeClientSession(ServerSession*); + void addClientSession(ServerSession*); + void removeClientSession(ServerSession*); - private: - std::vector<ServerSession*> clientSessions_; - }; + private: + std::vector<ServerSession*> clientSessions_; + }; } diff --git a/Limber/Server/SimpleUserRegistry.cpp b/Limber/Server/SimpleUserRegistry.cpp index 2fb2d9e..4f39030 100644 --- a/Limber/Server/SimpleUserRegistry.cpp +++ b/Limber/Server/SimpleUserRegistry.cpp @@ -1,10 +1,10 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include "Limber/Server/SimpleUserRegistry.h" +#include <Limber/Server/SimpleUserRegistry.h> namespace Swift { @@ -12,12 +12,12 @@ SimpleUserRegistry::SimpleUserRegistry() { } bool SimpleUserRegistry::isValidUserPassword(const JID& user, const SafeByteArray& password) const { - std::map<JID,SafeByteArray>::const_iterator i = users.find(user); - return i != users.end() ? i->second == password : false; + std::map<JID,SafeByteArray>::const_iterator i = users.find(user); + return i != users.end() ? i->second == password : false; } void SimpleUserRegistry::addUser(const JID& user, const std::string& password) { - users.insert(std::make_pair(user, createSafeByteArray(password))); + users.insert(std::make_pair(user, createSafeByteArray(password))); } } diff --git a/Limber/Server/SimpleUserRegistry.h b/Limber/Server/SimpleUserRegistry.h index bcf194d..9963b2c 100644 --- a/Limber/Server/SimpleUserRegistry.h +++ b/Limber/Server/SimpleUserRegistry.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -7,22 +7,23 @@ #pragma once #include <map> +#include <string> #include <Swiften/JID/JID.h> -#include <string> -#include "Limber/Server/UserRegistry.h" + +#include <Limber/Server/UserRegistry.h> namespace Swift { - - class SimpleUserRegistry : public UserRegistry { - public: - SimpleUserRegistry(); - virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password) const; - void addUser(const JID& user, const std::string& password); + class SimpleUserRegistry : public UserRegistry { + public: + SimpleUserRegistry(); + + virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password) const; + void addUser(const JID& user, const std::string& password); - private: - std::map<JID, SafeByteArray> users; - }; + private: + std::map<JID, SafeByteArray> users; + }; } diff --git a/Limber/Server/UnitTest/ServerStanzaRouterTest.cpp b/Limber/Server/UnitTest/ServerStanzaRouterTest.cpp index a300b85..5f34d92 100644 --- a/Limber/Server/UnitTest/ServerStanzaRouterTest.cpp +++ b/Limber/Server/UnitTest/ServerStanzaRouterTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -7,144 +7,145 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include "Swiften/Elements/Message.h" -#include "Limber/Server/ServerStanzaRouter.h" -#include "Limber/Server/ServerSession.h" +#include <Swiften/Elements/Message.h> + +#include <Limber/Server/ServerSession.h> +#include <Limber/Server/ServerStanzaRouter.h> using namespace Swift; class ServerStanzaRouterTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ServerStanzaRouterTest); - CPPUNIT_TEST(testRouteStanza_FullJID); - CPPUNIT_TEST(testRouteStanza_FullJIDWithNegativePriority); - CPPUNIT_TEST(testRouteStanza_FullJIDWithOnlyBareJIDMatchingSession); - CPPUNIT_TEST(testRouteStanza_BareJIDWithoutMatchingSession); - CPPUNIT_TEST(testRouteStanza_BareJIDWithMultipleSessions); - CPPUNIT_TEST(testRouteStanza_BareJIDWithOnlyNegativePriorities); - CPPUNIT_TEST(testRouteStanza_BareJIDWithChangingPresence); - CPPUNIT_TEST_SUITE_END(); - - public: - ServerStanzaRouterTest() {} - - void setUp() { - } - - void tearDown() { - } - - void testRouteStanza_FullJID() { - ServerStanzaRouter testling; - MockServerSession session1(JID("foo@bar.com/Bla"), 0); - testling.addClientSession(&session1); - MockServerSession session2(JID("foo@bar.com/Baz"), 0); - testling.addClientSession(&session2); - - bool result = testling.routeStanza(createMessageTo("foo@bar.com/Baz")); - - CPPUNIT_ASSERT(result); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size())); - } - - void testRouteStanza_FullJIDWithNegativePriority() { - ServerStanzaRouter testling; - MockServerSession session1(JID("foo@bar.com/Bla"), -1); - testling.addClientSession(&session1); - MockServerSession session2(JID("foo@bar.com/Baz"), 0); - testling.addClientSession(&session2); - - bool result = testling.routeStanza(createMessageTo("foo@bar.com/Bla")); - - CPPUNIT_ASSERT(result); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session1.sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session2.sentStanzas.size())); - } - - void testRouteStanza_FullJIDWithOnlyBareJIDMatchingSession() { - ServerStanzaRouter testling; - MockServerSession session(JID("foo@bar.com/Bla"), 0); - testling.addClientSession(&session); - - bool result = testling.routeStanza(createMessageTo("foo@bar.com/Baz")); - - CPPUNIT_ASSERT(result); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session.sentStanzas.size())); - } - - void testRouteStanza_BareJIDWithoutMatchingSession() { - ServerStanzaRouter testling; - - bool result = testling.routeStanza(createMessageTo("foo@bar.com")); - - CPPUNIT_ASSERT(!result); - } - - void testRouteStanza_BareJIDWithMultipleSessions() { - ServerStanzaRouter testling; - MockServerSession session1(JID("foo@bar.com/Bla"), 1); - testling.addClientSession(&session1); - MockServerSession session2(JID("foo@bar.com/Baz"), 8); - testling.addClientSession(&session2); - MockServerSession session3(JID("foo@bar.com/Bar"), 5); - testling.addClientSession(&session3); - - bool result = testling.routeStanza(createMessageTo("foo@bar.com")); - - CPPUNIT_ASSERT(result); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session3.sentStanzas.size())); - } - - void testRouteStanza_BareJIDWithOnlyNegativePriorities() { - ServerStanzaRouter testling; - MockServerSession session(JID("foo@bar.com/Bla"), -1); - testling.addClientSession(&session); - - bool result = testling.routeStanza(createMessageTo("foo@bar.com")); - - CPPUNIT_ASSERT(!result); - } - - void testRouteStanza_BareJIDWithChangingPresence() { - ServerStanzaRouter testling; - MockServerSession session1(JID("foo@bar.com/Baz"), 8); - testling.addClientSession(&session1); - MockServerSession session2(JID("foo@bar.com/Bar"), 5); - testling.addClientSession(&session2); - - session1.priority = 3; - session2.priority = 4; - bool result = testling.routeStanza(createMessageTo("foo@bar.com")); - - CPPUNIT_ASSERT(result); - CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size())); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size())); - } - - private: - boost::shared_ptr<Message> createMessageTo(const std::string& recipient) { - boost::shared_ptr<Message> message(new Message()); - message->setTo(JID(recipient)); - return message; - } - - class MockServerSession : public ServerSession { - public: - MockServerSession(const JID& jid, int priority) : jid(jid), priority(priority) {} - - virtual const JID& getJID() const { return jid; } - virtual int getPriority() const { return priority; } - - virtual void sendStanza(boost::shared_ptr<Stanza> stanza) { - sentStanzas.push_back(stanza); - } - - JID jid; - int priority; - std::vector< boost::shared_ptr<Stanza> > sentStanzas; - }; + CPPUNIT_TEST_SUITE(ServerStanzaRouterTest); + CPPUNIT_TEST(testRouteStanza_FullJID); + CPPUNIT_TEST(testRouteStanza_FullJIDWithNegativePriority); + CPPUNIT_TEST(testRouteStanza_FullJIDWithOnlyBareJIDMatchingSession); + CPPUNIT_TEST(testRouteStanza_BareJIDWithoutMatchingSession); + CPPUNIT_TEST(testRouteStanza_BareJIDWithMultipleSessions); + CPPUNIT_TEST(testRouteStanza_BareJIDWithOnlyNegativePriorities); + CPPUNIT_TEST(testRouteStanza_BareJIDWithChangingPresence); + CPPUNIT_TEST_SUITE_END(); + + public: + ServerStanzaRouterTest() {} + + void setUp() { + } + + void tearDown() { + } + + void testRouteStanza_FullJID() { + ServerStanzaRouter testling; + MockServerSession session1(JID("foo@bar.com/Bla"), 0); + testling.addClientSession(&session1); + MockServerSession session2(JID("foo@bar.com/Baz"), 0); + testling.addClientSession(&session2); + + bool result = testling.routeStanza(createMessageTo("foo@bar.com/Baz")); + + CPPUNIT_ASSERT(result); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size())); + } + + void testRouteStanza_FullJIDWithNegativePriority() { + ServerStanzaRouter testling; + MockServerSession session1(JID("foo@bar.com/Bla"), -1); + testling.addClientSession(&session1); + MockServerSession session2(JID("foo@bar.com/Baz"), 0); + testling.addClientSession(&session2); + + bool result = testling.routeStanza(createMessageTo("foo@bar.com/Bla")); + + CPPUNIT_ASSERT(result); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session1.sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session2.sentStanzas.size())); + } + + void testRouteStanza_FullJIDWithOnlyBareJIDMatchingSession() { + ServerStanzaRouter testling; + MockServerSession session(JID("foo@bar.com/Bla"), 0); + testling.addClientSession(&session); + + bool result = testling.routeStanza(createMessageTo("foo@bar.com/Baz")); + + CPPUNIT_ASSERT(result); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session.sentStanzas.size())); + } + + void testRouteStanza_BareJIDWithoutMatchingSession() { + ServerStanzaRouter testling; + + bool result = testling.routeStanza(createMessageTo("foo@bar.com")); + + CPPUNIT_ASSERT(!result); + } + + void testRouteStanza_BareJIDWithMultipleSessions() { + ServerStanzaRouter testling; + MockServerSession session1(JID("foo@bar.com/Bla"), 1); + testling.addClientSession(&session1); + MockServerSession session2(JID("foo@bar.com/Baz"), 8); + testling.addClientSession(&session2); + MockServerSession session3(JID("foo@bar.com/Bar"), 5); + testling.addClientSession(&session3); + + bool result = testling.routeStanza(createMessageTo("foo@bar.com")); + + CPPUNIT_ASSERT(result); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session3.sentStanzas.size())); + } + + void testRouteStanza_BareJIDWithOnlyNegativePriorities() { + ServerStanzaRouter testling; + MockServerSession session(JID("foo@bar.com/Bla"), -1); + testling.addClientSession(&session); + + bool result = testling.routeStanza(createMessageTo("foo@bar.com")); + + CPPUNIT_ASSERT(!result); + } + + void testRouteStanza_BareJIDWithChangingPresence() { + ServerStanzaRouter testling; + MockServerSession session1(JID("foo@bar.com/Baz"), 8); + testling.addClientSession(&session1); + MockServerSession session2(JID("foo@bar.com/Bar"), 5); + testling.addClientSession(&session2); + + session1.priority = 3; + session2.priority = 4; + bool result = testling.routeStanza(createMessageTo("foo@bar.com")); + + CPPUNIT_ASSERT(result); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(session1.sentStanzas.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(session2.sentStanzas.size())); + } + + private: + std::shared_ptr<Message> createMessageTo(const std::string& recipient) { + std::shared_ptr<Message> message(new Message()); + message->setTo(JID(recipient)); + return message; + } + + class MockServerSession : public ServerSession { + public: + MockServerSession(const JID& jid, int priority) : jid(jid), priority(priority) {} + + virtual const JID& getJID() const { return jid; } + virtual int getPriority() const { return priority; } + + virtual void sendStanza(std::shared_ptr<Stanza> stanza) { + sentStanzas.push_back(stanza); + } + + JID jid; + int priority; + std::vector< std::shared_ptr<Stanza> > sentStanzas; + }; }; CPPUNIT_TEST_SUITE_REGISTRATION(ServerStanzaRouterTest); diff --git a/Limber/Server/UserRegistry.cpp b/Limber/Server/UserRegistry.cpp index 51e5129..bd5d189 100644 --- a/Limber/Server/UserRegistry.cpp +++ b/Limber/Server/UserRegistry.cpp @@ -1,10 +1,10 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include "Limber/Server/UserRegistry.h" +#include <Limber/Server/UserRegistry.h> namespace Swift { diff --git a/Limber/Server/UserRegistry.h b/Limber/Server/UserRegistry.h index d7da38b..98b3e99 100644 --- a/Limber/Server/UserRegistry.h +++ b/Limber/Server/UserRegistry.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -7,15 +7,16 @@ #pragma once #include <string> + #include <Swiften/Base/SafeByteArray.h> namespace Swift { - class JID; + class JID; - class UserRegistry { - public: - virtual ~UserRegistry(); + class UserRegistry { + public: + virtual ~UserRegistry(); - virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password) const = 0; - }; + virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password) const = 0; + }; } diff --git a/Limber/main.cpp b/Limber/main.cpp index 9c060ad..26cb8d6 100644 --- a/Limber/main.cpp +++ b/Limber/main.cpp @@ -1,104 +1,106 @@ /* - * Copyright (c) 2010-2014 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ +#include <memory> #include <string> + #include <boost/bind.hpp> -#include <boost/shared_ptr.hpp> -#include "Swiften/Elements/IQ.h" -#include "Swiften/Elements/RosterPayload.h" -#include "Swiften/Elements/VCard.h" -#include "Swiften/Base/IDGenerator.h" -#include "Swiften/EventLoop/EventLoop.h" -#include "Swiften/EventLoop/SimpleEventLoop.h" -#include "Swiften/EventLoop/EventOwner.h" -#include "Swiften/Elements/Stanza.h" -#include "Swiften/Network/ConnectionServer.h" -#include "Swiften/Network/BoostConnection.h" -#include "Swiften/Network/BoostIOServiceThread.h" -#include "Swiften/Network/BoostConnectionServer.h" -#include "Limber/Server/SimpleUserRegistry.h" -#include "Limber/Server/ServerFromClientSession.h" -#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h" -#include "Swiften/Parser/PlatformXMLParserFactory.h" -#include "Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h" +#include <Swiften/Base/IDGenerator.h> +#include <Swiften/Elements/IQ.h> +#include <Swiften/Elements/RosterPayload.h> +#include <Swiften/Elements/Stanza.h> +#include <Swiften/Elements/VCard.h> +#include <Swiften/EventLoop/EventLoop.h> +#include <Swiften/EventLoop/EventOwner.h> +#include <Swiften/EventLoop/SimpleEventLoop.h> +#include <Swiften/Network/BoostConnection.h> +#include <Swiften/Network/BoostConnectionServer.h> +#include <Swiften/Network/BoostIOServiceThread.h> +#include <Swiften/Network/ConnectionServer.h> +#include <Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h> +#include <Swiften/Parser/PlatformXMLParserFactory.h> +#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> + +#include <Limber/Server/ServerFromClientSession.h> +#include <Limber/Server/SimpleUserRegistry.h> using namespace Swift; class Server { - public: - Server(UserRegistry* userRegistry, EventLoop* eventLoop) : userRegistry_(userRegistry) { - serverFromClientConnectionServer_ = BoostConnectionServer::create(5222, boostIOServiceThread_.getIOService(), eventLoop); - serverFromClientConnectionServer_->onNewConnection.connect(boost::bind(&Server::handleNewConnection, this, _1)); - serverFromClientConnectionServer_->start(); - } + public: + Server(UserRegistry* userRegistry, EventLoop* eventLoop) : userRegistry_(userRegistry) { + serverFromClientConnectionServer_ = BoostConnectionServer::create(5222, boostIOServiceThread_.getIOService(), eventLoop); + serverFromClientConnectionServer_->onNewConnection.connect(boost::bind(&Server::handleNewConnection, this, _1)); + serverFromClientConnectionServer_->start(); + } - private: - void handleNewConnection(boost::shared_ptr<Connection> c) { - boost::shared_ptr<ServerFromClientSession> session(new ServerFromClientSession(idGenerator_.generateID(), c, &payloadParserFactories_, &payloadSerializers_, &xmlParserFactory, userRegistry_)); - serverFromClientSessions_.push_back(session); - session->onElementReceived.connect(boost::bind(&Server::handleElementReceived, this, _1, session)); - session->onSessionFinished.connect(boost::bind(&Server::handleSessionFinished, this, session)); - session->startSession(); - } + private: + void handleNewConnection(std::shared_ptr<Connection> c) { + std::shared_ptr<ServerFromClientSession> session(new ServerFromClientSession(idGenerator_.generateID(), c, &payloadParserFactories_, &payloadSerializers_, &xmlParserFactory, userRegistry_)); + serverFromClientSessions_.push_back(session); + session->onElementReceived.connect(boost::bind(&Server::handleElementReceived, this, _1, session)); + session->onSessionFinished.connect(boost::bind(&Server::handleSessionFinished, this, session)); + session->startSession(); + } - void handleSessionFinished(boost::shared_ptr<ServerFromClientSession> session) { - serverFromClientSessions_.erase(std::remove(serverFromClientSessions_.begin(), serverFromClientSessions_.end(), session), serverFromClientSessions_.end()); - } + void handleSessionFinished(std::shared_ptr<ServerFromClientSession> session) { + serverFromClientSessions_.erase(std::remove(serverFromClientSessions_.begin(), serverFromClientSessions_.end(), session), serverFromClientSessions_.end()); + } - void handleElementReceived(boost::shared_ptr<ToplevelElement> element, boost::shared_ptr<ServerFromClientSession> session) { - boost::shared_ptr<Stanza> stanza(boost::dynamic_pointer_cast<Stanza>(element)); - if (!stanza) { - return; - } - stanza->setFrom(session->getRemoteJID()); - if (!stanza->getTo().isValid()) { - stanza->setTo(JID(session->getLocalJID())); - } - if (!stanza->getTo().isValid() || stanza->getTo() == session->getLocalJID() || stanza->getTo() == session->getRemoteJID().toBare()) { - if (boost::shared_ptr<IQ> iq = boost::dynamic_pointer_cast<IQ>(stanza)) { - if (iq->getPayload<RosterPayload>()) { - session->sendElement(IQ::createResult(iq->getFrom(), iq->getID(), boost::make_shared<RosterPayload>())); - } - if (iq->getPayload<VCard>()) { - if (iq->getType() == IQ::Get) { - boost::shared_ptr<VCard> vcard(new VCard()); - vcard->setNickname(iq->getFrom().getNode()); - session->sendElement(IQ::createResult(iq->getFrom(), iq->getID(), vcard)); - } - else { - session->sendElement(IQ::createError(iq->getFrom(), iq->getID(), ErrorPayload::Forbidden, ErrorPayload::Cancel)); - } - } - else { - session->sendElement(IQ::createError(iq->getFrom(), iq->getID(), ErrorPayload::FeatureNotImplemented, ErrorPayload::Cancel)); - } - } - } - } + void handleElementReceived(std::shared_ptr<ToplevelElement> element, std::shared_ptr<ServerFromClientSession> session) { + std::shared_ptr<Stanza> stanza(std::dynamic_pointer_cast<Stanza>(element)); + if (!stanza) { + return; + } + stanza->setFrom(session->getRemoteJID()); + if (!stanza->getTo().isValid()) { + stanza->setTo(JID(session->getLocalJID())); + } + if (!stanza->getTo().isValid() || stanza->getTo() == session->getLocalJID() || stanza->getTo() == session->getRemoteJID().toBare()) { + if (std::shared_ptr<IQ> iq = std::dynamic_pointer_cast<IQ>(stanza)) { + if (iq->getPayload<RosterPayload>()) { + session->sendElement(IQ::createResult(iq->getFrom(), iq->getID(), std::make_shared<RosterPayload>())); + } + if (iq->getPayload<VCard>()) { + if (iq->getType() == IQ::Get) { + std::shared_ptr<VCard> vcard(new VCard()); + vcard->setNickname(iq->getFrom().getNode()); + session->sendElement(IQ::createResult(iq->getFrom(), iq->getID(), vcard)); + } + else { + session->sendElement(IQ::createError(iq->getFrom(), iq->getID(), ErrorPayload::Forbidden, ErrorPayload::Cancel)); + } + } + else { + session->sendElement(IQ::createError(iq->getFrom(), iq->getID(), ErrorPayload::FeatureNotImplemented, ErrorPayload::Cancel)); + } + } + } + } - private: - IDGenerator idGenerator_; - PlatformXMLParserFactory xmlParserFactory; - UserRegistry* userRegistry_; - BoostIOServiceThread boostIOServiceThread_; - boost::shared_ptr<BoostConnectionServer> serverFromClientConnectionServer_; - std::vector< boost::shared_ptr<ServerFromClientSession> > serverFromClientSessions_; - FullPayloadParserFactoryCollection payloadParserFactories_; - FullPayloadSerializerCollection payloadSerializers_; + private: + IDGenerator idGenerator_; + PlatformXMLParserFactory xmlParserFactory; + UserRegistry* userRegistry_; + BoostIOServiceThread boostIOServiceThread_; + std::shared_ptr<BoostConnectionServer> serverFromClientConnectionServer_; + std::vector< std::shared_ptr<ServerFromClientSession> > serverFromClientSessions_; + FullPayloadParserFactoryCollection payloadParserFactories_; + FullPayloadSerializerCollection payloadSerializers_; }; int main() { - SimpleEventLoop eventLoop; - SimpleUserRegistry userRegistry; - userRegistry.addUser(JID("remko@localhost"), "remko"); - userRegistry.addUser(JID("kevin@localhost"), "kevin"); - userRegistry.addUser(JID("remko@limber.swift.im"), "remko"); - userRegistry.addUser(JID("kevin@limber.swift.im"), "kevin"); - Server server(&userRegistry, &eventLoop); - eventLoop.run(); - return 0; + SimpleEventLoop eventLoop; + SimpleUserRegistry userRegistry; + userRegistry.addUser(JID("remko@localhost"), "remko"); + userRegistry.addUser(JID("kevin@localhost"), "kevin"); + userRegistry.addUser(JID("remko@limber.swift.im"), "remko"); + userRegistry.addUser(JID("kevin@limber.swift.im"), "kevin"); + Server server(&userRegistry, &eventLoop); + eventLoop.run(); + return 0; } |