summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/quiche/quic/load_balancer/load_balancer_server_id.h
blob: a3398a9220d006a6598e2b78b31b6a52e500f220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef QUICHE_QUIC_LOAD_BALANCER_LOAD_BALANCER_SERVER_ID_H_
#define QUICHE_QUIC_LOAD_BALANCER_LOAD_BALANCER_SERVER_ID_H_

#include <array>

#include "quiche/quic/core/quic_types.h"
#include "quiche/quic/platform/api/quic_export.h"

namespace quic {

// The maximum number of bytes in a LoadBalancerServerId.
inline constexpr uint8_t kLoadBalancerMaxServerIdLen = 15;

// LoadBalancerServerId is the globally understood identifier for a given pool
// member. It is unique to any given QUIC-LB configuration. See
// draft-ietf-quic-load-balancers-12.
// Note: this has nothing to do with QuicServerID. It's an unfortunate collision
// between an internal term for the destination identifiers for a particular
// deployment (QuicServerID) and the object of a load balancing decision
// (LoadBalancerServerId).
class QUIC_EXPORT_PRIVATE LoadBalancerServerId {
 public:
  // Copies all the bytes from |data| into a new LoadBalancerServerId.
  static absl::optional<LoadBalancerServerId> Create(
      const absl::Span<const uint8_t> data);

  // Server IDs are opaque bytes, but defining these operators allows us to sort
  // them into a tree and define ranges.
  bool operator<(const LoadBalancerServerId& other) const {
    return data() < other.data();
  }
  bool operator==(const LoadBalancerServerId& other) const {
    return data() == other.data();
  }

  // Hash function to allow use as a key in unordered maps.
  template <typename H>
  friend H AbslHashValue(H h, const LoadBalancerServerId& server_id) {
    return H::combine_contiguous(std::move(h), server_id.data().data(),
                                 server_id.length());
  }

  absl::Span<const uint8_t> data() const {
    return absl::MakeConstSpan(data_.data(), length_);
  }
  uint8_t length() const { return length_; }

  // Returns the server ID in hex format.
  std::string ToString() const;

 private:
  // The constructor is private because it can't validate the input.
  LoadBalancerServerId(const absl::Span<const uint8_t> data);

  std::array<uint8_t, kLoadBalancerMaxServerIdLen> data_;
  uint8_t length_;
};

}  // namespace quic

#endif  // QUICHE_QUIC_LOAD_BALANCER_LOAD_BALANCER_SERVER_ID_H_