i2pd/libi2pd/NTCP2.h

80 lines
2.0 KiB
C
Raw Normal View History

2018-06-05 16:53:13 +00:00
#ifndef NTCP2_H__
#define NTCP2_H__
#include <inttypes.h>
#include <memory>
2018-06-11 18:05:30 +00:00
#include <thread>
2018-06-06 19:38:18 +00:00
#include <boost/asio.hpp>
2018-06-05 16:53:13 +00:00
#include "RouterInfo.h"
#include "TransportSession.h"
namespace i2p
{
namespace transport
{
2018-06-06 19:38:18 +00:00
class NTCP2Server;
2018-06-05 16:53:13 +00:00
class NTCP2Session: public TransportSession, public std::enable_shared_from_this<NTCP2Session>
{
public:
2018-06-11 18:05:30 +00:00
NTCP2Session (NTCP2Server& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr);
2018-06-05 16:53:13 +00:00
~NTCP2Session ();
2018-06-11 16:29:30 +00:00
void Terminate ();
2018-06-11 18:05:30 +00:00
void Done ();
2018-06-05 16:53:13 +00:00
2018-06-06 19:38:18 +00:00
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
void ClientLogin (); // Alice
2018-06-11 18:05:30 +00:00
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs) {}; // TODO
2018-06-06 19:38:18 +00:00
2018-06-05 16:53:13 +00:00
private:
2018-06-11 16:29:30 +00:00
bool KeyDerivationFunction1 (const uint8_t * rs, const uint8_t * pub, uint8_t * derived); // for SessionRequest
2018-06-05 16:53:13 +00:00
void CreateEphemeralKey (uint8_t * pub);
2018-06-06 19:38:18 +00:00
void SendSessionRequest ();
void HandleSessionRequestSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2018-06-11 16:29:30 +00:00
void HandleSessionCreatedReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2018-06-05 16:53:13 +00:00
private:
2018-06-06 19:38:18 +00:00
NTCP2Server& m_Server;
boost::asio::ip::tcp::socket m_Socket;
2018-06-11 16:29:30 +00:00
bool m_IsEstablished, m_IsTerminated;
2018-06-05 16:53:13 +00:00
uint8_t m_ExpandedPrivateKey[64]; // x25519 ephemeral key
2018-06-06 19:38:18 +00:00
uint8_t m_RemoteStaticKey[32], m_RemoteIV[16];
2018-06-11 16:29:30 +00:00
uint8_t * m_SessionRequestBuffer, * m_SessionCreatedBuffer;
2018-06-06 19:38:18 +00:00
};
class NTCP2Server
{
public:
2018-06-11 16:29:30 +00:00
NTCP2Server ();
~NTCP2Server ();
void Start ();
void Stop ();
2018-06-06 19:38:18 +00:00
boost::asio::io_service& GetService () { return m_Service; };
2018-06-11 16:29:30 +00:00
void Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn);
private:
void Run ();
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn);
2018-06-06 19:38:18 +00:00
private:
2018-06-11 16:29:30 +00:00
bool m_IsRunning;
std::thread * m_Thread;
2018-06-06 19:38:18 +00:00
boost::asio::io_service m_Service;
2018-06-11 16:29:30 +00:00
boost::asio::io_service::work m_Work;
2018-06-05 16:53:13 +00:00
};
}
}
#endif