• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 22550237931

01 Mar 2026 06:56PM UTC coverage: 64.812% (-8.9%) from 73.687%
22550237931

Pull #14920

github

web-flow
Merge e05854a6d into 90823fa90
Pull Request #14920: draft: rust based configuration file parser and loader - v4

561 of 789 new or added lines in 4 files covered. (71.1%)

23225 existing lines in 498 files now uncovered.

131605 of 203055 relevant lines covered (64.81%)

2328818.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/util-lua-flowintlib.c
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)
UNCOV
33
{
×
UNCOV
34
    DetectLuaData *ld;
×
UNCOV
35
    lua_pushlightuserdata(luastate, (void *)&luaext_key_ld);
×
UNCOV
36
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
37
    ld = lua_touserdata(luastate, -1);
×
UNCOV
38
    return ld;
×
UNCOV
39
}
×
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)
UNCOV
51
{
×
UNCOV
52
    DetectLuaData *ld = GetLuaData(L);
×
UNCOV
53
    const char *name = luaL_checkstring(L, 1);
×
UNCOV
54
    uint32_t *flowvar_id = lua_newuserdata(L, sizeof(*flowvar_id));
×
UNCOV
55
    *flowvar_id = VarNameStoreRegister(name, VAR_TYPE_FLOW_INT);
×
UNCOV
56
    if (*flowvar_id == 0) {
×
57
        return luaL_error(L, "failed to register flowvar");
×
58
    }
×
UNCOV
59
    ld->flowint[ld->flowints++] = *flowvar_id;
×
60

UNCOV
61
    luaL_getmetatable(L, suricata_flowint_mt);
×
UNCOV
62
    lua_setmetatable(L, -2);
×
63

UNCOV
64
    return 1;
×
UNCOV
65
}
×
66

67
static int LuaFlowintGet(lua_State *L)
UNCOV
68
{
×
UNCOV
69
    const char *name = luaL_checkstring(L, 1);
×
UNCOV
70
    uint32_t *flowvar_id = lua_newuserdata(L, sizeof(*flowvar_id));
×
UNCOV
71
    *flowvar_id = VarNameStoreLookupByName(name, VAR_TYPE_FLOW_INT);
×
UNCOV
72
    if (*flowvar_id == 0) {
×
73
        return luaL_error(L, "flowvar does not exist");
×
74
    }
×
75

UNCOV
76
    luaL_getmetatable(L, suricata_flowint_mt);
×
UNCOV
77
    lua_setmetatable(L, -2);
×
78

UNCOV
79
    return 1;
×
UNCOV
80
}
×
81

82
static int LuaFlowintValue(lua_State *L)
UNCOV
83
{
×
UNCOV
84
    uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowint_mt);
×
UNCOV
85
    Flow *f = LuaStateGetFlow(L);
×
UNCOV
86
    if (f == NULL) {
×
87
        return LuaCallbackError(L, "flow is NULL");
×
88
    }
×
UNCOV
89
    FlowVar *fv = FlowVarGet(f, *flowvar_id);
×
UNCOV
90
    if (fv == NULL) {
×
UNCOV
91
        lua_pushnil(L);
×
UNCOV
92
    } else {
×
UNCOV
93
        lua_pushnumber(L, (lua_Number)fv->data.fv_int.value);
×
UNCOV
94
    }
×
UNCOV
95
    return 1;
×
UNCOV
96
}
×
97

98
static int LuaFlowintSet(lua_State *L)
UNCOV
99
{
×
UNCOV
100
    const int value = (int)luaL_checkinteger(L, 2);
×
UNCOV
101
    uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowint_mt);
×
UNCOV
102
    Flow *f = LuaStateGetFlow(L);
×
UNCOV
103
    if (f == NULL) {
×
104
        return luaL_error(L, "no flow");
×
105
    }
×
106

UNCOV
107
    FlowVarAddInt(f, *flowvar_id, value);
×
108

UNCOV
109
    return 1;
×
UNCOV
110
}
×
111

112
static int LuaFlowintIncr(lua_State *L)
UNCOV
113
{
×
UNCOV
114
    uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowint_mt);
×
UNCOV
115
    Flow *f = LuaStateGetFlow(L);
×
UNCOV
116
    if (f == NULL) {
×
117
        return luaL_error(L, "no flow");
×
118
    }
×
119

UNCOV
120
    FlowVar *fv = FlowVarGet(f, *flowvar_id);
×
UNCOV
121
    uint32_t value;
×
UNCOV
122
    if (fv == NULL) {
×
UNCOV
123
        value = 1;
×
UNCOV
124
    } else {
×
UNCOV
125
        value = fv->data.fv_int.value;
×
UNCOV
126
        if (value < UINT32_MAX) {
×
UNCOV
127
            value++;
×
UNCOV
128
        }
×
UNCOV
129
    }
×
130

UNCOV
131
    FlowVarAddInt(f, *flowvar_id, value);
×
UNCOV
132
    lua_pushnumber(L, (lua_Number)value);
×
133

UNCOV
134
    return 1;
×
UNCOV
135
}
×
136

137
static int LuaFlowintDecr(lua_State *L)
UNCOV
138
{
×
UNCOV
139
    uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowint_mt);
×
UNCOV
140
    Flow *f = LuaStateGetFlow(L);
×
UNCOV
141
    if (f == NULL) {
×
142
        return luaL_error(L, "no flow");
×
143
    }
×
144

UNCOV
145
    FlowVar *fv = FlowVarGet(f, *flowvar_id);
×
UNCOV
146
    uint32_t value;
×
UNCOV
147
    if (fv == NULL) {
×
148
        value = 0;
×
UNCOV
149
    } else {
×
UNCOV
150
        value = fv->data.fv_int.value;
×
UNCOV
151
        if (value > 0) {
×
UNCOV
152
            value--;
×
UNCOV
153
        }
×
UNCOV
154
    }
×
155

UNCOV
156
    FlowVarAddInt(f, *flowvar_id, value);
×
UNCOV
157
    lua_pushnumber(L, (lua_Number)value);
×
158

UNCOV
159
    return 1;
×
UNCOV
160
}
×
161

162
static const luaL_Reg flowvarlib[] = {
163
    // clang-format off
164
    { "register", LuaFlowintRegister, },
165
    { "get", LuaFlowintGet },
166
    { NULL, NULL, },
167
    // clang-format on
168
};
169

170
static const luaL_Reg flowvarmt[] = {
171
    // clang-format off
172
    { "value", LuaFlowintValue, },
173
    { "set", LuaFlowintSet, },
174
    { "incr", LuaFlowintIncr, },
175
    { "decr", LuaFlowintDecr, },
176
    { NULL, NULL, },
177
    // clang-format on
178
};
179

180
int LuaLoadFlowintLib(lua_State *L)
UNCOV
181
{
×
UNCOV
182
    luaL_newmetatable(L, suricata_flowint_mt);
×
UNCOV
183
    lua_pushvalue(L, -1);
×
UNCOV
184
    lua_setfield(L, -2, "__index");
×
UNCOV
185
    luaL_setfuncs(L, flowvarmt, 0);
×
186

UNCOV
187
    luaL_newlib(L, flowvarlib);
×
UNCOV
188
    return 1;
×
UNCOV
189
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc