]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdistdist/dnsdist-lua-bindings-network.cc
Merge pull request #11431 from jroessler-ox/docs-kskzskroll-update
[thirdparty/pdns.git] / pdns / dnsdistdist / dnsdist-lua-bindings-network.cc
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-async.hh"
24 #include "dnsdist-lua.hh"
25 #include "dnsdist-lua-ffi.hh"
26 #include "dnsdist-lua-network.hh"
27 #include "dolog.hh"
28
29 void setupLuaBindingsNetwork(LuaContext& luaCtx, bool client)
30 {
31 luaCtx.writeFunction("newNetworkEndpoint", [client](const std::string& path) {
32 if (client) {
33 return std::shared_ptr<dnsdist::NetworkEndpoint>(nullptr);
34 }
35
36 try {
37 return std::make_shared<dnsdist::NetworkEndpoint>(path);
38 }
39 catch (const std::exception& e) {
40 warnlog("Error connecting to network endpoint: %s", e.what());
41 }
42 return std::shared_ptr<dnsdist::NetworkEndpoint>(nullptr);
43 });
44
45 luaCtx.registerFunction<bool (std::shared_ptr<dnsdist::NetworkEndpoint>::*)() const>("isValid", [](const std::shared_ptr<dnsdist::NetworkEndpoint>& endpoint) {
46 return endpoint != nullptr;
47 });
48
49 luaCtx.registerFunction<bool (std::shared_ptr<dnsdist::NetworkEndpoint>::*)(const std::string&) const>("send", [client](const std::shared_ptr<dnsdist::NetworkEndpoint>& endpoint, const std::string& payload) {
50 if (client || !endpoint || payload.empty()) {
51 return false;
52 }
53
54 return endpoint->send(payload);
55 });
56
57 luaCtx.writeFunction("newNetworkListener", [client]() {
58 if (client) {
59 return std::shared_ptr<dnsdist::NetworkListener>(nullptr);
60 }
61
62 return std::make_shared<dnsdist::NetworkListener>();
63 });
64
65 luaCtx.registerFunction<bool (std::shared_ptr<dnsdist::NetworkListener>::*)(const std::string&, uint16_t, std::function<void(uint16_t, std::string & dgram, const std::string& from)>)>("addUnixListeningEndpoint", [client](std::shared_ptr<dnsdist::NetworkListener>& listener, const std::string& path, uint16_t endpointID, std::function<void(uint16_t endpoint, std::string & dgram, const std::string& from)> cb) {
66 if (client || !cb) {
67 return false;
68 }
69
70 return listener->addUnixListeningEndpoint(path, endpointID, [cb = std::move(cb)](dnsdist::NetworkListener::EndpointID endpoint, std::string&& dgram, const std::string& from) {
71 {
72 auto lock = g_lua.lock();
73 cb(endpoint, dgram, from);
74 }
75 dnsdist::handleQueuedAsynchronousEvents();
76 });
77 });
78
79 // if you make the dnsdist_ffi_network_message_t* in the function prototype const, LuaWrapper will stop treating it like a lightuserdata, messing everything up!!
80 luaCtx.registerFunction<bool (std::shared_ptr<dnsdist::NetworkListener>::*)(const std::string&, uint16_t, std::function<void(dnsdist_ffi_network_message_t*)>)>("addUnixListeningEndpointFFI", [client](std::shared_ptr<dnsdist::NetworkListener>& listener, const std::string& path, uint16_t endpointID, std::function<void(dnsdist_ffi_network_message_t*)> cb) {
81 if (client) {
82 return false;
83 }
84
85 return listener->addUnixListeningEndpoint(path, endpointID, [cb](dnsdist::NetworkListener::EndpointID endpoint, std::string&& dgram, const std::string& from) {
86 {
87 auto lock = g_lua.lock();
88 dnsdist_ffi_network_message_t msg(dgram, from, endpoint);
89 cb(&msg);
90 }
91 dnsdist::handleQueuedAsynchronousEvents();
92 });
93 });
94
95 luaCtx.registerFunction<void (std::shared_ptr<dnsdist::NetworkListener>::*)()>("start", [client](std::shared_ptr<dnsdist::NetworkListener>& listener) {
96 if (client) {
97 return;
98 }
99
100 listener->start();
101 });
102
103 luaCtx.writeFunction("getResolvers", [](const std::string& resolvConfPath) -> LuaArray<std::string> {
104 auto resolvers = getResolvers(resolvConfPath);
105 LuaArray<std::string> result;
106 result.reserve(resolvers.size());
107 int counter = 1;
108 for (const auto& resolver : resolvers) {
109 result.emplace_back(counter, resolver.toString());
110 counter++;
111 }
112 return result;
113 });
114 };