i2pd/AddressBook.cpp

842 lines
26 KiB
C++
Raw Normal View History

2014-04-01 19:18:14 +00:00
#include <string.h>
2014-11-26 21:19:36 +00:00
#include <inttypes.h>
2014-04-01 19:18:14 +00:00
#include <string>
#include <map>
2014-11-26 21:19:36 +00:00
#include <fstream>
#include <chrono>
#include <condition_variable>
2016-03-26 14:31:47 +00:00
#include <openssl/rand.h>
2015-11-03 14:15:49 +00:00
#include "Base.h"
2014-04-01 19:18:14 +00:00
#include "util.h"
#include "Identity.h"
2016-02-11 00:00:00 +00:00
#include "FS.h"
2014-04-01 19:18:14 +00:00
#include "Log.h"
#include "HTTP.h"
#include "NetDb.h"
#include "ClientContext.h"
2014-04-01 19:18:14 +00:00
#include "AddressBook.h"
namespace i2p
{
2014-10-24 19:22:36 +00:00
namespace client
2014-04-01 19:18:14 +00:00
{
2016-02-11 00:00:00 +00:00
// TODO: this is actually proxy class
2014-11-26 21:19:36 +00:00
class AddressBookFilesystemStorage: public AddressBookStorage
{
private:
i2p::fs::HashedStorage storage;
2016-03-16 19:40:29 +00:00
std::string etagsPath, indexPath, localPath;
2014-11-26 21:19:36 +00:00
public:
AddressBookFilesystemStorage (): storage("addressbook", "b", "", "b32") {};
2015-11-03 14:15:49 +00:00
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const;
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address);
2014-11-26 21:19:36 +00:00
void RemoveAddress (const i2p::data::IdentHash& ident);
bool Init ();
2014-11-28 14:40:27 +00:00
int Load (std::map<std::string, i2p::data::IdentHash>& addresses);
2016-03-16 19:40:29 +00:00
int LoadLocal (std::map<std::string, i2p::data::IdentHash>& addresses);
2014-11-28 14:40:27 +00:00
int Save (const std::map<std::string, i2p::data::IdentHash>& addresses);
void SaveEtag (const i2p::data::IdentHash& subsciption, const std::string& etag, const std::string& lastModified);
2016-03-15 18:37:07 +00:00
bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified);
2016-03-16 19:40:29 +00:00
private:
int LoadFromFile (const std::string& filename, std::map<std::string, i2p::data::IdentHash>& addresses); // returns -1 if can't open file, otherwise number of records
2014-11-26 21:19:36 +00:00
};
bool AddressBookFilesystemStorage::Init()
{
storage.SetPlace(i2p::fs::GetDataDir());
// init storage
2016-03-19 12:07:09 +00:00
if (storage.Init(i2p::data::GetBase32SubstitutionTable(), 32))
{
// init ETags
etagsPath = i2p::fs::StorageRootPath (storage, "etags");
if (!i2p::fs::Exists (etagsPath))
i2p::fs::CreateDirectory (etagsPath);
// init address files
indexPath = i2p::fs::StorageRootPath (storage, "addresses.csv");
localPath = i2p::fs::StorageRootPath (storage, "local.csv");
return true;
}
return false;
}
2015-11-03 14:15:49 +00:00
std::shared_ptr<const i2p::data::IdentityEx> AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident) const
2014-11-26 21:19:36 +00:00
{
std::string filename = storage.Path(ident.ToBase32());
2016-02-11 00:00:00 +00:00
std::ifstream f(filename, std::ifstream::binary);
if (!f.is_open ()) {
LogPrint(eLogDebug, "Addressbook: Requested, but not found: ", filename);
return nullptr;
2014-11-26 21:19:36 +00:00
}
2016-02-11 00:00:00 +00:00
f.seekg (0,std::ios::end);
size_t len = f.tellg ();
if (len < i2p::data::DEFAULT_IDENTITY_SIZE) {
2016-03-11 00:46:52 +00:00
LogPrint (eLogError, "Addressbook: File ", filename, " is too short: ", len);
2015-11-03 14:15:49 +00:00
return nullptr;
2016-02-11 00:00:00 +00:00
}
f.seekg(0, std::ios::beg);
uint8_t * buf = new uint8_t[len];
f.read((char *)buf, len);
auto address = std::make_shared<i2p::data::IdentityEx>(buf, len);
delete[] buf;
return address;
2014-11-26 21:19:36 +00:00
}
2015-11-03 14:15:49 +00:00
void AddressBookFilesystemStorage::AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
2014-11-26 21:19:36 +00:00
{
std::string path = storage.Path( address->GetIdentHash().ToBase32() );
2016-02-11 00:00:00 +00:00
std::ofstream f (path, std::ofstream::binary | std::ofstream::out);
if (!f.is_open ()) {
2016-03-11 00:46:52 +00:00
LogPrint (eLogError, "Addressbook: can't open file ", path);
2016-02-11 00:00:00 +00:00
return;
2014-11-26 21:19:36 +00:00
}
2016-02-11 00:00:00 +00:00
size_t len = address->GetFullLen ();
uint8_t * buf = new uint8_t[len];
address->ToBuffer (buf, len);
f.write ((char *)buf, len);
delete[] buf;
2014-11-26 21:19:36 +00:00
}
void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
{
storage.Remove( ident.ToBase32() );
2014-11-26 21:19:36 +00:00
}
2016-03-16 19:40:29 +00:00
int AddressBookFilesystemStorage::LoadFromFile (const std::string& filename, std::map<std::string, i2p::data::IdentHash>& addresses)
2014-11-27 21:26:55 +00:00
{
int num = 0;
2016-03-16 19:40:29 +00:00
std::ifstream f (filename, std::ifstream::in); // in text mode
if (!f) return -1;
2016-02-11 00:00:00 +00:00
addresses.clear ();
2016-03-16 19:40:29 +00:00
while (!f.eof ())
{
std::string s;
2016-02-11 00:00:00 +00:00
getline(f, s);
2016-03-16 19:40:29 +00:00
if (!s.length()) continue; // skip empty line
2016-02-11 00:00:00 +00:00
std::size_t pos = s.find(',');
if (pos != std::string::npos)
2014-11-27 21:26:55 +00:00
{
2016-02-11 00:00:00 +00:00
std::string name = s.substr(0, pos++);
std::string addr = s.substr(pos);
2014-11-27 21:26:55 +00:00
2016-02-11 00:00:00 +00:00
i2p::data::IdentHash ident;
ident.FromBase32 (addr);
addresses[name] = ident;
num++;
}
2014-11-27 21:26:55 +00:00
}
2016-03-16 19:40:29 +00:00
return num;
}
2016-02-11 00:00:00 +00:00
2016-03-16 19:40:29 +00:00
int AddressBookFilesystemStorage::Load (std::map<std::string, i2p::data::IdentHash>& addresses)
{
int num = LoadFromFile (indexPath, addresses);
if (num < 0)
{
LogPrint(eLogWarning, "Addressbook: Can't open ", indexPath);
return 0;
}
LogPrint(eLogInfo, "Addressbook: using index file ", indexPath);
2016-02-11 00:00:00 +00:00
LogPrint (eLogInfo, "Addressbook: ", num, " addresses loaded from storage");
2016-03-16 19:40:29 +00:00
return num;
}
int AddressBookFilesystemStorage::LoadLocal (std::map<std::string, i2p::data::IdentHash>& addresses)
{
int num = LoadFromFile (localPath, addresses);
if (num < 0) return 0;
LogPrint (eLogInfo, "Addressbook: ", num, " local addresses loaded");
2014-11-27 21:26:55 +00:00
return num;
}
2014-11-28 14:40:27 +00:00
int AddressBookFilesystemStorage::Save (const std::map<std::string, i2p::data::IdentHash>& addresses)
2014-11-27 21:26:55 +00:00
{
2016-06-01 00:00:00 +00:00
if (addresses.empty()) {
LogPrint(eLogWarning, "Addressbook: not saving empty addressbook");
return 0;
}
2014-11-27 21:26:55 +00:00
int num = 0;
std::ofstream f (indexPath, std::ofstream::out); // in text mode
2016-02-11 00:00:00 +00:00
if (!f.is_open ()) {
LogPrint (eLogWarning, "Addressbook: Can't open ", indexPath);
2016-02-11 00:00:00 +00:00
return 0;
}
2016-08-05 18:23:54 +00:00
for (const auto& it: addresses) {
2016-02-11 00:00:00 +00:00
f << it.first << "," << it.second.ToBase32 () << std::endl;
num++;
2014-11-27 21:26:55 +00:00
}
2016-02-11 00:00:00 +00:00
LogPrint (eLogInfo, "Addressbook: ", num, " addresses saved");
2014-11-27 21:26:55 +00:00
return num;
}
void AddressBookFilesystemStorage::SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified)
{
std::string fname = etagsPath + i2p::fs::dirSep + subscription.ToBase32 () + ".txt";
std::ofstream f (fname, std::ofstream::out | std::ofstream::trunc);
if (f)
2016-03-15 02:00:05 +00:00
{
f << etag << std::endl;
f<< lastModified << std::endl;
}
}
2016-03-15 18:37:07 +00:00
bool AddressBookFilesystemStorage::GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified)
{
std::string fname = etagsPath + i2p::fs::dirSep + subscription.ToBase32 () + ".txt";
std::ifstream f (fname, std::ofstream::in);
if (!f || f.eof ()) return false;
std::getline (f, etag);
if (f.eof ()) return false;
std::getline (f, lastModified);
return true;
}
2014-11-26 21:19:36 +00:00
//---------------------------------------------------------------------
AddressBook::AddressBook (): m_Storage(nullptr), m_IsLoaded (false), m_IsDownloading (false),
m_DefaultSubscription (nullptr), m_SubscriptionsUpdateTimer (nullptr)
{
}
2014-11-26 21:19:36 +00:00
AddressBook::~AddressBook ()
{
2015-03-30 14:21:52 +00:00
Stop ();
}
void AddressBook::Start ()
{
if (!m_Storage)
m_Storage = new AddressBookFilesystemStorage;
m_Storage->Init();
LoadHosts (); /* try storage, then hosts.txt, then download */
2015-03-30 14:21:52 +00:00
StartSubscriptions ();
2016-03-26 14:31:47 +00:00
StartLookups ();
2015-03-30 14:21:52 +00:00
}
2016-03-26 14:31:47 +00:00
void AddressBook::StartResolvers ()
{
LoadLocal ();
}
2015-03-30 14:21:52 +00:00
void AddressBook::Stop ()
{
2016-03-26 14:31:47 +00:00
StopLookups ();
2015-03-30 14:21:52 +00:00
StopSubscriptions ();
if (m_SubscriptionsUpdateTimer)
{
delete m_SubscriptionsUpdateTimer;
m_SubscriptionsUpdateTimer = nullptr;
}
2014-12-21 14:33:02 +00:00
if (m_IsDownloading)
{
2016-03-11 00:46:52 +00:00
LogPrint (eLogInfo, "Addressbook: subscriptions is downloading, abort");
2014-12-21 14:33:02 +00:00
for (int i = 0; i < 30; i++)
{
if (!m_IsDownloading)
{
2016-03-11 00:46:52 +00:00
LogPrint (eLogInfo, "Addressbook: subscriptions download complete");
2014-12-21 14:33:02 +00:00
break;
}
std::this_thread::sleep_for (std::chrono::seconds (1)); // wait for 1 seconds
}
2016-03-11 00:46:52 +00:00
LogPrint (eLogError, "Addressbook: subscription download timeout");
2015-03-30 14:21:52 +00:00
m_IsDownloading = false;
2016-08-05 18:23:54 +00:00
}
2014-11-28 20:08:23 +00:00
if (m_Storage)
{
m_Storage->Save (m_Addresses);
delete m_Storage;
2015-03-30 14:21:52 +00:00
m_Storage = nullptr;
2014-11-28 20:08:23 +00:00
}
m_DefaultSubscription = nullptr;
2015-03-30 14:21:52 +00:00
m_Subscriptions.clear ();
}
2014-10-24 19:22:36 +00:00
bool AddressBook::GetIdentHash (const std::string& address, i2p::data::IdentHash& ident)
2014-10-03 18:22:32 +00:00
{
auto pos = address.find(".b32.i2p");
if (pos != std::string::npos)
{
Base32ToByteStream (address.c_str(), pos, ident, 32);
return true;
}
else
{
pos = address.find (".i2p");
if (pos != std::string::npos)
{
auto identHash = FindAddress (address);
if (identHash)
{
ident = *identHash;
return true;
}
else
2016-03-26 19:02:27 +00:00
{
LookupAddress (address); // TODO:
return false;
2016-03-26 19:02:27 +00:00
}
2014-10-03 18:22:32 +00:00
}
}
// if not .b32 we assume full base64 address
i2p::data::IdentityEx dest;
2014-12-05 00:28:20 +00:00
if (!dest.FromBase64 (address))
return false;
ident = dest.GetIdentHash ();
return true;
2014-10-03 18:22:32 +00:00
}
2014-10-24 19:22:36 +00:00
const i2p::data::IdentHash * AddressBook::FindAddress (const std::string& address)
{
auto it = m_Addresses.find (address);
if (it != m_Addresses.end ())
return &it->second;
return nullptr;
}
2014-04-01 19:18:14 +00:00
2014-09-23 19:38:56 +00:00
void AddressBook::InsertAddress (const std::string& address, const std::string& base64)
{
2015-11-03 14:15:49 +00:00
auto ident = std::make_shared<i2p::data::IdentityEx>();
ident->FromBase64 (base64);
2014-11-26 21:51:36 +00:00
m_Storage->AddAddress (ident);
2015-11-03 14:15:49 +00:00
m_Addresses[address] = ident->GetIdentHash ();
2016-01-12 23:32:32 +00:00
LogPrint (eLogInfo, "Addressbook: added ", address," -> ", ToAddress(ident->GetIdentHash ()));
2014-09-23 19:38:56 +00:00
}
2015-11-03 14:15:49 +00:00
void AddressBook::InsertAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
2014-11-26 21:51:36 +00:00
{
m_Storage->AddAddress (address);
}
2015-11-03 14:15:49 +00:00
std::shared_ptr<const i2p::data::IdentityEx> AddressBook::GetAddress (const std::string& address)
2014-11-26 21:19:36 +00:00
{
2014-11-28 20:08:23 +00:00
i2p::data::IdentHash ident;
2015-11-03 14:15:49 +00:00
if (!GetIdentHash (address, ident)) return nullptr;
return m_Storage->GetAddress (ident);
2014-11-26 21:19:36 +00:00
}
void AddressBook::LoadHosts ()
2014-04-01 19:18:14 +00:00
{
2014-12-20 03:03:34 +00:00
if (m_Storage->Load (m_Addresses) > 0)
2014-11-28 14:40:27 +00:00
{
m_IsLoaded = true;
return;
}
2016-02-11 00:00:00 +00:00
// then try hosts.txt
std::ifstream f (i2p::fs::DataDirPath("hosts.txt"), std::ifstream::in); // in text mode
2014-12-21 14:33:02 +00:00
if (f.is_open ())
{
2016-07-16 00:00:00 +00:00
LoadHostsFromStream (f, false);
2014-12-21 14:33:02 +00:00
m_IsLoaded = true;
}
2014-12-20 03:03:34 +00:00
}
2016-07-16 00:00:00 +00:00
bool AddressBook::LoadHostsFromStream (std::istream& f, bool is_update)
2014-12-20 03:03:34 +00:00
{
2014-12-21 14:33:02 +00:00
std::unique_lock<std::mutex> l(m_AddressBookMutex);
2014-12-20 03:03:34 +00:00
int numAddresses = 0;
2016-04-01 16:51:34 +00:00
bool incomplete = false;
std::string s;
while (!f.eof ())
2014-04-01 19:18:14 +00:00
{
getline(f, s);
2014-04-01 20:27:40 +00:00
if (!s.length())
continue; // skip empty line
size_t pos = s.find('=');
if (pos != std::string::npos)
{
std::string name = s.substr(0, pos++);
std::string addr = s.substr(pos);
2015-11-03 14:15:49 +00:00
auto ident = std::make_shared<i2p::data::IdentityEx> ();
2016-07-16 00:00:00 +00:00
if (!ident->FromBase64(addr)) {
2016-03-11 00:46:52 +00:00
LogPrint (eLogError, "Addressbook: malformed address ", addr, " for ", name);
2016-04-01 16:51:34 +00:00
incomplete = f.eof ();
2016-07-16 00:00:00 +00:00
continue;
2016-04-01 16:51:34 +00:00
}
2016-07-16 00:00:00 +00:00
numAddresses++;
if (m_Addresses.count(name) > 0)
continue; /* already exists */
m_Addresses[name] = ident->GetIdentHash ();
m_Storage->AddAddress (ident);
if (is_update)
LogPrint(eLogInfo, "Addressbook: added new host: ", name);
2016-04-01 16:51:34 +00:00
}
else
incomplete = f.eof ();
}
2016-03-11 00:46:52 +00:00
LogPrint (eLogInfo, "Addressbook: ", numAddresses, " addresses processed");
2014-12-21 14:33:02 +00:00
if (numAddresses > 0)
{
2016-04-01 16:51:34 +00:00
if (!incomplete) m_IsLoaded = true;
2014-12-21 14:33:02 +00:00
m_Storage->Save (m_Addresses);
}
2016-04-01 16:51:34 +00:00
return !incomplete;
2014-12-20 03:03:34 +00:00
}
2014-12-22 20:06:54 +00:00
void AddressBook::LoadSubscriptions ()
{
if (!m_Subscriptions.size ())
{
2016-02-11 00:00:00 +00:00
std::ifstream f (i2p::fs::DataDirPath ("subscriptions.txt"), std::ifstream::in); // in text mode
2014-12-22 20:06:54 +00:00
if (f.is_open ())
{
std::string s;
while (!f.eof ())
{
getline(f, s);
if (!s.length()) continue; // skip empty line
m_Subscriptions.push_back (std::make_shared<AddressBookSubscription> (*this, s));
2014-12-22 20:06:54 +00:00
}
2016-01-18 00:00:00 +00:00
LogPrint (eLogInfo, "Addressbook: ", m_Subscriptions.size (), " subscriptions urls loaded");
2014-12-22 20:06:54 +00:00
}
else
2016-03-11 00:46:52 +00:00
LogPrint (eLogWarning, "Addressbook: subscriptions.txt not found in datadir");
2014-12-22 20:06:54 +00:00
}
else
2016-01-12 23:32:32 +00:00
LogPrint (eLogError, "Addressbook: subscriptions already loaded");
2014-12-22 20:06:54 +00:00
}
2016-03-24 18:48:07 +00:00
void AddressBook::LoadLocal ()
{
std::map<std::string, i2p::data::IdentHash> localAddresses;
m_Storage->LoadLocal (localAddresses);
2016-08-05 18:23:54 +00:00
for (const auto& it: localAddresses)
2016-03-24 18:48:07 +00:00
{
auto dot = it.first.find ('.');
if (dot != std::string::npos)
{
auto domain = it.first.substr (dot + 1);
auto it1 = m_Addresses.find (domain); // find domain in our addressbook
if (it1 != m_Addresses.end ())
{
auto dest = context.FindLocalDestination (it1->second);
if (dest)
{
// address is ours
std::shared_ptr<AddressResolver> resolver;
auto it2 = m_Resolvers.find (it1->second);
if (it2 != m_Resolvers.end ())
resolver = it2->second; // resolver exists
else
{
// create new resolver
resolver = std::make_shared<AddressResolver>(dest);
m_Resolvers.insert (std::make_pair(it1->second, resolver));
}
resolver->AddAddress (it.first, it.second);
}
}
}
}
}
2016-03-15 18:37:07 +00:00
bool AddressBook::GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified)
{
if (m_Storage)
return m_Storage->GetEtag (subscription, etag, lastModified);
else
return false;
}
void AddressBook::DownloadComplete (bool success, const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified)
{
m_IsDownloading = false;
int nextUpdateTimeout = CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT;
if (success)
{
if (m_DefaultSubscription) m_DefaultSubscription = nullptr;
if (m_IsLoaded)
nextUpdateTimeout = CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT;
else
m_IsLoaded = true;
2016-03-15 02:00:05 +00:00
if (m_Storage) m_Storage->SaveEtag (subscription, etag, lastModified);
}
if (m_SubscriptionsUpdateTimer)
{
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(nextUpdateTimeout));
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
this, std::placeholders::_1));
}
}
void AddressBook::StartSubscriptions ()
{
LoadSubscriptions ();
if (m_IsLoaded && m_Subscriptions.empty ()) return;
auto dest = i2p::client::context.GetSharedLocalDestination ();
if (dest)
{
m_SubscriptionsUpdateTimer = new boost::asio::deadline_timer (dest->GetService ());
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(INITIAL_SUBSCRIPTION_UPDATE_TIMEOUT));
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
this, std::placeholders::_1));
}
else
2016-03-11 00:46:52 +00:00
LogPrint (eLogError, "Addressbook: can't start subscriptions: missing shared local destination");
}
void AddressBook::StopSubscriptions ()
{
if (m_SubscriptionsUpdateTimer)
m_SubscriptionsUpdateTimer->cancel ();
}
void AddressBook::HandleSubscriptionsUpdateTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
auto dest = i2p::client::context.GetSharedLocalDestination ();
2016-02-11 00:00:00 +00:00
if (!dest) {
LogPrint(eLogWarning, "Addressbook: missing local destination, skip subscription update");
return;
}
if (!m_IsDownloading && dest->IsReady ())
{
if (!m_IsLoaded)
{
// download it from http://i2p-projekt.i2p/hosts.txt
LogPrint (eLogInfo, "Addressbook: trying to download it from default subscription.");
if (!m_DefaultSubscription)
m_DefaultSubscription = std::make_shared<AddressBookSubscription>(*this, DEFAULT_SUBSCRIPTION_ADDRESS);
m_IsDownloading = true;
std::thread load_hosts(std::bind (&AddressBookSubscription::CheckUpdates, m_DefaultSubscription));
load_hosts.detach(); // TODO: use join
}
else if (!m_Subscriptions.empty ())
{
// pick random subscription
auto ind = rand () % m_Subscriptions.size();
m_IsDownloading = true;
std::thread load_hosts(std::bind (&AddressBookSubscription::CheckUpdates, m_Subscriptions[ind]));
load_hosts.detach(); // TODO: use join
}
}
else
{
// try it again later
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(INITIAL_SUBSCRIPTION_RETRY_TIMEOUT));
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
this, std::placeholders::_1));
}
}
}
2016-03-26 14:31:47 +00:00
void AddressBook::StartLookups ()
{
auto dest = i2p::client::context.GetSharedLocalDestination ();
if (dest)
{
auto datagram = dest->GetDatagramDestination ();
if (!datagram)
datagram = dest->CreateDatagramDestination ();
datagram->SetReceiver (std::bind (&AddressBook::HandleLookupResponse, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
ADDRESS_RESPONSE_DATAGRAM_PORT);
}
}
void AddressBook::StopLookups ()
{
auto dest = i2p::client::context.GetSharedLocalDestination ();
if (dest)
{
auto datagram = dest->GetDatagramDestination ();
if (datagram)
datagram->ResetReceiver (ADDRESS_RESPONSE_DATAGRAM_PORT);
}
}
void AddressBook::LookupAddress (const std::string& address)
{
const i2p::data::IdentHash * ident = nullptr;
auto dot = address.find ('.');
if (dot != std::string::npos)
ident = FindAddress (address.substr (dot + 1));
if (!ident)
{
2016-07-16 00:00:00 +00:00
LogPrint (eLogError, "Addressbook: Can't find domain for ", address);
2016-03-26 14:31:47 +00:00
return;
}
auto dest = i2p::client::context.GetSharedLocalDestination ();
if (dest)
{
auto datagram = dest->GetDatagramDestination ();
if (datagram)
{
uint32_t nonce;
RAND_bytes ((uint8_t *)&nonce, 4);
{
std::unique_lock<std::mutex> l(m_LookupsMutex);
m_Lookups[nonce] = address;
}
2016-07-16 00:00:00 +00:00
LogPrint (eLogDebug, "Addressbook: Lookup of ", address, " to ", ident->ToBase32 (), " nonce=", nonce);
2016-03-26 14:31:47 +00:00
size_t len = address.length () + 9;
uint8_t * buf = new uint8_t[len];
memset (buf, 0, 4);
htobe32buf (buf + 4, nonce);
buf[8] = address.length ();
memcpy (buf + 9, address.c_str (), address.length ());
datagram->SendDatagramTo (buf, len, *ident, ADDRESS_RESPONSE_DATAGRAM_PORT, ADDRESS_RESOLVER_DATAGRAM_PORT);
delete[] buf;
}
}
}
void AddressBook::HandleLookupResponse (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
{
if (len < 44)
{
2016-07-16 00:00:00 +00:00
LogPrint (eLogError, "Addressbook: Lookup response is too short ", len);
2016-03-26 14:31:47 +00:00
return;
}
uint32_t nonce = bufbe32toh (buf + 4);
2016-07-16 00:00:00 +00:00
LogPrint (eLogDebug, "Addressbook: Lookup response received from ", from.GetIdentHash ().ToBase32 (), " nonce=", nonce);
2016-03-26 14:31:47 +00:00
std::string address;
{
std::unique_lock<std::mutex> l(m_LookupsMutex);
auto it = m_Lookups.find (nonce);
if (it != m_Lookups.end ())
{
address = it->second;
m_Lookups.erase (it);
}
}
if (address.length () > 0)
{
// TODO: verify from
m_Addresses[address] = buf + 8;
}
}
AddressBookSubscription::AddressBookSubscription (AddressBook& book, const std::string& link):
m_Book (book), m_Link (link)
{
}
void AddressBookSubscription::CheckUpdates ()
{
bool result = MakeRequest ();
m_Book.DownloadComplete (result, m_Ident, m_Etag, m_LastModified);
}
bool AddressBookSubscription::MakeRequest ()
{
i2p::http::URL url;
// must be run in separate thread
LogPrint (eLogInfo, "Addressbook: Downloading hosts database from ", m_Link);
if (!url.parse(m_Link)) {
LogPrint(eLogError, "Addressbook: failed to parse url: ", m_Link);
return false;
}
if (!m_Book.GetIdentHash (url.host, m_Ident)) {
LogPrint (eLogError, "Addressbook: Can't resolve ", url.host);
return false;
}
/* this code block still needs some love */
std::condition_variable newDataReceived;
std::mutex newDataReceivedMutex;
auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (m_Ident);
if (!leaseSet)
{
std::unique_lock<std::mutex> l(newDataReceivedMutex);
2016-08-09 02:15:09 +00:00
i2p::client::context.GetSharedLocalDestination ()->RequestDestination (m_Ident,
[&newDataReceived, &leaseSet, &newDataReceivedMutex](std::shared_ptr<i2p::data::LeaseSet> ls)
{
leaseSet = ls;
2016-08-09 02:15:09 +00:00
std::unique_lock<std::mutex> l1(newDataReceivedMutex);
newDataReceived.notify_all ();
2016-08-09 02:15:09 +00:00
});
if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout)
{
2016-08-09 02:15:09 +00:00
LogPrint (eLogError, "Addressbook: Subscription LeaseSet request timeout expired");
i2p::client::context.GetSharedLocalDestination ()->CancelDestinationRequest (m_Ident, false); // don't notify, because we know it already
return false;
}
}
if (!leaseSet) {
/* still no leaseset found */
LogPrint (eLogError, "Addressbook: LeaseSet for address ", url.host, " not found");
return false;
}
if (m_Etag.empty() && m_LastModified.empty()) {
m_Book.GetEtag (m_Ident, m_Etag, m_LastModified);
LogPrint (eLogDebug, "Addressbook: loaded for ", url.host, ": ETag: ", m_Etag, ", Last-Modified: ", m_LastModified);
}
/* save url parts for later use */
std::string dest_host = url.host;
int dest_port = url.port ? url.port : 80;
/* create http request & send it */
i2p::http::HTTPReq req;
req.add_header("Host", dest_host);
req.add_header("User-Agent", "Wget/1.11.4");
req.add_header("Connection", "close");
if (!m_Etag.empty())
req.add_header("If-None-Match", m_Etag);
if (!m_LastModified.empty())
req.add_header("If-Modified-Since", m_LastModified);
/* convert url to relative */
url.schema = "";
url.host = "";
req.uri = url.to_string();
auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, dest_port);
std::string request = req.to_string();
stream->Send ((const uint8_t *) request.data(), request.length());
/* read response */
std::string response;
uint8_t recv_buf[4096];
bool end = false;
while (!end) {
stream->AsyncReceive (boost::asio::buffer (recv_buf, 4096),
[&](const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (bytes_transferred)
response.append ((char *)recv_buf, bytes_transferred);
if (ecode == boost::asio::error::timed_out || !stream->IsOpen ())
end = true;
newDataReceived.notify_all ();
},
30); // wait for 30 seconds
std::unique_lock<std::mutex> l(newDataReceivedMutex);
if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout)
LogPrint (eLogError, "Addressbook: subscriptions request timeout expired");
}
// process remaining buffer
while (size_t len = stream->ReadSome (recv_buf, sizeof(recv_buf))) {
response.append ((char *)recv_buf, len);
}
/* parse response */
i2p::http::HTTPRes res;
int res_head_len = res.parse(response);
if (res_head_len < 0) {
LogPrint(eLogError, "Addressbook: can't parse http response from ", dest_host);
return false;
}
if (res_head_len == 0) {
LogPrint(eLogError, "Addressbook: incomplete http response from ", dest_host, ", interrupted by timeout");
return false;
}
/* assert: res_head_len > 0 */
response.erase(0, res_head_len);
if (res.code == 304) {
LogPrint (eLogInfo, "Addressbook: no updates from ", dest_host, ", code 304");
return false;
}
if (res.code != 200) {
LogPrint (eLogWarning, "Adressbook: can't get updates from ", dest_host, ", response code ", res.code);
return false;
}
int len = res.content_length();
if (response.empty()) {
LogPrint(eLogError, "Addressbook: empty response from ", dest_host, ", expected ", len, " bytes");
return false;
}
if (len > 0 && len != (int) response.length()) {
LogPrint(eLogError, "Addressbook: response size mismatch, expected: ", response.length(), ", got: ", len, "bytes");
return false;
}
/* assert: res.code == 200 */
auto it = res.headers.find("ETag");
if (it != res.headers.end()) {
m_Etag = it->second;
}
it = res.headers.find("If-Modified-Since");
if (it != res.headers.end()) {
m_LastModified = it->second;
}
if (res.is_chunked()) {
std::stringstream in(response), out;
i2p::http::MergeChunkedResponse (in, out);
response = out.str();
} else if (res.is_gzipped()) {
std::stringstream out;
2016-02-18 18:19:31 +00:00
i2p::data::GzipInflator inflator;
inflator.Inflate ((const uint8_t *) response.data(), response.length(), out);
if (out.fail()) {
LogPrint(eLogError, "Addressbook: can't gunzip http response");
2016-02-18 18:19:31 +00:00
return false;
}
response = out.str();
}
std::stringstream ss(response);
LogPrint (eLogInfo, "Addressbook: got update from ", dest_host);
2016-07-16 00:00:00 +00:00
m_Book.LoadHostsFromStream (ss, true);
return true;
2016-02-18 18:19:31 +00:00
}
2016-03-24 18:48:07 +00:00
AddressResolver::AddressResolver (std::shared_ptr<ClientDestination> destination):
m_LocalDestination (destination)
{
if (m_LocalDestination)
{
auto datagram = m_LocalDestination->GetDatagramDestination ();
if (!datagram)
datagram = m_LocalDestination->CreateDatagramDestination ();
datagram->SetReceiver (std::bind (&AddressResolver::HandleRequest, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
ADDRESS_RESOLVER_DATAGRAM_PORT);
}
}
2016-03-26 14:31:47 +00:00
AddressResolver::~AddressResolver ()
{
if (m_LocalDestination)
{
auto datagram = m_LocalDestination->GetDatagramDestination ();
if (datagram)
datagram->ResetReceiver (ADDRESS_RESOLVER_DATAGRAM_PORT);
}
}
2016-03-24 18:48:07 +00:00
void AddressResolver::HandleRequest (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
{
if (len < 9 || len < buf[8] + 9U)
{
2016-07-16 00:00:00 +00:00
LogPrint (eLogError, "Addressbook: Address request is too short ", len);
2016-03-24 18:48:07 +00:00
return;
}
// read requested address
uint8_t l = buf[8];
char address[255];
memcpy (address, buf + 9, l);
2016-03-26 14:31:47 +00:00
address[l] = 0;
2016-07-16 00:00:00 +00:00
LogPrint (eLogDebug, "Addressbook: Address request ", address);
2016-03-24 18:48:07 +00:00
// send response
2016-03-26 14:31:47 +00:00
uint8_t response[44];
2016-03-24 18:48:07 +00:00
memset (response, 0, 4); // reserved
memcpy (response + 4, buf + 4, 4); // nonce
auto it = m_LocalAddresses.find (address); // address lookup
if (it != m_LocalAddresses.end ())
memcpy (response + 8, it->second, 32); // ident
else
memset (response + 8, 0, 32); // not found
2016-03-26 14:31:47 +00:00
memset (response + 40, 0, 4); // set expiration time to zero
m_LocalDestination->GetDatagramDestination ()->SendDatagramTo (response, 44, from.GetIdentHash (), toPort, fromPort);
2016-03-24 18:48:07 +00:00
}
void AddressResolver::AddAddress (const std::string& name, const i2p::data::IdentHash& ident)
{
m_LocalAddresses[name] = ident;
}
2014-04-01 19:18:14 +00:00
}
}