2013-12-13 02:36:24 +00:00
|
|
|
#ifndef STREAMING_H__
|
|
|
|
#define STREAMING_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2013-12-20 02:19:44 +00:00
|
|
|
#include <map>
|
2014-01-26 23:22:30 +00:00
|
|
|
#include <set>
|
2014-01-01 23:19:03 +00:00
|
|
|
#include <cryptopp/dsa.h>
|
2014-01-26 23:22:30 +00:00
|
|
|
#include "I2PEndian.h"
|
2014-01-11 01:21:38 +00:00
|
|
|
#include "Queue.h"
|
2013-12-31 01:46:33 +00:00
|
|
|
#include "Identity.h"
|
2014-01-01 23:19:03 +00:00
|
|
|
#include "LeaseSet.h"
|
2013-12-20 02:19:44 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2014-01-11 03:23:17 +00:00
|
|
|
#include "Tunnel.h"
|
2013-12-13 02:36:24 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace stream
|
|
|
|
{
|
|
|
|
const uint16_t PACKET_FLAG_SYNCHRONIZE = 0x0001;
|
|
|
|
const uint16_t PACKET_FLAG_CLOSE = 0x0002;
|
|
|
|
const uint16_t PACKET_FLAG_RESET = 0x0004;
|
|
|
|
const uint16_t PACKET_FLAG_SIGNATURE_INCLUDED = 0x0008;
|
|
|
|
const uint16_t PACKET_FLAG_SIGNATURE_REQUESTED = 0x0010;
|
|
|
|
const uint16_t PACKET_FLAG_FROM_INCLUDED = 0x0020;
|
|
|
|
const uint16_t PACKET_FLAG_DELAY_REQUESTED = 0x0040;
|
|
|
|
const uint16_t PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED = 0x0080;
|
|
|
|
const uint16_t PACKET_FLAG_PROFILE_INTERACTIVE = 0x0100;
|
|
|
|
const uint16_t PACKET_FLAG_ECHO = 0x0200;
|
|
|
|
const uint16_t PACKET_FLAG_NO_ACK = 0x0400;
|
|
|
|
|
2014-01-12 20:57:10 +00:00
|
|
|
const size_t STREAMING_MTU = 1730;
|
|
|
|
const size_t MAX_PACKET_SIZE = 1754;
|
2014-01-11 01:21:38 +00:00
|
|
|
|
|
|
|
struct Packet
|
|
|
|
{
|
2014-01-12 20:57:10 +00:00
|
|
|
uint8_t buf[1754];
|
2014-01-11 01:21:38 +00:00
|
|
|
size_t len, offset;
|
|
|
|
|
|
|
|
Packet (): len (0), offset (0) {};
|
|
|
|
uint8_t * GetBuffer () { return buf + offset; };
|
|
|
|
size_t GetLength () const { return len - offset; };
|
2014-01-26 23:22:30 +00:00
|
|
|
|
|
|
|
uint32_t GetReceivedSeqn () const { return be32toh (*(uint32_t *)(buf + 8)); };
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PacketCmp
|
|
|
|
{
|
|
|
|
bool operator() (const Packet * p1, const Packet * p2) const
|
|
|
|
{
|
|
|
|
return p1->GetReceivedSeqn () < p2->GetReceivedSeqn ();
|
|
|
|
};
|
2014-01-11 01:21:38 +00:00
|
|
|
};
|
|
|
|
|
2013-12-31 01:46:33 +00:00
|
|
|
class StreamingDestination;
|
2013-12-20 02:19:44 +00:00
|
|
|
class Stream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-01-01 23:19:03 +00:00
|
|
|
Stream (StreamingDestination * local, const i2p::data::LeaseSet * remote);
|
2014-01-11 01:21:38 +00:00
|
|
|
~Stream ();
|
2013-12-20 02:19:44 +00:00
|
|
|
uint32_t GetSendStreamID () const { return m_SendStreamID; };
|
|
|
|
uint32_t GetRecvStreamID () const { return m_RecvStreamID; };
|
2014-01-01 23:19:03 +00:00
|
|
|
const i2p::data::LeaseSet * GetRemoteLeaseSet () const { return m_RemoteLeaseSet; };
|
2014-01-12 20:57:10 +00:00
|
|
|
bool IsOpen () const { return m_IsOpen; };
|
2014-01-10 03:26:30 +00:00
|
|
|
bool IsEstablished () const { return m_SendStreamID; };
|
2014-01-01 23:19:03 +00:00
|
|
|
|
2014-01-11 01:21:38 +00:00
|
|
|
void HandleNextPacket (Packet * packet);
|
2014-01-01 23:19:03 +00:00
|
|
|
size_t Send (uint8_t * buf, size_t len, int timeout); // timeout in seconds
|
2014-01-13 02:41:25 +00:00
|
|
|
size_t Receive (uint8_t * buf, size_t len, int timeout = 0); // returns 0 if timeout expired
|
2014-01-12 20:57:10 +00:00
|
|
|
void Close ();
|
|
|
|
|
2014-01-11 03:23:17 +00:00
|
|
|
private:
|
|
|
|
|
2014-01-12 20:57:10 +00:00
|
|
|
void ConnectAndSend (uint8_t * buf, size_t len);
|
2014-01-11 03:23:17 +00:00
|
|
|
void SendQuickAck ();
|
2014-01-26 23:22:30 +00:00
|
|
|
|
|
|
|
void SavePacket (Packet * packet);
|
2013-12-20 02:19:44 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-01-11 03:23:17 +00:00
|
|
|
uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber, m_LastReceivedSequenceNumber;
|
2014-01-12 20:57:10 +00:00
|
|
|
bool m_IsOpen;
|
2013-12-31 01:46:33 +00:00
|
|
|
StreamingDestination * m_LocalDestination;
|
2014-01-01 23:19:03 +00:00
|
|
|
const i2p::data::LeaseSet * m_RemoteLeaseSet;
|
2014-01-11 01:21:38 +00:00
|
|
|
i2p::util::Queue<Packet> m_ReceiveQueue;
|
2014-01-26 23:22:30 +00:00
|
|
|
std::set<Packet *, PacketCmp> m_SavedPackets;
|
2014-01-11 03:23:17 +00:00
|
|
|
i2p::tunnel::OutboundTunnel * m_OutboundTunnel;
|
2013-12-20 02:19:44 +00:00
|
|
|
};
|
|
|
|
|
2013-12-13 02:36:24 +00:00
|
|
|
class StreamingDestination
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2013-12-31 01:46:33 +00:00
|
|
|
StreamingDestination ();
|
2014-01-09 03:47:22 +00:00
|
|
|
~StreamingDestination ();
|
|
|
|
|
2014-01-01 23:19:03 +00:00
|
|
|
const i2p::data::Keys& GetKeys () const { return m_Keys; };
|
|
|
|
const i2p::data::Identity& GetIdentity () const { return m_Identity; };
|
2014-01-09 03:47:22 +00:00
|
|
|
I2NPMessage * GetLeaseSet ();
|
2014-01-01 23:19:03 +00:00
|
|
|
void Sign (uint8_t * buf, int len, uint8_t * signature) const;
|
2013-12-31 01:46:33 +00:00
|
|
|
|
2014-01-01 23:19:03 +00:00
|
|
|
Stream * CreateNewStream (const i2p::data::LeaseSet * remote);
|
|
|
|
void DeleteStream (Stream * stream);
|
2014-01-11 01:21:38 +00:00
|
|
|
void HandleNextPacket (Packet * packet);
|
2014-01-09 03:47:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
I2NPMessage * CreateLeaseSet () const;
|
2013-12-31 01:46:33 +00:00
|
|
|
|
2013-12-20 02:19:44 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
std::map<uint32_t, Stream *> m_Streams;
|
2013-12-31 01:46:33 +00:00
|
|
|
i2p::data::Keys m_Keys;
|
|
|
|
i2p::data::Identity m_Identity;
|
|
|
|
i2p::data::IdentHash m_IdentHash;
|
2014-01-01 23:19:03 +00:00
|
|
|
|
2014-01-09 03:47:22 +00:00
|
|
|
I2NPMessage * m_LeaseSet;
|
|
|
|
|
2014-01-01 23:19:03 +00:00
|
|
|
CryptoPP::DSA::PrivateKey m_SigningPrivateKey;
|
2013-12-20 02:19:44 +00:00
|
|
|
};
|
2014-01-01 23:19:03 +00:00
|
|
|
|
|
|
|
Stream * CreateStream (const i2p::data::LeaseSet * remote);
|
2014-01-13 02:41:25 +00:00
|
|
|
void DeleteStream (Stream * stream);
|
2013-12-20 02:19:44 +00:00
|
|
|
|
2013-12-13 02:36:24 +00:00
|
|
|
// assuming data is I2CP message
|
|
|
|
void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len);
|
2013-12-20 02:19:44 +00:00
|
|
|
I2NPMessage * CreateDataMessage (Stream * s, uint8_t * payload, size_t len);
|
2013-12-13 02:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|