i2pd/Streaming.h

288 lines
10 KiB
C
Raw Normal View History

2013-12-13 03:36:24 +01:00
#ifndef STREAMING_H__
#define STREAMING_H__
#include <inttypes.h>
2014-03-19 21:38:55 +01:00
#include <string>
#include <map>
#include <set>
2014-04-12 03:13:52 +02:00
#include <queue>
2014-03-23 21:00:05 +01:00
#include <thread>
2014-08-04 22:30:37 +02:00
#include <functional>
2014-10-04 02:23:55 +02:00
#include <mutex>
2014-03-23 21:00:05 +01:00
#include <boost/asio.hpp>
2014-03-25 19:26:39 +01:00
#include <boost/bind.hpp>
2014-01-02 00:19:03 +01:00
#include <cryptopp/dsa.h>
#include "I2PEndian.h"
2013-12-31 02:46:33 +01:00
#include "Identity.h"
2014-01-02 00:19:03 +01:00
#include "LeaseSet.h"
#include "I2NPProtocol.h"
2014-03-15 14:16:55 +01:00
#include "TunnelPool.h"
2014-08-30 04:10:00 +02:00
#include "Garlic.h"
2013-12-13 03:36:24 +01: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 21:57:10 +01:00
const size_t STREAMING_MTU = 1730;
2014-08-07 01:19:59 +02:00
const size_t MAX_PACKET_SIZE = 4096;
2014-08-09 20:47:00 +02:00
const size_t COMPRESSION_THRESHOLD_SIZE = 66;
2014-08-11 00:27:23 +02:00
const int RESEND_TIMEOUT = 10; // in seconds
2014-08-12 01:32:07 +02:00
const int MAX_NUM_RESEND_ATTEMPTS = 5;
2014-08-09 20:47:00 +02:00
2014-01-11 02:21:38 +01:00
struct Packet
{
2014-03-25 00:27:20 +01:00
uint8_t buf[MAX_PACKET_SIZE];
2014-01-11 02:21:38 +01:00
size_t len, offset;
2014-08-12 01:32:07 +02:00
int numResendAttempts;
Packet (): len (0), offset (0), numResendAttempts (0) {};
2014-01-11 02:21:38 +01:00
uint8_t * GetBuffer () { return buf + offset; };
size_t GetLength () const { return len - offset; };
2014-01-30 05:13:59 +01:00
uint32_t GetSendStreamID () const { return be32toh (*(uint32_t *)buf); };
uint32_t GetReceiveStreamID () const { return be32toh (*(uint32_t *)(buf + 4)); };
uint32_t GetSeqn () const { return be32toh (*(uint32_t *)(buf + 8)); };
uint32_t GetAckThrough () const { return be32toh (*(uint32_t *)(buf + 12)); };
uint8_t GetNACKCount () const { return buf[16]; };
2014-08-12 21:57:23 +02:00
uint32_t GetNACK (int i) const { return be32toh (((uint32_t *)(buf + 17))[i]); };
2014-01-30 05:13:59 +01:00
const uint8_t * GetOption () const { return buf + 17 + GetNACKCount ()*4 + 3; }; // 3 = resendDelay + flags
uint16_t GetFlags () const { return be16toh (*(uint16_t *)(GetOption () - 2)); };
uint16_t GetOptionSize () const { return be16toh (*(uint16_t *)GetOption ()); };
const uint8_t * GetOptionData () const { return GetOption () + 2; };
const uint8_t * GetPayload () const { return GetOptionData () + GetOptionSize (); };
2014-08-06 21:44:00 +02:00
bool IsSYN () const { return GetFlags () & PACKET_FLAG_SYNCHRONIZE; };
2014-08-11 00:27:23 +02:00
bool IsNoAck () const { return GetFlags () & PACKET_FLAG_NO_ACK; };
};
struct PacketCmp
{
bool operator() (const Packet * p1, const Packet * p2) const
{
2014-01-30 05:13:59 +01:00
return p1->GetSeqn () < p2->GetSeqn ();
};
2014-01-11 02:21:38 +01:00
};
2013-12-31 02:46:33 +01:00
class StreamingDestination;
class Stream
{
public:
2014-08-01 20:54:14 +02:00
Stream (boost::asio::io_service& service, StreamingDestination * local, const i2p::data::LeaseSet& remote); // outgoing
Stream (boost::asio::io_service& service, StreamingDestination * local); // incoming
2014-01-11 02:21:38 +01:00
~Stream ();
uint32_t GetSendStreamID () const { return m_SendStreamID; };
uint32_t GetRecvStreamID () const { return m_RecvStreamID; };
2014-08-01 20:54:14 +02:00
const i2p::data::LeaseSet * GetRemoteLeaseSet () const { return m_RemoteLeaseSet; };
2014-09-29 20:18:06 +02:00
const i2p::data::IdentityEx& GetRemoteIdentity () const { return m_RemoteIdentity; };
2014-01-12 21:57:10 +01:00
bool IsOpen () const { return m_IsOpen; };
2014-01-10 04:26:30 +01:00
bool IsEstablished () const { return m_SendStreamID; };
2014-08-04 22:30:37 +02:00
StreamingDestination * GetLocalDestination () { return m_LocalDestination; };
2014-01-02 00:19:03 +01:00
2014-01-11 02:21:38 +01:00
void HandleNextPacket (Packet * packet);
2014-10-02 03:18:41 +02:00
size_t Send (const uint8_t * buf, size_t len);
2014-04-10 21:34:51 +02:00
2014-03-25 19:26:39 +01:00
template<typename Buffer, typename ReceiveHandler>
void AsyncReceive (const Buffer& buffer, ReceiveHandler handler, int timeout = 0);
2014-01-12 21:57:10 +01:00
void Close ();
2014-03-18 03:55:02 +01:00
void SetLeaseSetUpdated () { m_LeaseSetUpdated = true; };
2014-03-25 19:26:39 +01:00
2014-01-11 04:23:17 +01:00
private:
2014-08-08 04:03:25 +02:00
void SendQuickAck ();
2014-03-25 00:27:20 +01:00
bool SendPacket (Packet * packet);
2014-08-12 22:35:35 +02:00
void SendPackets (const std::vector<Packet *>& packets);
void SavePacket (Packet * packet);
2014-02-02 04:20:41 +01:00
void ProcessPacket (Packet * packet);
2014-08-11 00:27:23 +02:00
void ProcessAck (Packet * packet);
2014-03-26 20:06:27 +01:00
size_t ConcatenatePackets (uint8_t * buf, size_t len);
2014-03-23 14:25:16 +01:00
void UpdateCurrentRemoteLease ();
2014-03-25 19:26:39 +01:00
template<typename Buffer, typename ReceiveHandler>
void HandleReceiveTimer (const boost::system::error_code& ecode, const Buffer& buffer, ReceiveHandler handler);
2014-08-11 00:27:23 +02:00
void ScheduleResend ();
void HandleResendTimer (const boost::system::error_code& ecode);
private:
2014-03-23 21:00:05 +01:00
boost::asio::io_service& m_Service;
2014-08-07 01:19:59 +02:00
uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber;
int32_t m_LastReceivedSequenceNumber;
2014-08-09 20:47:00 +02:00
bool m_IsOpen, m_LeaseSetUpdated;
2013-12-31 02:46:33 +01:00
StreamingDestination * m_LocalDestination;
2014-08-23 01:47:29 +02:00
i2p::data::IdentityEx m_RemoteIdentity;
2014-08-01 20:54:14 +02:00
const i2p::data::LeaseSet * m_RemoteLeaseSet;
2014-08-30 04:10:00 +02:00
i2p::garlic::GarlicRoutingSession * m_RoutingSession;
2014-03-23 14:25:16 +01:00
i2p::data::Lease m_CurrentRemoteLease;
i2p::tunnel::OutboundTunnel * m_CurrentOutboundTunnel;
2014-04-12 03:13:52 +02:00
std::queue<Packet *> m_ReceiveQueue;
std::set<Packet *, PacketCmp> m_SavedPackets;
2014-08-11 00:27:23 +02:00
std::set<Packet *, PacketCmp> m_SentPackets;
boost::asio::deadline_timer m_ReceiveTimer, m_ResendTimer;
};
class StreamingDestination: public i2p::data::LocalDestination
2013-12-13 03:36:24 +01:00
{
public:
StreamingDestination (boost::asio::io_service& service, bool isPublic);
StreamingDestination (boost::asio::io_service& service, const std::string& fullPath, bool isPublic);
StreamingDestination (boost::asio::io_service& service, const i2p::data::PrivateKeys& keys, bool isPublic);
2014-03-15 01:24:12 +01:00
~StreamingDestination ();
2014-08-04 22:30:37 +02:00
const i2p::data::LeaseSet * GetLeaseSet ();
2014-07-29 19:44:54 +02:00
i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; };
2014-08-01 20:54:14 +02:00
Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote);
2014-08-04 22:30:37 +02:00
void DeleteStream (Stream * stream);
void SetAcceptor (const std::function<void (Stream *)>& acceptor) { m_Acceptor = acceptor; };
2014-09-26 21:40:57 +02:00
void ResetAcceptor () { m_Acceptor = nullptr; };
bool IsAcceptorSet () const { return m_Acceptor != nullptr; };
2014-01-11 02:21:38 +01:00
void HandleNextPacket (Packet * packet);
2014-01-09 04:47:22 +01:00
// implements LocalDestination
2014-08-26 04:47:12 +02:00
const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; };
const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; };
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; };
2014-08-16 01:21:30 +02:00
void SetLeaseSetUpdated ();
2014-08-01 20:54:14 +02:00
private:
Stream * CreateNewIncomingStream ();
2014-08-16 01:21:30 +02:00
void UpdateLeaseSet ();
2014-08-01 20:54:14 +02:00
private:
2014-08-01 20:54:14 +02:00
boost::asio::io_service& m_Service;
2014-10-04 02:23:55 +02:00
std::mutex m_StreamsMutex;
std::map<uint32_t, Stream *> m_Streams;
2014-03-20 03:03:50 +01:00
i2p::data::PrivateKeys m_Keys;
uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256];
2014-03-15 14:16:55 +01:00
i2p::tunnel::TunnelPool * m_Pool;
i2p::data::LeaseSet * m_LeaseSet;
2014-08-20 17:12:53 +02:00
bool m_IsPublic;
2014-08-04 22:30:37 +02:00
std::function<void (Stream *)> m_Acceptor;
};
2014-01-02 00:19:03 +01:00
2014-03-23 21:00:05 +01:00
class StreamingDestinations
{
public:
StreamingDestinations (): m_IsRunning (false), m_Thread (nullptr),
m_Work (m_Service), m_SharedLocalDestination (nullptr) {};
~StreamingDestinations () {};
void Start ();
void Stop ();
void HandleNextPacket (i2p::data::IdentHash destination, Packet * packet);
2014-03-23 21:00:05 +01:00
Stream * CreateClientStream (const i2p::data::LeaseSet& remote);
2014-08-04 22:30:37 +02:00
void DeleteStream (Stream * stream);
StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; };
StreamingDestination * CreateNewLocalDestination (bool isPublic);
StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic);
2014-09-24 22:39:31 +02:00
void DeleteLocalDestination (StreamingDestination * destination);
2014-08-20 21:03:10 +02:00
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const;
StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic);
2014-08-20 21:03:10 +02:00
2014-03-23 21:00:05 +01:00
private:
void Run ();
2014-07-13 14:25:30 +02:00
void LoadLocalDestinations ();
void PostNextPacket (i2p::data::IdentHash destination, Packet * packet);
2014-03-23 21:00:05 +01:00
private:
bool m_IsRunning;
std::thread * m_Thread;
boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work;
2014-10-04 02:23:55 +02:00
std::mutex m_DestinationsMutex;
2014-07-11 21:39:38 +02:00
std::map<i2p::data::IdentHash, StreamingDestination *> m_Destinations;
2014-03-23 21:00:05 +01:00
StreamingDestination * m_SharedLocalDestination;
2014-09-29 01:15:04 +02:00
public:
// for HTTP
const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
2014-03-23 21:00:05 +01:00
};
Stream * CreateStream (const i2p::data::LeaseSet& remote);
void DeleteStream (Stream * stream);
2014-03-15 14:16:55 +01:00
void StartStreaming ();
void StopStreaming ();
2014-08-04 22:30:37 +02:00
StreamingDestination * GetSharedLocalDestination ();
StreamingDestination * CreateNewLocalDestination (bool isPublic = true);
StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true);
2014-09-24 22:39:31 +02:00
void DeleteLocalDestination (StreamingDestination * destination);
2014-08-26 20:56:00 +02:00
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination);
StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic);
2014-09-29 01:15:04 +02:00
// for HTTP
const StreamingDestinations& GetLocalDestinations ();
2013-12-13 03:36:24 +01:00
// assuming data is I2CP message
2014-03-23 21:00:05 +01:00
void HandleDataMessage (i2p::data::IdentHash destination, const uint8_t * buf, size_t len);
2014-03-25 00:27:20 +01:00
I2NPMessage * CreateDataMessage (Stream * s, const uint8_t * payload, size_t len);
2014-03-25 19:26:39 +01:00
//-------------------------------------------------
template<typename Buffer, typename ReceiveHandler>
void Stream::AsyncReceive (const Buffer& buffer, ReceiveHandler handler, int timeout)
{
2014-04-12 03:13:52 +02:00
if (!m_ReceiveQueue.empty ())
2014-03-27 20:56:13 +01:00
{
size_t received = ConcatenatePackets (boost::asio::buffer_cast<uint8_t *>(buffer), boost::asio::buffer_size(buffer));
if (received)
{
// TODO: post to stream's thread
handler (boost::system::error_code (), received);
return;
}
}
if (!m_IsOpen)
{
handler (boost::asio::error::make_error_code (boost::asio::error::connection_reset), 0);
return;
}
2014-03-25 19:26:39 +01:00
m_ReceiveTimer.expires_from_now (boost::posix_time::seconds(timeout));
2014-04-23 23:51:12 +02:00
m_ReceiveTimer.async_wait ([=](const boost::system::error_code& ecode)
2014-04-23 19:44:08 +02:00
{ this->HandleReceiveTimer (ecode, buffer, handler); });
2014-03-25 19:26:39 +01:00
}
template<typename Buffer, typename ReceiveHandler>
void Stream::HandleReceiveTimer (const boost::system::error_code& ecode, const Buffer& buffer, ReceiveHandler handler)
{
2014-03-26 20:06:27 +01:00
size_t received = ConcatenatePackets (boost::asio::buffer_cast<uint8_t *>(buffer), boost::asio::buffer_size(buffer));
2014-03-25 19:26:39 +01:00
if (ecode == boost::asio::error::operation_aborted)
// timeout not expired
2014-03-26 20:06:27 +01:00
handler (boost::system::error_code (), received);
2014-03-25 19:26:39 +01:00
else
// timeout expired
2014-03-26 20:06:27 +01:00
handler (boost::asio::error::make_error_code (boost::asio::error::timed_out), received);
2014-03-25 19:26:39 +01:00
}
2013-12-13 03:36:24 +01:00
}
}
#endif