Browse Source

find NTCP2 address by static key. Don't make router unreachable if can't connect by NTCP2

pull/1638/head
orignal 3 years ago
parent
commit
1ca0354cf2
  1. 24
      libi2pd/NTCP2.cpp
  2. 7
      libi2pd/RouterInfo.cpp
  3. 2
      libi2pd/RouterInfo.h

24
libi2pd/NTCP2.cpp

@ -333,8 +333,6 @@ namespace transport @@ -333,8 +333,6 @@ namespace transport
if (in_RemoteRouter) // Alice
{
m_Establisher->m_RemoteIdentHash = GetRemoteIdentity ()->GetIdentHash ();
if (!addr)
addr = in_RemoteRouter->GetNTCP2Address (true); // we need a published address
if (addr)
{
memcpy (m_Establisher->m_RemoteStaticKey, addr->ntcp2->staticKey, 32);
@ -342,7 +340,7 @@ namespace transport @@ -342,7 +340,7 @@ namespace transport
m_RemoteEndpoint = boost::asio::ip::tcp::endpoint (addr->host, addr->port);
}
else
LogPrint (eLogWarning, "NTCP2: Missing NTCP2 parameters");
LogPrint (eLogWarning, "NTCP2: Missing NTCP2 address");
}
m_NextRouterInfoResendTime = i2p::util::GetSecondsSinceEpoch () + NTCP2_ROUTERINFO_RESEND_INTERVAL +
rand ()%NTCP2_ROUTERINFO_RESEND_INTERVAL_THRESHOLD;
@ -658,19 +656,13 @@ namespace transport @@ -658,19 +656,13 @@ namespace transport
SendTerminationAndTerminate (eNTCP2Message3Error);
return;
}
auto addr = ri.GetNTCP2Address (false); // any NTCP2 address
auto addr = ri.GetNTCP2AddressWithStaticKey (m_Establisher->m_RemoteStaticKey);
if (!addr)
{
LogPrint (eLogError, "NTCP2: No NTCP2 address found in SessionConfirmed");
LogPrint (eLogError, "NTCP2: No NTCP2 address wth static key found in SessionConfirmed");
Terminate ();
return;
}
if (memcmp (addr->ntcp2->staticKey, m_Establisher->m_RemoteStaticKey, 32))
{
LogPrint (eLogError, "NTCP2: Static key mismatch in SessionConfirmed");
SendTerminationAndTerminate (eNTCP2IncorrectSParameter);
return;
}
i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, buf.data () + 3, size)); // TODO: should insert ri and not parse it twice
// TODO: process options
@ -1296,8 +1288,6 @@ namespace transport @@ -1296,8 +1288,6 @@ namespace transport
if (ecode != boost::asio::error::operation_aborted)
{
LogPrint (eLogInfo, "NTCP2: Not connected in ", timeout, " seconds");
if (conn->GetRemoteIdentity ())
i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true);
conn->Terminate ();
}
});
@ -1426,6 +1416,11 @@ namespace transport @@ -1426,6 +1416,11 @@ namespace transport
void NTCP2Server::ConnectWithProxy (std::shared_ptr<NTCP2Session> conn)
{
if(!m_ProxyEndpoint) return;
if (!conn || conn->GetRemoteEndpoint ().address ().is_unspecified ())
{
LogPrint (eLogError, "NTCP2: Can't connect to unspecified address");
return;
}
GetService().post([this, conn]()
{
if (this->AddNTCP2Session (conn))
@ -1439,7 +1434,6 @@ namespace transport @@ -1439,7 +1434,6 @@ namespace transport
if (ecode != boost::asio::error::operation_aborted)
{
LogPrint (eLogInfo, "NTCP2: Not connected in ", timeout, " seconds");
i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true);
conn->Terminate ();
}
});
@ -1633,8 +1627,6 @@ namespace transport @@ -1633,8 +1627,6 @@ namespace transport
return;
}
}
if(!e)
i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true);
timer->cancel();
conn->Terminate();
});

7
libi2pd/RouterInfo.cpp

@ -962,12 +962,13 @@ namespace data @@ -962,12 +962,13 @@ namespace data
return nullptr;
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetNTCP2Address (bool publishedOnly) const
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetNTCP2AddressWithStaticKey (const uint8_t * key) const
{
if (!key) return nullptr;
return GetAddress (
[publishedOnly](std::shared_ptr<const RouterInfo::Address> address)->bool
[key](std::shared_ptr<const RouterInfo::Address> address)->bool
{
return address->IsNTCP2 () && (!publishedOnly || address->IsPublishedNTCP2 ());
return address->IsNTCP2 () && !memcmp (address->ntcp2->staticKey, key, 32);
});
}

2
libi2pd/RouterInfo.h

@ -150,7 +150,7 @@ namespace data @@ -150,7 +150,7 @@ namespace data
uint64_t GetTimestamp () const { return m_Timestamp; };
int GetVersion () const { return m_Version; };
Addresses& GetAddresses () { return *m_Addresses; }; // should be called for local RI only, otherwise must return shared_ptr
std::shared_ptr<const Address> GetNTCP2Address (bool publishedOnly) const;
std::shared_ptr<const Address> GetNTCP2AddressWithStaticKey (const uint8_t * key) const;
std::shared_ptr<const Address> GetPublishedNTCP2V4Address () const;
std::shared_ptr<const Address> GetPublishedNTCP2V6Address () const;
std::shared_ptr<const Address> GetSSUAddress (bool v4only = true) const;

Loading…
Cancel
Save