From 999abd517cadd15c89af1c72ef33480f98b60d44 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 14 Jul 2014 22:06:58 -0400 Subject: [PATCH 01/16] save out-of-sequence fragments --- SSU.h | 1 - SSUData.cpp | 137 +++++++++++++++++++++++++++++----------------------- SSUData.h | 39 ++++++++++++--- 3 files changed, 109 insertions(+), 68 deletions(-) diff --git a/SSU.h b/SSU.h index f938a580..a64b555c 100644 --- a/SSU.h +++ b/SSU.h @@ -31,7 +31,6 @@ namespace ssu }; #pragma pack() - const size_t SSU_MTU = 1484; const int SSU_CONNECT_TIMEOUT = 5; // 5 seconds const int SSU_TERMINATION_TIMEOUT = 330; // 5.5 minutes diff --git a/SSUData.cpp b/SSUData.cpp index 46569c5d..fb887ea7 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -82,73 +82,90 @@ namespace ssu bool isLast = fragmentInfo & 0x010000; // bit 16 uint8_t fragmentNum = fragmentInfo >> 17; // bits 23 - 17 LogPrint ("SSU data fragment ", (int)fragmentNum, " of message ", msgID, " size=", (int)fragmentSize, isLast ? " last" : " non-last"); - I2NPMessage * msg = nullptr; - if (fragmentNum > 0) // follow-up fragment - { - auto it = m_IncomleteMessages.find (msgID); - if (it != m_IncomleteMessages.end ()) - { - if (fragmentNum == it->second->nextFragmentNum) - { - // expected fragment - msg = it->second->msg; - memcpy (msg->buf + msg->len, buf, fragmentSize); - msg->len += fragmentSize; - it->second->nextFragmentNum++; - } - else if (fragmentNum < it->second->nextFragmentNum) - // duplicate fragment - LogPrint ("Duplicate fragment ", (int)fragmentNum, " of message ", msgID, ". Ignored"); - else - { - // missing fragment - LogPrint ("Missing fragments from ", it->second->nextFragmentNum, " to ", fragmentNum - 1, " of message ", msgID); - //TODO - } - - if (isLast) - { - if (!msg) - DeleteI2NPMessage (it->second->msg); - delete it->second; - m_IncomleteMessages.erase (it); - } - } - else - // TODO: - LogPrint ("Unexpected follow-on fragment ", (int)fragmentNum, " of message ", msgID); - } - else // first fragment - { - msg = NewI2NPMessage (); - memcpy (msg->GetSSUHeader (), buf, fragmentSize); - msg->len += fragmentSize - sizeof (I2NPHeaderShort); - } - if (msg) - { - if (!fragmentNum && !isLast) - m_IncomleteMessages[msgID] = new IncompleteMessage (msg); - if (isLast) + // find message with msgID + I2NPMessage * msg = nullptr; + IncompleteMessage * incompleteMessage = nullptr; + auto it = m_IncomleteMessages.find (msgID); + if (it != m_IncomleteMessages.end ()) + { + // message exists + incompleteMessage = it->second; + msg = incompleteMessage->msg; + } + else + { + // create new message + msg = NewI2NPMessage (); + msg->len -= sizeof (I2NPHeaderShort); + incompleteMessage = new IncompleteMessage (msg); + m_IncomleteMessages[msgID] = incompleteMessage; + } + + // handle current fragment + if (fragmentNum == incompleteMessage->nextFragmentNum) + { + // expected fragment + memcpy (msg->buf + msg->len, buf, fragmentSize); + msg->len += fragmentSize; + incompleteMessage->nextFragmentNum++; + if (!isLast && !incompleteMessage->savedFragments.empty ()) { - SendMsgAck (msgID); - msg->FromSSU (msgID); - if (m_Session.GetState () == eSessionStateEstablished) - i2p::HandleI2NPMessage (msg); - else + // try saved fragments + for (auto it1 = incompleteMessage->savedFragments.begin (); it1 != incompleteMessage->savedFragments.end ();) { - // we expect DeliveryStatus - if (msg->GetHeader ()->typeID == eI2NPDeliveryStatus) + auto savedFragment = *it1; + if (savedFragment->fragmentNum == incompleteMessage->nextFragmentNum) { - LogPrint ("SSU session established"); - m_Session.Established (); - } + memcpy (msg->buf + msg->len, savedFragment->buf, savedFragment->len); + msg->len += savedFragment->len; + isLast = savedFragment->isLast; + incompleteMessage->nextFragmentNum++; + incompleteMessage->savedFragments.erase (it1++); + delete savedFragment; + } else - LogPrint ("SSU unexpected message ", (int)msg->GetHeader ()->typeID); - DeleteI2NPMessage (msg); + break; } + } + } + else + { + if (fragmentNum < incompleteMessage->nextFragmentNum) + // duplicate fragment + LogPrint ("Duplicate fragment ", (int)fragmentNum, " of message ", msgID, ". Ignored"); + else + { + // missing fragment + LogPrint ("Missing fragments from ", (int)incompleteMessage->nextFragmentNum, " to ", fragmentNum - 1, " of message ", msgID); + incompleteMessage->savedFragments.insert (new Fragment (fragmentNum, buf, fragmentSize, isLast)); } - } + isLast = false; + } + + if (isLast) + { + // delete incomplete message + delete incompleteMessage; + m_IncomleteMessages.erase (msgID); + // process message + SendMsgAck (msgID); + msg->FromSSU (msgID); + if (m_Session.GetState () == eSessionStateEstablished) + i2p::HandleI2NPMessage (msg); + else + { + // we expect DeliveryStatus + if (msg->GetHeader ()->typeID == eI2NPDeliveryStatus) + { + LogPrint ("SSU session established"); + m_Session.Established (); + } + else + LogPrint ("SSU unexpected message ", (int)msg->GetHeader ()->typeID); + DeleteI2NPMessage (msg); + } + } buf += fragmentSize; } } diff --git a/SSUData.h b/SSUData.h index 4829aa56..fb04879d 100644 --- a/SSUData.h +++ b/SSUData.h @@ -2,8 +2,10 @@ #define SSU_DATA_H__ #include +#include #include #include +#include #include "I2NPProtocol.h" namespace i2p @@ -11,6 +13,7 @@ namespace i2p namespace ssu { + const size_t SSU_MTU = 1484; // data flags const uint8_t DATA_FLAG_EXTENDED_DATA_INCLUDED = 0x02; const uint8_t DATA_FLAG_WANT_REPLY = 0x04; @@ -19,6 +22,34 @@ namespace ssu const uint8_t DATA_FLAG_ACK_BITFIELDS_INCLUDED = 0x40; const uint8_t DATA_FLAG_EXPLICIT_ACKS_INCLUDED = 0x80; + struct Fragment + { + int fragmentNum, len; + bool isLast; + uint8_t buf[SSU_MTU]; + + Fragment (int n, const uint8_t * b, int l, bool last): + fragmentNum (n), len (l), isLast (last) { memcpy (buf, b, len); }; + }; + + struct FragmentCmp + { + bool operator() (const Fragment * f1, const Fragment * f2) const + { + return f1->fragmentNum < f2->fragmentNum; + }; + }; + + struct IncompleteMessage + { + I2NPMessage * msg; + int nextFragmentNum; + std::set savedFragments; + + IncompleteMessage (I2NPMessage * m): msg (m), nextFragmentNum (0) {}; + ~IncompleteMessage () { for (auto it: savedFragments) { delete it; }; }; + }; + class SSUSession; class SSUData { @@ -37,13 +68,7 @@ namespace ssu private: - struct IncompleteMessage - { - I2NPMessage * msg; - uint8_t nextFragmentNum; - - IncompleteMessage (I2NPMessage * m): msg (m), nextFragmentNum (1) {}; - }; + SSUSession& m_Session; std::map m_IncomleteMessages; From 05a96fd3980219251ebb1fd2b38ede3ffc49119b Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 15 Jul 2014 08:03:45 -0400 Subject: [PATCH 02/16] fixed memory leak --- SSUData.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/SSUData.cpp b/SSUData.cpp index fb887ea7..64887602 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -126,7 +126,9 @@ namespace ssu } else break; - } + } + if (isLast) + LogPrint ("Message ", msgID, " complete"); } } else @@ -138,7 +140,12 @@ namespace ssu { // missing fragment LogPrint ("Missing fragments from ", (int)incompleteMessage->nextFragmentNum, " to ", fragmentNum - 1, " of message ", msgID); - incompleteMessage->savedFragments.insert (new Fragment (fragmentNum, buf, fragmentSize, isLast)); + auto savedFragment = new Fragment (fragmentNum, buf, fragmentSize, isLast); + if (!incompleteMessage->savedFragments.insert (savedFragment).second) + { + LogPrint ("Fragment ", (int)fragmentNum, " of message ", msgID, " already saved"); + delete savedFragment; + } } isLast = false; } From 2f601ce02f634432c117eb75b0005f9d93e1572b Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 15 Jul 2014 14:49:54 -0400 Subject: [PATCH 03/16] send ack per fragment. temporary disble check for duplicated through IV --- SSU.cpp | 4 ++-- SSUData.cpp | 35 ++++++++++++++++++++++++++++++++++- SSUData.h | 3 ++- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/SSU.cpp b/SSU.cpp index 8c2115f8..9660023d 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -84,10 +84,10 @@ namespace ssu else { ScheduleTermination (); - // check for duplicate + /* // check for duplicate const uint8_t * iv = ((SSUHeader *)buf)->iv; if (m_ReceivedIVs.count (iv)) return; // duplicate detected - m_ReceivedIVs.insert (iv); + m_ReceivedIVs.insert (iv);*/ if (m_IsSessionKey && Validate (buf, len, m_MacKey)) // try session key first DecryptSessionKey (buf, len); diff --git a/SSUData.cpp b/SSUData.cpp index 64887602..75a1df41 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -1,3 +1,4 @@ +#include #include "Log.h" #include "SSU.h" #include "SSUData.h" @@ -172,7 +173,9 @@ namespace ssu LogPrint ("SSU unexpected message ", (int)msg->GetHeader ()->typeID); DeleteI2NPMessage (msg); } - } + } + else + SendFragmentAck (msgID, fragmentNum); buf += fragmentSize; } } @@ -252,6 +255,36 @@ namespace ssu m_Session.FillHeaderAndEncrypt (PAYLOAD_TYPE_DATA, buf, 48); m_Session.Send (buf, 48); } + + void SSUData::SendFragmentAck (uint32_t msgID, int fragmentNum) + { + if (fragmentNum > 64) + { + LogPrint ("Fragment number ", fragmentNum, " exceeds 64"); + return; + } + uint8_t buf[64 + 18]; + uint8_t * payload = buf + sizeof (SSUHeader); + *payload = DATA_FLAG_ACK_BITFIELDS_INCLUDED; // flag + payload++; + *payload = 1; // number of ACK bitfields + payload++; + // one ack + *(uint32_t *)(payload) = htobe32 (msgID); // msgID + payload += 4; + div_t d = div (fragmentNum, 7); + memset (payload, 0x80, d.quot); // 0x80 means non-last + payload += d.quot; + *payload = 0x40 >> d.rem; // set corresponding bit + payload++; + *payload = 0; // number of fragments + + size_t len = d.quot < 4 ? 48 : 64; // 48 = 37 + 7 + 4 (3+1) + // encrypt message with session key + m_Session.FillHeaderAndEncrypt (PAYLOAD_TYPE_DATA, buf, len); + m_Session.Send (buf, len); + } + } } diff --git a/SSUData.h b/SSUData.h index fb04879d..603f50b8 100644 --- a/SSUData.h +++ b/SSUData.h @@ -64,7 +64,8 @@ namespace ssu private: void SendMsgAck (uint32_t msgID); - void ProcessSentMessageAck (uint32_t msgID); + void SendFragmentAck (uint32_t msgID, int fragmentNum); + void ProcessSentMessageAck (uint32_t msgID); private: From 9ee049aa63953fb642d4ea36bd20bbf41c383134 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 15 Jul 2014 21:40:44 -0400 Subject: [PATCH 04/16] close SSU session if not established --- SSU.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SSU.cpp b/SSU.cpp index 9660023d..63528e96 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -83,7 +83,8 @@ namespace ssu } else { - ScheduleTermination (); + if (m_State == eSessionStateEstablished) + ScheduleTermination (); /* // check for duplicate const uint8_t * iv = ((SSUHeader *)buf)->iv; if (m_ReceivedIVs.count (iv)) return; // duplicate detected @@ -652,7 +653,6 @@ namespace ssu if (m_State != eSessionStateFailed) { m_State = eSessionStateFailed; - Close (); m_Server.DeleteSession (this); // delete this } } From bff3d8f5c1660d199f3fd80b92e6e9a5553f098d Mon Sep 17 00:00:00 2001 From: Mikal Villa Date: Wed, 16 Jul 2014 18:41:40 +0200 Subject: [PATCH 05/16] Prepare support for POST/PUT --- HTTPServer.cpp | 112 +++++++++++++++++++++++++++---------------------- HTTPServer.h | 26 ++++++------ 2 files changed, 75 insertions(+), 63 deletions(-) diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 038f6321..98ac5950 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -13,7 +13,7 @@ namespace i2p namespace util { - const std::string HTTPConnection::itoopieImage = + const std::string HTTPConnection::itoopieImage = "\"\""; - - namespace misc_strings + + namespace misc_strings { const char name_value_separator[] = { ':', ' ' }; @@ -146,7 +146,7 @@ namespace util buffers.push_back(boost::asio::buffer(misc_strings::crlf)); } buffers.push_back(boost::asio::buffer(misc_strings::crlf)); - } + } buffers.push_back(boost::asio::buffer(content)); return buffers; } @@ -154,9 +154,9 @@ namespace util void HTTPConnection::Terminate () { if (m_Stream) - { + { m_Stream->Close (); - DeleteStream (m_Stream); + DeleteStream (m_Stream); } m_Socket->close (); delete this; @@ -193,11 +193,11 @@ namespace util { b32 = address.substr (1, pos - 1); // excluding leading '/' to next '/' uri = address.substr (pos); // rest of line - } + } - HandleDestinationRequest (b32, uri); - } - else + HandleDestinationRequest (b32, uri); + } + else HandleRequest (); } @@ -209,10 +209,10 @@ namespace util char * http = strstr (get, "HTTP"); if (http) return std::string (get + 4, http - get - 5); - } + } return ""; - } - + } + void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode) { Terminate (); @@ -233,7 +233,7 @@ namespace util FillContent (s); s << ""; SendReply (s.str ()); - } + } void HTTPConnection::FillContent (std::stringstream& s) { @@ -241,7 +241,7 @@ namespace util s << "Our external address:" << "
"; for (auto& address : i2p::context.GetRouterInfo().GetAddresses()) { - switch (address.transportStyle) + switch (address.transportStyle) { case i2p::data::RouterInfo::eTransportNTCP: s << "NTCP  "; @@ -257,31 +257,31 @@ namespace util s << "
Routers: " << i2p::data::netdb.GetNumRouters () << " "; s << "Floodfills: " << i2p::data::netdb.GetNumFloodfills () << " "; s << "LeaseSets: " << i2p::data::netdb.GetNumLeaseSets () << "
"; - + s << "

