From: Bert Hubert Date: Thu, 12 Jun 2008 18:39:32 +0000 (+0000) Subject: implement & hookup the lua hooks X-Git-Tag: rec-3.1.7.1~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4485aa358637580425efc116db224e7dcc235c06;p=thirdparty%2Fpdns.git implement & hookup the lua hooks git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1196 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- diff --git a/pdns/Makefile-recursor b/pdns/Makefile-recursor index c6b4b555df..ce0b7ebe1c 100644 --- a/pdns/Makefile-recursor +++ b/pdns/Makefile-recursor @@ -14,8 +14,7 @@ PDNS_RECURSOR_OBJECTS=syncres.o misc.o unix_utility.o qtype.o logger.o \ arguments.o lwres.o pdns_recursor.o recursor_cache.o dnsparser.o \ dnswriter.o dnsrecords.o rcpgenerator.o base64.o zoneparser-tng.o \ rec_channel.o rec_channel_rec.o malloc.o selectmplexer.o sillyrecords.o \ -dns_random.o aescrypt.o aeskey.o aes_modes.o aestab.o - +dns_random.o aescrypt.o aeskey.o aes_modes.o aestab.o lua-pdns-recursor.o REC_CONTROL_OBJECTS=rec_channel.o rec_control.o arguments.o @@ -25,15 +24,23 @@ all: message pdns_recursor rec_control # OS specific instructions -include sysdeps/$(shell uname).inc +ifeq ($(LUA), 1) + LUALIBS=-llua5.1 + CXXFLAGS+=-I/usr/include/lua5.1 -DPDNS_ENABLE_LUA +endif + + ifeq ($(STATIC),semi) - STATICFLAGS=-Wl,-Bstatic -lstdc++ -lgcc -Wl,-Bdynamic -static-libgcc -lm -lc + STATICFLAGS=-Wl,-Bstatic -lstdc++ $(LUALIBS) -lgcc -Wl,-Bdynamic -static-libgcc -lm -lc LINKCC=$(CC) -endif -ifeq ($(STATIC),full) - STATICFLAGS=-lstdc++ -lm -static +elseifeq ($(STATIC),full) + STATICFLAGS=-lstdc++ -lm $(LUALIBS) -static LINKCC=$(CC) +else + LDFLAGS += $(LUALIBS) endif + LDFLAGS += $(PROFILEFLAGS) $(STATICFLAGS) message: diff --git a/pdns/lua-pdns-recursor.cc b/pdns/lua-pdns-recursor.cc index f6278df71d..8ccceb2997 100644 --- a/pdns/lua-pdns-recursor.cc +++ b/pdns/lua-pdns-recursor.cc @@ -1,4 +1,39 @@ #include "lua-pdns-recursor.hh" + +#ifdef PDNS_ENABLE_LUA +#define PDNS_DO_LUA +#endif + +#ifdef LIBDIR +#define PDNS_DO_LUA +#endif + +#if !defined(PDNS_DO_LUA) && !defined(LIBDIR) + +// stub implementation + +PowerDNSLua::PowerDNSLua(const std::string& fname) +{ + throw runtime_error("Lua support disabled"); +} + +bool PowerDNSLua::nxdomain(const ComboAddress& remote, const string& query, const QType& qtype, vector& ret, int& res) +{ + return false; +} + +bool PowerDNSLua::prequery(const ComboAddress& remote, const string& query, const QType& qtype, vector& ret, int& res) +{ + return false; +} + +PowerDNSLua::~PowerDNSLua() +{ + +} + +#else + extern "C" { #undef L /* Include the Lua API header files. */ @@ -35,6 +70,8 @@ PowerDNSLua::PowerDNSLua(const std::string& fname) { d_lua = lua_open(); luaopen_base(d_lua); + luaopen_string(d_lua); + lua_settop(d_lua, 0); if(luaL_dofile(d_lua, fname.c_str())) throw runtime_error(string("Error loading LUA file '")+fname+"': "+ string(lua_isstring(d_lua, -1) ? lua_tostring(d_lua, -1) : "unknown error")); @@ -95,12 +132,18 @@ bool PowerDNSLua::passthrough(const string& func, const ComboAddress& remote, co rr.qtype = QType((int)(lua_tonumber(d_lua, -1))); lua_pop(d_lua, 1); - - lua_pushnumber(d_lua, 2); // 4 is now '1' + lua_pushnumber(d_lua, 2); // 4 is now '2' lua_gettable(d_lua, 3); // replace by the second entry of our table we hope - rr.content= lua_tostring(d_lua, -1); - lua_pop(d_lua, 2); // content + table + lua_pop(d_lua, 1); // content + + lua_pushnumber(d_lua, 3); // 4 is now '3' + lua_gettable(d_lua, 3); // replace by the second entry of our table we hope + rr.ttl = (uint32_t)lua_tonumber(d_lua, -1); + lua_pop(d_lua, 1); // content + + + lua_pop(d_lua, 1); // table // cerr<<"Adding content '"< g_pdl; using namespace boost; #ifdef __FreeBSD__ // see cvstrac ticket #26 @@ -534,7 +536,14 @@ void startDoResolve(void *p) if(!dc->d_mdp.d_header.rd) sr.setCacheOnly(); - int res=sr.beginResolve(dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_mdp.d_qclass, ret); + int res; + + if(!g_pdl.get() || !g_pdl->prequery(dc->d_remote, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res)) + res = sr.beginResolve(dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_mdp.d_qclass, ret); + + if(g_pdl.get() && (res < 0 || res == RCode::NXDomain || res == RCode::ServFail)) { + g_pdl->nxdomain(dc->d_remote, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res); + } if(res<0) { pw.getHeader()->rcode=RCode::ServFail; @@ -1610,6 +1619,35 @@ void parseAuthAndForwards() } } +string doReloadLuaScript(vector::const_iterator begin, vector::const_iterator end) +{ + string fname=::arg()["lua-dns-script"]; + try { + if(begin==end) { + if(!fname.empty()) + g_pdl = shared_ptr(new PowerDNSLua(fname)); + else + throw runtime_error("Asked to relead lua scripts, but no name passed and no default ('lua-dns-script') defined"); + } + else { + fname=*begin; + if(fname.empty()) { + g_pdl.reset(); + L<(new PowerDNSLua(fname)); + } + } + catch(exception& e) { + L<(new PowerDNSLua(::arg()["lua-dns-script"])); + L< empty; + empty.push_back(string()); + return doReloadLuaScript(empty.begin(), empty.end()); + } + + if(cmd=="top-remotes") return doTopRemotes(); diff --git a/pdns/syncres.hh b/pdns/syncres.hh index 7d455136dc..81d746e16e 100644 --- a/pdns/syncres.hh +++ b/pdns/syncres.hh @@ -501,6 +501,8 @@ struct RecursorStats } }; +string doReloadLuaScript(vector::const_iterator begin, vector::const_iterator end); + extern RecursorStats g_stats;