2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2021-06-29 23:08:11 +00:00
|
|
|
* Copyright (c) 2013-2021, 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
|
|
|
|
*/
|
|
|
|
|
2013-12-13 02:36:24 +00:00
|
|
|
#ifndef STREAMING_H__
|
|
|
|
#define STREAMING_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-03-19 20:38:55 +00:00
|
|
|
#include <string>
|
2021-06-29 23:08:11 +00:00
|
|
|
#include <unordered_map>
|
2014-01-26 23:22:30 +00:00
|
|
|
#include <set>
|
2014-04-12 01:13:52 +00:00
|
|
|
#include <queue>
|
2014-08-04 20:30:37 +00:00
|
|
|
#include <functional>
|
2014-11-23 16:33:58 +00:00
|
|
|
#include <memory>
|
2015-01-25 21:18:26 +00:00
|
|
|
#include <mutex>
|
2014-03-23 20:00:05 +00:00
|
|
|
#include <boost/asio.hpp>
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Base.h"
|
2014-01-26 23:22:30 +00:00
|
|
|
#include "I2PEndian.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-08-30 02:10:00 +00:00
|
|
|
#include "Garlic.h"
|
2014-10-07 00:18:18 +00:00
|
|
|
#include "Tunnel.h"
|
2017-01-11 02:31:52 +00:00
|
|
|
#include "util.h" // MemoryPool
|
2013-12-13 02:36:24 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-22 15:46:54 +00:00
|
|
|
namespace client
|
|
|
|
{
|
2016-05-25 20:18:02 +00:00
|
|
|
class ClientDestination;
|
2014-10-22 15:46:54 +00:00
|
|
|
}
|
2013-12-13 02:36:24 +00: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;
|
2019-02-05 20:32:18 +00:00
|
|
|
const uint16_t PACKET_FLAG_OFFLINE_SIGNATURE = 0x0800;
|
2013-12-13 02:36:24 +00:00
|
|
|
|
2014-01-12 20:57:10 +00:00
|
|
|
const size_t STREAMING_MTU = 1730;
|
2020-05-19 00:45:25 +00:00
|
|
|
const size_t STREAMING_MTU_RATCHETS = 1812;
|
2014-08-06 23:19:59 +00:00
|
|
|
const size_t MAX_PACKET_SIZE = 4096;
|
2018-01-06 03:48:51 +00:00
|
|
|
const size_t COMPRESSION_THRESHOLD_SIZE = 66;
|
|
|
|
const int MAX_NUM_RESEND_ATTEMPTS = 6;
|
2015-01-25 22:43:34 +00:00
|
|
|
const int WINDOW_SIZE = 6; // in messages
|
2015-01-29 20:34:43 +00:00
|
|
|
const int MIN_WINDOW_SIZE = 1;
|
2018-01-06 03:48:51 +00:00
|
|
|
const int MAX_WINDOW_SIZE = 128;
|
2015-01-29 20:34:43 +00:00
|
|
|
const int INITIAL_RTT = 8000; // in milliseconds
|
2015-03-10 15:11:42 +00:00
|
|
|
const int INITIAL_RTO = 9000; // in milliseconds
|
2017-11-24 20:37:17 +00:00
|
|
|
const int SYN_TIMEOUT = 200; // how long we wait for SYN after follow-on, in milliseconds
|
2015-12-15 03:23:28 +00:00
|
|
|
const size_t MAX_PENDING_INCOMING_BACKLOG = 128;
|
|
|
|
const int PENDING_INCOMING_TIMEOUT = 10; // in seconds
|
2021-01-18 17:58:27 +00:00
|
|
|
const int MAX_RECEIVE_TIMEOUT = 20; // in seconds
|
2016-07-28 15:16:29 +00:00
|
|
|
|
2014-01-11 01:21:38 +00:00
|
|
|
struct Packet
|
|
|
|
{
|
|
|
|
size_t len, offset;
|
2018-01-06 03:48:51 +00:00
|
|
|
uint8_t buf[MAX_PACKET_SIZE];
|
2015-02-03 18:46:44 +00:00
|
|
|
uint64_t sendTime;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2015-03-21 20:26:14 +00:00
|
|
|
Packet (): len (0), offset (0), sendTime (0) {};
|
2014-01-11 01:21:38 +00:00
|
|
|
uint8_t * GetBuffer () { return buf + offset; };
|
|
|
|
size_t GetLength () const { return len - offset; };
|
2014-01-26 23:22:30 +00:00
|
|
|
|
2014-12-29 22:04:02 +00:00
|
|
|
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 04:13:59 +00:00
|
|
|
uint8_t GetNACKCount () const { return buf[16]; };
|
2014-12-29 22:04:02 +00:00
|
|
|
uint32_t GetNACK (int i) const { return bufbe32toh (buf + 17 + 4 * i); };
|
2014-01-30 04:13:59 +00:00
|
|
|
const uint8_t * GetOption () const { return buf + 17 + GetNACKCount ()*4 + 3; }; // 3 = resendDelay + flags
|
2014-12-29 22:04:02 +00:00
|
|
|
uint16_t GetFlags () const { return bufbe16toh (GetOption () - 2); };
|
|
|
|
uint16_t GetOptionSize () const { return bufbe16toh (GetOption ()); };
|
2014-01-30 04:13:59 +00:00
|
|
|
const uint8_t * GetOptionData () const { return GetOption () + 2; };
|
|
|
|
const uint8_t * GetPayload () const { return GetOptionData () + GetOptionSize (); };
|
2014-08-06 19:44:00 +00:00
|
|
|
|
|
|
|
bool IsSYN () const { return GetFlags () & PACKET_FLAG_SYNCHRONIZE; };
|
2014-08-10 22:27:23 +00:00
|
|
|
bool IsNoAck () const { return GetFlags () & PACKET_FLAG_NO_ACK; };
|
2020-09-30 21:12:28 +00:00
|
|
|
bool IsEcho () const { return GetFlags () & PACKET_FLAG_ECHO; };
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2014-01-26 23:22:30 +00:00
|
|
|
|
|
|
|
struct PacketCmp
|
|
|
|
{
|
|
|
|
bool operator() (const Packet * p1, const Packet * p2) const
|
2018-01-06 04:01:44 +00:00
|
|
|
{
|
2018-01-06 03:48:51 +00:00
|
|
|
return p1->GetSeqn () < p2->GetSeqn ();
|
2014-01-26 23:22:30 +00:00
|
|
|
};
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2015-03-08 23:36:33 +00:00
|
|
|
|
2017-02-26 20:05:14 +00:00
|
|
|
typedef std::function<void (const boost::system::error_code& ecode)> SendHandler;
|
|
|
|
struct SendBuffer
|
|
|
|
{
|
|
|
|
uint8_t * buf;
|
|
|
|
size_t len, offset;
|
|
|
|
SendHandler handler;
|
|
|
|
|
|
|
|
SendBuffer (const uint8_t * b, size_t l, SendHandler h):
|
|
|
|
len(l), offset (0), handler(h)
|
|
|
|
{
|
|
|
|
buf = new uint8_t[len];
|
|
|
|
memcpy (buf, b, len);
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2020-11-29 19:59:34 +00:00
|
|
|
SendBuffer (size_t l): // creat empty buffer
|
|
|
|
len(l), offset (0)
|
|
|
|
{
|
|
|
|
buf = new uint8_t[len];
|
|
|
|
}
|
2017-02-26 20:05:14 +00:00
|
|
|
~SendBuffer ()
|
|
|
|
{
|
|
|
|
delete[] buf;
|
|
|
|
if (handler) handler(boost::system::error_code ());
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
2017-02-26 20:05:14 +00:00
|
|
|
size_t GetRemainingSize () const { return len - offset; };
|
2018-01-06 03:48:51 +00:00
|
|
|
const uint8_t * GetRemaningBuffer () const { return buf + offset; };
|
2017-02-26 20:05:14 +00:00
|
|
|
void Cancel () { if (handler) handler (boost::asio::error::make_error_code (boost::asio::error::operation_aborted)); handler = nullptr; };
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2017-02-26 20:05:14 +00:00
|
|
|
|
|
|
|
class SendBufferQueue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SendBufferQueue (): m_Size (0) {};
|
|
|
|
~SendBufferQueue () { CleanUp (); };
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
void Add (const uint8_t * buf, size_t len, SendHandler handler);
|
2020-11-29 19:59:34 +00:00
|
|
|
void Add (std::shared_ptr<SendBuffer> buf);
|
2018-01-06 03:48:51 +00:00
|
|
|
size_t Get (uint8_t * buf, size_t len);
|
2017-02-26 20:05:14 +00:00
|
|
|
size_t GetSize () const { return m_Size; };
|
|
|
|
bool IsEmpty () const { return m_Buffers.empty (); };
|
|
|
|
void CleanUp ();
|
2018-01-06 03:48:51 +00:00
|
|
|
|
|
|
|
private:
|
2017-02-26 20:05:14 +00:00
|
|
|
|
|
|
|
std::list<std::shared_ptr<SendBuffer> > m_Buffers;
|
|
|
|
size_t m_Size;
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
|
|
|
|
2015-03-08 23:36:33 +00:00
|
|
|
enum StreamStatus
|
|
|
|
{
|
2015-04-10 15:52:14 +00:00
|
|
|
eStreamStatusNew = 0,
|
2015-03-08 23:36:33 +00:00
|
|
|
eStreamStatusOpen,
|
|
|
|
eStreamStatusReset,
|
|
|
|
eStreamStatusClosing,
|
|
|
|
eStreamStatusClosed
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
|
|
|
|
2013-12-31 01:46:33 +00:00
|
|
|
class StreamingDestination;
|
2014-11-23 16:33:58 +00:00
|
|
|
class Stream: public std::enable_shared_from_this<Stream>
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
2013-12-20 02:19:44 +00:00
|
|
|
public:
|
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
Stream (boost::asio::io_service& service, StreamingDestination& local,
|
2015-01-27 16:27:58 +00:00
|
|
|
std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0); // outgoing
|
2018-01-06 03:48:51 +00:00
|
|
|
Stream (boost::asio::io_service& service, StreamingDestination& local); // incoming
|
2014-08-01 18:54:14 +00:00
|
|
|
|
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; };
|
2015-01-27 16:27:58 +00:00
|
|
|
std::shared_ptr<const i2p::data::LeaseSet> GetRemoteLeaseSet () const { return m_RemoteLeaseSet; };
|
2015-11-03 14:15:49 +00:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> GetRemoteIdentity () const { return m_RemoteIdentity; };
|
2015-04-10 15:52:14 +00:00
|
|
|
bool IsOpen () const { return m_Status == eStreamStatusOpen; };
|
2014-01-10 03:26:30 +00:00
|
|
|
bool IsEstablished () const { return m_SendStreamID; };
|
2015-04-10 15:52:14 +00:00
|
|
|
StreamStatus GetStatus () const { return m_Status; };
|
2014-10-07 14:44:42 +00:00
|
|
|
StreamingDestination& GetLocalDestination () { return m_LocalDestination; };
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2014-01-11 01:21:38 +00:00
|
|
|
void HandleNextPacket (Packet * packet);
|
2020-09-30 21:12:28 +00:00
|
|
|
void HandlePing (Packet * packet);
|
2014-10-02 01:18:41 +00:00
|
|
|
size_t Send (const uint8_t * buf, size_t len);
|
2015-04-09 19:07:25 +00:00
|
|
|
void AsyncSend (const uint8_t * buf, size_t len, SendHandler handler);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2014-03-25 18:26:39 +00:00
|
|
|
template<typename Buffer, typename ReceiveHandler>
|
|
|
|
void AsyncReceive (const Buffer& buffer, ReceiveHandler handler, int timeout = 0);
|
2014-12-26 13:55:24 +00:00
|
|
|
size_t ReadSome (uint8_t * buf, size_t len) { return ConcatenatePackets (buf, len); };
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2018-04-25 15:27:56 +00:00
|
|
|
void AsyncClose() { m_Service.post(std::bind(&Stream::Close, shared_from_this())); };
|
2018-04-24 13:45:16 +00:00
|
|
|
|
2018-04-25 15:27:56 +00:00
|
|
|
/** only call close from destination thread, use Stream::AsyncClose for other threads */
|
2014-01-12 20:57:10 +00:00
|
|
|
void Close ();
|
2014-12-17 20:31:13 +00:00
|
|
|
void Cancel () { m_ReceiveTimer.cancel (); };
|
2014-10-13 21:03:27 +00:00
|
|
|
|
|
|
|
size_t GetNumSentBytes () const { return m_NumSentBytes; };
|
|
|
|
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
|
2014-10-18 19:50:34 +00:00
|
|
|
size_t GetSendQueueSize () const { return m_SentPackets.size (); };
|
|
|
|
size_t GetReceiveQueueSize () const { return m_ReceiveQueue.size (); };
|
2017-02-26 20:05:14 +00:00
|
|
|
size_t GetSendBufferSize () const { return m_SendBuffer.GetSize (); };
|
2015-02-03 18:46:44 +00:00
|
|
|
int GetWindowSize () const { return m_WindowSize; };
|
|
|
|
int GetRTT () const { return m_RTT; };
|
2014-01-11 03:23:17 +00:00
|
|
|
|
2020-03-04 23:31:22 +00:00
|
|
|
void Terminate (bool deleteFromDestination = true);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-07-28 19:34:32 +00:00
|
|
|
private:
|
2015-03-09 16:06:35 +00:00
|
|
|
|
2017-01-23 02:22:12 +00:00
|
|
|
void CleanUp ();
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2015-01-25 21:18:26 +00:00
|
|
|
void SendBuffer ();
|
2014-08-08 02:03:25 +00:00
|
|
|
void SendQuickAck ();
|
2015-04-13 15:38:23 +00:00
|
|
|
void SendClose ();
|
2014-03-24 23:27:20 +00:00
|
|
|
bool SendPacket (Packet * packet);
|
2014-08-12 20:35:35 +00:00
|
|
|
void SendPackets (const std::vector<Packet *>& packets);
|
2016-10-23 00:08:15 +00:00
|
|
|
void SendUpdatedLeaseSet ();
|
2014-01-26 23:22:30 +00:00
|
|
|
|
|
|
|
void SavePacket (Packet * packet);
|
2014-02-02 03:20:41 +00:00
|
|
|
void ProcessPacket (Packet * packet);
|
2019-02-05 20:32:18 +00:00
|
|
|
bool ProcessOptions (uint16_t flags, Packet * packet);
|
2014-08-10 22:27:23 +00:00
|
|
|
void ProcessAck (Packet * packet);
|
2014-03-26 19:06:27 +00:00
|
|
|
size_t ConcatenatePackets (uint8_t * buf, size_t len);
|
2014-03-23 13:25:16 +00:00
|
|
|
|
2015-04-16 15:38:36 +00:00
|
|
|
void UpdateCurrentRemoteLease (bool expired = false);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2014-03-25 18:26:39 +00:00
|
|
|
template<typename Buffer, typename ReceiveHandler>
|
2016-10-23 00:08:15 +00:00
|
|
|
void HandleReceiveTimer (const boost::system::error_code& ecode, const Buffer& buffer, ReceiveHandler handler, int remainingTimeout);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2014-08-10 22:27:23 +00:00
|
|
|
void ScheduleResend ();
|
|
|
|
void HandleResendTimer (const boost::system::error_code& ecode);
|
2014-10-10 15:53:27 +00:00
|
|
|
void HandleAckSendTimer (const boost::system::error_code& ecode);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2013-12-20 02:19:44 +00:00
|
|
|
private:
|
|
|
|
|
2014-03-23 20:00:05 +00:00
|
|
|
boost::asio::io_service& m_Service;
|
2014-08-06 23:19:59 +00:00
|
|
|
uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber;
|
|
|
|
int32_t m_LastReceivedSequenceNumber;
|
2015-03-08 23:36:33 +00:00
|
|
|
StreamStatus m_Status;
|
|
|
|
bool m_IsAckSendScheduled;
|
2014-10-07 14:44:42 +00:00
|
|
|
StreamingDestination& m_LocalDestination;
|
2015-11-03 14:15:49 +00:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> m_RemoteIdentity;
|
2019-02-06 18:36:03 +00:00
|
|
|
std::shared_ptr<const i2p::crypto::Verifier> m_TransientVerifier; // in case of offline key
|
2015-01-27 16:27:58 +00:00
|
|
|
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
|
2015-01-22 20:31:34 +00:00
|
|
|
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
|
2016-02-11 03:51:08 +00:00
|
|
|
std::shared_ptr<const i2p::data::Lease> m_CurrentRemoteLease;
|
2015-01-27 19:55:46 +00:00
|
|
|
std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel;
|
2014-04-12 01:13:52 +00:00
|
|
|
std::queue<Packet *> m_ReceiveQueue;
|
2014-01-26 23:22:30 +00:00
|
|
|
std::set<Packet *, PacketCmp> m_SavedPackets;
|
2014-08-10 22:27:23 +00:00
|
|
|
std::set<Packet *, PacketCmp> m_SentPackets;
|
2014-10-10 15:53:27 +00:00
|
|
|
boost::asio::deadline_timer m_ReceiveTimer, m_ResendTimer, m_AckSendTimer;
|
2014-10-13 21:03:27 +00:00
|
|
|
size_t m_NumSentBytes, m_NumReceivedBytes;
|
2014-10-23 01:36:11 +00:00
|
|
|
uint16_t m_Port;
|
2015-01-25 21:18:26 +00:00
|
|
|
|
|
|
|
std::mutex m_SendBufferMutex;
|
2017-02-26 20:05:14 +00:00
|
|
|
SendBufferQueue m_SendBuffer;
|
2017-11-24 20:37:17 +00:00
|
|
|
int m_WindowSize, m_RTT, m_RTO, m_AckDelay;
|
2015-01-29 20:34:43 +00:00
|
|
|
uint64_t m_LastWindowSizeIncreaseTime;
|
2015-03-21 20:26:14 +00:00
|
|
|
int m_NumResendAttempts;
|
2020-05-19 00:45:25 +00:00
|
|
|
size_t m_MTU;
|
2013-12-20 02:19:44 +00:00
|
|
|
};
|
2014-03-25 18:26:39 +00:00
|
|
|
|
2016-02-08 19:42:20 +00:00
|
|
|
class StreamingDestination: public std::enable_shared_from_this<StreamingDestination>
|
2014-10-22 15:46:54 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-11-23 16:33:58 +00:00
|
|
|
typedef std::function<void (std::shared_ptr<Stream>)> Acceptor;
|
2014-11-18 14:33:58 +00:00
|
|
|
|
2016-05-25 20:18:02 +00:00
|
|
|
StreamingDestination (std::shared_ptr<i2p::client::ClientDestination> owner, uint16_t localPort = 0, bool gzip = true);
|
2018-01-06 03:48:51 +00:00
|
|
|
~StreamingDestination ();
|
2014-10-22 15:46:54 +00:00
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
2015-01-27 16:27:58 +00:00
|
|
|
std::shared_ptr<Stream> CreateNewOutgoingStream (std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0);
|
2018-01-06 03:48:51 +00:00
|
|
|
void DeleteStream (std::shared_ptr<Stream> stream);
|
2020-03-04 20:54:09 +00:00
|
|
|
bool DeleteStream (uint32_t recvStreamID);
|
2015-12-15 03:23:28 +00:00
|
|
|
void SetAcceptor (const Acceptor& acceptor);
|
|
|
|
void ResetAcceptor ();
|
2018-01-06 03:48:51 +00:00
|
|
|
bool IsAcceptorSet () const { return m_Acceptor != nullptr; };
|
2016-12-23 15:09:40 +00:00
|
|
|
void AcceptOnce (const Acceptor& acceptor);
|
2020-05-02 15:13:40 +00:00
|
|
|
void AcceptOnceAcceptor (std::shared_ptr<Stream> stream, Acceptor acceptor, Acceptor prev);
|
2016-12-23 15:09:40 +00:00
|
|
|
|
2016-05-25 20:18:02 +00:00
|
|
|
std::shared_ptr<i2p::client::ClientDestination> GetOwner () const { return m_Owner; };
|
2016-11-26 03:36:35 +00:00
|
|
|
void SetOwner (std::shared_ptr<i2p::client::ClientDestination> owner) { m_Owner = owner; };
|
2015-03-02 02:08:34 +00:00
|
|
|
uint16_t GetLocalPort () const { return m_LocalPort; };
|
2014-10-22 19:01:30 +00:00
|
|
|
|
|
|
|
void HandleDataMessagePayload (const uint8_t * buf, size_t len);
|
2020-05-02 15:13:40 +00:00
|
|
|
std::shared_ptr<I2NPMessage> CreateDataMessage (const uint8_t * payload, size_t len, uint16_t toPort, bool checksum = true);
|
2014-10-22 15:46:54 +00:00
|
|
|
|
2018-01-20 18:06:08 +00:00
|
|
|
Packet * NewPacket () { return m_PacketsPool.Acquire(); }
|
|
|
|
void DeletePacket (Packet * p) { return m_PacketsPool.Release(p); }
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2019-07-26 18:23:21 +00:00
|
|
|
private:
|
|
|
|
|
2014-10-22 19:01:30 +00:00
|
|
|
void HandleNextPacket (Packet * packet);
|
2019-07-26 18:23:21 +00:00
|
|
|
std::shared_ptr<Stream> CreateNewIncomingStream (uint32_t receiveStreamID);
|
2015-12-15 03:23:28 +00:00
|
|
|
void HandlePendingIncomingTimer (const boost::system::error_code& ecode);
|
2014-10-22 15:46:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2016-05-25 20:18:02 +00:00
|
|
|
std::shared_ptr<i2p::client::ClientDestination> m_Owner;
|
2015-03-02 02:08:34 +00:00
|
|
|
uint16_t m_LocalPort;
|
2016-02-29 19:44:15 +00:00
|
|
|
bool m_Gzip; // gzip compression of data messages
|
2014-10-22 15:46:54 +00:00
|
|
|
std::mutex m_StreamsMutex;
|
2021-06-29 23:08:11 +00:00
|
|
|
std::unordered_map<uint32_t, std::shared_ptr<Stream> > m_Streams; // sendStreamID->stream
|
|
|
|
std::unordered_map<uint32_t, std::shared_ptr<Stream> > m_IncomingStreams; // receiveStreamID->stream
|
|
|
|
std::shared_ptr<Stream> m_LastStream;
|
2014-11-18 14:33:58 +00:00
|
|
|
Acceptor m_Acceptor;
|
2015-12-15 03:23:28 +00:00
|
|
|
std::list<std::shared_ptr<Stream> > m_PendingIncomingStreams;
|
|
|
|
boost::asio::deadline_timer m_PendingIncomingTimer;
|
2021-06-29 23:08:11 +00:00
|
|
|
std::unordered_map<uint32_t, std::list<Packet *> > m_SavedPackets; // receiveStreamID->packets, arrived before SYN
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2018-01-20 18:06:08 +00:00
|
|
|
i2p::util::MemoryPool<Packet> m_PacketsPool;
|
2020-05-02 15:13:40 +00:00
|
|
|
i2p::util::MemoryPool<I2NPMessageBuffer<I2NP_MAX_MESSAGE_SIZE> > m_I2NPMsgsPool;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2014-10-22 15:46:54 +00:00
|
|
|
public:
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
i2p::data::GzipInflator m_Inflator;
|
|
|
|
i2p::data::GzipDeflator m_Deflator;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2014-10-22 15:46:54 +00:00
|
|
|
// for HTTP only
|
|
|
|
const decltype(m_Streams)& GetStreams () const { return m_Streams; };
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2014-10-22 15:46:54 +00:00
|
|
|
|
2014-03-25 18:26:39 +00:00
|
|
|
//-------------------------------------------------
|
|
|
|
|
|
|
|
template<typename Buffer, typename ReceiveHandler>
|
|
|
|
void Stream::AsyncReceive (const Buffer& buffer, ReceiveHandler handler, int timeout)
|
|
|
|
{
|
2015-04-02 19:10:02 +00:00
|
|
|
auto s = shared_from_this();
|
2018-01-15 13:19:57 +00:00
|
|
|
m_Service.post ([s, buffer, handler, timeout](void)
|
2014-03-27 19:56:13 +00:00
|
|
|
{
|
2018-01-01 13:28:42 +00:00
|
|
|
if (!s->m_ReceiveQueue.empty () || s->m_Status == eStreamStatusReset)
|
2016-10-23 00:08:15 +00:00
|
|
|
s->HandleReceiveTimer (boost::asio::error::make_error_code (boost::asio::error::operation_aborted), buffer, handler, 0);
|
2015-04-02 19:10:02 +00:00
|
|
|
else
|
|
|
|
{
|
2018-01-15 13:19:57 +00:00
|
|
|
int t = (timeout > MAX_RECEIVE_TIMEOUT) ? MAX_RECEIVE_TIMEOUT : timeout;
|
2016-10-23 00:08:15 +00:00
|
|
|
s->m_ReceiveTimer.expires_from_now (boost::posix_time::seconds(t));
|
2018-01-15 13:19:57 +00:00
|
|
|
int left = timeout - t;
|
|
|
|
auto self = s->shared_from_this();
|
|
|
|
self->m_ReceiveTimer.async_wait (
|
|
|
|
[self, buffer, handler, left](const boost::system::error_code & ec)
|
|
|
|
{
|
|
|
|
self->HandleReceiveTimer(ec, buffer, handler, left);
|
|
|
|
});
|
2015-04-02 19:10:02 +00:00
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
});
|
2014-03-25 18:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Buffer, typename ReceiveHandler>
|
2016-10-23 00:08:15 +00:00
|
|
|
void Stream::HandleReceiveTimer (const boost::system::error_code& ecode, const Buffer& buffer, ReceiveHandler handler, int remainingTimeout)
|
2014-03-25 18:26:39 +00:00
|
|
|
{
|
2014-03-26 19:06:27 +00:00
|
|
|
size_t received = ConcatenatePackets (boost::asio::buffer_cast<uint8_t *>(buffer), boost::asio::buffer_size(buffer));
|
2015-03-12 01:43:30 +00:00
|
|
|
if (received > 0)
|
|
|
|
handler (boost::system::error_code (), received);
|
|
|
|
else if (ecode == boost::asio::error::operation_aborted)
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
|
|
|
// timeout not expired
|
2016-12-13 16:01:13 +00:00
|
|
|
if (m_Status == eStreamStatusReset)
|
2015-03-12 01:43:30 +00:00
|
|
|
handler (boost::asio::error::make_error_code (boost::asio::error::connection_reset), 0);
|
2014-10-04 02:46:20 +00:00
|
|
|
else
|
2018-01-06 03:48:51 +00:00
|
|
|
handler (boost::asio::error::make_error_code (boost::asio::error::operation_aborted), 0);
|
|
|
|
}
|
2014-03-25 18:26:39 +00:00
|
|
|
else
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
2014-03-25 18:26:39 +00:00
|
|
|
// timeout expired
|
2016-10-23 00:08:15 +00:00
|
|
|
if (remainingTimeout <= 0)
|
|
|
|
handler (boost::asio::error::make_error_code (boost::asio::error::timed_out), received);
|
|
|
|
else
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
2016-10-23 00:08:15 +00:00
|
|
|
// itermediate iterrupt
|
|
|
|
SendUpdatedLeaseSet (); // send our leaseset if applicable
|
2018-01-06 03:48:51 +00:00
|
|
|
AsyncReceive (buffer, handler, remainingTimeout);
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 18:26:39 +00:00
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-13 02:36:24 +00:00
|
|
|
|
|
|
|
#endif
|