mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 04:04:16 +00:00
address resolver for NTCP address
This commit is contained in:
parent
0f227e8317
commit
f3fbf6bd89
@ -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 ()) ||
|
||||||
|
@ -96,7 +96,7 @@ namespace transport
|
|||||||
Transports transports;
|
Transports transports;
|
||||||
|
|
||||||
Transports::Transports ():
|
Transports::Transports ():
|
||||||
m_IsRunning (false), m_Thread (nullptr), m_Work (m_Service),
|
m_IsRunning (false), m_Thread (nullptr), m_Work (m_Service),
|
||||||
m_NTCPServer (nullptr), m_SSUServer (nullptr),
|
m_NTCPServer (nullptr), m_SSUServer (nullptr),
|
||||||
m_DHKeysPairSupplier (5) // 5 pre-generated keys
|
m_DHKeysPairSupplier (5) // 5 pre-generated keys
|
||||||
{
|
{
|
||||||
@ -218,12 +218,27 @@ namespace transport
|
|||||||
if (!peer.numAttempts) // NTCP
|
if (!peer.numAttempts) // NTCP
|
||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
auto s = std::make_shared<NTCPSession> (*m_NTCPServer, peer.router);
|
if (!address->host.is_unspecified ()) // we have address now
|
||||||
m_NTCPServer->Connect (address->host, address->port, s);
|
{
|
||||||
return true;
|
if (!peer.router->UsesIntroducer () && !peer.router->IsUnreachable ())
|
||||||
|
{
|
||||||
|
auto s = std::make_shared<NTCPSession> (*m_NTCPServer, peer.router);
|
||||||
|
m_NTCPServer->Connect (address->host, address->port, s);
|
||||||
|
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
|
||||||
@ -271,7 +286,40 @@ 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;
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user