From c7c6e5917a49f234e190fa16cd29a687ececc1d0 Mon Sep 17 00:00:00 2001
From: orignal <i2porignal@yandex.ru>
Date: Mon, 18 May 2020 20:45:25 -0400
Subject: [PATCH] Streaming MTU size 1812 for ECIESX25519AEADRatchet

---
 libi2pd/Streaming.cpp | 16 +++++++++++-----
 libi2pd/Streaming.h   |  2 ++
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/libi2pd/Streaming.cpp b/libi2pd/Streaming.cpp
index 3355d99a..ad3f6209 100644
--- a/libi2pd/Streaming.cpp
+++ b/libi2pd/Streaming.cpp
@@ -63,7 +63,7 @@ namespace stream
 		m_AckSendTimer (m_Service),  m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (port),
 		m_WindowSize (MIN_WINDOW_SIZE), m_RTT (INITIAL_RTT), m_RTO (INITIAL_RTO),
 		m_AckDelay (local.GetOwner ()->GetStreamingAckDelay ()),
-		m_LastWindowSizeIncreaseTime (0), m_NumResendAttempts (0)
+		m_LastWindowSizeIncreaseTime (0), m_NumResendAttempts (0), m_MTU (STREAMING_MTU)
 	{
 		RAND_bytes ((uint8_t *)&m_RecvStreamID, 4);
 		m_RemoteIdentity = remote->GetIdentity ();
@@ -75,7 +75,7 @@ namespace stream
 		m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service),
 		m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (0),  m_WindowSize (MIN_WINDOW_SIZE),
 		m_RTT (INITIAL_RTT), m_RTO (INITIAL_RTO), m_AckDelay (local.GetOwner ()->GetStreamingAckDelay ()),
-		m_LastWindowSizeIncreaseTime (0), m_NumResendAttempts (0)
+		m_LastWindowSizeIncreaseTime (0), m_NumResendAttempts (0), m_MTU (STREAMING_MTU)
 	{
 		RAND_bytes ((uint8_t *)&m_RecvStreamID, 4);
 	}
@@ -473,6 +473,12 @@ namespace stream
 				{
 					//  initial packet
 					m_Status = eStreamStatusOpen;
+					if (!m_RemoteLeaseSet) m_RemoteLeaseSet = m_LocalDestination.GetOwner ()->FindLeaseSet (m_RemoteIdentity->GetIdentHash ());;
+					if (m_RemoteLeaseSet)
+					{	
+						m_RoutingSession = m_LocalDestination.GetOwner ()->GetRoutingSession (m_RemoteLeaseSet, true);
+						m_MTU = m_RoutingSession->IsRatchets () ? STREAMING_MTU_RATCHETS : STREAMING_MTU;
+					}	
 					uint16_t flags = PACKET_FLAG_SYNCHRONIZE | PACKET_FLAG_FROM_INCLUDED |
 						PACKET_FLAG_SIGNATURE_INCLUDED | PACKET_FLAG_MAX_PACKET_SIZE_INCLUDED;
 					if (isNoAck) flags |= PACKET_FLAG_NO_ACK;
@@ -486,7 +492,7 @@ namespace stream
 					size += 2; // options size
 					m_LocalDestination.GetOwner ()->GetIdentity ()->ToBuffer (packet + size, identityLen);
 					size += identityLen; // from
-					htobe16buf (packet + size, STREAMING_MTU);
+					htobe16buf (packet + size, m_MTU);
 					size += 2; // max packet size
 					if (isOfflineSignature)
 					{
@@ -498,7 +504,7 @@ namespace stream
 					memset (signature, 0, signatureLen); // zeroes for now
 					size += signatureLen; // signature
 					htobe16buf (optionsSize, packet + size - 2 - optionsSize); // actual options size
-					size += m_SendBuffer.Get (packet + size, STREAMING_MTU); // payload
+					size += m_SendBuffer.Get (packet + size, m_MTU); // payload
 					m_LocalDestination.GetOwner ()->Sign (packet, size, signature);
 				}
 				else
@@ -508,7 +514,7 @@ namespace stream
 					size += 2; // flags
 					htobuf16 (packet + size, 0); // no options
 					size += 2; // options size
-					size += m_SendBuffer.Get(packet + size, STREAMING_MTU); // payload
+					size += m_SendBuffer.Get(packet + size, m_MTU); // payload
 				}
 				p->len = size;
 				packets.push_back (p);
diff --git a/libi2pd/Streaming.h b/libi2pd/Streaming.h
index 64e4c18d..d3d47794 100644
--- a/libi2pd/Streaming.h
+++ b/libi2pd/Streaming.h
@@ -41,6 +41,7 @@ namespace stream
 	const uint16_t PACKET_FLAG_OFFLINE_SIGNATURE = 0x0800;
 
 	const size_t STREAMING_MTU = 1730;
+	const size_t STREAMING_MTU_RATCHETS = 1812;
 	const size_t MAX_PACKET_SIZE = 4096;
 	const size_t COMPRESSION_THRESHOLD_SIZE = 66;
 	const int MAX_NUM_RESEND_ATTEMPTS = 6;
@@ -234,6 +235,7 @@ namespace stream
 			int m_WindowSize, m_RTT, m_RTO, m_AckDelay;
 			uint64_t m_LastWindowSizeIncreaseTime;
 			int m_NumResendAttempts;
+			size_t m_MTU;
 	};
 
 	class StreamingDestination: public std::enable_shared_from_this<StreamingDestination>