mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
5.1 KiB
150 lines
5.1 KiB
![]()
10 years ago
|
#ifndef DATAGRAM_H__
|
||
|
#define DATAGRAM_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
![]()
10 years ago
|
#include <memory>
|
||
![]()
10 years ago
|
#include <functional>
|
||
![]()
10 years ago
|
#include <map>
|
||
![]()
9 years ago
|
#include "Base.h"
|
||
![]()
10 years ago
|
#include "Identity.h"
|
||
![]()
10 years ago
|
#include "LeaseSet.h"
|
||
|
#include "I2NPProtocol.h"
|
||
![]()
8 years ago
|
#include "Garlic.h"
|
||
![]()
10 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace client
|
||
|
{
|
||
![]()
8 years ago
|
class ClientDestination;
|
||
![]()
10 years ago
|
}
|
||
|
namespace datagram
|
||
|
{
|
||
![]()
8 years ago
|
// milliseconds for max session idle time
|
||
![]()
8 years ago
|
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
|
||
![]()
8 years ago
|
const uint64_t DATAGRAM_SESSION_PATH_TIMEOUT = 10 * 1000;
|
||
![]()
8 years ago
|
// milliseconds interval a routing path is used before switching
|
||
![]()
8 years ago
|
const uint64_t DATAGRAM_SESSION_PATH_SWITCH_INTERVAL = 20 * 60 * 1000;
|
||
![]()
8 years ago
|
// milliseconds before lease expire should we try switching leases
|
||
![]()
8 years ago
|
const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW = 30 * 1000;
|
||
![]()
8 years ago
|
// milliseconds fudge factor for leases handover
|
||
|
const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_FUDGE = 1000;
|
||
![]()
8 years ago
|
// milliseconds minimum time between path switches
|
||
|
const uint64_t DATAGRAM_SESSION_PATH_MIN_LIFETIME = 5 * 1000;
|
||
![]()
8 years ago
|
// max 64 messages buffered in send queue for each datagram session
|
||
|
const size_t DATAGRAM_SEND_QUEUE_MAX_SIZE = 64;
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
class DatagramSession : public std::enable_shared_from_this<DatagramSession>
|
||
![]()
8 years ago
|
{
|
||
|
public:
|
||
![]()
8 years ago
|
DatagramSession(i2p::client::ClientDestination * localDestination, const i2p::data::IdentHash & remoteIdent);
|
||
|
|
||
|
void Start ();
|
||
![]()
8 years ago
|
void Stop ();
|
||
![]()
8 years ago
|
|
||
|
|
||
|
/** @brief ack the garlic routing path */
|
||
|
void Ack();
|
||
![]()
8 years ago
|
|
||
|
/** 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; }
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
struct Info
|
||
|
{
|
||
![]()
8 years ago
|
std::shared_ptr<const i2p::data::IdentHash> IBGW;
|
||
|
std::shared_ptr<const i2p::data::IdentHash> OBEP;
|
||
![]()
8 years ago
|
const uint64_t activity;
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
Info() : IBGW(nullptr), OBEP(nullptr), activity(0) {}
|
||
|
Info(const uint8_t * ibgw, const uint8_t * obep, const uint64_t a) :
|
||
|
activity(a) {
|
||
![]()
8 years ago
|
if(ibgw) IBGW = std::make_shared<i2p::data::IdentHash>(ibgw);
|
||
![]()
8 years ago
|
else IBGW = nullptr;
|
||
![]()
8 years ago
|
if(obep) OBEP = std::make_shared<i2p::data::IdentHash>(obep);
|
||
![]()
8 years ago
|
else OBEP = nullptr;
|
||
|
}
|
||
![]()
8 years ago
|
};
|
||
|
|
||
|
Info GetSessionInfo() const;
|
||
|
|
||
![]()
8 years ago
|
private:
|
||
|
|
||
![]()
8 years ago
|
void FlushSendQueue();
|
||
|
void ScheduleFlushSendQueue();
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void HandleSend(std::shared_ptr<I2NPMessage> msg);
|
||
|
|
||
|
std::shared_ptr<i2p::garlic::GarlicRoutingPath> GetSharedRoutingPath();
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void HandleLeaseSetUpdated(std::shared_ptr<i2p::data::LeaseSet> ls);
|
||
![]()
8 years ago
|
|
||
|
private:
|
||
|
i2p::client::ClientDestination * m_LocalDestination;
|
||
![]()
8 years ago
|
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;
|
||
![]()
8 years ago
|
bool m_RequestingLS;
|
||
![]()
8 years ago
|
};
|
||
![]()
8 years ago
|
|
||
|
typedef std::shared_ptr<DatagramSession> DatagramSession_ptr;
|
||
|
|
||
![]()
8 years ago
|
const size_t MAX_DATAGRAM_SIZE = 32768;
|
||
![]()
10 years ago
|
class DatagramDestination
|
||
|
{
|
||
![]()
10 years ago
|
typedef std::function<void (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)> Receiver;
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
public:
|
||
|
|
||
![]()
8 years ago
|
|
||
|
DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner);
|
||
|
~DatagramDestination ();
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
void SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash & ident, uint16_t fromPort = 0, uint16_t toPort = 0);
|
||
![]()
10 years ago
|
void HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; };
|
||
|
void ResetReceiver () { m_Receiver = nullptr; };
|
||
|
|
||
![]()
8 years ago
|
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); };
|
||
![]()
8 years ago
|
|
||
|
std::shared_ptr<DatagramSession::Info> GetInfoForRemote(const i2p::data::IdentHash & remote);
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
// clean up stale sessions
|
||
|
void CleanUp ();
|
||
|
|
||
![]()
10 years ago
|
private:
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
std::shared_ptr<DatagramSession> ObtainSession(const i2p::data::IdentHash & ident);
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
std::shared_ptr<I2NPMessage> CreateDataMessage (const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort);
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void HandleDatagram (uint16_t fromPort, uint16_t toPort, uint8_t *const& buf, size_t len);
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
/** find a receiver by port, if none by port is found try default receiever, otherwise returns nullptr */
|
||
|
Receiver FindReceiver(uint16_t port);
|
||
![]()
8 years ago
|
|
||
![]()
10 years ago
|
private:
|
||
![]()
8 years ago
|
i2p::client::ClientDestination * m_Owner;
|
||
![]()
8 years ago
|
i2p::data::IdentityEx m_Identity;
|
||
![]()
10 years ago
|
Receiver m_Receiver; // default
|
||
![]()
8 years ago
|
std::mutex m_SessionsMutex;
|
||
![]()
8 years ago
|
std::map<i2p::data::IdentHash, DatagramSession_ptr > m_Sessions;
|
||
![]()
8 years ago
|
std::mutex m_ReceiversMutex;
|
||
![]()
10 years ago
|
std::map<uint16_t, Receiver> m_ReceiversByPorts;
|
||
![]()
9 years ago
|
|
||
|
i2p::data::GzipInflator m_Inflator;
|
||
|
i2p::data::GzipDeflator m_Deflator;
|
||
![]()
8 years ago
|
};
|
||
![]()
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|