Browse Source

internal numeric id for families

pull/1748/head
orignal 3 years ago
parent
commit
ee1c4f4fdc
  1. 16
      libi2pd/Family.cpp
  2. 8
      libi2pd/Family.h
  3. 3
      libi2pd/NetDb.cpp
  4. 2
      libi2pd/NetDb.hpp
  5. 25
      libi2pd/RouterInfo.cpp
  6. 5
      libi2pd/RouterInfo.h
  7. 17
      libi2pd/Transports.cpp
  8. 4
      libi2pd/Transports.h

16
libi2pd/Family.cpp

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2020, The PurpleI2P Project * Copyright (c) 2013-2022, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -88,7 +88,7 @@ namespace data
} }
EVP_PKEY_free (pkey); EVP_PKEY_free (pkey);
if (verifier && cn) if (verifier && cn)
m_SigningKeys[cn] = verifier; m_SigningKeys.emplace (cn, std::make_pair(verifier, m_SigningKeys.size () + 1));
} }
SSL_free (ssl); SSL_free (ssl);
} }
@ -121,7 +121,7 @@ namespace data
} }
bool Families::VerifyFamily (const std::string& family, const IdentHash& ident, bool Families::VerifyFamily (const std::string& family, const IdentHash& ident,
const char * signature, const char * key) const char * signature, const char * key) const
{ {
uint8_t buf[100], signatureBuf[64]; uint8_t buf[100], signatureBuf[64];
size_t len = family.length (), signatureLen = strlen (signature); size_t len = family.length (), signatureLen = strlen (signature);
@ -137,11 +137,19 @@ namespace data
Base64ToByteStream (signature, signatureLen, signatureBuf, 64); Base64ToByteStream (signature, signatureLen, signatureBuf, 64);
auto it = m_SigningKeys.find (family); auto it = m_SigningKeys.find (family);
if (it != m_SigningKeys.end ()) if (it != m_SigningKeys.end ())
return it->second->Verify (buf, len, signatureBuf); return it->second.first->Verify (buf, len, signatureBuf);
// TODO: process key // TODO: process key
return true; return true;
} }
FamilyID Families::GetFamilyID (const std::string& family) const
{
auto it = m_SigningKeys.find (family);
if (it != m_SigningKeys.end ())
return it->second.second;
return 0;
}
std::string CreateFamilySignature (const std::string& family, const IdentHash& ident) std::string CreateFamilySignature (const std::string& family, const IdentHash& ident)
{ {
auto filename = i2p::fs::DataDirPath("family", (family + ".key")); auto filename = i2p::fs::DataDirPath("family", (family + ".key"));

8
libi2pd/Family.h

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2020, The PurpleI2P Project * Copyright (c) 2013-2022, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -19,6 +19,7 @@ namespace i2p
{ {
namespace data namespace data
{ {
typedef int FamilyID;
class Families class Families
{ {
public: public:
@ -27,7 +28,8 @@ namespace data
~Families (); ~Families ();
void LoadCertificates (); void LoadCertificates ();
bool VerifyFamily (const std::string& family, const IdentHash& ident, bool VerifyFamily (const std::string& family, const IdentHash& ident,
const char * signature, const char * key = nullptr); const char * signature, const char * key = nullptr) const;
FamilyID GetFamilyID (const std::string& family) const;
private: private:
@ -35,7 +37,7 @@ namespace data
private: private:
std::map<std::string, std::shared_ptr<i2p::crypto::Verifier> > m_SigningKeys; std::map<std::string, std::pair<std::shared_ptr<i2p::crypto::Verifier>, FamilyID> > m_SigningKeys; // family -> (verifier, id)
}; };
std::string CreateFamilySignature (const std::string& family, const IdentHash& ident); std::string CreateFamilySignature (const std::string& family, const IdentHash& ident);

3
libi2pd/NetDb.cpp

@ -1364,7 +1364,8 @@ namespace data
return res; return res;
} }
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouterInFamily(const std::string & fam) const { std::shared_ptr<const RouterInfo> NetDb::GetRandomRouterInFamily (FamilyID fam) const
{
return GetRandomRouter( return GetRandomRouter(
[fam](std::shared_ptr<const RouterInfo> router)->bool [fam](std::shared_ptr<const RouterInfo> router)->bool
{ {

2
libi2pd/NetDb.hpp

@ -96,7 +96,7 @@ namespace data
std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num, std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num,
std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const; std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const;
std::shared_ptr<const RouterInfo> GetClosestNonFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const; std::shared_ptr<const RouterInfo> GetClosestNonFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
std::shared_ptr<const RouterInfo> GetRandomRouterInFamily(const std::string & fam) const; std::shared_ptr<const RouterInfo> GetRandomRouterInFamily (FamilyID fam) const;
void SetUnreachable (const IdentHash& ident, bool unreachable); void SetUnreachable (const IdentHash& ident, bool unreachable);
void PostI2NPMsg (std::shared_ptr<const I2NPMessage> msg); void PostI2NPMsg (std::shared_ptr<const I2NPMessage> msg);

25
libi2pd/RouterInfo.cpp

@ -41,7 +41,7 @@ namespace data
} }
RouterInfo::RouterInfo (const std::string& fullPath): RouterInfo::RouterInfo (const std::string& fullPath):
m_IsUpdated (false), m_IsUnreachable (false), m_FamilyID (0), m_IsUpdated (false), m_IsUnreachable (false),
m_SupportedTransports (0),m_ReachableTransports (0), m_SupportedTransports (0),m_ReachableTransports (0),
m_Caps (0), m_Version (0) m_Caps (0), m_Version (0)
{ {
@ -51,8 +51,9 @@ namespace data
} }
RouterInfo::RouterInfo (std::shared_ptr<Buffer>&& buf, size_t len): RouterInfo::RouterInfo (std::shared_ptr<Buffer>&& buf, size_t len):
m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0), m_FamilyID (0), m_IsUpdated (true), m_IsUnreachable (false),
m_ReachableTransports (0), m_Caps (0), m_Version (0) m_SupportedTransports (0), m_ReachableTransports (0),
m_Caps (0), m_Version (0)
{ {
if (len <= MAX_RI_BUFFER_SIZE) if (len <= MAX_RI_BUFFER_SIZE)
{ {
@ -442,6 +443,7 @@ namespace data
// read properties // read properties
m_Version = 0; m_Version = 0;
bool isNetId = false; bool isNetId = false;
std::string family;
uint16_t size, r = 0; uint16_t size, r = 0;
s.read ((char *)&size, sizeof (size)); if (!s) return; s.read ((char *)&size, sizeof (size)); if (!s) return;
size = be16toh (size); size = be16toh (size);
@ -486,16 +488,15 @@ namespace data
// family // family
else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY)) else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY))
{ {
m_Family = value; family = value;
boost::to_lower (m_Family); boost::to_lower (family);
} }
else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY_SIG)) else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY_SIG))
{ {
if (!netdb.GetFamilies ().VerifyFamily (m_Family, GetIdentHash (), value)) if (netdb.GetFamilies ().VerifyFamily (family, GetIdentHash (), value))
{ m_FamilyID = netdb.GetFamilies ().GetFamilyID (family);
LogPrint (eLogWarning, "RouterInfo: Family signature verification failed"); else
m_Family.clear (); LogPrint (eLogWarning, "RouterInfo: Family ", family, " signature verification failed");
}
} }
if (!s) return; if (!s) return;
@ -505,9 +506,9 @@ namespace data
SetUnreachable (true); SetUnreachable (true);
} }
bool RouterInfo::IsFamily(const std::string & fam) const bool RouterInfo::IsFamily (FamilyID famid) const
{ {
return m_Family == fam; return m_FamilyID == famid;
} }
void RouterInfo::ExtractCaps (const char * value) void RouterInfo::ExtractCaps (const char * value)

5
libi2pd/RouterInfo.h

@ -19,6 +19,7 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include "Identity.h" #include "Identity.h"
#include "Profiling.h" #include "Profiling.h"
#include "Family.h"
namespace i2p namespace i2p
{ {
@ -252,7 +253,7 @@ namespace data
bool IsNewer (const uint8_t * buf, size_t len) const; bool IsNewer (const uint8_t * buf, size_t len) const;
/** return true if we are in a router family and the signature is valid */ /** return true if we are in a router family and the signature is valid */
bool IsFamily(const std::string & fam) const; bool IsFamily (FamilyID famid) const;
// implements RoutingDestination // implements RoutingDestination
std::shared_ptr<const IdentityEx> GetIdentity () const { return m_RouterIdentity; }; std::shared_ptr<const IdentityEx> GetIdentity () const { return m_RouterIdentity; };
@ -284,7 +285,7 @@ namespace data
private: private:
std::string m_Family; FamilyID m_FamilyID;
std::shared_ptr<const IdentityEx> m_RouterIdentity; std::shared_ptr<const IdentityEx> m_RouterIdentity;
std::shared_ptr<Buffer> m_Buffer; std::shared_ptr<Buffer> m_Buffer;
size_t m_BufferLen; size_t m_BufferLen;

17
libi2pd/Transports.cpp

@ -829,12 +829,18 @@ namespace transport
} }
return i2p::data::netdb.FindRouter (ident); return i2p::data::netdb.FindRouter (ident);
} }
void Transports::RestrictRoutesToFamilies(std::set<std::string> families)
void Transports::RestrictRoutesToFamilies(const std::set<std::string>& families)
{ {
std::lock_guard<std::mutex> lock(m_FamilyMutex); std::lock_guard<std::mutex> lock(m_FamilyMutex);
m_TrustedFamilies.clear(); m_TrustedFamilies.clear();
for ( const auto& fam : families ) for (auto fam : families)
m_TrustedFamilies.push_back(fam); {
boost::to_lower (fam);
auto id = i2p::data::netdb.GetFamilies ().GetFamilyID (fam);
if (id)
m_TrustedFamilies.push_back (id);
}
} }
void Transports::RestrictRoutesToRouters(std::set<i2p::data::IdentHash> routers) void Transports::RestrictRoutesToRouters(std::set<i2p::data::IdentHash> routers)
@ -856,20 +862,19 @@ namespace transport
{ {
{ {
std::lock_guard<std::mutex> l(m_FamilyMutex); std::lock_guard<std::mutex> l(m_FamilyMutex);
std::string fam; i2p::data::FamilyID fam = 0;
auto sz = m_TrustedFamilies.size(); auto sz = m_TrustedFamilies.size();
if(sz > 1) if(sz > 1)
{ {
auto it = m_TrustedFamilies.begin (); auto it = m_TrustedFamilies.begin ();
std::advance(it, rand() % sz); std::advance(it, rand() % sz);
fam = *it; fam = *it;
boost::to_lower(fam);
} }
else if (sz == 1) else if (sz == 1)
{ {
fam = m_TrustedFamilies[0]; fam = m_TrustedFamilies[0];
} }
if (fam.size()) if (fam)
return i2p::data::netdb.GetRandomRouterInFamily(fam); return i2p::data::netdb.GetRandomRouterInFamily(fam);
} }
{ {

4
libi2pd/Transports.h

@ -126,7 +126,7 @@ namespace transport
/** do we want to use restricted routes? */ /** do we want to use restricted routes? */
bool RoutesRestricted() const; bool RoutesRestricted() const;
/** restrict routes to use only these router families for first hops */ /** restrict routes to use only these router families for first hops */
void RestrictRoutesToFamilies(std::set<std::string> families); void RestrictRoutesToFamilies(const std::set<std::string>& families);
/** restrict routes to use only these routers for first hops */ /** restrict routes to use only these routers for first hops */
void RestrictRoutesToRouters(std::set<i2p::data::IdentHash> routers); void RestrictRoutesToRouters(std::set<i2p::data::IdentHash> routers);
@ -173,7 +173,7 @@ namespace transport
uint64_t m_LastBandwidthUpdateTime; uint64_t m_LastBandwidthUpdateTime;
/** which router families to trust for first hops */ /** which router families to trust for first hops */
std::vector<std::string> m_TrustedFamilies; std::vector<i2p::data::FamilyID> m_TrustedFamilies;
mutable std::mutex m_FamilyMutex; mutable std::mutex m_FamilyMutex;
/** which routers for first hop to trust */ /** which routers for first hop to trust */

Loading…
Cancel
Save