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

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

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

0.0
/src/util-lua-dnp3.c
1
/* Copyright (C) 2015 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

20
#include "app-layer-dnp3.h"
21
#include "app-layer-dnp3-objects.h"
22

23
#include "lua.h"
24
#include "lualib.h"
25
#include "lauxlib.h"
26

27
#include "util-lua.h"
28
#include "util-lua-common.h"
29
#include "util-lua-dnp3.h"
30
#include "util-lua-dnp3-objects.h"
31

32
/**
33
 * \brief Helper macro to push key and integer value onto a table.
34
 */
UNCOV
35
#define LUA_PUSHT_INT(l, k, v) do {             \
×
UNCOV
36
        lua_pushliteral(luastate, k);           \
×
UNCOV
37
        lua_pushinteger(luastate, v);           \
×
UNCOV
38
        lua_settable(luastate, -3);             \
×
UNCOV
39
    } while (0);
×
40

41
static void SCLuaPushTableBoolean(lua_State *L, const char *key, bool val)
UNCOV
42
{
×
UNCOV
43
    lua_pushstring(L, key);
×
UNCOV
44
    lua_pushboolean(L, val);
×
UNCOV
45
    lua_settable(L, -3);
×
UNCOV
46
}
×
47

48
static void DNP3PushPoints(lua_State *luastate, DNP3Object *object)
UNCOV
49
{
×
UNCOV
50
    DNP3Point *point;
×
UNCOV
51
    int i = 1;
×
52

UNCOV
53
    TAILQ_FOREACH(point, object->points, next) {
×
54
        lua_pushinteger(luastate, i++);
×
55
        lua_newtable(luastate);
×
56

57
        lua_pushliteral(luastate, "index");
×
58
        lua_pushinteger(luastate, point->index);
×
59
        lua_settable(luastate, -3);
×
60

61
        DNP3PushPoint(luastate, object, point);
×
62

63
        lua_settable(luastate, -3);
×
64
    }
×
UNCOV
65
}
×
66

67
static void DNP3PushObjects(lua_State *luastate, DNP3ObjectList *objects)
UNCOV
68
{
×
UNCOV
69
    DNP3Object *object = NULL;
×
UNCOV
70
    int i = 1;
×
71

UNCOV
72
    TAILQ_FOREACH(object, objects, next) {
×
UNCOV
73
        lua_pushinteger(luastate, i++);
×
UNCOV
74
        lua_newtable(luastate);
×
75

UNCOV
76
        lua_pushliteral(luastate, "group");
×
UNCOV
77
        lua_pushinteger(luastate, object->group);
×
UNCOV
78
        lua_settable(luastate, -3);
×
79

UNCOV
80
        lua_pushliteral(luastate, "variation");
×
UNCOV
81
        lua_pushinteger(luastate, object->variation);
×
UNCOV
82
        lua_settable(luastate, -3);
×
83

UNCOV
84
        lua_pushliteral(luastate, "points");
×
UNCOV
85
        lua_newtable(luastate);
×
UNCOV
86
        DNP3PushPoints(luastate, object);
×
UNCOV
87
        lua_settable(luastate, -3);
×
88

UNCOV
89
        lua_settable(luastate, -3);
×
UNCOV
90
    }
×
UNCOV
91
}
×
92

93
static void DNP3PushLinkHeader(lua_State *luastate, DNP3LinkHeader *header)
UNCOV
94
{
×
UNCOV
95
    LUA_PUSHT_INT(luastate, "len", header->len);
×
UNCOV
96
    LUA_PUSHT_INT(luastate, "control", header->control);
×
UNCOV
97
    LUA_PUSHT_INT(luastate, "dst", header->dst);
×
UNCOV
98
    LUA_PUSHT_INT(luastate, "src", header->src);
×
UNCOV
99
    LUA_PUSHT_INT(luastate, "crc", header->crc);
×
UNCOV
100
}
×
101

102
static void DNP3PushApplicationHeader(lua_State *luastate,
103
    DNP3ApplicationHeader *header)
UNCOV
104
{
×
UNCOV
105
    LUA_PUSHT_INT(luastate, "control", header->control);
×
UNCOV
106
    LUA_PUSHT_INT(luastate, "function_code", header->function_code);
×
UNCOV
107
}
×
108

