Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions examples/simple_repeater/MyMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,21 @@ static uint8_t max_loop_strict[] = { 0, /* 1-byte */ 1, /* 2-byte */ 1, /* 3

bool MyMesh::isLooped(const mesh::Packet* packet, const uint8_t max_counters[]) {
uint8_t hash_size = packet->getPathHashSize();
uint8_t hash_count = packet->getPathHashCount();
uint8_t n = 0;
uint8_t path_byte_len = packet->getPathByteLen();
uint8_t limit = max_counters[hash_size];
const uint8_t* path = packet->path;
while (hash_count > 0) { // count how many times this node is already in the path
if (self_id.isHashMatch(path, hash_size)) n++;
hash_count--;
path += hash_size;

for (uint8_t i = 0; i < path_byte_len; i += hash_size) {
uint8_t n = 0;

if (self_id.isHashMatch(&path[i], hash_size)) n++;

for (uint8_t j = i + hash_size; j < path_byte_len; j += hash_size) {
if (memcmp(&path[i], &path[j], hash_size) == 0) n++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loop detection will treat any repeated path hash segment as a loop, so equal 1-byte hashes can be considered loops if they collide and will cause healthy packets to be dropped. Is that okay?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's been the case since loop detection was added, tho the check and false positive rate is lower without this change because it only triggered on collisions with the self-id.

There are different thresholds for loop detection,

These two tests are passing with original code and my change. An ID has to appear 4 times, and match your ID to be dropped.

runTest(1, {0xAA, 0xAA, 0xAA}, max_loop_minimal, false, "3 repeats and self: under minimal limit");
runTest(1, {0xAA, 0xAA, 0xAA, 0xAA}, max_loop_minimal, true, "4 repeats and self: over minimal limit");

These tests pass with my change, the id must appear 5 times.

runTest(1, {0x11, 0x11, 0x11, 0x11}, max_loop_minimal, false, "4 repeats: under minimal limit");
runTest(1, {0x11, 0x11, 0x11, 0x11, 0x11}, max_loop_minimal, true, "5 repeats: over minimal limit");

Assuming random distribution of node ids there's a 0.15% chance to drop a full 64 hop packet.

About 0.15% — roughly 1 in 664 chance that when you pick 64 random numbers from 1–254, at least one
number shows up 5 or more times.

}
if (n >= limit) return true;
}
return n >= max_counters[hash_size];
return false;
}

bool MyMesh::allowPacketForward(const mesh::Packet *packet) {
Expand Down