2014-01-23 21:10:33 +00:00
|
|
|
#ifndef SSU_H__
|
|
|
|
#define SSU_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-01-24 21:30:07 +00:00
|
|
|
#include <map>
|
2014-02-09 23:28:34 +00:00
|
|
|
#include <list>
|
2014-01-23 21:10:33 +00:00
|
|
|
#include <boost/asio.hpp>
|
2014-01-27 21:52:17 +00:00
|
|
|
#include <cryptopp/modes.h>
|
|
|
|
#include <cryptopp/aes.h>
|
|
|
|
#include "I2PEndian.h"
|
2014-01-28 21:49:54 +00:00
|
|
|
#include "RouterInfo.h"
|
2014-01-29 21:49:53 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2014-01-23 21:10:33 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace ssu
|
|
|
|
{
|
2014-01-27 21:52:17 +00:00
|
|
|
#pragma pack(1)
|
|
|
|
struct SSUHeader
|
|
|
|
{
|
|
|
|
uint8_t mac[16];
|
|
|
|
uint8_t iv[16];
|
|
|
|
uint8_t flag;
|
|
|
|
uint32_t time;
|
|
|
|
};
|
|
|
|
#pragma pack()
|
|
|
|
|
2014-01-23 21:10:33 +00:00
|
|
|
const int SSU_MTU = 1484;
|
|
|
|
|
2014-01-30 13:28:11 +00:00
|
|
|
// payload types (4 bits)
|
2014-01-24 21:30:07 +00:00
|
|
|
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;
|
2014-02-09 02:06:40 +00:00
|
|
|
const uint8_t PAYLOAD_TYPE_SESSION_DESTROYED = 8;
|
2014-01-24 21:30:07 +00:00
|
|
|
|
2014-02-07 20:47:10 +00:00
|
|
|
// data flags
|
|
|
|
const uint8_t DATA_FLAG_EXTENDED_DATA_INCLUDED = 0x02;
|
|
|
|
const uint8_t DATA_FLAG_WANT_REPLY = 0x04;
|
|
|
|
const uint8_t DATA_FLAG_REQUEST_PREVIOUS_ACKS = 0x08;
|
|
|
|
const uint8_t DATA_FLAG_EXPLICIT_CONGESTION_NOTIFICATION = 0x10;
|
|
|
|
const uint8_t DATA_FLAG_ACK_BITFIELDS_INCLUDED = 0x40;
|
|
|
|
const uint8_t DATA_FLAG_EXPLICIT_ACKS_INCLUDED = 0x80;
|
|
|
|
|
2014-01-24 21:30:07 +00:00
|
|
|
enum SessionState
|
|
|
|
{
|
|
|
|
eSessionStateUnknown,
|
|
|
|
eSessionStateRequestSent,
|
|
|
|
eSessionStateRequestReceived,
|
|
|
|
eSessionStateCreatedSent,
|
|
|
|
eSessionStateCreatedReceived,
|
|
|
|
eSessionStateConfirmedSent,
|
|
|
|
eSessionStateConfirmedReceived,
|
2014-02-21 21:13:36 +00:00
|
|
|
eSessionRelayRequestSent,
|
|
|
|
eSessionRelayRequestReceived,
|
2014-03-01 02:28:05 +00:00
|
|
|
eSessionRelayResponseReceived,
|
2014-02-08 02:42:35 +00:00
|
|
|
eSessionStateEstablished
|
2014-01-24 21:30:07 +00:00
|
|
|
};
|
|
|
|
|
2014-01-28 21:49:54 +00:00
|
|
|
class SSUServer;
|
2014-01-24 21:30:07 +00:00
|
|
|
class SSUSession
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-02-09 13:52:56 +00:00
|
|
|
SSUSession (SSUServer * server, boost::asio::ip::udp::endpoint& remoteEndpoint,
|
|
|
|
const i2p::data::RouterInfo * router = nullptr);
|
2014-01-29 21:49:53 +00:00
|
|
|
void ProcessNextMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
|
|
|
|
|
|
|
void Connect ();
|
2014-02-21 21:13:36 +00:00
|
|
|
void ConnectThroughIntroducer (const i2p::data::RouterInfo::Introducer& introducer);
|
2014-02-09 02:06:40 +00:00
|
|
|
void Close ();
|
|
|
|
boost::asio::ip::udp::endpoint& GetRemoteEndpoint () { return m_RemoteEndpoint; };
|
2014-03-12 11:37:43 +00:00
|
|
|
const i2p::data::RouterInfo * GetRemoteRouter () const { return m_RemoteRouter; };
|
2014-01-29 21:49:53 +00:00
|
|
|
void SendI2NPMessage (I2NPMessage * msg);
|
2014-02-25 03:28:28 +00:00
|
|
|
|
2014-01-27 21:52:17 +00:00
|
|
|
private:
|
|
|
|
|
2014-02-03 19:40:38 +00:00
|
|
|
void CreateAESandMacKey (uint8_t * pubKey, uint8_t * aesKey, uint8_t * macKey);
|
2014-01-29 21:49:53 +00:00
|
|
|
|
2014-02-05 20:07:47 +00:00
|
|
|
void ProcessMessage (uint8_t * buf, size_t len); // call for established session
|
2014-01-29 21:49:53 +00:00
|
|
|
void ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
|
|
|
void SendSessionRequest ();
|
2014-02-21 21:13:36 +00:00
|
|
|
void SendRelayRequest (const i2p::data::RouterInfo::Introducer& introducer);
|
2014-01-29 21:49:53 +00:00
|
|
|
void ProcessSessionCreated (uint8_t * buf, size_t len);
|
2014-01-30 19:03:11 +00:00
|
|
|
void SendSessionCreated (const uint8_t * x);
|
2014-02-04 19:20:58 +00:00
|
|
|
void ProcessSessionConfirmed (uint8_t * buf, size_t len);
|
|
|
|
void SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, uint32_t relayTag);
|
2014-02-21 21:13:36 +00:00
|
|
|
void ProcessRelayResponse (uint8_t * buf, size_t len);
|
2014-02-09 23:28:34 +00:00
|
|
|
void Established ();
|
2014-02-05 20:07:47 +00:00
|
|
|
void ProcessData (uint8_t * buf, size_t len);
|
2014-02-07 20:47:10 +00:00
|
|
|
void SendMsgAck (uint32_t msgID);
|
2014-02-09 02:06:40 +00:00
|
|
|
void SendSesionDestroyed ();
|
2014-02-09 23:28:34 +00:00
|
|
|
void Send (i2p::I2NPMessage * msg);
|
2014-02-09 02:06:40 +00:00
|
|
|
|
2014-02-12 21:36:13 +00:00
|
|
|
bool ProcessIntroKeyEncryptedMessage (uint8_t expectedPayloadType, uint8_t * buf, size_t len);
|
2014-02-09 13:52:56 +00:00
|
|
|
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, const uint8_t * aesKey, const uint8_t * iv, const uint8_t * macKey);
|
|
|
|
void Decrypt (uint8_t * buf, size_t len, const uint8_t * aesKey);
|
|
|
|
bool Validate (uint8_t * buf, size_t len, const uint8_t * macKey);
|
2014-02-12 21:36:13 +00:00
|
|
|
const uint8_t * GetIntroKey () const;
|
2014-01-24 21:30:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-01-28 21:49:54 +00:00
|
|
|
SSUServer * m_Server;
|
|
|
|
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
|
2014-02-09 13:52:56 +00:00
|
|
|
const i2p::data::RouterInfo * m_RemoteRouter;
|
2014-01-24 21:30:07 +00:00
|
|
|
SessionState m_State;
|
2014-01-28 21:49:54 +00:00
|
|
|
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption m_Encryption;
|
2014-01-29 21:49:53 +00:00
|
|
|
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption m_Decryption;
|
2014-02-03 19:40:38 +00:00
|
|
|
uint8_t m_SessionKey[32], m_MacKey[32];
|
2014-02-06 15:16:50 +00:00
|
|
|
std::map<uint32_t, I2NPMessage *> m_IncomleteMessages;
|
2014-02-09 23:28:34 +00:00
|
|
|
std::list<i2p::I2NPMessage *> m_DelayedMessages;
|
2014-01-24 21:30:07 +00:00
|
|
|
};
|
|
|
|
|
2014-01-23 21:10:33 +00:00
|
|
|
class SSUServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SSUServer (boost::asio::io_service& service, int port);
|
2014-01-24 21:30:07 +00:00
|
|
|
~SSUServer ();
|
2014-01-23 21:10:33 +00:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2014-02-09 13:52:56 +00:00
|
|
|
SSUSession * GetSession (const i2p::data::RouterInfo * router);
|
2014-02-09 02:06:40 +00:00
|
|
|
void DeleteSession (SSUSession * session);
|
2014-02-21 21:13:36 +00:00
|
|
|
void DeleteAllSessions ();
|
|
|
|
|
2014-01-30 19:03:11 +00:00
|
|
|
const boost::asio::ip::udp::endpoint& GetEndpoint () const { return m_Endpoint; };
|
2014-01-28 21:49:54 +00:00
|
|
|
void Send (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
|
2014-02-21 21:13:36 +00:00
|
|
|
void ReassignSession (const boost::asio::ip::udp::endpoint& oldEndpoint, const boost::asio::ip::udp::endpoint& newEndpoint);
|
2014-01-23 21:10:33 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void Receive ();
|
|
|
|
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-01-30 19:03:11 +00:00
|
|
|
boost::asio::ip::udp::endpoint m_Endpoint;
|
2014-01-23 21:10:33 +00:00
|
|
|
boost::asio::ip::udp::socket m_Socket;
|
|
|
|
boost::asio::ip::udp::endpoint m_SenderEndpoint;
|
2014-01-28 21:49:54 +00:00
|
|
|
uint8_t m_ReceiveBuffer[2*SSU_MTU];
|
2014-01-24 21:30:07 +00:00
|
|
|
std::map<boost::asio::ip::udp::endpoint, SSUSession *> m_Sessions;
|
2014-02-25 03:28:28 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// for HTTP only
|
|
|
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
2014-01-23 21:10:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|