2014-10-05 08:54:59 -04:00
|
|
|
#ifndef DESTINATION_H__
|
|
|
|
#define DESTINATION_H__
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
2014-11-23 11:33:58 -05:00
|
|
|
#include <memory>
|
2014-11-30 10:51:22 -05:00
|
|
|
#include <map>
|
2014-12-25 16:47:15 -05:00
|
|
|
#include <set>
|
2014-11-30 10:51:22 -05:00
|
|
|
#include <string>
|
2014-12-26 19:09:44 -05:00
|
|
|
#include <functional>
|
2016-08-30 13:27:57 -04:00
|
|
|
#include <future>
|
2014-12-25 16:47:15 -05:00
|
|
|
#include <boost/asio.hpp>
|
2014-10-05 08:54:59 -04:00
|
|
|
#include "Identity.h"
|
|
|
|
#include "TunnelPool.h"
|
2015-11-03 09:15:49 -05:00
|
|
|
#include "Crypto.h"
|
2014-10-12 16:22:14 -04:00
|
|
|
#include "LeaseSet.h"
|
2014-10-06 20:18:18 -04:00
|
|
|
#include "Garlic.h"
|
2014-12-25 16:47:15 -05:00
|
|
|
#include "NetDb.h"
|
2014-10-05 08:54:59 -04:00
|
|
|
#include "Streaming.h"
|
2014-10-22 15:30:25 -04:00
|
|
|
#include "Datagram.h"
|
2014-10-05 08:54:59 -04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-16 12:37:39 -04:00
|
|
|
namespace client
|
2014-10-05 08:54:59 -04:00
|
|
|
{
|
2014-10-22 14:01:23 -04:00
|
|
|
const uint8_t PROTOCOL_TYPE_STREAMING = 6;
|
|
|
|
const uint8_t PROTOCOL_TYPE_DATAGRAM = 17;
|
|
|
|
const uint8_t PROTOCOL_TYPE_RAW = 18;
|
2014-11-28 13:01:35 -05:00
|
|
|
const int PUBLISH_CONFIRMATION_TIMEOUT = 5; // in seconds
|
2016-02-11 14:45:33 -05:00
|
|
|
const int PUBLISH_VERIFICATION_TIMEOUT = 10; // in seconds after successfull publish
|
|
|
|
const int PUBLISH_REGULAR_VERIFICATION_INTERNAL = 100; // in seconds periodically
|
2014-12-25 16:47:15 -05:00
|
|
|
const int LEASESET_REQUEST_TIMEOUT = 5; // in seconds
|
|
|
|
const int MAX_LEASESET_REQUEST_TIMEOUT = 40; // in seconds
|
2016-01-25 22:10:06 -05:00
|
|
|
const int DESTINATION_CLEANUP_TIMEOUT = 3; // in minutes
|
2015-12-17 07:58:09 +00:00
|
|
|
const unsigned int MAX_NUM_FLOODFILLS_PER_REQUEST = 7;
|
2014-12-25 16:47:15 -05:00
|
|
|
|
2014-11-30 10:51:22 -05:00
|
|
|
// 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 12:32:13 -04: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 15:32:55 -04:00
|
|
|
const char I2CP_PARAM_EXPLICIT_PEERS[] = "explicitPeers";
|
2015-01-02 00:56:26 +01:00
|
|
|
const int STREAM_REQUEST_TIMEOUT = 60; //in seconds
|
2016-01-23 20:52:21 -05:00
|
|
|
const char I2CP_PARAM_TAGS_TO_SEND[] = "crypto.tagsToSend";
|
|
|
|
const int DEFAULT_TAGS_TO_SEND = 40;
|
2016-02-29 14:44:15 -05:00
|
|
|
|
2015-01-07 20:44:24 +01:00
|
|
|
typedef std::function<void (std::shared_ptr<i2p::stream::Stream> stream)> StreamRequestComplete;
|
|
|
|
|
2016-05-23 10:33:01 -04:00
|
|
|
class LeaseSetDestination: public i2p::garlic::GarlicDestination,
|
|
|
|
public std::enable_shared_from_this<LeaseSetDestination>
|
2014-10-05 08:54:59 -04:00
|
|
|
{
|
2015-04-07 12:02:25 -04:00
|
|
|
typedef std::function<void (std::shared_ptr<i2p::data::LeaseSet> leaseSet)> RequestComplete;
|
|
|
|
// leaseSet = nullptr means not found
|
2014-12-25 16:47:15 -05:00
|
|
|
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-26 19:09:44 -05:00
|
|
|
RequestComplete requestComplete;
|
2016-02-26 16:17:29 -05:00
|
|
|
std::shared_ptr<i2p::tunnel::OutboundTunnel> outboundTunnel;
|
|
|
|
std::shared_ptr<i2p::tunnel::InboundTunnel> replyTunnel;
|
2014-12-25 16:47:15 -05:00
|
|
|
};
|
|
|
|
|
2015-01-02 13:35:38 +01:00
|
|
|
|
2014-10-05 08:54:59 -04:00
|
|
|
public:
|
|
|
|
|
2016-05-23 10:33:01 -04:00
|
|
|
LeaseSetDestination (bool isPublic, const std::map<std::string, std::string> * params = nullptr);
|
|
|
|
~LeaseSetDestination ();
|
2014-10-05 08:54:59 -04:00
|
|
|
|
2016-05-23 14:31:22 -04:00
|
|
|
virtual bool Start ();
|
|
|
|
virtual bool Stop ();
|
2014-10-13 11:21:57 -04:00
|
|
|
bool IsRunning () const { return m_IsRunning; };
|
2014-12-15 22:50:11 -05:00
|
|
|
boost::asio::io_service& GetService () { return m_Service; };
|
2015-01-19 22:28:13 -05:00
|
|
|
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; };
|
2016-02-07 19:45:06 -05:00
|
|
|
bool IsReady () const { return m_LeaseSet && !m_LeaseSet->IsExpired () && m_Pool->GetOutboundTunnels ().size () > 0; };
|
2015-01-27 11:27:58 -05:00
|
|
|
std::shared_ptr<const i2p::data::LeaseSet> FindLeaseSet (const i2p::data::IdentHash& ident);
|
2014-12-26 19:09:44 -05:00
|
|
|
bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr);
|
2016-08-08 11:53:38 -04:00
|
|
|
void CancelDestinationRequest (const i2p::data::IdentHash& dest, bool notify = true);
|
2016-05-26 15:53:32 -04:00
|
|
|
|
2014-10-07 21:08:00 -04:00
|
|
|
// implements GarlicDestination
|
2016-05-25 15:10:28 -04:00
|
|
|
std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSet ();
|
2015-04-05 12:54:15 -04:00
|
|
|
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const { return m_Pool; }
|
2015-02-05 18:53:43 -05:00
|
|
|
void HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
|
2014-10-07 21:08:00 -04:00
|
|
|
|
2014-10-08 14:17:17 -04:00
|
|
|
// override GarlicDestination
|
2014-12-08 15:36:00 -05:00
|
|
|
bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag);
|
2015-06-16 10:14:14 -04:00
|
|
|
void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
|
|
|
|
void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
|
2014-10-08 14:17:17 -04:00
|
|
|
void SetLeaseSetUpdated ();
|
|
|
|
|
2016-05-23 14:31:22 -04:00
|
|
|
protected:
|
2014-10-16 12:37:39 -04:00
|
|
|
|
2016-05-29 09:33:50 -04:00
|
|
|
void SetLeaseSet (i2p::data::LocalLeaseSet * newLeaseSet);
|
2016-09-08 10:16:42 -04:00
|
|
|
virtual void CleanupDestination () {}; // additional clean up in derived classes
|
2016-05-23 14:31:22 -04:00
|
|
|
// I2CP
|
|
|
|
virtual void HandleDataMessage (const uint8_t * buf, size_t len) = 0;
|
2016-05-29 09:33:50 -04:00
|
|
|
virtual void CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels) = 0;
|
2016-05-23 14:31:22 -04:00
|
|
|
|
2014-10-16 12:37:39 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
void Run ();
|
2014-10-05 08:54:59 -04:00
|
|
|
void UpdateLeaseSet ();
|
2014-11-28 13:01:35 -05:00
|
|
|
void Publish ();
|
|
|
|
void HandlePublishConfirmationTimer (const boost::system::error_code& ecode);
|
2016-02-11 14:45:33 -05:00
|
|
|
void HandlePublishVerificationTimer (const boost::system::error_code& ecode);
|
2014-12-25 16:47:15 -05:00
|
|
|
void HandleDatabaseStoreMessage (const uint8_t * buf, size_t len);
|
|
|
|
void HandleDatabaseSearchReplyMessage (const uint8_t * buf, size_t len);
|
2015-06-16 10:14:14 -04:00
|
|
|
void HandleDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
|
2014-10-05 08:54:59 -04:00
|
|
|
|
2014-12-26 19:09:44 -05:00
|
|
|
void RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete);
|
2015-12-13 10:51:43 -05:00
|
|
|
bool SendLeaseSetRequest (const i2p::data::IdentHash& dest, std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, std::shared_ptr<LeaseSetRequest> request);
|
2014-12-25 16:47:15 -05:00
|
|
|
void HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest);
|
2015-01-23 12:48:25 -05:00
|
|
|
void HandleCleanupTimer (const boost::system::error_code& ecode);
|
2016-05-23 10:33:01 -04:00
|
|
|
void CleanupRemoteLeaseSets ();
|
2016-05-23 14:31:22 -04:00
|
|
|
|
2014-10-05 08:54:59 -04:00
|
|
|
private:
|
|
|
|
|
2015-01-03 15:20:11 -05:00
|
|
|
volatile bool m_IsRunning;
|
2014-10-09 10:05:28 -04:00
|
|
|
std::thread * m_Thread;
|
2014-12-15 22:50:11 -05:00
|
|
|
boost::asio::io_service m_Service;
|
|
|
|
boost::asio::io_service::work m_Work;
|
2016-07-22 09:56:17 -04:00
|
|
|
mutable std::mutex m_RemoteLeaseSetsMutex;
|
2015-01-27 11:27:58 -05:00
|
|
|
std::map<i2p::data::IdentHash, std::shared_ptr<i2p::data::LeaseSet> > m_RemoteLeaseSets;
|
2015-12-13 10:51:43 -05:00
|
|
|
std::map<i2p::data::IdentHash, std::shared_ptr<LeaseSetRequest> > m_LeaseSetRequests;
|
2014-10-16 12:37:39 -04:00
|
|
|
|
2015-01-19 22:28:13 -05:00
|
|
|
std::shared_ptr<i2p::tunnel::TunnelPool> m_Pool;
|
2016-05-25 15:10:28 -04:00
|
|
|
std::shared_ptr<i2p::data::LocalLeaseSet> m_LeaseSet;
|
2014-10-16 12:37:39 -04:00
|
|
|
bool m_IsPublic;
|
2014-11-28 13:01:35 -05:00
|
|
|
uint32_t m_PublishReplyToken;
|
2014-12-07 16:10:25 -05:00
|
|
|
std::set<i2p::data::IdentHash> m_ExcludedFloodfills; // for publishing
|
2014-10-22 11:46:54 -04:00
|
|
|
|
2016-02-11 14:45:33 -05:00
|
|
|
boost::asio::deadline_timer m_PublishConfirmationTimer, m_PublishVerificationTimer, m_CleanupTimer;
|
2014-11-28 13:01:35 -05:00
|
|
|
|
2014-10-16 12:37:39 -04:00
|
|
|
public:
|
2014-10-12 16:22:14 -04:00
|
|
|
|
2014-10-16 12:37:39 -04:00
|
|
|
// for HTTP only
|
|
|
|
int GetNumRemoteLeaseSets () const { return m_RemoteLeaseSets.size (); };
|
|
|
|
};
|
2016-05-23 10:33:01 -04:00
|
|
|
|
|
|
|
class ClientDestination: public LeaseSetDestination
|
|
|
|
{
|
|
|
|
public:
|
2016-08-30 13:27:57 -04:00
|
|
|
// type for informing that a client destination is ready
|
|
|
|
typedef std::promise<std::shared_ptr<ClientDestination> > ReadyPromise;
|
|
|
|
|
2016-05-23 10:33:01 -04:00
|
|
|
ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map<std::string, std::string> * params = nullptr);
|
2016-05-23 14:31:22 -04:00
|
|
|
~ClientDestination ();
|
|
|
|
|
|
|
|
bool Start ();
|
|
|
|
bool Stop ();
|
2016-08-30 13:27:57 -04:00
|
|
|
|
|
|
|
// informs promise with shared_from_this() when this destination is ready to use
|
|
|
|
// if cancelled before ready, informs promise with nullptr
|
|
|
|
void Ready(ReadyPromise & p);
|
|
|
|
|
2016-05-26 14:54:33 -04:00
|
|
|
const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; };
|
2016-05-29 09:33:50 -04:00
|
|
|
void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); };
|
|
|
|
|
2016-05-23 14:31:22 -04:00
|
|
|
// streaming
|
|
|
|
std::shared_ptr<i2p::stream::StreamingDestination> CreateStreamingDestination (int port, bool gzip = true); // 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);
|
|
|
|
std::shared_ptr<i2p::stream::Stream> CreateStream (std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0);
|
|
|
|
void AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor);
|
|
|
|
void StopAcceptingStreams ();
|
|
|
|
bool IsAcceptingStreams () const;
|
2016-05-29 09:33:50 -04:00
|
|
|
|
2016-05-23 14:31:22 -04:00
|
|
|
// datagram
|
2016-08-21 22:29:55 -04:00
|
|
|
i2p::datagram::DatagramDestination * GetDatagramDestination () const { return m_DatagramDestination; };
|
|
|
|
i2p::datagram::DatagramDestination * CreateDatagramDestination ();
|
2016-05-23 14:31:22 -04:00
|
|
|
|
2016-05-29 09:33:50 -04:00
|
|
|
// implements LocalDestination
|
|
|
|
const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; };
|
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> GetIdentity () const { return m_Keys.GetPublic (); };
|
2016-05-23 10:33:01 -04:00
|
|
|
|
2016-05-23 14:31:22 -04:00
|
|
|
protected:
|
|
|
|
|
2016-09-08 10:16:42 -04:00
|
|
|
void CleanupDestination ();
|
2016-05-23 14:31:22 -04:00
|
|
|
// I2CP
|
|
|
|
void HandleDataMessage (const uint8_t * buf, size_t len);
|
2016-05-29 09:33:50 -04:00
|
|
|
void CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels);
|
2016-05-23 14:31:22 -04:00
|
|
|
|
2016-05-23 10:33:01 -04:00
|
|
|
private:
|
|
|
|
|
2016-05-25 16:18:02 -04:00
|
|
|
std::shared_ptr<ClientDestination> GetSharedFromThis ()
|
2016-05-29 09:33:50 -04:00
|
|
|
{ return std::static_pointer_cast<ClientDestination>(shared_from_this ()); }
|
|
|
|
void PersistTemporaryKeys ();
|
2016-05-25 16:18:02 -04:00
|
|
|
|
2016-08-30 13:27:57 -04:00
|
|
|
void ScheduleCheckForReady(ReadyPromise * p);
|
|
|
|
void HandleCheckForReady(const boost::system::error_code & ecode, ReadyPromise * p);
|
|
|
|
|
2016-05-23 10:33:01 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
i2p::data::PrivateKeys m_Keys;
|
2016-05-29 09:33:50 -04:00
|
|
|
uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256];
|
2016-05-23 14:31:22 -04:00
|
|
|
|
|
|
|
std::shared_ptr<i2p::stream::StreamingDestination> m_StreamingDestination; // default
|
|
|
|
std::map<uint16_t, std::shared_ptr<i2p::stream::StreamingDestination> > m_StreamingDestinationsByPorts;
|
2016-08-21 22:29:55 -04:00
|
|
|
i2p::datagram::DatagramDestination * m_DatagramDestination;
|
2016-05-23 14:31:22 -04:00
|
|
|
|
2016-08-30 13:27:57 -04:00
|
|
|
boost::asio::deadline_timer m_ReadyChecker;
|
|
|
|
|
2016-05-23 14:31:22 -04:00
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP only
|
|
|
|
std::vector<std::shared_ptr<const i2p::stream::Stream> > GetAllStreams () const;
|
2016-05-23 10:33:01 -04:00
|
|
|
};
|
2014-10-22 11:46:54 -04:00
|
|
|
}
|
2014-10-05 08:54:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|