2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2024-01-21 23:59:04 +00:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 13:18:41 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
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"
|
2017-04-22 00:04:16 +00:00
|
|
|
#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"
|
2021-06-08 19:36:27 +00:00
|
|
|
#include "ECIESX25519AEADRatchetSession.h"
|
2013-10-27 15:20:29 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2016-06-27 20:40:46 +00:00
|
|
|
#include "version.h"
|
2013-10-27 15:20:29 +00:00
|
|
|
|
2014-10-21 16:25:53 +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
|
|
|
}
|
|
|
|
|
2023-03-18 19:32:05 +00:00
|
|
|
std::shared_ptr<I2NPMessage> NewI2NPMediumMessage ()
|
|
|
|
{
|
|
|
|
return std::make_shared<I2NPMessageBuffer<I2NP_MAX_MEDIUM_MESSAGE_SIZE> >();
|
|
|
|
}
|
2023-05-08 14:50:27 +00:00
|
|
|
|
2021-06-27 19:49:57 +00:00
|
|
|
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage (bool endpoint)
|
2017-02-03 01:45:33 +00:00
|
|
|
{
|
2021-10-22 23:18:45 +00:00
|
|
|
return i2p::tunnel::tunnels.NewI2NPTunnelMessage (endpoint);
|
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
|
|
|
{
|
2023-03-18 19:32:05 +00:00
|
|
|
len += I2NP_HEADER_SIZE + 2;
|
|
|
|
if (len <= I2NP_MAX_SHORT_MESSAGE_SIZE) return NewI2NPShortMessage ();
|
|
|
|
if (len <= I2NP_MAX_MEDIUM_MESSAGE_SIZE) return NewI2NPMediumMessage ();
|
2023-05-08 14:50:27 +00:00
|
|
|
return NewI2NPMessage ();
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2015-05-11 19:17:43 +00:00
|
|
|
|
2020-05-02 15:13:40 +00:00
|
|
|
void I2NPMessage::FillI2NPMessageHeader (I2NPMessageType msgType, uint32_t replyMsgID, bool checksum)
|
2013-10-27 15:20:29 +00:00
|
|
|
{
|
2015-07-03 15:11:07 +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);
|
2015-07-03 15:11:07 +00:00
|
|
|
UpdateSize ();
|
2020-05-02 15:13:40 +00:00
|
|
|
if (checksum) UpdateChks ();
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 15:11:07 +00:00
|
|
|
void I2NPMessage::RenewI2NPMessageHeader ()
|
2014-03-20 16:48:03 +00:00
|
|
|
{
|
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);
|
2014-03-20 16:48:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 23:59:04 +00:00
|
|
|
bool I2NPMessage::IsExpired (uint64_t ts) const
|
2016-01-19 02:13:43 +00:00
|
|
|
{
|
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
|
2024-01-21 23:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool I2NPMessage::IsExpired () const
|
|
|
|
{
|
|
|
|
return IsExpired (i2p::util::GetMillisecondsSinceEpoch ());
|
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)
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "I2NP: Message length ", len, " exceeds max length ", msg->maxLen);
|
2015-07-03 15:11:07 +00:00
|
|
|
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
|
2021-11-27 19:53:53 +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
|
|
|
}
|
|
|
|
|
2024-02-27 08:15:15 +00:00
|
|
|
std::shared_ptr<I2NPMessage> CreateTunnelTestMsg (uint32_t msgID)
|
|
|
|
{
|
|
|
|
auto m = NewI2NPShortMessage ();
|
|
|
|
uint8_t * buf = m->GetPayload ();
|
|
|
|
htobe32buf (buf + TUNNEL_TEST_MSGID_OFFSET, msgID);
|
2024-02-27 17:33:07 +00:00
|
|
|
htobe64buf (buf + TUNNEL_TEST_TIMESTAMP_OFFSET, i2p::util::GetMonotonicMicroseconds ());
|
2024-02-27 08:15:15 +00:00
|
|
|
m->len += TUNNEL_TEST_SIZE;
|
|
|
|
m->FillI2NPMessageHeader (eI2NPTunnelTest);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
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 ();
|
2015-01-02 22:39:35 +00:00
|
|
|
uint8_t * buf = m->GetPayload ();
|
2014-02-24 20:16:39 +00:00
|
|
|
if (msgID)
|
|
|
|
{
|
2015-01-02 22:39:35 +00:00
|
|
|
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 ());
|
|
|
|
}
|
2015-01-02 22:39:35 +00:00
|
|
|
m->len += DELIVERY_STATUS_SIZE;
|
2015-07-03 15:11:07 +00:00
|
|
|
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,
|
2024-05-15 17:31:31 +00:00
|
|
|
uint32_t replyTunnelID, bool exploratory, std::unordered_set<i2p::data::IdentHash> * excludedPeers)
|
2013-10-27 15:20:29 +00:00
|
|
|
{
|
2023-03-22 01:25:00 +00:00
|
|
|
int cnt = excludedPeers ? excludedPeers->size () : 0;
|
|
|
|
auto m = cnt > 7 ? 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
|
2014-12-30 14:37:24 +00:00
|
|
|
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
|
|
|
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 ());
|
2015-07-03 15:11:07 +00:00
|
|
|
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,
|
2024-05-15 17:31:31 +00:00
|
|
|
const std::unordered_set<i2p::data::IdentHash>& excludedFloodfills,
|
2021-11-27 20:30:35 +00:00
|
|
|
std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel, const uint8_t * replyKey,
|
2022-05-20 16:56:05 +00:00
|
|
|
const uint8_t * replyTag, bool replyECIES)
|
2014-12-30 17:25:08 +00:00
|
|
|
{
|
2015-07-05 11:59:38 +00:00
|
|
|
int cnt = excludedFloodfills.size ();
|
2020-04-23 19:22:07 +00:00
|
|
|
auto m = cnt > 7 ? 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;
|
2020-09-20 01:15:42 +00:00
|
|
|
*buf = DATABASE_LOOKUP_DELIVERY_FLAG | DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP; // flags
|
|
|
|
*buf |= (replyECIES ? DATABASE_LOOKUP_ECIES_FLAG : DATABASE_LOOKUP_ENCRYPTION_FLAG);
|
2016-07-15 17:54:34 +00:00
|
|
|
buf ++;
|
2016-07-15 17:31:31 +00:00
|
|
|
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
|
2020-04-23 19:22:07 +00:00
|
|
|
if (cnt > 512)
|
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
LogPrint (eLogWarning, "I2NP: Too many peers to exclude ", cnt, " for DatabaseLookup");
|
2020-04-23 19:22:07 +00:00
|
|
|
cnt = 0;
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2014-12-30 17:25:08 +00:00
|
|
|
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);
|
2020-04-23 19:22:07 +00:00
|
|
|
buf[32] = 1; // 1 tag
|
2020-09-20 01:15:42 +00:00
|
|
|
if (replyECIES)
|
|
|
|
{
|
|
|
|
memcpy (buf + 33, replyTag, 8); // 8 bytes tag
|
|
|
|
buf += 41;
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-20 01:15:42 +00:00
|
|
|
memcpy (buf + 33, replyTag, 32); // 32 bytes tag
|
|
|
|
buf += 65;
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2014-12-30 17:25:08 +00:00
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
m->len += (buf - m->GetPayload ());
|
2015-07-03 15:11:07 +00:00
|
|
|
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,
|
2020-03-01 10:25:50 +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;
|
2015-07-03 15:11:07 +00:00
|
|
|
m->FillI2NPMessageHeader (eI2NPDatabaseSearchReply);
|
2018-01-06 03:48:51 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:30:35 +00:00
|
|
|
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::RouterInfo> router,
|
2022-05-20 16:56:05 +00:00
|
|
|
uint32_t replyToken, std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel)
|
2013-10-27 15:20:29 +00:00
|
|
|
{
|
2014-07-24 19:59:00 +00:00
|
|
|
if (!router) // we send own RouterInfo
|
2015-04-07 19:15:27 +00:00
|
|
|
router = context.GetSharedRouterInfo ();
|
2014-07-24 19:59:00 +00:00
|
|
|
|
2021-07-14 18:46:56 +00:00
|
|
|
if (!router->GetBuffer ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "I2NP: Invalid RouterInfo buffer for DatabaseStore");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-09-14 11:48:21 +00:00
|
|
|
|
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);
|
2015-01-28 21:16:25 +00:00
|
|
|
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)
|
|
|
|
{
|
2021-02-06 19:49:42 +00:00
|
|
|
if (replyTunnel)
|
|
|
|
{
|
|
|
|
htobe32buf (buf, replyTunnel->GetNextTunnelID ());
|
|
|
|
buf += 4; // reply tunnelID
|
|
|
|
memcpy (buf, replyTunnel->GetNextIdentHash (), 32);
|
|
|
|
buf += 32; // reply tunnel gateway
|
|
|
|
}
|
|
|
|
else
|
2021-09-14 11:48:21 +00:00
|
|
|
{
|
2021-02-06 19:49:42 +00:00
|
|
|
memset (buf, 0, 4); // zero tunnelID means direct reply
|
|
|
|
buf += 4;
|
|
|
|
memcpy (buf, context.GetIdentHash (), 32);
|
|
|
|
buf += 32;
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2015-01-28 21:16:25 +00:00
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
uint8_t * sizePtr = buf;
|
2013-11-20 12:46:09 +00:00
|
|
|
buf += 2;
|
2015-01-28 21:16:25 +00:00
|
|
|
m->len += (buf - payload); // payload size
|
2020-05-22 22:32:44 +00:00
|
|
|
size_t size = 0;
|
|
|
|
if (router->GetBufferLen () + (buf - payload) <= 940) // fits one tunnel message
|
|
|
|
size = i2p::data::GzipNoCompression (router->GetBuffer (), router->GetBufferLen (), buf, m->maxLen -m->len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i2p::data::GzipDeflator deflator;
|
|
|
|
size = deflator.Deflate (router->GetBuffer (), router->GetBufferLen (), buf, m->maxLen -m->len);
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2015-11-03 14:15:49 +00:00
|
|
|
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
|
|
|
|
2019-06-09 01:23:25 +00:00
|
|
|
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (const i2p::data::IdentHash& storeHash, std::shared_ptr<const i2p::data::LeaseSet> leaseSet)
|
2016-05-25 19:10:28 +00:00
|
|
|
{
|
|
|
|
if (!leaseSet) return nullptr;
|
|
|
|
auto m = NewI2NPShortMessage ();
|
2018-01-06 03:48:51 +00:00
|
|
|
uint8_t * payload = m->GetPayload ();
|
2019-06-09 01:23:25 +00:00
|
|
|
memcpy (payload + DATABASE_STORE_KEY_OFFSET, storeHash, 32);
|
2020-03-01 10:25:50 +00:00
|
|
|
payload[DATABASE_STORE_TYPE_OFFSET] = leaseSet->GetStoreType (); // 1 for LeaseSet
|
2016-05-25 19:10:28 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
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
|
|
|
{
|
2014-07-30 01:56:03 +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 ();
|
2019-04-09 19:36:10 +00:00
|
|
|
memcpy (payload + DATABASE_STORE_KEY_OFFSET, leaseSet->GetStoreHash (), 32);
|
2019-01-09 19:51:47 +00:00
|
|
|
payload[DATABASE_STORE_TYPE_OFFSET] = leaseSet->GetStoreType (); // LeaseSet or LeaseSet2
|
2015-01-03 02:11:40 +00:00
|
|
|
htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, replyToken);
|
|
|
|
size_t size = DATABASE_STORE_HEADER_SIZE;
|
2016-05-25 16:55:58 +00:00
|
|
|
if (replyToken && replyTunnel)
|
2014-08-20 15:12:53 +00:00
|
|
|
{
|
2016-05-25 16:55:58 +00:00
|
|
|
if (replyTunnel)
|
2014-08-20 15:12:53 +00:00
|
|
|
{
|
2016-05-25 16:55:58 +00:00
|
|
|
htobe32buf (payload + size, replyTunnel->GetNextTunnelID ());
|
2014-08-20 15:12:53 +00:00
|
|
|
size += 4; // reply tunnelID
|
2016-05-25 16:55:58 +00:00
|
|
|
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;
|
2015-07-03 15:11:07 +00:00
|
|
|
m->FillI2NPMessageHeader (eI2NPDatabaseStore);
|
2014-07-29 18:31:55 +00:00
|
|
|
return m;
|
|
|
|
}
|
2016-01-05 00:56:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-20 23:38:36 +00:00
|
|
|
static 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
|
|
|
{
|
2015-01-03 21:31:44 +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");
|
2024-02-23 20:24:52 +00:00
|
|
|
if (!i2p::context.DecryptTunnelBuildRecord (record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, clearText))
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "I2NP: Failed to decrypt tunnel build record");
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-24 16:40:43 +00:00
|
|
|
if (!memcmp ((const uint8_t *)i2p::context.GetIdentHash (), clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, 32) && // if next ident is now ours
|
|
|
|
!(clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG)) // and not endpoint
|
2024-02-23 20:24:52 +00:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "I2NP: Next ident is ours in tunnel build record");
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-02 23:49:07 +00:00
|
|
|
uint8_t retCode = 0;
|
2018-01-06 03:48:51 +00:00
|
|
|
// replace record to reply
|
2024-02-20 08:30:05 +00:00
|
|
|
if (i2p::context.AcceptsTunnels () && i2p::context.GetCongestionLevel (false) < CONGESTION_LEVEL_FULL)
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
2021-09-05 13:10:13 +00:00
|
|
|
auto transitTunnel = i2p::tunnel::CreateTransitTunnel (
|
2020-11-02 23:49:07 +00:00
|
|
|
bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
|
|
|
|
clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
|
|
|
clearText + ECIES_BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET,
|
|
|
|
clearText + ECIES_BUILD_REQUEST_RECORD_IV_KEY_OFFSET,
|
2021-06-08 19:36:27 +00:00
|
|
|
clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_GATEWAY_FLAG,
|
2021-09-05 13:10:13 +00:00
|
|
|
clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG);
|
2023-01-05 16:59:47 +00:00
|
|
|
if (!i2p::tunnel::tunnels.AddTransitTunnel (transitTunnel))
|
|
|
|
retCode = 30;
|
2014-10-01 02:08:05 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-02 23:49:07 +00:00
|
|
|
retCode = 30; // always reject with bandwidth reason (30)
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2021-09-05 13:10:13 +00:00
|
|
|
memset (record + ECIES_BUILD_RESPONSE_RECORD_OPTIONS_OFFSET, 0, 2); // no options
|
2021-11-27 20:30:35 +00:00
|
|
|
record[ECIES_BUILD_RESPONSE_RECORD_RET_OFFSET] = retCode;
|
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-03 21:31:44 +00:00
|
|
|
uint8_t * reply = records + j*TUNNEL_BUILD_RECORD_SIZE;
|
2021-09-05 13:10:13 +00:00
|
|
|
if (j == i)
|
|
|
|
{
|
|
|
|
uint8_t nonce[12];
|
|
|
|
memset (nonce, 0, 12);
|
|
|
|
auto& noiseState = i2p::context.GetCurrentNoiseState ();
|
2021-11-27 20:30:35 +00:00
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (reply, TUNNEL_BUILD_RECORD_SIZE - 16,
|
2021-09-05 13:10:13 +00:00
|
|
|
noiseState.m_H, 32, noiseState.m_CK, nonce, reply, TUNNEL_BUILD_RECORD_SIZE, true)) // encrypt
|
2020-11-02 23:49:07 +00:00
|
|
|
{
|
2021-09-05 13:10:13 +00:00
|
|
|
LogPrint (eLogWarning, "I2NP: Reply AEAD encryption failed");
|
|
|
|
return false;
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2020-11-02 23:49:07 +00:00
|
|
|
}
|
|
|
|
else
|
2021-09-14 11:48:21 +00:00
|
|
|
{
|
2021-09-05 13:10:13 +00:00
|
|
|
encryption.SetKey (clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET);
|
2021-09-14 11:48:21 +00:00
|
|
|
encryption.SetIV (clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_IV_OFFSET);
|
2020-11-02 23:49:07 +00:00
|
|
|
encryption.Encrypt(reply, TUNNEL_BUILD_RECORD_SIZE, reply);
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-20 23:38:36 +00:00
|
|
|
static 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");
|
2023-05-06 22:07:21 +00:00
|
|
|
if (num > i2p::tunnel::MAX_NUM_RECORDS)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "I2NP: Too many records in VaribleTunnelBuild message ", num);
|
|
|
|
return;
|
2023-05-08 15:33:40 +00:00
|
|
|
}
|
2020-11-07 23:28:38 +00:00
|
|
|
if (len < num*TUNNEL_BUILD_RECORD_SIZE + 1)
|
2016-01-30 15:35:32 +00:00
|
|
|
{
|
2021-06-06 17:55:38 +00:00
|
|
|
LogPrint (eLogError, "I2NP: VaribleTunnelBuild message of ", num, " records is too short ", len);
|
2016-01-30 15:35:32 +00:00
|
|
|
return;
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2013-10-27 15:20:29 +00:00
|
|
|
|
2020-03-01 10:25:50 +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);
|
2015-01-26 16:56:10 +00:00
|
|
|
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
|
|
|
|
{
|
2021-09-05 13:10:13 +00:00
|
|
|
uint8_t clearText[ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
|
|
|
if (HandleBuildRequestRecords (num, buf + 1, clearText))
|
2014-04-30 18:08:57 +00:00
|
|
|
{
|
2021-09-05 13:10:13 +00:00
|
|
|
if (clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG) // we are endpoint of outboud tunnel
|
2014-04-30 18:08:57 +00:00
|
|
|
{
|
2021-09-05 13:10:13 +00:00
|
|
|
// so we send it to reply tunnel
|
|
|
|
transports.SendMessage (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
CreateTunnelGatewayMsg (bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
|
|
|
eI2NPVariableTunnelBuildReply, buf, len,
|
|
|
|
bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2021-09-05 13:10:13 +00:00
|
|
|
else
|
|
|
|
transports.SendMessage (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
CreateI2NPMessage (eI2NPVariableTunnelBuild, buf, len,
|
|
|
|
bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 23:38:36 +00:00
|
|
|
static void HandleTunnelBuildMsg (uint8_t * buf, size_t len)
|
2014-04-30 18:08:57 +00:00
|
|
|
{
|
2021-09-05 13:10:13 +00:00
|
|
|
LogPrint (eLogWarning, "I2NP: TunnelBuild is too old for ECIES router");
|
2014-04-30 18:08:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 23:38:36 +00:00
|
|
|
static void HandleTunnelBuildReplyMsg (uint32_t replyMsgID, uint8_t * buf, size_t len, bool isShort)
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
2016-01-30 15:35:32 +00:00
|
|
|
int num = buf[0];
|
2021-07-20 23:38:36 +00:00
|
|
|
LogPrint (eLogDebug, "I2NP: TunnelBuildReplyMsg of ", num, " records replyMsgID=", replyMsgID);
|
2023-05-06 22:07:21 +00:00
|
|
|
if (num > i2p::tunnel::MAX_NUM_RECORDS)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "I2NP: Too many records in TunnelBuildReply message ", num);
|
|
|
|
return;
|
2023-05-08 15:33:40 +00:00
|
|
|
}
|
2021-07-20 23:38:36 +00:00
|
|
|
size_t recordSize = isShort ? SHORT_TUNNEL_BUILD_RECORD_SIZE : TUNNEL_BUILD_RECORD_SIZE;
|
|
|
|
if (len < num*recordSize + 1)
|
2016-01-30 15:35:32 +00:00
|
|
|
{
|
2021-07-20 23:38:36 +00:00
|
|
|
LogPrint (eLogError, "I2NP: TunnelBuildReply message of ", num, " records is too short ", len);
|
2016-01-30 15:35:32 +00:00
|
|
|
return;
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 16:56:10 +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);
|
2015-01-26 16:56:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-20 23:38:36 +00:00
|
|
|
static void HandleShortTunnelBuildMsg (uint32_t replyMsgID, uint8_t * buf, size_t len)
|
2021-06-06 17:55:38 +00:00
|
|
|
{
|
|
|
|
int num = buf[0];
|
|
|
|
LogPrint (eLogDebug, "I2NP: ShortTunnelBuild ", num, " records");
|
2023-05-06 22:07:21 +00:00
|
|
|
if (num > i2p::tunnel::MAX_NUM_RECORDS)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "I2NP: Too many records in ShortTunnelBuild message ", num);
|
|
|
|
return;
|
2023-05-08 15:33:40 +00:00
|
|
|
}
|
2021-06-06 17:55:38 +00:00
|
|
|
if (len < num*SHORT_TUNNEL_BUILD_RECORD_SIZE + 1)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "I2NP: ShortTunnelBuild message of ", num, " records is too short ", len);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-11 23:29:16 +00:00
|
|
|
auto tunnel = i2p::tunnel::tunnels.GetPendingInboundTunnel (replyMsgID);
|
|
|
|
if (tunnel)
|
|
|
|
{
|
|
|
|
// endpoint of inbound tunnel
|
|
|
|
LogPrint (eLogDebug, "I2NP: ShortTunnelBuild reply for tunnel ", tunnel->GetTunnelID ());
|
|
|
|
if (tunnel->HandleTunnelBuildResponse (buf, len))
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "I2NP: Inbound tunnel ", tunnel->GetTunnelID (), " has been created");
|
|
|
|
tunnel->SetState (i2p::tunnel::eTunnelStateEstablished);
|
|
|
|
i2p::tunnel::tunnels.AddInboundTunnel (tunnel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "I2NP: Inbound tunnel ", tunnel->GetTunnelID (), " has been declined");
|
|
|
|
tunnel->SetState (i2p::tunnel::eTunnelStateBuildFailed);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2021-06-06 17:55:38 +00:00
|
|
|
const uint8_t * record = buf + 1;
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
if (!memcmp (record, (const uint8_t *)i2p::context.GetRouterInfo ().GetIdentHash (), 16))
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "I2NP: Short request record ", i, " is ours");
|
|
|
|
uint8_t clearText[SHORT_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
2021-11-27 20:30:35 +00:00
|
|
|
if (!i2p::context.DecryptTunnelShortRequestRecord (record + SHORT_REQUEST_RECORD_ENCRYPTED_OFFSET, clearText))
|
2021-09-14 11:48:21 +00:00
|
|
|
{
|
2021-06-06 17:55:38 +00:00
|
|
|
LogPrint (eLogWarning, "I2NP: Can't decrypt short request record ", i);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-21 22:12:37 +00:00
|
|
|
if (clearText[SHORT_REQUEST_RECORD_LAYER_ENCRYPTION_TYPE]) // not AES
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "I2NP: Unknown layer encryption type ", clearText[SHORT_REQUEST_RECORD_LAYER_ENCRYPTION_TYPE], " in short request record");
|
|
|
|
return;
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2021-06-07 22:28:36 +00:00
|
|
|
auto& noiseState = i2p::context.GetCurrentNoiseState ();
|
2022-12-25 14:36:16 +00:00
|
|
|
uint8_t replyKey[32]; // AEAD/Chacha20/Poly1305
|
|
|
|
i2p::crypto::AESKey layerKey, ivKey; // AES
|
2021-07-05 18:31:07 +00:00
|
|
|
i2p::crypto::HKDF (noiseState.m_CK, nullptr, 0, "SMTunnelReplyKey", noiseState.m_CK);
|
|
|
|
memcpy (replyKey, noiseState.m_CK + 32, 32);
|
2021-11-27 20:30:35 +00:00
|
|
|
i2p::crypto::HKDF (noiseState.m_CK, nullptr, 0, "SMTunnelLayerKey", noiseState.m_CK);
|
2021-07-05 18:31:07 +00:00
|
|
|
memcpy (layerKey, noiseState.m_CK + 32, 32);
|
|
|
|
bool isEndpoint = clearText[SHORT_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG;
|
|
|
|
if (isEndpoint)
|
|
|
|
{
|
|
|
|
i2p::crypto::HKDF (noiseState.m_CK, nullptr, 0, "TunnelLayerIVKey", noiseState.m_CK);
|
|
|
|
memcpy (ivKey, noiseState.m_CK + 32, 32);
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2021-07-05 18:31:07 +00:00
|
|
|
else
|
2024-02-24 16:40:43 +00:00
|
|
|
{
|
|
|
|
if (!memcmp ((const uint8_t *)i2p::context.GetIdentHash (), clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET, 32)) // if next ident is now ours
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "I2NP: Next ident is ours in short request record");
|
|
|
|
return;
|
|
|
|
}
|
2021-07-05 18:31:07 +00:00
|
|
|
memcpy (ivKey, noiseState.m_CK , 32);
|
2024-02-24 16:40:43 +00:00
|
|
|
}
|
2021-07-21 22:12:37 +00:00
|
|
|
|
|
|
|
// check if we accept this tunnel
|
2024-02-05 20:44:17 +00:00
|
|
|
std::shared_ptr<i2p::tunnel::TransitTunnel> transitTunnel;
|
2021-07-21 22:12:37 +00:00
|
|
|
uint8_t retCode = 0;
|
2024-02-20 08:30:05 +00:00
|
|
|
if (!i2p::context.AcceptsTunnels () || i2p::context.GetCongestionLevel (false) >= CONGESTION_LEVEL_FULL)
|
2023-03-07 00:48:04 +00:00
|
|
|
retCode = 30;
|
2021-07-21 22:12:37 +00:00
|
|
|
if (!retCode)
|
2021-09-14 11:48:21 +00:00
|
|
|
{
|
2021-07-21 22:12:37 +00:00
|
|
|
// create new transit tunnel
|
2024-02-05 20:44:17 +00:00
|
|
|
transitTunnel = i2p::tunnel::CreateTransitTunnel (
|
2021-07-21 22:12:37 +00:00
|
|
|
bufbe32toh (clearText + SHORT_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
|
|
|
|
clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
bufbe32toh (clearText + SHORT_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
|
|
|
layerKey, ivKey,
|
|
|
|
clearText[SHORT_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_GATEWAY_FLAG,
|
|
|
|
clearText[SHORT_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG);
|
2023-01-05 16:59:47 +00:00
|
|
|
if (!i2p::tunnel::tunnels.AddTransitTunnel (transitTunnel))
|
|
|
|
retCode = 30;
|
2021-07-21 22:12:37 +00:00
|
|
|
}
|
2021-09-14 11:48:21 +00:00
|
|
|
|
2021-07-12 23:40:40 +00:00
|
|
|
// encrypt reply
|
|
|
|
uint8_t nonce[12];
|
|
|
|
memset (nonce, 0, 12);
|
|
|
|
uint8_t * reply = buf + 1;
|
|
|
|
for (int j = 0; j < num; j++)
|
2021-09-14 11:48:21 +00:00
|
|
|
{
|
2021-07-12 23:40:40 +00:00
|
|
|
nonce[4] = j; // nonce is record #
|
|
|
|
if (j == i)
|
|
|
|
{
|
|
|
|
memset (reply + SHORT_RESPONSE_RECORD_OPTIONS_OFFSET, 0, 2); // no options
|
2021-11-27 20:30:35 +00:00
|
|
|
reply[SHORT_RESPONSE_RECORD_RET_OFFSET] = retCode;
|
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (reply, SHORT_TUNNEL_BUILD_RECORD_SIZE - 16,
|
2021-07-12 23:40:40 +00:00
|
|
|
noiseState.m_H, 32, replyKey, nonce, reply, SHORT_TUNNEL_BUILD_RECORD_SIZE, true)) // encrypt
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "I2NP: Short reply AEAD encryption failed");
|
|
|
|
return;
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2021-07-12 23:40:40 +00:00
|
|
|
}
|
|
|
|
else
|
2021-11-27 20:30:35 +00:00
|
|
|
i2p::crypto::ChaCha20 (reply, SHORT_TUNNEL_BUILD_RECORD_SIZE, replyKey, nonce, reply);
|
2021-09-14 11:48:21 +00:00
|
|
|
reply += SHORT_TUNNEL_BUILD_RECORD_SIZE;
|
2021-07-12 23:40:40 +00:00
|
|
|
}
|
|
|
|
// send reply
|
2024-02-05 20:44:17 +00:00
|
|
|
auto onDrop = [transitTunnel]()
|
|
|
|
{
|
|
|
|
if (transitTunnel)
|
|
|
|
{
|
|
|
|
auto t = transitTunnel->GetCreationTime ();
|
|
|
|
if (t > i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT)
|
|
|
|
// make transit tunnel expired
|
|
|
|
transitTunnel->SetCreationTime (t - i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT);
|
|
|
|
}
|
|
|
|
};
|
2021-07-05 18:31:07 +00:00
|
|
|
if (isEndpoint)
|
2021-09-14 11:48:21 +00:00
|
|
|
{
|
2021-07-12 23:40:40 +00:00
|
|
|
auto replyMsg = NewI2NPShortMessage ();
|
|
|
|
replyMsg->Concat (buf, len);
|
|
|
|
replyMsg->FillI2NPMessageHeader (eI2NPShortTunnelBuildReply, bufbe32toh (clearText + SHORT_REQUEST_RECORD_SEND_MSG_ID_OFFSET));
|
2024-02-05 20:44:17 +00:00
|
|
|
if (transitTunnel) replyMsg->onDrop = onDrop;
|
2021-11-27 20:30:35 +00:00
|
|
|
if (memcmp ((const uint8_t *)i2p::context.GetIdentHash (),
|
2021-07-28 19:06:24 +00:00
|
|
|
clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET, 32)) // reply IBGW is not local?
|
|
|
|
{
|
2021-11-27 20:30:35 +00:00
|
|
|
i2p::crypto::HKDF (noiseState.m_CK, nullptr, 0, "RGarlicKeyAndTag", noiseState.m_CK);
|
2021-07-28 19:06:24 +00:00
|
|
|
uint64_t tag;
|
|
|
|
memcpy (&tag, noiseState.m_CK, 8);
|
|
|
|
// we send it to reply tunnel
|
|
|
|
transports.SendMessage (clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
CreateTunnelGatewayMsg (bufbe32toh (clearText + SHORT_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
2022-05-20 16:56:05 +00:00
|
|
|
i2p::garlic::WrapECIESX25519Message (replyMsg, noiseState.m_CK + 32, tag)));
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2021-07-28 19:06:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// IBGW is local
|
|
|
|
uint32_t tunnelID = bufbe32toh (clearText + SHORT_REQUEST_RECORD_NEXT_TUNNEL_OFFSET);
|
|
|
|
auto tunnel = i2p::tunnel::tunnels.GetTunnel (tunnelID);
|
|
|
|
if (tunnel)
|
2024-02-25 14:15:08 +00:00
|
|
|
{
|
2021-07-28 19:06:24 +00:00
|
|
|
tunnel->SendTunnelDataMsg (replyMsg);
|
2024-02-25 14:15:08 +00:00
|
|
|
tunnel->FlushTunnelDataMsgs ();
|
|
|
|
}
|
2021-07-28 19:06:24 +00:00
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "I2NP: Tunnel ", tunnelID, " not found for short tunnel build reply");
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2024-02-05 20:44:17 +00:00
|
|
|
{
|
|
|
|
auto msg = CreateI2NPMessage (eI2NPShortTunnelBuild, buf, len,
|
|
|
|
bufbe32toh (clearText + SHORT_REQUEST_RECORD_SEND_MSG_ID_OFFSET));
|
|
|
|
if (transitTunnel) msg->onDrop = onDrop;
|
|
|
|
transports.SendMessage (clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET, msg);
|
|
|
|
}
|
2021-06-06 17:55:38 +00:00
|
|
|
return;
|
2021-09-14 11:48:21 +00:00
|
|
|
}
|
2021-06-06 17:55:38 +00:00
|
|
|
record += SHORT_TUNNEL_BUILD_RECORD_SIZE;
|
2021-09-14 11:48:21 +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
|
|
|
{
|
2021-06-27 19:49:57 +00:00
|
|
|
auto msg = NewI2NPTunnelMessage (false);
|
2018-01-06 03:48:51 +00:00
|
|
|
msg->Concat (buf, i2p::tunnel::TUNNEL_DATA_MSG_SIZE);
|
2015-07-03 15:11:07 +00:00
|
|
|
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
|
|
|
{
|
2021-06-27 19:49:57 +00:00
|
|
|
auto msg = NewI2NPTunnelMessage (false);
|
2014-12-30 14:37:24 +00:00
|
|
|
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);
|
2015-07-03 15:11:07 +00:00
|
|
|
msg->FillI2NPMessageHeader (eI2NPTunnelData);
|
2013-10-27 15:20:29 +00:00
|
|
|
return msg;
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2015-06-26 01:49:16 +00:00
|
|
|
|
2021-06-27 19:49:57 +00:00
|
|
|
std::shared_ptr<I2NPMessage> CreateEmptyTunnelDataMsg (bool endpoint)
|
2015-06-26 01:49:16 +00:00
|
|
|
{
|
2021-06-27 19:49:57 +00:00
|
|
|
auto msg = NewI2NPTunnelMessage (endpoint);
|
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)
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "I2NP: Tunnel gateway buffer overflow ", msg->maxLen);
|
2015-07-03 15:11:07 +00:00
|
|
|
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
|
|
|
|
2015-06-19 18:38:31 +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
|
2024-02-05 20:44:17 +00:00
|
|
|
{
|
|
|
|
auto newMsg = CreateTunnelGatewayMsg (tunnelID, msg->GetBuffer (), msg->GetLength ());
|
|
|
|
if (msg->onDrop) newMsg->onDrop = msg->onDrop;
|
|
|
|
return newMsg;
|
|
|
|
}
|
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)
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "I2NP: Tunnel gateway buffer overflow ", msg->maxLen);
|
2015-07-03 15:11:07 +00:00
|
|
|
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);
|
2015-07-03 15:11:07 +00:00
|
|
|
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)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "I2NP: Message length ", len, " is smaller than header");
|
2017-12-01 17:57:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "I2NP: Message length ", l, " exceeds buffer length ", len);
|
2017-12-01 17:57:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-08 15:33:40 +00:00
|
|
|
void HandleTunnelBuildI2NPMessage (std::shared_ptr<I2NPMessage> msg)
|
2013-10-27 15:20:29 +00:00
|
|
|
{
|
2023-05-08 14:50:27 +00:00
|
|
|
if (msg)
|
2017-12-01 17:57:05 +00:00
|
|
|
{
|
2023-05-08 14:50:27 +00:00
|
|
|
uint8_t typeID = msg->GetTypeID();
|
|
|
|
uint32_t msgID = msg->GetMsgID();
|
|
|
|
LogPrint (eLogDebug, "I2NP: Handling tunnel build message with len=", msg->GetLength(),", type=", (int)typeID, ", msgID=", (unsigned int)msgID);
|
|
|
|
uint8_t * payload = msg->GetPayload();
|
|
|
|
auto size = msg->GetPayloadLength();
|
|
|
|
switch (typeID)
|
|
|
|
{
|
|
|
|
case eI2NPVariableTunnelBuild:
|
|
|
|
HandleVariableTunnelBuildMsg (msgID, payload, size);
|
|
|
|
break;
|
|
|
|
case eI2NPShortTunnelBuild:
|
|
|
|
HandleShortTunnelBuildMsg (msgID, payload, size);
|
|
|
|
break;
|
|
|
|
case eI2NPVariableTunnelBuildReply:
|
|
|
|
HandleTunnelBuildReplyMsg (msgID, payload, size, false);
|
|
|
|
break;
|
|
|
|
case eI2NPShortTunnelBuildReply:
|
|
|
|
HandleTunnelBuildReplyMsg (msgID, payload, size, true);
|
|
|
|
break;
|
|
|
|
case eI2NPTunnelBuild:
|
|
|
|
HandleTunnelBuildMsg (payload, size);
|
|
|
|
break;
|
|
|
|
case eI2NPTunnelBuildReply:
|
|
|
|
// TODO:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint (eLogError, "I2NP: Unexpected message with type", (int)typeID, " during handling TBM; skipping");
|
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 18:38:31 +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:
|
2023-04-20 18:23:41 +00:00
|
|
|
if (!msg->from)
|
|
|
|
i2p::tunnel::tunnels.PostTunnelData (msg);
|
2018-01-06 03:48:51 +00:00
|
|
|
break;
|
2013-11-20 12:46:09 +00:00
|
|
|
case eI2NPTunnelGateway:
|
2023-04-20 18:23:41 +00:00
|
|
|
if (!msg->from)
|
|
|
|
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
|
|
|
{
|
2022-06-01 01:43:31 +00:00
|
|
|
if (msg->from && msg->from->GetTunnelPool ())
|
|
|
|
msg->from->GetTunnelPool ()->ProcessGarlicMessage (msg);
|
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:
|
2023-05-21 22:43:58 +00:00
|
|
|
case eI2NPDatabaseSearchReply:
|
2023-04-20 18:23:41 +00:00
|
|
|
// forward to netDb if came directly or through exploratory tunnel as response to our request
|
|
|
|
if (!msg->from || !msg->from->GetTunnelPool () || msg->from->GetTunnelPool ()->IsExploratory ())
|
|
|
|
i2p::data::netdb.PostI2NPMsg (msg);
|
|
|
|
break;
|
2023-05-21 22:43:58 +00:00
|
|
|
|
2023-04-20 18:23:41 +00:00
|
|
|
case eI2NPDatabaseLookup:
|
|
|
|
// forward to netDb if floodfill and came directly
|
|
|
|
if (!msg->from && i2p::context.IsFloodfill ())
|
|
|
|
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 ())
|
2015-06-19 18:38:31 +00:00
|
|
|
msg->from->GetTunnelPool ()->ProcessDeliveryStatus (msg);
|
2014-03-17 20:50:03 +00:00
|
|
|
else
|
2015-06-19 18:38:31 +00:00
|
|
|
i2p::context.ProcessDeliveryStatusMessage (msg);
|
2018-01-06 03:48:51 +00:00
|
|
|
break;
|
2015-06-16 14:14:14 +00:00
|
|
|
}
|
2024-02-27 08:15:15 +00:00
|
|
|
case eI2NPTunnelTest:
|
|
|
|
if (msg->from && msg->from->GetTunnelPool ())
|
|
|
|
msg->from->GetTunnelPool ()->ProcessTunnelTest (msg);
|
|
|
|
break;
|
2018-01-06 03:48:51 +00:00
|
|
|
case eI2NPVariableTunnelBuild:
|
2015-01-27 01:49:16 +00:00
|
|
|
case eI2NPTunnelBuild:
|
2021-09-14 11:48:21 +00:00
|
|
|
case eI2NPShortTunnelBuild:
|
2023-04-20 18:23:41 +00:00
|
|
|
// forward to tunnel thread
|
|
|
|
if (!msg->from)
|
|
|
|
i2p::tunnel::tunnels.PostTunnelData (msg);
|
|
|
|
break;
|
|
|
|
case eI2NPVariableTunnelBuildReply:
|
|
|
|
case eI2NPTunnelBuildReply:
|
2021-09-14 11:48:21 +00:00
|
|
|
case eI2NPShortTunnelBuildReply:
|
2015-01-27 01:49:16 +00:00
|
|
|
// 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:
|
2023-05-08 14:50:27 +00:00
|
|
|
LogPrint(eLogError, "I2NP: Unexpected I2NP message with type ", int(typeID), " during handling; skipping");
|
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
|
|
|
|
2021-10-17 15:31:37 +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:
|
2015-06-21 19:08:22 +00:00
|
|
|
m_TunnelMsgs.push_back (msg);
|
2015-01-24 03:05:33 +00:00
|
|
|
break;
|
2018-01-06 03:48:51 +00:00
|
|
|
case eI2NPTunnelGateway:
|
2015-06-21 19:08:22 +00:00
|
|
|
m_TunnelGatewayMsgs.push_back (msg);
|
2018-01-06 03:48:51 +00:00
|
|
|
break;
|
2015-01-24 03:05:33 +00:00
|
|
|
default:
|
2015-06-21 19:08:22 +00:00
|
|
|
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
|
|
|
}
|