Coverage Report

Created: 2025-11-09 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/src/SdpLayer.cpp
Line
Count
Source
1
0
#define LOG_MODULE PacketLogModuleSdpLayer
2
3
#include "SdpLayer.h"
4
#include "Logger.h"
5
#include <sstream>
6
7
namespace pcpp
8
{
9
10
  std::vector<std::string> splitByWhiteSpaces(const std::string& str)
11
0
  {
12
0
    std::string buf;
13
0
    std::stringstream stream(str);
14
0
    std::vector<std::string> result;
15
0
    while (stream >> buf)
16
0
      result.push_back(buf);
17
18
0
    return result;
19
0
  }
20
21
  SdpLayer::SdpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
22
0
      : TextBasedProtocolMessage(data, dataLen, prevLayer, packet, SDP)
23
0
  {
24
0
    m_FieldsOffset = 0;
25
0
    parseFields();
26
0
  }
27
28
  SdpLayer::SdpLayer()
29
0
  {
30
0
    m_Protocol = SDP;
31
0
    m_FieldsOffset = 0;
32
0
  }
33
34
  SdpLayer::SdpLayer(const std::string& username, long sessionID, long sessionVersion, IPv4Address ipAddress,
35
                     const std::string& sessionName, long startTime, long stopTime)
36
0
  {
37
0
    m_Protocol = SDP;
38
0
    m_FieldsOffset = 0;
39
40
    // must initialize m_Data otherwise addField() will fail while trying to extend the layer
41
    // initializing in length of 1 but keeping m_DataLen with value of 0.
42
    // when extending the field m_Data is purged so there isn't a memory leak here
43
0
    m_Data = new uint8_t[1];
44
0
    m_DataLen = 0;
45
46
0
    addField(PCPP_SDP_PROTOCOL_VERSION_FIELD, "0");
47
48
0
    std::stringstream sessionIDStream;
49
0
    sessionIDStream << sessionID;
50
0
    std::stringstream sessionVersionStream;
51
0
    sessionVersionStream << sessionVersion;
52
0
    std::string networkInfo = "IN IP4 " + ipAddress.toString();
53
0
    std::string originatorFieldValue =
54
0
        username + " " + sessionIDStream.str() + " " + sessionVersionStream.str() + " " + networkInfo;
55
0
    addField(PCPP_SDP_ORIGINATOR_FIELD, originatorFieldValue);
56
57
0
    addField(PCPP_SDP_SESSION_NAME_FIELD, sessionName);
58
59
0
    addField(PCPP_SDP_CONNECTION_INFO_FIELD, networkInfo);
60
61
0
    std::stringstream startTimeStream;
62
0
    startTimeStream << startTime;
63
0
    std::stringstream stopTimeStream;
64
0
    stopTimeStream << stopTime;
65
0
    addField(PCPP_SDP_TIME_FIELD, startTimeStream.str() + " " + stopTimeStream.str());
66
0
  }
67
68
  std::string SdpLayer::toString() const
69
0
  {
70
0
    return "SDP Layer";
71
0
  }
72
73
  IPv4Address SdpLayer::getOwnerIPv4Address() const
74
0
  {
75
0
    HeaderField* originator = getFieldByName(PCPP_SDP_ORIGINATOR_FIELD);
76
0
    if (originator == nullptr)
77
0
      return IPv4Address::Zero;
78
79
0
    std::vector<std::string> tokens = splitByWhiteSpaces(originator->getFieldValue());
80
0
    if (tokens.size() < 6)
81
0
      return IPv4Address::Zero;
82
83
0
    if (tokens[3] != "IN" || tokens[4] != "IP4")
84
0
      return IPv4Address::Zero;
85
86
0
    try
87
0
    {
88
0
      return IPv4Address(tokens[5]);
89
0
    }
90
0
    catch (const std::exception&)
91
0
    {
92
0
      return IPv4Address::Zero;
93
0
    }
94
0
  }
95
96
  uint16_t SdpLayer::getMediaPort(const std::string& mediaType) const
97
0
  {
98
0
    int mediaFieldIndex = 0;
99
0
    HeaderField* mediaDesc = getFieldByName(PCPP_SDP_MEDIA_NAME_FIELD, mediaFieldIndex);
100
101
0
    while (mediaDesc != nullptr)
102
0
    {
103
0
      std::vector<std::string> tokens = splitByWhiteSpaces(mediaDesc->getFieldValue());
104
105
0
      if (tokens.size() >= 2 && tokens[0] == mediaType)
106
0
        return atoi(tokens[1].c_str());
107
108
0
      mediaFieldIndex++;
109
0
      mediaDesc = getFieldByName(PCPP_SDP_MEDIA_NAME_FIELD, mediaFieldIndex);
110
0
    }
111
112
0
    return 0;
113
0
  }
114
115
  bool SdpLayer::addMediaDescription(const std::string& mediaType, uint16_t mediaPort,
116
                                     const std::string& mediaProtocol, const std::string& mediaFormat,
117
                                     const std::vector<std::string>& mediaAttributes)
118
0
  {
119
0
    std::stringstream portStream;
120
0
    portStream << mediaPort;
121
122
0
    std::string mediaFieldValue = mediaType + " " + portStream.str() + " " + mediaProtocol + " " + mediaFormat;
123
0
    if (addField(PCPP_SDP_MEDIA_NAME_FIELD, mediaFieldValue) == nullptr)
124
0
    {
125
0
      PCPP_LOG_ERROR("Failed to add media description field");
126
0
      return false;
127
0
    }
128
129
0
    for (const auto& iter : mediaAttributes)
130
0
    {
131
0
      if (addField(PCPP_SDP_MEDIA_ATTRIBUTE_FIELD, iter) == nullptr)
132
0
      {
133
0
        PCPP_LOG_ERROR("Failed to add media attribute '" << iter << "'");
134
0
        return false;
135
0
      }
136
0
    }
137
138
0
    return true;
139
0
  }
140
141
}  // namespace pcpp