Skip to content

Commit 69c178f

Browse files
author
Damir Zainullin
committed
++
1 parent 95e9b9d commit 69c178f

File tree

5 files changed

+96
-90
lines changed

5 files changed

+96
-90
lines changed

include/ipfixprobe/inputPlugin.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class IPXP_API InputPlugin
9696
};
9797

9898
/// Statistics related to packet parsing.
99-
ParserStats m_parser_stats{10};
99+
ParserStats m_parser_stats {10};
100100

101101
private:
102102
void create_parser_stats_telemetry(

include/ipfixprobe/parser-stats.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ struct VlanStats {
163163
* \brief Structure for storing parser statistics.
164164
*/
165165
struct ParserStats {
166-
167166
ParserStats(size_t top_ports_count)
168167
: top_ports(top_ports_count)
169168
, mpls_packets(0)
@@ -175,8 +174,10 @@ struct ParserStats {
175174
, tcp_packets(0)
176175
, udp_packets(0)
177176
, seen_packets(0)
178-
, unknown_packets(0) {}
179-
177+
, unknown_packets(0)
178+
{
179+
}
180+
180181
TopPorts top_ports;
181182

182183
uint64_t mpls_packets;

src/plugins/input/parser/parser.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,8 @@ inline uint16_t parse_ipv6_hdr(const u_char* data_ptr, uint16_t data_len, Packet
465465
* \param [out] pkt Pointer to Packet structure where parsed fields will be stored.
466466
* \return Size of header in bytes.
467467
*/
468-
inline uint16_t parse_tcp_hdr(const u_char* data_ptr, uint16_t data_len, Packet* pkt, ParserStats& stats)
468+
inline uint16_t
469+
parse_tcp_hdr(const u_char* data_ptr, uint16_t data_len, Packet* pkt, ParserStats& stats)
469470
{
470471
struct tcphdr* tcp = (struct tcphdr*) data_ptr;
471472
if (sizeof(struct tcphdr) > data_len) {
@@ -480,7 +481,7 @@ inline uint16_t parse_tcp_hdr(const u_char* data_ptr, uint16_t data_len, Packet*
480481
pkt->tcp_window = ntohs(tcp->window);
481482

482483
stats.top_ports.increment_tcp_frequency(pkt->src_port);
483-
stats.top_ports.increment_tcp_frequency(pkt->dst_port);
484+
stats.top_ports.increment_tcp_frequency(pkt->dst_port);
484485

485486
DEBUG_MSG("TCP header:\n");
486487
DEBUG_MSG("\tSrc port:\t%u\n", ntohs(tcp->source));
@@ -547,7 +548,8 @@ inline uint16_t parse_tcp_hdr(const u_char* data_ptr, uint16_t data_len, Packet*
547548
* \param [out] pkt Pointer to Packet structure where parsed fields will be stored.
548549
* \return Size of header in bytes.
549550
*/
550-
inline uint16_t parse_udp_hdr(const u_char* data_ptr, uint16_t data_len, Packet* pkt, ParserStats& stats)
551+
inline uint16_t
552+
parse_udp_hdr(const u_char* data_ptr, uint16_t data_len, Packet* pkt, ParserStats& stats)
551553
{
552554
struct udphdr* udp = (struct udphdr*) data_ptr;
553555
if (sizeof(struct udphdr) > data_len) {
@@ -558,7 +560,7 @@ inline uint16_t parse_udp_hdr(const u_char* data_ptr, uint16_t data_len, Packet*
558560
pkt->dst_port = ntohs(udp->dest);
559561

560562
stats.top_ports.increment_udp_frequency(pkt->src_port);
561-
stats.top_ports.increment_udp_frequency(pkt->dst_port);
563+
stats.top_ports.increment_udp_frequency(pkt->dst_port);
562564

563565
DEBUG_MSG("UDP header:\n");
564566
DEBUG_MSG("\tSrc port:\t%u\n", ntohs(udp->source));

src/plugins/input/parser/topPorts.cpp

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,69 @@
77

88
#include "topPorts.hpp"
99

10-
#include <functional>
1110
#include <algorithm>
1211
#include <array>
1312
#include <cstdint>
13+
#include <functional>
1414
#include <limits>
15-
#include <vector>
1615
#include <string>
16+
#include <vector>
1717

1818
namespace ipxp {
1919

2020
TopPorts::TopPorts(size_t top_ports_count) noexcept
21-
: m_top_ports_count(top_ports_count)
21+
: m_top_ports_count(top_ports_count)
2222
{
2323
}
2424

2525
std::string TopPorts::PortStats::to_string() const noexcept
2626
{
27-
return std::to_string(port) + "[" +
28-
(protocol == Protocol::TCP ? "TCP" : "UDP") + "] - " + std::to_string(frequency);
27+
return std::to_string(port) + "[" + (protocol == Protocol::TCP ? "TCP" : "UDP") + "] - "
28+
+ std::to_string(frequency);
2929
}
3030

31-
bool update_port_buffer(std::vector<TopPorts::PortStats>& port_buffer, TopPorts::PortStats port_stats) noexcept{
32-
auto port_pos = std::lower_bound(port_buffer.begin(), port_buffer.end(), port_stats.frequency,
33-
[](const TopPorts::PortStats& port_frequency, size_t count) {
34-
return port_frequency.frequency >= count;
35-
});
36-
37-
if (port_pos != port_buffer.end()) {
38-
std::copy_backward(port_pos, std::prev(port_buffer.end()), port_buffer.end());
39-
*port_pos = port_stats;
40-
return true;
41-
//ports_inserted = std::min<size_t>(m_top_ports_count, ports_inserted + 1);
42-
}
43-
return false;
44-
};
31+
bool update_port_buffer(
32+
std::vector<TopPorts::PortStats>& port_buffer,
33+
TopPorts::PortStats port_stats) noexcept
34+
{
35+
auto port_pos = std::lower_bound(
36+
port_buffer.begin(),
37+
port_buffer.end(),
38+
port_stats.frequency,
39+
[](const TopPorts::PortStats& port_frequency, size_t count) {
40+
return port_frequency.frequency >= count;
41+
});
42+
43+
if (port_pos != port_buffer.end()) {
44+
std::copy_backward(port_pos, std::prev(port_buffer.end()), port_buffer.end());
45+
*port_pos = port_stats;
46+
return true;
47+
}
48+
return false;
49+
};
4550

4651
std::vector<TopPorts::PortStats> TopPorts::get_top_ports() const noexcept
4752
{
48-
std::vector<PortStats> port_buffer(m_top_ports_count);
49-
size_t ports_inserted = 0;
50-
51-
std::for_each(m_tcp_port_frequencies.begin(), m_tcp_port_frequencies.end(), [&, port = uint16_t{0}](size_t frequency) mutable {
52-
ports_inserted += update_port_buffer(port_buffer, {port++, frequency, PortStats::Protocol::TCP});
53-
});
54-
std::for_each(m_udp_port_frequencies.begin(), m_udp_port_frequencies.end(), [&, port = uint16_t{0}](size_t frequency) mutable{
55-
ports_inserted += update_port_buffer(port_buffer, {port++, frequency, PortStats::Protocol::UDP});
56-
});
57-
58-
port_buffer.resize(std::min(m_top_ports_count, ports_inserted));
59-
return port_buffer;
53+
std::vector<PortStats> port_buffer(m_top_ports_count);
54+
size_t ports_inserted = 0;
55+
56+
std::for_each(
57+
m_tcp_port_frequencies.begin(),
58+
m_tcp_port_frequencies.end(),
59+
[&, port = uint16_t {0}](size_t frequency) mutable {
60+
ports_inserted
61+
+= update_port_buffer(port_buffer, {port++, frequency, PortStats::Protocol::TCP});
62+
});
63+
std::for_each(
64+
m_udp_port_frequencies.begin(),
65+
m_udp_port_frequencies.end(),
66+
[&, port = uint16_t {0}](size_t frequency) mutable {
67+
ports_inserted
68+
+= update_port_buffer(port_buffer, {port++, frequency, PortStats::Protocol::UDP});
69+
});
70+
71+
port_buffer.resize(std::min(m_top_ports_count, ports_inserted));
72+
return port_buffer;
6073
}
6174

6275
} // namespace ipxp

src/plugins/input/parser/topPorts.hpp

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,63 @@
99
#include <array>
1010
#include <cstdint>
1111
#include <limits>
12-
#include <vector>
1312
#include <string>
13+
#include <vector>
1414

1515
namespace ipxp {
1616
/**
1717
* \brief Top ports counter.
1818
*/
1919
class TopPorts {
2020
public:
21+
/**
22+
* \brief Constructor.
23+
* \param top_ports_count Number of the most popular ports to track.
24+
*/
25+
TopPorts(size_t top_ports_count) noexcept;
2126

22-
/**
23-
* \brief Constructor.
24-
* \param top_ports_count Number of the most popular ports to track.
25-
*/
26-
TopPorts(size_t top_ports_count) noexcept;
27-
28-
/**
29-
* \brief Increments number of times given tcp port has been seen.
30-
* \param port Port to increment its frequency.
31-
*/
32-
void increment_tcp_frequency(uint16_t port) noexcept
33-
{
34-
m_tcp_port_frequencies[port]++;
35-
}
27+
/**
28+
* \brief Increments number of times given tcp port has been seen.
29+
* \param port Port to increment its frequency.
30+
*/
31+
void increment_tcp_frequency(uint16_t port) noexcept { m_tcp_port_frequencies[port]++; }
3632

37-
/**
38-
* \brief Increments number of times given udp port has been seen.
39-
* \param port Port to increment its frequency.
40-
*/
41-
void increment_udp_frequency(uint16_t port) noexcept
42-
{
43-
m_udp_port_frequencies[port]++;
44-
}
33+
/**
34+
* \brief Increments number of times given udp port has been seen.
35+
* \param port Port to increment its frequency.
36+
*/
37+
void increment_udp_frequency(uint16_t port) noexcept { m_udp_port_frequencies[port]++; }
4538

46-
/**
47-
* \brief Port frequency and protocol to which it belongs.
48-
*/
49-
struct PortStats {
50-
/**
51-
* \brief Protocol type.
52-
*/
53-
enum class Protocol {
54-
TCP,
55-
UDP
56-
};
39+
/**
40+
* \brief Port frequency and protocol to which it belongs.
41+
*/
42+
struct PortStats {
43+
/**
44+
* \brief Protocol type.
45+
*/
46+
enum class Protocol { TCP, UDP };
5747

58-
uint16_t port; /**< Port number. */
59-
size_t frequency; /**< Number of times the port has been seen. */
60-
Protocol protocol; /**< Protocol to which the port belongs. */
48+
uint16_t port; /**< Port number. */
49+
size_t frequency; /**< Number of times the port has been seen. */
50+
Protocol protocol; /**< Protocol to which the port belongs. */
6151

62-
/**
63-
* \brief Convert the port stats to string.
64-
* \return String representation of the port stats.
65-
*/
66-
std::string to_string() const noexcept;
67-
};
52+
/**
53+
* \brief Convert the port stats to string.
54+
* \return String representation of the port stats.
55+
*/
56+
std::string to_string() const noexcept;
57+
};
6858

69-
/**
70-
* \brief Get the top ports.
71-
* \return Vector of the most popular ports.
72-
*/
73-
std::vector<TopPorts::PortStats> get_top_ports() const noexcept;
59+
/**
60+
* \brief Get the top ports.
61+
* \return Vector of the most popular ports.
62+
*/
63+
std::vector<TopPorts::PortStats> get_top_ports() const noexcept;
7464

7565
private:
76-
std::array<std::size_t, std::numeric_limits<uint16_t>::max() + 1> m_tcp_port_frequencies {};
77-
std::array<std::size_t, std::numeric_limits<uint16_t>::max() + 1> m_udp_port_frequencies {};
78-
const size_t m_top_ports_count;
66+
std::array<std::size_t, std::numeric_limits<uint16_t>::max() + 1> m_tcp_port_frequencies {};
67+
std::array<std::size_t, std::numeric_limits<uint16_t>::max() + 1> m_udp_port_frequencies {};
68+
const size_t m_top_ports_count;
7969
};
8070

8171
} // namespace ipxp

0 commit comments

Comments
 (0)