Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: 39e0c4b736120277727aa8bd4bdfb92349c518c3 [file] [log] [blame]
Harald Alvestrandc85328f2019-02-28 06:51:001/*
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
Tommi8955cd72025-10-10 19:11:2514#include <cstdint>
Florent Castelli8037fc62024-08-29 13:00:4015#include <optional>
Philipp Hancke7fc962d2025-10-23 10:37:2116#include <vector>
Florent Castelli8037fc62024-08-29 13:00:4017
Harald Alvestrandc85328f2019-02-28 06:51:0018#include "api/dtls_transport_interface.h"
Harald Alvestrande8a2b3c2023-10-31 13:30:3019#include "api/ref_count.h"
Harald Alvestrandc85328f2019-02-28 06:51:0020#include "api/scoped_refptr.h"
Dor Henaefed552024-06-18 13:20:3521#include "rtc_base/system/rtc_export.h"
Harald Alvestrandc85328f2019-02-28 06:51:0022
23namespace webrtc {
24
Tommi8955cd72025-10-10 19:11:2525// 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.
37constexpr uint16_t kMaxSctpStreams = 1024;
38constexpr uint16_t kMaxSctpSid = kMaxSctpStreams - 1;
39constexpr uint16_t kMinSctpSid = 0;
40// The maximum number of streams that can be negotiated according to spec.
41constexpr 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)
47const int kSctpDefaultPort = 5000;
48
49// Error cause codes defined at
50// https://www.iana.org/assignments/sctp-parameters/sctp-parameters.xhtml#sctp-parameters-24
51enum 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 Alvestrandc85328f2019-02-28 06:51:0067// States of a SCTP transport, corresponding to the JS API specification.
68// http://w3c.github.io/webrtc-pc/#dom-rtcsctptransportstate
69enum 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 Bonadei66e76792019-04-02 09:33:5981class RTC_EXPORT SctpTransportInformation {
Harald Alvestrandc85328f2019-02-28 06:51:0082 public:
Tomas Gunnarsson92eebef2021-02-10 12:05:4483 SctpTransportInformation() = default;
84 SctpTransportInformation(const SctpTransportInformation&) = default;
Harald Alvestrandc85328f2019-02-28 06:51:0085 explicit SctpTransportInformation(SctpTransportState state);
Evan Shrubsoleee5ab342025-04-15 14:49:4686 SctpTransportInformation(SctpTransportState state,
87 scoped_refptr<DtlsTransportInterface> dtls_transport,
88 std::optional<double> max_message_size,
89 std::optional<int> max_channels);
Harald Alvestrandc85328f2019-02-28 06:51:0090 ~SctpTransportInformation();
91 // The DTLS transport that supports this SCTP transport.
Evan Shrubsoleee5ab342025-04-15 14:49:4692 scoped_refptr<DtlsTransportInterface> dtls_transport() const {
Harald Alvestrandc85328f2019-02-28 06:51:0093 return dtls_transport_;
94 }
95 SctpTransportState state() const { return state_; }
Florent Castelli8037fc62024-08-29 13:00:4096 std::optional<double> MaxMessageSize() const { return max_message_size_; }
97 std::optional<int> MaxChannels() const { return max_channels_; }
Harald Alvestrandc85328f2019-02-28 06:51:0098
99 private:
Bjorn Tereliusb41f07b2024-02-29 15:22:01100 SctpTransportState state_ = SctpTransportState::kNew;
Evan Shrubsoleee5ab342025-04-15 14:49:46101 scoped_refptr<DtlsTransportInterface> dtls_transport_;
Florent Castelli8037fc62024-08-29 13:00:40102 std::optional<double> max_message_size_;
103 std::optional<int> max_channels_;
Harald Alvestrandc85328f2019-02-28 06:51:00104};
105
106class 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 Alvestrand3d120662025-06-11 07:34:39122class SctpTransportInterface : public RefCountInterface {
Harald Alvestrandc85328f2019-02-28 06:51:00123 public:
124 // This function can be called from other threads.
Evan Shrubsoleee5ab342025-04-15 14:49:46125 virtual scoped_refptr<DtlsTransportInterface> dtls_transport() const = 0;
Harald Alvestrandc85328f2019-02-28 06:51:00126 // 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 Hanckec8396862025-03-13 14:37:51134// The size of the SCTP association send buffer. 256kB, the usrsctp default.
135constexpr int kSctpSendBufferSize = 256 * 1024;
136
137// SCTP options negotiated in the SDP.
138struct 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 Hancke7fc962d2025-10-23 10:37:21152
153 // draft-hancke-tsvwg-snap
154 std::optional<std::vector<uint8_t>> local_init;
155 std::optional<std::vector<uint8_t>> remote_init;
Philipp Hanckec8396862025-03-13 14:37:51156};
157
Harald Alvestrandc85328f2019-02-28 06:51:00158} // namespace webrtc
159
160#endif // API_SCTP_TRANSPORT_INTERFACE_H_