52 static const char luaext_key_ld[] =
"suricata:luadata";
59 lua_pushlightuserdata(luastate, (
void *)&luaext_key_ld);
60 lua_gettable(luastate, LUA_REGISTRYINDEX);
61 ld = lua_touserdata(luastate, -1);
80 FlowVar **ret_fv,
bool fv_may_be_null, uint32_t *ret_idx)
88 int ret = GetLuaData(luastate, &ld);
92 if (!lua_isnumber(luastate, 1)) {
95 int id = lua_tonumber(luastate, 1);
104 if (!fv_may_be_null && fv == NULL) {
117 if (!lua_isstring(luastate, 1)) {
120 const char *keystr = lua_tostring(luastate, 1);
121 if (keystr == NULL) {
124 if (!lua_isnumber(luastate, 2)) {
127 int keylen = lua_tonumber(luastate, 2);
128 if (keylen < 0 || keylen > 0xff) {
129 LUA_ERROR(
"key len out of range: max 256");
141 FlowVar **ret_fv,
bool fv_may_be_null, uint32_t *ret_idx)
149 int ret = GetLuaData(luastate, &ld);
153 if (!lua_isnumber(luastate, 1)) {
156 int id = lua_tonumber(luastate, 1);
160 uint32_t idx = ld->
flowint[id];
165 if (!fv_may_be_null && fv == NULL) {
174 static int LuaGetFlowvar(
lua_State *luastate)
181 ret = GetFlow(luastate, &f);
185 if (lua_isnumber(luastate, 1)) {
186 ret = GetFlowVarById(luastate, f, &fv,
false, NULL);
187 if (ret != 0 || fv == NULL)
189 }
else if (lua_isstring(luastate, 1)) {
190 ret = GetFlowVarByKey(luastate, f, &fv);
191 if (ret != 0 || fv == NULL)
194 LUA_ERROR(
"invalid data type as first argument");
203 static int LuaSetFlowvarById(
lua_State *luastate)
213 int ret = GetFlow(luastate, &f);
217 ret = GetFlowVarById(luastate, f, &fv,
true, &idx);
221 if (!lua_isstring(luastate, 2)) {
224 str = lua_tostring(luastate, 2);
229 if (!lua_isnumber(luastate, 3)) {
230 LUA_ERROR(
"buffer length not specified");
232 len = lua_tonumber(luastate, 3);
233 if (len < 0 || len > 0xffff) {
248 static int LuaSetFlowvarByKey(
lua_State *luastate)
256 int ret = GetFlow(luastate, &f);
260 const char *keystr = NULL;
263 keystr = lua_tostring(luastate, 1);
264 if (keystr == NULL) {
267 if (!lua_isnumber(luastate, 2)) {
270 keylen = lua_tonumber(luastate, 2);
271 if (keylen < 0 || keylen > 0xff) {
272 LUA_ERROR(
"key len out of range: max 256");
275 if (!lua_isstring(luastate, 3)) {
278 str = lua_tostring(luastate, 3);
283 if (!lua_isnumber(luastate, 4)) {
284 LUA_ERROR(
"buffer length not specified");
286 len = lua_tonumber(luastate, 4);
287 if (len < 0 || len > 0xffff) {
298 uint8_t *keybuf =
SCMalloc(keylen+1);
303 memcpy(keybuf, keystr, keylen);
304 keybuf[keylen] =
'\0';
310 static int LuaSetFlowvar(
lua_State *luastate)
312 if (lua_isnumber(luastate, 1)) {
313 return LuaSetFlowvarById(luastate);
315 return LuaSetFlowvarByKey(luastate);
319 static int LuaGetFlowint(
lua_State *luastate)
326 int ret = GetFlow(luastate, &f);
330 ret = GetFlowIntById(luastate, f, &fv,
false, NULL);
337 lua_pushnumber(luastate, (lua_Number)number);
342 static int LuaSetFlowint(
lua_State *luastate)
349 int ret = GetLuaData(luastate, &ld);
354 ret = GetFlow(luastate, &f);
359 if (!lua_isnumber(luastate, 1)) {
362 int id = lua_tonumber(luastate, 1);
367 if (!lua_isnumber(luastate, 2)) {
370 lua_Number luanumber = lua_tonumber(luastate, 2);
371 if (luanumber < 0 || id > (
double)UINT_MAX) {
373 "value must be unsigned 32bit int");
375 uint32_t number = (uint32_t)luanumber;
384 SCLogDebug(
"stored flow:%p idx:%u value:%u", f, idx, number);
388 static int LuaIncrFlowint(
lua_State *luastate)
396 int ret = GetFlow(luastate, &f);
400 ret = GetFlowIntById(luastate, f, &fv,
true, &idx);
408 if (number < UINT_MAX)
414 lua_pushnumber(luastate, (lua_Number)number);
415 SCLogDebug(
"incremented flow:%p idx:%u value:%u", f, idx, number);
420 static int LuaDecrFlowint(
lua_State *luastate)
428 int ret = GetFlow(luastate, &f);
432 ret = GetFlowIntById(luastate, f, &fv,
true, &idx);
446 lua_pushnumber(luastate, (lua_Number)number);
447 SCLogDebug(
"decremented flow:%p idx:%u value:%u", f, idx, number);
452 static int LuaGetByteVar(
lua_State *luastate)
460 int ret = GetLuaData(luastate, &ld);
464 if (!lua_isnumber(luastate, 1)) {
467 int id = lua_tonumber(luastate, 1);
471 uint32_t idx = ld->
bytevar[id];
473 lua_pushinteger(luastate, det_ctx->
byte_values[idx]);
484 lua_pushlightuserdata(lua_state, (
void *)&luaext_key_ld);
485 lua_pushlightuserdata(lua_state, (
void *)ld);
486 lua_settable(lua_state, LUA_REGISTRYINDEX);
513 lua_pushcfunction(lua_state, LuaGetFlowvar);
514 lua_setglobal(lua_state,
"ScFlowvarGet");
516 lua_pushcfunction(lua_state, LuaGetFlowvar);
517 lua_setglobal(lua_state,
"SCFlowvarGet");
519 lua_pushcfunction(lua_state, LuaSetFlowvar);
520 lua_setglobal(lua_state,
"ScFlowvarSet");
522 lua_pushcfunction(lua_state, LuaSetFlowvar);
523 lua_setglobal(lua_state,
"SCFlowvarSet");
525 lua_pushcfunction(lua_state, LuaGetFlowint);
526 lua_setglobal(lua_state,
"ScFlowintGet");
528 lua_pushcfunction(lua_state, LuaGetFlowint);
529 lua_setglobal(lua_state,
"SCFlowintGet");
531 lua_pushcfunction(lua_state, LuaSetFlowint);
532 lua_setglobal(lua_state,
"ScFlowintSet");
534 lua_pushcfunction(lua_state, LuaSetFlowint);
535 lua_setglobal(lua_state,
"SCFlowintSet");
537 lua_pushcfunction(lua_state, LuaIncrFlowint);
538 lua_setglobal(lua_state,
"ScFlowintIncr");
540 lua_pushcfunction(lua_state, LuaIncrFlowint);
541 lua_setglobal(lua_state,
"SCFlowintIncr");
543 lua_pushcfunction(lua_state, LuaDecrFlowint);
544 lua_setglobal(lua_state,
"ScFlowintDecr");
546 lua_pushcfunction(lua_state, LuaDecrFlowint);
547 lua_setglobal(lua_state,
"SCFlowintDecr");
549 lua_pushcfunction(lua_state, LuaGetByteVar);
550 lua_setglobal(lua_state,
"SCByteVarGet");