| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 API_SCTP_TRANSPORT_INTERFACE_H_ |
| 12 | #define API_SCTP_TRANSPORT_INTERFACE_H_ |
| 13 | |
| Tommi | 8955cd7 | 2025-10-10 19:11:25 | [diff] [blame] | 14 | #include <cstdint> |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 15 | #include <optional> |
| Philipp Hancke | 7fc962d | 2025-10-23 10:37:21 | [diff] [blame] | 16 | #include <vector> |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 17 | |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 18 | #include "api/dtls_transport_interface.h" |
| Harald Alvestrand | e8a2b3c | 2023-10-31 13:30:30 | [diff] [blame] | 19 | #include "api/ref_count.h" |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 20 | #include "api/scoped_refptr.h" |
| Dor Hen | aefed55 | 2024-06-18 13:20:35 | [diff] [blame] | 21 | #include "rtc_base/system/rtc_export.h" |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| Tommi | 8955cd7 | 2025-10-10 19:11:25 | [diff] [blame] | 25 | // Constants that are important to API users |
| 26 | |
| 27 | // The number of outgoing streams that we'll negotiate. Since stream IDs (SIDs) |
| 28 | // are 0-based, the highest usable SID is 1023. |
| 29 | // |
| 30 | // It's recommended to use the maximum of 65535 in: |
| 31 | // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.2 |
| 32 | // However, we use 1024 in order to save memory. usrsctp allocates 104 bytes |
| 33 | // for each pair of incoming/outgoing streams (on a 64-bit system), so 65535 |
| 34 | // streams would waste ~6MB. |
| 35 | // |
| 36 | // Note: "max" and "min" here are inclusive. |
| 37 | constexpr uint16_t kMaxSctpStreams = 1024; |
| 38 | constexpr uint16_t kMaxSctpSid = kMaxSctpStreams - 1; |
| 39 | constexpr uint16_t kMinSctpSid = 0; |
| 40 | // The maximum number of streams that can be negotiated according to spec. |
| 41 | constexpr uint16_t kSpecMaxSctpSid = 65535; |
| 42 | |
| 43 | // This is the default SCTP port to use. It is passed along the wire and the |
| 44 | // connectee and connector must be using the same port. It is not related to the |
| 45 | // ports at the IP level. (Corresponds to: sockaddr_conn.sconn_port in |
| 46 | // usrsctp.h) |
| 47 | const int kSctpDefaultPort = 5000; |
| 48 | |
| 49 | // Error cause codes defined at |
| 50 | // https://www.iana.org/assignments/sctp-parameters/sctp-parameters.xhtml#sctp-parameters-24 |
| 51 | enum class SctpErrorCauseCode : uint16_t { |
| 52 | kInvalidStreamIdentifier = 1, |
| 53 | kMissingMandatoryParameter = 2, |
| 54 | kStaleCookieError = 3, |
| 55 | kOutOfResource = 4, |
| 56 | kUnresolvableAddress = 5, |
| 57 | kUnrecognizedChunkType = 6, |
| 58 | kInvalidMandatoryParameter = 7, |
| 59 | kUnrecognizedParameters = 8, |
| 60 | kNoUserData = 9, |
| 61 | kCookieReceivedWhileShuttingDown = 10, |
| 62 | kRestartWithNewAddresses = 11, |
| 63 | kUserInitiatedAbort = 12, |
| 64 | kProtocolViolation = 13, |
| 65 | }; |
| 66 | |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 67 | // States of a SCTP transport, corresponding to the JS API specification. |
| 68 | // http://w3c.github.io/webrtc-pc/#dom-rtcsctptransportstate |
| 69 | enum class SctpTransportState { |
| 70 | kNew, // Has not started negotiating yet. Non-standard state. |
| 71 | kConnecting, // In the process of negotiating an association. |
| 72 | kConnected, // Completed negotiation of an association. |
| 73 | kClosed, // Closed by local or remote party. |
| 74 | kNumValues |
| 75 | }; |
| 76 | |
| 77 | // This object gives snapshot information about the changeable state of a |
| 78 | // SctpTransport. |
| 79 | // It reflects the readonly attributes of the object in the specification. |
| 80 | // http://w3c.github.io/webrtc-pc/#rtcsctptransport-interface |
| Mirko Bonadei | 66e7679 | 2019-04-02 09:33:59 | [diff] [blame] | 81 | class RTC_EXPORT SctpTransportInformation { |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 82 | public: |
| Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 83 | SctpTransportInformation() = default; |
| 84 | SctpTransportInformation(const SctpTransportInformation&) = default; |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 85 | explicit SctpTransportInformation(SctpTransportState state); |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 86 | SctpTransportInformation(SctpTransportState state, |
| 87 | scoped_refptr<DtlsTransportInterface> dtls_transport, |
| 88 | std::optional<double> max_message_size, |
| 89 | std::optional<int> max_channels); |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 90 | ~SctpTransportInformation(); |
| 91 | // The DTLS transport that supports this SCTP transport. |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 92 | scoped_refptr<DtlsTransportInterface> dtls_transport() const { |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 93 | return dtls_transport_; |
| 94 | } |
| 95 | SctpTransportState state() const { return state_; } |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 96 | std::optional<double> MaxMessageSize() const { return max_message_size_; } |
| 97 | std::optional<int> MaxChannels() const { return max_channels_; } |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 98 | |
| 99 | private: |
| Bjorn Terelius | b41f07b | 2024-02-29 15:22:01 | [diff] [blame] | 100 | SctpTransportState state_ = SctpTransportState::kNew; |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 101 | scoped_refptr<DtlsTransportInterface> dtls_transport_; |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 102 | std::optional<double> max_message_size_; |
| 103 | std::optional<int> max_channels_; |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | class SctpTransportObserverInterface { |
| 107 | public: |
| 108 | // This callback carries information about the state of the transport. |
| 109 | // The argument is a pass-by-value snapshot of the state. |
| 110 | // The callback will be called on the network thread. |
| 111 | virtual void OnStateChange(SctpTransportInformation info) = 0; |
| 112 | |
| 113 | protected: |
| 114 | virtual ~SctpTransportObserverInterface() = default; |
| 115 | }; |
| 116 | |
| 117 | // A SCTP transport, as represented to the outside world. |
| 118 | // This object is created on the network thread, and can only be |
| 119 | // accessed on that thread, except for functions explicitly marked otherwise. |
| 120 | // References can be held by other threads, and destruction can therefore |
| 121 | // be initiated by other threads. |
| Harald Alvestrand | 3d12066 | 2025-06-11 07:34:39 | [diff] [blame] | 122 | class SctpTransportInterface : public RefCountInterface { |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 123 | public: |
| 124 | // This function can be called from other threads. |
| Evan Shrubsole | ee5ab34 | 2025-04-15 14:49:46 | [diff] [blame] | 125 | virtual scoped_refptr<DtlsTransportInterface> dtls_transport() const = 0; |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 126 | // Returns information on the state of the SctpTransport. |
| 127 | // This function can be called from other threads. |
| 128 | virtual SctpTransportInformation Information() const = 0; |
| 129 | // Observer management. |
| 130 | virtual void RegisterObserver(SctpTransportObserverInterface* observer) = 0; |
| 131 | virtual void UnregisterObserver() = 0; |
| 132 | }; |
| 133 | |
| Philipp Hancke | c839686 | 2025-03-13 14:37:51 | [diff] [blame] | 134 | // The size of the SCTP association send buffer. 256kB, the usrsctp default. |
| 135 | constexpr int kSctpSendBufferSize = 256 * 1024; |
| 136 | |
| 137 | // SCTP options negotiated in the SDP. |
| 138 | struct SctpOptions { |
| 139 | // https://www.rfc-editor.org/rfc/rfc8841.html#name-sctp-port |
| 140 | // `local_port` and `remote_port` are passed along the wire and the |
| 141 | // listener and connector must be using the same port. They are not related |
| 142 | // to the ports at the IP level. If set to -1 we default to |
| 143 | // kSctpDefaultPort. |
| 144 | // TODO(bugs.webrtc.org/402429107): make these optional<uint16_t>. |
| 145 | int local_port = -1; |
| 146 | int remote_port = -1; |
| 147 | |
| 148 | // https://www.rfc-editor.org/rfc/rfc8841.html#name-max-message-size |
| 149 | // `max_message_size` sets the maxium message size on the connection. |
| 150 | // It must be smaller than or equal to kSctpSendBufferSize. |
| 151 | int max_message_size = kSctpSendBufferSize; |
| Philipp Hancke | 7fc962d | 2025-10-23 10:37:21 | [diff] [blame] | 152 | |
| 153 | // draft-hancke-tsvwg-snap |
| 154 | std::optional<std::vector<uint8_t>> local_init; |
| 155 | std::optional<std::vector<uint8_t>> remote_init; |
| Philipp Hancke | c839686 | 2025-03-13 14:37:51 | [diff] [blame] | 156 | }; |
| 157 | |
| Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 158 | } // namespace webrtc |
| 159 | |
| 160 | #endif // API_SCTP_TRANSPORT_INTERFACE_H_ |