From 4ff3b2dba09c5dfd92f0f0ffe3e2c511c3b83af6 Mon Sep 17 00:00:00 2001 From: Emanuele Faranda Date: Tue, 31 Jan 2017 17:09:25 +0100 Subject: [PATCH] Add application filter dropdown to the host page flows (#957) --- include/NetworkInterface.h | 2 +- scripts/lua/host_details.lua | 27 +++++++++++++++++++++- src/Lua.cpp | 43 ++++++++++++++++++++++-------------- src/NetworkInterface.cpp | 32 ++++++++++++++++++++++----- 4 files changed, 79 insertions(+), 25 deletions(-) diff --git a/include/NetworkInterface.h b/include/NetworkInterface.h index 64ab06675a..c432b04073 100644 --- a/include/NetworkInterface.h +++ b/include/NetworkInterface.h @@ -285,7 +285,7 @@ class NetworkInterface { void processFlow(ZMQ_Flow *zflow); void processInterfaceStats(sFlowInterfaceStats *stats); void dumpFlows(); - void getnDPIStats(nDPIStats *stats); + void getnDPIStats(nDPIStats *stats, AddressTree *allowed_hosts, const char *host_ip, u_int16_t vlan_id); void periodicStatsUpdate(); virtual void lua(lua_State* vm); void getnDPIProtocols(lua_State *vm); diff --git a/scripts/lua/host_details.lua b/scripts/lua/host_details.lua index d3e9c90454..a9ddeb2ad5 100644 --- a/scripts/lua/host_details.lua +++ b/scripts/lua/host_details.lua @@ -23,6 +23,7 @@ local host_pools_utils = require "host_pools_utils" debug_hosts = false page = _GET["page"] protocol_id = _GET["protocol"] +application = _GET["application"] host_info = url2hostinfo(_GET) host_ip = host_info["host"] host_name = hostinfo2hostkey(host_info) @@ -1457,6 +1458,9 @@ print [[ print (ntop.getHttpPrefix()) print [[/lua/get_flows_data.lua?ifname=]] print(ifId.."&") +if (application ~= nil) then + print("application="..application.."&") +end print (hostinfo2url(host_info)..'";') ntop.dumpFile(dirs.installdir .. "/httpdocs/inc/flows_stats_id.inc") @@ -1474,6 +1478,25 @@ elseif interface.isPcapDumpInterface() then active_flows_msg = "Flows" end +local application_filter = '' +if(application ~= nil) then + application_filter = '' +end +local dt_buttons = "['
']" + if(show_sprobe) then print [[ //console.log(url_update); @@ -1481,6 +1504,7 @@ print [[ flow_rows_option["type"] = 'host'; $("#table-flows").datatable({ url: url_update , + buttons: ]] print(dt_buttons) print[[, rowCallback: function ( row ) { return flow_table_setID(row); }, showPagination: true, ]] @@ -1502,6 +1526,7 @@ print [[ flow_rows_option["type"] = 'host'; $("#table-flows").datatable({ url: url_update, + buttons: ]] print(dt_buttons) print[[, rowCallback: function ( row ) { return flow_table_setID(row); }, showPagination: true, ]] @@ -1523,7 +1548,7 @@ print [[ hidden: true }, { - title: "Info", + title: "", field: "column_key", css: { textAlign: 'center' diff --git a/src/Lua.cpp b/src/Lua.cpp index 551825df3a..8fca148303 100644 --- a/src/Lua.cpp +++ b/src/Lua.cpp @@ -87,6 +87,22 @@ int ntop_lua_check(lua_State* vm, const char* func, int pos, int expected_type) /* ****************************************** */ +static void get_host_vlan_info(char* lua_ip, char** host_ip, + u_int16_t* vlan_id, + char *buf, u_int buf_len) { + char *where, *vlan = NULL; + + snprintf(buf, buf_len, "%s", lua_ip); + + if(((*host_ip) = strtok_r(buf, "@", &where)) != NULL) + vlan = strtok_r(NULL, "@", &where); + + if(vlan) + (*vlan_id) = (u_int16_t)atoi(vlan); +} + +/* ****************************************** */ + static NetworkInterface* handle_null_interface(lua_State* vm) { char allowed_ifname[MAX_INTERFACE_NAME_LEN]; @@ -285,11 +301,20 @@ static int ntop_select_interface(lua_State* vm) { static int ntop_get_ndpi_interface_stats(lua_State* vm) { NetworkInterface *ntop_interface = getCurrentInterface(vm); nDPIStats stats; + char *host_ip = NULL; + u_int16_t vlan_id = 0; + char buf[64]; ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__); + /* Optional host */ + if(lua_type(vm, 1) == LUA_TSTRING) get_host_vlan_info((char*)lua_tostring(vm, 1), &host_ip, &vlan_id, buf, sizeof(buf)); + + /* Optional VLAN id */ + if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2); + if(ntop_interface) { - ntop_interface->getnDPIStats(&stats); + ntop_interface->getnDPIStats(&stats, get_allowed_nets(vm), host_ip, vlan_id); lua_newtable(vm); stats.lua(ntop_interface, vm); @@ -1372,22 +1397,6 @@ static int ntop_send_udp_data(lua_State* vm) { /* ****************************************** */ -static void get_host_vlan_info(char* lua_ip, char** host_ip, - u_int16_t* vlan_id, - char *buf, u_int buf_len) { - char *where, *vlan = NULL; - - snprintf(buf, buf_len, "%s", lua_ip); - - if(((*host_ip) = strtok_r(buf, "@", &where)) != NULL) - vlan = strtok_r(NULL, "@", &where); - - if(vlan) - (*vlan_id) = (u_int16_t)atoi(vlan); -} - -/* ****************************************** */ - static int ntop_get_interface_flows(lua_State* vm, LocationPolicy location) { NetworkInterface *ntop_interface = getCurrentInterface(vm); char buf[64]; diff --git a/src/NetworkInterface.cpp b/src/NetworkInterface.cpp index c1dc08364b..f4dcdb06e5 100644 --- a/src/NetworkInterface.cpp +++ b/src/NetworkInterface.cpp @@ -1876,18 +1876,38 @@ void NetworkInterface::findFlowHosts(u_int16_t vlanId, /* **************************************************** */ -static bool flow_sum_protos(GenericHashEntry *f, void *user_data) { - nDPIStats *stats = (nDPIStats*)user_data; - Flow *flow = (Flow*)f; +struct ndpiStatsRetrieverData { + nDPIStats *stats; + Host *host; +}; - flow->sumStats(stats); +static bool flow_sum_protos(GenericHashEntry *flow, void *user_data) { + ndpiStatsRetrieverData *retriever = (ndpiStatsRetrieverData*)user_data; + nDPIStats *stats = retriever->stats; + Flow *f = (Flow*)flow; + + if(retriever->host + && (retriever->host != f->get_cli_host()) + && (retriever->host != f->get_srv_host())) + return(false); /* false = keep on walking */ + + f->sumStats(stats); return(false); /* false = keep on walking */ } /* **************************************************** */ -void NetworkInterface::getnDPIStats(nDPIStats *stats) { - walker(walker_flows, flow_sum_protos, (void*)stats); +void NetworkInterface::getnDPIStats(nDPIStats *stats, AddressTree *allowed_hosts, + const char *host_ip, u_int16_t vlan_id) { + ndpiStatsRetrieverData retriever; + + Host *h = NULL; + if (host_ip) + h = findHostsByIP(allowed_hosts, (char *)host_ip, vlan_id); + + retriever.stats = stats; + retriever.host = h; + walker(walker_flows, flow_sum_protos, (void*)&retriever); } /* **************************************************** */