mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
12 KiB
268 lines
12 KiB
11 years ago
|
#ifndef I2NP_PROTOCOL_H__
|
||
|
#define I2NP_PROTOCOL_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
10 years ago
|
#include <string.h>
|
||
11 years ago
|
#include <set>
|
||
10 years ago
|
#include <memory>
|
||
9 years ago
|
#include "Crypto.h"
|
||
11 years ago
|
#include "I2PEndian.h"
|
||
10 years ago
|
#include "Identity.h"
|
||
11 years ago
|
#include "RouterInfo.h"
|
||
10 years ago
|
#include "LeaseSet.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
10 years ago
|
{
|
||
|
// I2NP header
|
||
|
const size_t I2NP_HEADER_TYPEID_OFFSET = 0;
|
||
|
const size_t I2NP_HEADER_MSGID_OFFSET = I2NP_HEADER_TYPEID_OFFSET + 1;
|
||
|
const size_t I2NP_HEADER_EXPIRATION_OFFSET = I2NP_HEADER_MSGID_OFFSET + 4;
|
||
|
const size_t I2NP_HEADER_SIZE_OFFSET = I2NP_HEADER_EXPIRATION_OFFSET + 8;
|
||
|
const size_t I2NP_HEADER_CHKS_OFFSET = I2NP_HEADER_SIZE_OFFSET + 2;
|
||
10 years ago
|
const size_t I2NP_HEADER_SIZE = I2NP_HEADER_CHKS_OFFSET + 1;
|
||
10 years ago
|
|
||
|
// I2NP short header
|
||
|
const size_t I2NP_SHORT_HEADER_TYPEID_OFFSET = 0;
|
||
|
const size_t I2NP_SHORT_HEADER_EXPIRATION_OFFSET = I2NP_SHORT_HEADER_TYPEID_OFFSET + 1;
|
||
|
const size_t I2NP_SHORT_HEADER_SIZE = I2NP_SHORT_HEADER_EXPIRATION_OFFSET + 4;
|
||
10 years ago
|
|
||
|
// Tunnel Gateway header
|
||
|
const size_t TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET = 0;
|
||
|
const size_t TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET = TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET + 4;
|
||
|
const size_t TUNNEL_GATEWAY_HEADER_SIZE = TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET + 2;
|
||
10 years ago
|
|
||
|
// DeliveryStatus
|
||
|
const size_t DELIVERY_STATUS_MSGID_OFFSET = 0;
|
||
|
const size_t DELIVERY_STATUS_TIMESTAMP_OFFSET = DELIVERY_STATUS_MSGID_OFFSET + 4;
|
||
|
const size_t DELIVERY_STATUS_SIZE = DELIVERY_STATUS_TIMESTAMP_OFFSET + 8;
|
||
10 years ago
|
|
||
|
// DatabaseStore
|
||
|
const size_t DATABASE_STORE_KEY_OFFSET = 0;
|
||
|
const size_t DATABASE_STORE_TYPE_OFFSET = DATABASE_STORE_KEY_OFFSET + 32;
|
||
|
const size_t DATABASE_STORE_REPLY_TOKEN_OFFSET = DATABASE_STORE_TYPE_OFFSET + 1;
|
||
|
const size_t DATABASE_STORE_HEADER_SIZE = DATABASE_STORE_REPLY_TOKEN_OFFSET + 4;
|
||
10 years ago
|
|
||
|
// TunnelBuild
|
||
|
const size_t TUNNEL_BUILD_RECORD_SIZE = 528;
|
||
10 years ago
|
|
||
|
//BuildRequestRecordClearText
|
||
|
const size_t BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET = 0;
|
||
|
const size_t BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET = BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET + 4;
|
||
|
const size_t BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET = BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET + 32;
|
||
|
const size_t BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET = BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET + 4;
|
||
|
const size_t BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET = BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET + 32;
|
||
|
const size_t BUILD_REQUEST_RECORD_IV_KEY_OFFSET = BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET + 32;
|
||
|
const size_t BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET = BUILD_REQUEST_RECORD_IV_KEY_OFFSET + 32;
|
||
|
const size_t BUILD_REQUEST_RECORD_REPLY_IV_OFFSET = BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET + 32;
|
||
|
const size_t BUILD_REQUEST_RECORD_FLAG_OFFSET = BUILD_REQUEST_RECORD_REPLY_IV_OFFSET + 16;
|
||
|
const size_t BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET = BUILD_REQUEST_RECORD_FLAG_OFFSET + 1;
|
||
|
const size_t BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET = BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET + 4;
|
||
|
const size_t BUILD_REQUEST_RECORD_PADDING_OFFSET = BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET + 4;
|
||
|
const size_t BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE = 222;
|
||
10 years ago
|
|
||
10 years ago
|
// BuildRequestRecordEncrypted
|
||
|
const size_t BUILD_REQUEST_RECORD_TO_PEER_OFFSET = 0;
|
||
|
const size_t BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET = BUILD_REQUEST_RECORD_TO_PEER_OFFSET + 16;
|
||
|
|
||
10 years ago
|
// BuildResponseRecord
|
||
|
const size_t BUILD_RESPONSE_RECORD_HASH_OFFSET = 0;
|
||
|
const size_t BUILD_RESPONSE_RECORD_PADDING_OFFSET = 32;
|
||
|
const size_t BUILD_RESPONSE_RECORD_PADDING_SIZE = 495;
|
||
|
const size_t BUILD_RESPONSE_RECORD_RET_OFFSET = BUILD_RESPONSE_RECORD_PADDING_OFFSET + BUILD_RESPONSE_RECORD_PADDING_SIZE;
|
||
11 years ago
|
|
||
|
enum I2NPMessageType
|
||
|
{
|
||
|
eI2NPDatabaseStore = 1,
|
||
|
eI2NPDatabaseLookup = 2,
|
||
|
eI2NPDatabaseSearchReply = 3,
|
||
|
eI2NPDeliveryStatus = 10,
|
||
|
eI2NPGarlic = 11,
|
||
|
eI2NPTunnelData = 18,
|
||
|
eI2NPTunnelGateway = 19,
|
||
|
eI2NPData = 20,
|
||
11 years ago
|
eI2NPTunnelBuild = 21,
|
||
|
eI2NPTunnelBuildReply = 22,
|
||
11 years ago
|
eI2NPVariableTunnelBuild = 23,
|
||
|
eI2NPVariableTunnelBuildReply = 24
|
||
|
};
|
||
|
|
||
11 years ago
|
const int NUM_TUNNEL_BUILD_RECORDS = 8;
|
||
|
|
||
10 years ago
|
// DatabaseLookup flags
|
||
|
const uint8_t DATABASE_LOOKUP_DELIVERY_FLAG = 0x01;
|
||
9 years ago
|
const uint8_t DATABASE_LOOKUP_ENCRYPTION_FLAG = 0x02;
|
||
10 years ago
|
const uint8_t DATABASE_LOOKUP_TYPE_FLAGS_MASK = 0x0C;
|
||
|
const uint8_t DATABASE_LOOKUP_TYPE_NORMAL_LOOKUP = 0;
|
||
|
const uint8_t DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP = 0x04; // 0100
|
||
|
const uint8_t DATABASE_LOOKUP_TYPE_ROUTERINFO_LOOKUP = 0x08; // 1000
|
||
|
const uint8_t DATABASE_LOOKUP_TYPE_EXPLORATORY_LOOKUP = 0x0C; // 1100
|
||
|
|
||
11 years ago
|
namespace tunnel
|
||
|
{
|
||
|
class InboundTunnel;
|
||
10 years ago
|
class TunnelPool;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
const size_t I2NP_MAX_MESSAGE_SIZE = 32768;
|
||
10 years ago
|
const size_t I2NP_MAX_SHORT_MESSAGE_SIZE = 4096;
|
||
9 years ago
|
const unsigned int I2NP_MESSAGE_EXPIRATION_TIMEOUT = 8000; // in milliseconds (as initial RTT)
|
||
|
const unsigned int I2NP_MESSAGE_CLOCK_SKEW = 60*1000; // 1 minute in milliseconds
|
||
|
|
||
11 years ago
|
struct I2NPMessage
|
||
|
{
|
||
10 years ago
|
uint8_t * buf;
|
||
10 years ago
|
size_t len, offset, maxLen;
|
||
10 years ago
|
std::shared_ptr<i2p::tunnel::InboundTunnel> from;
|
||
11 years ago
|
|
||
10 years ago
|
I2NPMessage (): buf (nullptr),len (I2NP_HEADER_SIZE + 2),
|
||
10 years ago
|
offset(2), maxLen (0), from (nullptr) {}; // reserve 2 bytes for NTCP header
|
||
|
|
||
10 years ago
|
// header accessors
|
||
10 years ago
|
uint8_t * GetHeader () { return GetBuffer (); };
|
||
|
const uint8_t * GetHeader () const { return GetBuffer (); };
|
||
|
void SetTypeID (uint8_t typeID) { GetHeader ()[I2NP_HEADER_TYPEID_OFFSET] = typeID; };
|
||
|
uint8_t GetTypeID () const { return GetHeader ()[I2NP_HEADER_TYPEID_OFFSET]; };
|
||
|
void SetMsgID (uint32_t msgID) { htobe32buf (GetHeader () + I2NP_HEADER_MSGID_OFFSET, msgID); };
|
||
|
uint32_t GetMsgID () const { return bufbe32toh (GetHeader () + I2NP_HEADER_MSGID_OFFSET); };
|
||
|
void SetExpiration (uint64_t expiration) { htobe64buf (GetHeader () + I2NP_HEADER_EXPIRATION_OFFSET, expiration); };
|
||
|
uint64_t GetExpiration () const { return bufbe64toh (GetHeader () + I2NP_HEADER_EXPIRATION_OFFSET); };
|
||
|
void SetSize (uint16_t size) { htobe16buf (GetHeader () + I2NP_HEADER_SIZE_OFFSET, size); };
|
||
|
uint16_t GetSize () const { return bufbe16toh (GetHeader () + I2NP_HEADER_SIZE_OFFSET); };
|
||
10 years ago
|
void UpdateSize () { SetSize (GetPayloadLength ()); };
|
||
10 years ago
|
void SetChks (uint8_t chks) { GetHeader ()[I2NP_HEADER_CHKS_OFFSET] = chks; };
|
||
10 years ago
|
void UpdateChks ()
|
||
|
{
|
||
|
uint8_t hash[32];
|
||
9 years ago
|
SHA256(GetPayload (), GetPayloadLength (), hash);
|
||
10 years ago
|
GetHeader ()[I2NP_HEADER_CHKS_OFFSET] = hash[0];
|
||
10 years ago
|
}
|
||
|
|
||
|
// payload
|
||
10 years ago
|
uint8_t * GetPayload () { return GetBuffer () + I2NP_HEADER_SIZE; };
|
||
10 years ago
|
const uint8_t * GetPayload () const { return GetBuffer () + I2NP_HEADER_SIZE; };
|
||
11 years ago
|
uint8_t * GetBuffer () { return buf + offset; };
|
||
11 years ago
|
const uint8_t * GetBuffer () const { return buf + offset; };
|
||
10 years ago
|
size_t GetLength () const { return len - offset; };
|
||
10 years ago
|
size_t GetPayloadLength () const { return GetLength () - I2NP_HEADER_SIZE; };
|
||
10 years ago
|
|
||
10 years ago
|
void Align (size_t alignment)
|
||
|
{
|
||
10 years ago
|
if (len + alignment > maxLen) return;
|
||
10 years ago
|
size_t rem = ((size_t)GetBuffer ()) % alignment;
|
||
|
if (rem)
|
||
10 years ago
|
{
|
||
10 years ago
|
offset += (alignment - rem);
|
||
10 years ago
|
len += (alignment - rem);
|
||
|
}
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
size_t Concat (const uint8_t * buf1, size_t len1)
|
||
|
{
|
||
|
// make sure with don't write beyond maxLen
|
||
|
if (len + len1 > maxLen) len1 = maxLen - len;
|
||
|
memcpy (buf + len, buf1, len1);
|
||
|
len += len1;
|
||
|
return len1;
|
||
|
}
|
||
|
|
||
11 years ago
|
I2NPMessage& operator=(const I2NPMessage& other)
|
||
|
{
|
||
|
memcpy (buf + offset, other.buf + other.offset, other.GetLength ());
|
||
|
len = offset + other.GetLength ();
|
||
11 years ago
|
from = other.from;
|
||
11 years ago
|
return *this;
|
||
|
}
|
||
9 years ago
|
|
||
11 years ago
|
// for SSU only
|
||
10 years ago
|
uint8_t * GetSSUHeader () { return buf + offset + I2NP_HEADER_SIZE - I2NP_SHORT_HEADER_SIZE; };
|
||
11 years ago
|
void FromSSU (uint32_t msgID) // we have received SSU message and convert it to regular
|
||
|
{
|
||
10 years ago
|
const uint8_t * ssu = GetSSUHeader ();
|
||
10 years ago
|
GetHeader ()[I2NP_HEADER_TYPEID_OFFSET] = ssu[I2NP_SHORT_HEADER_TYPEID_OFFSET]; // typeid
|
||
10 years ago
|
SetMsgID (msgID);
|
||
|
SetExpiration (bufbe32toh (ssu + I2NP_SHORT_HEADER_EXPIRATION_OFFSET)*1000LL);
|
||
|
SetSize (len - offset - I2NP_HEADER_SIZE);
|
||
|
SetChks (0);
|
||
11 years ago
|
}
|
||
11 years ago
|
uint32_t ToSSU () // return msgID
|
||
|
{
|
||
10 years ago
|
uint8_t header[I2NP_HEADER_SIZE];
|
||
10 years ago
|
memcpy (header, GetHeader (), I2NP_HEADER_SIZE);
|
||
10 years ago
|
uint8_t * ssu = GetSSUHeader ();
|
||
|
ssu[I2NP_SHORT_HEADER_TYPEID_OFFSET] = header[I2NP_HEADER_TYPEID_OFFSET]; // typeid
|
||
|
htobe32buf (ssu + I2NP_SHORT_HEADER_EXPIRATION_OFFSET, bufbe64toh (header + I2NP_HEADER_EXPIRATION_OFFSET)/1000LL);
|
||
|
len = offset + I2NP_SHORT_HEADER_SIZE + bufbe16toh (header + I2NP_HEADER_SIZE_OFFSET);
|
||
|
return bufbe32toh (header + I2NP_HEADER_MSGID_OFFSET);
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
void FillI2NPMessageHeader (I2NPMessageType msgType, uint32_t replyMsgID = 0);
|
||
|
void RenewI2NPMessageHeader ();
|
||
9 years ago
|
bool IsExpired () const;
|
||
11 years ago
|
};
|
||
10 years ago
|
|
||
|
template<int sz>
|
||
|
struct I2NPMessageBuffer: public I2NPMessage
|
||
|
{
|
||
10 years ago
|
I2NPMessageBuffer () { buf = m_Buffer; maxLen = sz; };
|
||
9 years ago
|
uint8_t m_Buffer[sz + 32]; // 16 alignment + 16 padding
|
||
10 years ago
|
};
|
||
|
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> NewI2NPMessage ();
|
||
|
std::shared_ptr<I2NPMessage> NewI2NPShortMessage ();
|
||
8 years ago
|
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage ();
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> NewI2NPMessage (size_t len);
|
||
10 years ago
|
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, size_t len, uint32_t replyMsgID = 0);
|
||
|
std::shared_ptr<I2NPMessage> CreateI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from = nullptr);
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CopyI2NPMessage (std::shared_ptr<I2NPMessage> msg);
|
||
|
|
||
10 years ago
|
std::shared_ptr<I2NPMessage> CreateDeliveryStatusMsg (uint32_t msgID);
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateRouterInfoDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
|
||
10 years ago
|
uint32_t replyTunnelID, bool exploratory = false, std::set<i2p::data::IdentHash> * excludedPeers = nullptr);
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest,
|
||
10 years ago
|
const std::set<i2p::data::IdentHash>& excludedFloodfills,
|
||
9 years ago
|
std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel, const uint8_t * replyKey, const uint8_t * replyTag);
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateDatabaseSearchReply (const i2p::data::IdentHash& ident, std::vector<i2p::data::IdentHash> routers);
|
||
11 years ago
|
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::RouterInfo> router = nullptr, uint32_t replyToken = 0);
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::LeaseSet> leaseSet); // for floodfill only
|
||
|
std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::LocalLeaseSet> leaseSet, uint32_t replyToken = 0, std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel = nullptr);
|
||
9 years ago
|
bool IsRouterInfoMsg (std::shared_ptr<I2NPMessage> msg);
|
||
|
|
||
10 years ago
|
bool HandleBuildRequestRecords (int num, uint8_t * records, uint8_t * clearText);
|
||
11 years ago
|
void HandleVariableTunnelBuildMsg (uint32_t replyMsgID, uint8_t * buf, size_t len);
|
||
|
void HandleVariableTunnelBuildReplyMsg (uint32_t replyMsgID, uint8_t * buf, size_t len);
|
||
11 years ago
|
void HandleTunnelBuildMsg (uint8_t * buf, size_t len);
|
||
|
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (const uint8_t * buf);
|
||
|
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload);
|
||
10 years ago
|
std::shared_ptr<I2NPMessage> CreateEmptyTunnelDataMsg ();
|
||
11 years ago
|
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len);
|
||
|
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType,
|
||
11 years ago
|
const uint8_t * buf, size_t len, uint32_t replyMsgID = 0);
|
||
10 years ago
|
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, std::shared_ptr<I2NPMessage> msg);
|
||
11 years ago
|
|
||
10 years ago
|
size_t GetI2NPMessageLength (const uint8_t * msg);
|
||
11 years ago
|
void HandleI2NPMessage (uint8_t * msg, size_t len);
|
||
10 years ago
|
void HandleI2NPMessage (std::shared_ptr<I2NPMessage> msg);
|
||
10 years ago
|
|
||
|
class I2NPMessagesHandler
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
~I2NPMessagesHandler ();
|
||
10 years ago
|
void PutNextMessage (std::shared_ptr<I2NPMessage> msg);
|
||
10 years ago
|
void Flush ();
|
||
|
|
||
|
private:
|
||
|
|
||
10 years ago
|
std::vector<std::shared_ptr<I2NPMessage> > m_TunnelMsgs, m_TunnelGatewayMsgs;
|
||
10 years ago
|
};
|
||
9 years ago
|
|
||
|
const uint16_t DEFAULT_MAX_NUM_TRANSIT_TUNNELS = 2500;
|
||
|
void SetMaxNumTransitTunnels (uint16_t maxNumTransitTunnels);
|
||
11 years ago
|
}
|
||
|
|
||
|
#endif
|