Coverage Report

Created: 2025-05-16 06:24

/src/ntopng/include/ZMQParserInterface.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 _ZMQ_PARSER_INTERFACE_H_
23
#define _ZMQ_PARSER_INTERFACE_H_
24
25
#include "ntop_includes.h"
26
27
class ZMQParserInterface : public ParserInterface {
28
 private:
29
  typedef std::pair<u_int32_t, u_int32_t> pen_value_t;
30
  typedef std::pair<string, bool> description_value_t; /* < Description , Native Value > */
31
  typedef std::map<string, pen_value_t> labels_map_t;
32
  typedef std::map<pen_value_t, description_value_t> descriptions_map_t;
33
  typedef std::map<string, u_int32_t> counters_map_t;
34
  std::unordered_map<u_int32_t, bool> cloud_flow_exporters;
35
  u_int16_t top_vlan_id;
36
  std::unordered_map<std::string, u_int16_t> name_to_vlan;
37
  labels_map_t labels_map; /* Contains mappings between labels and integer IDs
38
                              (PEN and ID) */
39
  counters_map_t counters_map; /* Contains mappings between sFlow counter field
40
                                  labels and integer IDs */
41
  descriptions_map_t descriptions_map; /* Contains mappings between integer IDs
42
                                          and descriptions */
43
  u_int32_t polling_start_time;
44
  bool once, is_sampled_traffic;
45
  u_int32_t flow_max_idle, returned_flow_max_idle;
46
  u_int64_t zmq_initial_bytes, zmq_initial_pkts, zmq_initial_drops,
47
      zmq_remote_initial_exported_flows;
48
  std::map<u_int32_t, nProbeStats *> source_id_last_zmq_remote_stats;
49
  nProbeStats *zmq_remote_stats, *zmq_remote_stats_shadow;
50
  u_int32_t remote_lifetime_timeout, remote_idle_timeout;
51
  struct timeval last_zmq_remote_stats_update;
52
  RwLock lock;
53
#ifdef NTOPNG_PRO
54
  CustomAppMaps *custom_app_maps;
55
#endif
56
57
  void exporterLuaStats(lua_State *vm, nProbeStats *zrs);
58
  void loadVLANMappings();
59
  u_int16_t findVLANMapping(std::string name);
60
  
61
  bool preprocessFlow(ParsedFlow *flow);
62
  void addMapping(const char *sym, u_int32_t num, u_int32_t pen = 0,
63
                  const char *descr = NULL);
64
  void addCounterMapping(const char *sym, u_int32_t id);
65
  bool parsePENZeroField(ParsedFlow *const flow, u_int32_t field,
66
                         ParsedValue *value);
67
  bool parsePENNtopField(ParsedFlow *const flow, u_int32_t field,
68
                         ParsedValue *value);
69
  bool matchPENZeroField(ParsedFlow *const flow, u_int32_t field,
70
                         ParsedValue *value);
71
  bool matchPENNtopField(ParsedFlow *const flow, u_int32_t field,
72
                         ParsedValue *value);
73
  static bool parseContainerInfo(json_object *jo,
74
                                 ContainerInfo *const container_info);
75
  static void freeContainerInfo(ContainerInfo *const container_info);
76
  void addMappingFromRedis();
77
  bool parseNProbeAgentField(ParsedFlow *const flow, const char *key,
78
                             ParsedValue *value,
79
                             json_object *const jvalue);
80
  int parseSingleJSONFlow(json_object *o, u_int32_t source_id);
81
  int parseSingleTLVFlow(ndpi_deserializer *deserializer, u_int32_t source_id);
82
  void setFieldMap(const ZMQ_FieldMap *const field_map) const;
83
  void setFieldValueMap(const ZMQ_FieldValueMap *const field_value_map) const;
84
85
  u_int8_t parseOptionFieldMap(json_object *const jo);
86
  u_int8_t parseOptionFieldValueMap(json_object *const jo);
87
88
 protected:
89
  struct {
90
    u_int32_t num_flows,   /* flows processed */
91
        num_dropped_flows, /* flows unhandles (received but no room in the flow hash) */
92
        num_events, num_counters, num_hello, num_listening_ports, num_templates,
93
        num_options, num_network_events, num_snmp_interfaces, zmq_msg_rcvd,
94
        zmq_msg_drops;
95
  } recvStats, recvStatsCheckpoint;
96
0
  inline void updateFlowMaxIdle() {
97
0
    returned_flow_max_idle = max(remote_idle_timeout, flow_max_idle);
98
0
  }
99
100
 public:
101
  ZMQParserInterface(const char *endpoint,
102
                     const char *custom_interface_type = NULL);
103
  ~ZMQParserInterface();
104
105
0
  virtual InterfaceType getIfType() const { return (interface_type_ZMQ); }
106
0
  virtual bool isSampledTraffic() const { return (is_sampled_traffic); }
107
108
  bool getKeyId(char *sym, u_int32_t sym_len, u_int32_t *const pen,
109
                u_int32_t *const field) const;
110
  const char *getKeyDescription(u_int32_t pen, u_int32_t field) const;
111
  void luaGetAllKeyDescription(lua_State *vm);
112
  bool matchField(ParsedFlow *const flow, const char *key, ParsedValue *value);
113
114
  bool getCounterId(char *sym, u_int32_t sym_len, u_int32_t *id) const;
115
116
  u_int8_t parseJSONFlow(const char *payload, int payload_size,
117
                         u_int32_t source_id, u_int32_t msg_id);
118
  u_int8_t parseTLVFlow(const char *payload, int payload_size,
119
                        u_int32_t source_id, u_int32_t msg_id, void *data);
120
  u_int8_t parseEvent(const char *payload, int payload_size, u_int32_t source_id,
121
                      u_int32_t msg_id, void *data);
122
  u_int8_t parseTLVCounter(const char *payload, int payload_size);
123
  u_int8_t parseJSONCounter(const char *payload, int payload_size);
124
  u_int8_t parseJSONCustomIE(const char *payload, int payload_size);
125
  u_int8_t parseTemplate(const char *payload, int payload_size,
126
                         u_int32_t source_id, u_int32_t msg_id, void *data);
127
  u_int8_t parseOption(const char *payload, int payload_size,
128
                       u_int32_t source_id, u_int32_t msg_id, void *data);
129
  u_int8_t parseListeningPorts(const char *payload, int payload_size,
130
                               u_int32_t source_id, u_int32_t msg_id,
131
                               void *data);
132
  u_int8_t parseSNMPIntefaces(const char *payload, int payload_size,
133
                              u_int32_t source_id, u_int32_t msg_id, void *data);
134
135
  u_int32_t periodicStatsUpdateFrequency() const;
136
  virtual void setRemoteStats(nProbeStats *zrs);
137
#ifdef NTOPNG_PRO
138
  virtual bool getCustomAppDetails(u_int32_t remapped_app_id,
139
                                   u_int32_t *const pen,
140
                                   u_int32_t *const app_field,
141
                                   u_int32_t *const app_id);
142
#endif
143
0
  u_int32_t getNumDroppedPackets() {
144
0
    return zmq_remote_stats ? zmq_remote_stats->sflow_pkt_sample_drops : 0;
145
0
  };
146
  virtual void lua(lua_State *vm, bool fullStats);
147
  virtual void probeLuaStats(lua_State *vm);
148
0
  inline u_int32_t getFlowMaxIdle() { return (returned_flow_max_idle); }
149
};
150
151
#endif /* _ZMQ_PARSER_INTERFACE_H_ */