From fa6f137e1cf630fa5425073042793fa5a3d07f81 Mon Sep 17 00:00:00 2001 From: Audric Date: Sat, 18 Apr 2026 23:33:57 +0200 Subject: [PATCH] twin: expose partner roster in /status (fixes #3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reflector::refreshStatus() now emits a m_status["twin"] object (host, port, connection/hello state, prefix, peer_id, priorities) mirroring the existing trunks[] pattern. To populate the partner's connected-stations list, TwinLink now carries MsgTrunkNodeList (type 121) over the [TWIN] socket — reusing the trunk wire message, no new protocol type. Local roster changes flow via Reflector::sendNodeListToAllPeers() -> TwinLink::onLocalNodeListUpdated(); incoming lists are sanitized and cached in m_partner_nodes, surfaced as twin.nodes. Without this, [TWIN]-paired reflectors could not observe each other's rosters from /status — the data was not on the wire at all (TwinLink only carried talker state). --- src/svxlink/reflector/Reflector.cpp | 14 +++ src/svxlink/reflector/TwinLink.cpp | 127 ++++++++++++++++++++++++++++ src/svxlink/reflector/TwinLink.h | 22 ++++- tests/test_twin.py | 53 ++++++++++++ 4 files changed, 214 insertions(+), 2 deletions(-) diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index d10dd3f..8340f95 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -1678,6 +1678,15 @@ void Reflector::refreshStatus(void) } m_status["trunks"] = trunks; + if (m_twin_link != nullptr) + { + m_status["twin"] = m_twin_link->statusJson(); + } + else + { + m_status.removeMember("twin"); + } + Json::Value cluster_arr(Json::arrayValue); for (uint32_t tg : m_cluster_tgs) { @@ -3133,6 +3142,11 @@ void Reflector::sendNodeListToAllPeers(void) link->sendNodeList(nodes); } + if (m_twin_link != nullptr) + { + m_twin_link->onLocalNodeListUpdated(nodes); + } + if (m_mqtt != nullptr) { m_mqtt->publishLocalNodes(nodes); diff --git a/src/svxlink/reflector/TwinLink.cpp b/src/svxlink/reflector/TwinLink.cpp index 8dc208f..25c7dce 100644 --- a/src/svxlink/reflector/TwinLink.cpp +++ b/src/svxlink/reflector/TwinLink.cpp @@ -34,6 +34,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include #include #include +#include /**************************************************************************** @@ -70,6 +71,44 @@ using namespace Async; using namespace sigc; +namespace { + +// Byte-bound and strip control chars / ':' from an identifier coming over +// the wire. Matches the sanitizer used in TrunkLink for node-list entries; +// the twin partner is HMAC-authenticated but wire data still flows into +// logs and /status JSON, so keep the same hygiene. +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(c); + if (u < 0x20 || u == 0x7f) continue; + if (c == ':') continue; + out += c; + if (out.size() >= max_len) break; + } + return out; +} + +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(c); + if (u < 0x20 || u == 0x7f) continue; + out += c; + if (out.size() >= max_bytes) break; + } + return out; +} + +} /* anonymous namespace */ + + /**************************************************************************** * * TwinLink public methods @@ -268,6 +307,14 @@ void TwinLink::onExternalTrunkTalkerStop(uint32_t tg, } /* TwinLink::onExternalTrunkTalkerStop */ +void TwinLink::onLocalNodeListUpdated( + const std::vector& nodes) +{ + if (!isActive()) return; + sendMsg(MsgTrunkNodeList(nodes)); +} /* TwinLink::onLocalNodeListUpdated */ + + bool TwinLink::isActive(void) const { return (m_con.isConnected() && m_ob_hello_received) || @@ -275,6 +322,41 @@ bool TwinLink::isActive(void) const } /* TwinLink::isActive */ +Json::Value TwinLink::statusJson(void) const +{ + Json::Value obj(Json::objectValue); + obj["host"] = m_peer_host; + obj["port"] = m_peer_port; + obj["connected"] = isActive(); + obj["outbound_connected"] = m_con.isConnected(); + obj["outbound_hello"] = m_ob_hello_received; + obj["inbound_connected"] = (m_inbound_con != nullptr); + obj["inbound_hello"] = m_ib_hello_received; + obj["local_prefix"] = m_local_prefix; + obj["peer_id"] = m_peer_id_received; + obj["priority"] = static_cast(m_priority); + obj["peer_priority"] = static_cast(m_peer_priority); + + Json::Value nodes_arr(Json::arrayValue); + for (const auto& n : m_partner_nodes) + { + Json::Value entry(Json::objectValue); + entry["callsign"] = n.callsign; + entry["tg"] = static_cast(n.tg); + if (n.lat != 0.0f || n.lon != 0.0f) + { + entry["lat"] = n.lat; + entry["lon"] = n.lon; + } + if (!n.qth_name.empty()) entry["qth_name"] = n.qth_name; + nodes_arr.append(entry); + } + obj["nodes"] = nodes_arr; + + return obj; +} /* TwinLink::statusJson */ + + /**************************************************************************** * * TwinLink private methods @@ -370,6 +452,9 @@ void TwinLink::onFrameReceived(FramedTcpConnection* con, case MsgTrunkFlush::TYPE: handleMsgTrunkFlush(ss); break; + case MsgTrunkNodeList::TYPE: + handleMsgTrunkNodeList(ss); + break; case MsgTwinExtTalkerStart::TYPE: handleMsgTwinExtTalkerStart(ss); break; @@ -529,6 +614,48 @@ void TwinLink::handleMsgTwinExtTalkerStop(std::istream& is) } /* TwinLink::handleMsgTwinExtTalkerStop */ +void TwinLink::handleMsgTrunkNodeList(std::istream& is) +{ + MsgTrunkNodeList msg; + if (!msg.unpack(is)) + { + geulog::error("twin", "TWIN: Failed to unpack MsgTrunkNodeList"); + return; + } + + std::vector 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); + 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; + } + sanitized.push_back(std::move(e)); + } + if (dropped > 0) + { + geulog::warn("twin", "TWIN: dropped ", dropped, + " node list entrie(s) with empty/invalid callsign"); + } + + m_partner_nodes = std::move(sanitized); +} /* TwinLink::handleMsgTrunkNodeList */ + + void TwinLink::heartbeatTick(Async::Timer* /*t*/) { // Outbound heartbeat diff --git a/src/svxlink/reflector/TwinLink.h b/src/svxlink/reflector/TwinLink.h index b86decf..b3a373a 100644 --- a/src/svxlink/reflector/TwinLink.h +++ b/src/svxlink/reflector/TwinLink.h @@ -43,6 +43,8 @@ logic — the pair shares everything. #include #include +#include + /**************************************************************************** * @@ -56,6 +58,15 @@ logic — the pair shares everything. #include +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "ReflectorMsg.h" + + /**************************************************************************** * * Forward declarations @@ -63,8 +74,6 @@ logic — the pair shares everything. ****************************************************************************/ class Reflector; -class MsgTrunkHello; -class ReflectorMsg; class TwinLink { @@ -92,12 +101,19 @@ class TwinLink void onExternalTrunkTalkerStop(uint32_t tg, const std::string& peer_id); + // Called by Reflector when the local node roster changes, to mirror + // our connected-stations list to the twin partner. + void onLocalNodeListUpdated( + const std::vector& nodes); + bool isActive(void) const; const std::string& partnerHost(void) const { return m_peer_host; } uint16_t partnerPort(void) const { return m_peer_port; } const std::string& secret(void) const { return m_secret; } const std::string& localPrefix(void) const { return m_local_prefix; } + Json::Value statusJson(void) const; + private: using FramedTcpClient = Async::TcpPrioClient; @@ -124,6 +140,7 @@ class TwinLink bool m_ib_hello_received; unsigned m_ib_hb_tx_cnt; unsigned m_ib_hb_rx_cnt; + std::vector m_partner_nodes; // No copy TwinLink(const TwinLink&); @@ -140,6 +157,7 @@ class TwinLink void handleMsgTrunkAudio(std::istream& is); void handleMsgTrunkFlush(std::istream& is); void handleMsgTrunkHeartbeat(void); + void handleMsgTrunkNodeList(std::istream& is); void handleMsgTwinExtTalkerStart(std::istream& is); void handleMsgTwinExtTalkerStop(std::istream& is); void heartbeatTick(Async::Timer*); diff --git a/tests/test_twin.py b/tests/test_twin.py index 4e7730f..53ebb87 100644 --- a/tests/test_twin.py +++ b/tests/test_twin.py @@ -628,6 +628,59 @@ class TestTwinIntegration(unittest.TestCase): sender.close() sat.close() + # ------------------------------------------------------------------ + # Test 11: Twin partner's node roster appears in /status.twin.nodes + # ------------------------------------------------------------------ + def test_11_twin_partner_nodelist_in_status(self): + """A client authenticated on ref1 appears in ref2's /status under + twin.nodes, and disappears after it disconnects. + + Exercises the roster path added for issue #3: + ref1 client auth -> Reflector::sendNodeListToAllPeers + -> TwinLink::onLocalNodeListUpdated (MsgTrunkNodeList over [TWIN]) + -> ref2.handleMsgTrunkNodeList -> m_partner_nodes + -> /status.twin.nodes + """ + client = ClientPeer() + try: + ref1_client_port = T.twin_mapped_client_port("ref1") + client.connect(HOST, ref1_client_port) + client.authenticate(callsign=CLIENT_CALLSIGN, + password=CLIENT_PASSWORD) + + def callsign_on_twin(): + status = get_status(*_http("ref2")) + twin = status.get("twin", {}) + nodes = twin.get("nodes", []) + return any(n.get("callsign") == CLIENT_CALLSIGN for n in nodes) + + wait_until( + callsign_on_twin, + timeout=10.0, + interval=0.25, + msg=f"{CLIENT_CALLSIGN} did not appear in ref2 /status.twin.nodes " + f"after login on ref1 — MsgTrunkNodeList may not be " + f"flowing over the [TWIN] link", + ) + finally: + client.close() + + # After disconnect, ref1 re-publishes its (now-empty) roster; + # ref2.twin.nodes should drop the callsign. + def callsign_gone_from_twin(): + status = get_status(*_http("ref2")) + twin = status.get("twin", {}) + nodes = twin.get("nodes", []) + return not any(n.get("callsign") == CLIENT_CALLSIGN for n in nodes) + + wait_until( + callsign_gone_from_twin, + timeout=10.0, + interval=0.25, + msg=f"{CLIENT_CALLSIGN} still in ref2 /status.twin.nodes after " + f"disconnect from ref1", + ) + def docker_compose_output(*args) -> str: """Return the stdout+stderr of a docker-compose command as text."""