Skip to content

Commit

Permalink
filesytem-based addressbook
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Nov 26, 2014
1 parent aca87b5 commit fae01f6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
94 changes: 92 additions & 2 deletions AddressBook.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,103 @@
#include <string.h>
#include <inttypes.h>
#include <string>
#include <map>
#include <fstream>
#include <boost/filesystem.hpp>
#include "base64.h"
#include "util.h"
#include "Identity.h"
#include "Log.h"
#include "AddressBook.h"

#include <boost/algorithm/string.hpp>

namespace i2p
{
namespace client
{

class AddressBookFilesystemStorage: public AddressBookStorage
{
public:

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

private:

boost::filesystem::path GetPath () const { return i2p::util::filesystem::GetDefaultDataDir() / "addressbook"; };
};


AddressBookFilesystemStorage::AddressBookFilesystemStorage ()
{
auto path = GetPath ();
if (!boost::filesystem::exists (path))
{
// Create directory is necessary
if (!boost::filesystem::create_directory (path))
LogPrint (eLogError, "Failed to create addressbook directory");
}
}

bool AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const
{
auto filename = GetPath () / (ident.ToBase32() + ".b32");
std::ifstream f(filename.c_str (), std::ifstream::binary);
if (f.is_open ())
{
f.seekg (0,std::ios::end);
size_t len = f.tellg ();
if (len < i2p::data::DEFAULT_IDENTITY_SIZE)
{
LogPrint (eLogError, "File ", filename, " is too short. ", len);
return false;
}
f.seekg(0, std::ios::beg);
uint8_t * buf = new uint8_t[len];
f.read((char *)buf, len);
address.FromBuffer (buf, len);
delete[] buf;
return true;
}
else
return false;
}

void AddressBookFilesystemStorage::AddAddress (const i2p::data::IdentityEx& address)
{
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 ();
uint8_t * buf = new uint8_t[len];
address.ToBuffer (buf, len);
f.write ((char *)buf, len);
delete[] buf;
}
else
LogPrint (eLogError, "Can't open file ", filename);
}

void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
{
auto filename = GetPath () / (ident.ToBase32() + ".b32");
if (boost::filesystem::exists (filename))
boost::filesystem::remove (filename);
}

//---------------------------------------------------------------------
AddressBook::AddressBook (): m_IsLoaded (false), m_IsDowloading (false)
{
}

AddressBook::~AddressBook ()
{
delete m_Storage;
}

bool AddressBook::GetIdentHash (const std::string& address, i2p::data::IdentHash& ident)
{
auto pos = address.find(".b32.i2p");
Expand Down Expand Up @@ -59,10 +139,20 @@ namespace client
{
i2p::data::IdentityEx ident;
ident.FromBase64 (base64);
if (m_Storage) m_Storage->AddAddress (ident);
m_Addresses[address] = ident.GetIdentHash ();
LogPrint (address,"->",ident.GetIdentHash ().ToBase32 (), ".b32.i2p added");
}

bool AddressBook::GetAddress (const std::string& address, i2p::data::IdentityEx& identity)
{
if (!m_Storage)
m_Storage = new AddressBookFilesystemStorage ();
auto ident = FindAddress (address);
if (!ident) return false;
return m_Storage->GetAddress (*ident, identity);
}

void AddressBook::LoadHostsFromI2P ()
{
std::string content;
Expand Down
14 changes: 14 additions & 0 deletions AddressBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,35 @@ namespace i2p
{
namespace client
{
class AddressBookStorage // interface for storage
{
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 void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
};

class AddressBook
{
public:

AddressBook ();
~AddressBook ();
bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident);
bool GetAddress (const std::string& address, i2p::data::IdentityEx& identity);
const i2p::data::IdentHash * FindAddress (const std::string& address);
void InsertAddress (const std::string& address, const std::string& base64); // for jump service


private:

void LoadHosts ();
void LoadHostsFromI2P ();

std::map<std::string, i2p::data::IdentHash> m_Addresses;
AddressBookStorage * m_Storage;
bool m_IsLoaded, m_IsDowloading;
};
}
Expand Down

0 comments on commit fae01f6

Please sign in to comment.