]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnsdistdist/dnsdist-lua-bindings-kvs.cc
Merge pull request #9229 from rgacogne/dnsdist-webserver-allow-from
[thirdparty/pdns.git] / pdns / dnsdistdist / dnsdist-lua-bindings-kvs.cc
CommitLineData
4d4d5623
RG
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22#include "dnsdist.hh"
23#include "dnsdist-kvs.hh"
24#include "dnsdist-lua.hh"
25
26void setupLuaBindingsKVS(bool client)
27{
28 /* Key Value Store objects */
29 g_lua.writeFunction("KeyValueLookupKeySourceIP", []() {
30 return std::shared_ptr<KeyValueLookupKey>(new KeyValueLookupKeySourceIP());
31 });
32 g_lua.writeFunction("KeyValueLookupKeyQName", [](boost::optional<bool> wireFormat) {
33 return std::shared_ptr<KeyValueLookupKey>(new KeyValueLookupKeyQName(wireFormat ? *wireFormat : true));
34 });
35 g_lua.writeFunction("KeyValueLookupKeySuffix", [](boost::optional<size_t> minLabels, boost::optional<bool> wireFormat) {
36 return std::shared_ptr<KeyValueLookupKey>(new KeyValueLookupKeySuffix(minLabels ? *minLabels : 0, wireFormat ? *wireFormat : true));
37 });
38 g_lua.writeFunction("KeyValueLookupKeyTag", [](const std::string& tag) {
39 return std::shared_ptr<KeyValueLookupKey>(new KeyValueLookupKeyTag(tag));
40 });
41
42#ifdef HAVE_LMDB
43 g_lua.writeFunction("newLMDBKVStore", [client](const std::string& fname, const std::string& dbName) {
44 if (client) {
45 return std::shared_ptr<KeyValueStore>(nullptr);
46 }
47 return std::shared_ptr<KeyValueStore>(new LMDBKVStore(fname, dbName));
48 });
49#endif /* HAVE_LMDB */
50
51#ifdef HAVE_CDB
52 g_lua.writeFunction("newCDBKVStore", [client](const std::string& fname, time_t refreshDelay) {
53 if (client) {
54 return std::shared_ptr<KeyValueStore>(nullptr);
55 }
56 return std::shared_ptr<KeyValueStore>(new CDBKVStore(fname, refreshDelay));
57 });
58#endif /* HAVE_CDB */
59
60 g_lua.registerFunction<std::string(std::shared_ptr<KeyValueStore>::*)(const boost::variant<ComboAddress, DNSName, std::string>, boost::optional<bool> wireFormat)>("lookup", [](std::shared_ptr<KeyValueStore>& kvs, const boost::variant<ComboAddress, DNSName, std::string> keyVar, boost::optional<bool> wireFormat) {
61 std::string result;
62 if (!kvs) {
63 return result;
64 }
65
66 if (keyVar.type() == typeid(ComboAddress)) {
7e0221fc 67 const auto ca = boost::get<ComboAddress>(&keyVar);
4d4d5623 68 KeyValueLookupKeySourceIP lookup;
7e0221fc 69 for (const auto& key : lookup.getKeys(*ca)) {
4d4d5623
RG
70 if (kvs->getValue(key, result)) {
71 return result;
72 }
73 }
74 }
75 else if (keyVar.type() == typeid(DNSName)) {
7e0221fc 76 const DNSName* dn = boost::get<DNSName>(&keyVar);
4d4d5623 77 KeyValueLookupKeyQName lookup(wireFormat ? *wireFormat : true);
7e0221fc 78 for (const auto& key : lookup.getKeys(*dn)) {
4d4d5623
RG
79 if (kvs->getValue(key, result)) {
80 return result;
81 }
82 }
83 }
84 else if (keyVar.type() == typeid(std::string)) {
7e0221fc
RG
85 const std::string* keyStr = boost::get<std::string>(&keyVar);
86 kvs->getValue(*keyStr, result);
4d4d5623
RG
87 }
88
89 return result;
90 });
91
92 g_lua.registerFunction<std::string(std::shared_ptr<KeyValueStore>::*)(const DNSName&, boost::optional<size_t> minLabels, boost::optional<bool> wireFormat)>("lookupSuffix", [](std::shared_ptr<KeyValueStore>& kvs, const DNSName& dn, boost::optional<size_t> minLabels, boost::optional<bool> wireFormat) {
93 std::string result;
94 if (!kvs) {
95 return result;
96 }
97
98 KeyValueLookupKeySuffix lookup(minLabels ? *minLabels : 0, wireFormat ? *wireFormat : true);
99 for (const auto& key : lookup.getKeys(dn)) {
100 if (kvs->getValue(key, result)) {
101 return result;
102 }
103 }
104
105 return result;
106 });
107
108 g_lua.registerFunction<bool(std::shared_ptr<KeyValueStore>::*)()>("reload", [](std::shared_ptr<KeyValueStore>& kvs) {
109 if (!kvs) {
110 return false;
111 }
112
113 return kvs->reload();
114 });
115}