blob: 1d7a3c73f4fd2c02a2e6f6ef3cf0dfd10feb5f47 [file] [log] [blame]
ossu7bb87ee2017-01-23 12:56:251/*
Taylor Brandstetter3a034e12020-07-09 22:32:342 * Copyright 2020 The WebRTC project authors. All Rights Reserved.
ossu7bb87ee2017-01-23 12:56:253 *
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
Taylor Brandstetter3a034e12020-07-09 22:32:3411#ifndef PC_SCTP_DATA_CHANNEL_H_
12#define PC_SCTP_DATA_CHANNEL_H_
ossu7bb87ee2017-01-23 12:56:2513
Harald Alvestrand5761e7b2021-01-29 14:45:0814#include <stdint.h>
15
Steve Anton944c7552018-12-13 22:19:1016#include <memory>
ossu7bb87ee2017-01-23 12:56:2517#include <set>
18#include <string>
19
Harald Alvestrand5761e7b2021-01-29 14:45:0820#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 17:11:0021#include "api/data_channel_interface.h"
Harald Alvestrandfd5ae7f2020-05-16 06:37:4922#include "api/priority.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0823#include "api/rtc_error.h"
Mirko Bonadeid9708072019-01-25 19:26:4824#include "api/scoped_refptr.h"
Niels Möller2a707032020-06-16 14:39:1325#include "api/transport/data_channel_transport_interface.h"
Steve Anton10542f22019-01-11 17:11:0026#include "media/base/media_channel.h"
Taylor Brandstetter3a034e12020-07-09 22:32:3427#include "pc/data_channel_utils.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0828#include "rtc_base/copy_on_write_buffer.h"
Taylor Brandstetter3a034e12020-07-09 22:32:3429#include "rtc_base/ssl_stream_adapter.h" // For SSLRole
Artem Titove41c4332018-07-25 13:04:2830#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0831#include "rtc_base/thread.h"
32#include "rtc_base/thread_annotations.h"
ossu7bb87ee2017-01-23 12:56:2533
34namespace webrtc {
35
Taylor Brandstetter3a034e12020-07-09 22:32:3436class SctpDataChannel;
ossu7bb87ee2017-01-23 12:56:2537
Taylor Brandstetter3a034e12020-07-09 22:32:3438// TODO(deadbeef): Get rid of this and have SctpDataChannel depend on
39// SctpTransportInternal (pure virtual SctpTransport interface) instead.
40class SctpDataChannelProviderInterface {
ossu7bb87ee2017-01-23 12:56:2541 public:
42 // Sends the data to the transport.
Florent Castellid95b1492021-05-10 09:29:5643 virtual bool SendData(int sid,
44 const SendDataParams& params,
ossu7bb87ee2017-01-23 12:56:2545 const rtc::CopyOnWriteBuffer& payload,
46 cricket::SendDataResult* result) = 0;
47 // Connects to the transport signals.
Taylor Brandstetter3a034e12020-07-09 22:32:3448 virtual bool ConnectDataChannel(SctpDataChannel* data_channel) = 0;
ossu7bb87ee2017-01-23 12:56:2549 // Disconnects from the transport signals.
Taylor Brandstetter3a034e12020-07-09 22:32:3450 virtual void DisconnectDataChannel(SctpDataChannel* data_channel) = 0;
ossu7bb87ee2017-01-23 12:56:2551 // Adds the data channel SID to the transport for SCTP.
52 virtual void AddSctpDataStream(int sid) = 0;
Taylor Brandstettercdd05f02018-05-31 20:23:3253 // Begins the closing procedure by sending an outgoing stream reset. Still
54 // need to wait for callbacks to tell when this completes.
ossu7bb87ee2017-01-23 12:56:2555 virtual void RemoveSctpDataStream(int sid) = 0;
56 // Returns true if the transport channel is ready to send data.
57 virtual bool ReadyToSendData() const = 0;
58
59 protected:
Taylor Brandstetter3a034e12020-07-09 22:32:3460 virtual ~SctpDataChannelProviderInterface() {}
ossu7bb87ee2017-01-23 12:56:2561};
62
Tomas Gunnarsson0ca13d92020-06-10 10:17:5063// TODO(tommi): Change to not inherit from DataChannelInit but to have it as
64// a const member. Block access to the 'id' member since it cannot be const.
ossu7bb87ee2017-01-23 12:56:2565struct InternalDataChannelInit : public DataChannelInit {
Yves Gerey665174f2018-06-19 13:03:0566 enum OpenHandshakeRole { kOpener, kAcker, kNone };
ossu7bb87ee2017-01-23 12:56:2567 // The default role is kOpener because the default |negotiated| is false.
68 InternalDataChannelInit() : open_handshake_role(kOpener) {}
Harald Alvestrandf3736ed2019-04-08 11:09:3069 explicit InternalDataChannelInit(const DataChannelInit& base);
ossu7bb87ee2017-01-23 12:56:2570 OpenHandshakeRole open_handshake_role;
71};
72
Taylor Brandstetter3a034e12020-07-09 22:32:3473// Helper class to allocate unique IDs for SCTP DataChannels.
ossu7bb87ee2017-01-23 12:56:2574class SctpSidAllocator {
75 public:
76 // Gets the first unused odd/even id based on the DTLS role. If |role| is
77 // SSL_CLIENT, the allocated id starts from 0 and takes even numbers;
78 // otherwise, the id starts from 1 and takes odd numbers.
Taylor Brandstettercdd05f02018-05-31 20:23:3279 // Returns false if no ID can be allocated.
ossu7bb87ee2017-01-23 12:56:2580 bool AllocateSid(rtc::SSLRole role, int* sid);
81
82 // Attempts to reserve a specific sid. Returns false if it's unavailable.
83 bool ReserveSid(int sid);
84
85 // Indicates that |sid| isn't in use any more, and is thus available again.
86 void ReleaseSid(int sid);
87
88 private:
89 // Checks if |sid| is available to be assigned to a new SCTP data channel.
90 bool IsSidAvailable(int sid) const;
91
92 std::set<int> used_sids_;
93};
94
Taylor Brandstetter3a034e12020-07-09 22:32:3495// SctpDataChannel is an implementation of the DataChannelInterface based on
96// SctpTransport. It provides an implementation of unreliable or
97// reliabledata channels.
ossu7bb87ee2017-01-23 12:56:2598
99// DataChannel states:
100// kConnecting: The channel has been created the transport might not yet be
101// ready.
Taylor Brandstetter3a034e12020-07-09 22:32:34102// kOpen: The open handshake has been performed (if relevant) and the data
103// channel is able to send messages.
104// kClosing: DataChannelInterface::Close has been called, or the remote side
105// initiated the closing procedure, but the closing procedure has not
106// yet finished.
107// kClosed: The closing handshake is finished (possibly initiated from this,
108// side, possibly from the peer).
Taylor Brandstettercdd05f02018-05-31 20:23:32109//
110// How the closing procedure works for SCTP:
111// 1. Alice calls Close(), state changes to kClosing.
112// 2. Alice finishes sending any queued data.
113// 3. Alice calls RemoveSctpDataStream, sends outgoing stream reset.
114// 4. Bob receives incoming stream reset; OnClosingProcedureStartedRemotely
115// called.
Taylor Brandstetter3a034e12020-07-09 22:32:34116// 5. Bob sends outgoing stream reset.
117// 6. Alice receives incoming reset, Bob receives acknowledgement. Both receive
118// OnClosingProcedureComplete callback and transition to kClosed.
119class SctpDataChannel : public DataChannelInterface,
120 public sigslot::has_slots<> {
ossu7bb87ee2017-01-23 12:56:25121 public:
Taylor Brandstetter3a034e12020-07-09 22:32:34122 static rtc::scoped_refptr<SctpDataChannel> Create(
123 SctpDataChannelProviderInterface* provider,
ossu7bb87ee2017-01-23 12:56:25124 const std::string& label,
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42125 const InternalDataChannelInit& config,
126 rtc::Thread* signaling_thread,
127 rtc::Thread* network_thread);
ossu7bb87ee2017-01-23 12:56:25128
Taylor Brandstetter3a034e12020-07-09 22:32:34129 // Instantiates an API proxy for a SctpDataChannel instance that will be
130 // handed out to external callers.
Tomas Gunnarsson6476d0b2020-06-16 16:39:50131 static rtc::scoped_refptr<DataChannelInterface> CreateProxy(
Taylor Brandstetter3a034e12020-07-09 22:32:34132 rtc::scoped_refptr<SctpDataChannel> channel);
Bjorn Mellem175aa2e2018-11-08 19:23:22133
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42134 void RegisterObserver(DataChannelObserver* observer) override;
135 void UnregisterObserver() override;
ossu7bb87ee2017-01-23 12:56:25136
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42137 std::string label() const override { return label_; }
138 bool reliable() const override;
139 bool ordered() const override { return config_.ordered; }
Harald Alvestrandf3736ed2019-04-08 11:09:30140 // Backwards compatible accessors
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42141 uint16_t maxRetransmitTime() const override {
Harald Alvestrandf3736ed2019-04-08 11:09:30142 return config_.maxRetransmitTime ? *config_.maxRetransmitTime
143 : static_cast<uint16_t>(-1);
144 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42145 uint16_t maxRetransmits() const override {
Harald Alvestrandf3736ed2019-04-08 11:09:30146 return config_.maxRetransmits ? *config_.maxRetransmits
147 : static_cast<uint16_t>(-1);
148 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42149 absl::optional<int> maxPacketLifeTime() const override {
ossu7bb87ee2017-01-23 12:56:25150 return config_.maxRetransmitTime;
151 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42152 absl::optional<int> maxRetransmitsOpt() const override {
Harald Alvestrandf3736ed2019-04-08 11:09:30153 return config_.maxRetransmits;
154 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42155 std::string protocol() const override { return config_.protocol; }
156 bool negotiated() const override { return config_.negotiated; }
157 int id() const override { return config_.id; }
158 Priority priority() const override {
Harald Alvestrandfd5ae7f2020-05-16 06:37:49159 return config_.priority ? *config_.priority : Priority::kLow;
160 }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42161
Harald Alvestrand928e7a32019-07-31 11:16:45162 virtual int internal_id() const { return internal_id_; }
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42163
164 uint64_t buffered_amount() const override;
165 void Close() override;
166 DataState state() const override;
167 RTCError error() const override;
168 uint32_t messages_sent() const override;
169 uint64_t bytes_sent() const override;
170 uint32_t messages_received() const override;
171 uint64_t bytes_received() const override;
172 bool Send(const DataBuffer& buffer) override;
ossu7bb87ee2017-01-23 12:56:25173
Harald Alvestrand1f928d32019-03-28 10:29:38174 // Close immediately, ignoring any queued data or closing procedure.
Taylor Brandstetter3a034e12020-07-09 22:32:34175 // This is called when the underlying SctpTransport is being destroyed.
Harald Alvestrand1f928d32019-03-28 10:29:38176 // It is also called by the PeerConnection if SCTP ID assignment fails.
Harald Alvestranddfbfb462019-12-08 04:55:43177 void CloseAbruptlyWithError(RTCError error);
178 // Specializations of CloseAbruptlyWithError
179 void CloseAbruptlyWithDataChannelFailure(const std::string& message);
180 void CloseAbruptlyWithSctpCauseCode(const std::string& message,
181 uint16_t cause_code);
Harald Alvestrand1f928d32019-03-28 10:29:38182
ossu7bb87ee2017-01-23 12:56:25183 // Slots for provider to connect signals to.
Taylor Brandstetter3a034e12020-07-09 22:32:34184 //
185 // TODO(deadbeef): Make these private once we're hooking up signals ourselves,
186 // instead of relying on SctpDataChannelProviderInterface.
187
188 // Called when the SctpTransport's ready to use. That can happen when we've
189 // finished negotiation, or if the channel was created after negotiation has
190 // already finished.
191 void OnTransportReady(bool writable);
192
ossu7bb87ee2017-01-23 12:56:25193 void OnDataReceived(const cricket::ReceiveDataParams& params,
194 const rtc::CopyOnWriteBuffer& payload);
ossu7bb87ee2017-01-23 12:56:25195
ossu7bb87ee2017-01-23 12:56:25196 // Sets the SCTP sid and adds to transport layer if not set yet. Should only
197 // be called once.
198 void SetSctpSid(int sid);
Taylor Brandstettercdd05f02018-05-31 20:23:32199 // The remote side started the closing procedure by resetting its outgoing
200 // stream (our incoming stream). Sets state to kClosing.
201 void OnClosingProcedureStartedRemotely(int sid);
202 // The closing procedure is complete; both incoming and outgoing stream
203 // resets are done and the channel can transition to kClosed. Called
204 // asynchronously after RemoveSctpDataStream.
205 void OnClosingProcedureComplete(int sid);
ossu7bb87ee2017-01-23 12:56:25206 // Called when the transport channel is created.
207 // Only needs to be called for SCTP data channels.
208 void OnTransportChannelCreated();
Harald Alvestrand408cb4b2019-11-16 11:09:08209 // Called when the transport channel is unusable.
ossu7bb87ee2017-01-23 12:56:25210 // This method makes sure the DataChannel is disconnected and changes state
211 // to kClosed.
Harald Alvestrand408cb4b2019-11-16 11:09:08212 void OnTransportChannelClosed();
ossu7bb87ee2017-01-23 12:56:25213
Taylor Brandstetter3a034e12020-07-09 22:32:34214 DataChannelStats GetStats() const;
ossu7bb87ee2017-01-23 12:56:25215
216 // Emitted when state transitions to kOpen.
Taylor Brandstetter3a034e12020-07-09 22:32:34217 sigslot::signal1<DataChannelInterface*> SignalOpened;
ossu7bb87ee2017-01-23 12:56:25218 // Emitted when state transitions to kClosed.
Taylor Brandstetter3a034e12020-07-09 22:32:34219 // This signal can be used to tell when the channel's sid is free.
220 sigslot::signal1<DataChannelInterface*> SignalClosed;
ossu7bb87ee2017-01-23 12:56:25221
Harald Alvestrand928e7a32019-07-31 11:16:45222 // Reset the allocator for internal ID values for testing, so that
223 // the internal IDs generated are predictable. Test only.
224 static void ResetInternalIdAllocatorForTesting(int new_value);
225
ossu7bb87ee2017-01-23 12:56:25226 protected:
Taylor Brandstetter3a034e12020-07-09 22:32:34227 SctpDataChannel(const InternalDataChannelInit& config,
228 SctpDataChannelProviderInterface* client,
229 const std::string& label,
230 rtc::Thread* signaling_thread,
231 rtc::Thread* network_thread);
232 ~SctpDataChannel() override;
ossu7bb87ee2017-01-23 12:56:25233
234 private:
ossu7bb87ee2017-01-23 12:56:25235 // The OPEN(_ACK) signaling state.
236 enum HandshakeState {
237 kHandshakeInit,
238 kHandshakeShouldSendOpen,
239 kHandshakeShouldSendAck,
240 kHandshakeWaitingForAck,
241 kHandshakeReady
242 };
243
Tomas Gunnarsson0ca13d92020-06-10 10:17:50244 bool Init();
ossu7bb87ee2017-01-23 12:56:25245 void UpdateState();
246 void SetState(DataState state);
247 void DisconnectFromProvider();
248
249 void DeliverQueuedReceivedData();
250
251 void SendQueuedDataMessages();
252 bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
253 bool QueueSendDataMessage(const DataBuffer& buffer);
254
255 void SendQueuedControlMessages();
256 void QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer);
257 bool SendControlMessage(const rtc::CopyOnWriteBuffer& buffer);
258
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42259 rtc::Thread* const signaling_thread_;
260 rtc::Thread* const network_thread_;
Harald Alvestrand928e7a32019-07-31 11:16:45261 const int internal_id_;
Tomas Gunnarsson0ca13d92020-06-10 10:17:50262 const std::string label_;
263 const InternalDataChannelInit config_;
Taylor Brandstetter3a034e12020-07-09 22:32:34264 DataChannelObserver* observer_ RTC_GUARDED_BY(signaling_thread_) = nullptr;
265 DataState state_ RTC_GUARDED_BY(signaling_thread_) = kConnecting;
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42266 RTCError error_ RTC_GUARDED_BY(signaling_thread_);
Taylor Brandstetter3a034e12020-07-09 22:32:34267 uint32_t messages_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
268 uint64_t bytes_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
269 uint32_t messages_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
270 uint64_t bytes_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
Marina Cioceae448a3f2019-03-04 14:52:21271 // Number of bytes of data that have been queued using Send(). Increased
272 // before each transport send and decreased after each successful send.
Taylor Brandstetter3a034e12020-07-09 22:32:34273 uint64_t buffered_amount_ RTC_GUARDED_BY(signaling_thread_) = 0;
274 SctpDataChannelProviderInterface* const provider_
275 RTC_GUARDED_BY(signaling_thread_);
276 HandshakeState handshake_state_ RTC_GUARDED_BY(signaling_thread_) =
277 kHandshakeInit;
278 bool connected_to_provider_ RTC_GUARDED_BY(signaling_thread_) = false;
279 bool writable_ RTC_GUARDED_BY(signaling_thread_) = false;
Taylor Brandstettercdd05f02018-05-31 20:23:32280 // Did we already start the graceful SCTP closing procedure?
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42281 bool started_closing_procedure_ RTC_GUARDED_BY(signaling_thread_) = false;
ossu7bb87ee2017-01-23 12:56:25282 // Control messages that always have to get sent out before any queued
283 // data.
Tomas Gunnarsson7d3cfbf2020-06-15 11:47:42284 PacketQueue queued_control_data_ RTC_GUARDED_BY(signaling_thread_);
285 PacketQueue queued_received_data_ RTC_GUARDED_BY(signaling_thread_);
286 PacketQueue queued_send_data_ RTC_GUARDED_BY(signaling_thread_);
ossu7bb87ee2017-01-23 12:56:25287};
288
ossu7bb87ee2017-01-23 12:56:25289} // namespace webrtc
290
Taylor Brandstetter3a034e12020-07-09 22:32:34291#endif // PC_SCTP_DATA_CHANNEL_H_