Skip to content

Commit

Permalink
cumulative update from bitbucket
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Nov 3, 2015
1 parent 73d4025 commit 62cf839
Show file tree
Hide file tree
Showing 76 changed files with 2,399 additions and 2,263 deletions.
52 changes: 25 additions & 27 deletions AddressBook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#include <condition_variable>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <cryptopp/osrng.h>
#include "base64.h"
#include "Base.h"
#include "util.h"
#include "Identity.h"
#include "Log.h"
Expand All @@ -26,8 +25,8 @@ namespace client
public:

AddressBookFilesystemStorage ();
bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const;
void AddAddress (const i2p::data::IdentityEx& address);
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const;
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address);
void RemoveAddress (const i2p::data::IdentHash& ident);

int Load (std::map<std::string, i2p::data::IdentHash>& addresses);
Expand All @@ -50,7 +49,7 @@ namespace client
}
}

bool AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const
std::shared_ptr<const i2p::data::IdentityEx> AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident) const
{
auto filename = GetPath () / (ident.ToBase32() + ".b32");
std::ifstream f(filename.c_str (), std::ifstream::binary);
Expand All @@ -61,28 +60,28 @@ namespace client
if (len < i2p::data::DEFAULT_IDENTITY_SIZE)
{
LogPrint (eLogError, "File ", filename, " is too short. ", len);
return false;
return nullptr;
}
f.seekg(0, std::ios::beg);
uint8_t * buf = new uint8_t[len];
f.read((char *)buf, len);
address.FromBuffer (buf, len);
auto address = std::make_shared<i2p::data::IdentityEx>(buf, len);
delete[] buf;
return true;
return address;
}
else
return false;
return nullptr;
}

void AddressBookFilesystemStorage::AddAddress (const i2p::data::IdentityEx& address)
void AddressBookFilesystemStorage::AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
{
auto filename = GetPath () / (address.GetIdentHash ().ToBase32() + ".b32");
auto filename = GetPath () / (address->GetIdentHash ().ToBase32() + ".b32");
std::ofstream f (filename.c_str (), std::ofstream::binary | std::ofstream::out);
if (f.is_open ())
{
size_t len = address.GetFullLen ();
size_t len = address->GetFullLen ();
uint8_t * buf = new uint8_t[len];
address.ToBuffer (buf, len);
address->ToBuffer (buf, len);
f.write ((char *)buf, len);
delete[] buf;
}
Expand Down Expand Up @@ -256,29 +255,29 @@ namespace client

void AddressBook::InsertAddress (const std::string& address, const std::string& base64)
{
i2p::data::IdentityEx ident;
ident.FromBase64 (base64);
auto ident = std::make_shared<i2p::data::IdentityEx>();
ident->FromBase64 (base64);
if (!m_Storage)
m_Storage = CreateStorage ();
m_Storage->AddAddress (ident);
m_Addresses[address] = ident.GetIdentHash ();
LogPrint (address,"->", ToAddress(ident.GetIdentHash ()), " added");
m_Addresses[address] = ident->GetIdentHash ();
LogPrint (address,"->", ToAddress(ident->GetIdentHash ()), " added");
}

void AddressBook::InsertAddress (const i2p::data::IdentityEx& address)
void AddressBook::InsertAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
{
if (!m_Storage)
m_Storage = CreateStorage ();
m_Storage->AddAddress (address);
}

bool AddressBook::GetAddress (const std::string& address, i2p::data::IdentityEx& identity)
std::shared_ptr<const i2p::data::IdentityEx> AddressBook::GetAddress (const std::string& address)
{
if (!m_Storage)
m_Storage = CreateStorage ();
i2p::data::IdentHash ident;
if (!GetIdentHash (address, ident)) return false;
return m_Storage->GetAddress (ident, identity);
if (!GetIdentHash (address, ident)) return nullptr;
return m_Storage->GetAddress (ident);
}

void AddressBook::LoadHosts ()
Expand Down Expand Up @@ -332,10 +331,10 @@ namespace client
std::string name = s.substr(0, pos++);
std::string addr = s.substr(pos);

i2p::data::IdentityEx ident;
if (ident.FromBase64(addr))
auto ident = std::make_shared<i2p::data::IdentityEx> ();
if (ident->FromBase64(addr))
{
m_Addresses[name] = ident.GetIdentHash ();
m_Addresses[name] = ident->GetIdentHash ();
m_Storage->AddAddress (ident);
numAddresses++;
}
Expand Down Expand Up @@ -415,11 +414,10 @@ namespace client
{
auto dest = i2p::client::context.GetSharedLocalDestination ();
if (!dest) return;
if (m_IsLoaded && !m_IsDownloading && dest->IsReady ())
if (m_IsLoaded && !m_IsDownloading && dest->IsReady () && !m_Subscriptions.empty ())
{
// pick random subscription
CryptoPP::AutoSeededRandomPool rnd;
auto ind = rnd.GenerateWord32 (0, m_Subscriptions.size() - 1);
auto ind = rand () % m_Subscriptions.size();
m_IsDownloading = true;
m_Subscriptions[ind]->CheckSubscription ();
}
Expand Down
13 changes: 7 additions & 6 deletions AddressBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include <vector>
#include <iostream>
#include <mutex>
#include <memory>
#include <boost/asio.hpp>
#include "base64.h"
#include "Base.h"
#include "util.h"
#include "Identity.h"
#include "Log.h"
Expand All @@ -31,8 +32,8 @@ namespace client
public:

virtual ~AddressBookStorage () {};
virtual bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const = 0;
virtual void AddAddress (const i2p::data::IdentityEx& address) = 0;
virtual std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const = 0;
virtual void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) = 0;
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;

virtual int Load (std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
Expand All @@ -49,16 +50,16 @@ namespace client
void Start ();
void Stop ();
bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident);
bool GetAddress (const std::string& address, i2p::data::IdentityEx& identity);
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const std::string& address);
const i2p::data::IdentHash * FindAddress (const std::string& address);
void InsertAddress (const std::string& address, const std::string& base64); // for jump service
void InsertAddress (const i2p::data::IdentityEx& address);
void InsertAddress (std::shared_ptr<const i2p::data::IdentityEx> address);

