Coverage Report

Created: 2025-05-16 06:24

/src/ntopng/include/Utils.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 * (C) 2013-25 - 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 _UTILS_H_
23
#define _UTILS_H_
24
25
#include "ntop_includes.h"
26
27
typedef unsigned long long ticks;
28
29
#ifdef WIN32
30
#define _usleep(a) win_usleep(a)
31
#else
32
4
#define _usleep(a) usleep(a)
33
#endif
34
35
/* ******************************* */
36
37
class Utils {
38
 private:
39
  static bool validInterfaceName(const char *name);
40
  static bool validInterfaceDescription(const char *description);
41
  static bool validInterface(const pcap_if_t *pcap_if);
42
43
 public:
44
3.45k
  static inline bool isEmptyString(const char *s) { return (!s || !s[0]); }
45
  static char *toLowerResolvedNames(const char *const name);
46
  static char *jsonLabel(int label, const char *label_str, char *buf,
47
                         u_int buf_len);
48
  static char *formatTraffic(float numBits, bool bits, char *buf,
49
                             u_int buf_len);
50
  static char *formatPackets(float numPkts, char *buf, u_int buf_len);
51
  static const char *edition2name(NtopngEdition ntopng_edition);
52
  static char *l4proto2name(u_int8_t proto);
53
  static u_int8_t l4name2proto(const char *name);
54
  static u_int8_t queryname2type(const char *name);
55
  static bool isIPAddress(const char *ip);
56
  static void splitAddressAndVlan(char *addr, u_int16_t *vlan_id);
57
  static const bool isIpEmpty(ipAddress addr);
58
#ifdef __linux__
59
  static int setAffinityMask(char *cores_list, cpu_set_t *mask);
60
  static int setThreadAffinityWithMask(pthread_t thread, cpu_set_t *mask);
61
#endif
62
  static int setThreadAffinity(pthread_t thread, int core_id);
63
  static void setThreadName(const char *name);
64
  static char *trim(char *s);
65
  static u_int32_t hashString(const char *s,
66
                              u_int32_t len = 0 /* automatically computed */);
67
  static float timeval2ms(const struct timeval *tv);
68
#ifdef PROFILING
69
  static u_int64_t getTimeNsec();
70
#endif
71
  static float msTimevalDiff(const struct timeval *end,
72
                             const struct timeval *begin);
73
  static u_int32_t usecTimevalDiff(const struct timeval *end,
74
                                   const struct timeval *begin);
75
  /* Returns the difference new_value - cur_value and then stores new_value in
76
   * *cur_value */
77
  template <typename T>
78
  static inline T uintDiff(T *cur_value, T new_value) {
79
    T res = new_value > *cur_value ? new_value - *cur_value : 0;
80
    *cur_value = new_value;
81
    return res;
82
  };
83
  static size_t file_write(const char *path, const char *content,
84
                           size_t content_len);
85
  static size_t file_read(const char *path, char **content);
86
  static bool file_exists(const char *path);
87
  static bool dir_exists(const char *path);
88
  static int8_t num_files_in_dir(const char *dir);
89
  static bool mkdir_tree(char *const path);
90
  static int mkdir(const char *pathname, mode_t mode);
91
  static int remove_recursively(const char *path);
92
  static const char *trend2str(ValueTrend t);
93
  static int dropPrivileges();
94
  static char *base64_encode(unsigned char const *bytes_to_encode,
95
                             ssize_t in_len);
96
  static std::string base64_decode(std::string const &encoded_string);
97
  static double pearsonValueCorrelation(activity_bitmap *x, activity_bitmap *y);
98
  static double JaccardSimilarity(activity_bitmap *x, activity_bitmap *y);
99
  static int ifname2id(const char *name);
100
  static char *stringtolower(char *str);
101
  static std::string list2JsonArray(const char *s);
102
  static char *sanitizeHostName(char *str);
103
  static char *urlDecode(const char *src, char *dst, u_int dst_len);
104
  static bool isValidUTF8(const u_char *param, size_t length);
105
  static bool purifyHTTPparam(char *const param, bool strict, bool allowURL,
106
                              bool allowDots);
107
  static char *stripHTML(const char *str);
108
  static bool sendTCPData(char *host, int port, char *data, int timeout);
109
  static bool sendUDPData(char *host, int port, char *data);
110
  static bool postHTTPJsonData(char *bearer_token, char *username,
111
                               char *password, char *url, char *json,
112
             int connect_timeout,
113
             int max_duration_timeout,
114
             HTTPTranferStats *stats);
115
  static bool postHTTPJsonData(char *bearer_token, char *username,
116
                               char *password, char *url, char *json,
117
             int connect_timeout,
118
             int max_duration_timeout,
119
             HTTPTranferStats *stats,
120
                               char *return_data, int return_data_size,
121
                               int *response_code);
122
  static bool sendMail(lua_State *vm, char *from, char *to, char *cc,
123
                       char *message, char *smtp_server, char *username,
124
                       char *password, bool use_proxy, bool verbose);
125
  static bool postHTTPTextFile(lua_State *vm, char *username, char *password,
126
                               char *url, char *path,
127
             int connect_timeout,
128
             int max_duration_timeout,
129
                               HTTPTranferStats *stats);
130
  static bool httpGetPost(lua_State *vm, char *url, char *username,
131
                          char *password, char *user_header_token,
132
        int connect_timeout, int max_duration_timeout,        
133
                          bool return_content, bool use_cookie_authentication,
134
                          HTTPTranferStats *stats, const char *form_data,
135
                          char *write_fname, bool follow_redirects,
136
                          int ip_version, bool use_put_method = false);
137
  static long httpGet(const char *url, const char *username,
138
                      const char *password, const char *user_header_token,
139
                      int connect_timeout, int max_duration_timeout,
140
           char *const resp, const u_int resp_len);
141
  static bool progressCanContinue(ProgressState *progressState);
142
  static char *urlEncode(const char *url);
143
  static ticks getticks();
144
  static ticks gettickspersec();
145
  static char *getURL(char *url, char *buf, u_int buf_len);
146
  static bool discardOldFilesExceeding(const char *path,
147
                                       const unsigned long max_size);
148
  static u_int64_t macaddr_int(const u_int8_t *mac);
149
  static char *ifname2devname(const char *ifname, char *devname,
150
                              int devname_size);
151
  static void readMac(const char *ifname, dump_mac_t mac_addr);
152
  static u_int32_t readIPv4(char *ifname);
153
  static bool readIPv6(char *ifname, struct in6_addr *sin);
154
  static u_int32_t getMaxIfSpeed(const char *ifname);
155
  static u_int16_t getIfMTU(const char *ifname);
156
  static int ethtoolGet(const char *ifname, int cmd, uint32_t *v);
157
  static int ethtoolSet(const char *ifname, int cmd, uint32_t v);
158
  static int disableOffloads(const char *ifname);
159
  static bool isGoodNameToCategorize(char *name);
160
  static char *get2ndLevelDomain(char *_domainname);
161
  static char *tokenizer(char *arg, int c, char **data);
162
  static in_addr_t inet_addr(const char *cp);
163
  static char *intoaV4(unsigned int addr, char *buf, u_short bufLen);
164
  static char *intoaV6(struct ndpi_in6_addr ipv6, u_int8_t bitmask, char *buf,
165
                       u_short bufLen);
166
  static u_int64_t timeval2usec(const struct timeval *tv);
167
  static void xor_encdec(u_char *data, int data_len, u_char *key);
168
  static bool isPrintableChar(u_char c);
169
  static char *formatMacAddress(const u_int8_t *const mac, char *buf, u_int buf_len);
170
  static char *formatMac(const u_int8_t *const mac, char *buf, u_int buf_len);
171
  static u_int64_t encodeMacTo64(u_int8_t mac[6]);
172
  static void decode64ToMac(u_int64_t mac64, u_int8_t mac[6] /* out */);
173
  static void parseMac(u_int8_t *mac, const char *symMac);
174
  static u_int32_t macHash(const u_int8_t *const mac);
175
  static bool isEmptyMac(const u_int8_t *const mac);
176
  static bool isSpecialMac(u_int8_t *mac);
177
0
  inline static bool isBroadMulticastMac(const u_int8_t *mac) {
178
0
    return (isBroadcastMac(mac) || isMulticastMac(mac));
179
0
  }
180
  static bool isBroadcastMac(const u_int8_t *mac);
181
  static bool isMulticastMac(const u_int8_t *mac);
182
  static int numberOfSetBits(u_int32_t i);
183
  static void initRedis(Redis **r, const char *redis_host,
184
                        const char *redis_password, u_int16_t redis_port,
185
                        u_int8_t _redis_db_id, bool giveup_on_failure);
186
  static json_object *cloneJSONSimple(json_object *src);
187
188
  /* ScriptPeriodicity */
189
  static const char *periodicityToScriptName(ScriptPeriodicity p);
190
  static int periodicityToSeconds(ScriptPeriodicity p);
191
192
  /* eBPF-related */
193
  static int tcpStateStr2State(const char *state_str);
194
  static const char *tcpState2StateStr(int state);
195
  static eBPFEventType eBPFEventStr2Event(const char *event_str);
196
  static const char *eBPFEvent2EventStr(eBPFEventType event);
197
198
  static bool str2DetailsLevel(const char *details, DetailsLevel *out);
199
  static u_int32_t roundTime(u_int32_t now, u_int32_t rounder,
200
                             int32_t offset_from_utc);
201
  static bool isCriticalNetworkProtocol(u_int16_t protocol_id);
202
  static u_int32_t stringHash(const char *s);
203
  static const char *policySource2Str(L7PolicySource_t policy_source);
204
  static const char *captureDirection2Str(pcap_direction_t dir);
205
  static bool readInterfaceStats(const char *ifname, ProtoStats *in_stats,
206
                                 ProtoStats *out_stats);
207
  static bool shouldResolveHost(const char *host_ip);
208
  static bool mg_write_retry(struct mg_connection *conn, u_char *b, int len);
209
  static bool parseAuthenticatorJson(HTTPAuthenticator *auth, char *content);
210
  static void freeAuthenticator(HTTPAuthenticator *auth);
211
  static DetailsLevel bool2DetailsLevel(bool max, bool higher,
212
                                        bool normal = false);
213
214
  /* Patricia Tree */
215
  static ndpi_patricia_node_t *add_to_ptree(ndpi_patricia_tree_t *tree,
216
                                            int family, void *addr, int bits);
217
  static ndpi_patricia_node_t *ptree_match(ndpi_patricia_tree_t *tree,
218
                                           int family, const void *const addr,
219
                                           int bits);
220
  static ndpi_patricia_node_t *ptree_add_rule(ndpi_patricia_tree_t *ptree,
221
                                              const char *line);
222
  static bool ptree_prefix_print(ndpi_prefix_t *prefix, char *buffer,
223
                                 size_t bufsize);
224
225
  static inline void update_ewma(u_int32_t sample, u_int32_t *ewma,
226
175
                                 u_int8_t alpha_percent) {
227
175
    if (alpha_percent > 100) alpha_percent = 100;
228
175
    if (!ewma) return;
229
175
    (*ewma) = (alpha_percent * sample + (100 - alpha_percent) * (*ewma)) / 100;
230
175
  }
231
0
  static inline u_int64_t toUs(struct timeval *t) {
232
0
    return (((u_int64_t)t->tv_sec) * 1000000 + ((u_int64_t)t->tv_usec));
233
0
  };
234
  static void replacestr(char *line, const char *search, const char *replace);
235
  static u_int32_t getHostManagementIPv4Address();
236
  static bool isInterfaceUp(char *ifname);
237
  static bool maskHost(bool isLocalIP);
238
  static char *getInterfaceDescription(char *ifname, char *buf, int buf_len);
239
  static int bindSockToDevice(int sock, int family, const char *devicename);
240
  static bool execCmd(char *cmd, std::string *out);
241
  static void maximizeSocketBuffer(int sock_fd, bool rx_buffer, u_int max_buf_mb);
242
  static u_int32_t parsetime(char *str);
243
  static time_t str2epoch(const char *str);
244
  static u_int64_t mac2int(const u_int8_t *mac);
245
  static u_int8_t *int2mac(u_int64_t mac, u_int8_t *buf);
246
  static void listInterfaces(lua_State *vm);
247
  static bool validInterface(const ntop_if_t *ntop_if);
248
  static void containerInfoLua(lua_State *vm, const ContainerInfo *const cont);
249
  static char *ntop_lookupdev(char *ifname_out, int ifname_size);
250
  static int get_ifindex(const char *ifname);
251
  static char *get_real_name(const char *ifname_alias);
252
  /**
253
   * @brief Return all the available interfaces
254
   * @details Return all the available interfaces, unifying data from PF_RING
255
   * and pcap, and excluding invalid interfaces Interfaces are returned as a
256
   * linked-list in the **alldevsp parameter.
257
   *
258
   * @return returns 0 on success and -1 on failure
259
   */
260
  static int ntop_findalldevs(ntop_if_t **alldevsp);
261
  /**
262
   * @brief Free data returned with `Utils::ntop_findalldevs`
263
   * @details Frees data allocated during the call to `Utils::ntop_findalldevs`
264
   *
265
   * @return void
266
   */
267
  static void ntop_freealldevs(ntop_if_t *alldevs);
268
269
  /* System Host Montoring and Diagnose Functions */
270
  static bool getCPULoad(cpu_load_stats *out);
271
  static void luaMeminfo(lua_State *vm);
272
  static int retainWriteCapabilities();
273
  static int gainWriteCapabilities();
274
  static int dropWriteCapabilities();
275
  static u_int32_t findInterfaceGatewayIPv4(const char *ifname);
276
277
  /* Data Format */
278
  static char *formatTraffic(float numBits, bool bits, char *buf);
279
  static char *formatPackets(float numPkts, char *buf);
280
281
  /* Pcap files utiles */
282
  static void init_pcap_header(struct pcap_file_header *const h, int linktype,
283
                               int snaplen, bool nsec = false);
284
285
  /* Bitmap functions */
286
  static bool bitmapIsSet(u_int64_t bitmap, u_int8_t v);
287
  static u_int64_t bitmapSet(u_int64_t bitmap, u_int8_t v);
288
  static u_int64_t bitmapClear(u_int64_t bitmap, u_int8_t v);
289
  static int bitmapGetNext(u_int64_t bitmap, u_int8_t start);
290
291
0
  static inline u_int64_t bitmapOr(u_int64_t bitmap1, u_int64_t bitmap2) {
292
0
    return (bitmap1 | bitmap2);
293
0
  }
294
295
  static ndpi_os getOSFromFingerprint(const char *fingerprint, const char *manuf,
296
                                     DeviceType devtype);
297
  static DeviceType getDeviceTypeFromOsDetail(const char *os_detail,
298
                ndpi_os *hint);
299
  static ndpi_os OShint2ndpi_os(ndpi_os os);
300
  static u_int32_t pow2(u_int32_t v);
301
  static int exec(const char *command);
302
#ifdef __linux__
303
  static void deferredExec(const char *command);
304
#endif
305
  static void tlv2lua(lua_State *vm, ndpi_serializer *serializer);
306
  static void tlv2serializer(ndpi_serializer *tvl_serializer, ndpi_serializer *serializer);
307
  static u_int16_t countryCode2U16(const char *country_code);
308
  static char *countryU162Code(u_int16_t country_u16, char *country_code,
309
                               size_t country_code_size);
310
  static bool isNumber(const char *s, unsigned int s_len, bool *is_float);
311
  static bool isPingSupported();
312
  static ScoreCategory mapAlertToScoreCategory(AlertCategory check_category);
313
  /* Map alert score to AlertLevel */
314
  static AlertLevel mapScoreToSeverity(u_int32_t score);
315
  /* Map AlertLevel to score */
316
  static u_int8_t mapSeverityToScore(AlertLevel alert_level);
317
  /*
318
    Maps an AlertLevel into the corresponding AlertLevelGroup. Alert level
319
    groups are used to 'compress' alert levels into a reduced number of
320
    (grouped) levels.
321
   */
322
  static AlertLevelGroup mapAlertLevelToGroup(AlertLevel alert_level);
323
  static bool hasExtension(const char *path, const char *ext);
324
#ifndef WIN32
325
  static int mapSyslogFacilityTextToValue(const char *facility_text);
326
#endif
327
  static void buildSqliteAllowedNetworksFilters(lua_State *vm);
328
  static void make_session_key(char *buf, u_int buf_len);
329
  static const char *get_state_label(ThreadedActivityState ta_state);
330
  static bool endsWith(const char *base, const char *str);
331
  static int openSocket(int domain, int type, int protocol, const char *label);
332
  static void closeSocket(int socket);
333
  static const char **getMessagingTopics();
334
  static char *toHex(char *in, u_int in_len, char *out, u_int out_len);
335
  static bool fromHex(char *in, u_int in_len, char *out, u_int out_len);
336
337
  static void swap8(u_int8_t *a, u_int8_t *b);
338
  static void swap16(u_int16_t *a, u_int16_t *b);
339
  static void swap32(u_int32_t *a, u_int32_t *b);
340
  static void swap64(u_int64_t *a, u_int64_t *b);
341
  static void swapfloat(float *a, float *b);
342
  static void swapLen(void *a, void *b, u_int len);
343
  static char* createRandomString(char *buf, size_t buf_len);
344
  static IpAddress* parseHostString(char *host_ip, u_int16_t *vlan_id /* out */);
345
  static bool nwInterfaceExists(char *if_name);
346
  static bool readModbusDeviceInfo(char *ip_address, u_int8_t timeout_sec, lua_State *vm);
347
  static bool readEthernetIPDeviceInfo(char *ip_address, u_int8_t timeout_sec, lua_State *vm);
348
  static const char* OS2Str(ndpi_os os);
349
  static const char* learningMode2str(OSLearningMode mode);
350
  static const char* deviceType2str(DeviceType devtype);
351
  static bool checkNetworkList(char *network_list, char *rsp,
352
             bool (*callback)(char *, char *, void *user_data), void *user_data);
353
354
  static DeviceType osType2deviceType(ndpi_os t);
355
};
356
357
#endif /* _UTILS_H_ */