Adam Ierymenko 6 years ago
parent
commit
2f7d3e655a
2 changed files with 20 additions and 5 deletions
  1. 13 0
      node/Packet.cpp
  2. 7 5
      node/Packet.hpp

+ 13 - 0
node/Packet.cpp

@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <stdio.h>
 
 
 #include "Packet.hpp"
 #include "Packet.hpp"
+#include "Mutex.hpp"
 
 
 #ifdef _MSC_VER
 #ifdef _MSC_VER
 #define FORCE_INLINE static __forceinline
 #define FORCE_INLINE static __forceinline
@@ -932,4 +933,16 @@ bool Packet::uncompress()
 	return true;
 	return true;
 }
 }
 
 
+uint64_t Packet::nextPacketId()
+{
+	static uint64_t ctr = 0;
+	static Mutex lock;
+	lock.lock();
+	if (unlikely(ctr == 0))
+		Utils::getSecureRandom(&ctr,sizeof(ctr));
+	const uint64_t i = ctr++;
+	lock.unlock();
+	return i;
+}
+
 } // namespace ZeroTier
 } // namespace ZeroTier

+ 7 - 5
node/Packet.hpp

@@ -986,7 +986,7 @@ public:
 	ZT_ALWAYS_INLINE Packet() :
 	ZT_ALWAYS_INLINE Packet() :
 		Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
 		Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
 	{
 	{
-		setAt<uint64_t>(ZT_PACKET_IDX_IV,Utils::random());
+		setAt<uint64_t>(ZT_PACKET_IDX_IV,Packet::nextPacketId());
 		(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops
 		(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops
 	}
 	}
 
 
@@ -1002,7 +1002,7 @@ public:
 	ZT_ALWAYS_INLINE Packet(const Packet &prototype,const Address &dest) :
 	ZT_ALWAYS_INLINE Packet(const Packet &prototype,const Address &dest) :
 		Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(prototype)
 		Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(prototype)
 	{
 	{
-		setAt<uint64_t>(ZT_PACKET_IDX_IV,Utils::random());
+		setAt<uint64_t>(ZT_PACKET_IDX_IV,Packet::nextPacketId());
 		setDestination(dest);
 		setDestination(dest);
 	}
 	}
 
 
@@ -1016,7 +1016,7 @@ public:
 	ZT_ALWAYS_INLINE Packet(const Address &dest,const Address &source,const Verb v) :
 	ZT_ALWAYS_INLINE Packet(const Address &dest,const Address &source,const Verb v) :
 		Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
 		Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
 	{
 	{
-		setAt<uint64_t>(ZT_PACKET_IDX_IV,Utils::random());
+		setAt<uint64_t>(ZT_PACKET_IDX_IV,Packet::nextPacketId());
 		setDestination(dest);
 		setDestination(dest);
 		setSource(source);
 		setSource(source);
 		(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags and hops
 		(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags and hops
@@ -1033,7 +1033,7 @@ public:
 	ZT_ALWAYS_INLINE void reset(const Address &dest,const Address &source,const Verb v)
 	ZT_ALWAYS_INLINE void reset(const Address &dest,const Address &source,const Verb v)
 	{
 	{
 		setSize(ZT_PROTO_MIN_PACKET_LENGTH);
 		setSize(ZT_PROTO_MIN_PACKET_LENGTH);
-		setAt<uint64_t>(ZT_PACKET_IDX_IV,Utils::random());
+		setAt<uint64_t>(ZT_PACKET_IDX_IV,Packet::nextPacketId());
 		setDestination(dest);
 		setDestination(dest);
 		setSource(source);
 		setSource(source);
 		(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops
 		(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops
@@ -1047,7 +1047,7 @@ public:
 	 * technically different but otherwise identical copies of the same
 	 * technically different but otherwise identical copies of the same
 	 * packet.
 	 * packet.
 	 */
 	 */
-	ZT_ALWAYS_INLINE void newInitializationVector() { setAt<uint64_t>(ZT_PACKET_IDX_IV,Utils::random()); }
+	ZT_ALWAYS_INLINE void newInitializationVector() { setAt<uint64_t>(ZT_PACKET_IDX_IV,Packet::nextPacketId()); }
 
 
 	/**
 	/**
 	 * Set this packet's destination
 	 * Set this packet's destination
@@ -1238,6 +1238,8 @@ public:
 private:
 private:
 	static const unsigned char ZERO_KEY[32];
 	static const unsigned char ZERO_KEY[32];
 
 
+	static uint64_t nextPacketId();
+
 	/**
 	/**
 	 * Deterministically mangle a 256-bit crypto key based on packet
 	 * Deterministically mangle a 256-bit crypto key based on packet
 	 *
 	 *