74 static const char luaext_key_ld[] =
"suricata:luadata";
79 #define LUA_ERROR(msg) \
80 LuaCallbackError(luastate, (msg)); \
88 lua_pushlightuserdata(luastate, (
void *)&luaext_key_ld);
89 lua_gettable(luastate, LUA_REGISTRYINDEX);
90 ld = lua_touserdata(luastate, -1);
109 FlowVar **ret_fv,
bool fv_may_be_null, uint32_t *ret_idx)
117 int ret = GetLuaData(luastate, &ld);
121 if (!lua_isnumber(luastate, 1)) {
124 int id = lua_tonumber(luastate, 1);
128 uint32_t idx = ld->
flowvar[id];
133 if (!fv_may_be_null && fv == NULL) {
146 if (!lua_isstring(luastate, 1)) {
149 const char *keystr = lua_tostring(luastate, 1);
150 if (keystr == NULL) {
153 if (!lua_isnumber(luastate, 2)) {
156 int keylen = lua_tonumber(luastate, 2);
157 if (keylen < 0 || keylen > 0xff) {
158 LUA_ERROR(
"key len out of range: max 256");
170 FlowVar **ret_fv,
bool fv_may_be_null, uint32_t *ret_idx)
178 int ret = GetLuaData(luastate, &ld);
182 if (!lua_isnumber(luastate, 1)) {
185 int id = lua_tonumber(luastate, 1);
189 uint32_t idx = ld->
flowint[id];
194 if (!fv_may_be_null && fv == NULL) {
203 static int LuaGetFlowvar(
lua_State *luastate)
210 ret = GetFlow(luastate, &f);
214 if (lua_isnumber(luastate, 1)) {
215 ret = GetFlowVarById(luastate, f, &fv,
false, NULL);
216 if (ret != 0 || fv == NULL)
218 }
else if (lua_isstring(luastate, 1)) {
219 ret = GetFlowVarByKey(luastate, f, &fv);
220 if (ret != 0 || fv == NULL)
223 LUA_ERROR(
"invalid data type as first argument");
232 static int LuaSetFlowvarById(
lua_State *luastate)
242 int ret = GetFlow(luastate, &f);
246 ret = GetFlowVarById(luastate, f, &fv,
true, &idx);
250 if (!lua_isstring(luastate, 2)) {
253 str = lua_tostring(luastate, 2);
258 if (!lua_isnumber(luastate, 3)) {
259 LUA_ERROR(
"buffer length not specified");
261 len = lua_tonumber(luastate, 3);
262 if (len < 0 || len > 0xffff) {
277 static int LuaSetFlowvarByKey(
lua_State *luastate)
285 int ret = GetFlow(luastate, &f);
289 const char *keystr = NULL;
292 keystr = lua_tostring(luastate, 1);
293 if (keystr == NULL) {
296 if (!lua_isnumber(luastate, 2)) {
299 keylen = lua_tonumber(luastate, 2);
300 if (keylen < 0 || keylen > 0xff) {
301 LUA_ERROR(
"key len out of range: max 256");
304 if (!lua_isstring(luastate, 3)) {
307 str = lua_tostring(luastate, 3);
312 if (!lua_isnumber(luastate, 4)) {
313 LUA_ERROR(
"buffer length not specified");
315 len = lua_tonumber(luastate, 4);
316 if (len < 0 || len > 0xffff) {
327 uint8_t *keybuf =
SCMalloc(keylen+1);
332 memcpy(keybuf, keystr, keylen);
333 keybuf[keylen] =
'\0';
339 static int LuaSetFlowvar(
lua_State *luastate)
341 if (lua_isnumber(luastate, 1)) {
342 return LuaSetFlowvarById(luastate);
344 return LuaSetFlowvarByKey(luastate);
348 static int LuaGetFlowint(
lua_State *luastate)
355 int ret = GetFlow(luastate, &f);
359 ret = GetFlowIntById(luastate, f, &fv,
false, NULL);
366 lua_pushnumber(luastate, (lua_Number)number);
371 static int LuaSetFlowint(
lua_State *luastate)
378 int ret = GetLuaData(luastate, &ld);
383 ret = GetFlow(luastate, &f);
388 if (!lua_isnumber(luastate, 1)) {
391 int id = lua_tonumber(luastate, 1);
396 if (!lua_isnumber(luastate, 2)) {
399 lua_Number luanumber = lua_tonumber(luastate, 2);
400 if (luanumber < 0 || id > (
double)UINT_MAX) {
402 "value must be unsigned 32bit int");
404 uint32_t number = (uint32_t)luanumber;
413 SCLogDebug(
"stored flow:%p idx:%u value:%u", f, idx, number);
417 static int LuaIncrFlowint(
lua_State *luastate)
425 int ret = GetFlow(luastate, &f);
429 ret = GetFlowIntById(luastate, f, &fv,
true, &idx);
437 if (number < UINT_MAX)
443 lua_pushnumber(luastate, (lua_Number)number);
444 SCLogDebug(
"incremented flow:%p idx:%u value:%u", f, idx, number);
449 static int LuaDecrFlowint(
lua_State *luastate)
457 int ret = GetFlow(luastate, &f);
461 ret = GetFlowIntById(luastate, f, &fv,
true, &idx);
475 lua_pushnumber(luastate, (lua_Number)number);
476 SCLogDebug(
"decremented flow:%p idx:%u value:%u", f, idx, number);
481 static int LuaGetByteVar(
lua_State *luastate)
489 int ret = GetLuaData(luastate, &ld);
493 if (!lua_isnumber(luastate, 1)) {
496 int id = lua_tonumber(luastate, 1);
500 uint32_t idx = ld->
bytevar[id];
502 lua_pushinteger(luastate, det_ctx->
byte_values[idx]);
513 lua_pushlightuserdata(lua_state, (
void *)&luaext_key_ld);
514 lua_pushlightuserdata(lua_state, (
void *)ld);
515 lua_settable(lua_state, LUA_REGISTRYINDEX);
542 lua_pushcfunction(lua_state, LuaGetFlowvar);
543 lua_setglobal(lua_state,
"ScFlowvarGet");
545 lua_pushcfunction(lua_state, LuaGetFlowvar);
546 lua_setglobal(lua_state,
"SCFlowvarGet");
548 lua_pushcfunction(lua_state, LuaSetFlowvar);
549 lua_setglobal(lua_state,
"ScFlowvarSet");
551 lua_pushcfunction(lua_state, LuaSetFlowvar);
552 lua_setglobal(lua_state,
"SCFlowvarSet");
554 lua_pushcfunction(lua_state, LuaGetFlowint);
555 lua_setglobal(lua_state,
"ScFlowintGet");
557 lua_pushcfunction(lua_state, LuaGetFlowint);
558 lua_setglobal(lua_state,
"SCFlowintGet");
560 lua_pushcfunction(lua_state, LuaSetFlowint);
561 lua_setglobal(lua_state,
"ScFlowintSet");
563 lua_pushcfunction(lua_state, LuaSetFlowint);
564 lua_setglobal(lua_state,
"SCFlowintSet");
566 lua_pushcfunction(lua_state, LuaIncrFlowint);
567 lua_setglobal(lua_state,
"ScFlowintIncr");
569 lua_pushcfunction(lua_state, LuaIncrFlowint);
570 lua_setglobal(lua_state,
"SCFlowintIncr");
572 lua_pushcfunction(lua_state, LuaDecrFlowint);
573 lua_setglobal(lua_state,
"ScFlowintDecr");
575 lua_pushcfunction(lua_state, LuaDecrFlowint);
576 lua_setglobal(lua_state,
"SCFlowintDecr");
578 lua_pushcfunction(lua_state, LuaGetByteVar);
579 lua_setglobal(lua_state,
"SCByteVarGet");