output-lua: new file for common functions

Add output-lua-common.[ch] to store functions common to various parts
of the lua output framework.
pull/1112/head
Victor Julien 12 years ago
parent db30ed8c3e
commit 0bd4b9beca

@ -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 \

@ -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 */

@ -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__ */

@ -53,52 +53,7 @@
#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)
{

Loading…
Cancel
Save