blob: 4b76f3125b1d599480def9f0a8962d0637c27522 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef QUICHE_QUIC_CORE_QUIC_CONNECTION_ID_H_
6#define QUICHE_QUIC_CORE_QUIC_CONNECTION_ID_H_
7
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
dschinazib3241162019-06-10 17:59:37 -07009#include <vector>
vasilvv872e7a32019-03-12 16:42:44 -070010
QUICHE teama6ef0a62019-03-07 20:34:33 -050011#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
13
14namespace quic {
15
16enum QuicConnectionIdLength {
17 PACKET_0BYTE_CONNECTION_ID = 0,
18 PACKET_8BYTE_CONNECTION_ID = 8,
19};
20
21// This is a property of QUIC headers, it indicates whether the connection ID
22// should actually be sent over the wire (or was sent on received packets).
23enum QuicConnectionIdIncluded : uint8_t {
24 CONNECTION_ID_PRESENT = 1,
25 CONNECTION_ID_ABSENT = 2,
26};
27
28// Connection IDs can be 0-18 bytes per IETF specifications.
29const uint8_t kQuicMaxConnectionIdLength = 18;
30
31// kQuicDefaultConnectionIdLength is the only supported length for QUIC
32// versions < v99, and is the default picked for all versions.
33const uint8_t kQuicDefaultConnectionIdLength = 8;
34
QUICHE team963d57e2019-03-21 10:58:47 -070035// According to the IETF spec, the initial server connection ID generated by
36// the client must be at least this long.
37const uint8_t kQuicMinimumInitialConnectionIdLength = 8;
38
QUICHE teama6ef0a62019-03-07 20:34:33 -050039class QUIC_EXPORT_PRIVATE QuicConnectionId {
40 public:
41 // Creates a connection ID of length zero.
42 QuicConnectionId();
43
44 // Creates a connection ID from network order bytes.
45 QuicConnectionId(const char* data, uint8_t length);
46
dschinazib3241162019-06-10 17:59:37 -070047 // Creates a connection ID from another connection ID.
48 QuicConnectionId(const QuicConnectionId& other);
49
50 // Assignment operator.
51 QuicConnectionId& operator=(const QuicConnectionId& other);
52
QUICHE teama6ef0a62019-03-07 20:34:33 -050053 ~QuicConnectionId();
54
55 // Returns the length of the connection ID, in bytes.
56 uint8_t length() const;
57
58 // Sets the length of the connection ID, in bytes.
dschinazib3241162019-06-10 17:59:37 -070059 // WARNING: Calling set_length() can change the in-memory location of the
60 // connection ID. Callers must therefore ensure they call data() or
61 // mutable_data() after they call set_length().
QUICHE teama6ef0a62019-03-07 20:34:33 -050062 void set_length(uint8_t length);
63
64 // Returns a pointer to the connection ID bytes, in network byte order.
65 const char* data() const;
66
67 // Returns a mutable pointer to the connection ID bytes,
68 // in network byte order.
69 char* mutable_data();
70
71 // Returns whether the connection ID has length zero.
72 bool IsEmpty() const;
73
74 // Hash() is required to use connection IDs as keys in hash tables.
75 size_t Hash() const;
76
77 // Generates an ASCII string that represents
78 // the contents of the connection ID, or "0" if it is empty.
vasilvvc48c8712019-03-11 13:38:16 -070079 std::string ToString() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -050080
81 // operator<< allows easily logging connection IDs.
82 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
83 std::ostream& os,
84 const QuicConnectionId& v);
85
86 bool operator==(const QuicConnectionId& v) const;
87 bool operator!=(const QuicConnectionId& v) const;
88 // operator< is required to use connection IDs as keys in hash tables.
89 bool operator<(const QuicConnectionId& v) const;
90
91 private:
dschinazib3241162019-06-10 17:59:37 -070092 uint8_t length_; // length of the connection ID, in bytes.
93 // The connection ID is represented in network byte order.
94 union {
95 // When quic_use_allocated_connection_ids is false, the connection ID is
96 // stored in the first |length_| bytes of |data_|.
97 char data_[kQuicMaxConnectionIdLength];
98 // When quic_use_allocated_connection_ids is true, if the connection ID
99 // fits in |data_short_|, it is stored in the first |length_| bytes of
100 // |data_short_|. Otherwise it is stored in |data_long_| which is
101 // guaranteed to have a size equal to |length_|. A value of 11 was chosen
102 // because our commonly used connection ID length is 8 and with the length,
103 // the class is padded to 12 bytes anyway.
104 char data_short_[11];
105 char* data_long_;
106 };
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107};
108
109// Creates a connection ID of length zero, unless the restart flag
110// quic_connection_ids_network_byte_order is false in which case
111// it returns an 8-byte all-zeroes connection ID.
112QUIC_EXPORT_PRIVATE QuicConnectionId EmptyQuicConnectionId();
113
114// QuicConnectionIdHash can be passed as hash argument to hash tables.
115class QuicConnectionIdHash {
116 public:
117 size_t operator()(QuicConnectionId const& connection_id) const noexcept {
118 return connection_id.Hash();
119 }
120};
121
122} // namespace quic
123
124#endif // QUICHE_QUIC_CORE_QUIC_CONNECTION_ID_H_