feat(alias): add LOCAL_ALIASES to re-home short local TGs under an owned prefix

Re-home a short local talk group (e.g. 8) under the reflector's own owned
prefix as a globally-unique number (e.g. 2620008) via a single [GLOBAL]
LOCAL_ALIASES entry, so it routes across the trunk mesh like any owned TG
while local users keep dialing the short number.

- Config: LOCAL_ALIASES=localTG:aliasTG[/dir] (dir in|out, omitted = two-way);
  aliasTG must be owned by a LOCAL_PREFIX. Rejects unowned alias, an owned
  short side, duplicate short keys, and numeric overflow.
- Boundary rewrite: prefix routing / owner-relay / peer-interest run on the
  long owned number; local delivery and talker arbitration run on the short
  number, bridged in the four inbound trunk handlers.
- Rewrite-on-select folds a client that dials the long number onto the short
  number (select + monitor), so the two are one conversation locally and on
  the mesh.
- Twin: external-talker notify uses the short number to match the audio
  mirror; disconnect cleanup folds to short so an aliased TG cannot wedge.
- /status exposes local_aliases; satellite nodes warn that the setting is
  ignored (a local alias belongs on the mesh parent that owns the prefix).
- Docs (PEER_PROTOCOL, CONCEPTS, conf template) and integration tests
  (tests/test_alias.py): two-way / in / out, rewrite-on-select, status,
  validation, disconnect-wedge, and duplicate-key coverage.
This commit is contained in:
Audric committed 2026-07-03 18:17:16 +02:00
1 parent 7e625dd514
commit 5da075f150
12 files changed
+1187 -64

No files matched your search

