diff --git a/src/output-lua.c b/src/output-lua.c index d774f95632..918a6a48ed 100644 --- a/src/output-lua.c +++ b/src/output-lua.c @@ -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"); diff --git a/src/util-lua.c b/src/util-lua.c index 0e08dca7c3..5035ae6123 100644 --- a/src/util-lua.c +++ b/src/util-lua.c @@ -58,6 +58,10 @@ 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); diff --git a/src/util-lua.h b/src/util-lua.h index 831dda6ca9..4bf1bd3734 100644 --- a/src/util-lua.h +++ b/src/util-lua.h @@ -26,12 +26,33 @@ #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 */