2020-05-22 13:18:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
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>
|
2020-05-22 01:54:00 +00:00
|
|
|
#include <vector>
|
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
|
|
|
|
{
|
2017-04-08 16:51:35 +00:00
|
|
|
// milliseconds for max session idle time
|
2016-09-03 15:46:47 +00:00
|
|
|
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 15:58:26 +00:00
|
|
|
const uint64_t DATAGRAM_SESSION_PATH_TIMEOUT = 10 * 1000;
|
2016-09-03 15:46:47 +00:00
|
|
|
// milliseconds interval a routing path is used before switching
|
2016-10-06 17:41:18 +00:00
|
|
|
const uint64_t DATAGRAM_SESSION_PATH_SWITCH_INTERVAL = 20 * 60 * 1000;
|
2016-09-03 15:46:47 +00:00
|
|
|
// milliseconds before lease expire should we try switching leases
|
2017-04-09 12:52:42 +00:00
|
|
|
const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW = 30 * 1000;
|
2016-09-03 15:46:47 +00:00
|
|
|
// milliseconds fudge factor for leases handover
|
|
|
|
const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_FUDGE = 1000;
|
2016-10-10 12:30:33 +00:00
|
|
|
// milliseconds minimum time between path switches
|
|
|
|
const uint64_t DATAGRAM_SESSION_PATH_MIN_LIFETIME = 5 * 1000;
|
2020-03-01 10:25:50 +00:00
|
|
|
// max 64 messages buffered in send queue for each datagram session
|
|
|
|
const size_t DATAGRAM_SEND_QUEUE_MAX_SIZE = 64;
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2017-01-16 12:54:56 +00:00
|
|
|
class DatagramSession : public std::enable_shared_from_this<DatagramSession>
|
2016-09-03 15:46:47 +00:00
|
|
|
{
|
2017-01-17 17:13:56 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
DatagramSession(std::shared_ptr<i2p::client::ClientDestination> localDestination, const i2p::data::IdentHash & remoteIdent);
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2016-12-12 18:40:24 +00:00
|
|
|
|
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
/** @brief ack the garlic routing path */
|
|
|
|
void Ack();
|
2016-09-03 15:46:47 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
/** send an i2np message to remote endpoint for this session */
|
|
|
|
void SendMsg(std::shared_ptr<I2NPMessage> msg);
|
2020-06-09 20:26:45 +00:00
|
|
|
void FlushSendQueue();
|
2020-03-01 10:25:50 +00:00
|
|
|
/** get the last time in milliseconds for when we used this datagram session */
|
|
|
|
uint64_t LastActivity() const { return m_LastUse; }
|
2016-12-12 18:40:24 +00:00
|
|
|
|
2020-05-17 20:49:31 +00:00
|
|
|
bool IsRatchets () const { return m_RoutingSession && m_RoutingSession->IsRatchets (); }
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2016-09-03 17:58:34 +00:00
|
|
|
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;
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
Info() : IBGW(nullptr), OBEP(nullptr), activity(0) {}
|
|
|
|
Info(const uint8_t * ibgw, const uint8_t * obep, const uint64_t a) :
|
|
|
|
activity(a) {
|
|
|
|
if(ibgw) IBGW = std::make_shared<i2p::data::IdentHash>(ibgw);
|
|
|
|
else IBGW = nullptr;
|
|
|
|
if(obep) OBEP = std::make_shared<i2p::data::IdentHash>(obep);
|
|
|
|
else OBEP = nullptr;
|
|
|
|
}
|
|
|
|
};
|
2016-09-03 17:58:34 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
Info GetSessionInfo() const;
|
2016-09-03 17:58:34 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
private:
|
2016-09-03 15:46:47 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
std::shared_ptr<i2p::garlic::GarlicRoutingPath> GetSharedRoutingPath();
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
void HandleLeaseSetUpdated(std::shared_ptr<i2p::data::LeaseSet> ls);
|
2016-09-03 15:46:47 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
std::shared_ptr<i2p::client::ClientDestination> m_LocalDestination;
|
|
|
|
i2p::data::IdentHash m_RemoteIdent;
|
|
|
|
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
|
|
|
|
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
|
2020-06-14 15:16:08 +00:00
|
|
|
std::vector<std::shared_ptr<i2p::garlic::GarlicRoutingSession> > m_PendingRoutingSessions;
|
2020-03-01 10:25:50 +00:00
|
|
|
std::vector<std::shared_ptr<I2NPMessage> > m_SendQueue;
|
|
|
|
uint64_t m_LastUse;
|
|
|
|
bool m_RequestingLS;
|
2016-09-03 15:46:47 +00:00
|
|
|
};
|
2017-01-16 12:54:56 +00:00
|
|
|
|
|
|
|
typedef std::shared_ptr<DatagramSession> DatagramSession_ptr;
|
|
|
|
|
2017-04-08 16:51:35 +00:00
|
|
|
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;
|
2019-07-10 01:33:55 +00:00
|
|
|
typedef std::function<void (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)> RawReceiver;
|
2014-10-31 20:44:44 +00:00
|
|
|
|
2014-10-22 19:30:25 +00:00
|
|
|
public:
|
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner, bool gzip);
|
2017-04-08 16:51:35 +00:00
|
|
|
~DatagramDestination ();
|
2014-10-22 19:30:25 +00:00
|
|
|
|
2019-07-10 01:33:55 +00:00
|
|
|
void SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash & ident, uint16_t fromPort = 0, uint16_t toPort = 0);
|
|
|
|
void SendRawDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash & ident, uint16_t fromPort = 0, uint16_t toPort = 0);
|
2020-06-11 01:19:37 +00:00
|
|
|
// TODO: implement calls from other thread from SAM
|
2020-06-09 23:20:24 +00:00
|
|
|
|
|
|
|
std::shared_ptr<DatagramSession> GetSession(const i2p::data::IdentHash & ident);
|
|
|
|
void SendDatagram (std::shared_ptr<DatagramSession> session, const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort);
|
|
|
|
void SendRawDatagram (std::shared_ptr<DatagramSession> session, const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort);
|
|
|
|
void FlushSendQueue (std::shared_ptr<DatagramSession> session);
|
|
|
|
|
2019-07-10 01:33:55 +00:00
|
|
|
void HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len, bool isRaw = false);
|
2020-03-01 10:25:50 +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
|
|
|
|
2019-07-10 01:33:55 +00:00
|
|
|
void SetRawReceiver (const RawReceiver& receiver) { m_RawReceiver = receiver; };
|
|
|
|
void ResetRawReceiver () { m_RawReceiver = nullptr; };
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2016-09-03 17:58:34 +00:00
|
|
|
std::shared_ptr<DatagramSession::Info> GetInfoForRemote(const i2p::data::IdentHash & remote);
|
2017-04-08 16:51:35 +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:
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
std::shared_ptr<DatagramSession> ObtainSession(const i2p::data::IdentHash & ident);
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
std::shared_ptr<I2NPMessage> CreateDataMessage (const std::vector<std::pair<const uint8_t *, size_t> >& payloads,
|
2020-05-17 20:49:31 +00:00
|
|
|
uint16_t fromPort, uint16_t toPort, bool isRaw = false, bool checksum = true);
|
2016-08-27 17:17:34 +00:00
|
|
|
|
2016-10-09 14:55:55 +00:00
|
|
|
void HandleDatagram (uint16_t fromPort, uint16_t toPort, uint8_t *const& buf, size_t len);
|
2019-07-10 01:33:55 +00:00
|
|
|
void HandleRawDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
2020-03-01 10:25: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);
|
2017-04-08 16:51:35 +00:00
|
|
|
|
2014-10-22 19:30:25 +00:00
|
|
|
private:
|
2019-07-10 01:33:55 +00:00
|
|
|
|
|
|
|
std::shared_ptr<i2p::client::ClientDestination> m_Owner;
|
2015-04-04 00:34:37 +00:00
|
|
|
Receiver m_Receiver; // default
|
2019-07-10 01:33:55 +00:00
|
|
|
RawReceiver m_RawReceiver; // default
|
2020-05-18 16:01:13 +00:00
|
|
|
bool m_Gzip; // gzip compression of data messages
|
2016-09-03 14:24:06 +00:00
|
|
|
std::mutex m_SessionsMutex;
|
2017-01-16 12:54:56 +00:00
|
|
|
std::map<i2p::data::IdentHash, DatagramSession_ptr > m_Sessions;
|
2016-09-03 14:24:06 +00:00
|
|
|
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;
|
2020-05-22 01:54:00 +00:00
|
|
|
std::vector<uint8_t> m_From, m_Signature;
|
2020-06-11 01:19:37 +00:00
|
|
|
i2p::util::MemoryPool<I2NPMessageBuffer<I2NP_MAX_MESSAGE_SIZE> > m_I2NPMsgsPool;
|
2017-04-08 16:51:35 +00:00
|
|
|
};
|
2014-10-22 19:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|