suricata
util-lua-dns.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-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 /**
19  * \file
20  *
21  * \author Eric Leblond <eric@regit.org>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "util-lua-dns.h"
27 #include "util-lua.h"
28 #include "util-lua-common.h"
29 #include "rust.h"
30 
31 // #define DNS_MT "suricata:dns:tx"
32 static const char dns_tx[] = "suricata:dns:tx";
33 
34 struct LuaTx {
36 };
37 
38 static int LuaDnsGetTx(lua_State *L)
39 {
40  if (!(LuaStateNeedProto(L, ALPROTO_DNS))) {
41  return LuaCallbackError(L, "error: protocol not dns");
42  }
43  DNSTransaction *tx = LuaStateGetTX(L);
44  if (tx == NULL) {
45  return LuaCallbackError(L, "error: no tx available");
46  }
47  struct LuaTx *ltx = SCLuaNewUserData(L, SCLUA_TUD_DNS_TX, sizeof(*ltx));
48  if (ltx == NULL) {
49  return LuaCallbackError(L, "error: fail to allocate user data");
50  }
51  SCLuaTxRefInit(L, &ltx->ref, LuaStateGetTxId(L));
52 
53  luaL_getmetatable(L, dns_tx);
54  lua_setmetatable(L, -2);
55 
56  return 1;
57 }
58 
59 static int LuaDnsTxGetRrname(lua_State *L)
60 {
61  struct LuaTx *tx = SCLuaTestUserData(L, 1, dns_tx, SCLUA_TUD_DNS_TX);
62  if (tx == NULL) {
63  lua_pushnil(L);
64  return 1;
65  }
66  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
67  return SCDnsLuaGetRrname(L, txptr);
68 }
69 
70 static int LuaDnsTxGetTxid(lua_State *L)
71 {
72  struct LuaTx *tx = SCLuaTestUserData(L, 1, dns_tx, SCLUA_TUD_DNS_TX);
73  if (tx == NULL) {
74  lua_pushnil(L);
75  return 1;
76  }
77  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
78  return SCDnsLuaGetTxId(L, txptr);
79 }
80 
81 static int LuaDnsTxGetRcode(lua_State *L)
82 {
83  struct LuaTx *tx = SCLuaTestUserData(L, 1, dns_tx, SCLUA_TUD_DNS_TX);
84  if (tx == NULL) {
85  lua_pushnil(L);
86  return 1;
87  }
88  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
89  return SCDnsLuaGetRcode(L, txptr);
90 }
91 
92 static int LuaDnsTxGetRcodeString(lua_State *L)
93 {
94  struct LuaTx *tx = SCLuaTestUserData(L, 1, dns_tx, SCLUA_TUD_DNS_TX);
95  if (tx == NULL) {
96  lua_pushnil(L);
97  return 1;
98  }
99  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
100  return SCDnsLuaGetRcodeString(L, txptr);
101 }
102 
103 static int LuaDnsTxGetRecursionDesired(lua_State *L)
104 {
105  struct LuaTx *tx = SCLuaTestUserData(L, 1, dns_tx, SCLUA_TUD_DNS_TX);
106  if (tx == NULL) {
107  lua_pushnil(L);
108  return 1;
109  }
110  /* RD is a request flag (bit 0x0100); the response flags only carry
111  * RA (0x0080) and friends */
112  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
113  uint16_t flags = SCDnsTxGetRequestFlags(txptr);
114  int recursion_desired = flags & 0x0100 ? 1 : 0;
115  lua_pushboolean(L, recursion_desired);
116  return 1;
117 }
118 
119 static int LuaDnsTxGetQueries(lua_State *L)
120 {
121  struct LuaTx *tx = SCLuaTestUserData(L, 1, dns_tx, SCLUA_TUD_DNS_TX);
122  if (tx == NULL) {
123  lua_pushnil(L);
124  return 1;
125  }
126  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
127  return SCDnsLuaGetQueryTable(L, txptr);
128 }
129 
130 static int LuaDnsTxGetAnswers(lua_State *L)
131 {
132  struct LuaTx *tx = SCLuaTestUserData(L, 1, dns_tx, SCLUA_TUD_DNS_TX);
133  if (tx == NULL) {
134  lua_pushnil(L);
135  return 1;
136  }
137  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
138  return SCDnsLuaGetAnswerTable(L, txptr);
139 }
140 
141 static int LuaDnsTxGetAuthorities(lua_State *L)
142 {
143  struct LuaTx *tx = SCLuaTestUserData(L, 1, dns_tx, SCLUA_TUD_DNS_TX);
144  if (tx == NULL) {
145  lua_pushnil(L);
146  return 1;
147  }
148  SCLUA_RESOLVE_TX(L, &tx->ref, txptr);
149  return SCDnsLuaGetAuthorityTable(L, txptr);
150 }
151 
152 static const struct luaL_Reg txlib[] = {
153  // clang-format off
154  { "answers", LuaDnsTxGetAnswers },
155  { "authorities", LuaDnsTxGetAuthorities },
156  { "queries", LuaDnsTxGetQueries },
157  { "rcode", LuaDnsTxGetRcode },
158  { "rcode_string", LuaDnsTxGetRcodeString },
159  { "recursion_desired", LuaDnsTxGetRecursionDesired },
160  { "rrname", LuaDnsTxGetRrname },
161  { "txid", LuaDnsTxGetTxid },
162  { NULL, NULL, }
163  // clang-format on
164 };
165 
166 static const struct luaL_Reg dnslib[] = {
167  // clang-format off
168  { "get_tx", LuaDnsGetTx },
169  { NULL, NULL,},
170  // clang-format on
171 };
172 
174 {
175  luaL_newmetatable(L, dns_tx);
176  lua_pushvalue(L, -1);
177  lua_setfield(L, -2, "__index");
178  luaL_setfuncs(L, txlib, 0);
179 
180  luaL_newlib(L, dnslib);
181  return 1;
182 }
LuaTx::ref
SCLuaTxRef ref
Definition: util-lua-dns.c:35
util-lua-common.h
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:47
SCLUA_TUD_DNS_TX
@ SCLUA_TUD_DNS_TX
Definition: util-lua-common.h:63
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
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
flags
uint8_t flags
Definition: decode-gre.h:0
LuaStateGetTxId
uint64_t LuaStateGetTxId(lua_State *luastate)
get tx id from the lua state
Definition: util-lua.c:240
suricata-common.h
util-lua-dns.h
SCLuaLoadDnsLib
int SCLuaLoadDnsLib(lua_State *L)
Definition: util-lua-dns.c:173
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
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:289