blob: e74746707c9fc2267dfd44d8251d9d13dbf2ee21 [file] [log] [blame]
Danil Chapovalov1567d0b2016-01-15 17:34:27 +01001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h"
12
danilchap822a16f2016-09-27 09:27:47 -070013#include <utility>
14
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010015#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
danilchap4b9cad82016-03-30 13:34:31 -070016#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/checks.h"
18#include "webrtc/rtc_base/logging.h"
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010019
20namespace webrtc {
21namespace rtcp {
danilchap2f255d82016-10-17 02:07:54 -070022constexpr uint8_t Sdes::kPacketType;
danilchap74e8df8f2017-03-16 08:04:08 -070023constexpr size_t Sdes::kMaxNumberOfChunks;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010024// Source Description (SDES) (RFC 3550).
25//
26// 0 1 2 3
27// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
28// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29// header |V=2|P| SC | PT=SDES=202 | length |
30// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
31// chunk | SSRC/CSRC_1 |
32// 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33// | SDES items |
34// | ... |
35// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
36// chunk | SSRC/CSRC_2 |
37// 2 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38// | SDES items |
39// | ... |
40// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
41//
42// Canonical End-Point Identifier SDES Item (CNAME)
43//
44// 0 1 2 3
45// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
46// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47// | CNAME=1 | length | user and domain name ...
48// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
danilchap9f35d552016-02-11 08:19:00 -080049namespace {
50const uint8_t kTerminatorTag = 0;
51const uint8_t kCnameTag = 1;
52
53size_t ChunkSize(const Sdes::Chunk& chunk) {
54 // Chunk:
55 // SSRC/CSRC (4 bytes) | CNAME=1 (1 byte) | length (1 byte) | cname | padding.
56 size_t chunk_payload_size = 4 + 1 + 1 + chunk.cname.size();
57 size_t padding_size = 4 - (chunk_payload_size % 4); // Minimum 1.
58 return chunk_payload_size + padding_size;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010059}
60} // namespace
61
danilchap9f35d552016-02-11 08:19:00 -080062Sdes::Sdes() : block_length_(RtcpPacket::kHeaderLength) {}
63
64Sdes::~Sdes() {}
65
danilchap4b9cad82016-03-30 13:34:31 -070066bool Sdes::Parse(const CommonHeader& packet) {
danilchap2f255d82016-10-17 02:07:54 -070067 RTC_DCHECK_EQ(packet.type(), kPacketType);
danilchap9f35d552016-02-11 08:19:00 -080068
danilchap4b9cad82016-03-30 13:34:31 -070069 uint8_t number_of_chunks = packet.count();
danilchap9f35d552016-02-11 08:19:00 -080070 std::vector<Chunk> chunks; // Read chunk into temporary array, so that in
71 // case of an error original array would stay
72 // unchanged.
73 size_t block_length = kHeaderLength;
74
danilchap4b9cad82016-03-30 13:34:31 -070075 if (packet.payload_size_bytes() % 4 != 0) {
76 LOG(LS_WARNING) << "Invalid payload size " << packet.payload_size_bytes()
danilchap9f35d552016-02-11 08:19:00 -080077 << " bytes for a valid Sdes packet. Size should be"
78 " multiple of 4 bytes";
79 }
danilchap4b9cad82016-03-30 13:34:31 -070080 const uint8_t* const payload_end =
81 packet.payload() + packet.payload_size_bytes();
82 const uint8_t* looking_at = packet.payload();
danilchap9f35d552016-02-11 08:19:00 -080083 chunks.resize(number_of_chunks);
84 for (size_t i = 0; i < number_of_chunks;) {
85 // Each chunk consumes at least 8 bytes.
86 if (payload_end - looking_at < 8) {
87 LOG(LS_WARNING) << "Not enough space left for chunk #" << (i + 1);
88 return false;
89 }
90 chunks[i].ssrc = ByteReader<uint32_t>::ReadBigEndian(looking_at);
91 looking_at += sizeof(uint32_t);
92 bool cname_found = false;
93
94 uint8_t item_type;
95 while ((item_type = *(looking_at++)) != kTerminatorTag) {
96 if (looking_at >= payload_end) {
97 LOG(LS_WARNING) << "Unexpected end of packet while reading chunk #"
98 << (i + 1) << ". Expected to find size of the text.";
99 return false;
100 }
101 uint8_t item_length = *(looking_at++);
102 const size_t kTerminatorSize = 1;
103 if (looking_at + item_length + kTerminatorSize > payload_end) {
104 LOG(LS_WARNING) << "Unexpected end of packet while reading chunk #"
105 << (i + 1) << ". Expected to find text of size "
106 << item_length;
107 return false;
108 }
109 if (item_type == kCnameTag) {
110 if (cname_found) {
111 LOG(LS_WARNING) << "Found extra CNAME for same ssrc in chunk #"
112 << (i + 1);
113 return false;
114 }
115 cname_found = true;
116 chunks[i].cname.assign(reinterpret_cast<const char*>(looking_at),
117 item_length);
118 }
119 looking_at += item_length;
120 }
121 if (cname_found) {
122 // block_length calculates length of the packet that would be generated by
123 // Build/Create functions. Adjust it same way WithCName function does.
124 block_length += ChunkSize(chunks[i]);
125 ++i;
126 } else {
127 // RFC states CNAME item is mandatory.
128 // But same time it allows chunk without items.
129 // So while parsing, ignore all chunks without cname,
130 // but do not fail the parse.
131 LOG(LS_WARNING) << "CNAME not found for ssrc " << chunks[i].ssrc;
132 --number_of_chunks;
133 chunks.resize(number_of_chunks);
134 }
135 // Adjust to 32bit boundary.
136 looking_at += (payload_end - looking_at) % 4;
137 }
138
139 chunks_ = std::move(chunks);
140 block_length_ = block_length;
141 return true;
142}
143
danilchap822a16f2016-09-27 09:27:47 -0700144bool Sdes::AddCName(uint32_t ssrc, std::string cname) {
danilchap9f35d552016-02-11 08:19:00 -0800145 RTC_DCHECK_LE(cname.length(), 0xffu);
146 if (chunks_.size() >= kMaxNumberOfChunks) {
147 LOG(LS_WARNING) << "Max SDES chunks reached.";
148 return false;
149 }
150 Chunk chunk;
151 chunk.ssrc = ssrc;
danilchap822a16f2016-09-27 09:27:47 -0700152 chunk.cname = std::move(cname);
danilchap9f35d552016-02-11 08:19:00 -0800153 chunks_.push_back(chunk);
154 block_length_ += ChunkSize(chunk);
155 return true;
156}
157
eladalon8fa21c42017-06-16 07:07:47 -0700158size_t Sdes::BlockLength() const {
159 return block_length_;
160}
161
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100162bool Sdes::Create(uint8_t* packet,
163 size_t* index,
164 size_t max_length,
165 RtcpPacket::PacketReadyCallback* callback) const {
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100166 while (*index + BlockLength() > max_length) {
167 if (!OnBufferFull(packet, index, callback))
168 return false;
169 }
danilchap9f35d552016-02-11 08:19:00 -0800170 const size_t index_end = *index + BlockLength();
171 CreateHeader(chunks_.size(), kPacketType, HeaderLength(), packet, index);
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100172
danilchap9f35d552016-02-11 08:19:00 -0800173 for (const Sdes::Chunk& chunk : chunks_) {
174 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], chunk.ssrc);
175 ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 4], kCnameTag);
176 ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 5],
177 chunk.cname.size());
178 memcpy(&packet[*index + 6], chunk.cname.data(), chunk.cname.size());
179 *index += (6 + chunk.cname.size());
180
181 // In each chunk, the list of items must be terminated by one or more null
182 // octets. The next chunk must start on a 32-bit boundary.
183 // CNAME (1 byte) | length (1 byte) | name | padding.
184 size_t padding_size = 4 - ((6 + chunk.cname.size()) % 4);
185 const int kPadding = 0;
186 memset(packet + *index, kPadding, padding_size);
187 *index += padding_size;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100188 }
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100189
danilchap9f35d552016-02-11 08:19:00 -0800190 RTC_CHECK_EQ(*index, index_end);
191 return true;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100192}
193} // namespace rtcp
194} // namespace webrtc