* setting up framework for Regions, TransportKeys, etc

This commit is contained in:
Scott Powell 2025-11-03 14:23:32 +11:00
parent 5b4544b9fe
commit 03fc949014
6 changed files with 166 additions and 2 deletions

35
src/helpers/RegionMap.cpp Normal file
View file

@ -0,0 +1,35 @@
#include "RegionMap.h"
#include <SHA256.h>
void RegionMap::load(FILESYSTEM* _fs) {
// TODO
}
void RegionMap::save(FILESYSTEM* _fs) {
// TODO
}
RegionEntry* RegionMap::findMatch(mesh::Packet* packet, uint8_t mask) {
for (int i = 0; i < num_regions; i++) {
auto region = &regions[i];
if (region->flags & mask) { // does region allow this? (per 'mask' param)
TransportKey keys[4];
int num = _store->loadKeysFor(region->name, 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
}
const RegionEntry* RegionMap::findName(const char* name) const {
for (int i = 0; i < num_regions; i++) {
auto region = &regions[i];
if (strcmp(name, region->name) == 0) return region;
}
return NULL; // not found
}

39
src/helpers/RegionMap.h Normal file
View file

@ -0,0 +1,39 @@
#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_ALLOW_FLOOD 0x01
struct RegionEntry {
uint16_t id;
uint16_t parent;
uint8_t flags;
char name[31];
};
class RegionMap {
TransportKeyStore* _store;
uint16_t next_id;
uint16_t num_regions;
RegionEntry regions[MAX_REGION_ENTRIES];
RegionEntry wildcard;
public:
RegionMap(TransportKeyStore& store) : _store(&store) {
next_id = 1; num_regions = 0;
wildcard.id = wildcard.parent = 0;
wildcard.flags = REGION_ALLOW_FLOOD; // default behaviour, allow flood
}
void load(FILESYSTEM* _fs);
void save(FILESYSTEM* _fs);
RegionEntry* findMatch(mesh::Packet* packet, uint8_t mask);
const RegionEntry& getWildcard() const { return wildcard; }
const RegionEntry* findName(const char* name) const;
};

View file

@ -0,0 +1,44 @@
#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);
return code;
}
int TransportKeyStore::loadKeysFor(const char* name, 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!
if (*name == '#') { // is a publicly-known hashtag region
SHA256 sha;
sha.update(name, strlen(name));
sha.finalize(&keys[0], sizeof(keys[0].key));
n = 1;
} else {
// TODO: retrieve from difficult-to-copy keystore
}
// store in cache (if room)
for (int i = 0; i < n; i++) {
if (num_cache < MAX_TKS_ENTRIES) {
cache_ids[num_cache] = id;
cache_keys[num_cache] = keys[i];
num_cache++;
} else {
// TODO: evict oldest cache entry
}
}
return n;
}

View file

@ -0,0 +1,23 @@
#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;
};
#define MAX_TKS_ENTRIES 16
class TransportKeyStore {
uint16_t cache_ids[MAX_TKS_ENTRIES];
TransportKey cache_keys[MAX_TKS_ENTRIES];
int num_cache;
public:
TransportKeyStore() { num_cache = 0; }
int loadKeysFor(const char* name, uint16_t id, TransportKey keys[], int max_num);
};