2017-01-23 04:56:25 -08:00
|
|
|
/*
|
2020-07-09 15:32:34 -07:00
|
|
|
* Copyright 2020 The WebRTC project authors. All Rights Reserved.
|
2017-01-23 04:56:25 -08:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
#ifndef PC_SCTP_DATA_CHANNEL_H_
|
|
|
|
|
#define PC_SCTP_DATA_CHANNEL_H_
|
2017-01-23 04:56:25 -08:00
|
|
|
|
2021-01-29 14:45:08 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2018-12-13 14:19:10 -08:00
|
|
|
#include <memory>
|
2017-01-23 04:56:25 -08:00
|
|
|
#include <set>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2021-01-29 14:45:08 +00:00
|
|
|
#include "absl/types/optional.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "api/data_channel_interface.h"
|
2020-05-16 08:37:49 +02:00
|
|
|
#include "api/priority.h"
|
2021-01-29 14:45:08 +00:00
|
|
|
#include "api/rtc_error.h"
|
2019-01-25 20:26:48 +01:00
|
|
|
#include "api/scoped_refptr.h"
|
2020-06-16 16:39:13 +02:00
|
|
|
#include "api/transport/data_channel_transport_interface.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "media/base/media_channel.h"
|
2020-07-09 15:32:34 -07:00
|
|
|
#include "pc/data_channel_utils.h"
|
2023-03-12 16:59:25 +01:00
|
|
|
#include "pc/sctp_utils.h"
|
|
|
|
|
#include "rtc_base/containers/flat_set.h"
|
2021-01-29 14:45:08 +00:00
|
|
|
#include "rtc_base/copy_on_write_buffer.h"
|
2020-07-09 15:32:34 -07:00
|
|
|
#include "rtc_base/ssl_stream_adapter.h" // For SSLRole
|
2021-01-29 14:45:08 +00:00
|
|
|
#include "rtc_base/thread.h"
|
|
|
|
|
#include "rtc_base/thread_annotations.h"
|
2023-03-02 15:42:06 +01:00
|
|
|
#include "rtc_base/weak_ptr.h"
|
2017-01-23 04:56:25 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
class SctpDataChannel;
|
2017-01-23 04:56:25 -08:00
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
// TODO(deadbeef): Get rid of this and have SctpDataChannel depend on
|
|
|
|
|
// SctpTransportInternal (pure virtual SctpTransport interface) instead.
|
2022-05-11 09:35:36 +00:00
|
|
|
class SctpDataChannelControllerInterface {
|
2017-01-23 04:56:25 -08:00
|
|
|
public:
|
|
|
|
|
// Sends the data to the transport.
|
2021-05-10 11:29:56 +02:00
|
|
|
virtual bool SendData(int sid,
|
|
|
|
|
const SendDataParams& params,
|
2017-01-23 04:56:25 -08:00
|
|
|
const rtc::CopyOnWriteBuffer& payload,
|
|
|
|
|
cricket::SendDataResult* result) = 0;
|
|
|
|
|
// Connects to the transport signals.
|
2020-07-09 15:32:34 -07:00
|
|
|
virtual bool ConnectDataChannel(SctpDataChannel* data_channel) = 0;
|
2017-01-23 04:56:25 -08:00
|
|
|
// Disconnects from the transport signals.
|
2020-07-09 15:32:34 -07:00
|
|
|
virtual void DisconnectDataChannel(SctpDataChannel* data_channel) = 0;
|
2017-01-23 04:56:25 -08:00
|
|
|
// Adds the data channel SID to the transport for SCTP.
|
|
|
|
|
virtual void AddSctpDataStream(int sid) = 0;
|
2018-05-31 13:23:32 -07:00
|
|
|
// Begins the closing procedure by sending an outgoing stream reset. Still
|
|
|
|
|
// need to wait for callbacks to tell when this completes.
|
2017-01-23 04:56:25 -08:00
|
|
|
virtual void RemoveSctpDataStream(int sid) = 0;
|
|
|
|
|
// Returns true if the transport channel is ready to send data.
|
|
|
|
|
virtual bool ReadyToSendData() const = 0;
|
2023-03-02 10:51:16 +01:00
|
|
|
// Notifies the controller of state changes.
|
|
|
|
|
virtual void OnChannelStateChanged(SctpDataChannel* data_channel,
|
|
|
|
|
DataChannelInterface::DataState state) = 0;
|
2017-01-23 04:56:25 -08:00
|
|
|
|
|
|
|
|
protected:
|
2022-05-11 09:35:36 +00:00
|
|
|
virtual ~SctpDataChannelControllerInterface() {}
|
2017-01-23 04:56:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct InternalDataChannelInit : public DataChannelInit {
|
|
|
|
|
enum OpenHandshakeRole { kOpener, kAcker, kNone };
|
2021-07-30 22:30:23 +02:00
|
|
|
// The default role is kOpener because the default `negotiated` is false.
|
2017-01-23 04:56:25 -08:00
|
|
|
InternalDataChannelInit() : open_handshake_role(kOpener) {}
|
2019-04-08 13:09:30 +02:00
|
|
|
explicit InternalDataChannelInit(const DataChannelInit& base);
|
2023-03-03 11:28:23 +01:00
|
|
|
|
|
|
|
|
// Does basic validation to determine if a data channel instance can be
|
|
|
|
|
// constructed using the configuration.
|
|
|
|
|
bool IsValid() const;
|
|
|
|
|
|
2017-01-23 04:56:25 -08:00
|
|
|
OpenHandshakeRole open_handshake_role;
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
// Helper class to allocate unique IDs for SCTP DataChannels.
|
2017-01-23 04:56:25 -08:00
|
|
|
class SctpSidAllocator {
|
|
|
|
|
public:
|
2021-07-30 22:30:23 +02:00
|
|
|
// Gets the first unused odd/even id based on the DTLS role. If `role` is
|
2017-01-23 04:56:25 -08:00
|
|
|
// SSL_CLIENT, the allocated id starts from 0 and takes even numbers;
|
|
|
|
|
// otherwise, the id starts from 1 and takes odd numbers.
|
2018-05-31 13:23:32 -07:00
|
|
|
// Returns false if no ID can be allocated.
|
2023-03-12 16:59:25 +01:00
|
|
|
bool AllocateSid(rtc::SSLRole role, StreamId* sid);
|
2017-01-23 04:56:25 -08:00
|
|
|
|
|
|
|
|
// Attempts to reserve a specific sid. Returns false if it's unavailable.
|
2023-03-12 16:59:25 +01:00
|
|
|
bool ReserveSid(const StreamId& sid);
|
2017-01-23 04:56:25 -08:00
|
|
|
|
2021-07-30 22:30:23 +02:00
|
|
|
// Indicates that `sid` isn't in use any more, and is thus available again.
|
2023-03-12 16:59:25 +01:00
|
|
|
void ReleaseSid(const StreamId& sid);
|
2017-01-23 04:56:25 -08:00
|
|
|
|
|
|
|
|
private:
|
2023-03-12 16:59:25 +01:00
|
|
|
webrtc::flat_set<StreamId> used_sids_;
|
2017-01-23 04:56:25 -08:00
|
|
|
};
|
|
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
// SctpDataChannel is an implementation of the DataChannelInterface based on
|
|
|
|
|
// SctpTransport. It provides an implementation of unreliable or
|
2023-03-14 13:21:06 +01:00
|
|
|
// reliable data channels.
|
2017-01-23 04:56:25 -08:00
|
|
|
|
|
|
|
|
// DataChannel states:
|
|
|
|
|
// kConnecting: The channel has been created the transport might not yet be
|
|
|
|
|
// ready.
|
2020-07-09 15:32:34 -07:00
|
|
|
// kOpen: The open handshake has been performed (if relevant) and the data
|
|
|
|
|
// channel is able to send messages.
|
|
|
|
|
// kClosing: DataChannelInterface::Close has been called, or the remote side
|
|
|
|
|
// initiated the closing procedure, but the closing procedure has not
|
|
|
|
|
// yet finished.
|
|
|
|
|
// kClosed: The closing handshake is finished (possibly initiated from this,
|
|
|
|
|
// side, possibly from the peer).
|
2018-05-31 13:23:32 -07:00
|
|
|
//
|
|
|
|
|
// How the closing procedure works for SCTP:
|
|
|
|
|
// 1. Alice calls Close(), state changes to kClosing.
|
|
|
|
|
// 2. Alice finishes sending any queued data.
|
|
|
|
|
// 3. Alice calls RemoveSctpDataStream, sends outgoing stream reset.
|
|
|
|
|
// 4. Bob receives incoming stream reset; OnClosingProcedureStartedRemotely
|
|
|
|
|
// called.
|
2020-07-09 15:32:34 -07:00
|
|
|
// 5. Bob sends outgoing stream reset.
|
|
|
|
|
// 6. Alice receives incoming reset, Bob receives acknowledgement. Both receive
|
|
|
|
|
// OnClosingProcedureComplete callback and transition to kClosed.
|
2023-03-14 13:21:06 +01:00
|
|
|
class SctpDataChannel : public DataChannelInterface {
|
2017-01-23 04:56:25 -08:00
|
|
|
public:
|
2020-07-09 15:32:34 -07:00
|
|
|
static rtc::scoped_refptr<SctpDataChannel> Create(
|
2023-03-02 15:42:06 +01:00
|
|
|
rtc::WeakPtr<SctpDataChannelControllerInterface> controller,
|
2017-01-23 04:56:25 -08:00
|
|
|
const std::string& label,
|
2020-06-15 13:47:42 +02:00
|
|
|
const InternalDataChannelInit& config,
|
|
|
|
|
rtc::Thread* signaling_thread,
|
|
|
|
|
rtc::Thread* network_thread);
|
2017-01-23 04:56:25 -08:00
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
// Instantiates an API proxy for a SctpDataChannel instance that will be
|
|
|
|
|
// handed out to external callers.
|
2020-06-16 18:39:50 +02:00
|
|
|
static rtc::scoped_refptr<DataChannelInterface> CreateProxy(
|
2020-07-09 15:32:34 -07:00
|
|
|
rtc::scoped_refptr<SctpDataChannel> channel);
|
2018-11-08 11:23:22 -08:00
|
|
|
|
2020-06-15 13:47:42 +02:00
|
|
|
void RegisterObserver(DataChannelObserver* observer) override;
|
|
|
|
|
void UnregisterObserver() override;
|
2017-01-23 04:56:25 -08:00
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
std::string label() const override;
|
2020-06-15 13:47:42 +02:00
|
|
|
bool reliable() const override;
|
2023-03-12 16:59:25 +01:00
|
|
|
bool ordered() const override;
|
|
|
|
|
|
2019-04-08 13:09:30 +02:00
|
|
|
// Backwards compatible accessors
|
2023-03-12 16:59:25 +01:00
|
|
|
uint16_t maxRetransmitTime() const override;
|
|
|
|
|
uint16_t maxRetransmits() const override;
|
|
|
|
|
|
|
|
|
|
absl::optional<int> maxPacketLifeTime() const override;
|
|
|
|
|
absl::optional<int> maxRetransmitsOpt() const override;
|
|
|
|
|
std::string protocol() const override;
|
|
|
|
|
bool negotiated() const override;
|
|
|
|
|
int id() const override;
|
|
|
|
|
Priority priority() const override;
|
2020-06-15 13:47:42 +02:00
|
|
|
|
|
|
|
|
uint64_t buffered_amount() const override;
|
|
|
|
|
void Close() override;
|
|
|
|
|
DataState state() const override;
|
|
|
|
|
RTCError error() const override;
|
|
|
|
|
uint32_t messages_sent() const override;
|
|
|
|
|
uint64_t bytes_sent() const override;
|
|
|
|
|
uint32_t messages_received() const override;
|
|
|
|
|
uint64_t bytes_received() const override;
|
|
|
|
|
bool Send(const DataBuffer& buffer) override;
|
2017-01-23 04:56:25 -08:00
|
|
|
|
2019-03-28 11:29:38 +01:00
|
|
|
// Close immediately, ignoring any queued data or closing procedure.
|
2020-07-09 15:32:34 -07:00
|
|
|
// This is called when the underlying SctpTransport is being destroyed.
|
2019-03-28 11:29:38 +01:00
|
|
|
// It is also called by the PeerConnection if SCTP ID assignment fails.
|
2019-12-08 05:55:43 +01:00
|
|
|
void CloseAbruptlyWithError(RTCError error);
|
|
|
|
|
// Specializations of CloseAbruptlyWithError
|
|
|
|
|
void CloseAbruptlyWithDataChannelFailure(const std::string& message);
|
2019-03-28 11:29:38 +01:00
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
// Slots for controller to connect signals to.
|
2020-07-09 15:32:34 -07:00
|
|
|
//
|
|
|
|
|
// TODO(deadbeef): Make these private once we're hooking up signals ourselves,
|
2022-05-11 09:35:36 +00:00
|
|
|
// instead of relying on SctpDataChannelControllerInterface.
|
2020-07-09 15:32:34 -07:00
|
|
|
|
|
|
|
|
// Called when the SctpTransport's ready to use. That can happen when we've
|
|
|
|
|
// finished negotiation, or if the channel was created after negotiation has
|
|
|
|
|
// already finished.
|
|
|
|
|
void OnTransportReady(bool writable);
|
|
|
|
|
|
2017-01-23 04:56:25 -08:00
|
|
|
void OnDataReceived(const cricket::ReceiveDataParams& params,
|
|
|
|
|
const rtc::CopyOnWriteBuffer& payload);
|
|
|
|
|
|
|
|
|
|
// Sets the SCTP sid and adds to transport layer if not set yet. Should only
|
|
|
|
|
// be called once.
|
2023-03-12 16:59:25 +01:00
|
|
|
void SetSctpSid(const StreamId& sid);
|
2023-03-14 13:21:06 +01:00
|
|
|
|
2018-05-31 13:23:32 -07:00
|
|
|
// The remote side started the closing procedure by resetting its outgoing
|
|
|
|
|
// stream (our incoming stream). Sets state to kClosing.
|
2023-03-14 13:21:06 +01:00
|
|
|
void OnClosingProcedureStartedRemotely();
|
2018-05-31 13:23:32 -07:00
|
|
|
// The closing procedure is complete; both incoming and outgoing stream
|
|
|
|
|
// resets are done and the channel can transition to kClosed. Called
|
|
|
|
|
// asynchronously after RemoveSctpDataStream.
|
2023-03-14 09:23:51 +01:00
|
|
|
void OnClosingProcedureComplete();
|
2017-01-23 04:56:25 -08:00
|
|
|
// Called when the transport channel is created.
|
|
|
|
|
// Only needs to be called for SCTP data channels.
|
|
|
|
|
void OnTransportChannelCreated();
|
2019-11-16 12:09:08 +01:00
|
|
|
// Called when the transport channel is unusable.
|
2017-01-23 04:56:25 -08:00
|
|
|
// This method makes sure the DataChannel is disconnected and changes state
|
|
|
|
|
// to kClosed.
|
2021-06-29 14:58:23 +02:00
|
|
|
void OnTransportChannelClosed(RTCError error);
|
2017-01-23 04:56:25 -08:00
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
DataChannelStats GetStats() const;
|
2017-01-23 04:56:25 -08:00
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
const StreamId& sid() const { return id_; }
|
|
|
|
|
|
2019-07-31 07:16:45 -04:00
|
|
|
// Reset the allocator for internal ID values for testing, so that
|
|
|
|
|
// the internal IDs generated are predictable. Test only.
|
|
|
|
|
static void ResetInternalIdAllocatorForTesting(int new_value);
|
|
|
|
|
|
2017-01-23 04:56:25 -08:00
|
|
|
protected:
|
2020-07-09 15:32:34 -07:00
|
|
|
SctpDataChannel(const InternalDataChannelInit& config,
|
2023-03-02 15:42:06 +01:00
|
|
|
rtc::WeakPtr<SctpDataChannelControllerInterface> controller,
|
2020-07-09 15:32:34 -07:00
|
|
|
const std::string& label,
|
|
|
|
|
rtc::Thread* signaling_thread,
|
|
|
|
|
rtc::Thread* network_thread);
|
|
|
|
|
~SctpDataChannel() override;
|
2017-01-23 04:56:25 -08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// The OPEN(_ACK) signaling state.
|
|
|
|
|
enum HandshakeState {
|
|
|
|
|
kHandshakeInit,
|
|
|
|
|
kHandshakeShouldSendOpen,
|
|
|
|
|
kHandshakeShouldSendAck,
|
|
|
|
|
kHandshakeWaitingForAck,
|
|
|
|
|
kHandshakeReady
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-03 11:28:23 +01:00
|
|
|
void Init();
|
2017-01-23 04:56:25 -08:00
|
|
|
void UpdateState();
|
|
|
|
|
void SetState(DataState state);
|
2022-05-11 09:35:36 +00:00
|
|
|
void DisconnectFromTransport();
|
2017-01-23 04:56:25 -08:00
|
|
|
|
|
|
|
|
void DeliverQueuedReceivedData();
|
|
|
|
|
|
|
|
|
|
void SendQueuedDataMessages();
|
|
|
|
|
bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
|
|
|
|
|
bool QueueSendDataMessage(const DataBuffer& buffer);
|
|
|
|
|
|
|
|
|
|
void SendQueuedControlMessages();
|
|
|
|
|
void QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer);
|
|
|
|
|
bool SendControlMessage(const rtc::CopyOnWriteBuffer& buffer);
|
|
|
|
|
|
2020-06-15 13:47:42 +02:00
|
|
|
rtc::Thread* const signaling_thread_;
|
|
|
|
|
rtc::Thread* const network_thread_;
|
2023-03-12 16:59:25 +01:00
|
|
|
StreamId id_;
|
2019-07-31 07:16:45 -04:00
|
|
|
const int internal_id_;
|
2020-06-10 12:17:50 +02:00
|
|
|
const std::string label_;
|
2023-03-12 16:59:25 +01:00
|
|
|
const std::string protocol_;
|
|
|
|
|
const absl::optional<int> max_retransmit_time_;
|
|
|
|
|
const absl::optional<int> max_retransmits_;
|
|
|
|
|
const absl::optional<Priority> priority_;
|
|
|
|
|
const bool negotiated_;
|
|
|
|
|
const bool ordered_;
|
|
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
DataChannelObserver* observer_ RTC_GUARDED_BY(signaling_thread_) = nullptr;
|
|
|
|
|
DataState state_ RTC_GUARDED_BY(signaling_thread_) = kConnecting;
|
2020-06-15 13:47:42 +02:00
|
|
|
RTCError error_ RTC_GUARDED_BY(signaling_thread_);
|
2020-07-09 15:32:34 -07:00
|
|
|
uint32_t messages_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
|
|
|
|
|
uint64_t bytes_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
|
|
|
|
|
uint32_t messages_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
|
|
|
|
|
uint64_t bytes_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
|
2023-03-02 15:42:06 +01:00
|
|
|
rtc::WeakPtr<SctpDataChannelControllerInterface> controller_
|
2020-07-09 15:32:34 -07:00
|
|
|
RTC_GUARDED_BY(signaling_thread_);
|
|
|
|
|
HandshakeState handshake_state_ RTC_GUARDED_BY(signaling_thread_) =
|
|
|
|
|
kHandshakeInit;
|
2022-05-11 09:35:36 +00:00
|
|
|
bool connected_to_transport_ RTC_GUARDED_BY(signaling_thread_) = false;
|
2020-07-09 15:32:34 -07:00
|
|
|
bool writable_ RTC_GUARDED_BY(signaling_thread_) = false;
|
2018-05-31 13:23:32 -07:00
|
|
|
// Did we already start the graceful SCTP closing procedure?
|
2020-06-15 13:47:42 +02:00
|
|
|
bool started_closing_procedure_ RTC_GUARDED_BY(signaling_thread_) = false;
|
2017-01-23 04:56:25 -08:00
|
|
|
// Control messages that always have to get sent out before any queued
|
|
|
|
|
// data.
|
2020-06-15 13:47:42 +02:00
|
|
|
PacketQueue queued_control_data_ RTC_GUARDED_BY(signaling_thread_);
|
|
|
|
|
PacketQueue queued_received_data_ RTC_GUARDED_BY(signaling_thread_);
|
|
|
|
|
PacketQueue queued_send_data_ RTC_GUARDED_BY(signaling_thread_);
|
2017-01-23 04:56:25 -08:00
|
|
|
};
|
|
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
// Downcast a PeerConnectionInterface that points to a proxy object
|
|
|
|
|
// to its underlying SctpDataChannel object. For testing only.
|
|
|
|
|
SctpDataChannel* DowncastProxiedDataChannelInterfaceToSctpDataChannelForTesting(
|
|
|
|
|
DataChannelInterface* channel);
|
|
|
|
|
|
2017-01-23 04:56:25 -08:00
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
#endif // PC_SCTP_DATA_CHANNEL_H_
|