mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
381 lines
13 KiB
381 lines
13 KiB
11 years ago
|
#ifndef STREAMING_H__
|
||
|
#define STREAMING_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
11 years ago
|
#include <string>
|
||
11 years ago
|
#include <map>
|
||
11 years ago
|
#include <set>
|
||
11 years ago
|
#include <queue>
|
||
10 years ago
|
#include <functional>
|
||
10 years ago
|
#include <memory>
|
||
10 years ago
|
#include <mutex>
|
||
11 years ago
|
#include <boost/asio.hpp>
|
||
9 years ago
|
#include "Base.h"
|
||
11 years ago
|
#include "I2PEndian.h"
|
||
11 years ago
|
#include "Identity.h"
|
||
11 years ago
|
#include "LeaseSet.h"
|
||
11 years ago
|
#include "I2NPProtocol.h"
|
||
10 years ago
|
#include "Garlic.h"
|
||
10 years ago
|
#include "Tunnel.h"
|
||
8 years ago
|
#include "util.h" // MemoryPool
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace client
|
||
|
{
|
||
9 years ago
|
class ClientDestination;
|
||
10 years ago
|
}
|
||
11 years ago
|
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;
|
||
|
|
||
11 years ago
|
const size_t STREAMING_MTU = 1730;
|
||
10 years ago
|
const size_t MAX_PACKET_SIZE = 4096;
|
||
10 years ago
|
const size_t COMPRESSION_THRESHOLD_SIZE = 66;
|
||
10 years ago
|
const int ACK_SEND_TIMEOUT = 200; // in milliseconds
|
||
10 years ago
|
const int MAX_NUM_RESEND_ATTEMPTS = 6;
|
||
10 years ago
|
const int WINDOW_SIZE = 6; // in messages
|
||
10 years ago
|
const int MIN_WINDOW_SIZE = 1;
|
||
|
const int MAX_WINDOW_SIZE = 128;
|
||
|
const int INITIAL_RTT = 8000; // in milliseconds
|
||
10 years ago
|
const int INITIAL_RTO = 9000; // in milliseconds
|
||
9 years ago
|
const size_t MAX_PENDING_INCOMING_BACKLOG = 128;
|
||
|
const int PENDING_INCOMING_TIMEOUT = 10; // in seconds
|
||
8 years ago
|
const int MAX_RECEIVE_TIMEOUT = 30; // in seconds
|
||
8 years ago
|
|
||
|
/** i2cp option for limiting inbound stremaing connections */
|
||
8 years ago
|
const char I2CP_PARAM_STREAMING_MAX_CONNS_PER_MIN[] = "maxconns";
|
||
8 years ago
|
/** default maximum connections attempts per minute per destination */
|
||
|
const uint32_t DEFAULT_MAX_CONNS_PER_MIN = 600;
|
||
|
|
||
|
/**
|
||
|
* max banned destinations per local destination
|
||
|
* TODO: make configurable
|
||
|
*/
|
||
|
const uint16_t MAX_BANNED_CONNS = 9999;
|
||
|
/**
|
||
|
* length of a ban in ms
|
||
|
* TODO: make configurable
|
||
|
*/
|
||
|
const uint64_t DEFAULT_BAN_INTERVAL = 60 * 60 * 1000;
|
||
10 years ago
|
|
||
11 years ago
|
struct Packet
|
||
|
{
|
||
|
size_t len, offset;
|
||
10 years ago
|
uint8_t buf[MAX_PACKET_SIZE];
|
||
10 years ago
|
uint64_t sendTime;
|
||
10 years ago
|
|
||
10 years ago
|
Packet (): len (0), offset (0), sendTime (0) {};
|
||
11 years ago
|
uint8_t * GetBuffer () { return buf + offset; };
|
||
|
size_t GetLength () const { return len - offset; };
|
||
11 years ago
|
|
||
10 years ago
|
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); };
|
||
11 years ago
|
uint8_t GetNACKCount () const { return buf[16]; };
|
||
10 years ago
|
uint32_t GetNACK (int i) const { return bufbe32toh (buf + 17 + 4 * i); };
|
||
11 years ago
|
const uint8_t * GetOption () const { return buf + 17 + GetNACKCount ()*4 + 3; }; // 3 = resendDelay + flags
|
||
10 years ago
|
uint16_t GetFlags () const { return bufbe16toh (GetOption () - 2); };
|
||
|
uint16_t GetOptionSize () const { return bufbe16toh (GetOption ()); };
|
||
11 years ago
|
const uint8_t * GetOptionData () const { return GetOption () + 2; };
|
||
|
const uint8_t * GetPayload () const { return GetOptionData () + GetOptionSize (); };
|
||
10 years ago
|
|
||
|
bool IsSYN () const { return GetFlags () & PACKET_FLAG_SYNCHRONIZE; };
|
||
10 years ago
|
bool IsNoAck () const { return GetFlags () & PACKET_FLAG_NO_ACK; };
|
||
11 years ago
|
};
|
||
|
|
||
|
struct PacketCmp
|
||
|
{
|
||
|
bool operator() (const Packet * p1, const Packet * p2) const
|
||
|
{
|
||
11 years ago
|
return p1->GetSeqn () < p2->GetSeqn ();
|
||
11 years ago
|
};
|
||
11 years ago
|
};
|
||
10 years ago
|
|
||
8 years ago
|
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);
|
||
|
}
|
||
|
~SendBuffer ()
|
||
|
{
|
||
|
delete[] buf;
|
||
|
if (handler) handler(boost::system::error_code ());
|
||
|
}
|
||
|
size_t GetRemainingSize () const { return len - offset; };
|
||
|
const uint8_t * GetRemaningBuffer () const { return buf + offset; };
|
||
|
void Cancel () { if (handler) handler (boost::asio::error::make_error_code (boost::asio::error::operation_aborted)); handler = nullptr; };
|
||
|
};
|
||
|
|
||
|
class SendBufferQueue
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
SendBufferQueue (): m_Size (0) {};
|
||
|
~SendBufferQueue () { CleanUp (); };
|
||
|
|
||
|
void Add (const uint8_t * buf, size_t len, SendHandler handler);
|
||
|
size_t Get (uint8_t * buf, size_t len);
|
||
|
size_t GetSize () const { return m_Size; };
|
||
|
bool IsEmpty () const { return m_Buffers.empty (); };
|
||
|
void CleanUp ();
|
||
|
|
||
|
private:
|
||
|
|
||
|
std::list<std::shared_ptr<SendBuffer> > m_Buffers;
|
||
|
size_t m_Size;
|
||
|
};
|
||
|
|
||
10 years ago
|
enum StreamStatus
|
||
|
{
|
||
10 years ago
|
eStreamStatusNew = 0,
|
||
10 years ago
|
eStreamStatusOpen,
|
||
|
eStreamStatusReset,
|
||
|
eStreamStatusClosing,
|
||
|
eStreamStatusClosed
|
||
|
};
|
||
11 years ago
|
|
||
11 years ago
|
class StreamingDestination;
|
||
10 years ago
|
class Stream: public std::enable_shared_from_this<Stream>
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
Stream (boost::asio::io_service& service, StreamingDestination& local,
|
||
10 years ago
|
std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0); // outgoing
|
||
10 years ago
|
Stream (boost::asio::io_service& service, StreamingDestination& local); // incoming
|
||
10 years ago
|
|
||
11 years ago
|
~Stream ();
|
||
11 years ago
|
uint32_t GetSendStreamID () const { return m_SendStreamID; };
|
||
|
uint32_t GetRecvStreamID () const { return m_RecvStreamID; };
|
||
10 years ago
|
std::shared_ptr<const i2p::data::LeaseSet> GetRemoteLeaseSet () const { return m_RemoteLeaseSet; };
|
||
9 years ago
|
std::shared_ptr<const i2p::data::IdentityEx> GetRemoteIdentity () const { return m_RemoteIdentity; };
|
||
10 years ago
|
bool IsOpen () const { return m_Status == eStreamStatusOpen; };
|
||
11 years ago
|
bool IsEstablished () const { return m_SendStreamID; };
|
||
10 years ago
|
StreamStatus GetStatus () const { return m_Status; };
|
||
10 years ago
|
StreamingDestination& GetLocalDestination () { return m_LocalDestination; };
|
||
11 years ago
|
|
||
11 years ago
|
void HandleNextPacket (Packet * packet);
|
||
10 years ago
|
size_t Send (const uint8_t * buf, size_t len);
|
||
10 years ago
|
void AsyncSend (const uint8_t * buf, size_t len, SendHandler handler);
|
||
11 years ago
|
|
||
11 years ago
|
template<typename Buffer, typename ReceiveHandler>
|
||
|
void AsyncReceive (const Buffer& buffer, ReceiveHandler handler, int timeout = 0);
|
||
10 years ago
|
size_t ReadSome (uint8_t * buf, size_t len) { return ConcatenatePackets (buf, len); };
|
||
|
|
||
11 years ago
|
void Close ();
|
||
10 years ago
|
void Cancel () { m_ReceiveTimer.cancel (); };
|
||
10 years ago
|
|
||
|
size_t GetNumSentBytes () const { return m_NumSentBytes; };
|
||
|
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
|
||
10 years ago
|
size_t GetSendQueueSize () const { return m_SentPackets.size (); };
|
||
|
size_t GetReceiveQueueSize () const { return m_ReceiveQueue.size (); };
|
||
8 years ago
|
size_t GetSendBufferSize () const { return m_SendBuffer.GetSize (); };
|
||
10 years ago
|
int GetWindowSize () const { return m_WindowSize; };
|
||
|
int GetRTT () const { return m_RTT; };
|
||
11 years ago
|
|
||
8 years ago
|
/** don't call me */
|
||
10 years ago
|
void Terminate ();
|
||
8 years ago
|
|
||
|
private:
|
||
10 years ago
|
|
||
8 years ago
|
void CleanUp ();
|
||
|
|
||
10 years ago
|
void SendBuffer ();
|
||
10 years ago
|
void SendQuickAck ();
|
||
10 years ago
|
void SendClose ();
|
||
11 years ago
|
bool SendPacket (Packet * packet);
|
||
10 years ago
|
void SendPackets (const std::vector<Packet *>& packets);
|
||
8 years ago
|
void SendUpdatedLeaseSet ();
|
||
11 years ago
|
|
||
|
void SavePacket (Packet * packet);
|
||
11 years ago
|
void ProcessPacket (Packet * packet);
|
||
10 years ago
|
void ProcessAck (Packet * packet);
|
||
11 years ago
|
size_t ConcatenatePackets (uint8_t * buf, size_t len);
|
||
11 years ago
|
|
||
10 years ago
|
void UpdateCurrentRemoteLease (bool expired = false);
|
||
11 years ago
|
|
||
11 years ago
|
template<typename Buffer, typename ReceiveHandler>
|
||
8 years ago
|
void HandleReceiveTimer (const boost::system::error_code& ecode, const Buffer& buffer, ReceiveHandler handler, int remainingTimeout);
|
||
10 years ago
|
|
||
10 years ago
|
void ScheduleResend ();
|
||
|
void HandleResendTimer (const boost::system::error_code& ecode);
|
||
10 years ago
|
void HandleAckSendTimer (const boost::system::error_code& ecode);
|
||
10 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
11 years ago
|
boost::asio::io_service& m_Service;
|
||
10 years ago
|
uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber;
|
||
|
int32_t m_LastReceivedSequenceNumber;
|
||
10 years ago
|
StreamStatus m_Status;
|
||
|
bool m_IsAckSendScheduled;
|
||
10 years ago
|
StreamingDestination& m_LocalDestination;
|
||
9 years ago
|
std::shared_ptr<const i2p::data::IdentityEx> m_RemoteIdentity;
|
||
10 years ago
|
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
|
||
10 years ago
|
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
|
||
9 years ago
|
std::shared_ptr<const i2p::data::Lease> m_CurrentRemoteLease;
|
||
10 years ago
|
std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel;
|
||
11 years ago
|
std::queue<Packet *> m_ReceiveQueue;
|
||
11 years ago
|
std::set<Packet *, PacketCmp> m_SavedPackets;
|
||
10 years ago
|
std::set<Packet *, PacketCmp> m_SentPackets;
|
||
10 years ago
|
boost::asio::deadline_timer m_ReceiveTimer, m_ResendTimer, m_AckSendTimer;
|
||
10 years ago
|
size_t m_NumSentBytes, m_NumReceivedBytes;
|
||
10 years ago
|
uint16_t m_Port;
|
||
10 years ago
|
|
||
|
std::mutex m_SendBufferMutex;
|
||
8 years ago
|
SendBufferQueue m_SendBuffer;
|
||
10 years ago
|
int m_WindowSize, m_RTT, m_RTO;
|
||
10 years ago
|
uint64_t m_LastWindowSizeIncreaseTime;
|
||
10 years ago
|
int m_NumResendAttempts;
|
||
11 years ago
|
};
|
||
11 years ago
|
|
||
9 years ago
|
class StreamingDestination: public std::enable_shared_from_this<StreamingDestination>
|
||
10 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
typedef std::function<void (std::shared_ptr<Stream>)> Acceptor;
|
||
10 years ago
|
|
||
9 years ago
|
StreamingDestination (std::shared_ptr<i2p::client::ClientDestination> owner, uint16_t localPort = 0, bool gzip = true);
|
||
9 years ago
|
~StreamingDestination ();
|
||
10 years ago
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
|
|
||
10 years ago
|
std::shared_ptr<Stream> CreateNewOutgoingStream (std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0);
|
||
10 years ago
|
void DeleteStream (std::shared_ptr<Stream> stream);
|
||
9 years ago
|
void SetAcceptor (const Acceptor& acceptor);
|
||
|
void ResetAcceptor ();
|
||
10 years ago
|
bool IsAcceptorSet () const { return m_Acceptor != nullptr; };
|
||
8 years ago
|
void AcceptOnce (const Acceptor& acceptor);
|
||
|
|
||
9 years ago
|
std::shared_ptr<i2p::client::ClientDestination> GetOwner () const { return m_Owner; };
|
||
8 years ago
|
void SetOwner (std::shared_ptr<i2p::client::ClientDestination> owner) { m_Owner = owner; };
|
||
10 years ago
|
uint16_t GetLocalPort () const { return m_LocalPort; };
|
||
10 years ago
|
|
||
|
void HandleDataMessagePayload (const uint8_t * buf, size_t len);
|
||
9 years ago
|
std::shared_ptr<I2NPMessage> CreateDataMessage (const uint8_t * payload, size_t len, uint16_t toPort);
|
||
10 years ago
|
|
||
8 years ago
|
/** set max connections per minute per destination */
|
||
|
void SetMaxConnsPerMinute(const uint32_t conns);
|
||
8 years ago
|
|
||
|
Packet * NewPacket () { return m_PacketsPool.Acquire (); };
|
||
|
void DeletePacket (Packet * p) { if (p) m_PacketsPool.Release (p); };
|
||
8 years ago
|
|
||
10 years ago
|
private:
|
||
8 years ago
|
|
||
|
void AcceptOnceAcceptor (std::shared_ptr<Stream> stream, Acceptor acceptor, Acceptor prev);
|
||
|
|
||
10 years ago
|
void HandleNextPacket (Packet * packet);
|
||
10 years ago
|
std::shared_ptr<Stream> CreateNewIncomingStream ();
|
||
9 years ago
|
void HandlePendingIncomingTimer (const boost::system::error_code& ecode);
|
||
10 years ago
|
|
||
8 years ago
|
/** handle cleaning up connection tracking for ratelimits */
|
||
|
void HandleConnTrack(const boost::system::error_code& ecode);
|
||
|
|
||
|
bool DropNewStream(const i2p::data::IdentHash & ident);
|
||
|
|
||
|
void ScheduleConnTrack();
|
||
|
|
||
10 years ago
|
private:
|
||
|
|
||
9 years ago
|
std::shared_ptr<i2p::client::ClientDestination> m_Owner;
|
||
10 years ago
|
uint16_t m_LocalPort;
|
||
9 years ago
|
bool m_Gzip; // gzip compression of data messages
|
||
10 years ago
|
std::mutex m_StreamsMutex;
|
||
9 years ago
|
std::map<uint32_t, std::shared_ptr<Stream> > m_Streams; // sendStreamID->stream
|
||
10 years ago
|
Acceptor m_Acceptor;
|
||
8 years ago
|
uint32_t m_LastIncomingReceiveStreamID;
|
||
9 years ago
|
std::list<std::shared_ptr<Stream> > m_PendingIncomingStreams;
|
||
|
boost::asio::deadline_timer m_PendingIncomingTimer;
|
||
9 years ago
|
std::map<uint32_t, std::list<Packet *> > m_SavedPackets; // receiveStreamID->packets, arrived before SYN
|
||
8 years ago
|
|
||
|
std::mutex m_ConnsMutex;
|
||
|
/** how many connections per minute did each identity have */
|
||
|
std::map<i2p::data::IdentHash, uint32_t> m_Conns;
|
||
|
boost::asio::deadline_timer m_ConnTrackTimer;
|
||
|
uint32_t m_ConnsPerMinute;
|
||
|
/** banned identities */
|
||
|
std::vector<i2p::data::IdentHash> m_Banned;
|
||
|
uint64_t m_LastBanClear;
|
||
8 years ago
|
|
||
|
i2p::util::MemoryPool<Packet> m_PacketsPool;
|
||
|
|
||
10 years ago
|
public:
|
||
|
|
||
9 years ago
|
i2p::data::GzipInflator m_Inflator;
|
||
|
i2p::data::GzipDeflator m_Deflator;
|
||
|
|
||
10 years ago
|
// for HTTP only
|
||
|
const decltype(m_Streams)& GetStreams () const { return m_Streams; };
|
||
|
};
|
||
|
|
||
11 years ago
|
//-------------------------------------------------
|
||
|
|
||
|
template<typename Buffer, typename ReceiveHandler>
|
||
|
void Stream::AsyncReceive (const Buffer& buffer, ReceiveHandler handler, int timeout)
|
||
|
{
|
||
10 years ago
|
auto s = shared_from_this();
|
||
|
m_Service.post ([=](void)
|
||
11 years ago
|
{
|
||
8 years ago
|
if (!m_ReceiveQueue.empty () || m_Status == eStreamStatusReset)
|
||
8 years ago
|
s->HandleReceiveTimer (boost::asio::error::make_error_code (boost::asio::error::operation_aborted), buffer, handler, 0);
|
||
10 years ago
|
else
|
||
|
{
|
||
8 years ago
|
int t = (timeout > MAX_RECEIVE_TIMEOUT) ? MAX_RECEIVE_TIMEOUT : timeout;
|
||
|
s->m_ReceiveTimer.expires_from_now (boost::posix_time::seconds(t));
|
||
10 years ago
|
s->m_ReceiveTimer.async_wait ([=](const boost::system::error_code& ecode)
|
||
8 years ago
|
{ s->HandleReceiveTimer (ecode, buffer, handler, timeout - t); });
|
||
10 years ago
|
}
|
||
|
});
|
||
11 years ago
|
}
|
||
|
|
||
|
template<typename Buffer, typename ReceiveHandler>
|
||
8 years ago
|
void Stream::HandleReceiveTimer (const boost::system::error_code& ecode, const Buffer& buffer, ReceiveHandler handler, int remainingTimeout)
|
||
11 years ago
|
{
|
||
11 years ago
|
size_t received = ConcatenatePackets (boost::asio::buffer_cast<uint8_t *>(buffer), boost::asio::buffer_size(buffer));
|
||
10 years ago
|
if (received > 0)
|
||
|
handler (boost::system::error_code (), received);
|
||
|
else if (ecode == boost::asio::error::operation_aborted)
|
||
10 years ago
|
{
|
||
11 years ago
|
// timeout not expired
|
||
8 years ago
|
if (m_Status == eStreamStatusReset)
|
||
10 years ago
|
handler (boost::asio::error::make_error_code (boost::asio::error::connection_reset), 0);
|
||
10 years ago
|
else
|
||
10 years ago
|
handler (boost::asio::error::make_error_code (boost::asio::error::operation_aborted), 0);
|
||
10 years ago
|
}
|
||
11 years ago
|
else
|
||
8 years ago
|
{
|
||
11 years ago
|
// timeout expired
|
||
8 years ago
|
if (remainingTimeout <= 0)
|
||
|
handler (boost::asio::error::make_error_code (boost::asio::error::timed_out), received);
|
||
|
else
|
||
|
{
|
||
|
// itermediate iterrupt
|
||
|
SendUpdatedLeaseSet (); // send our leaseset if applicable
|
||
|
AsyncReceive (buffer, handler, remainingTimeout);
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|