From 112bf29b9ec512942fa9f734d1806c2f3d4618c3 Mon Sep 17 00:00:00 2001 From: Alfredo Cardigliano Date: Fri, 9 Aug 2024 18:00:13 +0200 Subject: [PATCH] Make code more clear --- include/Host.h | 2 +- include/LocalHost.h | 2 +- src/Host.cpp | 31 ++++++++++++------------------- src/HostStats.cpp | 17 +++++------------ src/LocalHost.cpp | 6 +++--- src/RemoteHost.cpp | 2 +- 6 files changed, 23 insertions(+), 37 deletions(-) diff --git a/include/Host.h b/include/Host.h index 70a1da8f30fb..8e5cf54ec872 100644 --- a/include/Host.h +++ b/include/Host.h @@ -908,7 +908,7 @@ class Host : public GenericHashEntry, inline void blacklistHost(char *blacklist_name) { setBlacklistName(blacklist_name); } inline char* getBlacklistName() { return(blacklist_name); } - virtual void setRxOnlyHost(bool set_it); + virtual void toggleRxOnlyHost(bool rx_only); inline bool resetHostTopSites() { if (stats) { stats->resetTopSitesData(); diff --git a/include/LocalHost.h b/include/LocalHost.h index 035e93d4dfd7..354fafb780c4 100644 --- a/include/LocalHost.h +++ b/include/LocalHost.h @@ -78,7 +78,7 @@ class LocalHost : public Host { if (network) network->updateRoundTripTime(rtt_msecs); } - void setRxOnlyHost(bool set_it); + void toggleRxOnlyHost(bool rx_only); virtual NetworkStats *getNetworkStats(int16_t networkId) { return (iface->getNetworkStats(networkId)); }; diff --git a/src/Host.cpp b/src/Host.cpp index f664d3e58d91..1ea52910ab5a 100644 --- a/src/Host.cpp +++ b/src/Host.cpp @@ -288,7 +288,7 @@ void Host::initialize(Mac *_mac, int32_t _iface_idx, is_dhcp_host = 0, is_crawler_bot_scanner = 0, is_in_broadcast_domain = 0, more_then_one_device = 0, device_ip = 0; - is_rx_only = 0; + is_rx_only = false; last_stats_reset = ntop->getLastStatsReset(); /* assume fresh stats, may be changed by deserialize */ @@ -301,7 +301,7 @@ void Host::initialize(Mac *_mac, int32_t _iface_idx, memset(&num_blacklisted_flows, 0, sizeof(num_blacklisted_flows)); memset(&customHostAlert, 0, sizeof(customHostAlert)); - setRxOnlyHost(true); + toggleRxOnlyHost(true); #ifdef NTOPNG_PRO host_traffic_shapers = NULL; @@ -666,7 +666,7 @@ void Host::lua_get_min_info(lua_State *vm) { lua_push_bool_table_entry(vm, "crawlerBotScannerHost", isCrawlerBotScannerHost()); lua_push_bool_table_entry(vm, "is_blacklisted", isBlacklisted()); - lua_push_bool_table_entry(vm, "is_rx_only", is_rx_only); + lua_push_bool_table_entry(vm, "is_rx_only", isRxOnlyHost()); lua_push_bool_table_entry(vm, "is_broadcast", isBroadcastHost()); lua_push_bool_table_entry(vm, "is_multicast", isMulticastHost()); lua_push_int32_table_entry(vm, "host_services_bitmap", host_services_bitmap); @@ -954,7 +954,7 @@ void Host::lua(lua_State *vm, AddressTree *ptree, bool host_details, if (blacklist_name != NULL) lua_push_str_table_entry(vm, "blacklist_name", blacklist_name); - lua_push_bool_table_entry(vm, "is_rx_only", is_rx_only); + lua_push_bool_table_entry(vm, "is_rx_only", isRxOnlyHost()); if (more_then_one_device) lua_push_bool_table_entry(vm, "more_then_one_device", more_then_one_device); @@ -1406,7 +1406,7 @@ void Host::serialize(json_object *my_object, DetailsLevel details_level) { json_object_object_add(my_object, "is_blacklisted", json_object_new_boolean(isBlacklisted())); json_object_object_add(my_object, "is_rx_only", - json_object_new_boolean(is_rx_only ? true : false)); + json_object_new_boolean(isRxOnlyHost())); json_object_object_add(my_object, "host_services_bitmap", json_object_new_int(host_services_bitmap)); @@ -2833,25 +2833,18 @@ u_int32_t Host::getNumContactsFromPeersAsServerTCPUDPNoTX() { /* *************************************** */ -void Host::setRxOnlyHost(bool set_it) { - if(is_rx_only == set_it) +void Host::toggleRxOnlyHost(bool rx_only) { + if(is_rx_only == rx_only) return; /* Nothing to do */ - //if(stats != NULL && !stats->getNumPktsRcvd() && !stats->getNumPktsSent()) return; - - if(is_rx_only && (set_it == false)) { - + if(rx_only == false) { /* Rx-only -> Not-Rx-only */ if(isUnicastHost()) { - /* It used to be rx-only ... */ iface->decNumHosts(isLocalHost(), true /* rx-only */); - /* .. and it is not anymore */ - iface->incNumHosts(isLocalHost(), false); + iface->incNumHosts(isLocalHost(), false /* not-rx-only */); } - - } else if((is_rx_only == false) && (set_it == true)) { - //ntop->getTrace()->traceEvent(TRACE_WARNING, "Internal error: invalid transition"); - /* The above issue is not reported as usually this branch is used during initialization */ + } else { + /* Not-Rx-only -> Rx-only: this should happen during initialization only */ } - is_rx_only = set_it; + is_rx_only = rx_only; } diff --git a/src/HostStats.cpp b/src/HostStats.cpp index af68fc3b02bd..8cc59ec1591e 100644 --- a/src/HostStats.cpp +++ b/src/HostStats.cpp @@ -408,19 +408,9 @@ void HostStats::incStats(time_t when, u_int8_t l4_proto, u_int ndpi_proto, u_int64_t sent_bytes, u_int64_t sent_goodput_bytes, u_int64_t rcvd_packets, u_int64_t rcvd_bytes, u_int64_t rcvd_goodput_bytes, bool peer_is_unicast) { - if ((getNumPktsSent() == 0) /* Current count */ - && (!host->isBroadcastHost()) && (!host->isMulticastHost()) && - (sent_packets > 0)) { - /* - This is a host (non broadcast/multicast host) - that used to be rcvdOnly and that has now sent traffic - */ - - host->setRxOnlyHost(false /* no longer RX-only */); - } - sent.incStats(when, sent_packets, sent_bytes), - rcvd.incStats(when, rcvd_packets, rcvd_bytes); + sent.incStats(when, sent_packets, sent_bytes); + rcvd.incStats(when, rcvd_packets, rcvd_bytes); if (ndpiStats) { ndpiStats->incStats(when, ndpi_proto, sent_packets, sent_bytes, @@ -446,6 +436,9 @@ void HostStats::incStats(time_t when, u_int8_t l4_proto, u_int ndpi_proto, /* Packet stats sent_stats and rcvd_stats are incremented in Flow::incStats */ l4stats.incStats(when, l4_proto, rcvd_packets, rcvd_bytes, sent_packets, sent_bytes); + + if (host->isRxOnlyHost() && !isReceiveOnly()) /* no longer RX-only */ + host->toggleRxOnlyHost(false); } #ifdef NTOPNG_PRO diff --git a/src/LocalHost.cpp b/src/LocalHost.cpp index 7bb360e24bff..d0bbb3891b8c 100644 --- a/src/LocalHost.cpp +++ b/src/LocalHost.cpp @@ -144,7 +144,7 @@ void LocalHost::initialize() { if (NetworkStats *ns = iface->getNetworkStats(local_network_id)) ns->incNumHosts(); - iface->incNumHosts(isLocalHost(), true /* Initialization: bytes are 0, considered RX only */); + iface->incNumHosts(true /* isLocalHost() */, true /* Initialization: bytes are 0, considered RX only */); } #ifdef LOCALHOST_DEBUG @@ -619,8 +619,8 @@ void LocalHost::setRouterMac(Mac *gw) { /* *************************************** */ -void LocalHost::setRxOnlyHost(bool set_it) { - Host::setRxOnlyHost(set_it); +void LocalHost::toggleRxOnlyHost(bool rx_only) { + Host::toggleRxOnlyHost(rx_only); if (isLocalUnicastHost()) { char hostbuf[64], *member; diff --git a/src/RemoteHost.cpp b/src/RemoteHost.cpp index bc355860c724..588d268286c9 100644 --- a/src/RemoteHost.cpp +++ b/src/RemoteHost.cpp @@ -81,5 +81,5 @@ void RemoteHost::initialize() { } if (isUnicastHost()) - iface->incNumHosts(isLocalHost(), true /* Initialization: bytes are 0, considered RX only */); + iface->incNumHosts(false /* isLocalHost() */, true /* Initialization: bytes are 0, considered RX only */); }