/src/pdns/pdns/dnsdistdist/dnsdist-lua-bindings-mmdb.cc
Line | Count | Source |
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-lua-types.hh" |
23 | | #include "dnsdist-lua.hh" |
24 | | #include "iputils.hh" |
25 | | #include <memory> |
26 | | #include "mmdb.hh" |
27 | | |
28 | | void setupLuaBindingsMMDB([[maybe_unused]] LuaContext& luaCtx) |
29 | 0 | { |
30 | | #ifdef HAVE_MMDB |
31 | | luaCtx.writeFunction("openMMDB", [](const std::string& name, std::optional<LuaAssociativeTable<boost::variant<bool>>> vars) { |
32 | | bool mmap{false}; |
33 | | getOptionalValue<bool>(vars, "mmap", mmap); |
34 | | |
35 | | auto mmdb = std::make_shared<MMDB>(name, mmap ? "mmap" : ""); |
36 | | |
37 | | return mmdb; |
38 | | }); |
39 | | |
40 | | luaCtx.registerFunction<std::optional<LuaAny> (std::shared_ptr<MMDB>::*)(const LuaTypeOrArrayOf<std::string>&, const boost::variant<ComboAddress, std::string>)>("query", [](std::shared_ptr<MMDB>& mmdb, const LuaTypeOrArrayOf<std::string>& queryParams, const boost::variant<ComboAddress, std::string>& ip) { |
41 | | std::optional<LuaAny> result{std::nullopt}; |
42 | | if (!mmdb) { |
43 | | return result; |
44 | | } |
45 | | |
46 | | LuaAny value{false}; |
47 | | |
48 | | ComboAddress queryIp; |
49 | | if (const auto str = boost::get<std::string>(&ip)) { |
50 | | queryIp = ComboAddress(*str); |
51 | | } |
52 | | else if (const auto addr = boost::get<ComboAddress>(&ip)) { |
53 | | queryIp = *addr; |
54 | | } |
55 | | else { |
56 | | return result; |
57 | | } |
58 | | |
59 | | if (mmdb->query(value, MMDBQueryParams(queryParams), queryIp)) { |
60 | | result = value; |
61 | | } |
62 | | |
63 | | return result; |
64 | | }); |
65 | | |
66 | | luaCtx.registerFunction<bool (std::shared_ptr<MMDB>::*)(const boost::variant<ComboAddress, std::string>)>("exists", [](std::shared_ptr<MMDB>& mmdb, const boost::variant<ComboAddress, std::string>& ip) { |
67 | | bool result = false; |
68 | | if (!mmdb) { |
69 | | return result; |
70 | | } |
71 | | |
72 | | if (const auto str = boost::get<std::string>(&ip)) { |
73 | | return mmdb->exists(ComboAddress(*str)); |
74 | | } |
75 | | else if (const auto addr = boost::get<ComboAddress>(&ip)) { |
76 | | return mmdb->exists(*addr); |
77 | | } |
78 | | return false; |
79 | | }); |
80 | | #endif |
81 | 0 | } |