2016-05-30 16:56:42 +00:00
|
|
|
/*
|
2019-01-29 16:30:31 +00:00
|
|
|
* Copyright (c) 2013-2019, The PurpleI2P Project
|
2016-05-30 16:56:42 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2016-05-12 19:37:46 +00:00
|
|
|
#ifndef I2CP_H__
|
|
|
|
#define I2CP_H__
|
|
|
|
|
2016-05-12 20:17:10 +00:00
|
|
|
#include <inttypes.h>
|
2016-05-12 19:37:46 +00:00
|
|
|
#include <string>
|
2016-05-12 20:17:10 +00:00
|
|
|
#include <memory>
|
2016-05-31 15:54:45 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <map>
|
2016-05-12 19:37:46 +00:00
|
|
|
#include <boost/asio.hpp>
|
2016-05-27 17:46:28 +00:00
|
|
|
#include "Destination.h"
|
2016-05-12 19:37:46 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
2016-06-02 17:26:41 +00:00
|
|
|
const uint8_t I2CP_PROTOCOL_BYTE = 0x2A;
|
2016-05-13 19:13:36 +00:00
|
|
|
const size_t I2CP_SESSION_BUFFER_SIZE = 4096;
|
2016-05-12 20:17:10 +00:00
|
|
|
|
2016-05-13 19:13:36 +00:00
|
|
|
const size_t I2CP_HEADER_LENGTH_OFFSET = 0;
|
|
|
|
const size_t I2CP_HEADER_TYPE_OFFSET = I2CP_HEADER_LENGTH_OFFSET + 4;
|
2018-01-06 03:48:51 +00:00
|
|
|
const size_t I2CP_HEADER_SIZE = I2CP_HEADER_TYPE_OFFSET + 1;
|
2016-05-13 19:13:36 +00:00
|
|
|
|
|
|
|
const uint8_t I2CP_GET_DATE_MESSAGE = 32;
|
2016-05-27 20:22:42 +00:00
|
|
|
const uint8_t I2CP_SET_DATE_MESSAGE = 33;
|
|
|
|
const uint8_t I2CP_CREATE_SESSION_MESSAGE = 1;
|
2018-01-06 03:48:51 +00:00
|
|
|
const uint8_t I2CP_RECONFIGURE_SESSION_MESSAGE = 2;
|
|
|
|
const uint8_t I2CP_SESSION_STATUS_MESSAGE = 20;
|
2016-06-01 14:05:40 +00:00
|
|
|
const uint8_t I2CP_DESTROY_SESSION_MESSAGE = 3;
|
2016-05-29 20:35:57 +00:00
|
|
|
const uint8_t I2CP_REQUEST_VARIABLE_LEASESET_MESSAGE = 37;
|
2018-01-06 03:48:51 +00:00
|
|
|
const uint8_t I2CP_CREATE_LEASESET_MESSAGE = 4;
|
2019-04-11 18:06:53 +00:00
|
|
|
const uint8_t I2CP_CREATE_LEASESET2_MESSAGE = 41;
|
2016-06-01 15:11:18 +00:00
|
|
|
const uint8_t I2CP_SEND_MESSAGE_MESSAGE = 5;
|
2018-01-06 03:48:51 +00:00
|
|
|
const uint8_t I2CP_SEND_MESSAGE_EXPIRES_MESSAGE = 36;
|
2016-06-01 18:38:13 +00:00
|
|
|
const uint8_t I2CP_MESSAGE_PAYLOAD_MESSAGE = 31;
|
2018-01-06 03:48:51 +00:00
|
|
|
const uint8_t I2CP_MESSAGE_STATUS_MESSAGE = 22;
|
2016-06-01 15:11:18 +00:00
|
|
|
const uint8_t I2CP_HOST_LOOKUP_MESSAGE = 38;
|
2018-01-06 03:48:51 +00:00
|
|
|
const uint8_t I2CP_HOST_REPLY_MESSAGE = 39;
|
2016-06-03 15:49:39 +00:00
|
|
|
const uint8_t I2CP_DEST_LOOKUP_MESSAGE = 34;
|
2018-01-06 03:48:51 +00:00
|
|
|
const uint8_t I2CP_DEST_REPLY_MESSAGE = 35;
|
|
|
|
const uint8_t I2CP_GET_BANDWIDTH_LIMITS_MESSAGE = 8;
|
2016-06-03 17:48:21 +00:00
|
|
|
const uint8_t I2CP_BANDWIDTH_LIMITS_MESSAGE = 23;
|
2016-05-30 16:56:42 +00:00
|
|
|
|
2016-06-01 19:30:57 +00:00
|
|
|
enum I2CPMessageStatus
|
|
|
|
{
|
|
|
|
eI2CPMessageStatusAccepted = 1,
|
|
|
|
eI2CPMessageStatusGuaranteedSuccess = 4,
|
|
|
|
eI2CPMessageStatusGuaranteedFailure = 5,
|
|
|
|
eI2CPMessageStatusNoLeaseSet = 21
|
|
|
|
};
|
|
|
|
|
2016-06-02 17:26:41 +00:00
|
|
|
// params
|
2018-01-06 03:48:51 +00:00
|
|
|
const char I2CP_PARAM_DONT_PUBLISH_LEASESET[] = "i2cp.dontPublishLeaseSet";
|
|
|
|
const char I2CP_PARAM_MESSAGE_RELIABILITY[] = "i2cp.messageReliability";
|
2016-06-02 17:26:41 +00:00
|
|
|
|
2016-05-27 17:46:28 +00:00
|
|
|
class I2CPSession;
|
|
|
|
class I2CPDestination: public LeaseSetDestination
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-06-08 18:14:19 +00:00
|
|
|
I2CPDestination (std::shared_ptr<I2CPSession> owner, std::shared_ptr<const i2p::data::IdentityEx> identity, bool isPublic, const std::map<std::string, std::string>& params);
|
2016-05-27 17:46:28 +00:00
|
|
|
|
2016-05-30 16:56:42 +00:00
|
|
|
void SetEncryptionPrivateKey (const uint8_t * key);
|
|
|
|
void LeaseSetCreated (const uint8_t * buf, size_t len); // called from I2CPSession
|
2019-01-29 16:30:31 +00:00
|
|
|
void LeaseSet2Created (uint8_t storeType, const uint8_t * buf, size_t len); // called from I2CPSession
|
2016-06-01 19:30:57 +00:00
|
|
|
void SendMsgTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash& ident, uint32_t nonce); // called from I2CPSession
|
2016-05-30 16:56:42 +00:00
|
|
|
|
2016-05-27 17:46:28 +00:00
|
|
|
// implements LocalDestination
|
2017-11-09 01:45:53 +00:00
|
|
|
bool Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const;
|
2016-05-27 17:46:28 +00:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> GetIdentity () const { return m_Identity; };
|
|
|
|
|
2016-06-03 17:01:12 +00:00
|
|
|
protected:
|
|
|
|
|
2016-05-27 17:46:28 +00:00
|
|
|
// I2CP
|
2016-06-01 18:38:13 +00:00
|
|
|
void HandleDataMessage (const uint8_t * buf, size_t len);
|
2016-05-29 20:35:57 +00:00
|
|
|
void CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels);
|
2016-05-27 17:46:28 +00:00
|
|
|
|
2016-06-01 18:38:13 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
std::shared_ptr<I2CPDestination> GetSharedFromThis ()
|
2018-01-06 03:48:51 +00:00
|
|
|
{ return std::static_pointer_cast<I2CPDestination>(shared_from_this ()); }
|
2016-06-01 19:30:57 +00:00
|
|
|
bool SendMsg (std::shared_ptr<I2NPMessage> msg, std::shared_ptr<const i2p::data::LeaseSet> remote);
|
2016-06-01 18:38:13 +00:00
|
|
|
|
2016-05-27 17:46:28 +00:00
|
|
|
private:
|
|
|
|
|
2016-06-08 18:14:19 +00:00
|
|
|
std::shared_ptr<I2CPSession> m_Owner;
|
2016-05-27 17:46:28 +00:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> m_Identity;
|
2016-05-30 16:56:42 +00:00
|
|
|
uint8_t m_EncryptionPrivateKey[256];
|
2017-11-08 18:49:48 +00:00
|
|
|
std::shared_ptr<i2p::crypto::CryptoKeyDecryptor> m_Decryptor;
|
2016-05-30 16:56:42 +00:00
|
|
|
uint64_t m_LeaseSetExpirationTime;
|
2016-05-27 17:46:28 +00:00
|
|
|
};
|
|
|
|
|
2016-05-13 19:13:36 +00:00
|
|
|
class I2CPServer;
|
2016-05-12 20:17:10 +00:00
|
|
|
class I2CPSession: public std::enable_shared_from_this<I2CPSession>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
#ifdef ANDROID
|
2016-06-23 18:01:41 +00:00
|
|
|
typedef boost::asio::local::stream_protocol proto;
|
2016-06-22 19:48:36 +00:00
|
|
|
#else
|
2016-06-23 18:01:41 +00:00
|
|
|
typedef boost::asio::ip::tcp proto;
|
2018-01-06 03:48:51 +00:00
|
|
|
#endif
|
2016-06-23 18:01:41 +00:00
|
|
|
|
|
|
|
I2CPSession (I2CPServer& owner, std::shared_ptr<proto::socket> socket);
|
|
|
|
|
2016-05-13 19:13:36 +00:00
|
|
|
~I2CPSession ();
|
|
|
|
|
2016-06-01 14:05:40 +00:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2016-05-29 20:35:57 +00:00
|
|
|
uint16_t GetSessionID () const { return m_SessionID; };
|
2016-11-18 00:14:25 +00:00
|
|
|
std::shared_ptr<const I2CPDestination> GetDestination () const { return m_Destination; };
|
2016-06-01 18:38:13 +00:00
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
// called from I2CPDestination
|
2016-05-29 20:35:57 +00:00
|
|
|
void SendI2CPMessage (uint8_t type, const uint8_t * payload, size_t len);
|
2018-01-06 03:48:51 +00:00
|
|
|
void SendMessagePayloadMessage (const uint8_t * payload, size_t len);
|
2016-06-01 19:30:57 +00:00
|
|
|
void SendMessageStatusMessage (uint32_t nonce, I2CPMessageStatus status);
|
2016-06-01 18:38:13 +00:00
|
|
|
|
2016-05-13 19:13:36 +00:00
|
|
|
// message handlers
|
|
|
|
void GetDateMessageHandler (const uint8_t * buf, size_t len);
|
2016-05-27 20:22:42 +00:00
|
|
|
void CreateSessionMessageHandler (const uint8_t * buf, size_t len);
|
2016-06-01 14:05:40 +00:00
|
|
|
void DestroySessionMessageHandler (const uint8_t * buf, size_t len);
|
2016-06-06 19:36:02 +00:00
|
|
|
void ReconfigureSessionMessageHandler (const uint8_t * buf, size_t len);
|
2016-05-30 16:56:42 +00:00
|
|
|
void CreateLeaseSetMessageHandler (const uint8_t * buf, size_t len);
|
2019-01-29 16:30:31 +00:00
|
|
|
void CreateLeaseSet2MessageHandler (const uint8_t * buf, size_t len);
|
2016-05-30 18:31:56 +00:00
|
|
|
void SendMessageMessageHandler (const uint8_t * buf, size_t len);
|
2016-06-03 16:03:36 +00:00
|
|
|
void SendMessageExpiresMessageHandler (const uint8_t * buf, size_t len);
|
2016-06-01 15:11:18 +00:00
|
|
|
void HostLookupMessageHandler (const uint8_t * buf, size_t len);
|
2016-06-03 15:49:39 +00:00
|
|
|
void DestLookupMessageHandler (const uint8_t * buf, size_t len);
|
2016-06-03 17:48:21 +00:00
|
|
|
void GetBandwidthLimitsMessageHandler (const uint8_t * buf, size_t len);
|
2016-05-12 20:17:10 +00:00
|
|
|
|
|
|
|
private:
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-05-12 20:17:10 +00:00
|
|
|
void ReadProtocolByte ();
|
2016-06-13 17:20:21 +00:00
|
|
|
void ReceiveHeader ();
|
|
|
|
void HandleReceivedHeader (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
void ReceivePayload ();
|
|
|
|
void HandleReceivedPayload (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
void HandleMessage ();
|
2016-05-12 20:17:10 +00:00
|
|
|
void Terminate ();
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-05-27 20:22:42 +00:00
|
|
|
void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, const uint8_t * buf);
|
|
|
|
std::string ExtractString (const uint8_t * buf, size_t len);
|
|
|
|
size_t PutString (uint8_t * buf, size_t len, const std::string& str);
|
2016-06-02 17:26:41 +00:00
|
|
|
void ExtractMapping (const uint8_t * buf, size_t len, std::map<std::string, std::string>& mapping);
|
2016-05-27 20:22:42 +00:00
|
|
|
|
2016-05-30 19:19:22 +00:00
|
|
|
void SendSessionStatusMessage (uint8_t status);
|
2016-06-01 15:11:18 +00:00
|
|
|
void SendHostReplyMessage (uint32_t requestID, std::shared_ptr<const i2p::data::IdentityEx> identity);
|
2016-05-30 19:19:22 +00:00
|
|
|
|
2016-05-12 20:17:10 +00:00
|
|
|
private:
|
|
|
|
|
2016-05-13 19:13:36 +00:00
|
|
|
I2CPServer& m_Owner;
|
2016-06-23 18:01:41 +00:00
|
|
|
std::shared_ptr<proto::socket> m_Socket;
|
2016-06-13 17:20:21 +00:00
|
|
|
uint8_t m_Header[I2CP_HEADER_SIZE], * m_Payload;
|
|
|
|
size_t m_PayloadLen;
|
2016-05-27 17:46:28 +00:00
|
|
|
|
|
|
|
std::shared_ptr<I2CPDestination> m_Destination;
|
2016-05-29 20:35:57 +00:00
|
|
|
uint16_t m_SessionID;
|
2016-06-01 18:38:13 +00:00
|
|
|
uint32_t m_MessageID;
|
2016-06-10 15:39:20 +00:00
|
|
|
bool m_IsSendAccepted;
|
2016-05-12 20:17:10 +00:00
|
|
|
};
|
2016-05-13 19:13:36 +00:00
|
|
|
typedef void (I2CPSession::*I2CPMessageHandler)(const uint8_t * buf, size_t len);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-05-12 19:37:46 +00:00
|
|
|
class I2CPServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
I2CPServer (const std::string& interface, int port);
|
2016-05-31 15:54:45 +00:00
|
|
|
~I2CPServer ();
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-05-31 15:54:45 +00:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
boost::asio::io_service& GetService () { return m_Service; };
|
|
|
|
|
2016-11-18 00:14:25 +00:00
|
|
|
bool InsertSession (std::shared_ptr<I2CPSession> session);
|
2016-05-31 15:54:45 +00:00
|
|
|
void RemoveSession (uint16_t sessionID);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void Run ();
|
|
|
|
|
|
|
|
void Accept ();
|
2016-06-22 19:48:36 +00:00
|
|
|
|
2016-06-23 18:01:41 +00:00
|
|
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<I2CPSession::proto::socket> socket);
|
2016-05-13 19:13:36 +00:00
|
|
|
|
|
|
|
private:
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-05-30 16:56:42 +00:00
|
|
|
I2CPMessageHandler m_MessagesHandlers[256];
|
2016-05-31 15:54:45 +00:00
|
|
|
std::map<uint16_t, std::shared_ptr<I2CPSession> > m_Sessions;
|
|
|
|
|
|
|
|
bool m_IsRunning;
|
2018-01-06 03:48:51 +00:00
|
|
|
std::thread * m_Thread;
|
2016-05-31 15:54:45 +00:00
|
|
|
boost::asio::io_service m_Service;
|
2016-06-23 18:01:41 +00:00
|
|
|
I2CPSession::proto::acceptor m_Acceptor;
|
2016-05-13 19:13:36 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
const decltype(m_MessagesHandlers)& GetMessagesHandlers () const { return m_MessagesHandlers; };
|
2016-11-18 00:14:25 +00:00
|
|
|
|
|
|
|
// for HTTP
|
|
|
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2016-05-12 19:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|