Danil Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h" |
Danil Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 12 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #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 Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace rtcp { |
danilchap | 2f255d8 | 2016-10-17 02:07:54 -0700 | [diff] [blame] | 22 | constexpr uint8_t Sdes::kPacketType; |
danilchap | 74e8df8f | 2017-03-16 08:04:08 -0700 | [diff] [blame] | 23 | constexpr size_t Sdes::kMaxNumberOfChunks; |
Danil Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 24 | // 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 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 49 | namespace { |
| 50 | const uint8_t kTerminatorTag = 0; |
| 51 | const uint8_t kCnameTag = 1; |
| 52 | |
| 53 | size_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 Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 59 | } |
| 60 | } // namespace |
| 61 | |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 62 | Sdes::Sdes() : block_length_(RtcpPacket::kHeaderLength) {} |
| 63 | |
| 64 | Sdes::~Sdes() {} |
| 65 | |
danilchap | 4b9cad8 | 2016-03-30 13:34:31 -0700 | [diff] [blame] | 66 | bool Sdes::Parse(const CommonHeader& packet) { |
danilchap | 2f255d8 | 2016-10-17 02:07:54 -0700 | [diff] [blame] | 67 | RTC_DCHECK_EQ(packet.type(), kPacketType); |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 68 | |
danilchap | 4b9cad8 | 2016-03-30 13:34:31 -0700 | [diff] [blame] | 69 | uint8_t number_of_chunks = packet.count(); |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 70 | 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 | |
danilchap | 4b9cad8 | 2016-03-30 13:34:31 -0700 | [diff] [blame] | 75 | if (packet.payload_size_bytes() % 4 != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 76 | 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"; |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 80 | } |
danilchap | 4b9cad8 | 2016-03-30 13:34:31 -0700 | [diff] [blame] | 81 | const uint8_t* const payload_end = |
| 82 | packet.payload() + packet.payload_size_bytes(); |
| 83 | const uint8_t* looking_at = packet.payload(); |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 84 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 88 | RTC_LOG(LS_WARNING) << "Not enough space left for chunk #" << (i + 1); |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 89 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 98 | RTC_LOG(LS_WARNING) |
| 99 | << "Unexpected end of packet while reading chunk #" << (i + 1) |
| 100 | << ". Expected to find size of the text."; |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 101 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 106 | RTC_LOG(LS_WARNING) |
| 107 | << "Unexpected end of packet while reading chunk #" << (i + 1) |
| 108 | << ". Expected to find text of size " << item_length; |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | if (item_type == kCnameTag) { |
| 112 | if (cname_found) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 113 | RTC_LOG(LS_WARNING) |
| 114 | << "Found extra CNAME for same ssrc in chunk #" << (i + 1); |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 115 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 133 | RTC_LOG(LS_WARNING) << "CNAME not found for ssrc " << chunks[i].ssrc; |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 134 | --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 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 146 | bool Sdes::AddCName(uint32_t ssrc, std::string cname) { |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 147 | RTC_DCHECK_LE(cname.length(), 0xffu); |
| 148 | if (chunks_.size() >= kMaxNumberOfChunks) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 149 | RTC_LOG(LS_WARNING) << "Max SDES chunks reached."; |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | Chunk chunk; |
| 153 | chunk.ssrc = ssrc; |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 154 | chunk.cname = std::move(cname); |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 155 | chunks_.push_back(chunk); |
| 156 | block_length_ += ChunkSize(chunk); |
| 157 | return true; |
| 158 | } |
| 159 | |
eladalon | 8fa21c4 | 2017-06-16 07:07:47 -0700 | [diff] [blame] | 160 | size_t Sdes::BlockLength() const { |
| 161 | return block_length_; |
| 162 | } |
| 163 | |
Danil Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 164 | bool Sdes::Create(uint8_t* packet, |
| 165 | size_t* index, |
| 166 | size_t max_length, |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame^] | 167 | PacketReadyCallback callback) const { |
Danil Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 168 | while (*index + BlockLength() > max_length) { |
| 169 | if (!OnBufferFull(packet, index, callback)) |
| 170 | return false; |
| 171 | } |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 172 | const size_t index_end = *index + BlockLength(); |
| 173 | CreateHeader(chunks_.size(), kPacketType, HeaderLength(), packet, index); |
Danil Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 174 | |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 175 | 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 Chapovalov | 6c17057 | 2017-09-15 16:48:14 +0200 | [diff] [blame] | 178 | ByteWriter<uint8_t>::WriteBigEndian( |
| 179 | &packet[*index + 5], static_cast<uint8_t>(chunk.cname.size())); |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 180 | 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 Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 190 | } |
Danil Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 191 | |
danilchap | 9f35d55 | 2016-02-11 08:19:00 -0800 | [diff] [blame] | 192 | RTC_CHECK_EQ(*index, index_end); |
| 193 | return true; |
Danil Chapovalov | 1567d0b | 2016-01-15 17:34:27 +0100 | [diff] [blame] | 194 | } |
| 195 | } // namespace rtcp |
| 196 | } // namespace webrtc |