i2pd/I2NPProtocol.h

222 lines
9.6 KiB
C
Raw Normal View History

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>
2015-01-01 21:51:15 +00:00
#include <cryptopp/sha.h>
2013-11-10 23:23:26 +00:00
#include <string.h>
2014-02-07 16:26:00 +00:00
#include "I2PEndian.h"
2014-12-30 17:25:08 +00:00
#include "Identity.h"
2013-10-27 15:20:29 +00:00
#include "RouterInfo.h"
2014-07-29 18:31:55 +00:00
#include "LeaseSet.h"
2013-10-27 15:20:29 +00:00
namespace i2p
2015-01-01 21:51:15 +00:00
{
// 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;
2015-01-01 23:53:44 +00:00
const size_t I2NP_HEADER_SIZE = I2NP_HEADER_CHKS_OFFSET + 1;
2015-01-02 04:00:33 +00:00
// 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;
2015-01-01 23:53:44 +00:00
// 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;
// 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;
2015-01-03 02:11:40 +00:00
// 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;
// TunnelBuild
const size_t TUNNEL_BUILD_RECORD_SIZE = 528;
//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;
2015-01-03 02:11:40 +00:00
// 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;
// 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;
2013-10-27 15:20:29 +00:00
enum I2NPMessageType
{
eI2NPDatabaseStore = 1,
eI2NPDatabaseLookup = 2,
eI2NPDatabaseSearchReply = 3,
eI2NPDeliveryStatus = 10,
eI2NPGarlic = 11,
eI2NPTunnelData = 18,
eI2NPTunnelGateway = 19,
eI2NPData = 20,
2014-04-30 18:08:57 +00:00
eI2NPTunnelBuild = 21,
eI2NPTunnelBuildReply = 22,
2013-10-27 15:20:29 +00:00
eI2NPVariableTunnelBuild = 23,
eI2NPVariableTunnelBuildReply = 24
};
2014-04-30 18:08:57 +00:00
const int NUM_TUNNEL_BUILD_RECORDS = 8;
namespace tunnel
{
class InboundTunnel;
2014-10-07 00:18:18 +00:00
class TunnelPool;
}
const size_t I2NP_MAX_MESSAGE_SIZE = 32768;
2014-07-30 20:52:35 +00:00
const size_t I2NP_MAX_SHORT_MESSAGE_SIZE = 2400;
2013-10-27 15:20:29 +00:00
struct I2NPMessage
{
2014-07-30 20:52:35 +00:00
uint8_t * buf;
2014-07-30 22:20:42 +00:00
size_t len, offset, maxLen;
i2p::tunnel::InboundTunnel * from;
2013-11-10 23:23:26 +00:00
2015-01-02 04:00:33 +00:00
I2NPMessage (): buf (nullptr),len (I2NP_HEADER_SIZE + 2),
offset(2), maxLen (0), from (nullptr) {}; // reserve 2 bytes for NTCP header
2015-01-01 21:51:15 +00:00
// header accessors
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); };
2015-01-02 04:00:33 +00:00
void UpdateSize () { SetSize (GetPayloadLength ()); };
void SetChks (uint8_t chks) { GetHeader ()[I2NP_HEADER_CHKS_OFFSET] = chks; };
2015-01-01 21:51:15 +00:00
void UpdateChks ()
{
uint8_t hash[32];
CryptoPP::SHA256().CalculateDigest(hash, GetPayload (), GetPayloadLength ());
GetHeader ()[I2NP_HEADER_CHKS_OFFSET] = hash[0];
2015-01-01 21:51:15 +00:00
}
// payload
2015-01-02 04:00:33 +00:00
uint8_t * GetPayload () { return GetBuffer () + I2NP_HEADER_SIZE; };
2013-10-27 15:20:29 +00:00
uint8_t * GetBuffer () { return buf + offset; };
2014-03-20 13:47:02 +00:00
const uint8_t * GetBuffer () const { return buf + offset; };
2015-01-01 21:51:15 +00:00
size_t GetLength () const { return len - offset; };
2015-01-02 04:00:33 +00:00
size_t GetPayloadLength () const { return GetLength () - I2NP_HEADER_SIZE; };
2015-01-01 21:51:15 +00:00
2014-11-26 16:04:49 +00:00
void Align (size_t alignment)
{
size_t rem = ((size_t)GetBuffer ()) % alignment;
if (rem)
2014-11-26 16:54:35 +00:00
{
2014-11-26 16:04:49 +00:00
offset += (alignment - rem);
2014-11-26 16:54:35 +00:00
len += (alignment - rem);
}
2014-11-26 16:04:49 +00:00
}
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 ();
from = other.from;
2013-11-10 23:23:26 +00:00
return *this;
}
2014-02-07 16:26:00 +00:00
// for SSU only
2015-01-02 04:00:33 +00:00
uint8_t * GetSSUHeader () { return buf + offset + I2NP_HEADER_SIZE - I2NP_SHORT_HEADER_SIZE; };
2014-02-07 16:26:00 +00:00
void FromSSU (uint32_t msgID) // we have received SSU message and convert it to regular
{
2015-01-02 04:00:33 +00:00
const uint8_t * ssu = GetSSUHeader ();
GetHeader ()[I2NP_HEADER_TYPEID_OFFSET] = ssu[I2NP_SHORT_HEADER_TYPEID_OFFSET]; // typeid
2015-01-02 04:00:33 +00:00
SetMsgID (msgID);
SetExpiration (bufbe32toh (ssu + I2NP_SHORT_HEADER_EXPIRATION_OFFSET)*1000LL);
SetSize (len - offset - I2NP_HEADER_SIZE);
SetChks (0);
2014-02-07 16:26:00 +00:00
}
2014-02-09 23:28:34 +00:00
uint32_t ToSSU () // return msgID
{
2015-01-02 04:00:33 +00:00
uint8_t header[I2NP_HEADER_SIZE];
memcpy (header, GetHeader (), I2NP_HEADER_SIZE);
2015-01-02 04:00:33 +00:00
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);
2014-02-09 23:28:34 +00:00
}
2013-10-27 15:20:29 +00:00
};
2014-07-30 20:52:35 +00:00
template<int sz>
struct I2NPMessageBuffer: public I2NPMessage
{
2014-07-30 22:20:42 +00:00
I2NPMessageBuffer () { buf = m_Buffer; maxLen = sz; };
2014-07-30 20:52:35 +00:00
uint8_t m_Buffer[sz];
};
2013-10-27 15:20:29 +00:00
I2NPMessage * NewI2NPMessage ();
2014-07-30 20:52:35 +00:00
I2NPMessage * NewI2NPShortMessage ();
2014-07-30 22:20:42 +00:00
I2NPMessage * NewI2NPMessage (size_t len);
2013-10-27 15:20:29 +00:00
void DeleteI2NPMessage (I2NPMessage * msg);
void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID = 0);
void RenewI2NPMessageHeader (I2NPMessage * msg);
2013-10-27 15:20:29 +00:00
I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID = 0);
I2NPMessage * CreateI2NPMessage (const uint8_t * buf, int len, i2p::tunnel::InboundTunnel * from = nullptr);
2013-10-27 15:20:29 +00:00
2014-01-09 03:47:22 +00:00
I2NPMessage * CreateDeliveryStatusMsg (uint32_t msgID);
2015-01-04 22:25:16 +00:00
I2NPMessage * CreateRouterInfoDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
uint32_t replyTunnelID, bool exploratory = false, std::set<i2p::data::IdentHash> * excludedPeers = nullptr);
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);
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
I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::RouterInfo * router = nullptr);
2014-08-20 15:12:53 +00:00
I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::LeaseSet * leaseSet, uint32_t replyToken = 0);
2015-01-04 14:33:19 +00:00
bool HandleBuildRequestRecords (int num, uint8_t * records, uint8_t * clearText);
2013-10-27 15:20:29 +00:00
void HandleVariableTunnelBuildMsg (uint32_t replyMsgID, uint8_t * buf, size_t len);
void HandleVariableTunnelBuildReplyMsg (uint32_t replyMsgID, uint8_t * buf, size_t len);
2014-04-30 18:08:57 +00:00
void HandleTunnelBuildMsg (uint8_t * buf, size_t len);
2013-10-27 15:20:29 +00:00
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
2014-10-12 01:27:55 +00:00
size_t GetI2NPMessageLength (const uint8_t * msg);
void HandleI2NPMessage (uint8_t * msg, size_t len);
void HandleI2NPMessage (I2NPMessage * msg);
2013-10-27 15:20:29 +00:00
}
#endif