85 lines
3.3 KiB
C++
85 lines
3.3 KiB
C++
#pragma once
|
|
|
|
// CUSTOM (TeTeHacko): the two decisions behind `filter hash add <hex> [secs]`
|
|
// (throttle a channel byte instead of muting it) and `filter age <min>|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 <name> [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 <stdint.h>
|
|
#include <string.h>
|
|
|
|
#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;
|
|
}
|