2014-03-20 00:38:55 +04:00
|
|
|
#include <fstream>
|
2014-01-11 05:21:38 +04:00
|
|
|
#include <algorithm>
|
2014-04-02 02:58:47 +04:00
|
|
|
#include <cryptopp/dh.h>
|
2013-12-13 06:36:24 +04:00
|
|
|
#include <cryptopp/gzip.h>
|
2014-07-13 16:25:30 +04:00
|
|
|
#include "util.h"
|
2013-12-13 06:36:24 +04:00
|
|
|
#include "Log.h"
|
|
|
|
#include "RouterInfo.h"
|
2013-12-20 06:19:44 +04:00
|
|
|
#include "RouterContext.h"
|
2013-12-31 05:46:33 +04:00
|
|
|
#include "Tunnel.h"
|
|
|
|
#include "Timestamp.h"
|
|
|
|
#include "CryptoConst.h"
|
2014-01-02 03:19:03 +04:00
|
|
|
#include "Garlic.h"
|
2014-08-01 22:54:14 +04:00
|
|
|
#include "NetDb.h"
|
2013-12-13 06:36:24 +04:00
|
|
|
#include "Streaming.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace stream
|
|
|
|
{
|
2014-03-24 00:00:05 +04:00
|
|
|
Stream::Stream (boost::asio::io_service& service, StreamingDestination * local,
|
|
|
|
const i2p::data::LeaseSet& remote): m_Service (service), m_SendStreamID (0),
|
2014-08-07 03:19:59 +04:00
|
|
|
m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false),
|
2014-08-09 22:47:00 +04:00
|
|
|
m_LeaseSetUpdated (true), m_LocalDestination (local),
|
2014-08-17 22:42:49 +04:00
|
|
|
m_RemoteLeaseSet (&remote), m_CurrentOutboundTunnel (nullptr),
|
|
|
|
m_ReceiveTimer (m_Service), m_ResendTimer (m_Service)
|
2013-12-20 06:19:44 +04:00
|
|
|
{
|
|
|
|
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
2014-03-23 17:25:16 +04:00
|
|
|
UpdateCurrentRemoteLease ();
|
2013-12-20 06:19:44 +04:00
|
|
|
}
|
|
|
|
|
2014-08-01 22:54:14 +04:00
|
|
|
Stream::Stream (boost::asio::io_service& service, StreamingDestination * local):
|
2014-08-07 03:19:59 +04:00
|
|
|
m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1),
|
2014-08-09 22:47:00 +04:00
|
|
|
m_IsOpen (false), m_LeaseSetUpdated (true), m_LocalDestination (local),
|
2014-08-17 22:42:49 +04:00
|
|
|
m_RemoteLeaseSet (nullptr), m_CurrentOutboundTunnel (nullptr),
|
|
|
|
m_ReceiveTimer (m_Service), m_ResendTimer (m_Service)
|
2014-08-01 22:54:14 +04:00
|
|
|
{
|
|
|
|
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
|
|
|
}
|
|
|
|
|
2014-01-11 05:21:38 +04:00
|
|
|
Stream::~Stream ()
|
2013-12-13 06:36:24 +04:00
|
|
|
{
|
2014-03-25 22:26:39 +04:00
|
|
|
m_ReceiveTimer.cancel ();
|
2014-08-11 02:27:23 +04:00
|
|
|
m_ResendTimer.cancel ();
|
2014-04-12 05:13:52 +04:00
|
|
|
while (!m_ReceiveQueue.empty ())
|
|
|
|
{
|
|
|
|
auto packet = m_ReceiveQueue.front ();
|
|
|
|
m_ReceiveQueue.pop ();
|
2014-01-11 05:21:38 +04:00
|
|
|
delete packet;
|
2014-04-12 05:13:52 +04:00
|
|
|
}
|
2014-01-27 03:22:30 +04:00
|
|
|
for (auto it: m_SavedPackets)
|
|
|
|
delete it;
|
2014-08-11 02:27:23 +04:00
|
|
|
for (auto it: m_SentPackets)
|
|
|
|
delete it;
|
2014-01-11 05:21:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::HandleNextPacket (Packet * packet)
|
|
|
|
{
|
2014-08-01 22:54:14 +04:00
|
|
|
if (!m_SendStreamID)
|
|
|
|
m_SendStreamID = packet->GetReceiveStreamID ();
|
|
|
|
|
2014-08-11 02:27:23 +04:00
|
|
|
if (!packet->IsNoAck ()) // ack received
|
|
|
|
ProcessAck (packet);
|
|
|
|
|
2014-08-07 03:19:59 +04:00
|
|
|
int32_t receivedSeqn = packet->GetSeqn ();
|
2014-08-07 06:08:57 +04:00
|
|
|
bool isSyn = packet->IsSYN ();
|
|
|
|
if (!receivedSeqn && !isSyn)
|
2014-08-06 23:44:00 +04:00
|
|
|
{
|
|
|
|
// plain ack
|
|
|
|
LogPrint ("Plain ACK received");
|
|
|
|
delete packet;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-02 07:20:41 +04:00
|
|
|
LogPrint ("Received seqn=", receivedSeqn);
|
2014-08-07 06:08:57 +04:00
|
|
|
if (isSyn || receivedSeqn == m_LastReceivedSequenceNumber + 1)
|
2014-01-19 21:01:12 +04:00
|
|
|
{
|
2014-02-02 07:20:41 +04:00
|
|
|
// we have received next in sequence message
|
|
|
|
ProcessPacket (packet);
|
2014-08-07 06:08:57 +04:00
|
|
|
|
2014-01-27 03:22:30 +04:00
|
|
|
// we should also try stored messages if any
|
|
|
|
for (auto it = m_SavedPackets.begin (); it != m_SavedPackets.end ();)
|
|
|
|
{
|
2014-08-07 03:19:59 +04:00
|
|
|
if ((*it)->GetSeqn () == (uint32_t)(m_LastReceivedSequenceNumber + 1))
|
2014-01-27 03:22:30 +04:00
|
|
|
{
|
2014-02-02 07:20:41 +04:00
|
|
|
Packet * savedPacket = *it;
|
2014-01-27 03:22:30 +04:00
|
|
|
m_SavedPackets.erase (it++);
|
|
|
|
|
2014-02-02 07:20:41 +04:00
|
|
|
ProcessPacket (savedPacket);
|
2014-01-27 03:22:30 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2014-03-26 01:43:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// send ack for last message
|
2014-03-30 17:02:43 +04:00
|
|
|
if (m_IsOpen)
|
2014-08-07 06:08:57 +04:00
|
|
|
SendQuickAck ();
|
|
|
|
else if (isSyn)
|
|
|
|
// we have to send SYN back to incoming connection
|
2014-08-08 06:03:25 +04:00
|
|
|
Send (nullptr, 0, 0); // also sets m_IsOpen
|
2014-01-19 21:01:12 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (receivedSeqn <= m_LastReceivedSequenceNumber)
|
|
|
|
{
|
|
|
|
// we have received duplicate. Most likely our outbound tunnel is dead
|
|
|
|
LogPrint ("Duplicate message ", receivedSeqn, " received");
|
2014-08-17 22:42:49 +04:00
|
|
|
m_CurrentOutboundTunnel = nullptr; // pick another outbound tunnel
|
2014-03-23 17:25:16 +04:00
|
|
|
UpdateCurrentRemoteLease (); // pick another lease
|
2014-07-09 06:09:58 +04:00
|
|
|
SendQuickAck (); // resend ack for previous message again
|
2014-01-27 03:22:30 +04:00
|
|
|
delete packet; // packet dropped
|
2014-01-19 21:01:12 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("Missing messages from ", m_LastReceivedSequenceNumber + 1, " to ", receivedSeqn - 1);
|
2014-01-27 03:22:30 +04:00
|
|
|
// save message and wait for missing message again
|
|
|
|
SavePacket (packet);
|
2014-01-19 21:01:12 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-02 07:20:41 +04: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");
|
|
|
|
|
2014-08-07 03:19:59 +04:00
|
|
|
if (flags & PACKET_FLAG_DELAY_REQUESTED)
|
|
|
|
{
|
|
|
|
optionData += 2;
|
|
|
|
}
|
|
|
|
|
2014-02-02 07:20:41 +04:00
|
|
|
if (flags & PACKET_FLAG_FROM_INCLUDED)
|
|
|
|
{
|
2014-08-23 03:47:29 +04:00
|
|
|
optionData += m_RemoteIdentity.FromBuffer (optionData, packet->GetOptionSize ());
|
|
|
|
LogPrint ("From identity ", m_RemoteIdentity.GetIdentHash ().ToBase64 ());
|
2014-08-07 03:19:59 +04:00
|
|
|
if (!m_RemoteLeaseSet)
|
2014-08-23 03:47:29 +04:00
|
|
|
LogPrint ("Incoming stream from ", m_RemoteIdentity.GetIdentHash ().ToBase64 ());
|
2014-02-02 07:20:41 +04:00
|
|
|
}
|
|
|
|
|
2014-08-05 02:22:54 +04:00
|
|
|
if (flags & PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED)
|
|
|
|
{
|
2014-08-07 06:08:57 +04:00
|
|
|
uint16_t maxPacketSize = be16toh (*(uint16_t *)optionData);
|
2014-08-07 03:19:59 +04:00
|
|
|
LogPrint ("Max packet size ", maxPacketSize);
|
2014-08-05 02:22:54 +04:00
|
|
|
optionData += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & PACKET_FLAG_SIGNATURE_INCLUDED)
|
|
|
|
{
|
|
|
|
LogPrint ("Signature");
|
2014-08-23 03:47:29 +04:00
|
|
|
uint8_t signature[256];
|
|
|
|
auto signatureLen = m_RemoteIdentity.GetSignatureLen ();
|
|
|
|
memcpy (signature, optionData, signatureLen);
|
|
|
|
memset (const_cast<uint8_t *>(optionData), 0, signatureLen);
|
|
|
|
if (!m_RemoteIdentity.Verify (packet->GetBuffer (), packet->GetLength (), signature))
|
|
|
|
{
|
|
|
|
LogPrint ("Signature verification failed");
|
|
|
|
Close ();
|
|
|
|
flags |= PACKET_FLAG_CLOSE;
|
|
|
|
}
|
|
|
|
memcpy (const_cast<uint8_t *>(optionData), signature, signatureLen);
|
|
|
|
optionData += signatureLen;
|
2014-08-05 02:22:54 +04:00
|
|
|
}
|
|
|
|
|
2014-02-02 07:20:41 +04:00
|
|
|
packet->offset = packet->GetPayload () - packet->buf;
|
|
|
|
if (packet->GetLength () > 0)
|
2014-03-29 16:11:00 +04:00
|
|
|
{
|
2014-04-12 05:13:52 +04:00
|
|
|
m_ReceiveQueue.push (packet);
|
2014-03-29 16:11:00 +04:00
|
|
|
m_ReceiveTimer.cancel ();
|
|
|
|
}
|
2014-02-02 07:20:41 +04:00
|
|
|
else
|
|
|
|
delete packet;
|
|
|
|
|
|
|
|
m_LastReceivedSequenceNumber = receivedSeqn;
|
|
|
|
|
2014-01-13 00:57:10 +04:00
|
|
|
if (flags & PACKET_FLAG_CLOSE)
|
|
|
|
{
|
|
|
|
LogPrint ("Closed");
|
2014-03-30 17:02:43 +04:00
|
|
|
SendQuickAck (); // send ack for close explicitly?
|
2014-01-13 00:57:10 +04:00
|
|
|
m_IsOpen = false;
|
2014-08-07 06:08:57 +04:00
|
|
|
m_ReceiveTimer.cancel ();
|
2014-08-12 03:32:07 +04:00
|
|
|
m_ResendTimer.cancel ();
|
2014-01-15 04:00:12 +04:00
|
|
|
}
|
2013-12-13 06:36:24 +04:00
|
|
|
}
|
2014-08-11 02:27:23 +04:00
|
|
|
|
|
|
|
void Stream::ProcessAck (Packet * packet)
|
|
|
|
{
|
|
|
|
uint32_t ackThrough = packet->GetAckThrough ();
|
2014-08-12 23:57:23 +04:00
|
|
|
int nackCount = packet->GetNACKCount ();
|
2014-08-11 02:27:23 +04:00
|
|
|
for (auto it = m_SentPackets.begin (); it != m_SentPackets.end ();)
|
|
|
|
{
|
2014-08-12 23:57:23 +04:00
|
|
|
auto seqn = (*it)->GetSeqn ();
|
|
|
|
if (seqn <= ackThrough)
|
2014-08-11 02:27:23 +04:00
|
|
|
{
|
2014-08-12 23:57:23 +04:00
|
|
|
if (nackCount > 0)
|
|
|
|
{
|
|
|
|
bool nacked = false;
|
|
|
|
for (int i = 0; i < nackCount; i++)
|
|
|
|
if (seqn == packet->GetNACK (i))
|
|
|
|
{
|
|
|
|
nacked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nacked)
|
|
|
|
{
|
|
|
|
LogPrint ("Packet ", seqn, " NACK");
|
2014-08-14 05:27:12 +04:00
|
|
|
it++;
|
2014-08-12 23:57:23 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 02:27:23 +04:00
|
|
|
auto sentPacket = *it;
|
2014-08-12 23:57:23 +04:00
|
|
|
LogPrint ("Packet ", seqn, " acknowledged");
|
2014-08-11 02:27:23 +04:00
|
|
|
m_SentPackets.erase (it++);
|
|
|
|
delete sentPacket;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (m_SentPackets.empty ())
|
|
|
|
m_ResendTimer.cancel ();
|
|
|
|
}
|
2014-01-27 03:22:30 +04:00
|
|
|
|
2014-04-19 03:27:39 +04:00
|
|
|
size_t Stream::Send (const uint8_t * buf, size_t len, int timeout)
|
2014-01-02 03:19:03 +04:00
|
|
|
{
|
2014-08-11 02:27:23 +04:00
|
|
|
bool isNoAck = m_LastReceivedSequenceNumber < 0; // first packet
|
2014-08-14 05:04:23 +04:00
|
|
|
while (!m_IsOpen || len > 0)
|
2014-04-19 03:27:39 +04:00
|
|
|
{
|
2014-08-11 19:31:01 +04:00
|
|
|
Packet * p = new Packet ();
|
|
|
|
uint8_t * packet = p->GetBuffer ();
|
|
|
|
// TODO: implement setters
|
|
|
|
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) = htobe32 (m_SequenceNumber++);
|
|
|
|
size += 4; // sequenceNum
|
|
|
|
if (isNoAck)
|
|
|
|
*(uint32_t *)(packet + size) = htobe32 (m_LastReceivedSequenceNumber);
|
|
|
|
else
|
|
|
|
*(uint32_t *)(packet + size) = 0;
|
|
|
|
size += 4; // ack Through
|
|
|
|
packet[size] = 0;
|
|
|
|
size++; // NACK count
|
|
|
|
size++; // resend delay
|
|
|
|
if (!m_IsOpen)
|
|
|
|
{
|
|
|
|
// initial packet
|
|
|
|
m_IsOpen = true;
|
|
|
|
uint16_t flags = PACKET_FLAG_SYNCHRONIZE | PACKET_FLAG_FROM_INCLUDED |
|
|
|
|
PACKET_FLAG_SIGNATURE_INCLUDED | PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED;
|
|
|
|
if (isNoAck) flags |= PACKET_FLAG_NO_ACK;
|
|
|
|
*(uint16_t *)(packet + size) = htobe16 (flags);
|
|
|
|
size += 2; // flags
|
2014-08-24 18:36:59 +04:00
|
|
|
size_t identityLen = m_LocalDestination->GetIdentity ().GetFullLen ();
|
|
|
|
size_t signatureLen = m_LocalDestination->GetIdentity ().GetSignatureLen ();
|
|
|
|
*(uint16_t *)(packet + size) = htobe16 (identityLen + signatureLen + 2); // identity + signature + packet size
|
2014-08-11 19:31:01 +04:00
|
|
|
size += 2; // options size
|
2014-08-24 18:36:59 +04:00
|
|
|
m_LocalDestination->GetIdentity ().ToBuffer (packet + size, identityLen);
|
|
|
|
size += identityLen; // from
|
2014-08-11 19:31:01 +04:00
|
|
|
*(uint16_t *)(packet + size) = htobe16 (STREAMING_MTU);
|
|
|
|
size += 2; // max packet size
|
|
|
|
uint8_t * signature = packet + size; // set it later
|
2014-08-24 18:36:59 +04:00
|
|
|
memset (signature, 0, signatureLen); // zeroes for now
|
|
|
|
size += signatureLen; // signature
|
2014-08-11 19:31:01 +04:00
|
|
|
size_t sentLen = STREAMING_MTU - size;
|
|
|
|
if (len < sentLen) sentLen = len;
|
|
|
|
memcpy (packet + size, buf, sentLen);
|
2014-08-12 02:33:01 +04:00
|
|
|
buf += sentLen;
|
2014-08-11 19:31:01 +04:00
|
|
|
len -= sentLen;
|
|
|
|
size += sentLen; // 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
|
|
|
|
size_t sentLen = STREAMING_MTU - size;
|
|
|
|
if (len < sentLen) sentLen = len;
|
|
|
|
memcpy (packet + size, buf, sentLen);
|
2014-08-12 02:33:01 +04:00
|
|
|
buf += sentLen;
|
2014-08-11 19:31:01 +04:00
|
|
|
len -= sentLen;
|
|
|
|
size += sentLen; // payload
|
|
|
|
}
|
|
|
|
p->len = size;
|
|
|
|
m_Service.post (boost::bind (&Stream::SendPacket, this, p));
|
|
|
|
}
|
|
|
|
|
2014-04-19 03:27:39 +04:00
|
|
|
return len;
|
2014-01-02 03:19:03 +04:00
|
|
|
}
|
2014-04-19 03:27:39 +04:00
|
|
|
|
2014-01-13 00:57:10 +04:00
|
|
|
|
2014-08-08 06:03:25 +04:00
|
|
|
void Stream::SendQuickAck ()
|
2014-01-11 07:23:17 +04:00
|
|
|
{
|
2014-08-13 00:52:04 +04:00
|
|
|
Packet p;
|
|
|
|
uint8_t * packet = p.GetBuffer ();
|
2014-01-11 07:23:17 +04: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
|
2014-08-08 06:03:25 +04:00
|
|
|
*(uint16_t *)(packet + size) = 0; // nof flags set
|
2014-01-11 07:23:17 +04:00
|
|
|
size += 2; // flags
|
2014-01-16 17:08:28 +04:00
|
|
|
*(uint16_t *)(packet + size) = 0; // no options
|
2014-01-11 07:23:17 +04:00
|
|
|
size += 2; // options size
|
2014-08-13 00:52:04 +04:00
|
|
|
p.len = size;
|
|
|
|
|
2014-08-13 02:42:53 +04:00
|
|
|
SendPackets (std::vector<Packet *> { &p });
|
2014-08-13 00:52:04 +04:00
|
|
|
LogPrint ("Quick Ack sent");
|
2014-01-11 07:23:17 +04:00
|
|
|
}
|
2014-01-13 00:57:10 +04:00
|
|
|
|
|
|
|
void Stream::Close ()
|
|
|
|
{
|
|
|
|
if (m_IsOpen)
|
|
|
|
{
|
|
|
|
m_IsOpen = false;
|
2014-08-07 00:00:34 +04:00
|
|
|
Packet * p = new Packet ();
|
|
|
|
uint8_t * packet = p->GetBuffer ();
|
2014-01-13 00:57:10 +04: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-15 00:24:57 +04:00
|
|
|
*(uint32_t *)(packet + size) = htobe32 (m_SequenceNumber++);
|
2014-01-13 00:57:10 +04: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-06 03:55:35 +04:00
|
|
|
*(uint16_t *)(packet + size) = htobe16 (PACKET_FLAG_CLOSE | PACKET_FLAG_SIGNATURE_INCLUDED);
|
2014-01-13 00:57:10 +04:00
|
|
|
size += 2; // flags
|
2014-08-24 18:36:59 +04:00
|
|
|
size_t signatureLen = m_LocalDestination->GetIdentity ().GetSignatureLen ();
|
|
|
|
*(uint16_t *)(packet + size) = htobe16 (signatureLen); // signature only
|
2014-01-13 00:57:10 +04:00
|
|
|
size += 2; // options size
|
|
|
|
uint8_t * signature = packet + size;
|
2014-08-24 18:36:59 +04:00
|
|
|
memset (packet + size, 0, signatureLen);
|
|
|
|
size += signatureLen; // signature
|
2014-01-13 00:57:10 +04:00
|
|
|
m_LocalDestination->Sign (packet, size, signature);
|
2014-03-25 03:27:20 +04:00
|
|
|
|
2014-08-07 00:00:34 +04:00
|
|
|
p->len = size;
|
|
|
|
m_Service.post (boost::bind (&Stream::SendPacket, this, p));
|
|
|
|
LogPrint ("FIN sent");
|
2014-01-13 00:57:10 +04:00
|
|
|
}
|
|
|
|
}
|
2014-03-26 23:06:27 +04:00
|
|
|
|
|
|
|
size_t Stream::ConcatenatePackets (uint8_t * buf, size_t len)
|
|
|
|
{
|
2014-01-11 05:21:38 +04:00
|
|
|
size_t pos = 0;
|
2014-04-12 05:13:52 +04:00
|
|
|
while (pos < len && !m_ReceiveQueue.empty ())
|
2014-01-11 05:21:38 +04:00
|
|
|
{
|
2014-04-12 05:13:52 +04: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 05:21:38 +04:00
|
|
|
{
|
2014-04-12 05:13:52 +04:00
|
|
|
m_ReceiveQueue.pop ();
|
|
|
|
delete packet;
|
|
|
|
}
|
2014-01-11 05:21:38 +04:00
|
|
|
}
|
|
|
|
return pos;
|
2014-03-26 23:06:27 +04:00
|
|
|
}
|
2014-03-18 06:55:02 +04:00
|
|
|
|
2014-03-25 03:27:20 +04:00
|
|
|
bool Stream::SendPacket (Packet * packet)
|
|
|
|
{
|
|
|
|
if (packet)
|
|
|
|
{
|
2014-08-13 00:52:04 +04:00
|
|
|
SendPackets (std::vector<Packet *> { packet });
|
|
|
|
bool isEmpty = m_SentPackets.empty ();
|
|
|
|
m_SentPackets.insert (packet);
|
|
|
|
if (isEmpty)
|
|
|
|
ScheduleResend ();
|
|
|
|
return true;
|
2014-03-25 03:27:20 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-13 00:35:35 +04:00
|
|
|
void Stream::SendPackets (const std::vector<Packet *>& packets)
|
|
|
|
{
|
|
|
|
if (!m_RemoteLeaseSet)
|
|
|
|
{
|
|
|
|
UpdateCurrentRemoteLease ();
|
|
|
|
if (!m_RemoteLeaseSet)
|
|
|
|
{
|
|
|
|
LogPrint ("Can't send packets. Missing remote LeaseSet");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 22:34:58 +04:00
|
|
|
const i2p::data::LeaseSet * leaseSet = nullptr;
|
2014-08-13 00:35:35 +04:00
|
|
|
if (m_LeaseSetUpdated)
|
|
|
|
{
|
2014-08-15 22:34:58 +04:00
|
|
|
leaseSet = m_LocalDestination->GetLeaseSet ();
|
2014-08-13 00:35:35 +04:00
|
|
|
m_LeaseSetUpdated = false;
|
|
|
|
}
|
|
|
|
|
2014-08-17 22:42:49 +04:00
|
|
|
m_CurrentOutboundTunnel = m_LocalDestination->GetTunnelPool ()->GetNextOutboundTunnel (m_CurrentOutboundTunnel);
|
|
|
|
if (m_CurrentOutboundTunnel)
|
2014-08-13 00:35:35 +04:00
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
|
|
|
if (ts >= m_CurrentRemoteLease.endDate)
|
|
|
|
UpdateCurrentRemoteLease ();
|
|
|
|
if (ts < m_CurrentRemoteLease.endDate)
|
|
|
|
{
|
|
|
|
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
|
|
|
|
for (auto it: packets)
|
|
|
|
{
|
|
|
|
auto msg = i2p::garlic::routing.WrapMessage (*m_RemoteLeaseSet,
|
|
|
|
CreateDataMessage (this, it->GetBuffer (), it->GetLength ()), leaseSet);
|
|
|
|
msgs.push_back (i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeTunnel,
|
|
|
|
m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID,
|
|
|
|
msg
|
|
|
|
});
|
2014-08-13 02:42:53 +04:00
|
|
|
leaseSet = nullptr; // send leaseSet only one time
|
2014-08-13 00:35:35 +04:00
|
|
|
}
|
2014-08-17 22:42:49 +04:00
|
|
|
m_CurrentOutboundTunnel->SendTunnelDataMsg (msgs);
|
2014-08-13 00:35:35 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("All leases are expired");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("No outbound tunnels in the pool");
|
|
|
|
}
|
|
|
|
|
2014-08-11 02:27:23 +04:00
|
|
|
void Stream::ScheduleResend ()
|
|
|
|
{
|
|
|
|
m_ResendTimer.cancel ();
|
|
|
|
m_ResendTimer.expires_from_now (boost::posix_time::seconds(RESEND_TIMEOUT));
|
|
|
|
m_ResendTimer.async_wait (boost::bind (&Stream::HandleResendTimer,
|
|
|
|
this, boost::asio::placeholders::error));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::HandleResendTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
2014-08-13 00:35:35 +04:00
|
|
|
std::vector<Packet *> packets;
|
2014-08-11 02:27:23 +04:00
|
|
|
for (auto it : m_SentPackets)
|
2014-08-12 03:32:07 +04:00
|
|
|
{
|
|
|
|
it->numResendAttempts++;
|
|
|
|
if (it->numResendAttempts <= MAX_NUM_RESEND_ATTEMPTS)
|
2014-08-13 00:35:35 +04:00
|
|
|
packets.push_back (it);
|
2014-08-12 03:32:07 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Close ();
|
2014-08-15 21:28:07 +04:00
|
|
|
m_ReceiveTimer.cancel ();
|
2014-08-12 03:32:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 00:35:35 +04:00
|
|
|
if (packets.size () > 0)
|
2014-08-15 05:38:46 +04:00
|
|
|
{
|
2014-08-17 22:42:49 +04:00
|
|
|
m_CurrentOutboundTunnel = nullptr; // pick another outbound tunnel
|
2014-08-15 05:38:46 +04:00
|
|
|
UpdateCurrentRemoteLease (); // pick another lease
|
2014-08-13 00:35:35 +04:00
|
|
|
SendPackets (packets);
|
2014-08-15 05:38:46 +04:00
|
|
|
}
|
2014-08-11 02:27:23 +04:00
|
|
|
ScheduleResend ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 17:25:16 +04:00
|
|
|
void Stream::UpdateCurrentRemoteLease ()
|
|
|
|
{
|
2014-08-06 19:09:06 +04:00
|
|
|
if (!m_RemoteLeaseSet)
|
|
|
|
{
|
2014-08-23 03:47:29 +04:00
|
|
|
m_RemoteLeaseSet = i2p::data::netdb.FindLeaseSet (m_RemoteIdentity.GetIdentHash ());
|
2014-08-06 19:09:06 +04:00
|
|
|
if (!m_RemoteLeaseSet)
|
2014-08-23 03:47:29 +04:00
|
|
|
LogPrint ("LeaseSet ", m_RemoteIdentity.GetIdentHash ().ToBase64 (), " not found");
|
2014-08-06 19:09:06 +04:00
|
|
|
}
|
2014-08-01 22:54:14 +04: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 17:25:16 +04:00
|
|
|
else
|
|
|
|
m_CurrentRemoteLease.endDate = 0;
|
|
|
|
}
|
2013-12-20 06:19:44 +04:00
|
|
|
|
2013-12-31 05:46:33 +04:00
|
|
|
|
2014-08-01 22:54:14 +04:00
|
|
|
StreamingDestination::StreamingDestination (boost::asio::io_service& service):
|
2014-08-20 19:12:53 +04:00
|
|
|
m_Service (service), m_LeaseSet (nullptr), m_IsPublic (false)
|
2013-12-31 05:46:33 +04:00
|
|
|
{
|
2014-08-24 18:36:59 +04:00
|
|
|
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (/*i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256*/); // uncomment for ECDSA
|
2014-04-02 02:58:47 +04:00
|
|
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
|
|
|
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
2014-06-26 03:28:33 +04:00
|
|
|
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
|
2013-12-31 05:46:33 +04:00
|
|
|
}
|
2014-01-09 07:47:22 +04:00
|
|
|
|
2014-08-01 22:54:14 +04:00
|
|
|
StreamingDestination::StreamingDestination (boost::asio::io_service& service, const std::string& fullPath):
|
2014-08-20 19:12:53 +04:00
|
|
|
m_Service (service), m_LeaseSet (nullptr), m_IsPublic (true)
|
2014-03-20 00:38:55 +04:00
|
|
|
{
|
|
|
|
std::ifstream s(fullPath.c_str (), std::ifstream::binary);
|
|
|
|
if (s.is_open ())
|
2014-08-24 00:06:56 +04:00
|
|
|
{
|
|
|
|
s.seekg (0, std::ios::end);
|
|
|
|
size_t len = s.tellg();
|
|
|
|
s.seekg (0, std::ios::beg);
|
|
|
|
uint8_t * buf = new uint8_t[len];
|
|
|
|
s.read ((char *)buf, len);
|
|
|
|
m_Keys.FromBuffer (buf, len);
|
|
|
|
delete[] buf;
|
2014-08-26 22:56:00 +04:00
|
|
|
LogPrint ("Local address ", m_Keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p loaded");
|
2014-08-24 00:06:56 +04:00
|
|
|
}
|
2014-03-20 00:38:55 +04:00
|
|
|
else
|
2014-08-26 22:56:00 +04:00
|
|
|
{
|
|
|
|
LogPrint ("Can't open file ", fullPath, " Creating new one");
|
|
|
|
// new eepsites use ECDSA
|
|
|
|
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256);
|
|
|
|
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
|
|
|
|
size_t len = m_Keys.GetFullLen ();
|
|
|
|
uint8_t * buf = new uint8_t[len];
|
|
|
|
len = m_Keys.ToBuffer (buf, len);
|
|
|
|
f.write ((char *)buf, len);
|
|
|
|
delete[] buf;
|
|
|
|
|
|
|
|
LogPrint ("New private keys file ", fullPath, " for ", m_Keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p created");
|
|
|
|
}
|
2014-05-03 23:29:00 +04:00
|
|
|
|
2014-04-02 02:58:47 +04:00
|
|
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
|
|
|
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
2014-07-13 16:25:30 +04:00
|
|
|
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
|
2014-03-20 00:38:55 +04:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:47:22 +04:00
|
|
|
StreamingDestination::~StreamingDestination ()
|
|
|
|
{
|
2014-03-15 17:16:55 +04:00
|
|
|
if (m_Pool)
|
|
|
|
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
|
2014-07-30 05:56:03 +04:00
|
|
|
delete m_LeaseSet;
|
2014-01-09 07:47:22 +04:00
|
|
|
}
|
2014-03-14 23:13:34 +04:00
|
|
|
|
2014-01-11 05:21:38 +04:00
|
|
|
void StreamingDestination::HandleNextPacket (Packet * packet)
|
2013-12-20 06:19:44 +04:00
|
|
|
{
|
2014-02-18 02:47:21 +04:00
|
|
|
uint32_t sendStreamID = packet->GetSendStreamID ();
|
2014-07-13 16:25:30 +04:00
|
|
|
if (sendStreamID)
|
2014-01-11 05:21:38 +04:00
|
|
|
{
|
2014-07-13 16:25:30 +04: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 22:54:14 +04:00
|
|
|
else // new incoming stream
|
2014-07-13 16:25:30 +04:00
|
|
|
{
|
2014-08-01 22:54:14 +04:00
|
|
|
auto incomingStream = CreateNewIncomingStream ();
|
2014-08-07 06:08:57 +04:00
|
|
|
incomingStream->HandleNextPacket (packet);
|
2014-08-05 00:30:37 +04:00
|
|
|
if (m_Acceptor != nullptr)
|
|
|
|
m_Acceptor (incomingStream);
|
2014-01-11 05:21:38 +04:00
|
|
|
}
|
2013-12-20 06:19:44 +04:00
|
|
|
}
|
2013-12-13 06:36:24 +04:00
|
|
|
|
2014-08-01 22:54:14 +04:00
|
|
|
Stream * StreamingDestination::CreateNewOutgoingStream (const i2p::data::LeaseSet& remote)
|
2013-12-20 06:19:44 +04:00
|
|
|
{
|
2014-08-01 22:54:14 +04:00
|
|
|
Stream * s = new Stream (m_Service, this, remote);
|
2013-12-20 06:19:44 +04:00
|
|
|
m_Streams[s->GetRecvStreamID ()] = s;
|
|
|
|
return s;
|
|
|
|
}
|
2013-12-31 05:46:33 +04:00
|
|
|
|
2014-08-01 22:54:14 +04:00
|
|
|
Stream * StreamingDestination::CreateNewIncomingStream ()
|
|
|
|
{
|
|
|
|
Stream * s = new Stream (m_Service, this);
|
|
|
|
m_Streams[s->GetRecvStreamID ()] = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-01-02 03:19:03 +04:00
|
|
|
void StreamingDestination::DeleteStream (Stream * stream)
|
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
{
|
|
|
|
m_Streams.erase (stream->GetRecvStreamID ());
|
|
|
|
delete stream;
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 00:30:37 +04:00
|
|
|
|
|
|
|
const i2p::data::LeaseSet * StreamingDestination::GetLeaseSet ()
|
2014-03-15 17:16:55 +04:00
|
|
|
{
|
2014-07-29 22:56:36 +04:00
|
|
|
if (!m_Pool) return nullptr;
|
2014-08-16 03:21:30 +04:00
|
|
|
if (!m_LeaseSet)
|
|
|
|
UpdateLeaseSet ();
|
|
|
|
return m_LeaseSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingDestination::UpdateLeaseSet ()
|
|
|
|
{
|
|
|
|
auto newLeaseSet = new i2p::data::LeaseSet (*m_Pool);
|
|
|
|
if (!m_LeaseSet)
|
|
|
|
m_LeaseSet = newLeaseSet;
|
|
|
|
else
|
2014-07-30 05:56:03 +04:00
|
|
|
{
|
2014-08-16 03:21:30 +04:00
|
|
|
// TODO: implement it better
|
|
|
|
*m_LeaseSet = *newLeaseSet;
|
|
|
|
delete newLeaseSet;
|
2014-07-30 05:56:03 +04:00
|
|
|
}
|
2014-08-16 03:21:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingDestination::SetLeaseSetUpdated ()
|
|
|
|
{
|
|
|
|
UpdateLeaseSet ();
|
|
|
|
for (auto it: m_Streams)
|
|
|
|
it.second->SetLeaseSetUpdated ();
|
2014-08-20 19:12:53 +04:00
|
|
|
if (m_IsPublic)
|
|
|
|
i2p::data::netdb.PublishLeaseSet (m_LeaseSet, m_Pool);
|
2013-12-31 05:46:33 +04:00
|
|
|
}
|
2014-03-24 00:00:05 +04:00
|
|
|
|
|
|
|
StreamingDestinations destinations;
|
|
|
|
void StreamingDestinations::Start ()
|
|
|
|
{
|
|
|
|
if (!m_SharedLocalDestination)
|
2014-07-11 23:39:38 +04:00
|
|
|
{
|
2014-08-01 22:54:14 +04:00
|
|
|
m_SharedLocalDestination = new StreamingDestination (m_Service);
|
2014-08-23 07:02:48 +04:00
|
|
|
m_Destinations[m_SharedLocalDestination->GetIdentity ().GetIdentHash ()] = m_SharedLocalDestination;
|
2014-07-11 23:39:38 +04:00
|
|
|
}
|
2014-08-26 22:56:00 +04:00
|
|
|
// LoadLocalDestinations ();
|
2014-07-13 16:25:30 +04:00
|
|
|
|
2014-03-24 00:00:05 +04:00
|
|
|
m_IsRunning = true;
|
|
|
|
m_Thread = new std::thread (std::bind (&StreamingDestinations::Run, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingDestinations::Stop ()
|
|
|
|
{
|
2014-07-11 23:39:38 +04:00
|
|
|
for (auto it: m_Destinations)
|
|
|
|
delete it.second;
|
|
|
|
m_Destinations.clear ();
|
|
|
|
m_SharedLocalDestination = 0; // deleted through m_Destination
|
2014-03-24 00:00:05 +04: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 16:25:30 +04: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 22:54:14 +04:00
|
|
|
auto localDestination = new StreamingDestination (m_Service, fullPath);
|
2014-08-26 22:56:00 +04:00
|
|
|
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
|
2014-07-13 16:25:30 +04:00
|
|
|
numDestinations++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (numDestinations > 0)
|
|
|
|
LogPrint (numDestinations, " local destinations loaded");
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:56:00 +04:00
|
|
|
StreamingDestination * StreamingDestinations::LoadLocalDestination (const std::string& filename)
|
|
|
|
{
|
|
|
|
auto localDestination = new StreamingDestination (m_Service, i2p::util::filesystem::GetFullPath (filename));
|
|
|
|
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
|
|
|
|
return localDestination;
|
|
|
|
}
|
|
|
|
|
2014-03-24 00:00:05 +04:00
|
|
|
Stream * StreamingDestinations::CreateClientStream (const i2p::data::LeaseSet& remote)
|
|
|
|
{
|
|
|
|
if (!m_SharedLocalDestination) return nullptr;
|
2014-08-01 22:54:14 +04:00
|
|
|
return m_SharedLocalDestination->CreateNewOutgoingStream (remote);
|
2014-03-24 00:00:05 +04:00
|
|
|
}
|
2014-08-05 00:30:37 +04:00
|
|
|
|
|
|
|
void StreamingDestinations::DeleteStream (Stream * stream)
|
2014-03-24 00:00:05 +04:00
|
|
|
{
|
2014-08-05 00:30:37 +04:00
|
|
|
if (stream)
|
|
|
|
{
|
|
|
|
m_Service.post (
|
|
|
|
[=](void)
|
|
|
|
{
|
|
|
|
stream->GetLocalDestination ()->DeleteStream (stream);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2014-03-24 00:00:05 +04:00
|
|
|
}
|
2014-08-05 00:30:37 +04:00
|
|
|
|
2014-03-24 00:00:05 +04:00
|
|
|
void StreamingDestinations::HandleNextPacket (i2p::data::IdentHash destination, Packet * packet)
|
2014-03-24 03:48:09 +04:00
|
|
|
{
|
|
|
|
m_Service.post (boost::bind (&StreamingDestinations::PostNextPacket, this, destination, packet));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingDestinations::PostNextPacket (i2p::data::IdentHash destination, Packet * packet)
|
2014-03-24 00:00:05 +04:00
|
|
|
{
|
2014-07-11 23:39:38 +04: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-24 00:00:05 +04:00
|
|
|
}
|
2014-08-20 23:03:10 +04:00
|
|
|
|
|
|
|
StreamingDestination * StreamingDestinations::FindLocalDestination (const i2p::data::IdentHash& destination) const
|
|
|
|
{
|
|
|
|
auto it = m_Destinations.find (destination);
|
|
|
|
if (it != m_Destinations.end ())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-08-26 22:56:00 +04:00
|
|
|
|
2014-02-21 03:42:55 +04:00
|
|
|
Stream * CreateStream (const i2p::data::LeaseSet& remote)
|
2014-01-02 03:19:03 +04:00
|
|
|
{
|
2014-03-24 00:00:05 +04:00
|
|
|
return destinations.CreateClientStream (remote);
|
2014-01-02 03:19:03 +04:00
|
|
|
}
|
|
|
|
|
2014-01-13 06:41:25 +04:00
|
|
|
void DeleteStream (Stream * stream)
|
2014-01-02 03:19:03 +04:00
|
|
|
{
|
2014-08-05 00:30:37 +04:00
|
|
|
destinations.DeleteStream (stream);
|
2014-01-02 03:19:03 +04:00
|
|
|
}
|
2014-03-15 17:16:55 +04:00
|
|
|
|
|
|
|
void StartStreaming ()
|
|
|
|
{
|
2014-03-24 00:00:05 +04:00
|
|
|
destinations.Start ();
|
2014-03-15 17:16:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void StopStreaming ()
|
|
|
|
{
|
2014-03-24 00:00:05 +04:00
|
|
|
destinations.Stop ();
|
2014-03-15 17:16:55 +04:00
|
|
|
}
|
2014-08-05 00:30:37 +04:00
|
|
|
|
|
|
|
StreamingDestination * GetSharedLocalDestination ()
|
|
|
|
{
|
|
|
|
return destinations.GetSharedLocalDestination ();
|
|
|
|
}
|
2014-08-20 23:03:10 +04:00
|
|
|
|
|
|
|
StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination)
|
|
|
|
{
|
|
|
|
return destinations.FindLocalDestination (destination);
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:56:00 +04:00
|
|
|
StreamingDestination * LoadLocalDestination (const std::string& filename)
|
|
|
|
{
|
|
|
|
return destinations.LoadLocalDestination (filename);
|
|
|
|
}
|
|
|
|
|
2014-03-24 00:00:05 +04:00
|
|
|
void HandleDataMessage (i2p::data::IdentHash destination, const uint8_t * buf, size_t len)
|
2013-12-13 06:36:24 +04: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 05:21:38 +04:00
|
|
|
Packet * uncompressed = new Packet;
|
|
|
|
uncompressed->offset = 0;
|
|
|
|
uncompressed->len = decompressor.MaxRetrievable ();
|
2014-01-13 00:57:10 +04:00
|
|
|
if (uncompressed->len > MAX_PACKET_SIZE)
|
|
|
|
{
|
2014-08-07 03:19:59 +04:00
|
|
|
LogPrint ("Received packet size ", uncompressed->len, " exceeds max packet size");
|
2014-01-13 00:57:10 +04:00
|
|
|
uncompressed->len = MAX_PACKET_SIZE;
|
|
|
|
}
|
2014-01-11 05:21:38 +04:00
|
|
|
decompressor.Get (uncompressed->buf, uncompressed->len);
|
2014-03-24 03:48:09 +04:00
|
|
|
// then forward to streaming engine thread
|
2014-03-24 00:00:05 +04:00
|
|
|
destinations.HandleNextPacket (destination, uncompressed);
|
2013-12-13 06:36:24 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Data: protocol ", buf[9], " is not supported");
|
|
|
|
}
|
2013-12-20 06:19:44 +04:00
|
|
|
|
2014-03-25 03:27:20 +04:00
|
|
|
I2NPMessage * CreateDataMessage (Stream * s, const uint8_t * payload, size_t len)
|
2013-12-20 06:19:44 +04:00
|
|
|
{
|
2014-08-09 22:47:00 +04:00
|
|
|
I2NPMessage * msg = NewI2NPShortMessage ();
|
|
|
|
CryptoPP::Gzip compressor; // DEFAULT_DEFLATE_LEVEL
|
|
|
|
if (len <= COMPRESSION_THRESHOLD_SIZE)
|
|
|
|
compressor.SetDeflateLevel (CryptoPP::Gzip::MIN_DEFLATE_LEVEL);
|
2013-12-20 06:19:44 +04:00
|
|
|
compressor.Put (payload, len);
|
|
|
|
compressor.MessageEnd();
|
|
|
|
int size = compressor.MaxRetrievable ();
|
|
|
|
uint8_t * buf = msg->GetPayload ();
|
2014-01-07 05:56:44 +04:00
|
|
|
*(uint32_t *)buf = htobe32 (size); // length
|
2013-12-20 06:19:44 +04:00
|
|
|
buf += 4;
|
|
|
|
compressor.Get (buf, size);
|
2014-01-14 06:18:32 +04:00
|
|
|
memset (buf + 4, 0, 4); // source and destination ports. TODO: fill with proper values later
|
2013-12-20 06:19:44 +04:00
|
|
|
buf[9] = 6; // streaming protocol
|
|
|
|
msg->len += size + 4;
|
|
|
|
FillI2NPMessageHeader (msg, eI2NPData);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
2013-12-13 06:36:24 +04:00
|
|
|
}
|
|
|
|
}
|