#include "dnsdist-configuration.hh"
#include "logging.hh"
#include "logr.hh"
+#include "mmdb.hh"
#if defined(HAVE_YAML_CONFIGURATION)
#include "base64.hh"
using XSKMap = std::vector<std::shared_ptr<XskSocket>>;
-using RegisteredTypes = std::variant<std::shared_ptr<DNSDistPacketCache>, std::shared_ptr<dnsdist::rust::settings::DNSSelector>, std::shared_ptr<dnsdist::rust::settings::DNSActionWrapper>, std::shared_ptr<dnsdist::rust::settings::DNSResponseActionWrapper>, std::shared_ptr<NetmaskGroup>, std::shared_ptr<KeyValueStore>, std::shared_ptr<KeyValueLookupKey>, std::shared_ptr<RemoteLoggerInterface>, std::shared_ptr<ServerPolicy>, std::shared_ptr<TimedIPSetRule>, std::shared_ptr<XSKMap>>;
+using RegisteredTypes = std::variant<std::shared_ptr<DNSDistPacketCache>, std::shared_ptr<dnsdist::rust::settings::DNSSelector>, std::shared_ptr<dnsdist::rust::settings::DNSActionWrapper>, std::shared_ptr<dnsdist::rust::settings::DNSResponseActionWrapper>, std::shared_ptr<NetmaskGroup>, std::shared_ptr<KeyValueStore>, std::shared_ptr<KeyValueLookupKey>, std::shared_ptr<RemoteLoggerInterface>, std::shared_ptr<ServerPolicy>, std::shared_ptr<TimedIPSetRule>, std::shared_ptr<XSKMap>
+#ifdef HAVE_MMDB
+ ,
+ std::shared_ptr<MMDB>
+#endif
+ >;
static LockGuarded<std::unordered_map<std::string, RegisteredTypes>> s_registeredTypesMap;
static std::atomic<bool> s_inConfigCheckMode;
static std::atomic<bool> s_inClientMode;
void addLuaBindingsForYAMLObjects([[maybe_unused]] LuaContext& luaCtx)
{
#if defined(HAVE_YAML_CONFIGURATION)
- using ReturnValue = std::optional<boost::variant<std::shared_ptr<DNSDistPacketCache>, std::shared_ptr<DNSRule>, std::shared_ptr<DNSAction>, std::shared_ptr<DNSResponseAction>, std::shared_ptr<NetmaskGroup>, std::shared_ptr<KeyValueStore>, std::shared_ptr<KeyValueLookupKey>, std::shared_ptr<RemoteLoggerInterface>, std::shared_ptr<ServerPolicy>, std::shared_ptr<TimedIPSetRule>, std::shared_ptr<XSKMap>>>;
+ using ReturnValue = std::optional<boost::variant<std::shared_ptr<DNSDistPacketCache>, std::shared_ptr<DNSRule>, std::shared_ptr<DNSAction>, std::shared_ptr<DNSResponseAction>, std::shared_ptr<NetmaskGroup>, std::shared_ptr<KeyValueStore>, std::shared_ptr<KeyValueLookupKey>, std::shared_ptr<RemoteLoggerInterface>, std::shared_ptr<ServerPolicy>, std::shared_ptr<TimedIPSetRule>, std::shared_ptr<XSKMap>
+#ifdef HAVE_MMDB
+ ,
+ std::shared_ptr<MMDB>
+#endif
+ >>;
luaCtx.writeFunction("getObjectFromYAMLConfiguration", [](const std::string& name) -> ReturnValue {
auto map = s_registeredTypesMap.lock();
if (auto* ptr = std::get_if<std::shared_ptr<XSKMap>>(&item->second)) {
return ReturnValue(*ptr);
}
+#ifdef HAVE_MMDB
+ if (auto* ptr = std::get_if<std::shared_ptr<MMDB>>(&item->second)) {
+ return ReturnValue(*ptr);
+ }
+#endif
return std::nullopt;
});
std::shared_ptr<DNSActionWrapper> 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<KeyValueStore>(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) + "'");
std::shared_ptr<DNSActionWrapper> 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<KeyValueStore>(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) + "'");
std::shared_ptr<DNSSelector> 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<KeyValueStore>(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) + "'");
std::shared_ptr<DNSSelector> 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<KeyValueStore>(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) + "'");
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) {
dnsdist::configuration::yaml::registerType<KeyValueStore>(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.mmdb);
+ if (!definedMmdb) {
+ throw std::runtime_error("Unable to find a MMDB named " + std::string(mmdb.mmdb));
+ }
+ LuaTypeOrArrayOf<std::string> queryParams;
+ if (!mmdb.query_param.empty()) {
+ queryParams = std::string(mmdb.query_param);
+ }
+ else {
+ std::vector<std::pair<int, std::string>> 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<KeyValueStore>(std::make_shared<MMDBKVStore>(definedMmdb, queryParams)) : std::shared_ptr<KeyValueStore>();
+ dnsdist::configuration::yaml::registerType<KeyValueStore>(store, mmdb.name);
+ }
+#endif /* defined(HAVE_MMDB) */
for (const auto& key : config.lookup_keys.source_ip_keys) {
auto lookup = createObjects ? std::shared_ptr<KeyValueLookupKey>(std::make_shared<KeyValueLookupKeySourceIP>(key.v4_mask, key.v6_mask, key.include_port)) : std::shared_ptr<KeyValueLookupKey>();
dnsdist::configuration::yaml::registerType<KeyValueLookupKey>(lookup, key.name);
auto lookup = createObjects ? std::shared_ptr<KeyValueLookupKey>(std::make_shared<KeyValueLookupKeyTag>(std::string(key.tag))) : std::shared_ptr<KeyValueLookupKey>();
dnsdist::configuration::yaml::registerType<KeyValueLookupKey>(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<MmdbConfiguration>& config)
+{
+#ifdef HAVE_MMDB
+ for (const auto& mmdb : config) {
+ dnsdist::configuration::yaml::registerType<MMDB>(std::make_shared<MMDB>(std::string(mmdb.file_name), mmdb.mmap ? "mmap" : ""), mmdb.name);
+ }
+#endif
}
void registerNMGObjects(const ::rust::Vec<NetmaskGroupConfiguration>& nmgs)
type: "MetricsConfiguration"
default: true
description: "Metrics-related settings"
+ - name: "mmdbs"
+ type: "Vec<MmdbConfiguration>"
+ default: true
+ description: "List of MMDB databases"
- name: "netmask_groups"
type: "Vec<NetmaskGroupConfiguration>"
default: true
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<String>"
+ 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:
type: "Vec<CdbKvStoreConfiguration>"
default: true
description: "List of CDB-based key-value stores"
+ - name: "mmdb"
+ type: "Vec<MmdbKvStoreConfiguration>"
+ default: true
+ description: "List of MMDB-based key-value stores"
- name: "lookup_keys"
type: "KvsLookupKeysConfiguration"
default: true