blob: 337c8b04a8487092e46ce0a36828e43f442ced7b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010012
danilchap822a16f2016-09-27 09:27:47 -070013#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/source/byte_io.h"
16#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
17#include "rtc_base/checks.h"
18#include "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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010076 RTC_LOG(LS_WARNING) << "Invalid payload size "
77 << packet.payload_size_bytes()
78 << " bytes for a valid Sdes packet. Size should be"
79 " multiple of 4 bytes";
danilchap9f35d552016-02-11 08:19:00 -080080 }
danilchap4b9cad82016-03-30 13:34:31 -070081 const uint8_t* const payload_end =
82 packet.payload() + packet.payload_size_bytes();
83 const uint8_t* looking_at = packet.payload();
danilchap9f35d552016-02-11 08:19:00 -080084 chunks.resize(number_of_chunks);
85 for (size_t i = 0; i < number_of_chunks;) {
86 // Each chunk consumes at least 8 bytes.
87 if (payload_end - looking_at < 8) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010088 RTC_LOG(LS_WARNING) << "Not enough space left for chunk #" << (i + 1);
danilchap9f35d552016-02-11 08:19:00 -080089 return false;
90 }
91 chunks[i].ssrc = ByteReader<uint32_t>::ReadBigEndian(looking_at);
92 looking_at += sizeof(uint32_t);
93 bool cname_found = false;
94
95 uint8_t item_type;
96 while ((item_type = *(looking_at++)) != kTerminatorTag) {
97 if (looking_at >= payload_end) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010098 RTC_LOG(LS_WARNING)
99 << "Unexpected end of packet while reading chunk #" << (i + 1)
100 << ". Expected to find size of the text.";
danilchap9f35d552016-02-11 08:19:00 -0800101 return false;
102 }
103 uint8_t item_length = *(looking_at++);
104 const size_t kTerminatorSize = 1;
105 if (looking_at + item_length + kTerminatorSize > payload_end) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100106 RTC_LOG(LS_WARNING)
107 << "Unexpected end of packet while reading chunk #" << (i + 1)
108 << ". Expected to find text of size " << item_length;
danilchap9f35d552016-02-11 08:19:00 -0800109 return false;
110 }
111 if (item_type == kCnameTag) {
112 if (cname_found) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100113 RTC_LOG(LS_WARNING)
114 << "Found extra CNAME for same ssrc in chunk #" << (i + 1);
danilchap9f35d552016-02-11 08:19:00 -0800115 return false;
116 }
117 cname_found = true;
118 chunks[i].cname.assign(reinterpret_cast<const char*>(looking_at),
119 item_length);
120 }
121 looking_at += item_length;
122 }
123 if (cname_found) {
124 // block_length calculates length of the packet that would be generated by
125 // Build/Create functions. Adjust it same way WithCName function does.
126 block_length += ChunkSize(chunks[i]);
127 ++i;
128 } else {
129 // RFC states CNAME item is mandatory.
130 // But same time it allows chunk without items.
131 // So while parsing, ignore all chunks without cname,
132 // but do not fail the parse.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100133 RTC_LOG(LS_WARNING) << "CNAME not found for ssrc " << chunks[i].ssrc;
danilchap9f35d552016-02-11 08:19:00 -0800134 --number_of_chunks;
135 chunks.resize(number_of_chunks);
136 }
137 // Adjust to 32bit boundary.
138 looking_at += (payload_end - looking_at) % 4;
139 }
140
141 chunks_ = std::move(chunks);
142 block_length_ = block_length;
143 return true;
144}
145
danilchap822a16f2016-09-27 09:27:47 -0700146bool Sdes::AddCName(uint32_t ssrc, std::string cname) {
danilchap9f35d552016-02-11 08:19:00 -0800147 RTC_DCHECK_LE(cname.length(), 0xffu);
148 if (chunks_.size() >= kMaxNumberOfChunks) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(LS_WARNING) << "Max SDES chunks reached.";
danilchap9f35d552016-02-11 08:19:00 -0800150 return false;
151 }
152 Chunk chunk;
153 chunk.ssrc = ssrc;
danilchap822a16f2016-09-27 09:27:47 -0700154 chunk.cname = std::move(cname);
danilchap9f35d552016-02-11 08:19:00 -0800155 chunks_.push_back(chunk);
156 block_length_ += ChunkSize(chunk);
157 return true;
158}
159
eladalon8fa21c42017-06-16 07:07:47 -0700160size_t Sdes::BlockLength() const {
161 return block_length_;
162}
163
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100164bool Sdes::Create(uint8_t* packet,
165 size_t* index,
166 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100167 PacketReadyCallback callback) const {
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100168 while (*index + BlockLength() > max_length) {
169 if (!OnBufferFull(packet, index, callback))
170 return false;
171 }
danilchap9f35d552016-02-11 08:19:00 -0800172 const size_t index_end = *index + BlockLength();
173 CreateHeader(chunks_.size(), kPacketType, HeaderLength(), packet, index);
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100174
danilchap9f35d552016-02-11 08:19:00 -0800175 for (const Sdes::Chunk& chunk : chunks_) {
176 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], chunk.ssrc);
177 ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 4], kCnameTag);
Danil Chapovalov6c170572017-09-15 16:48:14 +0200178 ByteWriter<uint8_t>::WriteBigEndian(
179 &packet[*index + 5], static_cast<uint8_t>(chunk.cname.size()));
danilchap9f35d552016-02-11 08:19:00 -0800180 memcpy(&packet[*index + 6], chunk.cname.data(), chunk.cname.size());
181 *index += (6 + chunk.cname.size());
182
183 // In each chunk, the list of items must be terminated by one or more null
184 // octets. The next chunk must start on a 32-bit boundary.
185 // CNAME (1 byte) | length (1 byte) | name | padding.
186 size_t padding_size = 4 - ((6 + chunk.cname.size()) % 4);
187 const int kPadding = 0;
188 memset(packet + *index, kPadding, padding_size);
189 *index += padding_size;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100190 }
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100191
danilchap9f35d552016-02-11 08:19:00 -0800192 RTC_CHECK_EQ(*index, index_end);
193 return true;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100194}
195} // namespace rtcp
196} // namespace webrtc