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 {
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  void *tx = LuaStateGetTX(L);
36  if (tx == NULL) {
37  return LuaCallbackError(L, "error: no tx available");
38  }
39  struct LuaTx *ltx = SCLuaNewUserData(L, SCLUA_TUD_NTP_TX, sizeof(*ltx));
40  if (ltx == NULL) {
41  return LuaCallbackError(L, "error: fail to allocate user data");
42  }
43  SCLuaTxRefInit(L, &ltx->ref, LuaStateGetTxId(L));
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 = SCLuaTestUserData(L, 1, ntp_tx, SCLUA_TUD_NTP_TX);
54  if (tx == NULL) {
55  lua_pushnil(L);
56  return 1;
57  }
58  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
59  return SCNtpLuaGetVersion(L, txptr);
60 }
61 
62 static int LuaNtpTxGetMode(lua_State *L)
63 {
64  struct LuaTx *tx = SCLuaTestUserData(L, 1, ntp_tx, SCLUA_TUD_NTP_TX);
65  if (tx == NULL) {
66  lua_pushnil(L);
67  return 1;
68  }
69  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
70  return SCNtpLuaGetMode(L, txptr);
71 }
72 
73 static int LuaNtpTxGetStratum(lua_State *L)
74 {
75  struct LuaTx *tx = SCLuaTestUserData(L, 1, ntp_tx, SCLUA_TUD_NTP_TX);
76  if (tx == NULL) {
77  lua_pushnil(L);
78  return 1;
79  }
80  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
81  return SCNtpLuaGetStratum(L, txptr);
82 }
83 
84 static int LuaNtpTxGetReferenceId(lua_State *L)
85 {
86  struct LuaTx *tx = SCLuaTestUserData(L, 1, ntp_tx, SCLUA_TUD_NTP_TX);
87  if (tx == NULL) {
88  lua_pushnil(L);
89  return 1;
90  }
91  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
92  return SCNtpLuaGetReferenceId(L, txptr);
93 }
94 
95 static const struct luaL_Reg txlib[] = {
96  // clang-format off
97  { "mode", LuaNtpTxGetMode },
98  { "reference_id", LuaNtpTxGetReferenceId },
99  { "stratum", LuaNtpTxGetStratum },
100  { "version", LuaNtpTxGetVersion },
101  { NULL, NULL, }
102  // clang-format on
103 };
104 
105 static const struct luaL_Reg ntplib[] = {
106  // clang-format off
107  { "get_tx", LuaNtpGetTx },
108  { NULL, NULL,}
109  // clang-format on
110 };
111 
113 {
114  luaL_newmetatable(L, ntp_tx);
115  lua_pushvalue(L, -1);
116  lua_setfield(L, -2, "__index");
117  luaL_setfuncs(L, txlib, 0);
118 
119  luaL_newlib(L, ntplib);
120  return 1;
121 }
LuaTx::ref
SCLuaTxRef ref
Definition: util-lua-dns.c:35
SCLUA_TUD_NTP_TX
@ SCLUA_TUD_NTP_TX
Definition: util-lua-common.h:66
util-lua-common.h
util-lua.h
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:218
SCLuaTxRef_
Definition: util-lua-common.h:92
util-lua-ntp.h
rust.h
lua_State
struct lua_State lua_State
Definition: suricata-common.h:527
LuaTx
Definition: util-lua-dns.c:34
SCLuaTestUserData
void * SCLuaTestUserData(lua_State *L, int idx, const char *mt, uint32_t type_id)
Definition: util-lua-common.c:123
SCLuaNewUserData
void * SCLuaNewUserData(lua_State *L, uint32_t type_id, size_t size)
Definition: util-lua-common.c:42
LuaStateGetTX
void * LuaStateGetTX(lua_State *luastate)
get tx pointer from the lua state
Definition: util-lua.c:234
LuaStateGetTxId
uint64_t LuaStateGetTxId(lua_State *luastate)
get tx id from the lua state
Definition: util-lua.c:240
suricata-common.h
SCLUA_RESOLVE_TX
#define SCLUA_RESOLVE_TX(L, refp, txvar)
Definition: util-lua-common.h:128
SCLuaTxRefInit
void SCLuaTxRefInit(lua_State *L, SCLuaTxRef *ref, uint64_t tx_id)
Definition: util-lua-common.c:164
ALPROTO_NTP
@ ALPROTO_NTP
Definition: app-layer-protos.h:52
SCLuaLoadNtpLib
int SCLuaLoadNtpLib(lua_State *L)
Definition: util-lua-ntp.c:112
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:289