--- /dev/null
+Util
+####
+
+The ``suricata.util`` library provides utility functions for Lua
+scripts.
+
+Setup
+*****
+
+The library must be loaded prior to use::
+
+ local util = require("suricata.util")
+
+Functions
+=========
+
+.. function:: thread_info()
+
+ Get information about the current thread.
+
+ :returns: Table containing thread information with the following fields:
+
+ - ``id`` (number): Thread ID
+ - ``name`` (string): Thread name
+ - ``group_name`` (string): Thread group name
+
+ Example::
+
+ local util = require("suricata.util")
+
+ local info = util.thread_info()
+ print("Thread ID: " .. info.id)
+ print("Thread Name: " .. info.name)
+ print("Thread Group: " .. info.group_name)
~~~~~~~~~~~~~
Decrement Flowint at index given by the first parameter.
-
-Misc
-----
-
-SCThreadInfo
-~~~~~~~~~~~~
-
-::
-
- tid, tname, tgroup = SCThreadInfo()
-
-It gives: tid (integer), tname (string), tgroup (string)
util-lua-smtp.h \
util-lua-ssh.h \
util-lua-tls.h \
+ util-lua-util.h \
util-lua.h \
util-macset.h \
util-magic.h \
util-lua-smtp.c \
util-lua-ssh.c \
util-lua-tls.c \
+ util-lua-util.c \
util-lua.c \
util-macset.c \
util-magic.c \
LuaStateSetDirection(lua_state, (flags & STREAM_TOSERVER));
}
-
-/**
- * \brief Register Suricata Lua functions
- */
-int LuaRegisterExtensions(lua_State *lua_state)
-{
- LuaRegisterFunctions(lua_state);
- return 0;
-}
extern const char luaext_key_ld[];
-int LuaRegisterExtensions(lua_State *);
-
void LuaExtensionsMatchSetup(lua_State *lua_state, DetectLuaData *, DetectEngineThreadCtx *det_ctx,
Flow *f, Packet *p, const Signature *s, uint8_t flags);
SCLuaSbLoadLibs(t->luastate);
}
- LuaRegisterExtensions(t->luastate);
LuaStateSetDetectLuaData(t->luastate, lua);
/* hackish, needed to allow unittests to pass buffers as scripts instead of files */
SCLuaSbLoadLibs(t->luastate);
}
- LuaRegisterExtensions(t->luastate);
-
int status = luaL_loadfile(t->luastate, lua->filename);
if (status) {
SCLogError("couldn't load file: %s", lua_tostring(t->luastate, -1));
lua_getglobal(luastate, "setup");
- /* register functions common to all */
- LuaRegisterFunctions(luastate);
-
if (lua_pcall(luastate, 0, 0, 0) != 0) {
SCLogError("couldn't run script 'setup' function: %s", lua_tostring(luastate, -1));
goto error;
#include "util-lua-ja3.h"
#include "util-lua-filelib.h"
#include "util-lua-log.h"
+#include "util-lua-util.h"
#include "lauxlib.h"
{ "suricata.smtp", SCLuaLoadSmtpLib },
{ "suricata.ssh", SCLuaLoadSshLib },
{ "suricata.tls", SCLuaLoadTlsLib },
+ { "suricata.util", SCLuaLoadUtilLib },
{ NULL, NULL },
};
lua_settable(luastate, -3);
}
-/** \internal
- * \brief fill lua stack with thread info
- * \param luastate the lua state
- * \param pa pointer to packet alert struct
- * \retval cnt number of data items placed on the stack
- *
- * Places: thread id (number), thread name (string, thread group name (string)
- */
-static int LuaCallbackThreadInfoPushToStackFromThreadVars(lua_State *luastate, const ThreadVars *tv)
-{
- unsigned long tid = SCGetThreadIdLong();
- lua_pushinteger (luastate, (lua_Integer)tid);
- lua_pushstring (luastate, tv->name);
- lua_pushstring (luastate, tv->thread_group_name);
- return 3;
-}
-
-/** \internal
- * \brief Wrapper for getting tuple info into a lua script
- * \retval cnt number of items placed on the stack
- */
-static int LuaCallbackThreadInfo(lua_State *luastate)
-{
- const ThreadVars *tv = LuaStateGetThreadVars(luastate);
- if (tv == NULL)
- return LuaCallbackError(luastate, "internal error: no tv");
-
- return LuaCallbackThreadInfoPushToStackFromThreadVars(luastate, tv);
-}
-
-int LuaRegisterFunctions(lua_State *luastate)
-{
- lua_pushcfunction(luastate, LuaCallbackThreadInfo);
- lua_setglobal(luastate, "SCThreadInfo");
- return 0;
-}
-
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
{
AppProto flow_alproto = 0;
lua_State *luastate, const char *key, const char *value, size_t len);
void LuaPushTableKeyValueArray(lua_State *luastate, const char *key, const uint8_t *value, size_t len);
-int LuaRegisterFunctions(lua_State *luastate);
-
int LuaStateNeedProto(lua_State *luastate, AppProto alproto);
/* hack to please scan-build. Even though LuaCallbackError *always*
--- /dev/null
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "suricata-common.h"
+#include "threads.h"
+#include "threadvars.h"
+
+#include "util-lua.h"
+#include "util-lua-common.h"
+#include "util-lua-util.h"
+
+#include "lua.h"
+#include "lauxlib.h"
+
+/**
+ * \brief Get thread information and return as a table
+ * \retval 1 table with thread info fields: id, name, thread_group_name
+ */
+static int LuaUtilThreadInfo(lua_State *luastate)
+{
+ const ThreadVars *tv = LuaStateGetThreadVars(luastate);
+ if (tv == NULL)
+ return LuaCallbackError(luastate, "internal error: no tv");
+
+ unsigned long tid = SCGetThreadIdLong();
+
+ lua_newtable(luastate);
+
+ lua_pushstring(luastate, "id");
+ lua_pushinteger(luastate, (lua_Integer)tid);
+ lua_settable(luastate, -3);
+
+ lua_pushstring(luastate, "name");
+ lua_pushstring(luastate, tv->name);
+ lua_settable(luastate, -3);
+
+ lua_pushstring(luastate, "group_name");
+ lua_pushstring(luastate, tv->thread_group_name);
+ lua_settable(luastate, -3);
+
+ return 1;
+}
+
+static const struct luaL_Reg utillib[] = {
+ { "thread_info", LuaUtilThreadInfo },
+ { NULL, NULL },
+};
+
+int SCLuaLoadUtilLib(lua_State *L)
+{
+ luaL_newlib(L, utillib);
+ return 1;
+}
--- /dev/null
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef SURICATA_UTIL_LUA_UTIL_H
+#define SURICATA_UTIL_LUA_UTIL_H
+
+#include "lua.h"
+
+int SCLuaLoadUtilLib(lua_State *L);
+
+#endif /* SURICATA_UTIL_LUA_UTIL_H */
\ No newline at end of file