blob: 712e38bef2b4cab499ac16bb0214b27e49e91498 [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 Evans4b66f632019-01-24 15:09:16 +09005#ifndef ARC_NETWORK_SUBNET_H_
6#define ARC_NETWORK_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>
11#include <vector>
12
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070013#include <base/callback.h>
14#include <base/macros.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 Evans4b66f632019-01-24 15:09:16 +090018namespace arc_networkd {
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070019
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080020// Represents an allocated address inside a subnet. The address is freed when
21// this object is destroyed.
Garrick Evans4b66f632019-01-24 15:09:16 +090022class BRILLO_EXPORT SubnetAddress {
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080023 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 Ekbotebccb4752018-10-31 13:53:08 -070040// Represents an allocated subnet.
Garrick Evans4b66f632019-01-24 15:09:16 +090041class BRILLO_EXPORT Subnet {
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070042 public:
Garrick Evans0dbd4182019-03-07 08:38:38 +090043 // Creates a new Subnet with the given network id and prefix. |release_cb|
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070044 // runs in the destructor of this class and can be used to free other
45 // resources associated with the subnet.
Garrick Evans0dbd4182019-03-07 08:38:38 +090046 Subnet(uint32_t network_id, uint32_t prefix, base::Closure release_cb);
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070047 ~Subnet();
48
Garrick Evans0dbd4182019-03-07 08:38:38 +090049 // Marks |addr| as allocated. |addr| must be in host-byte order. Returns
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080050 // nullptr if |addr| has already been allocated or if |addr| is not contained
Garrick Evans0dbd4182019-03-07 08:38:38 +090051 // within this subnet. Otherwise, the allocated address is automatically
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080052 // freed when the returned SubnetAddress is destroyed.
53 std::unique_ptr<SubnetAddress> Allocate(uint32_t addr);
54
Garrick Evans0dbd4182019-03-07 08:38:38 +090055 // Allocates the address at |offset|. Returns nullptr if |offset| is invalid
56 // (exceeds available IPs in the subnet) or is already allocated.
Garrick Evansf4a93292019-03-13 14:19:43 +090057 // |offset| is relative to the first usable host address; e.g. network + 1
Garrick Evans0dbd4182019-03-07 08:38:38 +090058 std::unique_ptr<SubnetAddress> AllocateAtOffset(uint32_t offset);
59
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070060 // 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 Evansf4a93292019-03-13 14:19:43 +090063 // |offset| is relative to the first usable host address; e.g. network + 1
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070064 uint32_t AddressAtOffset(uint32_t offset) const;
65
66 // Returns the number of available IPs in this subnet.
Garrick Evans0dbd4182019-03-07 08:38:38 +090067 uint32_t AvailableCount() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070068
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080069 // Returns the netmask in network-byte order.
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070070 uint32_t Netmask() const;
71
72 // Returns the prefix.
Garrick Evans0dbd4182019-03-07 08:38:38 +090073 uint32_t Prefix() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070074
75 private:
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080076 // Marks the address at |offset| as free.
77 void Free(uint32_t offset);
78
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070079 // Subnet network id in host byte order.
80 uint32_t network_id_;
81
82 // Prefix.
Garrick Evans0dbd4182019-03-07 08:38:38 +090083 uint32_t prefix_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070084
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080085 // Keeps track of allocated addresses.
86 std::vector<bool> addrs_;
87
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070088 // Callback to run when this object is deleted.
89 base::Closure release_cb_;
90
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080091 base::WeakPtrFactory<Subnet> weak_factory_;
92
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070093 DISALLOW_COPY_AND_ASSIGN(Subnet);
94};
95
Garrick Evans4b66f632019-01-24 15:09:16 +090096} // namespace arc_networkd
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070097
Garrick Evans4b66f632019-01-24 15:09:16 +090098#endif // ARC_NETWORK_SUBNET_H_