From 3cd01b932e1c85394272ae64fae67ebeda92fb00 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 23:11:59 +0200 Subject: Adding upstream version 1.8.3. Signed-off-by: Daniel Baumann --- dnsdist-lua-bindings-kvs.cc | 117 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 dnsdist-lua-bindings-kvs.cc (limited to 'dnsdist-lua-bindings-kvs.cc') diff --git a/dnsdist-lua-bindings-kvs.cc b/dnsdist-lua-bindings-kvs.cc new file mode 100644 index 0000000..ba1e732 --- /dev/null +++ b/dnsdist-lua-bindings-kvs.cc @@ -0,0 +1,117 @@ +/* + * This file is part of PowerDNS or dnsdist. + * Copyright -- PowerDNS.COM B.V. and its contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * In addition, for the avoidance of any doubt, permission is granted to + * link this program with OpenSSL and to (re)distribute the binaries + * produced as the result of such linking. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include "dnsdist.hh" +#include "dnsdist-kvs.hh" +#include "dnsdist-lua.hh" + +void setupLuaBindingsKVS(LuaContext& luaCtx, bool client) +{ +#ifdef HAVE_LMDB + luaCtx.writeFunction("newLMDBKVStore", [client](const std::string& fname, const std::string& dbName, boost::optional noLock) { + if (client) { + return std::shared_ptr(nullptr); + } + return std::shared_ptr(new LMDBKVStore(fname, dbName, noLock ? *noLock : false)); + }); +#endif /* HAVE_LMDB */ + +#ifdef HAVE_CDB + luaCtx.writeFunction("newCDBKVStore", [client](const std::string& fname, time_t refreshDelay) { + if (client) { + return std::shared_ptr(nullptr); + } + return std::shared_ptr(new CDBKVStore(fname, refreshDelay)); + }); +#endif /* HAVE_CDB */ + +#if defined(HAVE_LMDB) || defined(HAVE_CDB) + /* Key Value Store objects */ + luaCtx.writeFunction("KeyValueLookupKeySourceIP", [](boost::optional v4Mask, boost::optional v6Mask, boost::optional includePort) { + return std::shared_ptr(new KeyValueLookupKeySourceIP(v4Mask ? *v4Mask : 32, v6Mask ? *v6Mask : 128, includePort ? *includePort : false)); + }); + luaCtx.writeFunction("KeyValueLookupKeyQName", [](boost::optional wireFormat) { + return std::shared_ptr(new KeyValueLookupKeyQName(wireFormat ? *wireFormat : true)); + }); + luaCtx.writeFunction("KeyValueLookupKeySuffix", [](boost::optional minLabels, boost::optional wireFormat) { + return std::shared_ptr(new KeyValueLookupKeySuffix(minLabels ? *minLabels : 0, wireFormat ? *wireFormat : true)); + }); + luaCtx.writeFunction("KeyValueLookupKeyTag", [](const std::string& tag) { + return std::shared_ptr(new KeyValueLookupKeyTag(tag)); + }); + + luaCtx.registerFunction::*)(const boost::variant, boost::optional wireFormat)>("lookup", [](std::shared_ptr& kvs, const boost::variant keyVar, boost::optional wireFormat) { + std::string result; + if (!kvs) { + return result; + } + + if (keyVar.type() == typeid(ComboAddress)) { + const auto ca = boost::get(&keyVar); + KeyValueLookupKeySourceIP lookup(32, 128, false); + for (const auto& key : lookup.getKeys(*ca)) { + if (kvs->getValue(key, result)) { + return result; + } + } + } + else if (keyVar.type() == typeid(DNSName)) { + const DNSName* dn = boost::get(&keyVar); + KeyValueLookupKeyQName lookup(wireFormat ? *wireFormat : true); + for (const auto& key : lookup.getKeys(*dn)) { + if (kvs->getValue(key, result)) { + return result; + } + } + } + else if (keyVar.type() == typeid(std::string)) { + const std::string* keyStr = boost::get(&keyVar); + kvs->getValue(*keyStr, result); + } + + return result; + }); + + luaCtx.registerFunction::*)(const DNSName&, boost::optional minLabels, boost::optional wireFormat)>("lookupSuffix", [](std::shared_ptr& kvs, const DNSName& dn, boost::optional minLabels, boost::optional wireFormat) { + std::string result; + if (!kvs) { + return result; + } + + KeyValueLookupKeySuffix lookup(minLabels ? *minLabels : 0, wireFormat ? *wireFormat : true); + for (const auto& key : lookup.getKeys(dn)) { + if (kvs->getValue(key, result)) { + return result; + } + } + + return result; + }); + + luaCtx.registerFunction::*)()>("reload", [](std::shared_ptr& kvs) { + if (!kvs) { + return false; + } + + return kvs->reload(); + }); +#endif /* defined(HAVE_LMDB) || defined(HAVE_CDB) */ +} -- cgit v1.2.3