Coverage Report

Created: 2025-07-11 07:47

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