2014-01-23 21:10:33 +00:00
|
|
|
#ifndef SSU_H__
|
|
|
|
#define SSU_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-06-17 17:15:32 +00:00
|
|
|
#include <string.h>
|
2014-01-24 21:30:07 +00:00
|
|
|
#include <map>
|
2014-02-09 23:28:34 +00:00
|
|
|
#include <list>
|
2014-04-09 18:58:30 +00:00
|
|
|
#include <set>
|
2014-04-20 00:45:41 +00:00
|
|
|
#include <thread>
|
2014-01-23 21:10:33 +00:00
|
|
|
#include <boost/asio.hpp>
|
2014-05-15 02:49:22 +00:00
|
|
|
#include "aes.h"
|
2014-01-27 21:52:17 +00:00
|
|
|
#include "I2PEndian.h"
|
2014-04-04 18:56:46 +00:00
|
|
|
#include "Identity.h"
|
2014-01-28 21:49:54 +00:00
|
|
|
#include "RouterInfo.h"
|
2014-01-29 21:49:53 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2014-04-22 15:39:26 +00:00
|
|
|
#include "SSUData.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;
|
2014-04-07 20:41:29 +00:00
|
|
|
|
|
|
|
uint8_t GetPayloadType () const { return flag >> 4; };
|
2014-01-27 21:52:17 +00:00
|
|
|
};
|
|
|
|
#pragma pack()
|
|
|
|
|
2014-03-31 02:55:03 +00:00
|
|
|
const int SSU_CONNECT_TIMEOUT = 5; // 5 seconds
|
2014-04-08 17:25:19 +00:00
|
|
|
const int SSU_TERMINATION_TIMEOUT = 330; // 5.5 minutes
|
2014-09-07 00:43:20 +00:00
|
|
|
const int SSU_KEEP_ALIVE_INTERVAL = 30; // 30 seconds
|
2014-09-05 20:35:02 +00:00
|
|
|
const int SSU_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour
|
2014-09-09 12:03:05 +00:00
|
|
|
const size_t SSU_MAX_NUM_INTRODUCERS = 3;
|
2014-01-23 21:10:33 +00:00
|
|
|
|
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;
|
2014-04-07 19:31:38 +00:00
|
|
|
const uint8_t PAYLOAD_TYPE_PEER_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
|
|
|
|
|
|
|
enum SessionState
|
|
|
|
{
|
2014-06-10 02:56:16 +00:00
|
|
|
eSessionStateUnknown,
|
2014-04-09 16:25:40 +00:00
|
|
|
eSessionStateIntroduced,
|
2014-03-16 12:34:32 +00:00
|
|
|
eSessionStateEstablished,
|
|
|
|
eSessionStateFailed
|
2014-04-13 20:59:54 +00:00
|
|
|
};
|
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-04-02 14:49:16 +00:00
|
|
|
SSUSession (SSUServer& server, boost::asio::ip::udp::endpoint& remoteEndpoint,
|
2014-04-07 23:28:06 +00:00
|
|
|
const i2p::data::RouterInfo * router = nullptr, bool peerTest = false);
|
2014-01-29 21:49:53 +00:00
|
|
|
void ProcessNextMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
2014-03-31 02:55:03 +00:00
|
|
|
~SSUSession ();
|
|
|
|
|
2014-01-29 21:49:53 +00:00
|
|
|
void Connect ();
|
2014-04-09 15:58:57 +00:00
|
|
|
void Introduce (uint32_t iTag, const uint8_t * iKey);
|
|
|
|
void WaitForIntroduction ();
|
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-04-07 20:19:33 +00:00
|
|
|
void SendPeerTest (); // Alice
|
|
|
|
|
2014-04-22 15:39:26 +00:00
|
|
|
SessionState GetState () const { return m_State; };
|
2014-07-20 21:12:36 +00:00
|
|
|
size_t GetNumSentBytes () const { return m_NumSentBytes; };
|
|
|
|
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
|
|
|
|
|
2014-09-07 00:43:20 +00:00
|
|
|
void SendKeepAlive ();
|
2014-09-05 20:35:02 +00:00
|
|
|
uint32_t GetRelayTag () const { return m_RelayTag; };
|
2014-09-09 17:45:12 +00:00
|
|
|
uint32_t GetCreationTime () const { return m_CreationTime; };
|
2014-09-05 20:35:02 +00:00
|
|
|
|
2014-01-27 21:52:17 +00:00
|
|
|
private:
|
|
|
|
|
2014-05-15 02:49:22 +00:00
|
|
|
void CreateAESandMacKey (const uint8_t * pubKey);
|
2014-01-29 21:49:53 +00:00
|
|
|
|
2014-08-30 20:30:53 +00:00
|
|
|
void PostI2NPMessage (I2NPMessage * msg);
|
2014-04-07 19:31:38 +00:00
|
|
|
void ProcessMessage (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint); // 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-04-09 15:58:57 +00:00
|
|
|
void SendRelayRequest (uint32_t iTag, const uint8_t * iKey);
|
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);
|
2014-04-08 19:35:08 +00:00
|
|
|
void SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress);
|
2014-09-05 21:34:33 +00:00
|
|
|
void ProcessRelayRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& from);
|
|
|
|
void SendRelayResponse (uint32_t nonce, const boost::asio::ip::udp::endpoint& from,
|
|
|
|
const uint8_t * introKey, const boost::asio::ip::udp::endpoint& to);
|
2014-04-16 20:47:56 +00:00
|
|
|
void SendRelayIntro (SSUSession * session, const boost::asio::ip::udp::endpoint& from);
|
2014-02-21 21:13:36 +00:00
|
|
|
void ProcessRelayResponse (uint8_t * buf, size_t len);
|
2014-04-08 18:03:15 +00:00
|
|
|
void ProcessRelayIntro (uint8_t * buf, size_t len);
|
2014-02-09 23:28:34 +00:00
|
|
|
void Established ();
|
2014-03-16 12:34:32 +00:00
|
|
|
void Failed ();
|
2014-09-04 14:20:44 +00:00
|
|
|
void ScheduleConnectTimer ();
|
2014-03-31 02:55:03 +00:00
|
|
|
void HandleConnectTimer (const boost::system::error_code& ecode);
|
2014-04-07 19:31:38 +00:00
|
|
|
void ProcessPeerTest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint);
|
2014-09-04 20:27:22 +00:00
|
|
|
void SendPeerTest (uint32_t nonce, uint32_t address, uint16_t port, const uint8_t * introKey, bool toAddress = true);
|
2014-04-07 19:31:38 +00:00
|
|
|
void ProcessData (uint8_t * buf, size_t len);
|
2014-02-09 02:06:40 +00:00
|
|
|
void SendSesionDestroyed ();
|
2014-04-07 19:31:38 +00:00
|
|
|
void Send (uint8_t type, const uint8_t * payload, size_t len); // with session key
|
2014-06-10 14:39:29 +00:00
|
|
|
void Send (const uint8_t * buf, size_t size);
|
2014-02-09 02:06:40 +00:00
|
|
|
|
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);
|
2014-06-01 23:51:27 +00:00
|
|
|
void FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len); // with session key
|
2014-05-15 02:49:22 +00:00
|
|
|
void Decrypt (uint8_t * buf, size_t len, const uint8_t * aesKey);
|
|
|
|
void DecryptSessionKey (uint8_t * buf, size_t len);
|
2014-02-09 13:52:56 +00:00
|
|
|
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
|
|
|
|
2014-04-08 01:40:28 +00:00
|
|
|
void ScheduleTermination ();
|
|
|
|
void HandleTerminationTimer (const boost::system::error_code& ecode);
|
2014-09-05 20:35:02 +00:00
|
|
|
|
2014-01-24 21:30:07 +00:00
|
|
|
private:
|
2014-07-22 02:13:57 +00:00
|
|
|
|
2014-04-22 15:39:26 +00:00
|
|
|
friend class SSUData; // TODO: change in later
|
2014-04-02 14:49:16 +00:00
|
|
|
SSUServer& m_Server;
|
2014-01-28 21:49:54 +00:00
|
|
|
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
|
2014-02-09 13:52:56 +00:00
|
|
|
const i2p::data::RouterInfo * m_RemoteRouter;
|
2014-04-02 14:49:16 +00:00
|
|
|
boost::asio::deadline_timer m_Timer;
|
2014-04-04 18:56:46 +00:00
|
|
|
i2p::data::DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
|
2014-09-07 00:43:20 +00:00
|
|
|
bool m_PeerTest;
|
2014-04-08 19:35:08 +00:00
|
|
|
SessionState m_State;
|
2014-04-13 20:59:54 +00:00
|
|
|
bool m_IsSessionKey;
|
2014-04-08 19:35:08 +00:00
|
|
|
uint32_t m_RelayTag;
|
2014-04-09 18:58:30 +00:00
|
|
|
std::set<uint32_t> m_PeerTestNonces;
|
2014-06-01 23:51:27 +00:00
|
|
|
i2p::crypto::CBCEncryption m_SessionKeyEncryption;
|
2014-05-15 02:49:22 +00:00
|
|
|
i2p::crypto::CBCDecryption m_SessionKeyDecryption;
|
2014-02-03 19:40:38 +00:00
|
|
|
uint8_t m_SessionKey[32], m_MacKey[32];
|
2014-02-09 23:28:34 +00:00
|
|
|
std::list<i2p::I2NPMessage *> m_DelayedMessages;
|
2014-04-22 15:39:26 +00:00
|
|
|
SSUData m_Data;
|
2014-07-20 21:12:36 +00:00
|
|
|
size_t m_NumSentBytes, m_NumReceivedBytes;
|
2014-09-09 17:45:12 +00:00
|
|
|
uint32_t m_CreationTime; // seconds since epoch
|
2014-01-24 21:30:07 +00:00
|
|
|
};
|
|
|
|
|
2014-01-23 21:10:33 +00:00
|
|
|
class SSUServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-04-20 00:45:41 +00:00
|
|
|
SSUServer (int port);
|
2014-01-24 21:30:07 +00:00
|
|
|
~SSUServer ();
|
2014-01-23 21:10:33 +00:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2014-04-07 23:28:06 +00:00
|
|
|
SSUSession * GetSession (const i2p::data::RouterInfo * router, bool peerTest = false);
|
2014-03-26 01:17:03 +00:00
|
|
|
SSUSession * FindSession (const i2p::data::RouterInfo * router);
|
2014-04-16 19:54:28 +00:00
|
|
|
SSUSession * FindSession (const boost::asio::ip::udp::endpoint& e);
|
2014-09-05 20:35:02 +00:00
|
|
|
SSUSession * GetRandomEstablishedSession (const SSUSession * excluded);
|
2014-02-09 02:06:40 +00:00
|
|
|
void DeleteSession (SSUSession * session);
|
2014-02-21 21:13:36 +00:00
|
|
|
void DeleteAllSessions ();
|
|
|
|
|
2014-03-31 02:55:03 +00:00
|
|
|
boost::asio::io_service& GetService () { return m_Socket.get_io_service(); };
|
2014-01-30 19:03:11 +00:00
|
|
|
const boost::asio::ip::udp::endpoint& GetEndpoint () const { return m_Endpoint; };
|
2014-06-10 14:39:29 +00:00
|
|
|
void Send (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to);
|
2014-04-16 19:54:28 +00:00
|
|
|
void AddRelay (uint32_t tag, const boost::asio::ip::udp::endpoint& relay);
|
|
|
|
SSUSession * FindRelaySession (uint32_t tag);
|
2014-01-23 21:10:33 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-04-20 00:45:41 +00:00
|
|
|
void Run ();
|
2014-01-23 21:10:33 +00:00
|
|
|
void Receive ();
|
2014-09-07 00:43:20 +00:00
|
|
|
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
2014-01-23 21:10:33 +00:00
|
|
|
|
2014-09-03 20:49:48 +00:00
|
|
|
template<typename Filter>
|
|
|
|
SSUSession * GetRandomSession (Filter filter);
|
2014-09-07 00:43:20 +00:00
|
|
|
|
|
|
|
std::set<SSUSession *> FindIntroducers (int maxNumIntroducers);
|
|
|
|
void ScheduleIntroducersUpdateTimer ();
|
|
|
|
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode);
|
|
|
|
|
2014-01-23 21:10:33 +00:00
|
|
|
private:
|
2014-04-20 00:45:41 +00:00
|
|
|
|
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
|
|
|
boost::asio::io_service m_Service;
|
|
|
|
boost::asio::io_service::work m_Work;
|
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-09-07 00:43:20 +00:00
|
|
|
boost::asio::deadline_timer m_IntroducersUpdateTimer;
|
|
|
|
std::list<boost::asio::ip::udp::endpoint> m_Introducers; // introducers we are connected to
|
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-04-16 19:54:28 +00:00
|
|
|
std::map<uint32_t, boost::asio::ip::udp::endpoint> m_Relays; // we are introducer
|
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
|
|
|
|
|