summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Session')
-rw-r--r--Swiften/Session/BasicSessionStream.cpp140
-rw-r--r--Swiften/Session/BasicSessionStream.h75
-rw-r--r--Swiften/Session/Session.cpp82
-rw-r--r--Swiften/Session/Session.h105
-rw-r--r--Swiften/Session/SessionStream.cpp8
-rw-r--r--Swiften/Session/SessionStream.h69
-rw-r--r--Swiften/Session/SessionTracer.h29
7 files changed, 508 insertions, 0 deletions
diff --git a/Swiften/Session/BasicSessionStream.cpp b/Swiften/Session/BasicSessionStream.cpp
new file mode 100644
index 0000000..ed7f1eb
--- /dev/null
+++ b/Swiften/Session/BasicSessionStream.cpp
@@ -0,0 +1,140 @@
+#include "Swiften/Session/BasicSessionStream.h"
+
+#include <boost/bind.hpp>
+
+#include "Swiften/StreamStack/XMPPLayer.h"
+#include "Swiften/StreamStack/StreamStack.h"
+#include "Swiften/StreamStack/ConnectionLayer.h"
+#include "Swiften/StreamStack/WhitespacePingLayer.h"
+#include "Swiften/StreamStack/CompressionLayer.h"
+#include "Swiften/StreamStack/TLSLayer.h"
+#include "Swiften/StreamStack/TLSLayerFactory.h"
+
+namespace Swift {
+
+BasicSessionStream::BasicSessionStream(boost::shared_ptr<Connection> connection, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers, TLSLayerFactory* tlsLayerFactory, TimerFactory* timerFactory) : available(false), connection(connection), payloadParserFactories(payloadParserFactories), payloadSerializers(payloadSerializers), tlsLayerFactory(tlsLayerFactory), timerFactory(timerFactory) {
+}
+
+void BasicSessionStream::initialize() {
+ xmppLayer = boost::shared_ptr<XMPPLayer>(
+ new XMPPLayer(payloadParserFactories, payloadSerializers));
+ xmppLayer->onStreamStart.connect(boost::bind(&BasicSessionStream::handleStreamStartReceived, shared_from_this(), _1));
+ xmppLayer->onElement.connect(boost::bind(&BasicSessionStream::handleElementReceived, shared_from_this(), _1));
+ xmppLayer->onError.connect(boost::bind(
+ &BasicSessionStream::handleXMPPError, shared_from_this()));
+ xmppLayer->onDataRead.connect(boost::bind(&BasicSessionStream::handleDataRead, shared_from_this(), _1));
+ xmppLayer->onWriteData.connect(boost::bind(&BasicSessionStream::handleDataWritten, shared_from_this(), _1));
+
+ connection->onDisconnected.connect(boost::bind(&BasicSessionStream::handleConnectionError, shared_from_this(), _1));
+ connectionLayer = boost::shared_ptr<ConnectionLayer>(
+ new ConnectionLayer(connection));
+
+ streamStack = new StreamStack(xmppLayer, connectionLayer);
+
+ available = true;
+}
+
+BasicSessionStream::~BasicSessionStream() {
+ delete streamStack;
+}
+
+void BasicSessionStream::writeHeader(const ProtocolHeader& header) {
+ assert(available);
+ xmppLayer->writeHeader(header);
+}
+
+void BasicSessionStream::writeElement(boost::shared_ptr<Element> element) {
+ assert(available);
+ xmppLayer->writeElement(element);
+}
+
+void BasicSessionStream::writeFooter() {
+ assert(available);
+ xmppLayer->writeFooter();
+}
+
+bool BasicSessionStream::isAvailable() {
+ return available;
+}
+
+bool BasicSessionStream::supportsTLSEncryption() {
+ return tlsLayerFactory && tlsLayerFactory->canCreate();
+}
+
+void BasicSessionStream::addTLSEncryption() {
+ assert(available);
+ tlsLayer = tlsLayerFactory->createTLSLayer();
+ if (hasTLSCertificate() && !tlsLayer->setClientCertificate(getTLSCertificate())) {
+ onError(boost::shared_ptr<Error>(new Error(Error::InvalidTLSCertificateError)));
+ }
+ else {
+ streamStack->addLayer(tlsLayer);
+ tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, shared_from_this()));
+ tlsLayer->onConnected.connect(boost::bind(&BasicSessionStream::handleTLSConnected, shared_from_this()));
+ tlsLayer->connect();
+ }
+}
+
+void BasicSessionStream::addZLibCompression() {
+ boost::shared_ptr<CompressionLayer> compressionLayer(new CompressionLayer());
+ streamStack->addLayer(compressionLayer);
+}
+
+void BasicSessionStream::setWhitespacePingEnabled(bool enabled) {
+ if (enabled) {
+ if (!whitespacePingLayer) {
+ whitespacePingLayer = boost::shared_ptr<WhitespacePingLayer>(new WhitespacePingLayer(timerFactory));
+ streamStack->addLayer(whitespacePingLayer);
+ }
+ whitespacePingLayer->setActive();
+ }
+ else if (whitespacePingLayer) {
+ whitespacePingLayer->setInactive();
+ }
+}
+
+void BasicSessionStream::resetXMPPParser() {
+ xmppLayer->resetParser();
+}
+
+void BasicSessionStream::handleStreamStartReceived(const ProtocolHeader& header) {
+ onStreamStartReceived(header);
+}
+
+void BasicSessionStream::handleElementReceived(boost::shared_ptr<Element> element) {
+ onElementReceived(element);
+}
+
+void BasicSessionStream::handleXMPPError() {
+ available = false;
+ onError(boost::shared_ptr<Error>(new Error(Error::ParseError)));
+}
+
+void BasicSessionStream::handleTLSConnected() {
+ onTLSEncrypted();
+}
+
+void BasicSessionStream::handleTLSError() {
+ available = false;
+ onError(boost::shared_ptr<Error>(new Error(Error::TLSError)));
+}
+
+void BasicSessionStream::handleConnectionError(const boost::optional<Connection::Error>& error) {
+ available = false;
+ if (error == Connection::ReadError) {
+ onError(boost::shared_ptr<Error>(new Error(Error::ConnectionReadError)));
+ }
+ else {
+ onError(boost::shared_ptr<Error>(new Error(Error::ConnectionWriteError)));
+ }
+}
+
+void BasicSessionStream::handleDataRead(const ByteArray& data) {
+ onDataRead(String(data.getData(), data.getSize()));
+}
+
+void BasicSessionStream::handleDataWritten(const ByteArray& data) {
+ onDataWritten(String(data.getData(), data.getSize()));
+}
+
+};
diff --git a/Swiften/Session/BasicSessionStream.h b/Swiften/Session/BasicSessionStream.h
new file mode 100644
index 0000000..8618458
--- /dev/null
+++ b/Swiften/Session/BasicSessionStream.h
@@ -0,0 +1,75 @@
+#pragma once
+
+#include <boost/shared_ptr.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+#include "Swiften/Network/Connection.h"
+#include "Swiften/Session/SessionStream.h"
+
+namespace Swift {
+ class TLSLayerFactory;
+ class TLSLayer;
+ class TimerFactory;
+ class WhitespacePingLayer;
+ class PayloadParserFactoryCollection;
+ class PayloadSerializerCollection;
+ class StreamStack;
+ class XMPPLayer;
+ class ConnectionLayer;
+ class CompressionLayer;
+
+ class BasicSessionStream :
+ public SessionStream,
+ public boost::enable_shared_from_this<BasicSessionStream> {
+ public:
+ BasicSessionStream(
+ boost::shared_ptr<Connection> connection,
+ PayloadParserFactoryCollection* payloadParserFactories,
+ PayloadSerializerCollection* payloadSerializers,
+ TLSLayerFactory* tlsLayerFactory,
+ TimerFactory* whitespacePingLayerFactory
+ );
+ ~BasicSessionStream();
+
+ void initialize();
+
+ virtual bool isAvailable();
+
+ virtual void writeHeader(const ProtocolHeader& header);
+ virtual void writeElement(boost::shared_ptr<Element>);
+ virtual void writeFooter();
+
+ virtual void addZLibCompression();
+
+ virtual bool supportsTLSEncryption();
+ virtual void addTLSEncryption();
+
+ virtual void setWhitespacePingEnabled(bool);
+
+ virtual void resetXMPPParser();
+
+ private:
+ void handleConnectionError(const boost::optional<Connection::Error>& error);
+ void handleXMPPError();
+ void handleTLSConnected();
+ void handleTLSError();
+ void handleStreamStartReceived(const ProtocolHeader&);
+ void handleElementReceived(boost::shared_ptr<Element>);
+ void handleDataRead(const ByteArray& data);
+ void handleDataWritten(const ByteArray& data);
+
+ private:
+ bool available;
+ boost::shared_ptr<Connection> connection;
+ PayloadParserFactoryCollection* payloadParserFactories;
+ PayloadSerializerCollection* payloadSerializers;
+ TLSLayerFactory* tlsLayerFactory;
+ TimerFactory* timerFactory;
+ boost::shared_ptr<XMPPLayer> xmppLayer;
+ boost::shared_ptr<ConnectionLayer> connectionLayer;
+ StreamStack* streamStack;
+ boost::shared_ptr<CompressionLayer> compressionLayer;
+ boost::shared_ptr<TLSLayer> tlsLayer;
+ boost::shared_ptr<WhitespacePingLayer> whitespacePingLayer;
+ };
+}
diff --git a/Swiften/Session/Session.cpp b/Swiften/Session/Session.cpp
new file mode 100644
index 0000000..64456db
--- /dev/null
+++ b/Swiften/Session/Session.cpp
@@ -0,0 +1,82 @@
+#include "Swiften/Session/Session.h"
+
+#include <boost/bind.hpp>
+
+#include "Swiften/StreamStack/XMPPLayer.h"
+#include "Swiften/StreamStack/StreamStack.h"
+
+namespace Swift {
+
+Session::Session(
+ boost::shared_ptr<Connection> connection,
+ PayloadParserFactoryCollection* payloadParserFactories,
+ PayloadSerializerCollection* payloadSerializers) :
+ connection(connection),
+ payloadParserFactories(payloadParserFactories),
+ payloadSerializers(payloadSerializers),
+ streamStack(0),
+ finishing(false) {
+}
+
+Session::~Session() {
+ delete streamStack;
+}
+
+void Session::startSession() {
+ initializeStreamStack();
+ handleSessionStarted();
+}
+
+void Session::finishSession() {
+ finishing = true;
+ connection->disconnect();
+ handleSessionFinished(boost::optional<SessionError>());
+ finishing = false;
+ onSessionFinished(boost::optional<SessionError>());
+}
+
+void Session::finishSession(const SessionError& error) {
+ finishing = true;
+ connection->disconnect();
+ handleSessionFinished(boost::optional<SessionError>(error));
+ finishing = false;
+ onSessionFinished(boost::optional<SessionError>(error));
+}
+
+void Session::initializeStreamStack() {
+ xmppLayer = boost::shared_ptr<XMPPLayer>(
+ new XMPPLayer(payloadParserFactories, payloadSerializers));
+ xmppLayer->onStreamStart.connect(
+ boost::bind(&Session::handleStreamStart, shared_from_this(), _1));
+ xmppLayer->onElement.connect(boost::bind(&Session::handleElement, shared_from_this(), _1));
+ xmppLayer->onError.connect(
+ boost::bind(&Session::finishSession, shared_from_this(), XMLError));
+ xmppLayer->onDataRead.connect(boost::bind(boost::ref(onDataRead), _1));
+ xmppLayer->onWriteData.connect(boost::bind(boost::ref(onDataWritten), _1));
+ connection->onDisconnected.connect(
+ boost::bind(&Session::handleDisconnected, shared_from_this(), _1));
+ connectionLayer = boost::shared_ptr<ConnectionLayer>(new ConnectionLayer(connection));
+ streamStack = new StreamStack(xmppLayer, connectionLayer);
+}
+
+void Session::sendElement(boost::shared_ptr<Element> stanza) {
+ xmppLayer->writeElement(stanza);
+}
+
+void Session::handleDisconnected(const boost::optional<Connection::Error>& connectionError) {
+ if (connectionError) {
+ switch (*connectionError) {
+ case Connection::ReadError:
+ finishSession(ConnectionReadError);
+ break;
+ case Connection::WriteError:
+ finishSession(ConnectionWriteError);
+ break;
+ }
+ }
+ else {
+ finishSession();
+ }
+}
+
+}
diff --git a/Swiften/Session/Session.h b/Swiften/Session/Session.h
new file mode 100644
index 0000000..d63254a
--- /dev/null
+++ b/Swiften/Session/Session.h
@@ -0,0 +1,105 @@
+#pragma once
+
+#include <boost/shared_ptr.hpp>
+#include <boost/signal.hpp>
+#include <boost/optional.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+#include "Swiften/JID/JID.h"
+#include "Swiften/Elements/Element.h"
+#include "Swiften/Network/Connection.h"
+#include "Swiften/StreamStack/ConnectionLayer.h"
+
+namespace Swift {
+ class ProtocolHeader;
+ class StreamStack;
+ class JID;
+ class Element;
+ class ByteArray;
+ class PayloadParserFactoryCollection;
+ class PayloadSerializerCollection;
+ class XMPPLayer;
+
+ class Session : public boost::enable_shared_from_this<Session> {
+ public:
+ enum SessionError {
+ ConnectionReadError,
+ ConnectionWriteError,
+ XMLError,
+ AuthenticationFailedError,
+ NoSupportedAuthMechanismsError,
+ UnexpectedElementError,
+ ResourceBindError,
+ SessionStartError,
+ TLSError,
+ ClientCertificateLoadError,
+ ClientCertificateError
+ };
+
+ Session(
+ boost::shared_ptr<Connection> connection,
+ PayloadParserFactoryCollection* payloadParserFactories,
+ PayloadSerializerCollection* payloadSerializers);
+ virtual ~Session();
+
+ void startSession();
+ void finishSession();
+
+ void sendElement(boost::shared_ptr<Element>);
+
+ const JID& getLocalJID() const {
+ return localJID;
+ }
+
+ const JID& getRemoteJID() const {
+ return remoteJID;
+ }
+
+ boost::signal<void (boost::shared_ptr<Element>)> onElementReceived;
+ boost::signal<void (const boost::optional<SessionError>&)> onSessionFinished;
+ boost::signal<void (const ByteArray&)> onDataWritten;
+ boost::signal<void (const ByteArray&)> onDataRead;
+
+ protected:
+ void setRemoteJID(const JID& j) {
+ remoteJID = j;
+ }
+
+ void setLocalJID(const JID& j) {
+ localJID = j;
+ }
+
+ void finishSession(const SessionError&);
+
+ virtual void handleSessionStarted() {}
+ virtual void handleSessionFinished(const boost::optional<SessionError>&) {}
+ virtual void handleElement(boost::shared_ptr<Element>) = 0;
+ virtual void handleStreamStart(const ProtocolHeader&) = 0;
+
+ void initializeStreamStack();
+
+ boost::shared_ptr<XMPPLayer> getXMPPLayer() const {
+ return xmppLayer;
+ }
+
+ StreamStack* getStreamStack() const {
+ return streamStack;
+ }
+
+ void setFinished();
+
+ private:
+ void handleDisconnected(const boost::optional<Connection::Error>& error);
+
+ private:
+ JID localJID;
+ JID remoteJID;
+ boost::shared_ptr<Connection> connection;
+ PayloadParserFactoryCollection* payloadParserFactories;
+ PayloadSerializerCollection* payloadSerializers;
+ boost::shared_ptr<XMPPLayer> xmppLayer;
+ boost::shared_ptr<ConnectionLayer> connectionLayer;
+ StreamStack* streamStack;
+ bool finishing;
+ };
+}
diff --git a/Swiften/Session/SessionStream.cpp b/Swiften/Session/SessionStream.cpp
new file mode 100644
index 0000000..1d73d0f
--- /dev/null
+++ b/Swiften/Session/SessionStream.cpp
@@ -0,0 +1,8 @@
+#include "Swiften/Session/SessionStream.h"
+
+namespace Swift {
+
+SessionStream::~SessionStream() {
+}
+
+};
diff --git a/Swiften/Session/SessionStream.h b/Swiften/Session/SessionStream.h
new file mode 100644
index 0000000..8c64ccf
--- /dev/null
+++ b/Swiften/Session/SessionStream.h
@@ -0,0 +1,69 @@
+#pragma once
+
+#include <boost/signal.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Elements/ProtocolHeader.h"
+#include "Swiften/Elements/Element.h"
+#include "Swiften/Base/Error.h"
+#include "Swiften/TLS/PKCS12Certificate.h"
+
+namespace Swift {
+ class SessionStream {
+ public:
+ class Error : public Swift::Error {
+ public:
+ enum Type {
+ ParseError,
+ TLSError,
+ InvalidTLSCertificateError,
+ ConnectionReadError,
+ ConnectionWriteError
+ };
+
+ Error(Type type) : type(type) {}
+
+ Type type;
+ };
+
+ virtual ~SessionStream();
+
+ virtual bool isAvailable() = 0;
+
+ virtual void writeHeader(const ProtocolHeader& header) = 0;
+ virtual void writeFooter() = 0;
+ virtual void writeElement(boost::shared_ptr<Element>) = 0;
+
+ virtual void addZLibCompression() = 0;
+
+ virtual bool supportsTLSEncryption() = 0;
+ virtual void addTLSEncryption() = 0;
+ virtual void setWhitespacePingEnabled(bool enabled) = 0;
+
+ virtual void resetXMPPParser() = 0;
+
+ void setTLSCertificate(const PKCS12Certificate& cert) {
+ certificate = cert;
+ }
+
+ virtual bool hasTLSCertificate() {
+ return !certificate.isNull();
+ }
+
+
+ boost::signal<void (const ProtocolHeader&)> onStreamStartReceived;
+ boost::signal<void (boost::shared_ptr<Element>)> onElementReceived;
+ boost::signal<void (boost::shared_ptr<Error>)> onError;
+ boost::signal<void ()> onTLSEncrypted;
+ boost::signal<void (const String&)> onDataRead;
+ boost::signal<void (const String&)> onDataWritten;
+
+ protected:
+ const PKCS12Certificate& getTLSCertificate() const {
+ return certificate;
+ }
+
+ private:
+ PKCS12Certificate certificate;
+ };
+}
diff --git a/Swiften/Session/SessionTracer.h b/Swiften/Session/SessionTracer.h
new file mode 100644
index 0000000..29a07e0
--- /dev/null
+++ b/Swiften/Session/SessionTracer.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <iostream>
+
+#include "Swiften/Session/Session.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/Base/ByteArray.h"
+
+namespace Swift {
+ class SessionTracer {
+ public:
+ SessionTracer(boost::shared_ptr<Session> session) : session(session) {
+ session->onDataRead.connect(boost::bind(&SessionTracer::printData, this, '<', _1));
+ session->onDataWritten.connect(boost::bind(&SessionTracer::printData, this, '>', _1));
+ }
+
+ private:
+ void printData(char direction, const ByteArray& data) {
+ std::cerr << direction << direction << " " << session->getLocalJID() << " ";
+ for (unsigned int i = 0; i < 72 - session->getLocalJID().toString().getLength() - session->getRemoteJID().toString().getLength(); ++i) {
+ std::cerr << direction;
+ }
+ std::cerr << " " << session->getRemoteJID()<< " " << direction << direction << std::endl;
+ std::cerr << String(data.getData(), data.getSize()) << std::endl;
+ }
+
+ boost::shared_ptr<Session> session;
+ };
+}