xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/error_cause/stale_cookie_error_cause.h (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 #ifndef NET_DCSCTP_PACKET_ERROR_CAUSE_STALE_COOKIE_ERROR_CAUSE_H_
11 #define NET_DCSCTP_PACKET_ERROR_CAUSE_STALE_COOKIE_ERROR_CAUSE_H_
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 #include <string>
16 #include <vector>
17 
18 #include "absl/strings/string_view.h"
19 #include "api/array_view.h"
20 #include "net/dcsctp/packet/error_cause/error_cause.h"
21 #include "net/dcsctp/packet/tlv_trait.h"
22 
23 namespace dcsctp {
24 
25 // https://tools.ietf.org/html/rfc4960#section-3.3.10.3
26 struct StaleCookieParameterConfig : public ParameterConfig {
27   static constexpr int kType = 3;
28   static constexpr size_t kHeaderSize = 8;
29   static constexpr size_t kVariableLengthAlignment = 0;
30 };
31 
32 class StaleCookieErrorCause : public Parameter,
33                               public TLVTrait<StaleCookieParameterConfig> {
34  public:
35   static constexpr int kType = StaleCookieParameterConfig::kType;
36 
StaleCookieErrorCause(uint32_t staleness_us)37   explicit StaleCookieErrorCause(uint32_t staleness_us)
38       : staleness_us_(staleness_us) {}
39 
40   static absl::optional<StaleCookieErrorCause> Parse(
41       rtc::ArrayView<const uint8_t> data);
42 
43   void SerializeTo(std::vector<uint8_t>& out) const override;
44   std::string ToString() const override;
45 
staleness_us()46   uint16_t staleness_us() const { return staleness_us_; }
47 
48  private:
49   uint32_t staleness_us_;
50 };
51 
52 }  // namespace dcsctp
53 
54 #endif  // NET_DCSCTP_PACKET_ERROR_CAUSE_STALE_COOKIE_ERROR_CAUSE_H_
55