void LoadHostsFromStream (std::istream& f);
void DownloadComplete (bool success);
//This method returns the ".b32.i2p" address
std::string ToAddress(const i2p::data::IdentHash& ident) { return GetB32Address(ident); }
std::string ToAddress(const i2p::data::IdentityEx& ident) { return ToAddress(ident.GetIdentHash ()); }
std::string ToAddress(std::shared_ptr<const i2p::data::IdentityEx> ident) { return ToAddress(ident->GetIdentHash ()); }
private:

void StartSubscriptions ();
Expand Down
10 changes: 5 additions & 5 deletions BOB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@ namespace client
{
LogPrint (eLogDebug, "BOB: newkeys");
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys ();
SendReplyOK (m_Keys.GetPublic ().ToBase64 ().c_str ());
SendReplyOK (m_Keys.GetPublic ()->ToBase64 ().c_str ());
}

void BOBCommandSession::SetkeysCommandHandler (const char * operand, size_t len)
{
LogPrint (eLogDebug, "BOB: setkeys ", operand);
m_Keys.FromBase64 (operand);
SendReplyOK (m_Keys.GetPublic ().ToBase64 ().c_str ());
SendReplyOK (m_Keys.GetPublic ()->ToBase64 ().c_str ());
}

void BOBCommandSession::GetkeysCommandHandler (const char * operand, size_t len)
Expand All @@ -427,7 +427,7 @@ namespace client
void BOBCommandSession::GetdestCommandHandler (const char * operand, size_t len)
{
LogPrint (eLogDebug, "BOB: getdest");
SendReplyOK (m_Keys.GetPublic ().ToBase64 ().c_str ());
SendReplyOK (m_Keys.GetPublic ()->ToBase64 ().c_str ());
}

void BOBCommandSession::OuthostCommandHandler (const char * operand, size_t len)
Expand Down Expand Up @@ -477,15 +477,15 @@ namespace client
auto localDestination = m_CurrentDestination->GetLocalDestination ();
auto leaseSet = localDestination->FindLeaseSet (ident);
if (leaseSet)
SendReplyOK (leaseSet->GetIdentity ().ToBase64 ().c_str ());
SendReplyOK (leaseSet->GetIdentity ()->ToBase64 ().c_str ());
else
{
auto s = shared_from_this ();
localDestination->RequestDestination (ident,
[s](std::shared_ptr<i2p::data::LeaseSet> ls)
{
if (ls)
s->SendReplyOK (ls->GetIdentity ().ToBase64 ().c_str ());
s->SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ());
else
s->SendReplyError ("LeaseSet Not found");
}
Expand Down
68 changes: 66 additions & 2 deletions base64.cpp → Base.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <stdlib.h>
#include "base64.h"
#include "Log.h"
#include "Base.h"

namespace i2p
{
namespace data
{

static void iT64Build(void);

/*
Expand Down Expand Up @@ -265,5 +265,69 @@ namespace data
}
return ret;
}

GzipInflator::GzipInflator (): m_IsDirty (false)
{
memset (&m_Inflator, 0, sizeof (m_Inflator));
inflateInit2 (&m_Inflator, MAX_WBITS + 16); // gzip
}

GzipInflator::~GzipInflator ()
{
inflateEnd (&m_Inflator);
}

size_t GzipInflator::Inflate (const uint8_t * in, size_t inLen, uint8_t * out, size_t outLen)
{
if (m_IsDirty) inflateReset (&m_Inflator);
m_IsDirty = true;
m_Inflator.next_in = const_cast<uint8_t *>(in);
m_Inflator.avail_in = inLen;
m_Inflator.next_out = out;
m_Inflator.avail_out = outLen;
int err;
if ((err = inflate (&m_Inflator, Z_NO_FLUSH)) == Z_STREAM_END)
return outLen - m_Inflator.avail_out;
else
{
LogPrint (eLogError, "Decompression error ", err);
return 0;
}
}

GzipDeflator::GzipDeflator (): m_IsDirty (false)
{
memset (&m_Deflator, 0, sizeof (m_Deflator));
deflateInit2 (&m_Deflator, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY); // 15 + 16 sets gzip
}

GzipDeflator::~GzipDeflator ()
{
deflateEnd (&m_Deflator);
}

void GzipDeflator::SetCompressionLevel (int level)
{
deflateParams (&m_Deflator, level, Z_DEFAULT_STRATEGY);
}

size_t GzipDeflator::Deflate (const uint8_t * in, size_t inLen, uint8_t * out, size_t outLen)
{
if (m_IsDirty) deflateReset (&m_Deflator);
m_IsDirty = true;
m_Deflator.next_in = const_cast<uint8_t *>(in);
m_Deflator.avail_in = inLen;
m_Deflator.next_out = out;
m_Deflator.avail_out = outLen;
int err;
if ((err = deflate (&m_Deflator, Z_FINISH)) == Z_STREAM_END)
return outLen - m_Deflator.avail_out;
else
{
LogPrint (eLogError, "Compression error ", err);
return 0;
}
}
}
}

Loading

0 comments on commit 62cf839

Please sign in to comment.