| 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 PC_SCTP_UTILS_H_ |
| 12 | #define PC_SCTP_UTILS_H_ |
| 13 |
| 14 | #include <string> |
| 15 |
| 16 | #include "api/data_channel_interface.h" |
| 17 | #include "api/transport/data_channel_transport_interface.h" |
| 18 | #include "media/base/media_channel.h" |
| 19 | #include "media/sctp/sctp_transport_internal.h" |
| 20 | #include "net/dcsctp/public/types.h" |
| 21 | #include "rtc_base/copy_on_write_buffer.h" |
| 22 | #include "rtc_base/ssl_stream_adapter.h" // For SSLRole |
| 23 |
| 24 | namespace rtc { |
| 25 | class CopyOnWriteBuffer; |
| 26 | } // namespace rtc |
| 27 |
| 28 | namespace webrtc { |
| 29 | struct DataChannelInit; |
| 30 |
| 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: |
| 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; |
| 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. |
| 52 | bool HasValue() const { return id_.has_value(); } |
| 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. |
| 57 | int stream_id_int() const { |
| 58 | return id_.has_value() ? static_cast<int>(id_.value().value()) : -1; |
| 59 | } |
| 60 |
| 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_; } |
| 65 | bool operator!=(const StreamId& sid) const { return !(operator==(sid)); } |
| 66 |
| 67 | private: |
| 68 | absl::optional<dcsctp::StreamID> id_; |
| 69 | }; |
| 70 |
| 71 | // Read the message type and return true if it's an OPEN message. |
| 72 | bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload); |
| 73 |
| 74 | bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload, |
| 75 | std::string* label, |
| 76 | DataChannelInit* config); |
| 77 |
| 78 | bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload); |
| 79 |
| 80 | bool WriteDataChannelOpenMessage(const std::string& label, |
| 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, |
| 88 | const DataChannelInit& config, |
| 89 | rtc::CopyOnWriteBuffer* payload); |
| 90 | void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload); |
| 91 |
| 92 | } // namespace webrtc |
| 93 |
| 94 | #endif // PC_SCTP_UTILS_H_ |
| 95 |