Browse Source

replaced GetIdentHash by GetAddress

pull/1334/head
orignal 6 years ago
parent
commit
f5ab8f2062
  1. 62
      libi2pd_client/AddressBook.cpp
  2. 1
      libi2pd_client/AddressBook.h
  3. 41
      libi2pd_client/BOB.cpp
  4. 3
      libi2pd_client/HTTPProxy.cpp
  5. 6
      libi2pd_client/I2CP.cpp
  6. 12
      libi2pd_client/I2PTunnel.cpp
  7. 6
      libi2pd_client/MatchedDestination.cpp
  8. 10
      libi2pd_client/SAM.cpp

62
libi2pd_client/AddressBook.cpp

@ -315,63 +315,24 @@ namespace client
m_Subscriptions.clear (); m_Subscriptions.clear ();
} }
bool AddressBook::GetIdentHash (const std::string& address, i2p::data::IdentHash& ident) std::shared_ptr<const Address> AddressBook::GetAddress (const std::string& address)
{ {
auto pos = address.find(".b32.i2p"); auto pos = address.find(".b32.i2p");
if (pos != std::string::npos) if (pos != std::string::npos)
{ return std::make_shared<const Address>(address.substr (0, pos));
if (pos <= B33_ADDRESS_THRESHOLD)
{
Base32ToByteStream (address.c_str(), pos, ident, 32);
return true;
}
else
return false;
}
else else
{ {
pos = address.find (".i2p"); pos = address.find (".i2p");
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
auto addr = FindAddress (address); auto addr = FindAddress (address);
if (addr) if (!addr)
{
if (addr->IsIdentHash ())
{
ident = addr->identHash;
return true;
}
else
return false;
}
else
{
LookupAddress (address); // TODO: LookupAddress (address); // TODO:
return false; return addr;
}
} }
} }
// if not .b32 we assume full base64 address // if not .b32 we assume full base64 address
i2p::data::IdentityEx dest; i2p::data::IdentityEx dest;
if (!dest.FromBase64 (address))
return false;
ident = dest.GetIdentHash ();
return true;
}
std::shared_ptr<const Address> AddressBook::GetAddress (const std::string& address)
{
auto pos = address.find(".b32.i2p");
if (pos != std::string::npos)
return std::make_shared<const Address>(address.substr (0, pos));
else
{
pos = address.find (".i2p");
if (pos != std::string::npos)
return FindAddress (address);
}
// if not .b32 we assume full base64 address
i2p::data::IdentityEx dest;
if (!dest.FromBase64 (address)) if (!dest.FromBase64 (address))
return nullptr; return nullptr;
return std::make_shared<const Address>(dest.GetIdentHash ()); return std::make_shared<const Address>(dest.GetIdentHash ());
@ -401,9 +362,9 @@ namespace client
std::shared_ptr<const i2p::data::IdentityEx> AddressBook::GetFullAddress (const std::string& address) std::shared_ptr<const i2p::data::IdentityEx> AddressBook::GetFullAddress (const std::string& address)
{ {
i2p::data::IdentHash ident; auto addr = GetAddress (address);
if (!GetIdentHash (address, ident)) return nullptr; if (!addr || !addr->IsIdentHash ()) return nullptr;
return m_Storage->GetAddress (ident); return m_Storage->GetAddress (addr->identHash);
} }
void AddressBook::LoadHosts () void AddressBook::LoadHosts ()
@ -761,14 +722,19 @@ namespace client
i2p::http::URL url; i2p::http::URL url;
// must be run in separate thread // must be run in separate thread
LogPrint (eLogInfo, "Addressbook: Downloading hosts database from ", m_Link); LogPrint (eLogInfo, "Addressbook: Downloading hosts database from ", m_Link);
if (!url.parse(m_Link)) { if (!url.parse(m_Link))
{
LogPrint(eLogError, "Addressbook: failed to parse url: ", m_Link); LogPrint(eLogError, "Addressbook: failed to parse url: ", m_Link);
return false; return false;
} }
if (!m_Book.GetIdentHash (url.host, m_Ident)) { auto addr = m_Book.GetAddress (url.host);
if (!addr || !addr->IsIdentHash ())
{
LogPrint (eLogError, "Addressbook: Can't resolve ", url.host); LogPrint (eLogError, "Addressbook: Can't resolve ", url.host);
return false; return false;
} }
else
m_Ident = addr->identHash;
/* this code block still needs some love */ /* this code block still needs some love */
std::condition_variable newDataReceived; std::condition_variable newDataReceived;
std::mutex newDataReceivedMutex; std::mutex newDataReceivedMutex;

1
libi2pd_client/AddressBook.h

@ -74,7 +74,6 @@ namespace client
void Start (); void Start ();
void StartResolvers (); void StartResolvers ();
void Stop (); void Stop ();
bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident);
std::shared_ptr<const Address> GetAddress (const std::string& address); std::shared_ptr<const Address> GetAddress (const std::string& address);
std::shared_ptr<const i2p::data::IdentityEx> GetFullAddress (const std::string& address); std::shared_ptr<const i2p::data::IdentityEx> GetFullAddress (const std::string& address);
std::shared_ptr<const Address> FindAddress (const std::string& address); std::shared_ptr<const Address> FindAddress (const std::string& address);

