i2pd/SSU.h

105 lines
2.4 KiB
C
Raw Normal View History

2014-01-24 01:10:33 +04:00
#ifndef SSU_H__
#define SSU_H__
#include <inttypes.h>
2014-01-25 01:30:07 +04:00
#include <map>
2014-01-24 01:10:33 +04:00
#include <boost/asio.hpp>
2014-01-28 01:52:17 +04:00
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include "I2PEndian.h"
2014-01-29 01:49:54 +04:00
#include "RouterInfo.h"
2014-01-24 01:10:33 +04:00
namespace i2p
{
namespace ssu
{
2014-01-28 01:52:17 +04:00
#pragma pack(1)
struct SSUHeader
{
uint8_t mac[16];
uint8_t iv[16];
uint8_t flag;
uint32_t time;
};
#pragma pack()
2014-01-24 01:10:33 +04:00
const int SSU_MTU = 1484;
2014-01-25 01:30:07 +04:00
// payload types (4 bits)
const uint8_t PAYLOAD_TYPE_SESSION_REQUEST = 0;
const uint8_t PAYLOAD_TYPE_SESSION_CREATED = 1;
const uint8_t PAYLOAD_TYPE_SESSION_CONFIRMED = 2;
const uint8_t PAYLOAD_TYPE_RELAY_REQUEST = 3;
const uint8_t PAYLOAD_TYPE_RELAY_RESPONSE = 4;
const uint8_t PAYLOAD_TYPE_RELAY_INTRO = 5;
const uint8_t PAYLOAD_TYPE_DATA = 6;
const uint8_t PAYLOAD_TYPE_TEST = 7;
enum SessionState
{
eSessionStateUnknown,
eSessionStateRequestSent,
eSessionStateRequestReceived,
eSessionStateCreatedSent,
eSessionStateCreatedReceived,
eSessionStateConfirmedSent,
eSessionStateConfirmedReceived,
eSessionStateEstablised
};
2014-01-29 01:49:54 +04:00
class SSUServer;
2014-01-25 01:30:07 +04:00
class SSUSession
{
public:
2014-01-29 01:49:54 +04:00
SSUSession (SSUServer * server, const boost::asio::ip::udp::endpoint& remoteEndpoint,
i2p::data::RouterInfo * router = nullptr);
void ProcessNextMessage (uint8_t * buf, size_t len);
2014-01-28 01:52:17 +04:00
private:
2014-01-29 01:49:54 +04:00
void ProcessSessionRequest (uint8_t * buf, size_t len);
void Encrypt (uint8_t * buf, size_t len, uint8_t * aesKey, uint8_t * iv, uint8_t * macKey);
void Decrypt (uint8_t * buf, size_t len, uint8_t * aesKey);
bool Validate (uint8_t * buf, size_t len, uint8_t * macKey);
2014-01-25 01:30:07 +04:00
private:
2014-01-29 01:49:54 +04:00
SSUServer * m_Server;
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
2014-01-25 01:30:07 +04:00
SessionState m_State;
2014-01-29 01:49:54 +04:00
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption m_Encryption;
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption m_Decryption;
2014-01-25 01:30:07 +04:00
};
2014-01-24 01:10:33 +04:00
class SSUServer
{
public:
SSUServer (boost::asio::io_service& service, int port);
2014-01-25 01:30:07 +04:00
~SSUServer ();
2014-01-24 01:10:33 +04:00
void Start ();
void Stop ();
2014-01-29 01:49:54 +04:00
SSUSession * GetSession (i2p::data::RouterInfo * router);
void Send (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
2014-01-24 01:10:33 +04:00
private:
void Receive ();
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred);
private:
boost::asio::ip::udp::socket m_Socket;
boost::asio::ip::udp::endpoint m_SenderEndpoint;
2014-01-29 01:49:54 +04:00
uint8_t m_ReceiveBuffer[2*SSU_MTU];
2014-01-25 01:30:07 +04:00
std::map<boost::asio::ip::udp::endpoint, SSUSession *> m_Sessions;
2014-01-24 01:10:33 +04:00
};
}
}
#endif