Browse Source

send DeliveryStatusMsg for LeaseSet

pull/1499/head
orignal 5 years ago
parent
commit
4adc741de3
  1. 53
      libi2pd/ECIESX25519AEADRatchetSession.cpp
  2. 1
      libi2pd/ECIESX25519AEADRatchetSession.h
  3. 56
      libi2pd/Garlic.cpp
  4. 7
      libi2pd/Garlic.h

53
libi2pd/ECIESX25519AEADRatchetSession.cpp

@ -6,6 +6,8 @@ @@ -6,6 +6,8 @@
#include "Tag.h"
#include "I2PEndian.h"
#include "Timestamp.h"
#include "Tunnel.h"
#include "TunnelPool.h"
#include "ECIESX25519AEADRatchetSession.h"
namespace i2p
@ -460,12 +462,22 @@ namespace garlic @@ -460,12 +462,22 @@ namespace garlic
std::vector<uint8_t> ECIESX25519AEADRatchetSession::CreatePayload (std::shared_ptr<const I2NPMessage> msg)
{
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
size_t payloadLen = 7; // datatime
if (msg && m_Destination)
payloadLen += msg->GetPayloadLength () + 13 + 32;
auto leaseSet = CreateDatabaseStoreMsg (GetOwner ()->GetLeaseSet ());
if (leaseSet)
auto leaseSet = (GetLeaseSetUpdateStatus () == eLeaseSetUpdated) ? CreateDatabaseStoreMsg (GetOwner ()->GetLeaseSet ()) : nullptr;
std::shared_ptr<I2NPMessage> deliveryStatus;
if (leaseSet)
{
payloadLen += leaseSet->GetPayloadLength () + 13;
deliveryStatus = CreateEncryptedDeliveryStatusMsg (leaseSet->GetMsgID ());
payloadLen += deliveryStatus->GetPayloadLength () + 49;
if (GetLeaseSetUpdateMsgID ()) GetOwner ()->RemoveDeliveryStatusSession (GetLeaseSetUpdateMsgID ()); // remove previous
SetLeaseSetUpdateStatus (eLeaseSetSubmitted);
SetLeaseSetUpdateMsgID (leaseSet->GetMsgID ());
SetLeaseSetSubmissionTime (ts);
}
uint8_t paddingSize;
RAND_bytes (&paddingSize, 1);
paddingSize &= 0x0F; paddingSize++; // 1 - 16
@ -475,10 +487,13 @@ namespace garlic @@ -475,10 +487,13 @@ namespace garlic
// DateTime
v[offset] = eECIESx25519BlkDateTime; offset++;
htobe16buf (v.data () + offset, 4); offset += 2;
htobe32buf (v.data () + offset, i2p::util::GetSecondsSinceEpoch ()); offset += 4;
htobe32buf (v.data () + offset, ts/1000); offset += 4; // in seconds
// LeaseSet
if (leaseSet)
offset += CreateGarlicClove (leaseSet, v.data () + offset, payloadLen - offset);
// DeliveryStatus
if (deliveryStatus)
offset += CreateDeliveryStatusClove (deliveryStatus, v.data () + offset, payloadLen - offset);
// msg
if (msg && m_Destination)
offset += CreateGarlicClove (msg, v.data () + offset, payloadLen - offset, true);
@ -513,6 +528,38 @@ namespace garlic @@ -513,6 +528,38 @@ namespace garlic
return cloveSize + 3;
}
size_t ECIESX25519AEADRatchetSession::CreateDeliveryStatusClove (std::shared_ptr<const I2NPMessage> msg, uint8_t * buf, size_t len)
{
uint16_t cloveSize = msg->GetPayloadLength () + 9 + 37 /* delivery instruction */;
if ((int)len < cloveSize + 3) return 0;
buf[0] = eECIESx25519BlkGalicClove; // clove type
htobe16buf (buf + 1, cloveSize); // size
buf += 3;
if (GetOwner ())
{
auto inboundTunnel = GetOwner ()->GetTunnelPool ()->GetNextInboundTunnel ();
if (inboundTunnel)
{
// delivery instructions
*buf = eGarlicDeliveryTypeTunnel << 5; buf++; // delivery instructions flag tunnel
// hash and tunnelID sequence is reversed for Garlic
memcpy (buf, inboundTunnel->GetNextIdentHash (), 32); buf += 32;// To Hash
htobe32buf (buf, inboundTunnel->GetNextTunnelID ()); buf += 4;// tunnelID
}
else
{
LogPrint (eLogError, "Garlic: No inbound tunnels in the pool for DeliveryStatus");
return 0;
}
htobe32buf (buf + 1, msg->GetMsgID ()); // msgID
htobe32buf (buf + 5, msg->GetExpiration ()/1000); // expiration in seconds
memcpy (buf + 9, msg->GetPayload (), msg->GetPayloadLength ());
}
else
return 0;
return cloveSize + 3;
}
void ECIESX25519AEADRatchetSession::GenerateMoreReceiveTags (int numTags)
{
for (int i = 0; i < numTags; i++)

1
libi2pd/ECIESX25519AEADRatchetSession.h

@ -107,6 +107,7 @@ namespace garlic @@ -107,6 +107,7 @@ namespace garlic
std::vector<uint8_t> CreatePayload (std::shared_ptr<const I2NPMessage> msg);
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);
void GenerateMoreReceiveTags (int numTags);

56
libi2pd/Garlic.cpp

