summaryrefslogtreecommitdiffstats
blob: a0e6b239683b53d1d0dd995268bbf39a276eeb04 (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
/*
 * Copyright (c) 2010-2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Network/Connector.h>

#include <boost/bind.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/Network/DomainNameAddressQuery.h>
#include <Swiften/Network/DomainNameResolver.h>
#include <Swiften/Network/HostAddress.h>
#include <Swiften/Network/TimerFactory.h>

namespace Swift {

Connector::Connector(const std::string& hostname, unsigned short port, const boost::optional<std::string>& serviceLookupPrefix, DomainNameResolver* resolver, ConnectionFactory* connectionFactory, TimerFactory* timerFactory) : hostname(hostname), port(port), serviceLookupPrefix(serviceLookupPrefix), resolver(resolver), connectionFactory(connectionFactory), timerFactory(timerFactory), timeoutMilliseconds(0), queriedAllServices(true), foundSomeDNS(false) {
}

void Connector::setTimeoutMilliseconds(int milliseconds) {
    timeoutMilliseconds = milliseconds;
}

void Connector::start() {
    SWIFT_LOG(debug) << "Starting connector for " << hostname << std::endl;
    assert(!currentConnection);
    assert(!serviceQuery);
    assert(!timer);
    auto hostAddress = HostAddress::fromString(hostname);
    if (timeoutMilliseconds > 0) {
        timer = timerFactory->createTimer(timeoutMilliseconds);
        timer->onTick.connect(boost::bind(&Connector::handleTimeout, shared_from_this()));
    }
    if (serviceLookupPrefix) {
        queriedAllServices = false;
        serviceQuery = resolver->createServiceQuery(*serviceLookupPrefix, hostname);
        serviceQuery->onResult.connect(boost::bind(&Connector::handleServiceQueryResult, shared_from_this(), _1));
        serviceQuery->run();
    }
    else if (hostAddress) {
        // hostname is already a valid address; skip name lookup.
        foundSomeDNS = true;
        addressQueryResults.push_back(hostAddress.get());
        tryNextAddress();
    } else {
        queryAddress(hostname);
    }
}

void Connector::stop() {
    if (currentConnection) {
        currentConnection->onConnectFinished.disconnect(boost::bind(&Connector::handleConnectionConnectFinished, shared_from_this(), _1));
        currentConnection->disconnect();
    }
    finish(std::shared_ptr<Connection>());
}

void Connector::queryAddress(const std::string& hostname) {
    assert(!addressQuery);
    addressQuery = resolver->createAddressQuery(hostname);
    addressQuery->onResult.connect(boost::bind(&Connector::handleAddressQueryResult, shared_from_this(), _1, _2));
    addressQuery->run();
}

void Connector::handleServiceQueryResult(const std::vector<DomainNameServiceQuery::Result>& result) {
    SWIFT_LOG(debug) << result.size() << " SRV result(s)" << std::endl;
    serviceQueryResults = std::deque<DomainNameServiceQuery::Result>(result.begin(), result.end());
    serviceQuery.reset();
    if (!serviceQueryResults.empty()) {
        foundSomeDNS = true;
    }
    tryNextServiceOrFallback();
}

void Connector::tryNextServiceOrFallback() {
    if (queriedAllServices) {
        SWIFT_LOG(debug) << "Queried all services" << std::endl;
        finish(std::shared_ptr<Connection>());
    }
    else if (serviceQueryResults.empty()) {
        SWIFT_LOG(debug) << "Falling back on A resolution" << std::endl;
        // Fall back on simple address resolving
        queriedAllServices = true;
        queryAddress(hostname);
    }
    else {
        SWIFT_LOG(debug) << "Querying next address" << std::endl;
        queryAddress(serviceQueryResults.front().hostname);
    }
}

void Connector::handleAddressQueryResult(const std::vector<HostAddress>& addresses, boost::optional<DomainNameResolveError> error) {
    SWIFT_LOG(debug) << addresses.size() << " addresses" << std::endl;
    addressQuery.reset();
    if (error || addresses.empty()) {
        if (!serviceQueryResults.empty()) {
            serviceQueryResults.pop_front();
        }
        tryNextServiceOrFallback();
    }
    else {
        foundSomeDNS = true;
        addressQueryResults = std::deque<HostAddress>(addresses.begin(), addresses.end());
        tryNextAddress();
    }
}

void Connector::tryNextAddress() {
    if (addressQueryResults.empty()) {
        SWIFT_LOG(debug) << "Done trying addresses. Moving on." << std::endl;
        // Done trying all addresses. Move on to the next host.
        if (!serviceQueryResults.empty()) {
            serviceQueryResults.pop_front();
        }
        tryNextServiceOrFallback();
    }
    else {
        SWIFT_LOG(debug) << "Trying next address" << std::endl;
        HostAddress address = addressQueryResults.front();
        addressQueryResults.pop_front();

        unsigned short connectPort = (port == 0 ? 5222 : port);
        if (!serviceQueryResults.empty()) {
            connectPort = serviceQueryResults.front().port;
        }

        tryConnect(HostAddressPort(address, connectPort));
    }
}

void Connector::tryConnect(const HostAddressPort& target) {
    assert(!currentConnection);
    SWIFT_LOG(debug) << "Trying to connect to " << target.getAddress().toString() << ":" << target.getPort() << std::endl;
    currentConnection = connectionFactory->createConnection();
    currentConnection->onConnectFinished.connect(boost::bind(&Connector::handleConnectionConnectFinished, shared_from_this(), _1));
    currentConnection->connect(target);
    if (timer) {
        timer->start();
    }
}

void Connector::handleConnectionConnectFinished(bool error) {
    SWIFT_LOG(debug) << "ConnectFinished: " << (error ? "error" : "success") << std::endl;
    if (timer) {
            timer->stop();
            timer.reset();
    }
    if (!currentConnection) {
        // We've hit a race condition where multiple finisheds were on the eventloop queue at once.
        // This is particularly likely on macOS where the hourly momentary wakeup while asleep
        // can cause both a timeout and an onConnectFinished to be queued sequentially (SWIFT-232).
        // Let the first one process as normal, but ignore the second.
        return;
    }
    currentConnection->onConnectFinished.disconnect(boost::bind(&Connector::handleConnectionConnectFinished, shared_from_this(), _1));
    if (error) {
        currentConnection.reset();
        if (!addressQueryResults.empty()) {
            tryNextAddress();
        }
        else {
            if (!serviceQueryResults.empty()) {
                serviceQueryResults.pop_front();
            }
            tryNextServiceOrFallback();
        }
    }
    else {
        finish(currentConnection);
    }
}

void Connector::finish(std::shared_ptr<Connection> connection) {
    if (timer) {
        timer->stop();
        timer->onTick.disconnect(boost::bind(&Connector::handleTimeout, shared_from_this()));
        timer.reset();
    }
    if (serviceQuery) {
        serviceQuery->onResult.disconnect(boost::bind(&Connector::handleServiceQueryResult, shared_from_this(), _1));
        serviceQuery.reset();
    }
    if (addressQuery) {
        addressQuery->onResult.disconnect(boost::bind(&Connector::handleAddressQueryResult, shared_from_this(), _1, _2));
        addressQuery.reset();
    }
    if (currentConnection) {
        currentConnection->onConnectFinished.disconnect(boost::bind(&Connector::handleConnectionConnectFinished, shared_from_this(), _1));
        currentConnection.reset();
    }
    onConnectFinished(connection, (connection || foundSomeDNS) ? std::shared_ptr<Error>() : std::make_shared<DomainNameResolveError>());
}

void Connector::handleTimeout() {
    SWIFT_LOG(debug) << "Timeout" << std::endl;
    SWIFT_LOG_ASSERT(currentConnection, error) << "Connection not valid but triggered a timeout" <<std::endl;
    handleConnectionConnectFinished(true);
}

}