danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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/bye.h" |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [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" |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace rtcp { |
danilchap | 2f255d8 | 2016-10-17 02:07:54 -0700 | [diff] [blame] | 22 | constexpr uint8_t Bye::kPacketType; |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 23 | // Bye packet (BYE) (RFC 3550). |
| 24 | // |
| 25 | // 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 |
| 26 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 27 | // |V=2|P| SC | PT=BYE=203 | length | |
| 28 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 29 | // | SSRC/CSRC | |
| 30 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 31 | // : ... : |
| 32 | // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 33 | // (opt) | length | reason for leaving ... |
| 34 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 35 | Bye::Bye() : sender_ssrc_(0) {} |
| 36 | |
eladalon | 8fa21c4 | 2017-06-16 07:07:47 -0700 | [diff] [blame] | 37 | Bye::~Bye() = default; |
| 38 | |
danilchap | 54a2069 | 2016-04-05 08:48:11 -0700 | [diff] [blame] | 39 | bool Bye::Parse(const CommonHeader& packet) { |
danilchap | 2f255d8 | 2016-10-17 02:07:54 -0700 | [diff] [blame] | 40 | RTC_DCHECK_EQ(packet.type(), kPacketType); |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 41 | |
danilchap | 54a2069 | 2016-04-05 08:48:11 -0700 | [diff] [blame] | 42 | const uint8_t src_count = packet.count(); |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 43 | // Validate packet. |
danilchap | 54a2069 | 2016-04-05 08:48:11 -0700 | [diff] [blame] | 44 | if (packet.payload_size_bytes() < 4u * src_count) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame^] | 45 | RTC_LOG(LS_WARNING) |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 46 | << "Packet is too small to contain CSRCs it promise to have."; |
| 47 | return false; |
| 48 | } |
danilchap | 54a2069 | 2016-04-05 08:48:11 -0700 | [diff] [blame] | 49 | const uint8_t* const payload = packet.payload(); |
| 50 | bool has_reason = packet.payload_size_bytes() > 4u * src_count; |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 51 | uint8_t reason_length = 0; |
| 52 | if (has_reason) { |
| 53 | reason_length = payload[4u * src_count]; |
danilchap | 54a2069 | 2016-04-05 08:48:11 -0700 | [diff] [blame] | 54 | if (packet.payload_size_bytes() - 4u * src_count < 1u + reason_length) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame^] | 55 | RTC_LOG(LS_WARNING) << "Invalid reason length: " << reason_length; |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | // Once sure packet is valid, copy values. |
| 60 | if (src_count == 0) { // A count value of zero is valid, but useless. |
| 61 | sender_ssrc_ = 0; |
| 62 | csrcs_.clear(); |
| 63 | } else { |
| 64 | sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(payload); |
| 65 | csrcs_.resize(src_count - 1); |
| 66 | for (size_t i = 1; i < src_count; ++i) |
| 67 | csrcs_[i - 1] = ByteReader<uint32_t>::ReadBigEndian(&payload[4 * i]); |
| 68 | } |
| 69 | |
| 70 | if (has_reason) { |
| 71 | reason_.assign(reinterpret_cast<const char*>(&payload[4u * src_count + 1]), |
| 72 | reason_length); |
| 73 | } else { |
| 74 | reason_.clear(); |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool Bye::Create(uint8_t* packet, |
| 81 | size_t* index, |
| 82 | size_t max_length, |
| 83 | RtcpPacket::PacketReadyCallback* callback) const { |
| 84 | while (*index + BlockLength() > max_length) { |
| 85 | if (!OnBufferFull(packet, index, callback)) |
| 86 | return false; |
| 87 | } |
| 88 | const size_t index_end = *index + BlockLength(); |
| 89 | |
| 90 | CreateHeader(1 + csrcs_.size(), kPacketType, HeaderLength(), packet, index); |
| 91 | // Store srcs of the leaving clients. |
| 92 | ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], sender_ssrc_); |
| 93 | *index += sizeof(uint32_t); |
| 94 | for (uint32_t csrc : csrcs_) { |
| 95 | ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], csrc); |
| 96 | *index += sizeof(uint32_t); |
| 97 | } |
| 98 | // Store the reason to leave. |
| 99 | if (!reason_.empty()) { |
Danil Chapovalov | 6c17057 | 2017-09-15 16:48:14 +0200 | [diff] [blame] | 100 | uint8_t reason_length = static_cast<uint8_t>(reason_.size()); |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 101 | packet[(*index)++] = reason_length; |
| 102 | memcpy(&packet[*index], reason_.data(), reason_length); |
| 103 | *index += reason_length; |
| 104 | // Add padding bytes if needed. |
| 105 | size_t bytes_to_pad = index_end - *index; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 106 | RTC_DCHECK_LE(bytes_to_pad, 3); |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 107 | if (bytes_to_pad > 0) { |
| 108 | memset(&packet[*index], 0, bytes_to_pad); |
| 109 | *index += bytes_to_pad; |
| 110 | } |
| 111 | } |
| 112 | RTC_DCHECK_EQ(index_end, *index); |
| 113 | return true; |
| 114 | } |
| 115 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 116 | bool Bye::SetCsrcs(std::vector<uint32_t> csrcs) { |
| 117 | if (csrcs.size() > kMaxNumberOfCsrcs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame^] | 118 | RTC_LOG(LS_WARNING) << "Too many CSRCs for Bye packet."; |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 119 | return false; |
| 120 | } |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 121 | csrcs_ = std::move(csrcs); |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 125 | void Bye::SetReason(std::string reason) { |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 126 | RTC_DCHECK_LE(reason.size(), 0xffu); |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 127 | reason_ = std::move(reason); |
danilchap | 50c5136 | 2015-11-22 09:03:11 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | size_t Bye::BlockLength() const { |
| 131 | size_t src_count = (1 + csrcs_.size()); |
| 132 | size_t reason_size_in_32bits = reason_.empty() ? 0 : (reason_.size() / 4 + 1); |
| 133 | return kHeaderLength + 4 * (src_count + reason_size_in_32bits); |
| 134 | } |
| 135 | |
| 136 | } // namespace rtcp |
| 137 | } // namespace webrtc |