Netmask nm(netmask);
ComboAddress ca(ip);
lua_pop(lua, 1);
-
- if(nm.match(ip))
+
+ if(nm.match(ip))
return true;
}
return false;
}
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)
{
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
#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;
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;
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;
}
// 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;
} 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;
}
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"));
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");
}
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
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;
}
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;
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;
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;
}
}
-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;
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;
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;
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;
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;
}
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;
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
}
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;