diff --git a/src/util-lua-common.c b/src/util-lua-common.c index 3c9e7718d9..74116fec3e 100644 --- a/src/util-lua-common.c +++ b/src/util-lua-common.c @@ -158,6 +158,21 @@ static int LuaCallbackPacketPayload(lua_State *luastate) return LuaCallbackPacketPayloadPushToStackFromPacket(luastate, p); } +/** \internal + * \brief fill lua stack with packet timestamp + * \param luastate the lua state + * \param p packet + * \retval cnt number of data items placed on the stack + * + * Places: seconds (number), microseconds (number) + */ +static int LuaCallbackTimestampPushToStack(lua_State *luastate, const struct timeval *ts) +{ + lua_pushnumber(luastate, (double)ts->tv_sec); + lua_pushnumber(luastate, (double)ts->tv_usec); + return 2; +} + /** \internal * \brief fill lua stack with header info * \param luastate the lua state @@ -174,6 +189,19 @@ static int LuaCallbackTimeStringPushToStackFromPacket(lua_State *luastate, const return 1; } +/** \internal + * \brief Wrapper for getting packet timestamp (as numbers) into a lua script + * \retval cnt number of items placed on the stack + */ +static int LuaCallbackPacketTimestamp(lua_State *luastate) +{ + const Packet *p = LuaStateGetPacket(luastate); + if (p == NULL) + return LuaCallbackError(luastate, "internal error: no packet"); + + return LuaCallbackTimestampPushToStack(luastate, &p->ts); +} + /** \internal * \brief Wrapper for getting tuple info into a lua script * \retval cnt number of items placed on the stack @@ -719,6 +747,8 @@ int LuaRegisterFunctions(lua_State *luastate) /* registration of the callbacks */ lua_pushcfunction(luastate, LuaCallbackPacketPayload); lua_setglobal(luastate, "SCPacketPayload"); + lua_pushcfunction(luastate, LuaCallbackPacketTimestamp); + lua_setglobal(luastate, "SCPacketTimestamp"); lua_pushcfunction(luastate, LuaCallbackPacketTimeString); lua_setglobal(luastate, "SCPacketTimeString"); lua_pushcfunction(luastate, LuaCallbackTuple);