summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Swiften/Client/CoreClient.cpp9
-rw-r--r--Swiften/Network/BOSHConnectionPool.cpp2
-rw-r--r--Swiften/Network/ChainedConnector.cpp6
-rw-r--r--Swiften/Network/ChainedConnector.h5
-rw-r--r--Swiften/Network/Connector.cpp6
-rw-r--r--Swiften/Network/Connector.h10
-rw-r--r--Swiften/Network/ProxiedConnection.cpp2
-rw-r--r--Swiften/Network/UnitTest/BOSHConnectionTest.cpp2
-rw-r--r--Swiften/Network/UnitTest/ChainedConnectorTest.cpp2
-rw-r--r--Swiften/Network/UnitTest/ConnectorTest.cpp10
10 files changed, 30 insertions, 24 deletions
diff --git a/Swiften/Client/CoreClient.cpp b/Swiften/Client/CoreClient.cpp
index 4438135..f6a3bb8 100644
--- a/Swiften/Client/CoreClient.cpp
+++ b/Swiften/Client/CoreClient.cpp
@@ -1,44 +1,45 @@
/*
- * Copyright (c) 2010-2011 Remko Tronçon
+ * Copyright (c) 2010-2014 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <Swiften/Client/CoreClient.h>
#include <boost/bind.hpp>
+#include <boost/optional.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Base/IDGenerator.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Base/Algorithm.h>
#include <Swiften/Client/ClientSession.h>
#include <Swiften/TLS/CertificateVerificationError.h>
#include <Swiften/TLS/TLSError.h>
#include <Swiften/Network/ChainedConnector.h>
#include <Swiften/Network/NetworkFactories.h>
#include <Swiften/Network/ProxyProvider.h>
#include <Swiften/Network/DomainNameResolveError.h>
#include <Swiften/TLS/PKCS12Certificate.h>
#include <Swiften/Session/BasicSessionStream.h>
#include <Swiften/Session/BOSHSessionStream.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Client/ClientSessionStanzaChannel.h>
#include <Swiften/Network/SOCKS5ProxiedConnectionFactory.h>
#include <Swiften/Network/HTTPConnectProxiedConnectionFactory.h>
namespace Swift {
CoreClient::CoreClient(const JID& jid, const SafeByteArray& password, NetworkFactories* networkFactories) : jid_(jid), password_(password), networkFactories(networkFactories), disconnectRequested_(false), certificateTrustChecker(NULL) {
stanzaChannel_ = new ClientSessionStanzaChannel();
stanzaChannel_->onMessageReceived.connect(boost::bind(&CoreClient::handleMessageReceived, this, _1));
stanzaChannel_->onPresenceReceived.connect(boost::bind(&CoreClient::handlePresenceReceived, this, _1));
stanzaChannel_->onStanzaAcked.connect(boost::bind(&CoreClient::handleStanzaAcked, this, _1));
stanzaChannel_->onAvailableChanged.connect(boost::bind(&CoreClient::handleStanzaChannelAvailableChanged, this, _1));
iqRouter_ = new IQRouter(stanzaChannel_);
iqRouter_->setJID(jid);
}
CoreClient::~CoreClient() {
@@ -76,73 +77,77 @@ void CoreClient::connect(const ClientOptions& o) {
SWIFT_LOG(debug) << "Found SOCK5 Proxy: " << systemSOCKS5Proxy.getAddress().toString() << ":" << systemHTTPConnectProxy.getPort() << std::endl;
proxyConnectionFactories.push_back(new SOCKS5ProxiedConnectionFactory(networkFactories->getDomainNameResolver(), networkFactories->getConnectionFactory(), networkFactories->getTimerFactory(), systemSOCKS5Proxy.getAddress().toString(), systemSOCKS5Proxy.getPort()));
}
if (systemHTTPConnectProxy.isValid()) {
SWIFT_LOG(debug) << "Found HTTPConnect Proxy: " << systemHTTPConnectProxy.getAddress().toString() << ":" << systemHTTPConnectProxy.getPort() << std::endl;
proxyConnectionFactories.push_back(new HTTPConnectProxiedConnectionFactory(networkFactories->getDomainNameResolver(), networkFactories->getConnectionFactory(), networkFactories->getTimerFactory(), systemHTTPConnectProxy.getAddress().toString(), systemHTTPConnectProxy.getPort()));
}
break;
case ClientOptions::SOCKS5Proxy: {
SWIFT_LOG(debug) << " with manual configured SOCKS5 proxy" << std::endl;
std::string proxyHostname = o.manualProxyHostname.empty() ? systemSOCKS5Proxy.getAddress().toString() : o.manualProxyHostname;
int proxyPort = o.manualProxyPort == -1 ? systemSOCKS5Proxy.getPort() : o.manualProxyPort;
SWIFT_LOG(debug) << "Proxy: " << proxyHostname << ":" << proxyPort << std::endl;
proxyConnectionFactories.push_back(new SOCKS5ProxiedConnectionFactory(networkFactories->getDomainNameResolver(), networkFactories->getConnectionFactory(), networkFactories->getTimerFactory(), proxyHostname, proxyPort));
useDirectConnection = false;
break;
}
case ClientOptions::HTTPConnectProxy: {
SWIFT_LOG(debug) << " with manual configured HTTPConnect proxy" << std::endl;
std::string proxyHostname = o.manualProxyHostname.empty() ? systemHTTPConnectProxy.getAddress().toString() : o.manualProxyHostname;
int proxyPort = o.manualProxyPort == -1 ? systemHTTPConnectProxy.getPort() : o.manualProxyPort;
SWIFT_LOG(debug) << "Proxy: " << proxyHostname << ":" << proxyPort << std::endl;
proxyConnectionFactories.push_back(new HTTPConnectProxiedConnectionFactory(networkFactories->getDomainNameResolver(), networkFactories->getConnectionFactory(), networkFactories->getTimerFactory(), proxyHostname, proxyPort));
useDirectConnection = false;
break;
}
}
std::vector<ConnectionFactory*> connectionFactories(proxyConnectionFactories);
if (useDirectConnection) {
connectionFactories.push_back(networkFactories->getConnectionFactory());
}
// Create connector
std::string host = o.manualHostname.empty() ? jid_.getDomain() : o.manualHostname;
int port = o.manualPort;
+ boost::optional<std::string> serviceLookupPrefix;
+ if (o.manualHostname.empty()) {
+ serviceLookupPrefix = "_xmpp-client._tcp.";
+ }
assert(!connector_);
if (options.boshURL.isEmpty()) {
- connector_ = boost::make_shared<ChainedConnector>(host, port, o.manualHostname.empty(), networkFactories->getDomainNameResolver(), connectionFactories, networkFactories->getTimerFactory());
+ connector_ = boost::make_shared<ChainedConnector>(host, port, serviceLookupPrefix, networkFactories->getDomainNameResolver(), connectionFactories, networkFactories->getTimerFactory());
connector_->onConnectFinished.connect(boost::bind(&CoreClient::handleConnectorFinished, this, _1, _2));
connector_->setTimeoutMilliseconds(2*60*1000);
connector_->start();
}
else {
/* Autodiscovery of which proxy works is largely ok with a TCP session, because this is a one-off. With BOSH
* it would be quite painful given that potentially every stanza could be sent on a new connection.
*/
//sessionStream_ = boost::make_shared<BOSHSessionStream>(boost::make_shared<BOSHConnectionFactory>(options.boshURL, networkFactories->getConnectionFactory(), networkFactories->getXMLParserFactory(), networkFactories->getTLSContextFactory()), getPayloadParserFactories(), getPayloadSerializers(), networkFactories->getTLSContextFactory(), networkFactories->getTimerFactory(), networkFactories->getXMLParserFactory(), networkFactories->getEventLoop(), host, options.boshHTTPConnectProxyURL, options.boshHTTPConnectProxyAuthID, options.boshHTTPConnectProxyAuthPassword);
sessionStream_ = boost::shared_ptr<BOSHSessionStream>(new BOSHSessionStream(
options.boshURL,
getPayloadParserFactories(),
getPayloadSerializers(),
networkFactories->getConnectionFactory(),
networkFactories->getTLSContextFactory(),
networkFactories->getTimerFactory(),
networkFactories->getXMLParserFactory(),
networkFactories->getEventLoop(),
networkFactories->getDomainNameResolver(),
host,
options.boshHTTPConnectProxyURL,
options.boshHTTPConnectProxyAuthID,
options.boshHTTPConnectProxyAuthPassword));
sessionStream_->onDataRead.connect(boost::bind(&CoreClient::handleDataRead, this, _1));
sessionStream_->onDataWritten.connect(boost::bind(&CoreClient::handleDataWritten, this, _1));
bindSessionToStream();
}
}
void CoreClient::bindSessionToStream() {
session_ = ClientSession::create(jid_, sessionStream_, networkFactories->getIDNConverter(), networkFactories->getCryptoProvider());
session_->setCertificateTrustChecker(certificateTrustChecker);
session_->setUseStreamCompression(options.useStreamCompression);
session_->setAllowPLAINOverNonTLS(options.allowPLAINWithoutTLS);
diff --git a/Swiften/Network/BOSHConnectionPool.cpp b/Swiften/Network/BOSHConnectionPool.cpp
index 4517ffb..fdfe420 100644
--- a/Swiften/Network/BOSHConnectionPool.cpp
+++ b/Swiften/Network/BOSHConnectionPool.cpp
@@ -193,71 +193,71 @@ void BOSHConnectionPool::tryToSendQueuedData() {
/* 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(boost::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(boost::make_shared<BOSHError>(BOSHError::UndefinedCondition));
//}
else {
/* We might have just freed up a connection slot to send with */
tryToSendQueuedData();
}
}
boost::shared_ptr<BOSHConnection> BOSHConnectionPool::createConnection() {
- Connector::ref connector = Connector::create(boshURL.getHost(), URL::getPortOrDefaultPort(boshURL), false, resolver, connectionFactory, timerFactory);
+ 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);
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));
connection->connect();
connections.push_back(connection);
return connection;
}
void BOSHConnectionPool::destroyConnection(boost::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) {
diff --git a/Swiften/Network/ChainedConnector.cpp b/Swiften/Network/ChainedConnector.cpp
index 8c7c04b..14d66ae 100644
--- a/Swiften/Network/ChainedConnector.cpp
+++ b/Swiften/Network/ChainedConnector.cpp
@@ -1,87 +1,87 @@
/*
* Copyright (c) 2011 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <Swiften/Network/ChainedConnector.h>
#include <boost/bind.hpp>
#include <typeinfo>
#include <Swiften/Base/Log.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Network/Connector.h>
#include <Swiften/Network/ConnectionFactory.h>
using namespace Swift;
ChainedConnector::ChainedConnector(
const std::string& hostname,
int port,
- bool doServiceLookups,
+ const boost::optional<std::string>& serviceLookupPrefix,
DomainNameResolver* resolver,
const std::vector<ConnectionFactory*>& connectionFactories,
TimerFactory* timerFactory) :
hostname(hostname),
port(port),
- doServiceLookups(doServiceLookups),
+ serviceLookupPrefix(serviceLookupPrefix),
resolver(resolver),
connectionFactories(connectionFactories),
timerFactory(timerFactory),
timeoutMilliseconds(0) {
}
void ChainedConnector::setTimeoutMilliseconds(int milliseconds) {
timeoutMilliseconds = milliseconds;
}
void ChainedConnector::start() {
SWIFT_LOG(debug) << "Starting queued connector for " << hostname << std::endl;
connectionFactoryQueue = std::deque<ConnectionFactory*>(connectionFactories.begin(), connectionFactories.end());
tryNextConnectionFactory();
}
void ChainedConnector::stop() {
if (currentConnector) {
currentConnector->onConnectFinished.disconnect(boost::bind(&ChainedConnector::handleConnectorFinished, this, _1, _2));
currentConnector->stop();
currentConnector.reset();
}
finish(boost::shared_ptr<Connection>(), boost::shared_ptr<Error>());
}
void ChainedConnector::tryNextConnectionFactory() {
assert(!currentConnector);
if (connectionFactoryQueue.empty()) {
SWIFT_LOG(debug) << "No more connection factories" << std::endl;
finish(boost::shared_ptr<Connection>(), lastError);
}
else {
ConnectionFactory* connectionFactory = connectionFactoryQueue.front();
SWIFT_LOG(debug) << "Trying next connection factory: " << typeid(*connectionFactory).name() << std::endl;
connectionFactoryQueue.pop_front();
- currentConnector = Connector::create(hostname, port, doServiceLookups, resolver, connectionFactory, timerFactory);
+ currentConnector = Connector::create(hostname, port, serviceLookupPrefix, resolver, connectionFactory, timerFactory);
currentConnector->setTimeoutMilliseconds(timeoutMilliseconds);
currentConnector->onConnectFinished.connect(boost::bind(&ChainedConnector::handleConnectorFinished, this, _1, _2));
currentConnector->start();
}
}
void ChainedConnector::handleConnectorFinished(boost::shared_ptr<Connection> connection, boost::shared_ptr<Error> error) {
SWIFT_LOG(debug) << "Connector finished" << std::endl;
currentConnector->onConnectFinished.disconnect(boost::bind(&ChainedConnector::handleConnectorFinished, this, _1, _2));
lastError = error;
currentConnector.reset();
if (connection) {
finish(connection, error);
}
else {
tryNextConnectionFactory();
}
}
void ChainedConnector::finish(boost::shared_ptr<Connection> connection, boost::shared_ptr<Error> error) {
onConnectFinished(connection, error);
}
diff --git a/Swiften/Network/ChainedConnector.h b/Swiften/Network/ChainedConnector.h
index 03462bc..0a1cca9 100644
--- a/Swiften/Network/ChainedConnector.h
+++ b/Swiften/Network/ChainedConnector.h
@@ -1,52 +1,53 @@
/*
* Copyright (c) 2011 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#pragma once
#include <string>
#include <vector>
#include <deque>
#include <boost/shared_ptr.hpp>
+#include <boost/optional.hpp>
#include <Swiften/Base/API.h>
#include <Swiften/Base/boost_bsignals.h>
#include <Swiften/Base/Error.h>
namespace Swift {
class Connection;
class Connector;
class ConnectionFactory;
class TimerFactory;
class DomainNameResolver;
class SWIFTEN_API ChainedConnector {
public:
- ChainedConnector(const std::string& hostname, int port, bool doServiceLookups, DomainNameResolver*, const std::vector<ConnectionFactory*>&, TimerFactory*);
+ ChainedConnector(const std::string& hostname, int port, const boost::optional<std::string>& serviceLookupPrefix, DomainNameResolver*, const std::vector<ConnectionFactory*>&, TimerFactory*);
void setTimeoutMilliseconds(int milliseconds);
void start();
void stop();
boost::signal<void (boost::shared_ptr<Connection>, boost::shared_ptr<Error>)> onConnectFinished;
private:
void finish(boost::shared_ptr<Connection> connection, boost::shared_ptr<Error>);
void tryNextConnectionFactory();
void handleConnectorFinished(boost::shared_ptr<Connection>, boost::shared_ptr<Error>);
private:
std::string hostname;
int port;
- bool doServiceLookups;
+ boost::optional<std::string> serviceLookupPrefix;
DomainNameResolver* resolver;
std::vector<ConnectionFactory*> connectionFactories;
TimerFactory* timerFactory;
int timeoutMilliseconds;
std::deque<ConnectionFactory*> connectionFactoryQueue;
boost::shared_ptr<Connector> currentConnector;
boost::shared_ptr<Error> lastError;
};
}
diff --git a/Swiften/Network/Connector.cpp b/Swiften/Network/Connector.cpp
index a0155cf..a58efbc 100644
--- a/Swiften/Network/Connector.cpp
+++ b/Swiften/Network/Connector.cpp
@@ -1,70 +1,70 @@
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <Swiften/Network/Connector.h>
#include <boost/bind.hpp>
#include <iostream>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/Network/DomainNameResolver.h>
#include <Swiften/Network/DomainNameAddressQuery.h>
#include <Swiften/Network/TimerFactory.h>
#include <Swiften/Base/Log.h>
namespace Swift {
-Connector::Connector(const std::string& hostname, int port, bool doServiceLookups, DomainNameResolver* resolver, ConnectionFactory* connectionFactory, TimerFactory* timerFactory) : hostname(hostname), port(port), doServiceLookups(doServiceLookups), resolver(resolver), connectionFactory(connectionFactory), timerFactory(timerFactory), timeoutMilliseconds(0), queriedAllServices(true), foundSomeDNS(false) {
+Connector::Connector(const std::string& hostname, int 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;
//std::cout << "Connector::start()" << std::endl;
assert(!currentConnection);
assert(!serviceQuery);
assert(!timer);
queriedAllServices = false;
- if (doServiceLookups) {
- serviceQuery = resolver->createServiceQuery("_xmpp-client._tcp." + hostname);
+ if (serviceLookupPrefix) {
+ serviceQuery = resolver->createServiceQuery((*serviceLookupPrefix) + hostname);
serviceQuery->onResult.connect(boost::bind(&Connector::handleServiceQueryResult, shared_from_this(), _1));
if (timeoutMilliseconds > 0) {
timer = timerFactory->createTimer(timeoutMilliseconds);
timer->onTick.connect(boost::bind(&Connector::handleTimeout, shared_from_this()));
}
serviceQuery->run();
}
else {
queryAddress(hostname);
}
}
void Connector::stop() {
finish(boost::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) {
diff --git a/Swiften/Network/Connector.h b/Swiften/Network/Connector.h
index 49ac271..16f8539 100644
--- a/Swiften/Network/Connector.h
+++ b/Swiften/Network/Connector.h
@@ -1,74 +1,74 @@
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#pragma once
#include <deque>
#include <Swiften/Base/boost_bsignals.h>
#include <boost/shared_ptr.hpp>
-
+#include <boost/optional.hpp>
#include <Swiften/Base/API.h>
#include <Swiften/Network/DomainNameServiceQuery.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/Network/Timer.h>
#include <Swiften/Network/HostAddressPort.h>
#include <string>
#include <Swiften/Network/DomainNameResolveError.h>
namespace Swift {
class DomainNameAddressQuery;
class DomainNameResolver;
class ConnectionFactory;
class TimerFactory;
class SWIFTEN_API Connector : public boost::bsignals::trackable, public boost::enable_shared_from_this<Connector> {
public:
typedef boost::shared_ptr<Connector> ref;
- static Connector::ref create(const std::string& hostname, int port, bool doServiceLookups, DomainNameResolver* resolver, ConnectionFactory* connectionFactory, TimerFactory* timerFactory) {
- return ref(new Connector(hostname, port, doServiceLookups, resolver, connectionFactory, timerFactory));
+ static Connector::ref create(const std::string& hostname, int port, const boost::optional<std::string>& serviceLookupPrefix, DomainNameResolver* resolver, ConnectionFactory* connectionFactory, TimerFactory* timerFactory) {
+ return ref(new Connector(hostname, port, serviceLookupPrefix, resolver, connectionFactory, timerFactory));
}
void setTimeoutMilliseconds(int milliseconds);
void start();
void stop();
boost::signal<void (boost::shared_ptr<Connection>, boost::shared_ptr<Error>)> onConnectFinished;
private:
- Connector(const std::string& hostname, int port, bool doServiceLookups, DomainNameResolver*, ConnectionFactory*, TimerFactory*);
+ Connector(const std::string& hostname, int port, const boost::optional<std::string>& serviceLookupPrefix, DomainNameResolver*, ConnectionFactory*, TimerFactory*);
void handleServiceQueryResult(const std::vector<DomainNameServiceQuery::Result>& result);
void handleAddressQueryResult(const std::vector<HostAddress>& address, boost::optional<DomainNameResolveError> error);
void queryAddress(const std::string& hostname);
void tryNextServiceOrFallback();
void tryNextAddress();
void tryConnect(const HostAddressPort& target);
void handleConnectionConnectFinished(bool error);
void finish(boost::shared_ptr<Connection>);
void handleTimeout();
private:
std::string hostname;
int port;
- bool doServiceLookups;
+ boost::optional<std::string> serviceLookupPrefix;
DomainNameResolver* resolver;
ConnectionFactory* connectionFactory;
TimerFactory* timerFactory;
int timeoutMilliseconds;
boost::shared_ptr<Timer> timer;
boost::shared_ptr<DomainNameServiceQuery> serviceQuery;
std::deque<DomainNameServiceQuery::Result> serviceQueryResults;
boost::shared_ptr<DomainNameAddressQuery> addressQuery;
std::deque<HostAddress> addressQueryResults;
bool queriedAllServices;
boost::shared_ptr<Connection> currentConnection;
bool foundSomeDNS;
};
}
diff --git a/Swiften/Network/ProxiedConnection.cpp b/Swiften/Network/ProxiedConnection.cpp
index 8bf12d3..0061820 100644
--- a/Swiften/Network/ProxiedConnection.cpp
+++ b/Swiften/Network/ProxiedConnection.cpp
@@ -21,71 +21,71 @@ ProxiedConnection::ProxiedConnection(
ConnectionFactory* connectionFactory,
TimerFactory* timerFactory,
const std::string& proxyHost,
int proxyPort) :
resolver_(resolver),
connectionFactory_(connectionFactory),
timerFactory_(timerFactory),
proxyHost_(proxyHost),
proxyPort_(proxyPort),
server_(HostAddressPort(HostAddress("0.0.0.0"), 0)) {
connected_ = false;
}
ProxiedConnection::~ProxiedConnection() {
cancelConnector();
if (connection_) {
connection_->onDataRead.disconnect(boost::bind(&ProxiedConnection::handleDataRead, shared_from_this(), _1));
connection_->onDisconnected.disconnect(boost::bind(&ProxiedConnection::handleDisconnected, shared_from_this(), _1));
}
if (connected_) {
std::cerr << "Warning: Connection was still established." << std::endl;
}
}
void ProxiedConnection::cancelConnector() {
if (connector_) {
connector_->onConnectFinished.disconnect(boost::bind(&ProxiedConnection::handleConnectFinished, shared_from_this(), _1));
connector_->stop();
connector_.reset();
}
}
void ProxiedConnection::connect(const HostAddressPort& server) {
server_ = server;
- connector_ = Connector::create(proxyHost_, proxyPort_, false, resolver_, connectionFactory_, timerFactory_);
+ connector_ = Connector::create(proxyHost_, proxyPort_, boost::optional<std::string>(), resolver_, connectionFactory_, timerFactory_);
connector_->onConnectFinished.connect(boost::bind(&ProxiedConnection::handleConnectFinished, shared_from_this(), _1));
connector_->start();
}
void ProxiedConnection::listen() {
assert(false);
connection_->listen();
}
void ProxiedConnection::disconnect() {
connected_ = false;
connection_->disconnect();
}
void ProxiedConnection::handleDisconnected(const boost::optional<Error>& error) {
onDisconnected(error);
}
void ProxiedConnection::write(const SafeByteArray& data) {
connection_->write(data);
}
void ProxiedConnection::handleConnectFinished(Connection::ref connection) {
cancelConnector();
if (connection) {
connection_ = connection;
connection_->onDataRead.connect(boost::bind(&ProxiedConnection::handleDataRead, shared_from_this(), _1));
connection_->onDisconnected.connect(boost::bind(&ProxiedConnection::handleDisconnected, shared_from_this(), _1));
initializeProxy();
}
else {
onConnectFinished(true);
}
}
diff --git a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
index 9c4bc27..0d06420 100644
--- a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
+++ b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp
@@ -160,71 +160,71 @@ class BOSHConnectionTest : public CppUnit::TestFixture {
"\r\n<body xmlns='http://jabber.org/protocol/httpbind'>"
"<bl"));
boost::shared_ptr<SafeByteArray> data3 = boost::make_shared<SafeByteArray>(createSafeByteArray(
"ah/>"
"</body>"));
connection->onDataRead(data1);
connection->onDataRead(data2);
CPPUNIT_ASSERT(dataRead.empty());
connection->onDataRead(data3);
CPPUNIT_ASSERT_EQUAL(std::string("<blah/>"), byteArrayToString(dataRead));
}
void testHTTPRequest() {
std::string data = "<blah/>";
std::string sid = "wigglebloom";
std::string fullBody = "<body xmlns='http://jabber.org/protocol/httpbind' sid='" + sid + "' rid='20'>" + data + "</body>";
std::pair<SafeByteArray, size_t> http = BOSHConnection::createHTTPRequest(createSafeByteArray(data), false, false, 20, sid, URL());
CPPUNIT_ASSERT_EQUAL(fullBody.size(), http.second);
}
void testHTTPRequest_Empty() {
std::string data = "";
std::string sid = "wigglebloomsickle";
std::string fullBody = "<body rid='42' sid='" + sid + "' xmlns='http://jabber.org/protocol/httpbind'>" + data + "</body>";
std::pair<SafeByteArray, size_t> http = BOSHConnection::createHTTPRequest(createSafeByteArray(data), false, false, 42, sid, URL());
CPPUNIT_ASSERT_EQUAL(fullBody.size(), http.second);
std::string response = safeByteArrayToString(http.first);
size_t bodyPosition = response.find("\r\n\r\n");
CPPUNIT_ASSERT_EQUAL(fullBody, response.substr(bodyPosition+4));
}
private:
BOSHConnection::ref createTestling() {
resolver->addAddress("wonderland.lit", HostAddress("127.0.0.1"));
- Connector::ref connector = Connector::create("wonderland.lit", 5280, false, resolver, connectionFactory, timerFactory);
+ Connector::ref connector = Connector::create("wonderland.lit", 5280, boost::optional<std::string>(), resolver, connectionFactory, timerFactory);
BOSHConnection::ref c = BOSHConnection::create(URL("http", "wonderland.lit", 5280, "/http-bind"), connector, &parserFactory);
c->onConnectFinished.connect(boost::bind(&BOSHConnectionTest::handleConnectFinished, this, _1));
c->onDisconnected.connect(boost::bind(&BOSHConnectionTest::handleDisconnected, this, _1));
c->onXMPPDataRead.connect(boost::bind(&BOSHConnectionTest::handleDataRead, this, _1));
c->onSessionStarted.connect(boost::bind(&BOSHConnectionTest::handleSID, this, _1));
c->setRID(42);
return c;
}
void handleConnectFinished(bool error) {
connectFinished = true;
connectFinishedWithError = error;
}
void handleDisconnected(bool e) {
disconnected = true;
disconnectedError = e;
}
void handleDataRead(const SafeByteArray& d) {
append(dataRead, d);
}
void handleSID(const std::string& s) {
sid = s;
}
struct MockConnection : public Connection {
public:
MockConnection(const std::vector<HostAddressPort>& failingPorts, EventLoop* eventLoop) : eventLoop(eventLoop), failingPorts(failingPorts), disconnected(false) {
}
void listen() { assert(false); }
void connect(const HostAddressPort& address) {
diff --git a/Swiften/Network/UnitTest/ChainedConnectorTest.cpp b/Swiften/Network/UnitTest/ChainedConnectorTest.cpp
index 9abed57..9176fe7 100644
--- a/Swiften/Network/UnitTest/ChainedConnectorTest.cpp
+++ b/Swiften/Network/UnitTest/ChainedConnectorTest.cpp
@@ -94,71 +94,71 @@ class ChainedConnectorTest : public CppUnit::TestFixture {
void testConnect_NoDNS() {
/* Reset resolver so there's no record */
delete resolver;
resolver = new StaticDomainNameResolver(eventLoop);
boost::shared_ptr<ChainedConnector> testling(createConnector());
connectionFactory1->connects = false;
connectionFactory2->connects = false;
testling->start();
//testling->stop();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(!connections[0]);
CPPUNIT_ASSERT(boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
void testStop() {
boost::shared_ptr<ChainedConnector> testling(createConnector());
connectionFactory1->connects = true;
connectionFactory2->connects = false;
testling->start();
testling->stop();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(!connections[0]);
}
private:
boost::shared_ptr<ChainedConnector> createConnector() {
std::vector<ConnectionFactory*> factories;
factories.push_back(connectionFactory1);
factories.push_back(connectionFactory2);
- boost::shared_ptr<ChainedConnector> connector = boost::make_shared<ChainedConnector>("foo.com", -1, true, resolver, factories, timerFactory);
+ boost::shared_ptr<ChainedConnector> connector = boost::make_shared<ChainedConnector>("foo.com", -1, boost::optional<std::string>("_xmpp-client._tcp."), resolver, factories, timerFactory);
connector->onConnectFinished.connect(boost::bind(&ChainedConnectorTest::handleConnectorFinished, this, _1, _2));
return connector;
}
void handleConnectorFinished(boost::shared_ptr<Connection> connection, boost::shared_ptr<Error> resultError) {
error = resultError;
boost::shared_ptr<MockConnection> c(boost::dynamic_pointer_cast<MockConnection>(connection));
if (connection) {
assert(c);
}
connections.push_back(c);
}
struct MockConnection : public Connection {
public:
MockConnection(bool connects, int id, EventLoop* eventLoop) : connects(connects), id(id), eventLoop(eventLoop) {
}
void listen() { assert(false); }
void connect(const HostAddressPort&) {
eventLoop->postEvent(boost::bind(boost::ref(onConnectFinished), !connects));
}
HostAddressPort getLocalAddress() const { return HostAddressPort(); }
void disconnect() { assert(false); }
void write(const SafeByteArray&) { assert(false); }
bool connects;
int id;
EventLoop* eventLoop;
};
struct MockConnectionFactory : public ConnectionFactory {
MockConnectionFactory(EventLoop* eventLoop, int id) : eventLoop(eventLoop), connects(true), id(id) {
}
diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp
index fe18340..3b1d4e4 100644
--- a/Swiften/Network/UnitTest/ConnectorTest.cpp
+++ b/Swiften/Network/UnitTest/ConnectorTest.cpp
@@ -1,37 +1,37 @@
/*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2014 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/optional.hpp>
#include <boost/bind.hpp>
#include <Swiften/Network/Connector.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Network/StaticDomainNameResolver.h>
#include <Swiften/Network/DummyTimerFactory.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Network/DomainNameAddressQuery.h>
using namespace Swift;
class ConnectorTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ConnectorTest);
CPPUNIT_TEST(testConnect);
CPPUNIT_TEST(testConnect_NoServiceLookups);
CPPUNIT_TEST(testConnect_NoServiceLookups_DefaultPort);
CPPUNIT_TEST(testConnect_FirstAddressHostFails);
CPPUNIT_TEST(testConnect_NoSRVHost);
CPPUNIT_TEST(testConnect_NoHosts);
CPPUNIT_TEST(testConnect_FirstSRVHostFails);
CPPUNIT_TEST(testConnect_AllSRVHostsFailWithoutFallbackHost);
CPPUNIT_TEST(testConnect_AllSRVHostsFailWithFallbackHost);
CPPUNIT_TEST(testConnect_SRVAndFallbackHostsFail);
//CPPUNIT_TEST(testConnect_TimeoutDuringResolve);
CPPUNIT_TEST(testConnect_TimeoutDuringConnectToOnlyCandidate);
@@ -43,87 +43,87 @@ class ConnectorTest : public CppUnit::TestFixture {
public:
void setUp() {
host1 = HostAddressPort(HostAddress("1.1.1.1"), 1234);
host2 = HostAddressPort(HostAddress("2.2.2.2"), 2345);
host3 = HostAddressPort(HostAddress("3.3.3.3"), 5222);
eventLoop = new DummyEventLoop();
resolver = new StaticDomainNameResolver(eventLoop);
connectionFactory = new MockConnectionFactory(eventLoop);
timerFactory = new DummyTimerFactory();
}
void tearDown() {
delete timerFactory;
delete connectionFactory;
delete resolver;
delete eventLoop;
}
void testConnect() {
Connector::ref testling(createConnector());
resolver->addXMPPClientService("foo.com", host1);
resolver->addXMPPClientService("foo.com", host2);
resolver->addAddress("foo.com", host3.getAddress());
testling->start();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(connections[0]);
CPPUNIT_ASSERT(host1 == *(connections[0]->hostAddressPort));
CPPUNIT_ASSERT(!boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
void testConnect_NoServiceLookups() {
- Connector::ref testling(createConnector(4321, false));
+ Connector::ref testling(createConnector(4321, boost::optional<std::string>()));
resolver->addXMPPClientService("foo.com", host1);
resolver->addXMPPClientService("foo.com", host2);
resolver->addAddress("foo.com", host3.getAddress());
testling->start();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(connections[0]);
CPPUNIT_ASSERT(host3.getAddress() == (*(connections[0]->hostAddressPort)).getAddress());
CPPUNIT_ASSERT(4321 == (*(connections[0]->hostAddressPort)).getPort());
CPPUNIT_ASSERT(!boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
void testConnect_NoServiceLookups_DefaultPort() {
- Connector::ref testling(createConnector(-1, false));
+ Connector::ref testling(createConnector(-1, boost::optional<std::string>()));
resolver->addXMPPClientService("foo.com", host1);
resolver->addXMPPClientService("foo.com", host2);
resolver->addAddress("foo.com", host3.getAddress());
testling->start();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(connections[0]);
CPPUNIT_ASSERT(host3.getAddress() == (*(connections[0]->hostAddressPort)).getAddress());
CPPUNIT_ASSERT_EQUAL(5222, (*(connections[0]->hostAddressPort)).getPort());
CPPUNIT_ASSERT(!boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
void testConnect_NoSRVHost() {
Connector::ref testling(createConnector());
resolver->addAddress("foo.com", host3.getAddress());
testling->start();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(connections[0]);
CPPUNIT_ASSERT(host3 == *(connections[0]->hostAddressPort));
CPPUNIT_ASSERT(!boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
void testConnect_FirstAddressHostFails() {
Connector::ref testling(createConnector());
HostAddress address1("1.1.1.1");
HostAddress address2("2.2.2.2");
resolver->addXMPPClientService("foo.com", "host-foo.com", 1234);
resolver->addAddress("host-foo.com", address1);
resolver->addAddress("host-foo.com", address2);
@@ -280,72 +280,72 @@ class ConnectorTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT(!boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
void testStop_DuringSRVQuery() {
Connector::ref testling(createConnector());
resolver->addXMPPClientService("foo.com", host1);
testling->start();
testling->stop();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(!connections[0]);
CPPUNIT_ASSERT(boost::dynamic_pointer_cast<DomainNameResolveError>(error));
}
void testStop_Timeout() {
Connector::ref testling(createConnector());
testling->setTimeoutMilliseconds(10);
resolver->addXMPPClientService("foo.com", host1);
testling->start();
testling->stop();
eventLoop->processEvents();
timerFactory->setTime(10);
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(!connections[0]);
}
private:
- Connector::ref createConnector(int port = -1, bool doServiceLookups = true) {
- Connector::ref connector = Connector::create("foo.com", port, doServiceLookups, resolver, connectionFactory, timerFactory);
+ Connector::ref createConnector(int port = -1, boost::optional<std::string> serviceLookupPrefix = boost::optional<std::string>("_xmpp-client._tcp.")) {
+ Connector::ref connector = Connector::create("foo.com", port, serviceLookupPrefix, resolver, connectionFactory, timerFactory);
connector->onConnectFinished.connect(boost::bind(&ConnectorTest::handleConnectorFinished, this, _1, _2));
return connector;
}
void handleConnectorFinished(boost::shared_ptr<Connection> connection, boost::shared_ptr<Error> resultError) {
boost::shared_ptr<MockConnection> c(boost::dynamic_pointer_cast<MockConnection>(connection));
if (connection) {
assert(c);
}
connections.push_back(c);
error = resultError;
}
struct MockConnection : public Connection {
public:
MockConnection(const std::vector<HostAddressPort>& failingPorts, bool isResponsive, EventLoop* eventLoop) : eventLoop(eventLoop), failingPorts(failingPorts), isResponsive(isResponsive) {}
void listen() { assert(false); }
void connect(const HostAddressPort& address) {
hostAddressPort = address;
if (isResponsive) {
bool fail = std::find(failingPorts.begin(), failingPorts.end(), address) != failingPorts.end();
eventLoop->postEvent(boost::bind(boost::ref(onConnectFinished), fail));
}
}
HostAddressPort getLocalAddress() const { return HostAddressPort(); }
void disconnect() { assert(false); }
void write(const SafeByteArray&) { assert(false); }
EventLoop* eventLoop;
boost::optional<HostAddressPort> hostAddressPort;
std::vector<HostAddressPort> failingPorts;
bool isResponsive;
};