Tunnels

"; for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ()) - { + { it->GetTunnelConfig ()->Print (s); if (it->GetTunnelPool () && !it->GetTunnelPool ()->IsExploratory ()) s << " " << "Pool"; if (it->IsFailed ()) s << " " << "Failed"; s << " " << (int)it->GetNumSentBytes () << "
"; - } + } for (auto it: i2p::tunnel::tunnels.GetInboundTunnels ()) - { + { it.second->GetTunnelConfig ()->Print (s); if (it.second->GetTunnelPool () && !it.second->GetTunnelPool ()->IsExploratory ()) s << " " << "Pool"; if (it.second->IsFailed ()) s << " " << "Failed"; s << " " << (int)it.second->GetNumReceivedBytes () << "
"; - } - + } + s << "

Transit tunnels

"; for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ()) - { + { if (dynamic_cast(it.second)) s << it.second->GetTunnelID () << "-->"; else if (dynamic_cast(it.second)) @@ -289,23 +289,23 @@ namespace util else s << "-->" << it.second->GetTunnelID () << "-->"; s << " " << it.second->GetNumTransmittedBytes () << "
"; - } + } s << "

Transports

"; s << "NTCP
"; for (auto it: i2p::transports.GetNTCPSessions ()) - { + { // RouterInfo of incoming connection doesn't have address bool outgoing = it.second->GetRemoteRouterInfo ().GetNTCPAddress (); if (it.second->IsEstablished ()) { if (outgoing) s << "-->"; - s << it.second->GetRemoteRouterInfo ().GetIdentHashAbbreviation () << ": " + s << it.second->GetRemoteRouterInfo ().GetIdentHashAbbreviation () << ": " << it.second->GetSocket ().remote_endpoint().address ().to_string (); if (!outgoing) s << "-->"; s << "
"; - } - } + } + } auto ssuServer = i2p::transports.GetSSUServer (); if (ssuServer) { @@ -319,12 +319,16 @@ namespace util s << endpoint.address ().to_string () << ":" << endpoint.port (); if (!outgoing) s << "-->"; s << "
"; - } - } + } + } s << "

