i2pd/Streaming.cpp

645 lines
18 KiB
C++
Raw Normal View History

2014-03-19 20:38:55 +00:00
#include <fstream>
2014-01-11 01:21:38 +00:00
#include <algorithm>
#include <cryptopp/dh.h>
2013-12-13 02:36:24 +00:00
#include <cryptopp/gzip.h>
2014-07-13 12:25:30 +00:00
#include "util.h"
2013-12-13 02:36:24 +00:00
#include "Log.h"
#include "RouterInfo.h"
#include "RouterContext.h"
2013-12-31 01:46:33 +00:00
#include "Tunnel.h"
#include "Timestamp.h"
#include "CryptoConst.h"
2014-01-01 23:19:03 +00:00
#include "Garlic.h"
2014-08-01 18:54:14 +00:00
#include "NetDb.h"
2013-12-13 02:36:24 +00:00
#include "Streaming.h"
namespace i2p
{
namespace stream
{
2014-03-23 20:00:05 +00:00
Stream::Stream (boost::asio::io_service& service, StreamingDestination * local,
const i2p::data::LeaseSet& remote): m_Service (service), m_SendStreamID (0),
2014-08-01 18:54:14 +00:00
m_SequenceNumber (0), m_LastReceivedSequenceNumber (0), m_IsOpen (false),
m_IsOutgoing(true), m_LeaseSetUpdated (true), m_LocalDestination (local),
m_RemoteLeaseSet (&remote), m_ReceiveTimer (m_Service)
{
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
2014-03-23 13:25:16 +00:00
UpdateCurrentRemoteLease ();
}
2014-08-01 18:54:14 +00:00
Stream::Stream (boost::asio::io_service& service, StreamingDestination * local):
m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (0),
2014-08-04 22:22:54 +00:00
m_IsOpen (true), m_IsOutgoing(true), m_LeaseSetUpdated (true), m_LocalDestination (local),
2014-08-01 18:54:14 +00:00
m_RemoteLeaseSet (nullptr), m_ReceiveTimer (m_Service)
{
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
}
2014-01-11 01:21:38 +00:00
Stream::~Stream ()
2013-12-13 02:36:24 +00:00
{
2014-03-25 18:26:39 +00:00
m_ReceiveTimer.cancel ();
2014-04-12 01:13:52 +00:00
while (!m_ReceiveQueue.empty ())
{
auto packet = m_ReceiveQueue.front ();
m_ReceiveQueue.pop ();
2014-01-11 01:21:38 +00:00
delete packet;
2014-04-12 01:13:52 +00:00
}
for (auto it: m_SavedPackets)
delete it;
2014-01-11 01:21:38 +00:00
}
void Stream::HandleNextPacket (Packet * packet)
{
2014-08-01 18:54:14 +00:00
if (!m_SendStreamID)
m_SendStreamID = packet->GetReceiveStreamID ();
2014-01-30 04:13:59 +00:00
uint32_t receivedSeqn = packet->GetSeqn ();
2014-02-02 03:20:41 +00:00
LogPrint ("Received seqn=", receivedSeqn);
if (!receivedSeqn || receivedSeqn == m_LastReceivedSequenceNumber + 1)
{
2014-02-02 03:20:41 +00:00
// we have received next in sequence message
ProcessPacket (packet);
// we should also try stored messages if any
for (auto it = m_SavedPackets.begin (); it != m_SavedPackets.end ();)
{
2014-01-30 04:13:59 +00:00
if ((*it)->GetSeqn () == m_LastReceivedSequenceNumber + 1)
{
2014-02-02 03:20:41 +00:00
Packet * savedPacket = *it;
m_SavedPackets.erase (it++);
2014-02-02 03:20:41 +00:00
ProcessPacket (savedPacket);
}
else
break;
2014-03-25 21:43:36 +00:00
}
// send ack for last message
2014-03-30 13:02:43 +00:00
if (m_IsOpen)
SendQuickAck ();
}
else
{
if (receivedSeqn <= m_LastReceivedSequenceNumber)
{
// we have received duplicate. Most likely our outbound tunnel is dead
LogPrint ("Duplicate message ", receivedSeqn, " received");
2014-03-23 13:25:16 +00:00
UpdateCurrentRemoteLease (); // pick another lease
SendQuickAck (); // resend ack for previous message again
delete packet; // packet dropped
}
else
{
LogPrint ("Missing messages from ", m_LastReceivedSequenceNumber + 1, " to ", receivedSeqn - 1);
// save message and wait for missing message again
SavePacket (packet);
}
}
2014-02-02 03:20:41 +00:00
}
void Stream::SavePacket (Packet * packet)
{
m_SavedPackets.insert (packet);
}
void Stream::ProcessPacket (Packet * packet)
{
// process flags
uint32_t receivedSeqn = packet->GetSeqn ();
uint16_t flags = packet->GetFlags ();
LogPrint ("Process seqn=", receivedSeqn, ", flags=", flags);
const uint8_t * optionData = packet->GetOptionData ();
if (flags & PACKET_FLAG_SYNCHRONIZE)
{
LogPrint ("Synchronize");
}
if (flags & PACKET_FLAG_FROM_INCLUDED)
{
LogPrint ("From identity");
2014-08-01 18:54:14 +00:00
if (!m_RemoteLeaseSet)
{
i2p::data::Identity * identity = (i2p::data::Identity *)optionData;
2014-08-04 20:30:37 +00:00
LogPrint ("Incoming stream from ", identity->Hash ().ToBase64 ());
2014-08-01 18:54:14 +00:00
m_RemoteLeaseSet = i2p::data::netdb.FindLeaseSet (identity->Hash ());
2014-08-04 20:30:37 +00:00
if (!m_RemoteLeaseSet)
2014-08-01 18:54:14 +00:00
LogPrint ("LeaseSet ", identity->Hash ().ToBase64 (), " not found");
}
2014-02-02 03:20:41 +00:00
optionData += sizeof (i2p::data::Identity);
}
2014-08-04 22:22:54 +00:00
if (flags & PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED)
{
LogPrint ("Max packet size");
optionData += 2;
}
if (flags & PACKET_FLAG_SIGNATURE_INCLUDED)
{
LogPrint ("Signature");
optionData += 40;
}
2014-02-02 03:20:41 +00:00
packet->offset = packet->GetPayload () - packet->buf;
if (packet->GetLength () > 0)
2014-03-29 12:11:00 +00:00
{
2014-04-12 01:13:52 +00:00
m_ReceiveQueue.push (packet);
2014-03-29 12:11:00 +00:00
m_ReceiveTimer.cancel ();
}
2014-02-02 03:20:41 +00:00
else
delete packet;
m_LastReceivedSequenceNumber = receivedSeqn;
2014-01-12 20:57:10 +00:00
if (flags & PACKET_FLAG_CLOSE)
{
LogPrint ("Closed");
2014-03-30 13:02:43 +00:00
SendQuickAck (); // send ack for close explicitly?
2014-01-12 20:57:10 +00:00
m_IsOpen = false;
2014-01-15 00:00:12 +00:00
}
2013-12-13 02:36:24 +00:00
}
2014-04-18 23:27:39 +00:00
size_t Stream::Send (const uint8_t * buf, size_t len, int timeout)
2014-01-01 23:19:03 +00:00
{
2014-03-24 23:27:20 +00:00
Packet * p = new Packet ();
uint8_t * packet = p->GetBuffer ();
// TODO: implement setters
2014-01-01 23:19:03 +00:00
size_t size = 0;
*(uint32_t *)(packet + size) = htobe32 (m_SendStreamID);
size += 4; // sendStreamID
*(uint32_t *)(packet + size) = htobe32 (m_RecvStreamID);
size += 4; // receiveStreamID
2014-04-14 20:24:57 +00:00
*(uint32_t *)(packet + size) = htobe32 (m_SequenceNumber++);
2014-01-01 23:19:03 +00:00
size += 4; // sequenceNum
*(uint32_t *)(packet + size) = 0; // TODO
size += 4; // ack Through
packet[size] = 0;
size++; // NACK count
size++; // resend delay
2014-04-18 23:27:39 +00:00
if (!m_IsOpen)
{
// initial packet
m_IsOpen = true;
*(uint16_t *)(packet + size) = htobe16 (PACKET_FLAG_SYNCHRONIZE |
PACKET_FLAG_FROM_INCLUDED | PACKET_FLAG_SIGNATURE_INCLUDED |
PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED | PACKET_FLAG_NO_ACK);
size += 2; // flags
*(uint16_t *)(packet + size) = htobe16 (sizeof (i2p::data::Identity) + 40 + 2); // identity + signature + packet size
size += 2; // options size
memcpy (packet + size, &m_LocalDestination->GetIdentity (), sizeof (i2p::data::Identity));
size += sizeof (i2p::data::Identity); // from
*(uint16_t *)(packet + size) = htobe16 (STREAMING_MTU);
size += 2; // max packet size
uint8_t * signature = packet + size; // set it later
memset (signature, 0, 40); // zeroes for now
size += 40; // signature
memcpy (packet + size, buf, len);
size += len; // payload
m_LocalDestination->Sign (packet, size, signature);
}
else
{
// follow on packet
*(uint16_t *)(packet + size) = 0;
size += 2; // flags
*(uint16_t *)(packet + size) = 0; // no options
size += 2; // options size
memcpy (packet + size, buf, len);
size += len; // payload
}
2014-03-24 23:27:20 +00:00
p->len = size;
m_Service.post (boost::bind (&Stream::SendPacket, this, p));
2014-04-18 23:27:39 +00:00
return len;
2014-01-01 23:19:03 +00:00
}
2014-04-18 23:27:39 +00:00
2014-01-12 20:57:10 +00:00
2014-01-11 03:23:17 +00:00
void Stream::SendQuickAck ()
{
2014-03-24 23:27:20 +00:00
uint8_t packet[MAX_PACKET_SIZE];
2014-01-11 03:23:17 +00:00
size_t size = 0;
*(uint32_t *)(packet + size) = htobe32 (m_SendStreamID);
size += 4; // sendStreamID
*(uint32_t *)(packet + size) = htobe32 (m_RecvStreamID);
size += 4; // receiveStreamID
*(uint32_t *)(packet + size) = 0; // this is plain Ack message
size += 4; // sequenceNum
*(uint32_t *)(packet + size) = htobe32 (m_LastReceivedSequenceNumber);
size += 4; // ack Through
packet[size] = 0;
size++; // NACK count
size++; // resend delay
*(uint16_t *)(packet + size) = 0; // nof flags set
size += 2; // flags
2014-01-16 13:08:28 +00:00
*(uint16_t *)(packet + size) = 0; // no options
2014-01-11 03:23:17 +00:00
size += 2; // options size
2014-03-24 23:27:20 +00:00
2014-03-18 02:55:02 +00:00
if (SendPacket (packet, size))
LogPrint ("Quick Ack sent");
2014-01-11 03:23:17 +00:00
}
2014-01-12 20:57:10 +00:00
void Stream::Close ()
{
if (m_IsOpen)
{
m_IsOpen = false;
2014-03-24 23:27:20 +00:00
uint8_t packet[MAX_PACKET_SIZE];
2014-01-12 20:57:10 +00:00
size_t size = 0;
*(uint32_t *)(packet + size) = htobe32 (m_SendStreamID);
size += 4; // sendStreamID
*(uint32_t *)(packet + size) = htobe32 (m_RecvStreamID);
size += 4; // receiveStreamID
2014-04-14 20:24:57 +00:00
*(uint32_t *)(packet + size) = htobe32 (m_SequenceNumber++);
2014-01-12 20:57:10 +00:00
size += 4; // sequenceNum
*(uint32_t *)(packet + size) = htobe32 (m_LastReceivedSequenceNumber);
size += 4; // ack Through
packet[size] = 0;
size++; // NACK count
size++; // resend delay
2014-08-05 23:55:35 +00:00
*(uint16_t *)(packet + size) = htobe16 (PACKET_FLAG_CLOSE | PACKET_FLAG_SIGNATURE_INCLUDED);
2014-01-12 20:57:10 +00:00
size += 2; // flags
2014-01-16 13:08:28 +00:00
*(uint16_t *)(packet + size) = htobe16 (40); // 40 bytes signature
2014-01-12 20:57:10 +00:00
size += 2; // options size
uint8_t * signature = packet + size;
memset (packet + size, 0, 40);
size += 40; // signature
m_LocalDestination->Sign (packet, size, signature);
2014-03-24 23:27:20 +00:00
2014-03-18 02:55:02 +00:00
if (SendPacket (packet, size))
LogPrint ("FIN sent");
2014-01-12 20:57:10 +00:00
}
}
2014-03-26 19:06:27 +00:00
size_t Stream::ConcatenatePackets (uint8_t * buf, size_t len)
{
2014-01-11 01:21:38 +00:00
size_t pos = 0;
2014-04-12 01:13:52 +00:00
while (pos < len && !m_ReceiveQueue.empty ())
2014-01-11 01:21:38 +00:00
{
2014-04-12 01:13:52 +00:00
Packet * packet = m_ReceiveQueue.front ();
size_t l = std::min (packet->GetLength (), len - pos);
memcpy (buf + pos, packet->GetBuffer (), l);
pos += l;
packet->offset += l;
if (!packet->GetLength ())
2014-01-11 01:21:38 +00:00
{
2014-04-12 01:13:52 +00:00
m_ReceiveQueue.pop ();
delete packet;
}
2014-01-11 01:21:38 +00:00
}
return pos;
2014-03-26 19:06:27 +00:00
}
2014-03-18 02:55:02 +00:00
2014-03-24 23:27:20 +00:00
bool Stream::SendPacket (Packet * packet)
{
if (packet)
{
bool ret = SendPacket (packet->GetBuffer (), packet->GetLength ());
delete packet;
return ret;
}
else
return false;
}
bool Stream::SendPacket (const uint8_t * buf, size_t len)
2014-03-18 02:55:02 +00:00
{
2014-08-01 18:54:14 +00:00
if (!m_RemoteLeaseSet)
{
LogPrint ("Can't send packet. Missing remote LeaseSet");
return false;
}
I2NPMessage * leaseSet = nullptr;
2014-04-23 18:47:17 +00:00
2014-04-23 19:09:01 +00:00
if (m_LeaseSetUpdated)
{
leaseSet = m_LocalDestination->GetLeaseSetMsg ();
2014-04-23 19:09:01 +00:00
m_LeaseSetUpdated = false;
}
2014-04-23 18:47:17 +00:00
2014-08-01 18:54:14 +00:00
I2NPMessage * msg = i2p::garlic::routing.WrapMessage (*m_RemoteLeaseSet,
2014-03-24 23:27:20 +00:00
CreateDataMessage (this, buf, len), leaseSet);
auto outboundTunnel = m_LocalDestination->GetTunnelPool ()->GetNextOutboundTunnel ();
if (outboundTunnel)
2014-03-18 02:55:02 +00:00
{
2014-03-23 13:25:16 +00:00
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
if (ts >= m_CurrentRemoteLease.endDate)
UpdateCurrentRemoteLease ();
if (ts < m_CurrentRemoteLease.endDate)
2014-03-18 02:55:02 +00:00
{
outboundTunnel->SendTunnelDataMsg (m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID, msg);
2014-03-18 02:55:02 +00:00
return true;
}
else
{
LogPrint ("All leases are expired");
DeleteI2NPMessage (msg);
}
}
else
{
LogPrint ("No outbound tunnels in the pool");
DeleteI2NPMessage (msg);
}
return false;
}
2014-03-23 13:25:16 +00:00
void Stream::UpdateCurrentRemoteLease ()
{
2014-08-01 18:54:14 +00:00
if (m_RemoteLeaseSet)
{
auto leases = m_RemoteLeaseSet->GetNonExpiredLeases ();
if (!leases.empty ())
{
uint32_t i = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, leases.size () - 1);
m_CurrentRemoteLease = leases[i];
}
else
m_CurrentRemoteLease.endDate = 0;
}
2014-03-23 13:25:16 +00:00
else
m_CurrentRemoteLease.endDate = 0;
}
2013-12-31 01:46:33 +00:00
2014-08-01 18:54:14 +00:00
StreamingDestination::StreamingDestination (boost::asio::io_service& service):
m_Service (service), m_LeaseSet (nullptr)
2013-12-31 01:46:33 +00:00
{
m_Keys = i2p::data::CreateRandomKeys ();
2014-05-03 19:29:00 +00:00
m_IdentHash = m_Keys.pub.Hash ();
2014-01-01 23:19:03 +00:00
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
2013-12-31 01:46:33 +00:00
}
2014-01-09 03:47:22 +00:00
2014-08-01 18:54:14 +00:00
StreamingDestination::StreamingDestination (boost::asio::io_service& service, const std::string& fullPath):
m_Service (service), m_LeaseSet (nullptr)
2014-03-19 20:38:55 +00:00
{
std::ifstream s(fullPath.c_str (), std::ifstream::binary);
if (s.is_open ())
2014-03-20 02:03:50 +00:00
s.read ((char *)&m_Keys, sizeof (m_Keys));
2014-03-19 20:38:55 +00:00
else
LogPrint ("Can't open file ", fullPath);
2014-05-03 19:29:00 +00:00
m_IdentHash = m_Keys.pub.Hash ();
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
2014-07-13 12:25:30 +00:00
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
2014-03-19 20:38:55 +00:00
}
2014-01-09 03:47:22 +00:00
StreamingDestination::~StreamingDestination ()
{
2014-03-15 13:16:55 +00:00
if (m_Pool)
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
delete m_LeaseSet;
2014-01-09 03:47:22 +00:00
}
2014-01-11 01:21:38 +00:00
void StreamingDestination::HandleNextPacket (Packet * packet)
{
2014-02-17 22:47:21 +00:00
uint32_t sendStreamID = packet->GetSendStreamID ();
2014-07-13 12:25:30 +00:00
if (sendStreamID)
2014-01-11 01:21:38 +00:00
{
2014-07-13 12:25:30 +00:00
auto it = m_Streams.find (sendStreamID);
if (it != m_Streams.end ())
it->second->HandleNextPacket (packet);
else
{
LogPrint ("Unknown stream ", sendStreamID);
delete packet;
}
}
2014-08-01 18:54:14 +00:00
else // new incoming stream
2014-07-13 12:25:30 +00:00
{
2014-08-01 18:54:14 +00:00
auto incomingStream = CreateNewIncomingStream ();
2014-08-04 20:30:37 +00:00
if (m_Acceptor != nullptr)
m_Acceptor (incomingStream);
2014-08-01 18:54:14 +00:00
incomingStream->HandleNextPacket (packet);
2014-01-11 01:21:38 +00:00
}
}
2013-12-13 02:36:24 +00:00
2014-08-01 18:54:14 +00:00
Stream * StreamingDestination::CreateNewOutgoingStream (const i2p::data::LeaseSet& remote)
{
2014-08-01 18:54:14 +00:00
Stream * s = new Stream (m_Service, this, remote);
m_Streams[s->GetRecvStreamID ()] = s;
return s;
}
2013-12-31 01:46:33 +00:00
2014-08-01 18:54:14 +00:00
Stream * StreamingDestination::CreateNewIncomingStream ()
{
Stream * s = new Stream (m_Service, this);
m_Streams[s->GetRecvStreamID ()] = s;
return s;
}
2014-01-01 23:19:03 +00:00
void StreamingDestination::DeleteStream (Stream * stream)
{
if (stream)
{
m_Streams.erase (stream->GetRecvStreamID ());
delete stream;
}
}
2014-01-10 03:26:30 +00:00
I2NPMessage * StreamingDestination::GetLeaseSetMsg ()
2014-08-04 20:30:37 +00:00
{
return CreateDatabaseStoreMsg (GetLeaseSet ());
}
const i2p::data::LeaseSet * StreamingDestination::GetLeaseSet ()
2014-03-15 13:16:55 +00:00
{
if (!m_Pool) return nullptr;
if (!m_LeaseSet || m_LeaseSet->HasExpiredLeases ())
{
auto newLeaseSet = new i2p::data::LeaseSet (*m_Pool);
// TODO: make it atomic
auto oldLeaseSet = m_LeaseSet;
m_LeaseSet = newLeaseSet;
delete oldLeaseSet;
for (auto it: m_Streams)
it.second->SetLeaseSetUpdated ();
}
2014-08-04 20:30:37 +00:00
return m_LeaseSet;
2013-12-31 01:46:33 +00:00
}
2014-08-04 20:30:37 +00:00
2014-07-29 17:44:54 +00:00
void StreamingDestination::Sign (const uint8_t * buf, int len, uint8_t * signature) const
2014-01-01 23:19:03 +00:00
{
CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature);
}
2014-03-23 20:00:05 +00:00
StreamingDestinations destinations;
void StreamingDestinations::Start ()
{
if (!m_SharedLocalDestination)
2014-07-11 19:39:38 +00:00
{
2014-08-01 18:54:14 +00:00
m_SharedLocalDestination = new StreamingDestination (m_Service);
2014-07-11 19:39:38 +00:00
m_Destinations[m_SharedLocalDestination->GetIdentHash ()] = m_SharedLocalDestination;
}
2014-07-13 12:25:30 +00:00
LoadLocalDestinations ();
2014-03-23 20:00:05 +00:00
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&StreamingDestinations::Run, this));
}
void StreamingDestinations::Stop ()
{
2014-07-11 19:39:38 +00:00
for (auto it: m_Destinations)
delete it.second;
m_Destinations.clear ();
m_SharedLocalDestination = 0; // deleted through m_Destination
2014-03-23 20:00:05 +00:00
m_IsRunning = false;
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
}
void StreamingDestinations::Run ()
{
m_Service.run ();
}
2014-07-13 12:25:30 +00:00
void StreamingDestinations::LoadLocalDestinations ()
{
int numDestinations = 0;
boost::filesystem::path p (i2p::util::filesystem::GetDataDir());
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator it (p); it != end; ++it)
{
if (boost::filesystem::is_regular_file (*it) && it->path ().extension () == ".dat")
{
auto fullPath =
#if BOOST_VERSION > 10500
it->path().string();
#else
it->path();
#endif
2014-08-01 18:54:14 +00:00
auto localDestination = new StreamingDestination (m_Service, fullPath);
2014-07-13 12:25:30 +00:00
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
numDestinations++;
}
}
if (numDestinations > 0)
LogPrint (numDestinations, " local destinations loaded");
}
2014-03-23 20:00:05 +00:00
Stream * StreamingDestinations::CreateClientStream (const i2p::data::LeaseSet& remote)
{
if (!m_SharedLocalDestination) return nullptr;
2014-08-01 18:54:14 +00:00
return m_SharedLocalDestination->CreateNewOutgoingStream (remote);
2014-03-23 20:00:05 +00:00
}
2014-08-04 20:30:37 +00:00
void StreamingDestinations::DeleteStream (Stream * stream)
2014-03-23 20:00:05 +00:00
{
2014-08-04 20:30:37 +00:00
if (stream)
{
m_Service.post (
[=](void)
{
stream->GetLocalDestination ()->DeleteStream (stream);
}
);
}
2014-03-23 20:00:05 +00:00
}
2014-08-04 20:30:37 +00:00
2014-03-23 20:00:05 +00:00
void StreamingDestinations::HandleNextPacket (i2p::data::IdentHash destination, Packet * packet)
{
m_Service.post (boost::bind (&StreamingDestinations::PostNextPacket, this, destination, packet));
}
void StreamingDestinations::PostNextPacket (i2p::data::IdentHash destination, Packet * packet)
2014-03-23 20:00:05 +00:00
{
2014-07-11 19:39:38 +00:00
auto it = m_Destinations.find (destination);
if (it != m_Destinations.end ())
it->second->HandleNextPacket (packet);
else
{
LogPrint ("Local destination ", destination.ToBase64 (), " not found");
delete packet;
}
2014-03-23 20:00:05 +00:00
}
2014-01-01 23:19:03 +00:00
Stream * CreateStream (const i2p::data::LeaseSet& remote)
2014-01-01 23:19:03 +00:00
{
2014-03-23 20:00:05 +00:00
return destinations.CreateClientStream (remote);
2014-01-01 23:19:03 +00:00
}
void DeleteStream (Stream * stream)
2014-01-01 23:19:03 +00:00
{
2014-08-04 20:30:37 +00:00
destinations.DeleteStream (stream);
2014-01-01 23:19:03 +00:00
}
2014-03-15 13:16:55 +00:00
void StartStreaming ()
{
2014-03-23 20:00:05 +00:00
destinations.Start ();
2014-03-15 13:16:55 +00:00
}
void StopStreaming ()
{
2014-03-23 20:00:05 +00:00
destinations.Stop ();
2014-03-15 13:16:55 +00:00
}
2014-08-04 20:30:37 +00:00
StreamingDestination * GetSharedLocalDestination ()
{
return destinations.GetSharedLocalDestination ();
}
2013-12-31 01:46:33 +00:00
2014-03-23 20:00:05 +00:00
void HandleDataMessage (i2p::data::IdentHash destination, const uint8_t * buf, size_t len)
2013-12-13 02:36:24 +00:00
{
uint32_t length = be32toh (*(uint32_t *)buf);
buf += 4;
// we assume I2CP payload
if (buf[9] == 6) // streaming protocol
{
// unzip it
CryptoPP::Gunzip decompressor;
decompressor.Put (buf, length);
decompressor.MessageEnd();
2014-01-11 01:21:38 +00:00
Packet * uncompressed = new Packet;
uncompressed->offset = 0;
uncompressed->len = decompressor.MaxRetrievable ();
2014-01-12 20:57:10 +00:00
if (uncompressed->len > MAX_PACKET_SIZE)
{
2014-03-18 02:55:02 +00:00
LogPrint ("Recieved packet size exceeds mac packet size");
2014-01-12 20:57:10 +00:00
uncompressed->len = MAX_PACKET_SIZE;
}
2014-01-11 01:21:38 +00:00
decompressor.Get (uncompressed->buf, uncompressed->len);
// then forward to streaming engine thread
2014-03-23 20:00:05 +00:00
destinations.HandleNextPacket (destination, uncompressed);
2013-12-13 02:36:24 +00:00
}
else
LogPrint ("Data: protocol ", buf[9], " is not supported");
}
2014-03-24 23:27:20 +00:00
I2NPMessage * CreateDataMessage (Stream * s, const uint8_t * payload, size_t len)
{
I2NPMessage * msg = NewI2NPMessage ();
CryptoPP::Gzip compressor;
2014-01-14 02:18:32 +00:00
compressor.SetDeflateLevel (CryptoPP::Gzip::MIN_DEFLATE_LEVEL);
compressor.Put (payload, len);
compressor.MessageEnd();
int size = compressor.MaxRetrievable ();
uint8_t * buf = msg->GetPayload ();
2014-01-07 01:56:44 +00:00
*(uint32_t *)buf = htobe32 (size); // length
buf += 4;
compressor.Get (buf, size);
2014-01-14 02:18:32 +00:00
memset (buf + 4, 0, 4); // source and destination ports. TODO: fill with proper values later
buf[9] = 6; // streaming protocol
msg->len += size + 4;
FillI2NPMessageHeader (msg, eI2NPData);
return msg;
}
2013-12-13 02:36:24 +00:00
}
}