From 7bee253e4690d4d84e2e7a81cd57436f0da1dee4 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 22 Apr 2014 20:43:58 -0400 Subject: [PATCH 01/10] fixed warning --- Daemon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Daemon.cpp b/Daemon.cpp index 7dfe54d9..e0c93cb3 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -33,7 +33,7 @@ namespace i2p i2p::proxy::HTTPProxy *httpProxy; }; - Daemon_Singleton::Daemon_Singleton() : d(*new Daemon_Singleton_Private()), running(1) {}; + Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {}; Daemon_Singleton::~Daemon_Singleton() { delete &d; }; From 42f228e75afcea53080de569d932100288d13240 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 23 Apr 2014 12:17:14 -0400 Subject: [PATCH 02/10] proper termination --- Queue.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Queue.h b/Queue.h index 85114bc1..52474aff 100644 --- a/Queue.h +++ b/Queue.h @@ -46,6 +46,12 @@ namespace util return el; } + void Wait () + { + std::unique_lock l(m_QueueMutex); + m_NonEmpty.wait (l); + } + bool Wait (int sec, int usec) { std::unique_lock l(m_QueueMutex); @@ -98,27 +104,33 @@ namespace util { public: - MsgQueue (): m_Thread (std::bind (&MsgQueue::Run, this)) , running(1) {}; + MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue::Run, this)) {}; void Stop() { - running = 0; + m_IsRunning = false; + Queue::WakeUp (); m_Thread.join(); } private: + void Run () { - Msg * msg = nullptr; - while ((msg = Queue::GetNext ()) != nullptr && running) + while (m_IsRunning) { - msg->Process (); - delete msg; + while (Msg * msg = Queue::Get ()) + { + msg->Process (); + delete msg; + } + Queue::Wait (); } } private: - std::thread m_Thread; - volatile int running; + + bool m_IsRunning; + std::thread m_Thread; }; } } From 58939de57eb78a80b97acc2279d92b5ef8e55519 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 23 Apr 2014 12:49:02 -0400 Subject: [PATCH 03/10] flush log only when queue is empty --- Log.cpp | 15 ++++++++++----- Log.h | 14 +++++++++++++- Queue.h | 8 ++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Log.cpp b/Log.cpp index 4db4920e..92fd8a25 100644 --- a/Log.cpp +++ b/Log.cpp @@ -2,14 +2,19 @@ #include "Daemon.h" -i2p::util::MsgQueue g_Log; +Log g_Log; void LogMsg::Process() { if (Daemon.isLogging == 1 && Daemon.logfile.is_open()) - { Daemon.logfile << s.str(); - Daemon.logfile.flush(); - } + output << s.str(); -} \ No newline at end of file +} + +void Log::Flush () +{ + if (Daemon.isLogging == 1 && Daemon.logfile.is_open()) + Daemon.logfile.flush(); +} + diff --git a/Log.h b/Log.h index acef8f8e..a5856ba7 100644 --- a/Log.h +++ b/Log.h @@ -3,6 +3,7 @@ #include #include +#include #include "Queue.h" struct LogMsg @@ -15,7 +16,18 @@ struct LogMsg void Process(); }; -extern i2p::util::MsgQueue g_Log; +class Log: public i2p::util::MsgQueue +{ + public: + + Log () { SetOnEmpty (std::bind (&Log::Flush, this)); }; + + private: + + void Flush (); +}; + +extern Log g_Log; template void LogPrint (std::stringstream& s, TValue arg) diff --git a/Queue.h b/Queue.h index 52474aff..22f51e69 100644 --- a/Queue.h +++ b/Queue.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace i2p { @@ -104,6 +105,8 @@ namespace util { public: + typedef std::function OnEmpty; + MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue::Run, this)) {}; void Stop() { @@ -112,6 +115,8 @@ namespace util m_Thread.join(); } + void SetOnEmpty (OnEmpty const & e) { m_OnEmpty = e; }; + private: void Run () @@ -123,6 +128,8 @@ namespace util msg->Process (); delete msg; } + if (m_OnEmpty != nullptr) + m_OnEmpty (); Queue::Wait (); } } @@ -131,6 +138,7 @@ namespace util bool m_IsRunning; std::thread m_Thread; + OnEmpty m_OnEmpty; }; } } From 66ff2c7875c51156d33782638b56700e8b7c7566 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 23 Apr 2014 13:44:08 -0400 Subject: [PATCH 04/10] eliminate boost::protect --- HTTPServer.cpp | 5 ++--- Streaming.h | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/HTTPServer.cpp b/HTTPServer.cpp index ac23ebb3..c2a235fa 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -1,5 +1,4 @@ #include -#include #include #include "base64.h" #include "Log.h" @@ -388,8 +387,8 @@ namespace util { if (m_Stream) m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192), - boost::protect (boost::bind (&HTTPConnection::HandleStreamReceive, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)), + boost::bind (&HTTPConnection::HandleStreamReceive, this, + boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred), 45); // 45 seconds timeout } diff --git a/Streaming.h b/Streaming.h index 88fd7c20..dd6e1378 100644 --- a/Streaming.h +++ b/Streaming.h @@ -214,8 +214,8 @@ namespace stream } } m_ReceiveTimer.expires_from_now (boost::posix_time::seconds(timeout)); - m_ReceiveTimer.async_wait (boost::bind (&Stream::HandleReceiveTimer, - this, boost::asio::placeholders::error, buffer, handler)); + m_ReceiveTimer.async_wait ([&buffer, handler, this](const boost::system::error_code& ecode) + { this->HandleReceiveTimer (ecode, buffer, handler); }); } template From ceb2ec1bc06f3ad68b0eb49bfb46f053344ec0de Mon Sep 17 00:00:00 2001 From: cpubug Date: Wed, 23 Apr 2014 22:43:54 +0400 Subject: [PATCH 05/10] load IdentHash from base32 string --- Identity.cpp | 6 ++++++ Identity.h | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Identity.cpp b/Identity.cpp index c7c3ed0e..5a59e26e 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -40,6 +40,12 @@ namespace data return *this; } + bool IdentHash::FromBase32(const std::string& s) + { + size_t count = Base32ToByteStream(s.c_str(), s.length(), m_Hash, sizeof(m_Hash)); + return count == sizeof(m_Hash); + } + Keys CreateRandomKeys () { Keys keys; diff --git a/Identity.h b/Identity.h index ff35a7db..12ca8535 100644 --- a/Identity.h +++ b/Identity.h @@ -73,7 +73,9 @@ namespace data bool operator== (const IdentHash& other) const { return !memcmp (m_Hash, other.m_Hash, 32); }; bool operator< (const IdentHash& other) const { return memcmp (m_Hash, other.m_Hash, 32) < 0; }; - + + bool FromBase32(const std::string&); + private: uint8_t m_Hash[32]; From c415d7fffcf1571258759a97fe1bbaa156ffb0bb Mon Sep 17 00:00:00 2001 From: cpubug Date: Wed, 23 Apr 2014 22:46:15 +0400 Subject: [PATCH 06/10] revert commit 61001ef --- Identity.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Identity.h b/Identity.h index 12ca8535..be9ae901 100644 --- a/Identity.h +++ b/Identity.h @@ -108,7 +108,7 @@ namespace data public: RoutingDestination (): m_ElGamalEncryption (nullptr) {}; - virtual ~RoutingDestination () { if (m_ElGamalEncryption) delete m_ElGamalEncryption; }; + virtual ~RoutingDestination () { delete m_ElGamalEncryption; }; virtual const IdentHash& GetIdentHash () const = 0; virtual const uint8_t * GetEncryptionPublicKey () const = 0; From 087e61dbcadc8c545bc3035b0e0840fa0a3b870a Mon Sep 17 00:00:00 2001 From: cpubug Date: Wed, 23 Apr 2014 22:47:17 +0400 Subject: [PATCH 07/10] fix crash --- Streaming.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Streaming.cpp b/Streaming.cpp index 9a23c313..b338c8c6 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -283,11 +283,11 @@ namespace stream bool Stream::SendPacket (const uint8_t * buf, size_t len) { const I2NPMessage * leaseSet = nullptr; - if (m_LeaseSetUpdated) - { - leaseSet = m_LocalDestination->GetLeaseSet (); - m_LeaseSetUpdated = false; - } + + leaseSet = m_LocalDestination->GetLeaseSet (); + if (!leaseSet) + return false; + I2NPMessage * msg = i2p::garlic::routing.WrapMessage (m_RemoteLeaseSet, CreateDataMessage (this, buf, len), leaseSet); if (!m_OutboundTunnel || m_OutboundTunnel->IsFailed ()) From cba921eb3e229e6e79bfc125a886f1e7fba72026 Mon Sep 17 00:00:00 2001 From: cpubug Date: Wed, 23 Apr 2014 22:48:02 +0400 Subject: [PATCH 08/10] fix CMAKE build --- build/CMakeLists.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index 5541301f..ac114a45 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -21,6 +21,7 @@ set ( SOURCES NTCPSession.cpp RouterContext.cpp SSU.cpp + SSUData.cpp TransitTunnel.cpp Tunnel.cpp TunnelGateway.cpp @@ -37,6 +38,7 @@ set ( SOURCES TunnelEndpoint.cpp TunnelPool.cpp util.cpp + Daemon.cpp ) set ( HEADERS @@ -48,6 +50,7 @@ set ( HEADERS NTCPSession.h RouterContext.h SSU.h + SSUData.h TransitTunnel.h Tunnel.h TunnelGateway.h @@ -64,8 +67,17 @@ set ( HEADERS TunnelEndpoint.h TunnelPool.h util.h + Daemon.h ) +if (WIN32) + list (APPEND SOURCES DeamonWin32.cpp) +else () + list (APPEND SOURCES DaemonLinux.cpp) +endif () + + + source_group ("Header Files" FILES ${HEADERS}) source_group ("Source Files" FILES ${SOURCES}) From 5e1f3a4939257145f1425479bda19eff30ef2746 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 23 Apr 2014 15:09:01 -0400 Subject: [PATCH 09/10] rolled back --- Streaming.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Streaming.cpp b/Streaming.cpp index b338c8c6..d5e3fbcd 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -284,9 +284,11 @@ namespace stream { const I2NPMessage * leaseSet = nullptr; - leaseSet = m_LocalDestination->GetLeaseSet (); - if (!leaseSet) - return false; + if (m_LeaseSetUpdated) + { + leaseSet = m_LocalDestination->GetLeaseSet (); + m_LeaseSetUpdated = false; + } I2NPMessage * msg = i2p::garlic::routing.WrapMessage (m_RemoteLeaseSet, CreateDataMessage (this, buf, len), leaseSet); From 3e81123d94d2430d42734e39431394e421d1b0f5 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 23 Apr 2014 17:51:12 -0400 Subject: [PATCH 10/10] fixed creash --- Streaming.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Streaming.h b/Streaming.h index dd6e1378..5a0ed383 100644 --- a/Streaming.h +++ b/Streaming.h @@ -214,7 +214,7 @@ namespace stream } } m_ReceiveTimer.expires_from_now (boost::posix_time::seconds(timeout)); - m_ReceiveTimer.async_wait ([&buffer, handler, this](const boost::system::error_code& ecode) + m_ReceiveTimer.async_wait ([=](const boost::system::error_code& ecode) { this->HandleReceiveTimer (ecode, buffer, handler); }); }