File: lua_functions.cc

package info (click to toggle)
pdns 3.1-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,356 kB
  • sloc: cpp: 42,003; ansic: 22,326; sh: 18,273; sql: 618; makefile: 590; yacc: 222; lex: 133; perl: 52
file content (320 lines) | stat: -rw-r--r-- 7,424 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
    Copyright (C) 2011 Fredrik Danerklint

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as published 
    by the Free Software Foundation

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#define LUABACKEND_EXTERN_F_HH

#include "luabackend.hh"

#include "pdns/logger.hh"
#include "pdns/arguments.hh"
#include "pdns/dnspacket.hh"

#include <iostream>
#include <sstream>
using namespace std;

const luaL_Reg lualibs[] = {
    {"", luaopen_base},
    {LUA_LOADLIBNAME, luaopen_package},
    {LUA_TABLIBNAME, luaopen_table},
    {LUA_IOLIBNAME, luaopen_io},
    {LUA_OSLIBNAME, luaopen_os},
    {LUA_STRLIBNAME, luaopen_string},
    {LUA_MATHLIBNAME, luaopen_math},
    {LUA_DBLIBNAME, luaopen_debug},
//    {LUA_COLIBNAME, luaopen_coroutine},
#ifdef USE_LUAJIT    
    {"bit",     luaopen_bit},
    {"jit",     luaopen_jit},
#endif
    {NULL, NULL}
};

int my_lua_panic (lua_State *lua) {
    lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND"); 
    LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);
    
    assert(lua == lb->lua);
    
    stringstream e;
    
    e << lb->backend_name << "LUA PANIC! '" << lua_tostring(lua,-1) << "'" << endl;
    
    throw LUAException (e.str());
    
    return 0;
}

int l_arg_get (lua_State *lua) {
    int i = lua_gettop(lua);
    if (i < 1)
	return 0;
	
    lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND"); 
    LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);

    string a = lua_tostring(lua, 1);

    if (::arg().isEmpty(a))
	lua_pushnil(lua);
    else 
        lua_pushstring(lua, lb->my_getArg(a).c_str());

    return 1;
}

int l_arg_mustdo (lua_State *lua) {
    int i = lua_gettop(lua);
    if (i < 1)
	return 0;
	
    lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND"); 
    LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);
    
    string a = lua_tostring(lua, 1);

    if (::arg().isEmpty(a))
	lua_pushnil(lua);
    else 
        lua_pushboolean(lua, lb->my_mustDo(a));

    return 1;
}

int l_dnspacket (lua_State *lua) {
    lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND"); 
    LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);

    if (lb->dnspacket == NULL) {
	lua_pushnil(lua);
	
	return 1;
    }

    lua_pushstring(lua, lb->dnspacket->getRemote().c_str());
    lua_pushnumber(lua, lb->dnspacket->getRemotePort());
    lua_pushstring(lua, lb->dnspacket->getLocal().c_str());
    
    return 3;
}

int l_logger (lua_State *lua) {
//    assert(lua == lb->lua);
    
    lua_getfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND"); 
    LUABackend* lb = (LUABackend*)lua_touserdata(lua, -1);
    
    int i = lua_gettop(lua);
    if (i < 1)
	return 0;

    int log_level = 0;
    stringstream s;
    int j;
    const char *ss;

    log_level = lua_tointeger(lua, 1);
    
    string space = "";
    
    for(j=2; j<=i; j++) {
	ss = lua_tostring(lua, j);
	s << space << ss;
	space = " ";
    }
    
    L.log(lb->backend_name + s.str(), (Logger::Urgency) log_level);
    
    return 0;
}

