-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathconnection_manager.cpp
106 lines (82 loc) · 2.98 KB
/
connection_manager.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdexcept>
#include "connection_manager.hpp"
connection_manager::connection_manager():
m_thread(&connection_manager::reaper_thread, this)
{}
connection_manager::~connection_manager()
{
m_stopper.stop();
m_thread.join();
}
std::shared_ptr<const struct process_info_t>
connection_manager::lookup_connection_info(const nfq_event_t &p_event)
{
const __uint128_t l_source_address = p_event.m_v6 ?
p_event.m_source_address_v6 : p_event.m_source_address;
const __uint128_t l_destination_address = p_event.m_v6 ?
p_event.m_destination_address_v6 : p_event.m_destination_address;
connection_tuple_t l_key = {
.m_protocol = static_cast<ip_protocol_t>(p_event.m_protocol),
.m_v6 = p_event.m_v6,
.m_source_address = l_source_address,
.m_destination_address = l_destination_address,
.m_source_port = p_event.m_source_port,
.m_destination_port = p_event.m_destination_port
};
std::lock_guard<std::mutex> l_guard(m_lock);
const auto l_iter = m_mapping.find(l_key);
if (l_iter != m_mapping.end()) {
l_iter->second.m_last_active = std::chrono::steady_clock::now();
return l_iter->second.m_process;
} else {
l_key.m_source_address = 0;
const auto l_iter2 = m_mapping.find(l_key);
if (l_iter2 != m_mapping.end()) {
l_iter2->second.m_last_active = std::chrono::steady_clock::now();
return l_iter2->second.m_process;
} else {
return nullptr;
}
}
}
void
connection_manager::add_connection_info(
const ebpf_event_t & p_event,
std::shared_ptr<const process_info_t> p_process
) {
const __uint128_t l_source_address = p_event.m_v6 ?
p_event.m_source_address_v6 : p_event.m_source_address;
const __uint128_t l_destination_address = p_event.m_v6 ?
p_event.m_destination_address_v6 : p_event.m_destination_address;
const connection_tuple_t l_key = {
.m_protocol = static_cast<ip_protocol_t>(p_event.m_protocol),
.m_v6 = p_event.m_v6,
.m_source_address = l_source_address,
.m_destination_address = l_destination_address,
.m_source_port = p_event.m_source_port,
.m_destination_port = p_event.m_destination_port
};
const item_t l_item = {
.m_last_active = std::chrono::steady_clock::now(),
.m_process = p_process
};
std::lock_guard<std::mutex> l_guard(m_lock);
m_mapping[l_key] = l_item;
}
void
connection_manager::reap()
{
const auto l_now = std::chrono::steady_clock::now();
std::lock_guard<std::mutex> l_guard(m_lock);
std::erase_if(m_mapping, [&](const auto &l_iter) {
return (l_now - l_iter.second.m_last_active) >
std::chrono::seconds{60 * 5};
});
}
void
connection_manager::reaper_thread()
{
while (!m_stopper.await_stop_for_milliseconds(1000)) {
reap();
}
}