1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker #ifndef NET_DCSCTP_PACKET_SCTP_PACKET_H_ 11*d9f75844SAndroid Build Coastguard Worker #define NET_DCSCTP_PACKET_SCTP_PACKET_H_ 12*d9f75844SAndroid Build Coastguard Worker 13*d9f75844SAndroid Build Coastguard Worker #include <stddef.h> 14*d9f75844SAndroid Build Coastguard Worker 15*d9f75844SAndroid Build Coastguard Worker #include <cstdint> 16*d9f75844SAndroid Build Coastguard Worker #include <functional> 17*d9f75844SAndroid Build Coastguard Worker #include <memory> 18*d9f75844SAndroid Build Coastguard Worker #include <utility> 19*d9f75844SAndroid Build Coastguard Worker #include <vector> 20*d9f75844SAndroid Build Coastguard Worker 21*d9f75844SAndroid Build Coastguard Worker #include "api/array_view.h" 22*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/common/internal_types.h" 23*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/packet/chunk/chunk.h" 24*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/public/dcsctp_options.h" 25*d9f75844SAndroid Build Coastguard Worker 26*d9f75844SAndroid Build Coastguard Worker namespace dcsctp { 27*d9f75844SAndroid Build Coastguard Worker 28*d9f75844SAndroid Build Coastguard Worker // The "Common Header", which every SCTP packet starts with, and is described in 29*d9f75844SAndroid Build Coastguard Worker // https://tools.ietf.org/html/rfc4960#section-3.1. 30*d9f75844SAndroid Build Coastguard Worker struct CommonHeader { 31*d9f75844SAndroid Build Coastguard Worker uint16_t source_port; 32*d9f75844SAndroid Build Coastguard Worker uint16_t destination_port; 33*d9f75844SAndroid Build Coastguard Worker VerificationTag verification_tag; 34*d9f75844SAndroid Build Coastguard Worker uint32_t checksum; 35*d9f75844SAndroid Build Coastguard Worker }; 36*d9f75844SAndroid Build Coastguard Worker 37*d9f75844SAndroid Build Coastguard Worker // Represents an immutable (received or to-be-sent) SCTP packet. 38*d9f75844SAndroid Build Coastguard Worker class SctpPacket { 39*d9f75844SAndroid Build Coastguard Worker public: 40*d9f75844SAndroid Build Coastguard Worker static constexpr size_t kHeaderSize = 12; 41*d9f75844SAndroid Build Coastguard Worker 42*d9f75844SAndroid Build Coastguard Worker struct ChunkDescriptor { ChunkDescriptorChunkDescriptor43*d9f75844SAndroid Build Coastguard Worker ChunkDescriptor(uint8_t type, 44*d9f75844SAndroid Build Coastguard Worker uint8_t flags, 45*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const uint8_t> data) 46*d9f75844SAndroid Build Coastguard Worker : type(type), flags(flags), data(data) {} 47*d9f75844SAndroid Build Coastguard Worker uint8_t type; 48*d9f75844SAndroid Build Coastguard Worker uint8_t flags; 49*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const uint8_t> data; 50*d9f75844SAndroid Build Coastguard Worker }; 51*d9f75844SAndroid Build Coastguard Worker 52*d9f75844SAndroid Build Coastguard Worker SctpPacket(SctpPacket&& other) = default; 53*d9f75844SAndroid Build Coastguard Worker SctpPacket& operator=(SctpPacket&& other) = default; 54*d9f75844SAndroid Build Coastguard Worker SctpPacket(const SctpPacket&) = delete; 55*d9f75844SAndroid Build Coastguard Worker SctpPacket& operator=(const SctpPacket&) = delete; 56*d9f75844SAndroid Build Coastguard Worker 57*d9f75844SAndroid Build Coastguard Worker // Used for building SctpPacket, as those are immutable. 58*d9f75844SAndroid Build Coastguard Worker class Builder { 59*d9f75844SAndroid Build Coastguard Worker public: 60*d9f75844SAndroid Build Coastguard Worker Builder(VerificationTag verification_tag, const DcSctpOptions& options); 61*d9f75844SAndroid Build Coastguard Worker 62*d9f75844SAndroid Build Coastguard Worker Builder(Builder&& other) = default; 63*d9f75844SAndroid Build Coastguard Worker Builder& operator=(Builder&& other) = default; 64*d9f75844SAndroid Build Coastguard Worker 65*d9f75844SAndroid Build Coastguard Worker // Adds a chunk to the to-be-built SCTP packet. 66*d9f75844SAndroid Build Coastguard Worker Builder& Add(const Chunk& chunk); 67*d9f75844SAndroid Build Coastguard Worker 68*d9f75844SAndroid Build Coastguard Worker // The number of bytes remaining in the packet for chunk storage until the 69*d9f75844SAndroid Build Coastguard Worker // packet reaches its maximum size. 70*d9f75844SAndroid Build Coastguard Worker size_t bytes_remaining() const; 71*d9f75844SAndroid Build Coastguard Worker 72*d9f75844SAndroid Build Coastguard Worker // Indicates if any packets have been added to the builder. empty()73*d9f75844SAndroid Build Coastguard Worker bool empty() const { return out_.empty(); } 74*d9f75844SAndroid Build Coastguard Worker 75*d9f75844SAndroid Build Coastguard Worker // Returns the payload of the build SCTP packet. The Builder will be cleared 76*d9f75844SAndroid Build Coastguard Worker // after having called this function, and can be used to build a new packet. 77*d9f75844SAndroid Build Coastguard Worker std::vector<uint8_t> Build(); 78*d9f75844SAndroid Build Coastguard Worker 79*d9f75844SAndroid Build Coastguard Worker private: 80*d9f75844SAndroid Build Coastguard Worker VerificationTag verification_tag_; 81*d9f75844SAndroid Build Coastguard Worker uint16_t source_port_; 82*d9f75844SAndroid Build Coastguard Worker uint16_t dest_port_; 83*d9f75844SAndroid Build Coastguard Worker // The maximum packet size is always even divisible by four, as chunks are 84*d9f75844SAndroid Build Coastguard Worker // always padded to a size even divisible by four. 85*d9f75844SAndroid Build Coastguard Worker size_t max_packet_size_; 86*d9f75844SAndroid Build Coastguard Worker std::vector<uint8_t> out_; 87*d9f75844SAndroid Build Coastguard Worker }; 88*d9f75844SAndroid Build Coastguard Worker 89*d9f75844SAndroid Build Coastguard Worker // Parses `data` as an SCTP packet and returns it if it validates. 90*d9f75844SAndroid Build Coastguard Worker static absl::optional<SctpPacket> Parse( 91*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const uint8_t> data, 92*d9f75844SAndroid Build Coastguard Worker bool disable_checksum_verification = false); 93*d9f75844SAndroid Build Coastguard Worker 94*d9f75844SAndroid Build Coastguard Worker // Returns the SCTP common header. common_header()95*d9f75844SAndroid Build Coastguard Worker const CommonHeader& common_header() const { return common_header_; } 96*d9f75844SAndroid Build Coastguard Worker 97*d9f75844SAndroid Build Coastguard Worker // Returns the chunks (types and offsets) within the packet. descriptors()98*d9f75844SAndroid Build Coastguard Worker rtc::ArrayView<const ChunkDescriptor> descriptors() const { 99*d9f75844SAndroid Build Coastguard Worker return descriptors_; 100*d9f75844SAndroid Build Coastguard Worker } 101*d9f75844SAndroid Build Coastguard Worker 102*d9f75844SAndroid Build Coastguard Worker private: SctpPacket(const CommonHeader & common_header,std::vector<uint8_t> data,std::vector<ChunkDescriptor> descriptors)103*d9f75844SAndroid Build Coastguard Worker SctpPacket(const CommonHeader& common_header, 104*d9f75844SAndroid Build Coastguard Worker std::vector<uint8_t> data, 105*d9f75844SAndroid Build Coastguard Worker std::vector<ChunkDescriptor> descriptors) 106*d9f75844SAndroid Build Coastguard Worker : common_header_(common_header), 107*d9f75844SAndroid Build Coastguard Worker data_(std::move(data)), 108*d9f75844SAndroid Build Coastguard Worker descriptors_(std::move(descriptors)) {} 109*d9f75844SAndroid Build Coastguard Worker 110*d9f75844SAndroid Build Coastguard Worker CommonHeader common_header_; 111*d9f75844SAndroid Build Coastguard Worker 112*d9f75844SAndroid Build Coastguard Worker // As the `descriptors_` refer to offset within data, and since SctpPacket is 113*d9f75844SAndroid Build Coastguard Worker // movable, `data` needs to be pointer stable, which it is according to 114*d9f75844SAndroid Build Coastguard Worker // http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#2321 115*d9f75844SAndroid Build Coastguard Worker std::vector<uint8_t> data_; 116*d9f75844SAndroid Build Coastguard Worker // The chunks and their offsets within `data_ `. 117*d9f75844SAndroid Build Coastguard Worker std::vector<ChunkDescriptor> descriptors_; 118*d9f75844SAndroid Build Coastguard Worker }; 119*d9f75844SAndroid Build Coastguard Worker } // namespace dcsctp 120*d9f75844SAndroid Build Coastguard Worker 121*d9f75844SAndroid Build Coastguard Worker #endif // NET_DCSCTP_PACKET_SCTP_PACKET_H_ 122