Browse Source

connect to SSU2 address

pull/1742/head
orignal 3 years ago
parent
commit
3c5c375f71
  1. 50
      libi2pd/RouterInfo.cpp
  2. 16
      libi2pd/RouterInfo.h
  3. 22
      libi2pd/SSU2.cpp
  4. 6
      libi2pd/SSU2.h
  5. 34
      libi2pd/Transports.cpp

50
libi2pd/RouterInfo.cpp

@ -386,7 +386,13 @@ namespace data @@ -386,7 +386,13 @@ namespace data
}
else if (address->transportStyle == eTransportSSU2)
{
// TODO:
if (address->IsV4 ()) supportedTransports |= eSSU2V4;
if (address->IsV6 ()) supportedTransports |= eSSU2V6;
if (address->port)
{
if (address->host.is_v4 ()) m_ReachableTransports |= eSSU2V4;
if (address->host.is_v6 ()) m_ReachableTransports |= eSSU2V6;
}
}
if (supportedTransports)
{
@ -693,11 +699,6 @@ namespace data @@ -693,11 +699,6 @@ namespace data
return m_SupportedTransports & (eSSUV4 | eSSUV6);
}
bool RouterInfo::IsSSUV6 () const
{
return m_SupportedTransports & eSSUV6;
}
bool RouterInfo::IsNTCP2 (bool v4only) const
{
if (v4only)
@ -706,25 +707,6 @@ namespace data @@ -706,25 +707,6 @@ namespace data
return m_SupportedTransports & (eNTCP2V4 | eNTCP2V6);
}
bool RouterInfo::IsNTCP2V6 () const
{
return m_SupportedTransports & eNTCP2V6;
}
bool RouterInfo::IsV6 () const
{
return m_SupportedTransports & (eSSUV6 | eNTCP2V6);
}
bool RouterInfo::IsV4 () const
{
return m_SupportedTransports & (eSSUV4 | eNTCP2V4);
}
bool RouterInfo::IsMesh () const
{
return m_SupportedTransports & eNTCP2V6Mesh;
}
void RouterInfo::EnableV6 ()
{
@ -841,6 +823,24 @@ namespace data @@ -841,6 +823,24 @@ namespace data
});
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSU2V4Address () const
{
return GetAddress (
[](std::shared_ptr<const RouterInfo::Address> address)->bool
{
return (address->transportStyle == eTransportSSU2) && address->IsV4();
});
}
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSU2V6Address () const
{
return GetAddress (
[](std::shared_ptr<const RouterInfo::Address> address)->bool
{
return (address->transportStyle == eTransportSSU2) && address->IsV6();
});
}
template<typename Filter>
std::shared_ptr<const RouterInfo::Address> RouterInfo::GetAddress (Filter filter) const
{

16
libi2pd/RouterInfo.h

@ -67,6 +67,8 @@ namespace data @@ -67,6 +67,8 @@ namespace data
eSSUV4 = 0x04,
eSSUV6 = 0x08,
eNTCP2V6Mesh = 0x10,
eSSU2V4 = 0x20,
eSSU2V6 = 0x40,
eAllTransports = 0xFF
};
typedef uint8_t CompatibleTransports;
@ -186,6 +188,8 @@ namespace data @@ -186,6 +188,8 @@ namespace data
std::shared_ptr<const Address> GetSSUAddress (bool v4only = true) const;
std::shared_ptr<const Address> GetSSUV6Address () const;
std::shared_ptr<const Address> GetYggdrasilAddress () const;
std::shared_ptr<const Address> GetSSU2V4Address () const;
std::shared_ptr<const Address> GetSSU2V6Address () const;
void AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu = 0);
void AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv,
@ -199,12 +203,14 @@ namespace data @@ -199,12 +203,14 @@ namespace data
bool IsReachable () const { return m_Caps & Caps::eReachable; };
bool IsECIES () const { return m_RouterIdentity->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD; };
bool IsSSU (bool v4only = true) const;
bool IsSSUV6 () const;
bool IsSSUV6 () const { return m_SupportedTransports & eSSUV6; };
bool IsNTCP2 (bool v4only = true) const;
bool IsNTCP2V6 () const;
bool IsV6 () const;
bool IsV4 () const;
bool IsMesh () const;
bool IsNTCP2V6 () const { return m_SupportedTransports & eNTCP2V6; };
bool IsSSU2V4 () const { return m_SupportedTransports & eSSU2V4; };
bool IsSSU2V6 () const { return m_SupportedTransports & eSSU2V6; };
bool IsV6 () const { return m_SupportedTransports & (eSSUV6 | eNTCP2V6 | eSSU2V6); };
bool IsV4 () const { return m_SupportedTransports & (eSSUV4 | eNTCP2V4 | eSSU2V4); };
bool IsMesh () const { return m_SupportedTransports & eNTCP2V6Mesh; };
void EnableV6 ();
void DisableV6 ();
void EnableV4 ();

