]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output-lua: add SCPacketTimeString
authorVictor Julien <victor@inliniac.net>
Wed, 19 Mar 2014 11:36:01 +0000 (12:36 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 15 Aug 2014 11:58:26 +0000 (13:58 +0200)
Add SCPacketTimeString to get the packets time string in the format:
    11/24/2009-18:57:25.179869

Example use:

    function log(args)
        ts = SCPacketTimeString()

lua/fast.lua
src/output-lua-common.c
src/output-lua.c

index f31616dcc93fe00e66263b17230b920684e849e0..731a32698eda0cc4bf8136eb5ef1d53c0ae4bf00 100644 (file)
@@ -12,6 +12,7 @@ function setup (args)
 end
 
 function log(args)
+    ts = SCPacketTimeString()
     sid, rev, gid = SCRuleIds()
     ipver, srcip, dstip, proto, sp, dp = SCPacketTuple()
     msg = SCRuleMsg()
@@ -19,7 +20,6 @@ function log(args)
     if class == nil then
         class = "unknown"
     end
-    ts = args['ts'];
 
     print (ts .. "  [**] [" .. gid .. ":" .. sid .. ":" .. rev .. "] " ..
            msg .. " [**] [Classification: " .. class .. "] [Priority: " ..
index c6e27f612050ccca1015361e1279b94ebe609e5f..0770705c6c281634ebbafcb5d04ae484681e20b5 100644 (file)
@@ -118,6 +118,35 @@ void LogLuaPushTableKeyValueArray(lua_State *luastate, const char *key, const ui
     lua_settable(luastate, -3);
 }
 
+/** \internal
+ *  \brief fill lua stack with header info
+ *  \param luastate the lua state
+ *  \param p packet
+ *  \retval cnt number of data items placed on the stack
+ *
+ *  Places: ts (string)
+ */
+static int LuaCallbackTimeStringPushToStackFromPacket(lua_State *luastate, const Packet *p)
+{
+    char timebuf[64];
+    CreateTimeString(&p->ts, timebuf, sizeof(timebuf));
+    lua_pushstring (luastate, timebuf);
+    return 1;
+}
+
+/** \internal
+ *  \brief Wrapper for getting tuple info into a lua script
+ *  \retval cnt number of items placed on the stack
+ */
+static int LuaCallbackPacketTimeString(lua_State *luastate)
+{
+    const Packet *p = LuaStateGetPacket(luastate);
+    if (p == NULL)
+        return LuaCallbackError(luastate, "internal error: no packet");
+
+    return LuaCallbackTimeStringPushToStackFromPacket(luastate, p);
+}
+
 /** \internal
  *  \brief fill lua stack with header info
  *  \param luastate the lua state
@@ -394,8 +423,11 @@ static int LuaCallbackLogError(lua_State *luastate)
 int LogLuaRegisterFunctions(lua_State *luastate)
 {
     /* registration of the callbacks */
+    lua_pushcfunction(luastate, LuaCallbackPacketTimeString);
+    lua_setglobal(luastate, "SCPacketTimeString");
     lua_pushcfunction(luastate, LuaCallbackTuple);
     lua_setglobal(luastate, "SCPacketTuple");
+
     lua_pushcfunction(luastate, LuaCallbackTupleFlow);
     lua_setglobal(luastate, "SCFlowTuple");
     lua_pushcfunction(luastate, LuaCallbackLogPath);
index e8580b22d753087abdda4d9dfeadb2dab6fad620..0d55b130414331031b820782f0e4c275c5d135d0 100644 (file)
@@ -149,11 +149,9 @@ static int LuaPacketLoggerAlerts(ThreadVars *tv, void *thread_data, const Packet
         LuaStateSetPacketAlert(td->lua_ctx->luastate, (PacketAlert *)pa);
 
         /* prepare data to pass to script */
-        lua_newtable(td->lua_ctx->luastate);
+        //lua_newtable(td->lua_ctx->luastate);
 
-        LogLuaPushTableKeyValueString(td->lua_ctx->luastate, "ts", timebuf);
-
-        int retval = lua_pcall(td->lua_ctx->luastate, 1, 0, 0);
+        int retval = lua_pcall(td->lua_ctx->luastate, 0, 0, 0);
         if (retval != 0) {
             SCLogInfo("failed to run script: %s", lua_tostring(td->lua_ctx->luastate, -1));
         }