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