i2pd/I2NPProtocol.cpp

571 lines
18 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>
2014-01-20 14:36:20 +00:00
#include "I2PEndian.h"
2013-10-27 15:20:29 +00:00
#include <cryptopp/sha.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"
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;
}
2014-07-06 21:48:16 +00:00
static std::atomic<uint32_t> I2NPmsgID(0); // TODO: create class
2013-10-27 15:20:29 +00:00
void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID)
{
I2NPHeader * header = msg->GetHeader ();
header->typeID = msgType;
if (replyMsgID) // for tunnel creation
header->msgID = htobe32 (replyMsgID);
else
{
header->msgID = htobe32 (I2NPmsgID);
I2NPmsgID++;
2013-10-27 15:20:29 +00:00
}
header->expiration = htobe64 (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number
int len = msg->GetLength () - sizeof (I2NPHeader);
2013-10-27 15:20:29 +00:00
header->size = htobe16 (len);
uint8_t hash[32];
CryptoPP::SHA256().CalculateDigest(hash, msg->GetPayload (), len);
header->chks = hash[0];
}
void RenewI2NPMessageHeader (I2NPMessage * msg)
{
if (msg)
{
I2NPHeader * header = msg->GetHeader ();
header->msgID = htobe32 (I2NPmsgID);
I2NPmsgID++;
header->expiration = htobe64 (i2p::util::GetMillisecondsSinceEpoch () + 5000);
}
}
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);
2013-10-27 15:20:29 +00:00
memcpy (msg->GetPayload (), buf, len);
msg->len += len;
FillI2NPMessageHeader (msg, msgType, replyMsgID);
return msg;
}
2013-11-10 23:23:26 +00:00
I2NPMessage * CreateI2NPMessage (const uint8_t * buf, int len)
{
I2NPMessage * msg = NewI2NPMessage ();
memcpy (msg->GetBuffer (), buf, len);
2014-01-17 13:12:57 +00:00
msg->len = msg->offset + len;
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
{
2014-01-17 13:12:57 +00:00
I2NPDeliveryStatusMsg msg;
2014-02-24 20:16:39 +00:00
if (msgID)
{
msg.msgID = htobe32 (msgID);
msg.timestamp = htobe64 (i2p::util::GetMillisecondsSinceEpoch ());
}
else // for SSU establishment
{
2014-02-26 01:37:39 +00:00
msg.msgID = htobe32 (i2p::context.GetRandomNumberGenerator ().GenerateWord32 ());
2014-02-24 20:16:39 +00:00
msg.timestamp = htobe64 (2); // netID = 2
}
2013-10-27 15:20:29 +00:00
return CreateI2NPMessage (eI2NPDeliveryStatus, (uint8_t *)&msg, sizeof (msg));
}
2013-11-19 01:37:38 +00:00
I2NPMessage * CreateDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
2014-07-08 11:45:16 +00:00
uint32_t replyTunnelID, bool exploratory, std::set<i2p::data::IdentHash> * excludedPeers,
bool encryption)
2013-10-27 15:20:29 +00:00
{
2013-11-19 01:37:38 +00:00
I2NPMessage * m = NewI2NPMessage ();
uint8_t * buf = m->GetPayload ();
memcpy (buf, key, 32); // key
buf += 32;
memcpy (buf, from, 32); // from
buf += 32;
if (replyTunnelID)
2013-10-27 15:20:29 +00:00
{
2014-07-08 11:45:16 +00:00
*buf = encryption ? 0x03: 0x01; // set delivery flag
2013-11-19 01:37:38 +00:00
*(uint32_t *)(buf+1) = htobe32 (replyTunnelID);
buf += 5;
}
else
{
2014-07-08 11:45:16 +00:00
encryption = false; // encryption can we set for tunnels only
2013-11-19 01:37:38 +00:00
*buf = 0; // flag
buf++;
}
if (exploratory)
{
*(uint16_t *)buf = htobe16 (1); // one exlude record
buf += 2;
// reply with non-floodfill routers only
memset (buf, 0, 32);
buf += 32;
}
else
{
2014-01-05 14:53:44 +00:00
if (excludedPeers)
{
int cnt = excludedPeers->size ();
*(uint16_t *)buf = htobe16 (cnt);
buf += 2;
for (auto& it: *excludedPeers)
{
memcpy (buf, it, 32);
buf += 32;
}
}
else
{
// nothing to exclude
*(uint16_t *)buf = htobe16 (0);
buf += 2;
}
2013-11-19 01:37:38 +00:00
}
2014-07-08 11:45:16 +00:00
if (encryption)
{
// session key and tag for reply
auto& rnd = i2p::context.GetRandomNumberGenerator ();
rnd.GenerateBlock (buf, 32); // key
buf[32] = 1; // 1 tag
rnd.GenerateBlock (buf + 33, 32); // tag
i2p::garlic::routing.AddSessionKey (buf, buf + 33); // introduce new key-tag to garlic engine
buf += 65;
}
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-07-25 02:01:07 +00:00
I2NPMessage * CreateDatabaseSearchReply (const i2p::data::IdentHash& ident,
const i2p::data::RouterInfo * floodfill)
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;
buf[len] = floodfill ? 1 : 0; // 1 router for now
len++;
if (floodfill)
{
memcpy (buf + len, floodfill->GetIdentHash (), 32);
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;
}
I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::RouterInfo * router)
2013-10-27 15:20:29 +00:00
{
if (!router) // we send own RouterInfo
router = &context.GetRouterInfo ();
2014-07-30 22:20:42 +00:00
I2NPMessage * m = NewI2NPShortMessage ();
2013-10-27 15:20:29 +00:00
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)m->GetPayload ();
memcpy (msg->key, router->GetIdentHash (), 32);
2013-10-27 15:20:29 +00:00
msg->type = 0;
msg->replyToken = 0;
CryptoPP::Gzip compressor;
compressor.Put (router->GetBuffer (), router->GetBufferLen ());
2013-10-27 15:20:29 +00:00
compressor.MessageEnd();
auto size = compressor.MaxRetrievable ();
2013-10-27 15:20:29 +00:00
uint8_t * buf = m->GetPayload () + sizeof (I2NPDatabaseStoreMsg);
2013-11-20 12:46:09 +00:00
*(uint16_t *)buf = htobe16 (size); // size
buf += 2;
// TODO: check if size doesn't exceed buffer
2013-10-27 15:20:29 +00:00
compressor.Get (buf, size);
2013-11-20 12:46:09 +00:00
m->len += sizeof (I2NPDatabaseStoreMsg) + 2 + size; // payload size
2013-10-27 15:20:29 +00:00
FillI2NPMessageHeader (m, eI2NPDatabaseStore);
return m;
}
2014-08-20 15:12:53 +00:00
I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::LeaseSet * leaseSet, uint32_t replyToken)
2014-07-29 18:31:55 +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 ();
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)payload;
memcpy (msg->key, leaseSet->GetIdentHash (), 32);
2014-07-29 18:31:55 +00:00
msg->type = 1; // LeaseSet
2014-08-20 15:12:53 +00:00
msg->replyToken = htobe32 (replyToken);
size_t size = sizeof (I2NPDatabaseStoreMsg);
if (replyToken)
{
auto leases = leaseSet->GetNonExpiredLeases ();
if (leases.size () > 0)
{
*(uint32_t *)(payload + size) = htobe32 (leases[0].tunnelID);
size += 4; // reply tunnelID
memcpy (payload + size, leases[0].tunnelGateway, 32);
size += 32; // reply tunnel gateway
}
else
msg->replyToken = 0;
}
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
I2NPBuildRequestRecordClearText CreateBuildRequestRecord (
const uint8_t * ourIdent, uint32_t receiveTunnelID,
const uint8_t * nextIdent, uint32_t nextTunnelID,
const uint8_t * layerKey,const uint8_t * ivKey,
const uint8_t * replyKey, const uint8_t * replyIV, uint32_t nextMessageID,
bool isGateway, bool isEndpoint)
{
I2NPBuildRequestRecordClearText clearText;
clearText.receiveTunnel = htobe32 (receiveTunnelID);
clearText.nextTunnel = htobe32(nextTunnelID);
memcpy (clearText.layerKey, layerKey, 32);
memcpy (clearText.ivKey, ivKey, 32);
memcpy (clearText.replyKey, replyKey, 32);
memcpy (clearText.replyIV, replyIV, 16);
clearText.flag = 0;
if (isGateway) clearText.flag |= 0x80;
if (isEndpoint) clearText.flag |= 0x40;
memcpy (clearText.ourIdent, ourIdent, 32);
memcpy (clearText.nextIdent, nextIdent, 32);
2014-09-03 17:10:30 +00:00
clearText.requestTime = htobe32 (i2p::util::GetHoursSinceEpoch ());
2013-10-27 15:20:29 +00:00
clearText.nextMessageID = htobe32(nextMessageID);
return clearText;
}
void EncryptBuildRequestRecord (const i2p::data::RouterInfo& router,
const I2NPBuildRequestRecordClearText& clearText,
I2NPBuildRequestRecordElGamalEncrypted& record)
{
router.GetElGamalEncryption ()->Encrypt ((uint8_t *)&clearText, sizeof(clearText), record.encrypted);
2013-12-10 13:10:49 +00:00
memcpy (record.toPeer, (const uint8_t *)router.GetIdentHash (), 16);
2013-10-27 15:20:29 +00:00
}
2014-04-30 18:08:57 +00:00
bool HandleBuildRequestRecords (int num, I2NPBuildRequestRecordElGamalEncrypted * records, I2NPBuildRequestRecordClearText& clearText)
{
for (int i = 0; i < num; i++)
{
if (!memcmp (records[i].toPeer, (const uint8_t *)i2p::context.GetRouterInfo ().GetIdentHash (), 16))
{
LogPrint ("Record ",i," is ours");
i2p::crypto::ElGamalDecrypt (i2p::context.GetPrivateKey (), records[i].encrypted, (uint8_t *)&clearText);
i2p::tunnel::TransitTunnel * transitTunnel =
i2p::tunnel::CreateTransitTunnel (
be32toh (clearText.receiveTunnel),
clearText.nextIdent, be32toh (clearText.nextTunnel),
clearText.layerKey, clearText.ivKey,
clearText.flag & 0x80, clearText.flag & 0x40);
i2p::tunnel::tunnels.AddTransitTunnel (transitTunnel);
// replace record to reply
I2NPBuildResponseRecord * reply = (I2NPBuildResponseRecord *)(records + i);
reply->ret = 0;
//TODO: fill filler
CryptoPP::SHA256().CalculateDigest(reply->hash, reply->padding, sizeof (reply->padding) + 1); // + 1 byte of ret
// 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++)
{
2014-05-15 22:58:26 +00:00
encryption.SetKey (clearText.replyKey);
encryption.SetIV (clearText.replyIV);
encryption.Encrypt((uint8_t *)(records + j), sizeof (records[j]), (uint8_t *)(records + j));
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");
i2p::tunnel::Tunnel * tunnel = i2p::tunnel::tunnels.GetPendingTunnel (replyMsgID);
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");
tunnel->SetState (i2p::tunnel::eTunnelStateEstablished);
2013-11-19 01:37:38 +00:00
i2p::tunnel::tunnels.AddInboundTunnel (static_cast<i2p::tunnel::InboundTunnel *>(tunnel));
}
else
LogPrint ("Inbound tunnel ", tunnel->GetTunnelID (), " has been declined");
2013-10-27 15:20:29 +00:00
}
else
{
I2NPBuildRequestRecordElGamalEncrypted * records = (I2NPBuildRequestRecordElGamalEncrypted *)(buf+1);
2014-04-30 18:08:57 +00:00
I2NPBuildRequestRecordClearText clearText;
if (HandleBuildRequestRecords (num, records, clearText))
{
if (clearText.flag & 0x40) // we are endpoint of outboud tunnel
{
// so we send it to reply tunnel
i2p::transports.SendMessage (clearText.nextIdent,
CreateTunnelGatewayMsg (be32toh (clearText.nextTunnel),
eI2NPVariableTunnelBuildReply, buf, len,
be32toh (clearText.nextMessageID)));
2013-10-27 15:20:29 +00:00
}
2014-04-30 18:08:57 +00:00
else
i2p::transports.SendMessage (clearText.nextIdent,
CreateI2NPMessage (eI2NPVariableTunnelBuild, buf, len, be32toh (clearText.nextMessageID)));
2013-10-27 15:20:29 +00:00
}
}
}
2014-04-30 18:08:57 +00:00
void HandleTunnelBuildMsg (uint8_t * buf, size_t len)
{
I2NPBuildRequestRecordClearText clearText;
if (HandleBuildRequestRecords (NUM_TUNNEL_BUILD_RECORDS, (I2NPBuildRequestRecordElGamalEncrypted *)buf, clearText))
{
if (clearText.flag & 0x40) // we are endpoint of outbound tunnel
{
// so we send it to reply tunnel
i2p::transports.SendMessage (clearText.nextIdent,
CreateTunnelGatewayMsg (be32toh (clearText.nextTunnel),
eI2NPTunnelBuildReply, buf, len,
be32toh (clearText.nextMessageID)));
}
else
i2p::transports.SendMessage (clearText.nextIdent,
CreateI2NPMessage (eI2NPTunnelBuild, buf, len, be32toh (clearText.nextMessageID)));
}
}
2013-10-27 15:20:29 +00:00
void HandleVariableTunnelBuildReplyMsg (uint32_t replyMsgID, uint8_t * buf, size_t len)
{
LogPrint ("VariableTunnelBuildReplyMsg replyMsgID=", replyMsgID);
i2p::tunnel::Tunnel * tunnel = i2p::tunnel::tunnels.GetPendingTunnel (replyMsgID);
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");
tunnel->SetState (i2p::tunnel::eTunnelStateEstablished);
2013-11-19 01:37:38 +00:00
i2p::tunnel::tunnels.AddOutboundTunnel (static_cast<i2p::tunnel::OutboundTunnel *>(tunnel));
}
else
LogPrint ("Outbound tunnel ", tunnel->GetTunnelID (), " has been declined");
2013-10-27 15:20:29 +00:00
}
else
LogPrint ("Pending tunnel for message ", replyMsgID, " not found");
}
I2NPMessage * CreateTunnelDataMsg (const uint8_t * buf)
{
I2NPMessage * msg = NewI2NPMessage ();
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)
{
I2NPMessage * msg = NewI2NPMessage ();
2013-12-10 13:10:49 +00:00
memcpy (msg->GetPayload () + 4, payload, i2p::tunnel::TUNNEL_DATA_MSG_SIZE - 4);
2013-10-27 15:20:29 +00:00
*(uint32_t *)(msg->GetPayload ()) = htobe32 (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);
2013-11-10 23:23:26 +00:00
TunnelGatewayHeader * header = (TunnelGatewayHeader *)msg->GetPayload ();
header->tunnelID = htobe32 (tunnelID);
header->length = htobe16 (len);
memcpy (msg->GetPayload () + sizeof (TunnelGatewayHeader), buf, len);
msg->len += sizeof (TunnelGatewayHeader) + len;
FillI2NPMessageHeader (msg, eI2NPTunnelGateway);
return msg;
}
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessage * msg)
{
if (msg->offset >= sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader))
{
// message is capable to be used without copying
TunnelGatewayHeader * header = (TunnelGatewayHeader *)(msg->GetBuffer () - sizeof (TunnelGatewayHeader));
header->tunnelID = htobe32 (tunnelID);
int len = msg->GetLength ();
header->length = htobe16 (len);
msg->offset -= (sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader));
msg->len = msg->offset + sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader) +len;
FillI2NPMessageHeader (msg, eI2NPTunnelGateway);
return msg;
}
else
{
I2NPMessage * msg1 = CreateTunnelGatewayMsg (tunnelID, msg->GetBuffer (), msg->GetLength ());
DeleteI2NPMessage (msg);
return msg1;
}
}
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);
2013-11-10 23:23:26 +00:00
size_t gatewayMsgOffset = sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader);
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;
TunnelGatewayHeader * header = (TunnelGatewayHeader *)msg->GetPayload ();
header->tunnelID = htobe32 (tunnelID);
header->length = htobe16 (len);
FillI2NPMessageHeader (msg, eI2NPTunnelGateway); // gateway message
return msg;
}
2013-10-27 15:20:29 +00:00
2013-11-10 23:23:26 +00:00
void HandleTunnelGatewayMsg (I2NPMessage * msg)
2013-10-27 15:20:29 +00:00
{
2013-11-10 23:23:26 +00:00
TunnelGatewayHeader * header = (TunnelGatewayHeader *)msg->GetPayload ();
2013-10-27 15:20:29 +00:00
uint32_t tunnelID = be32toh(header->tunnelID);
2013-11-10 23:23:26 +00:00
uint16_t len = be16toh(header->length);
// we make payload as new I2NP message to send
msg->offset += sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader);
msg->len = msg->offset + len;
LogPrint ("TunnelGateway of ", (int)len, " bytes for tunnel ", (unsigned int)tunnelID, ". Msg type ", (int)msg->GetHeader()->typeID);
2014-08-10 13:04:17 +00:00
if (msg->GetHeader()->typeID == eI2NPDatabaseStore ||
msg->GetHeader()->typeID == eI2NPDatabaseSearchReply)
2014-07-04 01:53:29 +00:00
{
2014-08-10 13:04:17 +00:00
// transit DatabaseStore my contain new/updated RI
// or DatabaseSearchReply with new routers
2014-07-04 01:53:29 +00:00
auto ds = NewI2NPMessage ();
*ds = *msg;
i2p::data::netdb.PostI2NPMsg (ds);
}
2013-11-10 23:23:26 +00:00
i2p::tunnel::TransitTunnel * tunnel = i2p::tunnel::tunnels.GetTransitTunnel (tunnelID);
2013-10-27 15:20:29 +00:00
if (tunnel)
tunnel->SendTunnelDataMsg (msg);
2013-10-27 15:20:29 +00:00
else
2013-11-10 23:23:26 +00:00
{
2013-10-27 15:20:29 +00:00
LogPrint ("Tunnel ", (unsigned int)tunnelID, " not found");
2013-11-10 23:23:26 +00:00
i2p::DeleteI2NPMessage (msg);
}
2013-10-27 15:20:29 +00:00
}
2013-12-14 01:07:35 +00:00
size_t GetI2NPMessageLength (uint8_t * msg)
{
I2NPHeader * header = (I2NPHeader *)msg;
return be16toh (header->size) + sizeof (I2NPHeader);
}
2013-11-10 23:23:26 +00:00
void HandleI2NPMessage (uint8_t * msg, size_t len)
2013-10-27 15:20:29 +00:00
{
I2NPHeader * header = (I2NPHeader *)msg;
uint32_t msgID = be32toh (header->msgID);
LogPrint ("I2NP msg received len=", len,", type=", (int)header->typeID, ", msgID=", (unsigned int)msgID);
uint8_t * buf = msg + sizeof (I2NPHeader);
int size = be16toh (header->size);
switch (header->typeID)
{
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:
2013-11-10 23:23:26 +00:00
LogPrint ("Unexpected message ", (int)header->typeID);
2013-10-27 15:20:29 +00:00
}
}
void HandleI2NPMessage (I2NPMessage * msg)
2013-10-27 15:20:29 +00:00
{
if (msg)
{
2013-11-20 12:46:09 +00:00
switch (msg->GetHeader ()->typeID)
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");
HandleTunnelGatewayMsg (msg);
break;
2014-03-17 20:50:03 +00:00
case eI2NPGarlic:
LogPrint ("Garlic");
i2p::garlic::routing.PostI2NPMsg (msg);
2014-03-17 20:50:03 +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:
LogPrint ("DeliveryStatus");
if (msg->from && msg->from->GetTunnelPool ())
msg->from->GetTunnelPool ()->ProcessDeliveryStatus (msg);
else
i2p::garlic::routing.PostI2NPMsg (msg);
2013-11-20 12:46:09 +00:00
break;
default:
HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ());
2013-11-20 12:46:09 +00:00
DeleteI2NPMessage (msg);
2013-10-27 15:20:29 +00:00
}
}
}
}