diff options
Diffstat (limited to 'Swiften/Network')
-rw-r--r-- | Swiften/Network/BoostConnection.cpp | 7 | ||||
-rw-r--r-- | Swiften/Network/BoostConnection.h | 2 | ||||
-rw-r--r-- | Swiften/Network/BoostConnectionServer.cpp | 9 | ||||
-rw-r--r-- | Swiften/Network/BoostConnectionServer.h | 4 | ||||
-rw-r--r-- | Swiften/Network/Connection.h | 6 | ||||
-rw-r--r-- | Swiften/Network/ConnectionServer.h | 3 | ||||
-rw-r--r-- | Swiften/Network/DummyConnection.h | 45 | ||||
-rw-r--r-- | Swiften/Network/FakeConnection.h | 4 | ||||
-rw-r--r-- | Swiften/Network/HostAddress.cpp | 61 | ||||
-rw-r--r-- | Swiften/Network/HostAddress.h | 10 | ||||
-rw-r--r-- | Swiften/Network/HostAddressPort.h | 19 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/ConnectorTest.cpp | 1 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/HostAddressTest.cpp | 24 |
13 files changed, 125 insertions, 70 deletions
diff --git a/Swiften/Network/BoostConnection.cpp b/Swiften/Network/BoostConnection.cpp index ccaae8a..8cd19f2 100644 --- a/Swiften/Network/BoostConnection.cpp +++ b/Swiften/Network/BoostConnection.cpp @@ -102,7 +102,7 @@ void BoostConnection::handleSocketRead(const boost::system::error_code& error, s void BoostConnection::handleDataWritten(const boost::system::error_code& error) { if (!error) { - return; + MainEventLoop::postEvent(boost::ref(onDataWritten), shared_from_this()); } if (error == boost::asio::error::eof) { MainEventLoop::postEvent(boost::bind(boost::ref(onDisconnected), boost::optional<Error>()), shared_from_this()); @@ -112,4 +112,9 @@ void BoostConnection::handleDataWritten(const boost::system::error_code& error) } } +HostAddressPort BoostConnection::getLocalAddress() const { + return HostAddressPort(socket_.local_endpoint()); +} + + } diff --git a/Swiften/Network/BoostConnection.h b/Swiften/Network/BoostConnection.h index 8d3d444..4f5352f 100644 --- a/Swiften/Network/BoostConnection.h +++ b/Swiften/Network/BoostConnection.h @@ -39,6 +39,8 @@ namespace Swift { return socket_; } + HostAddressPort getLocalAddress() const; + private: BoostConnection(boost::asio::io_service* ioService); diff --git a/Swiften/Network/BoostConnectionServer.cpp b/Swiften/Network/BoostConnectionServer.cpp index febe6c9..03ae19c 100644 --- a/Swiften/Network/BoostConnectionServer.cpp +++ b/Swiften/Network/BoostConnectionServer.cpp @@ -71,4 +71,13 @@ void BoostConnectionServer::handleAccept(boost::shared_ptr<BoostConnection> newC } } +HostAddressPort BoostConnectionServer::getAddressPort() const { + if (acceptor_) { + return HostAddressPort(acceptor_->local_endpoint()); + } + else { + return HostAddressPort(); + } +} + } diff --git a/Swiften/Network/BoostConnectionServer.h b/Swiften/Network/BoostConnectionServer.h index 3a3c096..abcb3af 100644 --- a/Swiften/Network/BoostConnectionServer.h +++ b/Swiften/Network/BoostConnectionServer.h @@ -28,10 +28,12 @@ namespace Swift { static ref create(int port, boost::asio::io_service* ioService) { return ref(new BoostConnectionServer(port, ioService)); } - + void start(); void stop(); + virtual HostAddressPort getAddressPort() const; + boost::signal<void (boost::optional<Error>)> onStopped; private: diff --git a/Swiften/Network/Connection.h b/Swiften/Network/Connection.h index 34f3ade..8c30ad7 100644 --- a/Swiften/Network/Connection.h +++ b/Swiften/Network/Connection.h @@ -10,10 +10,9 @@ #include "Swiften/Base/ByteArray.h" #include "Swiften/Base/String.h" +#include "Swiften/Network/HostAddressPort.h" namespace Swift { - class HostAddressPort; - class Connection { public: enum Error { @@ -29,9 +28,12 @@ namespace Swift { virtual void disconnect() = 0; virtual void write(const ByteArray& data) = 0; + virtual HostAddressPort getLocalAddress() const = 0; + public: boost::signal<void (bool /* error */)> onConnectFinished; boost::signal<void (const boost::optional<Error>&)> onDisconnected; boost::signal<void (const ByteArray&)> onDataRead; + boost::signal<void ()> onDataWritten; }; } diff --git a/Swiften/Network/ConnectionServer.h b/Swiften/Network/ConnectionServer.h index 5597d1b..8129372 100644 --- a/Swiften/Network/ConnectionServer.h +++ b/Swiften/Network/ConnectionServer.h @@ -10,12 +10,15 @@ #include "Swiften/Base/boost_bsignals.h" #include "Swiften/Network/Connection.h" +#include "Swiften/Network/HostAddressPort.h" namespace Swift { class ConnectionServer { public: virtual ~ConnectionServer(); + virtual HostAddressPort getAddressPort() const = 0; + boost::signal<void (boost::shared_ptr<Connection>)> onNewConnection; }; } diff --git a/Swiften/Network/DummyConnection.h b/Swiften/Network/DummyConnection.h index d1657d5..576965f 100644 --- a/Swiften/Network/DummyConnection.h +++ b/Swiften/Network/DummyConnection.h @@ -15,32 +15,35 @@ #include "Swiften/EventLoop/EventOwner.h" namespace Swift { - class DummyConnection : - public Connection, - public EventOwner, - public boost::enable_shared_from_this<DummyConnection> { + class DummyConnection : public Connection, public EventOwner, public boost::enable_shared_from_this<DummyConnection> { + public: + void listen() { + assert(false); + } - void listen() { - assert(false); - } + void connect(const HostAddressPort&) { + assert(false); + } - void connect(const HostAddressPort&) { - assert(false); - } + void disconnect() { + //assert(false); + } - void disconnect() { - assert(false); - } + void write(const ByteArray& data) { + MainEventLoop::postEvent(boost::ref(onDataWritten), shared_from_this()); + onDataSent(data); + } - void write(const ByteArray& data) { - onDataWritten(data); - } + void receive(const ByteArray& data) { + MainEventLoop::postEvent(boost::bind(boost::ref(onDataRead), ByteArray(data)), shared_from_this()); + } - void receive(const ByteArray& data) { - MainEventLoop::postEvent(boost::bind( - boost::ref(onDataRead), ByteArray(data)), shared_from_this()); - } + HostAddressPort getLocalAddress() const { + return localAddress; + } - boost::signal<void (const ByteArray&)> onDataWritten; + boost::signal<void (const ByteArray&)> onDataSent; + + HostAddressPort localAddress; }; } diff --git a/Swiften/Network/FakeConnection.h b/Swiften/Network/FakeConnection.h index 60b8c94..a89466f 100644 --- a/Swiften/Network/FakeConnection.h +++ b/Swiften/Network/FakeConnection.h @@ -36,6 +36,10 @@ namespace Swift { assert(false); } + virtual HostAddressPort getLocalAddress() const { + return HostAddressPort(); + } + void setError(const Error& e) { error = boost::optional<Error>(e); state = DisconnectedWithError; diff --git a/Swiften/Network/HostAddress.cpp b/Swiften/Network/HostAddress.cpp index e299429..b3876c0 100644 --- a/Swiften/Network/HostAddress.cpp +++ b/Swiften/Network/HostAddress.cpp @@ -9,8 +9,8 @@ #include <boost/numeric/conversion/cast.hpp> #include <boost/lexical_cast.hpp> #include <cassert> -#include <sstream> -#include <iomanip> +#include <stdexcept> +#include <boost/array.hpp> #include "Swiften/Base/foreach.h" #include "Swiften/Base/String.h" @@ -18,50 +18,43 @@ namespace Swift { HostAddress::HostAddress() { - for (int i = 0; i < 4; ++i) { - address_.push_back(0); - } } HostAddress::HostAddress(const String& address) { - std::vector<String> components = address.split('.'); - assert(components.size() == 4); - foreach(const String& component, components) { - address_.push_back(boost::lexical_cast<int>(component.getUTF8String())); + try { + address_ = boost::asio::ip::address::from_string(address.getUTF8String()); + } + catch (const std::exception& t) { } } HostAddress::HostAddress(const unsigned char* address, int length) { assert(length == 4 || length == 16); - address_.reserve(length); - for (int i = 0; i < length; ++i) { - address_.push_back(address[i]); - } -} - -std::string HostAddress::toString() const { - if (address_.size() == 4) { - std::ostringstream result; - for (size_t i = 0; i < address_.size() - 1; ++i) { - result << boost::numeric_cast<unsigned int>(address_[i]) << "."; - } - result << boost::numeric_cast<unsigned int>(address_[address_.size() - 1]); - return result.str(); - } - else if (address_.size() == 16) { - std::ostringstream result; - result << std::hex; - result.fill('0'); - for (size_t i = 0; i < (address_.size() / 2) - 1; ++i) { - result << std::setw(2) << boost::numeric_cast<unsigned int>(address_[2*i]) << std::setw(2) << boost::numeric_cast<unsigned int>(address_[(2*i)+1]) << ":"; + if (length == 4) { + boost::array<unsigned char, 4> data; + for (int i = 0; i < length; ++i) { + data[i] = address[i]; } - result << std::setw(2) << boost::numeric_cast<unsigned int>(address_[address_.size() - 2]) << std::setw(2) << boost::numeric_cast<unsigned int>(address_[address_.size() - 1]); - return result.str(); + address_ = boost::asio::ip::address(boost::asio::ip::address_v4(data)); } else { - assert(false); - return ""; + boost::array<unsigned char, 16> data; + for (int i = 0; i < length; ++i) { + data[i] = address[i]; + } + address_ = boost::asio::ip::address(boost::asio::ip::address_v6(data)); } } +HostAddress::HostAddress(const boost::asio::ip::address& address) : address_(address) { +} + +std::string HostAddress::toString() const { + return address_.to_string(); +} + +bool HostAddress::isValid() const { + return !(address_.is_v4() && address_.to_v4().to_ulong() == 0); +} + } diff --git a/Swiften/Network/HostAddress.h b/Swiften/Network/HostAddress.h index cf2db07..6e2bde0 100644 --- a/Swiften/Network/HostAddress.h +++ b/Swiften/Network/HostAddress.h @@ -8,6 +8,7 @@ #include <string> #include <vector> +#include <boost/asio.hpp> namespace Swift { class String; @@ -17,10 +18,7 @@ namespace Swift { HostAddress(); HostAddress(const String&); HostAddress(const unsigned char* address, int length); - - const std::vector<unsigned char>& getRawAddress() const { - return address_; - } + HostAddress(const boost::asio::ip::address& address); std::string toString() const; @@ -28,7 +26,9 @@ namespace Swift { return address_ == o.address_; } + bool isValid() const; + private: - std::vector<unsigned char> address_; + boost::asio::ip::address address_; }; } diff --git a/Swiften/Network/HostAddressPort.h b/Swiften/Network/HostAddressPort.h index cf24a26..6883380 100644 --- a/Swiften/Network/HostAddressPort.h +++ b/Swiften/Network/HostAddressPort.h @@ -4,17 +4,24 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ -#ifndef SWIFTEN_HostAddressPort_H -#define SWIFTEN_HostAddressPort_H +#pragma once + +#include <boost/asio.hpp> #include "Swiften/Network/HostAddress.h" namespace Swift { class HostAddressPort { public: - HostAddressPort(const HostAddress& address, int port) : address_(address), port_(port) { + HostAddressPort(const HostAddress& address = HostAddress(), int port = -1) : address_(address), port_(port) { + } + + HostAddressPort(const boost::asio::ip::tcp::endpoint& endpoint) { + address_ = HostAddress(endpoint.address()); + port_ = endpoint.port(); } + const HostAddress& getAddress() const { return address_; } @@ -27,10 +34,12 @@ namespace Swift { return address_ == o.address_ && port_ == o.port_; } + bool isValid() const { + return address_.isValid() && port_ > 0; + } + private: HostAddress address_; int port_; }; } - -#endif diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp index 2e396b3..07e520c 100644 --- a/Swiften/Network/UnitTest/ConnectorTest.cpp +++ b/Swiften/Network/UnitTest/ConnectorTest.cpp @@ -268,6 +268,7 @@ class ConnectorTest : public CppUnit::TestFixture { } } + HostAddressPort getLocalAddress() const { return HostAddressPort(); } void disconnect() { assert(false); } void write(const ByteArray&) { assert(false); } diff --git a/Swiften/Network/UnitTest/HostAddressTest.cpp b/Swiften/Network/UnitTest/HostAddressTest.cpp index 1cc31ad..45793fa 100644 --- a/Swiften/Network/UnitTest/HostAddressTest.cpp +++ b/Swiften/Network/UnitTest/HostAddressTest.cpp @@ -15,8 +15,11 @@ using namespace Swift; class HostAddressTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(HostAddressTest); CPPUNIT_TEST(testConstructor); + CPPUNIT_TEST(testConstructor_Invalid); + CPPUNIT_TEST(testConstructor_InvalidString); CPPUNIT_TEST(testToString); CPPUNIT_TEST(testToString_IPv6); + CPPUNIT_TEST(testToString_Invalid); CPPUNIT_TEST_SUITE_END(); public: @@ -24,6 +27,19 @@ class HostAddressTest : public CppUnit::TestFixture { HostAddress testling("192.168.1.254"); CPPUNIT_ASSERT_EQUAL(std::string("192.168.1.254"), testling.toString()); + CPPUNIT_ASSERT(testling.isValid()); + } + + void testConstructor_Invalid() { + HostAddress testling; + + CPPUNIT_ASSERT(!testling.isValid()); + } + + void testConstructor_InvalidString() { + HostAddress testling("invalid"); + + CPPUNIT_ASSERT(!testling.isValid()); } void testToString() { @@ -37,7 +53,13 @@ class HostAddressTest : public CppUnit::TestFixture { unsigned char address[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17}; HostAddress testling(address, 16); - CPPUNIT_ASSERT_EQUAL(std::string("0102:0304:0506:0708:090a:0b0c:0d0e:0f11"), testling.toString()); + CPPUNIT_ASSERT_EQUAL(std::string("102:304:506:708:90a:b0c:d0e:f11"), testling.toString()); + } + + void testToString_Invalid() { + HostAddress testling; + + CPPUNIT_ASSERT_EQUAL(std::string("0.0.0.0"), testling.toString()); } }; |