mirror of
https://github.com/ntop/ntopng.git
synced 2026-10-23 06:50:17 +00:00
Rework the `cppcheck:` recipe so it is usable in practice:
- `--enable=all` -> `--enable=warning,performance,portability`
(drops the `style`/`information`/`unusedFunction` groups, which produced
hundreds of opinion-level hits)
- drop `--force` (was re-analyzing every file once per #ifdef combination;
single files took 40+ min). Analyzes the default build config only.
- add `-j $(shell nproc)` + `--cppcheck-build-dir=.cppcheck-build` for
parallelism and incremental caching; `mkdir -p` the cache dir in the recipe
- suppress checks that are noise for this codebase:
* useInitializationList - micro perf, POD members
* duplInheritedMember - intentional non-virtual name shadowing
(operator[], swap, enqueue, incUses, lua, ...)
* noCopyConstructor / noOperatorEq - the flagged classes
(NetworkInterface, Ntop, Flow, ...) are non-copyable by design and
always held by pointer; the fix cppcheck suggests (add operator=)
is meaningless for them
- .gitignore: ignore the new .cppcheck-build/ cache directory
FlowAlertsLoader::registerRisk (src/FlowAlertsLoader.cpp)
Missing `else`: `alert_to_risk[alert_type.id] = risk` was executed even when
`alert_type.id >= MAX_DEFINED_FLOW_ALERT_TYPE` -> out-of-bounds array write.
The sibling registerAlert() already had the `else`.
Uninitialized ndpi_protocol structs (src/NetworkInterface.cpp x6,
src/LuaEngineInterface.cpp x1)
Local `ndpi_protocol` values were only partially initialized (proto.* set,
but breed/fpc/state/category/custom_category_userdata left indeterminate)
and then passed by value to get_ndpi_full_proto_name() /
get_ndpi_proto_category(). Added `memset(&x, 0, sizeof(x))` at each
declaration, matching the existing convention elsewhere in the tree.
Uninitialized locals
- src/ParserInterface.cpp: `src2dst_direction` was read on the
`flow == NULL` path (getFlow() not called). Initialize to a defined value;
the value is not otherwise observable on that path.
- include/Bitmap.h (toHexString): if `buf_len <= 0` the first loop never
runs and `strlen(buf)` reads uninitialized memory; also `strlen(buf) - 2`
underflows (unsigned) when the string is shorter than 2 bytes. Null-
terminate up front and hoist strlen into a `size_t`, iterate with
`pos + 2 < len`.
src/NetworkInterface.cpp incNumHosts()/decNumHosts()
`if (isShuttingDown() || (!host->isUnicastHost())) return;` - the second
term is always false, the function already returned on `!isUnicastHost()`
a few lines above. Dropped it.
include/NetworkInterface.h getActiveFlowsStats()
Declaration and definition disagreed on the first two parameter *names*
(`stats, status_stats` vs `ndpi_stats, stats`); types matched. Renamed the
header params to match the implementation.
No behavioural change; each argument was already non-negative.
- Interface id (`get_id()` returns int; kept int because 3 call sites test
`== -1`) formatted with %u in redis-key macros -> cast the argument
`(u_int)` at the call site:
include/AutonomousSystem.h, include/Country.h,
include/ObservationPoint.h, include/VLAN.h,
src/BroadcastDomains.cpp (x2), src/HostPools.cpp, src/LocalHost.cpp,
src/LuaEngineInterface.cpp (x3), src/NetworkInterface.cpp (x2),
src/SyslogParserInterface.cpp
- src/HTTPserver.cpp: `%d` with get_http_port()/get_https_port() (u_int)
-> `%u`; the scanf-width builder `"%%%u[^|]..."` fed `int` macros -> `%d`
- src/Flow.cpp: `"%u.%u.%u.%u"` with `int` octets from a `%d` sscanf -> `%d`;
`%u` (arg 13) with get_protocol_category() enum -> cast `(u_int)`
- src/MacManufacturers.cpp: `int _mac[3]` scanned with `%02x`
(wants `unsigned int*`) -> declare `u_int _mac[3]`
- src/StatsManager.cpp: `%lu` with `static_cast<long int>(key)` epoch values
-> `%ld` (respects the explicit signed cast)
- src/Trace.cpp: `"%s.%u"` with loop `int i` / `i + 1` -> `%d`
- src/Prefs.cpp: `%u` in usage() help text fed CONST_DEFAULT_* /
MAX_NUM_* / CLICKHOUSE_MAX_* int macros -> cast `(u_int)`
std::string / std::vector parameters taken by value, changed to `const &`
(declaration and definition kept in sync; none are virtual or overrides, so
callers are source-compatible):
- AggregatedFlowsStats::setInfoKey
- Flow::setL7JSON
- CustomFlowLuaScriptAlert::setAlertMessage
- CustomHostLuaScriptAlert::setAlertMessage, ctor (_msg)
- ScanRealtimeAlert ctor (_alerts, std::vector)
- CustomHostLuaScript::allocAlert (_msg)
- OtherAlertableEntity::getAlertCachedValue / setAlertCacheValue /
triggerAlert / releaseAlert (key, value)
- HostAlert::init and HostAlert(HostCheckID, ...) ctor (_check_name)
- Ntop::luaFlowCheckInfo / luaHostCheckInfo (check_name)
- Ntop::incBlacklisHits (listname)
- Ntop::setLuaCache (key, val)
143 lines
4.8 KiB
C++
143 lines
4.8 KiB
C++
/*
|
|
*
|
|
* (C) 2013-26 - ntop.org
|
|
*
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
*
|
|
*/
|
|
|
|
#ifndef _AUTONOMOUS_SYSTEM_H_
|
|
#define _AUTONOMOUS_SYSTEM_H_
|
|
|
|
// #define AS_LATENCY_DEBUG 1
|
|
|
|
#include "ntop_includes.h"
|
|
|
|
struct ExporterMapCmp {
|
|
bool operator()(const std::pair<struct ndpi_in6_addr, u_int16_t>& a,
|
|
const std::pair<struct ndpi_in6_addr, u_int16_t>& b) const {
|
|
// Compare IP addresses first
|
|
int cmp = memcmp(&a.first, &b.first, sizeof(struct ndpi_in6_addr));
|
|
if (cmp != 0) return(cmp < 0);
|
|
|
|
// If IPs are the same, compare the other entry (u_int16_t)
|
|
return(a.second < b.second);
|
|
}
|
|
};
|
|
|
|
class AutonomousSystem : public GenericHashEntry,
|
|
public GenericTrafficElement,
|
|
public Score {
|
|
private:
|
|
u_int32_t asn;
|
|
char* asname;
|
|
u_int32_t round_trip_time;
|
|
u_int64_t ipv4_bytes, ipv6_bytes;
|
|
u_int32_t alerted_flows_as_client, alerted_flows_as_server;
|
|
bool save_exporters_stats; /* Core ASN for which stats need to be saved */
|
|
std::map<std::pair<struct ndpi_in6_addr, u_int16_t>, TrafficCounter, ExporterMapCmp> exporters_map;
|
|
#ifdef NTOPNG_PRO
|
|
QoEStats qoe_stats;
|
|
#endif
|
|
|
|
#if defined(NTOPNG_PRO)
|
|
time_t nextMinPeriodicUpdate;
|
|
#endif
|
|
|
|
void incExportersStats(u_int64_t bytes_sent, u_int64_t bytes_rcvd,
|
|
struct ndpi_in6_addr *exporter_ip, u_int32_t in_index,
|
|
u_int32_t out_index);
|
|
|
|
inline void incSentStats(time_t t, u_int64_t num_pkts, u_int64_t num_bytes) {
|
|
if (first_seen == 0)
|
|
first_seen = t, last_seen = iface->getTimeLastPktRcvd();
|
|
sent.incStats(t, num_pkts, num_bytes);
|
|
}
|
|
inline void incRcvdStats(time_t t, u_int64_t num_pkts, u_int64_t num_bytes) {
|
|
rcvd.incStats(t, num_pkts, num_bytes);
|
|
}
|
|
|
|
#ifdef NTOPNG_PRO
|
|
void updateBehaviorStats(const struct timeval* tv);
|
|
#endif
|
|
|
|
public:
|
|
AutonomousSystem(NetworkInterface* _iface, IpAddress* ipa);
|
|
~AutonomousSystem();
|
|
|
|
void set_hash_entry_state_idle();
|
|
|
|
inline u_int32_t getNumHosts() { return getUses(); }
|
|
inline u_int32_t key() { return (asn); }
|
|
inline u_int32_t get_asn() { return (asn); }
|
|
inline char* get_asname() { return (asname ? asname : (char*)""); }
|
|
|
|
bool equal(u_int32_t asn);
|
|
|
|
inline void incStats(bool is_ipv4_traffic, time_t when, u_int16_t proto_id, u_int64_t sent_packets,
|
|
u_int64_t sent_bytes, u_int64_t rcvd_packets,
|
|
u_int64_t rcvd_bytes, struct ndpi_in6_addr *exporter_ip,
|
|
u_int32_t in_index, u_int32_t out_index) {
|
|
if (ndpiStats || (ndpiStats = new nDPIStats()))
|
|
ndpiStats->incStats(when, proto_id, sent_packets, sent_bytes,
|
|
rcvd_packets, rcvd_bytes);
|
|
incSentStats(when, sent_packets, sent_bytes);
|
|
incRcvdStats(when, rcvd_packets, rcvd_bytes);
|
|
incExportersStats(sent_bytes, rcvd_bytes, exporter_ip, out_index, in_index);
|
|
|
|
if(is_ipv4_traffic)
|
|
ipv4_bytes += sent_bytes + rcvd_bytes;
|
|
else
|
|
ipv6_bytes += sent_bytes + rcvd_bytes;
|
|
}
|
|
|
|
inline void incNumAlertedFlows(bool as_client) {
|
|
if (as_client)
|
|
alerted_flows_as_client++;
|
|
else
|
|
alerted_flows_as_server++;
|
|
};
|
|
|
|
void updateRoundTripTime(u_int32_t rtt_msecs);
|
|
void lua(lua_State* vm, DetailsLevel details_level, bool asListElement,
|
|
bool diff = false);
|
|
|
|
virtual void updateStats(const struct timeval* tv);
|
|
|
|
inline char* getSerializationKey(char* buf, u_int bufsize) {
|
|
snprintf(buf, bufsize, AS_SERIALIZED_KEY, (u_int)iface->get_id(), asn);
|
|
return (buf);
|
|
}
|
|
inline u_int32_t getTotalAlertedNumFlowsAsClient() const {
|
|
return (alerted_flows_as_client);
|
|
};
|
|
inline u_int32_t getTotalAlertedNumFlowsAsServer() const {
|
|
return (alerted_flows_as_server);
|
|
};
|
|
inline void saveExporterStatsPrefs(bool _save_exporters_stats) {
|
|
save_exporters_stats = _save_exporters_stats;
|
|
|
|
if (!save_exporters_stats)
|
|
exporters_map.clear(); /* Empty the map in case it's present */
|
|
}
|
|
void findExportersStats(u_int64_t bytes_sent, u_int64_t bytes_rcvd,
|
|
std::pair<struct ndpi_in6_addr, u_int16_t>* key);
|
|
#ifdef NTOPNG_PRO
|
|
void incQoEStats(QoEType qoe_type) { qoe_stats.incQoEStats(qoe_type); };
|
|
#endif
|
|
};
|
|
|
|
#endif /* _AUTONOMOUS_SYSTEM_H_ */
|