blob: c66b6c0dbcc9acadbfb6fb64f7e3f7e3c2d1719d [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>
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080015#include <base/memory/weak_ptr.h>
Garrick Evans4b66f632019-01-24 15:09:16 +090016#include <brillo/brillo_export.h>
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070017
Garrick Evans3388a032020-03-24 11:25:55 +090018namespace patchpanel {
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070019
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090020// Represents an allocated address inside an IPv4 subnet. The address is freed
21// when this object is destroyed.
Garrick Evans4b66f632019-01-24 15:09:16 +090022class BRILLO_EXPORT SubnetAddress {
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080023 public:
Hugo Benichi6c63ae22019-05-29 11:19:15 +090024 // 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 Benichibd8ec4d2019-05-28 12:52:49 +090028 SubnetAddress(uint32_t addr,
29 uint32_t prefix_length,
Anand K Mistrye1aee762021-10-11 12:28:56 +110030 base::OnceClosure release_cb);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090031 SubnetAddress(const SubnetAddress&) = delete;
32 SubnetAddress& operator=(const SubnetAddress&) = delete;
33
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080034 ~SubnetAddress();
35
36 // Returns this address in network-byte order.
37 uint32_t Address() const;
38
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090039 // 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 Evansc7ae82c2019-09-04 16:25:10 +090047 // Returns the subnet etmask in network-byte order.
48 uint32_t Netmask() const;
49
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080050 private:
Hugo Benichi6c63ae22019-05-29 11:19:15 +090051 // The address in network-byte order.
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080052 uint32_t addr_;
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090053 // The prefix length of the address.
54 uint32_t prefix_length_;
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080055
56 // Callback to run when this object is destroyed.
Anand K Mistrye1aee762021-10-11 12:28:56 +110057 base::OnceClosure release_cb_;
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080058};
59
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090060// Represents an allocated IPv4 subnet.
Garrick Evans4b66f632019-01-24 15:09:16 +090061class BRILLO_EXPORT Subnet {
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070062 public:
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090063 // Creates a new Subnet with the given base address and prefix length.
Hugo Benichi6c63ae22019-05-29 11:19:15 +090064 // |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 Mistrye1aee762021-10-11 12:28:56 +110067 Subnet(uint32_t base_addr,
68 uint32_t prefix_length,
69 base::OnceClosure release_cb);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090070 Subnet(const Subnet&) = delete;
71 Subnet& operator=(const Subnet&) = delete;
72
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070073 ~Subnet();
74
Hugo Benichi6c63ae22019-05-29 11:19:15 +090075 // Marks |addr| as allocated. |addr| must be in network-byte order. Returns
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080076 // nullptr if |addr| has already been allocated or if |addr| is not contained
Garrick Evans0dbd4182019-03-07 08:38:38 +090077 // within this subnet. Otherwise, the allocated address is automatically
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080078 // freed when the returned SubnetAddress is destroyed.
79 std::unique_ptr<SubnetAddress> Allocate(uint32_t addr);
80
Garrick Evans0dbd4182019-03-07 08:38:38 +090081 // Allocates the address at |offset|. Returns nullptr if |offset| is invalid
82 // (exceeds available IPs in the subnet) or is already allocated.
Garrick Evansf4a93292019-03-13 14:19:43 +090083 // |offset| is relative to the first usable host address; e.g. network + 1
Garrick Evans0dbd4182019-03-07 08:38:38 +090084 std::unique_ptr<SubnetAddress> AllocateAtOffset(uint32_t offset);
85
Hugo Benichi6c63ae22019-05-29 11:19:15 +090086 // Returns the address at the given |offset| in network-byte order. Returns
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070087 // 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 Evansf4a93292019-03-13 14:19:43 +090089 // |offset| is relative to the first usable host address; e.g. network + 1
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070090 uint32_t AddressAtOffset(uint32_t offset) const;
91
92 // Returns the number of available IPs in this subnet.
Garrick Evans0dbd4182019-03-07 08:38:38 +090093 uint32_t AvailableCount() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070094
Garrick Evans47c19272019-11-21 10:58:21 +090095 // Returns the base address in network-byte order.
96 uint32_t BaseAddress() const;
97
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080098 // Returns the netmask in network-byte order.
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070099 uint32_t Netmask() const;
100
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900101 // Returns the prefix in network-byte order.
Garrick Evans0dbd4182019-03-07 08:38:38 +0900102 uint32_t Prefix() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700103
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900104 // 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 Ekbotebccb4752018-10-31 13:53:08 -0700111 private:
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800112 // Marks the address at |offset| as free.
113 void Free(uint32_t offset);
114
Hugo Benichi6c63ae22019-05-29 11:19:15 +0900115 // Base address of the subnet, in network-byte order.
116 uint32_t base_addr_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700117
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900118 // Prefix length.
119 uint32_t prefix_length_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700120
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800121 // Keeps track of allocated addresses.
122 std::vector<bool> addrs_;
123
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700124 // Callback to run when this object is deleted.
Anand K Mistrye1aee762021-10-11 12:28:56 +1100125 base::OnceClosure release_cb_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700126
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800127 base::WeakPtrFactory<Subnet> weak_factory_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700128};
129
Garrick Evans3388a032020-03-24 11:25:55 +0900130} // namespace patchpanel
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700131
Garrick Evans3388a032020-03-24 11:25:55 +0900132#endif // PATCHPANEL_SUBNET_H_