2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2024-01-25 04:57:24 +03:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2014-10-24 00:56:50 +04:00
|
|
|
#include <string.h>
|
2016-05-11 22:12:38 +03:00
|
|
|
#include "Crypto.h"
|
2014-10-22 23:30:25 +04:00
|
|
|
#include "Log.h"
|
2014-10-24 00:56:50 +04:00
|
|
|
#include "TunnelBase.h"
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "Destination.h"
|
2014-10-22 23:30:25 +04:00
|
|
|
#include "Datagram.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace datagram
|
|
|
|
{
|
2020-05-18 19:01:13 +03:00
|
|
|
DatagramDestination::DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner, bool gzip):
|
2024-01-25 04:05:58 +03:00
|
|
|
m_Owner (owner), m_DefaultReceiver (nullptr), m_DefaultRawReceiver (nullptr), m_Gzip (gzip)
|
2014-10-24 00:56:50 +04:00
|
|
|
{
|
2021-09-14 14:48:21 +03:00
|
|
|
if (m_Gzip)
|
|
|
|
m_Deflator.reset (new i2p::data::GzipDeflator);
|
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
auto identityLen = m_Owner->GetIdentity ()->GetFullLen ();
|
|
|
|
m_From.resize (identityLen);
|
|
|
|
m_Owner->GetIdentity ()->ToBuffer (m_From.data (), identityLen);
|
|
|
|
m_Signature.resize (m_Owner->GetIdentity ()->GetSignatureLen ());
|
2014-10-24 00:56:50 +04:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
DatagramDestination::~DatagramDestination ()
|
|
|
|
{
|
2016-09-03 18:46:47 +03:00
|
|
|
m_Sessions.clear();
|
2015-11-03 17:15:49 +03:00
|
|
|
}
|
2016-12-12 21:40:24 +03:00
|
|
|
|
|
|
|
void DatagramDestination::SendDatagramTo(const uint8_t * payload, size_t len, const i2p::data::IdentHash & identity, uint16_t fromPort, uint16_t toPort)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2020-06-10 18:57:40 +03:00
|
|
|
auto session = ObtainSession(identity);
|
|
|
|
SendDatagram (session, payload, len, fromPort, toPort);
|
|
|
|
FlushSendQueue (session);
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2014-10-27 23:01:22 +03:00
|
|
|
|
2019-07-10 04:33:55 +03:00
|
|
|
void DatagramDestination::SendRawDatagramTo(const uint8_t * payload, size_t len, const i2p::data::IdentHash & identity, uint16_t fromPort, uint16_t toPort)
|
|
|
|
{
|
2020-06-10 18:57:40 +03:00
|
|
|
auto session = ObtainSession(identity);
|
|
|
|
SendRawDatagram (session, payload, len, fromPort, toPort);
|
|
|
|
FlushSendQueue (session);
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 02:20:24 +03:00
|
|
|
std::shared_ptr<DatagramSession> DatagramDestination::GetSession(const i2p::data::IdentHash & ident)
|
|
|
|
{
|
|
|
|
return ObtainSession(ident);
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 02:20:24 +03:00
|
|
|
void DatagramDestination::SendDatagram (std::shared_ptr<DatagramSession> session, const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort)
|
|
|
|
{
|
|
|
|
if (session)
|
|
|
|
{
|
|
|
|
if (m_Owner->GetIdentity ()->GetSigningKeyType () == i2p::data::SIGNING_KEY_TYPE_DSA_SHA1)
|
|
|
|
{
|
|
|
|
uint8_t hash[32];
|
|
|
|
SHA256(payload, len, hash);
|
|
|
|
m_Owner->Sign (hash, 32, m_Signature.data ());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_Owner->Sign (payload, len, m_Signature.data ());
|
|
|
|
|
|
|
|
auto msg = CreateDataMessage ({{m_From.data (), m_From.size ()}, {m_Signature.data (), m_Signature.size ()}, {payload, len}},
|
|
|
|
fromPort, toPort, false, !session->IsRatchets ()); // datagram
|
|
|
|
session->SendMsg(msg);
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 02:20:24 +03:00
|
|
|
|
|
|
|
void DatagramDestination::SendRawDatagram (std::shared_ptr<DatagramSession> session, const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort)
|
|
|
|
{
|
|
|
|
if (session)
|
|
|
|
session->SendMsg(CreateDataMessage ({{payload, len}}, fromPort, toPort, true, !session->IsRatchets ())); // raw
|
|
|
|
}
|
2021-02-01 14:36:51 +03:00
|
|
|
|
2020-06-10 02:20:24 +03:00
|
|
|
void DatagramDestination::FlushSendQueue (std::shared_ptr<DatagramSession> session)
|
2020-06-09 23:26:45 +03:00
|
|
|
{
|
2020-06-10 02:20:24 +03:00
|
|
|
if (session)
|
|
|
|
session->FlushSendQueue ();
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
|
2016-10-09 17:55:55 +03:00
|
|
|
void DatagramDestination::HandleDatagram (uint16_t fromPort, uint16_t toPort,uint8_t * const &buf, size_t len)
|
2014-10-27 23:30:56 +03:00
|
|
|
{
|
|
|
|
i2p::data::IdentityEx identity;
|
|
|
|
size_t identityLen = identity.FromBuffer (buf, len);
|
|
|
|
const uint8_t * signature = buf + identityLen;
|
|
|
|
size_t headerLen = identityLen + identity.GetSignatureLen ();
|
|
|
|
|
|
|
|
bool verified = false;
|
|
|
|
if (identity.GetSigningKeyType () == i2p::data::SIGNING_KEY_TYPE_DSA_SHA1)
|
2015-03-27 05:17:26 +03:00
|
|
|
{
|
|
|
|
uint8_t hash[32];
|
2015-11-03 17:15:49 +03:00
|
|
|
SHA256(buf + headerLen, len - headerLen, hash);
|
2015-03-27 05:17:26 +03:00
|
|
|
verified = identity.Verify (hash, 32, signature);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
else
|
2014-10-27 23:30:56 +03:00
|
|
|
verified = identity.Verify (buf + headerLen, len - headerLen, signature);
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2014-10-27 23:30:56 +03:00
|
|
|
if (verified)
|
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
auto h = identity.GetIdentHash();
|
2017-01-13 19:45:52 +03:00
|
|
|
auto session = ObtainSession(h);
|
|
|
|
session->Ack();
|
2016-09-03 17:24:06 +03:00
|
|
|
auto r = FindReceiver(toPort);
|
|
|
|
if(r)
|
|
|
|
r(identity, fromPort, toPort, buf + headerLen, len -headerLen);
|
2014-10-31 23:44:44 +03:00
|
|
|
else
|
2016-09-03 17:24:06 +03:00
|
|
|
LogPrint (eLogWarning, "DatagramDestination: no receiver for port ", toPort);
|
2014-10-27 23:30:56 +03:00
|
|
|
}
|
|
|
|
else
|
2018-01-06 06:48:51 +03:00
|
|
|
LogPrint (eLogWarning, "Datagram signature verification failed");
|
2014-10-27 23:30:56 +03:00
|
|
|
}
|
|
|
|
|
2019-07-10 04:33:55 +03:00
|
|
|
void DatagramDestination::HandleRawDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
|
|
|
{
|
2024-01-25 04:05:58 +03:00
|
|
|
auto r = FindRawReceiver(toPort);
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
r (fromPort, toPort, buf, len);
|
2019-07-10 04:33:55 +03:00
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "DatagramDestination: no receiver for raw datagram");
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
|
|
|
|
2024-01-25 04:05:58 +03:00
|
|
|
void DatagramDestination::SetReceiver (const Receiver& receiver, uint16_t port)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_ReceiversMutex);
|
|
|
|
m_ReceiversByPorts[port] = receiver;
|
|
|
|
if (!m_DefaultReceiver) {
|
|
|
|
m_DefaultReceiver = receiver;
|
|
|
|
m_DefaultReceiverPort = port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatagramDestination::ResetReceiver (uint16_t port)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_ReceiversMutex);
|
|
|
|
m_ReceiversByPorts.erase (port);
|
|
|
|
if (m_DefaultReceiverPort == port) {
|
|
|
|
m_DefaultReceiver = nullptr;
|
|
|
|
m_DefaultReceiverPort = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatagramDestination::SetRawReceiver (const RawReceiver& receiver, uint16_t port)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_RawReceiversMutex);
|
|
|
|
m_RawReceiversByPorts[port] = receiver;
|
|
|
|
if (!m_DefaultRawReceiver) {
|
|
|
|
m_DefaultRawReceiver = receiver;
|
|
|
|
m_DefaultRawReceiverPort = port;
|
|
|
|
}
|
2024-01-30 02:01:41 +03:00
|
|
|
}
|
2024-01-25 04:05:58 +03:00
|
|
|
|
|
|
|
void DatagramDestination::ResetRawReceiver (uint16_t port)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_RawReceiversMutex);
|
|
|
|
m_RawReceiversByPorts.erase (port);
|
|
|
|
if (m_DefaultRawReceiverPort == port) {
|
|
|
|
m_DefaultRawReceiver = nullptr;
|
|
|
|
m_DefaultRawReceiverPort = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-03 17:24:06 +03:00
|
|
|
DatagramDestination::Receiver DatagramDestination::FindReceiver(uint16_t port)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_ReceiversMutex);
|
2024-01-25 04:05:58 +03:00
|
|
|
Receiver r = nullptr;
|
2016-09-03 17:24:06 +03:00
|
|
|
auto itr = m_ReceiversByPorts.find(port);
|
|
|
|
if (itr != m_ReceiversByPorts.end())
|
|
|
|
r = itr->second;
|
2024-01-25 04:05:58 +03:00
|
|
|
else {
|
|
|
|
r = m_DefaultReceiver;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
DatagramDestination::RawReceiver DatagramDestination::FindRawReceiver(uint16_t port)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_RawReceiversMutex);
|
|
|
|
RawReceiver r = nullptr;
|
|
|
|
auto itr = m_RawReceiversByPorts.find(port);
|
|
|
|
if (itr != m_RawReceiversByPorts.end())
|
|
|
|
r = itr->second;
|
|
|
|
else {
|
|
|
|
r = m_DefaultRawReceiver;
|
|
|
|
}
|
2016-09-03 17:24:06 +03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-07-10 04:33:55 +03:00
|
|
|
void DatagramDestination::HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len, bool isRaw)
|
2014-10-22 23:30:25 +04:00
|
|
|
{
|
|
|
|
// unzip it
|
|
|
|
uint8_t uncompressed[MAX_DATAGRAM_SIZE];
|
2015-11-03 17:15:49 +03:00
|
|
|
size_t uncompressedLen = m_Inflator.Inflate (buf, len, uncompressed, MAX_DATAGRAM_SIZE);
|
|
|
|
if (uncompressedLen)
|
2019-07-10 04:33:55 +03:00
|
|
|
{
|
|
|
|
if (isRaw)
|
|
|
|
HandleRawDatagram (fromPort, toPort, uncompressed, uncompressedLen);
|
2020-03-01 13:25:50 +03:00
|
|
|
else
|
2019-07-10 04:33:55 +03:00
|
|
|
HandleDatagram (fromPort, toPort, uncompressed, uncompressedLen);
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2017-01-17 20:13:56 +03:00
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "Datagram: decompression failed");
|
2014-10-22 23:30:25 +04:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:49:31 +03:00
|
|
|
std::shared_ptr<I2NPMessage> DatagramDestination::CreateDataMessage (
|
2020-03-01 13:25:50 +03:00
|
|
|
const std::vector<std::pair<const uint8_t *, size_t> >& payloads,
|
|
|
|
uint16_t fromPort, uint16_t toPort, bool isRaw, bool checksum)
|
2014-10-24 00:56:50 +04:00
|
|
|
{
|
2021-09-14 14:48:21 +03:00
|
|
|
size_t size;
|
2020-06-11 04:19:37 +03:00
|
|
|
auto msg = m_I2NPMsgsPool.AcquireShared ();
|
2014-10-24 00:56:50 +04:00
|
|
|
uint8_t * buf = msg->GetPayload ();
|
2015-11-03 17:15:49 +03:00
|
|
|
buf += 4; // reserve for length
|
2021-09-14 14:48:21 +03:00
|
|
|
|
|
|
|
if (m_Gzip && m_Deflator)
|
|
|
|
size = m_Deflator->Deflate (payloads, buf, msg->maxLen - msg->len);
|
|
|
|
else
|
|
|
|
size = i2p::data::GzipNoCompression (payloads, buf, msg->maxLen - msg->len);
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
htobe32buf (msg->GetPayload (), size); // length
|
|
|
|
htobe16buf (buf + 4, fromPort); // source port
|
2018-01-06 06:48:51 +03:00
|
|
|
htobe16buf (buf + 6, toPort); // destination port
|
2019-07-10 20:31:49 +03:00
|
|
|
buf[9] = isRaw ? i2p::client::PROTOCOL_TYPE_RAW : i2p::client::PROTOCOL_TYPE_DATAGRAM; // raw or datagram protocol
|
2018-01-06 06:48:51 +03:00
|
|
|
msg->len += size + 4;
|
2020-05-17 23:49:31 +03:00
|
|
|
msg->FillI2NPMessageHeader (eI2NPData, 0, checksum);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2015-11-03 17:15:49 +03:00
|
|
|
else
|
|
|
|
msg = nullptr;
|
2014-10-24 00:56:50 +04:00
|
|
|
return msg;
|
2016-08-27 20:17:34 +03:00
|
|
|
}
|
|
|
|
|
2016-09-08 17:16:42 +03:00
|
|
|
void DatagramDestination::CleanUp ()
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2016-09-08 17:56:22 +03:00
|
|
|
if (m_Sessions.empty ()) return;
|
2016-09-08 17:46:13 +03:00
|
|
|
auto now = i2p::util::GetMillisecondsSinceEpoch();
|
|
|
|
LogPrint(eLogDebug, "DatagramDestination: clean up sessions");
|
2016-10-09 17:55:55 +03:00
|
|
|
std::unique_lock<std::mutex> lock(m_SessionsMutex);
|
2016-09-08 17:46:13 +03:00
|
|
|
// for each session ...
|
2018-01-06 06:48:51 +03:00
|
|
|
for (auto it = m_Sessions.begin (); it != m_Sessions.end (); )
|
2016-09-08 17:16:42 +03:00
|
|
|
{
|
2016-09-08 17:46:13 +03:00
|
|
|
// check if expired
|
|
|
|
if (now - it->second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE)
|
2016-09-08 17:16:42 +03:00
|
|
|
{
|
2016-09-08 17:46:13 +03:00
|
|
|
LogPrint(eLogInfo, "DatagramDestination: expiring idle session with ", it->first.ToBase32());
|
2017-01-17 20:13:56 +03:00
|
|
|
it->second->Stop ();
|
2016-09-08 17:46:13 +03:00
|
|
|
it = m_Sessions.erase (it); // we are expired
|
2016-09-08 17:16:42 +03:00
|
|
|
}
|
2016-09-08 17:46:13 +03:00
|
|
|
else
|
|
|
|
it++;
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2016-12-12 21:40:24 +03:00
|
|
|
std::shared_ptr<DatagramSession> DatagramDestination::ObtainSession(const i2p::data::IdentHash & identity)
|
2016-09-03 18:46:47 +03:00
|
|
|
{
|
|
|
|
std::shared_ptr<DatagramSession> session = nullptr;
|
|
|
|
std::lock_guard<std::mutex> lock(m_SessionsMutex);
|
2016-12-12 21:40:24 +03:00
|
|
|
auto itr = m_Sessions.find(identity);
|
2016-09-03 18:46:47 +03:00
|
|
|
if (itr == m_Sessions.end()) {
|
|
|
|
// not found, create new session
|
2016-12-12 21:40:24 +03:00
|
|
|
session = std::make_shared<DatagramSession>(m_Owner, identity);
|
2017-01-17 20:13:56 +03:00
|
|
|
session->Start ();
|
2016-12-12 21:40:24 +03:00
|
|
|
m_Sessions[identity] = session;
|
2016-09-03 18:46:47 +03:00
|
|
|
} else {
|
|
|
|
session = itr->second;
|
|
|
|
}
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2016-09-03 20:58:34 +03:00
|
|
|
std::shared_ptr<DatagramSession::Info> DatagramDestination::GetInfoForRemote(const i2p::data::IdentHash & remote)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_SessionsMutex);
|
|
|
|
for ( auto & item : m_Sessions)
|
|
|
|
{
|
|
|
|
if(item.first == remote) return std::make_shared<DatagramSession::Info>(item.second->GetSessionInfo());
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2019-07-10 04:33:55 +03:00
|
|
|
DatagramSession::DatagramSession(std::shared_ptr<i2p::client::ClientDestination> localDestination,
|
2020-03-01 13:25:50 +03:00
|
|
|
const i2p::data::IdentHash & remoteIdent) :
|
2016-09-03 18:46:47 +03:00
|
|
|
m_LocalDestination(localDestination),
|
2016-12-12 21:40:24 +03:00
|
|
|
m_RemoteIdent(remoteIdent),
|
2016-12-27 02:47:47 +03:00
|
|
|
m_RequestingLS(false)
|
2017-01-17 20:13:56 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatagramSession::Start ()
|
2016-09-03 18:46:47 +03:00
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
m_LastUse = i2p::util::GetMillisecondsSinceEpoch ();
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-08-27 20:17:34 +03:00
|
|
|
|
2017-01-17 20:13:56 +03:00
|
|
|
void DatagramSession::Stop ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-09-03 18:46:47 +03:00
|
|
|
void DatagramSession::SendMsg(std::shared_ptr<I2NPMessage> msg)
|
|
|
|
{
|
|
|
|
// we used this session
|
|
|
|
m_LastUse = i2p::util::GetMillisecondsSinceEpoch();
|
2020-06-11 04:19:37 +03:00
|
|
|
if (msg || m_SendQueue.empty ())
|
|
|
|
m_SendQueue.push_back(msg);
|
|
|
|
// flush queue right away if full
|
2021-02-01 14:36:51 +03:00
|
|
|
if (!msg || m_SendQueue.size() >= DATAGRAM_SEND_QUEUE_MAX_SIZE)
|
2020-06-11 04:19:37 +03:00
|
|
|
FlushSendQueue();
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-08-27 20:17:34 +03:00
|
|
|
|
2016-09-03 20:58:34 +03:00
|
|
|
DatagramSession::Info DatagramSession::GetSessionInfo() const
|
|
|
|
{
|
|
|
|
if(!m_RoutingSession)
|
2016-12-12 21:40:24 +03:00
|
|
|
return DatagramSession::Info(nullptr, nullptr, m_LastUse);
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2016-09-03 20:58:34 +03:00
|
|
|
auto routingPath = m_RoutingSession->GetSharedRoutingPath();
|
|
|
|
if (!routingPath)
|
2016-12-12 21:40:24 +03:00
|
|
|
return DatagramSession::Info(nullptr, nullptr, m_LastUse);
|
2016-09-03 20:58:34 +03:00
|
|
|
auto lease = routingPath->remoteLease;
|
|
|
|
auto tunnel = routingPath->outboundTunnel;
|
|
|
|
if(lease)
|
|
|
|
{
|
|
|
|
if(tunnel)
|
2016-12-12 21:40:24 +03:00
|
|
|
return DatagramSession::Info(lease->tunnelGateway, tunnel->GetEndpointIdentHash(), m_LastUse);
|
2016-09-03 20:58:34 +03:00
|
|
|
else
|
2016-12-12 21:40:24 +03:00
|
|
|
return DatagramSession::Info(lease->tunnelGateway, nullptr, m_LastUse);
|
2016-09-03 20:58:34 +03:00
|
|
|
}
|
|
|
|
else if(tunnel)
|
2016-12-12 21:40:24 +03:00
|
|
|
return DatagramSession::Info(nullptr, tunnel->GetEndpointIdentHash(), m_LastUse);
|
2016-09-03 20:58:34 +03:00
|
|
|
else
|
2016-12-12 21:40:24 +03:00
|
|
|
return DatagramSession::Info(nullptr, nullptr, m_LastUse);
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-08-27 20:17:34 +03:00
|
|
|
|
2016-12-12 21:40:24 +03:00
|
|
|
void DatagramSession::Ack()
|
2016-09-03 18:46:47 +03:00
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
m_LastUse = i2p::util::GetMillisecondsSinceEpoch();
|
|
|
|
auto path = GetSharedRoutingPath();
|
|
|
|
if(path)
|
|
|
|
path->updateTime = i2p::util::GetSecondsSinceEpoch ();
|
2020-05-17 23:49:31 +03:00
|
|
|
if (IsRatchets ())
|
2020-05-17 02:10:17 +03:00
|
|
|
SendMsg (nullptr); // send empty message in case if we have some data to send
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-08-27 20:17:34 +03:00
|
|
|
|
2016-12-12 21:40:24 +03:00
|
|
|
std::shared_ptr<i2p::garlic::GarlicRoutingPath> DatagramSession::GetSharedRoutingPath ()
|
2016-09-03 18:46:47 +03:00
|
|
|
{
|
2020-06-13 23:18:12 +03:00
|
|
|
if (!m_RemoteLeaseSet || m_RemoteLeaseSet->IsExpired ())
|
2020-05-23 22:58:11 +03:00
|
|
|
{
|
2020-06-13 23:18:12 +03:00
|
|
|
m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent);
|
|
|
|
if (!m_RemoteLeaseSet)
|
|
|
|
{
|
2021-02-01 14:36:51 +03:00
|
|
|
if(!m_RequestingLS)
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
2016-12-27 02:47:47 +03:00
|
|
|
m_RequestingLS = true;
|
|
|
|
m_LocalDestination->RequestDestination(m_RemoteIdent, std::bind(&DatagramSession::HandleLeaseSetUpdated, this, std::placeholders::_1));
|
|
|
|
}
|
2016-12-12 21:40:24 +03:00
|
|
|
return nullptr;
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-13 23:18:12 +03:00
|
|
|
|
2021-11-27 23:30:35 +03:00
|
|
|
if (!m_RoutingSession || m_RoutingSession->IsTerminated () || !m_RoutingSession->IsReadyToSend ())
|
2020-06-14 18:16:08 +03:00
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
for (auto& it: m_PendingRoutingSessions)
|
2020-12-19 23:07:12 +03:00
|
|
|
if (it->GetOwner () && m_RoutingSession->IsReadyToSend ()) // found established session
|
2020-06-14 18:16:08 +03:00
|
|
|
{
|
|
|
|
m_RoutingSession = it;
|
|
|
|
m_PendingRoutingSessions.clear ();
|
|
|
|
found = true;
|
|
|
|
break;
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
2020-06-14 18:16:08 +03:00
|
|
|
if (!found)
|
2021-02-01 14:36:51 +03:00
|
|
|
{
|
2020-06-14 18:16:08 +03:00
|
|
|
m_RoutingSession = m_LocalDestination->GetRoutingSession(m_RemoteLeaseSet, true);
|
2020-12-19 23:07:12 +03:00
|
|
|
if (!m_RoutingSession->GetOwner () || !m_RoutingSession->IsReadyToSend ())
|
2020-06-14 18:16:08 +03:00
|
|
|
m_PendingRoutingSessions.push_back (m_RoutingSession);
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 21:40:24 +03:00
|
|
|
auto path = m_RoutingSession->GetSharedRoutingPath();
|
2020-07-15 23:20:35 +03:00
|
|
|
if (path && m_RoutingSession->IsRatchets () &&
|
2022-05-20 19:56:05 +03:00
|
|
|
m_LastUse > m_RoutingSession->GetLastActivityTimestamp ()*1000 + DATAGRAM_SESSION_PATH_TIMEOUT)
|
2021-02-01 14:36:51 +03:00
|
|
|
{
|
2020-07-15 23:20:35 +03:00
|
|
|
m_RoutingSession->SetSharedRoutingPath (nullptr);
|
|
|
|
path = nullptr;
|
|
|
|
}
|
2021-02-01 14:36:51 +03:00
|
|
|
|
|
|
|
if (path)
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
|
|
|
if (path->outboundTunnel && !path->outboundTunnel->IsEstablished ())
|
2021-02-01 14:36:51 +03:00
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
// bad outbound tunnel, switch outbound tunnel
|
2020-06-13 23:18:12 +03:00
|
|
|
path->outboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel(path->outboundTunnel);
|
2020-06-17 21:24:25 +03:00
|
|
|
if (!path->outboundTunnel)
|
|
|
|
m_RoutingSession->SetSharedRoutingPath (nullptr);
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (path->remoteLease && path->remoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW))
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
// bad lease, switch to next one
|
2021-02-01 14:36:51 +03:00
|
|
|
if (m_RemoteLeaseSet)
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
|
|
|
auto ls = m_RemoteLeaseSet->GetNonExpiredLeasesExcluding(
|
2021-02-01 14:36:51 +03:00
|
|
|
[&](const i2p::data::Lease& l) -> bool
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
|
|
|
return l.tunnelID == path->remoteLease->tunnelID;
|
|
|
|
});
|
2016-12-12 21:40:24 +03:00
|
|
|
auto sz = ls.size();
|
2021-02-01 14:36:51 +03:00
|
|
|
if (sz)
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
auto idx = rand() % sz;
|
2020-06-13 23:18:12 +03:00
|
|
|
path->remoteLease = ls[idx];
|
2016-12-12 21:40:24 +03:00
|
|
|
}
|
2020-06-13 23:18:12 +03:00
|
|
|
else
|
2020-06-17 21:24:25 +03:00
|
|
|
m_RoutingSession->SetSharedRoutingPath (nullptr);
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
// no remote lease set?
|
|
|
|
LogPrint(eLogWarning, "DatagramSession: no cached remote lease set for ", m_RemoteIdent.ToBase32());
|
2020-06-17 21:24:25 +03:00
|
|
|
m_RoutingSession->SetSharedRoutingPath (nullptr);
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
else
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
// no current path, make one
|
|
|
|
path = std::make_shared<i2p::garlic::GarlicRoutingPath>();
|
2021-02-01 14:36:51 +03:00
|
|
|
|
|
|
|
if (m_RemoteLeaseSet)
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
|
|
|
// pick random next good lease
|
|
|
|
auto ls = m_RemoteLeaseSet->GetNonExpiredLeases();
|
|
|
|
auto sz = ls.size();
|
2021-02-01 14:36:51 +03:00
|
|
|
if (sz)
|
2017-04-09 15:52:42 +03:00
|
|
|
{
|
2020-06-13 23:18:12 +03:00
|
|
|
auto idx = rand() % sz;
|
|
|
|
path->remoteLease = ls[idx];
|
2017-04-09 15:52:42 +03:00
|
|
|
}
|
2020-06-17 21:24:25 +03:00
|
|
|
else
|
|
|
|
return nullptr;
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2021-11-09 02:23:26 +03:00
|
|
|
auto leaseRouter = i2p::data::netdb.FindRouter (path->remoteLease->tunnelGateway);
|
|
|
|
path->outboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel(nullptr,
|
|
|
|
leaseRouter ? leaseRouter->GetCompatibleTransports (false) : (i2p::data::RouterInfo::CompatibleTransports)i2p::data::RouterInfo::eAllTransports);
|
|
|
|
if (!path->outboundTunnel) return nullptr;
|
2021-02-01 14:36:51 +03:00
|
|
|
}
|
|
|
|
else
|
2020-06-13 23:18:12 +03:00
|
|
|
{
|
|
|
|
// no remote lease set currently, bail
|
|
|
|
LogPrint(eLogWarning, "DatagramSession: no remote lease set found for ", m_RemoteIdent.ToBase32());
|
|
|
|
return nullptr;
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-12-12 21:40:24 +03:00
|
|
|
m_RoutingSession->SetSharedRoutingPath(path);
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-12-12 21:40:24 +03:00
|
|
|
return path;
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-08-27 20:17:34 +03:00
|
|
|
|
2016-12-12 21:40:24 +03:00
|
|
|
void DatagramSession::HandleLeaseSetUpdated(std::shared_ptr<i2p::data::LeaseSet> ls)
|
2016-09-03 18:46:47 +03:00
|
|
|
{
|
2016-12-27 02:47:47 +03:00
|
|
|
m_RequestingLS = false;
|
2016-12-17 14:35:38 +03:00
|
|
|
if(!ls) return;
|
2016-12-12 21:40:24 +03:00
|
|
|
// only update lease set if found and newer than previous lease set
|
|
|
|
uint64_t oldExpire = 0;
|
|
|
|
if(m_RemoteLeaseSet) oldExpire = m_RemoteLeaseSet->GetExpirationTime();
|
|
|
|
if(ls && ls->GetExpirationTime() > oldExpire) m_RemoteLeaseSet = ls;
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-08-27 20:17:34 +03:00
|
|
|
|
2016-12-12 21:40:24 +03:00
|
|
|
void DatagramSession::FlushSendQueue ()
|
|
|
|
{
|
2020-06-09 23:26:45 +03:00
|
|
|
if (m_SendQueue.empty ()) return;
|
2016-12-12 21:40:24 +03:00
|
|
|
std::vector<i2p::tunnel::TunnelMessageBlock> send;
|
|
|
|
auto routingPath = GetSharedRoutingPath();
|
|
|
|
// if we don't have a routing path we will drop all queued messages
|
2016-12-18 20:56:34 +03:00
|
|
|
if(routingPath && routingPath->outboundTunnel && routingPath->remoteLease)
|
2016-09-03 18:46:47 +03:00
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
for (const auto & msg : m_SendQueue)
|
2016-09-03 18:46:47 +03:00
|
|
|
{
|
2016-12-12 21:40:24 +03:00
|
|
|
auto m = m_RoutingSession->WrapSingleMessage(msg);
|
2020-05-17 02:10:17 +03:00
|
|
|
if (m)
|
|
|
|
send.push_back(i2p::tunnel::TunnelMessageBlock{i2p::tunnel::eDeliveryTypeTunnel,routingPath->remoteLease->tunnelGateway, routingPath->remoteLease->tunnelID, m});
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2023-04-04 20:19:08 +03:00
|
|
|
routingPath->outboundTunnel->SendTunnelDataMsgs(send);
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2016-12-12 21:40:24 +03:00
|
|
|
m_SendQueue.clear();
|
2016-09-03 18:46:47 +03:00
|
|
|
}
|
2014-10-22 23:30:25 +04:00
|
|
|
}
|
|
|
|
}
|