QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
| 6 | |
| 7 | #include <cstdint> |
| 8 | #include <cstring> |
| 9 | #include <iomanip> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 10 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_endian.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 19 | |
| 20 | namespace quic { |
| 21 | |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 22 | QuicConnectionId::QuicConnectionId() : QuicConnectionId(nullptr, 0) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | |
| 24 | QuicConnectionId::QuicConnectionId(const char* data, uint8_t length) { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 25 | static_assert( |
| 26 | kQuicMaxConnectionIdLength <= std::numeric_limits<uint8_t>::max(), |
| 27 | "kQuicMaxConnectionIdLength too high"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | if (length > kQuicMaxConnectionIdLength) { |
| 29 | QUIC_BUG << "Attempted to create connection ID of length " << length; |
| 30 | length = kQuicMaxConnectionIdLength; |
| 31 | } |
| 32 | length_ = length; |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 33 | if (length_ == 0) { |
| 34 | return; |
| 35 | } |
| 36 | if (!GetQuicRestartFlag(quic_use_allocated_connection_ids)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 37 | memcpy(data_, data, length_); |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 38 | return; |
| 39 | } |
dschinazi | 6abd56b | 2019-06-18 18:04:30 -0700 | [diff] [blame] | 40 | QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 1, 6); |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 41 | if (length_ <= sizeof(data_short_)) { |
| 42 | memcpy(data_short_, data, length_); |
| 43 | return; |
| 44 | } |
| 45 | data_long_ = reinterpret_cast<char*>(malloc(length_)); |
| 46 | CHECK_NE(nullptr, data_long_); |
| 47 | memcpy(data_long_, data, length_); |
| 48 | } |
| 49 | |
| 50 | QuicConnectionId::~QuicConnectionId() { |
| 51 | if (!GetQuicRestartFlag(quic_use_allocated_connection_ids)) { |
| 52 | return; |
| 53 | } |
dschinazi | 6abd56b | 2019-06-18 18:04:30 -0700 | [diff] [blame] | 54 | QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 2, 6); |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 55 | if (length_ > sizeof(data_short_)) { |
| 56 | free(data_long_); |
| 57 | data_long_ = nullptr; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 61 | QuicConnectionId::QuicConnectionId(const QuicConnectionId& other) |
| 62 | : QuicConnectionId(other.data(), other.length()) {} |
| 63 | |
| 64 | QuicConnectionId& QuicConnectionId::operator=(const QuicConnectionId& other) { |
| 65 | set_length(other.length()); |
| 66 | memcpy(mutable_data(), other.data(), length_); |
| 67 | return *this; |
| 68 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 69 | |
| 70 | const char* QuicConnectionId::data() const { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 71 | if (!GetQuicRestartFlag(quic_use_allocated_connection_ids)) { |
| 72 | return data_; |
| 73 | } |
dschinazi | 6abd56b | 2019-06-18 18:04:30 -0700 | [diff] [blame] | 74 | QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 3, 6); |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 75 | if (length_ <= sizeof(data_short_)) { |
| 76 | return data_short_; |
| 77 | } |
| 78 | return data_long_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | char* QuicConnectionId::mutable_data() { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 82 | if (!GetQuicRestartFlag(quic_use_allocated_connection_ids)) { |
| 83 | return data_; |
| 84 | } |
dschinazi | 6abd56b | 2019-06-18 18:04:30 -0700 | [diff] [blame] | 85 | QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 4, 6); |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 86 | if (length_ <= sizeof(data_short_)) { |
| 87 | return data_short_; |
| 88 | } |
| 89 | return data_long_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | uint8_t QuicConnectionId::length() const { |
| 93 | return length_; |
| 94 | } |
| 95 | |
| 96 | void QuicConnectionId::set_length(uint8_t length) { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 97 | if (GetQuicRestartFlag(quic_use_allocated_connection_ids)) { |
dschinazi | 6abd56b | 2019-06-18 18:04:30 -0700 | [diff] [blame] | 98 | QUIC_RESTART_FLAG_COUNT_N(quic_use_allocated_connection_ids, 5, 6); |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 99 | char temporary_data[sizeof(data_short_)]; |
| 100 | if (length > sizeof(data_short_)) { |
| 101 | if (length_ <= sizeof(data_short_)) { |
| 102 | // Copy data from data_short_ to data_long_. |
| 103 | memcpy(temporary_data, data_short_, length_); |
| 104 | data_long_ = reinterpret_cast<char*>(malloc(length)); |
| 105 | CHECK_NE(nullptr, data_long_); |
| 106 | memcpy(data_long_, temporary_data, length_); |
| 107 | } else { |
| 108 | // Resize data_long_. |
| 109 | char* realloc_result = |
| 110 | reinterpret_cast<char*>(realloc(data_long_, length)); |
| 111 | CHECK_NE(nullptr, realloc_result); |
| 112 | data_long_ = realloc_result; |
| 113 | } |
| 114 | } else if (length_ > sizeof(data_short_)) { |
| 115 | // Copy data from data_long_ to data_short_. |
| 116 | memcpy(temporary_data, data_long_, length); |
| 117 | free(data_long_); |
| 118 | data_long_ = nullptr; |
| 119 | memcpy(data_short_, temporary_data, length); |
| 120 | } |
| 121 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 122 | length_ = length; |
| 123 | } |
| 124 | |
| 125 | bool QuicConnectionId::IsEmpty() const { |
| 126 | return length_ == 0; |
| 127 | } |
| 128 | |
| 129 | size_t QuicConnectionId::Hash() const { |
| 130 | uint64_t data_bytes[3] = {0, 0, 0}; |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 131 | static_assert(sizeof(data_bytes) >= kQuicMaxConnectionIdLength, |
| 132 | "kQuicMaxConnectionIdLength changed"); |
| 133 | memcpy(data_bytes, data(), length_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 134 | // This Hash function is designed to return the same value as the host byte |
| 135 | // order representation when the connection ID length is 64 bits. |
| 136 | return QuicEndian::NetToHost64(kQuicDefaultConnectionIdLength ^ length_ ^ |
| 137 | data_bytes[0] ^ data_bytes[1] ^ data_bytes[2]); |
| 138 | } |
| 139 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 140 | std::string QuicConnectionId::ToString() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 141 | if (IsEmpty()) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 142 | return std::string("0"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 143 | } |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 144 | return QuicTextUtils::HexEncode(data(), length_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | std::ostream& operator<<(std::ostream& os, const QuicConnectionId& v) { |
| 148 | os << v.ToString(); |
| 149 | return os; |
| 150 | } |
| 151 | |
| 152 | bool QuicConnectionId::operator==(const QuicConnectionId& v) const { |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 153 | return length_ == v.length_ && memcmp(data(), v.data(), length_) == 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool QuicConnectionId::operator!=(const QuicConnectionId& v) const { |
| 157 | return !(v == *this); |
| 158 | } |
| 159 | |
| 160 | bool QuicConnectionId::operator<(const QuicConnectionId& v) const { |
| 161 | if (length_ < v.length_) { |
| 162 | return true; |
| 163 | } |
| 164 | if (length_ > v.length_) { |
| 165 | return false; |
| 166 | } |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 167 | return memcmp(data(), v.data(), length_) < 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | QuicConnectionId EmptyQuicConnectionId() { |
| 171 | return QuicConnectionId(); |
| 172 | } |
| 173 | |
| 174 | static_assert(kQuicDefaultConnectionIdLength == sizeof(uint64_t), |
| 175 | "kQuicDefaultConnectionIdLength changed"); |
| 176 | static_assert(kQuicDefaultConnectionIdLength == PACKET_8BYTE_CONNECTION_ID, |
| 177 | "kQuicDefaultConnectionIdLength changed"); |
| 178 | |
| 179 | } // namespace quic |