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/Network | |
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/Network')
-rw-r--r-- | Swiften/Network/Connector.cpp | 5 | ||||
-rw-r--r-- | Swiften/Network/EnvironmentProxyProvider.cpp | 2 | ||||
-rw-r--r-- | Swiften/Network/HostAddress.cpp | 18 | ||||
-rw-r--r-- | Swiften/Network/HostAddress.h | 5 | ||||
-rw-r--r-- | Swiften/Network/MacOSXProxyProvider.cpp | 5 | ||||
-rw-r--r-- | Swiften/Network/MiniUPnPInterface.cpp | 2 | ||||
-rw-r--r-- | Swiften/Network/ProxiedConnection.cpp | 2 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp | 4 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/BOSHConnectionTest.cpp | 2 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/ChainedConnectorTest.cpp | 2 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/ConnectorTest.cpp | 16 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp | 30 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/HostAddressTest.cpp | 10 | ||||
-rw-r--r-- | Swiften/Network/WindowsProxyProvider.cpp | 18 | ||||
-rw-r--r-- | Swiften/Network/WindowsProxyProvider.h | 7 |
15 files changed, 72 insertions, 56 deletions
diff --git a/Swiften/Network/Connector.cpp b/Swiften/Network/Connector.cpp index 37cf35d..457d8a9 100644 --- a/Swiften/Network/Connector.cpp +++ b/Swiften/Network/Connector.cpp @@ -30,6 +30,7 @@ void Connector::start() { assert(!serviceQuery); assert(!timer); queriedAllServices = false; + auto hostAddress = HostAddress::fromString(hostname); if (timeoutMilliseconds > 0) { timer = timerFactory->createTimer(timeoutMilliseconds); timer->onTick.connect(boost::bind(&Connector::handleTimeout, shared_from_this())); @@ -39,10 +40,10 @@ void Connector::start() { serviceQuery->onResult.connect(boost::bind(&Connector::handleServiceQueryResult, shared_from_this(), _1)); serviceQuery->run(); } - else if (HostAddress(hostname).isValid()) { + else if (hostAddress) { // hostname is already a valid address; skip name lookup. foundSomeDNS = true; - addressQueryResults.push_back(HostAddress(hostname)); + addressQueryResults.push_back(hostAddress.get()); tryNextAddress(); } else { queryAddress(hostname); diff --git a/Swiften/Network/EnvironmentProxyProvider.cpp b/Swiften/Network/EnvironmentProxyProvider.cpp index faf2e5b..8edb136 100644 --- a/Swiften/Network/EnvironmentProxyProvider.cpp +++ b/Swiften/Network/EnvironmentProxyProvider.cpp @@ -50,7 +50,7 @@ HostAddressPort EnvironmentProxyProvider::getFromEnv(const char* envVarName, std address = address.substr(0, address.find(':')); } - return HostAddressPort(HostAddress(address), port); + return HostAddressPort(HostAddress::fromString(address).get_value_or(HostAddress()), port); } } diff --git a/Swiften/Network/HostAddress.cpp b/Swiften/Network/HostAddress.cpp index 63cd3f2..6eca80b 100644 --- a/Swiften/Network/HostAddress.cpp +++ b/Swiften/Network/HostAddress.cpp @@ -20,14 +20,6 @@ namespace Swift { HostAddress::HostAddress() { } -HostAddress::HostAddress(const std::string& address) { - boost::system::error_code errorCode; - address_ = boost::asio::ip::address::from_string(address, errorCode); - if (errorCode) { - SWIFT_LOG(warning) << "error: " << errorCode.message() << " (" << errorCode << ")" << ", " << "address: " << address << std::endl; - } -} - HostAddress::HostAddress(const unsigned char* address, size_t length) { assert(length == 4 || length == 16); if (length == 4) { @@ -69,4 +61,14 @@ bool HostAddress::isLocalhost() const { return address_ == localhost4 || address_ == localhost6; } +boost::optional<HostAddress> HostAddress::fromString(const std::string& addressString) { + boost::optional<HostAddress> hostAddress; + boost::system::error_code errorCode; + boost::asio::ip::address address = boost::asio::ip::address::from_string(addressString, errorCode); + if (!errorCode) { + hostAddress = HostAddress(address); + } + return hostAddress; +} + } diff --git a/Swiften/Network/HostAddress.h b/Swiften/Network/HostAddress.h index 00fe9bf..e4ddffb 100644 --- a/Swiften/Network/HostAddress.h +++ b/Swiften/Network/HostAddress.h @@ -3,11 +3,13 @@ * All rights reserved. * See the COPYING file for more information. */ + #pragma once #include <string> #include <boost/asio/ip/address.hpp> +#include <boost/optional.hpp> #include <Swiften/Base/API.h> @@ -15,7 +17,6 @@ namespace Swift { class SWIFTEN_API HostAddress { public: HostAddress(); - HostAddress(const std::string&); HostAddress(const unsigned char* address, size_t length); HostAddress(const boost::asio::ip::address& address); @@ -29,6 +30,8 @@ namespace Swift { bool isValid() const; bool isLocalhost() const; + static boost::optional<HostAddress> fromString(const std::string& addressString); + private: boost::asio::ip::address address_; }; diff --git a/Swiften/Network/MacOSXProxyProvider.cpp b/Swiften/Network/MacOSXProxyProvider.cpp index acea480..232fc60 100644 --- a/Swiften/Network/MacOSXProxyProvider.cpp +++ b/Swiften/Network/MacOSXProxyProvider.cpp @@ -16,9 +16,10 @@ #include <stdio.h> #include <stdlib.h> #include <iostream> -#include <boost/numeric/conversion/cast.hpp> #include <utility> +#include <boost/numeric/conversion/cast.hpp> + #ifndef SWIFTEN_PLATFORM_IPHONE #include <SystemConfiguration/SystemConfiguration.h> #endif @@ -67,7 +68,7 @@ static HostAddressPort getFromDictionary(CFDictionaryRef dict, CFStringRef enabl } if(host != "" && port != 0) { - ret = HostAddressPort(HostAddress(host), port); + ret = HostAddressPort(HostAddress::fromString(host).get(), port); } } } diff --git a/Swiften/Network/MiniUPnPInterface.cpp b/Swiften/Network/MiniUPnPInterface.cpp index d63b69e..dbe8bcd 100644 --- a/Swiften/Network/MiniUPnPInterface.cpp +++ b/Swiften/Network/MiniUPnPInterface.cpp @@ -69,7 +69,7 @@ boost::optional<HostAddress> MiniUPnPInterface::getPublicIP() { return boost::optional<HostAddress>(); } else { - return HostAddress(std::string(externalIPAddress)); + return HostAddress::fromString(std::string(externalIPAddress)); } } diff --git a/Swiften/Network/ProxiedConnection.cpp b/Swiften/Network/ProxiedConnection.cpp index 16bab0d..aa6c4d2 100644 --- a/Swiften/Network/ProxiedConnection.cpp +++ b/Swiften/Network/ProxiedConnection.cpp @@ -26,7 +26,7 @@ ProxiedConnection::ProxiedConnection( timerFactory_(timerFactory), proxyHost_(proxyHost), proxyPort_(proxyPort), - server_(HostAddressPort(HostAddress("0.0.0.0"), 0)) { + server_(HostAddressPort(HostAddress::fromString("0.0.0.0").get(), 0)) { connected_ = false; } diff --git a/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp b/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp index 8dbd09e..5d6fedd 100644 --- a/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp +++ b/Swiften/Network/UnitTest/BOSHConnectionPoolTest.cpp @@ -69,7 +69,7 @@ class BOSHConnectionPoolTest : public CppUnit::TestFixture { boshDataRead.clear(); boshDataWritten.clear(); resolver = new StaticDomainNameResolver(eventLoop); - resolver->addAddress(to, HostAddress("127.0.0.1")); + resolver->addAddress(to, HostAddress::fromString("127.0.0.1").get()); timerFactory = new DummyTimerFactory(); } @@ -234,7 +234,7 @@ class BOSHConnectionPoolTest : public CppUnit::TestFixture { void testSession() { to = "prosody.doomsong.co.uk"; - resolver->addAddress("prosody.doomsong.co.uk", HostAddress("127.0.0.1")); + resolver->addAddress("prosody.doomsong.co.uk", HostAddress::fromString("127.0.0.1").get()); path = "/http-bind/"; boshURL = URL("http", to, 5280, path); diff --git a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp index a791e96..99dd462 100644 --- a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp +++ b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp @@ -193,7 +193,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { private: BOSHConnection::ref createTestling() { - resolver->addAddress("wonderland.lit", HostAddress("127.0.0.1")); + resolver->addAddress("wonderland.lit", HostAddress::fromString("127.0.0.1").get()); 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, tlsContextFactory, TLSOptions()); c->onConnectFinished.connect(boost::bind(&BOSHConnectionTest::handleConnectFinished, this, _1)); diff --git a/Swiften/Network/UnitTest/ChainedConnectorTest.cpp b/Swiften/Network/UnitTest/ChainedConnectorTest.cpp index 3fad433..2d78cd7 100644 --- a/Swiften/Network/UnitTest/ChainedConnectorTest.cpp +++ b/Swiften/Network/UnitTest/ChainedConnectorTest.cpp @@ -34,7 +34,7 @@ class ChainedConnectorTest : public CppUnit::TestFixture { public: void setUp() { error.reset(); - host = HostAddressPort(HostAddress("1.1.1.1"), 1234); + host = HostAddressPort(HostAddress::fromString("1.1.1.1").get(), 1234); eventLoop = new DummyEventLoop(); resolver = new StaticDomainNameResolver(eventLoop); resolver->addXMPPClientService("foo.com", host); diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp index 20ad68d..8524439 100644 --- a/Swiften/Network/UnitTest/ConnectorTest.cpp +++ b/Swiften/Network/UnitTest/ConnectorTest.cpp @@ -43,9 +43,9 @@ 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); + host1 = HostAddressPort(HostAddress::fromString("1.1.1.1").get(), 1234); + host2 = HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345); + host3 = HostAddressPort(HostAddress::fromString("3.3.3.3").get(), 5222); eventLoop = new DummyEventLoop(); resolver = new StaticDomainNameResolver(eventLoop); connectionFactory = new MockConnectionFactory(eventLoop); @@ -122,8 +122,8 @@ class ConnectorTest : public CppUnit::TestFixture { void testConnect_FirstAddressHostFails() { Connector::ref testling(createConnector()); - HostAddress address1("1.1.1.1"); - HostAddress address2("2.2.2.2"); + auto address1 = HostAddress::fromString("1.1.1.1").get(); + auto address2 = HostAddress::fromString("2.2.2.2").get(); resolver->addXMPPClientService("foo.com", "host-foo.com", 1234); resolver->addAddress("host-foo.com", address1); resolver->addAddress("host-foo.com", address2); @@ -245,10 +245,10 @@ class ConnectorTest : public CppUnit::TestFixture { Connector::ref testling(createConnector()); testling->setTimeoutMilliseconds(10); + auto address2 = HostAddress::fromString("2.2.2.2").get(); + resolver->addXMPPClientService("foo.com", "host-foo.com", 1234); - HostAddress address1("1.1.1.1"); - resolver->addAddress("host-foo.com", address1); - HostAddress address2("2.2.2.2"); + resolver->addAddress("host-foo.com", HostAddress::fromString("1.1.1.1").get()); resolver->addAddress("host-foo.com", address2); connectionFactory->isResponsive = false; diff --git a/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp b/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp index 232847b..1a160b7 100644 --- a/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp +++ b/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp @@ -18,7 +18,6 @@ #include <Swiften/Base/Algorithm.h> #include <Swiften/Base/Log.h> -#include <Swiften/Base/foreach.h> #include <Swiften/EventLoop/DummyEventLoop.h> #include <Swiften/Network/Connection.h> #include <Swiften/Network/ConnectionFactory.h> @@ -65,8 +64,7 @@ namespace { int statusCode = boost::lexical_cast<int>(statusLineFields[1]); if (statusCode == 407) { - typedef std::pair<std::string, std::string> StrPair; - foreach (const StrPair& field, response) { + for (const auto& field : response) { if (to_lower(field.first) == to_lower("Proxy-Authenticate")) { if (field.second.size() >= 6 && field.second.substr(0, 6) == " NTLM ") { filterResponseReturn.push_back(std::pair<std::string, std::string>("Proxy-Authorization", "NTLM TlRMTVNTUAADAAAAGAAYAHIAAAAYABgAigAAABIAEgBIAAAABgAGAFoAAAASABIVNTUAADAAYAAAABAAEACiAAAANYKI4gUBKAoAAAAPTABBAEIAUwBNAE8ASwBFADMAXwBxAGEATABBAEIAUwBNAE8ASwBFADMA0NKq8HYYhj8AAAAAAAAAAAAAAAAAAAAAOIiih3mR+AkyM4r99sy1mdFonCu2ILODro1WTTrJ4b4JcXEzUBA2Ig==")); @@ -108,8 +106,8 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void setUp() { proxyHost = "doo.bah"; proxyPort = 1234; - proxyHostAddress = HostAddressPort(HostAddress("1.1.1.1"), proxyPort); - host = HostAddressPort(HostAddress("2.2.2.2"), 2345); + proxyHostAddress = HostAddressPort(HostAddress::fromString("1.1.1.1").get(), proxyPort); + host = HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345); eventLoop = new DummyEventLoop(); resolver = new StaticDomainNameResolver(eventLoop); resolver->addAddress(proxyHost, proxyHostAddress.getAddress()); @@ -148,14 +146,14 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void testConnect_SendsConnectRequest() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); CPPUNIT_ASSERT_EQUAL(createByteArray("CONNECT 2.2.2.2:2345 HTTP/1.1\r\n\r\n"), connectionFactory->connections[0]->dataWritten); } void testConnect_ReceiveConnectResponse() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n")); eventLoop->processEvents(); @@ -167,7 +165,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void testConnect_ReceiveConnectChunkedResponse() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 ")); eventLoop->processEvents(); @@ -182,7 +180,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void testConnect_ReceiveMalformedConnectResponse() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("FLOOP")); eventLoop->processEvents(); @@ -194,7 +192,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void testConnect_ReceiveErrorConnectResponse() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 401 Unauthorized\r\n\r\n")); eventLoop->processEvents(); @@ -206,7 +204,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void testConnect_ReceiveDataAfterConnect() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n")); eventLoop->processEvents(); @@ -217,7 +215,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void testWrite_AfterConnect() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n")); eventLoop->processEvents(); connectionFactory->connections[0]->dataWritten.clear(); @@ -229,7 +227,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void testDisconnect_AfterConnectRequest() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); testling->disconnect(); @@ -240,7 +238,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { void testDisconnect_AfterConnect() { HTTPConnectProxiedConnection::ref testling(createTestling()); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n")); eventLoop->processEvents(); @@ -257,7 +255,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { std::shared_ptr<ExampleHTTPTrafficFilter> httpTrafficFilter = std::make_shared<ExampleHTTPTrafficFilter>(); testling->setHTTPTrafficFilter(httpTrafficFilter); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); // set a default response so the server response is answered by the traffic filter httpTrafficFilter->filterResponseReturn.clear(); @@ -298,7 +296,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { std::shared_ptr<ProxyAuthenticationHTTPTrafficFilter> httpTrafficFilter = std::make_shared<ProxyAuthenticationHTTPTrafficFilter>(); testling->setHTTPTrafficFilter(httpTrafficFilter); - connect(testling, HostAddressPort(HostAddress("2.2.2.2"), 2345)); + connect(testling, HostAddressPort(HostAddress::fromString("2.2.2.2").get(), 2345)); // First HTTP CONNECT request assumes the proxy will work. CPPUNIT_ASSERT_EQUAL(createByteArray("CONNECT 2.2.2.2:2345 HTTP/1.1\r\n" diff --git a/Swiften/Network/UnitTest/HostAddressTest.cpp b/Swiften/Network/UnitTest/HostAddressTest.cpp index aceb9be..226346b 100644 --- a/Swiften/Network/UnitTest/HostAddressTest.cpp +++ b/Swiften/Network/UnitTest/HostAddressTest.cpp @@ -25,10 +25,10 @@ class HostAddressTest : public CppUnit::TestFixture { public: void testConstructor() { - HostAddress testling("192.168.1.254"); + auto testling = HostAddress::fromString("192.168.1.254"); - CPPUNIT_ASSERT_EQUAL(std::string("192.168.1.254"), testling.toString()); - CPPUNIT_ASSERT(testling.isValid()); + CPPUNIT_ASSERT_EQUAL(std::string("192.168.1.254"), testling->toString()); + CPPUNIT_ASSERT(testling->isValid()); } void testConstructor_Invalid() { @@ -38,9 +38,9 @@ class HostAddressTest : public CppUnit::TestFixture { } void testConstructor_InvalidString() { - HostAddress testling("invalid"); + auto testling = HostAddress::fromString("invalid"); - CPPUNIT_ASSERT(!testling.isValid()); + CPPUNIT_ASSERT(!testling); } void testToString() { diff --git a/Swiften/Network/WindowsProxyProvider.cpp b/Swiften/Network/WindowsProxyProvider.cpp index 75e087a..78bd72f 100644 --- a/Swiften/Network/WindowsProxyProvider.cpp +++ b/Swiften/Network/WindowsProxyProvider.cpp @@ -22,7 +22,8 @@ #include <Swiften/Base/ByteArray.h> #include <Swiften/Base/Log.h> -#include <Swiften/Base/foreach.h> +#include <Swiften/Network/HostAddress.h> +#include <Swiften/Network/HostAddressPort.h> namespace Swift { @@ -47,7 +48,7 @@ WindowsProxyProvider::WindowsProxyProvider() if(result == ERROR_SUCCESS) { std::vector<std::string> proxies = String::split(byteArrayToString(dataBuffer), ';'); std::pair<std::string, std::string> protocolAndProxy; - foreach(std::string proxy, proxies) { + for(auto&& proxy : proxies) { if(proxy.find('=') != std::string::npos) { protocolAndProxy = String::getSplittedAtFirst(proxy, '='); SWIFT_LOG(debug) << "Found proxy: " << protocolAndProxy.first << " => " << protocolAndProxy.second << std::endl; @@ -81,10 +82,10 @@ HostAddressPort WindowsProxyProvider::getAsHostAddressPort(std::string proxy) { // .c_str() is needed as tmp.second can include a \0 char which will end in an exception of the lexical cast. // with .c_str() the \0 will not be part of the string which is to be casted port = boost::lexical_cast<int> (tmp.second.c_str()); - ret = HostAddressPort(HostAddress(tmp.first), port); + ret = HostAddressPort(HostAddress::fromString(tmp.first).get(), port); } catch(...) { - SWIFT_LOG(error) << "Exception occured while parsing windows proxy \"getHostAddressPort\"." << std::endl; + SWIFT_LOG(error) << "Exception occured while parsing windows proxy \"getHostAddressPort\"." << std::endl; } return ret; @@ -99,17 +100,20 @@ bool WindowsProxyProvider::proxyEnabled(HKEY hKey) const { DWORD data = 0; ByteArray dataBuffer; - if(hKey == INVALID_HANDLE_VALUE) + if(hKey == INVALID_HANDLE_VALUE) { return ret; + } result = RegQueryValueEx(hKey, "ProxyEnable", NULL, &dataType, NULL, &dataSize); - if(result != ERROR_SUCCESS) + if(result != ERROR_SUCCESS) { return ret; + } dataBuffer.resize(dataSize); result = RegQueryValueEx(hKey, "ProxyEnable", NULL, &dataType, reinterpret_cast<BYTE*>(vecptr(dataBuffer)), &dataSize); - if(result != ERROR_SUCCESS) + if(result != ERROR_SUCCESS) { return ret; + } for(size_t t = 0; t < dataBuffer.size(); t++) { data += static_cast<int> (dataBuffer[t]) * pow(256, static_cast<double>(t)); diff --git a/Swiften/Network/WindowsProxyProvider.h b/Swiften/Network/WindowsProxyProvider.h index ded5049..0ca897d 100644 --- a/Swiften/Network/WindowsProxyProvider.h +++ b/Swiften/Network/WindowsProxyProvider.h @@ -4,9 +4,16 @@ * See Documentation/Licenses/BSD-simplified.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #pragma once #include <Swiften/Base/API.h> +#include <Swiften/Network/HostAddressPort.h> #include <Swiften/Network/ProxyProvider.h> namespace Swift { |