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