i2pd/Streaming.h

272 lines
9.5 KiB
C
Raw Normal View History

2013-12-13 06:36:24 +04:00
#ifndef STREAMING_H__
#define STREAMING_H__
#include <inttypes.h>
2014-03-20 00:38:55 +04:00
#include <string>
2015-01-26 00:18:26 +03:00
#include <sstream>
#include <map>
#include <set>
2014-04-12 05:13:52 +04:00
#include <queue>
2014-08-05 00:30:37 +04:00
#include <functional>
2014-11-23 19:33:58 +03:00
#include <memory>
2015-01-26 00:18:26 +03:00
#include <mutex>
2014-03-24 00:00:05 +04:00
#include <boost/asio.hpp>
#include "I2PEndian.h"
2013-12-31 05:46:33 +04:00
#include "Identity.h"
2014-01-02 03:19:03 +04:00
#include "LeaseSet.h"
#include "I2NPProtocol.h"
2014-08-30 06:10:00 +04:00
#include "Garlic.h"
2014-10-07 04:18:18 +04:00
#include "Tunnel.h"
2013-12-13 06:36:24 +04:00
namespace i2p
{
namespace client
{
class ClientDestination;
}
2013-12-13 06:36:24 +04:00
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-13 00:57:10 +04:00
const size_t STREAMING_MTU = 1730;
2014-08-07 03:19:59 +04:00
const size_t MAX_PACKET_SIZE = 4096;
2014-08-09 22:47:00 +04:00
const size_t COMPRESSION_THRESHOLD_SIZE = 66;
2014-10-10 19:53:27 +04:00
const int ACK_SEND_TIMEOUT = 200; // in milliseconds
const int MAX_NUM_RESEND_ATTEMPTS = 6;
2015-01-26 01:43:34 +03:00
const int WINDOW_SIZE = 6; // in messages
2015-01-29 23:34:43 +03:00
const int MIN_WINDOW_SIZE = 1;
const int MAX_WINDOW_SIZE = 128;
const int INITIAL_RTT = 8000; // in milliseconds
2015-03-10 18:11:42 +03:00
const int INITIAL_RTO = 9000; // in milliseconds
2014-08-09 22:47:00 +04:00
2014-01-11 05:21:38 +04:00
struct Packet
{
size_t len, offset;
2014-11-27 05:42:14 +03:00
uint8_t buf[MAX_PACKET_SIZE];
2015-02-03 21:46:44 +03:00
uint64_t sendTime;
2014-08-12 03:32:07 +04:00
Packet (): len (0), offset (0), sendTime (0) {};
2014-01-11 05:21:38 +04:00
uint8_t * GetBuffer () { return buf + offset; };
size_t GetLength () const { return len - offset; };
uint32_t GetSendStreamID () const { return bufbe32toh (buf); };
uint32_t GetReceiveStreamID () const { return bufbe32toh (buf + 4); };
uint32_t GetSeqn () const { return bufbe32toh (buf + 8); };
uint32_t GetAckThrough () const { return bufbe32toh (buf + 12); };
2014-01-30 08:13:59 +04:00
uint8_t GetNACKCount () const { return buf[16]; };
uint32_t GetNACK (int i) const { return bufbe32toh (buf + 17 + 4 * i); };
2014-01-30 08:13:59 +04:00
const uint8_t * GetOption () const { return buf + 17 + GetNACKCount ()*4 + 3; }; // 3 = resendDelay + flags
uint16_t GetFlags () const { return bufbe16toh (GetOption () - 2); };
uint16_t GetOptionSize () const { return bufbe16toh (GetOption ()); };
2014-01-30 08:13:59 +04:00
const uint8_t * GetOptionData () const { return GetOption () + 2; };
const uint8_t * GetPayload () const { return GetOptionData () + GetOptionSize (); };
2014-08-06 23:44:00 +04:00
bool IsSYN () const { return GetFlags () & PACKET_FLAG_SYNCHRONIZE; };
2014-08-11 02:27:23 +04:00
bool IsNoAck () const { return GetFlags () & PACKET_FLAG_NO_ACK; };
};
struct PacketCmp
{
bool operator() (const Packet * p1, const Packet * p2) const
{
2014-01-30 08:13:59 +04:00
return p1->GetSeqn () < p2->GetSeqn ();
};
2014-01-11 05:21:38 +04:00
};
2015-03-09 02:36:33 +03:00
enum StreamStatus
{
eStreamStatusNew = 0,
2015-03-09 02:36:33 +03:00
eStreamStatusOpen,
eStreamStatusReset,
eStreamStatusClosing,
eStreamStatusClosed
};
2014-01-11 05:21:38 +04:00
2013-12-31 05:46:33 +04:00
class StreamingDestination;
2014-11-23 19:33:58 +03:00
class Stream: public std::enable_shared_from_this<Stream>
{
public:
2015-04-09 22:07:25 +03:00
typedef std::function<void (const boost::system::error_code& ecode)> SendHandler;
2014-10-23 05:36:11 +04:00
Stream (boost::asio::io_service& service, StreamingDestination& local,
2015-01-27 19:27:58 +03:00
std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0); // outgoing
2014-10-07 18:44:42 +04:00
Stream (boost::asio::io_service& service, StreamingDestination& local); // incoming
2014-08-01 22:54:14 +04:00
2014-01-11 05:21:38 +04:00
~Stream ();
uint32_t GetSendStreamID () const { return m_SendStreamID; };
uint32_t GetRecvStreamID () const { return m_RecvStreamID; };
2015-01-27 19:27:58 +03:00
std::shared_ptr<const i2p::data::LeaseSet> GetRemoteLeaseSet () const { return m_RemoteLeaseSet; };
2014-09-29 22:18:06 +04:00
const i2p::data::IdentityEx& GetRemoteIdentity () const { return m_RemoteIdentity; };
bool IsOpen () const { return m_Status == eStreamStatusOpen; };
2014-01-10 07:26:30 +04:00
bool IsEstablished () const { return m_SendStreamID; };
StreamStatus GetStatus () const { return m_Status; };
2014-10-07 18:44:42 +04:00
StreamingDestination& GetLocalDestination () { return m_LocalDestination; };
2014-01-02 03:19:03 +04:00
2014-01-11 05:21:38 +04:00
void HandleNextPacket (Packet * packet);
2014-10-02 05:18:41 +04:00
size_t Send (const uint8_t * buf, size_t len);
2015-04-09 22:07:25 +03:00
void AsyncSend (const uint8_t * buf, size_t len, SendHandler handler);
2014-04-10 23:34:51 +04:00
2014-03-25 22:26:39 +04:00
template<typename Buffer, typename ReceiveHandler>
void AsyncReceive (const Buffer& buffer, ReceiveHandler handler, int timeout = 0);
size_t ReadSome (uint8_t * buf, size_t len) { return ConcatenatePackets (buf, len); };
2014-01-13 00:57:10 +04:00
void Close ();
2014-12-17 23:31:13 +03:00
void Cancel () { m_ReceiveTimer.cancel (); };
2014-10-14 01:03:27 +04:00
size_t GetNumSentBytes () const { return m_NumSentBytes; };
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
2014-10-18 23:50:34 +04:00
size_t GetSendQueueSize () const { return m_SentPackets.size (); };
size_t GetReceiveQueueSize () const { return m_ReceiveQueue.size (); };
2015-01-31 05:41:32 +03:00
size_t GetSendBufferSize () const { return m_SendBuffer.rdbuf ()->in_avail (); };
2015-02-03 21:46:44 +03:00
int GetWindowSize () const { return m_WindowSize; };
int GetRTT () const { return m_RTT; };
2014-10-14 01:03:27 +04:00
2014-01-11 07:23:17 +04:00
private:
2015-03-09 19:06:35 +03:00
void Terminate ();
2015-01-26 00:18:26 +03:00
void SendBuffer ();
2014-08-08 06:03:25 +04:00
void SendQuickAck ();
void SendClose ();
2014-03-25 03:27:20 +04:00
bool SendPacket (Packet * packet);
2014-08-13 00:35:35 +04:00
void SendPackets (const std::vector<Packet *>& packets);
void SavePacket (Packet * packet);
2014-02-02 07:20:41 +04:00
void ProcessPacket (Packet * packet);
2014-08-11 02:27:23 +04:00
void ProcessAck (Packet * packet);
2014-03-26 23:06:27 +04:00
size_t ConcatenatePackets (uint8_t * buf, size_t len);
2014-03-23 17:25:16 +04:00
void UpdateCurrentRemoteLease (bool expired = false);
2014-03-25 22:26:39 +04:00
template<typename Buffer, typename ReceiveHandler>
void HandleReceiveTimer (const boost::system::error_code& ecode, const Buffer& buffer, ReceiveHandler handler);
2015-03-23 20:08:04 +03:00
2014-08-11 02:27:23 +04:00
void ScheduleResend ();
void HandleResendTimer (const boost::system::error_code& ecode);
2014-10-10 19:53:27 +04:00
void HandleAckSendTimer (const boost::system::error_code& ecode);
2014-10-23 05:36:11 +04:00
2015-06-22 05:29:50 +03:00
std::shared_ptr<I2NPMessage> CreateDataMessage (const uint8_t * payload, size_t len);
2014-08-11 02:27:23 +04:00
private:
2014-03-24 00:00:05 +04:00
boost::asio::io_service& m_Service;
2014-08-07 03:19:59 +04:00
uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber;
int32_t m_LastReceivedSequenceNumber;
2015-03-09 02:36:33 +03:00
StreamStatus m_Status;
bool m_IsAckSendScheduled;
2014-10-07 18:44:42 +04:00
StreamingDestination& m_LocalDestination;
2014-08-23 03:47:29 +04:00
i2p::data::IdentityEx m_RemoteIdentity;
2015-01-27 19:27:58 +03:00
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
2015-01-22 23:31:34 +03:00
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
2014-03-23 17:25:16 +04:00
i2p::data::Lease m_CurrentRemoteLease;
2015-01-27 22:55:46 +03:00
std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel;
2014-04-12 05:13:52 +04:00
std::queue<Packet *> m_ReceiveQueue;
std::set<Packet *, PacketCmp> m_SavedPackets;
2014-08-11 02:27:23 +04:00
std::set<Packet *, PacketCmp> m_SentPackets;
2014-10-10 19:53:27 +04:00
boost::asio::deadline_timer m_ReceiveTimer, m_ResendTimer, m_AckSendTimer;
2014-10-14 01:03:27 +04:00
size_t m_NumSentBytes, m_NumReceivedBytes;
2014-10-23 05:36:11 +04:00
uint16_t m_Port;
2015-01-26 00:18:26 +03:00
std::mutex m_SendBufferMutex;
std::stringstream m_SendBuffer;
2015-03-10 18:11:42 +03:00
int m_WindowSize, m_RTT, m_RTO;
2015-01-29 23:34:43 +03:00
uint64_t m_LastWindowSizeIncreaseTime;
int m_NumResendAttempts;
2015-04-09 22:07:25 +03:00
SendHandler m_SendHandler;
};
2014-03-25 22:26:39 +04:00
class StreamingDestination
{
public:
2014-11-23 19:33:58 +03:00
typedef std::function<void (std::shared_ptr<Stream>)> Acceptor;
2014-11-18 17:33:58 +03:00
2015-03-02 05:08:34 +03:00
StreamingDestination (i2p::client::ClientDestination& owner, uint16_t localPort = 0):
m_Owner (owner), m_LocalPort (localPort) {};
~StreamingDestination () {};
void Start ();
void Stop ();
2015-01-27 19:27:58 +03:00
std::shared_ptr<Stream> CreateNewOutgoingStream (std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0);
2014-11-23 19:33:58 +03:00
void DeleteStream (std::shared_ptr<Stream> stream);
2014-11-18 17:33:58 +03:00
void SetAcceptor (const Acceptor& acceptor) { m_Acceptor = acceptor; };
2015-02-01 17:34:32 +03:00
void ResetAcceptor () { if (m_Acceptor) m_Acceptor (nullptr); m_Acceptor = nullptr; };
bool IsAcceptorSet () const { return m_Acceptor != nullptr; };
i2p::client::ClientDestination& GetOwner () { return m_Owner; };
2015-03-02 05:08:34 +03:00
uint16_t GetLocalPort () const { return m_LocalPort; };
void HandleDataMessagePayload (const uint8_t * buf, size_t len);
private:
void HandleNextPacket (Packet * packet);
2014-11-23 19:33:58 +03:00
std::shared_ptr<Stream> CreateNewIncomingStream ();
private:
i2p::client::ClientDestination& m_Owner;
2015-03-02 05:08:34 +03:00
uint16_t m_LocalPort;
std::mutex m_StreamsMutex;
2014-11-23 19:33:58 +03:00
std::map<uint32_t, std::shared_ptr<Stream> > m_Streams;
2014-11-18 17:33:58 +03:00
Acceptor m_Acceptor;
public:
// for HTTP only
const decltype(m_Streams)& GetStreams () const { return m_Streams; };
};
2014-03-25 22:26:39 +04:00
//-------------------------------------------------
template<typename Buffer, typename ReceiveHandler>
void Stream::AsyncReceive (const Buffer& buffer, ReceiveHandler handler, int timeout)
{
2015-04-02 22:10:02 +03:00
auto s = shared_from_this();
m_Service.post ([=](void)
2014-03-27 23:56:13 +04:00
{
2015-04-02 22:10:02 +03:00
if (!m_ReceiveQueue.empty () || m_Status == eStreamStatusReset)
s->HandleReceiveTimer (boost::asio::error::make_error_code (boost::asio::error::operation_aborted), buffer, handler);
else
{
2015-04-05 20:56:41 +03:00
s->m_ReceiveTimer.expires_from_now (boost::posix_time::seconds(timeout));
s->m_ReceiveTimer.async_wait ([=](const boost::system::error_code& ecode)
2015-04-02 22:10:02 +03:00
{ s->HandleReceiveTimer (ecode, buffer, handler); });
}
});
2014-03-25 22:26:39 +04:00
}
template<typename Buffer, typename ReceiveHandler>
void Stream::HandleReceiveTimer (const boost::system::error_code& ecode, const Buffer& buffer, ReceiveHandler handler)
{
2014-03-26 23:06:27 +04:00
size_t received = ConcatenatePackets (boost::asio::buffer_cast<uint8_t *>(buffer), boost::asio::buffer_size(buffer));
if (received > 0)
handler (boost::system::error_code (), received);
else if (ecode == boost::asio::error::operation_aborted)
{
2014-03-25 22:26:39 +04:00
// timeout not expired
if (m_Status == eStreamStatusReset)
handler (boost::asio::error::make_error_code (boost::asio::error::connection_reset), 0);
else
handler (boost::asio::error::make_error_code (boost::asio::error::operation_aborted), 0);
}
2014-03-25 22:26:39 +04:00
else
// timeout expired
2014-03-26 23:06:27 +04:00
handler (boost::asio::error::make_error_code (boost::asio::error::timed_out), received);
2014-03-25 22:26:39 +04:00
}
2013-12-13 06:36:24 +04:00
}
}
#endif