mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
370 lines
12 KiB
370 lines
12 KiB
![]()
10 years ago
|
#include <string.h>
|
||
|
#include <vector>
|
||
![]()
8 years ago
|
#include "Crypto.h"
|
||
![]()
10 years ago
|
#include "Log.h"
|
||
![]()
10 years ago
|
#include "TunnelBase.h"
|
||
|
#include "RouterContext.h"
|
||
|
#include "Destination.h"
|
||
![]()
10 years ago
|
#include "Datagram.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace datagram
|
||
|
{
|
||
![]()
8 years ago
|
DatagramDestination::DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner):
|
||
![]()
8 years ago
|
m_Owner (owner.get()),
|
||
|
m_Receiver (nullptr)
|
||
![]()
10 years ago
|
{
|
||
![]()
8 years ago
|
m_Identity.FromBase64 (owner->GetIdentity()->ToBase64());
|
||
![]()
10 years ago
|
}
|
||
![]()
9 years ago
|
|
||
|
DatagramDestination::~DatagramDestination ()
|
||
|
{
|
||
![]()
8 years ago
|
m_Sessions.clear();
|
||
![]()
9 years ago
|
}
|
||
![]()
8 years ago
|
|
||
|
void DatagramDestination::SendDatagramTo(const uint8_t * payload, size_t len, const i2p::data::IdentHash & identity, uint16_t fromPort, uint16_t toPort)
|
||
|
{
|
||
![]()
8 years ago
|
auto owner = m_Owner;
|
||
![]()
8 years ago
|
std::vector<uint8_t> v(MAX_DATAGRAM_SIZE);
|
||
|
uint8_t * buf = v.data();
|
||
|
auto identityLen = m_Identity.ToBuffer (buf, MAX_DATAGRAM_SIZE);
|
||
![]()
10 years ago
|
uint8_t * signature = buf + identityLen;
|
||
![]()
8 years ago
|
auto signatureLen = m_Identity.GetSignatureLen ();
|
||
![]()
10 years ago
|
uint8_t * buf1 = signature + signatureLen;
|
||
|
size_t headerLen = identityLen + signatureLen;
|
||
|
|
||
|
memcpy (buf1, payload, len);
|
||
![]()
8 years ago
|
if (m_Identity.GetSigningKeyType () == i2p::data::SIGNING_KEY_TYPE_DSA_SHA1)
|
||
![]()
10 years ago
|
{
|
||
|
uint8_t hash[32];
|
||
![]()
9 years ago
|
SHA256(buf1, len, hash);
|
||
![]()
8 years ago
|
owner->Sign (hash, 32, signature);
|
||
![]()
10 years ago
|
}
|
||
|
else
|
||
![]()
8 years ago
|
owner->Sign (buf1, len, signature);
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
auto msg = CreateDataMessage (buf, len + headerLen, fromPort, toPort);
|
||
![]()
8 years ago
|
auto session = ObtainSession(identity);
|
||
|
session->SendMsg(msg);
|
||
![]()
8 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
void DatagramDestination::HandleDatagram (uint16_t fromPort, uint16_t toPort,uint8_t * const &buf, size_t len)
|
||
![]()
10 years ago
|
{
|
||
|
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)
|
||
![]()
10 years ago
|
{
|
||
|
uint8_t hash[32];
|
||
![]()
9 years ago
|
SHA256(buf + headerLen, len - headerLen, hash);
|
||
![]()
10 years ago
|
verified = identity.Verify (hash, 32, signature);
|
||
|
}
|
||
![]()
10 years ago
|
else
|
||
|
verified = identity.Verify (buf + headerLen, len - headerLen, signature);
|
||
|
|
||
|
if (verified)
|
||
|
{
|
||
![]()
8 years ago
|
auto h = identity.GetIdentHash();
|
||
![]()
8 years ago
|
auto session = ObtainSession(h);
|
||
|
session->Ack();
|
||
![]()
8 years ago
|
auto r = FindReceiver(toPort);
|
||
|
if(r)
|
||
|
r(identity, fromPort, toPort, buf + headerLen, len -headerLen);
|
||
![]()
10 years ago
|
else
|
||
![]()
8 years ago
|
LogPrint (eLogWarning, "DatagramDestination: no receiver for port ", toPort);
|
||
![]()
10 years ago
|
}
|
||
|
else
|
||
![]()
10 years ago
|
LogPrint (eLogWarning, "Datagram signature verification failed");
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
DatagramDestination::Receiver DatagramDestination::FindReceiver(uint16_t port)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(m_ReceiversMutex);
|
||
|
Receiver r = m_Receiver;
|
||
|
auto itr = m_ReceiversByPorts.find(port);
|
||
|
if (itr != m_ReceiversByPorts.end())
|
||
|
r = itr->second;
|
||
|
return r;
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
void DatagramDestination::HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
||
![]()
10 years ago
|
{
|
||
|
// unzip it
|
||
|
uint8_t uncompressed[MAX_DATAGRAM_SIZE];
|
||
![]()
9 years ago
|
size_t uncompressedLen = m_Inflator.Inflate (buf, len, uncompressed, MAX_DATAGRAM_SIZE);
|
||
|
if (uncompressedLen)
|
||
![]()
8 years ago
|
HandleDatagram (fromPort, toPort, uncompressed, uncompressedLen);
|
||
![]()
8 years ago
|
else
|
||
|
LogPrint (eLogWarning, "Datagram: decompression failed");
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
std::shared_ptr<I2NPMessage> DatagramDestination::CreateDataMessage (const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort)
|
||
![]()
10 years ago
|
{
|
||
![]()
9 years ago
|
auto msg = NewI2NPMessage ();
|
||
![]()
10 years ago
|
uint8_t * buf = msg->GetPayload ();
|
||
![]()
9 years ago
|
buf += 4; // reserve for length
|
||
|
size_t size = m_Deflator.Deflate (payload, len, buf, msg->maxLen - msg->len);
|
||
|
if (size)
|
||
|
{
|
||
|
htobe32buf (msg->GetPayload (), size); // length
|
||
|
htobe16buf (buf + 4, fromPort); // source port
|
||
|
htobe16buf (buf + 6, toPort); // destination port
|
||
|
buf[9] = i2p::client::PROTOCOL_TYPE_DATAGRAM; // datagram protocol
|
||
|
msg->len += size + 4;
|
||
|
msg->FillI2NPMessageHeader (eI2NPData);
|
||
|
}
|
||
|
else
|
||
|
msg = nullptr;
|
||
![]()
10 years ago
|
return msg;
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
void DatagramDestination::CleanUp ()
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
if (m_Sessions.empty ()) return;
|
||
![]()
8 years ago
|
auto now = i2p::util::GetMillisecondsSinceEpoch();
|
||
|
LogPrint(eLogDebug, "DatagramDestination: clean up sessions");
|
||
![]()
8 years ago
|
std::unique_lock<std::mutex> lock(m_SessionsMutex);
|
||
![]()
8 years ago
|
// for each session ...
|
||
|
for (auto it = m_Sessions.begin (); it != m_Sessions.end (); )
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
// check if expired
|
||
|
if (now - it->second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE)
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
LogPrint(eLogInfo, "DatagramDestination: expiring idle session with ", it->first.ToBase32());
|
||
![]()
8 years ago
|
it->second->Stop ();
|
||
![]()
8 years ago
|
it = m_Sessions.erase (it); // we are expired
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
else
|
||
|
it++;
|
||
![]()
8 years ago
|
}
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
std::shared_ptr<DatagramSession> DatagramDestination::ObtainSession(const i2p::data::IdentHash & identity)
|
||
![]()
8 years ago
|
{
|
||
|
std::shared_ptr<DatagramSession> session = nullptr;
|
||
|
std::lock_guard<std::mutex> lock(m_SessionsMutex);
|
||
![]()
8 years ago
|
auto itr = m_Sessions.find(identity);
|
||
![]()
8 years ago
|
if (itr == m_Sessions.end()) {
|
||
|
// not found, create new session
|
||
![]()
8 years ago
|
session = std::make_shared<DatagramSession>(m_Owner, identity);
|
||
![]()
8 years ago
|
session->Start ();
|
||
![]()
8 years ago
|
m_Sessions[identity] = session;
|
||
![]()
8 years ago
|
} else {
|
||
|
session = itr->second;
|
||
|
}
|
||
|
return session;
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
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;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
DatagramSession::DatagramSession(i2p::client::ClientDestination * localDestination,
|
||
![]()
8 years ago
|
const i2p::data::IdentHash & remoteIdent) :
|
||
![]()
8 years ago
|
m_LocalDestination(localDestination),
|
||
![]()
8 years ago
|
m_RemoteIdent(remoteIdent),
|
||
![]()
8 years ago
|
m_SendQueueTimer(localDestination->GetService()),
|
||
|
m_RequestingLS(false)
|
||
![]()
8 years ago
|
{
|
||
|
}
|
||
|
|
||
|
void DatagramSession::Start ()
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
m_LastUse = i2p::util::GetMillisecondsSinceEpoch ();
|
||
|
ScheduleFlushSendQueue();
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void DatagramSession::Stop ()
|
||
|
{
|
||
|
m_SendQueueTimer.cancel ();
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
void DatagramSession::SendMsg(std::shared_ptr<I2NPMessage> msg)
|
||
|
{
|
||
|
// we used this session
|
||
|
m_LastUse = i2p::util::GetMillisecondsSinceEpoch();
|
||
|
// schedule send
|
||
![]()
8 years ago
|
auto self = shared_from_this();
|
||
|
m_LocalDestination->GetService().post(std::bind(&DatagramSession::HandleSend, self, msg));
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
DatagramSession::Info DatagramSession::GetSessionInfo() const
|
||
|
{
|
||
|
if(!m_RoutingSession)
|
||
![]()
8 years ago
|
return DatagramSession::Info(nullptr, nullptr, m_LastUse);
|
||
![]()
8 years ago
|
|
||
|
auto routingPath = m_RoutingSession->GetSharedRoutingPath();
|
||
|
if (!routingPath)
|
||
![]()
8 years ago
|
return DatagramSession::Info(nullptr, nullptr, m_LastUse);
|
||
![]()
8 years ago
|
auto lease = routingPath->remoteLease;
|
||
|
auto tunnel = routingPath->outboundTunnel;
|
||
|
if(lease)
|
||
|
{
|
||
|
if(tunnel)
|
||
![]()
8 years ago
|
return DatagramSession::Info(lease->tunnelGateway, tunnel->GetEndpointIdentHash(), m_LastUse);
|
||
![]()
8 years ago
|
else
|
||
![]()
8 years ago
|
return DatagramSession::Info(lease->tunnelGateway, nullptr, m_LastUse);
|
||
![]()
8 years ago
|
}
|
||
|
else if(tunnel)
|
||
![]()
8 years ago
|
return DatagramSession::Info(nullptr, tunnel->GetEndpointIdentHash(), m_LastUse);
|
||
![]()
8 years ago
|
else
|
||
![]()
8 years ago
|
return DatagramSession::Info(nullptr, nullptr, m_LastUse);
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void DatagramSession::Ack()
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
m_LastUse = i2p::util::GetMillisecondsSinceEpoch();
|
||
|
auto path = GetSharedRoutingPath();
|
||
|
if(path)
|
||
|
path->updateTime = i2p::util::GetSecondsSinceEpoch ();
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
std::shared_ptr<i2p::garlic::GarlicRoutingPath> DatagramSession::GetSharedRoutingPath ()
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
if(!m_RoutingSession) {
|
||
|
if(!m_RemoteLeaseSet) {
|
||
|
m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent);
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
if(!m_RemoteLeaseSet) {
|
||
|
// no remote lease set
|
||
![]()
8 years ago
|
if(!m_RequestingLS) {
|
||
|
m_RequestingLS = true;
|
||
|
m_LocalDestination->RequestDestination(m_RemoteIdent, std::bind(&DatagramSession::HandleLeaseSetUpdated, this, std::placeholders::_1));
|
||
|
}
|
||
![]()
8 years ago
|
return nullptr;
|
||
|
}
|
||
|
m_RoutingSession = m_LocalDestination->GetRoutingSession(m_RemoteLeaseSet, true);
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
auto path = m_RoutingSession->GetSharedRoutingPath();
|
||
|
if(path) {
|
||
|
if (m_CurrentOutboundTunnel && !m_CurrentOutboundTunnel->IsEstablished()) {
|
||
|
// bad outbound tunnel, switch outbound tunnel
|
||
|
m_CurrentOutboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel(m_CurrentOutboundTunnel);
|
||
|
path->outboundTunnel = m_CurrentOutboundTunnel;
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
if(m_CurrentRemoteLease && m_CurrentRemoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW)) {
|
||
![]()
8 years ago
|
// bad lease, switch to next one
|
||
![]()
8 years ago
|
if(m_RemoteLeaseSet && m_RemoteLeaseSet->IsExpired())
|
||
|
m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent);
|
||
![]()
8 years ago
|
if(m_RemoteLeaseSet) {
|
||
|
auto ls = m_RemoteLeaseSet->GetNonExpiredLeasesExcluding([&](const i2p::data::Lease& l) -> bool {
|
||
![]()
8 years ago
|
return l.tunnelID == m_CurrentRemoteLease->tunnelID;
|
||
![]()
8 years ago
|
});
|
||
|
auto sz = ls.size();
|
||
|
if (sz) {
|
||
|
auto idx = rand() % sz;
|
||
|
m_CurrentRemoteLease = ls[idx];
|
||
|
}
|
||
|
} else {
|
||
|
// no remote lease set?
|
||
|
LogPrint(eLogWarning, "DatagramSession: no cached remote lease set for ", m_RemoteIdent.ToBase32());
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
path->remoteLease = m_CurrentRemoteLease;
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
} else {
|
||
|
// no current path, make one
|
||
|
path = std::make_shared<i2p::garlic::GarlicRoutingPath>();
|
||
|
// switch outbound tunnel if bad
|
||
|
if(m_CurrentOutboundTunnel == nullptr || ! m_CurrentOutboundTunnel->IsEstablished()) {
|
||
|
m_CurrentOutboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel(m_CurrentOutboundTunnel);
|
||
|
}
|
||
|
// switch lease if bad
|
||
![]()
8 years ago
|
if(m_CurrentRemoteLease && m_CurrentRemoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW)) {
|
||
![]()
8 years ago
|
if(!m_RemoteLeaseSet) {
|
||
|
m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent);
|
||
|
}
|
||
|
if(m_RemoteLeaseSet) {
|
||
|
// pick random next good lease
|
||
|
auto ls = m_RemoteLeaseSet->GetNonExpiredLeasesExcluding([&] (const i2p::data::Lease & l) -> bool {
|
||
|
if(m_CurrentRemoteLease)
|
||
|
return l.tunnelGateway == m_CurrentRemoteLease->tunnelGateway;
|
||
|
return false;
|
||
|
});
|
||
|
auto sz = ls.size();
|
||
|
if(sz) {
|
||
|
auto idx = rand() % sz;
|
||
|
m_CurrentRemoteLease = ls[idx];
|
||
|
}
|
||
|
} else {
|
||
|
// no remote lease set currently, bail
|
||
|
LogPrint(eLogWarning, "DatagramSession: no remote lease set found for ", m_RemoteIdent.ToBase32());
|
||
|
return nullptr;
|
||
|
}
|
||
![]()
8 years ago
|
} else if (!m_CurrentRemoteLease) {
|
||
|
if(!m_RemoteLeaseSet) m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent);
|
||
|
if (m_RemoteLeaseSet)
|
||
|
{
|
||
|
auto ls = m_RemoteLeaseSet->GetNonExpiredLeases();
|
||
|
auto sz = ls.size();
|
||
|
if (sz) {
|
||
|
auto idx = rand() % sz;
|
||
|
m_CurrentRemoteLease = ls[idx];
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
path->outboundTunnel = m_CurrentOutboundTunnel;
|
||
|
path->remoteLease = m_CurrentRemoteLease;
|
||
|
m_RoutingSession->SetSharedRoutingPath(path);
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
return path;
|
||
|
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void DatagramSession::HandleLeaseSetUpdated(std::shared_ptr<i2p::data::LeaseSet> ls)
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
m_RequestingLS = false;
|
||
![]()
8 years ago
|
if(!ls) return;
|
||
![]()
8 years ago
|
// 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;
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void DatagramSession::HandleSend(std::shared_ptr<I2NPMessage> msg)
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
m_SendQueue.push_back(msg);
|
||
|
// flush queue right away if full
|
||
|
if(m_SendQueue.size() >= DATAGRAM_SEND_QUEUE_MAX_SIZE) FlushSendQueue();
|
||
|
}
|
||
|
|
||
|
void DatagramSession::FlushSendQueue ()
|
||
|
{
|
||
|
|
||
|
std::vector<i2p::tunnel::TunnelMessageBlock> send;
|
||
|
auto routingPath = GetSharedRoutingPath();
|
||
|
// if we don't have a routing path we will drop all queued messages
|
||
![]()
8 years ago
|
if(routingPath && routingPath->outboundTunnel && routingPath->remoteLease)
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
for (const auto & msg : m_SendQueue)
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
auto m = m_RoutingSession->WrapSingleMessage(msg);
|
||
|
send.push_back(i2p::tunnel::TunnelMessageBlock{i2p::tunnel::eDeliveryTypeTunnel,routingPath->remoteLease->tunnelGateway, routingPath->remoteLease->tunnelID, m});
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
routingPath->outboundTunnel->SendTunnelDataMsg(send);
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
m_SendQueue.clear();
|
||
|
ScheduleFlushSendQueue();
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void DatagramSession::ScheduleFlushSendQueue()
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
boost::posix_time::milliseconds dlt(10);
|
||
![]()
8 years ago
|
m_SendQueueTimer.expires_from_now(dlt);
|
||
![]()
8 years ago
|
auto self = shared_from_this();
|
||
|
m_SendQueueTimer.async_wait([self](const boost::system::error_code & ec) { if(ec) return; self->FlushSendQueue(); });
|
||
![]()
8 years ago
|
}
|
||
![]()
10 years ago
|
}
|
||
|
}
|
||
|
|