From 42ae55b5d4dcfed694862b969e8c6fb201418a0d Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 16:00:36 -0500 Subject: [PATCH] lib: add more type encoder funcs - in_addr - in6_addr - sockunion Signed-off-by: Quentin Young --- lib/frrlua.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/frrlua.h | 24 +++++++++++++++++----- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lib/frrlua.c b/lib/frrlua.c index f768575b66..ac28082680 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -105,6 +105,62 @@ int lua_pushinterface(lua_State *L, const struct interface *ifp) return 0; } +int lua_pushinaddr(lua_State *L, const struct in_addr *addr) +{ + zlog_debug("frrlua: pushing inaddr table"); + + char buf[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, addr, buf, sizeof(buf)); + + lua_newtable(L); + lua_pushinteger(L, addr->s_addr); + lua_setfield(L, -2, "value"); + lua_pushstring(L, buf); + lua_setfield(L, -2, "string"); + + return 0; +} + + +int lua_pushin6addr(lua_State *L, const struct in6_addr *addr) +{ + zlog_debug("frrlua: pushing in6addr table"); + + char buf[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, addr, buf, sizeof(buf)); + + lua_newtable(L); + lua_pushlstring(L, (const char *)addr->s6_addr, 16); + lua_setfield(L, -2, "value"); + lua_pushstring(L, buf); + lua_setfield(L, -2, "string"); + + return 0; +} + +int lua_pushsockunion(lua_State *L, const union sockunion *su) +{ + zlog_debug("frrlua: pushing sockunion table"); + + char buf[SU_ADDRSTRLEN]; + sockunion2str(su, buf, sizeof(buf)); + + lua_newtable(L); + lua_pushlstring(L, (const char *)sockunion_get_addr(su), + sockunion_get_addrlen(su)); + lua_setfield(L, -2, "value"); + lua_pushstring(L, buf); + lua_setfield(L, -2, "string"); + + return 0; +} + +int lua_pushtimet(lua_State *L, const time_t *time) +{ + lua_pushinteger(L, *time); + return 0; +} + /* * Logging. * diff --git a/lib/frrlua.h b/lib/frrlua.h index 6e646e337d..a411751172 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -43,12 +43,26 @@ int lua_pushprefix(lua_State *L, const struct prefix *prefix); int lua_pushinterface(lua_State *L, const struct interface *ifp); /* - * Retrieve a string from table on the top of the stack. - * - * key - * Key of string value in table + * Pushes a new table containing both numeric and string representations of an + * in_addr to the stack. + */ +int lua_pushinaddr(lua_State *L, const struct in_addr *addr); + +/* + * Pushes a new table containing both numeric and string representations of an + * in6_addr to the stack. + */ +int lua_pushin6addr(lua_State *L, const struct in6_addr *addr); + +/* + * Pushes a time_t to the stack. + */ +int lua_pushtimet(lua_State *L, const time_t *time); + +/* + * Pushes a table representing a sockunion to the stack. */ -const char *frrlua_table_get_string(lua_State *L, const char *key); +int lua_pushsockunion(lua_State *L, const union sockunion *su); /* * Retrieve an integer from table on the top of the stack. -- 2.39.5