From f5ab8f2062276565b6ad7996080aa32451cf431a Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 28 Mar 2019 12:19:19 -0400 Subject: [PATCH] replaced GetIdentHash by GetAddress --- libi2pd_client/AddressBook.cpp | 66 +++++++-------------------- libi2pd_client/AddressBook.h | 1 - libi2pd_client/BOB.cpp | 57 ++++++++++++++--------- libi2pd_client/HTTPProxy.cpp | 3 +- libi2pd_client/I2CP.cpp | 6 ++- libi2pd_client/I2PTunnel.cpp | 12 +++-- libi2pd_client/MatchedDestination.cpp | 6 +-- libi2pd_client/SAM.cpp | 10 ++-- 8 files changed, 75 insertions(+), 86 deletions(-) diff --git a/libi2pd_client/AddressBook.cpp b/libi2pd_client/AddressBook.cpp index bb38f7f9..9af66786 100644 --- a/libi2pd_client/AddressBook.cpp +++ b/libi2pd_client/AddressBook.cpp @@ -315,50 +315,6 @@ namespace client m_Subscriptions.clear (); } - bool AddressBook::GetIdentHash (const std::string& address, i2p::data::IdentHash& ident) - { - auto pos = address.find(".b32.i2p"); - if (pos != std::string::npos) - { - if (pos <= B33_ADDRESS_THRESHOLD) - { - Base32ToByteStream (address.c_str(), pos, ident, 32); - return true; - } - else - return false; - } - else - { - pos = address.find (".i2p"); - if (pos != std::string::npos) - { - auto addr = FindAddress (address); - if (addr) - { - if (addr->IsIdentHash ()) - { - ident = addr->identHash; - return true; - } - else - return false; - } - else - { - LookupAddress (address); // TODO: - return false; - } - } - } - // if not .b32 we assume full base64 address - i2p::data::IdentityEx dest; - if (!dest.FromBase64 (address)) - return false; - ident = dest.GetIdentHash (); - return true; - } - std::shared_ptr AddressBook::GetAddress (const std::string& address) { auto pos = address.find(".b32.i2p"); @@ -368,7 +324,12 @@ namespace client { pos = address.find (".i2p"); if (pos != std::string::npos) - return FindAddress (address); + { + auto addr = FindAddress (address); + if (!addr) + LookupAddress (address); // TODO: + return addr; + } } // if not .b32 we assume full base64 address i2p::data::IdentityEx dest; @@ -401,9 +362,9 @@ namespace client std::shared_ptr AddressBook::GetFullAddress (const std::string& address) { - i2p::data::IdentHash ident; - if (!GetIdentHash (address, ident)) return nullptr; - return m_Storage->GetAddress (ident); + auto addr = GetAddress (address); + if (!addr || !addr->IsIdentHash ()) return nullptr; + return m_Storage->GetAddress (addr->identHash); } void AddressBook::LoadHosts () @@ -761,14 +722,19 @@ namespace client i2p::http::URL url; // must be run in separate thread 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); 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); return false; } + else + m_Ident = addr->identHash; /* this code block still needs some love */ std::condition_variable newDataReceived; std::mutex newDataReceivedMutex; diff --git a/libi2pd_client/AddressBook.h b/libi2pd_client/AddressBook.h index ed8e37ae..b12c6c74 100644 --- a/libi2pd_client/AddressBook.h +++ b/libi2pd_client/AddressBook.h @@ -74,7 +74,6 @@ namespace client void Start (); void StartResolvers (); void Stop (); - bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident); std::shared_ptr GetAddress (const std::string& address); std::shared_ptr GetFullAddress (const std::string& address); std::shared_ptr FindAddress (const std::string& address); diff --git a/libi2pd_client/BOB.cpp b/libi2pd_client/BOB.cpp index 2a18c8ba..94d74726 100644 --- a/libi2pd_client/BOB.cpp +++ b/libi2pd_client/BOB.cpp @@ -72,19 +72,26 @@ namespace client 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->dataLen = receiver->bufferOffset - (eol - receiver->buffer + 1); - i2p::data::IdentHash ident; - if (!context.GetAddressBook ().GetIdentHash (receiver->buffer, ident)) + auto addr = context.GetAddressBook ().GetAddress (receiver->buffer); + if (!addr) { LogPrint (eLogError, "BOB: address ", receiver->buffer, " not found"); return; } - auto leaseSet = GetLocalDestination ()->FindLeaseSet (ident); - if (leaseSet) - CreateConnection (receiver, leaseSet); + if (addr->IsIdentHash ()) + { + auto leaseSet = GetLocalDestination ()->FindLeaseSet (addr->identHash); + if (leaseSet) + CreateConnection (receiver, leaseSet); + else + GetLocalDestination ()->RequestDestination (addr->identHash, + std::bind (&BOBI2PInboundTunnel::HandleDestinationRequestComplete, + this, std::placeholders::_1, receiver)); + } else - GetLocalDestination ()->RequestDestination (ident, - std::bind (&BOBI2PInboundTunnel::HandleDestinationRequestComplete, - this, std::placeholders::_1, receiver)); + GetLocalDestination ()->RequestDestinationWithEncryptedLeaseSet (addr->blindedPublicKey, + std::bind (&BOBI2PInboundTunnel::HandleDestinationRequestComplete, + this, std::placeholders::_1, receiver)); } else { @@ -540,29 +547,37 @@ namespace client void BOBCommandSession::LookupCommandHandler (const char * operand, size_t len) { LogPrint (eLogDebug, "BOB: lookup ", operand); - i2p::data::IdentHash ident; - if (!context.GetAddressBook ().GetIdentHash (operand, ident)) + auto addr = context.GetAddressBook ().GetAddress (operand); + if (!addr) { SendReplyError ("Address Not found"); return; } auto localDestination = m_CurrentDestination ? m_CurrentDestination->GetLocalDestination () : i2p::client::context.GetSharedLocalDestination (); - auto leaseSet = localDestination->FindLeaseSet (ident); - if (leaseSet) - SendReplyOK (leaseSet->GetIdentity ()->ToBase64 ().c_str ()); - else - { - auto s = shared_from_this (); - localDestination->RequestDestination (ident, - [s](std::shared_ptr ls) + if (addr->IsIdentHash ()) + { + // we might have leaseset already + auto leaseSet = localDestination->FindLeaseSet (addr->identHash); + if (leaseSet) + { + SendReplyOK (leaseSet->GetIdentity ()->ToBase64 ().c_str ()); + return; + } + } + // trying to request + auto s = shared_from_this (); + auto requstCallback = + [s](std::shared_ptr ls) { if (ls) s->SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ()); else 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) diff --git a/libi2pd_client/HTTPProxy.cpp b/libi2pd_client/HTTPProxy.cpp index 5fbefece..ee1b241d 100644 --- a/libi2pd_client/HTTPProxy.cpp +++ b/libi2pd_client/HTTPProxy.cpp @@ -339,9 +339,8 @@ namespace proxy { } } /* check dest_host really exists and inside I2P network */ - i2p::data::IdentHash identHash; 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); return true; /* request processed */ } diff --git a/libi2pd_client/I2CP.cpp b/libi2pd_client/I2CP.cpp index e507b066..d429461b 100644 --- a/libi2pd_client/I2CP.cpp +++ b/libi2pd_client/I2CP.cpp @@ -603,12 +603,16 @@ namespace client case 1: // address { 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"); SendHostReplyMessage (requestID, nullptr); return; } + else + ident = addr->identHash; break; } default: diff --git a/libi2pd_client/I2PTunnel.cpp b/libi2pd_client/I2PTunnel.cpp index e45c7288..290ce11e 100644 --- a/libi2pd_client/I2PTunnel.cpp +++ b/libi2pd_client/I2PTunnel.cpp @@ -810,9 +810,9 @@ namespace client void I2PUDPClientTunnel::TryResolving() { 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 addr; + while(!(addr = context.GetAddressBook().GetAddress(m_RemoteDest)) && !m_cancel_resolve) { LogPrint(eLogWarning, "UDP Tunnel: failed to lookup ", m_RemoteDest); 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"); 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()); } diff --git a/libi2pd_client/MatchedDestination.cpp b/libi2pd_client/MatchedDestination.cpp index 68f3759e..fa08ec51 100644 --- a/libi2pd_client/MatchedDestination.cpp +++ b/libi2pd_client/MatchedDestination.cpp @@ -14,13 +14,13 @@ namespace client 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); if(ls) - { HandleFoundCurrentLeaseSet(ls); - } else RequestDestination(m_RemoteIdent, std::bind(&MatchedTunnelDestination::HandleFoundCurrentLeaseSet, this, std::placeholders::_1)); } diff --git a/libi2pd_client/SAM.cpp b/libi2pd_client/SAM.cpp index 340ed0ba..224112ef 100644 --- a/libi2pd_client/SAM.cpp +++ b/libi2pd_client/SAM.cpp @@ -610,22 +610,22 @@ namespace client ExtractParams (buf, params); std::string& name = params[SAM_PARAM_NAME]; std::shared_ptr identity; - i2p::data::IdentHash ident; + std::shared_ptr addr; auto session = m_Owner.FindSession(m_ID); auto dest = session == nullptr ? context.GetSharedLocalDestination() : session->localDestination; if (name == "ME") SendNamingLookupReply (dest->GetIdentity ()); else if ((identity = context.GetAddressBook ().GetFullAddress (name)) != nullptr) 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) SendNamingLookupReply (leaseSet->GetIdentity ()); else - dest->RequestDestination (ident, + dest->RequestDestination (addr->identHash, std::bind (&SAMSocket::HandleNamingLookupLeaseSetRequestComplete, - shared_from_this (), std::placeholders::_1, ident)); + shared_from_this (), std::placeholders::_1, addr->identHash)); } else {