Coverage Report

Created: 2024-02-25 06:37

/src/ntopng/include/LuaEngine.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 * (C) 2013-24 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
#ifndef _LUA_H_
23
#define _LUA_H_
24
25
#include "ntop_includes.h"
26
/** @defgroup LuaEngine LuaEngine
27
 * Main ntopng lua group.
28
 */
29
30
/* ******************************* */
31
32
/** @class LuaEngine
33
 *  @brief Main class of lua.
34
 *
35
 *  @ingroup LuaEngine
36
 *
37
 */
38
39
class ThreadedActivity;
40
class ThreadedActivityStats;
41
42
class LuaEngine {
43
 protected:
44
  lua_State *L; /**< The LuaEngine state. */
45
  char *loaded_script_path;
46
  bool is_system_vm; /* Executed by callbacks */
47
  std::string cloud_string;
48
  std::set<std::string> requires;
49
  size_t mem_used;
50
  u_int32_t start_epoch;
51
  NtopngLuaContext *lua_context;
52
  
53
  void lua_register_classes(lua_State *L, LuaEngineMode mode);
54
55
 public:
56
  /**
57
   * @brief A Constructor
58
   * @details Creating a new lua state.
59
   *
60
   * @return A new instance of lua.
61
   */
62
  LuaEngine();
63
64
  /**
65
   * @brief A Destructor.
66
   *
67
   */
68
  virtual ~LuaEngine();
69
70
  /* Set Hosts and Networks into the Lua context */
71
  void setHost(Host *h);
72
  void setNetwork(NetworkStats *ns);
73
  void setFlow(Flow *f);
74
75
  void setThreadedActivityData(const ThreadedActivity *ta,
76
                               ThreadedActivityStats *tas, time_t deadline);
77
78
  Host *getHost();
79
  NetworkInterface* getNetworkInterface();
80
  NetworkStats*     getNetwork();
81
82
  int load_script(char *script_path, LuaEngineMode mode, NetworkInterface *iface);
83
  int run_loaded_script();
84
85
  /**
86
   * @brief Handling of request info of script.
87
   * @details Read from the request the parameters and put the GET parameters
88
   * and the _SESSION parameters into the environment. Once all parameters have
89
   * been load we running the script.
90
   *
91
   * @param conn This structure contains handle for the individual connection.
92
   * @param request_info This structure contains information about the HTTP
93
   * request.
94
   * @param script_path Full path of lua script.
95
   * @return The result of the execution of the script.
96
   */
97
  int handle_script_request(struct mg_connection *conn,
98
                            const struct mg_request_info *request_info,
99
                            char *script_path, bool *attack_attempt,
100
                            const char *user, const char *group,
101
                            const char *session_csrf, bool localuser);
102
103
  bool setParamsTable(lua_State *vm, const struct mg_request_info *request_info,
104
                      const char *table_name, const char *query) const;
105
106
  void luaRegister(lua_State *L, const char *class_name,
107
                   luaL_Reg *class_methods);
108
109
0
  inline lua_State *getState() const { return (L); }
110
111
  bool switchInterface(struct lua_State *vm, const char *ifid,
112
                       const char *observation_point_id, const char *user,
113
                       const char *group, const char *session);
114
  void setInterface(const char *user, char *const ifname, u_int16_t ifname_len,
115
                    bool *const is_allowed) const;
116
117
0
  inline void setAsSystemVM() { is_system_vm = true; }
118
0
  inline bool isSystemVM() { return (is_system_vm); }
119
120
  void pushResultString(char *str);
121
  void pushResultNumber(float f);
122
0
  const char* getCloudString() { return(cloud_string.c_str()); }
123
0
  inline lua_State* getState() { return(L);                    }
124
125
0
  inline size_t getMemUsed()         { return(mem_used); }
126
0
  inline void   incMemUsed(size_t v) { mem_used += v;    }
127
  bool require(std::string name);
128
};
129
130
/**
131
 * @brief Push string value to table entry specify the key.
132
 *
133
 * @param L The lua state.
134
 * @param key The key of hash table.
135
 * @param value The value of hash table.
136
 */
