i2pd/libi2pd/I2NPProtocol.cpp

693 lines
22 KiB
C++
Raw Normal View History

2013-10-27 15:20:29 +00:00
#include <string.h>
2014-07-06 21:48:16 +00:00
#include <atomic>
2015-11-03 14:15:49 +00:00
#include "Base.h"
#include "Log.h"
#include "Crypto.h"
2014-01-20 14:36:20 +00:00
#include "I2PEndian.h"
2013-10-27 15:20:29 +00:00
#include "Timestamp.h"
#include "RouterContext.h"
#include "NetDb.hpp"
2013-10-27 15:20:29 +00:00
#include "Tunnel.h"
#include "Transports.h"
2013-12-14 01:07:35 +00:00
#include "Garlic.h"
2013-10-27 15:20:29 +00:00
#include "I2NPProtocol.h"
#include "version.h"
2013-10-27 15:20:29 +00:00
using namespace i2p::transport;
2013-10-27 15:20:29 +00:00
namespace i2p
{
2015-11-24 18:09:12 +00:00
std::shared_ptr<I2NPMessage> NewI2NPMessage ()
2013-10-27 15:20:29 +00:00
{
2015-11-24 18:09:12 +00:00
return std::make_shared<I2NPMessageBuffer<I2NP_MAX_MESSAGE_SIZE> >();
2013-10-27 15:20:29 +00:00
}
2018-01-06 03:48:51 +00:00
2015-11-24 18:09:12 +00:00
std::shared_ptr<I2NPMessage> NewI2NPShortMessage ()
2014-07-30 20:52:35 +00:00
{
2015-11-24 18:09:12 +00:00
return std::make_shared<I2NPMessageBuffer<I2NP_MAX_SHORT_MESSAGE_SIZE> >();
2014-07-30 20:52:35 +00:00
}
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage ()
{
auto msg = new I2NPMessageBuffer<i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + 34>(); // reserved for alignment and NTCP 16 + 6 + 12
msg->Align (12);
return std::shared_ptr<I2NPMessage>(msg);
2018-01-06 03:48:51 +00:00
}
2015-11-24 18:09:12 +00:00
std::shared_ptr<I2NPMessage> NewI2NPMessage (size_t len)
2014-07-30 22:20:42 +00:00
{
2018-10-10 15:31:55 +00:00
return (len < I2NP_MAX_SHORT_MESSAGE_SIZE - I2NP_HEADER_SIZE - 2) ? NewI2NPShortMessage () : NewI2NPMessage ();
2018-01-06 03:48:51 +00:00
}
void I2NPMessage::FillI2NPMessageHeader (I2NPMessageType msgType, uint32_t replyMsgID)
2013-10-27 15:20:29 +00:00
{
SetTypeID (msgType);
2015-11-03 14:15:49 +00:00
if (!replyMsgID) RAND_bytes ((uint8_t *)&replyMsgID, 4);
2018-01-06 03:48:51 +00:00
SetMsgID (replyMsgID);
SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + I2NP_MESSAGE_EXPIRATION_TIMEOUT);
UpdateSize ();
UpdateChks ();
2018-01-06 03:48:51 +00:00
}
void I2NPMessage::RenewI2NPMessageHeader ()
{
2015-11-03 14:15:49 +00:00
uint32_t msgID;
RAND_bytes ((uint8_t *)&msgID, 4);
SetMsgID (msgID);
2018-01-06 03:48:51 +00:00
SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + I2NP_MESSAGE_EXPIRATION_TIMEOUT);
}
2016-01-19 02:13:43 +00:00
bool I2NPMessage::IsExpired () const
{
2016-01-19 16:16:50 +00:00
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
2018-01-06 03:48:51 +00:00
auto exp = GetExpiration ();
2016-01-19 16:16:50 +00:00
return (ts > exp + I2NP_MESSAGE_CLOCK_SKEW) || (ts < exp - 3*I2NP_MESSAGE_CLOCK_SKEW); // check if expired or too far in future
2018-01-06 03:48:51 +00:00
}
2016-01-05 19:29:18 +00:00
std::shared_ptr<I2NPMessage> CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, size_t len, uint32_t replyMsgID)
2013-10-27 15:20:29 +00:00
{
2015-11-24 18:09:12 +00:00
auto msg = NewI2NPMessage (len);
2016-01-05 19:29:18 +00:00
if (msg->Concat (buf, len) < len)
LogPrint (eLogError, "I2NP: message length ", len, " exceeds max length ", msg->maxLen);
msg->FillI2NPMessageHeader (msgType, replyMsgID);
2013-10-27 15:20:29 +00:00
return msg;
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:20:29 +00:00
2016-01-05 19:29:18 +00:00
std::shared_ptr<I2NPMessage> CreateI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from)
2013-11-10 23:23:26 +00:00
{
2015-11-24 18:09:12 +00:00
auto msg = NewI2NPMessage ();
2015-05-11 16:53:08 +00:00
if (msg->offset + len < msg->maxLen)
{
memcpy (msg->GetBuffer (), buf, len);
msg->len = msg->offset + len;
msg->from = from;
}
else
2015-12-17 07:11:36 +00:00
LogPrint (eLogError, "I2NP: message length ", len, " exceeds max length");
2015-11-24 18:09:12 +00:00
return msg;
2018-01-06 03:48:51 +00:00
}
2016-01-31 23:27:47 +00:00
std::shared_ptr<I2NPMessage> CopyI2NPMessage (std::shared_ptr<I2NPMessage> msg)
{
if (!msg) return nullptr;
auto newMsg = NewI2NPMessage (msg->len);
newMsg->offset = msg->offset;
*newMsg = *msg;
return newMsg;
2018-01-06 03:48:51 +00:00
}
2015-06-24 14:45:58 +00:00
std::shared_ptr<I2NPMessage> CreateDeliveryStatusMsg (uint32_t msgID)
2013-10-27 15:20:29 +00:00
{
2015-11-24 18:09:12 +00:00
auto m = NewI2NPShortMessage ();
uint8_t * buf = m->GetPayload ();
2014-02-24 20:16:39 +00:00
if (msgID)
{
htobe32buf (buf + DELIVERY_STATUS_MSGID_OFFSET, msgID);
htobe64buf (buf + DELIVERY_STATUS_TIMESTAMP_OFFSET, i2p::util::GetMillisecondsSinceEpoch ());
2014-02-24 20:16:39 +00:00
}
else // for SSU establishment
{
2015-11-03 14:15:49 +00:00
RAND_bytes ((uint8_t *)&msgID, 4);
htobe32buf (buf + DELIVERY_STATUS_MSGID_OFFSET, msgID);
2018-01-06 03:48:51 +00:00
htobe64buf (buf + DELIVERY_STATUS_TIMESTAMP_OFFSET, i2p::context.GetNetID ());
}
m->len += DELIVERY_STATUS_SIZE;
m->FillI2NPMessageHeader (eI2NPDeliveryStatus);
2015-11-24 18:09:12 +00:00
return m;
2013-10-27 15:20:29 +00:00
}
2018-01-06 03:48:51 +00:00
std::shared_ptr<I2NPMessage> CreateRouterInfoDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
2015-01-04 22:25:16 +00:00
uint32_t replyTunnelID, bool exploratory, std::set<i2p::data::IdentHash> * excludedPeers)
2013-10-27 15:20:29 +00:00
{
2015-11-24 18:09:12 +00:00
auto m = excludedPeers ? NewI2NPMessage () : NewI2NPShortMessage ();
2013-11-19 01:37:38 +00:00
uint8_t * buf = m->GetPayload ();
memcpy (buf, key, 32); // key
buf += 32;
memcpy (buf, from, 32); // from
buf += 32;
2018-01-06 03:48:51 +00:00
uint8_t flag = exploratory ? DATABASE_LOOKUP_TYPE_EXPLORATORY_LOOKUP : DATABASE_LOOKUP_TYPE_ROUTERINFO_LOOKUP;
2013-11-19 01:37:38 +00:00
if (replyTunnelID)
2013-10-27 15:20:29 +00:00
{
2015-02-02 16:06:36 +00:00
*buf = flag | DATABASE_LOOKUP_DELIVERY_FLAG; // set delivery flag
htobe32buf (buf+1, replyTunnelID);
2013-11-19 01:37:38 +00:00
buf += 5;
}
else
2018-01-06 03:48:51 +00:00
{
2015-01-04 22:25:16 +00:00
*buf = flag; // flag
2013-11-19 01:37:38 +00:00
buf++;
2018-01-06 03:48:51 +00:00
}
2015-01-04 22:25:16 +00:00
if (excludedPeers)
2013-11-19 01:37:38 +00:00
{
2015-01-04 22:25:16 +00:00
int cnt = excludedPeers->size ();
htobe16buf (buf, cnt);
2013-11-19 01:37:38 +00:00
buf += 2;
2015-01-04 22:25:16 +00:00
for (auto& it: *excludedPeers)
2014-01-05 14:53:44 +00:00
{
2015-01-04 22:25:16 +00:00
memcpy (buf, it, 32);
buf += 32;
2018-01-06 03:48:51 +00:00
}
2015-01-04 22:25:16 +00:00
}
else
2018-01-06 03:48:51 +00:00
{
2015-01-04 22:25:16 +00:00
// nothing to exclude
htobuf16 (buf, 0);
buf += 2;
2018-01-06 03:48:51 +00:00
}
m->len += (buf - m->GetPayload ());
m->FillI2NPMessageHeader (eI2NPDatabaseLookup);
2018-01-06 03:48:51 +00:00
return m;
}
2013-10-27 15:20:29 +00:00
2018-01-06 03:48:51 +00:00
std::shared_ptr<I2NPMessage> CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest,
2014-12-30 17:25:08 +00:00
const std::set<i2p::data::IdentHash>& excludedFloodfills,
2016-02-26 21:17:29 +00:00
std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel, const uint8_t * replyKey, const uint8_t * replyTag)
2014-12-30 17:25:08 +00:00
{
2015-07-05 11:59:38 +00:00
int cnt = excludedFloodfills.size ();
2015-11-24 18:09:12 +00:00
auto m = cnt > 0 ? NewI2NPMessage () : NewI2NPShortMessage ();
2014-12-30 17:25:08 +00:00
uint8_t * buf = m->GetPayload ();
memcpy (buf, dest, 32); // key
buf += 32;
memcpy (buf, replyTunnel->GetNextIdentHash (), 32); // reply tunnel GW
buf += 32;
*buf = DATABASE_LOOKUP_DELIVERY_FLAG | DATABASE_LOOKUP_ENCRYPTION_FLAG | DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP; // flags
2016-07-15 17:54:34 +00:00
buf ++;
htobe32buf (buf, replyTunnel->GetNextTunnelID ()); // reply tunnel ID
buf += 4;
2018-01-06 03:48:51 +00:00
2014-12-30 17:25:08 +00:00
// excluded
htobe16buf (buf, cnt);
buf += 2;
if (cnt > 0)
{
for (auto& it: excludedFloodfills)
{
memcpy (buf, it, 32);
buf += 32;
}
2018-01-06 03:48:51 +00:00
}
2014-12-30 17:25:08 +00:00
// encryption
memcpy (buf, replyKey, 32);
buf[32] = uint8_t( 1 ); // 1 tag
2014-12-30 17:25:08 +00:00
memcpy (buf + 33, replyTag, 32);
buf += 65;
2018-01-06 03:48:51 +00:00
m->len += (buf - m->GetPayload ());
m->FillI2NPMessageHeader (eI2NPDatabaseLookup);
2018-01-06 03:48:51 +00:00
return m;
}
2014-12-30 17:25:08 +00:00
2018-01-06 03:48:51 +00:00
std::shared_ptr<I2NPMessage> CreateDatabaseSearchReply (const i2p::data::IdentHash& ident,
2015-02-02 00:58:26 +00:00
std::vector<i2p::data::IdentHash> routers)
2014-01-06 03:21:59 +00:00
{
2015-11-24 18:09:12 +00:00
auto m = NewI2NPShortMessage ();
2014-01-06 03:21:59 +00:00
uint8_t * buf = m->GetPayload ();
2014-07-25 02:01:07 +00:00
size_t len = 0;
2014-01-06 03:21:59 +00:00
memcpy (buf, ident, 32);
2014-07-25 02:01:07 +00:00
len += 32;
2018-01-06 03:48:51 +00:00
buf[len] = routers.size ();
2014-07-25 02:01:07 +00:00
len++;
2016-08-05 18:23:54 +00:00
for (const auto& it: routers)
2014-07-25 02:01:07 +00:00
{
2015-02-02 00:58:26 +00:00
memcpy (buf + len, it, 32);
2014-07-25 02:01:07 +00:00
len += 32;
2018-01-06 03:48:51 +00:00
}
2014-07-25 02:01:07 +00:00
memcpy (buf + len, i2p::context.GetRouterInfo ().GetIdentHash (), 32);
2018-01-06 03:48:51 +00:00
len += 32;
2014-07-25 02:01:07 +00:00
m->len += len;
m->FillI2NPMessageHeader (eI2NPDatabaseSearchReply);
2018-01-06 03:48:51 +00:00
return m;
}
2015-11-03 14:15:49 +00:00
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::RouterInfo> router, uint32_t replyToken)
2013-10-27 15:20:29 +00:00
{
if (!router) // we send own RouterInfo
2015-04-07 19:15:27 +00:00
router = context.GetSharedRouterInfo ();
2015-11-24 18:09:12 +00:00
auto m = NewI2NPShortMessage ();
2018-01-06 03:48:51 +00:00
uint8_t * payload = m->GetPayload ();
2013-10-27 15:20:29 +00:00
2015-01-03 02:11:40 +00:00
memcpy (payload + DATABASE_STORE_KEY_OFFSET, router->GetIdentHash (), 32);
payload[DATABASE_STORE_TYPE_OFFSET] = 0; // RouterInfo
htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, replyToken);
uint8_t * buf = payload + DATABASE_STORE_HEADER_SIZE;
if (replyToken)
{
memset (buf, 0, 4); // zero tunnelID means direct reply
buf += 4;
memcpy (buf, router->GetIdentHash (), 32);
buf += 32;
2018-01-06 03:48:51 +00:00
}
2015-11-03 14:15:49 +00:00
uint8_t * sizePtr = buf;
2013-11-20 12:46:09 +00:00
buf += 2;
m->len += (buf - payload); // payload size
2015-11-03 14:15:49 +00:00
i2p::data::GzipDeflator deflator;
size_t size = deflator.Deflate (router->GetBuffer (), router->GetBufferLen (), buf, m->maxLen -m->len);
if (size)
2018-01-06 03:48:51 +00:00
{
2015-11-03 14:15:49 +00:00
htobe16buf (sizePtr, size); // size
m->len += size;
2018-01-06 03:48:51 +00:00
}
2015-11-03 14:15:49 +00:00
else
m = nullptr;
if (m)
m->FillI2NPMessageHeader (eI2NPDatabaseStore);
2013-10-27 15:20:29 +00:00
return m;
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:20:29 +00:00
2016-05-25 19:10:28 +00:00
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::LeaseSet> leaseSet)
{
if (!leaseSet) return nullptr;
auto m = NewI2NPShortMessage ();
2018-01-06 03:48:51 +00:00
uint8_t * payload = m->GetPayload ();
2016-05-25 19:10:28 +00:00
memcpy (payload + DATABASE_STORE_KEY_OFFSET, leaseSet->GetIdentHash (), 32);
payload[DATABASE_STORE_TYPE_OFFSET] = 1; // LeaseSet
htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0);
size_t size = DATABASE_STORE_HEADER_SIZE;
memcpy (payload + size, leaseSet->GetBuffer (), leaseSet->GetBufferLen ());
size += leaseSet->GetBufferLen ();
m->len += size;
m->FillI2NPMessageHeader (eI2NPDatabaseStore);
return m;
}
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::LocalLeaseSet> leaseSet, uint32_t replyToken, std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel)
2014-07-29 18:31:55 +00:00
{
if (!leaseSet) return nullptr;
2015-11-24 18:09:12 +00:00
auto m = NewI2NPShortMessage ();
2018-01-06 03:48:51 +00:00
uint8_t * payload = m->GetPayload ();
2015-01-03 02:11:40 +00:00
memcpy (payload + DATABASE_STORE_KEY_OFFSET, leaseSet->GetIdentHash (), 32);
payload[DATABASE_STORE_TYPE_OFFSET] = 1; // LeaseSet
htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, replyToken);
size_t size = DATABASE_STORE_HEADER_SIZE;
if (replyToken && replyTunnel)
2014-08-20 15:12:53 +00:00
{
if (replyTunnel)
2014-08-20 15:12:53 +00:00
{
htobe32buf (payload + size, replyTunnel->GetNextTunnelID ());
2014-08-20 15:12:53 +00:00
size += 4; // reply tunnelID
memcpy (payload + size, replyTunnel->GetNextIdentHash (), 32);
2014-08-20 15:12:53 +00:00
size += 32; // reply tunnel gateway
}
else
2015-01-03 02:11:40 +00:00
htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0);
2014-08-20 15:12:53 +00:00
}
memcpy (payload + size, leaseSet->GetBuffer (), leaseSet->GetBufferLen ());
size += leaseSet->GetBufferLen ();
m->len += size;
m->FillI2NPMessageHeader (eI2NPDatabaseStore);
2014-07-29 18:31:55 +00:00
return m;
}
bool IsRouterInfoMsg (std::shared_ptr<I2NPMessage> msg)
{
if (!msg || msg->GetTypeID () != eI2NPDatabaseStore) return false;
return !msg->GetPayload ()[DATABASE_STORE_TYPE_OFFSET]; // 0- RouterInfo
2018-01-06 03:48:51 +00:00
}
2016-04-20 18:53:50 +00:00
static uint16_t g_MaxNumTransitTunnels = DEFAULT_MAX_NUM_TRANSIT_TUNNELS; // TODO:
void SetMaxNumTransitTunnels (uint16_t maxNumTransitTunnels)
{
if (maxNumTransitTunnels > 0 && maxNumTransitTunnels <= 10000 && g_MaxNumTransitTunnels != maxNumTransitTunnels)
{
LogPrint (eLogDebug, "I2NP: Max number of transit tunnels set to ", maxNumTransitTunnels);
g_MaxNumTransitTunnels = maxNumTransitTunnels;
}
}
2015-01-04 14:33:19 +00:00
bool HandleBuildRequestRecords (int num, uint8_t * records, uint8_t * clearText)
2014-04-30 18:08:57 +00:00
{
for (int i = 0; i < num; i++)
2018-01-06 03:48:51 +00:00
{
uint8_t * record = records + i*TUNNEL_BUILD_RECORD_SIZE;
if (!memcmp (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)i2p::context.GetRouterInfo ().GetIdentHash (), 16))
2018-01-06 03:48:51 +00:00
{
2015-12-17 07:11:36 +00:00
LogPrint (eLogDebug, "I2NP: Build request record ", i, " is ours");
BN_CTX * ctx = BN_CTX_new ();
i2p::context.DecryptTunnelBuildRecord (record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, clearText, ctx);
BN_CTX_free (ctx);
2018-01-06 03:48:51 +00:00
// replace record to reply
if (i2p::context.AcceptsTunnels () &&
2016-04-20 18:53:50 +00:00
i2p::tunnel::tunnels.GetTransitTunnels ().size () <= g_MaxNumTransitTunnels &&
!i2p::transport::transports.IsBandwidthExceeded () &&
!i2p::transport::transports.IsTransitBandwidthExceeded ())
2018-01-06 03:48:51 +00:00
{
2016-03-01 20:22:36 +00:00
auto transitTunnel = i2p::tunnel::CreateTransitTunnel (
2018-01-06 03:48:51 +00:00
bufbe32toh (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
2015-01-04 14:33:19 +00:00
bufbe32toh (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
2018-01-06 03:48:51 +00:00
clearText + BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET,
clearText + BUILD_REQUEST_RECORD_IV_KEY_OFFSET,
clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] & 0x80,
2015-01-04 14:33:19 +00:00
clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET ] & 0x40);
i2p::tunnel::tunnels.AddTransitTunnel (transitTunnel);
record[BUILD_RESPONSE_RECORD_RET_OFFSET] = 0;
}
else
record[BUILD_RESPONSE_RECORD_RET_OFFSET] = 30; // always reject with bandwidth reason (30)
2018-01-06 03:48:51 +00:00
2014-04-30 18:08:57 +00:00
//TODO: fill filler
2015-11-03 14:15:49 +00:00
SHA256 (record + BUILD_RESPONSE_RECORD_PADDING_OFFSET, BUILD_RESPONSE_RECORD_PADDING_SIZE + 1, // + 1 byte of ret
2018-01-06 03:48:51 +00:00
record + BUILD_RESPONSE_RECORD_HASH_OFFSET);
2014-04-30 18:08:57 +00:00
// encrypt reply
2014-05-15 22:58:26 +00:00
i2p::crypto::CBCEncryption encryption;
2014-04-30 18:08:57 +00:00
for (int j = 0; j < num; j++)
{
2015-01-04 14:33:19 +00:00
encryption.SetKey (clearText + BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET);
encryption.SetIV (clearText + BUILD_REQUEST_RECORD_REPLY_IV_OFFSET);
uint8_t * reply = records + j*TUNNEL_BUILD_RECORD_SIZE;
2018-01-06 03:48:51 +00:00
encryption.Encrypt(reply, TUNNEL_BUILD_RECORD_SIZE, reply);
2014-04-30 18:08:57 +00:00
}
return true;
2018-01-06 03:48:51 +00:00
}
}
2014-04-30 18:08:57 +00:00
return false;
}
2013-10-27 15:20:29 +00:00
void HandleVariableTunnelBuildMsg (uint32_t replyMsgID, uint8_t * buf, size_t len)
2018-01-06 03:48:51 +00:00
{
2013-10-27 15:20:29 +00:00
int num = buf[0];
2015-12-17 07:11:36 +00:00
LogPrint (eLogDebug, "I2NP: VariableTunnelBuild ", num, " records");
2016-01-30 15:35:32 +00:00
if (len < num*BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE + 1)
{
LogPrint (eLogError, "VaribleTunnelBuild message of ", num, " records is too short ", len);
return;
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:20:29 +00:00
auto tunnel = i2p::tunnel::tunnels.GetPendingInboundTunnel (replyMsgID);
2013-10-27 15:20:29 +00:00
if (tunnel)
{
2013-11-19 01:37:38 +00:00
// endpoint of inbound tunnel
2015-12-17 07:11:36 +00:00
LogPrint (eLogDebug, "I2NP: VariableTunnelBuild reply for tunnel ", tunnel->GetTunnelID ());
2013-11-19 01:37:38 +00:00
if (tunnel->HandleTunnelBuildResponse (buf, len))
{
2015-12-17 07:11:36 +00:00
LogPrint (eLogInfo, "I2NP: Inbound tunnel ", tunnel->GetTunnelID (), " has been created");
2018-01-06 03:48:51 +00:00
tunnel->SetState (i2p::tunnel::eTunnelStateEstablished);
i2p::tunnel::tunnels.AddInboundTunnel (tunnel);
2013-11-19 01:37:38 +00:00
}
else
2014-09-26 14:15:34 +00:00
{
2015-12-17 07:11:36 +00:00
LogPrint (eLogInfo, "I2NP: Inbound tunnel ", tunnel->GetTunnelID (), " has been declined");
2018-01-06 03:48:51 +00:00
tunnel->SetState (i2p::tunnel::eTunnelStateBuildFailed);
2014-09-26 14:15:34 +00:00
}
2013-10-27 15:20:29 +00:00
}
else
{
2018-01-06 03:48:51 +00:00
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
if (HandleBuildRequestRecords (num, buf + 1, clearText))
2014-04-30 18:08:57 +00:00
{
2015-01-04 14:33:19 +00:00
if (clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] & 0x40) // we are endpoint of outboud tunnel
2014-04-30 18:08:57 +00:00
{
2018-01-06 03:48:51 +00:00
// so we send it to reply tunnel
transports.SendMessage (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
2015-11-24 18:09:12 +00:00
CreateTunnelGatewayMsg (bufbe32toh (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
2018-01-06 03:48:51 +00:00
eI2NPVariableTunnelBuildReply, buf, len,
bufbe32toh (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
}
else
transports.SendMessage (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
CreateI2NPMessage (eI2NPVariableTunnelBuild, buf, len,
2015-11-24 18:09:12 +00:00
bufbe32toh (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
2018-01-06 03:48:51 +00:00
}
}
2013-10-27 15:20:29 +00:00
}
2014-04-30 18:08:57 +00:00
void HandleTunnelBuildMsg (uint8_t * buf, size_t len)
{
2016-01-30 15:35:32 +00:00
if (len < NUM_TUNNEL_BUILD_RECORDS*BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE)
{
LogPrint (eLogError, "TunnelBuild message is too short ", len);
return;
2018-01-06 03:48:51 +00:00
}
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
if (HandleBuildRequestRecords (NUM_TUNNEL_BUILD_RECORDS, buf, clearText))
2014-04-30 18:08:57 +00:00
{
2015-01-04 14:33:19 +00:00
if (clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] & 0x40) // we are endpoint of outbound tunnel
2014-04-30 18:08:57 +00:00
{
2018-01-06 03:48:51 +00:00
// so we send it to reply tunnel
transports.SendMessage (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
2015-11-24 18:09:12 +00:00
CreateTunnelGatewayMsg (bufbe32toh (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
2018-01-06 03:48:51 +00:00
eI2NPTunnelBuildReply, buf, len,
bufbe32toh (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
}
else
transports.SendMessage (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
CreateI2NPMessage (eI2NPTunnelBuild, buf, len,
2015-11-24 18:09:12 +00:00
bufbe32toh (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
2018-01-06 03:48:51 +00:00
}
2014-04-30 18:08:57 +00:00
}
2013-10-27 15:20:29 +00:00
void HandleVariableTunnelBuildReplyMsg (uint32_t replyMsgID, uint8_t * buf, size_t len)
2018-01-06 03:48:51 +00:00
{
2016-01-30 15:35:32 +00:00
int num = buf[0];
LogPrint (eLogDebug, "I2NP: VariableTunnelBuildReplyMsg of ", num, " records replyMsgID=", replyMsgID);
if (len < num*BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE + 1)
{
LogPrint (eLogError, "VaribleTunnelBuildReply message of ", num, " records is too short ", len);
return;
2018-01-06 03:48:51 +00:00
}
auto tunnel = i2p::tunnel::tunnels.GetPendingOutboundTunnel (replyMsgID);
2013-10-27 15:20:29 +00:00
if (tunnel)
2018-01-06 03:48:51 +00:00
{
2013-11-19 01:37:38 +00:00
// reply for outbound tunnel
if (tunnel->HandleTunnelBuildResponse (buf, len))
2018-01-06 03:48:51 +00:00
{
2015-12-17 07:11:36 +00:00
LogPrint (eLogInfo, "I2NP: Outbound tunnel ", tunnel->GetTunnelID (), " has been created");
2018-01-06 03:48:51 +00:00
tunnel->SetState (i2p::tunnel::eTunnelStateEstablished);
i2p::tunnel::tunnels.AddOutboundTunnel (tunnel);
2018-01-06 03:48:51 +00:00
}
2013-11-19 01:37:38 +00:00
else
2014-09-26 14:15:34 +00:00
{
2015-12-17 07:11:36 +00:00
LogPrint (eLogInfo, "I2NP: Outbound tunnel ", tunnel->GetTunnelID (), " has been declined");
2018-01-06 03:48:51 +00:00
tunnel->SetState (i2p::tunnel::eTunnelStateBuildFailed);
2014-09-26 14:15:34 +00:00
}
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:20:29 +00:00
else
2015-12-17 07:11:36 +00:00
LogPrint (eLogWarning, "I2NP: Pending tunnel for message ", replyMsgID, " not found");
2013-10-27 15:20:29 +00:00
}
2015-11-24 18:09:12 +00:00
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (const uint8_t * buf)
2013-10-27 15:20:29 +00:00
{
auto msg = NewI2NPTunnelMessage ();
2018-01-06 03:48:51 +00:00
msg->Concat (buf, i2p::tunnel::TUNNEL_DATA_MSG_SIZE);
msg->FillI2NPMessageHeader (eI2NPTunnelData);
2013-10-27 15:20:29 +00:00
return msg;
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:20:29 +00:00
2018-01-06 03:48:51 +00:00
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload)
2013-10-27 15:20:29 +00:00
{
auto msg = NewI2NPTunnelMessage ();
htobe32buf (msg->GetPayload (), tunnelID);
2016-01-05 19:29:18 +00:00
msg->len += 4; // tunnelID
msg->Concat (payload, i2p::tunnel::TUNNEL_DATA_MSG_SIZE - 4);
msg->FillI2NPMessageHeader (eI2NPTunnelData);
2013-10-27 15:20:29 +00:00
return msg;
2018-01-06 03:48:51 +00:00
}
std::shared_ptr<I2NPMessage> CreateEmptyTunnelDataMsg ()
{
auto msg = NewI2NPTunnelMessage ();
2018-01-06 03:48:51 +00:00
msg->len += i2p::tunnel::TUNNEL_DATA_MSG_SIZE;
2015-11-24 18:09:12 +00:00
return msg;
2018-01-06 03:48:51 +00:00
}
2015-11-24 18:09:12 +00:00
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len)
2013-11-10 23:23:26 +00:00
{
2015-11-24 18:09:12 +00:00
auto msg = NewI2NPMessage (len);
2015-01-01 23:53:44 +00:00
uint8_t * payload = msg->GetPayload ();
htobe32buf (payload + TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET, tunnelID);
htobe16buf (payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET, len);
2016-01-05 19:29:18 +00:00
msg->len += TUNNEL_GATEWAY_HEADER_SIZE;
if (msg->Concat (buf, len) < len)
2018-01-06 03:48:51 +00:00
LogPrint (eLogError, "I2NP: tunnel gateway buffer overflow ", msg->maxLen);
msg->FillI2NPMessageHeader (eI2NPTunnelGateway);
2013-11-10 23:23:26 +00:00
return msg;
2018-01-06 03:48:51 +00:00
}
2013-11-10 23:23:26 +00:00
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, std::shared_ptr<I2NPMessage> msg)
2013-11-10 23:23:26 +00:00
{
2015-01-01 23:53:44 +00:00
if (msg->offset >= I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE)
2013-11-10 23:23:26 +00:00
{
// message is capable to be used without copying
2015-01-01 23:53:44 +00:00
uint8_t * payload = msg->GetBuffer () - TUNNEL_GATEWAY_HEADER_SIZE;
htobe32buf (payload + TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET, tunnelID);
2013-11-10 23:23:26 +00:00
int len = msg->GetLength ();
2015-01-01 23:53:44 +00:00
htobe16buf (payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET, len);
msg->offset -= (I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE);
msg->len = msg->offset + I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE +len;
2018-01-06 03:48:51 +00:00
msg->FillI2NPMessageHeader (eI2NPTunnelGateway);
2013-11-10 23:23:26 +00:00
return msg;
}
else
2018-01-06 03:48:51 +00:00
return CreateTunnelGatewayMsg (tunnelID, msg->GetBuffer (), msg->GetLength ());
2013-11-10 23:23:26 +00:00
}
2018-01-06 03:48:51 +00:00
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType,
2013-11-10 23:23:26 +00:00
const uint8_t * buf, size_t len, uint32_t replyMsgID)
{
2015-11-24 18:09:12 +00:00
auto msg = NewI2NPMessage (len);
2015-01-01 23:53:44 +00:00
size_t gatewayMsgOffset = I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE;
2013-11-10 23:23:26 +00:00
msg->offset += gatewayMsgOffset;
msg->len += gatewayMsgOffset;
2016-01-05 19:29:18 +00:00
if (msg->Concat (buf, len) < len)
LogPrint (eLogError, "I2NP: tunnel gateway buffer overflow ", msg->maxLen);
msg->FillI2NPMessageHeader (msgType, replyMsgID); // create content message
2013-11-10 23:23:26 +00:00
len = msg->GetLength ();
msg->offset -= gatewayMsgOffset;
2015-01-01 23:53:44 +00:00
uint8_t * payload = msg->GetPayload ();
htobe32buf (payload + TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET, tunnelID);
htobe16buf (payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET, len);
msg->FillI2NPMessageHeader (eI2NPTunnelGateway); // gateway message
2013-11-10 23:23:26 +00:00
return msg;
2018-01-06 03:48:51 +00:00
}
2013-12-14 01:07:35 +00:00
2017-12-01 17:57:05 +00:00
size_t GetI2NPMessageLength (const uint8_t * msg, size_t len)
2013-12-14 01:07:35 +00:00
{
2017-12-01 17:57:05 +00:00
if (len < I2NP_HEADER_SIZE_OFFSET + 2)
{
LogPrint (eLogError, "I2NP: message length ", len, " is smaller than header");
return len;
2018-01-06 03:48:51 +00:00
}
2017-12-01 17:57:05 +00:00
auto l = bufbe16toh (msg + I2NP_HEADER_SIZE_OFFSET) + I2NP_HEADER_SIZE;
if (l > len)
{
LogPrint (eLogError, "I2NP: message length ", l, " exceeds buffer length ", len);
l = len;
2018-01-06 03:48:51 +00:00
}
2017-12-01 17:57:05 +00:00
return l;
2018-01-06 03:48:51 +00:00
}
void HandleI2NPMessage (uint8_t * msg, size_t len)
2013-10-27 15:20:29 +00:00
{
2017-12-01 17:57:05 +00:00
if (len < I2NP_HEADER_SIZE)
{
LogPrint (eLogError, "I2NP: message length ", len, " is smaller than header");
return;
2018-01-06 03:48:51 +00:00
}
2015-01-02 04:00:33 +00:00
uint8_t typeID = msg[I2NP_HEADER_TYPEID_OFFSET];
2018-01-06 03:48:51 +00:00
uint32_t msgID = bufbe32toh (msg + I2NP_HEADER_MSGID_OFFSET);
2015-12-17 07:11:36 +00:00
LogPrint (eLogDebug, "I2NP: msg received len=", len,", type=", (int)typeID, ", msgID=", (unsigned int)msgID);
2015-01-02 04:00:33 +00:00
uint8_t * buf = msg + I2NP_HEADER_SIZE;
2017-12-01 17:57:05 +00:00
auto size = bufbe16toh (msg + I2NP_HEADER_SIZE_OFFSET);
len -= I2NP_HEADER_SIZE;
if (size > len)
{
LogPrint (eLogError, "I2NP: payload size ", size, " exceeds buffer length ", len);
size = len;
}
2015-01-02 04:00:33 +00:00
switch (typeID)
2018-01-06 03:48:51 +00:00
{
2013-10-27 15:20:29 +00:00
case eI2NPVariableTunnelBuild:
HandleVariableTunnelBuildMsg (msgID, buf, size);
2018-01-06 03:48:51 +00:00
break;
2013-10-27 15:20:29 +00:00
case eI2NPVariableTunnelBuildReply:
HandleVariableTunnelBuildReplyMsg (msgID, buf, size);
2018-01-06 03:48:51 +00:00
break;
2014-04-30 18:08:57 +00:00
case eI2NPTunnelBuild:
HandleTunnelBuildMsg (buf, size);
2018-01-06 03:48:51 +00:00
break;
2014-04-30 18:08:57 +00:00
case eI2NPTunnelBuildReply:
// TODO:
2018-01-06 03:48:51 +00:00
break;
2013-10-27 15:20:29 +00:00
default:
2015-12-17 07:11:36 +00:00
LogPrint (eLogWarning, "I2NP: Unexpected message ", (int)typeID);
2018-01-06 03:48:51 +00:00
}
2013-10-27 15:20:29 +00:00
}
void HandleI2NPMessage (std::shared_ptr<I2NPMessage> msg)
2013-10-27 15:20:29 +00:00
{
if (msg)
2018-01-06 03:48:51 +00:00
{
2015-12-17 07:11:36 +00:00
uint8_t typeID = msg->GetTypeID ();
2015-12-28 00:00:00 +00:00
LogPrint (eLogDebug, "I2NP: Handling message with type ", (int)typeID);
2015-12-17 07:11:36 +00:00
switch (typeID)
2018-01-06 03:48:51 +00:00
{
2013-11-20 12:46:09 +00:00
case eI2NPTunnelData:
i2p::tunnel::tunnels.PostTunnelData (msg);
2018-01-06 03:48:51 +00:00
break;
2013-11-20 12:46:09 +00:00
case eI2NPTunnelGateway:
i2p::tunnel::tunnels.PostTunnelData (msg);
2013-11-20 12:46:09 +00:00
break;
2014-03-17 20:50:03 +00:00
case eI2NPGarlic:
2015-06-16 14:14:14 +00:00
{
if (msg->from)
{
if (msg->from->GetTunnelPool ())
msg->from->GetTunnelPool ()->ProcessGarlicMessage (msg);
else
2015-12-17 07:11:36 +00:00
LogPrint (eLogInfo, "I2NP: Local destination for garlic doesn't exist anymore");
}
2014-10-08 11:55:46 +00:00
else
2018-01-06 03:48:51 +00:00
i2p::context.ProcessGarlicMessage (msg);
2015-06-16 14:14:14 +00:00
break;
}
2013-11-20 12:46:09 +00:00
case eI2NPDatabaseStore:
case eI2NPDatabaseSearchReply:
case eI2NPDatabaseLookup:
// forward to netDb
i2p::data::netdb.PostI2NPMsg (msg);
break;
2014-03-17 20:50:03 +00:00
case eI2NPDeliveryStatus:
2015-06-16 14:14:14 +00:00
{
2014-03-17 20:50:03 +00:00
if (msg->from && msg->from->GetTunnelPool ())
msg->from->GetTunnelPool ()->ProcessDeliveryStatus (msg);
2014-03-17 20:50:03 +00:00
else
i2p::context.ProcessDeliveryStatusMessage (msg);
2018-01-06 03:48:51 +00:00
break;
2015-06-16 14:14:14 +00:00
}
2018-01-06 03:48:51 +00:00
case eI2NPVariableTunnelBuild:
case eI2NPVariableTunnelBuildReply:
case eI2NPTunnelBuild:
2018-01-06 03:48:51 +00:00
case eI2NPTunnelBuildReply:
// forward to tunnel thread
i2p::tunnel::tunnels.PostTunnelData (msg);
2018-01-06 03:48:51 +00:00
break;
2013-11-20 12:46:09 +00:00
default:
HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ());
2018-01-06 03:48:51 +00:00
}
}
}
2015-01-23 03:00:41 +00:00
I2NPMessagesHandler::~I2NPMessagesHandler ()
{
Flush ();
}
2018-01-06 03:48:51 +00:00
void I2NPMessagesHandler::PutNextMessage (std::shared_ptr<I2NPMessage> msg)
2015-01-23 03:00:41 +00:00
{
if (msg)
{
2015-01-24 03:05:33 +00:00
switch (msg->GetTypeID ())
2018-01-06 03:48:51 +00:00
{
2015-01-24 03:05:33 +00:00
case eI2NPTunnelData:
m_TunnelMsgs.push_back (msg);
2015-01-24 03:05:33 +00:00
break;
2018-01-06 03:48:51 +00:00
case eI2NPTunnelGateway:
m_TunnelGatewayMsgs.push_back (msg);
2018-01-06 03:48:51 +00:00
break;
2015-01-24 03:05:33 +00:00
default:
HandleI2NPMessage (msg);
2018-01-06 03:48:51 +00:00
}
}
2015-01-23 03:00:41 +00:00
}
2018-01-06 03:48:51 +00:00
2015-01-23 03:00:41 +00:00
void I2NPMessagesHandler::Flush ()
{
if (!m_TunnelMsgs.empty ())
2018-01-06 03:48:51 +00:00
{
2015-01-23 03:00:41 +00:00
i2p::tunnel::tunnels.PostTunnelData (m_TunnelMsgs);
m_TunnelMsgs.clear ();
2018-01-06 03:48:51 +00:00
}
2015-01-24 03:05:33 +00:00
if (!m_TunnelGatewayMsgs.empty ())
2018-01-06 03:48:51 +00:00
{
2015-01-25 16:43:27 +00:00
i2p::tunnel::tunnels.PostTunnelData (m_TunnelGatewayMsgs);
2015-01-24 03:05:33 +00:00
m_TunnelGatewayMsgs.clear ();
2018-01-06 03:48:51 +00:00
}
}
2013-10-27 15:20:29 +00:00
}