From 7dee23fa0d943e50c51c28a1a4db561ad3ac3806 Mon Sep 17 00:00:00 2001 From: Jukka Vaisanen Date: Wed, 10 Jun 2026 18:07:44 +0300 Subject: [PATCH] ignore leading zero path padding for flood max --- examples/simple_repeater/MyMesh.cpp | 7 ++++--- examples/simple_room_server/MyMesh.cpp | 7 ++++--- examples/simple_sensor/SensorMesh.cpp | 2 +- src/Packet.cpp | 18 +++++++++++++++++- src/Packet.h | 1 + 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 096907494b..c73e5608f4 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -429,9 +429,10 @@ void MyMesh::sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, ui bool MyMesh::allowPacketForward(const mesh::Packet *packet) { if (_prefs.disable_fwd) return false; if (packet->isRouteFlood()) { - if (packet->getPathHashCount() >= _prefs.flood_max) return false; - if (packet->getRouteType() == ROUTE_TYPE_FLOOD && packet->getPathHashCount() >= _prefs.flood_max_unscoped) return false; - if (packet->getPayloadType() == PAYLOAD_TYPE_ADVERT && packet->getPathHashCount() >= _prefs.flood_max_advert) return false; + uint8_t flood_hops = packet->getPathHashCountExcludingLeadingZeros(); + if (flood_hops >= _prefs.flood_max) return false; + if (packet->getRouteType() == ROUTE_TYPE_FLOOD && flood_hops >= _prefs.flood_max_unscoped) return false; + if (packet->getPayloadType() == PAYLOAD_TYPE_ADVERT && flood_hops >= _prefs.flood_max_advert) return false; } if (packet->isRouteFlood() && recv_pkt_region == NULL) { MESH_DEBUG_PRINTLN("allowPacketForward: unknown transport code, or wildcard not allowed for FLOOD packet"); diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index 98b22fdb72..2c2ac51306 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -283,9 +283,10 @@ uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) { bool MyMesh::allowPacketForward(const mesh::Packet *packet) { if (_prefs.disable_fwd) return false; if (packet->isRouteFlood()) { - if (packet->getPathHashCount() >= _prefs.flood_max) return false; - if (packet->getRouteType() == ROUTE_TYPE_FLOOD && packet->getPathHashCount() >= _prefs.flood_max_unscoped) return false; - if (packet->getPayloadType() == PAYLOAD_TYPE_ADVERT && packet->getPathHashCount() >= _prefs.flood_max_advert) return false; + uint8_t flood_hops = packet->getPathHashCountExcludingLeadingZeros(); + if (flood_hops >= _prefs.flood_max) return false; + if (packet->getRouteType() == ROUTE_TYPE_FLOOD && flood_hops >= _prefs.flood_max_unscoped) return false; + if (packet->getPayloadType() == PAYLOAD_TYPE_ADVERT && flood_hops >= _prefs.flood_max_advert) return false; } return true; } diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index 879fcbf026..e418b9cb61 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -303,7 +303,7 @@ float SensorMesh::getAirtimeBudgetFactor() const { bool SensorMesh::allowPacketForward(const mesh::Packet* packet) { if (_prefs.disable_fwd) return false; - if (packet->isRouteFlood() && packet->getPathHashCount() >= _prefs.flood_max) return false; + if (packet->isRouteFlood() && packet->getPathHashCountExcludingLeadingZeros() >= _prefs.flood_max) return false; return true; } diff --git a/src/Packet.cpp b/src/Packet.cpp index aad3e2f48e..09ac4ecc97 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -34,6 +34,22 @@ uint8_t Packet::copyPath(uint8_t* dest, const uint8_t* src, uint8_t path_len) { return path_len; } +uint8_t Packet::getPathHashCountExcludingLeadingZeros() const { + uint8_t hash_count = getPathHashCount(); + uint8_t hash_size = getPathHashSize(); + if (hash_size > 3) return hash_count; + if (hash_count == 0 || path[0] != 0) return hash_count; + + uint8_t path_byte_len = hash_count * hash_size; + uint8_t zero_bytes = 1; + while (zero_bytes < path_byte_len && path[zero_bytes] == 0) { + zero_bytes++; + } + + uint8_t padding_count = zero_bytes / hash_size; + return hash_count - padding_count; +} + int Packet::getRawLength() const { return 2 + getPathByteLen() + payload_len + (hasTransportCodes() ? 4 : 0); } @@ -84,4 +100,4 @@ bool Packet::readFrom(const uint8_t src[], uint8_t len) { return true; // success } -} \ No newline at end of file +} diff --git a/src/Packet.h b/src/Packet.h index 0886a06c4e..3cd384d777 100644 --- a/src/Packet.h +++ b/src/Packet.h @@ -78,6 +78,7 @@ class Packet { uint8_t getPathHashSize() const { return (path_len >> 6) + 1; } uint8_t getPathHashCount() const { return path_len & 63; } + uint8_t getPathHashCountExcludingLeadingZeros() const; uint8_t getPathByteLen() const { return getPathHashCount() * getPathHashSize(); } void setPathHashCount(uint8_t n) { path_len &= ~63; path_len |= n; } void setPathHashSizeAndCount(uint8_t sz, uint8_t n) { path_len = ((sz - 1) << 6) | (n & 63); }