From 338b17ccf14b4004cbcd71f743025174259150bc Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 15 Jan 2022 12:48:49 -0500 Subject: [PATCH] LocalRouterInfo for own router --- libi2pd/RouterContext.cpp | 4 +-- libi2pd/RouterContext.h | 4 +-- libi2pd/RouterInfo.cpp | 63 ++++++++++++++++++++++----------------- libi2pd/RouterInfo.h | 24 +++++++++++---- 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index cc08006a..cca38cf0 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2021, The PurpleI2P Project +* Copyright (c) 2013-2022, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -57,7 +57,7 @@ namespace i2p void RouterContext::NewRouterInfo () { - i2p::data::RouterInfo routerInfo; + i2p::data::LocalRouterInfo routerInfo; routerInfo.SetRouterIdentity (GetIdentity ()); uint16_t port; i2p::config::GetOption("port", port); if (!port) diff --git a/libi2pd/RouterContext.h b/libi2pd/RouterContext.h index e237a1b9..55fb7bf8 100644 --- a/libi2pd/RouterContext.h +++ b/libi2pd/RouterContext.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2021, The PurpleI2P Project +* Copyright (c) 2013-2022, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -163,7 +163,7 @@ namespace garlic private: - i2p::data::RouterInfo m_RouterInfo; + i2p::data::LocalRouterInfo m_RouterInfo; i2p::data::PrivateKeys m_Keys; std::shared_ptr m_Decryptor, m_TunnelDecryptor; std::shared_ptr m_ECIESSession; diff --git a/libi2pd/RouterInfo.cpp b/libi2pd/RouterInfo.cpp index f07aa8e7..b4a92bbd 100644 --- a/libi2pd/RouterInfo.cpp +++ b/libi2pd/RouterInfo.cpp @@ -99,10 +99,7 @@ namespace data // don't clean up m_Addresses, it will be replaced in ReadFromStream m_Properties.clear (); // copy buffer - if (!m_Buffer) - m_Buffer = netdb.NewRouterInfoBuffer (); - memcpy (m_Buffer->data (), buf, len); - m_BufferLen = len; + UpdateBuffer (buf, len); // skip identity size_t identityLen = m_RouterIdentity->GetFullLen (); // read new RI @@ -787,29 +784,6 @@ namespace data return m_Buffer->data (); } - void RouterInfo::CreateBuffer (const PrivateKeys& privateKeys) - { - m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp - std::stringstream s; - uint8_t ident[1024]; - auto identLen = privateKeys.GetPublic ()->ToBuffer (ident, 1024); - auto signatureLen = privateKeys.GetPublic ()->GetSignatureLen (); - s.write ((char *)ident, identLen); - WriteToStream (s); - m_BufferLen = s.str ().size (); - if (!m_Buffer) - m_Buffer = netdb.NewRouterInfoBuffer (); - if (m_BufferLen + signatureLen < MAX_RI_BUFFER_SIZE) - { - memcpy (m_Buffer->data (), s.str ().c_str (), m_BufferLen); - // signature - privateKeys.Sign ((uint8_t *)m_Buffer->data (), m_BufferLen, (uint8_t *)m_Buffer->data () + m_BufferLen); - m_BufferLen += signatureLen; - } - else - LogPrint (eLogError, "RouterInfo: Our RouterInfo is too long ", m_BufferLen + signatureLen); - } - bool RouterInfo::SaveToFile (const std::string& fullPath) { if (!m_Buffer) @@ -1258,5 +1232,40 @@ namespace data m_SupportedTransports |= transports; } } + + void RouterInfo::UpdateBuffer (const uint8_t * buf, size_t len) + { + if (!m_Buffer) + m_Buffer = netdb.NewRouterInfoBuffer (); + if (len > m_Buffer->size ()) len = m_Buffer->size (); + memcpy (m_Buffer->data (), buf, len); + m_BufferLen = len; + } + + void RouterInfo::RefreshTimestamp () + { + m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); + } + + void LocalRouterInfo::CreateBuffer (const PrivateKeys& privateKeys) + { + RefreshTimestamp (); + std::stringstream s; + uint8_t ident[1024]; + auto identLen = privateKeys.GetPublic ()->ToBuffer (ident, 1024); + auto signatureLen = privateKeys.GetPublic ()->GetSignatureLen (); + s.write ((char *)ident, identLen); + WriteToStream (s); + size_t len = s.str ().size (); + if (len + signatureLen < MAX_RI_BUFFER_SIZE) + { + UpdateBuffer ((const uint8_t *)s.str ().c_str (), len); + // signature + privateKeys.Sign (GetBuffer (), len, GetBufferPointer (len)); + SetBufferLen (len + signatureLen); + } + else + LogPrint (eLogError, "RouterInfo: Our RouterInfo is too long ", len + signatureLen); + } } } diff --git a/libi2pd/RouterInfo.h b/libi2pd/RouterInfo.h index d4651e1c..bde7f305 100644 --- a/libi2pd/RouterInfo.h +++ b/libi2pd/RouterInfo.h @@ -169,13 +169,12 @@ namespace data typedef std::vector > Addresses; - RouterInfo (); RouterInfo (const std::string& fullPath); RouterInfo (const RouterInfo& ) = default; RouterInfo& operator=(const RouterInfo& ) = default; RouterInfo (std::shared_ptr&& buf, size_t len); RouterInfo (const uint8_t * buf, size_t len); - ~RouterInfo (); + virtual ~RouterInfo (); std::shared_ptr GetRouterIdentity () const { return m_RouterIdentity; }; void SetRouterIdentity (std::shared_ptr identity); @@ -238,8 +237,7 @@ namespace data const uint8_t * GetBuffer () const { return m_Buffer->data (); }; const uint8_t * LoadBuffer (const std::string& fullPath); // load if necessary - int GetBufferLen () const { return m_BufferLen; }; - void CreateBuffer (const PrivateKeys& privateKeys); + size_t GetBufferLen () const { return m_BufferLen; }; bool IsUpdated () const { return m_IsUpdated; }; void SetUpdated (bool updated) { m_IsUpdated = updated; }; @@ -261,13 +259,21 @@ namespace data bool IsDestination () const { return false; }; + protected: + + RouterInfo (); + uint8_t * GetBufferPointer (size_t offset = 0 ) { return m_Buffer->data () + offset; }; + void UpdateBuffer (const uint8_t * buf, size_t len); + void SetBufferLen (size_t len) { m_BufferLen = len; }; + void RefreshTimestamp (); + void WriteToStream (std::ostream& s) const; + private: bool LoadFile (const std::string& fullPath); void ReadFromFile (const std::string& fullPath); void ReadFromStream (std::istream& s); void ReadFromBuffer (bool verifySignature); - void WriteToStream (std::ostream& s) const; size_t ReadString (char* str, size_t len, std::istream& s) const; void WriteString (const std::string& str, std::ostream& s) const; void ExtractCaps (const char * value); @@ -291,6 +297,14 @@ namespace data int m_Version; mutable std::shared_ptr m_Profile; }; + + class LocalRouterInfo: public RouterInfo + { + public: + + LocalRouterInfo () = default; + void CreateBuffer (const PrivateKeys& privateKeys); + }; } }