wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef PC_SCTP_UTILS_H_ |
| 12 | #define PC_SCTP_UTILS_H_ |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 16 | #include "api/data_channel_interface.h" |
Niels Möller | bfcec4c | 2019-09-25 08:00:34 | [diff] [blame] | 17 | #include "api/transport/data_channel_transport_interface.h" |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 18 | #include "media/base/media_channel.h" |
Tommi | 4c84222 | 2023-03-21 10:35:24 | [diff] [blame] | 19 | #include "media/sctp/sctp_transport_internal.h" |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 20 | #include "net/dcsctp/public/types.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 21 | #include "rtc_base/copy_on_write_buffer.h" |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 22 | #include "rtc_base/ssl_stream_adapter.h" // For SSLRole |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 23 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 24 | namespace rtc { |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 25 | class CopyOnWriteBuffer; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 26 | } // namespace rtc |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | struct DataChannelInit; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 30 | |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 31 | // Wraps the `uint16_t` sctp data channel stream id value and does range |
| 32 | // checking. The class interface is `int` based to ease with DataChannelInit |
| 33 | // compatibility and types used in `DataChannelController`'s interface. Going |
| 34 | // forward, `int` compatibility won't be needed and we can either just use |
| 35 | // this class or the internal dcsctp::StreamID type. |
| 36 | class StreamId { |
| 37 | public: |
Tommi | 4c84222 | 2023-03-21 10:35:24 | [diff] [blame] | 38 | StreamId() = default; |
| 39 | explicit StreamId(int id) |
| 40 | : id_(id >= cricket::kMinSctpSid && id <= cricket::kSpecMaxSctpSid |
| 41 | ? absl::optional<uint16_t>(static_cast<uint16_t>(id)) |
| 42 | : absl::nullopt) {} |
| 43 | StreamId(const StreamId& sid) = default; |
| 44 | StreamId& operator=(const StreamId& sid) = default; |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 45 | |
| 46 | // Returns `true` if a valid stream id is contained, in the range of |
| 47 | // kMinSctpSid - kSpecMaxSctpSid ([0..0xffff]). Note that this |
| 48 | // is different than having `kMaxSctpSid` as the upper bound, which is |
| 49 | // the limit that is internally used by `SctpSidAllocator`. Sid values may |
| 50 | // be assigned to `StreamId` outside of `SctpSidAllocator` and have a higher |
| 51 | // id value than supplied by `SctpSidAllocator`, yet is still valid. |
Tommi | 4c84222 | 2023-03-21 10:35:24 | [diff] [blame] | 52 | bool HasValue() const { return id_.has_value(); } |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 53 | |
| 54 | // Provided for compatibility with existing code that hasn't been updated |
| 55 | // to use `StreamId` directly. New code should not use 'int' for the stream |
| 56 | // id but rather `StreamId` directly. |
Tommi | 4c84222 | 2023-03-21 10:35:24 | [diff] [blame] | 57 | int stream_id_int() const { |
| 58 | return id_.has_value() ? static_cast<int>(id_.value().value()) : -1; |
| 59 | } |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 60 | |
Tommi | 4c84222 | 2023-03-21 10:35:24 | [diff] [blame] | 61 | void reset() { id_ = absl::nullopt; } |
| 62 | |
| 63 | bool operator==(const StreamId& sid) const { return id_ == sid.id_; } |
| 64 | bool operator<(const StreamId& sid) const { return id_ < sid.id_; } |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 65 | bool operator!=(const StreamId& sid) const { return !(operator==(sid)); } |
| 66 | |
| 67 | private: |
Tommi | 4c84222 | 2023-03-21 10:35:24 | [diff] [blame] | 68 | absl::optional<dcsctp::StreamID> id_; |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 69 | }; |
| 70 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 71 | // Read the message type and return true if it's an OPEN message. |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 72 | bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 73 | |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 74 | bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload, |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 75 | std::string* label, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 76 | DataChannelInit* config); |
| 77 | |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 78 | bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 79 | |
| 80 | bool WriteDataChannelOpenMessage(const std::string& label, |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 81 | const std::string& protocol, |
| 82 | absl::optional<Priority> priority, |
| 83 | bool ordered, |
| 84 | absl::optional<int> max_retransmits, |
| 85 | absl::optional<int> max_retransmit_time, |
| 86 | rtc::CopyOnWriteBuffer* payload); |
| 87 | bool WriteDataChannelOpenMessage(const std::string& label, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 88 | const DataChannelInit& config, |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 89 | rtc::CopyOnWriteBuffer* payload); |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 90 | void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 91 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 92 | } // namespace webrtc |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 93 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 94 | #endif // PC_SCTP_UTILS_H_ |