#include "ednssubnet.hh"
#include <unordered_set>
-AuthLua4::AuthLua4(const std::string &fname) : BaseLua4(fname) {
-}
+AuthLua4::AuthLua4() { prepareContext(); }
#if !defined(HAVE_LUA)
class AuthLua4 : public BaseLua4
{
public:
- explicit AuthLua4(const string &fname);
+ AuthLua4();
bool updatePolicy(const DNSName &qname, QType qtype, const DNSName &zonename, DNSPacket *packet);
bool axfrfilter(const ComboAddress&, const DNSName&, const DNSResourceRecord&, std::vector<DNSResourceRecord>&);
~AuthLua4(); // this is so unique_ptr works with an incomplete type
protected:
- void postPrepareContext() override;
- void postLoad() override;
+ virtual void postPrepareContext() override;
+ virtual void postLoad() override;
private:
struct UpdatePolicyQuery {
DNSName qname;
#include "ednssubnet.hh"
#include "lua-base4.hh"
+BaseLua4::BaseLua4() {
+}
+
+void BaseLua4::loadFile(const std::string &fname) {
+ std::ifstream ifs(fname);
+ if(!ifs) {
+ theL()<<Logger::Error<<"Unable to read configuration file from '"<<fname<<"': "<<strerror(errno)<<endl;
+ return;
+ }
+ loadStream(ifs);
+};
+
+void BaseLua4::loadString(const std::string &script) {
+ std::istringstream iss(script);
+ loadStream(iss);
+};
+
#if !defined(HAVE_LUA)
-BaseLua4::BaseLua4(const std::string &fname) { return; }
void BaseLua4::prepareContext() { return; }
+void BaseLua4::loadStream(std::istream &is) { return; }
BaseLua4::~BaseLua4() { }
#else
#undef L
#include "ext/luawrapper/include/LuaContext.hpp"
-BaseLua4::BaseLua4(const std::string &fname) {
- prepareContext();
- std::ifstream ifs(fname);
- if(!ifs) {
- theL()<<Logger::Error<<"Unable to read configuration file from '"<<fname<<"': "<<strerror(errno)<<endl;
- return;
- }
- d_lw->executeCode(ifs);
- postLoad();
-};
-
void BaseLua4::prepareContext() {
d_lw = std::unique_ptr<LuaContext>(new LuaContext);
d_lw->writeVariable("pdns", d_pd);
}
+void BaseLua4::loadStream(std::istream &is) {
+ d_lw->executeCode(is);
+
+ postLoad();
+}
+
BaseLua4::~BaseLua4() { }
#endif
#endif
public:
- explicit BaseLua4(const std::string &fname);
-
+ BaseLua4();
+ void loadFile(const std::string &fname);
+ void loadString(const std::string &script);
+ void loadStream(std::istream &is);
virtual ~BaseLua4(); // this is so unique_ptr works with an incomplete type
protected:
void prepareContext();
#include "rec-snmp.hh"
#include <unordered_set>
-RecursorLua4::RecursorLua4(const std::string &fname) : BaseLua4(fname) {
-}
+RecursorLua4::RecursorLua4() { prepareContext(); }
static int followCNAMERecords(vector<DNSRecord>& ret, const QType& qtype)
{
class RecursorLua4 : public BaseLua4
{
public:
- explicit RecursorLua4(const std::string &fname);
+ RecursorLua4();
~RecursorLua4(); // this is so unique_ptr works with an incomplete type
struct DNSQuestion
typedef std::function<std::tuple<unsigned int,boost::optional<std::unordered_map<int,string> >,boost::optional<LuaContext::LuaObject>,boost::optional<std::string>,boost::optional<std::string> >(ComboAddress, Netmask, ComboAddress, DNSName, uint16_t, const std::map<uint16_t, EDNSOptionView>&, bool)> gettag_t;
gettag_t d_gettag; // public so you can query if we have this hooked
protected:
- void postPrepareContext() override;
- void postLoad() override;
+ virtual void postPrepareContext() override;
+ virtual void postLoad() override;
private:
typedef std::function<bool(DNSQuestion*)> luacall_t;
luacall_t d_prerpz, d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery;
}
else
{
- d_update_policy_lua = std::unique_ptr<AuthLua4>(new AuthLua4(fname));
+ d_update_policy_lua = std::unique_ptr<AuthLua4>(new AuthLua4());
+ d_update_policy_lua->loadFile(fname);
}
}
return new string("unloaded\n");
}
else {
- t_pdl = std::make_shared<RecursorLua4>(fname);
+ t_pdl = std::make_shared<RecursorLua4>();
+ t_pdl->loadFile(fname);
}
}
catch(std::exception& e) {
try {
if(!::arg()["lua-dns-script"].empty()) {
- t_pdl = std::make_shared<RecursorLua4>(::arg()["lua-dns-script"]);
+ t_pdl = std::make_shared<RecursorLua4>();
+ t_pdl->loadFile(::arg()["lua-dns-script"]);
L<<Logger::Warning<<"Loaded 'lua' script from '"<<::arg()["lua-dns-script"]<<"'"<<endl;
}
}