-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtopPorts.hpp
More file actions
71 lines (61 loc) · 1.8 KB
/
topPorts.hpp
File metadata and controls
71 lines (61 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* \file topPorts.hpp
* \brief TopPorts class declaration implementing the most popular ports.
* \author Damir Zainullin <zaidamilda@gmail.com>
* \date 2024
*/
#pragma once
#include <array>
#include <cstdint>
#include <limits>
#include <string>
#include <vector>
namespace ipxp {
/**
* \brief Top ports counter.
*/
class TopPorts {
public:
/**
* \brief Constructor.
* \param top_ports_count Number of the most popular ports to track.
*/
TopPorts(size_t top_ports_count) noexcept;
/**
* \brief Increments number of times given tcp port has been seen.
* \param port Port to increment its frequency.
*/
void increment_tcp_frequency(uint16_t port) noexcept { m_tcp_port_frequencies[port]++; }
/**
* \brief Increments number of times given udp port has been seen.
* \param port Port to increment its frequency.
*/
void increment_udp_frequency(uint16_t port) noexcept { m_udp_port_frequencies[port]++; }
/**
* \brief Port frequency and protocol to which it belongs.
*/
struct PortStats {
/**
* \brief Protocol type.
*/
enum class Protocol { TCP, UDP };
uint16_t port; /**< Port number. */
size_t frequency; /**< Number of times the port has been seen. */
Protocol protocol; /**< Protocol to which the port belongs. */
/**
* \brief Convert the port stats to string.
* \return String representation of the port stats.
*/
std::string to_string() const noexcept;
};
/**
* \brief Get the top ports.
* \return Vector of the most popular ports.
*/
std::vector<TopPorts::PortStats> get_top_ports() const noexcept;
private:
std::array<std::size_t, std::numeric_limits<uint16_t>::max() + 1> m_tcp_port_frequencies {};
std::array<std::size_t, std::numeric_limits<uint16_t>::max() + 1> m_udp_port_frequencies {};
const size_t m_top_ports_count;
};
} // namespace ipxp