From 5fcc4986bc320360b604600cd3fc13cde38a41c7 Mon Sep 17 00:00:00 2001 From: Simone Mainardi Date: Wed, 17 Jul 2019 18:31:25 +0200 Subject: [PATCH] Implements interface view hosts updates --- include/Flow.h | 7 +++++-- src/Flow.cpp | 2 +- src/ViewInterface.cpp | 27 ++++++++++++++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/Flow.h b/include/Flow.h index 57f051b6db..106a353ace 100644 --- a/include/Flow.h +++ b/include/Flow.h @@ -215,7 +215,6 @@ class Flow : public GenericHashEntry { void dumpPacketStats(lua_State* vm, bool cli2srv_direction); bool isReadyToBeMarkedAsIdle(); bool isBlacklistedFlow() const; - u_int16_t getStatsProtocol() const; inline bool isDeviceAllowedProtocol() { return(!cli_host || !srv_host || ((cli_host->getDeviceAllowedProtocolStatus(ndpiDetectedProtocol, true) == device_proto_allowed) && @@ -309,9 +308,13 @@ class Flow : public GenericHashEntry { void updateSeqNum(time_t when, u_int32_t sN, u_int32_t aN); void processDetectedProtocol(); void setDetectedProtocol(ndpi_protocol proto_id, bool forceDetection); - void setCustomApp(custom_app_t ca) { + inline void setCustomApp(custom_app_t ca) { memcpy(&custom_app, &ca, sizeof(custom_app)); }; + inline custom_app_t getCustomApp() const { + return custom_app; + }; + u_int16_t getStatsProtocol() const; void setJSONInfo(const char *json); #ifdef NTOPNG_PRO inline bool is_status_counted_in_aggregated_flow() const { return(status_counted_in_aggregated_flow); }; diff --git a/src/Flow.cpp b/src/Flow.cpp index 5bcf94cf79..94dd08241d 100644 --- a/src/Flow.cpp +++ b/src/Flow.cpp @@ -2439,7 +2439,7 @@ bool Flow::get_partial_traffic_stats(FlowTrafficStats *fts) { if(!fts) return false; - if(!last_partial && !(last_partial = (FlowTrafficStats*)malloc(sizeof(FlowTrafficStats)))) + if(!last_partial && !(last_partial = (FlowTrafficStats*)calloc(1, sizeof(FlowTrafficStats)))) return false; memcpy(&tmp, &stats, sizeof(stats)); diff --git a/src/ViewInterface.cpp b/src/ViewInterface.cpp index a58ce92c25..926961ddd2 100644 --- a/src/ViewInterface.cpp +++ b/src/ViewInterface.cpp @@ -293,15 +293,36 @@ void ViewInterface::lua(lua_State *vm) { static bool viewed_flows_walker(GenericHashEntry *flow, void *user_data, bool *matched) { ViewInterface *iface = (ViewInterface*)user_data; Flow *f = (Flow*)flow; + time_t now = time(NULL); - iface->purgeIdle(time(NULL)); + iface->purgeIdle(now); if(f->is_acknowledged_to_purge()) return false; /* Already visited for the last time after it has gone idle, keep walking */ FlowTrafficStats partials; - if(f->get_partial_traffic_stats(&partials)) { - + const IpAddress *cli_ip = f->get_cli_ip_addr(), *srv_ip = f->get_srv_ip_addr(); + + if(f->get_partial_traffic_stats(&partials) && cli_ip && srv_ip) { + Host *cli_host = NULL, *srv_host = NULL; + + iface->findFlowHosts(f->get_vlan_id(), + NULL /* no src mac yet */, (IpAddress*)cli_ip, &cli_host, + NULL /* no dst mac yet */, (IpAddress*)srv_ip, &srv_host); + + if(cli_host) { + cli_host->incStats(now, f->get_protocol(), f->getStatsProtocol(), f->getCustomApp(), + partials.cli2srv_packets, partials.cli2srv_bytes, partials.cli2srv_goodput_bytes, + partials.srv2cli_packets, partials.srv2cli_bytes, partials.srv2cli_goodput_bytes, + cli_ip->isNonEmptyUnicastAddress()); + } + + if(srv_host) { + srv_host->incStats(now, f->get_protocol(), f->getStatsProtocol(), f->getCustomApp(), + partials.srv2cli_packets, partials.srv2cli_bytes, partials.srv2cli_goodput_bytes, + partials.cli2srv_packets, partials.cli2srv_bytes, partials.cli2srv_goodput_bytes, + srv_ip->isNonEmptyUnicastAddress()); + } } /* The flow has already been marked as idle by the underlying viewed interface,