85 lines
3.3 KiB
C++
85 lines
3.3 KiB
C++
#pragma once
|
|
|
|
// CUSTOM (TeTeHacko): per-origin advert window, ported from DutchMeshCore's
|
|
// AdvertLimiter (Dutch-MeshCore/MeshCore d26421b0, examples/simple_repeater/
|
|
// AdvertLimiter.h; same MIT licence as this tree). Behaviour is theirs, names
|
|
// and the eviction counter are ours.
|
|
//
|
|
// Each origin's flood advert is forwarded at most once per window, so a node
|
|
// that re-advertises every minute (CP1 on the CZ mesh, since 30. 10. 2026:
|
|
// a flood advert every 60 s, seen by 40-90 observers each) stops being
|
|
// re-flooded by this repeater while it still stays reachable through it once
|
|
// per window. FairnessLimiter's advert bucket cannot do that:
|
|
// it is keyed on 7 bits of the key, so the chatty node and ~8 innocent ones
|
|
// share a bucket, and loosening it for them lets the chatty one through.
|
|
//
|
|
// Origins are keyed on the first 4 bytes of the advert's public key (payload
|
|
// starts with it). Fixed ring of ADVERT_WINDOW_SIZE entries: once full, the
|
|
// oldest entry is overwritten -- that origin gets a free pass next time. RAM
|
|
// only, so after a reboot every origin gets one free pass. Time is millis and
|
|
// compared with unsigned subtraction, so it survives the 49-day wrap.
|
|
// Header-only and dependency-free so it runs in the native tests.
|
|
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#ifndef ADVERT_WINDOW_SIZE
|
|
#define ADVERT_WINDOW_SIZE 256 // 8 B each => 2 KB RAM
|
|
#endif
|
|
#define ADVERT_WINDOW_KEY_LEN 4
|
|
#define ADVERT_WINDOW_MAX_HOURS 720 // 30 days; keeps hours*3600*1000 inside uint32
|
|
|
|
class AdvertWindow {
|
|
struct Entry {
|
|
uint8_t key[ADVERT_WINDOW_KEY_LEN];
|
|
uint32_t seen_ms;
|
|
};
|
|
|
|
Entry _cache[ADVERT_WINDOW_SIZE];
|
|
uint16_t _count = 0; // entries in use
|
|
uint16_t _head = 0; // next entry to overwrite once the cache is full
|
|
uint16_t _hours = 0; // 0 = off
|
|
uint32_t _evicted = 0; // origins forgotten because the ring was full
|
|
|
|
public:
|
|
AdvertWindow() { memset(_cache, 0, sizeof(_cache)); }
|
|
|
|
void setHours(uint16_t hours) { _hours = hours > ADVERT_WINDOW_MAX_HOURS ? ADVERT_WINDOW_MAX_HOURS : hours; }
|
|
uint16_t hours() const { return _hours; }
|
|
int count() const { return _count; }
|
|
int capacity() const { return ADVERT_WINDOW_SIZE; }
|
|
// Non-zero = the ring is too small for the mesh: origins fall out before
|
|
// their window ends and get a pass they should not have had.
|
|
uint32_t evicted() const { return _evicted; }
|
|
|
|
void clear() { _count = 0; _head = 0; }
|
|
|
|
// True when the advert from `key` may be forwarded now (and records it),
|
|
// false when the same origin already passed inside the current window.
|
|
bool allow(const uint8_t* key, uint32_t now_ms) {
|
|
if (_hours == 0 || key == nullptr) return true;
|
|
|
|
uint32_t window_ms = (uint32_t)_hours * 3600UL * 1000UL;
|
|
|
|
for (int i = 0; i < _count; i++) {
|
|
Entry& e = _cache[i];
|
|
if (memcmp(e.key, key, ADVERT_WINDOW_KEY_LEN) != 0) continue;
|
|
if (now_ms - e.seen_ms < window_ms) return false; // unsigned: wrap-safe
|
|
e.seen_ms = now_ms; // window elapsed: pass, and start a new window
|
|
return true;
|
|
}
|
|
|
|
Entry* e;
|
|
if (_count < ADVERT_WINDOW_SIZE) {
|
|
e = &_cache[_count++];
|
|
} else {
|
|
e = &_cache[_head]; // overwrite the oldest entry
|
|
_head = (uint16_t)((_head + 1) % ADVERT_WINDOW_SIZE);
|
|
_evicted++;
|
|
}
|
|
memcpy(e->key, key, ADVERT_WINDOW_KEY_LEN);
|
|
e->seen_ms = now_ms;
|
|
return true;
|
|
}
|
|
};
|