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 {
35  DNSTransaction *tx;
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 = (struct LuaTx *)lua_newuserdata(L, sizeof(*ltx));
48  if (ltx == NULL) {
49  return LuaCallbackError(L, "error: fail to allocate user data");
50  }
51  ltx->tx = tx;
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 = luaL_testudata(L, 1, dns_tx);
62  if (tx == NULL) {
63  lua_pushnil(L);
64  return 1;
65  }
66  return SCDnsLuaGetRrname(L, tx->tx);
67 }
68 
69 static int LuaDnsTxGetTxid(lua_State *L)
70 {
71  struct LuaTx *tx = luaL_testudata(L, 1, dns_tx);
72  if (tx == NULL) {
73  lua_pushnil(L);
74  return 1;
75  }
76  return SCDnsLuaGetTxId(L, tx->tx);
77 }
78 
79 static int LuaDnsTxGetRcode(lua_State *L)
80 {
81  struct LuaTx *tx = luaL_testudata(L, 1, dns_tx);
82  if (tx == NULL) {
83  lua_pushnil(L);
84  return 1;
85  }
86  return SCDnsLuaGetRcode(L, tx->tx);
87 }
88 
89 static int LuaDnsTxGetRcodeString(lua_State *L)
90 {
91  struct LuaTx *tx = luaL_testudata(L, 1, dns_tx);
92  if (tx == NULL) {
93  lua_pushnil(L);
94  return 1;
95  }
96  return SCDnsLuaGetRcodeString(L, tx->tx);
97 }
98 
99 static int LuaDnsTxGetRecursionDesired(lua_State *L)
100 {
101  struct LuaTx *tx = luaL_testudata(L, 1, dns_tx);
102  if (tx == NULL) {
103  lua_pushnil(L);
104  return 1;
105  }
106  uint16_t flags = SCDnsTxGetResponseFlags(tx->tx);
107  int recursion_desired = flags & 0x0080 ? 1 : 0;
108  lua_pushboolean(L, recursion_desired);
109  return 1;
110 }
111 
112 static int LuaDnsTxGetQueries(lua_State *L)
113 {
114  struct LuaTx *tx = luaL_testudata(L, 1, dns_tx);
115  if (tx == NULL) {
116  lua_pushnil(L);
117  return 1;
118  }
119  return SCDnsLuaGetQueryTable(L, tx->tx);
120 }
121 
122 static int LuaDnsTxGetAnswers(lua_State *L)
123 {
124  struct LuaTx *tx = luaL_testudata(L, 1, dns_tx);
125  if (tx == NULL) {
126  lua_pushnil(L);
127  return 1;
128  }
129  return SCDnsLuaGetAnswerTable(L, tx->tx);
130 }
131 
132 static int LuaDnsTxGetAuthorities(lua_State *L)
133 {
134  struct LuaTx *tx = luaL_testudata(L, 1, dns_tx);
135  if (tx == NULL) {
136  lua_pushnil(L);
137  return 1;
138  }
139  return SCDnsLuaGetAuthorityTable(L, tx->tx);
140 }
141 
142 static const struct luaL_Reg txlib[] = {
143  // clang-format off
144  { "answers", LuaDnsTxGetAnswers },
145  { "authorities", LuaDnsTxGetAuthorities },
146  { "queries", LuaDnsTxGetQueries },
147  { "rcode", LuaDnsTxGetRcode },
148  { "rcode_string", LuaDnsTxGetRcodeString },
149  { "recursion_desired", LuaDnsTxGetRecursionDesired },
150  { "rrname", LuaDnsTxGetRrname },
151  { "txid", LuaDnsTxGetTxid },
152  { NULL, NULL, }
153  // clang-format on
154 };
155 
156 static const struct luaL_Reg dnslib[] = {
157  // clang-format off
158  { "get_tx", LuaDnsGetTx },
159  { NULL, NULL,},
160  // clang-format on
161 };
162 
164 {
165  luaL_newmetatable(L, dns_tx);
166  lua_pushvalue(L, -1);
167  lua_setfield(L, -2, "__index");
168  luaL_setfuncs(L, txlib, 0);
169 
170  luaL_newlib(L, dnslib);
171  return 1;
172 }
util-lua-common.h
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:47
util-lua.h
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:59
LuaTx::tx
DNSTransaction * tx
Definition: util-lua-dns.c:35
rust.h
lua_State
struct lua_State lua_State
Definition: suricata-common.h:515
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
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
util-lua-dns.h
SCLuaLoadDnsLib
int SCLuaLoadDnsLib(lua_State *L)
Definition: util-lua-dns.c:163
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:560