Skip to content

Commit 69ced11

Browse files
committed
CTT - Add FlowKey structure
1 parent c86826a commit 69ced11

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

storage/flowKey.tpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* \file
3+
* \author Damir Zainullin <[email protected]>
4+
* \brief FlowKey structure declaration.
5+
*/
6+
/*
7+
* Copyright (C) 2023 CESNET
8+
*
9+
* LICENSE TERMS
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
* 3. Neither the name of the Company nor the names of its contributors
21+
* may be used to endorse or promote products derived from this
22+
* software without specific prior written permission.
23+
*/
24+
25+
#pragma once
26+
27+
#include <cstdint>
28+
#include <cstddef>
29+
#include <array>
30+
31+
namespace ipxp {
32+
33+
template<size_t AddressSize>
34+
struct FlowKey {
35+
uint16_t src_port;
36+
uint16_t dst_port;
37+
uint8_t proto;
38+
uint8_t ip_version;
39+
std::array<uint8_t, AddressSize> src_ip;
40+
std::array<uint8_t, AddressSize> dst_ip;
41+
uint16_t vlan_id;
42+
protected:
43+
void save_direct(const Packet& packet) noexcept
44+
{
45+
src_port = packet.src_port;
46+
dst_port = packet.dst_port;
47+
proto = packet.ip_proto;
48+
ip_version = packet.ip_version;
49+
vlan_id = packet.vlan_id;
50+
}
51+
52+
void save_reversed(const Packet& packet) noexcept
53+
{
54+
save_direct(packet);
55+
src_port = packet.dst_port;
56+
dst_port = packet.src_port;
57+
}
58+
59+
} __attribute__((packed));
60+
61+
struct FlowKeyv4 : FlowKey<4> {
62+
63+
static FlowKeyv4 save_direct(const Packet& packet) noexcept
64+
{
65+
FlowKeyv4 res;
66+
res.FlowKey::save_direct(packet);
67+
std::memcpy(res.src_ip.data(), &packet.src_ip.v4, 4);
68+
std::memcpy(res.dst_ip.data(), &packet.dst_ip.v4, 4);
69+
return res;
70+
}
71+
72+
static FlowKeyv4 save_reversed(const Packet& packet) noexcept
73+
{
74+
FlowKeyv4 res;
75+
res.FlowKey::save_reversed(packet);
76+
std::memcpy(res.src_ip.data(), &packet.dst_ip.v4, 4);
77+
std::memcpy(res.dst_ip.data(), &packet.src_ip.v4, 4);
78+
return res;
79+
}
80+
81+
};
82+
83+
struct FlowKeyv6 : FlowKey<16> {
84+
85+
static FlowKeyv6 save_direct(const Packet& packet) noexcept
86+
{
87+
FlowKeyv6 res;
88+
res.FlowKey::save_direct(packet);
89+
std::memcpy(res.src_ip.data(), &packet.src_ip.v4, 16);
90+
std::memcpy(res.dst_ip.data(), &packet.dst_ip.v4, 16);
91+
return res;
92+
}
93+
94+
static FlowKeyv6 save_reversed(const Packet& packet) noexcept
95+
{
96+
FlowKeyv6 res;
97+
res.FlowKey::save_reversed(packet);
98+
std::memcpy(res.src_ip.data(), &packet.dst_ip.v4, 16);
99+
std::memcpy(res.dst_ip.data(), &packet.src_ip.v4, 16);
100+
return res;
101+
}
102+
103+
};
104+
105+
} // namespace ipxp

0 commit comments

Comments
 (0)