Changeset View
Changeset View
Standalone View
Standalone View
plugins/process/network/helper/Packet.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * This file is part of KSysGuard. | ||||
3 | * Copyright 2019 Arjen Hiemstra <ahiemstra@heimr.nl> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU General Public License as | ||||
7 | * published by the Free Software Foundation; either version 2 of | ||||
8 | * the License or (at your option) version 3 or any later version | ||||
9 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
10 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
11 | * defined in Section 14 of version 3 of the license. | ||||
12 | * | ||||
13 | * This program is distributed in the hope that it will be useful, | ||||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
16 | * GNU General Public License for more details. | ||||
17 | * | ||||
18 | * You should have received a copy of the GNU General Public License | ||||
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
20 | */ | ||||
21 | | ||||
22 | #ifndef PACKET_H | ||||
23 | #define PACKET_H | ||||
24 | | ||||
25 | #include <chrono> | ||||
26 | #include <cstdint> | ||||
27 | #include <array> | ||||
28 | #include <functional> | ||||
29 | | ||||
30 | #include "TimeStamps.h" | ||||
31 | | ||||
32 | class Packet | ||||
33 | { | ||||
34 | public: | ||||
35 | enum class NetworkProtocolType { | ||||
36 | Unknown, | ||||
37 | IPv4, | ||||
38 | IPv6, | ||||
39 | }; | ||||
40 | | ||||
41 | enum class TransportProtocolType { | ||||
42 | Unknown, | ||||
43 | Tcp, | ||||
44 | Udp, | ||||
45 | }; | ||||
46 | | ||||
47 | enum class Direction { | ||||
48 | Inbound, | ||||
49 | Outbound, | ||||
50 | }; | ||||
51 | | ||||
52 | struct Address | ||||
53 | { | ||||
54 | std::array<uint32_t, 4> address = { 0 }; | ||||
55 | uint32_t port = 0; | ||||
56 | | ||||
57 | inline bool operator==(const Address &other) const | ||||
58 | { | ||||
59 | return address == other.address | ||||
60 | && port == other.port; | ||||
61 | } | ||||
62 | }; | ||||
63 | | ||||
64 | Packet(); | ||||
65 | | ||||
66 | Packet(const TimeStamp::MicroSeconds &timeStamp, const uint8_t *data, uint32_t dataLength, uint32_t packetSize); | ||||
67 | | ||||
68 | ~Packet(); | ||||
69 | | ||||
70 | Packet(const Packet &other) = delete; | ||||
71 | Packet(Packet &&other) = default; | ||||
72 | | ||||
73 | TimeStamp::MicroSeconds timeStamp() const; | ||||
74 | unsigned int size() const; | ||||
75 | NetworkProtocolType networkProtocol() const; | ||||
76 | TransportProtocolType transportProtocol() const; | ||||
77 | Address sourceAddress() const; | ||||
78 | Address destinationAddress() const; | ||||
79 | | ||||
80 | private: | ||||
81 | void parseIPv4(const uint8_t *data); | ||||
82 | void parseIPv6(const uint8_t *data); | ||||
83 | void parseTransport(uint8_t type, const uint8_t *data); | ||||
84 | | ||||
85 | TimeStamp::MicroSeconds m_timeStamp; | ||||
86 | unsigned int m_size = 0; | ||||
87 | | ||||
88 | NetworkProtocolType m_networkProtocol = NetworkProtocolType::Unknown; | ||||
89 | TransportProtocolType m_transportProtocol = TransportProtocolType::Unknown; | ||||
90 | | ||||
91 | Address m_sourceAddress; | ||||
92 | Address m_destinationAddress; | ||||
93 | }; | ||||
94 | | ||||
95 | namespace std { | ||||
96 | template <> struct hash<Packet::Address> | ||||
97 | { | ||||
98 | using argument_type = Packet::Address; | ||||
99 | using result_type = std::size_t; | ||||
100 | inline result_type operator()(argument_type const& address) const noexcept { | ||||
101 | return std::hash<uint32_t>{}(address.address[0]) | ||||
102 | ^ (std::hash<uint32_t>{}(address.address[1]) << 1) | ||||
103 | ^ (std::hash<uint32_t>{}(address.address[2]) << 2) | ||||
104 | ^ (std::hash<uint32_t>{}(address.address[3]) << 3) | ||||
105 | ^ (std::hash<uint32_t>{}(address.port) << 4); | ||||
106 | } | ||||
107 | }; | ||||
108 | } | ||||
109 | | ||||
110 | #endif // PACKET_H |