Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | |
| 11 | #include "media/sctp/dcsctp_transport.h" |
| 12 | |
Florent Castelli | 9bbfe9e | 2022-02-21 13:39:47 | [diff] [blame] | 13 | #include <atomic> |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 14 | #include <cstdint> |
Florent Castelli | 5183f00 | 2021-05-07 11:52:44 | [diff] [blame] | 15 | #include <limits> |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 16 | #include <utility> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "absl/strings/string_view.h" |
| 20 | #include "absl/types/optional.h" |
| 21 | #include "api/array_view.h" |
Victor Boivie | 2c1cfd0 | 2024-03-18 12:51:40 | [diff] [blame] | 22 | #include "api/data_channel_interface.h" |
Victor Boivie | cd54fd8 | 2024-02-28 11:16:15 | [diff] [blame] | 23 | #include "api/environment/environment.h" |
Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 24 | #include "api/priority.h" |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 25 | #include "media/base/media_channel.h" |
Florent Castelli | 6a11c84 | 2021-06-01 16:39:49 | [diff] [blame] | 26 | #include "net/dcsctp/public/dcsctp_socket_factory.h" |
Victor Boivie | 6174173 | 2021-05-10 15:22:42 | [diff] [blame] | 27 | #include "net/dcsctp/public/packet_observer.h" |
Victor Boivie | 5e726da | 2021-06-19 05:59:01 | [diff] [blame] | 28 | #include "net/dcsctp/public/text_pcap_packet_observer.h" |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 29 | #include "net/dcsctp/public/types.h" |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 30 | #include "p2p/base/packet_transport_internal.h" |
| 31 | #include "rtc_base/checks.h" |
Victor Boivie | 6174173 | 2021-05-10 15:22:42 | [diff] [blame] | 32 | #include "rtc_base/logging.h" |
Per K | f4aadf3 | 2024-02-27 08:01:15 | [diff] [blame] | 33 | #include "rtc_base/network/received_packet.h" |
Victor Boivie | 8df32eb | 2021-08-12 13:21:25 | [diff] [blame] | 34 | #include "rtc_base/socket.h" |
Victor Boivie | 6174173 | 2021-05-10 15:22:42 | [diff] [blame] | 35 | #include "rtc_base/strings/string_builder.h" |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 36 | #include "rtc_base/thread.h" |
| 37 | #include "rtc_base/trace_event.h" |
| 38 | #include "system_wrappers/include/clock.h" |
| 39 | |
| 40 | namespace webrtc { |
| 41 | |
| 42 | namespace { |
Victor Boivie | 8df32eb | 2021-08-12 13:21:25 | [diff] [blame] | 43 | using ::dcsctp::SendPacketStatus; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 44 | |
Victor Boivie | f72c064 | 2021-09-20 18:00:57 | [diff] [blame] | 45 | // When there is packet loss for a long time, the SCTP retry timers will use |
| 46 | // exponential backoff, which can grow to very long durations and when the |
| 47 | // connection recovers, it may take a long time to reach the new backoff |
| 48 | // duration. By limiting it to a reasonable limit, the time to recover reduces. |
| 49 | constexpr dcsctp::DurationMs kMaxTimerBackoffDuration = |
| 50 | dcsctp::DurationMs(3000); |
| 51 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 52 | enum class WebrtcPPID : dcsctp::PPID::UnderlyingType { |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 53 | // https://www.rfc-editor.org/rfc/rfc8832.html#section-8.1 |
| 54 | kDCEP = 50, |
| 55 | // https://www.rfc-editor.org/rfc/rfc8831.html#section-8 |
| 56 | kString = 51, |
| 57 | kBinaryPartial = 52, // Deprecated |
| 58 | kBinary = 53, |
| 59 | kStringPartial = 54, // Deprecated |
| 60 | kStringEmpty = 56, |
| 61 | kBinaryEmpty = 57, |
| 62 | }; |
| 63 | |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 64 | WebrtcPPID ToPPID(DataMessageType message_type, size_t size) { |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 65 | switch (message_type) { |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 66 | case DataMessageType::kControl: |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 67 | return WebrtcPPID::kDCEP; |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 68 | case DataMessageType::kText: |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 69 | return size > 0 ? WebrtcPPID::kString : WebrtcPPID::kStringEmpty; |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 70 | case DataMessageType::kBinary: |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 71 | return size > 0 ? WebrtcPPID::kBinary : WebrtcPPID::kBinaryEmpty; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 72 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 73 | } |
| 74 | |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 75 | absl::optional<DataMessageType> ToDataMessageType(dcsctp::PPID ppid) { |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 76 | switch (static_cast<WebrtcPPID>(ppid.value())) { |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 77 | case WebrtcPPID::kDCEP: |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 78 | return DataMessageType::kControl; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 79 | case WebrtcPPID::kString: |
| 80 | case WebrtcPPID::kStringPartial: |
| 81 | case WebrtcPPID::kStringEmpty: |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 82 | return DataMessageType::kText; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 83 | case WebrtcPPID::kBinary: |
| 84 | case WebrtcPPID::kBinaryPartial: |
| 85 | case WebrtcPPID::kBinaryEmpty: |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 86 | return DataMessageType::kBinary; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 87 | } |
| 88 | return absl::nullopt; |
| 89 | } |
| 90 | |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 91 | absl::optional<cricket::SctpErrorCauseCode> ToErrorCauseCode( |
| 92 | dcsctp::ErrorKind error) { |
| 93 | switch (error) { |
| 94 | case dcsctp::ErrorKind::kParseFailed: |
| 95 | return cricket::SctpErrorCauseCode::kUnrecognizedParameters; |
| 96 | case dcsctp::ErrorKind::kPeerReported: |
| 97 | return cricket::SctpErrorCauseCode::kUserInitiatedAbort; |
| 98 | case dcsctp::ErrorKind::kWrongSequence: |
| 99 | case dcsctp::ErrorKind::kProtocolViolation: |
| 100 | return cricket::SctpErrorCauseCode::kProtocolViolation; |
| 101 | case dcsctp::ErrorKind::kResourceExhaustion: |
| 102 | return cricket::SctpErrorCauseCode::kOutOfResource; |
| 103 | case dcsctp::ErrorKind::kTooManyRetries: |
| 104 | case dcsctp::ErrorKind::kUnsupportedOperation: |
| 105 | case dcsctp::ErrorKind::kNoError: |
| 106 | case dcsctp::ErrorKind::kNotConnected: |
| 107 | // No SCTP error cause code matches those |
| 108 | break; |
| 109 | } |
| 110 | return absl::nullopt; |
| 111 | } |
| 112 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 113 | bool IsEmptyPPID(dcsctp::PPID ppid) { |
| 114 | WebrtcPPID webrtc_ppid = static_cast<WebrtcPPID>(ppid.value()); |
| 115 | return webrtc_ppid == WebrtcPPID::kStringEmpty || |
| 116 | webrtc_ppid == WebrtcPPID::kBinaryEmpty; |
| 117 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 118 | } // namespace |
| 119 | |
Victor Boivie | cd54fd8 | 2024-02-28 11:16:15 | [diff] [blame] | 120 | DcSctpTransport::DcSctpTransport(const Environment& env, |
| 121 | rtc::Thread* network_thread, |
| 122 | rtc::PacketTransportInternal* transport) |
| 123 | : DcSctpTransport(env, |
| 124 | network_thread, |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 125 | transport, |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 126 | std::make_unique<dcsctp::DcSctpSocketFactory>()) {} |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 127 | DcSctpTransport::DcSctpTransport( |
Victor Boivie | cd54fd8 | 2024-02-28 11:16:15 | [diff] [blame] | 128 | const Environment& env, |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 129 | rtc::Thread* network_thread, |
| 130 | rtc::PacketTransportInternal* transport, |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 131 | std::unique_ptr<dcsctp::DcSctpSocketFactory> socket_factory) |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 132 | : network_thread_(network_thread), |
| 133 | transport_(transport), |
Victor Boivie | cd54fd8 | 2024-02-28 11:16:15 | [diff] [blame] | 134 | env_(env), |
| 135 | random_(env_.clock().TimeInMicroseconds()), |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 136 | socket_factory_(std::move(socket_factory)), |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 137 | task_queue_timeout_factory_( |
| 138 | *network_thread, |
| 139 | [this]() { return TimeMillis(); }, |
| 140 | [this](dcsctp::TimeoutID timeout_id) { |
| 141 | socket_->HandleTimeout(timeout_id); |
| 142 | }) { |
| 143 | RTC_DCHECK_RUN_ON(network_thread_); |
Florent Castelli | 9bbfe9e | 2022-02-21 13:39:47 | [diff] [blame] | 144 | static std::atomic<int> instance_count = 0; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 145 | rtc::StringBuilder sb; |
| 146 | sb << debug_name_ << instance_count++; |
| 147 | debug_name_ = sb.Release(); |
| 148 | ConnectTransportSignals(); |
| 149 | } |
| 150 | |
| 151 | DcSctpTransport::~DcSctpTransport() { |
| 152 | if (socket_) { |
| 153 | socket_->Close(); |
| 154 | } |
| 155 | } |
| 156 | |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 157 | void DcSctpTransport::SetOnConnectedCallback(std::function<void()> callback) { |
| 158 | RTC_DCHECK_RUN_ON(network_thread_); |
| 159 | on_connected_callback_ = std::move(callback); |
| 160 | } |
| 161 | |
| 162 | void DcSctpTransport::SetDataChannelSink(DataChannelSink* sink) { |
| 163 | RTC_DCHECK_RUN_ON(network_thread_); |
| 164 | data_channel_sink_ = sink; |
| 165 | if (data_channel_sink_ && ready_to_send_data_) { |
| 166 | data_channel_sink_->OnReadyToSend(); |
| 167 | } |
| 168 | } |
| 169 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 170 | void DcSctpTransport::SetDtlsTransport( |
| 171 | rtc::PacketTransportInternal* transport) { |
| 172 | RTC_DCHECK_RUN_ON(network_thread_); |
| 173 | DisconnectTransportSignals(); |
| 174 | transport_ = transport; |
| 175 | ConnectTransportSignals(); |
| 176 | MaybeConnectSocket(); |
| 177 | } |
| 178 | |
| 179 | bool DcSctpTransport::Start(int local_sctp_port, |
| 180 | int remote_sctp_port, |
| 181 | int max_message_size) { |
| 182 | RTC_DCHECK_RUN_ON(network_thread_); |
| 183 | RTC_DCHECK(max_message_size > 0); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 184 | RTC_DLOG(LS_INFO) << debug_name_ << "->Start(local=" << local_sctp_port |
| 185 | << ", remote=" << remote_sctp_port |
| 186 | << ", max_message_size=" << max_message_size << ")"; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 187 | |
| 188 | if (!socket_) { |
| 189 | dcsctp::DcSctpOptions options; |
| 190 | options.local_port = local_sctp_port; |
| 191 | options.remote_port = remote_sctp_port; |
| 192 | options.max_message_size = max_message_size; |
Victor Boivie | f72c064 | 2021-09-20 18:00:57 | [diff] [blame] | 193 | options.max_timer_backoff_duration = kMaxTimerBackoffDuration; |
| 194 | // Don't close the connection automatically on too many retransmissions. |
| 195 | options.max_retransmissions = absl::nullopt; |
| 196 | options.max_init_retransmits = absl::nullopt; |
Victor Boivie | 2c1cfd0 | 2024-03-18 12:51:40 | [diff] [blame] | 197 | options.per_stream_send_queue_limit = |
| 198 | DataChannelInterface::MaxSendQueueSize(); |
| 199 | // This is just set to avoid denial-of-service. Practically unlimited. |
| 200 | options.max_send_buffer_size = std::numeric_limits<size_t>::max(); |
Victor Boivie | b0a1d8b | 2024-06-12 10:17:22 | [diff] [blame] | 201 | options.enable_message_interleaving = |
| 202 | env_.field_trials().IsEnabled("WebRTC-DataChannelMessageInterleaving"); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 203 | |
Victor Boivie | 6174173 | 2021-05-10 15:22:42 | [diff] [blame] | 204 | std::unique_ptr<dcsctp::PacketObserver> packet_observer; |
| 205 | if (RTC_LOG_CHECK_LEVEL(LS_VERBOSE)) { |
Victor Boivie | 5e726da | 2021-06-19 05:59:01 | [diff] [blame] | 206 | packet_observer = |
| 207 | std::make_unique<dcsctp::TextPcapPacketObserver>(debug_name_); |
Victor Boivie | 6174173 | 2021-05-10 15:22:42 | [diff] [blame] | 208 | } |
| 209 | |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 210 | socket_ = socket_factory_->Create(debug_name_, *this, |
| 211 | std::move(packet_observer), options); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 212 | } else { |
| 213 | if (local_sctp_port != socket_->options().local_port || |
| 214 | remote_sctp_port != socket_->options().remote_port) { |
| 215 | RTC_LOG(LS_ERROR) |
| 216 | << debug_name_ << "->Start(local=" << local_sctp_port |
| 217 | << ", remote=" << remote_sctp_port |
| 218 | << "): Can't change ports on already started transport."; |
| 219 | return false; |
| 220 | } |
| 221 | socket_->SetMaxMessageSize(max_message_size); |
| 222 | } |
| 223 | |
| 224 | MaybeConnectSocket(); |
| 225 | |
Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 226 | for (const auto& [sid, stream_state] : stream_states_) { |
| 227 | socket_->SetStreamPriority(sid, stream_state.priority); |
| 228 | } |
| 229 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 233 | bool DcSctpTransport::OpenStream(int sid, PriorityValue priority) { |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 234 | RTC_DCHECK_RUN_ON(network_thread_); |
Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 235 | RTC_DLOG(LS_INFO) << debug_name_ << "->OpenStream(" << sid << ", " |
| 236 | << priority.value() << ")."; |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 237 | |
| 238 | StreamState stream_state; |
Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 239 | stream_state.priority = dcsctp::StreamPriority(priority.value()); |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 240 | stream_states_.insert_or_assign(dcsctp::StreamID(static_cast<uint16_t>(sid)), |
| 241 | stream_state); |
Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 242 | if (socket_) { |
| 243 | socket_->SetStreamPriority(dcsctp::StreamID(sid), |
| 244 | dcsctp::StreamPriority(priority.value())); |
| 245 | } |
| 246 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
| 250 | bool DcSctpTransport::ResetStream(int sid) { |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 251 | RTC_DCHECK_RUN_ON(network_thread_); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 252 | RTC_DLOG(LS_INFO) << debug_name_ << "->ResetStream(" << sid << ")."; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 253 | if (!socket_) { |
Florent Castelli | 8f04c7c | 2022-05-05 21:43:44 | [diff] [blame] | 254 | RTC_LOG(LS_ERROR) << debug_name_ << "->ResetStream(sid=" << sid |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 255 | << "): Transport is not started."; |
| 256 | return false; |
| 257 | } |
Florent Castelli | 8f04c7c | 2022-05-05 21:43:44 | [diff] [blame] | 258 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 259 | dcsctp::StreamID streams[1] = {dcsctp::StreamID(static_cast<uint16_t>(sid))}; |
Florent Castelli | 8f04c7c | 2022-05-05 21:43:44 | [diff] [blame] | 260 | |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 261 | auto it = stream_states_.find(streams[0]); |
| 262 | if (it == stream_states_.end()) { |
| 263 | RTC_LOG(LS_ERROR) << debug_name_ << "->ResetStream(sid=" << sid |
| 264 | << "): Stream is not open."; |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | StreamState& stream_state = it->second; |
| 269 | if (stream_state.closure_initiated || stream_state.incoming_reset_done || |
| 270 | stream_state.outgoing_reset_done) { |
Florent Castelli | 8f04c7c | 2022-05-05 21:43:44 | [diff] [blame] | 271 | // The closing procedure was already initiated by the remote, don't do |
| 272 | // anything. |
| 273 | return false; |
| 274 | } |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 275 | stream_state.closure_initiated = true; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 276 | socket_->ResetStreams(streams); |
| 277 | return true; |
| 278 | } |
| 279 | |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 280 | RTCError DcSctpTransport::SendData(int sid, |
| 281 | const SendDataParams& params, |
| 282 | const rtc::CopyOnWriteBuffer& payload) { |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 283 | RTC_DCHECK_RUN_ON(network_thread_); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 284 | RTC_DLOG(LS_VERBOSE) << debug_name_ << "->SendData(sid=" << sid |
| 285 | << ", type=" << static_cast<int>(params.type) |
| 286 | << ", length=" << payload.size() << ")."; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 287 | |
| 288 | if (!socket_) { |
| 289 | RTC_LOG(LS_ERROR) << debug_name_ |
| 290 | << "->SendData(...): Transport is not started."; |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 291 | return RTCError(RTCErrorType::INVALID_STATE); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 292 | } |
| 293 | |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 294 | // It is possible for a message to be sent from the signaling thread at the |
| 295 | // same time a data-channel is closing, but before the signaling thread is |
| 296 | // aware of it. So we need to keep track of currently active data channels and |
| 297 | // skip sending messages for the ones that are not open or closing. |
| 298 | // The sending errors are not impacting the data channel API contract as |
| 299 | // it is allowed to discard queued messages when the channel is closing. |
| 300 | auto stream_state = |
| 301 | stream_states_.find(dcsctp::StreamID(static_cast<uint16_t>(sid))); |
| 302 | if (stream_state == stream_states_.end()) { |
| 303 | RTC_LOG(LS_VERBOSE) << "Skipping message on non-open stream with sid: " |
| 304 | << sid; |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 305 | return RTCError(RTCErrorType::INVALID_STATE); |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (stream_state->second.closure_initiated || |
| 309 | stream_state->second.incoming_reset_done || |
| 310 | stream_state->second.outgoing_reset_done) { |
| 311 | RTC_LOG(LS_VERBOSE) << "Skipping message on closing stream with sid: " |
| 312 | << sid; |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 313 | return RTCError(RTCErrorType::INVALID_STATE); |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 314 | } |
| 315 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 316 | auto max_message_size = socket_->options().max_message_size; |
| 317 | if (max_message_size > 0 && payload.size() > max_message_size) { |
Victor Boivie | 402ceff | 2021-05-19 12:26:22 | [diff] [blame] | 318 | RTC_LOG(LS_WARNING) << debug_name_ |
| 319 | << "->SendData(...): " |
| 320 | "Trying to send packet bigger " |
| 321 | "than the max message size: " |
| 322 | << payload.size() << " vs max of " << max_message_size; |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 323 | return RTCError(RTCErrorType::INVALID_RANGE); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | std::vector<uint8_t> message_payload(payload.cdata(), |
| 327 | payload.cdata() + payload.size()); |
| 328 | if (message_payload.empty()) { |
| 329 | // https://www.rfc-editor.org/rfc/rfc8831.html#section-6.6 |
| 330 | // SCTP does not support the sending of empty user messages. Therefore, if |
| 331 | // an empty message has to be sent, the appropriate PPID (WebRTC String |
| 332 | // Empty or WebRTC Binary Empty) is used, and the SCTP user message of one |
| 333 | // zero byte is sent. |
| 334 | message_payload.push_back('\0'); |
| 335 | } |
| 336 | |
| 337 | dcsctp::DcSctpMessage message( |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 338 | dcsctp::StreamID(static_cast<uint16_t>(sid)), |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 339 | dcsctp::PPID(static_cast<uint16_t>(ToPPID(params.type, payload.size()))), |
| 340 | std::move(message_payload)); |
| 341 | |
| 342 | dcsctp::SendOptions send_options; |
| 343 | send_options.unordered = dcsctp::IsUnordered(!params.ordered); |
Florent Castelli | 5183f00 | 2021-05-07 11:52:44 | [diff] [blame] | 344 | if (params.max_rtx_ms.has_value()) { |
| 345 | RTC_DCHECK(*params.max_rtx_ms >= 0 && |
| 346 | *params.max_rtx_ms <= std::numeric_limits<uint16_t>::max()); |
| 347 | send_options.lifetime = dcsctp::DurationMs(*params.max_rtx_ms); |
| 348 | } |
| 349 | if (params.max_rtx_count.has_value()) { |
| 350 | RTC_DCHECK(*params.max_rtx_count >= 0 && |
| 351 | *params.max_rtx_count <= std::numeric_limits<uint16_t>::max()); |
| 352 | send_options.max_retransmissions = *params.max_rtx_count; |
| 353 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 354 | |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 355 | dcsctp::SendStatus error = socket_->Send(std::move(message), send_options); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 356 | switch (error) { |
| 357 | case dcsctp::SendStatus::kSuccess: |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 358 | return RTCError::OK(); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 359 | case dcsctp::SendStatus::kErrorResourceExhaustion: |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 360 | ready_to_send_data_ = false; |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 361 | return RTCError(RTCErrorType::RESOURCE_EXHAUSTED); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 362 | default: |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 363 | absl::string_view message = dcsctp::ToString(error); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 364 | RTC_LOG(LS_ERROR) << debug_name_ |
| 365 | << "->SendData(...): send() failed with error " |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 366 | << message << "."; |
| 367 | return RTCError(RTCErrorType::NETWORK_ERROR, message); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 368 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | bool DcSctpTransport::ReadyToSendData() { |
Tommi | 72b12a9 | 2023-03-21 19:29:16 | [diff] [blame] | 372 | RTC_DCHECK_RUN_ON(network_thread_); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 373 | return ready_to_send_data_; |
| 374 | } |
| 375 | |
| 376 | int DcSctpTransport::max_message_size() const { |
| 377 | if (!socket_) { |
| 378 | RTC_LOG(LS_ERROR) << debug_name_ |
| 379 | << "->max_message_size(...): Transport is not started."; |
| 380 | return 0; |
| 381 | } |
| 382 | return socket_->options().max_message_size; |
| 383 | } |
| 384 | |
| 385 | absl::optional<int> DcSctpTransport::max_outbound_streams() const { |
| 386 | if (!socket_) |
| 387 | return absl::nullopt; |
| 388 | return socket_->options().announced_maximum_outgoing_streams; |
| 389 | } |
| 390 | |
| 391 | absl::optional<int> DcSctpTransport::max_inbound_streams() const { |
| 392 | if (!socket_) |
| 393 | return absl::nullopt; |
| 394 | return socket_->options().announced_maximum_incoming_streams; |
| 395 | } |
| 396 | |
Victor Boivie | fea41f5 | 2024-03-11 15:43:31 | [diff] [blame] | 397 | size_t DcSctpTransport::buffered_amount(int sid) const { |
| 398 | if (!socket_) |
| 399 | return 0; |
| 400 | return socket_->buffered_amount(dcsctp::StreamID(sid)); |
| 401 | } |
| 402 | |
Victor Boivie | cdecc4e | 2024-03-18 12:47:34 | [diff] [blame] | 403 | size_t DcSctpTransport::buffered_amount_low_threshold(int sid) const { |
| 404 | if (!socket_) |
| 405 | return 0; |
| 406 | return socket_->buffered_amount_low_threshold(dcsctp::StreamID(sid)); |
| 407 | } |
| 408 | |
| 409 | void DcSctpTransport::SetBufferedAmountLowThreshold(int sid, size_t bytes) { |
| 410 | if (!socket_) |
| 411 | return; |
| 412 | socket_->SetBufferedAmountLowThreshold(dcsctp::StreamID(sid), bytes); |
| 413 | } |
| 414 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 415 | void DcSctpTransport::set_debug_name_for_testing(const char* debug_name) { |
| 416 | debug_name_ = debug_name; |
| 417 | } |
| 418 | |
Victor Boivie | 8df32eb | 2021-08-12 13:21:25 | [diff] [blame] | 419 | SendPacketStatus DcSctpTransport::SendPacketWithStatus( |
| 420 | rtc::ArrayView<const uint8_t> data) { |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 421 | RTC_DCHECK_RUN_ON(network_thread_); |
| 422 | RTC_DCHECK(socket_); |
| 423 | |
| 424 | if (data.size() > (socket_->options().mtu)) { |
| 425 | RTC_LOG(LS_ERROR) << debug_name_ |
| 426 | << "->SendPacket(...): " |
| 427 | "SCTP seems to have made a packet that is bigger " |
| 428 | "than its official MTU: " |
| 429 | << data.size() << " vs max of " << socket_->options().mtu; |
Victor Boivie | 8df32eb | 2021-08-12 13:21:25 | [diff] [blame] | 430 | return SendPacketStatus::kError; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 431 | } |
| 432 | TRACE_EVENT0("webrtc", "DcSctpTransport::SendPacket"); |
| 433 | |
| 434 | if (!transport_ || !transport_->writable()) |
Victor Boivie | 8df32eb | 2021-08-12 13:21:25 | [diff] [blame] | 435 | return SendPacketStatus::kError; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 436 | |
Victor Boivie | 767235d | 2021-09-06 10:10:14 | [diff] [blame] | 437 | RTC_DLOG(LS_VERBOSE) << debug_name_ << "->SendPacket(length=" << data.size() |
| 438 | << ")"; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 439 | |
| 440 | auto result = |
| 441 | transport_->SendPacket(reinterpret_cast<const char*>(data.data()), |
| 442 | data.size(), rtc::PacketOptions(), 0); |
| 443 | |
| 444 | if (result < 0) { |
Victor Boivie | 402ceff | 2021-05-19 12:26:22 | [diff] [blame] | 445 | RTC_LOG(LS_WARNING) << debug_name_ << "->SendPacket(length=" << data.size() |
| 446 | << ") failed with error: " << transport_->GetError() |
| 447 | << "."; |
Victor Boivie | 8df32eb | 2021-08-12 13:21:25 | [diff] [blame] | 448 | |
| 449 | if (rtc::IsBlockingError(transport_->GetError())) { |
| 450 | return SendPacketStatus::kTemporaryFailure; |
| 451 | } |
| 452 | return SendPacketStatus::kError; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 453 | } |
Victor Boivie | 8df32eb | 2021-08-12 13:21:25 | [diff] [blame] | 454 | return SendPacketStatus::kSuccess; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 455 | } |
| 456 | |
Henrik Boström | b951dc6 | 2022-01-26 17:38:13 | [diff] [blame] | 457 | std::unique_ptr<dcsctp::Timeout> DcSctpTransport::CreateTimeout( |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 458 | TaskQueueBase::DelayPrecision precision) { |
Henrik Boström | b951dc6 | 2022-01-26 17:38:13 | [diff] [blame] | 459 | return task_queue_timeout_factory_.CreateTimeout(precision); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | dcsctp::TimeMs DcSctpTransport::TimeMillis() { |
Victor Boivie | cd54fd8 | 2024-02-28 11:16:15 | [diff] [blame] | 463 | return dcsctp::TimeMs(env_.clock().TimeInMilliseconds()); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | uint32_t DcSctpTransport::GetRandomInt(uint32_t low, uint32_t high) { |
| 467 | return random_.Rand(low, high); |
| 468 | } |
| 469 | |
Victor Boivie | 236ac50 | 2021-05-20 17:34:18 | [diff] [blame] | 470 | void DcSctpTransport::OnTotalBufferedAmountLow() { |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 471 | RTC_DCHECK_RUN_ON(network_thread_); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 472 | if (!ready_to_send_data_) { |
| 473 | ready_to_send_data_ = true; |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 474 | if (data_channel_sink_) { |
| 475 | data_channel_sink_->OnReadyToSend(); |
| 476 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Victor Boivie | cdecc4e | 2024-03-18 12:47:34 | [diff] [blame] | 480 | void DcSctpTransport::OnBufferedAmountLow(dcsctp::StreamID stream_id) { |
| 481 | RTC_DCHECK_RUN_ON(network_thread_); |
| 482 | if (data_channel_sink_) { |
| 483 | data_channel_sink_->OnBufferedAmountLow(*stream_id); |
| 484 | } |
| 485 | } |
| 486 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 487 | void DcSctpTransport::OnMessageReceived(dcsctp::DcSctpMessage message) { |
| 488 | RTC_DCHECK_RUN_ON(network_thread_); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 489 | RTC_DLOG(LS_VERBOSE) << debug_name_ << "->OnMessageReceived(sid=" |
| 490 | << message.stream_id().value() |
| 491 | << ", ppid=" << message.ppid().value() |
| 492 | << ", length=" << message.payload().size() << ")."; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 493 | auto type = ToDataMessageType(message.ppid()); |
| 494 | if (!type.has_value()) { |
| 495 | RTC_LOG(LS_VERBOSE) << debug_name_ |
| 496 | << "->OnMessageReceived(): Received an unknown PPID " |
| 497 | << message.ppid().value() |
| 498 | << " on an SCTP packet. Dropping."; |
Florent Castelli | 691d4a0 | 2023-03-13 14:17:17 | [diff] [blame] | 499 | return; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 500 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 501 | receive_buffer_.Clear(); |
| 502 | if (!IsEmptyPPID(message.ppid())) |
| 503 | receive_buffer_.AppendData(message.payload().data(), |
| 504 | message.payload().size()); |
| 505 | |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 506 | if (data_channel_sink_) { |
Tommi | 4e1c957 | 2023-03-15 11:36:20 | [diff] [blame] | 507 | data_channel_sink_->OnDataReceived(message.stream_id().value(), *type, |
| 508 | receive_buffer_); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 509 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void DcSctpTransport::OnError(dcsctp::ErrorKind error, |
| 513 | absl::string_view message) { |
Victor Boivie | 8ae1adc | 2021-10-14 08:38:23 | [diff] [blame] | 514 | if (error == dcsctp::ErrorKind::kResourceExhaustion) { |
| 515 | // Indicates that a message failed to be enqueued, because the send buffer |
| 516 | // is full, which is a very common (and wanted) state for high throughput |
| 517 | // sending/benchmarks. |
| 518 | RTC_LOG(LS_VERBOSE) << debug_name_ |
| 519 | << "->OnError(error=" << dcsctp::ToString(error) |
| 520 | << ", message=" << message << ")."; |
| 521 | } else { |
| 522 | RTC_LOG(LS_ERROR) << debug_name_ |
| 523 | << "->OnError(error=" << dcsctp::ToString(error) |
| 524 | << ", message=" << message << ")."; |
| 525 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | void DcSctpTransport::OnAborted(dcsctp::ErrorKind error, |
| 529 | absl::string_view message) { |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 530 | RTC_DCHECK_RUN_ON(network_thread_); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 531 | RTC_LOG(LS_ERROR) << debug_name_ |
| 532 | << "->OnAborted(error=" << dcsctp::ToString(error) |
| 533 | << ", message=" << message << ")."; |
| 534 | ready_to_send_data_ = false; |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 535 | RTCError rtc_error(RTCErrorType::OPERATION_ERROR_WITH_DATA, |
| 536 | std::string(message)); |
| 537 | rtc_error.set_error_detail(RTCErrorDetailType::SCTP_FAILURE); |
| 538 | auto code = ToErrorCauseCode(error); |
| 539 | if (code.has_value()) { |
| 540 | rtc_error.set_sctp_cause_code(static_cast<uint16_t>(*code)); |
| 541 | } |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 542 | if (data_channel_sink_) { |
| 543 | data_channel_sink_->OnTransportClosed(rtc_error); |
| 544 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void DcSctpTransport::OnConnected() { |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 548 | RTC_DCHECK_RUN_ON(network_thread_); |
| 549 | RTC_DLOG(LS_INFO) << debug_name_ << "->OnConnected()."; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 550 | ready_to_send_data_ = true; |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 551 | if (data_channel_sink_) { |
| 552 | data_channel_sink_->OnReadyToSend(); |
| 553 | } |
| 554 | if (on_connected_callback_) { |
| 555 | on_connected_callback_(); |
| 556 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void DcSctpTransport::OnClosed() { |
Tommi | 72b12a9 | 2023-03-21 19:29:16 | [diff] [blame] | 560 | RTC_DCHECK_RUN_ON(network_thread_); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 561 | RTC_DLOG(LS_INFO) << debug_name_ << "->OnClosed()."; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 562 | ready_to_send_data_ = false; |
| 563 | } |
| 564 | |
| 565 | void DcSctpTransport::OnConnectionRestarted() { |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 566 | RTC_DLOG(LS_INFO) << debug_name_ << "->OnConnectionRestarted()."; |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void DcSctpTransport::OnStreamsResetFailed( |
| 570 | rtc::ArrayView<const dcsctp::StreamID> outgoing_streams, |
| 571 | absl::string_view reason) { |
| 572 | // TODO(orphis): Need a test to check for correct behavior |
| 573 | for (auto& stream_id : outgoing_streams) { |
Victor Boivie | 402ceff | 2021-05-19 12:26:22 | [diff] [blame] | 574 | RTC_LOG(LS_WARNING) |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 575 | << debug_name_ |
| 576 | << "->OnStreamsResetFailed(...): Outgoing stream reset failed" |
| 577 | << ", sid=" << stream_id.value() << ", reason: " << reason << "."; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void DcSctpTransport::OnStreamsResetPerformed( |
| 582 | rtc::ArrayView<const dcsctp::StreamID> outgoing_streams) { |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 583 | RTC_DCHECK_RUN_ON(network_thread_); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 584 | for (auto& stream_id : outgoing_streams) { |
| 585 | RTC_LOG(LS_INFO) << debug_name_ |
| 586 | << "->OnStreamsResetPerformed(...): Outgoing stream reset" |
| 587 | << ", sid=" << stream_id.value(); |
Florent Castelli | 8f04c7c | 2022-05-05 21:43:44 | [diff] [blame] | 588 | |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 589 | auto it = stream_states_.find(stream_id); |
| 590 | if (it == stream_states_.end()) { |
| 591 | // Ignoring an outgoing stream reset for a closed stream |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | StreamState& stream_state = it->second; |
| 596 | stream_state.outgoing_reset_done = true; |
| 597 | |
| 598 | if (stream_state.incoming_reset_done) { |
Florent Castelli | 8f04c7c | 2022-05-05 21:43:44 | [diff] [blame] | 599 | // When the close was not initiated locally, we can signal the end of the |
| 600 | // data channel close procedure when the remote ACKs the reset. |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 601 | if (data_channel_sink_) { |
| 602 | data_channel_sink_->OnChannelClosed(stream_id.value()); |
| 603 | } |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 604 | stream_states_.erase(stream_id); |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 605 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | void DcSctpTransport::OnIncomingStreamsReset( |
| 610 | rtc::ArrayView<const dcsctp::StreamID> incoming_streams) { |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 611 | RTC_DCHECK_RUN_ON(network_thread_); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 612 | for (auto& stream_id : incoming_streams) { |
| 613 | RTC_LOG(LS_INFO) << debug_name_ |
| 614 | << "->OnIncomingStreamsReset(...): Incoming stream reset" |
| 615 | << ", sid=" << stream_id.value(); |
Florent Castelli | 8f04c7c | 2022-05-05 21:43:44 | [diff] [blame] | 616 | |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 617 | auto it = stream_states_.find(stream_id); |
| 618 | if (it == stream_states_.end()) |
| 619 | return; |
| 620 | |
| 621 | StreamState& stream_state = it->second; |
| 622 | stream_state.incoming_reset_done = true; |
| 623 | |
| 624 | if (!stream_state.closure_initiated) { |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 625 | // When receiving an incoming stream reset event for a non local close |
| 626 | // procedure, the transport needs to reset the stream in the other |
| 627 | // direction too. |
| 628 | dcsctp::StreamID streams[1] = {stream_id}; |
| 629 | socket_->ResetStreams(streams); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 630 | if (data_channel_sink_) { |
| 631 | data_channel_sink_->OnChannelClosing(stream_id.value()); |
| 632 | } |
Florent Castelli | 8f04c7c | 2022-05-05 21:43:44 | [diff] [blame] | 633 | } |
| 634 | |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 635 | if (stream_state.outgoing_reset_done) { |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 636 | // The close procedure that was initiated locally is complete when we |
| 637 | // receive and incoming reset event. |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 638 | if (data_channel_sink_) { |
| 639 | data_channel_sink_->OnChannelClosed(stream_id.value()); |
| 640 | } |
Florent Castelli | dbc2ba2 | 2022-08-22 17:46:39 | [diff] [blame] | 641 | stream_states_.erase(stream_id); |
Florent Castelli | e3b74f8 | 2022-05-02 22:24:15 | [diff] [blame] | 642 | } |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
| 646 | void DcSctpTransport::ConnectTransportSignals() { |
| 647 | RTC_DCHECK_RUN_ON(network_thread_); |
| 648 | if (!transport_) { |
| 649 | return; |
| 650 | } |
| 651 | transport_->SignalWritableState.connect( |
| 652 | this, &DcSctpTransport::OnTransportWritableState); |
Per K | f4aadf3 | 2024-02-27 08:01:15 | [diff] [blame] | 653 | transport_->RegisterReceivedPacketCallback( |
| 654 | this, [&](rtc::PacketTransportInternal* transport, |
| 655 | const rtc::ReceivedPacket& packet) { |
| 656 | OnTransportReadPacket(transport, packet); |
| 657 | }); |
Tommi | 1d26fd3 | 2024-03-18 16:57:52 | [diff] [blame] | 658 | transport_->SetOnCloseCallback([this]() { |
| 659 | RTC_DCHECK_RUN_ON(network_thread_); |
| 660 | RTC_DLOG(LS_VERBOSE) << debug_name_ << "->OnTransportClosed()."; |
| 661 | if (data_channel_sink_) { |
| 662 | data_channel_sink_->OnTransportClosed({}); |
| 663 | } |
| 664 | }); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void DcSctpTransport::DisconnectTransportSignals() { |
| 668 | RTC_DCHECK_RUN_ON(network_thread_); |
| 669 | if (!transport_) { |
| 670 | return; |
| 671 | } |
| 672 | transport_->SignalWritableState.disconnect(this); |
Per K | f4aadf3 | 2024-02-27 08:01:15 | [diff] [blame] | 673 | transport_->DeregisterReceivedPacketCallback(this); |
Tommi | 1d26fd3 | 2024-03-18 16:57:52 | [diff] [blame] | 674 | transport_->SetOnCloseCallback(nullptr); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | void DcSctpTransport::OnTransportWritableState( |
| 678 | rtc::PacketTransportInternal* transport) { |
| 679 | RTC_DCHECK_RUN_ON(network_thread_); |
| 680 | RTC_DCHECK_EQ(transport_, transport); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 681 | RTC_DLOG(LS_VERBOSE) << debug_name_ |
| 682 | << "->OnTransportWritableState(), writable=" |
| 683 | << transport->writable(); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 684 | MaybeConnectSocket(); |
| 685 | } |
| 686 | |
| 687 | void DcSctpTransport::OnTransportReadPacket( |
| 688 | rtc::PacketTransportInternal* transport, |
Per K | f4aadf3 | 2024-02-27 08:01:15 | [diff] [blame] | 689 | const rtc::ReceivedPacket& packet) { |
Philipp Hancke | 8e64e2d | 2022-01-10 21:32:58 | [diff] [blame] | 690 | RTC_DCHECK_RUN_ON(network_thread_); |
Per K | f4aadf3 | 2024-02-27 08:01:15 | [diff] [blame] | 691 | if (packet.decryption_info() != rtc::ReceivedPacket::kDtlsDecrypted) { |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 692 | // We are only interested in SCTP packets. |
| 693 | return; |
| 694 | } |
| 695 | |
Per K | f4aadf3 | 2024-02-27 08:01:15 | [diff] [blame] | 696 | RTC_DLOG(LS_VERBOSE) << debug_name_ << "->OnTransportReadPacket(), length=" |
| 697 | << packet.payload().size(); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 698 | if (socket_) { |
Per K | f4aadf3 | 2024-02-27 08:01:15 | [diff] [blame] | 699 | socket_->ReceivePacket(packet.payload()); |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Florent Castelli | a6983c6 | 2021-05-06 08:50:07 | [diff] [blame] | 703 | void DcSctpTransport::MaybeConnectSocket() { |
| 704 | if (transport_ && transport_->writable() && socket_ && |
| 705 | socket_->state() == dcsctp::SocketState::kClosed) { |
| 706 | socket_->Connect(); |
| 707 | } |
| 708 | } |
| 709 | } // namespace webrtc |