i2pd/SSUSession.h

164 lines
6.3 KiB
C
Raw Normal View History

2014-10-30 22:13:29 +03:00
#ifndef SSU_SESSION_H__
#define SSU_SESSION_H__
#include <inttypes.h>
#include <set>
2014-11-24 20:26:11 +03:00
#include <memory>
2015-11-03 17:15:49 +03:00
#include "Crypto.h"
2014-10-30 22:13:29 +03:00
#include "I2NPProtocol.h"
#include "TransportSession.h"
#include "SSUData.h"
namespace i2p
{
namespace transport
{
2015-11-16 21:27:27 +03:00
const uint8_t SSU_HEADER_EXTENDED_OPTIONS_INCLUDED = 0x04;
2014-10-30 22:13:29 +03:00
struct SSUHeader
{
uint8_t mac[16];
uint8_t iv[16];
uint8_t flag;
2016-01-10 03:24:52 +03:00
uint8_t time[4];
2014-10-30 22:13:29 +03:00
uint8_t GetPayloadType () const { return flag >> 4; };
2015-11-16 21:27:27 +03:00
bool IsExtendedOptions () const { return flag & SSU_HEADER_EXTENDED_OPTIONS_INCLUDED; };
2014-10-30 22:13:29 +03:00
};
const int SSU_CONNECT_TIMEOUT = 5; // 5 seconds
const int SSU_TERMINATION_TIMEOUT = 330; // 5.5 minutes
2016-09-19 01:42:21 +03:00
const int SSU_CLOCK_SKEW = 60; // in seconds
2014-10-30 22:13:29 +03: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_PEER_TEST = 7;
const uint8_t PAYLOAD_TYPE_SESSION_DESTROYED = 8;
// extended options
const uint16_t EXTENDED_OPTIONS_FLAG_REQUEST_RELAY_TAG = 0x0001;
2014-10-30 22:13:29 +03:00
enum SessionState
{
eSessionStateUnknown,
eSessionStateIntroduced,
eSessionStateEstablished,
2015-02-07 23:25:06 +03:00
eSessionStateClosed,
2014-10-30 22:13:29 +03:00
eSessionStateFailed
};
2015-02-26 05:56:51 +03:00
enum PeerTestParticipant
{
ePeerTestParticipantUnknown = 0,
ePeerTestParticipantAlice1,
ePeerTestParticipantAlice2,
ePeerTestParticipantBob,
ePeerTestParticipantCharlie
};
2014-10-30 22:13:29 +03:00
class SSUServer;
2014-11-24 20:26:11 +03:00
class SSUSession: public TransportSession, public std::enable_shared_from_this<SSUSession>
2014-10-30 22:13:29 +03:00
{
public:
SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint,
std::shared_ptr<const i2p::data::RouterInfo> router = nullptr, bool peerTest = false);
2014-10-30 22:13:29 +03:00
void ProcessNextMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
~SSUSession ();
void Connect ();
void WaitForConnect ();
2015-12-10 06:17:43 +03:00
void Introduce (const i2p::data::RouterInfo::Introducer& introducer,
std::shared_ptr<const i2p::data::RouterInfo> to); // Alice to Charlie
2014-10-30 22:13:29 +03:00
void WaitForIntroduction ();
void Close ();
2015-02-07 04:53:48 +03:00
void Done ();
void Failed ();
2014-10-30 22:13:29 +03:00
boost::asio::ip::udp::endpoint& GetRemoteEndpoint () { return m_RemoteEndpoint; };
bool IsV6 () const { return m_RemoteEndpoint.address ().is_v6 (); };
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
2014-10-30 22:13:29 +03:00
void SendPeerTest (); // Alice
SessionState GetState () const { return m_State; };
size_t GetNumSentBytes () const { return m_NumSentBytes; };
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
void SendKeepAlive ();
uint32_t GetRelayTag () const { return m_RelayTag; };
2015-11-03 17:15:49 +03:00
const i2p::data::RouterInfo::IntroKey& GetIntroKey () const { return m_IntroKey; };
2014-10-30 22:13:29 +03:00
uint32_t GetCreationTime () const { return m_CreationTime; };
2015-02-15 22:17:55 +03:00
void FlushData ();
2014-10-30 22:13:29 +03:00
private:
2015-02-07 23:25:06 +03:00
boost::asio::io_service& GetService ();
2014-10-30 22:13:29 +03:00
void CreateAESandMacKey (const uint8_t * pubKey);
size_t GetSSUHeaderSize (const uint8_t * buf) const;
void PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs);
2014-10-30 22:13:29 +03:00
void ProcessMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint); // call for established session
void ProcessSessionRequest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
2014-10-30 22:13:29 +03:00
void SendSessionRequest ();
2015-12-10 06:17:43 +03:00
void SendRelayRequest (const i2p::data::RouterInfo::Introducer& introducer, uint32_t nonce);
2014-10-30 22:13:29 +03:00
void ProcessSessionCreated (uint8_t * buf, size_t len);
void SendSessionCreated (const uint8_t * x, bool sendRelayTag = true);
void ProcessSessionConfirmed (const uint8_t * buf, size_t len);
2014-10-30 22:13:29 +03:00
void SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, size_t ourAddressLen);
void ProcessRelayRequest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& from);
2014-10-30 22:13:29 +03:00
void SendRelayResponse (uint32_t nonce, const boost::asio::ip::udp::endpoint& from,
const uint8_t * introKey, const boost::asio::ip::udp::endpoint& to);
2015-11-30 18:23:05 +03:00
void SendRelayIntro (std::shared_ptr<SSUSession> session, const boost::asio::ip::udp::endpoint& from);
void ProcessRelayResponse (const uint8_t * buf, size_t len);
void ProcessRelayIntro (const uint8_t * buf, size_t len);
2014-10-30 22:13:29 +03:00
void Established ();
void ScheduleConnectTimer ();
void HandleConnectTimer (const boost::system::error_code& ecode);
2015-03-26 21:35:20 +03:00
void ProcessPeerTest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
2016-07-22 19:50:03 +03:00
void SendPeerTest (uint32_t nonce, const boost::asio::ip::address& address, uint16_t port, const uint8_t * introKey, bool toAddress = true, bool sendAddress = true);
2014-10-30 22:13:29 +03:00
void ProcessData (uint8_t * buf, size_t len);
void SendSesionDestroyed ();
void Send (uint8_t type, const uint8_t * payload, size_t len); // with session key
void Send (const uint8_t * buf, size_t size);
2016-02-26 02:40:40 +03:00
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, const i2p::crypto::AESKey& aesKey,
const uint8_t * iv, const i2p::crypto::MACKey& macKey, uint8_t flag = 0);
2014-10-30 22:13:29 +03:00
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len); // with session key
2015-11-03 17:15:49 +03:00
void Decrypt (uint8_t * buf, size_t len, const i2p::crypto::AESKey& aesKey);
2014-10-30 22:13:29 +03:00
void DecryptSessionKey (uint8_t * buf, size_t len);
2015-11-03 17:15:49 +03:00
bool Validate (uint8_t * buf, size_t len, const i2p::crypto::MACKey& macKey);
2014-10-30 22:13:29 +03:00
private:
friend class SSUData; // TODO: change in later
SSUServer& m_Server;
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
boost::asio::deadline_timer m_ConnectTimer;
2015-11-03 17:15:49 +03:00
bool m_IsPeerTest;
2014-10-30 22:13:29 +03:00
SessionState m_State;
bool m_IsSessionKey;
2016-12-30 06:06:33 +03:00
uint32_t m_RelayTag; // received from peer
uint32_t m_SentRelayTag; // sent by us
2014-10-30 22:13:29 +03:00
i2p::crypto::CBCEncryption m_SessionKeyEncryption;
i2p::crypto::CBCDecryption m_SessionKeyDecryption;
2014-11-01 21:56:13 +03:00
i2p::crypto::AESKey m_SessionKey;
i2p::crypto::MACKey m_MacKey;
2015-11-03 17:15:49 +03:00
i2p::data::RouterInfo::IntroKey m_IntroKey;
2014-10-30 22:13:29 +03:00
uint32_t m_CreationTime; // seconds since epoch
2015-02-08 16:50:05 +03:00
SSUData m_Data;
2015-02-15 22:17:55 +03:00
bool m_IsDataReceived;
2015-11-03 17:15:49 +03:00
std::unique_ptr<SignedData> m_SignedData; // we need it for SessionConfirmed only
2015-12-10 06:17:43 +03:00
std::map<uint32_t, std::shared_ptr<const i2p::data::RouterInfo> > m_RelayRequests; // nonce->Charlie
2014-10-30 22:13:29 +03:00
};
}
}
#endif