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