blob: f8988b9904beb79f3dd30f5f34156ac68a130fde [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);
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080032 ~SubnetAddress();
33
34 // Returns this address in network-byte order.
35 uint32_t Address() const;
36
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090037 // Returns the CIDR representation of this address, for instance
38 // 192.168.0.34/24.
39 std::string ToCidrString() const;
40
41 // Returns the IPv4 literal representation of this address, for instance
42 // 192.168.0.34.
43 std::string ToIPv4String() const;
44
Garrick Evansc7ae82c2019-09-04 16:25:10 +090045 // Returns the subnet etmask in network-byte order.
46 uint32_t Netmask() const;
47
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080048 private:
Hugo Benichi6c63ae22019-05-29 11:19:15 +090049 // The address in network-byte order.
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080050 uint32_t addr_;
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090051 // The prefix length of the address.
52 uint32_t prefix_length_;
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080053
54 // Callback to run when this object is destroyed.
55 base::Closure release_cb_;
56
57 DISALLOW_COPY_AND_ASSIGN(SubnetAddress);
58};
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.
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090067 Subnet(uint32_t base_addr, uint32_t prefix_length, base::Closure release_cb);
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070068 ~Subnet();
69
Hugo Benichi6c63ae22019-05-29 11:19:15 +090070 // Marks |addr| as allocated. |addr| must be in network-byte order. Returns
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080071 // nullptr if |addr| has already been allocated or if |addr| is not contained
Garrick Evans0dbd4182019-03-07 08:38:38 +090072 // within this subnet. Otherwise, the allocated address is automatically
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080073 // freed when the returned SubnetAddress is destroyed.
74 std::unique_ptr<SubnetAddress> Allocate(uint32_t addr);
75
Garrick Evans0dbd4182019-03-07 08:38:38 +090076 // Allocates the address at |offset|. Returns nullptr if |offset| is invalid
77 // (exceeds available IPs in the subnet) or is already allocated.
Garrick Evansf4a93292019-03-13 14:19:43 +090078 // |offset| is relative to the first usable host address; e.g. network + 1
Garrick Evans0dbd4182019-03-07 08:38:38 +090079 std::unique_ptr<SubnetAddress> AllocateAtOffset(uint32_t offset);
80
Hugo Benichi6c63ae22019-05-29 11:19:15 +090081 // Returns the address at the given |offset| in network-byte order. Returns
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070082 // INADDR_ANY if the offset exceeds the available IPs in the subnet.
83 // Available IPs do not include the network id or the broadcast address.
Garrick Evansf4a93292019-03-13 14:19:43 +090084 // |offset| is relative to the first usable host address; e.g. network + 1
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070085 uint32_t AddressAtOffset(uint32_t offset) const;
86
87 // Returns the number of available IPs in this subnet.
Garrick Evans0dbd4182019-03-07 08:38:38 +090088 uint32_t AvailableCount() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070089
Garrick Evans47c19272019-11-21 10:58:21 +090090 // Returns the base address in network-byte order.
91 uint32_t BaseAddress() const;
92
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080093 // Returns the netmask in network-byte order.
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070094 uint32_t Netmask() const;
95
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090096 // Returns the prefix in network-byte order.
Garrick Evans0dbd4182019-03-07 08:38:38 +090097 uint32_t Prefix() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070098
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090099 // Returns the prefix length.
100 uint32_t PrefixLength() const;
101
102 // Returns the CIDR representation of this subnet, for instance
103 // 192.168.0.0/24.
104 std::string ToCidrString() const;
105
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700106 private:
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800107 // Marks the address at |offset| as free.
108 void Free(uint32_t offset);
109
Hugo Benichi6c63ae22019-05-29 11:19:15 +0900110 // Base address of the subnet, in network-byte order.
111 uint32_t base_addr_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700112
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900113 // Prefix length.
114 uint32_t prefix_length_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700115
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800116 // Keeps track of allocated addresses.
117 std::vector<bool> addrs_;
118
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700119 // Callback to run when this object is deleted.
120 base::Closure release_cb_;
121
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800122 base::WeakPtrFactory<Subnet> weak_factory_;
123
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700124 DISALLOW_COPY_AND_ASSIGN(Subnet);
125};
126
Garrick Evans3388a032020-03-24 11:25:55 +0900127} // namespace patchpanel
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700128
Garrick Evans3388a032020-03-24 11:25:55 +0900129#endif // PATCHPANEL_SUBNET_H_