2016-05-30 19:56:42 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2016, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* 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 22:37:46 +03:00
|
|
|
#ifndef I2CP_H__
|
|
|
|
#define I2CP_H__
|
|
|
|
|
2016-05-12 23:17:10 +03:00
|
|
|
#include <inttypes.h>
|
2016-05-12 22:37:46 +03:00
|
|
|
#include <string>
|
2016-05-12 23:17:10 +03:00
|
|
|
#include <memory>
|
2016-05-12 22:37:46 +03:00
|
|
|
#include <boost/asio.hpp>
|
2016-05-27 20:46:28 +03:00
|
|
|
#include "Destination.h"
|
2016-05-12 22:37:46 +03:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
2016-05-12 23:17:10 +03:00
|
|
|
const uint8_t I2CP_PRTOCOL_BYTE = 0x2A;
|
2016-05-13 22:13:36 +03:00
|
|
|
const size_t I2CP_SESSION_BUFFER_SIZE = 4096;
|
2016-05-12 23:17:10 +03:00
|
|
|
|
2016-05-13 22:13:36 +03:00
|
|
|
const size_t I2CP_HEADER_LENGTH_OFFSET = 0;
|
|
|
|
const size_t I2CP_HEADER_TYPE_OFFSET = I2CP_HEADER_LENGTH_OFFSET + 4;
|
|
|
|
const size_t I2CP_HEADER_SIZE = I2CP_HEADER_TYPE_OFFSET + 1;
|
|
|
|
|
|
|
|
const uint8_t I2CP_GET_DATE_MESSAGE = 32;
|
2016-05-27 23:22:42 +03:00
|
|
|
const uint8_t I2CP_SET_DATE_MESSAGE = 33;
|
|
|
|
const uint8_t I2CP_CREATE_SESSION_MESSAGE = 1;
|
2016-05-30 22:19:22 +03:00
|
|
|
const uint8_t I2CP_SESSION_STATUS_MESSAGE = 20;
|
2016-05-29 23:35:57 +03:00
|
|
|
const uint8_t I2CP_REQUEST_VARIABLE_LEASESET_MESSAGE = 37;
|
2016-05-30 19:56:42 +03:00
|
|
|
const uint8_t I2CP_CREATE_LEASESET_MESSAGE = 4;
|
2016-05-30 21:31:56 +03:00
|
|
|
const uint8_t I2CP_SEND_MESSAGE_MESSAGE = 5;
|
2016-05-30 19:56:42 +03:00
|
|
|
|
2016-05-27 20:46:28 +03:00
|
|
|
class I2CPSession;
|
|
|
|
class I2CPDestination: public LeaseSetDestination
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
I2CPDestination (I2CPSession& owner, std::shared_ptr<const i2p::data::IdentityEx> identity, bool isPublic);
|
|
|
|
|
2016-05-30 19:56:42 +03:00
|
|
|
void SetEncryptionPrivateKey (const uint8_t * key);
|
|
|
|
void LeaseSetCreated (const uint8_t * buf, size_t len); // called from I2CPSession
|
2016-05-30 21:31:56 +03:00
|
|
|
void SendMsgTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash& ident); // called from I2CPSession
|
2016-05-30 19:56:42 +03:00
|
|
|
|
2016-05-27 20:46:28 +03:00
|
|
|
protected:
|
|
|
|
|
|
|
|
// implements LocalDestination
|
2016-05-29 16:33:50 +03:00
|
|
|
const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; };
|
2016-05-27 20:46:28 +03:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> GetIdentity () const { return m_Identity; };
|
|
|
|
|
|
|
|
// I2CP
|
2016-05-29 16:33:50 +03:00
|
|
|
void HandleDataMessage (const uint8_t * buf, size_t len) { /* TODO */ };
|
2016-05-29 23:35:57 +03:00
|
|
|
void CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels);
|
2016-05-27 20:46:28 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
I2CPSession& m_Owner;
|
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> m_Identity;
|
2016-05-30 19:56:42 +03:00
|
|
|
uint8_t m_EncryptionPrivateKey[256];
|
|
|
|
uint64_t m_LeaseSetExpirationTime;
|
2016-05-27 20:46:28 +03:00
|
|
|
};
|
|
|
|
|
2016-05-13 22:13:36 +03:00
|
|
|
class I2CPServer;
|
2016-05-12 23:17:10 +03:00
|
|
|
class I2CPSession: public std::enable_shared_from_this<I2CPSession>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-05-13 22:13:36 +03:00
|
|
|
I2CPSession (I2CPServer& owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
|
|
|
~I2CPSession ();
|
|
|
|
|
2016-05-29 23:35:57 +03:00
|
|
|
uint16_t GetSessionID () const { return m_SessionID; };
|
|
|
|
void SendI2CPMessage (uint8_t type, const uint8_t * payload, size_t len);
|
|
|
|
|
2016-05-13 22:13:36 +03:00
|
|
|
// message handlers
|
|
|
|
void GetDateMessageHandler (const uint8_t * buf, size_t len);
|
2016-05-27 23:22:42 +03:00
|
|
|
void CreateSessionMessageHandler (const uint8_t * buf, size_t len);
|
2016-05-30 19:56:42 +03:00
|
|
|
void CreateLeaseSetMessageHandler (const uint8_t * buf, size_t len);
|
2016-05-30 21:31:56 +03:00
|
|
|
void SendMessageMessageHandler (const uint8_t * buf, size_t len);
|
2016-05-12 23:17:10 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void ReadProtocolByte ();
|
|
|
|
void Receive ();
|
|
|
|
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2016-05-13 22:13:36 +03:00
|
|
|
void HandleNextMessage (const uint8_t * buf);
|
2016-05-12 23:17:10 +03:00
|
|
|
void Terminate ();
|
2016-05-29 23:35:57 +03:00
|
|
|
|
2016-05-27 23:22:42 +03: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-05-30 22:19:22 +03:00
|
|
|
void SendSessionStatusMessage (uint8_t status);
|
|
|
|
|
2016-05-12 23:17:10 +03:00
|
|
|
private:
|
|
|
|
|
2016-05-13 22:13:36 +03:00
|
|
|
I2CPServer& m_Owner;
|
2016-05-12 23:17:10 +03:00
|
|
|
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
|
2016-05-13 22:13:36 +03:00
|
|
|
uint8_t m_Buffer[I2CP_SESSION_BUFFER_SIZE], * m_NextMessage;
|
|
|
|
size_t m_NextMessageLen, m_NextMessageOffset;
|
2016-05-27 20:46:28 +03:00
|
|
|
|
|
|
|
std::shared_ptr<I2CPDestination> m_Destination;
|
2016-05-29 23:35:57 +03:00
|
|
|
uint16_t m_SessionID;
|
2016-05-12 23:17:10 +03:00
|
|
|
};
|
2016-05-13 22:13:36 +03:00
|
|
|
typedef void (I2CPSession::*I2CPMessageHandler)(const uint8_t * buf, size_t len);
|
|
|
|
|
2016-05-12 22:37:46 +03:00
|
|
|
class I2CPServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
I2CPServer (const std::string& interface, int port);
|
2016-05-13 22:13:36 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2016-05-30 19:56:42 +03:00
|
|
|
I2CPMessageHandler m_MessagesHandlers[256];
|
2016-05-13 22:13:36 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
const decltype(m_MessagesHandlers)& GetMessagesHandlers () const { return m_MessagesHandlers; };
|
2016-05-12 22:37:46 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|