sctp_transport_internal.h (6432B)
1 /* 2 * Copyright (c) 2016 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_SCTP_TRANSPORT_INTERNAL_H_ 12 #define MEDIA_SCTP_SCTP_TRANSPORT_INTERNAL_H_ 13 14 // TODO(deadbeef): Move SCTP code out of media/, and make it not depend on 15 // anything in media/. 16 17 #include <cstddef> 18 #include <cstdint> 19 #include <functional> 20 #include <optional> 21 22 #include "api/priority.h" 23 #include "api/rtc_error.h" 24 #include "api/sctp_transport_interface.h" 25 #include "api/transport/data_channel_transport_interface.h" 26 #include "p2p/dtls/dtls_transport_internal.h" 27 #include "rtc_base/copy_on_write_buffer.h" 28 29 namespace webrtc { 30 31 // Constants that are important to API users 32 33 // The number of outgoing streams that we'll negotiate. Since stream IDs (SIDs) 34 // are 0-based, the highest usable SID is 1023. 35 // 36 // It's recommended to use the maximum of 65535 in: 37 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.2 38 // However, we use 1024 in order to save memory. usrsctp allocates 104 bytes 39 // for each pair of incoming/outgoing streams (on a 64-bit system), so 65535 40 // streams would waste ~6MB. 41 // 42 // Note: "max" and "min" here are inclusive. 43 constexpr uint16_t kMaxSctpStreams = 1024; 44 constexpr uint16_t kMaxSctpSid = kMaxSctpStreams - 1; 45 constexpr uint16_t kMinSctpSid = 0; 46 // The maximum number of streams that can be negotiated according to spec. 47 constexpr uint16_t kSpecMaxSctpSid = 65535; 48 49 // This is the default SCTP port to use. It is passed along the wire and the 50 // connectee and connector must be using the same port. It is not related to the 51 // ports at the IP level. (Corresponds to: sockaddr_conn.sconn_port in 52 // usrsctp.h) 53 const int kSctpDefaultPort = 5000; 54 55 // Error cause codes defined at 56 // https://www.iana.org/assignments/sctp-parameters/sctp-parameters.xhtml#sctp-parameters-24 57 enum class SctpErrorCauseCode : uint16_t { 58 kInvalidStreamIdentifier = 1, 59 kMissingMandatoryParameter = 2, 60 kStaleCookieError = 3, 61 kOutOfResource = 4, 62 kUnresolvableAddress = 5, 63 kUnrecognizedChunkType = 6, 64 kInvalidMandatoryParameter = 7, 65 kUnrecognizedParameters = 8, 66 kNoUserData = 9, 67 kCookieReceivedWhileShuttingDown = 10, 68 kRestartWithNewAddresses = 11, 69 kUserInitiatedAbort = 12, 70 kProtocolViolation = 13, 71 }; 72 73 // Abstract SctpTransport interface for use internally (by PeerConnection etc.). 74 // Exists to allow mock/fake SctpTransports to be created. 75 class SctpTransportInternal { 76 public: 77 virtual ~SctpTransportInternal() {} 78 79 virtual void SetOnConnectedCallback(std::function<void()> callback) = 0; 80 virtual void SetDataChannelSink(DataChannelSink* sink) = 0; 81 82 // Changes what underlying DTLS transport is uses. Used when switching which 83 // bundled transport the SctpTransport uses. 84 virtual void SetDtlsTransport(DtlsTransportInternal* transport) = 0; 85 86 // When Start is called, connects as soon as possible; this can be called 87 // before DTLS completes, in which case the connection will begin when DTLS 88 // completes. This method can be called multiple times, though not if either 89 // of the ports are changed. 90 // 91 virtual bool Start(const SctpOptions& options) = 0; 92 // TODO(deadbeef): Support calling Start with different local/remote ports 93 // and create a new association? Not clear if this is something we need to 94 // support though. See: https://github.com/w3c/webrtc-pc/issues/979 95 [[deprecated("Call with SctpOptions")]] 96 virtual bool Start(int local_sctp_port, 97 int remote_sctp_port, 98 int max_message_size) { 99 return Start({ 100 .local_port = local_sctp_port, 101 .remote_port = remote_sctp_port, 102 .max_message_size = max_message_size, 103 }); 104 } 105 106 // NOTE: Initially there was a "Stop" method here, but it was never used, so 107 // it was removed. 108 109 // Informs SctpTransport that `sid` will start being used. Returns false if 110 // it is impossible to use `sid`, or if it's already in use. 111 // Until calling this, can't send data using `sid`. 112 // TODO(deadbeef): Actually implement the "returns false if `sid` can't be 113 // used" part. See: 114 // https://bugs.chromium.org/p/chromium/issues/detail?id=619849 115 virtual bool OpenStream(int sid, PriorityValue priority) = 0; 116 // The inverse of OpenStream. Begins the closing procedure, which will 117 // eventually result in SignalClosingProcedureComplete on the side that 118 // initiates it, and both SignalClosingProcedureStartedRemotely and 119 // SignalClosingProcedureComplete on the other side. 120 virtual bool ResetStream(int sid) = 0; 121 // Send data down this channel. 122 // Returns RTCError::OK() if successful an error otherwise. Notably 123 // RTCErrorType::RESOURCE_EXHAUSTED for blocked operations. 124 virtual RTCError SendData(int sid, 125 const SendDataParams& params, 126 const CopyOnWriteBuffer& payload) = 0; 127 128 // Indicates when the SCTP socket is created and not blocked by congestion 129 // control. This changes to false when SDR_BLOCK is returned from SendData, 130 // and 131 // changes to true when SignalReadyToSendData is fired. The underlying DTLS/ 132 // ICE channels may be unwritable while ReadyToSendData is true, because data 133 // can still be queued in usrsctp. 134 virtual bool ReadyToSendData() = 0; 135 // Returns the current max message size, set with Start(). 136 virtual int max_message_size() const = 0; 137 // Returns the current negotiated max # of outbound streams. 138 // Will return std::nullopt if negotiation is incomplete. 139 virtual std::optional<int> max_outbound_streams() const = 0; 140 // Returns the current negotiated max # of inbound streams. 141 virtual std::optional<int> max_inbound_streams() const = 0; 142 // Returns the amount of buffered data in the send queue for a stream. 143 virtual size_t buffered_amount(int sid) const = 0; 144 virtual size_t buffered_amount_low_threshold(int sid) const = 0; 145 virtual void SetBufferedAmountLowThreshold(int sid, size_t bytes) = 0; 146 147 // Helper for debugging. 148 virtual void set_debug_name_for_testing(const char* debug_name) = 0; 149 }; 150 151 } // namespace webrtc 152 153 154 #endif // MEDIA_SCTP_SCTP_TRANSPORT_INTERNAL_H_