i2pd/libi2pd/SSU2.h

111 lines
3.0 KiB
C
Raw Normal View History

2022-02-04 20:01:18 +00:00
/*
* Copyright (c) 2022, The PurpleI2P Project
2022-02-04 20:01:18 +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
*/
#ifndef SSU2_H__
#define SSU2_H__
#include <memory>
2022-03-01 02:46:00 +00:00
#include <map>
2022-02-28 01:15:14 +00:00
#include <unordered_map>
2022-03-01 02:46:00 +00:00
#include <boost/asio.hpp>
2022-02-04 20:01:18 +00:00
#include "Crypto.h"
#include "RouterInfo.h"
#include "TransportSession.h"
namespace i2p
{
namespace transport
{
const int SSU2_TERMINATION_TIMEOUT = 330; // 5.5 minutes
2022-03-01 02:46:00 +00:00
const size_t SSU2_SOCKET_RECEIVE_BUFFER_SIZE = 0x1FFFF; // 128K
const size_t SSU2_SOCKET_SEND_BUFFER_SIZE = 0x1FFFF; // 128K
2022-02-12 00:21:04 +00:00
enum SSU2MessageType
{
2022-03-05 02:51:40 +00:00
eSSU2SessionRequest = 0,
eSSU2SessionCreated = 1
2022-02-12 00:21:04 +00:00
};
2022-02-28 01:15:14 +00:00
class SSU2Server;
2022-02-04 20:01:18 +00:00
class SSU2Session: public TransportSession, public std::enable_shared_from_this<SSU2Session>
{
2022-02-12 00:21:04 +00:00
union Header
{
uint64_t ll[2];
uint8_t buf[16];
struct
{
2022-03-01 02:46:00 +00:00
uint64_t connID;
2022-03-09 02:33:21 +00:00
uint8_t packetNum[4];
uint8_t type;
uint8_t flags[3];
2022-02-12 00:21:04 +00:00
} h;
};
2022-02-04 20:01:18 +00:00
public:
2022-02-28 01:15:14 +00:00
SSU2Session (SSU2Server& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr,
2022-02-04 20:01:18 +00:00
std::shared_ptr<const i2p::data::RouterInfo::Address> addr = nullptr, bool peerTest = false);
~SSU2Session ();
2022-02-05 20:58:39 +00:00
2022-03-09 02:33:21 +00:00
void SetRemoteEndpoint (const boost::asio::ip::udp::endpoint& ep) { m_RemoteEndpoint = ep; };
2022-03-05 23:39:27 +00:00
void Done () override {};
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs) override {};
void ProcessSessionRequest (uint64_t connID, uint8_t * buf, size_t len);
2022-03-05 02:51:40 +00:00
bool ProcessSessionCreated (uint8_t * buf, size_t len);
2022-02-10 19:03:09 +00:00
private:
void SendSessionRequest ();
2022-03-09 02:33:21 +00:00
void SendSessionCreated (const uint8_t * X);
2022-03-05 02:51:40 +00:00
2022-02-05 20:58:39 +00:00
private:
2022-02-28 01:15:14 +00:00
SSU2Server& m_Server;
2022-02-05 20:58:39 +00:00
std::shared_ptr<i2p::crypto::X25519Keys> m_EphemeralKeys;
std::unique_ptr<i2p::crypto::NoiseSymmetricState> m_NoiseState;
2022-02-10 19:03:09 +00:00
std::shared_ptr<const i2p::data::RouterInfo::Address> m_Address;
2022-03-09 02:33:21 +00:00
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
2022-03-01 02:46:00 +00:00
uint64_t m_DestConnID, m_SourceConnID;
2022-02-04 20:01:18 +00:00
};
2022-02-28 01:15:14 +00:00
2022-03-11 21:17:44 +00:00
class SSU2Server: private i2p::util::RunnableServiceWithWork
2022-02-28 01:15:14 +00:00
{
public:
2022-03-11 21:17:44 +00:00
SSU2Server ();
2022-02-28 01:15:14 +00:00
~SSU2Server () {};
2022-03-11 21:17:44 +00:00
void Start ();
void Stop ();
boost::asio::io_service& GetService () { return GetIOService (); };
2022-02-28 01:15:14 +00:00
void AddSession (uint64_t connID, std::shared_ptr<SSU2Session> session);
2022-03-01 02:46:00 +00:00
void AddPendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep, std::shared_ptr<SSU2Session> session);
2022-02-28 01:15:14 +00:00
2022-03-01 02:46:00 +00:00
void Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to);
2022-02-28 01:15:14 +00:00
private:
2022-03-11 21:17:44 +00:00
void OpenSocket (int port);
2022-03-01 02:46:00 +00:00
void ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
2022-02-28 01:15:14 +00:00
private:
2022-03-01 02:46:00 +00:00
boost::asio::ip::udp::socket m_Socket;
2022-02-28 01:15:14 +00:00
std::unordered_map<uint64_t, std::shared_ptr<SSU2Session> > m_Sessions;
2022-03-01 02:46:00 +00:00
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<SSU2Session> > m_PendingOutgoingSessions;
2022-02-28 01:15:14 +00:00
};
2022-02-04 20:01:18 +00:00
}
}
#endif