2013-10-27 15:20:29 +00:00
|
|
|
#include <string.h>
|
2014-07-06 21:48:16 +00:00
|
|
|
#include <atomic>
|
2014-01-20 14:36:20 +00:00
|
|
|
#include "I2PEndian.h"
|
2013-11-20 12:46:09 +00:00
|
|
|
#include <cryptopp/gzip.h>
|
2013-10-27 15:20:29 +00:00
|
|
|
#include "ElGamal.h"
|
|
|
|
#include "Timestamp.h"
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "NetDb.h"
|
|
|
|
#include "Tunnel.h"
|
|
|
|
#include "base64.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"
|
|
|
|
|
2014-10-21 16:25:53 +00:00
|
|
|
using namespace i2p::transport;
|
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
I2NPMessage * NewI2NPMessage ()
|
|
|
|
{
|
2014-07-30 20:52:35 +00:00
|
|
|
return new I2NPMessageBuffer<I2NP_MAX_MESSAGE_SIZE>();
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 20:52:35 +00:00
|
|
|
I2NPMessage * NewI2NPShortMessage ()
|
|
|
|
{
|
|
|
|
return new I2NPMessageBuffer<I2NP_MAX_SHORT_MESSAGE_SIZE>();
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:20:42 +00:00
|
|
|
I2NPMessage * NewI2NPMessage (size_t len)
|
|
|
|
{
|
|
|
|
return (len < I2NP_MAX_SHORT_MESSAGE_SIZE/2) ? NewI2NPShortMessage () : NewI2NPMessage ();
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
void DeleteI2NPMessage (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:17:43 +00:00
|
|
|
std::shared_ptr<I2NPMessage> ToSharedI2NPMessage (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
return std::shared_ptr<I2NPMessage>(msg, DeleteI2NPMessage);
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID)
|
|
|
|
{
|
2015-01-01 21:51:15 +00:00
|
|
|
msg->SetTypeID (msgType);
|
2013-10-27 15:20:29 +00:00
|
|
|
if (replyMsgID) // for tunnel creation
|
2015-01-01 21:51:15 +00:00
|
|
|
msg->SetMsgID (replyMsgID);
|
2015-05-27 17:35:54 +00:00
|
|
|
else
|
|
|
|
msg->SetMsgID (i2p::context.GetRandomNumberGenerator ().GenerateWord32 ());
|
2015-01-01 21:51:15 +00:00
|
|
|
msg->SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number
|
|
|
|
msg->UpdateSize ();
|
|
|
|
msg->UpdateChks ();
|
|
|
|
}
|
|
|
|
|
2014-03-20 16:48:03 +00:00
|
|
|
void RenewI2NPMessageHeader (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
if (msg)
|
|
|
|
{
|
2015-05-27 17:35:54 +00:00
|
|
|
msg->SetMsgID (i2p::context.GetRandomNumberGenerator ().GenerateWord32 ());
|
2015-01-01 21:51:15 +00:00
|
|
|
msg->SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000);
|
2014-03-20 16:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID)
|
|
|
|
{
|
2014-07-30 22:20:42 +00:00
|
|
|
I2NPMessage * msg = NewI2NPMessage (len);
|
2015-05-11 16:53:08 +00:00
|
|
|
if (msg->len + len < msg->maxLen)
|
|
|
|
{
|
|
|
|
memcpy (msg->GetPayload (), buf, len);
|
|
|
|
msg->len += len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "I2NP message length ", len, " exceeds max length");
|
2013-10-27 15:20:29 +00:00
|
|
|
FillI2NPMessageHeader (msg, msgType, replyMsgID);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2015-02-05 23:53:43 +00:00
|
|
|
I2NPMessage * CreateI2NPMessage (const uint8_t * buf, int len, std::shared_ptr<i2p::tunnel::InboundTunnel> from)
|
2013-11-10 23:23:26 +00:00
|
|
|
{
|
|
|
|
I2NPMessage * 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
|
|
|
|
LogPrint (eLogError, "I2NP message length ", len, " exceeds max length");
|
2013-11-10 23:23:26 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2014-01-09 03:47:22 +00:00
|
|
|
I2NPMessage * CreateDeliveryStatusMsg (uint32_t msgID)
|
2013-10-27 15:20:29 +00:00
|
|
|
{
|
2015-02-12 16:40:42 +00:00
|
|
|
I2NPMessage * 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-01-02 22:39:35 +00:00
|
|
|
htobe32buf (buf + DELIVERY_STATUS_MSGID_OFFSET, i2p::context.GetRandomNumberGenerator ().GenerateWord32 ());
|
|
|
|
htobe64buf (buf + DELIVERY_STATUS_TIMESTAMP_OFFSET, 2); // netID = 2
|
|
|
|
}
|
|
|
|
m->len += DELIVERY_STATUS_SIZE;
|
|
|
|
FillI2NPMessageHeader (m, eI2NPDeliveryStatus);
|
|
|
|
return m;
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-04 22:25:16 +00:00
|
|
|
I2NPMessage * CreateRouterInfoDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
|
|
|
|
uint32_t replyTunnelID, bool exploratory, std::set<i2p::data::IdentHash> * excludedPeers)
|
2013-10-27 15:20:29 +00:00
|
|
|
{
|
2015-03-11 01:54:25 +00:00
|
|
|
I2NPMessage * m = 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;
|
2015-02-02 16:06:36 +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
|
|
|
|
{
|
2015-01-04 22:25:16 +00:00
|
|
|
*buf = flag; // flag
|
2013-11-19 01:37:38 +00:00
|
|
|
buf++;
|
|
|
|
}
|
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;
|
2014-01-05 14:53:44 +00:00
|
|
|
}
|
2015-01-04 22:25:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// nothing to exclude
|
|
|
|
htobuf16 (buf, 0);
|
|
|
|
buf += 2;
|
|
|
|
}
|
|
|
|
|
2013-11-19 01:37:38 +00:00
|
|
|
m->len += (buf - m->GetPayload ());
|
|
|
|
FillI2NPMessageHeader (m, eI2NPDatabaseLookup);
|
|
|
|
return m;
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
2014-12-30 17:25:08 +00:00
|
|
|
I2NPMessage * CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest,
|
|
|
|
const std::set<i2p::data::IdentHash>& excludedFloodfills,
|
|
|
|
const i2p::tunnel::InboundTunnel * replyTunnel, const uint8_t * replyKey, const uint8_t * replyTag)
|
|
|
|
{
|
2015-03-11 01:54:25 +00:00
|
|
|
I2NPMessage * m = 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;
|
2015-02-02 16:06:36 +00:00
|
|
|
*buf = DATABASE_LOOKUP_DELIVERY_FLAG | DATABASE_LOOKUP_ENCYPTION_FLAG | DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP; // flags
|
2014-12-30 17:25:08 +00:00
|
|
|
htobe32buf (buf + 1, replyTunnel->GetNextTunnelID ()); // reply tunnel ID
|
|
|
|
buf += 5;
|
|
|
|
|
|
|
|
// excluded
|
|
|
|
int cnt = excludedFloodfills.size ();
|
|
|
|
htobe16buf (buf, cnt);
|
|
|
|
buf += 2;
|
|
|
|
if (cnt > 0)
|
|
|
|
{
|
|
|
|
for (auto& it: excludedFloodfills)
|
|
|
|
{
|
|
|
|
memcpy (buf, it, 32);
|
|
|
|
buf += 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// encryption
|
|
|
|
memcpy (buf, replyKey, 32);
|
|
|
|
buf[32] = 1; // 1 tag
|
|
|
|
memcpy (buf + 33, replyTag, 32);
|
|
|
|
buf += 65;
|
|
|
|
|
|
|
|
m->len += (buf - m->GetPayload ());
|
|
|
|
FillI2NPMessageHeader (m, eI2NPDatabaseLookup);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-25 02:01:07 +00:00
|
|
|
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
|
|
|
{
|
2014-07-30 22:20:42 +00:00
|
|
|
I2NPMessage * 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;
|
2015-02-02 00:58:26 +00:00
|
|
|
buf[len] = routers.size ();
|
2014-07-25 02:01:07 +00:00
|
|
|
len++;
|
2015-02-02 00:58:26 +00:00
|
|
|
for (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;
|
|
|
|
}
|
|
|
|
memcpy (buf + len, i2p::context.GetRouterInfo ().GetIdentHash (), 32);
|
|
|
|
len += 32;
|
|
|
|
m->len += len;
|
2014-01-06 03:21:59 +00:00
|
|
|
FillI2NPMessageHeader (m, eI2NPDatabaseSearchReply);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:15:27 +00:00
|
|
|
I2NPMessage * CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::RouterInfo> router, uint32_t replyToken)
|
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
|
|
|
|
2014-07-30 22:20:42 +00:00
|
|
|
I2NPMessage * m = NewI2NPShortMessage ();
|
2015-01-03 02:11:40 +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)
|
|
|
|
{
|
|
|
|
memset (buf, 0, 4); // zero tunnelID means direct reply
|
|
|
|
buf += 4;
|
|
|
|
memcpy (buf, router->GetIdentHash (), 32);
|
|
|
|
buf += 32;
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
CryptoPP::Gzip compressor;
|
2014-07-24 19:59:00 +00:00
|
|
|
compressor.Put (router->GetBuffer (), router->GetBufferLen ());
|
2013-10-27 15:20:29 +00:00
|
|
|
compressor.MessageEnd();
|
2014-07-24 19:59:00 +00:00
|
|
|
auto size = compressor.MaxRetrievable ();
|
2014-12-30 14:37:24 +00:00
|
|
|
htobe16buf (buf, size); // size
|
2013-11-20 12:46:09 +00:00
|
|
|
buf += 2;
|
2014-07-24 19:59:00 +00:00
|
|
|
// TODO: check if size doesn't exceed buffer
|
2013-10-27 15:20:29 +00:00
|
|
|
compressor.Get (buf, size);
|
2015-01-28 21:16:25 +00:00
|
|
|
buf += size;
|
|
|
|
m->len += (buf - payload); // payload size
|
2013-10-27 15:20:29 +00:00
|
|
|
FillI2NPMessageHeader (m, eI2NPDatabaseStore);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:02:00 +00:00
|
|
|
I2NPMessage * CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::LeaseSet> leaseSet, uint32_t replyToken)
|
2014-07-29 18:31:55 +00:00
|
|
|
{
|
2014-07-30 01:56:03 +00:00
|
|
|
if (!leaseSet) return nullptr;
|
2014-07-30 22:20:42 +00:00
|
|
|
I2NPMessage * m = NewI2NPShortMessage ();
|
2014-08-20 15:12:53 +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;
|
2014-08-20 15:12:53 +00:00
|
|
|
if (replyToken)
|
|
|
|
{
|
|
|
|
auto leases = leaseSet->GetNonExpiredLeases ();
|
|
|
|
if (leases.size () > 0)
|
|
|
|
{
|
2014-12-30 14:37:24 +00:00
|
|
|
htobe32buf (payload + size, leases[0].tunnelID);
|
2014-08-20 15:12:53 +00:00
|
|
|
size += 4; // reply tunnelID
|
|
|
|
memcpy (payload + size, leases[0].tunnelGateway, 32);
|
|
|
|
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;
|
2014-07-29 18:31:55 +00:00
|
|
|
FillI2NPMessageHeader (m, eI2NPDatabaseStore);
|
|
|
|
return m;
|
|
|
|
}
|
2013-10-27 15:20:29 +00:00
|
|
|
|
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++)
|
|
|
|
{
|
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))
|
2014-04-30 18:08:57 +00:00
|
|
|
{
|
|
|
|
LogPrint ("Record ",i," is ours");
|
|
|
|
|
2015-01-04 14:33:19 +00:00
|
|
|
i2p::crypto::ElGamalDecrypt (i2p::context.GetEncryptionPrivateKey (), record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, clearText);
|
2015-01-03 21:31:44 +00:00
|
|
|
// replace record to reply
|
2015-02-23 20:06:14 +00:00
|
|
|
if (i2p::context.AcceptsTunnels () &&
|
2015-03-18 17:07:11 +00:00
|
|
|
i2p::tunnel::tunnels.GetTransitTunnels ().size () <= MAX_NUM_TRANSIT_TUNNELS &&
|
|
|
|
!i2p::transport::transports.IsBandwidthExceeded ())
|
2014-10-01 02:08:05 +00:00
|
|
|
{
|
|
|
|
i2p::tunnel::TransitTunnel * transitTunnel =
|
|
|
|
i2p::tunnel::CreateTransitTunnel (
|
2015-01-04 14:33:19 +00:00
|
|
|
bufbe32toh (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
|
|
|
|
clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
bufbe32toh (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
|
|
|
clearText + BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET,
|
|
|
|
clearText + BUILD_REQUEST_RECORD_IV_KEY_OFFSET,
|
|
|
|
clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] & 0x80,
|
|
|
|
clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET ] & 0x40);
|
2014-10-01 02:08:05 +00:00
|
|
|
i2p::tunnel::tunnels.AddTransitTunnel (transitTunnel);
|
2015-01-03 21:31:44 +00:00
|
|
|
record[BUILD_RESPONSE_RECORD_RET_OFFSET] = 0;
|
2014-10-01 02:08:05 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-03 21:31:44 +00:00
|
|
|
record[BUILD_RESPONSE_RECORD_RET_OFFSET] = 30; // always reject with bandwidth reason (30)
|
2014-10-01 02:08:05 +00:00
|
|
|
|
2014-04-30 18:08:57 +00:00
|
|
|
//TODO: fill filler
|
2015-01-03 21:31:44 +00:00
|
|
|
CryptoPP::SHA256().CalculateDigest(record + BUILD_RESPONSE_RECORD_HASH_OFFSET,
|
|
|
|
record + BUILD_RESPONSE_RECORD_PADDING_OFFSET, BUILD_RESPONSE_RECORD_PADDING_SIZE + 1); // + 1 byte of ret
|
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);
|
2015-01-03 21:31:44 +00:00
|
|
|
uint8_t * reply = records + j*TUNNEL_BUILD_RECORD_SIZE;
|
|
|
|
encryption.Encrypt(reply, TUNNEL_BUILD_RECORD_SIZE, reply);
|
2014-04-30 18:08:57 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
void HandleVariableTunnelBuildMsg (uint32_t replyMsgID, uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
int num = buf[0];
|
|
|
|
LogPrint ("VariableTunnelBuild ", num, " records");
|
|
|
|
|
2015-01-26 16:56:10 +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
|
2013-10-27 15:20:29 +00:00
|
|
|
LogPrint ("VariableTunnelBuild reply for tunnel ", tunnel->GetTunnelID ());
|
2013-11-19 01:37:38 +00:00
|
|
|
if (tunnel->HandleTunnelBuildResponse (buf, len))
|
|
|
|
{
|
|
|
|
LogPrint ("Inbound tunnel ", tunnel->GetTunnelID (), " has been created");
|
2014-08-18 18:37:19 +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
|
|
|
{
|
2013-11-19 01:37:38 +00:00
|
|
|
LogPrint ("Inbound tunnel ", tunnel->GetTunnelID (), " has been declined");
|
2014-09-26 14:15:34 +00:00
|
|
|
tunnel->SetState (i2p::tunnel::eTunnelStateBuildFailed);
|
|
|
|
}
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-04 14:33:19 +00:00
|
|
|
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
2015-01-03 21:31:44 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
// so we send it to reply tunnel
|
2015-01-04 14:33:19 +00:00
|
|
|
transports.SendMessage (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
CreateTunnelGatewayMsg (bufbe32toh (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
2014-04-30 18:08:57 +00:00
|
|
|
eI2NPVariableTunnelBuildReply, buf, len,
|
2015-01-04 14:33:19 +00:00
|
|
|
bufbe32toh (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
2014-04-30 18:08:57 +00:00
|
|
|
else
|
2015-01-04 14:33:19 +00:00
|
|
|
transports.SendMessage (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
CreateI2NPMessage (eI2NPVariableTunnelBuild, buf, len,
|
|
|
|
bufbe32toh (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:08:57 +00:00
|
|
|
void HandleTunnelBuildMsg (uint8_t * buf, size_t len)
|
|
|
|
{
|
2015-01-04 14:33:19 +00:00
|
|
|
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
2015-01-03 21:31:44 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
// so we send it to reply tunnel
|
2015-01-04 14:33:19 +00:00
|
|
|
transports.SendMessage (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
CreateTunnelGatewayMsg (bufbe32toh (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
2014-04-30 18:08:57 +00:00
|
|
|
eI2NPTunnelBuildReply, buf, len,
|
2015-01-04 14:33:19 +00:00
|
|
|
bufbe32toh (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
|
2014-04-30 18:08:57 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-04 14:33:19 +00:00
|
|
|
transports.SendMessage (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
|
|
|
CreateI2NPMessage (eI2NPTunnelBuild, buf, len,
|
|
|
|
bufbe32toh (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET)));
|
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)
|
|
|
|
{
|
|
|
|
LogPrint ("VariableTunnelBuildReplyMsg replyMsgID=", replyMsgID);
|
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)
|
|
|
|
{
|
2013-11-19 01:37:38 +00:00
|
|
|
// reply for outbound tunnel
|
|
|
|
if (tunnel->HandleTunnelBuildResponse (buf, len))
|
|
|
|
{
|
|
|
|
LogPrint ("Outbound tunnel ", tunnel->GetTunnelID (), " has been created");
|
2014-08-18 18:37:19 +00:00
|
|
|
tunnel->SetState (i2p::tunnel::eTunnelStateEstablished);
|
2015-01-26 16:56:10 +00:00
|
|
|
i2p::tunnel::tunnels.AddOutboundTunnel (tunnel);
|
2013-11-19 01:37:38 +00:00
|
|
|
}
|
|
|
|
else
|
2014-09-26 14:15:34 +00:00
|
|
|
{
|
2013-11-19 01:37:38 +00:00
|
|
|
LogPrint ("Outbound tunnel ", tunnel->GetTunnelID (), " has been declined");
|
2014-09-26 14:15:34 +00:00
|
|
|
tunnel->SetState (i2p::tunnel::eTunnelStateBuildFailed);
|
|
|
|
}
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Pending tunnel for message ", replyMsgID, " not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
I2NPMessage * CreateTunnelDataMsg (const uint8_t * buf)
|
|
|
|
{
|
2015-03-11 01:54:25 +00:00
|
|
|
I2NPMessage * msg = NewI2NPShortMessage ();
|
2013-12-10 13:10:49 +00:00
|
|
|
memcpy (msg->GetPayload (), buf, i2p::tunnel::TUNNEL_DATA_MSG_SIZE);
|
|
|
|
msg->len += i2p::tunnel::TUNNEL_DATA_MSG_SIZE;
|
2013-10-27 15:20:29 +00:00
|
|
|
FillI2NPMessageHeader (msg, eI2NPTunnelData);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2NPMessage * CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload)
|
|
|
|
{
|
2015-03-11 01:54:25 +00:00
|
|
|
I2NPMessage * msg = NewI2NPShortMessage ();
|
2013-12-10 13:10:49 +00:00
|
|
|
memcpy (msg->GetPayload () + 4, payload, i2p::tunnel::TUNNEL_DATA_MSG_SIZE - 4);
|
2014-12-30 14:37:24 +00:00
|
|
|
htobe32buf (msg->GetPayload (), tunnelID);
|
2013-12-10 13:10:49 +00:00
|
|
|
msg->len += i2p::tunnel::TUNNEL_DATA_MSG_SIZE;
|
2013-10-27 15:20:29 +00:00
|
|
|
FillI2NPMessageHeader (msg, eI2NPTunnelData);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2013-11-10 23:23:26 +00:00
|
|
|
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len)
|
|
|
|
{
|
2014-07-30 22:20:42 +00:00
|
|
|
I2NPMessage * 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);
|
|
|
|
memcpy (payload + TUNNEL_GATEWAY_HEADER_SIZE, buf, len);
|
|
|
|
msg->len += TUNNEL_GATEWAY_HEADER_SIZE + len;
|
2013-11-10 23:23:26 +00:00
|
|
|
FillI2NPMessageHeader (msg, eI2NPTunnelGateway);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
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;
|
2015-06-19 18:38:31 +00:00
|
|
|
FillI2NPMessageHeader (msg.get(), eI2NPTunnelGateway); // TODO
|
2013-11-10 23:23:26 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
I2NPMessage * msg1 = CreateTunnelGatewayMsg (tunnelID, msg->GetBuffer (), msg->GetLength ());
|
2015-06-19 18:38:31 +00:00
|
|
|
return ToSharedI2NPMessage (msg1);
|
2013-11-10 23:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType,
|
|
|
|
const uint8_t * buf, size_t len, uint32_t replyMsgID)
|
|
|
|
{
|
2014-07-30 22:20:42 +00:00
|
|
|
I2NPMessage * 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;
|
|
|
|
memcpy (msg->GetPayload (), buf, len);
|
|
|
|
msg->len += len;
|
|
|
|
FillI2NPMessageHeader (msg, msgType, replyMsgID); // create content message
|
|
|
|
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);
|
2013-11-10 23:23:26 +00:00
|
|
|
FillI2NPMessageHeader (msg, eI2NPTunnelGateway); // gateway message
|
|
|
|
return msg;
|
|
|
|
}
|
2013-12-14 01:07:35 +00:00
|
|
|
|
2014-10-12 01:27:55 +00:00
|
|
|
size_t GetI2NPMessageLength (const uint8_t * msg)
|
2013-12-14 01:07:35 +00:00
|
|
|
{
|
2015-01-02 04:00:33 +00:00
|
|
|
return bufbe16toh (msg + I2NP_HEADER_SIZE_OFFSET) + I2NP_HEADER_SIZE;
|
2013-12-14 01:07:35 +00:00
|
|
|
}
|
2013-11-10 23:23:26 +00:00
|
|
|
|
2014-03-13 00:13:49 +00:00
|
|
|
void HandleI2NPMessage (uint8_t * msg, size_t len)
|
2013-10-27 15:20:29 +00:00
|
|
|
{
|
2015-01-02 04:00:33 +00:00
|
|
|
uint8_t typeID = msg[I2NP_HEADER_TYPEID_OFFSET];
|
|
|
|
uint32_t msgID = bufbe32toh (msg + I2NP_HEADER_MSGID_OFFSET);
|
|
|
|
LogPrint ("I2NP msg received len=", len,", type=", (int)typeID, ", msgID=", (unsigned int)msgID);
|
2013-10-27 15:20:29 +00:00
|
|
|
|
2015-01-02 04:00:33 +00:00
|
|
|
uint8_t * buf = msg + I2NP_HEADER_SIZE;
|
|
|
|
int size = bufbe16toh (msg + I2NP_HEADER_SIZE_OFFSET);
|
|
|
|
switch (typeID)
|
2014-03-13 00:13:49 +00:00
|
|
|
{
|
2013-10-27 15:20:29 +00:00
|
|
|
case eI2NPVariableTunnelBuild:
|
|
|
|
LogPrint ("VariableTunnelBuild");
|
|
|
|
HandleVariableTunnelBuildMsg (msgID, buf, size);
|
|
|
|
break;
|
|
|
|
case eI2NPVariableTunnelBuildReply:
|
|
|
|
LogPrint ("VariableTunnelBuildReply");
|
|
|
|
HandleVariableTunnelBuildReplyMsg (msgID, buf, size);
|
2014-01-06 03:21:59 +00:00
|
|
|
break;
|
2014-04-30 18:08:57 +00:00
|
|
|
case eI2NPTunnelBuild:
|
|
|
|
LogPrint ("TunnelBuild");
|
|
|
|
HandleTunnelBuildMsg (buf, size);
|
|
|
|
break;
|
|
|
|
case eI2NPTunnelBuildReply:
|
|
|
|
LogPrint ("TunnelBuildReply");
|
|
|
|
// TODO:
|
|
|
|
break;
|
2013-10-27 15:20:29 +00:00
|
|
|
default:
|
2015-01-02 04:00:33 +00:00
|
|
|
LogPrint ("Unexpected message ", (int)typeID);
|
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)
|
|
|
|
{
|
2015-01-01 21:51:15 +00:00
|
|
|
switch (msg->GetTypeID ())
|
2013-10-27 15:20:29 +00:00
|
|
|
{
|
2013-11-20 12:46:09 +00:00
|
|
|
case eI2NPTunnelData:
|
|
|
|
LogPrint ("TunnelData");
|
|
|
|
i2p::tunnel::tunnels.PostTunnelData (msg);
|
|
|
|
break;
|
|
|
|
case eI2NPTunnelGateway:
|
|
|
|
LogPrint ("TunnelGateway");
|
2015-01-23 21:26:39 +00:00
|
|
|
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
|
|
|
{
|
2014-03-17 20:50:03 +00:00
|
|
|
LogPrint ("Garlic");
|
2014-12-16 19:50:29 +00:00
|
|
|
if (msg->from)
|
|
|
|
{
|
|
|
|
if (msg->from->GetTunnelPool ())
|
2015-06-19 18:38:31 +00:00
|
|
|
msg->from->GetTunnelPool ()->ProcessGarlicMessage (msg);
|
2014-12-16 19:50:29 +00:00
|
|
|
else
|
|
|
|
LogPrint (eLogInfo, "Local destination for garlic doesn't exist anymore");
|
|
|
|
}
|
2014-10-08 11:55:46 +00:00
|
|
|
else
|
2015-06-19 18:38:31 +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:
|
2013-11-23 01:41:28 +00:00
|
|
|
case eI2NPDatabaseSearchReply:
|
2014-07-23 15:10:03 +00:00
|
|
|
case eI2NPDatabaseLookup:
|
2014-07-24 19:59:00 +00:00
|
|
|
// forward to netDb
|
2015-06-19 18:38:31 +00:00
|
|
|
i2p::data::netdb.PostI2NPMsg (msg);
|
2014-07-23 15:10:03 +00:00
|
|
|
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
|
|
|
LogPrint ("DeliveryStatus");
|
|
|
|
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);
|
2015-06-16 14:14:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-01-27 01:49:16 +00:00
|
|
|
case eI2NPVariableTunnelBuild:
|
|
|
|
case eI2NPVariableTunnelBuildReply:
|
|
|
|
case eI2NPTunnelBuild:
|
|
|
|
case eI2NPTunnelBuildReply:
|
|
|
|
// forward to tunnel thread
|
|
|
|
i2p::tunnel::tunnels.PostTunnelData (msg);
|
|
|
|
break;
|
2013-11-20 12:46:09 +00:00
|
|
|
default:
|
2014-03-13 00:13:49 +00:00
|
|
|
HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ());
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 03:00:41 +00:00
|
|
|
|
|
|
|
I2NPMessagesHandler::~I2NPMessagesHandler ()
|
|
|
|
{
|
|
|
|
Flush ();
|
|
|
|
}
|
|
|
|
|
2015-06-21 19:08:22 +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 ())
|
|
|
|
{
|
|
|
|
case eI2NPTunnelData:
|
2015-06-21 19:08:22 +00:00
|
|
|
m_TunnelMsgs.push_back (msg);
|
2015-01-24 03:05:33 +00:00
|
|
|
break;
|
2015-01-25 15:05:50 +00:00
|
|
|
case eI2NPTunnelGateway:
|
2015-06-21 19:08:22 +00:00
|
|
|
m_TunnelGatewayMsgs.push_back (msg);
|
2015-01-25 15:05:50 +00:00
|
|
|
break;
|
2015-01-24 03:05:33 +00:00
|
|
|
default:
|
2015-06-21 19:08:22 +00:00
|
|
|
HandleI2NPMessage (msg);
|
2015-01-24 03:05:33 +00:00
|
|
|
}
|
2015-01-23 03:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2NPMessagesHandler::Flush ()
|
|
|
|
{
|
|
|
|
if (!m_TunnelMsgs.empty ())
|
|
|
|
{
|
|
|
|
i2p::tunnel::tunnels.PostTunnelData (m_TunnelMsgs);
|
|
|
|
m_TunnelMsgs.clear ();
|
|
|
|
}
|
2015-01-24 03:05:33 +00:00
|
|
|
if (!m_TunnelGatewayMsgs.empty ())
|
|
|
|
{
|
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 ();
|
|
|
|
}
|
2015-01-23 03:00:41 +00:00
|
|
|
}
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|