/src/PcapPlusPlus/Packet++/header/PacketUtils.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "Packet.h" |
4 | | #include "IpAddress.h" |
5 | | #include "PointerVector.h" |
6 | | |
7 | | /// @file |
8 | | |
9 | | /// @namespace pcpp |
10 | | /// @brief The main namespace for the PcapPlusPlus lib |
11 | | namespace pcpp |
12 | | { |
13 | | /// A struct that represent a single buffer |
14 | | template <typename T> struct ScalarBuffer |
15 | | { |
16 | | /// The pointer to the buffer |
17 | | T* buffer; |
18 | | |
19 | | /// Buffer length |
20 | | size_t len; |
21 | | }; |
22 | | |
23 | | /// Computes the checksum for a vector of buffers |
24 | | /// @param[in] vec The vector of buffers |
25 | | /// @param[in] vecSize Number of ScalarBuffers in vector |
26 | | /// @return The checksum result |
27 | | uint16_t computeChecksum(const ScalarBuffer<const uint16_t> vec[], size_t vecSize); |
28 | | |
29 | | /// Computes the checksum for a vector of buffers |
30 | | /// @param[in] vec The vector of buffers |
31 | | /// @param[in] vecSize Number of ScalarBuffers in vector |
32 | | /// @return The checksum result |
33 | | PCPP_DEPRECATED("Use computeChecksum(const ScalarBuffer<const uint16_t> vec[], size_t vecSize) instead") |
34 | | uint16_t computeChecksum(ScalarBuffer<uint16_t> vec[], size_t vecSize); |
35 | | |
36 | | /// Computes the checksum for Pseudo header |
37 | | /// @param[in] dataPtr Data pointer |
38 | | /// @param[in] dataLen Data length |
39 | | /// @param[in] ipAddrType IP address type(IPv4/IPv6) type @ref IPAddress::AddressType |
40 | | /// @param[in] protocolType Current protocol type @ref IPProtocolTypes |
41 | | /// @param[in] srcIPAddress Source IP Address |
42 | | /// @param[in] dstIPAddress Destination IP Address |
43 | | /// @return The checksum result |
44 | | PCPP_DEPRECATED("Use the templated computePseudoHdrChecksum overload instead") |
45 | | uint16_t computePseudoHdrChecksum(uint8_t* dataPtr, size_t dataLen, IPAddress::AddressType ipAddrType, |
46 | | uint8_t protocolType, IPAddress srcIPAddress, IPAddress dstIPAddress); |
47 | | |
48 | | /// Computes the checksum for Pseudo header |
49 | | /// @tparam IPAddrT The IP address type |
50 | | /// @param[in] dataPtr Data pointer |
51 | | /// @param[in] dataLen Data length |
52 | | /// @param[in] protocolType Current protocol type @ref IPProtocolTypes |
53 | | /// @param[in] srcIPAddress Source IP Address |
54 | | /// @param[in] dstIPAddress Destination IP Address |
55 | | /// @return The checksum result |
56 | | template <typename IPAddrT> |
57 | | uint16_t computePseudoHdrChecksum(const uint8_t* dataPtr, size_t dataLen, uint8_t protocolType, |
58 | | const IPAddrT& srcIPAddress, const IPAddrT& dstIPAddress); |
59 | | |
60 | | extern template uint16_t computePseudoHdrChecksum<IPv4Address>(const uint8_t* dataPtr, size_t dataLen, |
61 | | uint8_t protocolType, |
62 | | const IPv4Address& srcIPAddress, |
63 | | const IPv4Address& dstIPAddress); |
64 | | extern template uint16_t computePseudoHdrChecksum<IPv6Address>(const uint8_t* dataPtr, size_t dataLen, |
65 | | uint8_t protocolType, |
66 | | const IPv6Address& srcIPAddress, |
67 | | const IPv6Address& dstIPAddress); |
68 | | |
69 | | /// Computes Fowler-Noll-Vo (FNV-1) 32bit hash function on an array of byte buffers. The hash is calculated on each |
70 | | /// byte in each byte buffer, as if all byte buffers were one long byte buffer |
71 | | /// @param[in] vec An array of byte buffers (ScalarBuffer of type uint8_t) |
72 | | /// @param[in] vecSize The length of vec |
73 | | /// @return The 32bit hash value |
74 | | PCPP_DEPRECATED("Use fnvHash(const ScalarBuffer<const uint8_t> vec[], size_t vecSize) instead") |
75 | | uint32_t fnvHash(ScalarBuffer<uint8_t> vec[], size_t vecSize); |
76 | | |
77 | | /// Computes Fowler-Noll-Vo (FNV-1) 32bit hash function on an array of byte buffers. The hash is calculated on each |
78 | | /// byte in each byte buffer, as if all byte buffers were one long byte buffer |
79 | | /// @param[in] vec An array of byte buffers (ScalarBuffer of type uint8_t) |
80 | | /// @param[in] vecSize The length of vec |
81 | | /// @return The 32bit hash value |
82 | | uint32_t fnvHash(const ScalarBuffer<const uint8_t> vec[], size_t vecSize); |
83 | | |
84 | | /// Computes Fowler-Noll-Vo (FNV-1) 32bit hash function on a byte buffer |
85 | | /// @param[in] buffer The byte buffer |
86 | | /// @param[in] bufSize The size of the byte buffer |
87 | | /// @return The 32bit hash value |
88 | | PCPP_DEPRECATED("Use fnvHash(const uint8_t* buffer, size_t bufSize) instead") |
89 | | uint32_t fnvHash(uint8_t* buffer, size_t bufSize); |
90 | | |
91 | | /// Computes Fowler-Noll-Vo (FNV-1) 32bit hash function on a byte buffer |
92 | | /// @param[in] buffer The byte buffer |
93 | | /// @param[in] bufSize The size of the byte buffer |
94 | | /// @return The 32bit hash value |
95 | | uint32_t fnvHash(const uint8_t* buffer, size_t bufSize); |
96 | | |
97 | | /// A method that is given a packet and calculates a hash value by the packet's 5-tuple. Supports IPv4, IPv6, |
98 | | /// TCP and UDP. For packets which doesn't have 5-tuple (for example: packets which aren't IPv4/6 or aren't |
99 | | /// TCP/UDP) the value of 0 will be returned |
100 | | /// @param[in] packet The packet to calculate hash for |
101 | | /// @param[in] directionUnique Make hash value unique for each direction |
102 | | /// @return The hash value calculated for this packet or 0 if the packet doesn't contain 5-tuple |
103 | | PCPP_DEPRECATED("Use hash5Tuple(const Packet& packet, bool const directionUnique) instead") |
104 | | uint32_t hash5Tuple(Packet* packet, bool const& directionUnique = false); |
105 | | |
106 | | /// A method that is given a packet and calculates a hash value by the packet's 5-tuple. Supports IPv4, IPv6, |
107 | | /// TCP and UDP. For packets which doesn't have 5-tuple (for example: packets which aren't IPv4/6 or aren't |
108 | | /// TCP/UDP) the value of 0 will be returned |
109 | | /// @param[in] packet The packet to calculate hash for |
110 | | /// @param[in] directionUnique Make hash value unique for each direction |
111 | | /// @return The hash value calculated for this packet or 0 if the packet doesn't contain 5-tuple |
112 | | uint32_t hash5Tuple(const Packet& packet, bool directionUnique = false); |
113 | | |
114 | | /// A method that is given a packet and calculates a hash value by the packet's 2-tuple (IP src + IP dst). Supports |
115 | | /// IPv4 and IPv6. For packets which aren't IPv4/6 the value of 0 will be returned |
116 | | /// @param[in] packet The packet to calculate hash for |
117 | | /// @return The hash value calculated for this packet or 0 if the packet isn't IPv4/6 |
118 | | PCPP_DEPRECATED("Use hash2Tuple(const Packet& packet) instead") |
119 | | uint32_t hash2Tuple(Packet* packet); |
120 | | |
121 | | /// A method that is given a packet and calculates a hash value by the packet's 2-tuple (IP src + IP dst). Supports |
122 | | /// IPv4 and IPv6. For packets which aren't IPv4/6 the value of 0 will be returned |
123 | | /// @param[in] packet The packet to calculate hash for |
124 | | /// @return The hash value calculated for this packet or 0 if the packet isn't IPv4/6 |
125 | | uint32_t hash2Tuple(const Packet& packet); |
126 | | |
127 | | /// @class PacketSerializer |
128 | | /// Serializes a collection of packets into a single array. The array |
129 | | /// is opened immediately in the constructor and closed when the |
130 | | /// PacketSerializer is destroyed, so all packets must be added within |
131 | | /// its lifetime. Not copyable, since copying would either double-write |
132 | | /// the array or leave one copy's array permanently unclosed. |
133 | | class PacketSerializer |
134 | | { |
135 | | public: |
136 | | /// Opens the "packets" array field on the given serializer. |
137 | | /// @param[in] serializer The serializer to write the array into |
138 | | explicit PacketSerializer(ISerializer& serializer) : m_Packets(serializer.writeArray(Packets)) |
139 | 0 | {} |
140 | | |
141 | | PacketSerializer(const PacketSerializer&) = delete; |
142 | | PacketSerializer& operator=(const PacketSerializer&) = delete; |
143 | | |
144 | | /// Serializes one packet and appends it to the array. |
145 | | /// @param[in] packet The packet to add |
146 | | void addPacket(const Packet& packet); |
147 | | |
148 | | /// Serializes one packet and appends it to the array. |
149 | | /// @param[in] packet The packet to add |
150 | | void addPacket(const Packet* packet); |
151 | | |
152 | | /// Serializes every packet in the vector and appends them to the |
153 | | /// array, in order. |
154 | | /// @param[in] packets The packets to add |
155 | | void addPackets(const PointerVector<Packet>& packets); |
156 | | |
157 | | /// Serializes every packet in the vector and appends them to the |
158 | | /// array, in order. |
159 | | /// @param[in] packets The packets to add |
160 | | void addPackets(const std::vector<Packet>& packets); |
161 | | |
162 | | private: |
163 | | static const FieldDescriptor Packets; |
164 | | |
165 | | ArrayScope m_Packets; |
166 | | }; |
167 | | } // namespace pcpp |