naming lookup through addressbook

This commit is contained in:
orignal 2014-11-26 16:51:36 -05:00
parent fae01f61d2
commit 68c321609d
4 changed files with 35 additions and 5 deletions

View File

@ -98,6 +98,11 @@ namespace client
delete m_Storage; delete m_Storage;
} }
AddressBookStorage * AddressBook::CreateStorage ()
{
return new AddressBookFilesystemStorage ();
}
bool AddressBook::GetIdentHash (const std::string& address, i2p::data::IdentHash& ident) bool AddressBook::GetIdentHash (const std::string& address, i2p::data::IdentHash& ident)
{ {
auto pos = address.find(".b32.i2p"); auto pos = address.find(".b32.i2p");
@ -139,15 +144,24 @@ namespace client
{ {
i2p::data::IdentityEx ident; i2p::data::IdentityEx ident;
ident.FromBase64 (base64); ident.FromBase64 (base64);
if (m_Storage) m_Storage->AddAddress (ident); if (!m_Storage)
m_Storage = CreateStorage ();
m_Storage->AddAddress (ident);
m_Addresses[address] = ident.GetIdentHash (); m_Addresses[address] = ident.GetIdentHash ();
LogPrint (address,"->",ident.GetIdentHash ().ToBase32 (), ".b32.i2p added"); LogPrint (address,"->",ident.GetIdentHash ().ToBase32 (), ".b32.i2p added");
} }
void AddressBook::InsertAddress (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) bool AddressBook::GetAddress (const std::string& address, i2p::data::IdentityEx& identity)
{ {
if (!m_Storage) if (!m_Storage)
m_Storage = new AddressBookFilesystemStorage (); m_Storage = CreateStorage ();
auto ident = FindAddress (address); auto ident = FindAddress (address);
if (!ident) return false; if (!ident) return false;
return m_Storage->GetAddress (*ident, identity); return m_Storage->GetAddress (*ident, identity);

View File

@ -33,10 +33,14 @@ namespace client
bool GetAddress (const std::string& address, i2p::data::IdentityEx& identity); bool GetAddress (const std::string& address, i2p::data::IdentityEx& identity);
const i2p::data::IdentHash * FindAddress (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 std::string& address, const std::string& base64); // for jump service
void InsertAddress (const i2p::data::IdentityEx& address);
private: private:
AddressBookStorage * CreateStorage ();
private:
void LoadHosts (); void LoadHosts ();
void LoadHostsFromI2P (); void LoadHostsFromI2P ();

13
SAM.cpp
View File

@ -410,8 +410,11 @@ namespace client
ExtractParams (buf, len, params); ExtractParams (buf, len, params);
std::string& name = params[SAM_PARAM_NAME]; std::string& name = params[SAM_PARAM_NAME];
i2p::data::IdentHash ident; i2p::data::IdentHash ident;
i2p::data::IdentityEx identity;
if (name == "ME") if (name == "ME")
SendNamingLookupReply (nullptr); SendNamingLookupReply (nullptr);
else if (context.GetAddressBook ().GetAddress (name, identity))
SendNamingLookupReply (identity);
else if (m_Session && context.GetAddressBook ().GetIdentHash (name, ident)) else if (m_Session && context.GetAddressBook ().GetIdentHash (name, ident))
{ {
auto leaseSet = m_Session->localDestination->FindLeaseSet (ident); auto leaseSet = m_Session->localDestination->FindLeaseSet (ident);
@ -437,10 +440,18 @@ namespace client
} }
void SAMSocket::SendNamingLookupReply (const i2p::data::LeaseSet * leaseSet) void SAMSocket::SendNamingLookupReply (const i2p::data::LeaseSet * leaseSet)
{
const i2p::data::IdentityEx& identity = leaseSet ? leaseSet->GetIdentity () : m_Session->localDestination->GetIdentity ();
if (leaseSet)
// we found LeaseSet for our address, store it to addressbook
context.GetAddressBook ().InsertAddress (identity);
SendNamingLookupReply (identity);
}
void SAMSocket::SendNamingLookupReply (const i2p::data::IdentityEx& identity)
{ {
uint8_t buf[1024]; uint8_t buf[1024];
char pub[1024]; char pub[1024];
const i2p::data::IdentityEx& identity = leaseSet ? leaseSet->GetIdentity () : m_Session->localDestination->GetIdentity ();
size_t l = identity.ToBuffer (buf, 1024); size_t l = identity.ToBuffer (buf, 1024);
size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, pub, 1024); size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, pub, 1024);
pub[l1] = 0; pub[l1] = 0;

1
SAM.h
View File

@ -105,6 +105,7 @@ namespace client
void HandleStreamDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident); void HandleStreamDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident);
void HandleNamingLookupDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident); void HandleNamingLookupDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident);
void SendNamingLookupReply (const i2p::data::LeaseSet * leaseSet); void SendNamingLookupReply (const i2p::data::LeaseSet * leaseSet);
void SendNamingLookupReply (const i2p::data::IdentityEx& identity);
void HandleSessionReadinessCheckTimer (const boost::system::error_code& ecode); void HandleSessionReadinessCheckTimer (const boost::system::error_code& ecode);
void SendSessionCreateReplyOk (); void SendSessionCreateReplyOk ();