2013-10-27 15:20:29 +00:00
|
|
|
#ifndef I2NP_PROTOCOL_H__
|
|
|
|
#define I2NP_PROTOCOL_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-01-05 14:53:44 +00:00
|
|
|
#include <set>
|
2013-11-10 23:23:26 +00:00
|
|
|
#include <string.h>
|
2014-02-07 16:26:00 +00:00
|
|
|
#include "I2PEndian.h"
|
2013-10-27 15:20:29 +00:00
|
|
|
#include "RouterInfo.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
#pragma pack (1)
|
|
|
|
|
|
|
|
struct I2NPHeader
|
|
|
|
{
|
|
|
|
uint8_t typeID;
|
|
|
|
uint32_t msgID;
|
|
|
|
uint64_t expiration;
|
|
|
|
uint16_t size;
|
|
|
|
uint8_t chks;
|
|
|
|
};
|
|
|
|
|
2014-02-07 16:26:00 +00:00
|
|
|
struct I2NPHeaderShort
|
|
|
|
{
|
|
|
|
uint8_t typeID;
|
|
|
|
uint32_t shortExpiration;
|
|
|
|
};
|
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
struct I2NPDatabaseStoreMsg
|
|
|
|
{
|
|
|
|
uint8_t key[32];
|
|
|
|
uint8_t type;
|
2013-11-20 12:46:09 +00:00
|
|
|
uint32_t replyToken;
|
2013-10-27 15:20:29 +00:00
|
|
|
};
|
|
|
|
|
2014-01-17 13:12:57 +00:00
|
|
|
struct I2NPDeliveryStatusMsg
|
|
|
|
{
|
|
|
|
uint32_t msgID;
|
|
|
|
uint64_t timestamp;
|
|
|
|
};
|
2013-11-19 01:37:38 +00:00
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
struct I2NPBuildRequestRecordClearText
|
|
|
|
{
|
|
|
|
uint32_t receiveTunnel;
|
|
|
|
uint8_t ourIdent[32];
|
|
|
|
uint32_t nextTunnel;
|
|
|
|
uint8_t nextIdent[32];
|
|
|
|
uint8_t layerKey[32];
|
|
|
|
uint8_t ivKey[32];
|
|
|
|
uint8_t replyKey[32];
|
|
|
|
uint8_t replyIV[16];
|
|
|
|
uint8_t flag;
|
|
|
|
uint32_t requestTime;
|
|
|
|
uint32_t nextMessageID;
|
|
|
|
uint8_t filler[29];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct I2NPBuildResponseRecord
|
|
|
|
{
|
|
|
|
uint8_t hash[32];
|
|
|
|
uint8_t padding[495];
|
|
|
|
uint8_t ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct I2NPBuildRequestRecordElGamalEncrypted
|
|
|
|
{
|
|
|
|
uint8_t toPeer[16];
|
|
|
|
uint8_t encrypted[512];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TunnelGatewayHeader
|
|
|
|
{
|
|
|
|
uint32_t tunnelID;
|
|
|
|
uint16_t length;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#pragma pack ()
|
|
|
|
|
|
|
|
enum I2NPMessageType
|
|
|
|
{
|
|
|
|
eI2NPDatabaseStore = 1,
|
|
|
|
eI2NPDatabaseLookup = 2,
|
|
|
|
eI2NPDatabaseSearchReply = 3,
|
|
|
|
eI2NPDeliveryStatus = 10,
|
|
|
|
eI2NPGarlic = 11,
|
|
|
|
eI2NPTunnelData = 18,
|
|
|
|
eI2NPTunnelGateway = 19,
|
|
|
|
eI2NPData = 20,
|
|
|
|
eI2NPVariableTunnelBuild = 23,
|
|
|
|
eI2NPVariableTunnelBuildReply = 24
|
|
|
|
};
|
|
|
|
|
|
|
|
const int NTCP_MAX_MESSAGE_SIZE = 16384;
|
|
|
|
struct I2NPMessage
|
|
|
|
{
|
|
|
|
uint8_t buf[NTCP_MAX_MESSAGE_SIZE];
|
|
|
|
size_t len, offset;
|
2013-11-10 23:23:26 +00:00
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
I2NPHeader * GetHeader () { return (I2NPHeader *)(buf + offset); };
|
|
|
|
uint8_t * GetPayload () { return buf + offset + sizeof(I2NPHeader); };
|
|
|
|
uint8_t * GetBuffer () { return buf + offset; };
|
|
|
|
size_t GetLength () const { return len - offset; };
|
2013-11-10 23:23:26 +00:00
|
|
|
|
|
|
|
I2NPMessage& operator=(const I2NPMessage& other)
|
|
|
|
{
|
|
|
|
memcpy (buf + offset, other.buf + other.offset, other.GetLength ());
|
|
|
|
len = offset + other.GetLength ();
|
|
|
|
return *this;
|
|
|
|
}
|
2014-02-07 16:26:00 +00:00
|
|
|
|
|
|
|
// for SSU only
|
|
|
|
uint8_t * GetSSUHeader () { return buf + offset + sizeof(I2NPHeader) - sizeof(I2NPHeaderShort); };
|
|
|
|
void FromSSU (uint32_t msgID) // we have received SSU message and convert it to regular
|
|
|
|
{
|
|
|
|
I2NPHeaderShort ssu = *(I2NPHeaderShort *)GetSSUHeader ();
|
|
|
|
I2NPHeader * header = GetHeader ();
|
|
|
|
header->typeID = ssu.typeID;
|
|
|
|
header->msgID = htobe32 (msgID);
|
|
|
|
header->expiration = htobe64 (be32toh (ssu.shortExpiration)*1000LL);
|
|
|
|
header->size = htobe16 (len - offset - sizeof (I2NPHeader));
|
|
|
|
header->chks = 0;
|
|
|
|
}
|
2013-10-27 15:20:29 +00:00
|
|
|
};
|
|
|
|
I2NPMessage * NewI2NPMessage ();
|
|
|
|
void DeleteI2NPMessage (I2NPMessage * msg);
|
|
|
|
void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID = 0);
|
|
|
|
I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID = 0);
|
2013-11-10 23:23:26 +00:00
|
|
|
I2NPMessage * CreateI2NPMessage (const uint8_t * buf, int len);
|
2013-10-27 15:20:29 +00:00
|
|
|
|
2014-01-09 03:47:22 +00:00
|
|
|
I2NPMessage * CreateDeliveryStatusMsg (uint32_t msgID);
|
2013-11-19 01:37:38 +00:00
|
|
|
I2NPMessage * CreateDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
|
2014-01-05 14:53:44 +00:00
|
|
|
uint32_t replyTunnelID, bool exploratory = false,
|
|
|
|
std::set<i2p::data::IdentHash> * excludedPeers = nullptr);
|
2014-01-06 03:21:59 +00:00
|
|
|
void HandleDatabaseLookupMsg (uint8_t * buf, size_t len);
|
|
|
|
I2NPMessage * CreateDatabaseSearchReply (const i2p::data::IdentHash& ident);
|
|
|
|
|
2013-10-27 15:20:29 +00:00
|
|
|
I2NPMessage * CreateDatabaseStoreMsg ();
|
|
|
|
|
|
|
|
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);
|
|
|
|
void EncryptBuildRequestRecord (const i2p::data::RouterInfo& router,
|
|
|
|
const I2NPBuildRequestRecordClearText& clearText,
|
|
|
|
I2NPBuildRequestRecordElGamalEncrypted& record);
|
|
|
|
|
|
|
|
void HandleVariableTunnelBuildMsg (uint32_t replyMsgID, uint8_t * buf, size_t len);
|
|
|
|
void HandleVariableTunnelBuildReplyMsg (uint32_t replyMsgID, uint8_t * buf, size_t len);
|
|
|
|
|
|
|
|
I2NPMessage * CreateTunnelDataMsg (const uint8_t * buf);
|
|
|
|
I2NPMessage * CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload);
|
|
|
|
|
2013-11-10 23:23:26 +00:00
|
|
|
void HandleTunnelGatewayMsg (I2NPMessage * msg);
|
|
|
|
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len);
|
|
|
|
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType,
|
|
|
|
const uint8_t * buf, size_t len, uint32_t replyMsgID = 0);
|
|
|
|
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessage * msg);
|
2013-12-14 01:07:35 +00:00
|
|
|
|
|
|
|
size_t GetI2NPMessageLength (uint8_t * msg);
|
2014-01-04 03:25:22 +00:00
|
|
|
void HandleI2NPMessage (uint8_t * msg, size_t len, bool isFromTunnel);
|
|
|
|
void HandleI2NPMessage (I2NPMessage * msg, bool isFromTunnel);
|
2013-10-27 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|