blob: 0ef432903db55eff4721632455b083c9417ea825 [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
danilchap822a16f2016-09-27 09:27:47 -070014#include <utility>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/rtp_rtcp/source/byte_io.h"
17#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010020
21namespace webrtc {
22namespace rtcp {
danilchap2f255d82016-10-17 02:07:54 -070023constexpr uint8_t Sdes::kPacketType;
danilchap74e8df8f2017-03-16 08:04:08 -070024constexpr size_t Sdes::kMaxNumberOfChunks;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010025// Source Description (SDES) (RFC 3550).
26//
27// 0 1 2 3
28// 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
29// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30// header |V=2|P| SC | PT=SDES=202 | length |
31// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
32// chunk | SSRC/CSRC_1 |
33// 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34// | SDES items |
35// | ... |
36// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
37// chunk | SSRC/CSRC_2 |
38// 2 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39// | SDES items |
40// | ... |
41// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
42//
43// Canonical End-Point Identifier SDES Item (CNAME)
44//
45// 0 1 2 3
46// 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
47// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48// | CNAME=1 | length | user and domain name ...
49// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
danilchap9f35d552016-02-11 08:19:00 -080050namespace {
51const uint8_t kTerminatorTag = 0;
52const uint8_t kCnameTag = 1;
53
54size_t ChunkSize(const Sdes::Chunk& chunk) {
55 // Chunk:
56 // SSRC/CSRC (4 bytes) | CNAME=1 (1 byte) | length (1 byte) | cname | padding.
57 size_t chunk_payload_size = 4 + 1 + 1 + chunk.cname.size();
58 size_t padding_size = 4 - (chunk_payload_size % 4); // Minimum 1.
59 return chunk_payload_size + padding_size;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010060}
61} // namespace
62
danilchap9f35d552016-02-11 08:19:00 -080063Sdes::Sdes() : block_length_(RtcpPacket::kHeaderLength) {}
64
65Sdes::~Sdes() {}
66
danilchap4b9cad82016-03-30 13:34:31 -070067bool Sdes::Parse(const CommonHeader& packet) {
danilchap2f255d82016-10-17 02:07:54 -070068 RTC_DCHECK_EQ(packet.type(), kPacketType);
danilchap9f35d552016-02-11 08:19:00 -080069
danilchap4b9cad82016-03-30 13:34:31 -070070 uint8_t number_of_chunks = packet.count();
danilchap9f35d552016-02-11 08:19:00 -080071 std::vector<Chunk> chunks; // Read chunk into temporary array, so that in
72 // case of an error original array would stay
73 // unchanged.
74 size_t block_length = kHeaderLength;
75
danilchap4b9cad82016-03-30 13:34:31 -070076 if (packet.payload_size_bytes() % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(LS_WARNING) << "Invalid payload size "
78 << packet.payload_size_bytes()
79 << " bytes for a valid Sdes packet. Size should be"
80 " multiple of 4 bytes";
danilchap9f35d552016-02-11 08:19:00 -080081 }
danilchap4b9cad82016-03-30 13:34:31 -070082 const uint8_t* const payload_end =
83 packet.payload() + packet.payload_size_bytes();
84 const uint8_t* looking_at = packet.payload();
danilchap9f35d552016-02-11 08:19:00 -080085 chunks.resize(number_of_chunks);
86 for (size_t i = 0; i < number_of_chunks;) {
87 // Each chunk consumes at least 8 bytes.
88 if (payload_end - looking_at < 8) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010089 RTC_LOG(LS_WARNING) << "Not enough space left for chunk #" << (i + 1);
danilchap9f35d552016-02-11 08:19:00 -080090 return false;
91 }
92 chunks[i].ssrc = ByteReader<uint32_t>::ReadBigEndian(looking_at);
93 looking_at += sizeof(uint32_t);
94 bool cname_found = false;
95
96 uint8_t item_type;
97 while ((item_type = *(looking_at++)) != kTerminatorTag) {
98 if (looking_at >= payload_end) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010099 RTC_LOG(LS_WARNING)
100 << "Unexpected end of packet while reading chunk #" << (i + 1)
101 << ". Expected to find size of the text.";
danilchap9f35d552016-02-11 08:19:00 -0800102 return false;
103 }
104 uint8_t item_length = *(looking_at++);
105 const size_t kTerminatorSize = 1;
106 if (looking_at + item_length + kTerminatorSize > payload_end) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100107 RTC_LOG(LS_WARNING)
108 << "Unexpected end of packet while reading chunk #" << (i + 1)
109 << ". Expected to find text of size " << item_length;
danilchap9f35d552016-02-11 08:19:00 -0800110 return false;
111 }
112 if (item_type == kCnameTag) {
113 if (cname_found) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_WARNING)
115 << "Found extra CNAME for same ssrc in chunk #" << (i + 1);
danilchap9f35d552016-02-11 08:19:00 -0800116 return false;
117 }
118 cname_found = true;
119 chunks[i].cname.assign(reinterpret_cast<const char*>(looking_at),
120 item_length);
121 }
122 looking_at += item_length;
123 }
124 if (cname_found) {
125 // block_length calculates length of the packet that would be generated by
126 // Build/Create functions. Adjust it same way WithCName function does.
127 block_length += ChunkSize(chunks[i]);
128 ++i;
129 } else {
130 // RFC states CNAME item is mandatory.
131 // But same time it allows chunk without items.
132 // So while parsing, ignore all chunks without cname,
133 // but do not fail the parse.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100134 RTC_LOG(LS_WARNING) << "CNAME not found for ssrc " << chunks[i].ssrc;
danilchap9f35d552016-02-11 08:19:00 -0800135 --number_of_chunks;
136 chunks.resize(number_of_chunks);
137 }
138 // Adjust to 32bit boundary.
139 looking_at += (payload_end - looking_at) % 4;
140 }
141
142 chunks_ = std::move(chunks);
143 block_length_ = block_length;
144 return true;
145}
146
danilchap822a16f2016-09-27 09:27:47 -0700147bool Sdes::AddCName(uint32_t ssrc, std::string cname) {
danilchap9f35d552016-02-11 08:19:00 -0800148 RTC_DCHECK_LE(cname.length(), 0xffu);
149 if (chunks_.size() >= kMaxNumberOfChunks) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100150 RTC_LOG(LS_WARNING) << "Max SDES chunks reached.";
danilchap9f35d552016-02-11 08:19:00 -0800151 return false;
152 }
153 Chunk chunk;
154 chunk.ssrc = ssrc;
danilchap822a16f2016-09-27 09:27:47 -0700155 chunk.cname = std::move(cname);
danilchap9f35d552016-02-11 08:19:00 -0800156 chunks_.push_back(chunk);
157 block_length_ += ChunkSize(chunk);
158 return true;
159}
160
eladalon8fa21c42017-06-16 07:07:47 -0700161size_t Sdes::BlockLength() const {
162 return block_length_;
163}
164
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100165bool Sdes::Create(uint8_t* packet,
166 size_t* index,
167 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100168 PacketReadyCallback callback) const {
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100169 while (*index + BlockLength() > max_length) {
170 if (!OnBufferFull(packet, index, callback))
171 return false;
172 }
danilchap9f35d552016-02-11 08:19:00 -0800173 const size_t index_end = *index + BlockLength();
174 CreateHeader(chunks_.size(), kPacketType, HeaderLength(), packet, index);
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100175
danilchap9f35d552016-02-11 08:19:00 -0800176 for (const Sdes::Chunk& chunk : chunks_) {
177 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], chunk.ssrc);
178 ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 4], kCnameTag);
Danil Chapovalov6c170572017-09-15 16:48:14 +0200179 ByteWriter<uint8_t>::WriteBigEndian(
180 &packet[*index + 5], static_cast<uint8_t>(chunk.cname.size()));
danilchap9f35d552016-02-11 08:19:00 -0800181 memcpy(&packet[*index + 6], chunk.cname.data(), chunk.cname.size());
182 *index += (6 + chunk.cname.size());
183
184 // In each chunk, the list of items must be terminated by one or more null
185 // octets. The next chunk must start on a 32-bit boundary.
186 // CNAME (1 byte) | length (1 byte) | name | padding.
187 size_t padding_size = 4 - ((6 + chunk.cname.size()) % 4);
188 const int kPadding = 0;
189 memset(packet + *index, kPadding, padding_size);
190 *index += padding_size;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100191 }
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100192
danilchap9f35d552016-02-11 08:19:00 -0800193 RTC_CHECK_EQ(*index, index_end);
194 return true;
Danil Chapovalov1567d0b2016-01-15 17:34:27 +0100195}
196} // namespace rtcp
197} // namespace webrtc