diff options
Diffstat (limited to 'lib/frrlua.c')
| -rw-r--r-- | lib/frrlua.c | 101 |
1 files changed, 92 insertions, 9 deletions
diff --git a/lib/frrlua.c b/lib/frrlua.c index e97e48121c..535649eff2 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -29,6 +29,8 @@ #include "log.h" #include "buffer.h" +DEFINE_MTYPE(LIB, SCRIPT_RES, "Scripting results"); + /* Lua stuff */ /* @@ -81,7 +83,7 @@ void lua_decode_prefix(lua_State *L, int idx, struct prefix *prefix) void *lua_toprefix(lua_State *L, int idx) { - struct prefix *p = XCALLOC(MTYPE_TMP, sizeof(struct prefix)); + struct prefix *p = XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct prefix)); lua_decode_prefix(L, idx, p); return p; } @@ -153,7 +155,8 @@ void lua_decode_interface(lua_State *L, int idx, struct interface *ifp) } void *lua_tointerface(lua_State *L, int idx) { - struct interface *ifp = XCALLOC(MTYPE_TMP, sizeof(struct interface)); + struct interface *ifp = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct interface)); lua_decode_interface(L, idx, ifp); return ifp; @@ -183,7 +186,8 @@ void lua_decode_inaddr(lua_State *L, int idx, struct in_addr *inaddr) void *lua_toinaddr(lua_State *L, int idx) { - struct in_addr *inaddr = XCALLOC(MTYPE_TMP, sizeof(struct in_addr)); + struct in_addr *inaddr = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct in_addr)); lua_decode_inaddr(L, idx, inaddr); return inaddr; } @@ -213,11 +217,27 @@ void lua_decode_in6addr(lua_State *L, int idx, struct in6_addr *in6addr) void *lua_toin6addr(lua_State *L, int idx) { - struct in6_addr *in6addr = XCALLOC(MTYPE_TMP, sizeof(struct in6_addr)); + struct in6_addr *in6addr = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(struct in6_addr)); lua_decode_in6addr(L, idx, in6addr); return in6addr; } +void lua_pushipaddr(lua_State *L, const struct ipaddr *addr) +{ + if (IS_IPADDR_V4(addr)) + lua_pushinaddr(L, &addr->ipaddr_v4); + else + lua_pushin6addr(L, &addr->ipaddr_v6); +} + +void lua_pushethaddr(lua_State *L, const struct ethaddr *addr) +{ + lua_newtable(L); + lua_pushinteger(L, *(addr->octet)); + lua_setfield(L, -2, "octet"); +} + void lua_pushsockunion(lua_State *L, const union sockunion *su) { char buf[SU_ADDRSTRLEN]; @@ -235,7 +255,9 @@ void lua_pushsockunion(lua_State *L, const union sockunion *su) void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su) { lua_getfield(L, idx, "string"); - str2sockunion(lua_tostring(L, -1), su); + if (str2sockunion(lua_tostring(L, -1), su) < 0) + zlog_err("Lua hook call: Failed to decode sockunion"); + lua_pop(L, 1); /* pop the table */ lua_pop(L, 1); @@ -243,7 +265,8 @@ void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su) void *lua_tosockunion(lua_State *L, int idx) { - union sockunion *su = XCALLOC(MTYPE_TMP, sizeof(union sockunion)); + union sockunion *su = + XCALLOC(MTYPE_SCRIPT_RES, sizeof(union sockunion)); lua_decode_sockunion(L, idx, su); return su; @@ -262,7 +285,7 @@ void lua_decode_timet(lua_State *L, int idx, time_t *t) void *lua_totimet(lua_State *L, int idx) { - time_t *t = XCALLOC(MTYPE_TMP, sizeof(time_t)); + time_t *t = XCALLOC(MTYPE_SCRIPT_RES, sizeof(time_t)); lua_decode_timet(L, idx, t); return t; @@ -283,12 +306,64 @@ void lua_decode_integerp(lua_State *L, int idx, long long *num) void *lua_tointegerp(lua_State *L, int idx) { - long long *num = XCALLOC(MTYPE_TMP, sizeof(long long)); + long long *num = XCALLOC(MTYPE_SCRIPT_RES, sizeof(long long)); lua_decode_integerp(L, idx, num); return num; } +void lua_pushnexthop(lua_State *L, const struct nexthop *nexthop) +{ + lua_newtable(L); + lua_pushinteger(L, nexthop->vrf_id); + lua_setfield(L, -2, "vrf_id"); + lua_pushinteger(L, nexthop->ifindex); + lua_setfield(L, -2, "ifindex"); + lua_pushinteger(L, nexthop->type); + lua_setfield(L, -2, "type"); + lua_pushinteger(L, nexthop->flags); + lua_setfield(L, -2, "flags"); + if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) { + lua_pushinteger(L, nexthop->bh_type); + lua_setfield(L, -2, "bh_type"); + } else if (nexthop->type == NEXTHOP_TYPE_IPV4) { + lua_pushinaddr(L, &nexthop->gate.ipv4); + lua_setfield(L, -2, "gate"); + } else if (nexthop->type == NEXTHOP_TYPE_IPV6) { + lua_pushin6addr(L, &nexthop->gate.ipv6); + lua_setfield(L, -2, "gate"); + } + lua_pushinteger(L, nexthop->nh_label_type); + lua_setfield(L, -2, "nh_label_type"); + lua_pushinteger(L, nexthop->weight); + lua_setfield(L, -2, "weight"); + lua_pushinteger(L, nexthop->backup_num); + lua_setfield(L, -2, "backup_num"); + lua_pushinteger(L, *(nexthop->backup_idx)); + lua_setfield(L, -2, "backup_idx"); + if (nexthop->nh_encap_type == NET_VXLAN) { + lua_pushinteger(L, nexthop->nh_encap.vni); + lua_setfield(L, -2, "vni"); + } + lua_pushinteger(L, nexthop->nh_encap_type); + lua_setfield(L, -2, "nh_encap_type"); + lua_pushinteger(L, nexthop->srte_color); + lua_setfield(L, -2, "srte_color"); +} + +void lua_pushnexthop_group(lua_State *L, const struct nexthop_group *ng) +{ + lua_newtable(L); + struct nexthop *nexthop; + int i = 0; + + for (ALL_NEXTHOPS_PTR(ng, nexthop)) { + lua_pushnexthop(L, nexthop); + lua_seti(L, -2, i); + i++; + } +} + void lua_decode_stringp(lua_State *L, int idx, char *str) { strlcpy(str, lua_tostring(L, idx), strlen(str) + 1); @@ -297,7 +372,7 @@ void lua_decode_stringp(lua_State *L, int idx, char *str) void *lua_tostringp(lua_State *L, int idx) { - char *string = XSTRDUP(MTYPE_TMP, lua_tostring(L, idx)); + char *string = XSTRDUP(MTYPE_SCRIPT_RES, lua_tostring(L, idx)); return string; } @@ -309,6 +384,14 @@ void lua_decode_noop(lua_State *L, int idx, const void *ptr) { } + +/* + * Noop decoder for int. + */ +void lua_decode_integer_noop(lua_State *L, int idx, int i) +{ +} + /* * Logging. * |
