Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "pc/sctp_transport.h" |
| 12 | |
Harald Alvestrand | 97716c0 | 2019-05-21 08:52:59 | [diff] [blame] | 13 | #include <algorithm> |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Mirko Bonadei | 9f6808b | 2021-05-21 18:46:09 | [diff] [blame] | 17 | #include "api/dtls_transport_interface.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 18 | #include "api/sequence_checker.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 20 | #include "rtc_base/logging.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 21 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
| 24 | SctpTransport::SctpTransport( |
| 25 | std::unique_ptr<cricket::SctpTransportInternal> internal) |
| 26 | : owner_thread_(rtc::Thread::Current()), |
| 27 | info_(SctpTransportState::kNew), |
| 28 | internal_sctp_transport_(std::move(internal)) { |
| 29 | RTC_DCHECK(internal_sctp_transport_.get()); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 30 | internal_sctp_transport_->SetOnConnectedCallback( |
| 31 | [this]() { OnAssociationChangeCommunicationUp(); }); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 32 | |
| 33 | if (dtls_transport_) { |
| 34 | UpdateInformation(SctpTransportState::kConnecting); |
| 35 | } else { |
| 36 | UpdateInformation(SctpTransportState::kNew); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | SctpTransport::~SctpTransport() { |
| 41 | // We depend on the network thread to call Clear() before dropping |
| 42 | // its last reference to this object. |
| 43 | RTC_DCHECK(owner_thread_->IsCurrent() || !internal_sctp_transport_); |
| 44 | } |
| 45 | |
| 46 | SctpTransportInformation SctpTransport::Information() const { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 47 | // TODO(tommi): Update PeerConnection::GetSctpTransport to hand out a proxy |
| 48 | // to the transport so that we can be sure that methods get called on the |
| 49 | // expected thread. Chromium currently calls this method from |
| 50 | // TransceiverStateSurfacer. |
| 51 | if (!owner_thread_->IsCurrent()) { |
Danil Chapovalov | 9e09a1f | 2022-09-08 16:38:10 | [diff] [blame] | 52 | return owner_thread_->BlockingCall([this] { return Information(); }); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 53 | } |
| 54 | RTC_DCHECK_RUN_ON(owner_thread_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 55 | return info_; |
| 56 | } |
| 57 | |
| 58 | void SctpTransport::RegisterObserver(SctpTransportObserverInterface* observer) { |
| 59 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 60 | RTC_DCHECK(observer); |
| 61 | RTC_DCHECK(!observer_); |
| 62 | observer_ = observer; |
| 63 | } |
| 64 | |
| 65 | void SctpTransport::UnregisterObserver() { |
| 66 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 67 | observer_ = nullptr; |
| 68 | } |
| 69 | |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 70 | RTCError SctpTransport::OpenChannel(int channel_id) { |
| 71 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 72 | RTC_DCHECK(internal_sctp_transport_); |
| 73 | internal_sctp_transport_->OpenStream(channel_id); |
| 74 | return RTCError::OK(); |
| 75 | } |
| 76 | |
| 77 | RTCError SctpTransport::SendData(int channel_id, |
| 78 | const SendDataParams& params, |
| 79 | const rtc::CopyOnWriteBuffer& buffer) { |
| 80 | RTC_DCHECK_RUN_ON(owner_thread_); |
Tommi | 1fabbac | 2023-03-21 13:48:51 | [diff] [blame] | 81 | return internal_sctp_transport_->SendData(channel_id, params, buffer); |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | RTCError SctpTransport::CloseChannel(int channel_id) { |
| 85 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 86 | RTC_DCHECK(internal_sctp_transport_); |
| 87 | internal_sctp_transport_->ResetStream(channel_id); |
| 88 | return RTCError::OK(); |
| 89 | } |
| 90 | |
| 91 | void SctpTransport::SetDataSink(DataChannelSink* sink) { |
| 92 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 93 | RTC_DCHECK(internal_sctp_transport_); |
| 94 | internal_sctp_transport_->SetDataChannelSink(sink); |
| 95 | } |
| 96 | |
| 97 | bool SctpTransport::IsReadyToSend() const { |
| 98 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 99 | RTC_DCHECK(internal_sctp_transport_); |
| 100 | return internal_sctp_transport_->ReadyToSendData(); |
| 101 | } |
| 102 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 103 | rtc::scoped_refptr<DtlsTransportInterface> SctpTransport::dtls_transport() |
| 104 | const { |
| 105 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 106 | return dtls_transport_; |
| 107 | } |
| 108 | |
| 109 | // Internal functions |
| 110 | void SctpTransport::Clear() { |
| 111 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 112 | RTC_DCHECK(internal()); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 113 | // Note that we delete internal_sctp_transport_, but |
| 114 | // only drop the reference to dtls_transport_. |
| 115 | dtls_transport_ = nullptr; |
| 116 | internal_sctp_transport_ = nullptr; |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 117 | UpdateInformation(SctpTransportState::kClosed); |
| 118 | } |
| 119 | |
| 120 | void SctpTransport::SetDtlsTransport( |
| 121 | rtc::scoped_refptr<DtlsTransport> transport) { |
| 122 | RTC_DCHECK_RUN_ON(owner_thread_); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 123 | SctpTransportState next_state = info_.state(); |
| 124 | dtls_transport_ = transport; |
| 125 | if (internal_sctp_transport_) { |
| 126 | if (transport) { |
| 127 | internal_sctp_transport_->SetDtlsTransport(transport->internal()); |
Lahiru Ginnaliya Gamathige | 60c0b44 | 2021-02-16 15:29:08 | [diff] [blame] | 128 | |
Mirko Bonadei | 9f6808b | 2021-05-21 18:46:09 | [diff] [blame] | 129 | transport->internal()->SubscribeDtlsTransportState( |
Lahiru Ginnaliya Gamathige | 60c0b44 | 2021-02-16 15:29:08 | [diff] [blame] | 130 | [this](cricket::DtlsTransportInternal* transport, |
Mirko Bonadei | 9f6808b | 2021-05-21 18:46:09 | [diff] [blame] | 131 | DtlsTransportState state) { |
Lahiru Ginnaliya Gamathige | 60c0b44 | 2021-02-16 15:29:08 | [diff] [blame] | 132 | OnDtlsStateChange(transport, state); |
| 133 | }); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 134 | if (info_.state() == SctpTransportState::kNew) { |
| 135 | next_state = SctpTransportState::kConnecting; |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 136 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 137 | } else { |
| 138 | internal_sctp_transport_->SetDtlsTransport(nullptr); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 139 | } |
| 140 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 141 | |
Harald Alvestrand | d61f2a7 | 2019-05-08 18:20:59 | [diff] [blame] | 142 | UpdateInformation(next_state); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 143 | } |
| 144 | |
Harald Alvestrand | 8d3d6cf | 2019-05-16 09:49:17 | [diff] [blame] | 145 | void SctpTransport::Start(int local_port, |
| 146 | int remote_port, |
| 147 | int max_message_size) { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 148 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 149 | info_ = SctpTransportInformation(info_.state(), info_.dtls_transport(), |
| 150 | max_message_size, info_.MaxChannels()); |
| 151 | |
| 152 | if (!internal()->Start(local_port, remote_port, max_message_size)) { |
| 153 | RTC_LOG(LS_ERROR) << "Failed to push down SCTP parameters, closing."; |
| 154 | UpdateInformation(SctpTransportState::kClosed); |
Harald Alvestrand | 8d3d6cf | 2019-05-16 09:49:17 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 158 | void SctpTransport::UpdateInformation(SctpTransportState state) { |
| 159 | RTC_DCHECK_RUN_ON(owner_thread_); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 160 | bool must_send_update = (state != info_.state()); |
| 161 | // TODO(https://bugs.webrtc.org/10358): Update max channels from internal |
| 162 | // SCTP transport when available. |
| 163 | if (internal_sctp_transport_) { |
| 164 | info_ = SctpTransportInformation( |
| 165 | state, dtls_transport_, info_.MaxMessageSize(), info_.MaxChannels()); |
| 166 | } else { |
| 167 | info_ = SctpTransportInformation( |
| 168 | state, dtls_transport_, info_.MaxMessageSize(), info_.MaxChannels()); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 169 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 170 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 171 | if (observer_ && must_send_update) { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 172 | observer_->OnStateChange(info_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Harald Alvestrand | 97716c0 | 2019-05-21 08:52:59 | [diff] [blame] | 176 | void SctpTransport::OnAssociationChangeCommunicationUp() { |
| 177 | RTC_DCHECK_RUN_ON(owner_thread_); |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 178 | RTC_DCHECK(internal_sctp_transport_); |
| 179 | if (internal_sctp_transport_->max_outbound_streams() && |
| 180 | internal_sctp_transport_->max_inbound_streams()) { |
| 181 | int max_channels = |
| 182 | std::min(*(internal_sctp_transport_->max_outbound_streams()), |
| 183 | *(internal_sctp_transport_->max_inbound_streams())); |
| 184 | // Record max channels. |
| 185 | info_ = SctpTransportInformation(info_.state(), info_.dtls_transport(), |
| 186 | info_.MaxMessageSize(), max_channels); |
Harald Alvestrand | 97716c0 | 2019-05-21 08:52:59 | [diff] [blame] | 187 | } |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 188 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 189 | UpdateInformation(SctpTransportState::kConnected); |
| 190 | } |
| 191 | |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 192 | void SctpTransport::OnDtlsStateChange(cricket::DtlsTransportInternal* transport, |
Mirko Bonadei | 9f6808b | 2021-05-21 18:46:09 | [diff] [blame] | 193 | DtlsTransportState state) { |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 194 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 195 | RTC_CHECK(transport == dtls_transport_->internal()); |
Mirko Bonadei | 9f6808b | 2021-05-21 18:46:09 | [diff] [blame] | 196 | if (state == DtlsTransportState::kClosed || |
| 197 | state == DtlsTransportState::kFailed) { |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 198 | UpdateInformation(SctpTransportState::kClosed); |
| 199 | // TODO(http://bugs.webrtc.org/11090): Close all the data channels |
| 200 | } |
| 201 | } |
| 202 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 203 | } // namespace webrtc |