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  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 *)lua_touserdata(luastate, 1);
57  if (s == NULL) {
58  LUA_ERROR("dataset is not initialized");
59  }
60 
61  const char *name = lua_tostring(luastate, 2);
62  if (name == NULL) {
63  LUA_ERROR("null string");
64  }
65 
67  if (dataset == NULL) {
68  LUA_ERROR("dataset not found");
69  }
70  s->set = dataset;
71  return 0;
72 }
73 
74 static int LuaDatasetAdd(lua_State *luastate)
75 {
76  SCLogDebug("add:start");
77  struct LuaDataset *s = (struct LuaDataset *)lua_touserdata(luastate, 1);
78  if (s == NULL) {
79  LUA_ERROR("dataset is not initialized");
80  }
81  if (!lua_isstring(luastate, 2)) {
82  LUA_ERROR("1st arg is not a string");
83  }
84  if (!lua_isnumber(luastate, 3)) {
85  LUA_ERROR("2nd arg is not a number");
86  }
87 
88  const uint8_t *str = (const uint8_t *)lua_tostring(luastate, 2);
89  if (str == NULL) {
90  LUA_ERROR("1st arg is not null string");
91  }
92 
93  uint32_t str_len = lua_tonumber(luastate, 3);
94 
95  int r = SCDatasetAdd(s->set, (const uint8_t *)str, str_len);
96  /* return value through luastate, as a luanumber */
97  lua_pushnumber(luastate, (lua_Number)r);
98  SCLogDebug("add:end");
99  return 1;
100 }
101 
102 static int LuaDatasetNew(lua_State *luastate)
103 {
104  SCLogDebug("new:start");
105  struct LuaDataset *s = (struct LuaDataset *)lua_newuserdata(luastate, sizeof(*s));
106  if (s == NULL) {
107  LUA_ERROR("failed to get userdata");
108  }
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:523
datasets.h
util-debug.h
SCDatasetAdd
int SCDatasetAdd(Dataset *set, const uint8_t *data, const uint32_t data_len)
Definition: datasets.c:1338
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:308
DATASET_TYPE_STRING
@ DATASET_TYPE_STRING
Definition: datasets.h:47
app-layer-protos.h
Dataset
Definition: datasets.h:55