From: Bert Hubert Date: Sat, 14 Jun 2008 12:23:11 +0000 (+0000) Subject: fix lua5.0 compatability for the enterprisetards, plus spiff up the lua bridge to... X-Git-Tag: rec-3.1.7.1~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4665c31e1191cb167e34eaed9fa022bddbeabc3e;p=thirdparty%2Fpdns.git fix lua5.0 compatability for the enterprisetards, plus spiff up the lua bridge to allow setting the 'place' of answers and the rcode, plus optional ability to override the qname git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1198 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- diff --git a/pdns/Makefile-recursor b/pdns/Makefile-recursor index c9d086859d..7fed2415ea 100644 --- a/pdns/Makefile-recursor +++ b/pdns/Makefile-recursor @@ -8,8 +8,13 @@ CFLAGS:=$(CFLAGS) -Wall $(OPTFLAGS) $(PROFILEFLAGS) LINKCC=$(CXX) CC?=gcc -LUA_CPPFLAGS_CONFIG ?= -I/usr/include/lua5.1 -LUA_LIBS_CONFIG ?= -llua5.1 +# Lua 5.1 settings +#LUA_CPPFLAGS_CONFIG ?= -I/usr/include/lua5.1 +#LUA_LIBS_CONFIG ?= -llua5.1 + +# Lua 5.0 settings +LUA_CPPFLAGS_CONFIG=-I/usr/include/lua50 +LUA_LIBS_CONFIG=-llua50 -llualib50 # static dependencies diff --git a/pdns/lua-pdns-recursor.cc b/pdns/lua-pdns-recursor.cc index a6ceef5e82..4109209986 100644 --- a/pdns/lua-pdns-recursor.cc +++ b/pdns/lua-pdns-recursor.cc @@ -65,7 +65,11 @@ PowerDNSLua::PowerDNSLua(const std::string& fname) luaopen_string(d_lua); lua_settop(d_lua, 0); +#ifndef LUA_VERSION_NUM + if(lua_dofile(d_lua, fname.c_str())) +#else if(luaL_dofile(d_lua, fname.c_str())) +#endif throw runtime_error(string("Error loading LUA file '")+fname+"': "+ string(lua_isstring(d_lua, -1) ? lua_tostring(d_lua, -1) : "unknown error")); lua_pushcfunction(d_lua, netmaskMatchLua); @@ -82,6 +86,36 @@ bool PowerDNSLua::prequery(const ComboAddress& remote, const string& query, cons return passthrough("prequery", remote, query, qtype, ret, res); } +bool PowerDNSLua::getFromTable(const std::string& key, std::string& value) +{ + lua_pushstring(d_lua, key.c_str()); // 4 is now '1' + lua_gettable(d_lua, -2); // replace by the first entry of our table we hope + + bool ret=false; + if(!lua_isnil(d_lua, -1)) { + value = lua_tostring(d_lua, -1); + ret=true; + } + lua_pop(d_lua, 1); + return ret; +} + + +bool PowerDNSLua::getFromTable(const std::string& key, uint32_t& value) +{ + lua_pushstring(d_lua, key.c_str()); // 4 is now '1' + lua_gettable(d_lua, -2); // replace by the first entry of our table we hope + + bool ret=false; + if(!lua_isnil(d_lua, -1)) { + value = lua_tonumber(d_lua, -1); + ret=true; + } + lua_pop(d_lua, 1); + return ret; +} + + bool PowerDNSLua::passthrough(const string& func, const ComboAddress& remote, const string& query, const QType& qtype, vector& ret, int& res) { lua_getglobal(d_lua, func.c_str()); @@ -96,11 +130,14 @@ bool PowerDNSLua::passthrough(const string& func, const ComboAddress& remote, co lua_pushnumber(d_lua, qtype.getCode() ); lua_call(d_lua, 3, 2); - if(!lua_toboolean(d_lua, 1)) { + res = lua_tonumber(d_lua, 1); // new rcode + if(res < 0) { // cerr << "handler did not handle"<& ret, int& res); + bool getFromTable(const std::string& key, std::string& value); + bool getFromTable(const std::string& key, uint32_t& value); bool d_failed; }; diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 1d4bd78249..e8f26042b3 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -317,9 +317,9 @@ int asendto(const char *data, int len, int flags, for(; chain.first != chain.second; chain.first++) { if(chain.first->key.fd > -1) { // don't chain onto existing chained waiter! - // cerr<<"Orig: "<key.domain<<", "<key.remote.toString()<<", id="<key.id - // <<", count="<key.chain.size()<<", origfd: "<key.fd<key.domain<<", "<key.remote.toString()<<", id="<key.id + <<", count="<key.chain.size()<<", origfd: "<key.fd<key.chain.insert(id); // we can chain *fd=-1; // gets used in waitEvent / sendEvent later on @@ -538,11 +538,12 @@ void startDoResolve(void *p) 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)) + 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(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) { @@ -1243,15 +1244,18 @@ void handleTCPClientWritable(int fd, FDMultiplexer::funcparam_t& var) // resend event to everybody chained onto it void doResends(MT_t::waiters_t::iterator& iter, PacketID resend, const string& content) { + if(iter->key.chain.empty()) return; + cerr<<"doResends called!\n"; for(PacketID::chain_t::iterator i=iter->key.chain.begin(); i != iter->key.chain.end() ; ++i) { resend.fd=-1; resend.id=*i; + cerr<<"\tResending "<run(&g_now); + Utility::gettimeofday(&g_now, 0); if(listenOnTCP) { if(TCPConnection::s_currentConnections > maxTcpClients) { // shutdown diff --git a/pdns/powerdns-example-script.lua b/pdns/powerdns-example-script.lua index 8f709b95a7..dcbe5f4560 100644 --- a/pdns/powerdns-example-script.lua +++ b/pdns/powerdns-example-script.lua @@ -2,17 +2,21 @@ function prequery ( ip, domain, qtype ) print ("prequery handler called for: ", ip, domain, qtype) ret = {} - ret[0]= {1, "9.8.7.6", 3601}; - ret[1]= {1, "10.11.12.13", 3601}; - ret[2]= {1, "11.12.13.14", 3601}; - +-- ret[1]= {1, "10.11.12.13", 3601}; +-- ret[2]= {1, "11.12.13.14", 3601}; if domain == "www.ds9c.nl." then + ret[0]= {qtype=1, content="9.8.7.6", ttl=3601} + ret[1]= {qtype=1, content="1.2.3.4", ttl=3601} print "dealing!" - return 1, ret + return 0, ret + elseif domain == "www.baddomain.com." + then + print "dealing - nx" + return 3, ret else print "not dealing!" - return false, ret + return -1, ret end end @@ -20,16 +24,18 @@ function nxdomain ( ip, domain, qtype ) print ("nxhandler called for: ", ip, domain, qtype) ret={} if qtype ~= 1 then return false, ret end -- only A records - if not string.match(domain, "^www.") then return false, ret end -- only things that start with www. +-- if not string.match(domain, "^www.") then return false, ret end -- only things that start with www. if matchnetmask(ip, "127.0.0.1/8") then print "dealing" - ret[0]={1, "127.1.2.3", 3602} - ret[1]={15, "25 ds9a.nl", 3602} - return 1, ret + ret[0]={qtype="5", content="www.webserver.com", ttl=3602} + ret[1]={qname="www.webserver.com", qtype="1", content="1.2.3.4", ttl=3602} + ret[2]={qname="webserver.com", qtype="2", content="ns1.webserver.com", place=2} +-- ret[1]={15, "25 ds9a.nl", 3602} + return 0, ret else print "not dealing" - return false, ret + return -1, ret end end