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 | |
| 22 | QuicConnectionId::QuicConnectionId() : length_(0) {} |
| 23 | |
| 24 | QuicConnectionId::QuicConnectionId(const char* data, uint8_t length) { |
| 25 | if (length > kQuicMaxConnectionIdLength) { |
| 26 | QUIC_BUG << "Attempted to create connection ID of length " << length; |
| 27 | length = kQuicMaxConnectionIdLength; |
| 28 | } |
| 29 | length_ = length; |
| 30 | if (length_ > 0) { |
| 31 | memcpy(data_, data, length_); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | QuicConnectionId::~QuicConnectionId() {} |
| 36 | |
| 37 | const char* QuicConnectionId::data() const { |
| 38 | return data_; |
| 39 | } |
| 40 | |
| 41 | char* QuicConnectionId::mutable_data() { |
| 42 | return data_; |
| 43 | } |
| 44 | |
| 45 | uint8_t QuicConnectionId::length() const { |
| 46 | return length_; |
| 47 | } |
| 48 | |
| 49 | void QuicConnectionId::set_length(uint8_t length) { |
| 50 | length_ = length; |
| 51 | } |
| 52 | |
| 53 | bool QuicConnectionId::IsEmpty() const { |
| 54 | return length_ == 0; |
| 55 | } |
| 56 | |
| 57 | size_t QuicConnectionId::Hash() const { |
| 58 | uint64_t data_bytes[3] = {0, 0, 0}; |
| 59 | static_assert(sizeof(data_bytes) >= sizeof(data_), "sizeof(data_) changed"); |
| 60 | memcpy(data_bytes, data_, length_); |
| 61 | // This Hash function is designed to return the same value as the host byte |
| 62 | // order representation when the connection ID length is 64 bits. |
| 63 | return QuicEndian::NetToHost64(kQuicDefaultConnectionIdLength ^ length_ ^ |
| 64 | data_bytes[0] ^ data_bytes[1] ^ data_bytes[2]); |
| 65 | } |
| 66 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 67 | std::string QuicConnectionId::ToString() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 68 | if (IsEmpty()) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 69 | return std::string("0"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 70 | } |
| 71 | return QuicTextUtils::HexEncode(data_, length_); |
| 72 | } |
| 73 | |
| 74 | std::ostream& operator<<(std::ostream& os, const QuicConnectionId& v) { |
| 75 | os << v.ToString(); |
| 76 | return os; |
| 77 | } |
| 78 | |
| 79 | bool QuicConnectionId::operator==(const QuicConnectionId& v) const { |
| 80 | return length_ == v.length_ && memcmp(data_, v.data_, length_) == 0; |
| 81 | } |
| 82 | |
| 83 | bool QuicConnectionId::operator!=(const QuicConnectionId& v) const { |
| 84 | return !(v == *this); |
| 85 | } |
| 86 | |
| 87 | bool QuicConnectionId::operator<(const QuicConnectionId& v) const { |
| 88 | if (length_ < v.length_) { |
| 89 | return true; |
| 90 | } |
| 91 | if (length_ > v.length_) { |
| 92 | return false; |
| 93 | } |
| 94 | return memcmp(data_, v.data_, length_) < 0; |
| 95 | } |
| 96 | |
| 97 | QuicConnectionId EmptyQuicConnectionId() { |
| 98 | return QuicConnectionId(); |
| 99 | } |
| 100 | |
| 101 | static_assert(kQuicDefaultConnectionIdLength == sizeof(uint64_t), |
| 102 | "kQuicDefaultConnectionIdLength changed"); |
| 103 | static_assert(kQuicDefaultConnectionIdLength == PACKET_8BYTE_CONNECTION_ID, |
| 104 | "kQuicDefaultConnectionIdLength changed"); |
| 105 | |
| 106 | } // namespace quic |