Skip to content

Commit

Permalink
Make code more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
cardigliano committed Aug 9, 2024
1 parent d944725 commit 112bf29
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 37 deletions.
2 changes: 1 addition & 1 deletion include/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion include/LocalHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
Expand Down
31 changes: 12 additions & 19 deletions src/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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;
}
17 changes: 5 additions & 12 deletions src/HostStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/LocalHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/RemoteHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */);
}

0 comments on commit 112bf29

Please sign in to comment.