i2pd/Destination.h

162 lines
6.7 KiB
C
Raw Normal View History

#ifndef DESTINATION_H__
#define DESTINATION_H__
#include <thread>
#include <mutex>
2014-11-23 16:33:58 +00:00
#include <memory>
#include <map>
#include <set>
#include <string>
2014-12-27 00:09:44 +00:00
#include <functional>
#include <boost/asio.hpp>
#include "Identity.h"
#include "TunnelPool.h"
2015-11-03 14:15:49 +00:00
#include "Crypto.h"
2014-10-12 20:22:14 +00:00
#include "LeaseSet.h"
2014-10-07 00:18:18 +00:00
#include "Garlic.h"
#include "NetDb.h"
#include "Streaming.h"
2014-10-22 19:30:25 +00:00
#include "Datagram.h"
namespace i2p
{
2014-10-16 16:37:39 +00:00
namespace client
{
const uint8_t PROTOCOL_TYPE_STREAMING = 6;
const uint8_t PROTOCOL_TYPE_DATAGRAM = 17;
const uint8_t PROTOCOL_TYPE_RAW = 18;
2014-11-28 18:01:35 +00:00
const int PUBLISH_CONFIRMATION_TIMEOUT = 5; // in seconds
const int LEASESET_REQUEST_TIMEOUT = 5; // in seconds
const int MAX_LEASESET_REQUEST_TIMEOUT = 40; // in seconds
2015-01-23 17:48:25 +00:00
const int DESTINATION_CLEANUP_TIMEOUT = 20; // in minutes
2015-12-17 07:58:09 +00:00
const unsigned int MAX_NUM_FLOODFILLS_PER_REQUEST = 7;
// I2CP
const char I2CP_PARAM_INBOUND_TUNNEL_LENGTH[] = "inbound.length";
const int DEFAULT_INBOUND_TUNNEL_LENGTH = 3;
const char I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH[] = "outbound.length";
const int DEFAULT_OUTBOUND_TUNNEL_LENGTH = 3;
2015-05-05 16:32:13 +00:00
const char I2CP_PARAM_INBOUND_TUNNELS_QUANTITY[] = "inbound.quantity";
const int DEFAULT_INBOUND_TUNNELS_QUANTITY = 5;
const char I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY[] = "outbound.quantity";
const int DEFAULT_OUTBOUND_TUNNELS_QUANTITY = 5;
2015-06-10 19:32:55 +00:00
const char I2CP_PARAM_EXPLICIT_PEERS[] = "explicitPeers";
const int STREAM_REQUEST_TIMEOUT = 60; //in seconds
const char I2CP_PARAM_TAGS_TO_SEND[] = "crypto.tagsToSend";
const int DEFAULT_TAGS_TO_SEND = 40;
2015-01-07 19:44:24 +00:00
typedef std::function<void (std::shared_ptr<i2p::stream::Stream> stream)> StreamRequestComplete;
2015-11-03 14:15:49 +00:00
class ClientDestination: public i2p::garlic::GarlicDestination,
public std::enable_shared_from_this<ClientDestination>
{
typedef std::function<void (std::shared_ptr<i2p::data::LeaseSet> leaseSet)> RequestComplete;
// leaseSet = nullptr means not found
struct LeaseSetRequest
{
LeaseSetRequest (boost::asio::io_service& service): requestTime (0), requestTimeoutTimer (service) {};
std::set<i2p::data::IdentHash> excluded;
uint64_t requestTime;
boost::asio::deadline_timer requestTimeoutTimer;
2014-12-27 00:09:44 +00:00
RequestComplete requestComplete;
};
public:
ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map<std::string, std::string> * params = nullptr);
2014-10-16 16:37:39 +00:00
~ClientDestination ();
2014-10-16 16:37:39 +00:00
virtual void Start ();
virtual void Stop ();
bool IsRunning () const { return m_IsRunning; };
boost::asio::io_service& GetService () { return m_Service; };
2015-01-20 03:28:13 +00:00
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; };
2015-04-15 22:25:05 +00:00
bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases () && m_Pool->GetOutboundTunnels ().size () > 0; };
2015-01-27 16:27:58 +00:00
std::shared_ptr<const i2p::data::LeaseSet> FindLeaseSet (const i2p::data::IdentHash& ident);
2014-12-27 00:09:44 +00:00
bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr);
2015-12-13 19:40:43 +00:00
void CancelDestinationRequest (const i2p::data::IdentHash& dest);
// streaming
std::shared_ptr<i2p::stream::StreamingDestination> CreateStreamingDestination (int port); // additional
std::shared_ptr<i2p::stream::StreamingDestination> GetStreamingDestination (int port = 0) const;
// following methods operate with default streaming destination
void CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port = 0);
2015-01-27 16:27:58 +00:00
std::shared_ptr<i2p::stream::Stream> CreateStream (std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0);
2014-11-18 14:33:58 +00:00
void AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor);
void StopAcceptingStreams ();
bool IsAcceptingStreams () const;
// datagram
i2p::datagram::DatagramDestination * GetDatagramDestination () const { return m_DatagramDestination; };
2014-10-31 20:44:44 +00:00
i2p::datagram::DatagramDestination * CreateDatagramDestination ();
// 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 16:37:39 +00:00
2014-10-08 01:08:00 +00:00
// implements GarlicDestination
2015-04-07 19:02:00 +00:00
std::shared_ptr<const i2p::data::LeaseSet> GetLeaseSet ();
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const { return m_Pool; }
2015-02-05 23:53:43 +00:00
void HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
2014-10-08 01:08:00 +00:00
// override GarlicDestination
bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag);
2015-06-16 14:14:14 +00:00
void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
void SetLeaseSetUpdated ();
2014-10-16 16:37:39 +00:00
// I2CP
void HandleDataMessage (const uint8_t * buf, size_t len);
private:
void Run ();
void UpdateLeaseSet ();
2014-11-28 18:01:35 +00:00
void Publish ();
void HandlePublishConfirmationTimer (const boost::system::error_code& ecode);
void HandleDatabaseStoreMessage (const uint8_t * buf, size_t len);
void HandleDatabaseSearchReplyMessage (const uint8_t * buf, size_t len);
2015-06-16 14:14:14 +00:00
void HandleDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
2014-12-27 00:09:44 +00:00
void RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete);
2015-12-13 15:51:43 +00:00
bool SendLeaseSetRequest (const i2p::data::IdentHash& dest, std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, std::shared_ptr<LeaseSetRequest> request);
void HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest);
2015-01-23 17:48:25 +00:00
void HandleCleanupTimer (const boost::system::error_code& ecode);
2015-01-28 19:20:28 +00:00
void CleanupRemoteLeaseSets ();
2015-12-21 15:17:00 +00:00
void PersistTemporaryKeys ();
private:
2015-01-03 20:20:11 +00:00
volatile bool m_IsRunning;
2014-10-09 14:05:28 +00:00
std::thread * m_Thread;
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];
2015-01-27 16:27:58 +00:00
std::map<i2p::data::IdentHash, std::shared_ptr<i2p::data::LeaseSet> > m_RemoteLeaseSets;
2015-12-13 15:51:43 +00:00
std::map<i2p::data::IdentHash, std::shared_ptr<LeaseSetRequest> > m_LeaseSetRequests;
2014-10-16 16:37:39 +00:00
2015-01-20 03:28:13 +00:00
std::shared_ptr<i2p::tunnel::TunnelPool> m_Pool;
2015-04-07 19:02:00 +00:00
std::shared_ptr<i2p::data::LeaseSet> m_LeaseSet;
2014-10-16 16:37:39 +00:00
bool m_IsPublic;
2014-11-28 18:01:35 +00:00
uint32_t m_PublishReplyToken;
std::set<i2p::data::IdentHash> m_ExcludedFloodfills; // for publishing
2014-11-28 18:01:35 +00:00
std::shared_ptr<i2p::stream::StreamingDestination> m_StreamingDestination; // default
std::map<uint16_t, std::shared_ptr<i2p::stream::StreamingDestination> > m_StreamingDestinationsByPorts;
2014-10-22 19:30:25 +00:00
i2p::datagram::DatagramDestination * m_DatagramDestination;
2015-01-23 17:48:25 +00:00
boost::asio::deadline_timer m_PublishConfirmationTimer, m_CleanupTimer;
2014-11-28 18:01:35 +00:00
2014-10-16 16:37:39 +00:00
public:
2014-10-12 20:22:14 +00:00
2014-10-16 16:37:39 +00:00
// for HTTP only
int GetNumRemoteLeaseSets () const { return m_RemoteLeaseSets.size (); };
};
}
}
#endif