137
extern void lua_push_str_table_entry(lua_State *L, const char *key,
138
                                     const char *value);
139
140
/**
141
 * @brief Push string value to table entry specify the key and lenght.
142
 *
143
 * @param L The lua state.
144
 * @param key The key of hash table.
145
 * @param value The value of hash table.
146
 * @param value_len lenght of the value field
147
 */
148
extern void lua_push_str_len_table_entry(lua_State *L, const char *key,
149
           const char *value,
150
           unsigned int value_len);
151
152
/**
153
 * @brief Push null value to table entry specify the key.
154
 *
155
 * @param L The lua state.
156
 * @param key The key of hash table.
157
 */
158
extern void lua_push_nil_table_entry(lua_State *L, const char *key);
159
160
/**
161
 * @brief Push int value to table entry specify the key.
162
 *
163
 * @param L The lua state.
164
 * @param key The key of hash table.
165
 * @param value The value of hash table.
166
 */
167
extern void lua_push_uint64_table_entry(lua_State *L, const char *key,
168
                                        u_int64_t value);
169
170
/**
171
 * @brief Push int64 value to table entry specify the key.
172
 *
173
 * @param L The lua state.
174
 * @param key The key of hash table.
175
 * @param value The value of hash table.
176
 */
177
void lua_push_int64_table_entry(lua_State *L, const char *key, int64_t value);
178
179
/**
180
 * @brief Push int32 value to table entry specify the key.
181
 *
182
 * @param L The lua state.
183
 * @param key The key of hash table.
184
 * @param value The value of hash table.
185
 */
186
void lua_push_int32_table_entry(lua_State *L, const char *key, int32_t value);
187
188
/**
189
 * @brief Push uint32 value to table entry specify the key.
190
 *
191
 * @param L The lua state.
192
 * @param key The key of hash table.
193
 * @param value The value of hash table.
194
 */
195
void lua_push_uint32_table_entry(lua_State *L, const char *key,
196
                                 u_int32_t value);
197
198
/**
199
 * @brief Push bool value to table entry specify the key.
200
 * @details Using LUA_NUMBER (double: 64 bit) in place of LUA_INTEGER
201
 * (ptrdiff_t: 32 or 64 bit according to the platform) to handle big counters.
202
 * (luaconf.h)
203
 *
204
 * @param L The lua state.
205
 * @param key The key of hash table.
206
 * @param value The value of hash table.
207
 */
208
extern void lua_push_bool_table_entry(lua_State *L, const char *key,
209
                                      bool value);
210
211
/**
212
 * @brief Push float value to table entry specify the key.
213
 *
214
 * @param L The lua state.
215
 * @param key The key of hash table.
216
 * @param value The value of hash table.
217
 */
218
extern void lua_push_float_table_entry(lua_State *L, const char *key,
219
                                       float value);
220
221
/**
222
 * @brief Push boolean value to table entry specify the key.
223
 *
224
 * @param L The lua state.
225
 * @param key The key of hash table.
226
 * @param value The value of hash table.
227
 */
228
extern void lua_push_bool_table_entry(lua_State *L, const char *key,
229
                                      bool value);
230
231
int ntop_lua_check(lua_State *vm, const char *func, int pos, int expected_type);
232
233
/**
234
 * @brief Checks the lua stack after a C function is called
235
 *
236
 * @param vm The lua state.
237
 * @param function_name The function name that triggered the error
238
 * @param val The return value
239
 */
240
extern int ntop_lua_return_value(lua_State *vm, const char *function_name,
241
                                 int val);
242
243
extern void get_host_vlan_info(char *lua_ip, char **host_ip, u_int16_t *vlan_id,
244
                               char *buf, u_int buf_len);
245
246
#endif /* _LUA_H_ */