blob: 733e2daa1e89950cd82d850adaa6f65100313b39 [file] [log] [blame]
Chirantan Ekbotebccb4752018-10-31 13:53:08 -07001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_SUBNET_H_
6#define PATCHPANEL_SUBNET_H_
Chirantan Ekbotebccb4752018-10-31 13:53:08 -07007
8#include <stdint.h>
9
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080010#include <memory>
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090011#include <string>
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080012#include <vector>
13
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070014#include <base/callback.h>
15#include <base/macros.h>
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080016#include <base/memory/weak_ptr.h>
Garrick Evans4b66f632019-01-24 15:09:16 +090017#include <brillo/brillo_export.h>
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070018
Garrick Evans3388a032020-03-24 11:25:55 +090019namespace patchpanel {
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070020
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090021// Represents an allocated address inside an IPv4 subnet. The address is freed
22// when this object is destroyed.
Garrick Evans4b66f632019-01-24 15:09:16 +090023class BRILLO_EXPORT SubnetAddress {
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080024 public:
Hugo Benichi6c63ae22019-05-29 11:19:15 +090025 // 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 Benichibd8ec4d2019-05-28 12:52:49 +090029 SubnetAddress(uint32_t addr,
30 uint32_t prefix_length,
31 base::Closure release_cb);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090032 SubnetAddress(const SubnetAddress&) = delete;
33 SubnetAddress& operator=(const SubnetAddress&) = delete;
34
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080035 ~SubnetAddress();
36
37 // Returns this address in network-byte order.
38 uint32_t Address() const;
39
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090040 // 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 Evansc7ae82c2019-09-04 16:25:10 +090048 // Returns the subnet etmask in network-byte order.
49 uint32_t Netmask() const;
50
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080051 private:
Hugo Benichi6c63ae22019-05-29 11:19:15 +090052 // The address in network-byte order.
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080053 uint32_t addr_;
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090054 // The prefix length of the address.
55 uint32_t prefix_length_;
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080056
57 // Callback to run when this object is destroyed.
58 base::Closure release_cb_;
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080059};
60
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090061// Represents an allocated IPv4 subnet.
Garrick Evans4b66f632019-01-24 15:09:16 +090062class BRILLO_EXPORT Subnet {
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070063 public:
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090064 // Creates a new Subnet with the given base address and prefix length.
Hugo Benichi6c63ae22019-05-29 11:19:15 +090065 // |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 Benichibd8ec4d2019-05-28 12:52:49 +090068 Subnet(uint32_t base_addr, uint32_t prefix_length, base::Closure release_cb);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090069 Subnet(const Subnet&) = delete;
70 Subnet& operator=(const Subnet&) = delete;
71
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070072 ~Subnet();
73
Hugo Benichi6c63ae22019-05-29 11:19:15 +090074 // Marks |addr| as allocated. |addr| must be in network-byte order. Returns
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080075 // nullptr if |addr| has already been allocated or if |addr| is not contained
Garrick Evans0dbd4182019-03-07 08:38:38 +090076 // within this subnet. Otherwise, the allocated address is automatically
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080077 // freed when the returned SubnetAddress is destroyed.
78 std::unique_ptr<SubnetAddress> Allocate(uint32_t addr);
79
Garrick Evans0dbd4182019-03-07 08:38:38 +090080 // Allocates the address at |offset|. Returns nullptr if |offset| is invalid
81 // (exceeds available IPs in the subnet) or is already allocated.
Garrick Evansf4a93292019-03-13 14:19:43 +090082 // |offset| is relative to the first usable host address; e.g. network + 1
Garrick Evans0dbd4182019-03-07 08:38:38 +090083 std::unique_ptr<SubnetAddress> AllocateAtOffset(uint32_t offset);
84
Hugo Benichi6c63ae22019-05-29 11:19:15 +090085 // Returns the address at the given |offset| in network-byte order. Returns
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070086 // 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 Evansf4a93292019-03-13 14:19:43 +090088 // |offset| is relative to the first usable host address; e.g. network + 1
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070089 uint32_t AddressAtOffset(uint32_t offset) const;
90
91 // Returns the number of available IPs in this subnet.
Garrick Evans0dbd4182019-03-07 08:38:38 +090092 uint32_t AvailableCount() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070093
Garrick Evans47c19272019-11-21 10:58:21 +090094 // Returns the base address in network-byte order.
95 uint32_t BaseAddress() const;
96
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080097 // Returns the netmask in network-byte order.
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070098 uint32_t Netmask() const;
99
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900100 // Returns the prefix in network-byte order.
Garrick Evans0dbd4182019-03-07 08:38:38 +0900101 uint32_t Prefix() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700102
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900103 // 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 Ekbotebccb4752018-10-31 13:53:08 -0700110 private:
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800111 // Marks the address at |offset| as free.
112 void Free(uint32_t offset);
113
Hugo Benichi6c63ae22019-05-29 11:19:15 +0900114 // Base address of the subnet, in network-byte order.
115 uint32_t base_addr_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700116
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900117 // Prefix length.
118 uint32_t prefix_length_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700119
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800120 // Keeps track of allocated addresses.
121 std::vector<bool> addrs_;
122
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700123 // Callback to run when this object is deleted.
124 base::Closure release_cb_;
125
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800126 base::WeakPtrFactory<Subnet> weak_factory_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700127};
128
Garrick Evans3388a032020-03-24 11:25:55 +0900129} // namespace patchpanel
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700130
Garrick Evans3388a032020-03-24 11:25:55 +0900131#endif // PATCHPANEL_SUBNET_H_