41
libi2pd_client/BOB.cpp

@ -72,17 +72,24 @@ namespace client
if (eol != receiver->buffer && eol[-1] == '\r') eol[-1] = 0; // workaround for Transmission, it sends '\r\n' terminated address if (eol != receiver->buffer && eol[-1] == '\r') eol[-1] = 0; // workaround for Transmission, it sends '\r\n' terminated address
receiver->data = (uint8_t *)eol + 1; receiver->data = (uint8_t *)eol + 1;
receiver->dataLen = receiver->bufferOffset - (eol - receiver->buffer + 1); receiver->dataLen = receiver->bufferOffset - (eol - receiver->buffer + 1);
i2p::data::IdentHash ident; auto addr = context.GetAddressBook ().GetAddress (receiver->buffer);
if (!context.GetAddressBook ().GetIdentHash (receiver->buffer, ident)) if (!addr)
{ {
LogPrint (eLogError, "BOB: address ", receiver->buffer, " not found"); LogPrint (eLogError, "BOB: address ", receiver->buffer, " not found");
return; return;
} }
auto leaseSet = GetLocalDestination ()->FindLeaseSet (ident); if (addr->IsIdentHash ())
{
auto leaseSet = GetLocalDestination ()->FindLeaseSet (addr->identHash);
if (leaseSet) if (leaseSet)
CreateConnection (receiver, leaseSet); CreateConnection (receiver, leaseSet);
else else
GetLocalDestination ()->RequestDestination (ident, GetLocalDestination ()->RequestDestination (addr->identHash,
std::bind (&BOBI2PInboundTunnel::HandleDestinationRequestComplete,
this, std::placeholders::_1, receiver));
}
else
GetLocalDestination ()->RequestDestinationWithEncryptedLeaseSet (addr->blindedPublicKey,
std::bind (&BOBI2PInboundTunnel::HandleDestinationRequestComplete, std::bind (&BOBI2PInboundTunnel::HandleDestinationRequestComplete,
this, std::placeholders::_1, receiver)); this, std::placeholders::_1, receiver));
} }
@ -540,29 +547,37 @@ namespace client
void BOBCommandSession::LookupCommandHandler (const char * operand, size_t len) void BOBCommandSession::LookupCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: lookup ", operand); LogPrint (eLogDebug, "BOB: lookup ", operand);
i2p::data::IdentHash ident; auto addr = context.GetAddressBook ().GetAddress (operand);
if (!context.GetAddressBook ().GetIdentHash (operand, ident)) if (!addr)
{ {
SendReplyError ("Address Not found"); SendReplyError ("Address Not found");
return; return;
} }
auto localDestination = m_CurrentDestination ? m_CurrentDestination->GetLocalDestination () : i2p::client::context.GetSharedLocalDestination (); auto localDestination = m_CurrentDestination ? m_CurrentDestination->GetLocalDestination () : i2p::client::context.GetSharedLocalDestination ();
auto leaseSet = localDestination->FindLeaseSet (ident); if (addr->IsIdentHash ())
{
// we might have leaseset already
auto leaseSet = localDestination->FindLeaseSet (addr->identHash);
if (leaseSet) if (leaseSet)
SendReplyOK (leaseSet->GetIdentity ()->ToBase64 ().c_str ());
else
{ {
SendReplyOK (leaseSet->GetIdentity ()->ToBase64 ().c_str ());
return;
}
}
// trying to request
auto s = shared_from_this (); auto s = shared_from_this ();
localDestination->RequestDestination (ident, auto requstCallback =
[s](std::shared_ptr<i2p::data::LeaseSet> ls) [s](std::shared_ptr<i2p::data::LeaseSet> ls)
{ {
if (ls) if (ls)
s->SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ()); s->SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ());
else else
s->SendReplyError ("LeaseSet Not found"); s->SendReplyError ("LeaseSet Not found");
} };
); if (addr->IsIdentHash ())
} localDestination->RequestDestination (addr->identHash, requstCallback);
else
localDestination->RequestDestinationWithEncryptedLeaseSet (addr->blindedPublicKey, requstCallback);
} }
void BOBCommandSession::ClearCommandHandler (const char * operand, size_t len) void BOBCommandSession::ClearCommandHandler (const char * operand, size_t len)