+135
View File
@@ -31,7 +31,10 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
****************************************************************************/
#include <cassert>
#include <cerrno>
#include <cstdlib>
#include <list>
#include <set>
#include <sstream>
#include <unistd.h>
#include <algorithm>
@@ -530,6 +533,15 @@ bool Reflector::initialize(Async::Config &cfg)
"accept child satellites; remove the [SATELLITE] block or "
"unset SATELLITE_OF.");
}
std::string aliases_probe;
if (cfg.getValue("GLOBAL", "LOCAL_ALIASES", aliases_probe) &&
!aliases_probe.empty())
{
geulog::warn("core",
"LOCAL_ALIASES is ignored on a satellite node (SATELLITE_OF is "
"set). A local alias must be configured on the parent reflector "
"that owns the prefix and participates in the trunk mesh.");
}
m_satellite_client = new SatelliteClient(this, cfg);
if (!m_satellite_client->initialize())
{
@@ -1748,6 +1760,21 @@ void Reflector::refreshStatus(void)
}
m_status["cluster_tgs"] = cluster_arr;
{
Json::Value aliases(Json::objectValue);
for (const auto& kv : m_alias_select) // long -> short, one per alias
{
Json::Value a(Json::objectValue);
a["alias"] = kv.first; // routable long number
a["direction"] =
(m_alias_route.count(kv.second) && m_alias_deliver.count(kv.first))
? "two-way"
: (m_alias_route.count(kv.second) ? "out" : "in");
aliases[std::to_string(kv.second)] = a; // keyed by short local number
}
m_status["local_aliases"] = aliases;
}
{
Json::Value sats(Json::objectValue);
for (auto& kv : m_satellite_con_map)
@@ -2440,6 +2467,10 @@ void Reflector::initTrunkLinks(void)
}
}
}
// Parse local aliases now that m_local_prefixes and m_all_prefixes are
// both fully populated, since validation calls isLocalTG().
parseLocalAliases();
} /* Reflector::initTrunkLinks */
@@ -3172,6 +3203,110 @@ bool Reflector::isLocalTG(uint32_t tg) const
} /* Reflector::isLocalTG */
void Reflector::parseLocalAliases(void)
{
m_alias_route.clear();
m_alias_deliver.clear();
m_alias_select.clear();
std::string aliases_str;
if (!m_cfg->getValue("GLOBAL", "LOCAL_ALIASES", aliases_str) ||
aliases_str.empty())
{
return;
}
std::istringstream ss(aliases_str);
std::string entry;
std::set<uint32_t> seen_short;
while (std::getline(ss, entry, ','))
{
// trim surrounding whitespace
size_t b = entry.find_first_not_of(" \t");
size_t e = entry.find_last_not_of(" \t");
if (b == std::string::npos) continue;
entry = entry.substr(b, e - b + 1);
// split optional "/dir" suffix
std::string dir;
size_t slash = entry.find('/');
if (slash != std::string::npos)
{
dir = entry.substr(slash + 1);
entry = entry.substr(0, slash);
}
if (!dir.empty() && dir != "in" && dir != "out")
{
geulog::error("core", "LOCAL_ALIASES: bad direction '", dir,
"' in entry (expected in|out); skipping");
continue;
}
size_t colon = entry.find(':');
if (colon == std::string::npos)
{
geulog::error("core", "LOCAL_ALIASES: entry '", entry,
"' missing ':'; skipping");
continue;
}
char* endp = nullptr;
errno = 0;
unsigned long local_tg = std::strtoul(entry.substr(0, colon).c_str(),
&endp, 10);
bool local_ok = (endp != nullptr && *endp == '\0' && local_tg > 0 &&
errno != ERANGE);
endp = nullptr;
errno = 0;
unsigned long alias_tg = std::strtoul(entry.substr(colon + 1).c_str(),
&endp, 10);
bool alias_ok = (endp != nullptr && *endp == '\0' && alias_tg > 0 &&
errno != ERANGE);
if (!local_ok || !alias_ok)
{
geulog::error("core", "LOCAL_ALIASES: non-numeric TG in entry '",
entry, "'; skipping");
continue;
}
if (!isLocalTG(static_cast<uint32_t>(alias_tg)))
{
geulog::error("core", "LOCAL_ALIASES: alias TG ", alias_tg,
" is not under any LOCAL_PREFIX; rejecting alias ",
local_tg, ":", alias_tg);
continue;
}
if (isLocalTG(static_cast<uint32_t>(local_tg)))
{
geulog::error("core", "LOCAL_ALIASES: local TG ", local_tg,
" is itself owned by a LOCAL_PREFIX; the short side of an "
"alias must be a non-owned number; rejecting alias ",
local_tg, ":", alias_tg);
continue;
}
uint32_t s = static_cast<uint32_t>(local_tg);
uint32_t l = static_cast<uint32_t>(alias_tg);
if (seen_short.count(s))
{
geulog::error("core", "LOCAL_ALIASES: duplicate local TG ", s,
"; keeping the first mapping, ignoring ", s, ":", l);
continue;
}
seen_short.insert(s);
if (dir != "in") m_alias_route[s] = l; // two-way or /out publishes
if (dir != "out") m_alias_deliver[l] = s; // two-way or /in delivers
m_alias_select[l] = s; // reserve number in all cases
geulog::info("core", "LOCAL_ALIAS ", s, " <-> ", l,
(dir.empty() ? " (two-way)" :
dir == "in" ? " (in)" : " (out)"));
}
} /* Reflector::parseLocalAliases */
bool Reflector::hasPrefixRoute(uint32_t tg) const
{
const std::string s = std::to_string(tg);
+30
View File
@@ -253,6 +253,29 @@ class Reflector : public sigc::trackable
// Used to gate owner-relay fanout of trunk-received audio.
bool isLocalTG(uint32_t tg) const;
// Local alias table (see docs/PEER_PROTOCOL.md "Local aliases"). A short
// local TG is re-homed under an owned prefix so it routes
// internationally. Populated from [GLOBAL] LOCAL_ALIASES.
//
// short -> long for the trunk wire (identity if not aliased)
uint32_t aliasToRoute(uint32_t tg) const
{
auto it = m_alias_route.find(tg);
return (it != m_alias_route.end()) ? it->second : tg;
}
// long -> short for local delivery (identity if not aliased or /out-only)
uint32_t aliasToLocal(uint32_t tg) const
{
auto it = m_alias_deliver.find(tg);
return (it != m_alias_deliver.end()) ? it->second : tg;
}
// long -> short for rewrite-on-select (identity if not aliased)
uint32_t aliasSelect(uint32_t tg) const
{
auto it = m_alias_select.find(tg);
return (it != m_alias_select.end()) ? it->second : tg;
}
// True iff any prefix in m_all_prefixes (local + every configured
// [TRUNK_x] REMOTE_PREFIX) prefix-matches this TG. Used to gate
// trunk-to-trunk fanout: a non-owner gateway forwards onward only if
@@ -456,6 +479,12 @@ class Reflector : public sigc::trackable
// initTrunkLinks() and refreshed by addTrunkLink/removeTrunkLink.
std::vector<std::string> m_local_prefixes;
std::vector<std::string> m_all_prefixes;
// Local alias table (see docs/PEER_PROTOCOL.md "Local aliases").
// A short local TG is re-homed under an owned prefix so it routes
// internationally. Populated from [GLOBAL] LOCAL_ALIASES.
std::map<uint32_t, uint32_t> m_alias_route; // short -> long (publish outbound)
std::map<uint32_t, uint32_t> m_alias_deliver; // long -> short (deliver inbound)
std::map<uint32_t, uint32_t> m_alias_select; // long -> short (rewrite-on-select, all dirs)
// 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;
@@ -540,6 +569,7 @@ class Reflector : public sigc::trackable
std::vector<std::string> collectAllTrunkPrefixes(void) const;
void refreshStatus(void);
void initTrunkLinks(void);
void parseLocalAliases(void);
void initTrunkServer(void);
void initTwinLink(void);
void initTwinServer(void);
+29 -10
View File
@@ -934,7 +934,18 @@ void ReflectorClient::handleSelectTG(std::istream& is)
sendError("Illegal MsgSelectTG protocol message received");
return;
}
if (msg.tg() != m_current_tg)
uint32_t sel_tg = msg.tg();
if (m_reflector != nullptr)
{
uint32_t folded = m_reflector->aliasSelect(sel_tg);
if (folded != sel_tg)
{
geulog::info("client", m_callsign, ": alias-fold select TG #", sel_tg,
" -> #", folded);
sel_tg = folded;
}
}
if (sel_tg != m_current_tg)
{
auto talker = TGHandler::instance()->talkerForTG(m_current_tg);
if (talker == this)
@@ -949,7 +960,7 @@ void ReflectorClient::handleSelectTG(std::istream& is)
sendUdpMsg(MsgUdpFlushSamples());
}
setTg(msg.tg());
setTg(sel_tg);
}
} /* ReflectorClient::handleSelectTG */
@@ -964,18 +975,26 @@ void ReflectorClient::handleTgMonitor(std::istream& is)
sendError("Illegal MsgTgMonitor protocol message received");
return;
}
auto tgs = msg.tgs();
auto it = tgs.cbegin();
while (it != tgs.end())
std::set<uint32_t> tgs;
for (const auto& tg : msg.tgs())
{
const auto& tg = *it;
if (!TGHandler::instance()->allowTgMonitoring(this, tg) || (tg == 0))
uint32_t mon_tg = tg;
if (m_reflector != nullptr)
{
geulog::info("client", m_callsign, ": Not allowed to monitor TG #", tg);
tgs.erase(it++);
mon_tg = m_reflector->aliasSelect(mon_tg);
if (mon_tg != tg)
{
geulog::info("client", m_callsign, ": alias-fold monitor TG #", tg,
" -> #", mon_tg);
}
}
if (!TGHandler::instance()->allowTgMonitoring(this, mon_tg) || (mon_tg == 0))
{
geulog::info("client", m_callsign, ": Not allowed to monitor TG #",
mon_tg);
continue;
}
++it;
tgs.insert(mon_tg);
}
{
std::ostringstream tglist;
+63 -49
View File
@@ -596,7 +596,8 @@ Json::Value TrunkLink::statusJson(void) const
Json::Value talkers(Json::objectValue);
for (uint32_t tg : m_peer_active_tgs)
{
const std::string cs = TGHandler::instance()->trunkTalkerForTG(tg);
const std::string cs =
TGHandler::instance()->trunkTalkerForTG(m_reflector->aliasToLocal(tg));
if (!cs.empty())
{
talkers[std::to_string(tg)] = cs;
@@ -782,67 +783,71 @@ void TrunkLink::onInboundDisconnected(Async::FramedTcpConnection* con,
void TrunkLink::onLocalTalkerStart(uint32_t tg, const std::string& callsign)
{
if (!isActive() || isBlacklisted(tg) || !isAllowed(tg)) return;
bool mapped = m_tg_map_out.count(tg) > 0;
if (!isActive()) return;
uint32_t rtg = m_reflector->aliasToRoute(tg); // short -> long for the mesh
if (isBlacklisted(rtg) || !isAllowed(rtg)) return;
bool mapped = m_tg_map_out.count(rtg) > 0;
if (!mapped &&
!isSharedTG(tg) && !m_reflector->isClusterTG(tg) &&
!isPeerInterestedTG(tg))
!isSharedTG(rtg) && !m_reflector->isClusterTG(rtg) &&
!isPeerInterestedTG(rtg))
{
return;
}
sendMsg(MsgPeerTalkerStart(mapTgOut(tg), callsign));
sendMsg(MsgPeerTalkerStart(mapTgOut(rtg), callsign));
} /* TrunkLink::onLocalTalkerStart */
void TrunkLink::onLocalTalkerStop(uint32_t tg)
{
if (!isActive() || isBlacklisted(tg) || !isAllowed(tg)) return;
bool mapped = m_tg_map_out.count(tg) > 0;
if (!isActive()) return;
uint32_t rtg = m_reflector->aliasToRoute(tg);
if (isBlacklisted(rtg) || !isAllowed(rtg)) return;
bool mapped = m_tg_map_out.count(rtg) > 0;
if (!mapped &&
!isSharedTG(tg) && !m_reflector->isClusterTG(tg) &&
!isPeerInterestedTG(tg))
!isSharedTG(rtg) && !m_reflector->isClusterTG(rtg) &&
!isPeerInterestedTG(rtg))
{
return;
}
// If we cleared our local talker because we were yielding to this peer,
// don't send TrunkTalkerStop — the peer already owns the TG.
if (m_yielded_tgs.count(tg))
if (m_yielded_tgs.count(tg)) // yielded is keyed by the short/local number
{
return;
}
sendMsg(MsgPeerTalkerStop(mapTgOut(tg)));
sendMsg(MsgPeerTalkerStop(mapTgOut(rtg)));
} /* TrunkLink::onLocalTalkerStop */
void TrunkLink::onLocalAudio(uint32_t tg, const std::vector<uint8_t>& audio)
{
if (!isActive() || isBlacklisted(tg) || !isAllowed(tg)
|| m_yielded_tgs.count(tg))
{
return;
}
bool mapped = m_tg_map_out.count(tg) > 0;
if (!isActive() || m_yielded_tgs.count(tg)) return;
uint32_t rtg = m_reflector->aliasToRoute(tg);
if (isBlacklisted(rtg) || !isAllowed(rtg)) return;
bool mapped = m_tg_map_out.count(rtg) > 0;
if (!mapped &&
!isSharedTG(tg) && !m_reflector->isClusterTG(tg) &&
!isPeerInterestedTG(tg))
!isSharedTG(rtg) && !m_reflector->isClusterTG(rtg) &&
!isPeerInterestedTG(rtg))
{
return;
}
sendMsg(MsgPeerAudio(mapTgOut(tg), audio));
sendMsg(MsgPeerAudio(mapTgOut(rtg), audio));
} /* TrunkLink::onLocalAudio */
void TrunkLink::onLocalFlush(uint32_t tg)
{
if (!isActive() || isBlacklisted(tg) || !isAllowed(tg)) return;
bool mapped = m_tg_map_out.count(tg) > 0;
if (!isActive()) return;
uint32_t rtg = m_reflector->aliasToRoute(tg);
if (isBlacklisted(rtg) || !isAllowed(rtg)) return;
bool mapped = m_tg_map_out.count(rtg) > 0;
if (!mapped &&
!isSharedTG(tg) && !m_reflector->isClusterTG(tg) &&
!isPeerInterestedTG(tg))
!isSharedTG(rtg) && !m_reflector->isClusterTG(rtg) &&
!isPeerInterestedTG(rtg))
{
return;
}
sendMsg(MsgPeerFlush(mapTgOut(tg)));
sendMsg(MsgPeerFlush(mapTgOut(rtg)));
} /* TrunkLink::onLocalFlush */
@@ -1086,34 +1091,36 @@ void TrunkLink::handleMsgPeerTalkerStart(std::istream& is)
return;
}
uint32_t deliver_tg = m_reflector->aliasToLocal(local_tg);
// Tie-break: if we already have a local talker on this TG, decide who wins.
// Lower priority value wins. If equal (shouldn't happen), local wins.
ReflectorClient* local_talker = TGHandler::instance()->talkerForTG(local_tg);
ReflectorClient* local_talker = TGHandler::instance()->talkerForTG(deliver_tg);
if (local_talker != nullptr)
{
if (m_priority <= m_peer_priority)
{
// We win — ignore peer's claim
geulog::info("trunk", m_section, ": TG #", local_tg,
geulog::info("trunk", m_section, ": TG #", deliver_tg,
" conflict — local wins (our priority=", m_priority,
" <= peer=", m_peer_priority, ")");
return;
}
// We defer — clear local talker and accept remote
geulog::info("trunk", m_section, ": TG #", local_tg,
geulog::info("trunk", m_section, ": TG #", deliver_tg,
" conflict — deferring to peer (our priority=", m_priority,
" > peer=", m_peer_priority, ")");
m_yielded_tgs.insert(local_tg);
TGHandler::instance()->setTalkerForTG(local_tg, nullptr);
m_yielded_tgs.insert(deliver_tg);
TGHandler::instance()->setTalkerForTG(deliver_tg, nullptr);
// onTalkerUpdated will fire; Reflector must not re-send TrunkTalkerStart
// for this TG since it's in m_yielded_tgs (checked in Reflector.cpp)
}
m_peer_active_tgs.insert(local_tg);
m_peer_interested_tgs[local_tg] = std::time(nullptr);
TGHandler::instance()->setTrunkTalkerForTGViaPeer(local_tg, msg.callsign(),
TGHandler::instance()->setTrunkTalkerForTGViaPeer(deliver_tg, msg.callsign(),
peerId());
m_reflector->notifyExternalTrunkTalkerStart(local_tg, m_section, msg.callsign());
m_reflector->notifyExternalTrunkTalkerStart(deliver_tg, m_section, msg.callsign());
// Prefix-routed fanout: forward to every other trunk peer if we have a
// route for this TG in our prefix table. isSharedTG inside the per-trunk
@@ -1151,10 +1158,12 @@ void TrunkLink::handleMsgPeerTalkerStop(std::istream& is)
return;
}
m_yielded_tgs.erase(local_tg);
uint32_t deliver_tg = m_reflector->aliasToLocal(local_tg);
m_yielded_tgs.erase(deliver_tg);
m_peer_active_tgs.erase(local_tg);
TGHandler::instance()->clearTrunkTalkerForTG(local_tg);
m_reflector->notifyExternalTrunkTalkerStop(local_tg, m_section);
TGHandler::instance()->clearTrunkTalkerForTG(deliver_tg);
m_reflector->notifyExternalTrunkTalkerStop(deliver_tg, m_section);
// Prefix-routed fanout (mirrors talker-start): only fan out if we have
// a route. Cluster TGs without a prefix match stay single-hop.
@@ -1188,6 +1197,8 @@ void TrunkLink::handleMsgPeerAudio(std::istream& is)
return;
}
uint32_t deliver_tg = m_reflector->aliasToLocal(local_tg);
// Only forward audio if this peer has claimed the TG via TalkerStart
if (m_peer_active_tgs.find(local_tg) == m_peer_active_tgs.end())
{
@@ -1195,7 +1206,7 @@ void TrunkLink::handleMsgPeerAudio(std::istream& is)
}
// PTY-driven mute: drop audio if the current peer talker is muted
std::string cs = TGHandler::instance()->trunkTalkerForTG(local_tg);
std::string cs = TGHandler::instance()->trunkTalkerForTG(deliver_tg);
if (!cs.empty() && isCallsignMuted(cs))
{
return;
@@ -1209,21 +1220,21 @@ void TrunkLink::handleMsgPeerAudio(std::istream& is)
MsgUdpAudio udp_msg(msg.audio());
m_reflector->broadcastUdpMsg(udp_msg,
ReflectorClient::mkOrFilter(
ReflectorClient::TgFilter(local_tg),
ReflectorClient::TgFilter(deliver_tg),
ReflectorClient::mkAndFilter(
ReflectorClient::TgMonitorFilter(local_tg),
ReflectorClient::TgMonitorFilter(deliver_tg),
ReflectorClient::mkAndFilter(
ReflectorClient::PassiveObserverFilter(),
ReflectorClient::EarliestMonitorTalkerFilter(local_tg)))));
ReflectorClient::EarliestMonitorTalkerFilter(deliver_tg)))));
// Forward trunk audio to connected satellites
m_reflector->forwardAudioToSatellitesExcept(nullptr, local_tg, msg.audio());
m_reflector->forwardAudioToSatellitesExcept(nullptr, deliver_tg, msg.audio());
// Mirror inbound trunk audio to the [TWIN] partner so PAIRED inbound
// (sticky on one twin) still reaches the non-sticky twin's local clients
// and its satellites. TwinLink::handleMsgPeerAudio does not re-forward to
// trunks, so no echo back to the originating peer.
m_reflector->forwardTrunkAudioToTwin(local_tg, msg.audio());
m_reflector->forwardTrunkAudioToTwin(deliver_tg, msg.audio());
// Prefix-routed fanout (mirrors talker-start): only fan out if we have
// a route. Cluster TGs without a prefix match stay single-hop.
@@ -1256,20 +1267,22 @@ void TrunkLink::handleMsgPeerFlush(std::istream& is)
return;
}
uint32_t deliver_tg = m_reflector->aliasToLocal(local_tg);
m_reflector->broadcastUdpMsg(MsgUdpFlushSamples(),
ReflectorClient::mkOrFilter(
ReflectorClient::TgFilter(local_tg),
ReflectorClient::TgFilter(deliver_tg),
ReflectorClient::mkAndFilter(
ReflectorClient::TgMonitorFilter(local_tg),
ReflectorClient::TgMonitorFilter(deliver_tg),
ReflectorClient::mkAndFilter(
ReflectorClient::PassiveObserverFilter(),
ReflectorClient::EarliestMonitorTalkerFilter(local_tg)))));
ReflectorClient::EarliestMonitorTalkerFilter(deliver_tg)))));
// Forward trunk flush to connected satellites
m_reflector->forwardFlushToSatellitesExcept(nullptr, local_tg);
m_reflector->forwardFlushToSatellitesExcept(nullptr, deliver_tg);
// Mirror flush to the [TWIN] partner — pairs with the audio mirror above.
m_reflector->forwardTrunkFlushToTwin(local_tg);
m_reflector->forwardTrunkFlushToTwin(deliver_tg);
// Prefix-routed fanout (mirrors talker-start): only fan out if we have
// a route. Cluster TGs without a prefix match stay single-hop.
@@ -1864,8 +1877,9 @@ void TrunkLink::clearPeerTalkerState(void)
{
for (uint32_t tg : m_peer_active_tgs)
{
TGHandler::instance()->clearTrunkTalkerForTG(tg);
m_reflector->notifyExternalTrunkTalkerStop(tg, m_section);
uint32_t deliver_tg = m_reflector->aliasToLocal(tg);
TGHandler::instance()->clearTrunkTalkerForTG(deliver_tg);
m_reflector->notifyExternalTrunkTalkerStop(deliver_tg, m_section);
}
m_peer_active_tgs.clear();
m_yielded_tgs.clear();
@@ -13,6 +13,12 @@ LISTEN_PORT=5300
#CODECS=OPUS
TG_FOR_V1_CLIENTS=999
#LOCAL_PREFIX=1
# LOCAL_ALIASES — re-home short local TGs under an owned prefix so they are
# reachable internationally by their globally-unique number. Comma-separated
# entries "localTG:aliasTG[/dir]"; dir is "in" or "out" (omitted = two-way).
# The aliasTG MUST fall under a LOCAL_PREFIX or the entry is rejected at load.
# Local users keep dialing localTG; the trunk mesh sees aliasTG.
#LOCAL_ALIASES=8:2620008,9:2620009/out,4001:2620400/in
#CLUSTER_TGS=222,2221,91
#RANDOM_QSY_RANGE=12399:100
#HTTP_SRV_PORT=8080