dcsctp_transport.h (6628B)
1 /* 2 * Copyright (c) 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 #ifndef MEDIA_SCTP_DCSCTP_TRANSPORT_H_ 12 #define MEDIA_SCTP_DCSCTP_TRANSPORT_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <functional> 17 #include <memory> 18 #include <optional> 19 #include <string> 20 21 #include "absl/strings/string_view.h" 22 #include "api/array_view.h" 23 #include "api/dtls_transport_interface.h" 24 #include "api/environment/environment.h" 25 #include "api/field_trials_view.h" 26 #include "api/priority.h" 27 #include "api/rtc_error.h" 28 #include "api/sctp_transport_interface.h" 29 #include "api/task_queue/task_queue_base.h" 30 #include "api/transport/data_channel_transport_interface.h" 31 #include "media/sctp/sctp_transport_internal.h" 32 #include "net/dcsctp/public/dcsctp_message.h" 33 #include "net/dcsctp/public/dcsctp_options.h" 34 #include "net/dcsctp/public/dcsctp_socket.h" 35 #include "net/dcsctp/public/dcsctp_socket_factory.h" 36 #include "net/dcsctp/public/timeout.h" 37 #include "net/dcsctp/public/types.h" 38 #include "net/dcsctp/timer/task_queue_timeout.h" 39 #include "p2p/base/packet_transport_internal.h" 40 #include "p2p/dtls/dtls_transport_internal.h" 41 #include "rtc_base/containers/flat_map.h" 42 #include "rtc_base/copy_on_write_buffer.h" 43 #include "rtc_base/network/received_packet.h" 44 #include "rtc_base/random.h" 45 #include "rtc_base/third_party/sigslot/sigslot.h" 46 #include "rtc_base/thread.h" 47 #include "rtc_base/thread_annotations.h" 48 49 namespace webrtc { 50 51 class DcSctpTransport : public SctpTransportInternal, 52 public dcsctp::DcSctpSocketCallbacks, 53 public sigslot::has_slots<> { 54 public: 55 DcSctpTransport(const Environment& env, 56 Thread* network_thread, 57 DtlsTransportInternal* transport); 58 DcSctpTransport(const Environment& env, 59 Thread* network_thread, 60 DtlsTransportInternal* transport, 61 std::unique_ptr<dcsctp::DcSctpSocketFactory> socket_factory); 62 ~DcSctpTransport() override; 63 64 // SctpTransportInternal 65 void SetOnConnectedCallback(std::function<void()> callback) override; 66 void SetDataChannelSink(DataChannelSink* sink) override; 67 void SetDtlsTransport(DtlsTransportInternal* transport) override; 68 bool Start(const SctpOptions& options) override; 69 bool OpenStream(int sid, PriorityValue priority) override; 70 bool ResetStream(int sid) override; 71 RTCError SendData(int sid, 72 const SendDataParams& params, 73 const CopyOnWriteBuffer& payload) override; 74 bool ReadyToSendData() override; 75 int max_message_size() const override; 76 std::optional<int> max_outbound_streams() const override; 77 std::optional<int> max_inbound_streams() const override; 78 size_t buffered_amount(int sid) const override; 79 size_t buffered_amount_low_threshold(int sid) const override; 80 void SetBufferedAmountLowThreshold(int sid, size_t bytes) override; 81 void set_debug_name_for_testing(const char* debug_name) override; 82 83 private: 84 // dcsctp::DcSctpSocketCallbacks 85 dcsctp::SendPacketStatus SendPacketWithStatus( 86 ArrayView<const uint8_t> data) override; 87 std::unique_ptr<dcsctp::Timeout> CreateTimeout( 88 TaskQueueBase::DelayPrecision precision) override; 89 dcsctp::TimeMs TimeMillis() override; 90 uint32_t GetRandomInt(uint32_t low, uint32_t high) override; 91 void OnTotalBufferedAmountLow() override; 92 void OnBufferedAmountLow(dcsctp::StreamID stream_id) override; 93 void OnMessageReceived(dcsctp::DcSctpMessage message) override; 94 void OnError(dcsctp::ErrorKind error, absl::string_view message) override; 95 void OnAborted(dcsctp::ErrorKind error, absl::string_view message) override; 96 void OnConnected() override; 97 void OnClosed() override; 98 void OnConnectionRestarted() override; 99 void OnStreamsResetFailed(ArrayView<const dcsctp::StreamID> outgoing_streams, 100 absl::string_view reason) override; 101 void OnStreamsResetPerformed( 102 ArrayView<const dcsctp::StreamID> outgoing_streams) override; 103 void OnIncomingStreamsReset( 104 ArrayView<const dcsctp::StreamID> incoming_streams) override; 105 106 // Transport callbacks 107 void ConnectTransportSignals(); 108 void DisconnectTransportSignals(); 109 void OnTransportWritableState(PacketTransportInternal* transport); 110 void OnTransportReadPacket(PacketTransportInternal* transport, 111 const ReceivedIpPacket& packet); 112 void OnDtlsTransportState(DtlsTransportInternal* transport, 113 DtlsTransportState); 114 void MaybeConnectSocket(); 115 116 Thread* network_thread_; 117 DtlsTransportInternal* transport_; 118 Environment env_; 119 Random random_; 120 121 std::unique_ptr<dcsctp::DcSctpSocketFactory> socket_factory_; 122 dcsctp::TaskQueueTimeoutFactory task_queue_timeout_factory_; 123 std::unique_ptr<dcsctp::DcSctpSocketInterface> socket_; 124 std::string debug_name_ = "DcSctpTransport"; 125 CopyOnWriteBuffer receive_buffer_; 126 127 // Used to keep track of the state of data channels. 128 // Reset needs to happen both ways before signaling the transport 129 // is closed. 130 struct StreamState { 131 // True when the local connection has initiated the reset. 132 // If a connection receives a reset for a stream that isn't 133 // already being reset locally, it needs to fire the signal 134 // SignalClosingProcedureStartedRemotely. 135 bool closure_initiated = false; 136 // True when the local connection received OnIncomingStreamsReset 137 bool incoming_reset_done = false; 138 // True when the local connection received OnStreamsResetPerformed 139 bool outgoing_reset_done = false; 140 // Priority of the stream according to RFC 8831, section 6.4 141 dcsctp::StreamPriority priority = 142 dcsctp::StreamPriority(PriorityValue(Priority::kLow).value()); 143 }; 144 145 // Map of all currently open or closing data channels 146 flat_map<dcsctp::StreamID, StreamState> stream_states_ 147 RTC_GUARDED_BY(network_thread_); 148 bool ready_to_send_data_ RTC_GUARDED_BY(network_thread_) = false; 149 std::function<void()> on_connected_callback_ RTC_GUARDED_BY(network_thread_); 150 DataChannelSink* data_channel_sink_ RTC_GUARDED_BY(network_thread_) = nullptr; 151 152 static dcsctp::DcSctpOptions CreateDcSctpOptions( 153 const SctpOptions& options, 154 const FieldTrialsView& field_trials); 155 }; 156 157 } // namespace webrtc 158 159 #endif // MEDIA_SCTP_DCSCTP_TRANSPORT_H_