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-protobuf.cc | 172 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 dnsdist-lua-bindings-protobuf.cc (limited to 'dnsdist-lua-bindings-protobuf.cc') diff --git a/dnsdist-lua-bindings-protobuf.cc b/dnsdist-lua-bindings-protobuf.cc new file mode 100644 index 0000000..e532a56 --- /dev/null +++ b/dnsdist-lua-bindings-protobuf.cc @@ -0,0 +1,172 @@ +/* + * 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 "config.h" + +#include "dnsdist.hh" +#include "dnsdist-lua.hh" + +#ifndef DISABLE_PROTOBUF +#include "dnsdist-protobuf.hh" +#include "dnstap.hh" +#include "fstrm_logger.hh" +#include "ipcipher.hh" +#include "remote_logger.hh" + +#ifdef HAVE_FSTRM +static void parseFSTRMOptions(boost::optional>& params, LuaAssociativeTable& options) +{ + if (!params) { + return; + } + + static std::vector const potentialOptions = { "bufferHint", "flushTimeout", "inputQueueSize", "outputQueueSize", "queueNotifyThreshold", "reopenInterval" }; + + for (const auto& potentialOption : potentialOptions) { + getOptionalValue(params, potentialOption, options[potentialOption]); + } +} +#endif /* HAVE_FSTRM */ + +void setupLuaBindingsProtoBuf(LuaContext& luaCtx, bool client, bool configCheck) +{ +#ifdef HAVE_IPCIPHER + luaCtx.registerFunction("ipencrypt", [](const ComboAddress& ca, const std::string& key) { + return encryptCA(ca, key); + }); + luaCtx.registerFunction("ipdecrypt", [](const ComboAddress& ca, const std::string& key) { + return decryptCA(ca, key); + }); + + luaCtx.writeFunction("makeIPCipherKey", [](const std::string& password) { + return makeIPCipherKey(password); + }); +#endif /* HAVE_IPCIPHER */ + + /* ProtobufMessage */ + luaCtx.registerFunction("setTag", [](DNSDistProtoBufMessage& message, const std::string& strValue) { + message.addTag(strValue); + }); + luaCtx.registerFunction)>("setTagArray", [](DNSDistProtoBufMessage& message, const LuaArray& tags) { + for (const auto& tag : tags) { + message.addTag(tag.second); + } + }); + + luaCtx.registerFunction sec, boost::optional uSec)>("setProtobufResponseType", + [](DNSDistProtoBufMessage& message, boost::optional sec, boost::optional uSec) { + message.setType(pdns::ProtoZero::Message::MessageType::DNSResponseType); + message.setQueryTime(sec ? *sec : 0, uSec ? *uSec : 0); + }); + + luaCtx.registerFunction("addResponseRR", [](DNSDistProtoBufMessage& message, + const std::string& strQueryName, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& strBlob) { + message.addRR(DNSName(strQueryName), uType, uClass, uTTL, strBlob); + }); + luaCtx.registerFunction("setEDNSSubnet", [](DNSDistProtoBufMessage& message, const Netmask& subnet) { message.setEDNSSubnet(subnet); }); + luaCtx.registerFunction("setQuestion", [](DNSDistProtoBufMessage& message, const DNSName& qname, uint16_t qtype, uint16_t qclass) { message.setQuestion(qname, qtype, qclass); }); + luaCtx.registerFunction("setBytes", [](DNSDistProtoBufMessage& message, size_t bytes) { message.setBytes(bytes); }); + luaCtx.registerFunction("setTime", [](DNSDistProtoBufMessage& message, time_t sec, uint32_t usec) { message.setTime(sec, usec); }); + luaCtx.registerFunction("setQueryTime", [](DNSDistProtoBufMessage& message, time_t sec, uint32_t usec) { message.setQueryTime(sec, usec); }); + luaCtx.registerFunction("setResponseCode", [](DNSDistProtoBufMessage& message, uint8_t rcode) { message.setResponseCode(rcode); }); + + luaCtx.registerFunction)>("setRequestor", [](DNSDistProtoBufMessage& message, const ComboAddress& addr, boost::optional port) { + message.setRequestor(addr); + if (port) { + message.setRequestorPort(*port); + } + }); + luaCtx.registerFunction)>("setRequestorFromString", [](DNSDistProtoBufMessage& message, const std::string& str, boost::optional port) { + message.setRequestor(ComboAddress(str)); + if (port) { + message.setRequestorPort(*port); + } + }); + luaCtx.registerFunction)>("setResponder", [](DNSDistProtoBufMessage& message, const ComboAddress& addr, boost::optional port) { + message.setResponder(addr); + if (port) { + message.setResponderPort(*port); + } + }); + luaCtx.registerFunction)>("setResponderFromString", [](DNSDistProtoBufMessage& message, const std::string& str, boost::optional port) { + message.setResponder(ComboAddress(str)); + if (port) { + message.setResponderPort(*port); + } + }); + luaCtx.registerFunction("setServerIdentity", [](DNSDistProtoBufMessage& message, const std::string& str) { + message.setServerIdentity(str); + }); + + luaCtx.registerFunction("setExtra", [](DnstapMessage& message, const std::string& str) { + message.setExtra(str); + }); + + /* RemoteLogger */ + luaCtx.writeFunction("newRemoteLogger", [client,configCheck](const std::string& remote, boost::optional timeout, boost::optional maxQueuedEntries, boost::optional reconnectWaitTime) { + if (client || configCheck) { + return std::shared_ptr(nullptr); + } + return std::shared_ptr(new RemoteLogger(ComboAddress(remote), timeout ? *timeout : 2, maxQueuedEntries ? (*maxQueuedEntries*100) : 10000, reconnectWaitTime ? *reconnectWaitTime : 1, client)); + }); + + luaCtx.writeFunction("newFrameStreamUnixLogger", [client,configCheck](const std::string& address, boost::optional> params) { +#ifdef HAVE_FSTRM + if (client || configCheck) { + return std::shared_ptr(nullptr); + } + + LuaAssociativeTable options; + parseFSTRMOptions(params, options); + checkAllParametersConsumed("newRemoteLogger", params); + return std::shared_ptr(new FrameStreamLogger(AF_UNIX, address, !client, options)); +#else + throw std::runtime_error("fstrm support is required to build an AF_UNIX FrameStreamLogger"); +#endif /* HAVE_FSTRM */ + }); + + luaCtx.writeFunction("newFrameStreamTcpLogger", [client,configCheck](const std::string& address, boost::optional> params) { +#if defined(HAVE_FSTRM) && defined(HAVE_FSTRM_TCP_WRITER_INIT) + if (client || configCheck) { + return std::shared_ptr(nullptr); + } + + LuaAssociativeTable options; + parseFSTRMOptions(params, options); + checkAllParametersConsumed("newFrameStreamTcpLogger", params); + return std::shared_ptr(new FrameStreamLogger(AF_INET, address, !client, options)); +#else + throw std::runtime_error("fstrm with TCP support is required to build an AF_INET FrameStreamLogger"); +#endif /* HAVE_FSTRM */ + }); + + luaCtx.registerFunction::*)()const>("toString", [](const std::shared_ptr& logger) { + if (logger) { + return logger->toString(); + } + return std::string(); + }); +} +#else /* DISABLE_PROTOBUF */ +void setupLuaBindingsProtoBuf(LuaContext&, bool, bool) +{ +} +#endif /* DISABLE_PROTOBUF */ -- cgit v1.2.3