From ba2951ee988b8132dc84f65f1ae8066967aae5c7 Mon Sep 17 00:00:00 2001 From: Peter van Dijk Date: Mon, 15 Jun 2015 16:59:24 +0200 Subject: [PATCH] unbreak recursor building with Lua --- pdns/lua-pdns.cc | 66 ++++++++++++++++++++++---------------------- pdns/lua-pdns.hh | 4 +-- pdns/lua-recursor.cc | 60 ++++++++++++++++++++-------------------- 3 files changed, 65 insertions(+), 65 deletions(-) diff --git a/pdns/lua-pdns.cc b/pdns/lua-pdns.cc index ed0bda06f..38402635e 100644 --- a/pdns/lua-pdns.cc +++ b/pdns/lua-pdns.cc @@ -46,8 +46,8 @@ bool netmaskMatchTable(lua_State* lua, const std::string& ip) Netmask nm(netmask); ComboAddress ca(ip); lua_pop(lua, 1); - - if(nm.match(ip)) + + if(nm.match(ip)) return true; } return false; @@ -83,10 +83,10 @@ static bool getFromTable(lua_State *lua, const std::string &key, uint32_t& value } void pushResourceRecordsTable(lua_State* lua, const vector& records) -{ +{ // make a table of tables lua_newtable(lua); - + int pos=0; BOOST_FOREACH(const DNSResourceRecord& rr, records) { @@ -94,36 +94,36 @@ void pushResourceRecordsTable(lua_State* lua, const vector& r lua_pushnumber(lua, ++pos); // "row" table lua_newtable(lua); - + lua_pushstring(lua, rr.qname.toString().c_str()); lua_setfield(lua, -2, "qname"); // pushes value at the top of the stack to the table immediately below that (-1 = top, -2 is below) - + lua_pushstring(lua, rr.content.c_str()); lua_setfield(lua, -2, "content"); - + lua_pushnumber(lua, rr.qtype.getCode()); lua_setfield(lua, -2, "qtype"); - + lua_pushnumber(lua, rr.ttl); lua_setfield(lua, -2, "ttl"); - + lua_pushnumber(lua, rr.d_place); lua_setfield(lua, -2, "place"); - + lua_pushnumber(lua, rr.qclass); lua_setfield(lua, -2, "qclass"); - + lua_settable(lua, -3); // pushes the table we just built into the master table at position pushed above } } // override the __index metatable under loglevels to return Logger::Error to account for nil accesses to the loglevels table -int loglevels_index(lua_State* lua) +int loglevels_index(lua_State* lua) { lua_pushnumber(lua, Logger::Error); return 1; } // push the loglevel subtable onto the stack that will eventually be the pdns table -void pushSyslogSecurityLevelTable(lua_State* lua) +void pushSyslogSecurityLevelTable(lua_State* lua) { lua_newtable(lua); // this function takes the global lua_state from the PowerDNSLua constructor and populates it with the syslog enums values @@ -159,12 +159,12 @@ int getLuaTableLength(lua_State* lua, int depth) #elif LUA_VERSION_NUM < 502 return lua_objlen(lua, 2); #else - return lua_rawlen(lua, 2); + return lua_rawlen(lua, 2); #endif } // expects a table at offset 2, and, importantly DOES NOT POP IT from the stack - only the contents -void popResourceRecordsTable(lua_State *lua, const string &query, vector& ret) +void popResourceRecordsTable(lua_State *lua, const DNSName &query, vector& ret) { /* get the result */ DNSResourceRecord rr; @@ -179,7 +179,7 @@ void popResourceRecordsTable(lua_State *lua, const string &query, vectorgetLocal().toString().c_str()); return 1; } @@ -253,7 +253,7 @@ int getLocalAddressLua(lua_State* lua) // called by lua to indicate that this answer is 'variable' and should not be cached int setVariableLua(lua_State* lua) { - lua_getfield(lua, LUA_REGISTRYINDEX, "__PowerDNSLua"); + lua_getfield(lua, LUA_REGISTRYINDEX, "__PowerDNSLua"); PowerDNSLua* pl = (PowerDNSLua*)lua_touserdata(lua, -1); pl->setVariable(); return 0; @@ -271,7 +271,7 @@ int logLua(lua_State *lua) } else if(argc >= 2) { string message=lua_tostring(lua, 1); int urgencylevel = lua_tonumber(lua, 2); - theL()<& records); -void popResourceRecordsTable(lua_State *lua, const string &query, vector& ret); +void popResourceRecordsTable(lua_State *lua, const DNSName &query, vector& ret); void pushSyslogSecurityLevelTable(lua_State *lua); int getLuaTableLength(lua_State* lua, int depth); void luaStackDump (lua_State *L); diff --git a/pdns/lua-recursor.cc b/pdns/lua-recursor.cc index d222c4931..a955a796d 100644 --- a/pdns/lua-recursor.cc +++ b/pdns/lua-recursor.cc @@ -67,7 +67,7 @@ extern "C" { static int getRegisteredNameLua(lua_State *L) { const char *name = luaL_checkstring(L, 1); - string regname=getRegisteredName(name); + string regname=getRegisteredName(name).toString(); lua_pushstring(L, regname.c_str()); return 1; } @@ -89,16 +89,16 @@ int followCNAMERecords(vector& ret, const QType& qtype) break; } } - if(target.empty()) + if(target.empty()) return 0; - + if(target[target.size()-1]!='.') target.append(1, '.'); int rcode=directResolve(target, qtype, 1, resolved); // 1 == class BOOST_FOREACH(const DNSResourceRecord& rr, resolved) - { + { ret.push_back(rr); } return rcode; @@ -108,11 +108,11 @@ int followCNAMERecords(vector& ret, const QType& qtype) int getFakeAAAARecords(const std::string& qname, const std::string& prefix, vector& ret) { int rcode=directResolve(qname, QType(QType::A), 1, ret); - + ComboAddress prefixAddress(prefix); BOOST_FOREACH(DNSResourceRecord& rr, ret) - { + { if(rr.qtype.getCode() == QType::A && rr.d_place==DNSResourceRecord::ANSWER) { ComboAddress ipv4(rr.content); uint32_t tmp; @@ -135,19 +135,19 @@ int getFakePTRRecords(const std::string& qname, const std::string& prefix, vecto stringtok(parts, qname, "."); if(parts.size() < 8) return -1; - + string newquery; for(int n = 0; n < 4; ++n) { - newquery += + newquery += lexical_cast(strtol(parts[n*2].c_str(), 0, 16) + 16*strtol(parts[n*2+1].c_str(), 0, 16)); newquery.append(1,'.'); } newquery += "in-addr.arpa."; - + int rcode = directResolve(newquery, QType(QType::PTR), 1, ret); BOOST_FOREACH(DNSResourceRecord& rr, ret) - { + { if(rr.qtype.getCode() == QType::PTR && rr.d_place==DNSResourceRecord::ANSWER) { rr.qname = qname; } @@ -156,7 +156,7 @@ int getFakePTRRecords(const std::string& qname, const std::string& prefix, vecto } -bool RecursorLua::nxdomain(const ComboAddress& remote, const ComboAddress& local,const string& query, const QType& qtype, vector& ret, int& res, bool* variable) +bool RecursorLua::nxdomain(const ComboAddress& remote, const ComboAddress& local,const DNSName& query, const QType& qtype, vector& ret, int& res, bool* variable) { if(d_nofuncs.nxdomain) return false; @@ -164,14 +164,14 @@ bool RecursorLua::nxdomain(const ComboAddress& remote, const ComboAddress& local return passthrough("nxdomain", remote, local, query, qtype, ret, res, variable); } -bool RecursorLua::preresolve(const ComboAddress& remote, const ComboAddress& local,const string& query, const QType& qtype, vector& ret, int& res, bool* variable) +bool RecursorLua::preresolve(const ComboAddress& remote, const ComboAddress& local,const DNSName& query, const QType& qtype, vector& ret, int& res, bool* variable) { if(d_nofuncs.preresolve) return false; return passthrough("preresolve", remote, local, query, qtype, ret, res, variable); } -bool RecursorLua::nodata(const ComboAddress& remote, const ComboAddress& local,const string& query, const QType& qtype, vector& ret, int& res, bool* variable) +bool RecursorLua::nodata(const ComboAddress& remote, const ComboAddress& local,const DNSName& query, const QType& qtype, vector& ret, int& res, bool* variable) { if(d_nofuncs.nodata) return false; @@ -179,14 +179,14 @@ bool RecursorLua::nodata(const ComboAddress& remote, const ComboAddress& local,c return passthrough("nodata", remote, local, query, qtype, ret, res, variable); } -bool RecursorLua::postresolve(const ComboAddress& remote, const ComboAddress& local,const string& query, const QType& qtype, vector& ret, int& res, bool* variable) +bool RecursorLua::postresolve(const ComboAddress& remote, const ComboAddress& local,const DNSName& query, const QType& qtype, vector& ret, int& res, bool* variable) { if(d_nofuncs.postresolve) return false; return passthrough("postresolve", remote, local, query, qtype, ret, res, variable); } -bool RecursorLua::preoutquery(const ComboAddress& ns, const ComboAddress& requestor, const string& query, const QType& qtype, vector& ret, int& res) +bool RecursorLua::preoutquery(const ComboAddress& ns, const ComboAddress& requestor, const DNSName& query, const QType& qtype, vector& ret, int& res) { if(d_nofuncs.preoutquery) return false; @@ -206,27 +206,27 @@ bool RecursorLua::ipfilter(const ComboAddress& remote, const ComboAddress& local lua_pop(d_lua, 1); return false; } - d_local = local; - - ComboAddress* ca=(ComboAddress*)lua_newuserdata(d_lua, sizeof(ComboAddress)); + d_local = local; + + ComboAddress* ca=(ComboAddress*)lua_newuserdata(d_lua, sizeof(ComboAddress)); *ca=remote; luaL_getmetatable(d_lua, "iputils.ca"); lua_setmetatable(d_lua, -2); - if(lua_pcall(d_lua, 1, 1, 0)) { + if(lua_pcall(d_lua, 1, 1, 0)) { string error=string("lua error in 'ipfilter' while processing: ")+lua_tostring(d_lua, -1); lua_pop(d_lua, 1); throw runtime_error(error); return false; } - int newres = (int)lua_tonumber(d_lua, 1); + int newres = (int)lua_tonumber(d_lua, 1); lua_pop(d_lua, 1); return newres != -1; } -bool RecursorLua::passthrough(const string& func, const ComboAddress& remote, const ComboAddress& local, const string& query, const QType& qtype, vector& ret, +bool RecursorLua::passthrough(const string& func, const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector& ret, int& res, bool* variable) { d_variable = false; @@ -239,19 +239,19 @@ bool RecursorLua::passthrough(const string& func, const ComboAddress& remote, co lua_pop(d_lua, 1); return false; } - - d_local = local; + + d_local = local; /* the first argument */ if(strcmp(func.c_str(),"preoutquery")) lua_pushstring(d_lua, remote.toString().c_str() ); else { - ComboAddress* ca=(ComboAddress*)lua_newuserdata(d_lua, sizeof(ComboAddress)); + ComboAddress* ca=(ComboAddress*)lua_newuserdata(d_lua, sizeof(ComboAddress)); *ca=remote; luaL_getmetatable(d_lua, "iputils.ca"); lua_setmetatable(d_lua, -2); } - lua_pushstring(d_lua, query.c_str() ); + lua_pushstring(d_lua, query.toString().c_str() ); lua_pushnumber(d_lua, qtype.getCode() ); int extraParameter = 0; @@ -266,7 +266,7 @@ bool RecursorLua::passthrough(const string& func, const ComboAddress& remote, co } if(lua_pcall(d_lua, 3 + extraParameter, 3, 0)) { // NOTE! Means we always get 3 stack entries back, no matter what our lua hook returned! - string error=string("lua error in '"+func+"' while processing query for '"+query+"|"+qtype.getName()+": ")+lua_tostring(d_lua, -1); + string error=string("lua error in '"+func+"' while processing query for '"+query.toString()+"|"+qtype.getName()+": ")+lua_tostring(d_lua, -1); lua_pop(d_lua, 1); throw runtime_error(error); return false; @@ -274,7 +274,7 @@ bool RecursorLua::passthrough(const string& func, const ComboAddress& remote, co if(variable) *variable |= d_variable; - + if(!lua_isnumber(d_lua, 1)) { string tocall = lua_tostring(d_lua,1); lua_remove(d_lua, 1); // the name @@ -293,14 +293,14 @@ bool RecursorLua::passthrough(const string& func, const ComboAddress& remote, co } else if(tocall == "followCNAMERecords") { popResourceRecordsTable(d_lua, query, ret); - lua_pop(d_lua, 2); + lua_pop(d_lua, 2); res = followCNAMERecords(ret, qtype); } return true; - // returned a followup + // returned a followup } - + int newres = (int)lua_tonumber(d_lua, 1); // new rcode if(newres == -1) { // cerr << "handler did not handle"<