mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 03:53:44 +02:00
GitHub issue #461 -- plus a bit of cleanup and optimization
This commit is contained in:
parent
ef46d3c97d
commit
e10325e133
6 changed files with 29 additions and 19 deletions
|
@ -56,12 +56,9 @@ public:
|
||||||
THING_IPV6_ADDRESS = 3
|
THING_IPV6_ADDRESS = 3
|
||||||
};
|
};
|
||||||
|
|
||||||
CertificateOfOwnership() :
|
CertificateOfOwnership()
|
||||||
_networkId(0),
|
|
||||||
_ts(0),
|
|
||||||
_id(0),
|
|
||||||
_thingCount(0)
|
|
||||||
{
|
{
|
||||||
|
memset(this,0,sizeof(CertificateOfOwnership));
|
||||||
}
|
}
|
||||||
|
|
||||||
CertificateOfOwnership(const uint64_t nwid,const uint64_t ts,const Address &issuedTo,const uint32_t id) :
|
CertificateOfOwnership(const uint64_t nwid,const uint64_t ts,const Address &issuedTo,const uint32_t id) :
|
||||||
|
|
|
@ -72,13 +72,15 @@ bool IncomingPacket::tryDecode(const RuntimeEnvironment *RR)
|
||||||
if (peer) {
|
if (peer) {
|
||||||
if (!trusted) {
|
if (!trusted) {
|
||||||
if (!dearmor(peer->key())) {
|
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());
|
TRACE("dropped packet from %s(%s), MAC authentication failed (size: %u)",sourceAddress.toString().c_str(),_path->address().toString().c_str(),size());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!uncompress()) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -374,6 +374,7 @@ private:
|
||||||
|
|
||||||
struct _IncomingConfigChunk
|
struct _IncomingConfigChunk
|
||||||
{
|
{
|
||||||
|
_IncomingConfigChunk() { memset(this,0,sizeof(_IncomingConfigChunk)); }
|
||||||
uint64_t ts;
|
uint64_t ts;
|
||||||
uint64_t updateId;
|
uint64_t updateId;
|
||||||
uint64_t haveChunkIds[ZT_NETWORK_MAX_UPDATE_CHUNKS];
|
uint64_t haveChunkIds[ZT_NETWORK_MAX_UPDATE_CHUNKS];
|
||||||
|
|
|
@ -94,7 +94,9 @@ void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,const Address &toA
|
||||||
_packet.newInitializationVector();
|
_packet.newInitializationVector();
|
||||||
_packet.setDestination(toAddr2);
|
_packet.setDestination(toAddr2);
|
||||||
RR->node->expectReplyTo(_packet.packetId());
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1066,7 +1066,7 @@ void Packet::armor(const void *key,bool encryptPayload,unsigned int counter)
|
||||||
uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
|
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
|
// 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
|
// 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);
|
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()
|
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
|
if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 64))) { // don't bother compressing tiny packets
|
||||||
int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
|
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)) {
|
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);
|
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;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Packet::uncompress()
|
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 ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
|
||||||
if (size() > ZT_PACKET_IDX_PAYLOAD) {
|
if (size() > ZT_PACKET_IDX_PAYLOAD) {
|
||||||
unsigned int compLen = 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))) {
|
if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
|
||||||
setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
|
setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
|
||||||
memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)ucl),buf,ucl);
|
memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,ucl);
|
||||||
} else return false;
|
} 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1322,7 +1322,7 @@ public:
|
||||||
/**
|
/**
|
||||||
* @return Value of link quality counter extracted from this packet's ID, range 0 to 7 (3 bits)
|
* @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
|
* Set packet verb
|
||||||
|
|
Loading…
Add table
Reference in a new issue