i2pd/Destination.h

132 lines
3.9 KiB
C
Raw Normal View History

#ifndef DESTINATION_H__
#define DESTINATION_H__
#include <thread>
#include <mutex>
#include "Identity.h"
#include "TunnelPool.h"
#include "CryptoConst.h"
2014-10-13 00:22:14 +04:00
#include "LeaseSet.h"
2014-10-07 04:18:18 +04:00
#include "Garlic.h"
#include "Streaming.h"
namespace i2p
{
2014-10-16 20:37:39 +04:00
namespace client
{
2014-10-16 20:37:39 +04:00
class ClientDestination: public i2p::garlic::GarlicDestination
{
public:
2014-10-16 20:37:39 +04:00
ClientDestination (bool isPublic, i2p::data::SigningKeyType sigType);
ClientDestination (const std::string& fullPath, bool isPublic);
ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic);
~ClientDestination ();
2014-10-16 20:37:39 +04:00
virtual void Start ();
virtual void Stop ();
bool IsRunning () const { return m_IsRunning; };
2014-10-16 20:37:39 +04:00
boost::asio::io_service * GetService () { return m_Service; };
i2p::tunnel::TunnelPool * GetTunnelPool () { return m_Pool; };
bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases (); };
2014-10-07 22:06:50 +04:00
void ResetCurrentOutboundTunnel () { m_CurrentOutboundTunnel = nullptr; };
2014-10-13 00:22:14 +04:00
const i2p::data::LeaseSet * FindLeaseSet (const i2p::data::IdentHash& ident);
2014-10-16 20:37:39 +04:00
void SendTunnelDataMsgs (const std::vector<i2p::tunnel::TunnelMessageBlock>& msgs);
// implements LocalDestination
const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; };
const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; };
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; };
2014-10-16 20:37:39 +04:00
2014-10-08 05:08:00 +04:00
// implements GarlicDestination
const i2p::data::LeaseSet * GetLeaseSet ();
void HandleI2NPMessage (const uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from);
2014-10-08 05:08:00 +04:00
// override GarlicDestination
void ProcessGarlicMessage (I2NPMessage * msg);
void ProcessDeliveryStatusMessage (I2NPMessage * msg);
void SetLeaseSetUpdated ();
2014-10-16 20:37:39 +04:00
// I2CP
void HandleDataMessage (const uint8_t * buf, size_t len);
I2NPMessage * CreateDataMessage (const uint8_t * payload, size_t len);
protected:
virtual void HandleNextPacket (i2p::stream::Packet * packet) = 0; // TODO
private:
void Run ();
void UpdateLeaseSet ();
2014-10-16 20:37:39 +04:00
void HandleDatabaseStoreMessage (const uint8_t * buf, size_t len);
private:
2014-10-09 18:05:28 +04:00
bool m_IsRunning;
std::thread * m_Thread;
2014-10-13 20:33:51 +04:00
boost::asio::io_service * m_Service;
boost::asio::io_service::work * m_Work;
i2p::data::PrivateKeys m_Keys;
uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256];
2014-10-16 20:37:39 +04:00
std::map<i2p::data::IdentHash, i2p::data::LeaseSet *> m_RemoteLeaseSets;
i2p::tunnel::TunnelPool * m_Pool;
i2p::tunnel::OutboundTunnel * m_CurrentOutboundTunnel;
i2p::data::LeaseSet * m_LeaseSet;
2014-10-16 20:37:39 +04:00
bool m_IsPublic;
public:
2014-10-13 00:22:14 +04:00
2014-10-16 20:37:39 +04:00
// for HTTP only
int GetNumRemoteLeaseSets () const { return m_RemoteLeaseSets.size (); };
};
}
namespace stream
{
class StreamingDestination: public i2p::client::ClientDestination
{
public:
StreamingDestination (bool isPublic, i2p::data::SigningKeyType sigType):
ClientDestination (isPublic, sigType) {};
StreamingDestination (const std::string& fullPath, bool isPublic):
ClientDestination (fullPath, isPublic) {};
StreamingDestination (const i2p::data::PrivateKeys& keys, bool isPublic):
ClientDestination (keys, isPublic) {};
~StreamingDestination () {};
void Start ();
void Stop ();
Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote);
void DeleteStream (Stream * stream);
void SetAcceptor (const std::function<void (Stream *)>& acceptor) { m_Acceptor = acceptor; };
void ResetAcceptor () { m_Acceptor = nullptr; };
bool IsAcceptorSet () const { return m_Acceptor != nullptr; };
// ClientDestination
void HandleNextPacket (Packet * packet);
private:
Stream * CreateNewIncomingStream ();
private:
std::mutex m_StreamsMutex;
std::map<uint32_t, Stream *> m_Streams;
std::function<void (Stream *)> m_Acceptor;
2014-10-13 00:22:14 +04:00
2014-10-12 02:21:52 +04:00
public:
// for HTTP only
const decltype(m_Streams)& GetStreams () const { return m_Streams; };
2014-10-16 04:52:17 +04:00
};
}
}
#endif