From 8802ba3f67ea175a1b15d7e16aa4dea9d698f93e Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 19 Mar 2014 18:38:29 +0100 Subject: [PATCH] output-lua: expose flow start time string SCFlowTimeString: returns string form of start time of a flow Example: function log(args) startts = SCFlowTimeString() ts = SCPacketTimeString() if ts == startts then print("new flow") end --- src/output-lua-common.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/output-lua-common.c b/src/output-lua-common.c index 12fc337225..272621c821 100644 --- a/src/output-lua-common.c +++ b/src/output-lua-common.c @@ -147,6 +147,44 @@ static int LuaCallbackPacketTimeString(lua_State *luastate) return LuaCallbackTimeStringPushToStackFromPacket(luastate, p); } +/** \internal + * \brief fill lua stack with time string + * \param luastate the lua state + * \param flow flow + * \retval cnt number of data items placed on the stack + * + * Places: ts (string) + */ +static int LuaCallbackTimeStringPushToStackFromFlow(lua_State *luastate, const Flow *flow) +{ + char timebuf[64]; + CreateTimeString(&flow->startts, timebuf, sizeof(timebuf)); + lua_pushstring (luastate, timebuf); + return 1; +} + +/** \internal + * \brief Wrapper for getting ts info into a lua script + * \retval cnt number of items placed on the stack + */ +static int LuaCallbackFlowTimeString(lua_State *luastate) +{ + int r = 0; + int flow_is_locked = 0; + Flow *flow = LuaStateGetFlow(luastate, &flow_is_locked); + if (flow == NULL) + return LuaCallbackError(luastate, "internal error: no flow"); + + if (!flow_is_locked) { + FLOWLOCK_RDLOCK(flow); + r = LuaCallbackTimeStringPushToStackFromFlow(luastate, flow); + FLOWLOCK_UNLOCK(flow); + } else { + r = LuaCallbackTimeStringPushToStackFromFlow(luastate, flow); + } + return r; +} + /** \internal * \brief fill lua stack with header info * \param luastate the lua state @@ -517,8 +555,11 @@ int LogLuaRegisterFunctions(lua_State *luastate) lua_pushcfunction(luastate, LuaCallbackTuple); lua_setglobal(luastate, "SCPacketTuple"); + lua_pushcfunction(luastate, LuaCallbackFlowTimeString); + lua_setglobal(luastate, "SCFlowTimeString"); lua_pushcfunction(luastate, LuaCallbackTupleFlow); lua_setglobal(luastate, "SCFlowTuple"); + lua_pushcfunction(luastate, LuaCallbackLogPath); lua_setglobal(luastate, "SCLogPath");