blob: a26dbf86f72eb6e521600befb9fe3ab6501b7de9 [file] [log] [blame]
Victor Boivieb6580cc2021-04-08 09:56:59 +02001/*
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
21namespace 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.
28class StateCookie {
29 public:
Victor Boiviee0b45c22022-08-18 21:12:31 +000030 static constexpr size_t kCookieSize = 36;
Victor Boivieb6580cc2021-04-08 09:56:59 +020031
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
50 VerificationTag initiate_tag() const { return initiate_tag_; }
51 TSN initial_tsn() const { return initial_tsn_; }
52 uint32_t a_rwnd() const { return a_rwnd_; }
53 TieTag tie_tag() const { return tie_tag_; }
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_