From 07654212f8712b8959f4de9bce506963f9533f0c Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 3 Oct 2014 14:22:32 -0400 Subject: [PATCH] parse address in AddressBook --- AddressBook.cpp | 23 +++++++++++++++++++++++ AddressBook.h | 1 + HTTPProxy.cpp | 2 +- HTTPServer.cpp | 32 +++++--------------------------- I2PTunnel.cpp | 26 ++++++-------------------- NetDb.h | 3 +-- 6 files changed, 37 insertions(+), 50 deletions(-) diff --git a/AddressBook.cpp b/AddressBook.cpp index b08f946a..2d0d2624 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -18,6 +18,29 @@ namespace data { } + bool AddressBook::GetIdentHash (const std::string& address, IdentHash& ident) + { + 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; + } + } + } + return false; + } const IdentHash * AddressBook::FindAddress (const std::string& address) { diff --git a/AddressBook.h b/AddressBook.h index 22875d04..db9d2319 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -18,6 +18,7 @@ namespace data public: AddressBook (); + bool GetIdentHash (const std::string& address, IdentHash& ident); const IdentHash * FindAddress (const std::string& address); void InsertAddress (const std::string& address, const std::string& base64); // for jump service diff --git a/HTTPProxy.cpp b/HTTPProxy.cpp index d7a77a4d..7ad64cfa 100644 --- a/HTTPProxy.cpp +++ b/HTTPProxy.cpp @@ -73,7 +73,7 @@ namespace proxy { LogPrint ("Jump service for ", r.host, " found. Inserting to address book"); auto base64 = r.uri.substr (addressPos + 1); - i2p::data::netdb.InsertAddress (r.host, base64); + i2p::data::netdb.GetAddressBook ().InsertAddress (r.host, base64); } } diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 361d0a20..b89946e6 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -829,34 +829,12 @@ namespace util void HTTPConnection::SendToAddress (const std::string& address, const char * buf, size_t len) { i2p::data::IdentHash destination; - if (address.find(".b32.i2p") != std::string::npos) + if (!i2p::data::netdb.GetAddressBook ().GetIdentHash (address, destination)) { - if (i2p::data::Base32ToByteStream(address.c_str(), address.length() - strlen(".b32.i2p"), (uint8_t *)destination, 32) != 32) - { - LogPrint ("Invalid Base32 address ", address); - SendReply ("" + itoopieImage + "
Invalid Base32 address", 400); - return; - } - } - else - { - if (address.find(".i2p") != std::string::npos) - { - auto addr = i2p::data::netdb.FindAddress(address); - if (!addr) - { - LogPrint ("Unknown address ", address); - SendReply ("" + itoopieImage + "
Unknown address " + address + "", 404); - return; - } - destination = *addr; - } - else - { - SendReply ("Unexpected address " + address + "", 404); - return; - } - } + LogPrint ("Unknown address ", address); + SendReply ("" + itoopieImage + "
Unknown address " + address + "", 404); + return; + } SendToDestination (destination, buf, len); } diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 3093e1d2..f583b7de 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -153,23 +153,9 @@ namespace stream void I2PClientTunnel::Start () { - auto pos = m_Destination.find(".b32.i2p"); - if (pos != std::string::npos) - { - uint8_t hash[32]; - i2p::data::Base32ToByteStream (m_Destination.c_str(), pos, hash, 32); - m_DestinationIdentHash = new i2p::data::IdentHash (hash); - } - else - { - pos = m_Destination.find (".i2p"); - if (pos != std::string::npos) - { - auto identHash = i2p::data::netdb.FindAddress (m_Destination); - if (identHash) - m_DestinationIdentHash = new i2p::data::IdentHash (*identHash); - } - } + i2p::data::IdentHash identHash; + if (i2p::data::netdb.GetAddressBook ().GetIdentHash (m_Destination, identHash)) + m_DestinationIdentHash = new i2p::data::IdentHash (identHash); if (m_DestinationIdentHash) { i2p::data::netdb.Subscribe (*m_DestinationIdentHash, GetLocalDestination ()->GetTunnelPool ()); @@ -206,10 +192,10 @@ namespace stream m_RemoteLeaseSet = i2p::data::netdb.FindLeaseSet (*m_DestinationIdentHash); else { - auto identHash = i2p::data::netdb.FindAddress (m_Destination); - if (identHash) + i2p::data::IdentHash identHash; + if (i2p::data::netdb.GetAddressBook ().GetIdentHash (m_Destination, identHash)) { - m_DestinationIdentHash = new i2p::data::IdentHash (*identHash); + m_DestinationIdentHash = new i2p::data::IdentHash (identHash); i2p::data::netdb.Subscribe (*m_DestinationIdentHash, GetSharedLocalDestination ()->GetTunnelPool ()); } } diff --git a/NetDb.h b/NetDb.h index 35447a70..31bd05fa 100644 --- a/NetDb.h +++ b/NetDb.h @@ -66,8 +66,7 @@ namespace data void AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len); RouterInfo * FindRouter (const IdentHash& ident) const; LeaseSet * FindLeaseSet (const IdentHash& destination) const; - const IdentHash * FindAddress (const std::string& address) { return m_AddressBook.FindAddress (address); }; // TODO: move AddressBook away from NetDb - void InsertAddress (const std::string& address, const std::string& base64) { m_AddressBook.InsertAddress (address, base64); }; + AddressBook& GetAddressBook () { return m_AddressBook; };// TODO: move AddressBook away from NetDb void Subscribe (const IdentHash& ident, i2p::tunnel::TunnelPool * pool = nullptr); // keep LeaseSets upto date void Unsubscribe (const IdentHash& ident);