xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/qbone/platform/ip_range.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2019 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_QBONE_PLATFORM_IP_RANGE_H_
6 #define QUICHE_QUIC_QBONE_PLATFORM_IP_RANGE_H_
7 
8 #include "absl/strings/str_cat.h"
9 #include "quiche/quic/platform/api/quic_ip_address.h"
10 #include "quiche/quic/platform/api/quic_ip_address_family.h"
11 
12 namespace quic {
13 
14 class IpRange {
15  public:
16   // Default constructor to have an uninitialized IpRange.
IpRange()17   IpRange() : prefix_length_(0) {}
18 
19   // prefix will be automatically truncated to prefix_length, so that any bit
20   // after prefix_length are zero.
21   IpRange(const QuicIpAddress& prefix, size_t prefix_length);
22 
23   bool operator==(IpRange other) const;
24   bool operator!=(IpRange other) const;
25 
26   // Parses range that looks like "10.0.0.1/8". Tailing bits will be set to zero
27   // after prefix_length. Return false if the parsing failed.
28   bool FromString(const std::string& range);
29 
30   // Returns the string representation of this object.
ToString()31   std::string ToString() const {
32     if (IsInitialized()) {
33       return absl::StrCat(prefix_.ToString(), "/", prefix_length_);
34     }
35     return "(uninitialized)";
36   }
37 
38   // Whether this object is initialized.
IsInitialized()39   bool IsInitialized() const { return prefix_.IsInitialized(); }
40 
41   // Returns the first available IP address in this IpRange. The resulting
42   // address will be uninitialized if there is no available address.
43   QuicIpAddress FirstAddressInRange() const;
44 
45   // The address family of this IpRange.
address_family()46   IpAddressFamily address_family() const { return prefix_.address_family(); }
47 
48   // The subnet's prefix address.
prefix()49   QuicIpAddress prefix() const { return prefix_; }
50 
51   // The subnet's prefix length.
prefix_length()52   size_t prefix_length() const { return prefix_length_; }
53 
54  private:
55   QuicIpAddress prefix_;
56   size_t prefix_length_;
57 };
58 
59 }  // namespace quic
60 
61 #endif  // QUICHE_QUIC_QBONE_PLATFORM_IP_RANGE_H_
62