Flibusta

"; - } - + } void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& uri) + { + HandleDestinationRequest(address, "GET", "", uri); + } + + void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& method, const std::string& data, const std::string& uri) { i2p::data::IdentHash destination; std::string fullAddress; @@ -363,7 +367,7 @@ namespace util fullAddress = address + ".b32.i2p"; } } - + auto leaseSet = i2p::data::netdb.FindLeaseSet (destination); if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) { @@ -374,23 +378,29 @@ namespace util { SendReply (leaseSet ? "" + itoopieImage + "
Leases expired" : "" + itoopieImage + "LeaseSet not found", 504); return; - } + } } - if (!m_Stream) + if (!m_Stream) m_Stream = i2p::stream::CreateStream (*leaseSet); if (m_Stream) { - std::string request = "GET " + uri + " HTTP/1.1\n Host:" + fullAddress + "\n"; - m_Stream->Send ((uint8_t *)request.c_str (), request.length (), 10); + std::string request = method+" " + uri + " HTTP/1.1\n Host:" + fullAddress + "\r\n"; + if (!strcmp(method.c_str(), "GET")) + { + // POST/PUT, apply body + request += "\r\n"+ data; + } + LogPrint("HTTP Client Request: ", request); + m_Stream->Send ((uint8_t *)request.c_str (), request.length (), 10); AsyncStreamReceive (); - } - } - + } + } + void HTTPConnection::AsyncStreamReceive () { if (m_Stream) m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192), - boost::bind (&HTTPConnection::HandleStreamReceive, this, + boost::bind (&HTTPConnection::HandleStreamReceive, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred), 45); // 45 seconds timeout } @@ -408,7 +418,7 @@ namespace util SendReply ("" + itoopieImage + "
Not responding", 504); else Terminate (); - } + } } void HTTPConnection::SendReply (const std::string& content, int status) @@ -421,16 +431,16 @@ namespace util m_Reply.headers[1].value = "text/html"; boost::asio::async_write (*m_Socket, m_Reply.to_buffers(status), - boost::bind (&HTTPConnection::HandleWriteReply, this, + boost::bind (&HTTPConnection::HandleWriteReply, this, boost::asio::placeholders::error)); } - - HTTPServer::HTTPServer (int port): - m_Thread (nullptr), m_Work (m_Service), + + HTTPServer::HTTPServer (int port): + m_Thread (nullptr), m_Work (m_Service), m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_NewSocket (nullptr) { - + } HTTPServer::~HTTPServer () @@ -450,17 +460,17 @@ namespace util m_Acceptor.close(); m_Service.stop (); if (m_Thread) - { - m_Thread->join (); + { + m_Thread->join (); delete m_Thread; m_Thread = nullptr; - } + } } void HTTPServer::Run () { m_Service.run (); - } + } void HTTPServer::Accept () { @@ -476,7 +486,7 @@ namespace util CreateConnection(m_NewSocket); // new HTTPConnection(m_NewSocket); Accept (); } - } + } void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket) { diff --git a/HTTPServer.h b/HTTPServer.h index cb594794..39c61070 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -14,13 +14,13 @@ namespace util class HTTPConnection { protected: - + struct header { std::string name; std::string value; }; - + struct request { std::string method; @@ -38,7 +38,7 @@ namespace util std::vector to_buffers (int status); }; - + public: HTTPConnection (boost::asio::ip::tcp::socket * socket): m_Socket (socket), m_Stream (nullptr) { Receive (); }; @@ -48,9 +48,9 @@ namespace util void Terminate (); void Receive (); - void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); + void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void AsyncStreamReceive (); - void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); + void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleWriteReply(const boost::system::error_code& ecode); void HandleWrite (const boost::system::error_code& ecode); void SendReply (const std::string& content, int status = 200); @@ -58,9 +58,9 @@ namespace util void HandleRequest (); void FillContent (std::stringstream& s); std::string ExtractAddress (); - + protected: - + boost::asio::ip::tcp::socket * m_Socket; i2p::stream::Stream * m_Stream; char m_Buffer[8192], m_StreamBuffer[8192]; @@ -68,14 +68,16 @@ namespace util reply m_Reply; protected: - + + virtual void HandleDestinationRequest(const std::string& address, const std::string& uri); + virtual void HandleDestinationRequest(const std::string& address, const std::string& method, const std::string& data, const std::string& uri); virtual void RunRequest (); private: static const std::string itoopieImage; - }; + }; class HTTPServer { @@ -89,9 +91,9 @@ namespace util private: - void Run (); + void Run (); void Accept (); - void HandleAccept(const boost::system::error_code& ecode); + void HandleAccept(const boost::system::error_code& ecode); private: @@ -103,7 +105,7 @@ namespace util protected: virtual void CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket); - }; + }; } } From 1ec8728ab056342ccb014515e030950ce51992af Mon Sep 17 00:00:00 2001 From: Mikal Villa Date: Wed, 16 Jul 2014 18:59:17 +0200 Subject: [PATCH 06/16] Makefile now detects which file to use (OSX or Linux) Issue #82 --- Makefile | 39 +++++---------------------------------- Makefile.linux | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 Makefile.linux diff --git a/Makefile b/Makefile index 5c04f3fd..8c426254 100644 --- a/Makefile +++ b/Makefile @@ -1,38 +1,9 @@ -CC = g++ -CFLAGS = -g -Wall -std=c++0x -OBJECTS = obj/CryptoConst.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/Transports.o \ - obj/RouterContext.o obj/NetDb.o obj/LeaseSet.o obj/Tunnel.o obj/TunnelEndpoint.o \ - obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \ - obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o \ - obj/UPnP.o obj/TunnelPool.o obj/HTTPProxy.o obj/AddressBook.o obj/Daemon.o \ - obj/DaemonLinux.o obj/SSUData.o obj/i2p.o obj/aes.o obj/SOCKS.o -INCFLAGS = -LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread -LIBS = +UNAME := $(shell uname -s) -#check if AES-NI is supported by CPU -ifneq ($(shell grep -c aes /proc/cpuinfo),0) - CPU_FLAGS = -DAESNI +ifeq ($(UNAME),Darwin) + include Makefile.osx +else + include Makefile.linux endif -all: obj i2p - -i2p: $(OBJECTS:obj/%=obj/%) - $(CC) -o $@ $^ $(LDFLAGS) $(LIBS) - -.SUFFIXES: -.SUFFIXES: .c .cc .C .cpp .o - -obj/%.o : %.cpp - $(CC) -o $@ $< -c $(CFLAGS) $(INCFLAGS) $(CPU_FLAGS) - -obj: - mkdir -p obj - -clean: - rm -fr obj i2p - -.PHONY: all -.PHONY: clean - diff --git a/Makefile.linux b/Makefile.linux new file mode 100644 index 00000000..5c04f3fd --- /dev/null +++ b/Makefile.linux @@ -0,0 +1,38 @@ + +CC = g++ +CFLAGS = -g -Wall -std=c++0x +OBJECTS = obj/CryptoConst.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/Transports.o \ + obj/RouterContext.o obj/NetDb.o obj/LeaseSet.o obj/Tunnel.o obj/TunnelEndpoint.o \ + obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \ + obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o \ + obj/UPnP.o obj/TunnelPool.o obj/HTTPProxy.o obj/AddressBook.o obj/Daemon.o \ + obj/DaemonLinux.o obj/SSUData.o obj/i2p.o obj/aes.o obj/SOCKS.o +INCFLAGS = +LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread +LIBS = + +#check if AES-NI is supported by CPU +ifneq ($(shell grep -c aes /proc/cpuinfo),0) + CPU_FLAGS = -DAESNI +endif + +all: obj i2p + +i2p: $(OBJECTS:obj/%=obj/%) + $(CC) -o $@ $^ $(LDFLAGS) $(LIBS) + +.SUFFIXES: +.SUFFIXES: .c .cc .C .cpp .o + +obj/%.o : %.cpp + $(CC) -o $@ $< -c $(CFLAGS) $(INCFLAGS) $(CPU_FLAGS) + +obj: + mkdir -p obj + +clean: + rm -fr obj i2p + +.PHONY: all +.PHONY: clean + From d2160efdc8fea10fd1e4c768762e5d7ba05e1159 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 17 Jul 2014 07:40:34 -0400 Subject: [PATCH 07/16] create inbound tunnel though outbound --- TunnelPool.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TunnelPool.cpp b/TunnelPool.cpp index e04cf501..7f65c731 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -188,7 +188,7 @@ namespace tunnel hops.push_back (hop); } std::reverse (hops.begin (), hops.end ()); - auto * tunnel = tunnels.CreateTunnel (new TunnelConfig (hops)); + auto * tunnel = tunnels.CreateTunnel (new TunnelConfig (hops), outboundTunnel); tunnel->SetTunnelPool (this); } From c5860a62543353106140ac8845fe11f6c47e34a3 Mon Sep 17 00:00:00 2001 From: Mikal Villa Date: Thu, 17 Jul 2014 19:02:05 +0200 Subject: [PATCH 08/16] Removing Qt build file. Issue #82 --- build/i2pd.pro | 85 -------------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 build/i2pd.pro diff --git a/build/i2pd.pro b/build/i2pd.pro deleted file mode 100644 index 713e6c7d..00000000 --- a/build/i2pd.pro +++ /dev/null @@ -1,85 +0,0 @@ -TEMPLATE = app -CONFIG += console -CONFIG -= app_bundle -CONFIG -= qt - -TARGET = ./../../i2pd_qt - -QMAKE_CXXFLAGS += -std=c++0x - -LIBS += -lcrypto++ -LIBS += \ - -lboost_system\ - -lboost_filesystem\ - -lboost_regex\ - -lboost_program_options\ - -lpthread - -SOURCES += \ - ../LeaseSet.cpp \ - ../i2p.cpp \ - ../HTTPServer.cpp \ - ../HTTPProxy.cpp \ - ../Garlic.cpp \ - ../base64.cpp \ - ../AddressBook.cpp \ - ../util.cpp \ - ../UPnP.cpp \ - ../TunnelPool.cpp \ - ../TunnelGateway.cpp \ - ../TunnelEndpoint.cpp \ - ../Tunnel.cpp \ - ../Transports.cpp \ - ../TransitTunnel.cpp \ - ../Streaming.cpp \ - ../SSU.cpp \ - ../RouterInfo.cpp \ - ../RouterContext.cpp \ - ../Reseed.cpp \ - ../NTCPSession.cpp \ - ../NetDb.cpp \ - ../Log.cpp \ - ../Identity.cpp \ - ../I2NPProtocol.cpp \ - ../SOCKS.cpp - -HEADERS += \ - ../LeaseSet.h \ - ../Identity.h \ - ../HTTPServer.h \ - ../HTTPProxy.h \ - ../hmac.h \ - ../Garlic.h \ - ../ElGamal.h \ - ../CryptoConst.h \ - ../base64.h \ - ../AddressBook.h \ - ../util.h \ - ../UPnP.h \ - ../TunnelPool.h \ - ../TunnelGateway.h \ - ../TunnelEndpoint.h \ - ../TunnelConfig.h \ - ../TunnelBase.h \ - ../Tunnel.h \ - ../Transports.h \ - ../TransitTunnel.h \ - ../Timestamp.h \ - ../Streaming.h \ - ../SSU.h \ - ../RouterInfo.h \ - ../RouterContext.h \ - ../Reseed.h \ - ../Queue.h \ - ../NTCPSession.h \ - ../NetDb.h \ - ../Log.h \ - ../LittleBigEndian.h \ - ../I2PEndian.h \ - ../I2NPProtocol.h \ - ../SOCKS.h - -OTHER_FILES += \ - ../README.md \ - ../Makefile \ - ../LICENSE From 02296ef775fa4afe5d4049906fac0b99bae14b8d Mon Sep 17 00:00:00 2001 From: Mikal Villa Date: Thu, 17 Jul 2014 19:12:52 +0200 Subject: [PATCH 09/16] Moving file list to a common one. Still in makefile format, but now with CPP/H as input instead of OBJECTS. Issue #82 --- Makefile.linux | 7 +------ Makefile.osx | 7 +------ filelist.mk | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 filelist.mk diff --git a/Makefile.linux b/Makefile.linux index 5c04f3fd..2ebc1889 100644 --- a/Makefile.linux +++ b/Makefile.linux @@ -1,12 +1,7 @@ CC = g++ CFLAGS = -g -Wall -std=c++0x -OBJECTS = obj/CryptoConst.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/Transports.o \ - obj/RouterContext.o obj/NetDb.o obj/LeaseSet.o obj/Tunnel.o obj/TunnelEndpoint.o \ - obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \ - obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o \ - obj/UPnP.o obj/TunnelPool.o obj/HTTPProxy.o obj/AddressBook.o obj/Daemon.o \ - obj/DaemonLinux.o obj/SSUData.o obj/i2p.o obj/aes.o obj/SOCKS.o +include filelist.mk INCFLAGS = LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread LIBS = diff --git a/Makefile.osx b/Makefile.osx index 494898fd..1c48d2a6 100644 --- a/Makefile.osx +++ b/Makefile.osx @@ -1,11 +1,6 @@ CC = clang++ CFLAGS = -g -Wall -std=c++11 -lstdc++ -I/usr/local/include -OBJECTS = obj/CryptoConst.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/Transports.o \ - obj/RouterContext.o obj/NetDb.o obj/LeaseSet.o obj/Tunnel.o obj/TunnelEndpoint.o \ - obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \ - obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o \ - obj/UPnP.o obj/TunnelPool.o obj/HTTPProxy.o obj/AddressBook.o obj/Daemon.o \ - obj/DaemonLinux.o obj/SSUData.o obj/i2p.o obj/aes.o obj/SOCKS.o +include filelist.mk INCFLAGS = -DCRYPTOPP_DISABLE_ASM LDFLAGS = -Wl,-rpath,/usr/local/lib -L/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread LIBS = diff --git a/filelist.mk b/filelist.mk new file mode 100644 index 00000000..4ebf7c09 --- /dev/null +++ b/filelist.mk @@ -0,0 +1,18 @@ + + +CPP_FILES := CryptoConst.cpp base64.cpp NTCPSession.cpp RouterInfo.cpp Transports.cpp \ + RouterContext.cpp NetDb.cpp LeaseSet.cpp Tunnel.cpp TunnelEndpoint.cpp TunnelGateway.cpp \ + TransitTunnel.cpp I2NPProtocol.cpp Log.cpp Garlic.cpp HTTPServer.cpp Streaming.cpp Identity.cpp \ + SSU.cpp util.cpp Reseed.cpp DaemonLinux.cpp SSUData.cpp i2p.cpp aes.cpp SOCKS.cpp UPnP.cpp \ + TunnelPool.cpp HTTPProxy.cpp AddressBook.cpp Daemon.cpp + + +H_FILES := CryptoConst.h base64.h NTCPSession.h RouterInfo.h Transports.h \ + RouterContext.h NetDb.h LeaseSet.h Tunnel.h TunnelEndpoint.h TunnelGateway.h \ + TransitTunnel.h I2NPProtocol.h Log.h Garlic.h HTTPServer.h Streaming.h Identity.h \ + SSU.h util.h Reseed.h DaemonLinux.h SSUData.h i2p.h aes.h SOCKS.h UPnP.h TunnelPool.h \ + HTTPProxy.h AddressBook.h Daemon.h + + +OBJECTS = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) + From 275155998fdd45c0f27d56a8967adf83f5afe05a Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 17 Jul 2014 15:00:33 -0400 Subject: [PATCH 10/16] handle individaul bitfields ack --- SSUData.cpp | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/SSUData.cpp b/SSUData.cpp index 75a1df41..d6b9e6e7 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -61,11 +61,39 @@ namespace ssu buf++; for (int i = 0; i < numBitfields; i++) { + uint32_t msgID = be32toh (*(uint32_t *)buf); buf += 4; // msgID - // TODO: process individual Ack bitfields - while (*buf & 0x80) // not last + auto it = m_SentMessages.find (msgID); + int numSentFragments = it->second.size (); + // process individual Ack bitfields + bool isNonLast = false; + int fragment = 0; + do + { + uint8_t bitfield = *buf; + isNonLast = bitfield & 0x80; + bitfield &= 0x7F; // clear MSB + if (bitfield && it != m_SentMessages.end ()) + { + // process bits + uint8_t mask = 0x40; + for (int j = 0; j < 7; j++) + { + if (bitfield & mask) + { + if (fragment < numSentFragments) + { + delete it->second[fragment]; + it->second[fragment] = nullptr; + } + } + fragment++; + mask >>= 1; + } + } buf++; - buf++; // last byte + } + while (isNonLast); } } uint8_t numFragments = *buf; // number of fragments From 05d869254df909f41889eb40d29cfa07129197c9 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 17 Jul 2014 15:22:32 -0400 Subject: [PATCH 11/16] process extended data --- SSUData.cpp | 35 ++++++++++++++++++++++++++++------- SSUData.h | 2 ++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/SSUData.cpp b/SSUData.cpp index d6b9e6e7..bf9135cc 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -39,12 +39,8 @@ namespace ssu } } - void SSUData::ProcessMessage (uint8_t * buf, size_t len) + void SSUData::ProcessAcks (uint8_t *& buf, uint8_t flag) { - //uint8_t * start = buf; - uint8_t flag = *buf; - buf++; - LogPrint ("Process SSU data flags=", (int)flag); if (flag & DATA_FLAG_EXPLICIT_ACKS_INCLUDED) { // explicit ACKs @@ -83,7 +79,7 @@ namespace ssu { if (fragment < numSentFragments) { - delete it->second[fragment]; + delete[] it->second[fragment]; it->second[fragment] = nullptr; } } @@ -95,7 +91,11 @@ namespace ssu } while (isNonLast); } - } + } + } + + void SSUData::ProcessFragments (uint8_t * buf) + { uint8_t numFragments = *buf; // number of fragments buf++; for (int i = 0; i < numFragments; i++) @@ -208,6 +208,27 @@ namespace ssu } } + void SSUData::ProcessMessage (uint8_t * buf, size_t len) + { + //uint8_t * start = buf; + uint8_t flag = *buf; + buf++; + LogPrint ("Process SSU data flags=", (int)flag); + // process acks if presented + if (flag & (DATA_FLAG_ACK_BITFIELDS_INCLUDED | DATA_FLAG_EXPLICIT_ACKS_INCLUDED)) + ProcessAcks (buf, flag); + // extended data if presented + if (flag & DATA_FLAG_EXTENDED_DATA_INCLUDED) + { + uint8_t extendedDataSize = *buf; + buf++; // size + LogPrint ("SSU extended data of ", extendedDataSize, " bytes presented"); + buf += extendedDataSize; + } + // process data + ProcessFragments (buf); + } + void SSUData::Send (i2p::I2NPMessage * msg) { uint32_t msgID = msg->ToSSU (); diff --git a/SSUData.h b/SSUData.h index 603f50b8..9cc548e0 100644 --- a/SSUData.h +++ b/SSUData.h @@ -65,6 +65,8 @@ namespace ssu void SendMsgAck (uint32_t msgID); void SendFragmentAck (uint32_t msgID, int fragmentNum); + void ProcessAcks (uint8_t *& buf, uint8_t flag); + void ProcessFragments (uint8_t * buf); void ProcessSentMessageAck (uint32_t msgID); private: From 8801a144a0e17082971ebb6522c8830798c66629 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 18 Jul 2014 20:32:45 -0400 Subject: [PATCH 12/16] close connection to first hop of declined tunnel --- I2NPProtocol.cpp | 1 + Transports.cpp | 17 +++++++++++++++++ Transports.h | 6 ++++-- Tunnel.cpp | 4 +++- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index f0833986..dbc095dd 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -359,6 +359,7 @@ namespace i2p else { LogPrint ("Outbound tunnel ", tunnel->GetTunnelID (), " has been declined"); + i2p::transports.CloseSession (tunnel->GetTunnelConfig ()->GetFirstHop ()->router); delete tunnel; } } diff --git a/Transports.cpp b/Transports.cpp index e86ed66e..851e244d 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -263,6 +263,23 @@ namespace i2p } } + void Transports::CloseSession (const i2p::data::RouterInfo * router) + { + if (!router) return; + m_Service.post (boost::bind (&Transports::PostCloseSession, this, router)); + } + + void Transports::PostCloseSession (const i2p::data::RouterInfo * router) + { + auto ssuSession = m_SSUServer ? m_SSUServer->FindSession (router) : nullptr; + if (ssuSession) // try SSU first + { + m_SSUServer->DeleteSession (ssuSession); + LogPrint ("SSU session closed"); + } + // TODO: delete NTCP + } + void Transports::DetectExternalIP () { for (int i = 0; i < 5; i ++) diff --git a/Transports.h b/Transports.h index b0ff8455..e5e6ddfa 100644 --- a/Transports.h +++ b/Transports.h @@ -63,13 +63,15 @@ namespace i2p i2p::ntcp::NTCPSession * FindNTCPSession (const i2p::data::IdentHash& ident); void SendMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); - + void CloseSession (const i2p::data::RouterInfo * router); + private: void Run (); void HandleAccept (i2p::ntcp::NTCPServerConnection * conn, const boost::system::error_code& error); void PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); - + void PostCloseSession (const i2p::data::RouterInfo * router); + void DetectExternalIP (); private: diff --git a/Tunnel.cpp b/Tunnel.cpp index 88bcd528..1ce7ac82 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -361,10 +361,12 @@ namespace tunnel void Tunnels::ManageTunnels () { // check pending tunnel. if something is still there, wipe it out - // because it wouldn't be reponded anyway + // because it wouldn't be responded anyway for (auto& it : m_PendingTunnels) { LogPrint ("Pending tunnel build request ", it.first, " has not been responded. Deleted"); + if (it.second->GetTunnelConfig ()->GetFirstHop ()->isGateway) // outbound + i2p::transports.CloseSession (it.second->GetTunnelConfig ()->GetFirstHop ()->router); delete it.second; } m_PendingTunnels.clear (); From 541a49c35951f3ec7bab2f6df5c7a9c098ec019b Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 19 Jul 2014 18:53:02 -0400 Subject: [PATCH 13/16] fixed memory leak --- SSUData.cpp | 15 ++++++++------- SSUData.h | 9 ++++++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/SSUData.cpp b/SSUData.cpp index bf9135cc..ab0ed45d 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -22,7 +22,7 @@ namespace ssu } for (auto it: m_SentMessages) { - for (auto f: it.second) + for (auto f: it.second.fragments) delete[] f; } } @@ -33,7 +33,7 @@ namespace ssu if (it != m_SentMessages.end ()) { // delete all ack-ed message's fragments - for (auto f: it->second) + for (auto f: it->second.fragments) delete[] f; m_SentMessages.erase (it); } @@ -59,8 +59,7 @@ namespace ssu { uint32_t msgID = be32toh (*(uint32_t *)buf); buf += 4; // msgID - auto it = m_SentMessages.find (msgID); - int numSentFragments = it->second.size (); + auto it = m_SentMessages.find (msgID); // process individual Ack bitfields bool isNonLast = false; int fragment = 0; @@ -71,6 +70,7 @@ namespace ssu bitfield &= 0x7F; // clear MSB if (bitfield && it != m_SentMessages.end ()) { + int numSentFragments = it->second.fragments.size (); // process bits uint8_t mask = 0x40; for (int j = 0; j < 7; j++) @@ -79,8 +79,8 @@ namespace ssu { if (fragment < numSentFragments) { - delete[] it->second[fragment]; - it->second[fragment] = nullptr; + delete[] it->second.fragments[fragment]; + it->second.fragments[fragment] = nullptr; } } fragment++; @@ -238,7 +238,8 @@ namespace ssu DeleteI2NPMessage (msg); return; } - auto fragments = m_SentMessages[msgID]; + SentMessage& sentMessage = m_SentMessages[msgID]; + auto& fragments = sentMessage.fragments; msgID = htobe32 (msgID); size_t payloadSize = SSU_MTU - sizeof (SSUHeader) - 9; // 9 = flag + #frg(1) + messageID(4) + frag info (3) size_t len = msg->GetLength (); diff --git a/SSUData.h b/SSUData.h index 9cc548e0..1cf18eff 100644 --- a/SSUData.h +++ b/SSUData.h @@ -49,6 +49,13 @@ namespace ssu IncompleteMessage (I2NPMessage * m): msg (m), nextFragmentNum (0) {}; ~IncompleteMessage () { for (auto it: savedFragments) { delete it; }; }; }; + + struct SentMessage + { + std::vector fragments; + uint32_t nextResendTime; // in seconds + int numResends; + }; class SSUSession; class SSUData @@ -75,7 +82,7 @@ namespace ssu SSUSession& m_Session; std::map m_IncomleteMessages; - std::map > m_SentMessages; // msgID -> fragments + std::map m_SentMessages; // msgID -> fragments }; } } From d7bcaaa3f7fa123aabcfcabb44eebe8426461cf7 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 20 Jul 2014 10:38:39 -0400 Subject: [PATCH 14/16] resend --- SSUData.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++----------- SSUData.h | 21 ++++++++++---- 2 files changed, 78 insertions(+), 22 deletions(-) diff --git a/SSUData.cpp b/SSUData.cpp index ab0ed45d..f2236cfd 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -1,5 +1,7 @@ #include +#include #include "Log.h" +#include "Timestamp.h" #include "SSU.h" #include "SSUData.h" @@ -8,7 +10,7 @@ namespace i2p namespace ssu { SSUData::SSUData (SSUSession& session): - m_Session (session) + m_Session (session), m_ResendTimer (session.m_Server.GetService ()) { } @@ -21,10 +23,7 @@ namespace ssu delete it.second; } for (auto it: m_SentMessages) - { - for (auto f: it.second.fragments) - delete[] f; - } + delete it.second; } void SSUData::ProcessSentMessageAck (uint32_t msgID) @@ -32,10 +31,10 @@ namespace ssu auto it = m_SentMessages.find (msgID); if (it != m_SentMessages.end ()) { - // delete all ack-ed message's fragments - for (auto f: it->second.fragments) - delete[] f; + delete it->second; m_SentMessages.erase (it); + if (m_SentMessages.empty ()) + m_ResendTimer.cancel (); } } @@ -70,7 +69,7 @@ namespace ssu bitfield &= 0x7F; // clear MSB if (bitfield && it != m_SentMessages.end ()) { - int numSentFragments = it->second.fragments.size (); + int numSentFragments = it->second->fragments.size (); // process bits uint8_t mask = 0x40; for (int j = 0; j < 7; j++) @@ -79,8 +78,8 @@ namespace ssu { if (fragment < numSentFragments) { - delete[] it->second.fragments[fragment]; - it->second.fragments[fragment] = nullptr; + delete it->second->fragments[fragment]; + it->second->fragments[fragment] = nullptr; } } fragment++; @@ -237,9 +236,14 @@ namespace ssu LogPrint ("SSU message ", msgID, " already sent"); DeleteI2NPMessage (msg); return; - } - SentMessage& sentMessage = m_SentMessages[msgID]; - auto& fragments = sentMessage.fragments; + } + if (m_SentMessages.empty ()) // schedule resend at first message only + ScheduleResend (); + SentMessage * sentMessage = new SentMessage; + m_SentMessages[msgID] = sentMessage; + sentMessage->nextResendTime = i2p::util::GetSecondsSinceEpoch () + RESEND_INTERVAL; + sentMessage->numResends = 0; + auto& fragments = sentMessage->fragments; msgID = htobe32 (msgID); size_t payloadSize = SSU_MTU - sizeof (SSUHeader) - 9; // 9 = flag + #frg(1) + messageID(4) + frag info (3) size_t len = msg->GetLength (); @@ -248,8 +252,9 @@ namespace ssu uint32_t fragmentNum = 0; while (len > 0) { - uint8_t * buf = new uint8_t[SSU_MTU + 18]; - fragments.push_back (buf); + Fragment * fragment = new Fragment; + uint8_t * buf = fragment->buf; + fragments.push_back (fragment); uint8_t * payload = buf + sizeof (SSUHeader); *payload = DATA_FLAG_WANT_REPLY; // for compatibility payload++; @@ -272,6 +277,7 @@ namespace ssu size += payload - buf; if (size & 0x0F) // make sure 16 bytes boundary size = ((size >> 4) + 1) << 4; // (/16 + 1)*16 + fragment->len = size; // encrypt message with session key m_Session.FillHeaderAndEncrypt (PAYLOAD_TYPE_DATA, buf, size); @@ -335,6 +341,47 @@ namespace ssu m_Session.Send (buf, len); } + void SSUData::ScheduleResend() + { + m_ResendTimer.cancel (); + m_ResendTimer.expires_from_now (boost::posix_time::seconds(RESEND_INTERVAL)); + m_ResendTimer.async_wait (boost::bind (&SSUData::HandleResendTimer, + this, boost::asio::placeholders::error)); + } + + void SSUData::HandleResendTimer (const boost::system::error_code& ecode) + { + if (ecode != boost::asio::error::operation_aborted) + { + uint32_t ts = i2p::util::GetSecondsSinceEpoch (); + for (auto it = m_SentMessages.begin (); it != m_SentMessages.end ();) + { + if (ts >= it->second->nextResendTime) + { + bool isEmpty = true; + for (auto f: it->second->fragments) + if (f) + { + isEmpty = false; + m_Session.Send (f->buf, f->len); // resend + } + + it->second->numResends++; + if (isEmpty || it->second->numResends >= MAX_NUM_RESENDS) + { + delete it->second; + it = m_SentMessages.erase (it); + } + else + it++; + } + else + it++; + } + if (!m_SentMessages.empty ()) + ScheduleResend (); + } + } } } diff --git a/SSUData.h b/SSUData.h index 1cf18eff..45ba31c1 100644 --- a/SSUData.h +++ b/SSUData.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "I2NPProtocol.h" namespace i2p @@ -14,6 +15,8 @@ namespace ssu { const size_t SSU_MTU = 1484; + const int RESEND_INTERVAL = 3; // in seconds + const int MAX_NUM_RESENDS = 5; // data flags const uint8_t DATA_FLAG_EXTENDED_DATA_INCLUDED = 0x02; const uint8_t DATA_FLAG_WANT_REPLY = 0x04; @@ -24,10 +27,12 @@ namespace ssu struct Fragment { - int fragmentNum, len; + int fragmentNum; + size_t len; bool isLast; - uint8_t buf[SSU_MTU]; + uint8_t buf[SSU_MTU + 18]; + Fragment () = default; Fragment (int n, const uint8_t * b, int l, bool last): fragmentNum (n), len (l), isLast (last) { memcpy (buf, b, len); }; }; @@ -52,9 +57,11 @@ namespace ssu struct SentMessage { - std::vector fragments; + std::vector fragments; uint32_t nextResendTime; // in seconds int numResends; + + ~SentMessage () { for (auto it: fragments) { delete it; }; }; }; class SSUSession; @@ -76,13 +83,15 @@ namespace ssu void ProcessFragments (uint8_t * buf); void ProcessSentMessageAck (uint32_t msgID); - private: - + void ScheduleResend (); + void HandleResendTimer (const boost::system::error_code& ecode); + private: SSUSession& m_Session; std::map m_IncomleteMessages; - std::map m_SentMessages; // msgID -> fragments + std::map m_SentMessages; + boost::asio::deadline_timer m_ResendTimer; }; } } From 756a920c1a6277bd85391c4889e2a9daffd50ba8 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 20 Jul 2014 17:12:36 -0400 Subject: [PATCH 15/16] show number sent/received bytes through the status page --- HTTPServer.cpp | 2 ++ NTCPSession.cpp | 10 +++++----- NTCPSession.h | 5 +++++ SSU.cpp | 7 ++++--- SSU.h | 6 +++++- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 98ac5950..7057cb46 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -303,6 +303,7 @@ namespace util s << it.second->GetRemoteRouterInfo ().GetIdentHashAbbreviation () << ": " << it.second->GetSocket ().remote_endpoint().address ().to_string (); if (!outgoing) s << "-->"; + s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]"; s << "
"; } } @@ -318,6 +319,7 @@ namespace util if (outgoing) s << "-->"; s << endpoint.address ().to_string () << ":" << endpoint.port (); if (!outgoing) s << "-->"; + s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]"; s << "
"; } } diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 14b79c19..1d1f098a 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -21,15 +21,15 @@ namespace ntcp { NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo): m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false), - m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), m_NextMessage (nullptr) + m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), m_NextMessage (nullptr), + m_NumSentBytes (0), m_NumReceivedBytes (0) { - m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); + m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); } NTCPSession::~NTCPSession () { delete m_DHKeysPair; - delete m_NextMessage; } void NTCPSession::CreateAESKey (uint8_t * pubKey, uint8_t * aesKey) @@ -403,7 +403,7 @@ namespace ntcp } else { - LogPrint ("Received: ", bytes_transferred); + m_NumReceivedBytes += bytes_transferred; m_ReceiveBufferOffset += bytes_transferred; if (m_ReceiveBufferOffset >= 16) @@ -514,7 +514,7 @@ namespace ntcp } else { - LogPrint ("Msg sent: ", bytes_transferred); + m_NumSentBytes += bytes_transferred; ScheduleTermination (); // reset termination timer } } diff --git a/NTCPSession.h b/NTCPSession.h index 4b9e40ce..1d03708b 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -78,6 +78,9 @@ namespace ntcp void ClientLogin (); void ServerLogin (); void SendI2NPMessage (I2NPMessage * msg); + + size_t GetNumSentBytes () const { return m_NumSentBytes; }; + size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; }; protected: @@ -142,6 +145,8 @@ namespace ntcp i2p::I2NPMessage * m_NextMessage; std::list m_DelayedMessages; size_t m_NextMessageOffset; + + size_t m_NumSentBytes, m_NumReceivedBytes; }; class NTCPClient: public NTCPSession diff --git a/SSU.cpp b/SSU.cpp index 63528e96..05802f1f 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -19,7 +19,8 @@ namespace ssu const i2p::data::RouterInfo * router, bool peerTest ): m_Server (server), m_RemoteEndpoint (remoteEndpoint), m_RemoteRouter (router), m_Timer (m_Server.GetService ()), m_PeerTest (peerTest), m_State (eSessionStateUnknown), - m_IsSessionKey (false), m_RelayTag (0), m_Data (*this) + m_IsSessionKey (false), m_RelayTag (0), m_Data (*this), + m_NumSentBytes (0), m_NumReceivedBytes (0) { m_DHKeysPair = i2p::transports.GetNextDHKeysPair (); } @@ -74,6 +75,7 @@ namespace ssu void SSUSession::ProcessNextMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint) { + m_NumReceivedBytes += len; if (m_State == eSessionStateIntroduced) { // HolePunch received @@ -842,6 +844,7 @@ namespace ssu void SSUSession::Send (const uint8_t * buf, size_t size) { + m_NumSentBytes += size; m_Server.Send (buf, size, m_RemoteEndpoint); } @@ -910,7 +913,6 @@ namespace ssu void SSUServer::Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to) { m_Socket.send_to (boost::asio::buffer (buf, len), to); - LogPrint ("SSU sent ", len, " bytes"); } void SSUServer::Receive () @@ -923,7 +925,6 @@ namespace ssu { if (!ecode) { - LogPrint ("SSU received ", bytes_transferred, " bytes"); SSUSession * session = nullptr; auto it = m_Sessions.find (m_SenderEndpoint); if (it != m_Sessions.end ()) diff --git a/SSU.h b/SSU.h index a64b555c..01870cd2 100644 --- a/SSU.h +++ b/SSU.h @@ -73,7 +73,10 @@ namespace ssu void SendPeerTest (); // Alice SessionState GetState () const { return m_State; }; - + size_t GetNumSentBytes () const { return m_NumSentBytes; }; + size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; }; + + private: void CreateAESandMacKey (const uint8_t * pubKey); @@ -131,6 +134,7 @@ namespace ssu std::list m_DelayedMessages; std::set m_ReceivedIVs; SSUData m_Data; + size_t m_NumSentBytes, m_NumReceivedBytes; }; class SSUServer From de377b45b4fe5819ba81e3395f1c6ee6e73a0795 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 20 Jul 2014 18:16:55 -0400 Subject: [PATCH 16/16] delete unreachable SSU sessions --- SSU.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SSU.cpp b/SSU.cpp index 05802f1f..392fcf0f 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -824,6 +824,7 @@ namespace ssu // encrypt message with session key FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_DESTROYED, buf, 48); Send (buf, 48); + LogPrint ("SSU session destoryed sent"); } } @@ -1021,7 +1022,12 @@ namespace ssu introducerSession->Introduce (introducer->iTag, introducer->iKey); } else + { LogPrint ("Router is unreachable, but no introducers presented. Ignored"); + m_Sessions.erase (remoteEndpoint); + delete session; + session = nullptr; + } } } }