diff --git a/libnettracer/src/netstat.cpp b/libnettracer/src/netstat.cpp index 18dad51..a2ce493 100644 --- a/libnettracer/src/netstat.cpp +++ b/libnettracer/src/netstat.cpp @@ -113,6 +113,16 @@ struct TimeGuard { } }; +std::pair NetStat::countTcpSessions() { + const auto& container4 = connections(); + unsigned incoming = + std::count_if(std::begin(container4), std::end(container4), [](const auto& it) { return it.second.state.Direction == 1; }); + + const auto& container6 = connections(); + incoming += std::count_if(std::begin(container6), std::end(container6), [](const auto& it) { return it.second.state.Direction == 1; }); + return std::pair{incoming, container4.size() + container6.size() - incoming}; +} + void NetStat::map_loop(const bpf_fds& fdsIPv4, const bpf_fds& fdsIPv6) { using namespace std::literals::chrono_literals; @@ -127,17 +137,17 @@ void NetStat::map_loop(const bpf_fds& fdsIPv4, const bpf_fds& fdsIPv6) { clean_bpf(fdsIPv6); if (kbhit || outputCtr.time_elapsed()) { - const auto tcpSessions = - fmt::format("Total tcp sessions: {}", connections().size() + connections().size()); + const auto tcpSessions = countTcpSessions(); + const auto tcpSessionsStr = fmt::format("Number of passive tcp sessions: {}, active: {}", tcpSessions.first, tcpSessions.second); if (interactive) { print_human_readable(); print_human_readable(); - std::cout << tcpSessions; + std::cout << tcpSessionsStr; } else { print(); print(); if (logCtr.time_elapsed()) { - LOG_INFO(tcpSessions); + LOG_INFO(tcpSessionsStr); } } outputCtr.reset(); diff --git a/libnettracer/src/netstat.h b/libnettracer/src/netstat.h index 69e20f8..77d2d15 100644 --- a/libnettracer/src/netstat.h +++ b/libnettracer/src/netstat.h @@ -67,6 +67,7 @@ class NetStat { void initConnection(const tcpTable&); void initConnections(); + std::pair countTcpSessions(); template void update(const bpf_fds& fds);