i2pd/SSUData.h

85 lines
1.8 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>
2014-04-22 19:39:26 +04:00
#include "I2NPProtocol.h"
namespace i2p
{
namespace ssu
{
2014-07-15 06:06:58 +04:00
const size_t SSU_MTU = 1484;
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
{
int fragmentNum, len;
bool isLast;
uint8_t buf[SSU_MTU];
Fragment (int n, const uint8_t * b, int l, bool last):
fragmentNum (n), len (l), isLast (last) { memcpy (buf, b, len); };
};
struct FragmentCmp
{
bool operator() (const Fragment * f1, const Fragment * f2) const
{
return f1->fragmentNum < f2->fragmentNum;
};
};
struct IncompleteMessage
{
I2NPMessage * msg;
int nextFragmentNum;
std::set<Fragment *, FragmentCmp> savedFragments;
IncompleteMessage (I2NPMessage * m): msg (m), nextFragmentNum (0) {};
~IncompleteMessage () { for (auto it: savedFragments) { delete it; }; };
};
2014-04-22 19:39:26 +04:00
class SSUSession;
class SSUData
{
public:
SSUData (SSUSession& session);
~SSUData ();
void ProcessMessage (uint8_t * buf, size_t len);
2014-06-10 18:39:29 +04:00
void Send (i2p::I2NPMessage * msg);
2014-04-22 19:39:26 +04:00
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-04-22 19:39:26 +04:00
private:
2014-07-15 06:06:58 +04:00
2014-04-22 19:39:26 +04:00
SSUSession& m_Session;
std::map<uint32_t, IncompleteMessage *> m_IncomleteMessages;
2014-06-12 19:14:22 +04:00
std::map<uint32_t, std::vector<uint8_t *> > m_SentMessages; // msgID -> fragments
2014-04-22 19:39:26 +04:00
};
}
}
#endif