void register_lua_functions(lua_State *lua) {
    lua_gc(lua, LUA_GCSTOP, 0);  // stop collector during initialization 

    const luaL_Reg *lib = lualibs;
    for (; lib->func; lib++) {
        lua_pushcfunction(lua, lib->func);
        lua_pushstring(lua, lib->name);
        lua_call(lua, 1, 0);
    }

    lua_gc(lua, LUA_GCRESTART, 0);

    lua_pushinteger(lua, Logger::All);
    lua_setglobal(lua, "log_all");

    lua_pushinteger(lua, Logger::NTLog);
    lua_setglobal(lua, "log_ntlog");

    lua_pushinteger(lua, Logger::Alert);
    lua_setglobal(lua, "log_alert");

    lua_pushinteger(lua, Logger::Critical);
    lua_setglobal(lua, "log_critical");

    lua_pushinteger(lua, Logger::Error);
    lua_setglobal(lua, "log_error");

    lua_pushinteger(lua, Logger::Warning);
    lua_setglobal(lua, "log_warning");

    lua_pushinteger(lua, Logger::Notice);
    lua_setglobal(lua, "log_notice");

    lua_pushinteger(lua, Logger::Info);
    lua_setglobal(lua, "log_info");
    
    lua_pushinteger(lua, Logger::Debug);
    lua_setglobal(lua, "log_debug");

    lua_pushinteger(lua, Logger::None);
    lua_setglobal(lua, "log_none");
    
    lua_pushcfunction(lua, l_dnspacket);
    lua_setglobal(lua, "dnspacket");
    
    lua_pushcfunction(lua, l_logger);
    lua_setglobal(lua, "logger");

    lua_pushcfunction(lua, l_arg_get);
    lua_setglobal(lua, "getarg");

    lua_pushcfunction(lua, l_arg_mustdo);
    lua_setglobal(lua, "mustdo");
    
    lua_newtable(lua);
    for(vector<QType::namenum>::const_iterator iter = QType::names.begin(); iter != QType::names.end(); ++iter) {
	lua_pushnumber(lua, iter->second);
	lua_setfield(lua, -2, iter->first.c_str());
    }
    lua_pushnumber(lua, 3);
    lua_setfield(lua, -2, "NXDOMAIN");
    lua_setglobal(lua, "QTypes");
}

bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, string& value) {
  lua_pushstring(lua, key.c_str()); 
  lua_gettable(lua, -2);  

  bool ret = false;
  
  if(!lua_isnil(lua, -1)) {
    value = lua_tostring(lua, -1);
    ret = true;
  }
  
  lua_pop(lua, 1);
  
  return ret;
}

bool LUABackend::getValueFromTable(lua_State *lua, uint32_t key, string& value) {
  lua_pushnumber(lua, key); 
  lua_gettable(lua, -2);  

  bool ret = false;
  
  if(!lua_isnil(lua, -1)) {
    value = lua_tostring(lua, -1);
    ret = true;
  }
  
  lua_pop(lua, 1);
  
  return ret;
}

bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, time_t& value) {
  lua_pushstring(lua, key.c_str()); 
  lua_gettable(lua, -2);  

  bool ret = false;
  
  if(!lua_isnil(lua, -1)) {
    value = (time_t)lua_tonumber(lua, -1);
    ret = true;
  }
  
  lua_pop(lua, 1);
  
  return ret;
}

bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, uint32_t& value) {
  lua_pushstring(lua, key.c_str()); 
  lua_gettable(lua, -2);  

  bool ret = false;
  
  if(!lua_isnil(lua, -1)) {
    value = (uint32_t)lua_tonumber(lua, -1);
    ret = true;
  }
  
  lua_pop(lua, 1);
  
  return ret;
}

bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, uint16_t& value) {
  lua_pushstring(lua, key.c_str()); 
  lua_gettable(lua, -2);  

  bool ret = false;
  
  if(!lua_isnil(lua, -1)) {
    value = (uint16_t)lua_tonumber(lua, -1);
    ret = true;
  }
  
  lua_pop(lua, 1);
  
  return ret;
}

bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, int& value) {
  lua_pushstring(lua, key.c_str()); 
  lua_gettable(lua, -2);  

  bool ret = false;
  
  if(!lua_isnil(lua, -1)) {
    value = (int)lua_tonumber(lua, -1);
    ret = true;
  }
  
  lua_pop(lua, 1);
  
  return ret;
}

bool LUABackend::getValueFromTable(lua_State *lua, const std::string& key, bool& value) {
  lua_pushstring(lua, key.c_str()); 
  lua_gettable(lua, -2);  

  bool ret = false;
  
  if(!lua_isnil(lua, -1)) {
    value = lua_toboolean(lua, -1);
    ret = true;
  }
  
  lua_pop(lua, 1);
  
  return ret;
}