109
static void DNP3PushRequest(lua_State *luastate, DNP3Transaction *tx)
UNCOV
110
{
×
111
    /* Link header. */
UNCOV
112
    lua_pushliteral(luastate, "link_header");
×
UNCOV
113
    lua_newtable(luastate);
×
UNCOV
114
    DNP3PushLinkHeader(luastate, &tx->lh);
×
UNCOV
115
    lua_settable(luastate, -3);
×
116

117
    /* Transport header. */
UNCOV
118
    LUA_PUSHT_INT(luastate, "transport_header", tx->th);
×
119

120
    /* Application header. */
UNCOV
121
    lua_pushliteral(luastate, "application_header");
×
UNCOV
122
    lua_newtable(luastate);
×
UNCOV
123
    DNP3PushApplicationHeader(luastate, &tx->ah);
×
UNCOV
124
    lua_settable(luastate, -3);
×
125

UNCOV
126
    lua_pushliteral(luastate, "objects");
×
UNCOV
127
    lua_newtable(luastate);
×
UNCOV
128
    DNP3PushObjects(luastate, &tx->objects);
×
UNCOV
129
    lua_settable(luastate, -3);
×
UNCOV
130
}
×
131

132
static void DNP3PushResponse(lua_State *luastate, DNP3Transaction *tx)
133
{
×
134
    /* Link header. */
135
    lua_pushliteral(luastate, "link_header");
×
136
    lua_newtable(luastate);
×
137
    DNP3PushLinkHeader(luastate, &tx->lh);
×
138
    lua_settable(luastate, -3);
×
139

140
    /* Transport header. */
141
    LUA_PUSHT_INT(luastate, "transport_header", tx->th);
×
142

143
    /* Application header. */
144
    lua_pushliteral(luastate, "application_header");
×
145
    lua_newtable(luastate);
×
146
    DNP3PushApplicationHeader(luastate, &tx->ah);
×
147
    lua_settable(luastate, -3);
×
148

149
    /* Internal indicators. */
150
    LUA_PUSHT_INT(luastate, "indicators", tx->iin.iin1 << 8 | tx->iin.iin2);
×
151

152
    lua_pushliteral(luastate, "objects");
×
153
    lua_newtable(luastate);
×
154
    DNP3PushObjects(luastate, &tx->objects);
×
155
    lua_settable(luastate, -3);
×
156
}
×
157

158
static int DNP3GetTx(lua_State *luastate)
UNCOV
159
{
×
UNCOV
160
    if (!LuaStateNeedProto(luastate, ALPROTO_DNP3)) {
×
161
        return LuaCallbackError(luastate, "error: protocol not dnp3");
×
162
    }
×
163

UNCOV
164
    DNP3Transaction *tx = LuaStateGetTX(luastate);
×
UNCOV
165
    if (tx == NULL) {
×
166
        return LuaCallbackError(luastate, "error: no tx");
×
167
    }
×
168

UNCOV
169
    lua_newtable(luastate);
×
170

UNCOV
171
    lua_pushliteral(luastate, "tx_num");
×
UNCOV
172
    lua_pushinteger(luastate, tx->tx_num);
×
UNCOV
173
    lua_settable(luastate, -3);
×
174

UNCOV
175
    SCLuaPushTableBoolean(luastate, "is_request", tx->is_request);
×
UNCOV
176
    if (tx->is_request) {
×
UNCOV
177
        lua_pushliteral(luastate, "request");
×
UNCOV
178
        lua_newtable(luastate);
×
UNCOV
179
        SCLuaPushTableBoolean(luastate, "done", tx->done);
×
UNCOV
180
        SCLuaPushTableBoolean(luastate, "complete", tx->complete);
×
UNCOV
181
        DNP3PushRequest(luastate, tx);
×
UNCOV
182
        lua_settable(luastate, -3);
×
UNCOV
183
    } else {
×
184
        lua_pushliteral(luastate, "response");
×
185
        lua_newtable(luastate);
×
186
        SCLuaPushTableBoolean(luastate, "done", tx->done);
×
187
        SCLuaPushTableBoolean(luastate, "complete", tx->complete);
×
188
        DNP3PushResponse(luastate, tx);
×
189
        lua_settable(luastate, -3);
×
190
    }
×
191

UNCOV
192
    return 1;
×
UNCOV
193
}
×
194

195
static const struct luaL_Reg dnp3lib[] = {
196
    // clang-format off
197
    { "get_tx", DNP3GetTx, },
198
    { NULL, NULL, }
199
    // clang-format on
200
};
201

202
int SCLuaLoadDnp3Lib(lua_State *L)
UNCOV
203
{
×
UNCOV
204
    luaL_newlib(L, dnp3lib);
×
UNCOV
205
    return 1;
×
UNCOV
206
}
×
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