From 924f2815365ca02cd37c37d791582461e1a7238e Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sat, 2 Apr 2016 08:05:14 -0400 Subject: [PATCH 01/10] * Don't set m_Session to nullptr in SAMSocket::Terminate * check for null localDestination in SAMSocket::Terminate --- SAM.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/SAM.cpp b/SAM.cpp index ecdd513d..bcb4623a 100644 --- a/SAM.cpp +++ b/SAM.cpp @@ -49,7 +49,6 @@ namespace client { if (m_Session) { m_Session->DelSocket (shared_from_this ()); - m_Session = nullptr; } break; } @@ -58,8 +57,8 @@ namespace client if (m_Session) { m_Session->DelSocket (shared_from_this ()); - m_Session->localDestination->StopAcceptingStreams (); - m_Session = nullptr; + if (m_Session->localDestination) + m_Session->localDestination->StopAcceptingStreams (); } break; } From 47ce2398a4f5d7e4a50814eb8fa8990547f74999 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 11 May 2016 08:41:32 -0400 Subject: [PATCH 02/10] fix http unit test SIGBUS in os x --- HTTP.cpp | 27 +++++++++++++-------------- HTTP.h | 4 ++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/HTTP.cpp b/HTTP.cpp index 74acfb7a..02b58f99 100644 --- a/HTTP.cpp +++ b/HTTP.cpp @@ -10,23 +10,22 @@ namespace i2p { namespace http { - const char *HTTP_METHODS[] = { + const std::vector HTTP_METHODS = { "GET", "HEAD", "POST", "PUT", "PATCH", - "DELETE", "OPTIONS", "CONNECT", - NULL + "DELETE", "OPTIONS", "CONNECT" }; - const char *HTTP_VERSIONS[] = { - "HTTP/1.0", "HTTP/1.1", NULL + const std::vector HTTP_VERSIONS = { + "HTTP/1.0", "HTTP/1.1" }; - bool in_cstr_array(const char **haystack, const char *needle) { - for (const char *p = haystack[0]; p != NULL; p++) { - if (strcmp(p, needle) == 0) - return true; - } - return false; + inline bool is_http_version(const std::string & str) { + return std::find(HTTP_VERSIONS.begin(), HTTP_VERSIONS.end(), str) != std::end(HTTP_VERSIONS); } + inline bool is_http_method(const std::string & str) { + return std::find(HTTP_METHODS.begin(), HTTP_METHODS.end(), str) != std::end(HTTP_METHODS); + } + void strsplit(const std::string & line, std::vector &tokens, char delim, std::size_t limit = 0) { std::size_t count = 0; std::stringstream ss(line); @@ -205,9 +204,9 @@ namespace http { strsplit(line, tokens, ' '); if (tokens.size() != 3) return -1; - if (!in_cstr_array(HTTP_METHODS, tokens[0].c_str())) + if (!is_http_method(tokens[0])) return -1; - if (!in_cstr_array(HTTP_VERSIONS, tokens[2].c_str())) + if (!is_http_version(tokens[2])) return -1; if (!url.parse(tokens[1])) return -1; @@ -288,7 +287,7 @@ namespace http { strsplit(line, tokens, ' ', 3); if (tokens.size() != 3) return -1; - if (!in_cstr_array(HTTP_VERSIONS, tokens[0].c_str())) + if (!is_http_version(tokens[0])) return -1; code = atoi(tokens[1].c_str()); if (code < 100 || code >= 600) diff --git a/HTTP.h b/HTTP.h index 864ad88b..9bd31c75 100644 --- a/HTTP.h +++ b/HTTP.h @@ -19,8 +19,8 @@ namespace i2p { namespace http { const char CRLF[] = "\r\n"; /**< HTTP line terminator */ const char HTTP_EOH[] = "\r\n\r\n"; /**< HTTP end-of-headers mark */ - extern const char *HTTP_METHODS[]; /**< list of valid HTTP methods */ - extern const char *HTTP_VERSIONS[]; /**< list of valid HTTP versions */ + extern const std::vector HTTP_METHODS; /**< list of valid HTTP methods */ + extern const std::vector HTTP_VERSIONS; /**< list of valid HTTP versions */ struct URL { std::string schema; From 3cfbc05bf9bfb72fa6351daef31f47f4c279c054 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 8 Jun 2016 09:56:13 -0400 Subject: [PATCH 03/10] set pointer to null after delete --- I2CP.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/I2CP.cpp b/I2CP.cpp index ec06895f..9963cb33 100644 --- a/I2CP.cpp +++ b/I2CP.cpp @@ -178,6 +178,7 @@ namespace client memcpy (m_NextMessage + m_NextMessageOffset, m_Buffer, offset); HandleNextMessage (m_NextMessage); delete[] m_NextMessage; + m_NextMessage = nullptr } } while (offset < bytes_transferred) From 05939a2bbc1947103b58b5e67fc0295bb8757e8c Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 13 Jun 2016 08:50:53 -0400 Subject: [PATCH 04/10] special case for i2p.rocks in proxy --- HTTP.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/HTTP.cpp b/HTTP.cpp index a23f5a72..b4ba9940 100644 --- a/HTTP.cpp +++ b/HTTP.cpp @@ -71,7 +71,11 @@ namespace http { bool URL::parse(const std::string& url) { std::size_t pos_p = 0; /* < current parse position */ std::size_t pos_c = 0; /* < work position */ - if (url.at(0) != '/') { + if (url.at(0) == "/" && url.find("/http://") == url.begin()) { + /* specical case */ + pos_p ++; + } + if(url.at(0) != "/" || pos_b > 0) { /* schema */ pos_c = url.find("://"); if (pos_c != std::string::npos) { From a183ca8661c4ffa7c863ef36aff018fb449a9035 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 13 Jun 2016 08:52:21 -0400 Subject: [PATCH 05/10] fix special case --- HTTP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HTTP.cpp b/HTTP.cpp index b4ba9940..1a79502c 100644 --- a/HTTP.cpp +++ b/HTTP.cpp @@ -71,7 +71,7 @@ namespace http { bool URL::parse(const std::string& url) { std::size_t pos_p = 0; /* < current parse position */ std::size_t pos_c = 0; /* < work position */ - if (url.at(0) == "/" && url.find("/http://") == url.begin()) { + if (url.at(0) == "/" && url.find("/http://") == 0) { /* specical case */ pos_p ++; } From ea7e6615f2406f050f2b8201deb43cc061b95a57 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 13 Jun 2016 08:52:54 -0400 Subject: [PATCH 06/10] fix typo --- HTTP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HTTP.cpp b/HTTP.cpp index 1a79502c..451c099f 100644 --- a/HTTP.cpp +++ b/HTTP.cpp @@ -75,7 +75,7 @@ namespace http { /* specical case */ pos_p ++; } - if(url.at(0) != "/" || pos_b > 0) { + if(url.at(0) != "/" || pos_p > 0) { /* schema */ pos_c = url.find("://"); if (pos_c != std::string::npos) { From 09fc767bb0af70265923ada3bc811fe12e7308a6 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 13 Jun 2016 08:53:35 -0400 Subject: [PATCH 07/10] fix another typo --- HTTP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HTTP.cpp b/HTTP.cpp index 451c099f..53115bda 100644 --- a/HTTP.cpp +++ b/HTTP.cpp @@ -71,11 +71,11 @@ namespace http { bool URL::parse(const std::string& url) { std::size_t pos_p = 0; /* < current parse position */ std::size_t pos_c = 0; /* < work position */ - if (url.at(0) == "/" && url.find("/http://") == 0) { + if (url.at(0) == '/' && url.find("/http://") == 0) { /* specical case */ pos_p ++; } - if(url.at(0) != "/" || pos_p > 0) { + if(url.at(0) != '/' || pos_p > 0) { /* schema */ pos_c = url.find("://"); if (pos_c != std::string::npos) { From 9eaa51442f434e6a25aa75b198ab4246483778f6 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 13 Jun 2016 09:01:38 -0400 Subject: [PATCH 08/10] update comment --- HTTP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HTTP.cpp b/HTTP.cpp index 53115bda..bb389356 100644 --- a/HTTP.cpp +++ b/HTTP.cpp @@ -72,7 +72,7 @@ namespace http { std::size_t pos_p = 0; /* < current parse position */ std::size_t pos_c = 0; /* < work position */ if (url.at(0) == '/' && url.find("/http://") == 0) { - /* specical case */ + /* special case for i2p.rocks inproxy */ pos_p ++; } if(url.at(0) != '/' || pos_p > 0) { From fa68e392c81f929f4388b1c18043b30759461499 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 13 Jun 2016 11:34:44 -0400 Subject: [PATCH 09/10] don't abort when ntcp fails to bind --- Daemon.cpp | 30 ++++++++++++++------- I2CP.cpp | 20 +++++++++----- NTCPSession.cpp | 66 +++++++++++++++++++++++++++++----------------- NTCPSession.h | 5 +++- NetDb.cpp | 2 +- SSU.h | 2 +- Transports.cpp | 70 +++++++++++++++++++++++++++++++------------------ Transports.h | 3 +++ 8 files changed, 129 insertions(+), 69 deletions(-) diff --git a/Daemon.cpp b/Daemon.cpp index 7ca28a6f..b580f827 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -198,25 +198,34 @@ namespace i2p bool Daemon_Singleton::start() { - bool http; i2p::config::GetOption("http.enabled", http); - if (http) { - std::string httpAddr; i2p::config::GetOption("http.address", httpAddr); - uint16_t httpPort; i2p::config::GetOption("http.port", httpPort); - LogPrint(eLogInfo, "Daemon: starting HTTP Server at ", httpAddr, ":", httpPort); - d.httpServer = std::unique_ptr(new i2p::http::HTTPServer(httpAddr, httpPort)); - d.httpServer->Start(); - } - LogPrint(eLogInfo, "Daemon: starting NetDB"); i2p::data::netdb.Start(); #ifdef USE_UPNP LogPrint(eLogInfo, "Daemon: starting UPnP"); d.m_UPnP.Start (); -#endif +#endif LogPrint(eLogInfo, "Daemon: starting Transports"); i2p::transport::transports.Start(); + if (i2p::transport::transports.IsBoundNTCP() || i2p::transport::transports.IsBoundSSU()) { + LogPrint(eLogInfo, "Daemon: Transports started"); + } else { + LogPrint(eLogError, "Daemon: failed to start Transports"); + /** shut down netdb right away */ + i2p::data::netdb.Stop(); + return false; + } + + bool http; i2p::config::GetOption("http.enabled", http); + if (http) { + std::string httpAddr; i2p::config::GetOption("http.address", httpAddr); + uint16_t httpPort; i2p::config::GetOption("http.port", httpPort); + LogPrint(eLogInfo, "Daemon: starting HTTP Server at ", httpAddr, ":", httpPort); + d.httpServer = std::unique_ptr(new i2p::http::HTTPServer(httpAddr, httpPort)); + d.httpServer->Start(); + } + LogPrint(eLogInfo, "Daemon: starting Tunnels"); i2p::tunnel::tunnels.Start(); @@ -232,6 +241,7 @@ namespace i2p d.m_I2PControlService = std::unique_ptr(new i2p::client::I2PControlService (i2pcpAddr, i2pcpPort)); d.m_I2PControlService->Start (); } + return true; } diff --git a/I2CP.cpp b/I2CP.cpp index dab7f9b0..b7f91cdd 100644 --- a/I2CP.cpp +++ b/I2CP.cpp @@ -469,13 +469,19 @@ namespace client if (m_Destination) { i2p::data::IdentityEx identity; - offset += identity.FromBuffer (buf + offset, len - offset); - uint32_t payloadLen = bufbe32toh (buf + offset); - offset += 4; - uint32_t nonce = bufbe32toh (buf + offset + payloadLen); - if (m_IsSendAccepted) - SendMessageStatusMessage (nonce, eI2CPMessageStatusAccepted); // accepted - m_Destination->SendMsgTo (buf + offset, payloadLen, identity.GetIdentHash (), nonce); + size_t identsize = identity.FromBuffer (buf + offset, len - offset); + if (identsize) + { + offset += identsize; + uint32_t payloadLen = bufbe32toh (buf + offset); + offset += 4; + uint32_t nonce = bufbe32toh (buf + offset + payloadLen); + if (m_IsSendAccepted) + SendMessageStatusMessage (nonce, eI2CPMessageStatusAccepted); // accepted + m_Destination->SendMsgTo (buf + offset, payloadLen, identity.GetIdentHash (), nonce); + } + else + LogPrint(eLogError, "I2CP: invalid identity"); } } else diff --git a/NTCPSession.cpp b/NTCPSession.cpp index ae020f5f..5cd31960 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -760,30 +760,46 @@ namespace transport auto& addresses = context.GetRouterInfo ().GetAddresses (); for (auto address: addresses) { - if (address->transportStyle == i2p::data::RouterInfo::eTransportNTCP && address->host.is_v4 ()) - { - m_NTCPAcceptor = new boost::asio::ip::tcp::acceptor (m_Service, - boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address->port)); - - LogPrint (eLogInfo, "NTCP: Start listening TCP port ", address->port); - auto conn = std::make_shared(*this); - m_NTCPAcceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAccept, this, - conn, std::placeholders::_1)); - - if (context.SupportsV6 ()) + if (address->transportStyle == i2p::data::RouterInfo::eTransportNTCP) + { + if (address->host.is_v4()) { - m_NTCPV6Acceptor = new boost::asio::ip::tcp::acceptor (m_Service); - m_NTCPV6Acceptor->open (boost::asio::ip::tcp::v6()); - m_NTCPV6Acceptor->set_option (boost::asio::ip::v6_only (true)); - m_NTCPV6Acceptor->bind (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), address->port)); - m_NTCPV6Acceptor->listen (); - - LogPrint (eLogInfo, "NTCP: Start listening V6 TCP port ", address->port); - auto conn = std::make_shared (*this); - m_NTCPV6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAcceptV6, - this, conn, std::placeholders::_1)); + try + { + m_NTCPAcceptor = new boost::asio::ip::tcp::acceptor (m_Service, + boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address->port)); + } catch ( std::exception & ex ) { + /** fail to bind ip4 */ + LogPrint(eLogError, "NTCP: Failed to bind to ip4 port ",address->port, ex.what()); + continue; + } + + LogPrint (eLogInfo, "NTCP: Start listening TCP port ", address->port); + auto conn = std::make_shared(*this); + m_NTCPAcceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAccept, this, + conn, std::placeholders::_1)); + } + else if (address->host.is_v6() && context.SupportsV6 ()) + { + m_NTCPV6Acceptor = new boost::asio::ip::tcp::acceptor (m_Service); + try + { + m_NTCPV6Acceptor->open (boost::asio::ip::tcp::v6()); + m_NTCPV6Acceptor->set_option (boost::asio::ip::v6_only (true)); + + m_NTCPV6Acceptor->bind (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), address->port)); + m_NTCPV6Acceptor->listen (); + + LogPrint (eLogInfo, "NTCP: Start listening V6 TCP port ", address->port); + auto conn = std::make_shared (*this); + m_NTCPV6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAcceptV6, + this, conn, std::placeholders::_1)); + } catch ( std::exception & ex ) { + LogPrint(eLogError, "NTCP: failed to bind to ip6 port ", address->port); + continue; + } } - } + } } } } @@ -795,9 +811,11 @@ namespace transport if (m_IsRunning) { m_IsRunning = false; - delete m_NTCPAcceptor; + if (m_NTCPAcceptor) + delete m_NTCPAcceptor; m_NTCPAcceptor = nullptr; - delete m_NTCPV6Acceptor; + if (m_NTCPV6Acceptor) + delete m_NTCPV6Acceptor; m_NTCPV6Acceptor = nullptr; m_Service.stop (); diff --git a/NTCPSession.h b/NTCPSession.h index f4ce18a6..2a60f1dc 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -144,7 +144,10 @@ namespace transport void RemoveNTCPSession (std::shared_ptr session); std::shared_ptr FindNTCPSession (const i2p::data::IdentHash& ident); void Connect (const boost::asio::ip::address& address, int port, std::shared_ptr conn); - + + bool IsBoundV4() const { return m_NTCPAcceptor != nullptr; }; + bool IsBoundV6() const { return m_NTCPV6Acceptor != nullptr; }; + boost::asio::io_service& GetService () { return m_Service; }; void Ban (const boost::asio::ip::address& addr); diff --git a/NetDb.cpp b/NetDb.cpp index d2afc50a..a4c685db 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -67,7 +67,7 @@ namespace data } m_LeaseSets.clear(); m_Requests.Stop (); - } + } } void NetDb::Run () diff --git a/SSU.h b/SSU.h index 8ee58ffa..fc79b981 100644 --- a/SSU.h +++ b/SSU.h @@ -62,7 +62,7 @@ namespace transport std::shared_ptr GetPeerTestSession (uint32_t nonce); void UpdatePeerTest (uint32_t nonce, PeerTestParticipant role); void RemovePeerTest (uint32_t nonce); - + private: void Run (); diff --git a/Transports.cpp b/Transports.cpp index 2d3d423f..52b1d261 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -46,7 +46,7 @@ namespace transport int num; while ((num = m_QueueSize - m_Queue.size ()) > 0) CreateDHKeysPairs (num); - std::unique_lock l(m_AcquiredMutex); + std::unique_lock l(m_AcquiredMutex); m_Acquired.wait (l); // wait for element gets aquired } } @@ -60,7 +60,7 @@ namespace transport { auto pair = std::make_shared (); pair->GenerateKeys (); - std::unique_lock l(m_AcquiredMutex); + std::unique_lock l(m_AcquiredMutex); m_Queue.push (pair); } } @@ -69,7 +69,7 @@ namespace transport std::shared_ptr DHKeysPairSupplier::Acquire () { { - std::unique_lock l(m_AcquiredMutex); + std::unique_lock l(m_AcquiredMutex); if (!m_Queue.empty ()) { auto pair = m_Queue.front (); @@ -86,7 +86,7 @@ namespace transport void DHKeysPairSupplier::Return (std::shared_ptr pair) { - std::unique_lock l(m_AcquiredMutex); + std::unique_lock l(m_AcquiredMutex); m_Queue.push (pair); } @@ -115,9 +115,16 @@ namespace transport for (auto address : addresses) { if (!m_NTCPServer) - { + { m_NTCPServer = new NTCPServer (); m_NTCPServer->Start (); + if (!(m_NTCPServer->IsBoundV6() || m_NTCPServer->IsBoundV4())) { + /** failed to bind to NTCP */ + LogPrint(eLogError, "Transports: failed to bind to TCP"); + m_NTCPServer->Stop(); + delete m_NTCPServer; + m_NTCPServer = nullptr; + } } if (address->transportStyle == RouterInfo::eTransportSSU && address->host.is_v4 ()) @@ -126,7 +133,14 @@ namespace transport { m_SSUServer = new SSUServer (address->port); LogPrint (eLogInfo, "Transports: Start listening UDP port ", address->port); - m_SSUServer->Start (); + try { + m_SSUServer->Start (); + } catch ( std::exception & ex ) { + LogPrint(eLogError, "Transports: Failed to bind to UDP port", address->port); + delete m_SSUServer; + m_SSUServer = nullptr; + continue; + } DetectExternalIP (); } else @@ -206,7 +220,7 @@ namespace transport void Transports::SendMessage (const i2p::data::IdentHash& ident, std::shared_ptr msg) { - SendMessages (ident, std::vector > {msg }); + SendMessages (ident, std::vector > {msg }); } void Transports::SendMessages (const i2p::data::IdentHash& ident, const std::vector >& msgs) @@ -231,7 +245,7 @@ namespace transport { auto r = netdb.FindRouter (ident); { - std::unique_lock l(m_PeersMutex); + std::unique_lock l(m_PeersMutex); it = m_Peers.insert (std::pair(ident, { 0, r, {}, i2p::util::GetSecondsSinceEpoch (), {} })).first; } @@ -288,7 +302,7 @@ namespace transport } } else - LogPrint (eLogWarning, "Transports: NTCP address is not present for ", i2p::data::GetIdentHashAbbreviation (ident), ", trying SSU"); + LogPrint (eLogDebug, "Transports: NTCP address is not present for ", i2p::data::GetIdentHashAbbreviation (ident), ", trying SSU"); } if (peer.numAttempts == 1)// SSU { @@ -320,7 +334,7 @@ namespace transport } LogPrint (eLogError, "Transports: No NTCP or SSU addresses available"); peer.Done (); - std::unique_lock l(m_PeersMutex); + std::unique_lock l(m_PeersMutex); m_Peers.erase (ident); return false; } @@ -352,7 +366,7 @@ namespace transport else { LogPrint (eLogError, "Transports: RouterInfo not found, Failed to send messages"); - std::unique_lock l(m_PeersMutex); + std::unique_lock l(m_PeersMutex); m_Peers.erase (it); } } @@ -396,7 +410,7 @@ namespace transport } } LogPrint (eLogError, "Transports: Unable to resolve NTCP address: ", ecode.message ()); - std::unique_lock l(m_PeersMutex); + std::unique_lock l(m_PeersMutex); m_Peers.erase (it1); } } @@ -438,7 +452,7 @@ namespace transport } } LogPrint (eLogError, "Transports: Unable to resolve SSU address: ", ecode.message ()); - std::unique_lock l(m_PeersMutex); + std::unique_lock l(m_PeersMutex); m_Peers.erase (it1); } } @@ -446,7 +460,7 @@ namespace transport void Transports::CloseSession (std::shared_ptr router) { if (!router) return; - m_Service.post (std::bind (&Transports::PostCloseSession, this, router)); + m_Service.post (std::bind (&Transports::PostCloseSession, this, router)); } void Transports::PostCloseSession (std::shared_ptr router) @@ -458,6 +472,12 @@ namespace transport LogPrint (eLogDebug, "Transports: SSU session closed"); } // TODO: delete NTCP + auto ntcpSession = m_NTCPServer ? m_NTCPServer->FindNTCPSession(router->GetIdentHash()) : nullptr; + if (ntcpSession) + { + m_NTCPServer->RemoveNTCPSession(ntcpSession); + LogPrint(eLogDebug, "Transports: NTCP session closed"); + } } void Transports::DetectExternalIP () @@ -468,14 +488,14 @@ namespace transport for (int i = 0; i < 5; i++) { auto router = i2p::data::netdb.GetRandomPeerTestRouter (); - if (router && router->IsSSU (!context.SupportsV6 ())) - m_SSUServer->CreateSession (router, true); // peer test + if (router && router->IsSSU (!context.SupportsV6 ())) + m_SSUServer->CreateSession (router, true); // peer test else { // if not peer test capable routers found pick any router = i2p::data::netdb.GetRandomRouter (); if (router && router->IsSSU ()) - m_SSUServer->CreateSession (router); // no peer test + m_SSUServer->CreateSession (router); // no peer test } } } @@ -498,7 +518,7 @@ namespace transport statusChanged = true; i2p::context.SetStatus (eRouterStatusTesting); // first time only } - m_SSUServer->CreateSession (router, true); // peer test + m_SSUServer->CreateSession (router, true); // peer test } } } @@ -517,7 +537,7 @@ namespace transport void Transports::PeerConnected (std::shared_ptr session) { m_Service.post([session, this]() - { + { auto remoteIdentity = session->GetRemoteIdentity (); if (!remoteIdentity) return; auto ident = remoteIdentity->GetIdentHash (); @@ -530,7 +550,7 @@ namespace transport // check if first message is our DatabaseStore (publishing) auto firstMsg = it->second.delayedMessages[0]; if (firstMsg && firstMsg->GetTypeID () == eI2NPDatabaseStore && - i2p::data::IdentHash(firstMsg->GetPayload () + DATABASE_STORE_KEY_OFFSET) == i2p::context.GetIdentHash ()) + i2p::data::IdentHash(firstMsg->GetPayload () + DATABASE_STORE_KEY_OFFSET) == i2p::context.GetIdentHash ()) sendDatabaseStore = false; // we have it in the list already } if (sendDatabaseStore) @@ -542,7 +562,7 @@ namespace transport else // incoming connection { session->SendI2NPMessages ({ CreateDatabaseStoreMsg () }); // send DatabaseStore - std::unique_lock l(m_PeersMutex); + std::unique_lock l(m_PeersMutex); m_Peers.insert (std::make_pair (ident, Peer{ 0, nullptr, { session }, i2p::util::GetSecondsSinceEpoch (), {} })); } }); @@ -551,7 +571,7 @@ namespace transport void Transports::PeerDisconnected (std::shared_ptr session) { m_Service.post([session, this]() - { + { auto remoteIdentity = session->GetRemoteIdentity (); if (!remoteIdentity) return; auto ident = remoteIdentity->GetIdentHash (); @@ -565,7 +585,7 @@ namespace transport ConnectToPeer (ident, it->second); else { - std::unique_lock l(m_PeersMutex); + std::unique_lock l(m_PeersMutex); m_Peers.erase (it); } } @@ -590,14 +610,14 @@ namespace transport if (it->second.sessions.empty () && ts > it->second.creationTime + SESSION_CREATION_TIMEOUT) { LogPrint (eLogWarning, "Transports: Session to peer ", it->first.ToBase64 (), " has not been created in ", SESSION_CREATION_TIMEOUT, " seconds"); - std::unique_lock l(m_PeersMutex); + std::unique_lock l(m_PeersMutex); it = m_Peers.erase (it); } else it++; } UpdateBandwidth (); // TODO: use separate timer(s) for it - if (i2p::context.GetStatus () == eRouterStatusTesting) // if still testing, repeat peer test + if (i2p::context.GetStatus () == eRouterStatusTesting) // if still testing, repeat peer test DetectExternalIP (); m_PeerCleanupTimer.expires_from_now (boost::posix_time::seconds(5*SESSION_CREATION_TIMEOUT)); m_PeerCleanupTimer.async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1)); diff --git a/Transports.h b/Transports.h index 3bfe1f8b..0e1b621b 100644 --- a/Transports.h +++ b/Transports.h @@ -75,6 +75,9 @@ namespace transport void Start (); void Stop (); + + bool IsBoundNTCP() const { return m_NTCPServer != nullptr; } + bool IsBoundSSU() const { return m_SSUServer != nullptr; } boost::asio::io_service& GetService () { return m_Service; }; std::shared_ptr GetNextDHKeysPair (); From e868d427dd703eca51b075c8ec95c54c7c89efe6 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 17 Jun 2016 09:02:12 -0400 Subject: [PATCH 10/10] add options to not use ntcp or ssu --- Config.cpp | 2 ++ Daemon.cpp | 4 +++- I2CP.cpp | 15 ++++++++++----- Makefile.linux | 6 +++--- Transports.cpp | 6 +++--- Transports.h | 2 +- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Config.cpp b/Config.cpp index e6d44d59..3156ac66 100644 --- a/Config.cpp +++ b/Config.cpp @@ -124,6 +124,8 @@ namespace config { ("notransit", value()->zero_tokens()->default_value(false), "Router will not accept transit tunnels at startup") ("floodfill", value()->zero_tokens()->default_value(false), "Router will be floodfill") ("bandwidth", value()->default_value(""), "Bandwidth limit: integer in kbps or letters: L (32), O (256), P (2048), X (>9000)") + ("ntcp", value()->zero_tokens()->default_value(true), "enable ntcp transport") + ("ssu", value()->zero_tokens()->default_value(true), "enable ssu transport") #ifdef _WIN32 ("svcctl", value()->default_value(""), "Windows service management ('install' or 'remove')") ("insomnia", value()->zero_tokens()->default_value(false), "Prevent system from sleeping") diff --git a/Daemon.cpp b/Daemon.cpp index b580f827..0a82a5cb 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -205,8 +205,10 @@ namespace i2p LogPrint(eLogInfo, "Daemon: starting UPnP"); d.m_UPnP.Start (); #endif + bool ntcp; i2p::config::GetOption("ntcp", ntcp); + bool ssu; i2p::config::GetOption("ssu", ssu); LogPrint(eLogInfo, "Daemon: starting Transports"); - i2p::transport::transports.Start(); + i2p::transport::transports.Start(ntcp, ssu); if (i2p::transport::transports.IsBoundNTCP() || i2p::transport::transports.IsBoundSSU()) { LogPrint(eLogInfo, "Daemon: Transports started"); } else { diff --git a/I2CP.cpp b/I2CP.cpp index b7f91cdd..3005efd0 100644 --- a/I2CP.cpp +++ b/I2CP.cpp @@ -474,11 +474,16 @@ namespace client { offset += identsize; uint32_t payloadLen = bufbe32toh (buf + offset); - offset += 4; - uint32_t nonce = bufbe32toh (buf + offset + payloadLen); - if (m_IsSendAccepted) - SendMessageStatusMessage (nonce, eI2CPMessageStatusAccepted); // accepted - m_Destination->SendMsgTo (buf + offset, payloadLen, identity.GetIdentHash (), nonce); + if (payloadLen + offset <= len) + { + offset += 4; + uint32_t nonce = bufbe32toh (buf + offset + payloadLen); + if (m_IsSendAccepted) + SendMessageStatusMessage (nonce, eI2CPMessageStatusAccepted); // accepted + m_Destination->SendMsgTo (buf + offset, payloadLen, identity.GetIdentHash (), nonce); + } + else + LogPrint(eLogError, "I2CP: cannot send message, too big"); } else LogPrint(eLogError, "I2CP: invalid identity"); diff --git a/Makefile.linux b/Makefile.linux index e00fd705..70e9e7dd 100644 --- a/Makefile.linux +++ b/Makefile.linux @@ -28,15 +28,15 @@ endif NEEDED_CXXFLAGS += -fPIC ifeq ($(USE_STATIC),yes) - LIBDIR := /usr/lib + #LIBDIR = /usr/lib LDLIBS = $(LIBDIR)/libboost_system.a LDLIBS += $(LIBDIR)/libboost_date_time.a LDLIBS += $(LIBDIR)/libboost_filesystem.a LDLIBS += $(LIBDIR)/libboost_program_options.a - LDLIBS += $(LIBDIR)/libcrypto.a LDLIBS += $(LIBDIR)/libssl.a + LDLIBS += $(LIBDIR)/libcrypto.a LDLIBS += $(LIBDIR)/libz.a - LDLIBS += -lpthread -static-libstdc++ -static-libgcc + LDLIBS += -lpthread -static-libstdc++ -static-libgcc -lrt USE_AESNI := no else LDLIBS = -lcrypto -lssl -lz -lboost_system -lboost_date_time -lboost_filesystem -lboost_program_options -lpthread diff --git a/Transports.cpp b/Transports.cpp index 52b1d261..be27ffd1 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -105,7 +105,7 @@ namespace transport Stop (); } - void Transports::Start () + void Transports::Start (bool enableNTCP, bool enableSSU) { m_DHKeysPairSupplier.Start (); m_IsRunning = true; @@ -114,7 +114,7 @@ namespace transport auto& addresses = context.GetRouterInfo ().GetAddresses (); for (auto address : addresses) { - if (!m_NTCPServer) + if (!m_NTCPServer && enableNTCP) { m_NTCPServer = new NTCPServer (); m_NTCPServer->Start (); @@ -129,7 +129,7 @@ namespace transport if (address->transportStyle == RouterInfo::eTransportSSU && address->host.is_v4 ()) { - if (!m_SSUServer) + if (!m_SSUServer && enableSSU) { m_SSUServer = new SSUServer (address->port); LogPrint (eLogInfo, "Transports: Start listening UDP port ", address->port); diff --git a/Transports.h b/Transports.h index 0e1b621b..a7e7dc1d 100644 --- a/Transports.h +++ b/Transports.h @@ -73,7 +73,7 @@ namespace transport Transports (); ~Transports (); - void Start (); + void Start (bool enableNTCP=true, bool enableSSU=true); void Stop (); bool IsBoundNTCP() const { return m_NTCPServer != nullptr; }