diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-05-18 16:22:06 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-05-18 16:50:18 (GMT) |
commit | 8472194d716e0e3b95a66bf5be45c246ee79d0af (patch) | |
tree | b9f473e0e606b012ee5013e85d2cf5e7523b7029 /Swiften/Network | |
parent | b1b3b8f88517ad8b14795cc8a6265962d2935be3 (diff) | |
download | swift-contrib-8472194d716e0e3b95a66bf5be45c246ee79d0af.zip swift-contrib-8472194d716e0e3b95a66bf5be45c246ee79d0af.tar.bz2 |
Propagate use of SafeByteArray down to the connection.
Diffstat (limited to 'Swiften/Network')
-rw-r--r-- | Swiften/Network/BoostConnection.cpp | 15 | ||||
-rw-r--r-- | Swiften/Network/BoostConnection.h | 9 | ||||
-rw-r--r-- | Swiften/Network/Connection.h | 6 | ||||
-rw-r--r-- | Swiften/Network/DummyConnection.cpp | 4 | ||||
-rw-r--r-- | Swiften/Network/DummyConnection.h | 6 | ||||
-rw-r--r-- | Swiften/Network/FakeConnection.h | 4 | ||||
-rw-r--r-- | Swiften/Network/HTTPConnectProxiedConnection.cpp | 12 | ||||
-rw-r--r-- | Swiften/Network/HTTPConnectProxiedConnection.h | 4 | ||||
-rw-r--r-- | Swiften/Network/SOCKS5ProxiedConnection.cpp | 8 | ||||
-rw-r--r-- | Swiften/Network/SOCKS5ProxiedConnection.h | 4 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/ChainedConnectorTest.cpp | 2 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/ConnectorTest.cpp | 2 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp | 20 |
13 files changed, 50 insertions, 46 deletions
diff --git a/Swiften/Network/BoostConnection.cpp b/Swiften/Network/BoostConnection.cpp index b56ad38..ac3b444 100644 --- a/Swiften/Network/BoostConnection.cpp +++ b/Swiften/Network/BoostConnection.cpp @@ -8,6 +8,7 @@ #include <iostream> #include <string> +#include <algorithm> #include <boost/bind.hpp> #include <boost/thread.hpp> #include <boost/asio/placeholders.hpp> @@ -19,6 +20,7 @@ #include <Swiften/Base/ByteArray.h> #include <Swiften/Network/HostAddressPort.h> #include <Swiften/Base/sleep.h> +#include <Swiften/Base/SafeAllocator.h> namespace Swift { @@ -29,8 +31,8 @@ static const size_t BUFFER_SIZE = 4096; // A reference-counted non-modifiable buffer class. class SharedBuffer { public: - SharedBuffer(const ByteArray& data) : - data_(new std::vector<char>(data.begin(), data.end())), + SharedBuffer(const SafeByteArray& data) : + data_(new std::vector<char, SafeAllocator<char> >(data.begin(), data.end())), buffer_(boost::asio::buffer(*data_)) { } @@ -41,7 +43,7 @@ class SharedBuffer { const boost::asio::const_buffer* end() const { return &buffer_ + 1; } private: - boost::shared_ptr< std::vector<char> > data_; + boost::shared_ptr< std::vector<char, SafeAllocator<char> > > data_; boost::asio::const_buffer buffer_; }; @@ -78,7 +80,7 @@ void BoostConnection::disconnect() { socket_.close(); } -void BoostConnection::write(const ByteArray& data) { +void BoostConnection::write(const SafeByteArray& data) { boost::lock_guard<boost::mutex> lock(writeMutex_); if (!writing_) { writing_ = true; @@ -89,7 +91,7 @@ void BoostConnection::write(const ByteArray& data) { } } -void BoostConnection::doWrite(const ByteArray& data) { +void BoostConnection::doWrite(const SafeByteArray& data) { boost::asio::async_write(socket_, SharedBuffer(data), boost::bind(&BoostConnection::handleDataWritten, shared_from_this(), boost::asio::placeholders::error)); } @@ -114,7 +116,8 @@ void BoostConnection::doRead() { void BoostConnection::handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred) { SWIFT_LOG(debug) << "Socket read " << error << std::endl; if (!error) { - eventLoop->postEvent(boost::bind(boost::ref(onDataRead), createByteArray(&readBuffer_[0], bytesTransferred)), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onDataRead), createSafeByteArray(&readBuffer_[0], bytesTransferred)), shared_from_this()); + std::fill(readBuffer_.begin(), readBuffer_.end(), 0); doRead(); } else if (/*error == boost::asio::error::eof ||*/ error == boost::asio::error::operation_aborted) { diff --git a/Swiften/Network/BoostConnection.h b/Swiften/Network/BoostConnection.h index 0e7624d..259fcec 100644 --- a/Swiften/Network/BoostConnection.h +++ b/Swiften/Network/BoostConnection.h @@ -13,6 +13,7 @@ #include <Swiften/Network/Connection.h> #include <Swiften/EventLoop/EventOwner.h> +#include <Swiften/Base/SafeByteArray.h> namespace boost { class thread; @@ -37,7 +38,7 @@ namespace Swift { virtual void listen(); virtual void connect(const HostAddressPort& address); virtual void disconnect(); - virtual void write(const ByteArray& data); + virtual void write(const SafeByteArray& data); boost::asio::ip::tcp::socket& getSocket() { return socket_; @@ -52,15 +53,15 @@ namespace Swift { void handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred); void handleDataWritten(const boost::system::error_code& error); void doRead(); - void doWrite(const ByteArray& data); + void doWrite(const SafeByteArray& data); private: EventLoop* eventLoop; boost::shared_ptr<boost::asio::io_service> ioService; boost::asio::ip::tcp::socket socket_; - std::vector<char> readBuffer_; + SafeByteArray readBuffer_; boost::mutex writeMutex_; bool writing_; - ByteArray writeQueue_; + SafeByteArray writeQueue_; }; } diff --git a/Swiften/Network/Connection.h b/Swiften/Network/Connection.h index 74d25aa..1b9977f 100644 --- a/Swiften/Network/Connection.h +++ b/Swiften/Network/Connection.h @@ -9,7 +9,7 @@ #include <boost/shared_ptr.hpp> #include <Swiften/Base/boost_bsignals.h> -#include <Swiften/Base/ByteArray.h> +#include <Swiften/Base/SafeByteArray.h> namespace Swift { class HostAddressPort; @@ -29,14 +29,14 @@ namespace Swift { virtual void listen() = 0; virtual void connect(const HostAddressPort& address) = 0; virtual void disconnect() = 0; - virtual void write(const ByteArray& data) = 0; + virtual void write(const SafeByteArray& 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 (const SafeByteArray&)> onDataRead; boost::signal<void ()> onDataWritten; }; } diff --git a/Swiften/Network/DummyConnection.cpp b/Swiften/Network/DummyConnection.cpp index ffc6dc2..9a9d138 100644 --- a/Swiften/Network/DummyConnection.cpp +++ b/Swiften/Network/DummyConnection.cpp @@ -14,8 +14,8 @@ namespace Swift { DummyConnection::DummyConnection(EventLoop* eventLoop) : eventLoop(eventLoop) { } -void DummyConnection::receive(const ByteArray& data) { - eventLoop->postEvent(boost::bind(boost::ref(onDataRead), ByteArray(data)), shared_from_this()); +void DummyConnection::receive(const SafeByteArray& data) { + eventLoop->postEvent(boost::bind(boost::ref(onDataRead), SafeByteArray(data)), shared_from_this()); } void DummyConnection::listen() { diff --git a/Swiften/Network/DummyConnection.h b/Swiften/Network/DummyConnection.h index 3024302..5191e30 100644 --- a/Swiften/Network/DummyConnection.h +++ b/Swiften/Network/DummyConnection.h @@ -25,18 +25,18 @@ namespace Swift { //assert(false); } - void write(const ByteArray& data) { + void write(const SafeByteArray& data) { eventLoop->postEvent(boost::ref(onDataWritten), shared_from_this()); onDataSent(data); } - void receive(const ByteArray& data); + void receive(const SafeByteArray& data); HostAddressPort getLocalAddress() const { return localAddress; } - boost::signal<void (const ByteArray&)> onDataSent; + boost::signal<void (const SafeByteArray&)> onDataSent; EventLoop* eventLoop; HostAddressPort localAddress; diff --git a/Swiften/Network/FakeConnection.h b/Swiften/Network/FakeConnection.h index 65f6650..99cb584 100644 --- a/Swiften/Network/FakeConnection.h +++ b/Swiften/Network/FakeConnection.h @@ -41,7 +41,7 @@ namespace Swift { virtual void connect(const HostAddressPort& address); virtual void disconnect(); - virtual void write(const ByteArray& data) { + virtual void write(const SafeByteArray& data) { dataWritten.push_back(data); } @@ -51,7 +51,7 @@ namespace Swift { EventLoop* eventLoop; boost::optional<HostAddressPort> connectedTo; - std::vector<ByteArray> dataWritten; + std::vector<SafeByteArray> dataWritten; boost::optional<Error> error; State state; bool delayConnect; diff --git a/Swiften/Network/HTTPConnectProxiedConnection.cpp b/Swiften/Network/HTTPConnectProxiedConnection.cpp index 61fe597..20819ff 100644 --- a/Swiften/Network/HTTPConnectProxiedConnection.cpp +++ b/Swiften/Network/HTTPConnectProxiedConnection.cpp @@ -57,7 +57,7 @@ void HTTPConnectProxiedConnection::handleDisconnected(const boost::optional<Erro onDisconnected(error); } -void HTTPConnectProxiedConnection::write(const ByteArray& data) { +void HTTPConnectProxiedConnection::write(const SafeByteArray& data) { connection_->write(data); } @@ -66,17 +66,17 @@ void HTTPConnectProxiedConnection::handleConnectionConnectFinished(bool error) { if (!error) { std::stringstream connect; connect << "CONNECT " << server_.getAddress().toString() << ":" << server_.getPort() << " HTTP/1.1\r\n\r\n"; - connection_->write(createByteArray(connect.str())); + connection_->write(createSafeByteArray(connect.str())); } else { onConnectFinished(true); } } -void HTTPConnectProxiedConnection::handleDataRead(const ByteArray& data) { +void HTTPConnectProxiedConnection::handleDataRead(const SafeByteArray& data) { if (!connected_) { - SWIFT_LOG(debug) << byteArrayToString(data) << std::endl; - std::vector<std::string> tmp = String::split(byteArrayToString(data), ' '); + SWIFT_LOG(debug) << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl; + std::vector<std::string> tmp = String::split(byteArrayToString(ByteArray(data.begin(), data.end())), ' '); if(tmp.size() > 1) { int status = boost::lexical_cast<int> (tmp[1].c_str()); SWIFT_LOG(debug) << "Proxy Status: " << status << std::endl; @@ -85,7 +85,7 @@ void HTTPConnectProxiedConnection::handleDataRead(const ByteArray& data) { onConnectFinished(false); return; } - SWIFT_LOG(debug) << "HTTP Proxy returned an error: " << byteArrayToString(data) << std::endl; + SWIFT_LOG(debug) << "HTTP Proxy returned an error: " << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl; } disconnect(); onConnectFinished(true); diff --git a/Swiften/Network/HTTPConnectProxiedConnection.h b/Swiften/Network/HTTPConnectProxiedConnection.h index 4719267..96c6be8 100644 --- a/Swiften/Network/HTTPConnectProxiedConnection.h +++ b/Swiften/Network/HTTPConnectProxiedConnection.h @@ -34,14 +34,14 @@ namespace Swift { virtual void listen(); virtual void connect(const HostAddressPort& address); virtual void disconnect(); - virtual void write(const ByteArray& data); + virtual void write(const SafeByteArray& data); virtual HostAddressPort getLocalAddress() const; private: HTTPConnectProxiedConnection(ConnectionFactory* connectionFactory, HostAddressPort proxy); void handleConnectionConnectFinished(bool error); - void handleDataRead(const ByteArray& data); + void handleDataRead(const SafeByteArray& data); void handleDisconnected(const boost::optional<Error>& error); private: diff --git a/Swiften/Network/SOCKS5ProxiedConnection.cpp b/Swiften/Network/SOCKS5ProxiedConnection.cpp index cf531e5..f8084ab 100644 --- a/Swiften/Network/SOCKS5ProxiedConnection.cpp +++ b/Swiften/Network/SOCKS5ProxiedConnection.cpp @@ -60,7 +60,7 @@ void SOCKS5ProxiedConnection::handleDisconnected(const boost::optional<Error>& e onDisconnected(error); } -void SOCKS5ProxiedConnection::write(const ByteArray& data) { +void SOCKS5ProxiedConnection::write(const SafeByteArray& data) { if (connection_) { connection_->write(data); } @@ -72,7 +72,7 @@ void SOCKS5ProxiedConnection::handleConnectionConnectFinished(bool error) { SWIFT_LOG(debug) << "Connection to proxy established, now connect to the server via it." << std::endl; proxyState_ = ProxyAuthenticating; - ByteArray socksConnect; + SafeByteArray socksConnect; socksConnect.push_back(0x05); // VER = SOCKS5 = 0x05 socksConnect.push_back(0x01); // Number of authentication methods after this byte. socksConnect.push_back(0x00); // 0x00 == no authentication @@ -86,8 +86,8 @@ void SOCKS5ProxiedConnection::handleConnectionConnectFinished(bool error) { } } -void SOCKS5ProxiedConnection::handleDataRead(const ByteArray& data) { - ByteArray socksConnect; +void SOCKS5ProxiedConnection::handleDataRead(const SafeByteArray& data) { + SafeByteArray socksConnect; boost::asio::ip::address rawAddress = server_.getAddress().getRawAddress(); assert(rawAddress.is_v4() || rawAddress.is_v6()); if (!connected_) { diff --git a/Swiften/Network/SOCKS5ProxiedConnection.h b/Swiften/Network/SOCKS5ProxiedConnection.h index e594e0f..942b6ce 100644 --- a/Swiften/Network/SOCKS5ProxiedConnection.h +++ b/Swiften/Network/SOCKS5ProxiedConnection.h @@ -34,7 +34,7 @@ namespace Swift { virtual void listen(); virtual void connect(const HostAddressPort& address); virtual void disconnect(); - virtual void write(const ByteArray& data); + virtual void write(const SafeByteArray& data); virtual HostAddressPort getLocalAddress() const; @@ -42,7 +42,7 @@ namespace Swift { SOCKS5ProxiedConnection(ConnectionFactory* connectionFactory, const HostAddressPort& proxy); void handleConnectionConnectFinished(bool error); - void handleDataRead(const ByteArray& data); + void handleDataRead(const SafeByteArray& data); void handleDisconnected(const boost::optional<Error>& error); private: diff --git a/Swiften/Network/UnitTest/ChainedConnectorTest.cpp b/Swiften/Network/UnitTest/ChainedConnectorTest.cpp index 9c4c99d..c7d23da 100644 --- a/Swiften/Network/UnitTest/ChainedConnectorTest.cpp +++ b/Swiften/Network/UnitTest/ChainedConnectorTest.cpp @@ -128,7 +128,7 @@ class ChainedConnectorTest : public CppUnit::TestFixture { HostAddressPort getLocalAddress() const { return HostAddressPort(); } void disconnect() { assert(false); } - void write(const ByteArray&) { assert(false); } + void write(const SafeByteArray&) { assert(false); } bool connects; int id; diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp index 6151b5c..6488e67 100644 --- a/Swiften/Network/UnitTest/ConnectorTest.cpp +++ b/Swiften/Network/UnitTest/ConnectorTest.cpp @@ -269,7 +269,7 @@ class ConnectorTest : public CppUnit::TestFixture { HostAddressPort getLocalAddress() const { return HostAddressPort(); } void disconnect() { assert(false); } - void write(const ByteArray&) { assert(false); } + void write(const SafeByteArray&) { assert(false); } EventLoop* eventLoop; boost::optional<HostAddressPort> hostAddressPort; diff --git a/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp b/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp index d33c988..90be27e 100644 --- a/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp +++ b/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp @@ -76,7 +76,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345)); eventLoop->processEvents(); - connectionFactory->connections[0]->onDataRead(createByteArray("HTTP/1.0 200 Connection established\r\n\r\n")); + connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 200 Connection established\r\n\r\n")); eventLoop->processEvents(); CPPUNIT_ASSERT(connectFinished); @@ -89,7 +89,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345)); eventLoop->processEvents(); - connectionFactory->connections[0]->onDataRead(createByteArray("FLOOP")); + connectionFactory->connections[0]->onDataRead(createSafeByteArray("FLOOP")); eventLoop->processEvents(); CPPUNIT_ASSERT(connectFinished); @@ -102,7 +102,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345)); eventLoop->processEvents(); - connectionFactory->connections[0]->onDataRead(createByteArray("HTTP/1.0 401 Unauthorized\r\n\r\n")); + connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 401 Unauthorized\r\n\r\n")); eventLoop->processEvents(); CPPUNIT_ASSERT(connectFinished); @@ -114,10 +114,10 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { HTTPConnectProxiedConnection::ref testling(createTestling()); testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345)); eventLoop->processEvents(); - connectionFactory->connections[0]->onDataRead(createByteArray("HTTP/1.0 200 Connection established\r\n\r\n")); + connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 200 Connection established\r\n\r\n")); eventLoop->processEvents(); - connectionFactory->connections[0]->onDataRead(createByteArray("abcdef")); + connectionFactory->connections[0]->onDataRead(createSafeByteArray("abcdef")); CPPUNIT_ASSERT_EQUAL(createByteArray("abcdef"), dataRead); } @@ -126,11 +126,11 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { HTTPConnectProxiedConnection::ref testling(createTestling()); testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345)); eventLoop->processEvents(); - connectionFactory->connections[0]->onDataRead(createByteArray("HTTP/1.0 200 Connection established\r\n\r\n")); + connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 200 Connection established\r\n\r\n")); eventLoop->processEvents(); connectionFactory->connections[0]->dataWritten.clear(); - testling->write(createByteArray("abcdef")); + testling->write(createSafeByteArray("abcdef")); CPPUNIT_ASSERT_EQUAL(createByteArray("abcdef"), connectionFactory->connections[0]->dataWritten); } @@ -151,7 +151,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { HTTPConnectProxiedConnection::ref testling(createTestling()); testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345)); eventLoop->processEvents(); - connectionFactory->connections[0]->onDataRead(createByteArray("HTTP/1.0 200 Connection established\r\n\r\n")); + connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 200 Connection established\r\n\r\n")); eventLoop->processEvents(); testling->disconnect(); @@ -180,7 +180,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { disconnectedError = e; } - void handleDataRead(const ByteArray& d) { + void handleDataRead(const SafeByteArray& d) { append(dataRead, d); } @@ -204,7 +204,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture { onDisconnected(boost::optional<Connection::Error>()); } - void write(const ByteArray& d) { + void write(const SafeByteArray& d) { append(dataWritten, d); } |