/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(ScalarBuffer<uint16_t> vec[], size_t vecSize); |
28 | | |
29 | | /// Computes the checksum for Pseudo header |
30 | | /// @param[in] dataPtr Data pointer |
31 | | /// @param[in] dataLen Data length |
32 | | /// @param[in] ipAddrType IP address type(IPv4/IPv6) type @ref IPAddress::AddressType |
33 | | /// @param[in] protocolType Current protocol type @ref IPProtocolTypes |
34 | | /// @param[in] srcIPAddress Source IP Address |
35 | | /// @param[in] dstIPAddress Destination IP Address |
36 | | /// @return The checksum result |
37 | | uint16_t computePseudoHdrChecksum(uint8_t* dataPtr, size_t dataLen, IPAddress::AddressType ipAddrType, |
38 | | uint8_t protocolType, IPAddress srcIPAddress, IPAddress dstIPAddress); |
39 | | |
40 | | /// Computes Fowler-Noll-Vo (FNV-1) 32bit hash function on an array of byte buffers. The hash is calculated on each |
41 | | /// byte in each byte buffer, as if all byte buffers were one long byte buffer |
42 | | /// @param[in] vec An array of byte buffers (ScalarBuffer of type uint8_t) |
43 | | /// @param[in] vecSize The length of vec |
44 | | /// @return The 32bit hash value |
45 | | uint32_t fnvHash(ScalarBuffer<uint8_t> vec[], size_t vecSize); |
46 | | |
47 | | /// Computes Fowler-Noll-Vo (FNV-1) 32bit hash function on a byte buffer |
48 | | /// @param[in] buffer The byte buffer |
49 | | /// @param[in] bufSize The size of the byte buffer |
50 | | /// @return The 32bit hash value |
51 | | uint32_t fnvHash(uint8_t* buffer, size_t bufSize); |
52 | | |
53 | | /// A method that is given a packet and calculates a hash value by the packet's 5-tuple. Supports IPv4, IPv6, |
54 | | /// TCP and UDP. For packets which doesn't have 5-tuple (for example: packets which aren't IPv4/6 or aren't |
55 | | /// TCP/UDP) the value of 0 will be returned |
56 | | /// @param[in] packet The packet to calculate hash for |
57 | | /// @param[in] directionUnique Make hash value unique for each direction |
58 | | /// @return The hash value calculated for this packet or 0 if the packet doesn't contain 5-tuple |
59 | | uint32_t hash5Tuple(Packet* packet, bool const& directionUnique = false); |
60 | | |
61 | | /// A method that is given a packet and calculates a hash value by the packet's 2-tuple (IP src + IP dst). Supports |
62 | | /// IPv4 and IPv6. For packets which aren't IPv4/6 the value of 0 will be returned |
63 | | /// @param[in] packet The packet to calculate hash for |
64 | | /// @return The hash value calculated for this packet or 0 if the packet isn't IPv4/6 |
65 | | uint32_t hash2Tuple(Packet* packet); |
66 | | |
67 | | /// @class PacketSerializer |
68 | | /// Serializes a collection of packets into a single array. The array |
69 | | /// is opened immediately in the constructor and closed when the |
70 | | /// PacketSerializer is destroyed, so all packets must be added within |
71 | | /// its lifetime. Not copyable, since copying would either double-write |
72 | | /// the array or leave one copy's array permanently unclosed. |
73 | | class PacketSerializer |
74 | | { |
75 | | public: |
76 | | /// Opens the "packets" array field on the given serializer. |
77 | | /// @param[in] serializer The serializer to write the array into |
78 | | explicit PacketSerializer(ISerializer& serializer) : m_Packets(serializer.writeArray(Packets)) |
79 | 0 | {} |
80 | | |
81 | | PacketSerializer(const PacketSerializer&) = delete; |
82 | | PacketSerializer& operator=(const PacketSerializer&) = delete; |
83 | | |
84 | | /// Serializes one packet and appends it to the array. |
85 | | /// @param[in] packet The packet to add |
86 | | void addPacket(const Packet& packet); |
87 | | |
88 | | /// Serializes one packet and appends it to the array. |
89 | | /// @param[in] packet The packet to add |
90 | | void addPacket(const Packet* packet); |
91 | | |
92 | | /// Serializes every packet in the vector and appends them to the |
93 | | /// array, in order. |
94 | | /// @param[in] packets The packets to add |
95 | | void addPackets(const PointerVector<Packet>& packets); |
96 | | |
97 | | /// Serializes every packet in the vector and appends them to the |
98 | | /// array, in order. |
99 | | /// @param[in] packets The packets to add |
100 | | void addPackets(const std::vector<Packet>& packets); |
101 | | |
102 | | private: |
103 | | static const FieldDescriptor Packets; |
104 | | |
105 | | ArrayScope m_Packets; |
106 | | }; |
107 | | } // namespace pcpp |