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