1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-22 08:14:15 +00:00

split between CreateSession and CreateSessionThrough Introducer

This commit is contained in:
orignal 2015-11-26 16:20:24 -05:00
parent 3c8e331809
commit d69f297c05
2 changed files with 79 additions and 54 deletions

132
SSU.cpp
View File

@ -274,12 +274,17 @@ namespace transport
void SSUServer::CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest) void SSUServer::CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest)
{ {
std::shared_ptr<SSUSession> session;
if (router) if (router)
{ {
if (router->UsesIntroducer ())
{
CreateSessionThroughIntroducer (router, peerTest);
return;
}
auto address = router->GetSSUAddress (!context.SupportsV6 ()); auto address = router->GetSSUAddress (!context.SupportsV6 ());
if (address) if (address)
{ {
std::shared_ptr<SSUSession> session;
boost::asio::ip::udp::endpoint remoteEndpoint (address->host, address->port); boost::asio::ip::udp::endpoint remoteEndpoint (address->host, address->port);
auto it = m_Sessions.find (remoteEndpoint); auto it = m_Sessions.find (remoteEndpoint);
if (it != m_Sessions.end ()) if (it != m_Sessions.end ())
@ -296,63 +301,82 @@ namespace transport
std::unique_lock<std::mutex> l(m_SessionsMutex); std::unique_lock<std::mutex> l(m_SessionsMutex);
m_Sessions[remoteEndpoint] = session; m_Sessions[remoteEndpoint] = session;
} }
if (!router->UsesIntroducer ()) // connect
{ LogPrint ("Creating new SSU session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), "] ",
// connect directly remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port ());
LogPrint ("Creating new SSU session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), "] ", session->Connect ();
remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port ()); }
session->Connect (); }
} else
else LogPrint (eLogWarning, "Router ", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), " doesn't have SSU address");
{ }
// connect through introducer }
int numIntroducers = address->introducers.size ();
if (numIntroducers > 0)
{
std::shared_ptr<SSUSession> introducerSession;
const i2p::data::RouterInfo::Introducer * introducer = nullptr;
// we might have a session to introducer already
for (int i = 0; i < numIntroducers; i++)
{
introducer = &(address->introducers[i]);
it = m_Sessions.find (boost::asio::ip::udp::endpoint (introducer->iHost, introducer->iPort));
if (it != m_Sessions.end ())
{
introducerSession = it->second;
break;
}
}
if (introducerSession) // session found void SSUServer::CreateSessionThroughIntroducer (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest)
LogPrint ("Session to introducer already exists"); {
else // create new if (router && router->UsesIntroducer ())
{ {
LogPrint ("Creating new session to introducer"); auto address = router->GetSSUAddress (!context.SupportsV6 ());
introducer = &(address->introducers[0]); // TODO: if (address)
boost::asio::ip::udp::endpoint introducerEndpoint (introducer->iHost, introducer->iPort); {
introducerSession = std::make_shared<SSUSession> (*this, introducerEndpoint, router); boost::asio::ip::udp::endpoint remoteEndpoint (address->host, address->port);
std::unique_lock<std::mutex> l(m_SessionsMutex); auto it = m_Sessions.find (remoteEndpoint);
m_Sessions[introducerEndpoint] = introducerSession; // check if session if presented alredy
} if (it != m_Sessions.end ())
// introduce {
LogPrint ("Introduce new SSU session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), auto session = it->second;
"] through introducer ", introducer->iHost, ":", introducer->iPort); if (peerTest && session->GetState () == eSessionStateEstablished)
session->WaitForIntroduction (); session->SendPeerTest ();
if (i2p::context.GetRouterInfo ().UsesIntroducer ()) // if we are unreachable return;
{ }
uint8_t buf[1]; // create new session
Send (buf, 0, remoteEndpoint); // send HolePunch int numIntroducers = address->introducers.size ();
} if (numIntroducers > 0)
introducerSession->Introduce (*introducer); {
} std::shared_ptr<SSUSession> introducerSession;
else const i2p::data::RouterInfo::Introducer * introducer = nullptr;
{ // we might have a session to introducer already
LogPrint (eLogWarning, "Can't connect to unreachable router. No introducers presented"); for (int i = 0; i < numIntroducers; i++)
std::unique_lock<std::mutex> l(m_SessionsMutex); {
m_Sessions.erase (remoteEndpoint); introducer = &(address->introducers[i]);
it = m_Sessions.find (boost::asio::ip::udp::endpoint (introducer->iHost, introducer->iPort));
if (it != m_Sessions.end ())
{
introducerSession = it->second;
break;
} }
} }
if (introducerSession) // session found
LogPrint ("Session to introducer already exists");
else // create new
{
LogPrint ("Creating new session to introducer");
introducer = &(address->introducers[0]); // TODO:
boost::asio::ip::udp::endpoint introducerEndpoint (introducer->iHost, introducer->iPort);
introducerSession = std::make_shared<SSUSession> (*this, introducerEndpoint, router);
std::unique_lock<std::mutex> l(m_SessionsMutex);
m_Sessions[introducerEndpoint] = introducerSession;
}
// create session
auto session = std::make_shared<SSUSession> (*this, remoteEndpoint, router, peerTest);
{
std::unique_lock<std::mutex> l(m_SessionsMutex);
m_Sessions[remoteEndpoint] = session;
}
// introduce
LogPrint ("Introduce new SSU session to [", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()),
"] through introducer ", introducer->iHost, ":", introducer->iPort);
session->WaitForIntroduction ();
if (i2p::context.GetRouterInfo ().UsesIntroducer ()) // if we are unreachable
{
uint8_t buf[1];
Send (buf, 0, remoteEndpoint); // send HolePunch
}
introducerSession->Introduce (*introducer);
} }
else
LogPrint (eLogWarning, "Can't connect to unreachable router. No introducers presented");
} }
else else
LogPrint (eLogWarning, "Router ", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), " doesn't have SSU address"); LogPrint (eLogWarning, "Router ", i2p::data::GetIdentHashAbbreviation (router->GetIdentHash ()), " doesn't have SSU address");

1
SSU.h
View File

@ -41,6 +41,7 @@ namespace transport
void Start (); void Start ();
void Stop (); void Stop ();
void CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false); void CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false);
void CreateSessionThroughIntroducer (std::shared_ptr<const i2p::data::RouterInfo> router, bool peerTest = false);
std::shared_ptr<SSUSession> FindSession (std::shared_ptr<const i2p::data::RouterInfo> router) const; std::shared_ptr<SSUSession> FindSession (std::shared_ptr<const i2p::data::RouterInfo> router) const;
std::shared_ptr<SSUSession> FindSession (const boost::asio::ip::udp::endpoint& e) const; std::shared_ptr<SSUSession> FindSession (const boost::asio::ip::udp::endpoint& e) const;
std::shared_ptr<SSUSession> GetRandomEstablishedSession (std::shared_ptr<const SSUSession> excluded); std::shared_ptr<SSUSession> GetRandomEstablishedSession (std::shared_ptr<const SSUSession> excluded);