2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2022-02-02 22:33:33 +00:00
|
|
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
2020-05-22 13:18:41 +00:00
|
|
|
*
|
|
|
|
* 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-04-22 15:39:26 +00:00
|
|
|
#ifndef SSU_DATA_H__
|
|
|
|
#define SSU_DATA_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-07-15 02:06:58 +00:00
|
|
|
#include <string.h>
|
2014-06-12 15:14:22 +00:00
|
|
|
#include <vector>
|
2021-09-25 22:30:17 +00:00
|
|
|
#include <map>
|
|
|
|
#include <unordered_map>
|
2015-01-31 01:30:39 +00:00
|
|
|
#include <memory>
|
2014-07-20 14:38:39 +00:00
|
|
|
#include <boost/asio.hpp>
|
2014-04-22 15:39:26 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2014-09-18 18:37:29 +00:00
|
|
|
#include "Identity.h"
|
|
|
|
#include "RouterInfo.h"
|
2022-07-13 23:35:18 +00:00
|
|
|
#include "TransportSession.h"
|
2014-04-22 15:39:26 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-21 16:25:53 +00:00
|
|
|
namespace transport
|
2014-04-22 15:39:26 +00:00
|
|
|
{
|
2014-10-29 15:17:30 +00:00
|
|
|
const size_t SSU_MTU_V4 = 1484;
|
2017-02-12 15:12:12 +00:00
|
|
|
const size_t SSU_MTU_V6 = 1488;
|
2014-10-29 15:17:30 +00:00
|
|
|
const size_t SSU_V4_MAX_PACKET_SIZE = SSU_MTU_V4 - IPV4_HEADER_SIZE - UDP_HEADER_SIZE; // 1456
|
2017-02-12 15:12:12 +00:00
|
|
|
const size_t SSU_V6_MAX_PACKET_SIZE = SSU_MTU_V6 - IPV6_HEADER_SIZE - UDP_HEADER_SIZE; // 1440
|
2014-07-20 14:38:39 +00:00
|
|
|
const int RESEND_INTERVAL = 3; // in seconds
|
|
|
|
const int MAX_NUM_RESENDS = 5;
|
2015-02-08 13:50:05 +00:00
|
|
|
const int DECAY_INTERVAL = 20; // in seconds
|
|
|
|
const int INCOMPLETE_MESSAGES_CLEANUP_TIMEOUT = 30; // in seconds
|
2021-09-21 23:01:22 +00:00
|
|
|
const int RECEIVED_MESSAGES_CLEANUP_TIMEOUT = 40; // in seconds
|
2015-12-17 07:58:09 +00:00
|
|
|
const unsigned int MAX_NUM_RECEIVED_MESSAGES = 1000; // how many msgID we store for duplicates check
|
2016-06-26 21:03:04 +00:00
|
|
|
const int MAX_OUTGOING_WINDOW_SIZE = 200; // how many unacked message we can store
|
2014-04-22 15:39:26 +00:00
|
|
|
// 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;
|
2018-01-06 03:48:51 +00:00
|
|
|
const uint8_t DATA_FLAG_EXPLICIT_ACKS_INCLUDED = 0x80;
|
2014-04-22 15:39:26 +00:00
|
|
|
|
2014-07-15 02:06:58 +00:00
|
|
|
struct Fragment
|
|
|
|
{
|
2014-07-20 14:38:39 +00:00
|
|
|
int fragmentNum;
|
|
|
|
size_t len;
|
2014-07-15 02:06:58 +00:00
|
|
|
bool isLast;
|
2014-10-29 15:17:30 +00:00
|
|
|
uint8_t buf[SSU_V4_MAX_PACKET_SIZE + 18]; // use biggest
|
2014-07-15 02:06:58 +00:00
|
|
|
|
2014-07-20 14:38:39 +00:00
|
|
|
Fragment () = default;
|
2018-01-06 03:48:51 +00:00
|
|
|
Fragment (int n, const uint8_t * b, int l, bool last):
|
|
|
|
fragmentNum (n), len (l), isLast (last) { memcpy (buf, b, len); };
|
|
|
|
};
|
2014-07-15 02:06:58 +00:00
|
|
|
|
|
|
|
struct FragmentCmp
|
|
|
|
{
|
2021-09-11 22:58:27 +00:00
|
|
|
bool operator() (const std::shared_ptr<Fragment>& f1, const std::shared_ptr<Fragment>& f2) const
|
2018-01-06 04:01:44 +00:00
|
|
|
{
|
2018-01-06 03:48:51 +00:00
|
|
|
return f1->fragmentNum < f2->fragmentNum;
|
2014-07-15 02:06:58 +00:00
|
|
|
};
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
|
|
|
|
2014-07-15 02:06:58 +00:00
|
|
|
struct IncompleteMessage
|
|
|
|
{
|
2015-06-21 19:08:22 +00:00
|
|
|
std::shared_ptr<I2NPMessage> msg;
|
2018-01-06 03:48:51 +00:00
|
|
|
int nextFragmentNum;
|
2015-02-08 13:50:05 +00:00
|
|
|
uint32_t lastFragmentInsertTime; // in seconds
|
2020-12-13 02:40:07 +00:00
|
|
|
uint64_t receivedFragmentsBits;
|
2021-09-11 22:58:27 +00:00
|
|
|
std::set<std::shared_ptr<Fragment>, FragmentCmp> savedFragments;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2022-02-02 22:33:33 +00:00
|
|
|
IncompleteMessage (std::shared_ptr<I2NPMessage>&& m): msg (m), nextFragmentNum (0),
|
2020-12-13 02:40:07 +00:00
|
|
|
lastFragmentInsertTime (0), receivedFragmentsBits (0) {};
|
2018-01-06 03:48:51 +00:00
|
|
|
void AttachNextFragment (const uint8_t * fragment, size_t fragmentSize);
|
2014-07-15 02:06:58 +00:00
|
|
|
};
|
2014-07-19 22:53:02 +00:00
|
|
|
|
|
|
|
struct SentMessage
|
|
|
|
{
|
2021-09-11 22:58:27 +00:00
|
|
|
std::vector<std::shared_ptr<Fragment> > fragments;
|
2014-07-19 22:53:02 +00:00
|
|
|
uint32_t nextResendTime; // in seconds
|
|
|
|
int numResends;
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
|
|
|
|
2014-04-22 15:39:26 +00:00
|
|
|
class SSUSession;
|
|
|
|
class SSUData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
SSUData (SSUSession& session);
|
2014-04-22 15:39:26 +00:00
|
|
|
~SSUData ();
|
2015-02-08 13:50:05 +00:00
|
|
|
|
|
|
|
void Start ();
|
2018-01-06 03:48:51 +00:00
|
|
|
void Stop ();
|
2021-09-22 23:09:56 +00:00
|
|
|
void CleanUp (uint64_t ts);
|
2021-11-27 20:30:35 +00:00
|
|
|
|
2014-04-22 15:39:26 +00:00
|
|
|
void ProcessMessage (uint8_t * buf, size_t len);
|
2015-02-15 19:17:55 +00:00
|
|
|
void FlushReceivedMessage ();
|
2015-06-17 14:47:26 +00:00
|
|
|
void Send (std::shared_ptr<i2p::I2NPMessage> msg);
|
2014-04-22 15:39:26 +00:00
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
void AdjustPacketSize (std::shared_ptr<const i2p::data::RouterInfo> remoteRouter);
|
2014-09-18 18:37:29 +00:00
|
|
|
void UpdatePacketSize (const i2p::data::IdentHash& remoteIdent);
|
|
|
|
|
2014-06-10 15:19:31 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
void SendMsgAck (uint32_t msgID);
|
2020-12-13 02:40:07 +00:00
|
|
|
void SendFragmentAck (uint32_t msgID, uint64_t bits);
|
2014-07-17 19:22:32 +00:00
|
|
|
void ProcessAcks (uint8_t *& buf, uint8_t flag);
|
|
|
|
void ProcessFragments (uint8_t * buf);
|
2018-01-06 03:48:51 +00:00
|
|
|
void ProcessSentMessageAck (uint32_t msgID);
|
2014-06-10 15:19:31 +00:00
|
|
|
|
2014-07-20 14:38:39 +00:00
|
|
|
void ScheduleResend ();
|
2018-01-06 03:48:51 +00:00
|
|
|
void HandleResendTimer (const boost::system::error_code& ecode);
|
2015-02-08 13:50:05 +00:00
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
private:
|
2014-04-22 15:39:26 +00:00
|
|
|
|
|
|
|
SSUSession& m_Session;
|
2021-09-25 22:30:17 +00:00
|
|
|
std::map<uint32_t, std::shared_ptr<IncompleteMessage> > m_IncompleteMessages;
|
|
|
|
std::map<uint32_t, std::shared_ptr<SentMessage> > m_SentMessages;
|
|
|
|
std::unordered_map<uint32_t, uint64_t> m_ReceivedMessages; // msgID -> timestamp in seconds
|
2021-09-22 02:13:34 +00:00
|
|
|
boost::asio::deadline_timer m_ResendTimer;
|
2014-10-29 15:17:30 +00:00
|
|
|
int m_MaxPacketSize, m_PacketSize;
|
2015-01-30 03:35:57 +00:00
|
|
|
i2p::I2NPMessagesHandler m_Handler;
|
2016-08-07 20:27:36 +00:00
|
|
|
uint32_t m_LastMessageReceivedTime; // in second
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2014-04-22 15:39:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|