MCPcopy Create free account
hub / github.com/PurpleI2P/i2pd / LocalLeaseSet

Method LocalLeaseSet

libi2pd/LeaseSet.cpp:752–803  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

750 }
751
752 LocalLeaseSet::LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * encryptionPublicKey, std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels):
753 m_ExpirationTime (0), m_Identity (identity)
754 {
755 int num = tunnels.size ();
756 if (num > MAX_NUM_LEASES) num = MAX_NUM_LEASES;
757 // identity
758 auto signingKeyLen = m_Identity->GetSigningPublicKeyLen ();
759 m_BufferLen = m_Identity->GetFullLen () + 256 + signingKeyLen + 1 + num*LEASE_SIZE + m_Identity->GetSignatureLen ();
760 m_Buffer = new uint8_t[m_BufferLen];
761 auto offset = m_Identity->ToBuffer (m_Buffer, m_BufferLen);
762 memcpy (m_Buffer + offset, encryptionPublicKey, 256);
763 offset += 256;
764 memset (m_Buffer + offset, 0, signingKeyLen);
765 offset += signingKeyLen;
766 // num leases
767 auto numLeasesPos = offset;
768 m_Buffer[offset] = num;
769 offset++;
770 // leases
771 m_Leases = m_Buffer + offset;
772 auto currentTime = i2p::util::GetMillisecondsSinceEpoch ();
773 int skipped = 0;
774 for (int i = 0; i < num; i++)
775 {
776 uint64_t ts = tunnels[i]->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD; // 1 minute before expiration
777 ts *= 1000; // in milliseconds
778 if (ts <= currentTime)
779 {
780 // already expired, skip
781 skipped++;
782 continue;
783 }
784 if (ts > m_ExpirationTime) m_ExpirationTime = ts;
785 // make sure leaseset is newer than previous, but adding some time to expiration date
786 ts += (currentTime - tunnels[i]->GetCreationTime ()*1000LL)*2/i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT; // up to 2 secs
787 memcpy (m_Buffer + offset, tunnels[i]->GetNextIdentHash (), 32);
788 offset += 32; // gateway id
789 htobe32buf (m_Buffer + offset, tunnels[i]->GetNextTunnelID ());
790 offset += 4; // tunnel id
791 htobe64buf (m_Buffer + offset, ts);
792 offset += 8; // end date
793 }
794 if (skipped > 0)
795 {
796 // adjust num leases
797 if (skipped > num) skipped = num;
798 num -= skipped;
799 m_BufferLen -= skipped*LEASE_SIZE;
800 m_Buffer[numLeasesPos] = num;
801 }
802 // we don't sign it yet. must be signed later on
803 }
804
805 LocalLeaseSet::LocalLeaseSet (std::shared_ptr<const IdentityEx> identity, const uint8_t * buf, size_t len):
806 m_ExpirationTime (0), m_Identity (identity)

Callers

nothing calls this directly

Calls 9

htobe32bufFunction · 0.85
htobe64bufFunction · 0.85
GetFullLenMethod · 0.45
GetSignatureLenMethod · 0.45
ToBufferMethod · 0.45
GetCreationTimeMethod · 0.45
GetNextTunnelIDMethod · 0.45

Tested by

no test coverage detected