]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
lua: add flow store and retrieval wrappers
authorVictor Julien <victor@inliniac.net>
Mon, 3 Mar 2014 13:09:18 +0000 (14:09 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 15 Aug 2014 11:58:26 +0000 (13:58 +0200)
Add flow store and retrieval wrappers for accessing the flow through
Lua's lightuserdata method.

The flow functions store/retrieve a lock hint as well.

src/output-lua.c
src/util-lua.c
src/util-lua.h

index d774f95632a41d8cdbf3e6da45196c2325aa60a7..918a6a48ed64eb565e17855cc7b7cc8cde662b38 100644 (file)
@@ -87,6 +87,7 @@ static int LuaTxLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow
 
     LuaStateSetPacket(td->lua_ctx->luastate, (Packet *)p);
     LuaStateSetTX(td->lua_ctx->luastate, txptr);
+    LuaStateSetFlow(td->lua_ctx->luastate, f, /* locked */FALSE);
 
     /* prepare data to pass to script */
     lua_getglobal(td->lua_ctx->luastate, "log");
@@ -151,6 +152,7 @@ static int LuaPacketLoggerAlerts(ThreadVars *tv, void *thread_data, const Packet
         lua_getglobal(td->lua_ctx->luastate, "log");
 
         LuaStateSetPacket(td->lua_ctx->luastate, (Packet *)p);
+        LuaStateSetFlow(td->lua_ctx->luastate, p->flow, /* unlocked */TRUE);
 
         /* prepare data to pass to script */
         lua_newtable(td->lua_ctx->luastate);
@@ -224,6 +226,7 @@ static int LuaPacketLogger(ThreadVars *tv, void *thread_data, const Packet *p)
     lua_getglobal(td->lua_ctx->luastate, "log");
 
     LuaStateSetPacket(td->lua_ctx->luastate, (Packet *)p);
+    LuaStateSetFlow(td->lua_ctx->luastate, p->flow, /* unlocked */TRUE);
 
     /* prepare data to pass to script */
     lua_newtable(td->lua_ctx->luastate);
@@ -273,6 +276,7 @@ static int LuaFileLogger(ThreadVars *tv, void *thread_data, const Packet *p, con
 
     LuaStateSetPacket(td->lua_ctx->luastate, (Packet *)p);
     LuaStateSetTX(td->lua_ctx->luastate, txptr);
+    LuaStateSetFlow(td->lua_ctx->luastate, p->flow, /* locked */FALSE);
 
     /* get the lua function to call */
     lua_getglobal(td->lua_ctx->luastate, "log");
index 0e08dca7c3f17d3496aabbfd2318b3b5efb1dc92..5035ae61238b4cf1a42f354ce56510f3617a966e 100644 (file)
 const char lua_ext_key_tx[] = "suricata:lua:tx:ptr";
 /* key for p (packet) pointer */
 const char lua_ext_key_p[] = "suricata:lua:pkt:ptr";
+/* key for f (flow) pointer */
+const char lua_ext_key_flow[] = "suricata:lua:flow:ptr";
+/* key for flow lock hint bool */
+const char lua_ext_key_flow_lock_hint[] = "suricata:lua:flow:lock_hint";
 
 /** \brief get packet pointer from the lua state */
 Packet *LuaStateGetPacket(lua_State *luastate)
@@ -91,6 +95,37 @@ void LuaStateSetTX(lua_State *luastate, void *txptr)
     lua_settable(luastate, LUA_REGISTRYINDEX);
 }
 
+Flow *LuaStateGetFlow(lua_State *luastate, int *lock_hint)
+{
+    Flow *f = NULL;
+    int need_flow_lock = 0;
+
+    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow);
+    lua_gettable(luastate, LUA_REGISTRYINDEX);
+    f = lua_touserdata(luastate, -1);
+
+    /* need flow lock hint */
+    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow_lock_hint);
+    lua_gettable(luastate, LUA_REGISTRYINDEX);
+    need_flow_lock = lua_toboolean(luastate, -1);
+
+    *lock_hint = need_flow_lock;
+    return f;
+}
+
+void LuaStateSetFlow(lua_State *luastate, Flow *f, int need_flow_lock)
+{
+    /* flow */
+    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow);
+    lua_pushlightuserdata(luastate, (void *)f);
+    lua_settable(luastate, LUA_REGISTRYINDEX);
+
+    /* flow lock status hint */
+    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow_lock_hint);
+    lua_pushboolean(luastate, need_flow_lock);
+    lua_settable(luastate, LUA_REGISTRYINDEX);
+}
+
 /** \brief dump stack from lua state to screen */
 void LuaPrintStack(lua_State *state) {
     int size = lua_gettop(state);
index 831dda6ca9741fb7e4047d868ecd86b1c59aeff1..4bf1bd3734a5495fc3089e7dd35d2dd556922961 100644 (file)
 
 #ifdef HAVE_LUA
 
+/* gets */
+
 Packet *LuaStateGetPacket(lua_State *luastate);
 void *LuaStateGetTX(lua_State *luastate);
 
+/** \brief get flow pointer from lua state
+ *
+ *  \param lock_hint[out] pointer to bool indicating if flow is
+ *                        locked (TRUE) or unlocked unlocked (FALSE)
+ *
+ *  \retval f flow poiner or NULL if it was not set
+ */
+Flow *LuaStateGetFlow(lua_State *luastate, int *lock_hint);
+
+/* sets */
+
 void LuaStateSetPacket(lua_State *luastate, Packet *p);
 void LuaStateSetTX(lua_State *luastate, void *tx);
 
+/** \brief set a flow pointer in the lua state
+ *
+ *  \param f flow pointer
+ *  \param need_flow_lock bool indicating if flow is locked (TRUE)
+ *                        or unlocked unlocked (FALSE)
+ */
+void LuaStateSetFlow(lua_State *luastate, Flow *f, int need_flow_lock);
+
 void LuaPrintStack(lua_State *state);
 
 #endif /* HAVE_LUA */