Extend flood policy and harden message retries

This commit is contained in:
mikecarper committed 2026-08-07 14:26:31 -07:00
1 parent 687e114828
commit 7f56da7a34
34 files changed
+5297 -1338

No files matched your search

+21 -255
View File
@@ -497,92 +497,20 @@ static void formatFloodRetryPathGate(char* dest, uint8_t path_gate) {
}
}
static bool parseFloodChannelBlockHops(const char* value, uint8_t& max_hops) {
static bool parseFloodChannelHops(const char* value, uint8_t& max_hops) {
if (value == NULL) {
return false;
}
value = skipSpacesConst(value);
if (strcmp(value, "all") == 0) {
max_hops = FLOOD_CHANNEL_BLOCK_HOPS_ALL;
max_hops = FLOOD_CHANNEL_HOPS_ALL;
return true;
}
return parseUint8Strict(value, 1, 7, max_hops);
}
static bool parseFloodChannelBlockRowHops(const char* value, uint8_t& max_hops) {
if (value == NULL) {
return false;
}
value = skipSpacesConst(value);
if (strcmp(value, "default") == 0 || strcmp(value, "def") == 0 || strcmp(value, "inherit") == 0) {
max_hops = FLOOD_CHANNEL_BLOCK_HOPS_INHERIT;
return true;
}
return parseFloodChannelBlockHops(value, max_hops);
}
static bool parseFloodChannelBlockHopAssignment(const char* text, bool allow_bare, uint8_t& max_hops) {
char token[16];
text = skipSpacesConst(text);
if (text == NULL || *text == 0) {
return false;
}
size_t len = 0;
while (text[len] && text[len] != ' ' && len + 1 < sizeof(token)) {
token[len] = text[len];
len++;
}
token[len] = 0;
const char* value = NULL;
if (strncmp(token, "h=", 2) == 0) {
value = token + 2;
} else if (strncmp(token, "hops=", 5) == 0) {
value = token + 5;
} else if (allow_bare) {
value = token;
} else {
return false;
}
return parseFloodChannelBlockRowHops(value, max_hops);
}
static bool looksFloodChannelBlockHopAssignment(const char* text) {
text = skipSpacesConst(text);
if (text == NULL || *text == 0) {
return false;
}
return (*text >= '0' && *text <= '9')
|| strncmp(text, "h=", 2) == 0
|| strncmp(text, "hops=", 5) == 0
|| strncmp(text, "all", 3) == 0
|| strncmp(text, "def", 3) == 0
|| strncmp(text, "default", 7) == 0
|| strncmp(text, "inherit", 7) == 0;
}
static bool trimFloodChannelBlockHopSuffix(char* name, uint8_t& max_hops) {
size_t len = strlen(name);
while (len > 0 && name[len - 1] == ' ') {
name[--len] = 0;
}
char* token = strrchr(name, ' ');
if (token == NULL) {
return true;
}
if (strncmp(token + 1, "h=", 2) != 0 && strncmp(token + 1, "hops=", 5) != 0) {
return true;
}
if (!parseFloodChannelBlockHopAssignment(token + 1, false, max_hops)) {
return false;
}
*token = 0;
return strlen(name) > 0;
}
static void formatFloodChannelBlockHops(char* dest, uint8_t max_hops) {
if (max_hops == FLOOD_CHANNEL_BLOCK_HOPS_ALL) {
static void formatFloodChannelHops(char* dest, uint8_t max_hops) {
if (max_hops == FLOOD_CHANNEL_HOPS_ALL) {
strcpy(dest, "h=all");
} else {
sprintf(dest, "h>%u", (unsigned int)max_hops);
@@ -699,69 +627,6 @@ static bool formatFloodRetryBucketCollisionWarning(char* reply, const NodePrefs*
return true;
}
static bool parseFloodChannelBlockKey(const char* text, uint8_t secret[PUB_KEY_SIZE], uint8_t& key_len) {
if (text == NULL || text[0] == 0) {
return false;
}
memset(secret, 0, PUB_KEY_SIZE);
if (text[0] == '#') {
if (!isValidName(text)) {
return false;
}
mesh::Utils::sha256(secret, CIPHER_KEY_SIZE, (const uint8_t*)text, strlen(text));
key_len = CIPHER_KEY_SIZE;
return true;
}
size_t hex_len = strlen(text);
if (!(hex_len == CIPHER_KEY_SIZE * 2 || hex_len == PUB_KEY_SIZE * 2)) {
return false;
}
for (size_t i = 0; i < hex_len; i++) {
if (!mesh::Utils::isHexChar(text[i])) {
return false;
}
}
key_len = (uint8_t)(hex_len / 2);
return mesh::Utils::fromHex(secret, key_len, text);
}
static bool parseFloodChannelBlockDotIndex(const char*& cursor, int& index) {
if (*cursor != '.') {
index = 0;
return true;
}
cursor++;
if (*cursor < '0' || *cursor > '9') {
return false;
}
int value = 0;
while (*cursor >= '0' && *cursor <= '9') {
value = (value * 10) + (*cursor - '0');
if (value > FLOOD_CHANNEL_BLOCK_SLOTS) {
return false;
}
cursor++;
}
if (value < 1) {
return false;
}
index = value;
return true;
}
static void copyTrimmedFloodChannelBlockName(char* dest, size_t dest_len, const char* src) {
src = skipSpacesConst(src);
StrHelper::strncpy(dest, src, dest_len);
size_t len = strlen(dest);
while (len > 0 && dest[len - 1] == ' ') {
dest[--len] = 0;
}
}
static void applyDirectRetryPreset(NodePrefs* prefs, uint8_t preset) {
prefs->retry_preset = preset;
if (preset == RETRY_PRESET_INFRA) {
@@ -1208,8 +1073,8 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
memset(_prefs->battery_alert_region, 0, sizeof(_prefs->battery_alert_region));
_prefs->direct_retry_recent_enabled = DIRECT_RETRY_RECENT_DEFAULT;
_prefs->flood_channel_data_enabled = 1;
_prefs->flood_channel_block_max_hops = FLOOD_CHANNEL_BLOCK_HOPS_ALL;
_prefs->flood_channel_data_max_hops = FLOOD_CHANNEL_BLOCK_HOPS_ALL;
_prefs->legacy_flood_channel_block_max_hops = FLOOD_CHANNEL_HOPS_ALL;
_prefs->flood_channel_data_max_hops = FLOOD_CHANNEL_HOPS_ALL;
has_flood_retry_prefs = file.available() >= 2;
if (has_flood_retry_prefs) {
file.read((uint8_t *)&_prefs->flood_retry_attempts, sizeof(_prefs->flood_retry_attempts)); // 311
@@ -1244,8 +1109,9 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
if (file.available() >= (int)sizeof(_prefs->flood_channel_data_enabled)) {
file.read((uint8_t *)&_prefs->flood_channel_data_enabled, sizeof(_prefs->flood_channel_data_enabled));
}
if (file.available() >= (int)sizeof(_prefs->flood_channel_block_max_hops)) {
file.read((uint8_t *)&_prefs->flood_channel_block_max_hops, sizeof(_prefs->flood_channel_block_max_hops));
if (file.available() >= (int)sizeof(_prefs->legacy_flood_channel_block_max_hops)) {
file.read((uint8_t *)&_prefs->legacy_flood_channel_block_max_hops,
sizeof(_prefs->legacy_flood_channel_block_max_hops));
}
if (file.available() >= (int)sizeof(_prefs->flood_channel_data_max_hops)) {
file.read((uint8_t *)&_prefs->flood_channel_data_max_hops, sizeof(_prefs->flood_channel_data_max_hops));
@@ -1431,13 +1297,14 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
_prefs->direct_retry_recent_enabled = constrain(_prefs->direct_retry_recent_enabled, 0, 1);
_prefs->flood_channel_data_enabled = constrain(_prefs->flood_channel_data_enabled, 0, 1);
_prefs->telemetry_access = constrain(_prefs->telemetry_access, 0, 1);
if (_prefs->flood_channel_block_max_hops != FLOOD_CHANNEL_BLOCK_HOPS_ALL
&& (_prefs->flood_channel_block_max_hops < 1 || _prefs->flood_channel_block_max_hops > 7)) {
_prefs->flood_channel_block_max_hops = FLOOD_CHANNEL_BLOCK_HOPS_ALL;
if (_prefs->legacy_flood_channel_block_max_hops != FLOOD_CHANNEL_HOPS_ALL
&& (_prefs->legacy_flood_channel_block_max_hops < 1
|| _prefs->legacy_flood_channel_block_max_hops > 7)) {
_prefs->legacy_flood_channel_block_max_hops = FLOOD_CHANNEL_HOPS_ALL;
}
if (_prefs->flood_channel_data_max_hops != FLOOD_CHANNEL_BLOCK_HOPS_ALL
if (_prefs->flood_channel_data_max_hops != FLOOD_CHANNEL_HOPS_ALL
&& (_prefs->flood_channel_data_max_hops < 1 || _prefs->flood_channel_data_max_hops > 7)) {
_prefs->flood_channel_data_max_hops = FLOOD_CHANNEL_BLOCK_HOPS_ALL;
_prefs->flood_channel_data_max_hops = FLOOD_CHANNEL_HOPS_ALL;
}
if (_prefs->battery_alert_low_percent < 1
|| _prefs->battery_alert_low_percent > 100
@@ -1564,7 +1431,7 @@ static bool writeCommonPrefsImage(Writer& writer, NodePrefs* prefs) {
WRITE_COMMON_PREFS(&prefs->battery_alert_critical_percent);
WRITE_COMMON_PREFS(&prefs->direct_retry_recent_enabled);
WRITE_COMMON_PREFS(&prefs->flood_channel_data_enabled);
WRITE_COMMON_PREFS(&prefs->flood_channel_block_max_hops);
WRITE_COMMON_PREFS(&prefs->legacy_flood_channel_block_max_hops);
WRITE_COMMON_PREFS(&prefs->flood_channel_data_max_hops);
WRITE_COMMON_PREFS(&prefs->telemetry_access); // 674
#if defined(ENABLE_OTA)
@@ -1708,7 +1575,8 @@ void CommonCLI::savePrefs(FILESYSTEM* fs, PrefsSaveRouting::Scope scope) {
file.write((uint8_t *)&_prefs->battery_alert_critical_percent, sizeof(_prefs->battery_alert_critical_percent));
file.write((uint8_t *)&_prefs->direct_retry_recent_enabled, sizeof(_prefs->direct_retry_recent_enabled));
file.write((uint8_t *)&_prefs->flood_channel_data_enabled, sizeof(_prefs->flood_channel_data_enabled));
file.write((uint8_t *)&_prefs->flood_channel_block_max_hops, sizeof(_prefs->flood_channel_block_max_hops));
file.write((uint8_t *)&_prefs->legacy_flood_channel_block_max_hops,
sizeof(_prefs->legacy_flood_channel_block_max_hops));
file.write((uint8_t *)&_prefs->flood_channel_data_max_hops, sizeof(_prefs->flood_channel_data_max_hops));
file.write((uint8_t *)&_prefs->telemetry_access, sizeof(_prefs->telemetry_access)); // 674
#if defined(ENABLE_OTA)
@@ -3945,7 +3813,7 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
}
} else if (memcmp(config, "flood.channel.data.hops ", 24) == 0) {
uint8_t max_hops;
if (parseFloodChannelBlockHops(&config[24], max_hops)) {
if (parseFloodChannelHops(&config[24], max_hops)) {
_prefs->flood_channel_data_max_hops = max_hops;
savePrefs();
strcpy(reply, "OK");
@@ -3964,62 +3832,6 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
} else {
strcpy(reply, "Error, must be on or off");
}
} else if (memcmp(config, "flood.channel.block.hops ", 25) == 0) {
uint8_t max_hops;
if (parseFloodChannelBlockHops(&config[25], max_hops)) {
_prefs->flood_channel_block_max_hops = max_hops;
savePrefs();
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, must be all or 1-7");
}
} else if (memcmp(config, "flood.channel.block", 19) == 0
&& (config[19] == ' ' || config[19] == '.')) {
const char* cursor = &config[19];
int index = 0;
if (!parseFloodChannelBlockDotIndex(cursor, index)) {
sprintf(reply, "Error, index 1-%d", FLOOD_CHANNEL_BLOCK_SLOTS);
return;
}
cursor = skipSpacesConst(cursor);
const char* key_start = cursor;
while (*cursor && *cursor != ' ') cursor++;
size_t key_len_text = cursor - key_start;
char key_text[PUB_KEY_SIZE * 2 + 1];
if (key_len_text == 0 || key_len_text >= sizeof(key_text)) {
strcpy(reply, "Error, use: set flood.channel.block[.n] <hex-key> <name>|#channel");
return;
}
memcpy(key_text, key_start, key_len_text);
key_text[key_len_text] = 0;
char name[FLOOD_CHANNEL_BLOCK_NAME_LEN];
uint8_t block_hops = FLOOD_CHANNEL_BLOCK_HOPS_INHERIT;
if (key_text[0] == '#') {
StrHelper::strncpy(name, key_text, sizeof(name));
const char* extra = skipSpacesConst(cursor);
if (*extra && looksFloodChannelBlockHopAssignment(extra)
&& !parseFloodChannelBlockHopAssignment(extra, true, block_hops)) {
strcpy(reply, "Error, hops must be all, default, or 1-7");
return;
}
} else {
copyTrimmedFloodChannelBlockName(name, sizeof(name), cursor);
if (!trimFloodChannelBlockHopSuffix(name, block_hops)) {
strcpy(reply, "Error, bad name or hops");
return;
}
}
uint8_t secret[PUB_KEY_SIZE];
uint8_t decoded_key_len = 0;
if (!parseFloodChannelBlockKey(key_text, secret, decoded_key_len)) {
strcpy(reply, "Error, key must be 128/256-bit hex or #channel");
} else if (name[0] == 0 || !isValidName(name)) {
strcpy(reply, "Error, bad name");
} else {
_callbacks->setFloodChannelBlock(index, secret, decoded_key_len, name, block_hops, reply);
}
} else if (memcmp(config, "flood.retry.count ", 18) == 0) {
int attempts = looksUnsignedInteger(&config[18]) ? _atoi(&config[18]) : -1;
if (attempts >= 0 && attempts <= 15) {
@@ -4570,36 +4382,12 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
sprintf(reply, "> %s", StrHelper::ftoa(_prefs->tx_delay_factor));
} else if (strcmp(config, "flood.channel.data.hops") == 0) {
char hops[8];
formatFloodChannelBlockHops(hops, _prefs->flood_channel_data_max_hops);
formatFloodChannelHops(hops, _prefs->flood_channel_data_max_hops);
sprintf(reply, "> %s", hops);
} else if (strcmp(config, "flood.channel.data") == 0) {
char hops[8];
formatFloodChannelBlockHops(hops, _prefs->flood_channel_data_max_hops);
formatFloodChannelHops(hops, _prefs->flood_channel_data_max_hops);
sprintf(reply, "> %s %s", _prefs->flood_channel_data_enabled ? "on" : "off", hops);
} else if (memcmp(config, "flood.channel.block.hops", 24) == 0) {
char hops[8];
formatFloodChannelBlockHops(hops, _prefs->flood_channel_block_max_hops);
sprintf(reply, "> %s", hops);
} else if (memcmp(config, "flood.channel.block", 19) == 0
&& (config[19] == 0 || config[19] == ' ' || config[19] == '.')) {
const char* cursor = &config[19];
int index = 0;
if (!parseFloodChannelBlockDotIndex(cursor, index)) {
sprintf(reply, "Error, index 1-%d", FLOOD_CHANNEL_BLOCK_SLOTS);
return;
}
cursor = skipSpacesConst(cursor);
char selector[8];
if (index > 0 && *cursor != 0) {
strcpy(reply, "Error, use index or selector");
return;
}
if (index > 0) {
snprintf(selector, sizeof(selector), "%d", index);
_callbacks->formatFloodChannelBlocks(selector, reply);
} else {
_callbacks->formatFloodChannelBlocks(cursor, reply);
}
} else if (memcmp(config, "flood.max.advert", 16) == 0) {
sprintf(reply, "> %d", (uint32_t)_prefs->flood_max_advert);
} else if (memcmp(config, "flood.max.unscoped", 18) == 0) {
@@ -4799,28 +4587,6 @@ void CommonCLI::handleDelCmd(char* command, char* reply) {
_callbacks->deleteScheduledRadioParams(true, skipSpacesConst(&config[11]), reply);
} else if (memcmp(config, "radioat", 7) == 0 && (config[7] == 0 || config[7] == ' ')) {
_callbacks->deleteScheduledRadioParams(false, skipSpacesConst(&config[7]), reply);
} else if (memcmp(config, "flood.channel.block", 19) == 0
&& (config[19] == ' ' || config[19] == '.')) {
const char* cursor = &config[19];
int index = 0;
if (!parseFloodChannelBlockDotIndex(cursor, index)) {
sprintf(reply, "Error, index 1-%d", FLOOD_CHANNEL_BLOCK_SLOTS);
return;
}
cursor = skipSpacesConst(cursor);
char selector[8];
if (index > 0 && *cursor != 0) {
strcpy(reply, "Error, use index or selector");
return;
}
if (index > 0) {
snprintf(selector, sizeof(selector), "%d", index);
_callbacks->deleteFloodChannelBlock(selector, reply);
} else if (*cursor != 0) {
_callbacks->deleteFloodChannelBlock(cursor, reply);
} else {
strcpy(reply, "Error, use: del flood.channel.block <index|name|prefix>");
}
} else {
strcpy(reply, "unknown del: ");
StrHelper::strncpy(&reply[13], config, 160 - 14);