Selaa lähdekoodia

GitHub issue #461 -- plus a bit of cleanup and optimization

Adam Ierymenko 8 vuotta sitten
vanhempi
commit
e10325e133
6 muutettua tiedostoa jossa 29 lisäystä ja 19 poistoa
  1. 2 5
      node/CertificateOfOwnership.hpp
  2. 3 1
      node/IncomingPacket.cpp
  3. 1 0
      node/Network.hpp
  4. 3 1
      node/OutboundMulticast.cpp
  5. 19 11
      node/Packet.cpp
  6. 1 1
      node/Packet.hpp

+ 2 - 5
node/CertificateOfOwnership.hpp

@@ -56,12 +56,9 @@ public:
 		THING_IPV6_ADDRESS = 3
 	};
 
-	CertificateOfOwnership() :
-		_networkId(0),
-		_ts(0),
-		_id(0),
-		_thingCount(0)
+	CertificateOfOwnership()
 	{
+		memset(this,0,sizeof(CertificateOfOwnership));
 	}
 
 	CertificateOfOwnership(const uint64_t nwid,const uint64_t ts,const Address &issuedTo,const uint32_t id) :

+ 3 - 1
node/IncomingPacket.cpp

@@ -72,13 +72,15 @@ bool IncomingPacket::tryDecode(const RuntimeEnvironment *RR)
 		if (peer) {
 			if (!trusted) {
 				if (!dearmor(peer->key())) {
+					//fprintf(stderr,"dropped packet from %s(%s), MAC authentication failed (size: %u)" ZT_EOL_S,sourceAddress.toString().c_str(),_path->address().toString().c_str(),size());
 					TRACE("dropped packet from %s(%s), MAC authentication failed (size: %u)",sourceAddress.toString().c_str(),_path->address().toString().c_str(),size());
 					return true;
 				}
 			}
 
 			if (!uncompress()) {
-				TRACE("dropped packet from %s(%s), compressed data invalid (verb may be %u)",sourceAddress.toString().c_str(),_path->address().toString().c_str(),(unsigned int)verb());
+				//fprintf(stderr,"dropped packet from %s(%s), compressed data invalid (size %u, verb may be %u)" ZT_EOL_S,sourceAddress.toString().c_str(),_path->address().toString().c_str(),size(),(unsigned int)verb());
+				TRACE("dropped packet from %s(%s), compressed data invalid (size %u, verb may be %u)",sourceAddress.toString().c_str(),_path->address().toString().c_str(),size(),(unsigned int)verb());
 				return true;
 			}
 

+ 1 - 0
node/Network.hpp

@@ -374,6 +374,7 @@ private:
 
 	struct _IncomingConfigChunk
 	{
+		_IncomingConfigChunk() { memset(this,0,sizeof(_IncomingConfigChunk)); }
 		uint64_t ts;
 		uint64_t updateId;
 		uint64_t haveChunkIds[ZT_NETWORK_MAX_UPDATE_CHUNKS];

+ 3 - 1
node/OutboundMulticast.cpp

@@ -94,7 +94,9 @@ void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,const Address &toA
 		_packet.newInitializationVector();
 		_packet.setDestination(toAddr2);
 		RR->node->expectReplyTo(_packet.packetId());
-		RR->sw->send(_packet,true);
+
+		Packet tmp(_packet); // make a copy of packet so as not to garble the original -- GitHub issue #461
+		RR->sw->send(tmp,true);
 	}
 }
 

+ 19 - 11
node/Packet.cpp

@@ -1066,7 +1066,7 @@ void Packet::armor(const void *key,bool encryptPayload,unsigned int counter)
 	uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
 
 	// Mask least significant 3 bits of packet ID with counter to embed packet send counter for QoS use
-	data[7] = (data[7] & 0xf8) | ((uint8_t)counter & 0x07);
+	data[7] = (data[7] & 0xf8) | (uint8_t)(counter & 0x07);
 
 	// Set flag now, since it affects key mangle function
 	setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE);
@@ -1124,35 +1124,43 @@ void Packet::cryptField(const void *key,unsigned int start,unsigned int len)
 
 bool Packet::compress()
 {
-	unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
+	char *const data = reinterpret_cast<char *>(unsafeData());
+	char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
+
 	if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 64))) { // don't bother compressing tiny packets
 		int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
-		int cl = LZ4_compress_fast((const char *)field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)pl),(char *)buf,pl,ZT_PROTO_MAX_PACKET_LENGTH * 2,2);
+		int cl = LZ4_compress_fast(data + ZT_PACKET_IDX_PAYLOAD,buf,pl,ZT_PROTO_MAX_PACKET_LENGTH * 2,2);
 		if ((cl > 0)&&(cl < pl)) {
-			(*this)[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
+			data[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
 			setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
-			memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)cl),buf,cl);
+			memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,cl);
 			return true;
 		}
 	}
-	(*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
+	data[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
+
 	return false;
 }
 
 bool Packet::uncompress()
 {
-	unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH];
+	char *const data = reinterpret_cast<char *>(unsafeData());
+	char buf[ZT_PROTO_MAX_PACKET_LENGTH];
+
 	if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
 		if (size() > ZT_PACKET_IDX_PAYLOAD) {
 			unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
-			int ucl = LZ4_decompress_safe((const char *)field(ZT_PACKET_IDX_PAYLOAD,compLen),(char *)buf,compLen,sizeof(buf));
+			int ucl = LZ4_decompress_safe((const char *)data + ZT_PACKET_IDX_PAYLOAD,buf,compLen,sizeof(buf));
 			if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
 				setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
-				memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)ucl),buf,ucl);
-			} else return false;
+				memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,ucl);
+			} else {
+				return false;
+			}
 		}
-		(*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
+		data[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
 	}
+
 	return true;
 }
 

+ 1 - 1
node/Packet.hpp

@@ -1322,7 +1322,7 @@ public:
 	/**
 	 * @return Value of link quality counter extracted from this packet's ID, range 0 to 7 (3 bits)
 	 */
-	inline unsigned int linkQualityCounter() const { return (unsigned int)(reinterpret_cast<const uint8_t *>(data())[7] & 7); }
+	inline unsigned int linkQualityCounter() const { return (unsigned int)(reinterpret_cast<const uint8_t *>(data())[7] & 0x07); }
 
 	/**
 	 * Set packet verb