blob: 4022283b55fc50fa0f5e50d8e6c0055227d3eb41 [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#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
6
7#include <cstdint>
8#include <cstring>
9#include <iomanip>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
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 teama6ef0a62019-03-07 20:34:33 -050018#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
19
20namespace quic {
21
22QuicConnectionId::QuicConnectionId() : length_(0) {}
23
24QuicConnectionId::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
35QuicConnectionId::~QuicConnectionId() {}
36
37const char* QuicConnectionId::data() const {
38 return data_;
39}
40
41char* QuicConnectionId::mutable_data() {
42 return data_;
43}
44
45uint8_t QuicConnectionId::length() const {
46 return length_;
47}
48
49void QuicConnectionId::set_length(uint8_t length) {
50 length_ = length;
51}
52
53bool QuicConnectionId::IsEmpty() const {
54 return length_ == 0;
55}
56
57size_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
vasilvvc48c8712019-03-11 13:38:16 -070067std::string QuicConnectionId::ToString() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -050068 if (IsEmpty()) {
vasilvvc48c8712019-03-11 13:38:16 -070069 return std::string("0");
QUICHE teama6ef0a62019-03-07 20:34:33 -050070 }
71 return QuicTextUtils::HexEncode(data_, length_);
72}
73
74std::ostream& operator<<(std::ostream& os, const QuicConnectionId& v) {
75 os << v.ToString();
76 return os;
77}
78
79bool QuicConnectionId::operator==(const QuicConnectionId& v) const {
80 return length_ == v.length_ && memcmp(data_, v.data_, length_) == 0;
81}
82
83bool QuicConnectionId::operator!=(const QuicConnectionId& v) const {
84 return !(v == *this);
85}
86
87bool 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
97QuicConnectionId EmptyQuicConnectionId() {
98 return QuicConnectionId();
99}
100
101static_assert(kQuicDefaultConnectionIdLength == sizeof(uint64_t),
102 "kQuicDefaultConnectionIdLength changed");
103static_assert(kQuicDefaultConnectionIdLength == PACKET_8BYTE_CONNECTION_ID,
104 "kQuicDefaultConnectionIdLength changed");
105
106} // namespace quic