suricata
util-lua-ntp.c
Go to the documentation of this file.
1 /* Copyright (C) 2026 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 "util-lua-ntp.h"
20 #include "util-lua.h"
21 #include "util-lua-common.h"
22 #include "rust.h"
23 
24 static const char ntp_tx[] = "suricata:ntp:tx";
25 
26 struct LuaTx {
27  NTPTransaction *tx;
28 };
29 
30 static int LuaNtpGetTx(lua_State *L)
31 {
32  if (!(LuaStateNeedProto(L, ALPROTO_NTP))) {
33  return LuaCallbackError(L, "error: protocol not ntp");
34  }
35  NTPTransaction *tx = LuaStateGetTX(L);
36  if (tx == NULL) {
37  return LuaCallbackError(L, "error: no tx available");
38  }
39  struct LuaTx *ltx = (struct LuaTx *)lua_newuserdata(L, sizeof(*ltx));
40  if (ltx == NULL) {
41  return LuaCallbackError(L, "error: fail to allocate user data");
42  }
43  ltx->tx = tx;
44 
45  luaL_getmetatable(L, ntp_tx);
46  lua_setmetatable(L, -2);
47 
48  return 1;
49 }
50 
51 static int LuaNtpTxGetVersion(lua_State *L)
52 {
53  struct LuaTx *tx = luaL_testudata(L, 1, ntp_tx);
54  if (tx == NULL) {
55  lua_pushnil(L);
56  return 1;
57  }
58  return SCNtpLuaGetVersion(L, tx->tx);
59 }
60 
61 static int LuaNtpTxGetMode(lua_State *L)
62 {
63  struct LuaTx *tx = luaL_testudata(L, 1, ntp_tx);
64  if (tx == NULL) {
65  lua_pushnil(L);
66  return 1;
67  }
68  return SCNtpLuaGetMode(L, tx->tx);
69 }
70 
71 static int LuaNtpTxGetStratum(lua_State *L)
72 {
73  struct LuaTx *tx = luaL_testudata(L, 1, ntp_tx);
74  if (tx == NULL) {
75  lua_pushnil(L);
76  return 1;
77  }
78  return SCNtpLuaGetStratum(L, tx->tx);
79 }
80 
81 static int LuaNtpTxGetReferenceId(lua_State *L)
82 {
83  struct LuaTx *tx = luaL_testudata(L, 1, ntp_tx);
84  if (tx == NULL) {
85  lua_pushnil(L);
86  return 1;
87  }
88  return SCNtpLuaGetReferenceId(L, tx->tx);
89 }
90 
91 static const struct luaL_Reg txlib[] = {
92  // clang-format off
93  { "mode", LuaNtpTxGetMode },
94  { "reference_id", LuaNtpTxGetReferenceId },
95  { "stratum", LuaNtpTxGetStratum },
96  { "version", LuaNtpTxGetVersion },
97  { NULL, NULL, }
98  // clang-format on
99 };
100 
101 static const struct luaL_Reg ntplib[] = {
102  // clang-format off
103  { "get_tx", LuaNtpGetTx },
104  { NULL, NULL,}
105  // clang-format on
106 };
107 
109 {
110  luaL_newmetatable(L, ntp_tx);
111  lua_pushvalue(L, -1);
112  lua_setfield(L, -2, "__index");
113  luaL_setfuncs(L, txlib, 0);
114 
115  luaL_newlib(L, ntplib);
116  return 1;
117 }
util-lua-common.h
util-lua.h
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:39
util-lua-ntp.h
LuaTx::tx
NTPTransaction * tx
Definition: util-lua-ntp.c:27
rust.h
lua_State
struct lua_State lua_State
Definition: suricata-common.h:523
LuaTx
Definition: util-lua-dns.c:34
LuaStateGetTX
void * LuaStateGetTX(lua_State *luastate)
get tx pointer from the lua state
Definition: util-lua.c:134
suricata-common.h
ALPROTO_NTP
@ ALPROTO_NTP
Definition: app-layer-protos.h:52
LuaTx::tx
DNSTransaction * tx
Definition: util-lua-dns.c:35
SCLuaLoadNtpLib
int SCLuaLoadNtpLib(lua_State *L)
Definition: util-lua-ntp.c:108
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:102