i2pd/Datagram.h

150 lines
5.1 KiB
C
Raw Normal View History

2014-10-22 23:30:25 +04:00
#ifndef DATAGRAM_H__
#define DATAGRAM_H__
#include <inttypes.h>
2015-01-29 05:37:08 +03:00
#include <memory>
2014-10-31 23:44:44 +03:00
#include <functional>
2015-04-04 03:34:37 +03:00
#include <map>
2015-11-03 17:15:49 +03:00
#include "Base.h"
2014-10-31 23:44:44 +03:00
#include "Identity.h"
2014-10-24 00:56:50 +04:00
#include "LeaseSet.h"
#include "I2NPProtocol.h"
#include "Garlic.h"
2014-10-22 23:30:25 +04:00
namespace i2p
{
namespace client
{
2016-05-25 23:18:02 +03:00
class ClientDestination;
2014-10-22 23:30:25 +04:00
}
namespace datagram
{
// milliseconds for max session idle time
const uint64_t DATAGRAM_SESSION_MAX_IDLE = 10 * 60 * 1000;
// milliseconds for how long we try sticking to a dead routing path before trying to switch
2016-10-08 18:58:26 +03:00
const uint64_t DATAGRAM_SESSION_PATH_TIMEOUT = 10 * 1000;
// milliseconds interval a routing path is used before switching
2016-10-06 20:41:18 +03:00
const uint64_t DATAGRAM_SESSION_PATH_SWITCH_INTERVAL = 20 * 60 * 1000;
// milliseconds before lease expire should we try switching leases
const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW = 10 * 1000;
// milliseconds fudge factor for leases handover
const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_FUDGE = 1000;
2016-10-10 15:30:33 +03:00
// milliseconds minimum time between path switches
const uint64_t DATAGRAM_SESSION_PATH_MIN_LIFETIME = 5 * 1000;
2016-12-12 21:40:24 +03:00
// max 64 messages buffered in send queue for each datagram session
const size_t DATAGRAM_SEND_QUEUE_MAX_SIZE = 64;
2017-01-16 15:54:56 +03:00
class DatagramSession : public std::enable_shared_from_this<DatagramSession>
{
public:
2017-01-17 20:13:56 +03:00
DatagramSession(i2p::client::ClientDestination * localDestination, const i2p::data::IdentHash & remoteIdent);
void Start ();
void Stop ();
2016-12-12 21:40:24 +03:00
/** @brief ack the garlic routing path */
void Ack();
/** send an i2np message to remote endpoint for this session */
void SendMsg(std::shared_ptr<I2NPMessage> msg);
/** get the last time in milliseconds for when we used this datagram session */
uint64_t LastActivity() const { return m_LastUse; }
2016-12-12 21:40:24 +03:00
2016-09-03 20:58:34 +03:00
struct Info
{
2016-09-03 23:54:39 +03:00
std::shared_ptr<const i2p::data::IdentHash> IBGW;
std::shared_ptr<const i2p::data::IdentHash> OBEP;
2016-09-03 20:58:34 +03:00
const uint64_t activity;
2016-12-12 21:40:24 +03:00
Info() : IBGW(nullptr), OBEP(nullptr), activity(0) {}
Info(const uint8_t * ibgw, const uint8_t * obep, const uint64_t a) :
activity(a) {
2016-09-03 23:54:39 +03:00
if(ibgw) IBGW = std::make_shared<i2p::data::IdentHash>(ibgw);
2016-09-03 23:43:02 +03:00
else IBGW = nullptr;
2016-09-03 23:54:39 +03:00
if(obep) OBEP = std::make_shared<i2p::data::IdentHash>(obep);
2016-09-03 23:43:02 +03:00
else OBEP = nullptr;
}
2016-09-03 20:58:34 +03:00
};
Info GetSessionInfo() const;
private:
2016-12-12 21:40:24 +03:00
void FlushSendQueue();
void ScheduleFlushSendQueue();
2016-12-12 21:40:24 +03:00
void HandleSend(std::shared_ptr<I2NPMessage> msg);
std::shared_ptr<i2p::garlic::GarlicRoutingPath> GetSharedRoutingPath();
2016-12-12 21:40:24 +03:00
void HandleLeaseSetUpdated(std::shared_ptr<i2p::data::LeaseSet> ls);
private:
i2p::client::ClientDestination * m_LocalDestination;
2016-12-12 21:40:24 +03:00
i2p::data::IdentHash m_RemoteIdent;
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
std::shared_ptr<const i2p::data::Lease> m_CurrentRemoteLease;
std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel;
boost::asio::deadline_timer m_SendQueueTimer;
std::vector<std::shared_ptr<I2NPMessage> > m_SendQueue;
uint64_t m_LastUse;
2016-12-27 02:47:47 +03:00
bool m_RequestingLS;
};
2017-01-16 15:54:56 +03:00
typedef std::shared_ptr<DatagramSession> DatagramSession_ptr;
const size_t MAX_DATAGRAM_SIZE = 32768;
2014-10-22 23:30:25 +04:00
class DatagramDestination
{
typedef std::function<void (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)> Receiver;
2014-10-31 23:44:44 +03:00
2014-10-22 23:30:25 +04:00
public:
DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner);
~DatagramDestination ();
2014-10-22 23:30:25 +04:00
2016-12-12 21:40:24 +03:00
void SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash & ident, uint16_t fromPort = 0, uint16_t toPort = 0);
2015-03-02 05:08:34 +03:00
void HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
2014-10-22 23:30:25 +04:00
2014-10-31 23:44:44 +03:00
void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; };
void ResetReceiver () { m_Receiver = nullptr; };
void SetReceiver (const Receiver& receiver, uint16_t port) { std::lock_guard<std::mutex> lock(m_ReceiversMutex); m_ReceiversByPorts[port] = receiver; };
void ResetReceiver (uint16_t port) { std::lock_guard<std::mutex> lock(m_ReceiversMutex); m_ReceiversByPorts.erase (port); };
2016-09-03 20:58:34 +03:00
std::shared_ptr<DatagramSession::Info> GetInfoForRemote(const i2p::data::IdentHash & remote);
// clean up stale sessions
void CleanUp ();
2014-10-24 00:56:50 +04:00
private:
2016-12-12 21:40:24 +03:00
std::shared_ptr<DatagramSession> ObtainSession(const i2p::data::IdentHash & ident);
2015-11-24 21:09:12 +03:00
std::shared_ptr<I2NPMessage> CreateDataMessage (const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort);
2016-10-09 17:55:55 +03:00
void HandleDatagram (uint16_t fromPort, uint16_t toPort, uint8_t *const& buf, size_t len);
2014-10-24 00:56:50 +04:00
/** find a receiver by port, if none by port is found try default receiever, otherwise returns nullptr */
Receiver FindReceiver(uint16_t port);
2014-10-22 23:30:25 +04:00
private:
2016-08-22 05:54:06 +03:00
i2p::client::ClientDestination * m_Owner;
2016-10-09 17:55:55 +03:00
i2p::data::IdentityEx m_Identity;
2015-04-04 03:34:37 +03:00
Receiver m_Receiver; // default
std::mutex m_SessionsMutex;
2017-01-16 15:54:56 +03:00
std::map<i2p::data::IdentHash, DatagramSession_ptr > m_Sessions;
std::mutex m_ReceiversMutex;
2015-04-04 03:34:37 +03:00
std::map<uint16_t, Receiver> m_ReceiversByPorts;
2015-11-03 17:15:49 +03:00
i2p::data::GzipInflator m_Inflator;
i2p::data::GzipDeflator m_Deflator;
};
2014-10-22 23:30:25 +04:00
}
}
#endif