Skip to content
/ i2pd Public
forked from PurpleI2P/i2pd

Commit

Permalink
SSU2: check if socket is opened before sending data, handle network_r…
Browse files Browse the repository at this point in the history
…eset error

Signed-off-by: r4sas <r4sas@i2pmail.org>
  • Loading branch information
r4sas committed May 26, 2023
1 parent dc6a42c commit ae439b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
24 changes: 12 additions & 12 deletions libi2pd/Profiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace data
{
return boost::posix_time::second_clock::local_time();
}

RouterProfile::RouterProfile ():
m_LastUpdateTime (GetTime ()), m_IsUpdated (false),
m_LastDeclineTime (0), m_LastUnreachableTime (0),
Expand Down Expand Up @@ -175,8 +175,8 @@ namespace data
{
m_HasConnected = true;
UpdateTime ();
}
}

bool RouterProfile::IsLowPartcipationRate () const
{
return 4*m_NumTunnelsAgreed < m_NumTunnelsDeclined; // < 20% rate
Expand Down Expand Up @@ -223,7 +223,7 @@ namespace data
m_LastUnreachableTime = 0;
return (bool)m_LastUnreachableTime;
}

bool RouterProfile::IsUseful() const {
return
m_NumTunnelsAgreed >= PEER_PROFILE_USEFUL_THRESHOLD ||
Expand All @@ -240,7 +240,7 @@ namespace data
auto it = g_Profiles.find (identHash);
if (it != g_Profiles.end ())
return it->second;
}
}
auto profile = std::make_shared<RouterProfile> ();
profile->Load (identHash); // if possible
std::unique_lock<std::mutex> l(g_ProfilesMutex);
Expand All @@ -261,7 +261,7 @@ namespace data
{
std::unique_lock<std::mutex> l(g_ProfilesMutex);
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();)
{
{
if ((ts - it->second->GetLastUpdateTime ()).total_seconds () > PEER_PROFILE_PERSIST_INTERVAL)
{
if (it->second->IsUpdated ())
Expand All @@ -270,11 +270,11 @@ namespace data
}
else
it++;
}
}
}
for (auto& it: tmp)
if (it.second) it.second->Save (it.first);
}
}

void SaveProfiles ()
{
Expand All @@ -288,22 +288,22 @@ namespace data
for (auto& it: tmp)
if (it.second->IsUseful() && it.second->IsUpdated () && (ts - it.second->GetLastUpdateTime ()).total_seconds () < PEER_PROFILE_EXPIRATION_TIMEOUT*3600)
it.second->Save (it.first);
}
}

void DeleteObsoleteProfiles ()
{
{
auto ts = GetTime ();
std::unique_lock<std::mutex> l(g_ProfilesMutex);
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();)
{
{
if ((ts - it->second->GetLastUpdateTime ()).total_seconds () >= PEER_PROFILE_EXPIRATION_TIMEOUT*3600)
it = g_Profiles.erase (it);
else
it++;
}
}

struct stat st;
std::time_t now = std::time(nullptr);

Expand Down
23 changes: 22 additions & 1 deletion libi2pd/SSU2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace transport
if (address->IsV4 ())
{
found = true;
LogPrint (eLogDebug, "SSU2: Opening IPv4 socket at Start");
OpenSocket (boost::asio::ip::udp::endpoint (m_AddressV4, port));
m_ReceiveService.GetService ().post(
[this]()
Expand All @@ -89,6 +90,7 @@ namespace transport
if (address->IsV6 ())
{
found = true;
LogPrint (eLogDebug, "SSU2: Opening IPv6 socket at Start");
OpenSocket (boost::asio::ip::udp::endpoint (m_AddressV6, port));
m_ReceiveService.GetService ().post(
[this]()
Expand Down Expand Up @@ -243,10 +245,12 @@ namespace transport
if (!ecode
|| ecode == boost::asio::error::connection_refused
|| ecode == boost::asio::error::connection_reset
|| ecode == boost::asio::error::network_reset
|| ecode == boost::asio::error::network_unreachable
|| ecode == boost::asio::error::host_unreachable
#ifdef _WIN32 // windows can throw WinAPI error, which is not handled by ASIO
|| ecode.value() == boost::winapi::ERROR_CONNECTION_REFUSED_
|| ecode.value() == boost::winapi::WSAENETRESET_ // 10052
|| ecode.value() == boost::winapi::ERROR_NETWORK_UNREACHABLE_
|| ecode.value() == boost::winapi::ERROR_HOST_UNREACHABLE_
#endif
Expand Down Expand Up @@ -303,7 +307,7 @@ namespace transport
else
{
auto ep = socket.local_endpoint ();
socket.close ();
LogPrint (eLogCritical, "SSU2: Reopening socket in HandleReceivedFrom: code ", ecode.value(), ": ", ecode.message ());
OpenSocket (ep);
Receive (socket);
}
Expand Down Expand Up @@ -558,16 +562,25 @@ namespace transport
SendThroughProxy (header, headerLen, nullptr, 0, payload, payloadLen, to);
return;
}

std::vector<boost::asio::const_buffer> bufs
{
boost::asio::buffer (header, headerLen),
boost::asio::buffer (payload, payloadLen)
};

boost::system::error_code ec;
if (to.address ().is_v6 ())
{
if (!m_SocketV6.is_open ()) return;
m_SocketV6.send_to (bufs, to, 0, ec);
}
else
{
if (!m_SocketV4.is_open ()) return;
m_SocketV4.send_to (bufs, to, 0, ec);
}

if (!ec)
i2p::transport::transports.UpdateSentBytes (headerLen + payloadLen);
else
Expand All @@ -582,17 +595,25 @@ namespace transport
SendThroughProxy (header, headerLen, headerX, headerXLen, payload, payloadLen, to);
return;
}

std::vector<boost::asio::const_buffer> bufs
{
boost::asio::buffer (header, headerLen),
boost::asio::buffer (headerX, headerXLen),
boost::asio::buffer (payload, payloadLen)
};

boost::system::error_code ec;
if (to.address ().is_v6 ())
{
if (!m_SocketV6.is_open ()) return;
m_SocketV6.send_to (bufs, to, 0, ec);
}
else
{
if (!m_SocketV4.is_open ()) return;
m_SocketV4.send_to (bufs, to, 0, ec);
}

if (!ec)
i2p::transport::transports.UpdateSentBytes (headerLen + headerXLen + payloadLen);
Expand Down

0 comments on commit ae439b5

Please sign in to comment.