From 9c6f27e12bd10ed5817e9f4b37679d8be735e11f Mon Sep 17 00:00:00 2001 From: Audric Date: Wed, 15 Apr 2026 14:41:18 +0200 Subject: [PATCH] Load trunk peer definitions from Redis at startup When [REDIS] is configured, Reflector::initialize synthesizes [TRUNK_*] sections from 'trunk:
:peer' hashes before the trunk discovery loop. .conf [TRUNK_*] sections are ignored (with a warning) when Redis is configured. The importer --import-conf-to-redis now also writes peer definitions into 'trunk:
:peer' so existing configs can migrate. --- src/svxlink/reflector/RedisStore.cpp | 59 ++++++++++++++++++++++++++ src/svxlink/reflector/RedisStore.h | 10 +++++ src/svxlink/reflector/Reflector.cpp | 31 ++++++++++++++ src/svxlink/reflector/svxreflector.cpp | 29 +++++++++++++ 4 files changed, 129 insertions(+) diff --git a/src/svxlink/reflector/RedisStore.cpp b/src/svxlink/reflector/RedisStore.cpp index eed8d23..c98da85 100644 --- a/src/svxlink/reflector/RedisStore.cpp +++ b/src/svxlink/reflector/RedisStore.cpp @@ -194,6 +194,65 @@ std::map RedisStore::loadTrunkTgMap(const std::string& secti return out; } +std::map +RedisStore::loadTrunkPeers(void) +{ + std::map out; + if (!m_sync) return out; + std::string pattern = keyFor("trunk:*:peer"); + redisReply* r = (redisReply*)redisCommand(m_sync, "KEYS %s", pattern.c_str()); + if (!r || r->type != REDIS_REPLY_ARRAY) { + if (r) freeReplyObject(r); + return out; + } + std::vector matched_keys; + for (size_t i = 0; i < r->elements; ++i) { + if (r->element[i]->type == REDIS_REPLY_STRING) { + matched_keys.emplace_back(r->element[i]->str, r->element[i]->len); + } + } + freeReplyObject(r); + + // Extract section name: strip ":trunk:" prefix and ":peer" suffix. + std::string left = keyFor("trunk:"); + const std::string right = ":peer"; + for (const std::string& key : matched_keys) { + if (key.size() < left.size() + right.size()) continue; + if (key.substr(0, left.size()) != left) continue; + if (key.substr(key.size() - right.size()) != right) continue; + std::string section = key.substr(left.size(), + key.size() - left.size() - right.size()); + if (section.empty()) continue; + + redisReply* h = (redisReply*)redisCommand(m_sync, "HGETALL %s", key.c_str()); + if (!h || h->type != REDIS_REPLY_ARRAY) { + if (h) freeReplyObject(h); + continue; + } + TrunkPeerConfig cfg; + for (size_t i = 0; i + 1 < h->elements; i += 2) { + std::string field(h->element[i]->str, h->element[i]->len); + std::string value(h->element[i+1]->str, h->element[i+1]->len); + if (field == "host") cfg.host = value; + else if (field == "port") cfg.port = value; + else if (field == "secret") cfg.secret = value; + else if (field == "remote_prefix") cfg.remote_prefix = value; + else if (field == "peer_id") cfg.peer_id = value; + } + freeReplyObject(h); + + // Require host + secret + remote_prefix + if (cfg.host.empty() || cfg.secret.empty() || cfg.remote_prefix.empty()) { + std::cerr << "*** WARN: RedisStore: trunk peer " << section + << " missing required fields (host/secret/remote_prefix); " + << "skipping" << std::endl; + continue; + } + out[section] = std::move(cfg); + } + return out; +} + void RedisStore::publishConfigChanged(const std::string& scope) { if (!m_sync) return; std::string ch = channelName(); diff --git a/src/svxlink/reflector/RedisStore.h b/src/svxlink/reflector/RedisStore.h index 67067ae..5ac50d6 100644 --- a/src/svxlink/reflector/RedisStore.h +++ b/src/svxlink/reflector/RedisStore.h @@ -32,6 +32,15 @@ class RedisStore : public sigc::trackable std::string tls_client_key; }; + struct TrunkPeerConfig + { + std::string host; + std::string port; + std::string secret; + std::string remote_prefix; + std::string peer_id; + }; + explicit RedisStore(const Config& cfg); ~RedisStore(); @@ -56,6 +65,7 @@ class RedisStore : public sigc::trackable std::string loadTrunkFilter(const std::string& section, const std::string& field); // "blacklist"|"allow" std::map loadTrunkTgMap(const std::string& section); + std::map loadTrunkPeers(void); // Publish a config.changed scope token (sync). void publishConfigChanged(const std::string& scope); diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index 3643c5b..e6c6271 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -2168,6 +2168,37 @@ void Reflector::initTrunkLinks(void) } } + // If Redis is configured, load trunk peer definitions from Redis and + // synthesize them into m_cfg so the existing trunk initialization + // loop picks them up. Warn if the .conf ALSO has [TRUNK_*] sections + // (they are ignored per the Redis-overrides rule). + if (m_redis != nullptr) { + std::list conf_sections = m_cfg->listSections(); + bool conf_has_trunk = false; + for (const std::string& s : conf_sections) { + if (s.size() >= 6 && s.substr(0, 6) == "TRUNK_") { conf_has_trunk = true; break; } + } + if (conf_has_trunk) { + std::cerr << "WARN: [TRUNK_*] sections in svxreflector.conf are " + "ignored because [REDIS] is configured. Run " + "--import-conf-to-redis to migrate." << std::endl; + } + + auto peers = m_redis->loadTrunkPeers(); + for (const auto& kv : peers) { + const std::string& section = kv.first; + const auto& p = kv.second; + m_cfg->setValue(section, "HOST", p.host); + if (!p.port.empty()) m_cfg->setValue(section, "PORT", p.port); + m_cfg->setValue(section, "SECRET", p.secret); + m_cfg->setValue(section, "REMOTE_PREFIX", p.remote_prefix); + if (!p.peer_id.empty()) + m_cfg->setValue(section, "PEER_ID", p.peer_id); + } + std::cout << "Redis: loaded " << peers.size() << " trunk peer(s)" + << std::endl; + } + // First pass: collect all remote prefixes so we can do longest-prefix-match std::vector trunk_sections; for (const auto& section : m_cfg->listSections()) diff --git a/src/svxlink/reflector/svxreflector.cpp b/src/svxlink/reflector/svxreflector.cpp index 395d8c2..ba09695 100644 --- a/src/svxlink/reflector/svxreflector.cpp +++ b/src/svxlink/reflector/svxreflector.cpp @@ -262,6 +262,35 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) ++trunks_n; std::string trunk_label = sec.substr(6); // "TRUNK_AB" → "AB" + // Import the peer definition hash. + std::string host, port_s_peer, secret, remote_prefix, peer_id; + cfg.getValue(sec, "HOST", host); + cfg.getValue(sec, "PORT", port_s_peer); + cfg.getValue(sec, "SECRET", secret); + cfg.getValue(sec, "REMOTE_PREFIX", remote_prefix); + cfg.getValue(sec, "PEER_ID", peer_id); + if (!host.empty() && !secret.empty() && !remote_prefix.empty()) + { + std::string pk = kf("trunk:" + trunk_label + ":peer"); + auto setField = [&](const char* field, const std::string& value) { + if (value.empty()) return; + if (dry_run) { + std::cout << "[dry-run] HSET " << pk << " " << field + << " " << (std::string(field) == "secret" ? "" : value) + << std::endl; + } else { + redisReply* r = (redisReply*)redisCommand(ctx, "HSET %s %s %s", + pk.c_str(), field, value.c_str()); + if (r) freeReplyObject(r); + } + }; + setField("host", host); + setField("port", port_s_peer); + setField("secret", secret); + setField("remote_prefix", remote_prefix); + setField("peer_id", peer_id); + } + auto importSet = [&](const std::string& field, const std::string& subkey) { std::string s; if (!cfg.getValue(sec, field, s) || s.empty()) return;