summaryrefslogtreecommitdiffstats
blob: e4ca471a9c81226a492e78bd4bb8b9000f44ec80 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright (c) 2011-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
#include <Swiften/Network/BOSHConnectionPool.h>

#include <climits>

#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Base/SafeString.h>
#include <Swiften/Network/CachingDomainNameResolver.h>
#include <Swiften/Network/HTTPConnectProxiedConnectionFactory.h>

namespace Swift {
BOSHConnectionPool::BOSHConnectionPool(const URL& boshURL, DomainNameResolver* realResolver, ConnectionFactory* connectionFactoryParameter, XMLParserFactory* parserFactory, TLSContextFactory* tlsFactory, TimerFactory* timerFactory, EventLoop* eventLoop, const std::string& to, unsigned long long initialRID, const URL& boshHTTPConnectProxyURL, const SafeString& boshHTTPConnectProxyAuthID, const SafeString& boshHTTPConnectProxyAuthPassword, const TLSOptions& tlsOptions, std::shared_ptr<HTTPTrafficFilter> trafficFilter) :
        boshURL(boshURL),
        connectionFactory(connectionFactoryParameter),
        xmlParserFactory(parserFactory),
        timerFactory(timerFactory),
        rid(initialRID),
        pendingTerminate(false),
        to(to),
        requestLimit(2),
        restartCount(0),
        pendingRestart(false),
        tlsContextFactory_(tlsFactory),
        tlsOptions_(tlsOptions) {

    if (!boshHTTPConnectProxyURL.isEmpty()) {
        connectionFactory = new HTTPConnectProxiedConnectionFactory(realResolver, connectionFactory, timerFactory, boshHTTPConnectProxyURL.getHost(), URL::getPortOrDefaultPort(boshHTTPConnectProxyURL), boshHTTPConnectProxyAuthID, boshHTTPConnectProxyAuthPassword, trafficFilter);
    }
    resolver = new CachingDomainNameResolver(realResolver, eventLoop);
}

BOSHConnectionPool::~BOSHConnectionPool() {
    /* Don't do a normal close here. Instead kill things forcibly, as close() or writeFooter() will already have been called */
    std::vector<BOSHConnection::ref> connectionCopies = connections;
    for (auto&& connection : connectionCopies) {
        if (connection) {
            destroyConnection(connection);
            connection->disconnect();
        }
    }
    for (auto factory : myConnectionFactories) {
        delete factory;
    }
    delete resolver;
}

void BOSHConnectionPool::write(const SafeByteArray& data) {
    dataQueue.push_back(data);
    tryToSendQueuedData();
}

void BOSHConnectionPool::handleDataRead(const SafeByteArray& data) {
    onXMPPDataRead(data);
    tryToSendQueuedData(); /* Will rebalance the connections */
}

void BOSHConnectionPool::restartStream() {
    BOSHConnection::ref connection = getSuitableConnection();
    if (connection) {
        pendingRestart = false;
        rid++;
        connection->setRID(rid);
        connection->restartStream();
        restartCount++;
    }
    else {
        pendingRestart = true;
    }
}

void BOSHConnectionPool::setTLSCertificate(CertificateWithKey::ref certWithKey) {
    clientCertificate = certWithKey;
}

bool BOSHConnectionPool::isTLSEncrypted() const {
    return !pinnedCertificateChain_.empty();
}

Certificate::ref BOSHConnectionPool::getPeerCertificate() const {
    Certificate::ref peerCertificate;
    if (!pinnedCertificateChain_.empty()) {
        peerCertificate = pinnedCertificateChain_[0];
    }
    return peerCertificate;
}

std::vector<Certificate::ref> BOSHConnectionPool::getPeerCertificateChain() const {
    return pinnedCertificateChain_;
}

std::shared_ptr<CertificateVerificationError> BOSHConnectionPool::getPeerCertificateVerificationError() const {
    return lastVerificationError_;
}

void BOSHConnectionPool::writeFooter() {
    pendingTerminate = true;
    tryToSendQueuedData();
}

void BOSHConnectionPool::open() {
    createConnection();
}

void BOSHConnectionPool::close() {
    if (!sid.empty()) {
        writeFooter();
    }
    else {
        pendingTerminate = true;
        std::vector<BOSHConnection::ref> connectionCopies = connections;
        for (auto&& connection : connectionCopies) {
            if (connection) {
                connection->disconnect();
            }
        }
    }
}

void BOSHConnectionPool::handleSessionStarted(const std::string& sessionID, size_t requests) {
    sid = sessionID;
    requestLimit = requests;
    onSessionStarted();
}

void BOSHConnectionPool::handleConnectFinished(bool error, BOSHConnection::ref connection) {
    if (error) {
        onSessionTerminated(std::make_shared<BOSHError>(BOSHError::UndefinedCondition));
        /*TODO: We can probably manage to not terminate the stream here and use the rid/ack retry
         * logic to just swallow the error and try again (some number of tries).
         */
    }
    else {
        if (connection->getPeerCertificate() && pinnedCertificateChain_.empty()) {
            pinnedCertificateChain_ = connection->getPeerCertificateChain();
        }
        if (!pinnedCertificateChain_.empty()) {
            lastVerificationError_ = connection->getPeerCertificateVerificationError();
        }

        if (sid.empty()) {
            connection->startStream(to, rid);
        }
        if (pendingRestart) {
            restartStream();
        }
        tryToSendQueuedData();
    }
}

BOSHConnection::ref BOSHConnectionPool::getSuitableConnection() {
    BOSHConnection::ref suitableConnection;
    for (auto&& connection : connections) {
        if (connection->isReadyToSend()) {
            suitableConnection = connection;
            break;
        }
    }

    if (!suitableConnection && connections.size() < requestLimit) {
        /* This is not a suitable connection because it won't have yet connected and added TLS if needed. */
        BOSHConnection::ref newConnection = createConnection();
        newConnection->setSID(sid);
    }
    assert(connections.size() <= requestLimit);
    assert((!suitableConnection) || suitableConnection->isReadyToSend());
    return suitableConnection;
}

void BOSHConnectionPool::tryToSendQueuedData() {
    if (sid.empty()) {
        /* If we've not got as far as stream start yet, pend */
        return;
    }

    BOSHConnection::ref suitableConnection = getSuitableConnection();
    bool toSend = !dataQueue.empty();
    if (suitableConnection) {
        if (toSend) {
            rid++;
            suitableConnection->setRID(rid);
            SafeByteArray data;
            for (const auto& datum : dataQueue) {
                data.insert(data.end(), datum.begin(), datum.end());
            }
            suitableConnection->write(data);
            dataQueue.clear();
        }
        else if (pendingTerminate) {
            rid++;
            suitableConnection->setRID(rid);
            suitableConnection->terminateStream();
            sid = "";
            close();
        }
    }
    if (!pendingTerminate) {
        /* Ensure there's always a session waiting to read data for us */
        bool pending = false;
        for (auto&& connection : connections) {
            if (connection && !connection->isReadyToSend()) {
                pending = true;
            }
        }
        if (!pending) {
            if (restartCount >= 1) {
                /* Don't open a second connection until we've restarted the stream twice - i.e. we've authed and resource bound.*/
                if (suitableConnection) {
                    rid++;
                    suitableConnection->setRID(rid);
                    suitableConnection->write(createSafeByteArray(""));
                }
                else {
                /* My thought process I went through when writing this, to aid anyone else confused why this can happen...
                 *
                 * What to do here? I think this isn't possible.
                   If you didn't have two connections, suitable would have made one.
                   If you have two connections and neither is suitable, pending would be true.
                   If you have a non-pending connection, it's suitable.

                   If I decide to do something here, remove assert above.

                   Ah! Yes, because there's a period between creating the connection and it being connected. */
                }
            }
        }
    }
}

void BOSHConnectionPool::handleHTTPError(const std::string& /*errorCode*/) {
    handleSessionTerminated(std::make_shared<BOSHError>(BOSHError::UndefinedCondition));
}

void BOSHConnectionPool::handleConnectionDisconnected(bool/* error*/, BOSHConnection::ref connection) {
    destroyConnection(connection);
    if (pendingTerminate && sid.empty() && connections.empty()) {
        handleSessionTerminated(BOSHError::ref());
    }
    //else if (error) {
    //    handleSessionTerminated(std::make_shared<BOSHError>(BOSHError::UndefinedCondition));
    //}
    else {
        /* We might have just freed up a connection slot to send with */
        tryToSendQueuedData();
    }
}

std::shared_ptr<BOSHConnection> BOSHConnectionPool::createConnection() {
    Connector::ref connector = Connector::create(boshURL.getHost(), URL::getPortOrDefaultPort(boshURL), boost::optional<std::string>(), resolver, connectionFactory, timerFactory);
    BOSHConnection::ref connection = BOSHConnection::create(boshURL, connector, xmlParserFactory, tlsContextFactory_, tlsOptions_);
    connection->onXMPPDataRead.connect(boost::bind(&BOSHConnectionPool::handleDataRead, this, _1));
    connection->onSessionStarted.connect(boost::bind(&BOSHConnectionPool::handleSessionStarted, this, _1, _2));
    connection->onBOSHDataRead.connect(boost::bind(&BOSHConnectionPool::handleBOSHDataRead, this, _1));
    connection->onBOSHDataWritten.connect(boost::bind(&BOSHConnectionPool::handleBOSHDataWritten, this, _1));
    connection->onDisconnected.connect(boost::bind(&BOSHConnectionPool::handleConnectionDisconnected, this, _1, connection));
    connection->onConnectFinished.connect(boost::bind(&BOSHConnectionPool::handleConnectFinished, this, _1, connection));
    connection->onSessionTerminated.connect(boost::bind(&BOSHConnectionPool::handleSessionTerminated, this, _1));
    connection->onHTTPError.connect(boost::bind(&BOSHConnectionPool::handleHTTPError, this, _1));

    if (boshURL.getScheme() == "https") {
        bool success = connection->setClientCertificate(clientCertificate);
        SWIFT_LOG(debug) << "setClientCertificate, success: " << success << std::endl;
    }

    connection->connect();
    connections.push_back(connection);
    return connection;
}

void BOSHConnectionPool::destroyConnection(std::shared_ptr<BOSHConnection> connection) {
    connections.erase(std::remove(connections.begin(), connections.end(), connection), connections.end());
    connection->onXMPPDataRead.disconnect(boost::bind(&BOSHConnectionPool::handleDataRead, this, _1));
    connection->onSessionStarted.disconnect(boost::bind(&BOSHConnectionPool::handleSessionStarted, this, _1, _2));
    connection->onBOSHDataRead.disconnect(boost::bind(&BOSHConnectionPool::handleBOSHDataRead, this, _1));
    connection->onBOSHDataWritten.disconnect(boost::bind(&BOSHConnectionPool::handleBOSHDataWritten, this, _1));
    connection->onDisconnected.disconnect(boost::bind(&BOSHConnectionPool::handleConnectionDisconnected, this, _1, connection));
    connection->onConnectFinished.disconnect(boost::bind(&BOSHConnectionPool::handleConnectFinished, this, _1, connection));
    connection->onSessionTerminated.disconnect(boost::bind(&BOSHConnectionPool::handleSessionTerminated, this, _1));
    connection->onHTTPError.disconnect(boost::bind(&BOSHConnectionPool::handleHTTPError, this, _1));
}

void BOSHConnectionPool::handleSessionTerminated(BOSHError::ref error) {
    onSessionTerminated(error);
}

void BOSHConnectionPool::handleBOSHDataRead(const SafeByteArray& data) {
    onBOSHDataRead(data);
}

void BOSHConnectionPool::handleBOSHDataWritten(const SafeByteArray& data) {
    onBOSHDataWritten(data);
}

}