Browse Source

SSU2: check if socket is opened before sending data, handle network_reset error

Signed-off-by: r4sas <r4sas@i2pmail.org>
pull/1928/merge
R4SAS 1 year ago
parent
commit
ae439b5385
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 24
      libi2pd/Profiling.cpp
  2. 23
      libi2pd/SSU2.cpp

24
libi2pd/Profiling.cpp

@ -30,7 +30,7 @@ namespace data @@ -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),
@ -175,8 +175,8 @@ namespace data @@ -175,8 +175,8 @@ namespace data
{
m_HasConnected = true;
UpdateTime ();
}
}
bool RouterProfile::IsLowPartcipationRate () const
{
return 4*m_NumTunnelsAgreed < m_NumTunnelsDeclined; // < 20% rate
@ -223,7 +223,7 @@ namespace data @@ -223,7 +223,7 @@ namespace data
m_LastUnreachableTime = 0;
return (bool)m_LastUnreachableTime;
}
bool RouterProfile::IsUseful() const {
return
m_NumTunnelsAgreed >= PEER_PROFILE_USEFUL_THRESHOLD ||
@ -240,7 +240,7 @@ namespace data @@ -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);
@ -261,7 +261,7 @@ namespace data @@ -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 ())
@ -270,11 +270,11 @@ namespace data @@ -270,11 +270,11 @@ namespace data
}
else
it++;
}
}
}
for (auto& it: tmp)
if (it.second) it.second->Save (it.first);
}
}
void SaveProfiles ()
{
@ -288,22 +288,22 @@ namespace data @@ -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);

23
libi2pd/SSU2.cpp

@ -78,6 +78,7 @@ namespace transport @@ -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]()
@ -89,6 +90,7 @@ namespace transport @@ -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]()
@ -243,10 +245,12 @@ namespace transport @@ -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
@ -303,7 +307,7 @@ namespace transport @@ -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);
}
@ -558,16 +562,25 @@ namespace transport @@ -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
@ -582,17 +595,25 @@ namespace transport @@ -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);

Loading…
Cancel
Save