i2pd/Datagram.cpp

146 lines
4.6 KiB
C++
Raw Normal View History

2014-10-24 00:56:50 +04:00
#include <string.h>
#include <vector>
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
{
2016-05-25 23:18:02 +03:00
DatagramDestination::DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner):
2014-10-31 23:44:44 +03:00
m_Owner (owner), m_Receiver (nullptr)
2014-10-24 00:56:50 +04:00
{
}
2015-11-03 17:15:49 +03:00
DatagramDestination::~DatagramDestination ()
{
}
2015-03-27 18:29:40 +03:00
void DatagramDestination::SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash& ident, uint16_t fromPort, uint16_t toPort)
{
2016-08-22 05:00:31 +03:00
auto owner = m_Owner;
2016-08-22 04:57:36 +03:00
auto i = owner->GetIdentity();
uint8_t buf[MAX_DATAGRAM_SIZE];
2016-08-22 04:55:00 +03:00
auto identityLen = i->ToBuffer (buf, MAX_DATAGRAM_SIZE);
uint8_t * signature = buf + identityLen;
2016-08-22 04:55:00 +03:00
auto signatureLen = i->GetSignatureLen ();
uint8_t * buf1 = signature + signatureLen;
size_t headerLen = identityLen + signatureLen;
memcpy (buf1, payload, len);
2016-08-22 04:55:00 +03:00
if (i->GetSigningKeyType () == i2p::data::SIGNING_KEY_TYPE_DSA_SHA1)
{
uint8_t hash[32];
2015-11-03 17:15:49 +03:00
SHA256(buf1, len, hash);
2016-08-22 04:51:32 +03:00
owner->Sign (hash, 32, signature);
}
else
2016-08-22 04:51:32 +03:00
owner->Sign (buf1, len, signature);
2015-03-27 04:23:59 +03:00
2015-03-27 18:29:40 +03:00
auto msg = CreateDataMessage (buf, len + headerLen, fromPort, toPort);
2016-08-22 04:51:32 +03:00
auto remote = owner->FindLeaseSet (ident);
2015-03-27 04:23:59 +03:00
if (remote)
2016-08-22 04:51:32 +03:00
owner->GetService ().post (std::bind (&DatagramDestination::SendMsg, this, msg, remote));
2015-03-27 04:23:59 +03:00
else
2016-08-22 04:51:32 +03:00
owner->RequestDestination (ident, std::bind (&DatagramDestination::HandleLeaseSetRequestComplete, this, std::placeholders::_1, msg));
}
2015-11-24 21:09:12 +03:00
void DatagramDestination::HandleLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> remote, std::shared_ptr<I2NPMessage> msg)
2015-03-27 04:23:59 +03:00
{
if (remote)
SendMsg (msg, remote);
2015-03-27 04:23:59 +03:00
}
2015-11-24 21:09:12 +03:00
void DatagramDestination::SendMsg (std::shared_ptr<I2NPMessage> msg, std::shared_ptr<const i2p::data::LeaseSet> remote)
2014-10-24 00:56:50 +04:00
{
2015-11-03 17:15:49 +03:00
auto outboundTunnel = m_Owner->GetTunnelPool ()->GetNextOutboundTunnel ();
2015-01-29 05:37:08 +03:00
auto leases = remote->GetNonExpiredLeases ();
if (!leases.empty () && outboundTunnel)
2014-10-24 00:56:50 +04:00
{
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
2015-11-03 17:15:49 +03:00
uint32_t i = rand () % leases.size ();
2015-11-24 21:09:12 +03:00
auto garlic = m_Owner->WrapMessage (remote, msg, true);
2014-10-24 00:56:50 +04:00
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeTunnel,
2016-02-09 18:46:27 +03:00
leases[i]->tunnelGateway, leases[i]->tunnelID,
2015-06-22 05:29:50 +03:00
garlic
2014-10-24 00:56:50 +04:00
});
outboundTunnel->SendTunnelDataMsg (msgs);
2014-10-24 00:56:50 +04:00
}
else
{
if (outboundTunnel)
LogPrint (eLogWarning, "Failed to send datagram. All leases expired");
else
LogPrint (eLogWarning, "Failed to send datagram. No outbound tunnels");
}
2014-10-24 00:56:50 +04:00
}
2015-03-02 05:08:34 +03:00
void DatagramDestination::HandleDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * 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);
}
2014-10-27 23:30:56 +03:00
else
verified = identity.Verify (buf + headerLen, len - headerLen, signature);
if (verified)
{
2015-04-04 03:34:37 +03:00
auto it = m_ReceiversByPorts.find (toPort);
if (it != m_ReceiversByPorts.end ())
it->second (identity, fromPort, toPort, buf + headerLen, len -headerLen);
else if (m_Receiver != nullptr)
m_Receiver (identity, fromPort, toPort, buf + headerLen, len -headerLen);
2014-10-31 23:44:44 +03:00
else
LogPrint (eLogWarning, "Receiver for datagram is not set");
2014-10-27 23:30:56 +03:00
}
else
2014-10-31 23:44:44 +03:00
LogPrint (eLogWarning, "Datagram signature verification failed");
2014-10-27 23:30:56 +03:00
}
2015-03-02 05:08:34 +03:00
void DatagramDestination::HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
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)
2015-03-02 05:08:34 +03:00
HandleDatagram (fromPort, toPort, uncompressed, uncompressedLen);
2014-10-22 23:30:25 +04:00
}
2015-11-24 21:09:12 +03:00
std::shared_ptr<I2NPMessage> DatagramDestination::CreateDataMessage (const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort)
2014-10-24 00:56:50 +04:00
{
2015-11-24 21:09:12 +03:00
auto msg = NewI2NPMessage ();
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
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;
2014-10-24 00:56:50 +04:00
return msg;
}
2014-10-22 23:30:25 +04:00
}
}