From: Ensar Sarajčić Date: Tue, 19 May 2026 08:53:21 +0000 (+0200) Subject: dnsdist: add mmdb to YAML configuration X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a32dcadf5aea26bfaca815352dbfa70e708a4e53;p=thirdparty%2Fpdns.git dnsdist: add mmdb to YAML configuration Signed-off-by: Ensar Sarajčić --- diff --git a/pdns/dnsdistdist/dnsdist-configuration-yaml.cc b/pdns/dnsdistdist/dnsdist-configuration-yaml.cc index 19b164ec27..cf0ec18125 100644 --- a/pdns/dnsdistdist/dnsdist-configuration-yaml.cc +++ b/pdns/dnsdistdist/dnsdist-configuration-yaml.cc @@ -28,6 +28,7 @@ #include "dnsdist-configuration.hh" #include "logging.hh" #include "logr.hh" +#include "mmdb.hh" #if defined(HAVE_YAML_CONFIGURATION) #include "base64.hh" @@ -71,7 +72,12 @@ struct Context using XSKMap = std::vector>; -using RegisteredTypes = std::variant, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr>; +using RegisteredTypes = std::variant, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr +#ifdef HAVE_MMDB + , + std::shared_ptr +#endif + >; static LockGuarded> s_registeredTypesMap; static std::atomic s_inConfigCheckMode; static std::atomic s_inClientMode; @@ -1394,7 +1400,12 @@ bool loadConfigurationFromFile(const std::string& fileName, [[maybe_unused]] boo void addLuaBindingsForYAMLObjects([[maybe_unused]] LuaContext& luaCtx) { #if defined(HAVE_YAML_CONFIGURATION) - using ReturnValue = std::optional, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr>>; + using ReturnValue = std::optional, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr +#ifdef HAVE_MMDB + , + std::shared_ptr +#endif + >>; luaCtx.writeFunction("getObjectFromYAMLConfiguration", [](const std::string& name) -> ReturnValue { auto map = s_registeredTypesMap.lock(); @@ -1435,6 +1446,11 @@ void addLuaBindingsForYAMLObjects([[maybe_unused]] LuaContext& luaCtx) if (auto* ptr = std::get_if>(&item->second)) { return ReturnValue(*ptr); } +#ifdef HAVE_MMDB + if (auto* ptr = std::get_if>(&item->second)) { + return ReturnValue(*ptr); + } +#endif return std::nullopt; }); @@ -1698,7 +1714,7 @@ std::shared_ptr getNetmaskGroupSelector(const NetmaskGroupSelectorC std::shared_ptr getKeyValueStoreLookupAction([[maybe_unused]] const KeyValueStoreLookupActionConfiguration& config) { -#if defined(HAVE_LMDB) || defined(HAVE_CDB) +#if defined(HAVE_LMDB) || defined(HAVE_CDB) || defined(HAVE_MMDB) auto kvs = dnsdist::configuration::yaml::getRegisteredTypeByName(std::string(config.kvs_name)); if (!kvs && !(dnsdist::configuration::yaml::s_inClientMode || dnsdist::configuration::yaml::s_inConfigCheckMode)) { throw std::runtime_error("Unable to find the key-value store named '" + std::string(config.kvs_name) + "'"); @@ -1716,7 +1732,7 @@ std::shared_ptr getKeyValueStoreLookupAction([[maybe_unused]] std::shared_ptr getKeyValueStoreRangeLookupAction([[maybe_unused]] const KeyValueStoreRangeLookupActionConfiguration& config) { -#if defined(HAVE_LMDB) || defined(HAVE_CDB) +#if defined(HAVE_LMDB) || defined(HAVE_CDB) || defined(HAVE_MMDB) auto kvs = dnsdist::configuration::yaml::getRegisteredTypeByName(std::string(config.kvs_name)); if (!kvs && !(dnsdist::configuration::yaml::s_inClientMode || dnsdist::configuration::yaml::s_inConfigCheckMode)) { throw std::runtime_error("Unable to find the key-value store named '" + std::string(config.kvs_name) + "'"); @@ -1734,7 +1750,7 @@ std::shared_ptr getKeyValueStoreRangeLookupAction([[maybe_unus std::shared_ptr getKeyValueStoreLookupSelector([[maybe_unused]] const KeyValueStoreLookupSelectorConfiguration& config) { -#if defined(HAVE_LMDB) || defined(HAVE_CDB) +#if defined(HAVE_LMDB) || defined(HAVE_CDB) || defined(HAVE_MMDB) auto kvs = dnsdist::configuration::yaml::getRegisteredTypeByName(std::string(config.kvs_name)); if (!kvs && !(dnsdist::configuration::yaml::s_inClientMode || dnsdist::configuration::yaml::s_inConfigCheckMode)) { throw std::runtime_error("Unable to find the key-value store named '" + std::string(config.kvs_name) + "'"); @@ -1752,7 +1768,7 @@ std::shared_ptr getKeyValueStoreLookupSelector([[maybe_unused]] con std::shared_ptr getKeyValueStoreRangeLookupSelector([[maybe_unused]] const KeyValueStoreRangeLookupSelectorConfiguration& config) { -#if defined(HAVE_LMDB) || defined(HAVE_CDB) +#if defined(HAVE_LMDB) || defined(HAVE_CDB) || defined(HAVE_MMDB) auto kvs = dnsdist::configuration::yaml::getRegisteredTypeByName(std::string(config.kvs_name)); if (!kvs && !(dnsdist::configuration::yaml::s_inClientMode || dnsdist::configuration::yaml::s_inConfigCheckMode)) { throw std::runtime_error("Unable to find the key-value store named '" + std::string(config.kvs_name) + "'"); @@ -2005,7 +2021,7 @@ void registerDnstapLogger([[maybe_unused]] const DnstapLoggerConfiguration& conf void registerKVSObjects([[maybe_unused]] const KeyValueStoresConfiguration& config) { -#if defined(HAVE_LMDB) || defined(HAVE_CDB) +#if defined(HAVE_LMDB) || defined(HAVE_CDB) || defined(HAVE_MMDB) bool createObjects = !dnsdist::configuration::yaml::s_inClientMode && !dnsdist::configuration::yaml::s_inConfigCheckMode; #if defined(HAVE_LMDB) for (const auto& lmdb : config.lmdb) { @@ -2019,6 +2035,29 @@ void registerKVSObjects([[maybe_unused]] const KeyValueStoresConfiguration& conf dnsdist::configuration::yaml::registerType(store, cdb.name); } #endif /* defined(HAVE_CDB) */ +#if defined(HAVE_MMDB) + for (const auto& mmdb : config.mmdb) { + auto definedMmdb = dnsdist::configuration::yaml::getRegisteredTypeByName(mmdb.mmdb); + if (!definedMmdb) { + throw std::runtime_error("Unable to find a MMDB named " + std::string(mmdb.mmdb)); + } + LuaTypeOrArrayOf queryParams; + if (!mmdb.query_param.empty()) { + queryParams = std::string(mmdb.query_param); + } + else { + std::vector> params; + params.reserve(mmdb.query_params.size()); + int i = 1; + for (const auto& param : mmdb.query_params) { + params.emplace_back(i++, param); + } + queryParams = params; + } + auto store = createObjects ? std::shared_ptr(std::make_shared(definedMmdb, queryParams)) : std::shared_ptr(); + dnsdist::configuration::yaml::registerType(store, mmdb.name); + } +#endif /* defined(HAVE_MMDB) */ for (const auto& key : config.lookup_keys.source_ip_keys) { auto lookup = createObjects ? std::shared_ptr(std::make_shared(key.v4_mask, key.v6_mask, key.include_port)) : std::shared_ptr(); dnsdist::configuration::yaml::registerType(lookup, key.name); @@ -2035,7 +2074,16 @@ void registerKVSObjects([[maybe_unused]] const KeyValueStoresConfiguration& conf auto lookup = createObjects ? std::shared_ptr(std::make_shared(std::string(key.tag))) : std::shared_ptr(); dnsdist::configuration::yaml::registerType(lookup, key.name); } -#endif /* defined(HAVE_LMDB) || defined(HAVE_CDB) */ +#endif /* defined(HAVE_LMDB) || defined(HAVE_CDB) || defined(HAVE_MMDB) */ +} + +void registerMMDBObjects([[maybe_unused]] const ::rust::Vec& config) +{ +#ifdef HAVE_MMDB + for (const auto& mmdb : config) { + dnsdist::configuration::yaml::registerType(std::make_shared(std::string(mmdb.file_name), mmdb.mmap ? "mmap" : ""), mmdb.name); + } +#endif } void registerNMGObjects(const ::rust::Vec& nmgs) diff --git a/pdns/dnsdistdist/dnsdist-rust-bridge.hh b/pdns/dnsdistdist/dnsdist-rust-bridge.hh index be86c01e14..a03f8391e0 100644 --- a/pdns/dnsdistdist/dnsdist-rust-bridge.hh +++ b/pdns/dnsdistdist/dnsdist-rust-bridge.hh @@ -33,12 +33,14 @@ struct DNSResponseActionWrapper struct ProtobufLoggerConfiguration; struct DnstapLoggerConfiguration; struct KeyValueStoresConfiguration; +struct MmdbConfiguration; struct NetmaskGroupConfiguration; struct TimedIpSetConfiguration; void registerProtobufLogger(const ProtobufLoggerConfiguration& config); void registerDnstapLogger(const DnstapLoggerConfiguration& config); void registerKVSObjects(const KeyValueStoresConfiguration& config); +void registerMMDBObjects(const ::rust::Vec& config); void registerNMGObjects(const ::rust::Vec& nmgs); void registerTimedIPSetObjects(const ::rust::Vec& sets); diff --git a/pdns/dnsdistdist/dnsdist-rust-lib/rust-middle-in.rs b/pdns/dnsdistdist/dnsdist-rust-lib/rust-middle-in.rs index 8bfac01e27..7da6bc1290 100644 --- a/pdns/dnsdistdist/dnsdist-rust-lib/rust-middle-in.rs +++ b/pdns/dnsdistdist/dnsdist-rust-lib/rust-middle-in.rs @@ -15,6 +15,7 @@ fn registerProtobufLogger(config: &ProtobufLoggerConfiguration); fn registerDnstapLogger(config: &DnstapLoggerConfiguration); fn registerKVSObjects(config: &KeyValueStoresConfiguration); + fn registerMMDBObjects(config: &Vec); fn registerNMGObjects(nmgs: &Vec); fn registerTimedIPSetObjects(sets: &Vec); } diff --git a/pdns/dnsdistdist/dnsdist-rust-lib/rust-post-in.rs b/pdns/dnsdistdist/dnsdist-rust-lib/rust-post-in.rs index 091441c408..5ab7448220 100644 --- a/pdns/dnsdistdist/dnsdist-rust-lib/rust-post-in.rs +++ b/pdns/dnsdistdist/dnsdist-rust-lib/rust-post-in.rs @@ -70,6 +70,7 @@ fn get_global_configuration_from_serde( ebpf: serde.ebpf, edns_client_subnet: serde.edns_client_subnet, general: serde.general, + mmdbs: serde.mmdbs, key_value_stores: serde.key_value_stores, load_balancing_policies: serde.load_balancing_policies, logging: serde.logging, @@ -91,6 +92,8 @@ fn get_global_configuration_from_serde( }; // this needs to be done before the rules so that they can refer to the loggers register_remote_loggers(&config.remote_logging); + // this needs to be done before the KVS so they can refer to the DBs + dnsdistsettings::registerMMDBObjects(&config.mmdbs); // this needs to be done before the rules so that they can refer to the KVS objects dnsdistsettings::registerKVSObjects(&config.key_value_stores); // this needs to be done before the rules so that they can refer to the NMG objects diff --git a/pdns/dnsdistdist/dnsdist-settings-definitions.yml b/pdns/dnsdistdist/dnsdist-settings-definitions.yml index f703630122..d1540c9c47 100644 --- a/pdns/dnsdistdist/dnsdist-settings-definitions.yml +++ b/pdns/dnsdistdist/dnsdist-settings-definitions.yml @@ -73,6 +73,10 @@ global: type: "MetricsConfiguration" default: true description: "Metrics-related settings" + - name: "mmdbs" + type: "Vec" + default: true + description: "List of MMDB databases" - name: "netmask_groups" type: "Vec" default: true @@ -310,6 +314,38 @@ cdb_kv_store: type: "u32" description: "The delay in seconds between two checks of the database modification time. 0 means disabled" +mmdb_kv_store: + description: "MMDB-based key-value store" + parameters: + - name: "name" + type: "String" + description: "The name of this object" + - name: "mmdb" + type: "String" + description: "Name of an existing MMDB database" + - name: "query_param" + type: "String" + default: "" + description: "Key to look up in the MMDB database associated with an IP - top level only." + - name: "query_params" + type: "Vec" + default: true + description: "List of nested keys to look up in the MMDB database associated with an IP" + +mmdb: + description: "MMDB database" + parameters: + - name: "name" + type: "String" + description: "The name of this object" + - name: "file_name" + type: "String" + description: "The path to the MMDB file" + - name: "mmap" + type: "bool" + default: "false" + description: "Whether to open the MMDB in mmap mode" + kvs_lookup_key_source_ip: description: "Lookup key that can be used with :ref:`yaml-settings-KeyValueStoreLookupAction` or :ref:`yaml-settings-KeyValueStoreLookupSelector`, will return the source IP of the client in network byte-order" parameters: @@ -404,6 +440,10 @@ key_value_stores: type: "Vec" default: true description: "List of CDB-based key-value stores" + - name: "mmdb" + type: "Vec" + default: true + description: "List of MMDB-based key-value stores" - name: "lookup_keys" type: "KvsLookupKeysConfiguration" default: true