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.
23 #include "dnsdist-async.hh"
24 #include "dnsdist-lua.hh"
25 #include "dnsdist-lua-ffi.hh"
26 #include "dnsdist-lua-network.hh"
29 void setupLuaBindingsNetwork(LuaContext
& luaCtx
, bool client
)
31 luaCtx
.writeFunction("newNetworkEndpoint", [client
](const std::string
& path
) {
33 return std::shared_ptr
<dnsdist::NetworkEndpoint
>(nullptr);
37 return std::make_shared
<dnsdist::NetworkEndpoint
>(path
);
39 catch (const std::exception
& e
) {
40 warnlog("Error connecting to network endpoint: %s", e
.what());
42 return std::shared_ptr
<dnsdist::NetworkEndpoint
>(nullptr);
45 luaCtx
.registerFunction
<bool (std::shared_ptr
<dnsdist::NetworkEndpoint
>::*)() const>("isValid", [](const std::shared_ptr
<dnsdist::NetworkEndpoint
>& endpoint
) {
46 return endpoint
!= nullptr;
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()) {
54 return endpoint
->send(payload
);
57 luaCtx
.writeFunction("newNetworkListener", [client
]() {
59 return std::shared_ptr
<dnsdist::NetworkListener
>(nullptr);
62 return std::make_shared
<dnsdist::NetworkListener
>();
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
) {
70 return listener
->addUnixListeningEndpoint(path
, endpointID
, [cb
= std::move(cb
)](dnsdist::NetworkListener::EndpointID endpoint
, std::string
&& dgram
, const std::string
& from
) {
72 auto lock
= g_lua
.lock();
73 cb(endpoint
, dgram
, from
);
75 dnsdist::handleQueuedAsynchronousEvents();
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
) {
85 return listener
->addUnixListeningEndpoint(path
, endpointID
, [cb
](dnsdist::NetworkListener::EndpointID endpoint
, std::string
&& dgram
, const std::string
& from
) {
87 auto lock
= g_lua
.lock();
88 dnsdist_ffi_network_message_t
msg(dgram
, from
, endpoint
);
91 dnsdist::handleQueuedAsynchronousEvents();
95 luaCtx
.registerFunction
<void (std::shared_ptr
<dnsdist::NetworkListener
>::*)()>("start", [client
](std::shared_ptr
<dnsdist::NetworkListener
>& listener
) {
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());
108 for (const auto& resolver
: resolvers
) {
109 result
.emplace_back(counter
, resolver
.toString());