suricata
util-lua-common.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2021 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Common function for Lua Output
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "threads.h"
29 #include "threadvars.h"
30 
31 #include "output.h"
32 #include "util-conf.h"
33 
34 #include "lua.h"
35 
36 #include "util-lua.h"
37 #include "util-lua-common.h"
38 
39 int LuaCallbackError(lua_State *luastate, const char *msg)
40 {
41  lua_pushnil(luastate);
42  lua_pushstring(luastate, msg);
43  return 2;
44 }
45 
46 const char *LuaGetStringArgument(lua_State *luastate, int idx)
47 {
48  /* get argument */
49  if (!lua_isstring(luastate, idx))
50  return NULL;
51  const char *str = lua_tostring(luastate, idx);
52  if (str == NULL)
53  return NULL;
54  if (strlen(str) == 0)
55  return NULL;
56  return str;
57 }
58 
59 void LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value)
60 {
61  lua_pushstring(luastate, key);
62  lua_pushnumber(luastate, value);
63  lua_settable(luastate, -3);
64 }
65 
66 void LuaPushTableKeyValueBoolean(lua_State *luastate, const char *key, bool value)
67 {
68  lua_pushstring(luastate, key);
69  lua_pushboolean(luastate, value);
70  lua_settable(luastate, -3);
71 }
72 
73 /** \brief Push a key plus string value to the stack
74  *
75  * If value is NULL, string "(null")" will be put on the stack.
76  */
77 void LuaPushTableKeyValueString(lua_State *luastate, const char *key, const char *value)
78 {
79  lua_pushstring(luastate, key);
80  lua_pushstring(luastate, value ? value : "(null)");
81  lua_settable(luastate, -3);
82 }
83 
84 /** \brief Push a key plus string value with length to the stack.
85  */
87  lua_State *luastate, const char *key, const char *value, size_t len)
88 {
89  lua_pushstring(luastate, key);
90  lua_pushlstring(luastate, value, len);
91  lua_settable(luastate, -3);
92 }
93 
95  lua_State *luastate, const char *key, const uint8_t *value, size_t len)
96 {
97  lua_pushstring(luastate, key);
98  LuaPushStringBuffer(luastate, value, len);
99  lua_settable(luastate, -3);
100 }
101 
102 int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
103 {
104  AppProto flow_alproto = 0;
105  Flow *flow = LuaStateGetFlow(luastate);
106  if (flow == NULL)
107  return LuaCallbackError(luastate, "internal error: no flow");
108 
109  flow_alproto = flow->alproto;
110 
111  return (alproto == flow_alproto);
112 }
len
uint8_t len
Definition: app-layer-dnp3.h:2
util-lua-common.h
LuaPushTableKeyValueInt
void LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value)
Definition: util-lua-common.c:59
util-lua.h
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:86
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:39
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
LuaGetStringArgument
const char * LuaGetStringArgument(lua_State *luastate, int idx)
Definition: util-lua-common.c:46
lua_State
struct lua_State lua_State
Definition: suricata-common.h:523
LuaPushTableKeyValueArray
void LuaPushTableKeyValueArray(lua_State *luastate, const char *key, const uint8_t *value, size_t len)
Definition: util-lua-common.c:94
util-conf.h
suricata-common.h
threadvars.h
str
#define str(s)
Definition: suricata-common.h:308
LuaStateGetFlow
Flow * LuaStateGetFlow(lua_State *luastate)
get flow pointer from lua state
Definition: util-lua.c:161
LuaPushTableKeyValueBoolean
void LuaPushTableKeyValueBoolean(lua_State *luastate, const char *key, bool value)
Definition: util-lua-common.c:66
LuaPushTableKeyValueLString
void LuaPushTableKeyValueLString(lua_State *luastate, const char *key, const char *value, size_t len)
Push a key plus string value with length to the stack.
Definition: util-lua-common.c:86
LuaPushTableKeyValueString
void LuaPushTableKeyValueString(lua_State *luastate, const char *key, const char *value)
Push a key plus string value to the stack.
Definition: util-lua-common.c:77
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:450
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:102
output.h
LuaPushStringBuffer
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
Definition: util-lua.c:319