From b2fb466cde74c332b2d8ab140b89f6ffbc39faef Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 19 Mar 2014 12:02:51 -0400 Subject: [PATCH] extract caps --- RouterInfo.cpp | 40 +++++++++++++++++++++++++++++++--------- RouterInfo.h | 16 ++++++++++++---- SSU.cpp | 21 +++++++++++++-------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/RouterInfo.cpp b/RouterInfo.cpp index adae686c..56c05591 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -18,13 +18,13 @@ namespace i2p namespace data { RouterInfo::RouterInfo (const char * filename): - m_IsUpdated (false), m_IsUnreachable (false), m_SupportedTransports (0) + m_IsUpdated (false), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0) { ReadFromFile (filename); } RouterInfo::RouterInfo (const uint8_t * buf, int len): - m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0) + m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0) { memcpy (m_Buffer, buf, len); m_BufferLen = len; @@ -175,6 +175,10 @@ namespace data r += ReadString (value, s); s.seekg (1, std::ios_base::cur); r++; // ; m_Properties[key] = value; + + // extract caps + if (strcmp (key, "caps")) + ExtractCaps (value); } CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity)); @@ -185,6 +189,29 @@ namespace data SetUnreachable (true); } + void RouterInfo::ExtractCaps (const char * value) + { + m_Caps = 0; + const char * cap = value; + while (*cap) + { + switch (*cap) + { + case 'f': + m_Caps |= Caps::eFloodfill; + break; + case 'O': + m_Caps |= Caps::eHighBanwidth; + break; + case 'R': + m_Caps |= Caps::eReachable; + break; + default: ; + } + cap++; + } + } + void RouterInfo::UpdateIdentHashBase64 () { size_t l = i2p::data::ByteStreamToBase64 (m_IdentHash, 32, m_IdentHashBase64, 48); @@ -337,10 +364,7 @@ namespace data bool RouterInfo::IsFloodfill () const { - const char * caps = GetProperty ("caps"); - if (caps) - return strchr (caps, 'f'); - return false; + return m_Caps & Caps::eFloodfill; } bool RouterInfo::IsNTCP (bool v4only) const @@ -361,9 +385,7 @@ namespace data bool RouterInfo::UsesIntroducer () const { - if (!IsSSU ()) return false; - auto address = GetSSUAddress (true); // no introducers for v6 - return address && !address->introducers.empty (); + return !(m_Caps & Caps::eReachable); // non-reachable } const RouterInfo::Address * RouterInfo::GetNTCPAddress (bool v4only) const diff --git a/RouterInfo.h b/RouterInfo.h index 52a8d9e9..afde30bc 100644 --- a/RouterInfo.h +++ b/RouterInfo.h @@ -20,11 +20,18 @@ namespace data enum SupportedTranports { eNTCPV4 = 0x01, - eNTCPV6 = 0x20, - eSSUV4 = 0x40, - eSSUV6 = 0x80 + eNTCPV6 = 0x02, + eSSUV4 = 0x04, + eSSUV6 = 0x08 }; + enum Caps + { + eFloodfill = 0x01, + eHighBanwidth = 0x02, + eReachable = 0x04 + }; + enum TransportStyle { eTransportUnknown = 0, @@ -102,6 +109,7 @@ namespace data void WriteToStream (std::ostream& s); size_t ReadString (char * str, std::istream& s); void WriteString (const std::string& str, std::ostream& s); + void ExtractCaps (const char * value); void UpdateIdentHashBase64 (); const Address * GetAddress (TransportStyle s, bool v4only) const; @@ -117,7 +125,7 @@ namespace data std::vector
m_Addresses; std::map m_Properties; bool m_IsUpdated, m_IsUnreachable; - uint8_t m_SupportedTransports; + uint8_t m_SupportedTransports, m_Caps; }; } } diff --git a/SSU.cpp b/SSU.cpp index 64886e90..b118c70a 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -822,14 +822,19 @@ namespace ssu } else { - // connect to introducer - auto& introducer = address->introducers[0]; // TODO: - boost::asio::ip::udp::endpoint introducerEndpoint (introducer.iHost, introducer.iPort); - session = new SSUSession (this, introducerEndpoint, router); - m_Sessions[introducerEndpoint] = session; - LogPrint ("New SSU session to [", router->GetIdentHashAbbreviation (), - "] created through introducer ", introducerEndpoint.address ().to_string (), ":", introducerEndpoint.port ()); - session->ConnectThroughIntroducer (introducer); + // connect through introducer + if (address->introducers.size () > 0) + { + auto& introducer = address->introducers[0]; // TODO: + boost::asio::ip::udp::endpoint introducerEndpoint (introducer.iHost, introducer.iPort); + session = new SSUSession (this, introducerEndpoint, router); + m_Sessions[introducerEndpoint] = session; + LogPrint ("New SSU session to [", router->GetIdentHashAbbreviation (), + "] through introducer ", introducerEndpoint.address ().to_string (), ":", introducerEndpoint.port ()); + session->ConnectThroughIntroducer (introducer); + } + else + LogPrint ("Router is unreachable, but not introducers presentd. Ignored"); } } }