2014-10-22 19:30:25 +00:00
|
|
|
#ifndef DATAGRAM_H__
|
|
|
|
#define DATAGRAM_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2015-01-29 02:37:08 +00:00
|
|
|
#include <memory>
|
2014-10-31 20:44:44 +00:00
|
|
|
#include <functional>
|
2015-04-04 00:34:37 +00:00
|
|
|
#include <map>
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Base.h"
|
2014-10-31 20:44:44 +00:00
|
|
|
#include "Identity.h"
|
2014-10-23 20:56:50 +00:00
|
|
|
#include "LeaseSet.h"
|
|
|
|
#include "I2NPProtocol.h"
|
2016-08-27 17:17:34 +00:00
|
|
|
#include "Garlic.h"
|
2014-10-22 19:30:25 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
2016-05-25 20:18:02 +00:00
|
|
|
class ClientDestination;
|
2014-10-22 19:30:25 +00:00
|
|
|
}
|
|
|
|
namespace datagram
|
|
|
|
{
|
2016-09-03 15:46:47 +00:00
|
|
|
// 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
|
|
|
|
const uint64_t DATAGRAM_SESSION_PATH_TIMEOUT = 5000;
|
|
|
|
// milliseconds interval a routing path is used before switching
|
|
|
|
const uint64_t DATAGRAM_SESSION_PATH_SWITCH_INTERVAL = 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;
|
|
|
|
|
|
|
|
|
|
|
|
class DatagramSession
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DatagramSession(i2p::client::ClientDestination * localDestination,
|
|
|
|
const i2p::data::IdentHash & remoteIdent);
|
|
|
|
|
|
|
|
/** 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-09-03 17:58:34 +00:00
|
|
|
/** get the last time in milliseconds when we successfully sent data */
|
|
|
|
uint64_t LastSuccess() const { return m_LastSuccess; }
|
|
|
|
struct Info
|
|
|
|
{
|
2016-09-03 20:54:39 +00:00
|
|
|
std::shared_ptr<const i2p::data::IdentHash> IBGW;
|
|
|
|
std::shared_ptr<const i2p::data::IdentHash> OBEP;
|
2016-09-03 17:58:34 +00:00
|
|
|
const uint64_t activity;
|
|
|
|
const uint64_t success;
|
2016-09-03 20:29:50 +00:00
|
|
|
Info() : IBGW(nullptr), OBEP(nullptr), activity(0), success(0) {}
|
2016-09-03 20:43:02 +00:00
|
|
|
Info(const uint8_t * ibgw, const uint8_t * obep, const uint64_t a, const uint64_t s) :
|
2016-09-03 20:29:50 +00:00
|
|
|
activity(a),
|
2016-09-03 20:43:02 +00:00
|
|
|
success(s) {
|
2016-09-03 20:54:39 +00:00
|
|
|
if(ibgw) IBGW = std::make_shared<i2p::data::IdentHash>(ibgw);
|
2016-09-03 20:43:02 +00:00
|
|
|
else IBGW = nullptr;
|
2016-09-03 20:54:39 +00:00
|
|
|
if(obep) OBEP = std::make_shared<i2p::data::IdentHash>(obep);
|
2016-09-03 20:43:02 +00:00
|
|
|
else OBEP = nullptr;
|
|
|
|
}
|
2016-09-03 17:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Info GetSessionInfo() const;
|
|
|
|
|
|
|
|
|
2016-09-03 15:46:47 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
/** update our routing path we are using, mark that we have changed paths */
|
|
|
|
void UpdateRoutingPath(const std::shared_ptr<i2p::garlic::GarlicRoutingPath> & path);
|
|
|
|
|
|
|
|
/** return true if we should switch routing paths because of path lifetime or timeout otherwise false */
|
|
|
|
bool ShouldUpdateRoutingPath() const;
|
|
|
|
|
|
|
|
/** return true if we should switch the lease for out routing path otherwise return false */
|
|
|
|
bool ShouldSwitchLease() const;
|
|
|
|
|
|
|
|
/** get next usable routing path, try reusing outbound tunnels */
|
|
|
|
std::shared_ptr<i2p::garlic::GarlicRoutingPath> GetNextRoutingPath();
|
|
|
|
/**
|
|
|
|
* mark current routing path as invalid and clear it
|
|
|
|
* if the outbound tunnel we were using was okay don't use the IBGW in the routing path's lease next time
|
|
|
|
*/
|
|
|
|
void ResetRoutingPath();
|
|
|
|
|
|
|
|
/** get next usable lease, does not fetch or update if expired or have no lease set */
|
|
|
|
std::shared_ptr<const i2p::data::Lease> GetNextLease();
|
|
|
|
|
|
|
|
void HandleSend(std::shared_ptr<I2NPMessage> msg);
|
|
|
|
void HandleGotLeaseSet(std::shared_ptr<const i2p::data::LeaseSet> remoteIdent,
|
|
|
|
std::shared_ptr<I2NPMessage> msg);
|
|
|
|
void UpdateLeaseSet(std::shared_ptr<I2NPMessage> msg=nullptr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
i2p::client::ClientDestination * m_LocalDestination;
|
|
|
|
i2p::data::IdentHash m_RemoteIdentity;
|
|
|
|
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
|
|
|
|
// Ident hash of IBGW that are invalid
|
|
|
|
std::vector<i2p::data::IdentHash> m_InvalidIBGW;
|
|
|
|
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
|
|
|
|
uint64_t m_LastUse;
|
|
|
|
uint64_t m_LastPathChange;
|
|
|
|
uint64_t m_LastSuccess;
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t MAX_DATAGRAM_SIZE = 32768;
|
2014-10-22 19:30:25 +00:00
|
|
|
class DatagramDestination
|
|
|
|
{
|
2015-03-03 20:31:49 +00:00
|
|
|
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 20:44:44 +00:00
|
|
|
|
2014-10-22 19:30:25 +00:00
|
|
|
public:
|
|
|
|
|
2016-09-03 17:58:34 +00:00
|
|
|
|
2016-05-25 20:18:02 +00:00
|
|
|
DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner);
|
2015-11-03 14:15:49 +00:00
|
|
|
~DatagramDestination ();
|
2014-10-22 19:30:25 +00:00
|
|
|
|
2015-03-27 15:29:40 +00: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 02:08:34 +00:00
|
|
|
void HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
2014-10-22 19:30:25 +00:00
|
|
|
|
2014-10-31 20:44:44 +00:00
|
|
|
void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; };
|
|
|
|
void ResetReceiver () { m_Receiver = nullptr; };
|
|
|
|
|
2016-09-03 14:24:06 +00:00
|
|
|
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 17:58:34 +00:00
|
|
|
|
|
|
|
std::shared_ptr<DatagramSession::Info> GetInfoForRemote(const i2p::data::IdentHash & remote);
|
2016-09-03 15:46:47 +00:00
|
|
|
|
2016-09-08 14:16:42 +00:00
|
|
|
// clean up stale sessions
|
|
|
|
void CleanUp ();
|
|
|
|
|
2014-10-23 20:56:50 +00:00
|
|
|
private:
|
2016-09-08 14:16:42 +00:00
|
|
|
|
2016-09-03 15:46:47 +00:00
|
|
|
std::shared_ptr<DatagramSession> ObtainSession(const i2p::data::IdentHash & ident);
|
2015-03-27 01:23:59 +00:00
|
|
|
|
2015-11-24 18:09:12 +00:00
|
|
|
std::shared_ptr<I2NPMessage> CreateDataMessage (const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort);
|
2016-08-27 17:17:34 +00:00
|
|
|
|
2015-03-02 02:08:34 +00:00
|
|
|
void HandleDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
2014-10-23 20:56:50 +00:00
|
|
|
|
2016-09-03 14:24:06 +00: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 19:30:25 +00:00
|
|
|
private:
|
2016-08-22 02:54:06 +00:00
|
|
|
i2p::client::ClientDestination * m_Owner;
|
2015-04-04 00:34:37 +00:00
|
|
|
Receiver m_Receiver; // default
|
2016-09-03 14:24:06 +00:00
|
|
|
std::mutex m_SessionsMutex;
|
|
|
|
std::map<i2p::data::IdentHash, std::shared_ptr<DatagramSession> > m_Sessions;
|
|
|
|
std::mutex m_ReceiversMutex;
|
2015-04-04 00:34:37 +00:00
|
|
|
std::map<uint16_t, Receiver> m_ReceiversByPorts;
|
2015-11-03 14:15:49 +00:00
|
|
|
|
|
|
|
i2p::data::GzipInflator m_Inflator;
|
|
|
|
i2p::data::GzipDeflator m_Deflator;
|
2014-10-22 19:30:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|