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 *)luaL_testudata(luastate, 1, "dataset::metatable");
45  if (s != NULL && s->set != NULL) {
46  SCLogDebug("deref %s", s->set->name);
47  s->set = NULL;
48  }
49  SCLogDebug("gc:done");
50  return 0;
51 }
52 
53 static int LuaDatasetGetRef(lua_State *luastate)
54 {
55  SCLogDebug("get");
56  struct LuaDataset *s = (struct LuaDataset *)luaL_checkudata(luastate, 1, "dataset::metatable");
57 
58  const char *name = lua_tostring(luastate, 2);
59  if (name == NULL) {
60  LUA_ERROR("null string");
61  }
62 
64  if (dataset == NULL) {
65  LUA_ERROR("dataset not found");
66  }
67  s->set = dataset;
68  return 0;
69 }
70 
71 static int LuaDatasetAdd(lua_State *luastate)
72 {
73  SCLogDebug("add:start");
74  struct LuaDataset *s = luaL_checkudata(luastate, 1, "dataset::metatable");
75  if (s->set == NULL) {
76  LUA_ERROR("dataset is not initialized (call :get first)");
77  }
78 
79  size_t real_len = 0;
80  const uint8_t *str = (const uint8_t *)lua_tolstring(luastate, 2, &real_len);
81  if (str == NULL) {
82  LUA_ERROR("1st arg is not a string");
83  }
84 
85  if (!lua_isinteger(luastate, 3)) {
86  LUA_ERROR("2nd arg is not a string");
87  }
88  lua_Integer n = lua_tointeger(luastate, 3);
89  if (n < 0 || (size_t)n > real_len) {
90  LUA_ERROR("length out of range for supplied string");
91  }
92  uint32_t str_len = (uint32_t)n;
93 
94  int r = SCDatasetAdd(s->set, str, str_len);
95  /* return value through luastate, as a luanumber */
96  lua_pushnumber(luastate, (lua_Number)r);
97  SCLogDebug("add:end");
98  return 1;
99 }
100 
101 static int LuaDatasetNew(lua_State *luastate)
102 {
103  SCLogDebug("new:start");
104  struct LuaDataset *s = (struct LuaDataset *)lua_newuserdata(luastate, sizeof(*s));
105  if (s == NULL) {
106  LUA_ERROR("failed to get userdata");
107  }
108  memset(s, 0, sizeof(*s));
109  luaL_getmetatable(luastate, "dataset::metatable");
110  lua_setmetatable(luastate, -2);
111  SCLogDebug("new:done");
112  return 1;
113 }
114 
115 // clang-format off
116 static const luaL_Reg datasetlib[] = {
117  { "new", LuaDatasetNew },
118  { "get", LuaDatasetGetRef },
119  { "add", LuaDatasetAdd },
120  { "__gc", LuaDatasetGC },
121  { NULL, NULL }
122 };
123 // clang-format on
124 
126 {
127  luaL_newmetatable(luastate, "dataset::metatable");
128  lua_pushvalue(luastate, -1);
129  lua_setfield(luastate, -2, "__index");
130  luaL_setfuncs(luastate, datasetlib, 0);
131  luaL_newlib(luastate, datasetlib);
132 
133  return 1;
134 }
LUA_ERROR
#define LUA_ERROR(msg)
Definition: util-lua-common.h:45
util-lua-common.h
Dataset::name
char name[DATASET_NAME_MAX_LEN+1]
Definition: datasets.h:56
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
name
const char * name
Definition: detect-engine-proto.c:48
util-lua.h
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:530
datasets.h
util-debug.h
SCDatasetAdd
int SCDatasetAdd(Dataset *set, const uint8_t *data, const uint32_t data_len)
Definition: datasets.c:1334
LuaDataset
Definition: util-lua-dataset.c:37
suricata-common.h
LuaLoadDatasetLib
int LuaLoadDatasetLib(lua_State *luastate)
Definition: util-lua-dataset.c:125
DatasetFind
Dataset * DatasetFind(const char *name, enum DatasetTypes type)
look for set by name without creating it
Definition: datasets.c:320
str
#define str(s)
Definition: suricata-common.h:316
DATASET_TYPE_STRING
@ DATASET_TYPE_STRING
Definition: datasets.h:47
app-layer-protos.h
Dataset
Definition: datasets.h:55