Coverage Report

Created: 2025-07-11 07:47

/src/PcapPlusPlus/Packet++/header/SdpLayer.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include "IpAddress.h"
4
#include "TextBasedProtocol.h"
5
#include <vector>
6
7
/// @file
8
9
/// @namespace pcpp
10
/// @brief The main namespace for the PcapPlusPlus lib
11
namespace pcpp
12
{
13
14
/// Protocol version (v)
15
1.83k
#define PCPP_SDP_PROTOCOL_VERSION_FIELD "v"
16
/// Originator and session identifier (o)
17
916
#define PCPP_SDP_ORIGINATOR_FIELD "o"
18
/// Session name (s)
19
0
#define PCPP_SDP_SESSION_NAME_FIELD "s"
20
/// Session title, media title or short information (i)
21
#define PCPP_SDP_INFO_FIELD "i"
22
/// URI of description (u)
23
#define PCPP_SDP_URI_FIELD "u"
24
/// Email address with optional name of contacts (e)
25
#define PCPP_SDP_EMAIL_FIELD "e"
26
/// Phone number with optional name of contacts (p)
27
#define PCPP_SDP_PHONE_FIELD "p"
28
/// Connection information (c)
29
0
#define PCPP_SDP_CONNECTION_INFO_FIELD "c"
30
/// Bandwidth information (b)
31
#define PCPP_SDP_BANDWIDTH_FIELD "b"
32
/// Time the session is active (t)
33
0
#define PCPP_SDP_TIME_FIELD "t"
34
/// Repeat times (r)
35
#define PCPP_SDP_REPEAT_TIMES_FIELD "r"
36
/// Time zone adjustments (z)
37
#define PCPP_SDP_TIME_ZONE_FIELD "z"
38
/// Encryption key (k)
39
#define PCPP_SDP_ENCRYPTION_KEY_FIELD "k"
40
/// Media attribute (a)
41
581
#define PCPP_SDP_MEDIA_ATTRIBUTE_FIELD "a"
42
/// Media name and transport address (m)
43
2.24k
#define PCPP_SDP_MEDIA_NAME_FIELD "m"
44
45
  /// @class SdpLayer
46
  /// Represents a SDP (Session Description Protocol) message. SDP is a text-based protocol described by a series of
47
  /// fields, one per line (lines are separated by CRLF). The form of each field is as follows:<BR>
48
  /// @code
49
  /// [character]=[value]
50
  /// @endcode
51
  /// Each character represents a certain type of field. All field type are represented as macros in SdpLayer.h file
52
  /// (for example: PCPP_SDP_ORIGINATOR_FIELD is a macro for the originator field (o=) ).<BR>
53
  /// For more details about SDP structure please refer to its Wikipedia page:
54
  /// https://en.wikipedia.org/wiki/Session_Description_Protocol
55
  class SdpLayer : public TextBasedProtocolMessage
56
  {
57
  public:
58
    /// A constructor that creates the layer from an existing packet raw data
59
    /// @param[in] data A pointer to the raw data
60
    /// @param[in] dataLen Size of the data in bytes
61
    /// @param[in] prevLayer A pointer to the previous layer
62
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
63
    SdpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
64
65
    /// An empty c'tor which initialize an empty message with no fields
66
    SdpLayer();
67
68
    /// A c'tor which initializes a message with the minimum required fields.<BR>
69
    /// After this c'tor the message will look like this:
70
    ///
71
    /// @code
72
    /// v=0
73
    /// o=[username] [sessionID] [sessionVersion] IN IP4 [ipAddress]
74
    /// s=[sessionName]
75
    /// c=IN IP4 [ipAddress]
76
    /// t=[startTime] [endTime]
77
    /// @endcode
78
    ///
79
    /// @param[in] username User's login on the originating host
80
    /// @param[in] sessionID A globally unique identifier for the session
81
    /// @param[in] sessionVersion A version number for this session description
82
    /// @param[in] ipAddress The address of the machine from which the session is created
83
    /// @param[in] sessionName A textual session name
84
    /// @param[in] startTime The start time of the session
85
    /// @param[in] stopTime The stop time of the session
86
    SdpLayer(const std::string& username, long sessionID, long sessionVersion, IPv4Address ipAddress,
87
             const std::string& sessionName, long startTime, long stopTime);
88
89
    ~SdpLayer() override = default;
90
91
    /// A copy constructor for this layer. Inherits the base copy constructor and doesn't add
92
    /// anything else
93
    /// @param[in] other The instance to copy from
94
916
    SdpLayer(const SdpLayer& other) : TextBasedProtocolMessage(other)
95
916
    {}
96
97
    /// An assignment operator overload for this layer. Inherits the base assignment operator
98
    /// and doesn't add anything else
99
    /// @param[in] other The instance to copy from
100
    SdpLayer& operator=(const SdpLayer& other)
101
0
    {
102
0
      TextBasedProtocolMessage::operator=(other);
103
0
      return *this;
104
0
    }
105
106
    /// The 'originator' field (o=) contains the IP address of the the machine from which the session is created.
107
    /// This IP address can be used to track the RTP data relevant for the call. This method extracts this IP
108
    /// address from the 'originator' field and returns it. A value of IPv4Address#Zero will be returned in the
109
    /// following cases: (1) if 'originator' field doesn't exist; (2) if it doesn't contain the IP address; (3) if
110
    /// it contains a non-IPv4 address
111
    /// @return The IP address of the the machine from which the session is created
112
    IPv4Address getOwnerIPv4Address() const;
113
114
    /// The 'media-description' field (m=) contains the transport port to which the media stream is sent. This port
115
    /// can be used to track the RTP data relevant for the call. This method extracts this port from the
116
    /// 'media-description' field and returns it. Since a SDP message can contain several 'media-description'
117
    /// fields, one for each media type (e.g audio, image, etc.), the user is required to provide the media type. A
118
    /// value of 0 will be returned in the following cases: (1) if 'media-description' field doesn't exist; (2) if
119
    /// provided media type was not found; (3) if 'media-description' field didn't contain a port
120
    /// @param[in] mediaType The media type to search in
121
    /// @return The transport port to which the media stream is sent
122
    uint16_t getMediaPort(const std::string& mediaType) const;
123
124
    /// Adds a 'media-description' field (m=) with all necessary data and attribute fields (a=) with data relevant
125
    /// for this media.<BR> After this method is run the following block of fields will be added at the end of the
126
    /// message:
127
    ///
128
    /// @code
129
    /// m=[mediaType] [mediaPort] [mediaProtocol] [mediaFormat]
130
    /// a=[1st media attribute]
131
    /// a=[2nd media attribute]
132
    /// ...
133
    /// @endcode
134
    ///
135
    /// @param[in] mediaType The media type, usually "audio", "video", "text" or "image"
136
    /// @param[in] mediaPort The transport port to which the media stream is sent
137
    /// @param[in] mediaProtocol The transport protocol, usually "udp", "RTP/AVP" or "RTP/SAVP"
138
    /// @param[in] mediaFormat A space-separated list of media format description. For example: "8 96"
139
    /// @param[in] mediaAttributes A vector of media attributes. Each string in this vector will be
140
    /// translated into a 'media-attribute' field (a=)
141
    /// @return True if all fields were added properly or false if at least one field was failed to be added
142
    bool addMediaDescription(const std::string& mediaType, uint16_t mediaPort, const std::string& mediaProtocol,
143
                             const std::string& mediaFormat, const std::vector<std::string>& mediaAttributes);
144
145
    // overridden methods
146
147
    OsiModelLayer getOsiModelLayer() const override
148
916
    {
149
916
      return OsiModelSesionLayer;
150
916
    }
151
152
    std::string toString() const override;
153
154
  protected:
155
    // implementation of abstract methods
156
    char getHeaderFieldNameValueSeparator() const override
157
11.9k
    {
158
11.9k
      return '=';
159
11.9k
    }
160
    bool spacesAllowedBetweenHeaderFieldNameAndValue() const override
161
11.9k
    {
162
11.9k
      return false;
163
11.9k
    }
164
  };
165
}  // namespace pcpp