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/error_cause/error_cause.h"
11
12 #include <stddef.h>
13
14 #include <cstdint>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19
20 #include "absl/types/optional.h"
21 #include "api/array_view.h"
22 #include "net/dcsctp/common/math.h"
23 #include "net/dcsctp/packet/error_cause/cookie_received_while_shutting_down_cause.h"
24 #include "net/dcsctp/packet/error_cause/invalid_mandatory_parameter_cause.h"
25 #include "net/dcsctp/packet/error_cause/invalid_stream_identifier_cause.h"
26 #include "net/dcsctp/packet/error_cause/missing_mandatory_parameter_cause.h"
27 #include "net/dcsctp/packet/error_cause/no_user_data_cause.h"
28 #include "net/dcsctp/packet/error_cause/out_of_resource_error_cause.h"
29 #include "net/dcsctp/packet/error_cause/protocol_violation_cause.h"
30 #include "net/dcsctp/packet/error_cause/restart_of_an_association_with_new_address_cause.h"
31 #include "net/dcsctp/packet/error_cause/stale_cookie_error_cause.h"
32 #include "net/dcsctp/packet/error_cause/unrecognized_chunk_type_cause.h"
33 #include "net/dcsctp/packet/error_cause/unrecognized_parameter_cause.h"
34 #include "net/dcsctp/packet/error_cause/unresolvable_address_cause.h"
35 #include "net/dcsctp/packet/error_cause/user_initiated_abort_cause.h"
36 #include "rtc_base/strings/string_builder.h"
37
38 namespace dcsctp {
39
40 template <class ErrorCause>
ParseAndPrint(ParameterDescriptor descriptor,rtc::StringBuilder & sb)41 bool ParseAndPrint(ParameterDescriptor descriptor, rtc::StringBuilder& sb) {
42 if (descriptor.type == ErrorCause::kType) {
43 absl::optional<ErrorCause> p = ErrorCause::Parse(descriptor.data);
44 if (p.has_value()) {
45 sb << p->ToString();
46 } else {
47 sb << "Failed to parse error cause of type " << ErrorCause::kType;
48 }
49 return true;
50 }
51 return false;
52 }
53
ErrorCausesToString(const Parameters & parameters)54 std::string ErrorCausesToString(const Parameters& parameters) {
55 rtc::StringBuilder sb;
56
57 std::vector<ParameterDescriptor> descriptors = parameters.descriptors();
58 for (size_t i = 0; i < descriptors.size(); ++i) {
59 if (i > 0) {
60 sb << "\n";
61 }
62
63 const ParameterDescriptor& d = descriptors[i];
64 if (!ParseAndPrint<InvalidStreamIdentifierCause>(d, sb) &&
65 !ParseAndPrint<MissingMandatoryParameterCause>(d, sb) &&
66 !ParseAndPrint<StaleCookieErrorCause>(d, sb) &&
67 !ParseAndPrint<OutOfResourceErrorCause>(d, sb) &&
68 !ParseAndPrint<UnresolvableAddressCause>(d, sb) &&
69 !ParseAndPrint<UnrecognizedChunkTypeCause>(d, sb) &&
70 !ParseAndPrint<InvalidMandatoryParameterCause>(d, sb) &&
71 !ParseAndPrint<UnrecognizedParametersCause>(d, sb) &&
72 !ParseAndPrint<NoUserDataCause>(d, sb) &&
73 !ParseAndPrint<CookieReceivedWhileShuttingDownCause>(d, sb) &&
74 !ParseAndPrint<RestartOfAnAssociationWithNewAddressesCause>(d, sb) &&
75 !ParseAndPrint<UserInitiatedAbortCause>(d, sb) &&
76 !ParseAndPrint<ProtocolViolationCause>(d, sb)) {
77 sb << "Unhandled parameter of type: " << d.type;
78 }
79 }
80
81 return sb.Release();
82 }
83 } // namespace dcsctp
84