i2pd/Datagram.cpp

145 lines
4.7 KiB
C++
Raw Normal View History

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