From 53a9a6fc755c8f3cf8c77f3041207597b0a2224c Mon Sep 17 00:00:00 2001 From: Audric Date: Thu, 26 Mar 2026 10:55:47 +0100 Subject: [PATCH] Support overlapping TG prefixes with longest-prefix-match routing isSharedTG now finds the longest matching remote prefix and verifies no other prefix in the mesh is more specific. Each TrunkLink receives the full set of all prefixes at startup via setAllPrefixes(). initTrunkLinks is split into two passes: collect all prefixes first, then create links with full knowledge. --- docs/TRUNK_PROTOCOL.md | 23 +++++++++++++++++++++-- src/svxlink/reflector/Reflector.cpp | 9 ++++++++- src/svxlink/reflector/TrunkLink.cpp | 29 ++++++++++++++++++++++++++--- src/svxlink/reflector/TrunkLink.h | 5 +++++ 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/docs/TRUNK_PROTOCOL.md b/docs/TRUNK_PROTOCOL.md index 7d41ec5..0ef7969 100644 --- a/docs/TRUNK_PROTOCOL.md +++ b/docs/TRUNK_PROTOCOL.md @@ -46,7 +46,9 @@ dedicated port (default 5302, separate from the client port 5300). ## TG Ownership and Routing A TG number belongs to the reflector whose `LOCAL_PREFIX` is a string prefix -of the TG's decimal representation: +of the TG's decimal representation. When multiple prefixes match (because one +prefix is itself a prefix of another), the **longest matching prefix wins** — +analogous to longest-prefix-match in IP routing. | TG number | Decimal string | Owned by | |-----------|---------------|----------| @@ -58,7 +60,24 @@ of the TG's decimal representation: | 25 | "25" | prefix "2" | | 30 | "30" | prefix "3" | -Ownership check: `std::to_string(tg).substr(0, prefix.size()) == prefix` +### Overlapping Prefixes + +Prefixes may overlap — one prefix can be a prefix of another. This enables +hierarchical TG numbering schemes. For example, with prefixes `"12"` and +`"120"`: + +| TG number | Matches | Longest match | Owned by | +|-----------|----------------|---------------|--------------| +| 125 | "12" | "12" (len 2) | prefix "12" | +| 1205 | "12" and "120" | "120" (len 3) | prefix "120" | +| 12 | "12" | "12" (len 2) | prefix "12" | +| 1200 | "12" and "120" | "120" (len 3) | prefix "120" | + +At startup, each `TrunkLink` receives the complete set of all prefixes in the +mesh (local + all remotes). `isSharedTG(tg)` finds the best matching remote +prefix for the peer, then verifies no other prefix in the mesh is a longer +match. If a longer match exists, the TG belongs to that other reflector +instead. In a full mesh of N reflectors, each `[TRUNK_x]` section on reflector A points at reflector B and carries only TGs whose decimal string starts with B's prefix diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index c239ee7..44c1302 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -1994,14 +1994,16 @@ void Reflector::initTrunkLinks(void) } } + // First pass: collect all remote prefixes so we can do longest-prefix-match + std::vector trunk_sections; for (const auto& section : m_cfg->listSections()) { if (section.substr(0, 6) != "TRUNK_") { continue; } + trunk_sections.push_back(section); - // Collect remote prefixes for validation std::string remote_prefix_str; m_cfg->getValue(section, "REMOTE_PREFIX", remote_prefix_str); { @@ -2014,10 +2016,15 @@ void Reflector::initTrunkLinks(void) if (!token.empty()) all_prefixes.push_back(token); } } + } + // Second pass: create trunk links with full prefix knowledge + for (const auto& section : trunk_sections) + { auto* link = new TrunkLink(this, *m_cfg, section); if (link->initialize()) { + link->setAllPrefixes(all_prefixes); m_trunk_links.push_back(link); } else diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index 3825b52..4b5a318 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -199,13 +199,36 @@ bool TrunkLink::initialize(void) bool TrunkLink::isSharedTG(uint32_t tg) const { const std::string s = std::to_string(tg); + + // Find the best (longest) matching remote prefix for this peer + size_t best_remote_len = 0; for (const auto& prefix : m_remote_prefix) { if (s.size() >= prefix.size() && - s.compare(0, prefix.size(), prefix) == 0) - return true; + s.compare(0, prefix.size(), prefix) == 0 && + prefix.size() > best_remote_len) + { + best_remote_len = prefix.size(); + } } - return false; + if (best_remote_len == 0) + { + return false; // no remote prefix matches at all + } + + // Check if any prefix in the mesh is a longer match — if so, that other + // reflector is more specific and this TG doesn't belong to this peer. + for (const auto& prefix : m_all_prefixes) + { + if (prefix.size() > best_remote_len && + s.size() >= prefix.size() && + s.compare(0, prefix.size(), prefix) == 0) + { + return false; // a longer prefix claims this TG + } + } + + return true; } /* TrunkLink::isSharedTG */ diff --git a/src/svxlink/reflector/TrunkLink.h b/src/svxlink/reflector/TrunkLink.h index 5b60c25..a8affa8 100644 --- a/src/svxlink/reflector/TrunkLink.h +++ b/src/svxlink/reflector/TrunkLink.h @@ -92,6 +92,10 @@ class TrunkLink : public sigc::trackable bool initialize(void); bool isSharedTG(uint32_t tg) const; + void setAllPrefixes(const std::vector& all_prefixes) + { + m_all_prefixes = all_prefixes; + } const std::string& section(void) const { return m_section; } Json::Value statusJson(void) const; @@ -128,6 +132,7 @@ class TrunkLink : public sigc::trackable Async::Timer m_heartbeat_timer; unsigned m_hb_tx_cnt; unsigned m_hb_rx_cnt; + std::vector m_all_prefixes; // all prefixes in the mesh // TGs where we suppressed our local talker to defer to the peer std::set m_yielded_tgs; // TGs currently held by this specific trunk peer (for scoped cleanup)