From: Victor Julien Date: Wed, 19 Feb 2014 12:49:35 +0000 (+0100) Subject: output-lua: new file for common functions X-Git-Tag: suricata-2.1beta2~160 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0bd4b9beca02fc9c29270953bd84a2aa63fe22aa;p=thirdparty%2Fsuricata.git output-lua: new file for common functions Add output-lua-common.[ch] to store functions common to various parts of the lua output framework. --- diff --git a/src/Makefile.am b/src/Makefile.am index 3aad3466a7..6cb523d152 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 0000000000..027d4ef802 --- /dev/null +++ b/src/output-lua-common.c @@ -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 + * + * 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 +#include +#include + +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 index 0000000000..eeefbaa170 --- /dev/null +++ b/src/output-lua-common.h @@ -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 + */ + +#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__ */ diff --git a/src/output-lua-http.c b/src/output-lua-http.c index 09564d3744..d2bc2aaeb3 100644 --- a/src/output-lua-http.c +++ b/src/output-lua-http.c @@ -53,52 +53,7 @@ #include #include -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) {