diff --git a/README.md b/README.md index 4b8dbf3f..82ee311a 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,14 @@ twice a day; everything below is on top of that. Our code is marked `#hashtag` channels), stop forwarding messages of listed sender names (`filter add|del|list`). Names are spoofable: it contains a runaway bot, it is not security. -* **Channel-hash deny-forward** -- `filter hash add `: keyless, works on - private channels too; reception is unaffected, only forwarding. +* **Channel-hash deny-forward** -- `filter hash add [secs]`: keyless, + works on private channels too; reception is unaffected, only forwarding. + With `secs` the channel is throttled (one packet per `secs`) instead of cut. +* **Message age limit** -- `filter age |off` (opt-in, 1-10080 min): do not + forward group texts on the decrypted channels whose sender timestamp is + older; inactive while the repeater clock is unset. It judges the sender's + clock, so companions without a time sync are dropped too -- keep it in days. +* `filter dryrun on` counts every `filter` verdict but forwards anyway. ### Repeater: diagnostics and operations diff --git a/examples/simple_repeater/ChannelCommon.h b/examples/simple_repeater/ChannelCommon.h index e7a60c82..d2b599df 100644 --- a/examples/simple_repeater/ChannelCommon.h +++ b/examples/simple_repeater/ChannelCommon.h @@ -76,6 +76,7 @@ void MyMesh::channelsInit() { memset(known_channel_flags, 0, sizeof(known_channel_flags)); #ifdef FILTER_CHANNEL_PSKS filter_dropped = 0; + filter_age_dropped = 0; { // comma-separated hex PSKs const char* seg = FILTER_CHANNEL_PSKS; @@ -110,7 +111,6 @@ void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Gro uint8_t* data, size_t len) { if (type != PAYLOAD_TYPE_GRP_TXT) return; // GRP_DATA (telemetry etc.) is not ours if (len < 5) return; - if ((data[4] >> 2) != 0) return; // only plain text, per BaseChatMesh.cpp:374 // which registry entry decrypted this? (the caller passes a COPY, not our slot) int chan_idx = -1; @@ -122,6 +122,25 @@ void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Gro } if (chan_idx < 0) return; +#ifdef FILTER_CHANNEL_PSKS + // `filter age` (ChannelFilter.h): any group text, not only plain ones -- the + // timestamp leads every GRP_TXT. Same veto as the deny list below. + if (known_channel_flags[chan_idx] & CHAN_FLAG_FILTER) { + uint32_t sender_ts; + memcpy(&sender_ts, data, 4); + if (msgAgeCheck(sender_ts, getRTCClock()->getCurrentTime(), _prefs.filter_age_min) == MSG_AGE_OLD) { + filter_age_dropped++; + if (!_prefs.filter_dry_run) { + packet->markDoNotRetransmit(); + MESH_DEBUG_PRINTLN("filter: drop fwd, zprava starsi nez %u min", _prefs.filter_age_min); + return; + } + } + } +#endif + + if ((data[4] >> 2) != 0) return; // only plain text, per BaseChatMesh.cpp:374 + // Work on a bounded copy. Upstream NUL-terminates the caller's buffer in place // (data[len] = 0), which is fine there but relies on headroom this function // cannot verify -- and every other parse in this fork is bounds-checked. @@ -152,9 +171,11 @@ void MyMesh::onChannelText(mesh::Packet* packet, int chan_idx, const char* sende // (Mesh.cpp:357). Local processing already happened (rxlog/pktfeed capture // on receive), so the observer evidence trail is intact; the node just // refuses to re-air it. And the bot must not answer a denied sender either. - packet->markDoNotRetransmit(); filter_dropped++; - MESH_DEBUG_PRINTLN("filter: drop fwd, sender na deny listu"); + if (!_prefs.filter_dry_run) { // dry-run: counted, but forwarded + packet->markDoNotRetransmit(); + MESH_DEBUG_PRINTLN("filter: drop fwd, sender na deny listu"); + } return; } #endif diff --git a/examples/simple_repeater/ChannelFilter.h b/examples/simple_repeater/ChannelFilter.h index b8692c0c..530e7e0f 100644 --- a/examples/simple_repeater/ChannelFilter.h +++ b/examples/simple_repeater/ChannelFilter.h @@ -82,7 +82,7 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max return true; } - strcpy(reply, "Err - filter [add|del |list|clear]"); + strcpy(reply, "Err - filter [add|del |list|clear|hash|age|dryrun]"); return true; } @@ -107,8 +107,17 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max // stays intact. // * Runtime-editable over RF admin and persisted in prefs (f_dhash/f_dhash_n), // so an incident response is `filter hash add 43`, not a mast reflash. +// +// THROTTLE (tth #3, 2. 11. 2026): an entry may carry an interval -- +// `filter hash add 43 60` lets one packet of that byte through per 60 s and +// drops the rest, so a chatty channel is tamed instead of cut off. The window +// state is RAM only (first match after boot passes) and only a pass moves it; +// see HashThrottle in src/helpers/ChannelGate.h. Interval 0 = block, as before. +// +// `filter dryrun on` counts every verdict of `filter` (hash, age, sender deny) +// but forwards anyway -- the way to try a new rule on a live mast. -bool MyMesh::filterHashDenied(const mesh::Packet* packet) const { +bool MyMesh::filterHashDenied(const mesh::Packet* packet) { uint8_t n = _prefs.deny_hash_n; if (n == 0) return false; uint8_t type = packet->getPayloadType(); @@ -116,13 +125,25 @@ bool MyMesh::filterHashDenied(const mesh::Packet* packet) const { if (packet->payload_len < 1) return false; if (n > sizeof(_prefs.deny_hash)) n = sizeof(_prefs.deny_hash); for (int i = 0; i < n; i++) { - if (_prefs.deny_hash[i] == packet->payload[0]) return true; + if (_prefs.deny_hash[i] != packet->payload[0]) continue; + uint16_t secs = _prefs.deny_hash_secs[i]; + if (secs != 0 && hash_throttle.pass(i, secs, millis())) { + filter_hash_passed++; + return false; + } + filter_hash_dropped++; + return !_prefs.filter_dry_run; } return false; } -// `filter hash` -- show list + drop counter; `filter hash add|del ` -- -// edit (1 byte, e.g. `43` or `0x43`); `filter hash clear` -- empty the list. +// `filter hash` -- show list + counters (passed = let through by a throttle); +// `filter hash add [secs]` -- block (no secs / 0) or throttle to one +// packet per secs; re-adding a listed byte changes its interval; +// `filter hash del `; `filter hash clear`. +// Listing: `43` = blocked, `43/60` = throttled to one per 60 s. The Ansible +// module (tth.meshcore.meshcore_filter_hash) parses this line -- keep the +// "filter hash: dropped N, passed M, deny:" shape stable. bool MyMesh::filterHashHandleCommand(const char* command, char* reply, int reply_max) { if (memcmp(command, "filter hash", 11) != 0 || (command[11] != 0 && command[11] != ' ')) return false; @@ -133,6 +154,8 @@ bool MyMesh::filterHashHandleCommand(const char* command, char* reply, int reply if (strcmp(arg, "clear") == 0) { _prefs.deny_hash_n = 0; memset(_prefs.deny_hash, 0, sizeof(_prefs.deny_hash)); + memset(_prefs.deny_hash_secs, 0, sizeof(_prefs.deny_hash_secs)); + hash_throttle.reset(); savePrefs(); strcpy(reply, "OK - hash deny list cleared"); return true; @@ -145,35 +168,123 @@ bool MyMesh::filterHashHandleCommand(const char* command, char* reply, int reply if (hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X')) hex += 2; char* end; long v = strtol(hex, &end, 16); - if (end == hex || *end != 0 || end - hex > 2 || v < 0 || v > 0xFF) { + if (end == hex || (*end != 0 && *end != ' ') || end - hex > 2 || v < 0 || v > 0xFF) { strcpy(reply, "Err - 1 byte hex, e.g. 43"); return true; } + long secs = 0; + if (*end == ' ') { + if (is_del) { strcpy(reply, "Err - filter hash del "); return true; } + const char* s = end + 1; + secs = strtol(s, &end, 10); + if (end == s || *end != 0 || secs < 0 || secs > 65535) { + strcpy(reply, "Err - secs 0..65535 (0 = block)"); + return true; + } + } int at = -1; for (int i = 0; i < n; i++) { if (_prefs.deny_hash[i] == (uint8_t)v) { at = i; break; } } if (is_add) { - if (at >= 0) { strcpy(reply, "Err - already listed"); return true; } - if (n >= sizeof(_prefs.deny_hash)) { strcpy(reply, "Err - list full"); return true; } - _prefs.deny_hash[n++] = (uint8_t)v; + if (at >= 0) { + if (_prefs.deny_hash_secs[at] == (uint16_t)secs) { strcpy(reply, "Err - already listed"); return true; } + _prefs.deny_hash_secs[at] = (uint16_t)secs; // change of interval + hash_throttle.reset(at); + } else { + if (n >= sizeof(_prefs.deny_hash)) { strcpy(reply, "Err - list full"); return true; } + _prefs.deny_hash[n] = (uint8_t)v; + _prefs.deny_hash_secs[n] = (uint16_t)secs; + hash_throttle.reset(n); + n++; + } } else { if (at < 0) { strcpy(reply, "Err - not listed"); return true; } - for (int i = at; i + 1 < n; i++) _prefs.deny_hash[i] = _prefs.deny_hash[i + 1]; - _prefs.deny_hash[--n] = 0; + for (int i = at; i + 1 < n; i++) { + _prefs.deny_hash[i] = _prefs.deny_hash[i + 1]; + _prefs.deny_hash_secs[i] = _prefs.deny_hash_secs[i + 1]; + } + hash_throttle.remove(at); + --n; + _prefs.deny_hash[n] = 0; + _prefs.deny_hash_secs[n] = 0; } _prefs.deny_hash_n = n; savePrefs(); // fall through to the listing as the reply } else if (arg[0] != 0 && strcmp(arg, "list") != 0) { - strcpy(reply, "Err - filter hash [add|del |list|clear]"); + strcpy(reply, "Err - filter hash [add [secs]|del |list|clear]"); return true; } - int len = snprintf(reply, reply_max, "filter hash: dropped %u, deny:", filter_hash_dropped); - if (n == 0 && len < reply_max) snprintf(&reply[len], reply_max - len, " (nic)"); + int len = snprintf(reply, reply_max, "filter hash: dropped %u, passed %u, deny:", + filter_hash_dropped, filter_hash_passed); + if (n == 0 && len < reply_max) len += snprintf(&reply[len], reply_max - len, " (nic)"); for (int i = 0; i < n && len < reply_max; i++) { - len += snprintf(&reply[len], reply_max - len, " %02X", _prefs.deny_hash[i]); + if (_prefs.deny_hash_secs[i]) { + len += snprintf(&reply[len], reply_max - len, " %02X/%u", _prefs.deny_hash[i], _prefs.deny_hash_secs[i]); + } else { + len += snprintf(&reply[len], reply_max - len, " %02X", _prefs.deny_hash[i]); + } } return true; } + +// `filter dryrun [on|off]` -- verdicts of `filter` (hash, age, sender deny) +// are counted but the packet is forwarded anyway. Persisted (f_dry). +// `filter age [|off]` -- refuse to forward a group text the node can +// DECRYPT (channels of FILTER_CHANNEL_PSKS) whose sender timestamp is older +// than minutes by THIS node's clock (1..10080). Off by default. +// * It judges the sender's clock: a companion that never got a time sync +// stamps 2024 and is dropped. On the CZ mesh 2. 11. 2026 every one of the +// 5/2026 messages older than 10 min was such a clock, none a reflood -- +// hence opt-in, and days rather than minutes. +// * Inactive while our own clock reads before 2026 (RTC-less boot). A clock +// running AHEAD is not detected and makes everything look old -- check the +// drift (tools/fw_status.py) before turning it on. +bool MyMesh::filterMiscHandleCommand(const char* command, char* reply, int reply_max) { + if (strcmp(command, "filter dryrun") == 0 || strcmp(command, "filter dryrun on") == 0 + || strcmp(command, "filter dryrun off") == 0) { + if (command[13] == ' ') { + _prefs.filter_dry_run = (command[15] == 'n') ? 1 : 0; + savePrefs(); + snprintf(reply, reply_max, "OK - filter dry-run %s", _prefs.filter_dry_run ? "on" : "off"); + } else { + snprintf(reply, reply_max, "filter dry-run %s", _prefs.filter_dry_run ? "on" : "off"); + } + return true; + } + + if (memcmp(command, "filter age", 10) != 0 || (command[10] != 0 && command[10] != ' ')) return false; +#ifndef FILTER_CHANNEL_PSKS + strcpy(reply, "Err - no channel keys in this build (FILTER_CHANNEL_PSKS)"); + return true; +#else + const char* arg = (command[10] == ' ') ? &command[11] : ""; + bool changed = false; + if (strcmp(arg, "off") == 0) { + _prefs.filter_age_min = 0; + changed = true; + } else if (arg[0] != 0) { + char* end; + long v = strtol(arg, &end, 10); + if (end == arg || *end != 0 || v < 0 || v > MSG_AGE_MAX_MINUTES) { + snprintf(reply, reply_max, "Err - filter age <1..%d min>|off", MSG_AGE_MAX_MINUTES); + return true; + } + _prefs.filter_age_min = (uint16_t)v; + changed = true; + } + if (changed) savePrefs(); + + if (_prefs.filter_age_min == 0) { + snprintf(reply, reply_max, "%sfilter age off, dropped %u", changed ? "OK - " : "", filter_age_dropped); + } else { + uint32_t now = getRTCClock()->getCurrentTime(); + snprintf(reply, reply_max, "%sfilter age %um%s%s, dropped %u", changed ? "OK - " : "", + _prefs.filter_age_min, _prefs.filter_dry_run ? " DRY-RUN" : "", + now < MSG_AGE_CLOCK_VALID_FROM ? " (clock not set, inactive)" : "", filter_age_dropped); + } + return true; +#endif +} diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 499d2d70..4f188477 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -699,14 +699,6 @@ void MyMesh::sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, ui bool MyMesh::allowPacketForward(const mesh::Packet *packet) { if (_prefs.disable_fwd) return false; - // CUSTOM (TeTeHacko): keyless channel-hash deny list (`filter hash`). Before - // takeForwardingRateLimit() in routeRecvPacket(), so a vetoed packet does not - // eat FairnessLimiter tokens either. - if (filterHashDenied(packet)) { - filter_hash_dropped++; - MESH_DEBUG_PRINTLN("allowPacketForward: channel hash %02X on deny list", packet->payload[0]); - return false; - } if (packet->isRouteFlood() && mesh::isFloodHopLimitExceeded(packet, _prefs.flood_max, _prefs.flood_max_unscoped, _prefs.flood_max_advert)) { return false; @@ -729,6 +721,14 @@ bool MyMesh::allowPacketForward(const mesh::Packet *packet) { return false; } } + // CUSTOM (TeTeHacko): keyless channel-hash deny list / throttle (`filter hash`). + // LAST of the checks here, so a throttle pass is only spent on a packet that + // would otherwise go out; and before takeForwardingRateLimit() in + // routeRecvPacket(), so a vetoed packet does not eat FairnessLimiter tokens. + if (filterHashDenied(packet)) { + MESH_DEBUG_PRINTLN("allowPacketForward: channel hash %02X on deny list", packet->payload[0]); + return false; + } return true; } @@ -1405,6 +1405,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc #endif _prefs.fairness_enabled = FAIRNESS_ENABLED_DEFAULT; filter_hash_dropped = 0; + filter_hash_passed = 0; _prefs.interference_threshold = 0; // disabled _prefs.cad_enabled = 0; // hardware CAD before TX (off by default; 'set cad on') _prefs.loop_detect = LOOP_DETECT_MINIMAL; @@ -1845,6 +1846,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply // CUSTOM (TeTeHacko): `filter hash [add|del |clear]` — keyless channel-hash // deny list. Before filterHandleCommand(), which would claim any `filter ...`. if (filterHashHandleCommand(command, reply, reply_max)) return; + // CUSTOM (TeTeHacko): `filter dryrun [on|off]`, `filter age [|off]`. Also + // before filterHandleCommand(), which would take `dryrun`/`age` for names. + if (filterMiscHandleCommand(command, reply, reply_max)) return; #ifdef FILTER_CHANNEL_PSKS // CUSTOM (TeTeHacko): `filter add|del|list|clear` — runtime deny list for // channel messages this repeater refuses to forward. See ChannelFilter.h. diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 9b7aaafd..598006ac 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include "RateLimiter.h" @@ -210,6 +211,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { // upstream proposal #1502). Kill switch: _prefs.fairness_enabled. FairnessLimiter fairness_limiter; uint32_t filter_hash_dropped; // forwards vetoed by the channel-hash deny list + uint32_t filter_hash_passed; // throttled entries: forwards let through within budget + HashThrottle hash_throttle; // per-entry windows of `filter hash add ` unsigned long next_fair_sender_normal_refill, next_fair_sender_low_refill, next_fair_group_refill; uint32_t pending_discover_tag; unsigned long pending_discover_until; @@ -249,6 +252,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #endif #ifdef FILTER_CHANNEL_PSKS uint32_t filter_dropped; // forwards vetoed by the deny list + uint32_t filter_age_dropped; // forwards vetoed by `filter age` #endif #ifdef BOT_CHANNEL_PSK // Default zde, ne az v ChannelBot.h -- ten se includuje na konci MyMesh.cpp, @@ -323,8 +327,10 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { // CUSTOM (TeTeHacko): `fairness [on|off|grid|cap ...|refill ...]` CLI. Defined in MyMesh.cpp. bool fairnessHandleCommand(const char* command, char* reply, int reply_max); // CUSTOM (TeTeHacko): keyless deny-forward by channel hash. Definitions in ChannelFilter.h. - bool filterHashDenied(const mesh::Packet* packet) const; + bool filterHashDenied(const mesh::Packet* packet); bool filterHashHandleCommand(const char* command, char* reply, int reply_max); + // `filter dryrun [on|off]` and `filter age [|off]`. Definitions in ChannelFilter.h. + bool filterMiscHandleCommand(const char* command, char* reply, int reply_max); // Refill interval in ms: persisted pref (seconds) if set, else the build default. uint32_t fairGroupRefillMs() const { return _prefs.fair_group_refill_s ? (uint32_t)_prefs.fair_group_refill_s * 1000 : FAIRNESS_GROUP_REFILL_MS; diff --git a/src/helpers/ChannelGate.h b/src/helpers/ChannelGate.h new file mode 100644 index 00000000..6cb927fb --- /dev/null +++ b/src/helpers/ChannelGate.h @@ -0,0 +1,84 @@ +#pragma once + +// CUSTOM (TeTeHacko): the two decisions behind `filter hash add [secs]` +// (throttle a channel byte instead of muting it) and `filter age |off` +// (refuse to re-air group texts stamped long ago). Header-only and free of +// mesh/Arduino types so both run in the native tests; the glue that feeds them +// packets lives in examples/simple_repeater/ChannelFilter.h. Issue tth #3. +// +// Both ideas come from DutchMeshCore (`filter sender add [secs]`, +// 18341f13; `filter age`, 89dbb2bd). Behaviour follows theirs where it matters: +// an over-rate message does not extend the throttle window, the first match +// after a reboot is a free pass (state is RAM only), the age limit is inactive +// while the repeater clock is unset, and a message stamped in the future is +// never old. + +#include +#include + +#define HASH_THROTTLE_SLOTS 8 // = sizeof(NodePrefs::deny_hash) + +// One throttle per deny-list slot. Slots follow the prefs list order, so the +// caller must remove() when it deletes an entry, or later entries would +// inherit the wrong window. +class HashThrottle { + uint32_t _last_ms[HASH_THROTTLE_SLOTS]; + uint8_t _armed[HASH_THROTTLE_SLOTS]; // 0 = nothing passed since boot/reset + +public: + HashThrottle() { reset(); } + + void reset() { + memset(_last_ms, 0, sizeof(_last_ms)); + memset(_armed, 0, sizeof(_armed)); + } + + void reset(int slot) { + if (slot < 0 || slot >= HASH_THROTTLE_SLOTS) return; + _last_ms[slot] = 0; + _armed[slot] = 0; + } + + // Close the gap after the prefs list dropped `slot` (entries above shift down). + void remove(int slot) { + if (slot < 0 || slot >= HASH_THROTTLE_SLOTS) return; + for (int i = slot; i + 1 < HASH_THROTTLE_SLOTS; i++) { + _last_ms[i] = _last_ms[i + 1]; + _armed[i] = _armed[i + 1]; + } + reset(HASH_THROTTLE_SLOTS - 1); + } + + // true = this packet may be forwarded. secs 0 = blocked outright. Only a + // pass moves the window, so a sender pushing hard still gets exactly one + // message per `secs`. Unsigned subtraction survives the millis() wrap. + bool pass(int slot, uint16_t secs, uint32_t now_ms) { + if (secs == 0 || slot < 0 || slot >= HASH_THROTTLE_SLOTS) return false; + if (_armed[slot] && (uint32_t)(now_ms - _last_ms[slot]) < (uint32_t)secs * 1000u) return false; + _armed[slot] = 1; + _last_ms[slot] = now_ms; + return true; + } +}; + +// A repeater without RTC or GPS boots at 15 May 2024 (VolatileRTCClock) and +// would call every live message old. Anything before 2026-01-01 = clock unset. +#define MSG_AGE_CLOCK_VALID_FROM 1767225600UL +#define MSG_AGE_MAX_MINUTES 10080 // one week, as in DMC + +enum MsgAgeVerdict { + MSG_AGE_OFF, // limit not set + MSG_AGE_CLOCK_UNSET, // limit set, but our clock cannot be trusted -> pass + MSG_AGE_FRESH, + MSG_AGE_OLD, +}; + +// sender_ts is the 4-byte timestamp at the start of a decrypted group text. +// It is the SENDER's clock: a companion running behind looks "old" here, which +// is why the limit is opt-in and meant to be generous (days, not minutes). +inline MsgAgeVerdict msgAgeCheck(uint32_t sender_ts, uint32_t now, uint16_t max_minutes) { + if (max_minutes == 0) return MSG_AGE_OFF; + if (now < MSG_AGE_CLOCK_VALID_FROM) return MSG_AGE_CLOCK_UNSET; + if (sender_ts >= now) return MSG_AGE_FRESH; + return (now - sender_ts > (uint32_t)max_minutes * 60u) ? MSG_AGE_OLD : MSG_AGE_FRESH; +} diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 95d0eac7..d45498f9 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -81,6 +81,9 @@ public: // keyless -- works on private channels too. See ChannelFilter.h (`filter hash`). uint8_t deny_hash[8]; uint8_t deny_hash_n = 0; + uint16_t deny_hash_secs[8]; // per entry: 0 = block, else pass one per N s (throttle) + uint16_t filter_age_min = 0; // drop decrypted group texts older than this; 0 = off + uint8_t filter_dry_run = 0; // count `filter` verdicts, forward anyway // Battery state as an icon on the advertised name (see BatteryIcon.h). // Off by default: it costs bytes of the name, so it has to be asked for. uint8_t batt_icon = 0; // boolean @@ -212,6 +215,9 @@ private: def("f_deny", _parent->filter_deny, sizeof(_parent->filter_deny)); def("f_dhash", (void*)_parent->deny_hash, sizeof(_parent->deny_hash)); def("f_dhash_n", _parent->deny_hash_n); + def("f_dhash_s", (void*)_parent->deny_hash_secs, sizeof(_parent->deny_hash_secs)); + def("f_age", _parent->filter_age_min); + def("f_dry", _parent->filter_dry_run); def("fair_gcap", _parent->fair_group_cap); def("fair_scap", _parent->fair_sender_cap); def("fair_acap", _parent->fair_advert_cap); @@ -267,6 +273,7 @@ public: owner_info[0] = 0; filter_deny[0] = 0; memset(deny_hash, 0, sizeof(deny_hash)); + memset(deny_hash_secs, 0, sizeof(deny_hash_secs)); } CommonRadioPrefs* getRadioPrefs() { return &radio; } diff --git a/test/test_channel_gate/test_channel_gate.cpp b/test/test_channel_gate/test_channel_gate.cpp new file mode 100644 index 00000000..72f402e1 --- /dev/null +++ b/test/test_channel_gate/test_channel_gate.cpp @@ -0,0 +1,102 @@ +#include +#include "helpers/ChannelGate.h" + +// ── HashThrottle ───────────────────────────────────────────────────────────── + +TEST(HashThrottle, ZeroSecsBlocksEverything) { + HashThrottle t; + for (uint32_t ms = 0; ms < 10 * 60 * 1000; ms += 30 * 1000) EXPECT_FALSE(t.pass(0, 0, ms)); +} + +TEST(HashThrottle, FirstMatchAfterBootPasses) { + HashThrottle t; + EXPECT_TRUE(t.pass(0, 60, 123456)); + EXPECT_FALSE(t.pass(0, 60, 123457)); +} + +TEST(HashThrottle, OnePassPerWindow) { + HashThrottle t; + EXPECT_TRUE(t.pass(0, 60, 0 + 1)); + EXPECT_FALSE(t.pass(0, 60, 59999)); + EXPECT_TRUE(t.pass(0, 60, 60001)); + EXPECT_FALSE(t.pass(0, 60, 60002)); +} + +TEST(HashThrottle, OverRateDoesNotExtendWindow) { + HashThrottle t; + EXPECT_TRUE(t.pass(0, 60, 1000)); + // hammering inside the window must not push the next pass further out + for (uint32_t ms = 2000; ms < 61000; ms += 1000) EXPECT_FALSE(t.pass(0, 60, ms)); + EXPECT_TRUE(t.pass(0, 60, 61000)); +} + +TEST(HashThrottle, SlotsAreIndependent) { + HashThrottle t; + EXPECT_TRUE(t.pass(0, 60, 1000)); + EXPECT_TRUE(t.pass(1, 60, 1001)); + EXPECT_FALSE(t.pass(0, 60, 1002)); + EXPECT_FALSE(t.pass(1, 60, 1003)); +} + +TEST(HashThrottle, RemoveShiftsLaterSlotsDown) { + HashThrottle t; + EXPECT_TRUE(t.pass(0, 60, 1000)); // slot 0 used + EXPECT_TRUE(t.pass(1, 60, 1000)); // slot 1 used + t.remove(0); // old slot 1 is now slot 0 + EXPECT_FALSE(t.pass(0, 60, 2000)); // still inside ITS window + EXPECT_TRUE(t.pass(1, 60, 2000)); // freed slot starts fresh +} + +TEST(HashThrottle, SurvivesMillisWrap) { + HashThrottle t; + EXPECT_TRUE(t.pass(0, 60, 0xFFFFFF00u)); + EXPECT_FALSE(t.pass(0, 60, 0x00000100u)); // 512 ms later, across the wrap + EXPECT_TRUE(t.pass(0, 60, 0xFFFFFF00u + 60000u)); // wraps to 60 s later +} + +TEST(HashThrottle, BadSlotNeverPasses) { + HashThrottle t; + EXPECT_FALSE(t.pass(-1, 60, 0)); + EXPECT_FALSE(t.pass(HASH_THROTTLE_SLOTS, 60, 0)); +} + +// ── msgAgeCheck ────────────────────────────────────────────────────────────── + +static const uint32_t NOW = 1793625936; // 2026-11-02 13:25 UTC + +TEST(MsgAge, OffWhenZero) { + EXPECT_EQ(msgAgeCheck(0, NOW, 0), MSG_AGE_OFF); +} + +TEST(MsgAge, InactiveWhileClockUnset) { + // RTC-less repeater after boot: 15 May 2024 + EXPECT_EQ(msgAgeCheck(1715770351, 1715770351 + 3600, 60), MSG_AGE_CLOCK_UNSET); + EXPECT_EQ(msgAgeCheck(0, MSG_AGE_CLOCK_VALID_FROM - 1, 60), MSG_AGE_CLOCK_UNSET); +} + +TEST(MsgAge, FreshAndOldAroundTheLimit) { + EXPECT_EQ(msgAgeCheck(NOW - 60 * 60, NOW, 60), MSG_AGE_FRESH); // exactly the limit + EXPECT_EQ(msgAgeCheck(NOW - 60 * 60 - 1, NOW, 60), MSG_AGE_OLD); +} + +TEST(MsgAge, FutureIsNeverOld) { + EXPECT_EQ(msgAgeCheck(NOW + 8 * 3600, NOW, 1), MSG_AGE_FRESH); + EXPECT_EQ(msgAgeCheck(0xFFFFFFFFu, NOW, 1), MSG_AGE_FRESH); +} + +TEST(MsgAge, SenderWithUnsetClockIsOld) { + // companion that never got a time sync stamps 2024 -- that IS what the + // limit drops; documented trade-off, measured on the CZ mesh 2. 11. 2026 + EXPECT_EQ(msgAgeCheck(1715770351, NOW, MSG_AGE_MAX_MINUTES), MSG_AGE_OLD); + EXPECT_EQ(msgAgeCheck(0, NOW, MSG_AGE_MAX_MINUTES), MSG_AGE_OLD); +} + +TEST(MsgAge, WeekLimitDoesNotOverflow) { + EXPECT_EQ(msgAgeCheck(NOW - 10080u * 60u, NOW, MSG_AGE_MAX_MINUTES), MSG_AGE_FRESH); + EXPECT_EQ(msgAgeCheck(NOW - 10080u * 60u - 1, NOW, MSG_AGE_MAX_MINUTES), MSG_AGE_OLD); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}