3
libi2pd_client/HTTPProxy.cpp

@ -339,9 +339,8 @@ namespace proxy {
} }
} }
/* check dest_host really exists and inside I2P network */ /* check dest_host really exists and inside I2P network */
i2p::data::IdentHash identHash;
if (str_rmatch(dest_host, ".i2p")) { if (str_rmatch(dest_host, ".i2p")) {
if (!i2p::client::context.GetAddressBook ().GetIdentHash (dest_host, identHash)) { if (!i2p::client::context.GetAddressBook ().GetAddress (dest_host)) {
HostNotFound(dest_host); HostNotFound(dest_host);
return true; /* request processed */ return true; /* request processed */
} }

6
libi2pd_client/I2CP.cpp

@ -603,12 +603,16 @@ namespace client
case 1: // address case 1: // address
{ {
auto name = ExtractString (buf + 11, len - 11); auto name = ExtractString (buf + 11, len - 11);
if (!i2p::client::context.GetAddressBook ().GetIdentHash (name, ident)) auto addr = i2p::client::context.GetAddressBook ().GetAddress (name);
if (!addr || !addr->IsIdentHash ())
{ {
// TODO: handle blinded addresses
LogPrint (eLogError, "I2CP: address ", name, " not found"); LogPrint (eLogError, "I2CP: address ", name, " not found");
SendHostReplyMessage (requestID, nullptr); SendHostReplyMessage (requestID, nullptr);
return; return;
} }
else
ident = addr->identHash;
break; break;
} }
default: default:

12
libi2pd_client/I2PTunnel.cpp

@ -810,9 +810,9 @@ namespace client
void I2PUDPClientTunnel::TryResolving() { void I2PUDPClientTunnel::TryResolving() {
LogPrint(eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest); LogPrint(eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest);
i2p::data::IdentHash * h = new i2p::data::IdentHash;
while(!context.GetAddressBook().GetIdentHash(m_RemoteDest, *h) && !m_cancel_resolve) std::shared_ptr<const Address> addr;
while(!(addr = context.GetAddressBook().GetAddress(m_RemoteDest)) && !m_cancel_resolve)
{ {
LogPrint(eLogWarning, "UDP Tunnel: failed to lookup ", m_RemoteDest); LogPrint(eLogWarning, "UDP Tunnel: failed to lookup ", m_RemoteDest);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
@ -822,7 +822,13 @@ namespace client
LogPrint(eLogError, "UDP Tunnel: lookup of ", m_RemoteDest, " was cancelled"); LogPrint(eLogError, "UDP Tunnel: lookup of ", m_RemoteDest, " was cancelled");
return; return;
} }
m_RemoteIdent = h; if (!addr || !addr->IsIdentHash ())
{
LogPrint(eLogError, "UDP Tunnel: ", m_RemoteDest, " not found");
return;
}
m_RemoteIdent = new i2p::data::IdentHash;
*m_RemoteIdent = addr->identHash;
LogPrint(eLogInfo, "UDP Tunnel: resolved ", m_RemoteDest, " to ", m_RemoteIdent->ToBase32()); LogPrint(eLogInfo, "UDP Tunnel: resolved ", m_RemoteDest, " to ", m_RemoteIdent->ToBase32());
} }

6
libi2pd_client/MatchedDestination.cpp

@ -14,13 +14,13 @@ namespace client
void MatchedTunnelDestination::ResolveCurrentLeaseSet() void MatchedTunnelDestination::ResolveCurrentLeaseSet()
{ {
if(i2p::client::context.GetAddressBook().GetIdentHash(m_RemoteName, m_RemoteIdent)) auto addr = i2p::client::context.GetAddressBook().GetAddress (m_RemoteName);
if(addr && addr->IsIdentHash ())
{ {
m_RemoteIdent = addr->identHash;
auto ls = FindLeaseSet(m_RemoteIdent); auto ls = FindLeaseSet(m_RemoteIdent);
if(ls) if(ls)
{
HandleFoundCurrentLeaseSet(ls); HandleFoundCurrentLeaseSet(ls);
}
else else
RequestDestination(m_RemoteIdent, std::bind(&MatchedTunnelDestination::HandleFoundCurrentLeaseSet, this, std::placeholders::_1)); RequestDestination(m_RemoteIdent, std::bind(&MatchedTunnelDestination::HandleFoundCurrentLeaseSet, this, std::placeholders::_1));
} }

10
libi2pd_client/SAM.cpp

@ -610,22 +610,22 @@ namespace client
ExtractParams (buf, params); ExtractParams (buf, params);
std::string& name = params[SAM_PARAM_NAME]; std::string& name = params[SAM_PARAM_NAME];
std::shared_ptr<const i2p::data::IdentityEx> identity; std::shared_ptr<const i2p::data::IdentityEx> identity;
i2p::data::IdentHash ident; std::shared_ptr<const Address> addr;
auto session = m_Owner.FindSession(m_ID); auto session = m_Owner.FindSession(m_ID);
auto dest = session == nullptr ? context.GetSharedLocalDestination() : session->localDestination; auto dest = session == nullptr ? context.GetSharedLocalDestination() : session->localDestination;
if (name == "ME") if (name == "ME")
SendNamingLookupReply (dest->GetIdentity ()); SendNamingLookupReply (dest->GetIdentity ());
else if ((identity = context.GetAddressBook ().GetFullAddress (name)) != nullptr) else if ((identity = context.GetAddressBook ().GetFullAddress (name)) != nullptr)
SendNamingLookupReply (identity); SendNamingLookupReply (identity);
else if (context.GetAddressBook ().GetIdentHash (name, ident)) else if ((addr = context.GetAddressBook ().GetAddress (name)) && addr->IsIdentHash ())
{ {
auto leaseSet = dest->FindLeaseSet (ident); auto leaseSet = dest->FindLeaseSet (addr->identHash);
if (leaseSet) if (leaseSet)
SendNamingLookupReply (leaseSet->GetIdentity ()); SendNamingLookupReply (leaseSet->GetIdentity ());
else else
dest->RequestDestination (ident, dest->RequestDestination (addr->identHash,
std::bind (&SAMSocket::HandleNamingLookupLeaseSetRequestComplete, std::bind (&SAMSocket::HandleNamingLookupLeaseSetRequestComplete,
shared_from_this (), std::placeholders::_1, ident)); shared_from_this (), std::placeholders::_1, addr->identHash));
} }
else else
{ {

Loading…
Cancel
Save