Browse Source

moved peer test 5-7 to SSU2PeerTestSession

pull/2101/head
orignal 2 months ago
parent
commit
2c594dc67a
  1. 82
      libi2pd/SSU2Session.cpp
  2. 16
      libi2pd/SSU2Session.h

82
libi2pd/SSU2Session.cpp

@ -2165,6 +2165,7 @@ namespace transport
void SSU2Session::HandlePeerTest (const uint8_t * buf, size_t len) void SSU2Session::HandlePeerTest (const uint8_t * buf, size_t len)
{ {
// msgs 1-4
if (len < 3) return; if (len < 3) return;
uint8_t msg = buf[0]; uint8_t msg = buf[0];
size_t offset = 3; // points to signed data size_t offset = 3; // points to signed data
@ -2400,35 +2401,6 @@ namespace transport
LogPrint (eLogWarning, "SSU2: Unknown peer test 4 nonce ", nonce); LogPrint (eLogWarning, "SSU2: Unknown peer test 4 nonce ", nonce);
break; break;
} }
case 5: // Alice from Charlie 1
if (htobe64 (((uint64_t)nonce << 32) | nonce) == m_SourceConnID)
{
bool isConnectedRecently = m_Server.IsConnectedRecently (m_RemoteEndpoint);
if (m_Address)
{
if (!isConnectedRecently)
SetRouterStatus (eRouterStatusOK);
SendPeerTest (6, buf + offset, len - offset, m_Address->i);
}
else
// we received msg 5 before msg 4
m_State = isConnectedRecently ? eSSU2SessionStateVoidPeerTestReceived : eSSU2SessionStatePeerTestReceived;
}
else
LogPrint (eLogWarning, "SSU2: Peer test 5 nonce mismatch ", nonce, " connID=", m_SourceConnID);
break;
case 6: // Charlie from Alice
if (m_Address)
SendPeerTest (7, buf + offset, len - offset, m_Address->i);
else
LogPrint (eLogWarning, "SSU2: Unknown address for peer test 6");
m_Server.RemoveSession (~htobe64 (((uint64_t)nonce << 32) | nonce));
break;
case 7: // Alice from Charlie 2
if (m_Address->IsV6 ())
i2p::context.SetStatusV6 (eRouterStatusOK); // set status OK for ipv6 even if from SSU2
m_Server.RemoveSession (htobe64 (((uint64_t)nonce << 32) | nonce));
break;
default: default:
LogPrint (eLogWarning, "SSU2: PeerTest unexpected msg num ", buf[0]); LogPrint (eLogWarning, "SSU2: PeerTest unexpected msg num ", buf[0]);
} }
@ -3165,5 +3137,57 @@ namespace transport
HandlePayload (payload, len - 48); HandlePayload (payload, len - 48);
return true; return true;
} }
void SSU2PeerTestSession::HandlePeerTest (const uint8_t * buf, size_t len)
{
// msgs 5-7
if (len < 8) return;
uint8_t msg = buf[0];
size_t offset = 3; // points to signed data after msg + code + flag
uint32_t nonce = bufbe32toh (buf + offset + 1); // 1 - ver
switch (msg) // msg
{
case 5: // Alice from Charlie 1
{
if (htobe64 (((uint64_t)nonce << 32) | nonce) == GetSourceConnID ())
{
bool isConnectedRecently = GetServer ().IsConnectedRecently (GetRemoteEndpoint ());
auto addr = GetAddress ();
if (addr)
{
if (!isConnectedRecently)
SetRouterStatus (eRouterStatusOK);
SendPeerTest (6, buf + offset, len - offset, addr->i);
}
else
// we received msg 5 before msg 4
SetState (isConnectedRecently ? eSSU2SessionStateVoidPeerTestReceived : eSSU2SessionStatePeerTestReceived);
}
else
LogPrint (eLogWarning, "SSU2: Peer test 5 nonce mismatch ", nonce, " connID=", GetSourceConnID ());
break;
}
case 6: // Charlie from Alice
{
auto addr = GetAddress ();
if (addr)
SendPeerTest (7, buf + offset, len - offset, addr->i);
else
LogPrint (eLogWarning, "SSU2: Unknown address for peer test 6");
GetServer ().RemoveSession (~htobe64 (((uint64_t)nonce << 32) | nonce));
break;
}
case 7: // Alice from Charlie 2
{
auto addr = GetAddress ();
if (addr && addr->IsV6 ())
i2p::context.SetStatusV6 (eRouterStatusOK); // set status OK for ipv6 even if from SSU2
GetServer ().RemoveSession (htobe64 (((uint64_t)nonce << 32) | nonce));
break;
}
default:
LogPrint (eLogWarning, "SSU2: PeerTest unexpected msg num ", msg);
}
}
} }
} }

