Browse Source

use ack request instead DeliveryStatus for LeaseSet confirmation

pull/1515/head
orignal 5 years ago
parent
commit
e3c8f3fd6f
  1. 54
      libi2pd/ECIESX25519AEADRatchetSession.cpp
  2. 4
      libi2pd/ECIESX25519AEADRatchetSession.h
  3. 4
      libi2pd/Garlic.cpp

54
libi2pd/ECIESX25519AEADRatchetSession.cpp

@ -75,8 +75,8 @@ namespace garlic
} }
} }
ECIESX25519AEADRatchetSession::ECIESX25519AEADRatchetSession (GarlicDestination * owner): ECIESX25519AEADRatchetSession::ECIESX25519AEADRatchetSession (GarlicDestination * owner, bool attachLeaseSet):
GarlicRoutingSession (owner, true) GarlicRoutingSession (owner, attachLeaseSet)
{ {
ResetKeys (); ResetKeys ();
} }
@ -217,6 +217,18 @@ namespace garlic
case eECIESx25519BlkPadding: case eECIESx25519BlkPadding:
LogPrint (eLogDebug, "Garlic: padding"); LogPrint (eLogDebug, "Garlic: padding");
break; break;
case eECIESx25519BlkAck:
{
LogPrint (eLogDebug, "Garlic: ack");
int numAcks = size >> 2; // /4
auto offset1 = offset;
for (auto i = 0; i < numAcks; i++)
{
offset1 += 2; // tagsetid
MessageConfirmed (bufbe16toh (buf + offset1)); offset1 += 2; // N
}
break;
}
case eECIESx25519BlkAckRequest: case eECIESx25519BlkAckRequest:
{ {
LogPrint (eLogDebug, "Garlic: ack request"); LogPrint (eLogDebug, "Garlic: ack request");
@ -398,6 +410,10 @@ namespace garlic
GetOwner ()->AddECIESx25519Session (m_RemoteStaticKey, shared_from_this ()); GetOwner ()->AddECIESx25519Session (m_RemoteStaticKey, shared_from_this ());
HandlePayload (payload.data (), len - 16); HandlePayload (payload.data (), len - 16);
// we have received reply to NS with LeaseSet in it
SetLeaseSetUpdateStatus (eLeaseSetUpToDate);
SetLeaseSetUpdateMsgID (0);
return true; return true;
} }
@ -466,7 +482,7 @@ namespace garlic
auto m = NewI2NPMessage (); auto m = NewI2NPMessage ();
m->Align (12); // in order to get buf aligned to 16 (12 + 4) m->Align (12); // in order to get buf aligned to 16 (12 + 4)
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length
auto payload = CreatePayload (msg); auto payload = CreatePayload (msg, m_State != eSessionStateEstablished);
size_t len = payload.size (); size_t len = payload.size ();
switch (m_State) switch (m_State)
@ -501,24 +517,25 @@ namespace garlic
return m; return m;
} }
std::vector<uint8_t> ECIESX25519AEADRatchetSession::CreatePayload (std::shared_ptr<const I2NPMessage> msg) std::vector<uint8_t> ECIESX25519AEADRatchetSession::CreatePayload (std::shared_ptr<const I2NPMessage> msg, bool first)
{ {
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch (); uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
size_t payloadLen = 7; // datatime size_t payloadLen = 0;
if (first) payloadLen += 7;// datatime
if (msg && m_Destination) if (msg && m_Destination)
payloadLen += msg->GetPayloadLength () + 13 + 32; payloadLen += msg->GetPayloadLength () + 13 + 32;
auto leaseSet = (GetLeaseSetUpdateStatus () == eLeaseSetUpdated) ? CreateDatabaseStoreMsg (GetOwner ()->GetLeaseSet ()) : nullptr; auto leaseSet = (GetLeaseSetUpdateStatus () == eLeaseSetUpdated) ? CreateDatabaseStoreMsg (GetOwner ()->GetLeaseSet ()) : nullptr;
std::shared_ptr<I2NPMessage> deliveryStatus;
if (leaseSet) if (leaseSet)
{ {
payloadLen += leaseSet->GetPayloadLength () + 13; payloadLen += leaseSet->GetPayloadLength () + 13;
deliveryStatus = CreateEncryptedDeliveryStatusMsg (leaseSet->GetMsgID ()); if (!first)
payloadLen += deliveryStatus->GetPayloadLength () + 49; {
if (GetLeaseSetUpdateMsgID ()) GetOwner ()->RemoveDeliveryStatusSession (GetLeaseSetUpdateMsgID ()); // remove previous // ack request
SetLeaseSetUpdateStatus (eLeaseSetSubmitted); SetLeaseSetUpdateStatus (eLeaseSetSubmitted);
SetLeaseSetUpdateMsgID (leaseSet->GetMsgID ()); SetLeaseSetUpdateMsgID (m_SendTagset.GetNextIndex ());
SetLeaseSetSubmissionTime (ts); SetLeaseSetSubmissionTime (ts);
GetOwner ()->DeliveryStatusSent (shared_from_this (), leaseSet->GetMsgID ()); payloadLen += 4;
}
} }
if (m_AckRequests.size () > 0) if (m_AckRequests.size () > 0)
payloadLen += m_AckRequests.size ()*4 + 3; payloadLen += m_AckRequests.size ()*4 + 3;
@ -529,15 +546,24 @@ namespace garlic
std::vector<uint8_t> v(payloadLen); std::vector<uint8_t> v(payloadLen);
size_t offset = 0; size_t offset = 0;
// DateTime // DateTime
if (first)
{
v[offset] = eECIESx25519BlkDateTime; offset++; v[offset] = eECIESx25519BlkDateTime; offset++;
htobe16buf (v.data () + offset, 4); offset += 2; htobe16buf (v.data () + offset, 4); offset += 2;
htobe32buf (v.data () + offset, ts/1000); offset += 4; // in seconds htobe32buf (v.data () + offset, ts/1000); offset += 4; // in seconds
}
// LeaseSet // LeaseSet
if (leaseSet) if (leaseSet)
{
offset += CreateGarlicClove (leaseSet, v.data () + offset, payloadLen - offset); offset += CreateGarlicClove (leaseSet, v.data () + offset, payloadLen - offset);
// DeliveryStatus if (!first)
if (deliveryStatus) {
offset += CreateDeliveryStatusClove (deliveryStatus, v.data () + offset, payloadLen - offset); // ack request
v[offset] = eECIESx25519BlkAckRequest; offset++;
htobe16buf (v.data () + offset, 1); offset += 2;
v[offset] = 0; offset++; // flags
}
}
// msg // msg
if (msg && m_Destination) if (msg && m_Destination)
offset += CreateGarlicClove (msg, v.data () + offset, payloadLen - offset, true); offset += CreateGarlicClove (msg, v.data () + offset, payloadLen - offset, true);

4
libi2pd/ECIESX25519AEADRatchetSession.h

@ -74,7 +74,7 @@ namespace garlic
public: public:
ECIESX25519AEADRatchetSession (GarlicDestination * owner); ECIESX25519AEADRatchetSession (GarlicDestination * owner, bool attachLeaseSet);
~ECIESX25519AEADRatchetSession (); ~ECIESX25519AEADRatchetSession ();
bool HandleNextMessage (const uint8_t * buf, size_t len, int index = 0); bool HandleNextMessage (const uint8_t * buf, size_t len, int index = 0);
@ -109,7 +109,7 @@ namespace garlic
bool NextNewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen); bool NextNewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
bool NewExistingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen); bool NewExistingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
std::vector<uint8_t> CreatePayload (std::shared_ptr<const I2NPMessage> msg); std::vector<uint8_t> CreatePayload (std::shared_ptr<const I2NPMessage> msg, bool first);
size_t CreateGarlicClove (std::shared_ptr<const I2NPMessage> msg, uint8_t * buf, size_t len, bool isDestination = false); size_t CreateGarlicClove (std::shared_ptr<const I2NPMessage> msg, uint8_t * buf, size_t len, bool isDestination = false);
size_t CreateDeliveryStatusClove (std::shared_ptr<const I2NPMessage> msg, uint8_t * buf, size_t len); size_t CreateDeliveryStatusClove (std::shared_ptr<const I2NPMessage> msg, uint8_t * buf, size_t len);

4
libi2pd/Garlic.cpp

@ -690,7 +690,7 @@ namespace garlic
session = it->second; session = it->second;
if (!session) if (!session)
{ {
session = std::make_shared<ECIESX25519AEADRatchetSession> (this); session = std::make_shared<ECIESX25519AEADRatchetSession> (this, true);
session->SetRemoteStaticKey (staticKey); session->SetRemoteStaticKey (staticKey);
} }
session->SetDestination (destination->GetIdentHash ()); // TODO: remove session->SetDestination (destination->GetIdentHash ()); // TODO: remove
@ -920,7 +920,7 @@ namespace garlic
m_ECIESx25519Tags.erase (tag); m_ECIESx25519Tags.erase (tag);
} }
else else
session = std::make_shared<ECIESX25519AEADRatchetSession> (this); // incoming session = std::make_shared<ECIESX25519AEADRatchetSession> (this, false); // incoming
if (!session->HandleNextMessage (buf, len, index)) if (!session->HandleNextMessage (buf, len, index))
LogPrint (eLogError, "Garlic: can't handle ECIES-X25519-AEAD-Ratchet message"); LogPrint (eLogError, "Garlic: can't handle ECIES-X25519-AEAD-Ratchet message");

Loading…
Cancel
Save