]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdistdist/dnsdist-lua-bindings-protobuf.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
24 #include "dnsdist-lua.hh"
26 #include "dnsdist-protobuf.hh"
28 #include "fstrm_logger.hh"
29 #include "remote_logger.hh"
32 #include "ipcipher.hh"
33 #endif /* HAVE_LIBCRYPTO */
36 static void parseFSTRMOptions(const boost::optional
<std::unordered_map
<std::string
, unsigned int>>& params
, std::unordered_map
<string
, unsigned int>& options
)
42 static std::vector
<std::string
> const potentialOptions
= { "bufferHint", "flushTimeout", "inputQueueSize", "outputQueueSize", "queueNotifyThreshold", "reopenInterval" };
44 for (const auto& potentialOption
: potentialOptions
) {
45 if (params
->count(potentialOption
)) {
46 options
[potentialOption
] = boost::get
<unsigned int>(params
->at(potentialOption
));
50 #endif /* HAVE_FSTRM */
52 void setupLuaBindingsProtoBuf(bool client
, bool configCheck
)
55 g_lua
.registerFunction
<ComboAddress(ComboAddress::*)(const std::string
& key
)>("ipencrypt", [](const ComboAddress
& ca
, const std::string
& key
) {
56 return encryptCA(ca
, key
);
58 g_lua
.registerFunction
<ComboAddress(ComboAddress::*)(const std::string
& key
)>("ipdecrypt", [](const ComboAddress
& ca
, const std::string
& key
) {
59 return decryptCA(ca
, key
);
62 g_lua
.writeFunction("makeIPCipherKey", [](const std::string
& password
) {
63 return makeIPCipherKey(password
);
65 #endif /* HAVE_LIBCRYPTO */
68 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(std::string
)>("setTag", [](DNSDistProtoBufMessage
& message
, const std::string
& strValue
) {
69 message
.addTag(strValue
);
71 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(vector
<pair
<int, string
>>)>("setTagArray", [](DNSDistProtoBufMessage
& message
, const vector
<pair
<int, string
>>&tags
) {
72 for (const auto& tag
: tags
) {
73 message
.addTag(tag
.second
);
76 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(boost::optional
<time_t> sec
, boost::optional
<uint32_t> uSec
)>("setProtobufResponseType",
77 [](DNSDistProtoBufMessage
& message
, boost::optional
<time_t> sec
, boost::optional
<uint32_t> uSec
) {
78 message
.setType(DNSProtoBufMessage::Response
);
79 message
.setQueryTime(sec
?*sec
:0, uSec
?*uSec
:0);
81 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(const std::string
& strQueryName
, uint16_t uType
, uint16_t uClass
, uint32_t uTTL
, const std::string
& strBlob
)>("addResponseRR", [](DNSDistProtoBufMessage
& message
,
82 const std::string
& strQueryName
, uint16_t uType
, uint16_t uClass
, uint32_t uTTL
, const std::string
& strBlob
) {
83 message
.addRR(DNSName(strQueryName
), uType
, uClass
, uTTL
, strBlob
);
85 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(const Netmask
&)>("setEDNSSubnet", [](DNSDistProtoBufMessage
& message
, const Netmask
& subnet
) { message
.setEDNSSubnet(subnet
); });
86 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(const DNSName
&, uint16_t, uint16_t)>("setQuestion", [](DNSDistProtoBufMessage
& message
, const DNSName
& qname
, uint16_t qtype
, uint16_t qclass
) { message
.setQuestion(qname
, qtype
, qclass
); });
87 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(size_t)>("setBytes", [](DNSDistProtoBufMessage
& message
, size_t bytes
) { message
.setBytes(bytes
); });
88 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(time_t, uint32_t)>("setTime", [](DNSDistProtoBufMessage
& message
, time_t sec
, uint32_t usec
) { message
.setTime(sec
, usec
); });
89 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(time_t, uint32_t)>("setQueryTime", [](DNSDistProtoBufMessage
& message
, time_t sec
, uint32_t usec
) { message
.setQueryTime(sec
, usec
); });
90 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(uint8_t)>("setResponseCode", [](DNSDistProtoBufMessage
& message
, uint8_t rcode
) { message
.setResponseCode(rcode
); });
91 g_lua
.registerFunction
<std::string(DNSDistProtoBufMessage::*)()>("toDebugString", [](const DNSDistProtoBufMessage
& message
) { return message
.toDebugString(); });
92 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(const ComboAddress
&, boost::optional
<uint16_t>)>("setRequestor", [](DNSDistProtoBufMessage
& message
, const ComboAddress
& addr
, boost::optional
<uint16_t> port
) {
93 message
.setRequestor(addr
);
95 message
.setRequestorPort(*port
);
98 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(const std::string
&, boost::optional
<uint16_t>)>("setRequestorFromString", [](DNSDistProtoBufMessage
& message
, const std::string
& str
, boost::optional
<uint16_t> port
) {
99 message
.setRequestor(str
);
101 message
.setRequestorPort(*port
);
104 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(const ComboAddress
&, boost::optional
<uint16_t>)>("setResponder", [](DNSDistProtoBufMessage
& message
, const ComboAddress
& addr
, boost::optional
<uint16_t> port
) {
105 message
.setResponder(addr
);
107 message
.setResponderPort(*port
);
110 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(const std::string
&, boost::optional
<uint16_t>)>("setResponderFromString", [](DNSDistProtoBufMessage
& message
, const std::string
& str
, boost::optional
<uint16_t> port
) {
111 message
.setResponder(str
);
113 message
.setResponderPort(*port
);
116 g_lua
.registerFunction
<void(DNSDistProtoBufMessage::*)(const std::string
&)>("setServerIdentity", [](DNSDistProtoBufMessage
& message
, const std::string
& str
) {
117 message
.setServerIdentity(str
);
120 g_lua
.registerFunction
<std::string(DnstapMessage::*)()>("toDebugString", [](const DnstapMessage
& message
) { return message
.toDebugString(); });
121 g_lua
.registerFunction
<void(DnstapMessage::*)(const std::string
&)>("setExtra", [](DnstapMessage
& message
, const std::string
& str
) {
122 message
.setExtra(str
);
126 g_lua
.writeFunction("newRemoteLogger", [client
,configCheck
](const std::string
& remote
, boost::optional
<uint16_t> timeout
, boost::optional
<uint64_t> maxQueuedEntries
, boost::optional
<uint8_t> reconnectWaitTime
) {
127 if (client
|| configCheck
) {
128 return std::shared_ptr
<RemoteLoggerInterface
>(nullptr);
130 return std::shared_ptr
<RemoteLoggerInterface
>(new RemoteLogger(ComboAddress(remote
), timeout
? *timeout
: 2, maxQueuedEntries
? (*maxQueuedEntries
*100) : 10000, reconnectWaitTime
? *reconnectWaitTime
: 1, client
));
133 g_lua
.writeFunction("newFrameStreamUnixLogger", [client
,configCheck
](const std::string
& address
, boost::optional
<std::unordered_map
<std::string
, unsigned int>> params
) {
135 if (client
|| configCheck
) {
136 return std::shared_ptr
<RemoteLoggerInterface
>(nullptr);
139 std::unordered_map
<string
, unsigned int> options
;
140 parseFSTRMOptions(params
, options
);
141 return std::shared_ptr
<RemoteLoggerInterface
>(new FrameStreamLogger(AF_UNIX
, address
, !client
, options
));
143 throw std::runtime_error("fstrm support is required to build an AF_UNIX FrameStreamLogger");
144 #endif /* HAVE_FSTRM */
147 g_lua
.writeFunction("newFrameStreamTcpLogger", [client
,configCheck
](const std::string
& address
, boost::optional
<std::unordered_map
<std::string
, unsigned int>> params
) {
148 #if defined(HAVE_FSTRM) && defined(HAVE_FSTRM_TCP_WRITER_INIT)
149 if (client
|| configCheck
) {
150 return std::shared_ptr
<RemoteLoggerInterface
>(nullptr);
153 std::unordered_map
<string
, unsigned int> options
;
154 parseFSTRMOptions(params
, options
);
155 return std::shared_ptr
<RemoteLoggerInterface
>(new FrameStreamLogger(AF_INET
, address
, !client
, options
));
157 throw std::runtime_error("fstrm with TCP support is required to build an AF_INET FrameStreamLogger");
158 #endif /* HAVE_FSTRM */
161 g_lua
.registerFunction
<std::string(std::shared_ptr
<RemoteLoggerInterface
>::*)()>("toString", [](const std::shared_ptr
<RemoteLoggerInterface
>& logger
) {
163 return logger
->toString();
165 return std::string();