Skip to content

Commit

Permalink
Send peer test msg 6 with delay if msg 4 was received before msg 5
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Oct 17, 2024
1 parent 0ccf0a6 commit 0213f05
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
32 changes: 14 additions & 18 deletions libi2pd/SSU2OutOfSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace transport
{
if (htobe64 (((uint64_t)nonce << 32) | nonce) == GetSourceConnID ())
{
m_PeerTestResendTimer.cancel (); // calcel delayed msg 6 if any
m_IsConnectedRecently = GetServer ().IsConnectedRecently (GetRemoteEndpoint ());
if (GetAddress ())
{
Expand Down Expand Up @@ -111,9 +112,6 @@ namespace transport
case 7: // Alice from Charlie 2
{
m_PeerTestResendTimer.cancel (); // no more msg 6 resends
auto addr = GetAddress ();
if (addr && addr->IsV6 ())
i2p::context.SetStatusV6 (eRouterStatusOK); // set status OK for ipv6 even if from SSU2
GetServer ().AddConnectedRecently (GetRemoteEndpoint (), i2p::util::GetSecondsSinceEpoch ());
GetServer ().RequestRemoveSession (GetConnID ());
break;
Expand Down Expand Up @@ -163,26 +161,27 @@ namespace transport
GetServer ().Send (header.buf, 16, h + 16, 16, payload, payloadSize, GetRemoteEndpoint ());
}

void SSU2PeerTestSession::SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen)
void SSU2PeerTestSession::SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, bool delayed)
{
#if __cplusplus >= 202002L // C++20
m_SignedData.assign (signedData, signedData + signedDataLen);
#else
m_SignedData.resize (signedDataLen);
memcpy (m_SignedData.data (), signedData, signedDataLen);
#endif
SendPeerTest (msg);
if (!delayed)
SendPeerTest (msg);
// schedule resend for msgs 5 or 6
if (msg == 5 || msg == 6)
ScheduleResend ();
ScheduleResend (msg);
}

void SSU2PeerTestSession::SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen,
std::shared_ptr<const i2p::data::RouterInfo::Address> addr)
std::shared_ptr<const i2p::data::RouterInfo::Address> addr, bool delayed)
{
if (!addr) return;
SetAddress (addr);
SendPeerTest (msg, signedData, signedDataLen);
SendPeerTest (msg, signedData, signedDataLen, delayed);
}

void SSU2PeerTestSession::Connect ()
Expand All @@ -196,40 +195,37 @@ namespace transport
return false;
}

void SSU2PeerTestSession::ScheduleResend ()
void SSU2PeerTestSession::ScheduleResend (uint8_t msg)
{
if (m_NumResends < SSU2_PEER_TEST_MAX_NUM_RESENDS)
{
m_PeerTestResendTimer.expires_from_now (boost::posix_time::milliseconds(
SSU2_PEER_TEST_RESEND_INTERVAL + GetServer ().GetRng ()() % SSU2_PEER_TEST_RESEND_INTERVAL_VARIANCE));
std::weak_ptr<SSU2PeerTestSession> s(std::static_pointer_cast<SSU2PeerTestSession>(shared_from_this ()));
m_PeerTestResendTimer.async_wait ([s](const boost::system::error_code& ecode)
m_PeerTestResendTimer.async_wait ([s, msg](const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
auto s1 = s.lock ();
if (s1)
{
int msg = 0;
if (s1->m_MsgNumReceived < 6)
msg = (s1->m_MsgNumReceived == 5) ? 6 : 5;
if (msg) // 5 or 6
if (msg > s1->m_MsgNumReceived)
{
s1->SendPeerTest (msg);
s1->ScheduleResend ();
s1->m_NumResends++;
s1->ScheduleResend (msg);
}
}
}
});
m_NumResends++;
}
}

SSU2HolePunchSession::SSU2HolePunchSession (SSU2Server& server, uint32_t nonce,
const boost::asio::ip::udp::endpoint& remoteEndpoint,
std::shared_ptr<const i2p::data::RouterInfo::Address> addr):
SSU2Session (server), // we create full incoming session
m_Nonce (nonce), m_NumResends (0), m_HolePunchResendTimer (server.GetService ())
m_NumResends (0), m_HolePunchResendTimer (server.GetService ())
{
// we are Charlie
uint64_t destConnID = htobe64 (((uint64_t)nonce << 32) | nonce); // dest id
Expand Down Expand Up @@ -313,11 +309,11 @@ namespace transport
if (s1 && s1->GetState () == eSSU2SessionStateHolePunch)
{
s1->SendHolePunch ();
s1->m_NumResends++;
s1->ScheduleResend ();
}
}
});
m_NumResends++;
}
}

Expand Down
7 changes: 3 additions & 4 deletions libi2pd/SSU2OutOfSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ namespace transport
void SetStatusChanged () { m_IsStatusChanged = true; }

void SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen,
std::shared_ptr<const i2p::data::RouterInfo::Address> addr);
std::shared_ptr<const i2p::data::RouterInfo::Address> addr, bool delayed = false);
bool ProcessPeerTest (uint8_t * buf, size_t len) override;
void Connect () override; // outgoing
bool ProcessFirstIncomingMessage (uint64_t connID, uint8_t * buf, size_t len) override; // incoming

private:

void SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen); // PeerTest message
void SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, bool delayed = false); // PeerTest message
void SendPeerTest (uint8_t msg); // send or resend m_SignedData
void HandlePeerTest (const uint8_t * buf, size_t len) override;

void ScheduleResend ();
void ScheduleResend (uint8_t msg);

private:

Expand Down Expand Up @@ -74,7 +74,6 @@ namespace transport

private:

uint32_t m_Nonce;
int m_NumResends;
std::vector<uint8_t> m_RelayResponseBlock;
boost::asio::deadline_timer m_HolePunchResendTimer;
Expand Down
4 changes: 3 additions & 1 deletion libi2pd/SSU2Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2312,7 +2312,7 @@ namespace transport
{
if (!session->IsConnectedRecently ())
SetRouterStatus (eRouterStatusOK);
// send msg 6
// send msg 6 immeditely
session->SendPeerTest (6, buf + offset, len - offset, addr);
}
else
Expand All @@ -2323,6 +2323,8 @@ namespace transport
session->m_Address = addr;
if (GetTestingState ())
{
// schedule msg 6 with delay
session->SendPeerTest (6, buf + offset, len - offset, addr, true);
SetTestingState (false);
if (GetRouterStatus () != eRouterStatusFirewalled && addr->IsPeerTesting ())
{
Expand Down

0 comments on commit 0213f05

Please sign in to comment.