I2NPProtocol.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef I2NP_PROTOCOL_H__
  2. #define I2NP_PROTOCOL_H__
  3. #include <inttypes.h>
  4. #include <string.h>
  5. #include <set>
  6. #include <memory>
  7. #include "Crypto.h"
  8. #include "I2PEndian.h"
  9. #include "Identity.h"
  10. #include "RouterInfo.h"
  11. #include "LeaseSet.h"
  12. namespace i2p
  13. {
  14. // I2NP header
  15. const size_t I2NP_HEADER_TYPEID_OFFSET = 0;
  16. const size_t I2NP_HEADER_MSGID_OFFSET = I2NP_HEADER_TYPEID_OFFSET + 1;
  17. const size_t I2NP_HEADER_EXPIRATION_OFFSET = I2NP_HEADER_MSGID_OFFSET + 4;
  18. const size_t I2NP_HEADER_SIZE_OFFSET = I2NP_HEADER_EXPIRATION_OFFSET + 8;
  19. const size_t I2NP_HEADER_CHKS_OFFSET = I2NP_HEADER_SIZE_OFFSET + 2;
  20. const size_t I2NP_HEADER_SIZE = I2NP_HEADER_CHKS_OFFSET + 1;
  21. // I2NP short header
  22. const size_t I2NP_SHORT_HEADER_TYPEID_OFFSET = 0;
  23. const size_t I2NP_SHORT_HEADER_EXPIRATION_OFFSET = I2NP_SHORT_HEADER_TYPEID_OFFSET + 1;
  24. const size_t I2NP_SHORT_HEADER_SIZE = I2NP_SHORT_HEADER_EXPIRATION_OFFSET + 4;
  25. // I2NP NTCP2 header
  26. const size_t I2NP_NTCP2_HEADER_SIZE = I2NP_HEADER_EXPIRATION_OFFSET + 4;
  27. // Tunnel Gateway header
  28. const size_t TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET = 0;
  29. const size_t TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET = TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET + 4;
  30. const size_t TUNNEL_GATEWAY_HEADER_SIZE = TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET + 2;
  31. // DeliveryStatus
  32. const size_t DELIVERY_STATUS_MSGID_OFFSET = 0;
  33. const size_t DELIVERY_STATUS_TIMESTAMP_OFFSET = DELIVERY_STATUS_MSGID_OFFSET + 4;
  34. const size_t DELIVERY_STATUS_SIZE = DELIVERY_STATUS_TIMESTAMP_OFFSET + 8;
  35. // DatabaseStore
  36. const size_t DATABASE_STORE_KEY_OFFSET = 0;
  37. const size_t DATABASE_STORE_TYPE_OFFSET = DATABASE_STORE_KEY_OFFSET + 32;
  38. const size_t DATABASE_STORE_REPLY_TOKEN_OFFSET = DATABASE_STORE_TYPE_OFFSET + 1;
  39. const size_t DATABASE_STORE_HEADER_SIZE = DATABASE_STORE_REPLY_TOKEN_OFFSET + 4;
  40. // TunnelBuild
  41. const size_t TUNNEL_BUILD_RECORD_SIZE = 528;
  42. //BuildRequestRecordClearText
  43. const size_t BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET = 0;
  44. const size_t BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET = BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET + 4;
  45. const size_t BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET = BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET + 32;
  46. const size_t BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET = BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET + 4;
  47. const size_t BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET = BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET + 32;
  48. const size_t BUILD_REQUEST_RECORD_IV_KEY_OFFSET = BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET + 32;
  49. const size_t BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET = BUILD_REQUEST_RECORD_IV_KEY_OFFSET + 32;
  50. const size_t BUILD_REQUEST_RECORD_REPLY_IV_OFFSET = BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET + 32;
  51. const size_t BUILD_REQUEST_RECORD_FLAG_OFFSET = BUILD_REQUEST_RECORD_REPLY_IV_OFFSET + 16;
  52. const size_t BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET = BUILD_REQUEST_RECORD_FLAG_OFFSET + 1;
  53. const size_t BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET = BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET + 4;
  54. const size_t BUILD_REQUEST_RECORD_PADDING_OFFSET = BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET + 4;
  55. const size_t BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE = 222;
  56. // BuildRequestRecordEncrypted
  57. const size_t BUILD_REQUEST_RECORD_TO_PEER_OFFSET = 0;
  58. const size_t BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET = BUILD_REQUEST_RECORD_TO_PEER_OFFSET + 16;
  59. // BuildResponseRecord
  60. const size_t BUILD_RESPONSE_RECORD_HASH_OFFSET = 0;
  61. const size_t BUILD_RESPONSE_RECORD_PADDING_OFFSET = 32;
  62. const size_t BUILD_RESPONSE_RECORD_PADDING_SIZE = 495;
  63. const size_t BUILD_RESPONSE_RECORD_RET_OFFSET = BUILD_RESPONSE_RECORD_PADDING_OFFSET + BUILD_RESPONSE_RECORD_PADDING_SIZE;
  64. enum I2NPMessageType
  65. {
  66. eI2NPDummyMsg = 0,
  67. eI2NPDatabaseStore = 1,
  68. eI2NPDatabaseLookup = 2,
  69. eI2NPDatabaseSearchReply = 3,
  70. eI2NPDeliveryStatus = 10,
  71. eI2NPGarlic = 11,
  72. eI2NPTunnelData = 18,
  73. eI2NPTunnelGateway = 19,
  74. eI2NPData = 20,
  75. eI2NPTunnelBuild = 21,
  76. eI2NPTunnelBuildReply = 22,
  77. eI2NPVariableTunnelBuild = 23,
  78. eI2NPVariableTunnelBuildReply = 24
  79. };
  80. const int NUM_TUNNEL_BUILD_RECORDS = 8;
  81. // DatabaseLookup flags
  82. const uint8_t DATABASE_LOOKUP_DELIVERY_FLAG = 0x01;
  83. const uint8_t DATABASE_LOOKUP_ENCRYPTION_FLAG = 0x02;
  84. const uint8_t DATABASE_LOOKUP_TYPE_FLAGS_MASK = 0x0C;
  85. const uint8_t DATABASE_LOOKUP_TYPE_NORMAL_LOOKUP = 0;
  86. const uint8_t DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP = 0x04; // 0100
  87. const uint8_t DATABASE_LOOKUP_TYPE_ROUTERINFO_LOOKUP = 0x08; // 1000
  88. const uint8_t DATABASE_LOOKUP_TYPE_EXPLORATORY_LOOKUP = 0x0C; // 1100
  89. namespace tunnel
  90. {
  91. class InboundTunnel;
  92. class TunnelPool;
  93. }
  94. const size_t I2NP_MAX_MESSAGE_SIZE = 62708;
  95. const size_t I2NP_MAX_SHORT_MESSAGE_SIZE = 4096;
  96. const unsigned int I2NP_MESSAGE_EXPIRATION_TIMEOUT = 8000; // in milliseconds (as initial RTT)
  97. const unsigned int I2NP_MESSAGE_CLOCK_SKEW = 60*1000; // 1 minute in milliseconds
  98. struct I2NPMessage
  99. {
  100. uint8_t * buf;
  101. size_t len, offset, maxLen;
  102. std::shared_ptr<i2p::tunnel::InboundTunnel> from;
  103. I2NPMessage (): buf (nullptr),len (I2NP_HEADER_SIZE + 2),
  104. offset(2), maxLen (0), from (nullptr) {}; // reserve 2 bytes for NTCP header
  105. // header accessors
  106. uint8_t * GetHeader () { return GetBuffer (); };
  107. const uint8_t * GetHeader () const { return GetBuffer (); };
  108. void SetTypeID (uint8_t typeID) { GetHeader ()[I2NP_HEADER_TYPEID_OFFSET] = typeID; };
  109. uint8_t GetTypeID () const { return GetHeader ()[I2NP_HEADER_TYPEID_OFFSET]; };
  110. void SetMsgID (uint32_t msgID) { htobe32buf (GetHeader () + I2NP_HEADER_MSGID_OFFSET, msgID); };
  111. uint32_t GetMsgID () const { return bufbe32toh (GetHeader () + I2NP_HEADER_MSGID_OFFSET); };
  112. void SetExpiration (uint64_t expiration) { htobe64buf (GetHeader () + I2NP_HEADER_EXPIRATION_OFFSET, expiration); };
  113. uint64_t GetExpiration () const { return bufbe64toh (GetHeader () + I2NP_HEADER_EXPIRATION_OFFSET); };
  114. void SetSize (uint16_t size) { htobe16buf (GetHeader () + I2NP_HEADER_SIZE_OFFSET, size); };
  115. uint16_t GetSize () const { return bufbe16toh (GetHeader () + I2NP_HEADER_SIZE_OFFSET); };
  116. void UpdateSize () { SetSize (GetPayloadLength ()); };
  117. void SetChks (uint8_t chks) { GetHeader ()[I2NP_HEADER_CHKS_OFFSET] = chks; };
  118. void UpdateChks ()
  119. {
  120. uint8_t hash[32];
  121. SHA256(GetPayload (), GetPayloadLength (), hash);
  122. GetHeader ()[I2NP_HEADER_CHKS_OFFSET] = hash[0];
  123. }
  124. // payload
  125. uint8_t * GetPayload () { return GetBuffer () + I2NP_HEADER_SIZE; };
  126. const uint8_t * GetPayload () const { return GetBuffer () + I2NP_HEADER_SIZE; };
  127. uint8_t * GetBuffer () { return buf + offset; };
  128. const uint8_t * GetBuffer () const { return buf + offset; };
  129. size_t GetLength () const { return len - offset; };
  130. size_t GetPayloadLength () const { return GetLength () - I2NP_HEADER_SIZE; };
  131. void Align (size_t alignment)
  132. {
  133. if (len + alignment > maxLen) return;
  134. size_t rem = ((size_t)GetBuffer ()) % alignment;
  135. if (rem)
  136. {
  137. offset += (alignment - rem);
  138. len += (alignment - rem);
  139. }
  140. }
  141. size_t Concat (const uint8_t * buf1, size_t len1)
  142. {
  143. // make sure with don't write beyond maxLen
  144. if (len + len1 > maxLen) len1 = maxLen - len;
  145. memcpy (buf + len, buf1, len1);
  146. len += len1;
  147. return len1;
  148. }
  149. I2NPMessage& operator=(const I2NPMessage& other)
  150. {
  151. memcpy (buf + offset, other.buf + other.offset, other.GetLength ());
  152. len = offset + other.GetLength ();
  153. from = other.from;
  154. return *this;
  155. }
  156. // for SSU only
  157. uint8_t * GetSSUHeader () { return buf + offset + I2NP_HEADER_SIZE - I2NP_SHORT_HEADER_SIZE; };
  158. void FromSSU (uint32_t msgID) // we have received SSU message and convert it to regular
  159. {
  160. const uint8_t * ssu = GetSSUHeader ();
  161. GetHeader ()[I2NP_HEADER_TYPEID_OFFSET] = ssu[I2NP_SHORT_HEADER_TYPEID_OFFSET]; // typeid
  162. SetMsgID (msgID);
  163. SetExpiration (bufbe32toh (ssu + I2NP_SHORT_HEADER_EXPIRATION_OFFSET)*1000LL);
  164. SetSize (len - offset - I2NP_HEADER_SIZE);
  165. SetChks (0);
  166. }
  167. uint32_t ToSSU () // return msgID
  168. {
  169. uint8_t header[I2NP_HEADER_SIZE];
  170. memcpy (header, GetHeader (), I2NP_HEADER_SIZE);
  171. uint8_t * ssu = GetSSUHeader ();
  172. ssu[I2NP_SHORT_HEADER_TYPEID_OFFSET] = header[I2NP_HEADER_TYPEID_OFFSET]; // typeid
  173. htobe32buf (ssu + I2NP_SHORT_HEADER_EXPIRATION_OFFSET, bufbe64toh (header + I2NP_HEADER_EXPIRATION_OFFSET)/1000LL);
  174. len = offset + I2NP_SHORT_HEADER_SIZE + bufbe16toh (header + I2NP_HEADER_SIZE_OFFSET);
  175. return bufbe32toh (header + I2NP_HEADER_MSGID_OFFSET);
  176. }
  177. // for NTCP2 only
  178. uint8_t * GetNTCP2Header () { return GetPayload () - I2NP_NTCP2_HEADER_SIZE; };
  179. size_t GetNTCP2Length () const { return GetPayloadLength () + I2NP_NTCP2_HEADER_SIZE; };
  180. void FromNTCP2 ()
  181. {
  182. const uint8_t * ntcp2 = GetNTCP2Header ();
  183. memcpy (GetHeader () + I2NP_HEADER_TYPEID_OFFSET, ntcp2 + I2NP_HEADER_TYPEID_OFFSET, 5); // typeid + msgid
  184. SetExpiration (bufbe32toh (ntcp2 + I2NP_HEADER_EXPIRATION_OFFSET)*1000LL);
  185. SetSize (len - offset - I2NP_HEADER_SIZE);
  186. SetChks (0);
  187. }
  188. void ToNTCP2 ()
  189. {
  190. uint8_t * ntcp2 = GetNTCP2Header ();
  191. htobe32buf (ntcp2 + I2NP_HEADER_EXPIRATION_OFFSET, bufbe64toh (GetHeader () + I2NP_HEADER_EXPIRATION_OFFSET)/1000LL);
  192. memcpy (ntcp2 + I2NP_HEADER_TYPEID_OFFSET, GetHeader () + I2NP_HEADER_TYPEID_OFFSET, 5); // typeid + msgid
  193. }
  194. void FillI2NPMessageHeader (I2NPMessageType msgType, uint32_t replyMsgID = 0);
  195. void RenewI2NPMessageHeader ();
  196. bool IsExpired () const;
  197. };
  198. template<int sz>
  199. struct I2NPMessageBuffer: public I2NPMessage
  200. {
  201. I2NPMessageBuffer () { buf = m_Buffer; maxLen = sz; };
  202. uint8_t m_Buffer[sz + 32]; // 16 alignment + 16 padding
  203. };
  204. std::shared_ptr<I2NPMessage> NewI2NPMessage ();
  205. std::shared_ptr<I2NPMessage> NewI2NPShortMessage ();
  206. std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage ();
  207. std::shared_ptr<I2NPMessage> NewI2NPMessage (size_t len);
  208. std::shared_ptr<I2NPMessage> CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, size_t len, uint32_t replyMsgID = 0);
  209. std::shared_ptr<I2NPMessage> CreateI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from = nullptr);
  210. std::shared_ptr<I2NPMessage> CopyI2NPMessage (std::shared_ptr<I2NPMessage> msg);
  211. std::shared_ptr<I2NPMessage> CreateDeliveryStatusMsg (uint32_t msgID);
  212. std::shared_ptr<I2NPMessage> CreateRouterInfoDatabaseLookupMsg (const uint8_t * key, const uint8_t * from,
  213. uint32_t replyTunnelID, bool exploratory = false, std::set<i2p::data::IdentHash> * excludedPeers = nullptr);
  214. std::shared_ptr<I2NPMessage> CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest,
  215. const std::set<i2p::data::IdentHash>& excludedFloodfills,
  216. std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel, const uint8_t * replyKey, const uint8_t * replyTag);
  217. std::shared_ptr<I2NPMessage> CreateDatabaseSearchReply (const i2p::data::IdentHash& ident, std::vector<i2p::data::IdentHash> routers);
  218. std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (std::shared_ptr<const i2p::data::RouterInfo> router = nullptr, uint32_t replyToken = 0);
  219. std::shared_ptr<I2NPMessage> CreateDatabaseStoreMsg (const i2p::data::IdentHash& storeHash, std::shared_ptr<const i2p::data::LeaseSet> leaseSet); // for floodfill only
  220. 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);
  221. bool IsRouterInfoMsg (std::shared_ptr<I2NPMessage> msg);
  222. bool HandleBuildRequestRecords (int num, uint8_t * records, uint8_t * clearText);
  223. void HandleVariableTunnelBuildMsg (uint32_t replyMsgID, uint8_t * buf, size_t len);
  224. void HandleVariableTunnelBuildReplyMsg (uint32_t replyMsgID, uint8_t * buf, size_t len);
  225. void HandleTunnelBuildMsg (uint8_t * buf, size_t len);
  226. std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (const uint8_t * buf);
  227. std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload);
  228. std::shared_ptr<I2NPMessage> CreateEmptyTunnelDataMsg ();
  229. std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len);
  230. std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType,
  231. const uint8_t * buf, size_t len, uint32_t replyMsgID = 0);
  232. std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, std::shared_ptr<I2NPMessage> msg);
  233. size_t GetI2NPMessageLength (const uint8_t * msg, size_t len);
  234. void HandleI2NPMessage (uint8_t * msg, size_t len);
  235. void HandleI2NPMessage (std::shared_ptr<I2NPMessage> msg);
  236. class I2NPMessagesHandler
  237. {
  238. public:
  239. ~I2NPMessagesHandler ();
  240. void PutNextMessage (std::shared_ptr<I2NPMessage> msg);
  241. void Flush ();
  242. private:
  243. std::vector<std::shared_ptr<I2NPMessage> > m_TunnelMsgs, m_TunnelGatewayMsgs;
  244. };
  245. const uint16_t DEFAULT_MAX_NUM_TRANSIT_TUNNELS = 2500;
  246. void SetMaxNumTransitTunnels (uint16_t maxNumTransitTunnels);
  247. }
  248. #endif