16
libi2pd/SSU2Session.h

@ -280,10 +280,17 @@ namespace transport
protected: protected:
SSU2Server& GetServer () { return m_Server; }
RouterStatus GetRouterStatus () const;
void SetRouterStatus (RouterStatus status) const;
uint64_t GetSourceConnID () const { return m_SourceConnID; }
void SetSourceConnID (uint64_t sourceConnID) { m_SourceConnID = sourceConnID; } void SetSourceConnID (uint64_t sourceConnID) { m_SourceConnID = sourceConnID; }
uint64_t GetDestConnID () const { return m_DestConnID; }
void SetDestConnID (uint64_t destConnID) { m_DestConnID = destConnID; } void SetDestConnID (uint64_t destConnID) { m_DestConnID = destConnID; }
void HandlePayload (const uint8_t * buf, size_t len); void HandlePayload (const uint8_t * buf, size_t len);
void SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, const uint8_t * introKey); // PeerTest message
private: private:
@ -310,7 +317,6 @@ namespace transport
void SendQuickAck (); void SendQuickAck ();
void SendTermination (); void SendTermination ();
void SendHolePunch (uint32_t nonce, const boost::asio::ip::udp::endpoint& ep, const uint8_t * introKey, uint64_t token); void SendHolePunch (uint32_t nonce, const boost::asio::ip::udp::endpoint& ep, const uint8_t * introKey, uint64_t token);
void SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, const uint8_t * introKey); // PeerTest message
void SendPathResponse (const uint8_t * data, size_t len); void SendPathResponse (const uint8_t * data, size_t len);
void SendPathChallenge (); void SendPathChallenge ();
@ -323,8 +329,6 @@ namespace transport
size_t CreateEndpoint (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& ep); size_t CreateEndpoint (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& ep);
std::shared_ptr<const i2p::data::RouterInfo::Address> FindLocalAddress () const; std::shared_ptr<const i2p::data::RouterInfo::Address> FindLocalAddress () const;
void AdjustMaxPayloadSize (); void AdjustMaxPayloadSize ();
RouterStatus GetRouterStatus () const;
void SetRouterStatus (RouterStatus status) const;
bool GetTestingState () const; bool GetTestingState () const;
void SetTestingState(bool testing) const; void SetTestingState(bool testing) const;
std::shared_ptr<const i2p::data::RouterInfo> ExtractRouterInfo (const uint8_t * buf, size_t size); std::shared_ptr<const i2p::data::RouterInfo> ExtractRouterInfo (const uint8_t * buf, size_t size);
@ -334,7 +338,7 @@ namespace transport
void HandleRelayRequest (const uint8_t * buf, size_t len); void HandleRelayRequest (const uint8_t * buf, size_t len);
void HandleRelayIntro (const uint8_t * buf, size_t len, int attempts = 0); void HandleRelayIntro (const uint8_t * buf, size_t len, int attempts = 0);
void HandleRelayResponse (const uint8_t * buf, size_t len); void HandleRelayResponse (const uint8_t * buf, size_t len);
void HandlePeerTest (const uint8_t * buf, size_t len); virtual void HandlePeerTest (const uint8_t * buf, size_t len);
void HandleI2NPMsg (std::shared_ptr<I2NPMessage>&& msg); void HandleI2NPMsg (std::shared_ptr<I2NPMessage>&& msg);
size_t CreateAddressBlock (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& ep); size_t CreateAddressBlock (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& ep);
@ -395,6 +399,10 @@ namespace transport
std::shared_ptr<SSU2Session> mainSession); std::shared_ptr<SSU2Session> mainSession);
bool ProcessPeerTest (uint8_t * buf, size_t len) override; bool ProcessPeerTest (uint8_t * buf, size_t len) override;
private:
void HandlePeerTest (const uint8_t * buf, size_t len) override;
private: private:

Loading…
Cancel
Save