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 | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 5 | #ifndef ARC_NETWORK_SUBNET_H_ |
| 6 | #define ARC_NETWORK_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> |
| 11 | #include <vector> |
| 12 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 13 | #include <base/callback.h> |
| 14 | #include <base/macros.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 | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 18 | namespace arc_networkd { |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 19 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 20 | // Represents an allocated address inside a subnet. The address is freed when |
| 21 | // 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: |
| 24 | SubnetAddress(uint32_t addr, base::Closure release_cb); |
| 25 | ~SubnetAddress(); |
| 26 | |
| 27 | // Returns this address in network-byte order. |
| 28 | uint32_t Address() const; |
| 29 | |
| 30 | private: |
| 31 | // The address in host-byte order. |
| 32 | uint32_t addr_; |
| 33 | |
| 34 | // Callback to run when this object is destroyed. |
| 35 | base::Closure release_cb_; |
| 36 | |
| 37 | DISALLOW_COPY_AND_ASSIGN(SubnetAddress); |
| 38 | }; |
| 39 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 40 | // Represents an allocated subnet. |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 41 | class BRILLO_EXPORT Subnet { |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 42 | public: |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 43 | // Creates a new Subnet with the given network id and prefix. |release_cb| |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 44 | // runs in the destructor of this class and can be used to free other |
| 45 | // resources associated with the subnet. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 46 | Subnet(uint32_t network_id, uint32_t prefix, base::Closure release_cb); |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 47 | ~Subnet(); |
| 48 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 49 | // Marks |addr| as allocated. |addr| must be in host-byte order. Returns |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 50 | // 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] | 51 | // within this subnet. Otherwise, the allocated address is automatically |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 52 | // freed when the returned SubnetAddress is destroyed. |
| 53 | std::unique_ptr<SubnetAddress> Allocate(uint32_t addr); |
| 54 | |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 55 | // Allocates the address at |offset|. Returns nullptr if |offset| is invalid |
| 56 | // (exceeds available IPs in the subnet) or is already allocated. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 57 | // |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] | 58 | std::unique_ptr<SubnetAddress> AllocateAtOffset(uint32_t offset); |
| 59 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 60 | // Returns the address at the given |offset| in network byte order. Returns |
| 61 | // INADDR_ANY if the offset exceeds the available IPs in the subnet. |
| 62 | // Available IPs do not include the network id or the broadcast address. |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 63 | // |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] | 64 | uint32_t AddressAtOffset(uint32_t offset) const; |
| 65 | |
| 66 | // Returns the number of available IPs in this subnet. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 67 | uint32_t AvailableCount() const; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 68 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 69 | // Returns the netmask in network-byte order. |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 70 | uint32_t Netmask() const; |
| 71 | |
| 72 | // Returns the prefix. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 73 | uint32_t Prefix() const; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 74 | |
| 75 | private: |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 76 | // Marks the address at |offset| as free. |
| 77 | void Free(uint32_t offset); |
| 78 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 79 | // Subnet network id in host byte order. |
| 80 | uint32_t network_id_; |
| 81 | |
| 82 | // Prefix. |
Garrick Evans | 0dbd418 | 2019-03-07 08:38:38 +0900 | [diff] [blame] | 83 | uint32_t prefix_; |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 84 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 85 | // Keeps track of allocated addresses. |
| 86 | std::vector<bool> addrs_; |
| 87 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 88 | // Callback to run when this object is deleted. |
| 89 | base::Closure release_cb_; |
| 90 | |
Chirantan Ekbote | 817b0c2 | 2018-11-14 16:55:10 -0800 | [diff] [blame] | 91 | base::WeakPtrFactory<Subnet> weak_factory_; |
| 92 | |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 93 | DISALLOW_COPY_AND_ASSIGN(Subnet); |
| 94 | }; |
| 95 | |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 96 | } // namespace arc_networkd |
Chirantan Ekbote | bccb475 | 2018-10-31 13:53:08 -0700 | [diff] [blame] | 97 | |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 98 | #endif // ARC_NETWORK_SUBNET_H_ |