diff options
author | Tobias Markmann <tm@ayena.de> | 2016-11-08 14:29:17 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2016-11-18 08:49:39 (GMT) |
commit | 43479ef719ea8fc6abbf654730b47c4583140508 (patch) | |
tree | c0a05a837b8988c0875fedb6161c08f3dcb2ffb0 /Swiften/FileTransfer | |
parent | c82f95fd431e702137d5f2e3dda4cf0ae424e837 (diff) | |
download | swift-43479ef719ea8fc6abbf654730b47c4583140508.zip swift-43479ef719ea8fc6abbf654730b47c4583140508.tar.bz2 |
Improve string to HostAddress conversion API
Previously HostAddress had a constructor which allowed
initialisation via a std::string. This initialisation can
fail and this is heavily used for checking whether a string
is a valid IP address.
This constructor is removed in this commit and replaced by
a static method HostAddress::fromString, taking a string and
returning an optional HostAddress. This clearly communicates
that the conversion can fail.
Test-Information:
./scons test=all passes on macOS 10.12.1.
Change-Id: Idaafee6f84010ce541c55f267ac77ad6ac8f02b4
Diffstat (limited to 'Swiften/FileTransfer')
4 files changed, 34 insertions, 33 deletions
diff --git a/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp b/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp index 2975193..09b664f 100644 --- a/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp +++ b/Swiften/FileTransfer/LocalJingleTransportCandidateGenerator.cpp @@ -18,7 +18,6 @@ #include <boost/bind.hpp> #include <Swiften/Base/Log.h> -#include <Swiften/Base/foreach.h> #include <Swiften/Elements/JingleS5BTransportPayload.h> #include <Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.h> #include <Swiften/FileTransfer/SOCKS5BytestreamServerManager.h> @@ -140,7 +139,7 @@ void LocalJingleTransportCandidateGenerator::emitOnLocalTransportCandidatesGener if (options_.isDirectAllowed()) { // get direct candidates std::vector<HostAddressPort> directCandidates = s5bServerManager->getHostAddressPorts(); - foreach(HostAddressPort addressPort, directCandidates) { + for(auto&& addressPort : directCandidates) { if (addressPort.getAddress().getRawAddress().is_v6() && addressPort.getAddress().getRawAddress().to_v6().is_link_local()) { continue; @@ -158,7 +157,7 @@ void LocalJingleTransportCandidateGenerator::emitOnLocalTransportCandidatesGener if (options_.isAssistedAllowed()) { // get assissted candidates std::vector<HostAddressPort> assisstedCandidates = s5bServerManager->getAssistedHostAddressPorts(); - foreach(HostAddressPort addressPort, assisstedCandidates) { + for (auto&& addressPort : assisstedCandidates) { JingleS5BTransportPayload::Candidate candidate; candidate.type = JingleS5BTransportPayload::Candidate::AssistedType; candidate.jid = ownJID; @@ -170,17 +169,18 @@ void LocalJingleTransportCandidateGenerator::emitOnLocalTransportCandidatesGener } if (options_.isProxiedAllowed() && s5bProxy->getOrDiscoverS5BProxies().is_initialized()) { - foreach(S5BProxyRequest::ref proxy, s5bProxy->getOrDiscoverS5BProxies().get()) { + for (auto&& proxy : s5bProxy->getOrDiscoverS5BProxies().get()) { if (proxy->getStreamHost()) { // FIXME: Added this test, because there were cases where this wasn't initialized. Investigate this. (Remko) JingleS5BTransportPayload::Candidate candidate; candidate.type = JingleS5BTransportPayload::Candidate::ProxyType; candidate.jid = (*proxy->getStreamHost()).jid; - HostAddress address = (*proxy->getStreamHost()).host; - assert(address.isValid()); - candidate.hostPort = HostAddressPort(address, (*proxy->getStreamHost()).port); - candidate.priority = 65536 * 10 + LOCAL_PREFERENCE; - candidate.cid = idGenerator->generateID(); - candidates.push_back(candidate); + auto address = HostAddress::fromString((*proxy->getStreamHost()).host); + if (address) { + candidate.hostPort = HostAddressPort(address.get(), (*proxy->getStreamHost()).port); + candidate.priority = 65536 * 10 + LOCAL_PREFERENCE; + candidate.cid = idGenerator->generateID(); + candidates.push_back(candidate); + } } } } diff --git a/Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.cpp b/Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.cpp index 367676b..cd4cfaa 100644 --- a/Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.cpp +++ b/Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.cpp @@ -18,7 +18,6 @@ #include <boost/bind.hpp> #include <Swiften/Base/Log.h> -#include <Swiften/Base/foreach.h> #include <Swiften/FileTransfer/SOCKS5BytestreamClientSession.h> #include <Swiften/Network/ConnectionFactory.h> #include <Swiften/Network/DomainNameAddressQuery.h> @@ -37,8 +36,8 @@ SOCKS5BytestreamProxiesManager::~SOCKS5BytestreamProxiesManager() { proxyFinder_->stop(); } - foreach (const ProxySessionsMap::value_type& sessionsForID, proxySessions_) { - foreach (const ProxyJIDClientSessionVector::value_type& session, sessionsForID.second) { + for (const auto& sessionsForID : proxySessions_) { + for (const auto& session : sessionsForID.second) { session.second->onSessionReady.disconnect(boost::bind(&SOCKS5BytestreamProxiesManager::handleProxySessionReady, this,sessionsForID.first, session.first, session.second, _1)); session.second->onFinished.disconnect(boost::bind(&SOCKS5BytestreamProxiesManager::handleProxySessionFinished, this, sessionsForID.first, session.first, session.second, _1)); } @@ -47,7 +46,7 @@ SOCKS5BytestreamProxiesManager::~SOCKS5BytestreamProxiesManager() { void SOCKS5BytestreamProxiesManager::addS5BProxy(S5BProxyRequest::ref proxy) { if (proxy) { - SWIFT_LOG_ASSERT(HostAddress(proxy->getStreamHost().get().host).isValid(), warning) << std::endl; + SWIFT_LOG_ASSERT(HostAddress::fromString(proxy->getStreamHost().get().host), warning) << std::endl; if (!localS5BProxies_) { localS5BProxies_ = std::vector<S5BProxyRequest::ref>(); } @@ -67,17 +66,19 @@ void SOCKS5BytestreamProxiesManager::connectToProxies(const std::string& session ProxyJIDClientSessionVector clientSessions; if (localS5BProxies_) { - foreach(S5BProxyRequest::ref proxy, localS5BProxies_.get()) { - std::shared_ptr<Connection> conn = connectionFactory_->createConnection(); - - HostAddressPort addressPort = HostAddressPort(proxy->getStreamHost().get().host, proxy->getStreamHost().get().port); - SWIFT_LOG_ASSERT(addressPort.isValid(), warning) << std::endl; - std::shared_ptr<SOCKS5BytestreamClientSession> session = std::make_shared<SOCKS5BytestreamClientSession>(conn, addressPort, sessionID, timerFactory_); - JID proxyJid = proxy->getStreamHost().get().jid; - clientSessions.push_back(std::pair<JID, std::shared_ptr<SOCKS5BytestreamClientSession> >(proxyJid, session)); - session->onSessionReady.connect(boost::bind(&SOCKS5BytestreamProxiesManager::handleProxySessionReady, this,sessionID, proxyJid, session, _1)); - session->onFinished.connect(boost::bind(&SOCKS5BytestreamProxiesManager::handleProxySessionFinished, this, sessionID, proxyJid, session, _1)); - session->start(); + for (auto&& proxy : localS5BProxies_.get()) { + auto proxyHostAddress = HostAddress::fromString(proxy->getStreamHost().get().host); + if (proxyHostAddress) { + std::shared_ptr<Connection> conn = connectionFactory_->createConnection(); + HostAddressPort addressPort = HostAddressPort(proxyHostAddress.get(), proxy->getStreamHost().get().port); + SWIFT_LOG_ASSERT(addressPort.isValid(), warning) << std::endl; + std::shared_ptr<SOCKS5BytestreamClientSession> session = std::make_shared<SOCKS5BytestreamClientSession>(conn, addressPort, sessionID, timerFactory_); + JID proxyJid = proxy->getStreamHost().get().jid; + clientSessions.push_back(std::pair<JID, std::shared_ptr<SOCKS5BytestreamClientSession> >(proxyJid, session)); + session->onSessionReady.connect(boost::bind(&SOCKS5BytestreamProxiesManager::handleProxySessionReady, this,sessionID, proxyJid, session, _1)); + session->onFinished.connect(boost::bind(&SOCKS5BytestreamProxiesManager::handleProxySessionFinished, this, sessionID, proxyJid, session, _1)); + session->start(); + } } } @@ -116,9 +117,10 @@ std::shared_ptr<SOCKS5BytestreamClientSession> SOCKS5BytestreamProxiesManager::c void SOCKS5BytestreamProxiesManager::handleProxiesFound(std::vector<S5BProxyRequest::ref> proxyHosts) { proxyFinder_->onProxiesFound.disconnect(boost::bind(&SOCKS5BytestreamProxiesManager::handleProxiesFound, this, _1)); - foreach(S5BProxyRequest::ref proxy, proxyHosts) { + for (auto&& proxy : proxyHosts) { if (proxy) { - if (HostAddress(proxy->getStreamHost().get().host).isValid()) { + auto proxyHostAddress = HostAddress::fromString(proxy->getStreamHost().get().host); + if (proxyHostAddress) { addS5BProxy(proxy); onDiscoveredProxiesChanged(); } @@ -146,7 +148,7 @@ void SOCKS5BytestreamProxiesManager::handleNameLookupResult(const std::vector<Ho } else { // generate proxy per returned address - foreach (const HostAddress& address, addresses) { + for (const auto& address : addresses) { S5BProxyRequest::StreamHost streamHost = proxy->getStreamHost().get(); S5BProxyRequest::ref proxyForAddress = std::make_shared<S5BProxyRequest>(*proxy); streamHost.host = address.toString(); diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp b/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp index 33a5283..f749735 100644 --- a/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp +++ b/Swiften/FileTransfer/SOCKS5BytestreamServerManager.cpp @@ -17,7 +17,6 @@ #include <boost/bind.hpp> #include <Swiften/Base/Log.h> -#include <Swiften/Base/foreach.h> #include <Swiften/FileTransfer/SOCKS5BytestreamServer.h> #include <Swiften/FileTransfer/SOCKS5BytestreamServerPortForwardingUser.h> #include <Swiften/FileTransfer/SOCKS5BytestreamServerResourceUser.h> @@ -88,8 +87,8 @@ std::vector<HostAddressPort> SOCKS5BytestreamServerManager::getHostAddressPorts( std::vector<HostAddressPort> result; if (connectionServer) { std::vector<NetworkInterface> networkInterfaces = networkEnvironment->getNetworkInterfaces(); - foreach (const NetworkInterface& networkInterface, networkInterfaces) { - foreach (const HostAddress& address, networkInterface.getAddresses()) { + for (const auto& networkInterface : networkInterfaces) { + for (const auto& address : networkInterface.getAddresses()) { result.push_back(HostAddressPort(address, connectionServerPort)); } } @@ -118,7 +117,7 @@ void SOCKS5BytestreamServerManager::initialize() { int port; for (port = LISTEN_PORTS_BEGIN; port < LISTEN_PORTS_END; ++port) { SWIFT_LOG(debug) << "Trying to start server on port " << port << std::endl; - connectionServer = connectionServerFactory->createConnectionServer(HostAddress("::"), port); + connectionServer = connectionServerFactory->createConnectionServer(HostAddress::fromString("::").get(), port); boost::optional<ConnectionServer::Error> error = connectionServer->tryStart(); if (!error) { break; diff --git a/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp b/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp index cb43d78..290dda5 100644 --- a/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp +++ b/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp @@ -53,7 +53,7 @@ class SOCKS5BytestreamClientSessionTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE_END(); public: - SOCKS5BytestreamClientSessionTest() : destinationAddressPort(HostAddressPort(HostAddress("127.0.0.1"), 8888)) {} + SOCKS5BytestreamClientSessionTest() : destinationAddressPort(HostAddressPort(HostAddress::fromString("127.0.0.1").get(), 8888)) {} void setUp() { crypto = std::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create()); |