diff options
-rw-r--r-- | lua/common.c | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/lua/common.c b/lua/common.c index c414005c..5b22dd4c 100644 --- a/lua/common.c +++ b/lua/common.c @@ -69,6 +69,7 @@ static int luaB_trace(lua_State *L) { #define lua_settableip4(L, idx, val) lua_sett(L, idx, val, ip4) #define lua_settablelightuserdata(L, idx, val) lua_sett(L, idx, val, lightuserdata) #define lua_settableeattr(L, idx, val) lua_sett(L, idx, val, eattr) +#define lua_settablevalue(L, idx, val) lua_sett(L, idx, val, value) #define lua_setglobalcfunction(L, n, val) do { \ lua_pushcfunction(L, val); \ @@ -237,27 +238,54 @@ lua_bird_state *luaB_init(lua_State *L, struct linpool *lp) { return lbs; } +static int luaB_route_ea_find(lua_State *L) { + int n = lua_gettop(L); + if (n != 2) { + log(L_WARN "ea_find needs exactly 1 argument"); + return 0; + } + + lua_pushliteral(L, "_internal"); + lua_gettable(L, 1); + if (!lua_isuserdata(L, -1)) + luaL_error(L, "fatal: bird internal state not found, type %d", lua_type(L, -1)); + + struct rte *e = lua_touserdata(L, -1); + int ea = lua_tointeger(L, 2); + lua_pop(L, 2); + + struct ea_list *eattrs = e->attrs->eattrs; + eattr *t = ea_find(eattrs, ea); + + if (t) { + lua_pusheattr(L, t); + return 1; + } else { + log(L_ERR "eattr not found"); + return 0; + } +} + void luaB_push_route(lua_State *L, struct rte *e) { lua_newtable(L); + lua_settablelightuserdata(L, "_internal", e); lua_settableaddr(L, "prefix", e->net->n.addr); + lua_settablecfunction(L, "ea_find", luaB_route_ea_find); + + lua_newtable(L); + lua_settablevalue(L, "__index", -2-1); + lua_setmetatable(L, -2); + lua_setglobal(L, "route"); } -#define BA_TUNNEL_ENCAP 0x17 - void luaB_push_eattrs(lua_State *L, struct ea_list *ea) { lua_newtable(L); if (!ea) { log(L_ERR "Null eattrs"); } - - eattr *t = ea_find(ea, EA_CODE(PROTOCOL_BGP, BA_TUNNEL_ENCAP)); - - if (t) { - lua_settableeattr(L, "tunnel_encap", t); - } else - log(L_ERR "Tunnel encap not found"); - lua_setglobal(L, "eattrs"); + lua_settablecfunction(L, "__tostring", luaB_addr_tostring); + lua_setmetatable(L, -2); } |