]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
unbreak recursor building with Lua
authorPeter van Dijk <peter.van.dijk@netherlabs.nl>
Mon, 15 Jun 2015 14:59:24 +0000 (16:59 +0200)
committermind04 <mind04@monshouwer.org>
Tue, 30 Jun 2015 06:12:48 +0000 (08:12 +0200)
pdns/lua-pdns.cc
pdns/lua-pdns.hh
pdns/lua-recursor.cc

index ed0bda06fd4ee9a307f4b29c22797f6ac4416ffb..38402635e81be29bc3991a5329c4cb004fd7211e 100644 (file)
@@ -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<DNSResourceRecord>& 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<DNSResourceRecord>& 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<DNSResourceRecord>& ret)
+void popResourceRecordsTable(lua_State *lua, const DNSName &query, vector<DNSResourceRecord>& ret)
 {
   /* get the result */
   DNSResourceRecord rr;
@@ -179,7 +179,7 @@ void popResourceRecordsTable(lua_State *lua, const string &query, vector<DNSReso
     lua_gettable(lua, 2);
 
     uint32_t tmpnum=0;
-    if(!getFromTable(lua, "qtype", tmpnum)) 
+    if(!getFromTable(lua, "qtype", tmpnum))
       rr.qtype=QType::A;
     else
       rr.qtype=tmpnum;
@@ -225,27 +225,27 @@ int netmaskMatchLua(lua_State *lua)
       result = netmaskMatchTable(lua, ip);
     }
     else {
-      for(int n=2 ; n <= lua_gettop(lua); ++n) { 
+      for(int n=2 ; n <= lua_gettop(lua); ++n) {
         string netmask=lua_tostring(lua, n);
         Netmask nm(netmask);
         ComboAddress ca(ip);
-        
+
         result = nm.match(ip);
         if(result)
           break;
       }
     }
   }
-  
+
   lua_pushboolean(lua, result);
   return 1;
 }
 
 int getLocalAddressLua(lua_State* lua)
 {
-  lua_getfield(lua, LUA_REGISTRYINDEX, "__PowerDNSLua"); 
+  lua_getfield(lua, LUA_REGISTRYINDEX, "__PowerDNSLua");
   PowerDNSLua* pl = (PowerDNSLua*)lua_touserdata(lua, -1);
-  
+
   lua_pushstring(lua, pl->getLocal().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()<<urgencylevel<<" "<<message<<endl; 
+    theL()<<urgencylevel<<" "<<message<<endl;
   }
   return 0;
 }
@@ -328,10 +328,10 @@ PowerDNSLua::PowerDNSLua(const std::string& fname)
   luaopen_base(d_lua);
   luaopen_string(d_lua);
 
-  if(lua_dofile(d_lua,  fname.c_str())) 
+  if(lua_dofile(d_lua,  fname.c_str()))
 #else
   luaL_openlibs(d_lua);
-  if(luaL_dofile(d_lua,  fname.c_str())) 
+  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"));
 
@@ -342,8 +342,8 @@ PowerDNSLua::PowerDNSLua(const std::string& fname)
 
   lua_pushcfunction(d_lua, getLocalAddressLua);
   lua_setglobal(d_lua, "getlocaladdress");
-  
-  lua_pushlightuserdata(d_lua, (void*)this); 
+
+  lua_pushlightuserdata(d_lua, (void*)this);
   lua_setfield(d_lua, LUA_REGISTRYINDEX, "__PowerDNSLua");
 }
 
@@ -370,28 +370,28 @@ void luaStackDump (lua_State *L) {
   for (i = 1; i <= top; i++) {  /* repeat for each level */
     int t = lua_type(L, i);
     switch (t) {
-      
+
     case LUA_TSTRING:  /* strings */
       printf("`%s'", lua_tostring(L, i));
       break;
-      
+
     case LUA_TBOOLEAN:  /* booleans */
       printf(lua_toboolean(L, i) ? "true" : "false");
       break;
-      
+
     case LUA_TNUMBER:  /* numbers */
       printf("%g", lua_tonumber(L, i));
       break;
-      
+
     default:  /* other values */
       printf("%s", lua_typename(L, t));
       break;
-      
+
     }
     printf("  ");  /* put a separator */
   }
   printf("\n");  /* end the listing */
 }
-#endif 
+#endif
 
 #endif
index beac27762d093954d8c5f7057ec4236ee9ef7772..be616b0e325bae2249ed1798fd212a267f809e25 100644 (file)
@@ -27,13 +27,13 @@ protected: // FIXME?
   bool getFromTable(const std::string& key, std::string& value);
   bool getFromTable(const std::string& key, uint32_t& value);
   bool d_failed;
-  bool d_variable;  
+  bool d_variable;
   ComboAddress d_local;
 };
 // enum for policy decisions, used by both auth and recursor. Not all values supported everywhere.
 namespace PolicyDecision { enum returnTypes { PASS=-1, DROP=-2, TRUNCATE=-3 }; };
 void pushResourceRecordsTable(lua_State* lua, const vector<DNSResourceRecord>& records);
-void popResourceRecordsTable(lua_State *lua, const string &query, vector<DNSResourceRecord>& ret);
+void popResourceRecordsTable(lua_State *lua, const DNSName &query, vector<DNSResourceRecord>& ret);
 void pushSyslogSecurityLevelTable(lua_State *lua);
 int getLuaTableLength(lua_State* lua, int depth);
 void luaStackDump (lua_State *L);
index d222c4931966ae598d8f2ad93bd93bd9c3a2adc5..a955a796dcd877f5cf81d91e76a38bd5bbea0436 100644 (file)
@@ -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<DNSResourceRecord>& 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<DNSResourceRecord>& ret, const QType& qtype)
 int getFakeAAAARecords(const std::string& qname, const std::string& prefix, vector<DNSResourceRecord>& 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<string>(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<DNSResourceRecord>& ret, int& res, bool* variable)
+bool RecursorLua::nxdomain(const ComboAddress& remote, const ComboAddress& local,const DNSName& query, const QType& qtype, vector<DNSResourceRecord>& 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<DNSResourceRecord>& ret, int& res, bool* variable)
+bool RecursorLua::preresolve(const ComboAddress& remote, const ComboAddress& local,const DNSName& query, const QType& qtype, vector<DNSResourceRecord>& 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<DNSResourceRecord>& ret, int& res, bool* variable)
+bool RecursorLua::nodata(const ComboAddress& remote, const ComboAddress& local,const DNSName& query, const QType& qtype, vector<DNSResourceRecord>& 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<DNSResourceRecord>& ret, int& res, bool* variable)
+bool RecursorLua::postresolve(const ComboAddress& remote, const ComboAddress& local,const DNSName& query, const QType& qtype, vector<DNSResourceRecord>& 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<DNSResourceRecord>& ret, int& res)
+bool RecursorLua::preoutquery(const ComboAddress& ns, const ComboAddress& requestor, const DNSName& query, const QType& qtype, vector<DNSResourceRecord>& 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<DNSResourceRecord>& ret, 
+bool RecursorLua::passthrough(const string& func, const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSResourceRecord>& 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"<<endl;