From 19383fd428b5c226901556096892386a4bcd1410 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Fri, 21 Mar 2014 14:25:04 +0100 Subject: [PATCH] output-lua: alproto string callback 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 | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/output-lua-common.c b/src/output-lua-common.c index 6880d402f2..bb8b3065b3 100644 --- a/src/output-lua-common.c +++ b/src/output-lua-common.c @@ -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");