diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index fad4ed5d..58ae5855 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -576,8 +576,11 @@ namespace transport m_Socket.close (); transports.PeerDisconnected (shared_from_this ()); m_Server.RemoveNTCP2Session (shared_from_this ()); - if (!m_IntermediateQueue.empty ()) - m_SendQueue.splice (m_SendQueue.end (), m_IntermediateQueue); + { + std::lock_guard l(m_IntermediateQueueMutex); + if (!m_IntermediateQueue.empty ()) + m_SendQueue.splice (m_SendQueue.end (), m_IntermediateQueue); + } for (auto& it: m_SendQueue) it->Drop (); m_SendQueue.clear (); diff --git a/libi2pd_client/UDPTunnel.cpp b/libi2pd_client/UDPTunnel.cpp index 990739e3..b5fe5d4f 100644 --- a/libi2pd_client/UDPTunnel.cpp +++ b/libi2pd_client/UDPTunnel.cpp @@ -354,7 +354,10 @@ namespace client dgram->ResetReceiver (m_inPort); dgram->ResetRawReceiver (m_inPort); } - m_Sessions.clear (); + { + std::lock_guard lock (m_SessionsMutex); + m_Sessions.clear (); + } } std::vector > I2PUDPServerTunnel::GetSessions () @@ -433,7 +436,10 @@ namespace client } m_cancel_resolve = true; - m_Sessions.clear(); + { + std::lock_guard lock (m_SessionsMutex); + m_Sessions.clear(); + } if(m_LocalSocket && m_LocalSocket->is_open ()) m_LocalSocket->close (); @@ -496,6 +502,7 @@ namespace client auto remotePort = m_RecvEndpoint.port (); if (!m_LastPort || m_LastPort != remotePort) { + std::lock_guard lock (m_SessionsMutex); auto itr = m_Sessions.find (remotePort); if (itr != m_Sessions.end ()) m_LastSession = itr->second; @@ -637,21 +644,27 @@ namespace client void I2PUDPClientTunnel::HandleRecvFromI2PRaw (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len) { - auto itr = m_Sessions.find (toPort); - // found convo ? - if (itr != m_Sessions.end ()) + std::shared_ptr convo; + { + std::lock_guard lock (m_SessionsMutex); + auto itr = m_Sessions.find (toPort); + // found convo ? + if (itr != m_Sessions.end ()) + convo = itr->second; + } + if (convo) { // found convo if (len > 0) { LogPrint (eLogDebug, "UDP Client: Got ", len, "B from ", isIdentity ? Identity.ToBase32 () : ""); boost::system::error_code ec; - m_LocalSocket->send_to (boost::asio::buffer (buf, len), itr->second->first, 0, ec); + m_LocalSocket->send_to (boost::asio::buffer (buf, len), convo->first, 0, ec); if (!ec) // mark convo as active - itr->second->second = i2p::util::GetMillisecondsSinceEpoch (); + convo->second = i2p::util::GetMillisecondsSinceEpoch (); else - LogPrint (eLogInfo, "UDP Client: Send exception: ", ec.message (), " to ", itr->second->first); + LogPrint (eLogInfo, "UDP Client: Send exception: ", ec.message (), " to ", convo->first); } } else