/src/PcapPlusPlus/Packet++/header/UdpLayer.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "Layer.h" |
4 | | |
5 | | /// @file |
6 | | |
7 | | /** |
8 | | * \namespace pcpp |
9 | | * \brief The main namespace for the PcapPlusPlus lib |
10 | | */ |
11 | | namespace pcpp |
12 | | { |
13 | | |
14 | | /** |
15 | | * @struct udphdr |
16 | | * Represents an UDP protocol header |
17 | | */ |
18 | | #pragma pack(push,1) |
19 | | struct udphdr |
20 | | { |
21 | | /** Source port */ |
22 | | uint16_t portSrc; |
23 | | /** Destination port */ |
24 | | uint16_t portDst; |
25 | | /** Length of header and payload in bytes */ |
26 | | uint16_t length; |
27 | | /** Error-checking of the header and data */ |
28 | | uint16_t headerChecksum; |
29 | | }; |
30 | | #pragma pack(pop) |
31 | | |
32 | | |
33 | | /** |
34 | | * @class UdpLayer |
35 | | * Represents an UDP (User Datagram Protocol) protocol layer |
36 | | */ |
37 | | class UdpLayer : public Layer |
38 | | { |
39 | | public: |
40 | | /** |
41 | | * A constructor that creates the layer from an existing packet raw data |
42 | | * @param[in] data A pointer to the raw data (will be casted to @ref udphdr) |
43 | | * @param[in] dataLen Size of the data in bytes |
44 | | * @param[in] prevLayer A pointer to the previous layer |
45 | | * @param[in] packet A pointer to the Packet instance where layer will be stored in |
46 | | */ |
47 | 291k | UdpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = UDP; } |
48 | | |
49 | | /** |
50 | | * A constructor that allocates a new UDP header with source and destination ports |
51 | | * @param[in] portSrc Source UDP port address |
52 | | * @param[in] portDst Destination UDP port |
53 | | */ |
54 | | UdpLayer(uint16_t portSrc, uint16_t portDst); |
55 | | |
56 | | /** |
57 | | * Get a pointer to the UDP header. Notice this points directly to the data, so every change will change the actual packet data |
58 | | * @return A pointer to the @ref udphdr |
59 | | */ |
60 | 805k | udphdr* getUdpHeader() const { return (udphdr*)m_Data; } |
61 | | |
62 | | /** |
63 | | * @return UDP source port |
64 | | */ |
65 | | uint16_t getSrcPort() const; |
66 | | |
67 | | /** |
68 | | * @return UDP destination port |
69 | | */ |
70 | | uint16_t getDstPort() const; |
71 | | |
72 | | /** |
73 | | * Calculate the checksum from header and data and possibly write the result to @ref udphdr#headerChecksum |
74 | | * @param[in] writeResultToPacket If set to true then checksum result will be written to @ref udphdr#headerChecksum |
75 | | * @return The checksum result |
76 | | */ |
77 | | uint16_t calculateChecksum(bool writeResultToPacket); |
78 | | |
79 | | // implement abstract methods |
80 | | |
81 | | /** |
82 | | * Currently identifies the following next layers: DnsLayer, DhcpLayer, VxlanLayer, SipRequestLayer, SipResponseLayer, |
83 | | * RadiusLayer. Otherwise sets PayloadLayer |
84 | | */ |
85 | | void parseNextLayer(); |
86 | | |
87 | | /** |
88 | | * @return Size of @ref udphdr |
89 | | */ |
90 | 196k | size_t getHeaderLen() const { return sizeof(udphdr); } |
91 | | |
92 | | /** |
93 | | * Calculate @ref udphdr#headerChecksum field |
94 | | */ |
95 | | void computeCalculateFields(); |
96 | | |
97 | | std::string toString() const; |
98 | | |
99 | 55.7k | OsiModelLayer getOsiModelLayer() const { return OsiModelTransportLayer; } |
100 | | }; |
101 | | |
102 | | } // namespace pcpp |