xref: /aosp_15_r20/external/webrtc/net/dcsctp/socket/state_cookie.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_SOCKET_STATE_COOKIE_H_
11 #define NET_DCSCTP_SOCKET_STATE_COOKIE_H_
12 
13 #include <cstdint>
14 #include <vector>
15 
16 #include "absl/types/optional.h"
17 #include "api/array_view.h"
18 #include "net/dcsctp/common/internal_types.h"
19 #include "net/dcsctp/socket/capabilities.h"
20 
21 namespace dcsctp {
22 
23 // This is serialized as a state cookie and put in INIT_ACK. The client then
24 // responds with this in COOKIE_ECHO.
25 //
26 // NOTE: Expect that the client will modify it to try to exploit the library.
27 // Do not trust anything in it; no pointers or anything like that.
28 class StateCookie {
29  public:
30   static constexpr size_t kCookieSize = 36;
31 
StateCookie(VerificationTag initiate_tag,TSN initial_tsn,uint32_t a_rwnd,TieTag tie_tag,Capabilities capabilities)32   StateCookie(VerificationTag initiate_tag,
33               TSN initial_tsn,
34               uint32_t a_rwnd,
35               TieTag tie_tag,
36               Capabilities capabilities)
37       : initiate_tag_(initiate_tag),
38         initial_tsn_(initial_tsn),
39         a_rwnd_(a_rwnd),
40         tie_tag_(tie_tag),
41         capabilities_(capabilities) {}
42 
43   // Returns a serialized version of this cookie.
44   std::vector<uint8_t> Serialize();
45 
46   // Deserializes the cookie, and returns absl::nullopt if that failed.
47   static absl::optional<StateCookie> Deserialize(
48       rtc::ArrayView<const uint8_t> cookie);
49 
initiate_tag()50   VerificationTag initiate_tag() const { return initiate_tag_; }
initial_tsn()51   TSN initial_tsn() const { return initial_tsn_; }
a_rwnd()52   uint32_t a_rwnd() const { return a_rwnd_; }
tie_tag()53   TieTag tie_tag() const { return tie_tag_; }
capabilities()54   const Capabilities& capabilities() const { return capabilities_; }
55 
56  private:
57   const VerificationTag initiate_tag_;
58   const TSN initial_tsn_;
59   const uint32_t a_rwnd_;
60   const TieTag tie_tag_;
61   const Capabilities capabilities_;
62 };
63 }  // namespace dcsctp
64 
65 #endif  // NET_DCSCTP_SOCKET_STATE_COOKIE_H_
66