blob: 868a8be826e8fcd2293726d7ef35589e59fb3c5b [file] [log] [blame]
wu@webrtc.org1d1ffc92013-10-16 18:12:021/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org1d1ffc92013-10-16 18:12:023 *
kjellanderb24317b2016-02-10 15:54:434 * 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.org1d1ffc92013-10-16 18:12:029 */
10
Steve Anton10542f22019-01-11 17:11:0011#ifndef PC_SCTP_UTILS_H_
12#define PC_SCTP_UTILS_H_
wu@webrtc.org1d1ffc92013-10-16 18:12:0213
14#include <string>
15
Steve Anton10542f22019-01-11 17:11:0016#include "api/data_channel_interface.h"
Niels Möllerbfcec4c2019-09-25 08:00:3417#include "api/transport/data_channel_transport_interface.h"
Bjorn A Mellembc3eebc2019-09-23 21:53:5418#include "media/base/media_channel.h"
Tommi4c842222023-03-21 10:35:2419#include "media/sctp/sctp_transport_internal.h"
Tommi492296c2023-03-12 15:59:2520#include "net/dcsctp/public/types.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0821#include "rtc_base/copy_on_write_buffer.h"
Tommi492296c2023-03-12 15:59:2522#include "rtc_base/ssl_stream_adapter.h" // For SSLRole
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5823
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5224namespace rtc {
jbaucheec21bd2016-03-20 13:15:4325class CopyOnWriteBuffer;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5226} // namespace rtc
wu@webrtc.org1d1ffc92013-10-16 18:12:0227
28namespace webrtc {
29struct DataChannelInit;
wu@webrtc.org1d1ffc92013-10-16 18:12:0230
Tommi492296c2023-03-12 15:59:2531// 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.
36class StreamId {
37 public:
Tommi4c842222023-03-21 10:35:2438 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;
Tommi492296c2023-03-12 15:59:2545
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.
Tommi4c842222023-03-21 10:35:2452 bool HasValue() const { return id_.has_value(); }
Tommi492296c2023-03-12 15:59:2553
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.
Tommi4c842222023-03-21 10:35:2457 int stream_id_int() const {
58 return id_.has_value() ? static_cast<int>(id_.value().value()) : -1;
59 }
Tommi492296c2023-03-12 15:59:2560
Tommi4c842222023-03-21 10:35:2461 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_; }
Tommi492296c2023-03-12 15:59:2565 bool operator!=(const StreamId& sid) const { return !(operator==(sid)); }
66
67 private:
Tommi4c842222023-03-21 10:35:2468 absl::optional<dcsctp::StreamID> id_;
Tommi492296c2023-03-12 15:59:2569};
70
deadbeefab9b2d12015-10-14 18:33:1171// Read the message type and return true if it's an OPEN message.
jbaucheec21bd2016-03-20 13:15:4372bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload);
deadbeefab9b2d12015-10-14 18:33:1173
jbaucheec21bd2016-03-20 13:15:4374bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org1d1ffc92013-10-16 18:12:0275 std::string* label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5876 DataChannelInit* config);
77
jbaucheec21bd2016-03-20 13:15:4378bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload);
wu@webrtc.org1d1ffc92013-10-16 18:12:0279
80bool WriteDataChannelOpenMessage(const std::string& label,
Tommi492296c2023-03-12 15:59:2581 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);
87bool WriteDataChannelOpenMessage(const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5888 const DataChannelInit& config,
jbaucheec21bd2016-03-20 13:15:4389 rtc::CopyOnWriteBuffer* payload);
jbaucheec21bd2016-03-20 13:15:4390void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload);
Bjorn A Mellembc3eebc2019-09-23 21:53:5491
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5892} // namespace webrtc
wu@webrtc.org1d1ffc92013-10-16 18:12:0293
Steve Anton10542f22019-01-11 17:11:0094#endif // PC_SCTP_UTILS_H_