diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 1d00fea6..4d789d33 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -172,23 +172,26 @@ namespace i2p return m; } - I2NPMessage * CreateDatabaseStoreMsg () + I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::RouterInfo * router) { + if (!router) // we send own RouterInfo + router = &context.GetRouterInfo (); + I2NPMessage * m = NewI2NPMessage (); I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)m->GetPayload (); - memcpy (msg->key, context.GetRouterInfo ().GetIdentHash (), 32); + memcpy (msg->key, router->GetIdentHash (), 32); msg->type = 0; msg->replyToken = 0; CryptoPP::Gzip compressor; - compressor.Put (context.GetRouterInfo ().GetBuffer (), context.GetRouterInfo ().GetBufferLen ()); + compressor.Put (router->GetBuffer (), router->GetBufferLen ()); compressor.MessageEnd(); - // WARNING!!! MaxRetrievable() return uint64_t. Есть подозрение, что что-то не так - int size = compressor.MaxRetrievable (); + auto size = compressor.MaxRetrievable (); uint8_t * buf = m->GetPayload () + sizeof (I2NPDatabaseStoreMsg); *(uint16_t *)buf = htobe16 (size); // size buf += 2; + // TODO: check if size doesn't exceed buffer compressor.Get (buf, size); m->len += sizeof (I2NPDatabaseStoreMsg) + 2 + size; // payload size FillI2NPMessageHeader (m, eI2NPDatabaseStore); @@ -504,15 +507,9 @@ namespace i2p i2p::garlic::routing.HandleGarlicMessage (msg); break; case eI2NPDatabaseStore: - LogPrint ("DatabaseStore"); - i2p::data::netdb.PostI2NPMsg (msg); - break; case eI2NPDatabaseSearchReply: - LogPrint ("DatabaseSearchReply"); - i2p::data::netdb.PostI2NPMsg (msg); - break; case eI2NPDatabaseLookup: - LogPrint ("DatabaseLookup"); + // forward to netDb i2p::data::netdb.PostI2NPMsg (msg); break; case eI2NPDeliveryStatus: diff --git a/I2NPProtocol.h b/I2NPProtocol.h index 38925343..6d8dfc04 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -156,7 +156,7 @@ namespace tunnel std::set * excludedPeers = nullptr, bool encryption = false); I2NPMessage * CreateDatabaseSearchReply (const i2p::data::IdentHash& ident); - I2NPMessage * CreateDatabaseStoreMsg (); + I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::RouterInfo * router = nullptr); I2NPBuildRequestRecordClearText CreateBuildRequestRecord ( const uint8_t * ourIdent, uint32_t receiveTunnelID, diff --git a/NetDb.cpp b/NetDb.cpp index a36e7b37..9717fed8 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -109,13 +109,16 @@ namespace data switch (msg->GetHeader ()->typeID) { case eI2NPDatabaseStore: + LogPrint ("DatabaseStore"); HandleDatabaseStoreMsg (msg->GetPayload (), msg->GetLength ()); // TODO i2p::DeleteI2NPMessage (msg); break; case eI2NPDatabaseSearchReply: + LogPrint ("DatabaseSearchReply"); HandleDatabaseSearchReplyMsg (msg); break; case eI2NPDatabaseLookup: + LogPrint ("DatabaseLookup"); HandleDatabaseLookupMsg (msg); break; default: // WTF? @@ -560,12 +563,23 @@ namespace data uint32_t replyTunnelID = 0; if (flag & 0x01) //reply to tunnel replyTunnelID = be32toh (*(uint32_t *)(buf + 64)); - // TODO: implement search. We send non-found for now - I2NPMessage * replyMsg = CreateDatabaseSearchReply (buf); - if (replyTunnelID) - i2p::tunnel::tunnels.GetNextOutboundTunnel ()->SendTunnelDataMsg (buf+32, replyTunnelID, replyMsg); + + I2NPMessage * replyMsg = nullptr; + auto router = FindRouter (buf); + if (router) + { + router->LoadBuffer (); + replyMsg = CreateDatabaseStoreMsg (router); + } else - i2p::transports.SendMessage (buf, replyMsg); + replyMsg = CreateDatabaseSearchReply (buf); + if (replyMsg) + { + if (replyTunnelID) + i2p::tunnel::tunnels.GetNextOutboundTunnel ()->SendTunnelDataMsg (buf+32, replyTunnelID, replyMsg); + else + i2p::transports.SendMessage (buf, replyMsg); + } i2p::DeleteI2NPMessage (msg); } diff --git a/RouterInfo.cpp b/RouterInfo.cpp index 6105a639..54b19a8e 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -346,7 +346,7 @@ namespace data s.write (properties.str ().c_str (), properties.str ().size ()); } - const uint8_t * RouterInfo::GetBuffer () + const uint8_t * RouterInfo::LoadBuffer () { if (!m_Buffer) { diff --git a/RouterInfo.h b/RouterInfo.h index 65e166aa..cc50b7c9 100644 --- a/RouterInfo.h +++ b/RouterInfo.h @@ -99,7 +99,7 @@ namespace data bool IsUnreachable () const { return m_IsUnreachable; }; const uint8_t * GetBuffer () const { return m_Buffer; }; - const uint8_t * GetBuffer (); // load if necessary + const uint8_t * LoadBuffer (); // load if necessary int GetBufferLen () const { return m_BufferLen; }; void CreateBuffer ();