]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output-lua: alproto string callback
authorVictor Julien <victor@inliniac.net>
Fri, 21 Mar 2014 13:25:04 +0000 (14:25 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 15 Aug 2014 11:58:26 +0000 (13:58 +0200)
SCFlowAppLayerProto: get alproto as string from the flow. If alproto
is not (yet) known, it returns "unknown".

    function log(args)
        alproto = SCFlowAppLayerProto()
        if alproto ~= nil then
            print (alproto)
        end
    end

src/output-lua-common.c

index 6880d402f20edee4684579b7e1df3d5f9c2f9cd9..bb8b3065b3b24c1b4fe11d8ff7b3f55742ec583a 100644 (file)
@@ -320,6 +320,45 @@ static int LuaCallbackTupleFlow(lua_State *luastate)
     return r;
 }
 
+/** \internal
+ *  \brief fill lua stack with AppLayerProto
+ *  \param luastate the lua state
+ *  \param f flow, locked
+ *  \retval cnt number of data items placed on the stack
+ *
+ *  Places: alproto as string (string)
+ */
+static int LuaCallbackAppLayerProtoPushToStackFromFlow(lua_State *luastate, const Flow *f)
+{
+    const char *string = AppProtoToString(f->alproto);
+    if (string == NULL)
+        string = "unknown";
+    lua_pushstring(luastate, string);
+    return 1;
+}
+
+/** \internal
+ *  \brief Wrapper for getting AppLayerProto info into a lua script
+ *  \retval cnt number of items placed on the stack
+ */
+static int LuaCallbackAppLayerProtoFlow(lua_State *luastate)
+{
+    int r = 0;
+    int lock_hint = 0;
+    Flow *f = LuaStateGetFlow(luastate, &lock_hint);
+    if (f == NULL)
+        return LuaCallbackError(luastate, "internal error: no flow");
+
+    if (lock_hint) {
+        FLOWLOCK_RDLOCK(f);
+        r = LuaCallbackAppLayerProtoPushToStackFromFlow(luastate, f);
+        FLOWLOCK_UNLOCK(f);
+    } else {
+        r = LuaCallbackAppLayerProtoPushToStackFromFlow(luastate, f);
+    }
+    return r;
+}
+
 /** \internal
  *  \brief fill lua stack with alert info
  *  \param luastate the lua state
@@ -589,6 +628,8 @@ int LogLuaRegisterFunctions(lua_State *luastate)
     lua_setglobal(luastate, "SCFlowTimeString");
     lua_pushcfunction(luastate, LuaCallbackTupleFlow);
     lua_setglobal(luastate, "SCFlowTuple");
+    lua_pushcfunction(luastate, LuaCallbackAppLayerProtoFlow);
+    lua_setglobal(luastate, "SCFlowAppLayerProto");
 
     lua_pushcfunction(luastate, LuaCallbackLogPath);
     lua_setglobal(luastate, "SCLogPath");