summaryrefslogtreecommitdiffstats
blob: 697115aa6d116766a3e8a420b8ce322ee3398b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Session/BasicSessionStream.h>

#include <boost/bind.hpp>
#include <boost/smart_ptr/make_shared.hpp>

#include <Swiften/StreamStack/CompressionLayer.h>
#include <Swiften/StreamStack/ConnectionLayer.h>
#include <Swiften/StreamStack/StreamStack.h>
#include <Swiften/StreamStack/TLSLayer.h>
#include <Swiften/StreamStack/WhitespacePingLayer.h>
#include <Swiften/StreamStack/XMPPLayer.h>
#include <Swiften/TLS/TLSContext.h>
#include <Swiften/TLS/TLSContextFactory.h>

namespace Swift {

BasicSessionStream::BasicSessionStream(
        StreamType streamType,
        boost::shared_ptr<Connection> connection,
        PayloadParserFactoryCollection* payloadParserFactories,
        PayloadSerializerCollection* payloadSerializers,
        TLSContextFactory* tlsContextFactory,
        TimerFactory* timerFactory,
        XMLParserFactory* xmlParserFactory,
        const TLSOptions& tlsOptions) :
            available(false),
            connection(connection),
            tlsContextFactory(tlsContextFactory),
            timerFactory(timerFactory),
            compressionLayer(NULL),
            tlsLayer(NULL),
            whitespacePingLayer(NULL),
            tlsOptions_(tlsOptions) {
    xmppLayer = new XMPPLayer(payloadParserFactories, payloadSerializers, xmlParserFactory, streamType);
    xmppLayer->onStreamStart.connect(boost::bind(&BasicSessionStream::handleStreamStartReceived, this, _1));
    xmppLayer->onElement.connect(boost::bind(&BasicSessionStream::handleElementReceived, this, _1));
    xmppLayer->onError.connect(boost::bind(&BasicSessionStream::handleXMPPError, this));
    xmppLayer->onDataRead.connect(boost::bind(&BasicSessionStream::handleDataRead, this, _1));
    xmppLayer->onWriteData.connect(boost::bind(&BasicSessionStream::handleDataWritten, this, _1));

    connection->onDisconnected.connect(boost::bind(&BasicSessionStream::handleConnectionFinished, this, _1));
    connectionLayer = new ConnectionLayer(connection);

    streamStack = new StreamStack(xmppLayer, connectionLayer);

    available = true;
}

BasicSessionStream::~BasicSessionStream() {
    delete compressionLayer;

    if (tlsLayer) {
        tlsLayer->onError.disconnect(boost::bind(&BasicSessionStream::handleTLSError, this, _1));
        tlsLayer->onConnected.disconnect(boost::bind(&BasicSessionStream::handleTLSConnected, this));
        delete tlsLayer;
    }
    delete whitespacePingLayer;
    delete streamStack;

    connection->onDisconnected.disconnect(boost::bind(&BasicSessionStream::handleConnectionFinished, this, _1));
    delete connectionLayer;

    xmppLayer->onStreamStart.disconnect(boost::bind(&BasicSessionStream::handleStreamStartReceived, this, _1));
    xmppLayer->onElement.disconnect(boost::bind(&BasicSessionStream::handleElementReceived, this, _1));
    xmppLayer->onError.disconnect(boost::bind(&BasicSessionStream::handleXMPPError, this));
    xmppLayer->onDataRead.disconnect(boost::bind(&BasicSessionStream::handleDataRead, this, _1));
    xmppLayer->onWriteData.disconnect(boost::bind(&BasicSessionStream::handleDataWritten, this, _1));
    delete xmppLayer;
}

void BasicSessionStream::writeHeader(const ProtocolHeader& header) {
    assert(available);
    xmppLayer->writeHeader(header);
}

void BasicSessionStream::writeElement(boost::shared_ptr<ToplevelElement> element) {
    assert(available);
    xmppLayer->writeElement(element);
}

void BasicSessionStream::writeFooter() {
    assert(available);
    xmppLayer->writeFooter();
}

void BasicSessionStream::writeData(const std::string& data) {
    assert(available);
    xmppLayer->writeData(data);
}

void BasicSessionStream::close() {
    connection->disconnect();
}

bool BasicSessionStream::isOpen() {
    return available;
}

bool BasicSessionStream::supportsTLSEncryption() {
    return tlsContextFactory && tlsContextFactory->canCreate();
}

void BasicSessionStream::addTLSEncryption() {
    assert(available);
    tlsLayer = new TLSLayer(tlsContextFactory, tlsOptions_);
    if (hasTLSCertificate() && !tlsLayer->setClientCertificate(getTLSCertificate())) {
        onClosed(boost::make_shared<SessionStreamError>(SessionStreamError::InvalidTLSCertificateError));
    }
    else {
        streamStack->addLayer(tlsLayer);
        tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, this, _1));
        tlsLayer->onConnected.connect(boost::bind(&BasicSessionStream::handleTLSConnected, this));
        tlsLayer->connect();
    }
}

bool BasicSessionStream::isTLSEncrypted() {
    return tlsLayer;
}

Certificate::ref BasicSessionStream::getPeerCertificate() const {
    return tlsLayer->getPeerCertificate();
}

std::vector<Certificate::ref> BasicSessionStream::getPeerCertificateChain() const {
    return tlsLayer->getPeerCertificateChain();
}

boost::shared_ptr<CertificateVerificationError> BasicSessionStream::getPeerCertificateVerificationError() const {
    return tlsLayer->getPeerCertificateVerificationError();
}

ByteArray BasicSessionStream::getTLSFinishMessage() const {
    return tlsLayer->getContext()->getFinishMessage();
}

bool BasicSessionStream::supportsZLibCompression() {
    return true;
}

void BasicSessionStream::addZLibCompression() {
    compressionLayer = new CompressionLayer();
    streamStack->addLayer(compressionLayer);
}

void BasicSessionStream::setWhitespacePingEnabled(bool enabled) {
    if (enabled) {
        if (!whitespacePingLayer) {
            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<ToplevelElement> element) {
    onElementReceived(element);
}

void BasicSessionStream::handleXMPPError() {
    available = false;
    onClosed(boost::make_shared<SessionStreamError>(SessionStreamError::ParseError));
}

void BasicSessionStream::handleTLSConnected() {
    onTLSEncrypted();
}

void BasicSessionStream::handleTLSError(boost::shared_ptr<TLSError> error) {
    available = false;
    onClosed(error);
}

void BasicSessionStream::handleConnectionFinished(const boost::optional<Connection::Error>& error) {
    available = false;
    if (error == Connection::ReadError) {
        onClosed(boost::make_shared<SessionStreamError>(SessionStreamError::ConnectionReadError));
    }
    else if (error) {
        onClosed(boost::make_shared<SessionStreamError>(SessionStreamError::ConnectionWriteError));
    }
    else {
        onClosed(boost::shared_ptr<SessionStreamError>());
    }
}

void BasicSessionStream::handleDataRead(const SafeByteArray& data) {
    onDataRead(data);
}

void BasicSessionStream::handleDataWritten(const SafeByteArray& data) {
    onDataWritten(data);
}

}