@ -58,6 +58,34 @@ namespace garlic @@ -58,6 +58,34 @@ namespace garlic
m_SharedRoutingPath = path;
}
bool GarlicRoutingSession::MessageConfirmed (uint32_t msgID)
{
if (msgID == GetLeaseSetUpdateMsgID ())
{
SetLeaseSetUpdateStatus (eLeaseSetUpToDate);
SetLeaseSetUpdateMsgID (0);
LogPrint (eLogInfo, "Garlic: LeaseSet update confirmed");
return true;
}
return false;
}
std::shared_ptr<I2NPMessage> GarlicRoutingSession::CreateEncryptedDeliveryStatusMsg (uint32_t msgID)
{
auto msg = CreateDeliveryStatusMsg (msgID);
if (GetOwner ())
{
//encrypt
uint8_t key[32], tag[32];
RAND_bytes (key, 32); // random session key
RAND_bytes (tag, 32); // random session tag
GetOwner ()->SubmitSessionKey (key, tag);
ElGamalAESSession garlic (key, tag);
msg = garlic.WrapSingleMessage (msg);
}
return msg;
}
ElGamalAESSession::ElGamalAESSession (GarlicDestination * owner,
std::shared_ptr<const i2p::data::RoutingDestination> destination, int numTags, bool attachLeaseSet):
GarlicRoutingSession (owner, attachLeaseSet),
@ -289,19 +317,12 @@ namespace garlic @@ -289,19 +317,12 @@ namespace garlic
htobe32buf (buf + size, inboundTunnel->GetNextTunnelID ()); // tunnelID
size += 4;
// create msg
auto msg = CreateDeliveryStatusMsg (msgID);
if (GetOwner ())
auto msg = CreateEncryptedDeliveryStatusMsg (msgID);
if (msg)
{
//encrypt
uint8_t key[32], tag[32];
RAND_bytes (key, 32); // random session key
RAND_bytes (tag, 32); // random session tag
GetOwner ()->SubmitSessionKey (key, tag);
ElGamalAESSession garlic (key, tag);
msg = garlic.WrapSingleMessage (msg);
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
size += msg->GetLength ();
}
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
size += msg->GetLength ();
// fill clove
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 8000; // 8 sec
uint32_t cloveID;
@ -334,17 +355,12 @@ namespace garlic @@ -334,17 +355,12 @@ namespace garlic
return tags;
}
void ElGamalAESSession::MessageConfirmed (uint32_t msgID)
bool ElGamalAESSession::MessageConfirmed (uint32_t msgID)
{
TagsConfirmed (msgID);
if (msgID == GetLeaseSetUpdateMsgID ())
{
SetLeaseSetUpdateStatus (eLeaseSetUpToDate);
SetLeaseSetUpdateMsgID (0);
LogPrint (eLogInfo, "Garlic: LeaseSet update confirmed");
}
else
if (!GarlicRoutingSession::MessageConfirmed (msgID))
CleanupExpiredTags ();
return true;
}
void ElGamalAESSession::TagsConfirmed (uint32_t msgID)
@ -775,7 +791,7 @@ namespace garlic @@ -775,7 +791,7 @@ namespace garlic
void GarlicDestination::HandleDeliveryStatusMessage (uint32_t msgID)
{
ElGamalAESSessionPtr session;
GarlicRoutingSessionPtr session;
{
std::unique_lock<std::mutex> l(m_DeliveryStatusSessionsMutex);
auto it = m_DeliveryStatusSessions.find (msgID);

7
libi2pd/Garlic.h

@ -104,6 +104,7 @@ namespace garlic @@ -104,6 +104,7 @@ namespace garlic
virtual ~GarlicRoutingSession ();
virtual std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg) = 0;
virtual bool CleanupUnconfirmedTags () { return false; }; // for I2CP, override in ElGamalAESSession
virtual bool MessageConfirmed (uint32_t msgID);
void SetLeaseSetUpdated ()
{
@ -127,6 +128,8 @@ namespace garlic @@ -127,6 +128,8 @@ namespace garlic
void SetLeaseSetUpdateMsgID (uint32_t msgID) { m_LeaseSetUpdateMsgID = msgID; }
void SetLeaseSetSubmissionTime (uint64_t ts) { m_LeaseSetSubmissionTime = ts; }
std::shared_ptr<I2NPMessage> CreateEncryptedDeliveryStatusMsg (uint32_t msgID);
private:
GarlicDestination * m_Owner;
@ -165,7 +168,7 @@ namespace garlic @@ -165,7 +168,7 @@ namespace garlic
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg);
void MessageConfirmed (uint32_t msgID);
bool MessageConfirmed (uint32_t msgID);
bool CleanupExpiredTags (); // returns true if something left
bool CleanupUnconfirmedTags (); // returns true if something has been deleted
@ -267,7 +270,7 @@ namespace garlic @@ -267,7 +270,7 @@ namespace garlic
std::unordered_map<uint64_t, ECIESX25519AEADRatchetIndexSession> m_ECIESx25519Tags; // session tag -> session
// DeliveryStatus
std::mutex m_DeliveryStatusSessionsMutex;
std::unordered_map<uint32_t, ElGamalAESSessionPtr> m_DeliveryStatusSessions; // msgID -> session
std::unordered_map<uint32_t, GarlicRoutingSessionPtr> m_DeliveryStatusSessions; // msgID -> session
public:

Loading…
Cancel
Save