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-network.cc | 114 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 dnsdist-lua-bindings-network.cc (limited to 'dnsdist-lua-bindings-network.cc') diff --git a/dnsdist-lua-bindings-network.cc b/dnsdist-lua-bindings-network.cc new file mode 100644 index 0000000..62dce3b --- /dev/null +++ b/dnsdist-lua-bindings-network.cc @@ -0,0 +1,114 @@ +/* + * 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-async.hh" +#include "dnsdist-lua.hh" +#include "dnsdist-lua-ffi.hh" +#include "dnsdist-lua-network.hh" +#include "dolog.hh" + +void setupLuaBindingsNetwork(LuaContext& luaCtx, bool client) +{ + luaCtx.writeFunction("newNetworkEndpoint", [client](const std::string& path) { + if (client) { + return std::shared_ptr(nullptr); + } + + try { + return std::make_shared(path); + } + catch (const std::exception& e) { + warnlog("Error connecting to network endpoint: %s", e.what()); + } + return std::shared_ptr(nullptr); + }); + + luaCtx.registerFunction::*)() const>("isValid", [](const std::shared_ptr& endpoint) { + return endpoint != nullptr; + }); + + luaCtx.registerFunction::*)(const std::string&) const>("send", [client](const std::shared_ptr& endpoint, const std::string& payload) { + if (client || !endpoint || payload.empty()) { + return false; + } + + return endpoint->send(payload); + }); + + luaCtx.writeFunction("newNetworkListener", [client]() { + if (client) { + return std::shared_ptr(nullptr); + } + + return std::make_shared(); + }); + + luaCtx.registerFunction::*)(const std::string&, uint16_t, std::function)>("addUnixListeningEndpoint", [client](std::shared_ptr& listener, const std::string& path, uint16_t endpointID, std::function cb) { + if (client || !cb) { + return false; + } + + return listener->addUnixListeningEndpoint(path, endpointID, [cb](dnsdist::NetworkListener::EndpointID endpoint, std::string&& dgram, const std::string& from) { + { + auto lock = g_lua.lock(); + cb(endpoint, dgram, from); + } + dnsdist::handleQueuedAsynchronousEvents(); + }); + }); + + // 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!! + luaCtx.registerFunction::*)(const std::string&, uint16_t, std::function)>("addUnixListeningEndpointFFI", [client](std::shared_ptr& listener, const std::string& path, uint16_t endpointID, std::function cb) { + if (client) { + return false; + } + + return listener->addUnixListeningEndpoint(path, endpointID, [cb](dnsdist::NetworkListener::EndpointID endpoint, std::string&& dgram, const std::string& from) { + { + auto lock = g_lua.lock(); + dnsdist_ffi_network_message_t msg(dgram, from, endpoint); + cb(&msg); + } + dnsdist::handleQueuedAsynchronousEvents(); + }); + }); + + luaCtx.registerFunction::*)()>("start", [client](std::shared_ptr& listener) { + if (client) { + return; + } + + listener->start(); + }); + + luaCtx.writeFunction("getResolvers", [](const std::string& resolvConfPath) -> LuaArray { + auto resolvers = getResolvers(resolvConfPath); + LuaArray result; + result.reserve(resolvers.size()); + int counter = 1; + for (const auto& resolver : resolvers) { + result.emplace_back(counter, resolver.toString()); + counter++; + } + return result; + }); +}; -- cgit v1.2.3