redis: mirror peer node lists + sanitize trunk-received strings
Peer node lists (MsgTrunkNodeList, type 121) now propagate into Redis alongside the existing MQTT publish, so dashboards reading Redis see a complete view of local + peer rosters. Each remote entry is stored as live:peer_node:<peer_id>:<callsign> with 60s TTL; a per-peer cache diffs incoming snapshots so dropped callsigns are DEL'd, and a full trunk-down deletes all of that peer's entries. All untrusted strings arriving via the trunk (peer_id from the hello, plus callsign/qth/lat/lon in node lists) are now sanitized at receive: control chars + ':' stripped, length bounded (32 callsign, 64 peer_id and qth), non-finite and out-of-range coordinates cleared. Entries whose callsign becomes empty after sanitization are dropped. Also reorders TrunkLink in/outbound onDisconnected so the state-change notification fires after the per-direction state is cleared, letting consumers that inspect isActive() (like the Redis peer-node cleanup) see the correct post-disconnect state. Adds RedisPeerNodeTest (3 cases: populate+diff, sanitization, trunk- down cleanup) and a build_node_list wire helper in test_trunk.py. Exposes the trunk port in docker-compose.redis.yml so the host can inject trunk frames against the r1 test reflector.
This commit is contained in:
1 parent
0700e01453
commit
7b95d0d2ce
10 files changed
+399
-9
No files matched your search
@@ -314,6 +314,43 @@ void RedisStore::clearLiveClient(const std::string& callsign) {
|
||||
m_live_queue->push(std::move(op));
|
||||
}
|
||||
|
||||
void RedisStore::pushPeerNode(const std::string& peer_id,
|
||||
const std::string& callsign,
|
||||
uint32_t tg,
|
||||
float lat, float lon,
|
||||
const std::string& qth_name) {
|
||||
if (!m_live_queue) return;
|
||||
RedisLiveQueue::Op op;
|
||||
op.op = RedisLiveQueue::OpType::HSET;
|
||||
op.key = keyFor("live:peer_node:" + peer_id + ":" + callsign);
|
||||
m_live_keys.insert(op.key);
|
||||
op.fields = {
|
||||
{"peer_id", peer_id},
|
||||
{"callsign", callsign},
|
||||
{"tg", std::to_string(tg)},
|
||||
{"updated_at", std::to_string(std::time(nullptr))}
|
||||
};
|
||||
if (lat != 0.0f || lon != 0.0f) {
|
||||
op.fields.emplace_back("lat", std::to_string(lat));
|
||||
op.fields.emplace_back("lon", std::to_string(lon));
|
||||
}
|
||||
if (!qth_name.empty()) {
|
||||
op.fields.emplace_back("qth_name", qth_name);
|
||||
}
|
||||
op.ttl_s = 60;
|
||||
m_live_queue->push(std::move(op));
|
||||
}
|
||||
|
||||
void RedisStore::clearPeerNode(const std::string& peer_id,
|
||||
const std::string& callsign) {
|
||||
if (!m_live_queue) return;
|
||||
RedisLiveQueue::Op op;
|
||||
op.op = RedisLiveQueue::OpType::DEL;
|
||||
op.key = keyFor("live:peer_node:" + peer_id + ":" + callsign);
|
||||
m_live_keys.erase(op.key);
|
||||
m_live_queue->push(std::move(op));
|
||||
}
|
||||
|
||||
void RedisStore::pushLiveTrunk(const std::string& section,
|
||||
const std::string& state,
|
||||
const std::string& peer_id) {
|
||||
|
||||
@@ -81,6 +81,13 @@ class RedisStore : public sigc::trackable
|
||||
void pushLiveTrunk(const std::string& section,
|
||||
const std::string& state,
|
||||
const std::string& peer_id);
|
||||
void pushPeerNode(const std::string& peer_id,
|
||||
const std::string& callsign,
|
||||
uint32_t tg,
|
||||
float lat, float lon,
|
||||
const std::string& qth_name);
|
||||
void clearPeerNode(const std::string& peer_id,
|
||||
const std::string& callsign);
|
||||
|
||||
uint64_t droppedLiveWrites(void) const { return m_dropped_live_writes; }
|
||||
size_t liveQueueSize(void) const;
|
||||
|
||||
@@ -3034,6 +3034,27 @@ void Reflector::onPeerNodeList(const std::string& peer_id,
|
||||
{
|
||||
m_mqtt->publishPeerNodes(peer_id, nodes);
|
||||
}
|
||||
|
||||
if (m_redis != nullptr && !peer_id.empty())
|
||||
{
|
||||
std::set<std::string> seen;
|
||||
for (const auto& n : nodes)
|
||||
{
|
||||
if (n.callsign.empty()) continue;
|
||||
seen.insert(n.callsign);
|
||||
m_redis->pushPeerNode(peer_id, n.callsign, n.tg,
|
||||
n.lat, n.lon, n.qth_name);
|
||||
}
|
||||
auto& prev = m_peer_node_cache[peer_id];
|
||||
for (const std::string& cs : prev)
|
||||
{
|
||||
if (seen.find(cs) == seen.end())
|
||||
{
|
||||
m_redis->clearPeerNode(peer_id, cs);
|
||||
}
|
||||
}
|
||||
prev = std::move(seen);
|
||||
}
|
||||
} /* Reflector::onPeerNodeList */
|
||||
|
||||
|
||||
@@ -3059,6 +3080,30 @@ void Reflector::onTrunkStateChanged(const std::string& section,
|
||||
{
|
||||
m_redis->pushLiveTrunk(section, up ? "up" : "down", peer_id);
|
||||
}
|
||||
|
||||
// When a trunk link is fully down (both directions), clear any node
|
||||
// entries we were mirroring from this peer into Redis. Keeps Redis in
|
||||
// sync with the fact that we can no longer observe the peer's roster.
|
||||
if (!up && m_redis != nullptr && !peer_id.empty())
|
||||
{
|
||||
TrunkLink* link = nullptr;
|
||||
for (auto* l : m_trunk_links)
|
||||
{
|
||||
if (l->section() == section) { link = l; break; }
|
||||
}
|
||||
if (link == nullptr || !link->isActive())
|
||||
{
|
||||
auto it = m_peer_node_cache.find(peer_id);
|
||||
if (it != m_peer_node_cache.end())
|
||||
{
|
||||
for (const std::string& cs : it->second)
|
||||
{
|
||||
m_redis->clearPeerNode(peer_id, cs);
|
||||
}
|
||||
m_peer_node_cache.erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
} /* Reflector::onTrunkStateChanged */
|
||||
|
||||
|
||||
|
||||
@@ -325,6 +325,9 @@ class Reflector : public sigc::trackable
|
||||
Json::Value m_status;
|
||||
|
||||
std::vector<TrunkLink*> m_trunk_links;
|
||||
// Tracks last-seen callsign set per peer_id so we can DEL dropped
|
||||
// callsigns from Redis when a peer's node list shrinks.
|
||||
std::map<std::string, std::set<std::string>> m_peer_node_cache;
|
||||
std::set<std::string> m_redis_trunk_sections; // sections loaded from Redis
|
||||
std::set<uint32_t> m_cluster_tgs;
|
||||
bool m_trunk_debug = false;
|
||||
|
||||
@@ -33,6 +33,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
@@ -75,6 +76,42 @@ using namespace Async;
|
||||
using namespace sigc;
|
||||
|
||||
|
||||
// Strip control chars (< 0x20 and 0x7f) and ':' (Redis key delimiter),
|
||||
// then truncate to `max_len`. Used for untrusted identifiers received
|
||||
// over the trunk: peer_id from hellos and callsigns from node lists.
|
||||
static std::string sanitizeIdent(const std::string& in, size_t max_len)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(std::min(in.size(), max_len));
|
||||
for (char c : in)
|
||||
{
|
||||
unsigned char u = static_cast<unsigned char>(c);
|
||||
if (u < 0x20 || u == 0x7f) continue;
|
||||
if (c == ':') continue;
|
||||
out += c;
|
||||
if (out.size() >= max_len) break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Strip control chars only and truncate to `max_bytes`. Used for free-
|
||||
// form text fields (e.g. QTH name) where non-ASCII bytes (UTF-8) are
|
||||
// legitimate. Truncation is byte-level; callers tolerate the possibility
|
||||
// of a split UTF-8 sequence at the boundary.
|
||||
static std::string sanitizeText(const std::string& in, size_t max_bytes)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(std::min(in.size(), max_bytes));
|
||||
for (char c : in)
|
||||
{
|
||||
unsigned char u = static_cast<unsigned char>(c);
|
||||
if (u < 0x20 || u == 0x7f) continue;
|
||||
out += c;
|
||||
if (out.size() >= max_bytes) break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static std::vector<std::string> splitPrefixes(const std::string& s)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
@@ -508,7 +545,7 @@ void TrunkLink::acceptInboundConnection(Async::FramedTcpConnection* con,
|
||||
// (all twins share same priority; last-writer wins is fine)
|
||||
m_peer_priority = hello.priority();
|
||||
if (m_peer_id_received.empty())
|
||||
m_peer_id_received = hello.id();
|
||||
m_peer_id_received = sanitizeIdent(hello.id(), 64);
|
||||
|
||||
cout << m_section << ": paired inbound accepted from peer '"
|
||||
<< hello.id() << "' " << con->remoteHost() << ":"
|
||||
@@ -556,7 +593,7 @@ void TrunkLink::acceptInboundConnection(Async::FramedTcpConnection* con,
|
||||
con->frameReceived.connect(
|
||||
mem_fun(*this, &TrunkLink::onFrameReceived));
|
||||
|
||||
m_peer_id_received = hello.id();
|
||||
m_peer_id_received = sanitizeIdent(hello.id(), 64);
|
||||
cout << m_section << ": Accepted inbound from " << con->remoteHost()
|
||||
<< ":" << con->remotePort() << " peer='" << hello.id()
|
||||
<< "' priority=" << m_peer_priority << endl;
|
||||
@@ -601,7 +638,6 @@ void TrunkLink::onInboundDisconnected(Async::FramedTcpConnection* con,
|
||||
}
|
||||
|
||||
cout << m_section << ": Inbound trunk connection lost" << endl;
|
||||
m_reflector->onTrunkStateChanged(m_section, peerId(), "inbound", false);
|
||||
|
||||
if (m_debug)
|
||||
{
|
||||
@@ -625,6 +661,10 @@ void TrunkLink::onInboundDisconnected(Async::FramedTcpConnection* con,
|
||||
{
|
||||
m_heartbeat_timer.setEnable(false);
|
||||
}
|
||||
|
||||
// Emit after state is cleared so isActive()-based cleanup in consumers
|
||||
// (e.g. Redis peer-node mirror) sees the correct post-disconnect state.
|
||||
m_reflector->onTrunkStateChanged(m_section, peerId(), "inbound", false);
|
||||
} /* TrunkLink::onInboundDisconnected */
|
||||
|
||||
|
||||
@@ -731,7 +771,6 @@ void TrunkLink::onDisconnected(TcpConnection* con,
|
||||
{
|
||||
cout << m_section << ": Outbound disconnected: "
|
||||
<< TcpConnection::disconnectReasonStr(reason) << endl;
|
||||
m_reflector->onTrunkStateChanged(m_section, peerId(), "outbound", false);
|
||||
|
||||
if (m_debug)
|
||||
{
|
||||
@@ -751,6 +790,10 @@ void TrunkLink::onDisconnected(TcpConnection* con,
|
||||
m_heartbeat_timer.setEnable(false);
|
||||
}
|
||||
|
||||
// Emit after state is cleared so isActive()-based cleanup in consumers
|
||||
// sees the correct post-disconnect state.
|
||||
m_reflector->onTrunkStateChanged(m_section, peerId(), "outbound", false);
|
||||
|
||||
// TcpPrioClient auto-reconnects — nothing else to do
|
||||
} /* TrunkLink::onDisconnected */
|
||||
|
||||
@@ -898,7 +941,7 @@ void TrunkLink::handleMsgTrunkHello(std::istream& is, bool is_inbound)
|
||||
}
|
||||
|
||||
m_peer_priority = msg.priority();
|
||||
m_peer_id_received = msg.id();
|
||||
m_peer_id_received = sanitizeIdent(msg.id(), 64);
|
||||
m_ob_hello_received = true;
|
||||
|
||||
cout << m_section << ": Trunk hello from peer '" << msg.id()
|
||||
@@ -1605,7 +1648,7 @@ void TrunkLink::onPairedOutboundFrame(FramedTcpClient* client,
|
||||
// (all twins share the same peer priority; last-writer wins is fine here)
|
||||
m_peer_priority = msg.priority();
|
||||
if (m_peer_id_received.empty())
|
||||
m_peer_id_received = msg.id();
|
||||
m_peer_id_received = sanitizeIdent(msg.id(), 64);
|
||||
|
||||
cout << m_section << ": paired outbound #" << idx
|
||||
<< " hello received (peer='" << msg.id()
|
||||
@@ -1746,7 +1789,50 @@ void TrunkLink::handleMsgTrunkNodeList(std::istream& is)
|
||||
<< "]: Failed to unpack MsgTrunkNodeList" << endl;
|
||||
return;
|
||||
}
|
||||
m_reflector->onPeerNodeList(peerId(), msg.nodes());
|
||||
|
||||
// Sanitize every entry before handing off. Untrusted strings from peer
|
||||
// reflectors flow into Redis key names, MQTT payloads and log output;
|
||||
// keep them byte-bounded and free of control / delimiter characters.
|
||||
std::vector<MsgTrunkNodeList::NodeEntry> sanitized;
|
||||
sanitized.reserve(msg.nodes().size());
|
||||
unsigned dropped = 0;
|
||||
for (const auto& n : msg.nodes())
|
||||
{
|
||||
MsgTrunkNodeList::NodeEntry e;
|
||||
e.callsign = sanitizeIdent(n.callsign, 32);
|
||||
if (e.callsign.empty())
|
||||
{
|
||||
++dropped;
|
||||
continue;
|
||||
}
|
||||
e.tg = n.tg;
|
||||
e.qth_name = sanitizeText(n.qth_name, 64);
|
||||
|
||||
// Lat/lon: reject non-finite; drop out-of-range values by zeroing
|
||||
// them (keep the callsign, lose the coordinates). Downstream consumers
|
||||
// already treat (0,0) as "no location".
|
||||
if (std::isfinite(n.lat) && std::isfinite(n.lon) &&
|
||||
n.lat >= -90.0f && n.lat <= 90.0f &&
|
||||
n.lon >= -180.0f && n.lon <= 180.0f)
|
||||
{
|
||||
e.lat = n.lat;
|
||||
e.lon = n.lon;
|
||||
}
|
||||
else
|
||||
{
|
||||
e.lat = 0.0f;
|
||||
e.lon = 0.0f;
|
||||
}
|
||||
sanitized.push_back(std::move(e));
|
||||
}
|
||||
if (dropped > 0)
|
||||
{
|
||||
cerr << "*** WARN[" << m_section << "]: dropped " << dropped
|
||||
<< " node list entrie(s) with empty/invalid callsign after "
|
||||
"sanitization" << endl;
|
||||
}
|
||||
|
||||
m_reflector->onPeerNodeList(peerId(), sanitized);
|
||||
} /* TrunkLink::handleMsgTrunkNodeList */
|
||||
|
||||
|
||||
|
||||
@@ -124,6 +124,8 @@ class TrunkLink : public sigc::trackable
|
||||
{
|
||||
return m_paired ? !m_ib_cons.empty() : (m_inbound_con != nullptr);
|
||||
}
|
||||
// True when at least one direction (inbound or outbound) is ready.
|
||||
bool isActive(void) const;
|
||||
|
||||
// Accept an inbound connection from a peer that has already sent a hello
|
||||
void acceptInboundConnection(Async::FramedTcpConnection* con,
|
||||
@@ -228,7 +230,6 @@ class TrunkLink : public sigc::trackable
|
||||
TrunkLink(const TrunkLink&);
|
||||
TrunkLink& operator=(const TrunkLink&);
|
||||
|
||||
bool isActive(void) const;
|
||||
bool isOutboundReady(void) const;
|
||||
bool isInboundReady(void) const;
|
||||
bool isOwnedTG(uint32_t tg) const;
|
||||
|
||||
Reference in new issue
Block a user