2013-12-13 06:36:24 +04:00
|
|
|
#include <endian.h>
|
|
|
|
#include <string>
|
|
|
|
#include <cryptopp/gzip.h>
|
2013-12-31 05:46:33 +04:00
|
|
|
#include <cryptopp/dsa.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"
|
2013-12-13 06:36:24 +04:00
|
|
|
#include "Streaming.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace stream
|
|
|
|
{
|
2013-12-31 05:46:33 +04:00
|
|
|
Stream::Stream (StreamingDestination * local, const i2p::data::IdentHash& remote):
|
|
|
|
m_SendStreamID (0), m_LocalDestination (local)
|
2013-12-20 06:19:44 +04:00
|
|
|
{
|
|
|
|
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::HandleNextPacket (const uint8_t * buf, size_t len)
|
2013-12-13 06:36:24 +04:00
|
|
|
{
|
|
|
|
const uint8_t * end = buf + len;
|
|
|
|
buf += 4; // sendStreamID
|
|
|
|
buf += 4; // receiveStreamID
|
|
|
|
buf += 4; // sequenceNum
|
|
|
|
buf += 4; // ackThrough
|
|
|
|
int nackCount = buf[0];
|
|
|
|
buf++; // NACK count
|
|
|
|
buf += 4*nackCount; // NACKs
|
|
|
|
buf++; // resendDelay
|
|
|
|
uint16_t flags = be16toh (*(uint16_t *)buf);
|
|
|
|
buf += 2; // flags
|
|
|
|
uint16_t optionalSize = be16toh (*(uint16_t *)buf);
|
|
|
|
buf += 2; // optional size
|
|
|
|
const uint8_t * optionalData = buf;
|
|
|
|
buf += optionalSize;
|
|
|
|
|
|
|
|
// process flags
|
|
|
|
if (flags & PACKET_FLAG_SYNCHRONIZE)
|
|
|
|
{
|
|
|
|
LogPrint ("Synchronize");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & PACKET_FLAG_SIGNATURE_INCLUDED)
|
|
|
|
{
|
|
|
|
LogPrint ("Signature");
|
|
|
|
optionalData += 40;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & PACKET_FLAG_FROM_INCLUDED)
|
|
|
|
{
|
|
|
|
LogPrint ("From identity");
|
2013-12-20 06:19:44 +04:00
|
|
|
optionalData += sizeof (i2p::data::Identity);
|
2013-12-13 06:36:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// we have reached payload section
|
|
|
|
std::string str((const char *)buf, end-buf);
|
|
|
|
LogPrint ("Payload: ", str);
|
|
|
|
}
|
2013-12-20 06:19:44 +04:00
|
|
|
|
2013-12-31 05:46:33 +04:00
|
|
|
StreamingDestination * sharedLocalDestination = nullptr;
|
|
|
|
|
|
|
|
StreamingDestination::StreamingDestination ()
|
|
|
|
{
|
|
|
|
// TODO: read from file later
|
|
|
|
m_Keys = i2p::data::CreateRandomKeys ();
|
|
|
|
m_Identity = m_Keys;
|
|
|
|
m_IdentHash = i2p::data::CalculateIdentHash (m_Identity);
|
|
|
|
}
|
|
|
|
|
2013-12-20 06:19:44 +04:00
|
|
|
void StreamingDestination::HandleNextPacket (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t sendStreamID = *(uint32_t *)(buf);
|
|
|
|
auto it = m_Streams.find (sendStreamID);
|
|
|
|
if (it != m_Streams.end ())
|
|
|
|
it->second->HandleNextPacket (buf, len);
|
|
|
|
else
|
|
|
|
LogPrint ("Unknown stream ", sendStreamID);
|
|
|
|
}
|
2013-12-13 06:36:24 +04:00
|
|
|
|
2013-12-20 06:19:44 +04:00
|
|
|
Stream * StreamingDestination::CreateNewStream (const i2p::data::IdentHash& destination)
|
|
|
|
{
|
|
|
|
/*i2p::data::LeaseSet * leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
|
|
|
if (!leaseSet)
|
|
|
|
{
|
|
|
|
i2p::data::netdb.RequestDestination (destination);
|
|
|
|
sleep (5); // wait for 5 seconds
|
|
|
|
leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
|
|
|
if (!leaseSet)
|
|
|
|
{
|
|
|
|
LogPrint ("Couldn't find LeaseSet");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} */
|
2013-12-31 05:46:33 +04:00
|
|
|
Stream * s = new Stream (this, destination);
|
2013-12-20 06:19:44 +04:00
|
|
|
m_Streams[s->GetRecvStreamID ()] = s;
|
|
|
|
return s;
|
|
|
|
}
|
2013-12-31 05:46:33 +04: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;
|
|
|
|
memcpy (buf + size, &m_Identity, sizeof (m_Identity));
|
|
|
|
size += sizeof (m_Identity); // destination
|
|
|
|
memcpy (buf + size, i2p::context.GetLeaseSetPublicKey (), 256);
|
|
|
|
size += 256; // encryption key
|
|
|
|
memset (buf + size, 0, 128);
|
|
|
|
size += 128; // signing key
|
|
|
|
auto tunnel = i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
|
|
|
if (tunnel)
|
|
|
|
{
|
|
|
|
buf[size] = 1; // 1 lease
|
|
|
|
size++; // num
|
|
|
|
memcpy (buf + size, (const uint8_t *)tunnel->GetNextIdentHash (), 32);
|
|
|
|
size += 32; // tunnel_gw
|
|
|
|
*(uint32_t *)(buf + size) = htobe32 (tunnel->GetNextTunnelID ());
|
|
|
|
size += 4; // tunnel_id
|
|
|
|
uint64_t ts = tunnel->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT;
|
|
|
|
ts *= 1000; // in milliseconds
|
|
|
|
*(uint64_t *)(buf + size) = htobe64 (ts);
|
|
|
|
size += 8; // end_date
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[size] = 0; // zero leases
|
|
|
|
size++; // num
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptoPP::DSA::PrivateKey signingPrivateKey;
|
|
|
|
signingPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
|
|
|
|
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
|
|
|
|
CryptoPP::DSA::Signer signer (signingPrivateKey);
|
|
|
|
signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, size, buf+ size);
|
|
|
|
size += 40; // signature
|
|
|
|
|
|
|
|
m->len += size + sizeof (I2NPDatabaseStoreMsg);
|
|
|
|
FillI2NPMessageHeader (m, eI2NPDatabaseStore);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:36:24 +04:00
|
|
|
void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
uint8_t uncompressed[2048];
|
2013-12-20 06:19:44 +04:00
|
|
|
int uncompressedSize = decompressor.MaxRetrievable ();
|
|
|
|
decompressor.Get (uncompressed, uncompressedSize);
|
2013-12-13 06:36:24 +04:00
|
|
|
// then forward to streaming engine
|
2013-12-20 06:19:44 +04:00
|
|
|
// TODO: we have onle one destination, might be more
|
2013-12-31 05:46:33 +04:00
|
|
|
if (sharedLocalDestination)
|
|
|
|
sharedLocalDestination->HandleNextPacket (uncompressed, uncompressedSize);
|
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
|
|
|
|
|
|
|
I2NPMessage * CreateDataMessage (Stream * s, uint8_t * payload, size_t len)
|
|
|
|
{
|
|
|
|
I2NPMessage * msg = NewI2NPMessage ();
|
|
|
|
CryptoPP::Gzip compressor;
|
|
|
|
compressor.Put (payload, len);
|
|
|
|
compressor.MessageEnd();
|
|
|
|
int size = compressor.MaxRetrievable ();
|
|
|
|
uint8_t * buf = msg->GetPayload ();
|
|
|
|
*(uint16_t *)buf = htobe32 (size); // length
|
|
|
|
buf += 4;
|
|
|
|
compressor.Get (buf, size);
|
|
|
|
buf[9] = 6; // streaming protocol
|
|
|
|
msg->len += size + 4;
|
|
|
|
FillI2NPMessageHeader (msg, eI2NPData);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
2013-12-13 06:36:24 +04:00
|
|
|
}
|
|
|
|
}
|