i2pd/Transports.cpp

568 lines
16 KiB
C++
Raw Normal View History

2015-11-03 17:15:49 +03:00
#include <openssl/dh.h>
2013-10-27 19:26:39 +04:00
#include "Log.h"
2015-11-03 17:15:49 +03:00
#include "Crypto.h"
2013-10-27 19:26:39 +04:00
#include "RouterContext.h"
#include "I2NPProtocol.h"
#include "NetDb.h"
#include "Transports.h"
using namespace i2p::data;
namespace i2p
{
namespace transport
2013-10-27 19:26:39 +04:00
{
2014-10-20 23:19:56 +04:00
DHKeysPairSupplier::DHKeysPairSupplier (int size):
m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr)
{
}
2014-04-05 00:29:40 +04:00
DHKeysPairSupplier::~DHKeysPairSupplier ()
{
Stop ();
}
void DHKeysPairSupplier::Start ()
{
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&DHKeysPairSupplier::Run, this));
}
void DHKeysPairSupplier::Stop ()
{
m_IsRunning = false;
m_Acquired.notify_one ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
}
void DHKeysPairSupplier::Run ()
{
while (m_IsRunning)
{
int num;
while ((num = m_QueueSize - m_Queue.size ()) > 0)
CreateDHKeysPairs (num);
std::unique_lock<std::mutex> l(m_AcquiredMutex);
m_Acquired.wait (l); // wait for element gets aquired
}
}
void DHKeysPairSupplier::CreateDHKeysPairs (int num)
{
if (num > 0)
{
2015-11-03 17:15:49 +03:00
i2p::crypto::DHKeys dh;
2014-04-05 00:29:40 +04:00
for (int i = 0; i < num; i++)
{
2015-11-03 17:15:49 +03:00
auto pair = std::make_shared<i2p::crypto::DHKeys> ();
pair->GenerateKeys ();
2014-07-07 20:34:19 +04:00
std::unique_lock<std::mutex> l(m_AcquiredMutex);
2014-04-05 00:29:40 +04:00
m_Queue.push (pair);
}
}
}
2015-11-03 17:15:49 +03:00
std::shared_ptr<i2p::crypto::DHKeys> DHKeysPairSupplier::Acquire ()
2014-04-05 00:29:40 +04:00
{
if (!m_Queue.empty ())
{
2014-07-07 20:34:19 +04:00
std::unique_lock<std::mutex> l(m_AcquiredMutex);
2014-04-05 00:29:40 +04:00
auto pair = m_Queue.front ();
m_Queue.pop ();
m_Acquired.notify_one ();
return pair;
}
else // queue is empty, create new
2015-11-03 17:15:49 +03:00
{
auto pair = std::make_shared<i2p::crypto::DHKeys> ();
pair->GenerateKeys ();
2014-04-05 00:29:40 +04:00
return pair;
}
}
2015-11-03 17:15:49 +03:00
void DHKeysPairSupplier::Return (std::shared_ptr<i2p::crypto::DHKeys> pair)
{
std::unique_lock<std::mutex> l(m_AcquiredMutex);
m_Queue.push (pair);
}
2013-10-27 19:26:39 +04:00
Transports transports;
Transports::Transports ():
2015-02-11 22:45:25 +03:00
m_IsRunning (false), m_Thread (nullptr), m_Work (m_Service), m_PeerCleanupTimer (m_Service),
2015-03-17 02:33:59 +03:00
m_NTCPServer (nullptr), m_SSUServer (nullptr), m_DHKeysPairSupplier (5), // 5 pre-generated keys
2015-03-17 22:19:38 +03:00
m_TotalSentBytes(0), m_TotalReceivedBytes(0), m_InBandwidth (0), m_OutBandwidth (0),
m_LastInBandwidthUpdateBytes (0), m_LastOutBandwidthUpdateBytes (0), m_LastBandwidthUpdateTime (0)
2013-10-27 19:26:39 +04:00
{
}
Transports::~Transports ()
{
Stop ();
}
void Transports::Start ()
{
2014-04-05 00:29:40 +04:00
m_DHKeysPairSupplier.Start ();
2013-12-29 19:48:57 +04:00
m_IsRunning = true;
2013-10-27 19:26:39 +04:00
m_Thread = new std::thread (std::bind (&Transports::Run, this));
// create acceptors
auto addresses = context.GetRouterInfo ().GetAddresses ();
for (auto& address : addresses)
{
2015-01-12 01:41:56 +03:00
if (!m_NTCPServer)
2013-10-27 19:26:39 +04:00
{
2015-01-12 01:41:56 +03:00
m_NTCPServer = new NTCPServer (address.port);
m_NTCPServer->Start ();
2014-02-25 04:25:26 +04:00
}
2015-01-12 01:41:56 +03:00
if (address.transportStyle == RouterInfo::eTransportSSU && address.host.is_v4 ())
2014-02-25 04:25:26 +04:00
{
2014-01-24 01:10:33 +04:00
if (!m_SSUServer)
{
m_SSUServer = new SSUServer (address.port);
2015-12-18 15:57:22 +03:00
LogPrint (eLogInfo, "Transports: Start listening UDP port ", address.port);
2014-01-24 01:10:33 +04:00
m_SSUServer->Start ();
2014-02-25 04:25:26 +04:00
DetectExternalIP ();
2014-01-24 01:10:33 +04:00
}
else
2015-12-18 15:57:22 +03:00
LogPrint (eLogError, "Transports: SSU server already exists");
2014-01-24 01:10:33 +04:00
}
2013-10-27 19:26:39 +04:00
}
2015-02-11 22:45:25 +03:00
m_PeerCleanupTimer.expires_from_now (boost::posix_time::seconds(5*SESSION_CREATION_TIMEOUT));
m_PeerCleanupTimer.async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1));
2013-10-27 19:26:39 +04:00
}
void Transports::Stop ()
{
2015-02-11 22:45:25 +03:00
m_PeerCleanupTimer.cancel ();
2015-01-13 06:53:35 +03:00
m_Peers.clear ();
2014-01-24 01:10:33 +04:00
if (m_SSUServer)
{
m_SSUServer->Stop ();
delete m_SSUServer;
2014-09-22 21:28:46 +04:00
m_SSUServer = nullptr;
2014-04-09 05:56:34 +04:00
}
2015-01-12 01:41:56 +03:00
if (m_NTCPServer)
{
m_NTCPServer->Stop ();
delete m_NTCPServer;
m_NTCPServer = nullptr;
}
2014-01-24 01:10:33 +04:00
2014-04-05 00:29:40 +04:00
m_DHKeysPairSupplier.Stop ();
2013-12-29 19:48:57 +04:00
m_IsRunning = false;
2013-10-27 19:26:39 +04:00
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
2014-09-22 21:28:46 +04:00
m_Thread = nullptr;
2013-10-27 19:26:39 +04:00
}
}
2013-11-29 16:52:09 +04:00
void Transports::Run ()
{
2013-12-29 19:48:57 +04:00
while (m_IsRunning)
2013-11-29 16:52:09 +04:00
{
2013-12-29 19:48:57 +04:00
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
2015-12-18 15:57:22 +03:00
LogPrint (eLogError, "Transports: runtime exception: ", ex.what ());
2013-12-29 19:48:57 +04:00
}
2013-11-29 16:52:09 +04:00
}
}
2015-03-17 22:19:38 +03:00
void Transports::UpdateBandwidth ()
{
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
if (m_LastBandwidthUpdateTime > 0)
{
auto delta = ts - m_LastBandwidthUpdateTime;
if (delta > 0)
{
2015-03-18 02:04:58 +03:00
m_InBandwidth = (m_TotalReceivedBytes - m_LastInBandwidthUpdateBytes)*1000/delta; // per second
m_OutBandwidth = (m_TotalSentBytes - m_LastOutBandwidthUpdateBytes)*1000/delta; // per second
2015-03-17 22:19:38 +03:00
}
}
m_LastBandwidthUpdateTime = ts;
m_LastInBandwidthUpdateBytes = m_TotalReceivedBytes;
m_LastOutBandwidthUpdateBytes = m_TotalSentBytes;
}
bool Transports::IsBandwidthExceeded () const
{
if (i2p::context.GetRouterInfo ().IsHighBandwidth ()) return false;
return std::max (m_InBandwidth, m_OutBandwidth) > LOW_BANDWIDTH_LIMIT;
}
2013-10-27 19:26:39 +04:00
2015-06-17 18:41:07 +03:00
void Transports::SendMessage (const i2p::data::IdentHash& ident, std::shared_ptr<i2p::I2NPMessage> msg)
{
2015-07-02 20:43:03 +03:00
SendMessages (ident, std::vector<std::shared_ptr<i2p::I2NPMessage> > {msg });
2015-06-17 18:41:07 +03:00
}
void Transports::SendMessages (const i2p::data::IdentHash& ident, const std::vector<std::shared_ptr<i2p::I2NPMessage> >& msgs)
2015-01-21 05:05:57 +03:00
{
m_Service.post (std::bind (&Transports::PostMessages, this, ident, msgs));
}
2015-01-14 05:31:39 +03:00
2015-06-17 18:41:07 +03:00
void Transports::PostMessages (i2p::data::IdentHash ident, std::vector<std::shared_ptr<i2p::I2NPMessage> > msgs)
2015-01-21 05:05:57 +03:00
{
if (ident == i2p::context.GetRouterInfo ().GetIdentHash ())
{
// we send it to ourself
for (auto it: msgs)
i2p::HandleI2NPMessage (it);
2015-01-21 05:05:57 +03:00
return;
}
auto it = m_Peers.find (ident);
if (it == m_Peers.end ())
{
2015-04-14 17:40:46 +03:00
bool connected = false;
try
{
auto r = netdb.FindRouter (ident);
it = m_Peers.insert (std::pair<i2p::data::IdentHash, Peer>(ident, { 0, r, {},
2015-04-14 17:40:46 +03:00
i2p::util::GetSecondsSinceEpoch () })).first;
connected = ConnectToPeer (ident, it->second);
}
catch (std::exception& ex)
{
2015-12-18 15:57:22 +03:00
LogPrint (eLogError, "Transports: PostMessages exception:", ex.what ());
2015-04-14 17:40:46 +03:00
}
2015-06-17 18:41:07 +03:00
if (!connected) return;
2015-01-21 05:05:57 +03:00
}
if (!it->second.sessions.empty ())
2015-06-17 18:41:07 +03:00
it->second.sessions.front ()->SendI2NPMessages (msgs);
2015-01-21 05:05:57 +03:00
else
{
for (auto it1: msgs)
2015-06-17 18:41:07 +03:00
it->second.delayedMessages.push_back (it1);
2015-01-21 05:05:57 +03:00
}
}
2015-01-14 05:31:39 +03:00
bool Transports::ConnectToPeer (const i2p::data::IdentHash& ident, Peer& peer)
{
if (peer.router) // we have RI already
{
if (!peer.numAttempts) // NTCP
{
peer.numAttempts++;
2015-01-16 23:25:44 +03:00
auto address = peer.router->GetNTCPAddress (!context.SupportsV6 ());
if (address)
{
2015-02-04 17:40:00 +03:00
#if BOOST_VERSION >= 104900
2015-01-16 23:25:44 +03:00
if (!address->host.is_unspecified ()) // we have address now
2015-02-04 17:40:00 +03:00
#else
boost::system::error_code ecode;
address->host.to_string (ecode);
if (!ecode)
#endif
2015-01-16 23:25:44 +03:00
{
if (!peer.router->UsesIntroducer () && !peer.router->IsUnreachable ())
{
auto s = std::make_shared<NTCPSession> (*m_NTCPServer, peer.router);
m_NTCPServer->Connect (address->host, address->port, s);
return true;
}
}
else // we don't have address
{
2015-01-17 07:01:40 +03:00
if (address->addressString.length () > 0) // trying to resolve
2015-01-16 23:25:44 +03:00
{
2015-12-18 15:57:22 +03:00
LogPrint (eLogDebug, "Transports: Resolving NTCP ", address->addressString);
2015-01-16 23:25:44 +03:00
NTCPResolve (address->addressString, ident);
return true;
}
}
2014-02-11 04:27:55 +04:00
}
else
2015-12-18 15:57:22 +03:00
LogPrint (eLogWarning, "Transports: NTCP address is not present for ", i2p::data::GetIdentHashAbbreviation (ident), ", trying SSU");
2013-12-29 19:48:57 +04:00
}
if (peer.numAttempts == 1)// SSU
2014-01-19 19:05:54 +04:00
{
2015-01-14 05:31:39 +03:00
peer.numAttempts++;
if (m_SSUServer && peer.router->IsSSU (!context.SupportsV6 ()))
{
auto address = peer.router->GetSSUAddress (!context.SupportsV6 ());
#if BOOST_VERSION >= 104900
if (!address->host.is_unspecified ()) // we have address now
#else
boost::system::error_code ecode;
address->host.to_string (ecode);
if (!ecode)
#endif
2015-11-25 20:51:35 +03:00
{
m_SSUServer->CreateSession (peer.router, address->host, address->port);
2015-01-14 05:31:39 +03:00
return true;
2015-11-25 20:51:35 +03:00
}
2015-12-03 23:45:01 +03:00
else // we don't have address
{
if (address->addressString.length () > 0) // trying to resolve
{
2015-12-18 15:57:22 +03:00
LogPrint (eLogDebug, "Transports: Resolving SSU ", address->addressString);
2015-12-03 23:45:01 +03:00
SSUResolve (address->addressString, ident);
return true;
}
}
2015-01-14 05:31:39 +03:00
}
2014-01-19 19:05:54 +04:00
}
2015-12-18 15:57:22 +03:00
LogPrint (eLogError, "Transports: No NTCP or SSU addresses available");
peer.Done ();
2015-01-14 05:31:39 +03:00
m_Peers.erase (ident);
return false;
}
else // otherwise request RI
{
2015-12-18 15:57:22 +03:00
LogPrint (eLogInfo, "Transports: RouterInfo for ", ident.ToBase64 (), " not found, requested");
i2p::data::netdb.RequestDestination (ident, std::bind (
&Transports::RequestComplete, this, std::placeholders::_1, ident));
2013-10-27 19:26:39 +04:00
}
2015-01-14 05:31:39 +03:00
return true;
2013-10-27 19:26:39 +04:00
}
void Transports::RequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, const i2p::data::IdentHash& ident)
{
m_Service.post (std::bind (&Transports::HandleRequestComplete, this, r, ident));
}
void Transports::HandleRequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, const i2p::data::IdentHash& ident)
{
2015-01-14 05:31:39 +03:00
auto it = m_Peers.find (ident);
if (it != m_Peers.end ())
{
if (r)
{
2015-12-18 15:57:22 +03:00
LogPrint (eLogDebug, "Transports: RouterInfo for ", ident.ToBase64 (), " found, Trying to connect");
2015-01-14 05:31:39 +03:00
it->second.router = r;
ConnectToPeer (ident, it->second);
}
else
{
2015-12-18 15:57:22 +03:00
LogPrint (eLogError, "Transports: RouterInfo not found, Failed to send messages");
2015-01-14 05:31:39 +03:00
m_Peers.erase (it);
}
}
}
2015-01-16 23:25:44 +03:00
2015-01-17 07:01:40 +03:00
void Transports::NTCPResolve (const std::string& addr, const i2p::data::IdentHash& ident)
2015-01-16 23:25:44 +03:00
{
auto resolver = std::make_shared<boost::asio::ip::tcp::resolver>(m_Service);
2015-01-17 16:22:43 +03:00
resolver->async_resolve (boost::asio::ip::tcp::resolver::query (addr, ""),
2015-01-16 23:25:44 +03:00
std::bind (&Transports::HandleNTCPResolve, this,
std::placeholders::_1, std::placeholders::_2, ident, resolver));
}
void Transports::HandleNTCPResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it,
2015-02-15 18:23:06 +03:00
i2p::data::IdentHash ident, std::shared_ptr<boost::asio::ip::tcp::resolver> resolver)
2015-01-16 23:25:44 +03:00
{
auto it1 = m_Peers.find (ident);
2015-02-15 18:23:06 +03:00
if (it1 != m_Peers.end ())
2015-01-16 23:25:44 +03:00
{
auto& peer = it1->second;
2015-02-15 18:23:06 +03:00
if (!ecode && peer.router)
2015-01-16 23:25:44 +03:00
{
auto address = (*it).endpoint ().address ();
2015-12-18 15:57:22 +03:00
LogPrint (eLogDebug, "Transports: ", (*it).host_name (), " has been resolved to ", address);
2015-01-16 23:25:44 +03:00
auto addr = peer.router->GetNTCPAddress ();
if (addr)
{
auto s = std::make_shared<NTCPSession> (*m_NTCPServer, peer.router);
m_NTCPServer->Connect (address, addr->port, s);
return;
}
}
2015-12-18 15:57:22 +03:00
LogPrint (eLogError, "Transports: Unable to resolve NTCP address: ", ecode.message ());
2015-02-15 18:23:06 +03:00
m_Peers.erase (it1);
2015-01-16 23:25:44 +03:00
}
}
2015-12-03 23:45:01 +03:00
void Transports::SSUResolve (const std::string& addr, const i2p::data::IdentHash& ident)
{
auto resolver = std::make_shared<boost::asio::ip::tcp::resolver>(m_Service);
resolver->async_resolve (boost::asio::ip::tcp::resolver::query (addr, ""),
std::bind (&Transports::HandleSSUResolve, this,
std::placeholders::_1, std::placeholders::_2, ident, resolver));
}
void Transports::HandleSSUResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it,
i2p::data::IdentHash ident, std::shared_ptr<boost::asio::ip::tcp::resolver> resolver)
{
auto it1 = m_Peers.find (ident);
if (it1 != m_Peers.end ())
{
auto& peer = it1->second;
if (!ecode && peer.router)
{
auto address = (*it).endpoint ().address ();
2015-12-18 15:57:22 +03:00
LogPrint (eLogDebug, "Transports: ", (*it).host_name (), " has been resolved to ", address);
2015-12-03 23:45:01 +03:00
auto addr = peer.router->GetSSUAddress (!context.SupportsV6 ());;
if (addr)
{
m_SSUServer->CreateSession (peer.router, address, addr->port);
return;
}
}
2015-12-18 15:57:22 +03:00
LogPrint (eLogError, "Transports: Unable to resolve SSU address: ", ecode.message ());
2015-12-03 23:45:01 +03:00
m_Peers.erase (it1);
}
}
2014-11-24 20:26:11 +03:00
void Transports::CloseSession (std::shared_ptr<const i2p::data::RouterInfo> router)
{
if (!router) return;
2015-01-17 16:22:43 +03:00
m_Service.post (std::bind (&Transports::PostCloseSession, this, router));
}
2014-11-24 20:26:11 +03:00
void Transports::PostCloseSession (std::shared_ptr<const i2p::data::RouterInfo> router)
{
auto ssuSession = m_SSUServer ? m_SSUServer->FindSession (router) : nullptr;
if (ssuSession) // try SSU first
{
m_SSUServer->DeleteSession (ssuSession);
2015-12-18 15:57:22 +03:00
LogPrint (eLogDebug, "Transports: SSU session closed");
}
// TODO: delete NTCP
}
2014-02-09 06:06:40 +04:00
void Transports::DetectExternalIP ()
{
2015-02-26 22:17:16 +03:00
if (m_SSUServer)
2014-02-09 06:06:40 +04:00
{
2015-02-26 22:17:16 +03:00
i2p::context.SetStatus (eRouterStatusTesting);
for (int i = 0; i < 5; i++)
{
auto router = i2p::data::netdb.GetRandomPeerTestRouter ();
2015-11-25 20:51:35 +03:00
if (router && router->IsSSU (!context.SupportsV6 ()))
m_SSUServer->CreateSession (router, true); // peer test
2015-02-26 22:17:16 +03:00
else
{
// if not peer test capable routers found pick any
router = i2p::data::netdb.GetRandomRouter ();
if (router && router->IsSSU ())
2015-11-25 20:51:35 +03:00
m_SSUServer->CreateSession (router); // no peer test
2015-02-26 22:17:16 +03:00
}
}
}
else
2015-12-18 15:57:22 +03:00
LogPrint (eLogError, "Transports: Can't detect external IP. SSU is not available");
2014-02-09 06:06:40 +04:00
}
2015-11-03 17:15:49 +03:00
void Transports::PeerTest ()
{
if (m_SSUServer)
{
bool statusChanged = false;
for (int i = 0; i < 5; i++)
{
auto router = i2p::data::netdb.GetRandomPeerTestRouter ();
2015-11-25 20:51:35 +03:00
if (router && router->IsSSU (!context.SupportsV6 ()))
2015-11-03 17:15:49 +03:00
{
if (!statusChanged)
{
statusChanged = true;
i2p::context.SetStatus (eRouterStatusTesting); // first time only
}
2015-11-25 20:51:35 +03:00
m_SSUServer->CreateSession (router, true); // peer test
2015-11-03 17:15:49 +03:00
}
}
}
}
std::shared_ptr<i2p::crypto::DHKeys> Transports::GetNextDHKeysPair ()
2014-04-04 21:30:13 +04:00
{
2014-04-05 00:29:40 +04:00
return m_DHKeysPairSupplier.Acquire ();
2014-04-04 21:30:13 +04:00
}
2015-11-03 17:15:49 +03:00
void Transports::ReuseDHKeysPair (std::shared_ptr<i2p::crypto::DHKeys> pair)
{
m_DHKeysPairSupplier.Return (pair);
}
2015-01-13 06:53:35 +03:00
void Transports::PeerConnected (std::shared_ptr<TransportSession> session)
{
m_Service.post([session, this]()
{
2015-11-03 17:15:49 +03:00
if (!session->GetRemoteIdentity ()) return;
auto ident = session->GetRemoteIdentity ()->GetIdentHash ();
2015-01-13 06:53:35 +03:00
auto it = m_Peers.find (ident);
if (it != m_Peers.end ())
{
it->second.sessions.push_back (session);
session->SendI2NPMessages (it->second.delayedMessages);
it->second.delayedMessages.clear ();
2015-01-13 06:53:35 +03:00
}
2015-01-14 05:31:39 +03:00
else // incoming connection
m_Peers.insert (std::make_pair (ident, Peer{ 0, nullptr, { session }, i2p::util::GetSecondsSinceEpoch () }));
2015-01-13 06:53:35 +03:00
});
}
void Transports::PeerDisconnected (std::shared_ptr<TransportSession> session)
{
m_Service.post([session, this]()
{
2015-11-03 17:15:49 +03:00
if (!session->GetRemoteIdentity ()) return;
auto ident = session->GetRemoteIdentity ()->GetIdentHash ();
2015-01-13 06:53:35 +03:00
auto it = m_Peers.find (ident);
if (it != m_Peers.end ())
2015-01-14 05:31:39 +03:00
{
it->second.sessions.remove (session);
if (it->second.sessions.empty ()) // TODO: why?
{
if (it->second.delayedMessages.size () > 0)
ConnectToPeer (ident, it->second);
else
m_Peers.erase (it);
}
2015-01-14 05:31:39 +03:00
}
2015-01-13 06:53:35 +03:00
});
}
2015-02-11 22:45:25 +03:00
bool Transports::IsConnected (const i2p::data::IdentHash& ident) const
{
auto it = m_Peers.find (ident);
return it != m_Peers.end ();
}
2015-02-11 22:45:25 +03:00
void Transports::HandlePeerCleanupTimer (const boost::system::error_code& ecode)
{
2015-02-12 06:48:26 +03:00
if (ecode != boost::asio::error::operation_aborted)
2015-02-11 22:45:25 +03:00
{
2015-02-12 06:48:26 +03:00
auto ts = i2p::util::GetSecondsSinceEpoch ();
for (auto it = m_Peers.begin (); it != m_Peers.end (); )
2015-02-11 22:45:25 +03:00
{
if (it->second.sessions.empty () && ts > it->second.creationTime + SESSION_CREATION_TIMEOUT)
2015-02-12 06:48:26 +03:00
{
2015-12-18 15:57:22 +03:00
LogPrint (eLogWarning, "Transports: Session to peer ", it->first.ToBase64 (), " has not been created in ", SESSION_CREATION_TIMEOUT, " seconds");
2015-02-12 06:48:26 +03:00
it = m_Peers.erase (it);
}
else
it++;
2015-02-11 22:45:25 +03:00
}
2015-03-17 22:19:38 +03:00
UpdateBandwidth (); // TODO: use separate timer(s) for it
if (i2p::context.GetStatus () == eRouterStatusTesting) // if still testing, repeat peer test
DetectExternalIP ();
2015-02-12 06:48:26 +03:00
m_PeerCleanupTimer.expires_from_now (boost::posix_time::seconds(5*SESSION_CREATION_TIMEOUT));
m_PeerCleanupTimer.async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1));
}
2015-02-11 22:45:25 +03:00
}
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRandomPeer () const
{
2015-11-03 17:15:49 +03:00
if (!m_Peers.size ()) return nullptr;
auto it = m_Peers.begin ();
2015-11-03 17:15:49 +03:00
std::advance (it, rand () % m_Peers.size ());
return it != m_Peers.end () ? it->second.router : nullptr;
}
2013-10-27 19:26:39 +04:00
}
}