Coverage Report

Created: 2026-10-17 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/header/UdpLayer.h
Line
Count
Source
1
#pragma once
2
3
#include "Layer.h"
4
#include <algorithm>
5
#include <iterator>
6
7
/// @file
8
9
/// @namespace pcpp
10
/// @brief The main namespace for the PcapPlusPlus lib
11
namespace pcpp
12
{
13
  /// @struct udphdr
14
  /// Represents an UDP protocol header
15
#pragma pack(push, 1)
16
  struct udphdr
17
  {
18
    /// Source port
19
    uint16_t portSrc;
20
    /// Destination port
21
    uint16_t portDst;
22
    /// Length of header and payload in bytes
23
    uint16_t length;
24
    ///  Error-checking of the header and data
25
    uint16_t headerChecksum;
26
  };
27
#pragma pack(pop)
28
  static_assert(sizeof(udphdr) == 8, "udphdr size is not 8 bytes");
29
30
  /// @class UdpLayer
31
  /// Represents an UDP (User Datagram Protocol) protocol layer
32
  class UdpLayer : public Layer
33
  {
34
  public:
35
    /// A constructor that creates the layer from an existing packet raw data
36
    /// @param[in] data A pointer to the raw data (will be casted to @ref udphdr)
37
    /// @param[in] dataLen Size of the data in bytes
38
    /// @param[in] prevLayer A pointer to the previous layer
39
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
40
    UdpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
41
601k
        : Layer(data, dataLen, prevLayer, packet, UDP)
42
601k
    {}
43
44
    /// A constructor that allocates a new UDP header with source and destination ports
45
    /// @param[in] portSrc Source UDP port address
46
    /// @param[in] portDst Destination UDP port
47
    UdpLayer(uint16_t portSrc, uint16_t portDst);
48
49
    /// Get a pointer to the UDP header. Notice this points directly to the data, so every change will change the
50
    /// actual packet data
51
    /// @return A pointer to the @ref udphdr
52
    udphdr* getUdpHeader() const
53
1.60M
    {
54
1.60M
      return reinterpret_cast<udphdr*>(m_Data);
55
1.60M
    }
56
57
    /// @return UDP source port
58
    uint16_t getSrcPort() const;
59
60
    /// @return UDP destination port
61
    uint16_t getDstPort() const;
62
63
    /// Calculate the checksum from header and data and possibly write the result to @ref udphdr#headerChecksum
64
    /// @param[in] writeResultToPacket If set to true then checksum result will be written to @ref
65
    /// udphdr#headerChecksum
66
    /// @return The checksum result
67
    uint16_t calculateChecksum(bool writeResultToPacket);
68
69
    /// A static method that validates the input data
70
    /// @param[in] data The pointer to the beginning of a byte stream of an UDP packet
71
    /// @param[in] dataLen The length of the byte stream
72
    /// @return True if the data is valid and can represent a UDP packet
73
    static inline bool isDataValid(const uint8_t* data, size_t dataLen);
74
75
    // implement abstract methods
76
77
    /// Currently identifies the following next layers: DnsLayer, DhcpLayer, VxlanLayer, SipRequestLayer,
78
    /// SipResponseLayer, RadiusLayer. Otherwise sets PayloadLayer
79
    void parseNextLayer() override;
80
81
    /// @return Size of @ref udphdr
82
    size_t getHeaderLen() const override
83
244k
    {
84
244k
      return sizeof(udphdr);
85
244k
    }
86
87
    /// Calculate @ref udphdr#headerChecksum field
88
    void computeCalculateFields() override;
89
90
    std::string toString() const override;
91
92
    OsiModelLayer getOsiModelLayer() const override
93
99.4k
    {
94
99.4k
      return OsiModelTransportLayer;
95
99.4k
    }
96
97
    /// @struct SerializedFields
98
    /// Fields written by UdpLayer's serializeLayer(), in addition to
99
    /// Layer::SerializedFields.
100
    struct SerializedFields : Layer::SerializedFields
101
    {
102
      /// @return All field descriptors for UdpLayer
103
      static std::vector<FieldDescriptor> all()
104
0
      {
105
0
        auto result = Layer::SerializedFields::all();
106
0
        std::initializer_list<FieldDescriptor> extra{ SrcPort, DstPort, Checksum };
107
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
108
0
        return result;
109
0
      }
110
111
      /// @brief Source port, in host byte order
112
      static const FieldDescriptor SrcPort;
113
114
      /// @brief Destination port, in host byte order
115
      static const FieldDescriptor DstPort;
116
117
      /// @brief Checksum, as a hex string
118
      static const FieldDescriptor Checksum;
119
    };
120
121
  protected:
122
    void serializeLayer(ObjectScope& serializer) const override;
123
  };
124
125
  bool UdpLayer::isDataValid(const uint8_t* data, size_t dataLen)
126
603k
  {
127
603k
    return data && dataLen >= sizeof(udphdr);
128
603k
  }
129
}  // namespace pcpp