|
|
@ -46,7 +46,7 @@ namespace transport |
|
|
|
int num; |
|
|
|
int num; |
|
|
|
while ((num = m_QueueSize - m_Queue.size ()) > 0) |
|
|
|
while ((num = m_QueueSize - m_Queue.size ()) > 0) |
|
|
|
CreateDHKeysPairs (num); |
|
|
|
CreateDHKeysPairs (num); |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
m_Acquired.wait (l); // wait for element gets aquired
|
|
|
|
m_Acquired.wait (l); // wait for element gets aquired
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -60,7 +60,7 @@ namespace transport |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto pair = std::make_shared<i2p::crypto::DHKeys> (); |
|
|
|
auto pair = std::make_shared<i2p::crypto::DHKeys> (); |
|
|
|
pair->GenerateKeys (); |
|
|
|
pair->GenerateKeys (); |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
m_Queue.push (pair); |
|
|
|
m_Queue.push (pair); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -69,7 +69,7 @@ namespace transport |
|
|
|
std::shared_ptr<i2p::crypto::DHKeys> DHKeysPairSupplier::Acquire () |
|
|
|
std::shared_ptr<i2p::crypto::DHKeys> DHKeysPairSupplier::Acquire () |
|
|
|
{ |
|
|
|
{ |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
if (!m_Queue.empty ()) |
|
|
|
if (!m_Queue.empty ()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto pair = m_Queue.front (); |
|
|
|
auto pair = m_Queue.front (); |
|
|
@ -86,7 +86,7 @@ namespace transport |
|
|
|
|
|
|
|
|
|
|
|
void DHKeysPairSupplier::Return (std::shared_ptr<i2p::crypto::DHKeys> pair) |
|
|
|
void DHKeysPairSupplier::Return (std::shared_ptr<i2p::crypto::DHKeys> pair) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex); |
|
|
|
m_Queue.push (pair); |
|
|
|
m_Queue.push (pair); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -105,7 +105,7 @@ namespace transport |
|
|
|
Stop (); |
|
|
|
Stop (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Transports::Start () |
|
|
|
void Transports::Start (bool enableNTCP, bool enableSSU) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_DHKeysPairSupplier.Start (); |
|
|
|
m_DHKeysPairSupplier.Start (); |
|
|
|
m_IsRunning = true; |
|
|
|
m_IsRunning = true; |
|
|
@ -114,19 +114,36 @@ namespace transport |
|
|
|
auto& addresses = context.GetRouterInfo ().GetAddresses (); |
|
|
|
auto& addresses = context.GetRouterInfo ().GetAddresses (); |
|
|
|
for (auto address : addresses) |
|
|
|
for (auto address : addresses) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!m_NTCPServer) |
|
|
|
if (!m_NTCPServer && enableNTCP) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_NTCPServer = new NTCPServer (); |
|
|
|
m_NTCPServer = new NTCPServer (); |
|
|
|
m_NTCPServer->Start (); |
|
|
|
m_NTCPServer->Start (); |
|
|
|
|
|
|
|
if (!(m_NTCPServer->IsBoundV6() || m_NTCPServer->IsBoundV4())) { |
|
|
|
|
|
|
|
/** failed to bind to NTCP */ |
|
|
|
|
|
|
|
LogPrint(eLogError, "Transports: failed to bind to TCP"); |
|
|
|
|
|
|
|
m_NTCPServer->Stop(); |
|
|
|
|
|
|
|
delete m_NTCPServer; |
|
|
|
|
|
|
|
m_NTCPServer = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (address->transportStyle == RouterInfo::eTransportSSU && address->host.is_v4 ()) |
|
|
|
if (address->transportStyle == RouterInfo::eTransportSSU) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!m_SSUServer) |
|
|
|
if (!m_SSUServer && enableSSU) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_SSUServer = new SSUServer (address->port); |
|
|
|
if (address->host.is_v4()) |
|
|
|
|
|
|
|
m_SSUServer = new SSUServer (address->port); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
m_SSUServer = new SSUServer (address->host, address->port); |
|
|
|
LogPrint (eLogInfo, "Transports: Start listening UDP port ", address->port); |
|
|
|
LogPrint (eLogInfo, "Transports: Start listening UDP port ", address->port); |
|
|
|
m_SSUServer->Start (); |
|
|
|
try { |
|
|
|
|
|
|
|
m_SSUServer->Start (); |
|
|
|
|
|
|
|
} catch ( std::exception & ex ) { |
|
|
|
|
|
|
|
LogPrint(eLogError, "Transports: Failed to bind to UDP port", address->port); |
|
|
|
|
|
|
|
delete m_SSUServer; |
|
|
|
|
|
|
|
m_SSUServer = nullptr; |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
DetectExternalIP (); |
|
|
|
DetectExternalIP (); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
@ -231,7 +248,7 @@ namespace transport |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto r = netdb.FindRouter (ident); |
|
|
|
auto r = netdb.FindRouter (ident); |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
it = m_Peers.insert (std::pair<i2p::data::IdentHash, Peer>(ident, { 0, r, {}, |
|
|
|
it = m_Peers.insert (std::pair<i2p::data::IdentHash, Peer>(ident, { 0, r, {}, |
|
|
|
i2p::util::GetSecondsSinceEpoch (), {} })).first; |
|
|
|
i2p::util::GetSecondsSinceEpoch (), {} })).first; |
|
|
|
} |
|
|
|
} |
|
|
@ -288,7 +305,7 @@ namespace transport |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint (eLogWarning, "Transports: NTCP address is not present for ", i2p::data::GetIdentHashAbbreviation (ident), ", trying SSU"); |
|
|
|
LogPrint (eLogDebug, "Transports: NTCP address is not present for ", i2p::data::GetIdentHashAbbreviation (ident), ", trying SSU"); |
|
|
|
} |
|
|
|
} |
|
|
|
if (peer.numAttempts == 1)// SSU
|
|
|
|
if (peer.numAttempts == 1)// SSU
|
|
|
|
{ |
|
|
|
{ |
|
|
@ -320,7 +337,7 @@ namespace transport |
|
|
|
} |
|
|
|
} |
|
|
|
LogPrint (eLogError, "Transports: No NTCP or SSU addresses available"); |
|
|
|
LogPrint (eLogError, "Transports: No NTCP or SSU addresses available"); |
|
|
|
peer.Done (); |
|
|
|
peer.Done (); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
m_Peers.erase (ident); |
|
|
|
m_Peers.erase (ident); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
@ -352,7 +369,7 @@ namespace transport |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint (eLogError, "Transports: RouterInfo not found, Failed to send messages"); |
|
|
|
LogPrint (eLogError, "Transports: RouterInfo not found, Failed to send messages"); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
m_Peers.erase (it); |
|
|
|
m_Peers.erase (it); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -396,7 +413,7 @@ namespace transport |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
LogPrint (eLogError, "Transports: Unable to resolve NTCP address: ", ecode.message ()); |
|
|
|
LogPrint (eLogError, "Transports: Unable to resolve NTCP address: ", ecode.message ()); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
m_Peers.erase (it1); |
|
|
|
m_Peers.erase (it1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -438,7 +455,7 @@ namespace transport |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
LogPrint (eLogError, "Transports: Unable to resolve SSU address: ", ecode.message ()); |
|
|
|
LogPrint (eLogError, "Transports: Unable to resolve SSU address: ", ecode.message ()); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
m_Peers.erase (it1); |
|
|
|
m_Peers.erase (it1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -469,18 +486,21 @@ namespace transport |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (m_SSUServer) |
|
|
|
if (m_SSUServer) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
#ifndef MESHNET |
|
|
|
i2p::context.SetStatus (eRouterStatusTesting); |
|
|
|
i2p::context.SetStatus (eRouterStatusTesting); |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++) |
|
|
|
for (int i = 0; i < 5; i++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto router = i2p::data::netdb.GetRandomPeerTestRouter (); |
|
|
|
auto router = i2p::data::netdb.GetRandomPeerTestRouter (); |
|
|
|
if (router && router->IsSSU (!context.SupportsV6 ())) |
|
|
|
if (router && router->IsSSU (!context.SupportsV6 ())) |
|
|
|
m_SSUServer->CreateSession (router, true); // peer test
|
|
|
|
m_SSUServer->CreateSession (router, true); // peer test
|
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
// if not peer test capable routers found pick any
|
|
|
|
// if not peer test capable routers found pick any
|
|
|
|
router = i2p::data::netdb.GetRandomRouter (); |
|
|
|
router = i2p::data::netdb.GetRandomRouter (); |
|
|
|
if (router && router->IsSSU ()) |
|
|
|
if (router && router->IsSSU ()) |
|
|
|
m_SSUServer->CreateSession (router); // no peer test
|
|
|
|
m_SSUServer->CreateSession (router); // no peer test
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -503,7 +523,7 @@ namespace transport |
|
|
|
statusChanged = true; |
|
|
|
statusChanged = true; |
|
|
|
i2p::context.SetStatus (eRouterStatusTesting); // first time only
|
|
|
|
i2p::context.SetStatus (eRouterStatusTesting); // first time only
|
|
|
|
} |
|
|
|
} |
|
|
|
m_SSUServer->CreateSession (router, true); // peer test
|
|
|
|
m_SSUServer->CreateSession (router, true); // peer test
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -535,7 +555,7 @@ namespace transport |
|
|
|
// check if first message is our DatabaseStore (publishing)
|
|
|
|
// check if first message is our DatabaseStore (publishing)
|
|
|
|
auto firstMsg = it->second.delayedMessages[0]; |
|
|
|
auto firstMsg = it->second.delayedMessages[0]; |
|
|
|
if (firstMsg && firstMsg->GetTypeID () == eI2NPDatabaseStore && |
|
|
|
if (firstMsg && firstMsg->GetTypeID () == eI2NPDatabaseStore && |
|
|
|
i2p::data::IdentHash(firstMsg->GetPayload () + DATABASE_STORE_KEY_OFFSET) == i2p::context.GetIdentHash ()) |
|
|
|
i2p::data::IdentHash(firstMsg->GetPayload () + DATABASE_STORE_KEY_OFFSET) == i2p::context.GetIdentHash ()) |
|
|
|
sendDatabaseStore = false; // we have it in the list already
|
|
|
|
sendDatabaseStore = false; // we have it in the list already
|
|
|
|
} |
|
|
|
} |
|
|
|
if (sendDatabaseStore) |
|
|
|
if (sendDatabaseStore) |
|
|
@ -547,7 +567,7 @@ namespace transport |
|
|
|
else // incoming connection
|
|
|
|
else // incoming connection
|
|
|
|
{ |
|
|
|
{ |
|
|
|
session->SendI2NPMessages ({ CreateDatabaseStoreMsg () }); // send DatabaseStore
|
|
|
|
session->SendI2NPMessages ({ CreateDatabaseStoreMsg () }); // send DatabaseStore
|
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
m_Peers.insert (std::make_pair (ident, Peer{ 0, nullptr, { session }, i2p::util::GetSecondsSinceEpoch (), {} })); |
|
|
|
m_Peers.insert (std::make_pair (ident, Peer{ 0, nullptr, { session }, i2p::util::GetSecondsSinceEpoch (), {} })); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
@ -570,7 +590,7 @@ namespace transport |
|
|
|
ConnectToPeer (ident, it->second); |
|
|
|
ConnectToPeer (ident, it->second); |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
m_Peers.erase (it); |
|
|
|
m_Peers.erase (it); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -595,14 +615,20 @@ namespace transport |
|
|
|
if (it->second.sessions.empty () && ts > it->second.creationTime + SESSION_CREATION_TIMEOUT) |
|
|
|
if (it->second.sessions.empty () && ts > it->second.creationTime + SESSION_CREATION_TIMEOUT) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint (eLogWarning, "Transports: Session to peer ", it->first.ToBase64 (), " has not been created in ", SESSION_CREATION_TIMEOUT, " seconds"); |
|
|
|
LogPrint (eLogWarning, "Transports: Session to peer ", it->first.ToBase64 (), " has not been created in ", SESSION_CREATION_TIMEOUT, " seconds"); |
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
auto profile = i2p::data::GetRouterProfile(it->first); |
|
|
|
|
|
|
|
if (profile) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
profile->TunnelNonReplied(); |
|
|
|
|
|
|
|
profile->Save(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex); |
|
|
|
it = m_Peers.erase (it); |
|
|
|
it = m_Peers.erase (it); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
it++; |
|
|
|
it++; |
|
|
|
} |
|
|
|
} |
|
|
|
UpdateBandwidth (); // TODO: use separate timer(s) for it
|
|
|
|
UpdateBandwidth (); // TODO: use separate timer(s) for it
|
|
|
|
if (i2p::context.GetStatus () == eRouterStatusTesting) // if still testing, repeat peer test
|
|
|
|
if (i2p::context.GetStatus () == eRouterStatusTesting) // if still testing, repeat peer test
|
|
|
|
DetectExternalIP (); |
|
|
|
DetectExternalIP (); |
|
|
|
m_PeerCleanupTimer.expires_from_now (boost::posix_time::seconds(5*SESSION_CREATION_TIMEOUT)); |
|
|
|
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)); |
|
|
|
m_PeerCleanupTimer.async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1)); |
|
|
@ -617,6 +643,30 @@ namespace transport |
|
|
|
std::advance (it, rand () % m_Peers.size ()); |
|
|
|
std::advance (it, rand () % m_Peers.size ()); |
|
|
|
return it != m_Peers.end () ? it->second.router : nullptr; |
|
|
|
return it != m_Peers.end () ? it->second.router : nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void Transports::RestrictRoutes(std::vector<std::string> families) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(m_FamilyMutex); |
|
|
|
|
|
|
|
m_TrustedFamilies.clear(); |
|
|
|
|
|
|
|
for ( auto fam : families ) |
|
|
|
|
|
|
|
m_TrustedFamilies.push_back(fam); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Transports::RoutesRestricted() const { |
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(m_FamilyMutex); |
|
|
|
|
|
|
|
return m_TrustedFamilies.size() > 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** XXX: if routes are not restricted this dies */ |
|
|
|
|
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRestrictedPeer() const { |
|
|
|
|
|
|
|
std::string fam; |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(m_FamilyMutex); |
|
|
|
|
|
|
|
// TODO: random family (?)
|
|
|
|
|
|
|
|
fam = m_TrustedFamilies[0]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
boost::to_lower(fam); |
|
|
|
|
|
|
|
return i2p::data::netdb.GetRandomRouterInFamily(fam); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|