xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/chunk/init_ack_chunk.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #include "net/dcsctp/packet/chunk/init_ack_chunk.h"
11 
12 #include <stdint.h>
13 
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 #include "absl/types/optional.h"
19 #include "api/array_view.h"
20 #include "net/dcsctp/packet/bounded_byte_reader.h"
21 #include "net/dcsctp/packet/bounded_byte_writer.h"
22 #include "net/dcsctp/packet/parameter/parameter.h"
23 #include "net/dcsctp/packet/tlv_trait.h"
24 #include "rtc_base/strings/string_format.h"
25 
26 namespace dcsctp {
27 
28 // https://tools.ietf.org/html/rfc4960#section-3.3.3
29 
30 //   0                   1                   2                   3
31 //   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
32 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 //  |   Type = 2    |  Chunk Flags  |      Chunk Length             |
34 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 //  |                         Initiate Tag                          |
36 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 //  |              Advertised Receiver Window Credit                |
38 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 //  |  Number of Outbound Streams   |  Number of Inbound Streams    |
40 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 //  |                          Initial TSN                          |
42 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 //  \                                                               \
44 //  /              Optional/Variable-Length Parameters              /
45 //  \                                                               \
46 //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 constexpr int InitAckChunk::kType;
48 
Parse(rtc::ArrayView<const uint8_t> data)49 absl::optional<InitAckChunk> InitAckChunk::Parse(
50     rtc::ArrayView<const uint8_t> data) {
51   absl::optional<BoundedByteReader<kHeaderSize>> reader = ParseTLV(data);
52   if (!reader.has_value()) {
53     return absl::nullopt;
54   }
55 
56   VerificationTag initiate_tag(reader->Load32<4>());
57   uint32_t a_rwnd = reader->Load32<8>();
58   uint16_t nbr_outbound_streams = reader->Load16<12>();
59   uint16_t nbr_inbound_streams = reader->Load16<14>();
60   TSN initial_tsn(reader->Load32<16>());
61   absl::optional<Parameters> parameters =
62       Parameters::Parse(reader->variable_data());
63   if (!parameters.has_value()) {
64     return absl::nullopt;
65   }
66   return InitAckChunk(initiate_tag, a_rwnd, nbr_outbound_streams,
67                       nbr_inbound_streams, initial_tsn, *std::move(parameters));
68 }
69 
SerializeTo(std::vector<uint8_t> & out) const70 void InitAckChunk::SerializeTo(std::vector<uint8_t>& out) const {
71   rtc::ArrayView<const uint8_t> parameters = parameters_.data();
72   BoundedByteWriter<kHeaderSize> writer = AllocateTLV(out, parameters.size());
73 
74   writer.Store32<4>(*initiate_tag_);
75   writer.Store32<8>(a_rwnd_);
76   writer.Store16<12>(nbr_outbound_streams_);
77   writer.Store16<14>(nbr_inbound_streams_);
78   writer.Store32<16>(*initial_tsn_);
79   writer.CopyToVariableData(parameters);
80 }
81 
ToString() const82 std::string InitAckChunk::ToString() const {
83   return rtc::StringFormat("INIT_ACK, initiate_tag=0x%0x, initial_tsn=%u",
84                            *initiate_tag(), *initial_tsn());
85 }
86 }  // namespace dcsctp
87