2014-03-19 20:38:55 +00:00
|
|
|
#include <fstream>
|
2014-01-11 01:21:38 +00:00
|
|
|
#include <algorithm>
|
2014-04-01 22:58:47 +00:00
|
|
|
#include <cryptopp/dh.h>
|
2013-12-13 02:36:24 +00:00
|
|
|
#include <cryptopp/gzip.h>
|
|
|
|
#include "Log.h"
|
|
|
|
#include "RouterInfo.h"
|
2013-12-20 02:19:44 +00:00
|
|
|
#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"
|
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),
|
|
|
|
m_SequenceNumber (0), m_LastReceivedSequenceNumber (0), m_IsOpen (false),
|
|
|
|
m_LeaseSetUpdated (true), m_LocalDestination (local), m_RemoteLeaseSet (remote),
|
2014-03-25 18:26:39 +00:00
|
|
|
m_OutboundTunnel (nullptr), m_ReceiveTimer (m_Service)
|
2013-12-20 02:19:44 +00:00
|
|
|
{
|
|
|
|
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
2014-03-23 13:25:16 +00:00
|
|
|
UpdateCurrentRemoteLease ();
|
2013-12-20 02:19:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2014-01-26 23:22:30 +00:00
|
|
|
for (auto it: m_SavedPackets)
|
|
|
|
delete it;
|
2014-01-11 01:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::HandleNextPacket (Packet * packet)
|
|
|
|
{
|
2014-01-10 03:26:30 +00:00
|
|
|
if (!m_SendStreamID)
|
2014-01-30 04:13:59 +00:00
|
|
|
m_SendStreamID = packet->GetReceiveStreamID ();
|
|
|
|
|
|
|
|
uint32_t receivedSeqn = packet->GetSeqn ();
|
2014-02-02 03:20:41 +00:00
|
|
|
LogPrint ("Received seqn=", receivedSeqn);
|
2014-01-19 17:01:12 +00:00
|
|
|
if (!receivedSeqn || receivedSeqn == m_LastReceivedSequenceNumber + 1)
|
|
|
|
{
|
2014-02-02 03:20:41 +00:00
|
|
|
// we have received next in sequence message
|
|
|
|
ProcessPacket (packet);
|
2014-01-26 23:22:30 +00:00
|
|
|
|
|
|
|
// 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-01-26 23:22:30 +00:00
|
|
|
{
|
2014-02-02 03:20:41 +00:00
|
|
|
Packet * savedPacket = *it;
|
2014-01-26 23:22:30 +00:00
|
|
|
m_SavedPackets.erase (it++);
|
|
|
|
|
2014-02-02 03:20:41 +00:00
|
|
|
ProcessPacket (savedPacket);
|
2014-01-26 23:22:30 +00:00
|
|
|
}
|
|
|
|
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 ();
|
2014-01-19 17:01:12 +00:00
|
|
|
}
|
|
|
|
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
|
2014-01-19 17:01:12 +00:00
|
|
|
m_OutboundTunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel (); // pick another tunnel
|
|
|
|
if (m_OutboundTunnel)
|
|
|
|
SendQuickAck (); // resend ack for previous message again
|
2014-01-26 23:22:30 +00:00
|
|
|
delete packet; // packet dropped
|
2014-01-19 17:01:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("Missing messages from ", m_LastReceivedSequenceNumber + 1, " to ", receivedSeqn - 1);
|
2014-01-26 23:22:30 +00:00
|
|
|
// save message and wait for missing message again
|
|
|
|
SavePacket (packet);
|
2014-01-19 17:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
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_SIGNATURE_INCLUDED)
|
|
|
|
{
|
|
|
|
LogPrint ("Signature");
|
|
|
|
optionData += 40;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & PACKET_FLAG_FROM_INCLUDED)
|
|
|
|
{
|
|
|
|
LogPrint ("From identity");
|
|
|
|
optionData += sizeof (i2p::data::Identity);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-26 23:22:30 +00:00
|
|
|
|
2014-01-01 23:19:03 +00:00
|
|
|
size_t Stream::Send (uint8_t * buf, size_t len, int timeout)
|
|
|
|
{
|
2014-01-12 20:57:10 +00:00
|
|
|
if (!m_IsOpen)
|
|
|
|
ConnectAndSend (buf, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: implement
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::ConnectAndSend (uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
m_IsOpen = true;
|
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
|
|
|
|
// TODO: for initial packet only, following packets have different falgs
|
|
|
|
*(uint16_t *)(packet + size) = htobe16 (PACKET_FLAG_SYNCHRONIZE |
|
2014-01-12 20:57:10 +00:00
|
|
|
PACKET_FLAG_FROM_INCLUDED | PACKET_FLAG_SIGNATURE_INCLUDED |
|
|
|
|
PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED | PACKET_FLAG_NO_ACK);
|
2014-01-01 23:19:03 +00:00
|
|
|
size += 2; // flags
|
2014-01-12 20:57:10 +00:00
|
|
|
*(uint16_t *)(packet + size) = htobe16 (sizeof (i2p::data::Identity) + 40 + 2); // identity + signature + packet size
|
2014-01-01 23:19:03 +00:00
|
|
|
size += 2; // options size
|
|
|
|
memcpy (packet + size, &m_LocalDestination->GetIdentity (), sizeof (i2p::data::Identity));
|
|
|
|
size += sizeof (i2p::data::Identity); // from
|
2014-01-12 20:57:10 +00:00
|
|
|
*(uint16_t *)(packet + size) = htobe16 (STREAMING_MTU);
|
|
|
|
size += 2; // max packet size
|
2014-01-01 23:19:03 +00:00
|
|
|
uint8_t * signature = packet + size; // set it later
|
|
|
|
memset (signature, 0, 40); // zeroes for now
|
2014-03-24 23:27:20 +00:00
|
|
|
size += 40; // signature
|
2014-01-01 23:19:03 +00:00
|
|
|
memcpy (packet + size, buf, len);
|
|
|
|
size += len; // payload
|
|
|
|
m_LocalDestination->Sign (packet, size, signature);
|
2014-03-24 23:27:20 +00:00
|
|
|
p->len = size;
|
2014-03-18 02:55:02 +00:00
|
|
|
|
2014-03-24 23:27:20 +00:00
|
|
|
m_Service.post (boost::bind (&Stream::SendPacket, this, p));
|
2014-01-01 23:19:03 +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
|
|
|
|
*(uint16_t *)(packet + size) = PACKET_FLAG_CLOSE | PACKET_FLAG_SIGNATURE_INCLUDED;
|
|
|
|
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-03-20 13:47:02 +00:00
|
|
|
const I2NPMessage * leaseSet = nullptr;
|
2014-03-18 02:55:02 +00:00
|
|
|
if (m_LeaseSetUpdated)
|
|
|
|
{
|
|
|
|
leaseSet = m_LocalDestination->GetLeaseSet ();
|
|
|
|
m_LeaseSetUpdated = false;
|
|
|
|
}
|
|
|
|
I2NPMessage * msg = i2p::garlic::routing.WrapMessage (m_RemoteLeaseSet,
|
2014-03-24 23:27:20 +00:00
|
|
|
CreateDataMessage (this, buf, len), leaseSet);
|
2014-04-14 20:24:57 +00:00
|
|
|
if (!m_OutboundTunnel || m_OutboundTunnel->IsFailed ())
|
2014-03-18 02:55:02 +00:00
|
|
|
m_OutboundTunnel = m_LocalDestination->GetTunnelPool ()->GetNextOutboundTunnel ();
|
|
|
|
if (m_OutboundTunnel)
|
|
|
|
{
|
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
|
|
|
{
|
2014-03-23 13:25:16 +00:00
|
|
|
m_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 ()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2013-12-20 02:19:44 +00:00
|
|
|
|
2013-12-31 01:46:33 +00:00
|
|
|
|
2014-03-15 00:24:12 +00:00
|
|
|
StreamingDestination::StreamingDestination (): m_LeaseSet (nullptr)
|
2013-12-31 01:46:33 +00:00
|
|
|
{
|
|
|
|
m_Keys = i2p::data::CreateRandomKeys ();
|
2014-04-08 07:11:48 +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));
|
2014-04-01 22:58:47 +00:00
|
|
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
|
|
|
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
2014-04-01 17:55:09 +00:00
|
|
|
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this);
|
2013-12-31 01:46:33 +00:00
|
|
|
}
|
2014-01-09 03:47:22 +00:00
|
|
|
|
2014-03-19 20:38:55 +00:00
|
|
|
StreamingDestination::StreamingDestination (const std::string& fullPath): m_LeaseSet (nullptr)
|
|
|
|
{
|
|
|
|
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-04-01 22:58:47 +00:00
|
|
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
|
|
|
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
2014-04-01 17:55:09 +00:00
|
|
|
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this);
|
2014-03-19 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 03:47:22 +00:00
|
|
|
StreamingDestination::~StreamingDestination ()
|
|
|
|
{
|
|
|
|
if (m_LeaseSet)
|
|
|
|
DeleteI2NPMessage (m_LeaseSet);
|
2014-03-15 13:16:55 +00:00
|
|
|
if (m_Pool)
|
|
|
|
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
|
2014-01-09 03:47:22 +00:00
|
|
|
}
|
2014-03-14 19:13:34 +00:00
|
|
|
|
2014-01-11 01:21:38 +00:00
|
|
|
void StreamingDestination::HandleNextPacket (Packet * packet)
|
2013-12-20 02:19:44 +00:00
|
|
|
{
|
2014-02-17 22:47:21 +00:00
|
|
|
uint32_t sendStreamID = packet->GetSendStreamID ();
|
2013-12-20 02:19:44 +00:00
|
|
|
auto it = m_Streams.find (sendStreamID);
|
|
|
|
if (it != m_Streams.end ())
|
2014-01-11 01:21:38 +00:00
|
|
|
it->second->HandleNextPacket (packet);
|
2013-12-20 02:19:44 +00:00
|
|
|
else
|
2014-01-11 01:21:38 +00:00
|
|
|
{
|
2013-12-20 02:19:44 +00:00
|
|
|
LogPrint ("Unknown stream ", sendStreamID);
|
2014-01-11 01:21:38 +00:00
|
|
|
delete packet;
|
|
|
|
}
|
2013-12-20 02:19:44 +00:00
|
|
|
}
|
2013-12-13 02:36:24 +00:00
|
|
|
|
2014-03-23 20:00:05 +00:00
|
|
|
Stream * StreamingDestination::CreateNewStream (boost::asio::io_service& service,
|
|
|
|
const i2p::data::LeaseSet& remote)
|
2013-12-20 02:19:44 +00:00
|
|
|
{
|
2014-03-23 20:00:05 +00:00
|
|
|
Stream * s = new Stream (service, this, remote);
|
2013-12-20 02:19:44 +00:00
|
|
|
m_Streams[s->GetRecvStreamID ()] = s;
|
|
|
|
return s;
|
|
|
|
}
|
2013-12-31 01:46:33 +00:00
|
|
|
|
2014-01-01 23:19:03 +00:00
|
|
|
void StreamingDestination::DeleteStream (Stream * stream)
|
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
{
|
|
|
|
m_Streams.erase (stream->GetRecvStreamID ());
|
|
|
|
delete stream;
|
|
|
|
}
|
|
|
|
}
|
2014-01-09 03:47:22 +00:00
|
|
|
|
2014-03-15 13:16:55 +00:00
|
|
|
void StreamingDestination::UpdateLeaseSet ()
|
2014-01-09 03:47:22 +00:00
|
|
|
{
|
2014-03-15 13:16:55 +00:00
|
|
|
auto newLeaseSet = CreateLeaseSet ();
|
|
|
|
// TODO: make it atomic
|
|
|
|
auto oldLeaseSet = m_LeaseSet;
|
|
|
|
m_LeaseSet = newLeaseSet;
|
|
|
|
if (oldLeaseSet)
|
|
|
|
DeleteI2NPMessage (oldLeaseSet);
|
2014-03-18 02:55:02 +00:00
|
|
|
for (auto it: m_Streams)
|
|
|
|
it.second->SetLeaseSetUpdated ();
|
2014-03-15 13:16:55 +00:00
|
|
|
}
|
2014-01-10 03:26:30 +00:00
|
|
|
|
2014-03-20 13:47:02 +00:00
|
|
|
const I2NPMessage * StreamingDestination::GetLeaseSet ()
|
2014-03-15 13:16:55 +00:00
|
|
|
{
|
2014-03-20 16:48:03 +00:00
|
|
|
if (!m_LeaseSet)
|
|
|
|
m_LeaseSet = CreateLeaseSet ();
|
|
|
|
else
|
|
|
|
RenewI2NPMessageHeader (m_LeaseSet);
|
2014-01-09 03:47:22 +00:00
|
|
|
return m_LeaseSet;
|
|
|
|
}
|
2014-01-01 23:19:03 +00:00
|
|
|
|
2013-12-31 01:46:33 +00:00
|
|
|
I2NPMessage * StreamingDestination::CreateLeaseSet () const
|
|
|
|
{
|
|
|
|
I2NPMessage * m = NewI2NPMessage ();
|
|
|
|
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)m->GetPayload ();
|
|
|
|
memcpy (msg->key, (const uint8_t *)m_IdentHash, 32);
|
|
|
|
msg->type = 1; // LeaseSet
|
|
|
|
msg->replyToken = 0;
|
|
|
|
|
|
|
|
uint8_t * buf = m->GetPayload () + sizeof (I2NPDatabaseStoreMsg);
|
|
|
|
size_t size = 0;
|
2014-03-20 02:03:50 +00:00
|
|
|
memcpy (buf + size, &m_Keys.pub, sizeof (m_Keys.pub));
|
|
|
|
size += sizeof (m_Keys.pub); // destination
|
2014-03-16 02:02:33 +00:00
|
|
|
memcpy (buf + size, m_Pool->GetEncryptionPublicKey (), 256);
|
2013-12-31 01:46:33 +00:00
|
|
|
size += 256; // encryption key
|
|
|
|
memset (buf + size, 0, 128);
|
|
|
|
size += 128; // signing key
|
2014-03-15 13:16:55 +00:00
|
|
|
auto tunnels = m_Pool->GetInboundTunnels (5); // 5 tunnels maximum
|
2014-01-15 00:00:12 +00:00
|
|
|
buf[size] = tunnels.size (); // num leases
|
|
|
|
size++; // num
|
|
|
|
for (auto it: tunnels)
|
2013-12-31 01:46:33 +00:00
|
|
|
{
|
2014-01-15 00:00:12 +00:00
|
|
|
auto tunnel = it;
|
2013-12-31 01:46:33 +00:00
|
|
|
memcpy (buf + size, (const uint8_t *)tunnel->GetNextIdentHash (), 32);
|
|
|
|
size += 32; // tunnel_gw
|
|
|
|
*(uint32_t *)(buf + size) = htobe32 (tunnel->GetNextTunnelID ());
|
|
|
|
size += 4; // tunnel_id
|
2014-03-23 13:25:16 +00:00
|
|
|
uint64_t ts = tunnel->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - 60; // 1 minute before expiration
|
2013-12-31 01:46:33 +00:00
|
|
|
ts *= 1000; // in milliseconds
|
|
|
|
*(uint64_t *)(buf + size) = htobe64 (ts);
|
|
|
|
size += 8; // end_date
|
|
|
|
}
|
2014-01-01 23:19:03 +00:00
|
|
|
Sign (buf, size, buf+ size);
|
2013-12-31 01:46:33 +00:00
|
|
|
size += 40; // signature
|
2014-03-22 20:05:58 +00:00
|
|
|
LogPrint ("Local LeaseSet of ", tunnels.size (), " leases created");
|
2013-12-31 01:46:33 +00:00
|
|
|
m->len += size + sizeof (I2NPDatabaseStoreMsg);
|
|
|
|
FillI2NPMessageHeader (m, eI2NPDatabaseStore);
|
|
|
|
return m;
|
|
|
|
}
|
2014-01-01 23:19:03 +00:00
|
|
|
|
|
|
|
void StreamingDestination::Sign (uint8_t * buf, int len, uint8_t * signature) const
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
m_SharedLocalDestination = new StreamingDestination ();
|
|
|
|
|
|
|
|
m_IsRunning = true;
|
|
|
|
m_Thread = new std::thread (std::bind (&StreamingDestinations::Run, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingDestinations::Stop ()
|
|
|
|
{
|
|
|
|
delete m_SharedLocalDestination;
|
|
|
|
|
|
|
|
m_IsRunning = false;
|
|
|
|
m_Service.stop ();
|
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingDestinations::Run ()
|
|
|
|
{
|
|
|
|
m_Service.run ();
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream * StreamingDestinations::CreateClientStream (const i2p::data::LeaseSet& remote)
|
|
|
|
{
|
|
|
|
if (!m_SharedLocalDestination) return nullptr;
|
|
|
|
return m_SharedLocalDestination->CreateNewStream (m_Service, remote);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingDestinations::DeleteClientStream (Stream * stream)
|
|
|
|
{
|
|
|
|
if (m_SharedLocalDestination)
|
|
|
|
m_SharedLocalDestination->DeleteStream (stream);
|
|
|
|
else
|
|
|
|
delete stream;
|
|
|
|
}
|
2014-03-23 23:48:09 +00:00
|
|
|
|
2014-03-23 20:00:05 +00:00
|
|
|
void StreamingDestinations::HandleNextPacket (i2p::data::IdentHash destination, Packet * packet)
|
2014-03-23 23:48:09 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
// TODO: we have onle one destination, might be more
|
|
|
|
if (m_SharedLocalDestination)
|
|
|
|
m_SharedLocalDestination->HandleNextPacket (packet);
|
|
|
|
}
|
2014-01-01 23:19:03 +00:00
|
|
|
|
2014-02-20 23:42:55 +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
|
|
|
}
|
|
|
|
|
2014-01-13 02:41:25 +00:00
|
|
|
void DeleteStream (Stream * stream)
|
2014-01-01 23:19:03 +00:00
|
|
|
{
|
2014-03-23 20:00:05 +00:00
|
|
|
destinations.DeleteClientStream (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
|
|
|
}
|
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);
|
2014-03-23 23:48:09 +00:00
|
|
|
// 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");
|
|
|
|
}
|
2013-12-20 02:19:44 +00:00
|
|
|
|
2014-03-24 23:27:20 +00:00
|
|
|
I2NPMessage * CreateDataMessage (Stream * s, const uint8_t * payload, size_t len)
|
2013-12-20 02:19:44 +00:00
|
|
|
{
|
|
|
|
I2NPMessage * msg = NewI2NPMessage ();
|
|
|
|
CryptoPP::Gzip compressor;
|
2014-01-14 02:18:32 +00:00
|
|
|
compressor.SetDeflateLevel (CryptoPP::Gzip::MIN_DEFLATE_LEVEL);
|
2013-12-20 02:19:44 +00:00
|
|
|
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
|
2013-12-20 02:19:44 +00:00
|
|
|
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
|
2013-12-20 02:19:44 +00:00
|
|
|
buf[9] = 6; // streaming protocol
|
|
|
|
msg->len += size + 4;
|
|
|
|
FillI2NPMessageHeader (msg, eI2NPData);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
2013-12-13 02:36:24 +00:00
|
|
|
}
|
|
|
|
}
|