2014-10-24 00:56:50 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <cryptopp/sha.h>
|
2014-10-22 23:30:25 +04:00
|
|
|
#include <cryptopp/gzip.h>
|
|
|
|
#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
|
|
|
|
{
|
2014-10-24 00:56:50 +04:00
|
|
|
DatagramDestination::DatagramDestination (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-04-04 03:34:37 +03:00
|
|
|
|
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)
|
2014-10-27 23:01:22 +03:00
|
|
|
{
|
|
|
|
uint8_t buf[MAX_DATAGRAM_SIZE];
|
|
|
|
auto identityLen = m_Owner.GetIdentity ().ToBuffer (buf, MAX_DATAGRAM_SIZE);
|
|
|
|
uint8_t * signature = buf + identityLen;
|
|
|
|
auto signatureLen = m_Owner.GetIdentity ().GetSignatureLen ();
|
|
|
|
uint8_t * buf1 = signature + signatureLen;
|
|
|
|
size_t headerLen = identityLen + signatureLen;
|
|
|
|
|
|
|
|
memcpy (buf1, payload, len);
|
|
|
|
if (m_Owner.GetIdentity ().GetSigningKeyType () == i2p::data::SIGNING_KEY_TYPE_DSA_SHA1)
|
|
|
|
{
|
|
|
|
uint8_t hash[32];
|
|
|
|
CryptoPP::SHA256().CalculateDigest (hash, buf1, len);
|
|
|
|
m_Owner.Sign (hash, 32, signature);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_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);
|
2015-03-27 04:23:59 +03:00
|
|
|
auto remote = m_Owner.FindLeaseSet (ident);
|
|
|
|
if (remote)
|
|
|
|
m_Owner.GetService ().post (std::bind (&DatagramDestination::SendMsg, this, msg, remote));
|
|
|
|
else
|
|
|
|
m_Owner.RequestDestination (ident, std::bind (&DatagramDestination::HandleLeaseSetRequestComplete,
|
|
|
|
this, std::placeholders::_1, msg, ident));
|
2014-10-27 23:01:22 +03:00
|
|
|
}
|
|
|
|
|
2015-03-27 04:23:59 +03:00
|
|
|
void DatagramDestination::HandleLeaseSetRequestComplete (bool success, I2NPMessage * msg, i2p::data::IdentHash ident)
|
|
|
|
{
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
auto remote = m_Owner.FindLeaseSet (ident);
|
|
|
|
if (remote)
|
|
|
|
{
|
|
|
|
SendMsg (msg, remote);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DeleteI2NPMessage (msg);
|
|
|
|
}
|
|
|
|
|
2015-01-29 05:37:08 +03:00
|
|
|
void DatagramDestination::SendMsg (I2NPMessage * msg, std::shared_ptr<const i2p::data::LeaseSet> remote)
|
2014-10-24 00:56:50 +04:00
|
|
|
{
|
2014-11-26 21:20:35 +03:00
|
|
|
auto outboundTunnel = m_Owner.GetTunnelPool ()->GetNextOutboundTunnel ();
|
2015-01-29 05:37:08 +03:00
|
|
|
auto leases = remote->GetNonExpiredLeases ();
|
2014-11-26 21:20:35 +03:00
|
|
|
if (!leases.empty () && outboundTunnel)
|
2014-10-24 00:56:50 +04:00
|
|
|
{
|
|
|
|
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
|
|
|
|
uint32_t i = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, leases.size () - 1);
|
2014-10-27 23:01:22 +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,
|
|
|
|
leases[i].tunnelGateway, leases[i].tunnelID,
|
2014-10-27 23:01:22 +03:00
|
|
|
garlic
|
2014-10-24 00:56:50 +04:00
|
|
|
});
|
2014-11-26 21:20:35 +03:00
|
|
|
outboundTunnel->SendTunnelDataMsg (msgs);
|
2014-10-24 00:56:50 +04:00
|
|
|
}
|
|
|
|
else
|
2014-10-27 23:01:22 +03:00
|
|
|
{
|
2014-11-26 21:20:35 +03:00
|
|
|
if (outboundTunnel)
|
|
|
|
LogPrint (eLogWarning, "Failed to send datagram. All leases expired");
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "Failed to send datagram. No outbound tunnels");
|
2014-10-27 23:01:22 +03:00
|
|
|
DeleteI2NPMessage (msg);
|
|
|
|
}
|
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];
|
|
|
|
CryptoPP::SHA256().CalculateDigest (hash, buf + headerLen, len - headerLen);
|
|
|
|
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)
|
2015-03-03 23:31:49 +03:00
|
|
|
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
|
|
|
|
CryptoPP::Gunzip decompressor;
|
|
|
|
decompressor.Put (buf, len);
|
|
|
|
decompressor.MessageEnd();
|
|
|
|
uint8_t uncompressed[MAX_DATAGRAM_SIZE];
|
|
|
|
auto uncompressedLen = decompressor.MaxRetrievable ();
|
|
|
|
if (uncompressedLen <= MAX_DATAGRAM_SIZE)
|
|
|
|
{
|
|
|
|
decompressor.Get (uncompressed, uncompressedLen);
|
2015-03-02 05:08:34 +03:00
|
|
|
HandleDatagram (fromPort, toPort, uncompressed, uncompressedLen);
|
2014-10-22 23:30:25 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Received datagram size ", uncompressedLen, " exceeds max size");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-27 18:29:40 +03:00
|
|
|
I2NPMessage * DatagramDestination::CreateDataMessage (const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort)
|
2014-10-24 00:56:50 +04:00
|
|
|
{
|
|
|
|
I2NPMessage * msg = NewI2NPMessage ();
|
|
|
|
CryptoPP::Gzip compressor; // default level
|
|
|
|
compressor.Put (payload, len);
|
|
|
|
compressor.MessageEnd();
|
|
|
|
int size = compressor.MaxRetrievable ();
|
|
|
|
uint8_t * buf = msg->GetPayload ();
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (buf, size); // length
|
2014-10-24 00:56:50 +04:00
|
|
|
buf += 4;
|
|
|
|
compressor.Get (buf, size);
|
2015-03-27 18:29:40 +03:00
|
|
|
htobe16buf (buf + 4, fromPort); // source port
|
|
|
|
htobe16buf (buf + 6, toPort); // destination port
|
2014-10-24 00:56:50 +04:00
|
|
|
buf[9] = i2p::client::PROTOCOL_TYPE_DATAGRAM; // datagram protocol
|
|
|
|
msg->len += size + 4;
|
|
|
|
FillI2NPMessageHeader (msg, eI2NPData);
|
|
|
|
return msg;
|
|
|
|
}
|
2014-10-22 23:30:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|