i2pd/SSUData.h

129 lines
3.7 KiB
C
Raw Normal View History

2014-04-22 19:39:26 +04:00
#ifndef SSU_DATA_H__
#define SSU_DATA_H__
#include <inttypes.h>
2014-07-15 06:06:58 +04:00
#include <string.h>
2014-04-22 19:39:26 +04:00
#include <map>
2014-06-12 19:14:22 +04:00
#include <vector>
2014-07-15 06:06:58 +04:00
#include <set>
2015-01-31 04:30:39 +03:00
#include <memory>
2014-07-20 18:38:39 +04:00
#include <boost/asio.hpp>
2014-04-22 19:39:26 +04:00
#include "I2NPProtocol.h"
2014-09-18 22:37:29 +04:00
#include "Identity.h"
#include "RouterInfo.h"
2014-04-22 19:39:26 +04:00
namespace i2p
{
namespace transport
2014-04-22 19:39:26 +04:00
{
2014-10-29 18:17:30 +03:00
const size_t SSU_MTU_V4 = 1484;
const size_t SSU_MTU_V6 = 1472;
2014-09-15 01:54:55 +04:00
const size_t IPV4_HEADER_SIZE = 20;
2014-10-29 18:17:30 +03:00
const size_t IPV6_HEADER_SIZE = 40;
2014-09-15 01:54:55 +04:00
const size_t UDP_HEADER_SIZE = 8;
2014-10-29 18:17:30 +03:00
const size_t SSU_V4_MAX_PACKET_SIZE = SSU_MTU_V4 - IPV4_HEADER_SIZE - UDP_HEADER_SIZE; // 1456
const size_t SSU_V6_MAX_PACKET_SIZE = SSU_MTU_V6 - IPV6_HEADER_SIZE - UDP_HEADER_SIZE; // 1424
2014-07-20 18:38:39 +04:00
const int RESEND_INTERVAL = 3; // in seconds
const int MAX_NUM_RESENDS = 5;
2015-02-08 16:50:05 +03:00
const int DECAY_INTERVAL = 20; // in seconds
const int MAX_NUM_RECEIVED_MESSAGES = 1000; // how many msgID we store for duplicates check
const int INCOMPLETE_MESSAGES_CLEANUP_TIMEOUT = 30; // in seconds
2014-04-22 19:39:26 +04: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;
const uint8_t DATA_FLAG_EXPLICIT_ACKS_INCLUDED = 0x80;
2014-07-15 06:06:58 +04:00
struct Fragment
{
2014-07-20 18:38:39 +04:00
int fragmentNum;
size_t len;
2014-07-15 06:06:58 +04:00
bool isLast;
2014-10-29 18:17:30 +03:00
uint8_t buf[SSU_V4_MAX_PACKET_SIZE + 18]; // use biggest
2014-07-15 06:06:58 +04:00
2014-07-20 18:38:39 +04:00
Fragment () = default;
2014-07-15 06:06:58 +04:00
Fragment (int n, const uint8_t * b, int l, bool last):
fragmentNum (n), len (l), isLast (last) { memcpy (buf, b, len); };
};
struct FragmentCmp
{
2015-02-05 20:13:37 +03:00
bool operator() (const std::unique_ptr<Fragment>& f1, const std::unique_ptr<Fragment>& f2) const
2014-07-15 06:06:58 +04:00
{
return f1->fragmentNum < f2->fragmentNum;
};
};
struct IncompleteMessage
{
std::shared_ptr<I2NPMessage> msg;
2014-07-15 06:06:58 +04:00
int nextFragmentNum;
2015-02-08 16:50:05 +03:00
uint32_t lastFragmentInsertTime; // in seconds
2015-02-05 20:13:37 +03:00
std::set<std::unique_ptr<Fragment>, FragmentCmp> savedFragments;
2014-07-15 06:06:58 +04:00
IncompleteMessage (std::shared_ptr<I2NPMessage> m): msg (m), nextFragmentNum (0), lastFragmentInsertTime (0) {};
void AttachNextFragment (const uint8_t * fragment, size_t fragmentSize);
2014-07-15 06:06:58 +04:00
};
2014-07-20 02:53:02 +04:00
struct SentMessage
{
2015-01-31 04:30:39 +03:00
std::vector<std::unique_ptr<Fragment> > fragments;
2014-07-20 02:53:02 +04:00
uint32_t nextResendTime; // in seconds
int numResends;
};
2014-07-15 06:06:58 +04:00
2014-04-22 19:39:26 +04:00
class SSUSession;
class SSUData
{
public:
SSUData (SSUSession& session);
~SSUData ();
2015-02-08 16:50:05 +03:00
void Start ();
2015-02-08 01:58:29 +03:00
void Stop ();
2014-04-22 19:39:26 +04:00
void ProcessMessage (uint8_t * buf, size_t len);
2015-02-15 22:17:55 +03:00
void FlushReceivedMessage ();
void Send (std::shared_ptr<i2p::I2NPMessage> msg);
2014-04-22 19:39:26 +04:00
2014-09-18 22:37:29 +04:00
void UpdatePacketSize (const i2p::data::IdentHash& remoteIdent);
2014-06-10 19:19:31 +04:00
private:
void SendMsgAck (uint32_t msgID);
void SendFragmentAck (uint32_t msgID, int fragmentNum);
2014-07-17 23:22:32 +04:00
void ProcessAcks (uint8_t *& buf, uint8_t flag);
void ProcessFragments (uint8_t * buf);
void ProcessSentMessageAck (uint32_t msgID);
2014-06-10 19:19:31 +04:00
2014-07-20 18:38:39 +04:00
void ScheduleResend ();
void HandleResendTimer (const boost::system::error_code& ecode);
2015-02-08 16:50:05 +03:00
void ScheduleDecay ();
void HandleDecayTimer (const boost::system::error_code& ecode);
void ScheduleIncompleteMessagesCleanup ();
void HandleIncompleteMessagesCleanupTimer (const boost::system::error_code& ecode);
2014-07-15 06:06:58 +04:00
2014-09-18 22:37:29 +04:00
void AdjustPacketSize (const i2p::data::RouterInfo& remoteRouter);
2014-07-20 18:38:39 +04:00
private:
2014-04-22 19:39:26 +04:00
SSUSession& m_Session;
std::map<uint32_t, std::unique_ptr<IncompleteMessage> > m_IncompleteMessages;
std::map<uint32_t, std::unique_ptr<SentMessage> > m_SentMessages;
2014-09-30 04:08:26 +04:00
std::set<uint32_t> m_ReceivedMessages;
2015-02-08 16:50:05 +03:00
boost::asio::deadline_timer m_ResendTimer, m_DecayTimer, m_IncompleteMessagesCleanupTimer;
2014-10-29 18:17:30 +03:00
int m_MaxPacketSize, m_PacketSize;
2015-01-30 06:35:57 +03:00
i2p::I2NPMessagesHandler m_Handler;
2014-04-22 19:39:26 +04:00
};
}
}
#endif