1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-15 17:59:58 +00:00
i2pd/libi2pd/TunnelEndpoint.h

85 lines
2.8 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013-2025, 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
*/
2013-11-10 18:19:49 -05:00
#ifndef TUNNEL_ENDPOINT_H__
#define TUNNEL_ENDPOINT_H__
#include <inttypes.h>
#include <vector>
#include <list>
2013-11-10 18:19:49 -05:00
#include <string>
2021-09-25 18:30:17 -04:00
#include <unordered_map>
#include <memory>
2013-11-10 18:19:49 -05:00
#include "I2NPProtocol.h"
#include "TunnelBase.h"
namespace i2p
{
namespace tunnel
{
class TunnelEndpoint final
2018-01-06 11:48:51 +08:00
{
2014-07-04 20:54:03 -04:00
struct TunnelMessageBlockEx: public TunnelMessageBlock
{
2016-12-06 16:23:52 -05:00
uint64_t receiveTime; // milliseconds since epoch
2014-07-04 20:54:03 -04:00
uint8_t nextFragmentNum;
2018-01-06 11:48:51 +08:00
};
struct Fragment
{
Fragment (bool last, uint64_t t, const uint8_t * buf, size_t size):
2025-01-14 13:30:47 -05:00
isLastFragment (last), receiveTime (t), data (size) { memcpy (data.data(), buf, size); };
bool isLastFragment;
uint64_t receiveTime; // milliseconds since epoch
std::vector<uint8_t> data;
2018-01-06 11:48:51 +08:00
};
2013-11-10 18:19:49 -05:00
public:
TunnelEndpoint (bool isInbound): m_IsInbound (isInbound), m_NumReceivedBytes (0), m_CurrentMsgID (0) {};
~TunnelEndpoint () = default;
2013-12-10 08:10:49 -05:00
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
2018-01-06 11:48:51 +08:00
void Cleanup ();
void HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg);
void FlushI2NPMsgs ();
2013-11-10 18:19:49 -05:00
const i2p::data::IdentHash * GetCurrentHash () const; // return null if not avaiable
const std::unique_ptr<TunnelTransportSender>& GetSender () const { return m_Sender; };
2013-11-10 18:19:49 -05:00
private:
void HandleFollowOnFragment (uint32_t msgID, bool isLastFragment, uint8_t fragmentNum, const uint8_t * fragment, size_t size);
bool ConcatFollowOnFragment (TunnelMessageBlockEx& msg, const uint8_t * fragment, size_t size) const; // true if success
void HandleCurrenMessageFollowOnFragment (const uint8_t * fragment, size_t size, bool isLastFragment);
2013-11-10 18:19:49 -05:00
void HandleNextMessage (const TunnelMessageBlock& msg);
void SendMessageTo (const i2p::data::IdentHash& to, std::shared_ptr<i2p::I2NPMessage> msg);
void AddOutOfSequenceFragment (uint32_t msgID, uint8_t fragmentNum, bool isLastFragment, const uint8_t * fragment, size_t size);
bool ConcatNextOutOfSequenceFragment (uint32_t msgID, TunnelMessageBlockEx& msg); // true if something added
2018-01-06 11:48:51 +08:00
void HandleOutOfSequenceFragments (uint32_t msgID, TunnelMessageBlockEx& msg);
void AddIncompleteCurrentMessage ();
2018-01-06 11:48:51 +08:00
private:
2014-06-11 10:56:20 -04:00
2021-09-25 18:30:17 -04:00
std::unordered_map<uint32_t, TunnelMessageBlockEx> m_IncompleteMessages;
std::unordered_map<uint64_t, Fragment> m_OutOfSequenceFragments; // ((msgID << 8) + fragment#)->fragment
bool m_IsInbound;
2013-12-10 08:10:49 -05:00
size_t m_NumReceivedBytes;
TunnelMessageBlockEx m_CurrentMessage;
uint32_t m_CurrentMsgID;
// I2NP messages to send
std::list<std::shared_ptr<i2p::I2NPMessage> > m_I2NPMsgs; // to send
i2p::data::IdentHash m_CurrentHash; // send msgs to
std::unique_ptr<TunnelTransportSender> m_Sender;
2018-01-06 11:48:51 +08:00
};
}
2013-11-10 18:19:49 -05:00
}
#endif