diff --git a/NetDb.cpp b/NetDb.cpp index 15cf176d..c3022181 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -264,6 +264,7 @@ namespace data #else RouterInfo * r = new RouterInfo(it1->path().c_str()); #endif + r->DeleteBuffer (); m_RouterInfos[r->GetIdentHash ()] = r; if (r->IsFloodfill ()) m_Floodfills.push_back (r); @@ -305,6 +306,7 @@ namespace data { it.second->SaveToFile (GetFilePath(fullDirectory, it.second)); it.second->SetUpdated (false); + it.second->DeleteBuffer (); count++; } else diff --git a/RouterInfo.cpp b/RouterInfo.cpp index bce874e9..c02ae96a 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -20,19 +20,28 @@ namespace data RouterInfo::RouterInfo (const char * filename): m_IsUpdated (false), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0) { + m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; ReadFromFile (filename); } RouterInfo::RouterInfo (const uint8_t * buf, int len): m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0) { + m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; memcpy (m_Buffer, buf, len); m_BufferLen = len; ReadFromBuffer (); } + RouterInfo::~RouterInfo () + { + delete m_Buffer; + } + void RouterInfo::Update (const uint8_t * buf, int len) { + if (!m_Buffer) + m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; m_IsUpdated = true; m_IsUnreachable = false; m_SupportedTransports = 0; @@ -42,12 +51,13 @@ namespace data memcpy (m_Buffer, buf, len); m_BufferLen = len; ReadFromBuffer (); + // don't delete buffer until save to file } void RouterInfo::SetRouterIdentity (const Identity& identity) { m_RouterIdentity = identity; - m_IdentHash = m_RouterIdentity.Hash (); + m_IdentHash = m_RouterIdentity.Hash (); UpdateIdentHashBase64 (); UpdateRoutingKey (); m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); @@ -338,8 +348,13 @@ namespace data void RouterInfo::SaveToFile (const std::string& fullPath) { - std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out); - f.write ((char *)m_Buffer, m_BufferLen); + if (m_Buffer) + { + std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out); + f.write ((char *)m_Buffer, m_BufferLen); + } + else + LogPrint ("Can't save to file"); } size_t RouterInfo::ReadString (char * str, std::istream& s) diff --git a/RouterInfo.h b/RouterInfo.h index 0f25a9ad..160583c1 100644 --- a/RouterInfo.h +++ b/RouterInfo.h @@ -64,11 +64,11 @@ namespace data }; RouterInfo (const char * filename); - RouterInfo () = default; + RouterInfo (): m_Buffer (nullptr) {}; RouterInfo (const RouterInfo& ) = default; RouterInfo& operator=(const RouterInfo& ) = default; RouterInfo (const uint8_t * buf, int len); - void Update (const uint8_t * buf, int len); + ~RouterInfo (); const Identity& GetRouterIdentity () const { return m_RouterIdentity; }; void SetRouterIdentity (const Identity& identity); @@ -108,10 +108,14 @@ namespace data void SetUpdated (bool updated) { m_IsUpdated = updated; }; void SaveToFile (const std::string& fullPath); + void Update (const uint8_t * buf, int len); + void DeleteBuffer () { delete m_Buffer; m_Buffer = nullptr; }; + // implements RoutingDestination const IdentHash& GetIdentHash () const { return m_IdentHash; }; const uint8_t * GetEncryptionPublicKey () const { return m_RouterIdentity.publicKey; }; bool IsDestination () const { return false; }; + private: @@ -131,7 +135,7 @@ namespace data IdentHash m_IdentHash; RoutingKey m_RoutingKey; char m_IdentHashBase64[48], m_IdentHashAbbreviation[5]; - uint8_t m_Buffer[MAX_RI_BUFFER_SIZE]; + uint8_t * m_Buffer; int m_BufferLen; uint64_t m_Timestamp; std::vector
m_Addresses;