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/chunk.h"
11
12 #include <cstdint>
13 #include <memory>
14 #include <utility>
15
16 #include "absl/types/optional.h"
17 #include "api/array_view.h"
18 #include "net/dcsctp/common/math.h"
19 #include "net/dcsctp/packet/chunk/abort_chunk.h"
20 #include "net/dcsctp/packet/chunk/cookie_ack_chunk.h"
21 #include "net/dcsctp/packet/chunk/cookie_echo_chunk.h"
22 #include "net/dcsctp/packet/chunk/data_chunk.h"
23 #include "net/dcsctp/packet/chunk/error_chunk.h"
24 #include "net/dcsctp/packet/chunk/forward_tsn_chunk.h"
25 #include "net/dcsctp/packet/chunk/heartbeat_ack_chunk.h"
26 #include "net/dcsctp/packet/chunk/heartbeat_request_chunk.h"
27 #include "net/dcsctp/packet/chunk/idata_chunk.h"
28 #include "net/dcsctp/packet/chunk/iforward_tsn_chunk.h"
29 #include "net/dcsctp/packet/chunk/init_ack_chunk.h"
30 #include "net/dcsctp/packet/chunk/init_chunk.h"
31 #include "net/dcsctp/packet/chunk/reconfig_chunk.h"
32 #include "net/dcsctp/packet/chunk/sack_chunk.h"
33 #include "net/dcsctp/packet/chunk/shutdown_ack_chunk.h"
34 #include "net/dcsctp/packet/chunk/shutdown_chunk.h"
35 #include "net/dcsctp/packet/chunk/shutdown_complete_chunk.h"
36 #include "net/dcsctp/packet/tlv_trait.h"
37
38 namespace dcsctp {
39
40 template <class Chunk>
ParseAndPrint(uint8_t chunk_type,rtc::ArrayView<const uint8_t> data,rtc::StringBuilder & sb)41 bool ParseAndPrint(uint8_t chunk_type,
42 rtc::ArrayView<const uint8_t> data,
43 rtc::StringBuilder& sb) {
44 if (chunk_type == Chunk::kType) {
45 absl::optional<Chunk> c = Chunk::Parse(data);
46 if (c.has_value()) {
47 sb << c->ToString();
48 } else {
49 sb << "Failed to parse chunk of type " << chunk_type;
50 }
51 return true;
52 }
53 return false;
54 }
55
DebugConvertChunkToString(rtc::ArrayView<const uint8_t> data)56 std::string DebugConvertChunkToString(rtc::ArrayView<const uint8_t> data) {
57 rtc::StringBuilder sb;
58
59 if (data.empty()) {
60 sb << "Failed to parse chunk due to empty data";
61 } else {
62 uint8_t chunk_type = data[0];
63 if (!ParseAndPrint<DataChunk>(chunk_type, data, sb) &&
64 !ParseAndPrint<InitChunk>(chunk_type, data, sb) &&
65 !ParseAndPrint<InitAckChunk>(chunk_type, data, sb) &&
66 !ParseAndPrint<SackChunk>(chunk_type, data, sb) &&
67 !ParseAndPrint<HeartbeatRequestChunk>(chunk_type, data, sb) &&
68 !ParseAndPrint<HeartbeatAckChunk>(chunk_type, data, sb) &&
69 !ParseAndPrint<AbortChunk>(chunk_type, data, sb) &&
70 !ParseAndPrint<ErrorChunk>(chunk_type, data, sb) &&
71 !ParseAndPrint<CookieEchoChunk>(chunk_type, data, sb) &&
72 !ParseAndPrint<CookieAckChunk>(chunk_type, data, sb) &&
73 !ParseAndPrint<ShutdownChunk>(chunk_type, data, sb) &&
74 !ParseAndPrint<ShutdownAckChunk>(chunk_type, data, sb) &&
75 !ParseAndPrint<ShutdownCompleteChunk>(chunk_type, data, sb) &&
76 !ParseAndPrint<ReConfigChunk>(chunk_type, data, sb) &&
77 !ParseAndPrint<ForwardTsnChunk>(chunk_type, data, sb) &&
78 !ParseAndPrint<IDataChunk>(chunk_type, data, sb) &&
79 !ParseAndPrint<IForwardTsnChunk>(chunk_type, data, sb)) {
80 sb << "Unhandled chunk type: " << static_cast<int>(chunk_type);
81 }
82 }
83 return sb.Release();
84 }
85 } // namespace dcsctp
86