mirror of https://github.com/meshcore-dev/MeshCore
20 changed files with 821 additions and 22 deletions
@ -0,0 +1,23 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <stdint.h> |
||||
|
|
||||
|
class RateLimiter { |
||||
|
uint32_t _start_timestamp; |
||||
|
uint32_t _secs; |
||||
|
uint16_t _maximum, _count; |
||||
|
|
||||
|
public: |
||||
|
RateLimiter(uint16_t maximum, uint32_t secs): _maximum(maximum), _secs(secs), _start_timestamp(0), _count(0) { } |
||||
|
|
||||
|
bool allow(uint32_t now) { |
||||
|
if (now < _start_timestamp + _secs) { |
||||
|
_count++; |
||||
|
if (_count > _maximum) return false; // deny
|
||||
|
} else { // time window now expired
|
||||
|
_start_timestamp = now; |
||||
|
_count = 1; |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
}; |
||||
@ -0,0 +1,237 @@ |
|||||
|
#include "RegionMap.h" |
||||
|
#include <helpers/TxtDataHelpers.h> |
||||
|
#include <SHA256.h> |
||||
|
|
||||
|
RegionMap::RegionMap(TransportKeyStore& store) : _store(&store) { |
||||
|
next_id = 1; num_regions = 0; home_id = 0; |
||||
|
wildcard.id = wildcard.parent = 0; |
||||
|
wildcard.flags = 0; // default behaviour, allow flood and direct
|
||||
|
strcpy(wildcard.name, "*"); |
||||
|
} |
||||
|
|
||||
|
bool RegionMap::is_name_char(char c) { |
||||
|
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.' || c == '_' || c == '#'; |
||||
|
} |
||||
|
|
||||
|
static File openWrite(FILESYSTEM* _fs, const char* filename) { |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
_fs->remove(filename); |
||||
|
return _fs->open(filename, FILE_O_WRITE); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return _fs->open(filename, "w"); |
||||
|
#else |
||||
|
return _fs->open(filename, "w", true); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
bool RegionMap::load(FILESYSTEM* _fs) { |
||||
|
if (_fs->exists("/regions2")) { |
||||
|
#if defined(RP2040_PLATFORM) |
||||
|
File file = _fs->open("/regions2", "r"); |
||||
|
#else |
||||
|
File file = _fs->open("/regions2"); |
||||
|
#endif |
||||
|
|
||||
|
if (file) { |
||||
|
uint8_t pad[128]; |
||||
|
|
||||
|
num_regions = 0; next_id = 1; home_id = 0; |
||||
|
|
||||
|
bool success = file.read(pad, 5) == 5; // reserved header
|
||||
|
success = success && file.read((uint8_t *) &home_id, sizeof(home_id)) == sizeof(home_id); |
||||
|
success = success && file.read((uint8_t *) &wildcard.flags, sizeof(wildcard.flags)) == sizeof(wildcard.flags); |
||||
|
success = success && file.read((uint8_t *) &next_id, sizeof(next_id)) == sizeof(next_id); |
||||
|
|
||||
|
if (success) { |
||||
|
while (num_regions < MAX_REGION_ENTRIES) { |
||||
|
auto r = ®ions[num_regions]; |
||||
|
|
||||
|
success = file.read((uint8_t *) &r->id, sizeof(r->id)) == sizeof(r->id); |
||||
|
success = success && file.read((uint8_t *) &r->parent, sizeof(r->parent)) == sizeof(r->parent); |
||||
|
success = success && file.read((uint8_t *) r->name, sizeof(r->name)) == sizeof(r->name); |
||||
|
success = success && file.read((uint8_t *) &r->flags, sizeof(r->flags)) == sizeof(r->flags); |
||||
|
success = success && file.read(pad, sizeof(pad)) == sizeof(pad); |
||||
|
|
||||
|
if (!success) break; // EOF
|
||||
|
|
||||
|
if (r->id >= next_id) { // make sure next_id is valid
|
||||
|
next_id = r->id + 1; |
||||
|
} |
||||
|
num_regions++; |
||||
|
} |
||||
|
} |
||||
|
file.close(); |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
return false; // failed
|
||||
|
} |
||||
|
|
||||
|
bool RegionMap::save(FILESYSTEM* _fs) { |
||||
|
File file = openWrite(_fs, "/regions2"); |
||||
|
if (file) { |
||||
|
uint8_t pad[128]; |
||||
|
memset(pad, 0, sizeof(pad)); |
||||
|
|
||||
|
bool success = file.write(pad, 5) == 5; // reserved header
|
||||
|
success = success && file.write((uint8_t *) &home_id, sizeof(home_id)) == sizeof(home_id); |
||||
|
success = success && file.write((uint8_t *) &wildcard.flags, sizeof(wildcard.flags)) == sizeof(wildcard.flags); |
||||
|
success = success && file.write((uint8_t *) &next_id, sizeof(next_id)) == sizeof(next_id); |
||||
|
|
||||
|
if (success) { |
||||
|
for (int i = 0; i < num_regions; i++) { |
||||
|
auto r = ®ions[i]; |
||||
|
|
||||
|
success = file.write((uint8_t *) &r->id, sizeof(r->id)) == sizeof(r->id); |
||||
|
success = success && file.write((uint8_t *) &r->parent, sizeof(r->parent)) == sizeof(r->parent); |
||||
|
success = success && file.write((uint8_t *) r->name, sizeof(r->name)) == sizeof(r->name); |
||||
|
success = success && file.write((uint8_t *) &r->flags, sizeof(r->flags)) == sizeof(r->flags); |
||||
|
success = success && file.write(pad, sizeof(pad)) == sizeof(pad); |
||||
|
if (!success) break; // write failed
|
||||
|
} |
||||
|
} |
||||
|
file.close(); |
||||
|
return true; |
||||
|
} |
||||
|
return false; // failed
|
||||
|
} |
||||
|
|
||||
|
RegionEntry* RegionMap::putRegion(const char* name, uint16_t parent_id, uint16_t id) { |
||||
|
const char* sp = name; // check for illegal name chars
|
||||
|
while (*sp) { |
||||
|
if (!is_name_char(*sp)) return NULL; // error
|
||||
|
sp++; |
||||
|
} |
||||
|
|
||||
|
auto region = findByName(name); |
||||
|
if (region) { |
||||
|
if (region->id == parent_id) return NULL; // ERROR: invalid parent!
|
||||
|
|
||||
|
region->parent = parent_id; // re-parent / move this region in the hierarchy
|
||||
|
} else { |
||||
|
if (id == 0 && num_regions >= MAX_REGION_ENTRIES) return NULL; // full!
|
||||
|
|
||||
|
region = ®ions[num_regions++]; // alloc new RegionEntry
|
||||
|
region->flags = REGION_DENY_FLOOD; // DENY by default
|
||||
|
region->id = id == 0 ? next_id++ : id; |
||||
|
StrHelper::strncpy(region->name, name, sizeof(region->name)); |
||||
|
region->parent = parent_id; |
||||
|
} |
||||
|
return region; |
||||
|
} |
||||
|
|
||||
|
RegionEntry* RegionMap::findMatch(mesh::Packet* packet, uint8_t mask) { |
||||
|
for (int i = 0; i < num_regions; i++) { |
||||
|
auto region = ®ions[i]; |
||||
|
if ((region->flags & mask) == 0) { // does region allow this? (per 'mask' param)
|
||||
|
TransportKey keys[4]; |
||||
|
int num; |
||||
|
if (region->name[0] == '#') { // auto hashtag region
|
||||
|
_store->getAutoKeyFor(region->id, region->name, keys[0]); |
||||
|
num = 1; |
||||
|
} else { |
||||
|
num = _store->loadKeysFor(region->id, keys, 4); |
||||
|
} |
||||
|
for (int j = 0; j < num; j++) { |
||||
|
uint16_t code = keys[j].calcTransportCode(packet); |
||||
|
if (packet->transport_codes[0] == code) { // a match!!
|
||||
|
return region; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return NULL; // no matches
|
||||
|
} |
||||
|
|
||||
|
RegionEntry* RegionMap::findByName(const char* name) { |
||||
|
if (strcmp(name, "*") == 0) return &wildcard; |
||||
|
|
||||
|
for (int i = 0; i < num_regions; i++) { |
||||
|
auto region = ®ions[i]; |
||||
|
if (strcmp(name, region->name) == 0) return region; |
||||
|
} |
||||
|
return NULL; // not found
|
||||
|
} |
||||
|
|
||||
|
RegionEntry* RegionMap::findByNamePrefix(const char* prefix) { |
||||
|
if (strcmp(prefix, "*") == 0) return &wildcard; |
||||
|
|
||||
|
RegionEntry* partial = NULL; |
||||
|
for (int i = 0; i < num_regions; i++) { |
||||
|
auto region = ®ions[i]; |
||||
|
if (strcmp(prefix, region->name) == 0) return region; // is a complete match, preference this one
|
||||
|
if (memcmp(prefix, region->name, strlen(prefix)) == 0) { |
||||
|
partial = region; |
||||
|
} |
||||
|
} |
||||
|
return partial; |
||||
|
} |
||||
|
|
||||
|
RegionEntry* RegionMap::findById(uint16_t id) { |
||||
|
if (id == 0) return &wildcard; // special root Region
|
||||
|
|
||||
|
for (int i = 0; i < num_regions; i++) { |
||||
|
auto region = ®ions[i]; |
||||
|
if (region->id == id) return region; |
||||
|
} |
||||
|
return NULL; // not found
|
||||
|
} |
||||
|
|
||||
|
RegionEntry* RegionMap::getHomeRegion() { |
||||
|
return findById(home_id); |
||||
|
} |
||||
|
|
||||
|
void RegionMap::setHomeRegion(const RegionEntry* home) { |
||||
|
home_id = home ? home->id : 0; |
||||
|
} |
||||
|
|
||||
|
bool RegionMap::removeRegion(const RegionEntry& region) { |
||||
|
if (region.id == 0) return false; // failed (cannot remove the wildcard Region)
|
||||
|
|
||||
|
int i; // first check region has no child regions
|
||||
|
for (i = 0; i < num_regions; i++) { |
||||
|
if (regions[i].parent == region.id) return false; // failed (must remove child Regions first)
|
||||
|
} |
||||
|
|
||||
|
i = 0; |
||||
|
while (i < num_regions) { |
||||
|
if (region.id == regions[i].id) break; |
||||
|
i++; |
||||
|
} |
||||
|
if (i >= num_regions) return false; // failed (not found)
|
||||
|
|
||||
|
num_regions--; // remove from regions array
|
||||
|
while (i < num_regions) { |
||||
|
regions[i] = regions[i + 1]; |
||||
|
i++; |
||||
|
} |
||||
|
return true; // success
|
||||
|
} |
||||
|
|
||||
|
bool RegionMap::clear() { |
||||
|
num_regions = 0; |
||||
|
return true; // success
|
||||
|
} |
||||
|
|
||||
|
void RegionMap::printChildRegions(int indent, const RegionEntry* parent, Stream& out) const { |
||||
|
for (int i = 0; i < indent; i++) { |
||||
|
out.print(' '); |
||||
|
} |
||||
|
|
||||
|
if (parent->flags & REGION_DENY_FLOOD) { |
||||
|
out.printf("%s%s\n", parent->name, parent->id == home_id ? "^" : ""); |
||||
|
} else { |
||||
|
out.printf("%s%s F\n", parent->name, parent->id == home_id ? "^" : ""); |
||||
|
} |
||||
|
|
||||
|
for (int i = 0; i < num_regions; i++) { |
||||
|
auto r = ®ions[i]; |
||||
|
if (r->parent == parent->id) { |
||||
|
printChildRegions(indent + 1, r, out); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void RegionMap::exportTo(Stream& out) const { |
||||
|
printChildRegions(0, &wildcard, out); // recursive
|
||||
|
} |
||||
@ -0,0 +1,52 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <Arduino.h> // needed for PlatformIO |
||||
|
#include <Packet.h> |
||||
|
#include "TransportKeyStore.h" |
||||
|
|
||||
|
#ifndef MAX_REGION_ENTRIES |
||||
|
#define MAX_REGION_ENTRIES 32 |
||||
|
#endif |
||||
|
|
||||
|
#define REGION_DENY_FLOOD 0x01 |
||||
|
#define REGION_DENY_DIRECT 0x02 // reserved for future
|
||||
|
|
||||
|
struct RegionEntry { |
||||
|
uint16_t id; |
||||
|
uint16_t parent; |
||||
|
uint8_t flags; |
||||
|
char name[31]; |
||||
|
}; |
||||
|
|
||||
|
class RegionMap { |
||||
|
TransportKeyStore* _store; |
||||
|
uint16_t next_id, home_id; |
||||
|
uint16_t num_regions; |
||||
|
RegionEntry regions[MAX_REGION_ENTRIES]; |
||||
|
RegionEntry wildcard; |
||||
|
|
||||
|
void printChildRegions(int indent, const RegionEntry* parent, Stream& out) const; |
||||
|
|
||||
|
public: |
||||
|
RegionMap(TransportKeyStore& store); |
||||
|
|
||||
|
static bool is_name_char(char c); |
||||
|
|
||||
|
bool load(FILESYSTEM* _fs); |
||||
|
bool save(FILESYSTEM* _fs); |
||||
|
|
||||
|
RegionEntry* putRegion(const char* name, uint16_t parent_id, uint16_t id = 0); |
||||
|
RegionEntry* findMatch(mesh::Packet* packet, uint8_t mask); |
||||
|
RegionEntry& getWildcard() { return wildcard; } |
||||
|
RegionEntry* findByName(const char* name); |
||||
|
RegionEntry* findByNamePrefix(const char* prefix); |
||||
|
RegionEntry* findById(uint16_t id); |
||||
|
RegionEntry* getHomeRegion(); // NOTE: can be NULL
|
||||
|
void setHomeRegion(const RegionEntry* home); |
||||
|
bool removeRegion(const RegionEntry& region); |
||||
|
bool clear(); |
||||
|
void resetFrom(const RegionMap& src) { num_regions = 0; next_id = src.next_id; } |
||||
|
int getCount() const { return num_regions; } |
||||
|
|
||||
|
void exportTo(Stream& out) const; |
||||
|
}; |
||||
@ -0,0 +1,92 @@ |
|||||
|
#include "TransportKeyStore.h" |
||||
|
#include <SHA256.h> |
||||
|
|
||||
|
uint16_t TransportKey::calcTransportCode(const mesh::Packet* packet) const { |
||||
|
uint16_t code; |
||||
|
SHA256 sha; |
||||
|
sha.resetHMAC(key, sizeof(key)); |
||||
|
uint8_t type = packet->getPayloadType(); |
||||
|
sha.update(&type, 1); |
||||
|
sha.update(packet->payload, packet->payload_len); |
||||
|
sha.finalizeHMAC(key, sizeof(key), &code, 2); |
||||
|
if (code == 0) { // reserve codes 0000 and FFFF
|
||||
|
code++; |
||||
|
} else if (code == 0xFFFF) { |
||||
|
code--; |
||||
|
} |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
bool TransportKey::isNull() const { |
||||
|
for (int i = 0; i < sizeof(key); i++) { |
||||
|
if (key[i]) return false; |
||||
|
} |
||||
|
return true; // key is all zeroes
|
||||
|
} |
||||
|
|
||||
|
void TransportKeyStore::putCache(uint16_t id, const TransportKey& key) { |
||||
|
if (num_cache < MAX_TKS_ENTRIES) { |
||||
|
cache_ids[num_cache] = id; |
||||
|
cache_keys[num_cache] = key; |
||||
|
num_cache++; |
||||
|
} else { |
||||
|
// TODO: evict oldest cache entry
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void TransportKeyStore::getAutoKeyFor(uint16_t id, const char* name, TransportKey& dest) { |
||||
|
for (int i = 0; i < num_cache; i++) { // first, check cache
|
||||
|
if (cache_ids[i] == id) { // cache hit!
|
||||
|
dest = cache_keys[i]; |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
// calc key for publicly-known hashtag region name
|
||||
|
SHA256 sha; |
||||
|
sha.update(name, strlen(name)); |
||||
|
sha.finalize(&dest.key, sizeof(dest.key)); |
||||
|
|
||||
|
putCache(id, dest); |
||||
|
} |
||||
|
|
||||
|
int TransportKeyStore::loadKeysFor(uint16_t id, TransportKey keys[], int max_num) { |
||||
|
int n = 0; |
||||
|
for (int i = 0; i < num_cache && n < max_num; i++) { // first, check cache
|
||||
|
if (cache_ids[i] == id) { |
||||
|
keys[n++] = cache_keys[i]; |
||||
|
} |
||||
|
} |
||||
|
if (n > 0) return n; // cache hit!
|
||||
|
|
||||
|
// TODO: retrieve from difficult-to-copy keystore
|
||||
|
|
||||
|
// store in cache (if room)
|
||||
|
for (int i = 0; i < n; i++) { |
||||
|
putCache(id, keys[i]); |
||||
|
} |
||||
|
return n; |
||||
|
} |
||||
|
|
||||
|
bool TransportKeyStore::saveKeysFor(uint16_t id, const TransportKey keys[], int num) { |
||||
|
invalidateCache(); |
||||
|
|
||||
|
// TODO: update hardware keystore
|
||||
|
|
||||
|
return false; // failed
|
||||
|
} |
||||
|
|
||||
|
bool TransportKeyStore::removeKeys(uint16_t id) { |
||||
|
invalidateCache(); |
||||
|
|
||||
|
// TODO: remove from hardware keystore
|
||||
|
|
||||
|
return false; // failed
|
||||
|
} |
||||
|
|
||||
|
bool TransportKeyStore::clear() { |
||||
|
invalidateCache(); |
||||
|
|
||||
|
// TODO: clear hardware keystore
|
||||
|
|
||||
|
return false; // failed
|
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <Arduino.h> // needed for PlatformIO |
||||
|
#include <Packet.h> |
||||
|
#include <helpers/IdentityStore.h> |
||||
|
|
||||
|
struct TransportKey { |
||||
|
uint8_t key[16]; |
||||
|
|
||||
|
uint16_t calcTransportCode(const mesh::Packet* packet) const; |
||||
|
bool isNull() const; |
||||
|
}; |
||||
|
|
||||
|
#define MAX_TKS_ENTRIES 16 |
||||
|
|
||||
|
class TransportKeyStore { |
||||
|
uint16_t cache_ids[MAX_TKS_ENTRIES]; |
||||
|
TransportKey cache_keys[MAX_TKS_ENTRIES]; |
||||
|
int num_cache; |
||||
|
|
||||
|
void putCache(uint16_t id, const TransportKey& key); |
||||
|
void invalidateCache() { num_cache = 0; } |
||||
|
|
||||
|
public: |
||||
|
TransportKeyStore() { num_cache = 0; } |
||||
|
void getAutoKeyFor(uint16_t id, const char* name, TransportKey& dest); |
||||
|
int loadKeysFor(uint16_t id, TransportKey keys[], int max_num); |
||||
|
bool saveKeysFor(uint16_t id, const TransportKey keys[], int num); |
||||
|
bool removeKeys(uint16_t id); |
||||
|
bool clear(); |
||||
|
}; |
||||
Loading…
Reference in new issue