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:
Audric committed 2026-04-16 21:25:37 +02:00
1 parent 0700e01453
commit 7b95d0d2ce
10 files changed
+399 -9

No files matched your search

+42
View File
@@ -166,6 +166,45 @@ def build_heartbeat() -> bytes:
return struct.pack("!H", MSG_TRUNK_HEARTBEAT)
def build_node_list(nodes) -> bytes:
"""Build a MsgTrunkNodeList frame.
`nodes` is an iterable of (callsign, tg[, lat, lon, qth_name]) tuples.
Unspecified lat/lon default to 0.0 and qth_name to "".
Matches the on-wire layout: vec<string> callsigns, vec<u32> tgs,
vec<float> lat, vec<float> lon, vec<string> qth_names — each vector
prefixed with a u16 element count.
"""
norm = []
for n in nodes:
if len(n) == 2:
cs, tg = n
lat, lon, qth = 0.0, 0.0, ""
elif len(n) == 5:
cs, tg, lat, lon, qth = n
else:
raise ValueError("node tuple must be (cs, tg) or (cs, tg, lat, lon, qth)")
norm.append((cs, tg, lat, lon, qth))
payload = struct.pack("!H", MSG_TRUNK_NODE_LIST)
payload += struct.pack("!H", len(norm))
for cs, _tg, _lat, _lon, _qth in norm:
payload += pack_string(cs)
payload += struct.pack("!H", len(norm))
for _cs, tg, _lat, _lon, _qth in norm:
payload += struct.pack("!I", tg)
payload += struct.pack("!H", len(norm))
for _cs, _tg, lat, _lon, _qth in norm:
payload += struct.pack("!f", lat)
payload += struct.pack("!H", len(norm))
for _cs, _tg, _lat, lon, _qth in norm:
payload += struct.pack("!f", lon)
payload += struct.pack("!H", len(norm))
for _cs, _tg, _lat, _lon, qth in norm:
payload += pack_string(qth)
return payload
# ---------------------------------------------------------------------------
# Message parser
# ---------------------------------------------------------------------------
@@ -271,6 +310,9 @@ class TrunkPeer:
def send_heartbeat(self):
send_frame(self.sock, build_heartbeat())
def send_node_list(self, nodes):
send_frame(self.sock, build_node_list(nodes))
def recv_msg(self, timeout: float = 5.0):
old = self.sock.gettimeout()
self.sock.settimeout(timeout)