blob: 431cc741f609bafaa681e72f9957654bb7bb683a [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
dschinazib953d022019-08-01 18:05:58 -070028// Maximum connection ID length that we support in any packet or version.
dschinazib012d212019-08-01 18:07:26 -070029const uint8_t kQuicMaxConnectionIdAllVersionsLength = 20;
30
31// Maximum connection ID length supported by versions that use the encoding from
32// draft-ietf-quic-invariants-06.
33const uint8_t kQuicMaxConnectionIdWithLengthPrefixLength = 20;
34
35// Maximum connection ID length supported by versions that use the encoding from
36// draft-ietf-quic-invariants-05.
37const uint8_t kQuicMaxConnectionId4BitLength = 18;
QUICHE teama6ef0a62019-03-07 20:34:33 -050038
39// kQuicDefaultConnectionIdLength is the only supported length for QUIC
40// versions < v99, and is the default picked for all versions.
41const uint8_t kQuicDefaultConnectionIdLength = 8;
42
QUICHE team963d57e2019-03-21 10:58:47 -070043// According to the IETF spec, the initial server connection ID generated by
44// the client must be at least this long.
45const uint8_t kQuicMinimumInitialConnectionIdLength = 8;
46
QUICHE teama6ef0a62019-03-07 20:34:33 -050047class QUIC_EXPORT_PRIVATE QuicConnectionId {
48 public:
49 // Creates a connection ID of length zero.
50 QuicConnectionId();
51
52 // Creates a connection ID from network order bytes.
53 QuicConnectionId(const char* data, uint8_t length);
54
dschinazib3241162019-06-10 17:59:37 -070055 // Creates a connection ID from another connection ID.
56 QuicConnectionId(const QuicConnectionId& other);
57
58 // Assignment operator.
59 QuicConnectionId& operator=(const QuicConnectionId& other);
60
QUICHE teama6ef0a62019-03-07 20:34:33 -050061 ~QuicConnectionId();
62
63 // Returns the length of the connection ID, in bytes.
64 uint8_t length() const;
65
66 // Sets the length of the connection ID, in bytes.
dschinazib3241162019-06-10 17:59:37 -070067 // WARNING: Calling set_length() can change the in-memory location of the
68 // connection ID. Callers must therefore ensure they call data() or
69 // mutable_data() after they call set_length().
QUICHE teama6ef0a62019-03-07 20:34:33 -050070 void set_length(uint8_t length);
71
72 // Returns a pointer to the connection ID bytes, in network byte order.
73 const char* data() const;
74
75 // Returns a mutable pointer to the connection ID bytes,
76 // in network byte order.
77 char* mutable_data();
78
79 // Returns whether the connection ID has length zero.
80 bool IsEmpty() const;
81
82 // Hash() is required to use connection IDs as keys in hash tables.
dschinazi0fdff8e2019-07-18 09:07:39 -070083 // During the lifetime of a process, the output of Hash() is guaranteed to be
84 // the same for connection IDs that are equal to one another. Note however
85 // that this property is not guaranteed across process lifetimes. This makes
86 // Hash() suitable for data structures such as hash tables but not for sending
87 // a hash over the network.
QUICHE teama6ef0a62019-03-07 20:34:33 -050088 size_t Hash() const;
89
90 // Generates an ASCII string that represents
91 // the contents of the connection ID, or "0" if it is empty.
vasilvvc48c8712019-03-11 13:38:16 -070092 std::string ToString() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -050093
94 // operator<< allows easily logging connection IDs.
95 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(
96 std::ostream& os,
97 const QuicConnectionId& v);
98
99 bool operator==(const QuicConnectionId& v) const;
100 bool operator!=(const QuicConnectionId& v) const;
101 // operator< is required to use connection IDs as keys in hash tables.
102 bool operator<(const QuicConnectionId& v) const;
103
104 private:
dschinazib3241162019-06-10 17:59:37 -0700105 uint8_t length_; // length of the connection ID, in bytes.
106 // The connection ID is represented in network byte order.
107 union {
dschinazi0d06d7b2019-08-19 14:33:07 -0700108 // If the connection ID fits in |data_short_|, it is stored in the
109 // first |length_| bytes of |data_short_|.
110 // Otherwise it is stored in |data_long_| which is guaranteed to have a size
111 // equal to |length_|.
112 // A value of 11 was chosen because our commonly used connection ID length
113 // is 8 and with the length, the class is padded to 12 bytes anyway.
dschinazib3241162019-06-10 17:59:37 -0700114 char data_short_[11];
115 char* data_long_;
116 };
QUICHE teama6ef0a62019-03-07 20:34:33 -0500117};
118
119// Creates a connection ID of length zero, unless the restart flag
120// quic_connection_ids_network_byte_order is false in which case
121// it returns an 8-byte all-zeroes connection ID.
122QUIC_EXPORT_PRIVATE QuicConnectionId EmptyQuicConnectionId();
123
124// QuicConnectionIdHash can be passed as hash argument to hash tables.
dschinazi0fdff8e2019-07-18 09:07:39 -0700125// During the lifetime of a process, the output of QuicConnectionIdHash is
126// guaranteed to be the same for connection IDs that are equal to one another.
127// Note however that this property is not guaranteed across process lifetimes.
128// This makes QuicConnectionIdHash suitable for data structures such as hash
129// tables but not for sending a hash over the network.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500130class QuicConnectionIdHash {
131 public:
132 size_t operator()(QuicConnectionId const& connection_id) const noexcept {
133 return connection_id.Hash();
134 }
135};
136
137} // namespace quic
138
139#endif // QUICHE_QUIC_CORE_QUIC_CONNECTION_ID_H_