22
libi2pd/SSU2.cpp

@ -48,6 +48,11 @@ namespace transport @@ -48,6 +48,11 @@ namespace transport
{
}
void SSU2Session::Connect ()
{
SendSessionRequest ();
}
void SSU2Session::SendSessionRequest ()
{
// we are Alice
@ -488,6 +493,21 @@ namespace transport @@ -488,6 +493,21 @@ namespace transport
boost::system::error_code ec;
m_Socket.send_to (bufs, to, 0, ec);
}
bool SSU2Server::CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
std::shared_ptr<const i2p::data::RouterInfo::Address> address)
{
if (router && address)
GetService ().post (
[this, router, address]()
{
auto session = std::make_shared<SSU2Session> (*this, router, address);
session->Connect ();
});
else
return false;
return true;
}
}
}

6
libi2pd/SSU2.h

@ -81,7 +81,8 @@ namespace transport @@ -81,7 +81,8 @@ namespace transport
~SSU2Session ();
void SetRemoteEndpoint (const boost::asio::ip::udp::endpoint& ep) { m_RemoteEndpoint = ep; };
void Connect ();
void Done () override {};
void SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs) override {};
@ -130,6 +131,9 @@ namespace transport @@ -130,6 +131,9 @@ namespace transport
void Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to);
bool CreateSession (std::shared_ptr<const i2p::data::RouterInfo> router,
std::shared_ptr<const i2p::data::RouterInfo::Address> address);
private:

34
libi2pd/Transports.cpp

@ -536,6 +536,40 @@ namespace transport @@ -536,6 +536,40 @@ namespace transport
}
}
}
if (peer.numAttempts == 5 || peer.numAttempts == 6) // SSU2
{
if (m_SSU2Server)
{
std::shared_ptr<const RouterInfo::Address> address;
if (peer.numAttempts == 5) // SSU2 ipv6
{
if (context.GetRouterInfo ().IsSSU2V6 () && peer.router->IsReachableBy (RouterInfo::eSSU2V6))
{
address = peer.router->GetSSU2V6Address ();
if (address && m_CheckReserved && i2p::util::net::IsInReservedRange(address->host))
address = nullptr;
}
peer.numAttempts++;
}
if (!address && peer.numAttempts == 6) // SSU2 ipv4
{
if (context.GetRouterInfo ().IsSSU2V4 () && peer.router->IsReachableBy (RouterInfo::eSSU2V4))
{
address = peer.router->GetSSU2V4Address ();
if (address && m_CheckReserved && i2p::util::net::IsInReservedRange(address->host))
address = nullptr;
}
peer.numAttempts++;
}
if (address && address->published)
{
if (m_SSU2Server->CreateSession (peer.router, address))
return true;
}
}
else
peer.numAttempts += 2;
}
LogPrint (eLogInfo, "Transports: No compatble NTCP2 or SSU addresses available");
i2p::data::netdb.SetUnreachable (ident, true); // we are here because all connection attempts failed
peer.Done ();

Loading…
Cancel
Save