Browse Source

address resolver for NTCP address

pull/150/head
orignal 10 years ago
parent
commit
f3fbf6bd89
  1. 4
      RouterInfo.h
  2. 50
      Transports.cpp
  3. 4
      Transports.h

4
RouterInfo.h

@ -69,6 +69,7 @@ namespace data
{ {
TransportStyle transportStyle; TransportStyle transportStyle;
boost::asio::ip::address host; boost::asio::ip::address host;
char * addressString;
int port, mtu; int port, mtu;
uint64_t date; uint64_t date;
uint8_t cost; uint8_t cost;
@ -76,6 +77,9 @@ namespace data
Tag<32> key; // intro key for SSU Tag<32> key; // intro key for SSU
std::vector<Introducer> introducers; std::vector<Introducer> introducers;
Address (): addressString (nullptr) {};
~Address () { if (addressString) delete[] addressString; };
bool IsCompatible (const boost::asio::ip::address& other) const bool IsCompatible (const boost::asio::ip::address& other) const
{ {
return (host.is_v4 () && other.is_v4 ()) || return (host.is_v4 () && other.is_v4 ()) ||

50
Transports.cpp

@ -219,13 +219,28 @@ namespace transport
{ {
peer.numAttempts++; peer.numAttempts++;
auto address = peer.router->GetNTCPAddress (!context.SupportsV6 ()); auto address = peer.router->GetNTCPAddress (!context.SupportsV6 ());
if (address && !peer.router->UsesIntroducer () && !peer.router->IsUnreachable ()) if (address)
{
if (!address->host.is_unspecified ()) // we have address now
{
if (!peer.router->UsesIntroducer () && !peer.router->IsUnreachable ())
{ {
auto s = std::make_shared<NTCPSession> (*m_NTCPServer, peer.router); auto s = std::make_shared<NTCPSession> (*m_NTCPServer, peer.router);
m_NTCPServer->Connect (address->host, address->port, s); m_NTCPServer->Connect (address->host, address->port, s);
return true; return true;
} }
} }
else // we don't have address
{
if (address->addressString) // trying to resolve
{
LogPrint (eLogInfo, "Resolving ", address->addressString);
NTCPResolve (address->addressString, ident);
return true;
}
}
}
}
else if (peer.numAttempts == 1)// SSU else if (peer.numAttempts == 1)// SSU
{ {
peer.numAttempts++; peer.numAttempts++;
@ -272,6 +287,39 @@ namespace transport
} }
} }
void Transports::NTCPResolve (const char * addr, const i2p::data::IdentHash& ident)
{
auto resolver = std::make_shared<boost::asio::ip::tcp::resolver>(m_Service);
resolver->async_resolve (boost::asio::ip::tcp::resolver::query (addr),
std::bind (&Transports::HandleNTCPResolve, this,
std::placeholders::_1, std::placeholders::_2, ident, resolver));
}
void Transports::HandleNTCPResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it,
const i2p::data::IdentHash& ident, std::shared_ptr<boost::asio::ip::tcp::resolver> resolver)
{
auto it1 = m_Peers.find (ident);
if (it1 != m_Peers.end () && it1->second.router)
{
auto& peer = it1->second;
if (!ecode)
{
auto address = (*it).endpoint ().address ();
LogPrint (eLogInfo, (*it).host_name (), " has been resolved to ", address);
auto addr = peer.router->GetNTCPAddress ();
if (addr)
{
auto s = std::make_shared<NTCPSession> (*m_NTCPServer, peer.router);
m_NTCPServer->Connect (address, addr->port, s);
return;
}
}
}
LogPrint (eLogError, "Unable to resolve NTCP address: ", ecode.message ());
m_Peers.erase (it1);
}
void Transports::CloseSession (std::shared_ptr<const i2p::data::RouterInfo> router) void Transports::CloseSession (std::shared_ptr<const i2p::data::RouterInfo> router)
{ {
if (!router) return; if (!router) return;

4
Transports.h

@ -94,6 +94,10 @@ namespace transport
void PostCloseSession (std::shared_ptr<const i2p::data::RouterInfo> router); void PostCloseSession (std::shared_ptr<const i2p::data::RouterInfo> router);
bool ConnectToPeer (const i2p::data::IdentHash& ident, Peer& peer); bool ConnectToPeer (const i2p::data::IdentHash& ident, Peer& peer);
void NTCPResolve (const char * addr, const i2p::data::IdentHash& ident);
void HandleNTCPResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it,
const i2p::data::IdentHash& ident, std::shared_ptr<boost::asio::ip::tcp::resolver> resolver);
void DetectExternalIP (); void DetectExternalIP ();
private: private:

Loading…
Cancel
Save