Keep OTA replies separate from Companion messages and contacts
This commit is contained in:
1 parent
735ab4b9be
commit
dedae3d5ca
7 files changed
+537
-160
No files matched your search
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Mesh.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace mesh {
|
||||
@@ -13,6 +14,11 @@ class CompanionDirectAdminGuard {
|
||||
uint8_t secret_[PUB_KEY_SIZE] = {};
|
||||
uint32_t until_ = 0;
|
||||
bool enabled_ = false, login_ = false;
|
||||
uint32_t last_peer_timestamp_ = 0;
|
||||
uint8_t reply_[160] = {};
|
||||
uint16_t reply_length_ = 0;
|
||||
char correlation_[3] = {};
|
||||
bool waiting_ = false, ready_ = false;
|
||||
|
||||
public:
|
||||
static constexpr uint32_t LEASE_MS = 300000;
|
||||
@@ -20,7 +26,13 @@ public:
|
||||
void clear() {
|
||||
volatile uint8_t* bytes = secret_;
|
||||
for (size_t i = 0; i < sizeof(secret_); ++i) bytes[i] = 0;
|
||||
bytes = reply_;
|
||||
for (size_t i = 0; i < sizeof(reply_); ++i) bytes[i] = 0;
|
||||
enabled_ = login_ = false;
|
||||
waiting_ = ready_ = false;
|
||||
reply_length_ = 0;
|
||||
last_peer_timestamp_ = 0;
|
||||
memset(correlation_, 0, sizeof(correlation_));
|
||||
}
|
||||
bool active(uint32_t now) {
|
||||
if (enabled_ && static_cast<int32_t>(now - until_) >= 0) clear();
|
||||
@@ -33,7 +45,7 @@ public:
|
||||
return enabled_ && static_cast<int32_t>(now - until_) < 0 && peer_.matches(peer);
|
||||
}
|
||||
void arm(const Identity& peer, const uint8_t* secret, uint32_t now) {
|
||||
if (!matches(peer, now)) login_ = false;
|
||||
if (!matches(peer, now)) clear();
|
||||
peer_ = peer;
|
||||
memcpy(secret_, secret, sizeof(secret_));
|
||||
enabled_ = true;
|
||||
@@ -42,6 +54,70 @@ public:
|
||||
void beginLogin(const Identity& peer, uint32_t now) {
|
||||
if (matches(peer, now)) login_ = true;
|
||||
}
|
||||
bool matchesPrefix(const uint8_t* prefix, size_t len, uint32_t now) {
|
||||
return prefix && len <= PUB_KEY_SIZE && active(now) && !memcmp(peer_.pub_key, prefix, len);
|
||||
}
|
||||
void noteLogin(const uint8_t* data, size_t len) {
|
||||
if (login_ && data && len >= 13 && data[4] == 0 && (data[7] & 7) == 3)
|
||||
memcpy(&last_peer_timestamp_, data, 4);
|
||||
}
|
||||
bool expect(const uint8_t* correlation, uint32_t now) {
|
||||
if (!active(now) || !correlation || correlation[2] != '|') return false;
|
||||
for (size_t i = 0; i < 2; ++i) {
|
||||
const uint8_t c = correlation[i];
|
||||
if (c < '!' || c > '~' || c == '|') return false;
|
||||
}
|
||||
memcpy(correlation_, correlation, sizeof(correlation_));
|
||||
reply_length_ = 0;
|
||||
waiting_ = true;
|
||||
ready_ = false;
|
||||
until_ = now + LEASE_MS;
|
||||
return true;
|
||||
}
|
||||
// Called only after the selected peer's ECDH/MAC and DIRECT0 admission.
|
||||
// Other messages continue through the ordinary inbox unchanged.
|
||||
bool captureReply(const uint8_t* data, size_t len, uint32_t now) {
|
||||
if (!active(now) || (!waiting_ && !ready_) || !data || len < 8
|
||||
|| (data[4] >> 2) != 1 || memcmp(data + 5, correlation_, 3)) return false;
|
||||
if (ready_) return true; // duplicate maintenance replies do not enter the inbox
|
||||
uint32_t timestamp;
|
||||
memcpy(×tamp, data, 4);
|
||||
if (timestamp <= last_peer_timestamp_) return true; // consume stale copy only
|
||||
const uint8_t* text = data + 8;
|
||||
size_t length = len - 8;
|
||||
const void* zero = memchr(text, 0, length);
|
||||
if (zero) length = static_cast<const uint8_t*>(zero) - text;
|
||||
if (length > sizeof(reply_)) return true;
|
||||
memcpy(reply_, text, length);
|
||||
reply_length_ = length;
|
||||
last_peer_timestamp_ = timestamp;
|
||||
ready_ = true;
|
||||
waiting_ = false;
|
||||
until_ = now + LEASE_MS;
|
||||
return true;
|
||||
}
|
||||
void readReply(size_t offset, char* output, size_t capacity, uint32_t now) {
|
||||
active(now);
|
||||
if (offset > reply_length_) {
|
||||
snprintf(output, capacity, "ERR invalid reply offset");
|
||||
return;
|
||||
}
|
||||
const int used = snprintf(output, capacity,
|
||||
"> ota.reply req=%c%c state=%s bytes=%u off=%u data=",
|
||||
correlation_[0] ? correlation_[0] : '-', correlation_[1] ? correlation_[1] : '-',
|
||||
ready_ ? "ready" : waiting_ ? "pending" : "idle",
|
||||
static_cast<unsigned>(reply_length_), static_cast<unsigned>(offset));
|
||||
if (used < 0 || static_cast<size_t>(used) >= capacity) return;
|
||||
size_t at = used;
|
||||
const size_t end = offset + 40 < reply_length_ ? offset + 40 : reply_length_;
|
||||
static const char hex[] = "0123456789ABCDEF";
|
||||
for (; offset < end && at + 2 < capacity; ++offset) {
|
||||
output[at++] = hex[reply_[offset] >> 4];
|
||||
output[at++] = hex[reply_[offset] & 15];
|
||||
}
|
||||
if (at == static_cast<size_t>(used) && at + 1 < capacity) output[at++] = '-';
|
||||
output[at] = 0;
|
||||
}
|
||||
__attribute__((noinline)) bool matchesSecret(const uint8_t* secret, uint32_t now) {
|
||||
return active(now) && secret && !memcmp(secret_, secret, sizeof(secret_));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user