diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 2be70cec..7da308cc 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -72,6 +72,7 @@ namespace http { const char HTTP_PAGE_SAM_SESSION[] = "sam_session"; const char HTTP_PAGE_I2P_TUNNELS[] = "i2p_tunnels"; const char HTTP_PAGE_COMMANDS[] = "commands"; + const char HTTP_PAGE_LEASESETS[] = "leasesets"; const char HTTP_COMMAND_ENABLE_TRANSIT[] = "enable_transit"; const char HTTP_COMMAND_DISABLE_TRANSIT[] = "disable_transit"; const char HTTP_COMMAND_SHUTDOWN_START[] = "shutdown_start"; @@ -140,6 +141,7 @@ namespace http { " Main page
\r\n
\r\n" " Router commands
\r\n" " Local destinations
\r\n" + " Lease Sets
\r\n" " Tunnels
\r\n" " Transit tunnels
\r\n" " Transports
\r\n" @@ -327,6 +329,57 @@ namespace http { } } + void ShowLeasesSets(std::stringstream& s) + { + s << "
LeaseSets

"; + // for each lease set + i2p::data::netdb.VisitLeaseSets( + [&s](const i2p::data::IdentHash dest, std::shared_ptr leaseSet) + { + // create copy of lease set so we extract leases + i2p::data::LeaseSet ls(leaseSet->GetBuffer(), leaseSet->GetBufferLen()); + // begin lease set entry + s << "
"; + // invalid ? + if (!ls.IsValid()) + s << "
!! Invalid !!
"; + // ident + s << "
" << dest.ToBase32() << "
"; + // LeaseSet time + s << "
expires: " << ls.GetExpirationTime() << "
"; + // get non expired leases + auto leases = ls.GetNonExpiredLeases(); + // show non expired leases + s << "
Non Expired Leases: " << leases.size() << "
"; + // for each lease + s << "
"; + for ( auto & l : leases ) + { + // begin lease + s << "
"; + // gateway + s << "
Gateway: " << l->tunnelGateway.ToBase64() << "
"; + // tunnel id + s << "
TunnelID: " << l->tunnelID << "
"; + // end date + s << "
EndDate: " << l->endDate << "
"; + // end lease + s << "
"; + } + // end for each lease + s << "
"; + // end lease set entry + s << "
"; + // linebreak + s << "
"; + } + ); + // end for each lease set + } + void ShowTunnels (std::stringstream& s) { s << "Queue size: " << i2p::tunnel::tunnels.GetQueueSize () << "
\r\n"; @@ -639,6 +692,8 @@ namespace http { ShowSAMSession (s, params["sam_id"]); else if (page == HTTP_PAGE_I2P_TUNNELS) ShowI2PTunnels (s); + else if (page == HTTP_PAGE_LEASESETS) + ShowLeasesSets(s); else { res.code = 400; ShowError(s, "Unknown page: " + page); diff --git a/NetDb.cpp b/NetDb.cpp index f89617be..beafeaee 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -213,6 +213,7 @@ namespace data bool NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, std::shared_ptr from) { + std::unique_lock lock(m_LeaseSetsMutex); bool updated = false; if (!from) // unsolicited LS must be received directly { @@ -264,6 +265,7 @@ namespace data std::shared_ptr NetDb::FindLeaseSet (const IdentHash& destination) const { + std::unique_lock lock(m_LeaseSetsMutex); auto it = m_LeaseSets.find (destination); if (it != m_LeaseSets.end ()) return it->second; @@ -318,6 +320,13 @@ namespace data return true; } + void NetDb::VisitLeaseSets(LeaseSetVisitor v) + { + std::unique_lock lock(m_LeaseSetsMutex); + for ( auto & entry : m_LeaseSets) + v(entry.first, entry.second); + } + void NetDb::Load () { // make sure we cleanup netDb from previous attempts diff --git a/NetDb.h b/NetDb.h index bc7e8e7b..c7371407 100644 --- a/NetDb.h +++ b/NetDb.h @@ -32,7 +32,10 @@ namespace data const int NETDB_MIN_EXPIRATION_TIMEOUT = 90*60; // 1.5 hours const int NETDB_MAX_EXPIRATION_TIMEOUT = 27*60*60; // 27 hours const int NETDB_PUBLISH_INTERVAL = 60*40; - + + /** function for visiting a leaseset stored in a floodfill */ + typedef std::function)> LeaseSetVisitor; + class NetDb { public: @@ -80,7 +83,10 @@ namespace data int GetNumRouters () const { return m_RouterInfos.size (); }; int GetNumFloodfills () const { return m_Floodfills.size (); }; int GetNumLeaseSets () const { return m_LeaseSets.size (); }; - + + /** visit all lease sets we currently store */ + void VisitLeaseSets(LeaseSetVisitor v); + private: void Load (); @@ -98,6 +104,7 @@ namespace data private: + mutable std::mutex m_LeaseSetsMutex; std::map > m_LeaseSets; mutable std::mutex m_RouterInfosMutex; std::map > m_RouterInfos; diff --git a/RouterContext.cpp b/RouterContext.cpp index bb3d0110..44b967e8 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -49,7 +49,9 @@ namespace i2p uint16_t port; i2p::config::GetOption("port", port); if (!port) port = rand () % (30777 - 9111) + 9111; // I2P network ports range - std::string host = i2p::util::config::GetHost(); + bool ipv4; i2p::config::GetOption("ipv4", ipv4); + bool ipv6; i2p::config::GetOption("ipv6", ipv6); + std::string host = i2p::util::config::GetHost(ipv4, ipv6); routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ()); routerInfo.AddNTCPAddress (host.c_str(), port); routerInfo.SetCaps (i2p::data::RouterInfo::eReachable |