2017-02-06 14:02:11 +01:00
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "filter/filter.h"
|
2017-02-06 15:25:48 +01:00
|
|
|
#include "lua.h"
|
2017-02-06 14:02:11 +01:00
|
|
|
|
|
|
|
#include <lua.h>
|
|
|
|
#include <lualib.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
|
2018-01-12 15:13:06 +01:00
|
|
|
/* Docs: http://pgl.yoyo.org/luai/i/luaL_dostring */
|
|
|
|
|
|
|
|
struct f_val filter_lua_chunk(const char *chunk, struct rte **e, struct rta *a, struct ea_list **ea, struct linpool *lp) {
|
2017-02-06 14:02:11 +01:00
|
|
|
lua_State *L = luaL_newstate();
|
|
|
|
luaL_openlibs(L);
|
2018-01-12 15:13:06 +01:00
|
|
|
lua_bird_state *lbs = luaB_init(L, lp);
|
2017-02-08 12:36:32 +01:00
|
|
|
luaB_push_route(L, *e);
|
2017-02-06 14:02:11 +01:00
|
|
|
int le = luaL_dostring(L, chunk);
|
2018-01-12 15:13:06 +01:00
|
|
|
struct f_val out = F_VAL_VOID;
|
|
|
|
if (le && lbs->exception) {
|
|
|
|
out = F_VAL(T_RETURN, i, lbs->exception);
|
|
|
|
} else if (le) {
|
|
|
|
log(L_ERR "bad lua: %s", lua_tostring(L, -1));
|
|
|
|
out = F_VAL(T_RETURN, i, F_ERROR);
|
2017-02-06 14:02:11 +01:00
|
|
|
} else if (lua_isnumber(L, -1)) {
|
2018-01-12 15:13:06 +01:00
|
|
|
out = F_VAL(T_INT, i, lua_tonumber(L, -1));
|
2017-02-06 14:02:11 +01:00
|
|
|
} else {
|
2018-01-12 15:13:06 +01:00
|
|
|
log(L_WARN "lua return value is not a number (unimplemented): %s", lua_tostring(L, -1));
|
|
|
|
out = F_VAL(T_RETURN, i, F_ERROR);
|
2017-02-06 14:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_close(L);
|
|
|
|
return out;
|
|
|
|
}
|