Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS 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 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_SUBNET_H_ |
| 6 | #define PATCHPANEL_SUBNET_H_ |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 10 | #include <memory> |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 11 | #include <string> |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 14 | #include <base/callback.h> |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 16 | #include <brillo/brillo_export.h> |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 17 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 18 | namespace patchpanel { |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 19 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 20 | // Represents an allocated address inside an IPv4 subnet. The address is freed |
| 21 | // when this object is destroyed. |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 22 | class BRILLO_EXPORT SubnetAddress { |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 23 | public: |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 24 | // Creates a new SubnetAddress with the given base address and prefix length. |
| 25 | // |base_addr| must be in network-byte order. |release_cb| runs in the |
| 26 | // destructor of this class and can be used to free other resources associated |
| 27 | // with the subnet address. |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 28 | SubnetAddress(uint32_t addr, |
| 29 | uint32_t prefix_length, |
Anand K Mistry | e1aee76 | 2021-10-11 12:28:56 +1100 | [diff] [blame] | 30 | base::OnceClosure release_cb); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 31 | SubnetAddress(const SubnetAddress&) = delete; |
| 32 | SubnetAddress& operator=(const SubnetAddress&) = delete; |
| 33 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 34 | ~SubnetAddress(); |
| 35 | |
| 36 | // Returns this address in network-byte order. |
| 37 | uint32_t Address() const; |
| 38 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 39 | // Returns the CIDR representation of this address, for instance |
| 40 | // 192.168.0.34/24. |
| 41 | std::string ToCidrString() const; |
| 42 | |
| 43 | // Returns the IPv4 literal representation of this address, for instance |
| 44 | // 192.168.0.34. |
| 45 | std::string ToIPv4String() const; |
| 46 | |
Garrick Evans | c7ae82c | 2019-09-04 16:25:10 +0900 | [diff] [blame] | 47 | // Returns the subnet etmask in network-byte order. |
| 48 | uint32_t Netmask() const; |
| 49 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 50 | private: |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 51 | // The address in network-byte order. |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 52 | uint32_t addr_; |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 53 | // The prefix length of the address. |
| 54 | uint32_t prefix_length_; |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 55 | |
| 56 | // Callback to run when this object is destroyed. |
Anand K Mistry | e1aee76 | 2021-10-11 12:28:56 +1100 | [diff] [blame] | 57 | base::OnceClosure release_cb_; |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 60 | // Represents an allocated IPv4 subnet. |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 61 | class BRILLO_EXPORT Subnet { |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 62 | public: |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 63 | // Creates a new Subnet with the given base address and prefix length. |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 64 | // |base_addr| must be in network-byte order. |release_cb| runs in the |
| 65 | // destructor of this class and can be used to free other resources associated |
| 66 | // with the subnet. |
Anand K Mistry | e1aee76 | 2021-10-11 12:28:56 +1100 | [diff] [blame] | 67 | Subnet(uint32_t base_addr, |
| 68 | uint32_t prefix_length, |
| 69 | base::OnceClosure release_cb); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 70 | Subnet(const Subnet&) = delete; |
| 71 | Subnet& operator=(const Subnet&) = delete; |
| 72 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 73 | ~Subnet(); |
| 74 | |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 75 | // Marks |addr| as allocated. |addr| must be in network-byte order. Returns |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 76 | // nullptr if |addr| has already been allocated or if |addr| is not contained |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 77 | // within this subnet. Otherwise, the allocated address is automatically |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 78 | // freed when the returned SubnetAddress is destroyed. |
| 79 | std::unique_ptr<SubnetAddress> Allocate(uint32_t addr); |
| 80 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 81 | // Allocates the address at |offset|. Returns nullptr if |offset| is invalid |
| 82 | // (exceeds available IPs in the subnet) or is already allocated. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 83 | // |offset| is relative to the first usable host address; e.g. network + 1 |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 84 | std::unique_ptr<SubnetAddress> AllocateAtOffset(uint32_t offset); |
| 85 | |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 86 | // Returns the address at the given |offset| in network-byte order. Returns |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 87 | // INADDR_ANY if the offset exceeds the available IPs in the subnet. |
| 88 | // Available IPs do not include the network id or the broadcast address. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 89 | // |offset| is relative to the first usable host address; e.g. network + 1 |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 90 | uint32_t AddressAtOffset(uint32_t offset) const; |
| 91 | |
| 92 | // Returns the number of available IPs in this subnet. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 93 | uint32_t AvailableCount() const; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 94 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 95 | // Returns the base address in network-byte order. |
| 96 | uint32_t BaseAddress() const; |
| 97 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 98 | // Returns the netmask in network-byte order. |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 99 | uint32_t Netmask() const; |
| 100 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 101 | // Returns the prefix in network-byte order. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 102 | uint32_t Prefix() const; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 103 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 104 | // Returns the prefix length. |
| 105 | uint32_t PrefixLength() const; |
| 106 | |
| 107 | // Returns the CIDR representation of this subnet, for instance |
| 108 | // 192.168.0.0/24. |
| 109 | std::string ToCidrString() const; |
| 110 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 111 | private: |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 112 | // Marks the address at |offset| as free. |
| 113 | void Free(uint32_t offset); |
| 114 | |
Hugo Benichi | 6c63ae2 | 2019-05-29 11:19:15 +0900 | [diff] [blame] | 115 | // Base address of the subnet, in network-byte order. |
| 116 | uint32_t base_addr_; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 117 | |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 118 | // Prefix length. |
| 119 | uint32_t prefix_length_; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 120 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 121 | // Keeps track of allocated addresses. |
| 122 | std::vector<bool> addrs_; |
| 123 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 124 | // Callback to run when this object is deleted. |
Anand K Mistry | e1aee76 | 2021-10-11 12:28:56 +1100 | [diff] [blame] | 125 | base::OnceClosure release_cb_; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 126 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 127 | base::WeakPtrFactory<Subnet> weak_factory_; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 130 | } // namespace patchpanel |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 131 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 132 | #endif // PATCHPANEL_SUBNET_H_ |