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