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.
132 lines
3.8 KiB
132 lines
3.8 KiB
![]()
11 years ago
|
#ifndef SSU_DATA_H__
|
||
|
#define SSU_DATA_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
![]()
10 years ago
|
#include <string.h>
|
||
![]()
11 years ago
|
#include <map>
|
||
![]()
10 years ago
|
#include <vector>
|
||
![]()
8 years ago
|
#include <unordered_set>
|
||
![]()
10 years ago
|
#include <memory>
|
||
![]()
10 years ago
|
#include <boost/asio.hpp>
|
||
![]()
11 years ago
|
#include "I2NPProtocol.h"
|
||
![]()
10 years ago
|
#include "Identity.h"
|
||
|
#include "RouterInfo.h"
|
||
![]()
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
![]()
10 years ago
|
namespace transport
|
||
![]()
11 years ago
|
{
|
||
|
|
||
![]()
10 years ago
|
const size_t SSU_MTU_V4 = 1484;
|
||
![]()
8 years ago
|
#ifdef MESHNET
|
||
![]()
8 years ago
|
const size_t SSU_MTU_V6 = 1286;
|
||
![]()
8 years ago
|
#else
|
||
![]()
8 years ago
|
const size_t SSU_MTU_V6 = 1488;
|
||
![]()
8 years ago
|
#endif
|
||
![]()
10 years ago
|
const size_t IPV4_HEADER_SIZE = 20;
|
||
![]()
10 years ago
|
const size_t IPV6_HEADER_SIZE = 40;
|
||
![]()
10 years ago
|
const size_t UDP_HEADER_SIZE = 8;
|
||
![]()
10 years ago
|
const size_t SSU_V4_MAX_PACKET_SIZE = SSU_MTU_V4 - IPV4_HEADER_SIZE - UDP_HEADER_SIZE; // 1456
|
||
![]()
8 years ago
|
const size_t SSU_V6_MAX_PACKET_SIZE = SSU_MTU_V6 - IPV6_HEADER_SIZE - UDP_HEADER_SIZE; // 1440
|
||
![]()
10 years ago
|
const int RESEND_INTERVAL = 3; // in seconds
|
||
|
const int MAX_NUM_RESENDS = 5;
|
||
![]()
10 years ago
|
const int DECAY_INTERVAL = 20; // in seconds
|
||
|
const int INCOMPLETE_MESSAGES_CLEANUP_TIMEOUT = 30; // in seconds
|
||
![]()
9 years ago
|
const unsigned int MAX_NUM_RECEIVED_MESSAGES = 1000; // how many msgID we store for duplicates check
|
||
![]()
8 years ago
|
const int MAX_OUTGOING_WINDOW_SIZE = 200; // how many unacked message we can store
|
||
![]()
11 years ago
|
// data flags
|
||
|
const uint8_t DATA_FLAG_EXTENDED_DATA_INCLUDED = 0x02;
|
||
|
const uint8_t DATA_FLAG_WANT_REPLY = 0x04;
|
||
|
const uint8_t DATA_FLAG_REQUEST_PREVIOUS_ACKS = 0x08;
|
||
|
const uint8_t DATA_FLAG_EXPLICIT_CONGESTION_NOTIFICATION = 0x10;
|
||
|
const uint8_t DATA_FLAG_ACK_BITFIELDS_INCLUDED = 0x40;
|
||
|
const uint8_t DATA_FLAG_EXPLICIT_ACKS_INCLUDED = 0x80;
|
||
|
|
||
![]()
10 years ago
|
struct Fragment
|
||
|
{
|
||
![]()
10 years ago
|
int fragmentNum;
|
||
|
size_t len;
|
||
![]()
10 years ago
|
bool isLast;
|
||
![]()
10 years ago
|
uint8_t buf[SSU_V4_MAX_PACKET_SIZE + 18]; // use biggest
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
Fragment () = default;
|
||
![]()
10 years ago
|
Fragment (int n, const uint8_t * b, int l, bool last):
|
||
|
fragmentNum (n), len (l), isLast (last) { memcpy (buf, b, len); };
|
||
|
};
|
||
|
|
||
|
struct FragmentCmp
|
||
|
{
|
||
![]()
10 years ago
|
bool operator() (const std::unique_ptr<Fragment>& f1, const std::unique_ptr<Fragment>& f2) const
|
||
![]()
10 years ago
|
{
|
||
|
return f1->fragmentNum < f2->fragmentNum;
|
||
|
};
|
||
|
};
|
||
|
|
||
|
struct IncompleteMessage
|
||
|
{
|
||
![]()
9 years ago
|
std::shared_ptr<I2NPMessage> msg;
|
||
![]()
10 years ago
|
int nextFragmentNum;
|
||
![]()
10 years ago
|
uint32_t lastFragmentInsertTime; // in seconds
|
||
![]()
10 years ago
|
std::set<std::unique_ptr<Fragment>, FragmentCmp> savedFragments;
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
IncompleteMessage (std::shared_ptr<I2NPMessage> m): msg (m), nextFragmentNum (0), lastFragmentInsertTime (0) {};
|
||
![]()
10 years ago
|
void AttachNextFragment (const uint8_t * fragment, size_t fragmentSize);
|
||
![]()
10 years ago
|
};
|
||
![]()
10 years ago
|
|
||
|
struct SentMessage
|
||
|
{
|
||
![]()
10 years ago
|
std::vector<std::unique_ptr<Fragment> > fragments;
|
||
![]()
10 years ago
|
uint32_t nextResendTime; // in seconds
|
||
|
int numResends;
|
||
|
};
|
||
![]()
10 years ago
|
|
||
![]()
11 years ago
|
class SSUSession;
|
||
|
class SSUData
|
||
|
{
|
||
|
public:
|
||
|
|
||
![]()
9 years ago
|
SSUData (SSUSession& session);
|
||
![]()
11 years ago
|
~SSUData ();
|
||
![]()
10 years ago
|
|
||
|
void Start ();
|
||
![]()
10 years ago
|
void Stop ();
|
||
|
|
||
![]()
11 years ago
|
void ProcessMessage (uint8_t * buf, size_t len);
|
||
![]()
10 years ago
|
void FlushReceivedMessage ();
|
||
![]()
9 years ago
|
void Send (std::shared_ptr<i2p::I2NPMessage> msg);
|
||
![]()
11 years ago
|
|
||
![]()
9 years ago
|
void AdjustPacketSize (std::shared_ptr<const i2p::data::RouterInfo> remoteRouter);
|
||
![]()
10 years ago
|
void UpdatePacketSize (const i2p::data::IdentHash& remoteIdent);
|
||
|
|
||
![]()
10 years ago
|
private:
|
||
|
|
||
|
void SendMsgAck (uint32_t msgID);
|
||
![]()
10 years ago
|
void SendFragmentAck (uint32_t msgID, int fragmentNum);
|
||
![]()
10 years ago
|
void ProcessAcks (uint8_t *& buf, uint8_t flag);
|
||
|
void ProcessFragments (uint8_t * buf);
|
||
![]()
10 years ago
|
void ProcessSentMessageAck (uint32_t msgID);
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
void ScheduleResend ();
|
||
|
void HandleResendTimer (const boost::system::error_code& ecode);
|
||
![]()
10 years ago
|
|
||
|
void ScheduleIncompleteMessagesCleanup ();
|
||
|
void HandleIncompleteMessagesCleanupTimer (const boost::system::error_code& ecode);
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
private:
|
||
![]()
11 years ago
|
|
||
|
SSUSession& m_Session;
|
||
![]()
10 years ago
|
std::map<uint32_t, std::unique_ptr<IncompleteMessage> > m_IncompleteMessages;
|
||
|
std::map<uint32_t, std::unique_ptr<SentMessage> > m_SentMessages;
|
||
![]()
8 years ago
|
std::unordered_set<uint32_t> m_ReceivedMessages;
|
||
|
boost::asio::deadline_timer m_ResendTimer, m_IncompleteMessagesCleanupTimer;
|
||
![]()
10 years ago
|
int m_MaxPacketSize, m_PacketSize;
|
||
![]()
10 years ago
|
i2p::I2NPMessagesHandler m_Handler;
|
||
![]()
8 years ago
|
uint32_t m_LastMessageReceivedTime; // in second
|
||
![]()
11 years ago
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|