/src/suricata8/src/util-lua-flowintlib.c
Line | Count | Source |
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 | | #include "suricata-common.h" |
19 | | #include "app-layer-protos.h" |
20 | | #include "flow-var.h" |
21 | | #include "lauxlib.h" |
22 | | #include "util-debug.h" |
23 | | #include "util-lua-common.h" |
24 | | #include "util-lua-flowintlib.h" |
25 | | #include "util-lua.h" |
26 | | #include "util-var-name.h" |
27 | | #include "detect-lua.h" |
28 | | #include "detect-lua-extensions.h" |
29 | | |
30 | | static const char suricata_flowint_mt[] = "suricata:flowint:mt"; |
31 | | |
32 | | static DetectLuaData *GetLuaData(lua_State *luastate) |
33 | 0 | { |
34 | 0 | DetectLuaData *ld; |
35 | 0 | lua_pushlightuserdata(luastate, (void *)&luaext_key_ld); |
36 | 0 | lua_gettable(luastate, LUA_REGISTRYINDEX); |
37 | 0 | ld = lua_touserdata(luastate, -1); |
38 | 0 | return ld; |
39 | 0 | } |
40 | | |
41 | | /** |
42 | | * \brief Register a flowvar. |
43 | | * |
44 | | * Ensures that a flowvar exists for the provided name, will be |
45 | | * created if needed. |
46 | | * |
47 | | * The flowvar ID is returned, however as this is most likely to be |
48 | | * used in the scripts "init" method, this ID is unlikely to be used. |
49 | | */ |
50 | | static int LuaFlowintRegister(lua_State *L) |
51 | 0 | { |
52 | 0 | DetectLuaData *ld = GetLuaData(L); |
53 | 0 | const char *name = luaL_checkstring(L, 1); |
54 | 0 | uint32_t *flowvar_id = lua_newuserdata(L, sizeof(*flowvar_id)); |
55 | 0 | *flowvar_id = VarNameStoreRegister(name, VAR_TYPE_FLOW_INT); |
56 | 0 | if (*flowvar_id == 0) { |
57 | 0 | return luaL_error(L, "failed to register flowvar"); |
58 | 0 | } |
59 | 0 | if (ld->flowints >= DETECT_LUA_MAX_FLOWINTS) { |
60 | 0 | return luaL_error(L, "too many flowints registered"); |
61 | 0 | } |
62 | 0 | ld->flowint[ld->flowints++] = *flowvar_id; |
63 | |
|
64 | 0 | luaL_getmetatable(L, suricata_flowint_mt); |
65 | 0 | lua_setmetatable(L, -2); |
66 | |
|
67 | 0 | return 1; |
68 | 0 | } |
69 | | |
70 | | static int LuaFlowintGet(lua_State *L) |
71 | 0 | { |
72 | 0 | const char *name = luaL_checkstring(L, 1); |
73 | 0 | uint32_t *flowvar_id = lua_newuserdata(L, sizeof(*flowvar_id)); |
74 | 0 | *flowvar_id = VarNameStoreLookupByName(name, VAR_TYPE_FLOW_INT); |
75 | 0 | if (*flowvar_id == 0) { |
76 | 0 | return luaL_error(L, "flowvar does not exist"); |
77 | 0 | } |
78 | | |
79 | 0 | luaL_getmetatable(L, suricata_flowint_mt); |
80 | 0 | lua_setmetatable(L, -2); |
81 | |
|
82 | 0 | return 1; |
83 | 0 | } |
84 | | |
85 | | static int LuaFlowintValue(lua_State *L) |
86 | 0 | { |
87 | 0 | uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowint_mt); |
88 | 0 | Flow *f = LuaStateGetFlow(L); |
89 | 0 | if (f == NULL) { |
90 | 0 | return LuaCallbackError(L, "flow is NULL"); |
91 | 0 | } |
92 | 0 | FlowVar *fv = FlowVarGet(f, *flowvar_id); |
93 | 0 | if (fv == NULL) { |
94 | 0 | lua_pushnil(L); |
95 | 0 | } else { |
96 | 0 | lua_pushnumber(L, (lua_Number)fv->data.fv_int.value); |
97 | 0 | } |
98 | 0 | return 1; |
99 | 0 | } |
100 | | |
101 | | static int LuaFlowintSet(lua_State *L) |
102 | 0 | { |
103 | 0 | const int value = (int)luaL_checkinteger(L, 2); |
104 | 0 | uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowint_mt); |
105 | 0 | Flow *f = LuaStateGetFlow(L); |
106 | 0 | if (f == NULL) { |
107 | 0 | return luaL_error(L, "no flow"); |
108 | 0 | } |
109 | | |
110 | 0 | FlowVarAddInt(f, *flowvar_id, value); |
111 | |
|
112 | 0 | return 1; |
113 | 0 | } |
114 | | |
115 | | static int LuaFlowintIncr(lua_State *L) |
116 | 0 | { |
117 | 0 | uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowint_mt); |
118 | 0 | Flow *f = LuaStateGetFlow(L); |
119 | 0 | if (f == NULL) { |
120 | 0 | return luaL_error(L, "no flow"); |
121 | 0 | } |
122 | | |
123 | 0 | FlowVar *fv = FlowVarGet(f, *flowvar_id); |
124 | 0 | uint32_t value; |
125 | 0 | if (fv == NULL) { |
126 | 0 | value = 1; |
127 | 0 | } else { |
128 | 0 | value = fv->data.fv_int.value; |
129 | 0 | if (value < UINT32_MAX) { |
130 | 0 | value++; |
131 | 0 | } |
132 | 0 | } |
133 | |
|
134 | 0 | FlowVarAddInt(f, *flowvar_id, value); |
135 | 0 | lua_pushnumber(L, (lua_Number)value); |
136 | |
|
137 | 0 | return 1; |
138 | 0 | } |
139 | | |
140 | | static int LuaFlowintDecr(lua_State *L) |
141 | 0 | { |
142 | 0 | uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowint_mt); |
143 | 0 | Flow *f = LuaStateGetFlow(L); |
144 | 0 | if (f == NULL) { |
145 | 0 | return luaL_error(L, "no flow"); |
146 | 0 | } |
147 | | |
148 | 0 | FlowVar *fv = FlowVarGet(f, *flowvar_id); |
149 | 0 | uint32_t value; |
150 | 0 | if (fv == NULL) { |
151 | 0 | value = 0; |
152 | 0 | } else { |
153 | 0 | value = fv->data.fv_int.value; |
154 | 0 | if (value > 0) { |
155 | 0 | value--; |
156 | 0 | } |
157 | 0 | } |
158 | |
|
159 | 0 | FlowVarAddInt(f, *flowvar_id, value); |
160 | 0 | lua_pushnumber(L, (lua_Number)value); |
161 | |
|
162 | 0 | return 1; |
163 | 0 | } |
164 | | |
165 | | static const luaL_Reg flowvarlib[] = { |
166 | | // clang-format off |
167 | | { "register", LuaFlowintRegister, }, |
168 | | { "get", LuaFlowintGet }, |
169 | | { NULL, NULL, }, |
170 | | // clang-format on |
171 | | }; |
172 | | |
173 | | static const luaL_Reg flowvarmt[] = { |
174 | | // clang-format off |
175 | | { "value", LuaFlowintValue, }, |
176 | | { "set", LuaFlowintSet, }, |
177 | | { "incr", LuaFlowintIncr, }, |
178 | | { "decr", LuaFlowintDecr, }, |
179 | | { NULL, NULL, }, |
180 | | // clang-format on |
181 | | }; |
182 | | |
183 | | int LuaLoadFlowintLib(lua_State *L) |
184 | 0 | { |
185 | 0 | luaL_newmetatable(L, suricata_flowint_mt); |
186 | 0 | lua_pushvalue(L, -1); |
187 | 0 | lua_setfield(L, -2, "__index"); |
188 | 0 | luaL_setfuncs(L, flowvarmt, 0); |
189 | |
|
190 | 0 | luaL_newlib(L, flowvarlib); |
191 | 0 | return 1; |
192 | 0 | } |