]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output-lua: expose flow start time string
authorVictor Julien <victor@inliniac.net>
Wed, 19 Mar 2014 17:38:29 +0000 (18:38 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 15 Aug 2014 11:58:26 +0000 (13:58 +0200)
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

index 12fc3372257e1d3dc006087a20d92757050bc22c..272621c8217ccc02991e14ab2ab090fa222e1f19 100644 (file)
@@ -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");