-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmapping.cpp
73 lines (67 loc) · 2 KB
/
mapping.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
/*
Copyright (c) 2020-2021 Technica Engineering GmbH
GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
*/
#include "pcapng_exporter/mapping.hpp"
#include <sstream>
template <class T>
void merge_value(
std::optional<T>* value,
std::optional<T> change
) {
if (change.has_value()) {
*value = change;
}
}
namespace pcapng_exporter {
std::string get_interface_name(uint32_t channel_id) {
std::stringstream sstream;
sstream << std::hex << channel_id;
std::string hex_channel = sstream.str();
for (auto& c : hex_channel) {
c = toupper(c);
}
return hex_channel;
}
bool mapping_match(channel_info target, channel_info when) {
if (when.chl_id.has_value() && when.chl_id != target.chl_id) {
return false;
}
if (when.chl_link.has_value() && when.chl_link != target.chl_link) {
return false;
}
if (when.inf_name.has_value() && when.inf_name != target.inf_name) {
return false;
}
if (when.pkt_dir.has_value() && when.pkt_dir != target.pkt_dir){
return false;
}
return true;
}
channel_info mapping_resolve(std::vector<channel_mapping> mappings, light_packet_interface packet_interface, light_packet_header packet_header, uint32_t channel_id) {
// What to test against
channel_info target;
target.chl_id = channel_id;
target.chl_link = packet_interface.link_type;
target.inf_name = packet_interface.name
? std::optional(std::string(packet_interface.name))
: std::nullopt;
target.pkt_dir = (pkt_dir_enum)(packet_header.flags & 0x00000003);
// Result
channel_info result;
result.chl_id = target.chl_id;
result.pkt_dir = target.pkt_dir;
for (channel_mapping mapping : mappings) {
if (mapping_match(target, mapping.when)) {
merge_value(&result.chl_id, mapping.change.chl_id);
merge_value(&result.inf_name, mapping.change.inf_name);
merge_value(&result.pkt_dir, mapping.change.pkt_dir);
break;
}
}
if (!result.inf_name.has_value()) {
result.inf_name = get_interface_name(channel_id);
}
return result;
}
}