Coverage Report

Created: 2023-01-17 06:15

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