gzip/gunzip per destination

This commit is contained in:
orignal 2014-10-07 14:24:31 -04:00
parent 9f1e496fa4
commit a32d82f05b
2 changed files with 24 additions and 17 deletions

View File

@ -1,7 +1,6 @@
#include <fstream> #include <fstream>
#include <algorithm> #include <algorithm>
#include <cryptopp/dh.h> #include <cryptopp/dh.h>
#include <cryptopp/gzip.h>
#include "Log.h" #include "Log.h"
#include "util.h" #include "util.h"
#include "Destination.h" #include "Destination.h"
@ -178,20 +177,23 @@ namespace stream
if (buf[9] == 6) // streaming protocol if (buf[9] == 6) // streaming protocol
{ {
// unzip it // unzip it
CryptoPP::Gunzip decompressor; m_Decompressor.Put (buf, length);
decompressor.Put (buf, length); m_Decompressor.MessageEnd();
decompressor.MessageEnd();
Packet * uncompressed = new Packet; Packet * uncompressed = new Packet;
uncompressed->offset = 0; uncompressed->offset = 0;
uncompressed->len = decompressor.MaxRetrievable (); uncompressed->len = m_Decompressor.MaxRetrievable ();
if (uncompressed->len > MAX_PACKET_SIZE) if (uncompressed->len <= MAX_PACKET_SIZE)
{ {
LogPrint ("Received packet size ", uncompressed->len, " exceeds max packet size"); m_Decompressor.Get (uncompressed->buf, uncompressed->len);
uncompressed->len = MAX_PACKET_SIZE; // then forward to streaming thread
m_Service.post (boost::bind (&StreamingDestination::HandleNextPacket, this, uncompressed));
}
else
{
LogPrint ("Received packet size ", uncompressed->len, " exceeds max packet size. Skipped");
m_Decompressor.Skip ();
delete uncompressed;
} }
decompressor.Get (uncompressed->buf, uncompressed->len);
// then forward to streaming thread
m_Service.post (boost::bind (&StreamingDestination::HandleNextPacket, this, uncompressed));
} }
else else
LogPrint ("Data: unexpected protocol ", buf[9]); LogPrint ("Data: unexpected protocol ", buf[9]);
@ -200,16 +202,17 @@ namespace stream
I2NPMessage * StreamingDestination::CreateDataMessage (const uint8_t * payload, size_t len) I2NPMessage * StreamingDestination::CreateDataMessage (const uint8_t * payload, size_t len)
{ {
I2NPMessage * msg = NewI2NPShortMessage (); I2NPMessage * msg = NewI2NPShortMessage ();
CryptoPP::Gzip compressor; // DEFAULT_DEFLATE_LEVEL
if (len <= COMPRESSION_THRESHOLD_SIZE) if (len <= COMPRESSION_THRESHOLD_SIZE)
compressor.SetDeflateLevel (CryptoPP::Gzip::MIN_DEFLATE_LEVEL); m_Compressor.SetDeflateLevel (CryptoPP::Gzip::MIN_DEFLATE_LEVEL);
compressor.Put (payload, len); else
compressor.MessageEnd(); m_Compressor.SetDeflateLevel (CryptoPP::Gzip::DEFAULT_DEFLATE_LEVEL);
int size = compressor.MaxRetrievable (); m_Compressor.Put (payload, len);
m_Compressor.MessageEnd();
int size = m_Compressor.MaxRetrievable ();
uint8_t * buf = msg->GetPayload (); uint8_t * buf = msg->GetPayload ();
*(uint32_t *)buf = htobe32 (size); // length *(uint32_t *)buf = htobe32 (size); // length
buf += 4; buf += 4;
compressor.Get (buf, size); m_Compressor.Get (buf, size);
memset (buf + 4, 0, 4); // source and destination ports. TODO: fill with proper values later memset (buf + 4, 0, 4); // source and destination ports. TODO: fill with proper values later
buf[9] = 6; // streaming protocol buf[9] = 6; // streaming protocol
msg->len += size + 4; msg->len += size + 4;

View File

@ -3,6 +3,7 @@
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <cryptopp/gzip.h>
#include "Identity.h" #include "Identity.h"
#include "TunnelPool.h" #include "TunnelPool.h"
#include "CryptoConst.h" #include "CryptoConst.h"
@ -63,6 +64,9 @@ namespace stream
bool m_IsPublic; bool m_IsPublic;
std::function<void (Stream *)> m_Acceptor; std::function<void (Stream *)> m_Acceptor;
CryptoPP::Gzip m_Compressor;
CryptoPP::Gunzip m_Decompressor;
}; };
class StreamingDestinations class StreamingDestinations