suricata
util-lua-dataset.c
Go to the documentation of this file.
1 /* Copyright (C) 2025 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  * Dataset API for Lua.
22  *
23  * local dataset = require("suricata.dataset")
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "util-lua-dataset.h"
29 
30 #include "app-layer-protos.h" /* Required by util-lua-common. */
31 #include "util-lua-common.h"
32 #include "util-lua.h"
33 #include "util-debug.h"
34 
35 #include "datasets.h"
36 
37 struct LuaDataset {
39 };
40 
41 static int LuaDatasetGC(lua_State *luastate)
42 {
43  SCLogDebug("gc:start");
44  struct LuaDataset *s = (struct LuaDataset *)lua_touserdata(luastate, 1);
45  SCLogDebug("deref %s", s->set->name);
46  s->set = NULL;
47  SCLogDebug("gc:done");
48  return 0;
49 }
50 
51 static int LuaDatasetGetRef(lua_State *luastate)
52 {
53  SCLogDebug("get");
54  struct LuaDataset *s = (struct LuaDataset *)lua_touserdata(luastate, 1);
55  if (s == NULL) {
56  LUA_ERROR("dataset is not initialized");
57  }
58 
59  const char *name = lua_tostring(luastate, 2);
60  if (name == NULL) {
61  LUA_ERROR("null string");
62  }
63 
65  if (dataset == NULL) {
66  LUA_ERROR("dataset not found");
67  }
68  s->set = dataset;
69  return 0;
70 }
71 
72 static int LuaDatasetAdd(lua_State *luastate)
73 {
74  SCLogDebug("add:start");
75  struct LuaDataset *s = (struct LuaDataset *)lua_touserdata(luastate, 1);
76  if (s == NULL) {
77  LUA_ERROR("dataset is not initialized");
78  }
79  if (!lua_isstring(luastate, 2)) {
80  LUA_ERROR("1st arg is not a string");
81  }
82  if (!lua_isnumber(luastate, 3)) {
83  LUA_ERROR("2nd arg is not a number");
84  }
85 
86  const uint8_t *str = (const uint8_t *)lua_tostring(luastate, 2);
87  if (str == NULL) {
88  LUA_ERROR("1st arg is not null string");
89  }
90 
91  uint32_t str_len = lua_tonumber(luastate, 3);
92 
93  int r = DatasetAdd(s->set, (const uint8_t *)str, str_len);
94  /* return value through luastate, as a luanumber */
95  lua_pushnumber(luastate, (lua_Number)r);
96  SCLogDebug("add:end");
97  return 1;
98 }
99 
100 static int LuaDatasetNew(lua_State *luastate)
101 {
102  SCLogDebug("new:start");
103  struct LuaDataset *s = (struct LuaDataset *)lua_newuserdata(luastate, sizeof(*s));
104  if (s == NULL) {
105  LUA_ERROR("failed to get userdata");
106  }
107  luaL_getmetatable(luastate, "dataset::metatable");
108  lua_setmetatable(luastate, -2);
109  SCLogDebug("new:done");
110  return 1;
111 }
112 
113 // clang-format off
114 static const luaL_Reg datasetlib[] = {
115  { "new", LuaDatasetNew },
116  { "get", LuaDatasetGetRef },
117  { "add", LuaDatasetAdd },
118  { "__gc", LuaDatasetGC },
119  { NULL, NULL }
120 };
121 // clang-format on
122 
124 {
125  luaL_newmetatable(luastate, "dataset::metatable");
126  lua_pushvalue(luastate, -1);
127  lua_setfield(luastate, -2, "__index");
128  luaL_setfuncs(luastate, datasetlib, 0);
129  luaL_newlib(luastate, datasetlib);
130 }
LUA_ERROR
#define LUA_ERROR(msg)
Definition: util-lua-common.h:41
util-lua-common.h
Dataset::name
char name[DATASET_NAME_MAX_LEN+1]
Definition: datasets.h:41
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
util-lua.h
LuaLoadDatasetLib
void LuaLoadDatasetLib(lua_State *luastate)
Definition: util-lua-dataset.c:123
LuaDataset::set
Dataset * set
Definition: util-lua-dataset.c:38
util-lua-dataset.h
lua_State
struct lua_State lua_State
Definition: suricata-common.h:506
datasets.h
util-debug.h
DatasetAdd
int DatasetAdd(Dataset *set, const uint8_t *data, const uint32_t data_len)
Definition: datasets.c:1555
name
const char * name
Definition: tm-threads.c:2081
LuaDataset
Definition: util-lua-dataset.c:37
suricata-common.h
DatasetFind
Dataset * DatasetFind(const char *name, enum DatasetTypes type)
look for set by name without creating it
Definition: datasets.c:616
str
#define str(s)
Definition: suricata-common.h:291
DATASET_TYPE_STRING
@ DATASET_TYPE_STRING
Definition: datasets.h:32
app-layer-protos.h
Dataset
Definition: datasets.h:40