]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output-lua: new file for common functions
authorVictor Julien <victor@inliniac.net>
Wed, 19 Feb 2014 12:49:35 +0000 (13:49 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 15 Aug 2014 11:58:25 +0000 (13:58 +0200)
Add output-lua-common.[ch] to store functions common to various parts
of the lua output framework.

src/Makefile.am
src/output-lua-common.c [new file with mode: 0644]
src/output-lua-common.h [new file with mode: 0644]
src/output-lua-http.c

index 3aad3466a782962670b9c5894275b74ce69a898b..6cb523d15258484e4a0b0d2defb4673a95d0895e 100644 (file)
@@ -227,6 +227,7 @@ output-json-http.c output-json-http.h \
 output-json-ssh.c output-json-ssh.h \
 output-json-tls.c output-json-tls.h \
 output-lua.c output-lua.h \
+output-lua-common.c output-lua-common.h \
 output-lua-http.c output-lua-http.h \
 output-packet.c output-packet.h \
 output-streaming.c output-streaming.h \
diff --git a/src/output-lua-common.c b/src/output-lua-common.c
new file mode 100644 (file)
index 0000000..027d4ef
--- /dev/null
@@ -0,0 +1,104 @@
+/* Copyright (C) 2014 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.
+ */
+
+/**
+ * \file
+ *
+ * \author Victor Julien <victor@inliniac.net>
+ *
+ * Common function for Lua Output
+ */
+
+#include "suricata-common.h"
+#include "debug.h"
+#include "detect.h"
+#include "pkt-var.h"
+#include "conf.h"
+
+#include "threads.h"
+#include "threadvars.h"
+#include "tm-threads.h"
+
+#include "util-print.h"
+#include "util-unittest.h"
+
+#include "util-debug.h"
+
+#include "output.h"
+#include "app-layer-htp.h"
+#include "app-layer.h"
+#include "app-layer-parser.h"
+#include "util-privs.h"
+#include "util-buffer.h"
+#include "util-proto-name.h"
+#include "util-logopenfile.h"
+#include "util-time.h"
+
+#ifdef HAVE_LUA
+
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+
+extern const char lualog_ext_key_tx;
+
+void *LuaStateGetTX(lua_State *luastate)
+{
+    lua_pushlightuserdata(luastate, (void *)&lualog_ext_key_tx);
+    lua_gettable(luastate, LUA_REGISTRYINDEX);
+    void *tx = lua_touserdata(luastate, -1);
+    return tx;
+}
+
+int LuaCallbackError(lua_State *luastate, const char *msg)
+{
+    lua_pushnil(luastate);
+    lua_pushstring(luastate, msg);
+    return 2;
+}
+
+int LuaReturnStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
+{
+    /* we're using a buffer sized at a multiple of 4 as lua_pushlstring generates
+     * invalid read errors in valgrind otherwise. Adding in a nul to be sure.
+     *
+     * Buffer size = len + 1 (for nul) + whatever makes it a multiple of 4 */
+    size_t buflen = input_len + 1 + ((input_len + 1) % 4);
+    uint8_t buf[buflen];
+    memset(buf, 0x00, buflen);
+    memcpy(buf, input, input_len);
+    buf[input_len] = '\0';
+
+    /* return value through luastate, as a luastring */
+    lua_pushlstring(luastate, (char *)buf, input_len);
+    return 1;
+}
+
+const char *LuaGetStringArgument(lua_State *luastate, int argc)
+{
+    /* get argument */
+    if (!lua_isstring(luastate, argc))
+        return NULL;
+    const char *str = lua_tostring(luastate, argc);
+    if (str == NULL)
+        return NULL;
+    if (strlen(str) == 0)
+        return NULL;
+    return str;
+}
+
+#endif /* HAVE_LUA */
diff --git a/src/output-lua-common.h b/src/output-lua-common.h
new file mode 100644 (file)
index 0000000..eeefbaa
--- /dev/null
@@ -0,0 +1,38 @@
+/* Copyright (C) 2014 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.
+ */
+
+/**
+ * \file
+ *
+ * \author Victor Julien <victor@inliniac.net>
+ */
+
+#ifndef __OUTPUT_LUA_COMMON_H__
+#define __OUTPUT_LUA_COMMON_H__
+
+#ifdef HAVE_LUA
+
+void LuaPrintStack(lua_State *state);
+
+void *LuaStateGetTX(lua_State *luastate);
+int LuaCallbackError(lua_State *luastate, const char *msg);
+int LuaReturnStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len);
+const char *LuaGetStringArgument(lua_State *luastate, int argc);
+
+#endif /* HAVE_LUA */
+
+#endif /* __OUTPUT_LUA_COMMON_H__ */
index 09564d374445a6bd551dfbafe7ca1b168c5315bc..d2bc2aaeb3e2e59faf3f894c5ab6237e69745d4e 100644 (file)
 #include <lualib.h>
 #include <lauxlib.h>
 
-extern const char lualog_ext_key_tx;
-
-static void *LuaStateGetTX(lua_State *luastate)
-{
-    lua_pushlightuserdata(luastate, (void *)&lualog_ext_key_tx);
-    lua_gettable(luastate, LUA_REGISTRYINDEX);
-    void *tx = lua_touserdata(luastate, -1);
-    return tx;
-}
-
-static int LuaCallbackError(lua_State *luastate, const char *msg)
-{
-    lua_pushnil(luastate);
-    lua_pushstring(luastate, msg);
-    return 2;
-}
-
-static int LuaReturnStringBuffer(lua_State *luastate, uint8_t *input, size_t input_len)
-{
-    /* we're using a buffer sized at a multiple of 4 as lua_pushlstring generates
-     * invalid read errors in valgrind otherwise. Adding in a nul to be sure.
-     *
-     * Buffer size = len + 1 (for nul) + whatever makes it a multiple of 4 */
-    size_t buflen = input_len + 1 + ((input_len + 1) % 4);
-    uint8_t buf[buflen];
-    memset(buf, 0x00, buflen);
-    memcpy(buf, input, input_len);
-    buf[input_len] = '\0';
-
-    /* return value through luastate, as a luastring */
-    lua_pushlstring(luastate, (char *)buf, input_len);
-    return 1;
-}
-
-const char *LuaGetStringArgument(lua_State *luastate, int argc)
-{
-    /* get argument */
-    if (!lua_isstring(luastate, argc))
-        return NULL;
-    const char *str = lua_tostring(luastate, 1);
-    if (str == NULL)
-        return NULL;
-    if (strlen(str) == 0)
-        return NULL;
-    return str;
-}
+#include "output-lua-common.h"
 
 static int HttpGetRequestUriRaw(lua_State *luastate)
 {