From 05c4c88ff3800358a850f642bc802a4882036558 Mon Sep 17 00:00:00 2001 From: Audric Date: Sun, 5 Apr 2026 18:22:04 +0200 Subject: [PATCH] Require matching trunk section names and cap audio debug at 500 frames Trunk link matching now requires the peer's hello ID (section name) to match the local [TRUNK_x] section name. Both sides of a trunk link must use the same section name, agreed upon by both sysops. This replaces the previous matching by secret+prefix only. BREAKING CHANGE: existing deployments must rename their [TRUNK_x] sections so both sides use the same name for each link. Also cap TRUNK_DEBUG audio frame logging at 500 frames per trunk link to prevent excessive log output during sustained transmissions. --- src/svxlink/reflector/Reflector.cpp | 33 ++++++++++++++++++++--------- src/svxlink/reflector/TrunkLink.cpp | 21 ++++++++++++++++-- src/svxlink/reflector/TrunkLink.h | 1 + tests/generate_configs.py | 2 +- tests/test_trunk.py | 10 ++++----- tests/topology.py | 12 +++++++++-- 6 files changed, 59 insertions(+), 20 deletions(-) diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index 88391fd..aa6c8cd 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -2198,21 +2198,32 @@ void Reflector::trunkPendingFrameReceived(Async::FramedTcpConnection* con, safe_id.resize(64); } - // Find the matching TrunkLink by verifying the shared secret AND matching - // the peer's declared prefix against our configured REMOTE_PREFIX. - // This allows multiple trunk sections to share the same secret safely. + // Find the matching TrunkLink by section name, shared secret, and prefix. + // Both sides must use the same [TRUNK_x] section name — sysops agree on a + // shared link name. The peer's hello ID is its section name. TrunkLink* matched_link = nullptr; for (auto* link : m_trunk_links) { + // Section name must match (case-sensitive) + if (msg.id() != link->section()) + { + continue; + } + if (!msg.verify(link->secret())) { if (m_trunk_debug) { std::cout << "TRUNK [DEBUG]: peer '" << safe_id - << "' HMAC mismatch against " << link->section() - << std::endl; + << "' section matches " << link->section() + << " but HMAC mismatch" << std::endl; } - continue; + std::cerr << "*** ERROR: TRUNK inbound: peer '" << safe_id + << "' section matches " << link->section() + << " but authentication failed (wrong secret)" + << std::endl; + con->disconnect(); + return; } // Check if the peer's local_prefix matches this link's remote_prefix. @@ -2239,7 +2250,7 @@ void Reflector::trunkPendingFrameReceived(Async::FramedTcpConnection* con, matched_link = link; break; } - else if (m_trunk_debug) + else { std::string exp_str, peer_str; for (const auto& p : sorted_expected) @@ -2252,17 +2263,19 @@ void Reflector::trunkPendingFrameReceived(Async::FramedTcpConnection* con, if (!peer_str.empty()) peer_str += ","; peer_str += p; } - std::cout << "TRUNK [DEBUG]: peer '" << safe_id - << "' secret OK for " << link->section() + std::cerr << "*** ERROR: TRUNK inbound: peer '" << safe_id + << "' section matches " << link->section() << " but prefix mismatch: expected=[" << exp_str << "] got=[" << peer_str << "]" << std::endl; + con->disconnect(); + return; } } if (matched_link == nullptr) { std::cerr << "*** ERROR: TRUNK inbound: peer '" << safe_id - << "' failed authentication (no matching secret/prefix)" + << "' no matching section name" << std::endl; con->disconnect(); return; diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index 4073b1a..1755994 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -563,8 +563,25 @@ void TrunkLink::onFrameReceived(FramedTcpConnection* con, if (m_debug && header.type() != MsgTrunkHeartbeat::TYPE) { - cout << m_section << " [DEBUG]: rx " << (is_inbound ? "IB" : "OB") - << " type=" << header.type() << " len=" << data.size() << endl; + if (header.type() == MsgTrunkAudio::TYPE || + header.type() == MsgTrunkFlush::TYPE) + { + if (m_debug_frame_cnt < 500) + { + cout << m_section << " [DEBUG]: rx " << (is_inbound ? "IB" : "OB") + << " type=" << header.type() << " len=" << data.size() << endl; + if (++m_debug_frame_cnt == 500) + { + cout << m_section << " [DEBUG]: audio frame log limit reached (500)" + << " — suppressing further audio/flush debug" << endl; + } + } + } + else + { + cout << m_section << " [DEBUG]: rx " << (is_inbound ? "IB" : "OB") + << " type=" << header.type() << " len=" << data.size() << endl; + } } switch (header.type()) diff --git a/src/svxlink/reflector/TrunkLink.h b/src/svxlink/reflector/TrunkLink.h index eb2cb99..5df4b4b 100644 --- a/src/svxlink/reflector/TrunkLink.h +++ b/src/svxlink/reflector/TrunkLink.h @@ -141,6 +141,7 @@ class TrunkLink : public sigc::trackable Async::Config& m_cfg; std::string m_section; bool m_debug = false; + unsigned m_debug_frame_cnt = 0; std::string m_peer_host; uint16_t m_peer_port; std::string m_secret; diff --git a/tests/generate_configs.py b/tests/generate_configs.py index e4b4386..7b8f10b 100644 --- a/tests/generate_configs.py +++ b/tests/generate_configs.py @@ -47,7 +47,7 @@ def generate_reflector_conf(name: str) -> str: # Trunk sections for each peer reflector for peer_name in sorted(peers): peer = peers[peer_name] - section = T.trunk_section_name(peer_name) + section = T.trunk_section_name(name, peer_name) secret = T.trunk_secret(name, peer_name) lines += [ "", diff --git a/tests/test_trunk.py b/tests/test_trunk.py index 073a8cf..295accb 100644 --- a/tests/test_trunk.py +++ b/tests/test_trunk.py @@ -210,7 +210,7 @@ class TrunkPeer: self.sock = socket.create_connection((host, port), timeout=timeout) self.sock.settimeout(timeout) - def handshake(self, trunk_id: str = "TEST_PEER", local_prefix: str = TEST_PREFIX, + def handshake(self, trunk_id: str = "TRUNK_TEST", local_prefix: str = TEST_PREFIX, priority: int = None, secret: str = TEST_SECRET): if priority is None: priority = struct.unpack("!I", os.urandom(4))[0] @@ -552,7 +552,7 @@ class TestTrunkIntegration(unittest.TestCase): for peer in REFLECTOR_NAMES: if peer != name: wait_for_trunk_connected( - *_http(name), T.trunk_section_name(peer), timeout=30.0) + *_http(name), T.trunk_section_name(name, peer), timeout=30.0) labels = "\u2194".join(n.upper() for n in REFLECTOR_NAMES) sys.stderr.write(f"\r\033[K {G}\u2714{RST} Mesh connected ({labels})\n") sys.stderr.write(f"\033[2m{'─' * 50}\033[0m\n") @@ -584,7 +584,7 @@ class TestTrunkIntegration(unittest.TestCase): for peer in REFLECTOR_NAMES: if peer == name: continue - section = T.trunk_section_name(peer) + section = T.trunk_section_name(name, peer) self.assertTrue( status["trunks"][section]["connected"], f"{section} not connected on reflector-{name}") @@ -694,10 +694,10 @@ class TestTrunkIntegration(unittest.TestCase): # Verify NOT propagated to other peers (no trunk-to-trunk forwarding) time.sleep(1.0) - primary_section = T.trunk_section_name(self.PRIMARY) for name in REFLECTOR_NAMES: if name == self.PRIMARY: continue + primary_section = T.trunk_section_name(name, self.PRIMARY) status = get_status(*_http(name)) talkers = status["trunks"].get(primary_section, {}).get("active_talkers", {}) for tg in T.CLUSTER_TGS: @@ -777,7 +777,7 @@ class TestTrunkIntegration(unittest.TestCase): receiver = TrunkPeer() receiver.connect(*_trunk(self.PRIMARY)) rx_type, rx_fields = receiver.handshake( - trunk_id="TEST_RX", local_prefix=TEST_RX_PREFIX, + trunk_id="TRUNK_TEST_RX", local_prefix=TEST_RX_PREFIX, secret=TEST_RX_SECRET) self.assertEqual(rx_type, MSG_TRUNK_HELLO) diff --git a/tests/topology.py b/tests/topology.py index 7956e97..cbe660c 100644 --- a/tests/topology.py +++ b/tests/topology.py @@ -98,5 +98,13 @@ def service_name(name: str) -> str: def mapped_satellite_port(name: str) -> int: return REFLECTORS[name]["trunk_port_base"] + 303 -def trunk_section_name(peer_name: str) -> str: - return f"TRUNK_{peer_name.upper()}" +def trunk_section_name(name_a: str, name_b: str = "") -> str: + """Shared trunk section name for a link between two reflectors. + + Both sides must use the same section name. Convention: sorted pair. + If only one name given (legacy), returns TRUNK_ for test harness use. + """ + if not name_b: + return f"TRUNK_{name_a.upper()}" + pair = tuple(sorted([name_a, name_b])) + return f"TRUNK_{pair[0].upper()}_{pair[1].upper()}"